Skip to content

Commit 1377641

Browse files
theihorKernel Patches Daemon
authored andcommitted
bpf: Re-define bpf_wq_set_callback as magic kfunc
* Rename bpf_wq_set_callback_impl to bpf_wq_set_callback * void *aux__prog => struct bpf_prog_aux *aux__magic * Set KF_MAGIC_ARGS kfunc flag * Add bpf_wq_set_callback and _impl to magic_kfuncs BTF_ID_LIST * Update special kfunc checks in the verifier to accept both _impl and non-_impl BTF ids In the selftests, a bpf_wq_set_callback_impl() call is intentionally introduced to verify that both signatures are handled correctly. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent c49dd56 commit 1377641

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

kernel/bpf/helpers.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,18 +3119,17 @@ __bpf_kfunc int bpf_wq_start(struct bpf_wq *wq, unsigned int flags)
31193119
return 0;
31203120
}
31213121

3122-
__bpf_kfunc int bpf_wq_set_callback_impl(struct bpf_wq *wq,
3123-
int (callback_fn)(void *map, int *key, void *value),
3124-
unsigned int flags,
3125-
void *aux__prog)
3122+
__bpf_kfunc int bpf_wq_set_callback(struct bpf_wq *wq,
3123+
int (callback_fn)(void *map, int *key, void *value),
3124+
unsigned int flags,
3125+
struct bpf_prog_aux *aux__magic)
31263126
{
3127-
struct bpf_prog_aux *aux = (struct bpf_prog_aux *)aux__prog;
31283127
struct bpf_async_kern *async = (struct bpf_async_kern *)wq;
31293128

31303129
if (flags)
31313130
return -EINVAL;
31323131

3133-
return __bpf_async_set_callback(async, callback_fn, aux, flags, BPF_ASYNC_TYPE_WQ);
3132+
return __bpf_async_set_callback(async, callback_fn, aux__magic, flags, BPF_ASYNC_TYPE_WQ);
31343133
}
31353134

31363135
__bpf_kfunc void bpf_preempt_disable(void)
@@ -4483,7 +4482,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_memset)
44834482
BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
44844483
#endif
44854484
BTF_ID_FLAGS(func, bpf_wq_init)
4486-
BTF_ID_FLAGS(func, bpf_wq_set_callback_impl)
4485+
BTF_ID_FLAGS(func, bpf_wq_set_callback, KF_MAGIC_ARGS)
44874486
BTF_ID_FLAGS(func, bpf_wq_start)
44884487
BTF_ID_FLAGS(func, bpf_preempt_disable)
44894488
BTF_ID_FLAGS(func, bpf_preempt_enable)

kernel/bpf/verifier.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static bool is_async_callback_calling_kfunc(u32 btf_id);
512512
static bool is_callback_calling_kfunc(u32 btf_id);
513513
static bool is_bpf_throw_kfunc(struct bpf_insn *insn);
514514

515-
static bool is_bpf_wq_set_callback_impl_kfunc(u32 btf_id);
515+
static bool is_bpf_wq_set_callback_kfunc(u32 btf_id);
516516
static bool is_task_work_add_kfunc(u32 func_id);
517517

518518
static bool is_sync_callback_calling_function(enum bpf_func_id func_id)
@@ -554,7 +554,7 @@ static bool is_async_cb_sleepable(struct bpf_verifier_env *env, struct bpf_insn
554554

555555
/* bpf_wq and bpf_task_work callbacks are always sleepable. */
556556
if (bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
557-
(is_bpf_wq_set_callback_impl_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm)))
557+
(is_bpf_wq_set_callback_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm)))
558558
return true;
559559

560560
verifier_bug(env, "unhandled async callback in is_async_cb_sleepable");
@@ -3267,7 +3267,8 @@ static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset)
32673267
* magic_kfuncs is used as a list of (foo, foo_impl) pairs
32683268
*/
32693269
BTF_ID_LIST(magic_kfuncs)
3270-
BTF_ID_UNUSED
3270+
BTF_ID(func, bpf_wq_set_callback)
3271+
BTF_ID(func, bpf_wq_set_callback_impl)
32713272
BTF_ID_LIST_END(magic_kfuncs)
32723273

