Skip to content

Commit be45667

Browse files
theihorKernel Patches Daemon
authored andcommitted
bpf: implement bpf_wq_set_callback kfunc with implicit prog_aux
Add bpf_wq_set_callback BPF kfunc with KF_IMPLICIT_PROG_AUX_ARG, corresponding to bpf_wq_set_callback_impl kfunc. Teach the verifier about it. To be handled correctly, BTF for this kfunc must be generated with pahole version that supports KF_IMPLICIT_PROG_AUX_ARG. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent 4d251a0 commit be45667

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

kernel/bpf/helpers.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,12 +3065,11 @@ __bpf_kfunc int bpf_wq_start(struct bpf_wq *wq, unsigned int flags)
30653065
return 0;
30663066
}
30673067

3068-
__bpf_kfunc int bpf_wq_set_callback_impl(struct bpf_wq *wq,
3069-
int (callback_fn)(void *map, int *key, void *value),
3070-
unsigned int flags,
3071-
void *aux__prog)
3068+
__bpf_kfunc int bpf_wq_set_callback(struct bpf_wq *wq,
3069+
int (callback_fn)(void *map, int *key, void *value),
3070+
unsigned int flags,
3071+
struct bpf_prog_aux *aux)
30723072
{
3073-
struct bpf_prog_aux *aux = (struct bpf_prog_aux *)aux__prog;
30743073
struct bpf_async_kern *async = (struct bpf_async_kern *)wq;
30753074

30763075
if (flags)
@@ -3079,6 +3078,14 @@ __bpf_kfunc int bpf_wq_set_callback_impl(struct bpf_wq *wq,
30793078
return __bpf_async_set_callback(async, callback_fn, aux, flags, BPF_ASYNC_TYPE_WQ);
30803079
}
30813080

3081+
__bpf_kfunc int bpf_wq_set_callback_impl(struct bpf_wq *wq,
3082+
int (callback_fn)(void *map, int *key, void *value),
3083+
unsigned int flags,
3084+
void *aux__prog)
3085+
{
3086+
return bpf_wq_set_callback(wq, callback_fn, flags, aux__prog);
3087+
}
3088+
30823089
__bpf_kfunc void bpf_preempt_disable(void)
30833090
{
30843091
preempt_disable();
@@ -4374,6 +4381,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
43744381
BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS)
43754382
BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_TRUSTED_ARGS)
43764383
BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_TRUSTED_ARGS)
4384+
BTF_ID_FLAGS(func, bpf_wq_set_callback, KF_IMPLICIT_PROG_AUX_ARG)
43774385
BTF_KFUNCS_END(common_btf_ids)
43784386

43794387
static const struct btf_kfunc_id_set common_kfunc_set = {

kernel/bpf/verifier.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ static bool is_async_callback_calling_kfunc(u32 btf_id);
514514
static bool is_callback_calling_kfunc(u32 btf_id);
515515
static bool is_bpf_throw_kfunc(struct bpf_insn *insn);
516516

517-
static bool is_bpf_wq_set_callback_impl_kfunc(u32 btf_id);
517+
static bool is_bpf_wq_set_callback_kfunc(u32 btf_id);
518518

519519
static bool is_sync_callback_calling_function(enum bpf_func_id func_id)
520520
{
@@ -10586,7 +10586,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
1058610586
env->subprog_info[subprog].is_async_cb = true;
1058710587
async_cb = push_async_cb(env, env->subprog_info[subprog].start,
1058810588
insn_idx, subprog,
10589-
is_bpf_wq_set_callback_impl_kfunc(insn->imm) ||
10589+
is_bpf_wq_set_callback_kfunc(insn->imm) ||
1059010590
is_task_work_add_kfunc(insn->imm));
1059110591
if (!async_cb)
1059210592
return -EFAULT;
@@ -12278,6 +12278,7 @@ enum special_kfunc_type {
1227812278
KF___bpf_trap,
1227912279
KF_bpf_task_work_schedule_signal,
1228012280
KF_bpf_task_work_schedule_resume,
12281+
KF_bpf_wq_set_callback,
1228112282
};
1228212283

1228312284
BTF_ID_LIST(special_kfunc_list)
@@ -12350,6 +12351,7 @@ BTF_ID(func, bpf_res_spin_unlock_irqrestore)
1235012351
BTF_ID(func, __bpf_trap)
1235112352
BTF_ID(func, bpf_task_work_schedule_signal)
1235212353
BTF_ID(func, bpf_task_work_schedule_resume)
12354+
BTF_ID(func, bpf_wq_set_callback)
1235312355

1235412356
static bool is_task_work_add_kfunc(u32 func_id)
1235512357
{
@@ -12797,7 +12799,7 @@ static bool is_sync_callback_calling_kfunc(u32 btf_id)
1279712799

1279812800
static bool is_async_callback_calling_kfunc(u32 btf_id)
1279912801
{
12800-
return btf_id == special_kfunc_list[KF_bpf_wq_set_callback_impl] ||
12802+
return is_bpf_wq_set_callback_kfunc(btf_id) ||
1280112803
is_task_work_add_kfunc(btf_id);
1280212804
}
1280312805

@@ -12807,9 +12809,10 @@ static bool is_bpf_throw_kfunc(struct bpf_insn *insn)
1280712809
insn->imm == special_kfunc_list[KF_bpf_throw];
1280812810
}
1280912811

12810-
static bool is_bpf_wq_set_callback_impl_kfunc(u32 btf_id)
12812+
static bool is_bpf_wq_set_callback_kfunc(u32 btf_id)
1281112813
{
12812-
return btf_id == special_kfunc_list[KF_bpf_wq_set_callback_impl];
12814+
return btf_id == special_kfunc_list[KF_bpf_wq_set_callback_impl] ||
12815+
btf_id == special_kfunc_list[KF_bpf_wq_set_callback];
1281312816
}
1281412817

1281512818
static bool is_callback_calling_kfunc(u32 btf_id)
@@ -13910,7 +13913,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1391013913
meta.r0_rdonly = false;
1391113914
}
1391213915

13913-
if (is_bpf_wq_set_callback_impl_kfunc(meta.func_id)) {
13916+
if (is_bpf_wq_set_callback_kfunc(meta.func_id)) {
1391413917
err = push_callback_call(env, insn, insn_idx, meta.subprogno,
1391513918
set_timer_callback_state);
1391613919
if (err) {

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)