Skip to content

Commit 19bd300

Browse files
mykyta5anakryiko
authored andcommitted
bpf: Add verifier support for bpf_timer argument in kfuncs
Extend the verifier to recognize struct bpf_timer as a valid kfunc argument type. Previously, bpf_timer was only supported in BPF helpers. This prepares for adding timer-related kfuncs in subsequent patches. Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20260201025403.66625-3-alexei.starovoitov@gmail.com
1 parent 1bfbc26 commit 19bd300

1 file changed

Lines changed: 37 additions & 18 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8675,13 +8675,25 @@ static int check_map_field_pointer(struct bpf_verifier_env *env, u32 regno,
86758675
}
86768676

86778677
static int process_timer_func(struct bpf_verifier_env *env, int regno,
8678-
struct bpf_call_arg_meta *meta)
8678+
struct bpf_map_desc *map)
86798679
{
86808680
if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
86818681
verbose(env, "bpf_timer cannot be used for PREEMPT_RT.\n");
86828682
return -EOPNOTSUPP;
86838683
}
8684-
return check_map_field_pointer(env, regno, BPF_TIMER, &meta->map);
8684+
return check_map_field_pointer(env, regno, BPF_TIMER, map);
8685+
}
8686+
8687+
static int process_timer_helper(struct bpf_verifier_env *env, int regno,
8688+
struct bpf_call_arg_meta *meta)
8689+
{
8690+
return process_timer_func(env, regno, &meta->map);
8691+
}
8692+
8693+
static int process_timer_kfunc(struct bpf_verifier_env *env, int regno,
8694+
struct bpf_kfunc_call_arg_meta *meta)
8695+
{
8696+
return process_timer_func(env, regno, &meta->map);
86858697
}
86868698

86878699
static int process_kptr_func(struct bpf_verifier_env *env, int regno,
@@ -9973,7 +9985,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
99739985
}
99749986
break;
99759987
case ARG_PTR_TO_TIMER:
9976-
err = process_timer_func(env, regno, meta);
9988+
err = process_timer_helper(env, regno, meta);
99779989
if (err)
99789990
return err;
99799991
break;
@@ -12238,7 +12250,8 @@ enum {
1223812250
KF_ARG_WORKQUEUE_ID,
1223912251
KF_ARG_RES_SPIN_LOCK_ID,
1224012252
KF_ARG_TASK_WORK_ID,
12241-
KF_ARG_PROG_AUX_ID
12253+
KF_ARG_PROG_AUX_ID,
12254+
KF_ARG_TIMER_ID
1224212255
};
1224312256

1224412257
BTF_ID_LIST(kf_arg_btf_ids)
@@ -12251,6 +12264,7 @@ BTF_ID(struct, bpf_wq)
1225112264
BTF_ID(struct, bpf_res_spin_lock)
1225212265
BTF_ID(struct, bpf_task_work)
1225312266
BTF_ID(struct, bpf_prog_aux)
12267+
BTF_ID(struct, bpf_timer)
1225412268

1225512269
static bool __is_kfunc_ptr_arg_type(const struct btf *btf,
1225612270
const struct btf_param *arg, int type)
@@ -12294,6 +12308,11 @@ static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_par
1229412308
return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RB_NODE_ID);
1229512309
}
1229612310

12311+
static bool is_kfunc_arg_timer(const struct btf *btf, const struct btf_param *arg)
12312+
{
12313+
return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_TIMER_ID);
12314+
}
12315+
1229712316
static bool is_kfunc_arg_wq(const struct btf *btf, const struct btf_param *arg)
1229812317
{
1229912318
return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_WORKQUEUE_ID);
@@ -12393,6 +12412,7 @@ enum kfunc_ptr_arg_type {
1239312412
KF_ARG_PTR_TO_NULL,
1239412413
KF_ARG_PTR_TO_CONST_STR,
1239512414
KF_ARG_PTR_TO_MAP,
12415+
KF_ARG_PTR_TO_TIMER,
1239612416
KF_ARG_PTR_TO_WORKQUEUE,
1239712417
KF_ARG_PTR_TO_IRQ_FLAG,
1239812418
KF_ARG_PTR_TO_RES_SPIN_LOCK,
@@ -12646,6 +12666,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1264612666
if (is_kfunc_arg_wq(meta->btf, &args[argno]))
1264712667
return KF_ARG_PTR_TO_WORKQUEUE;
1264812668

12669+
if (is_kfunc_arg_timer(meta->btf, &args[argno]))
12670+
return KF_ARG_PTR_TO_TIMER;
12671+
1264912672
if (is_kfunc_arg_task_work(meta->btf, &args[argno]))
1265012673
return KF_ARG_PTR_TO_TASK_WORK;
1265112674

@@ -13439,6 +13462,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1343913462
case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
1344013463
case KF_ARG_PTR_TO_CONST_STR:
1344113464
case KF_ARG_PTR_TO_WORKQUEUE:
13465+
case KF_ARG_PTR_TO_TIMER:
1344213466
case KF_ARG_PTR_TO_TASK_WORK:
1344313467
case KF_ARG_PTR_TO_IRQ_FLAG:
1344413468
case KF_ARG_PTR_TO_RES_SPIN_LOCK:
@@ -13738,6 +13762,15 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1373813762
if (ret < 0)
1373913763
return ret;
1374013764
break;
13765+
case KF_ARG_PTR_TO_TIMER:
13766+
if (reg->type != PTR_TO_MAP_VALUE) {
13767+
verbose(env, "arg#%d doesn't point to a map value\n", i);
13768+
return -EINVAL;
13769+
}
13770+
ret = process_timer_kfunc(env, regno, meta);
13771+
if (ret < 0)
13772+
return ret;
13773+
break;
1374113774
case KF_ARG_PTR_TO_TASK_WORK:
1374213775
if (reg->type != PTR_TO_MAP_VALUE) {
1374313776
verbose(env, "arg#%d doesn't point to a map value\n", i);
@@ -21429,20 +21462,6 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,
2142921462
}
2143021463
}
2143121464

21432-
if (btf_record_has_field(map->record, BPF_TIMER)) {
21433-
if (is_tracing_prog_type(prog_type)) {
21434-
verbose(env, "tracing progs cannot use bpf_timer yet\n");
21435-
return -EINVAL;
21436-
}
21437-
}
21438-
21439-
if (btf_record_has_field(map->record, BPF_WORKQUEUE)) {
21440-
if (is_tracing_prog_type(prog_type)) {
21441-
verbose(env, "tracing progs cannot use bpf_wq yet\n");
21442-
return -EINVAL;
21443-
}
21444-
}
21445-
2144621465
if ((bpf_prog_is_offloaded(prog->aux) || bpf_map_is_offloaded(map)) &&
2144721466
!bpf_offload_prog_map_match(prog, map)) {
2144821467
verbose(env, "offload device mismatch between prog and map\n");

0 commit comments

Comments
 (0)