32733274
static s32 magic_kfunc_by_impl(s32 impl_func_id)
@@ -12385,6 +12386,7 @@ enum special_kfunc_type {
1238512386
KF___bpf_trap,
1238612387
KF_bpf_task_work_schedule_signal,
1238712388
KF_bpf_task_work_schedule_resume,
12389+
KF_bpf_wq_set_callback,
1238812390
};
1238912391

1239012392
BTF_ID_LIST(special_kfunc_list)
@@ -12459,6 +12461,7 @@ BTF_ID(func, bpf_dynptr_file_discard)
1245912461
BTF_ID(func, __bpf_trap)
1246012462
BTF_ID(func, bpf_task_work_schedule_signal)
1246112463
BTF_ID(func, bpf_task_work_schedule_resume)
12464+
BTF_ID(func, bpf_wq_set_callback)
1246212465

1246312466
static bool is_task_work_add_kfunc(u32 func_id)
1246412467
{
@@ -12906,7 +12909,7 @@ static bool is_sync_callback_calling_kfunc(u32 btf_id)
1290612909

1290712910
static bool is_async_callback_calling_kfunc(u32 btf_id)
1290812911
{
12909-
return btf_id == special_kfunc_list[KF_bpf_wq_set_callback_impl] ||
12912+
return is_bpf_wq_set_callback_kfunc(btf_id) ||
1291012913
is_task_work_add_kfunc(btf_id);
1291112914
}
1291212915

@@ -12916,9 +12919,10 @@ static bool is_bpf_throw_kfunc(struct bpf_insn *insn)
1291612919
insn->imm == special_kfunc_list[KF_bpf_throw];
1291712920
}
1291812921

12919-
static bool is_bpf_wq_set_callback_impl_kfunc(u32 btf_id)
12922+
static bool is_bpf_wq_set_callback_kfunc(u32 btf_id)
1292012923
{
12921-
return btf_id == special_kfunc_list[KF_bpf_wq_set_callback_impl];
12924+
return btf_id == special_kfunc_list[KF_bpf_wq_set_callback_impl] ||
12925+
btf_id == special_kfunc_list[KF_bpf_wq_set_callback];
1292212926
}
1292312927

1292412928
static bool is_callback_calling_kfunc(u32 btf_id)
@@ -14035,7 +14039,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1403514039
meta.r0_rdonly = false;
1403614040
}
1403714041

14038-
if (is_bpf_wq_set_callback_impl_kfunc(meta.func_id)) {
14042+
if (is_bpf_wq_set_callback_kfunc(meta.func_id)) {
1403914043
err = push_callback_call(env, insn, insn_idx, meta.subprogno,
1404014044
set_timer_callback_state);
1404114045
if (err) {

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,6 @@ extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
580580

581581
extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
582582
extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
583-
extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
584-
int (callback_fn)(void *map, int *key, void *value),
585-
unsigned int flags__k, void *aux__ign) __ksym;
586-
#define bpf_wq_set_callback(timer, cb, flags) \
587-
bpf_wq_set_callback_impl(timer, cb, flags, NULL)
588583

589584
struct bpf_iter_kmem_cache;
590585
extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym;

tools/testing/selftests/bpf/progs/wq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int test_hmap_elem_callback(void *map, int *key,
107107
if (bpf_wq_init(wq, map, 0) != 0)
108108
return -3;
109109

110-
if (bpf_wq_set_callback(wq, callback_fn, 0))
110+
if (bpf_wq_set_callback_impl(wq, callback_fn, 0, NULL))
111111
return -4;
112112

113113
if (bpf_wq_start(wq, 0))

tools/testing/selftests/bpf/progs/wq_failures.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ __failure
9797
/* check that the first argument of bpf_wq_set_callback()
9898
* is a correct bpf_wq pointer.
9999
*/
100-
__msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */
100+
__msg(": (85) call bpf_wq_set_callback#") /* anchor message */
101101
__msg("arg#0 doesn't point to a map value")
102102
long test_wrong_wq_pointer(void *ctx)
103103
{
@@ -123,7 +123,7 @@ __failure
123123
/* check that the first argument of bpf_wq_set_callback()
124124
* is a correct bpf_wq pointer.
125125
*/
126-
__msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */
126+
__msg(": (85) call bpf_wq_set_callback#") /* anchor message */
127127
__msg("off 1 doesn't point to 'struct bpf_wq' that is at 0")
128128
long test_wrong_wq_pointer_offset(void *ctx)
129129
{

0 commit comments

Comments
 (0)