sleepable bpf_timer (was: allow HID-BPF to do device IOs)
Subsystems (5)
v6 cover sleepable bpf_timer (was: allow HID-BPF to do device IOs)
New version of the sleepable bpf_timer code.
I'm posting this as this is the result of the previous review, so we can have a baseline to compare to.
The plan is now to introduce a new user API struct bpf_wq, as the timer API working on softIRQ seems to be quite far away from a wq.
For reference, the use cases I have in mind:
---
Basically, I need to be able to defer a HID-BPF program for the following reasons (from the aforementioned patch):
1. defer an event:
Sometimes we receive an out of proximity event, but the device can not be trusted enough, and we need to ensure that we won't receive another one in the following n milliseconds. So we need to wait those n milliseconds, and eventually re-inject that event in the stack.
2. inject new events in reaction to one given event:
We might want to transform one given event into several. This is the case for macro keys where a single key press is supposed to send a sequence of key presses. But this could also be used to patch a faulty behavior, if a device forgets to send a release event.
3. communicate with the device in reaction to one event:
We might want to communicate back to the device after a given event. For example a device might send us an event saying that it came back from sleeping state and needs to be re-initialized.
Currently we can achieve that by keeping a userspace program around, raise a bpf event, and let that userspace program inject the events and commands.
However, we are just keeping that program alive as a daemon for just scheduling commands. There is no logic in it, so it doesn't really justify an actual userspace wakeup. So a kernel workqueue seems simpler to handle.
bpf_timers are currently running in a soft IRQ context, this patch series implements a sleppable context for them.
Cheers,
Benjamin
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: John Fastabend <[email protected]>
To: KP Singh <[email protected]>
To: Stanislav Fomichev <[email protected]>
To: Hao Luo <[email protected]>
To: Jiri Olsa <[email protected]>
To: Mykola Lysenko <[email protected]>
To: Shuah Khan <[email protected]>
Cc: Benjamin Tissoires <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
---
Changes in v6:
- Use of a workqueue to clean up sleepable timers
- integrated Kumar's patch instead of mine
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- took various reviews into account
- rewrote the tests to be separated to not have a uggly include
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- dropped the HID changes, they can go independently from bpf-core
- addressed Alexei's and Eduard's remarks
- added selftests
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- fixed the crash from v2
- changed the API to have only BPF_F_TIMER_SLEEPABLE for
bpf_timer_start()
- split the new kfuncs/verifier patch into several sub-patches, for
easier reviews
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- make use of bpf_timer (and dropped the custom HID handling)
- implemented bpf_timer_set_sleepable_cb as a kfunc
- still not implemented global subprogs
- no sleepable bpf_timer selftests yet
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Benjamin Tissoires (5):
bpf/helpers: introduce sleepable bpf_timers
bpf/helpers: introduce bpf_timer_set_sleepable_cb() kfunc bpf/helpers: mark the callback of bpf_timer_set_sleepable_cb() as sleepable
tools: sync include/uapi/linux/bpf.h
selftests/bpf: add sleepable timer tests
Kumar Kartikeya Dwivedi (1):
bpf: Add support for KF_ARG_PTR_TO_TIMER
include/linux/bpf_verifier.h | 1 + include/uapi/linux/bpf.h | 13 ++ kernel/bpf/helpers.c | 202 ++++++++++++++++--- kernel/bpf/verifier.c | 98 +++++++++- tools/include/uapi/linux/bpf.h | 20 +- tools/testing/selftests/bpf/bpf_experimental.h | 5 + .../selftests/bpf/bpf_testmod/bpf_testmod.c | 5 + .../selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + tools/testing/selftests/bpf/prog_tests/timer.c | 34 ++++ .../testing/selftests/bpf/progs/timer_sleepable.c | 213 +++++++++++++++++++++ 10 files changed, 553 insertions(+), 39 deletions(-)
---
base-commit: 61df575632d6b39213f47810c441bddbd87c3606
change-id: 20240205-hid-bpf-sleepable-c01260fd91c4
Best regards,
Signature · 1 line
Benjamin Tissoires <[email protected]>
RFC PATCH v6 1/6 bpf/helpers: introduce sleepable bpf_timers
They are implemented as a workqueue, which means that there are no guarantees of timing nor ordering.
---
changes in v6:
- dropped extra spinlock
- implement cancel_and_free of a sleepable timer through a dedicated workqueue
no changes in v5
changes in v4:
- dropped __bpf_timer_compute_key()
- use a spin_lock instead of a semaphore
- ensure bpf_timer_cancel_and_free is not complaining about non sleepable context and use cancel_work() instead of cancel_work_sync()
- return -EINVAL if a delay is given to bpf_timer_start() with BPF_F_TIMER_SLEEPABLE
changes in v3:
- extracted the implementation in bpf_timer only, without bpf_timer_set_sleepable_cb()
- rely on schedule_work() only, from bpf_timer_start()
- add semaphore to ensure bpf_timer_work_cb() is accessing consistent data
changes in v2 (compared to the one attaches to v1 0/9):
- make use of a kfunc
- add a (non-used) BPF_F_TIMER_SLEEPABLE
- the callback is *not* called, it makes the kernel crashes
--- include/uapi/linux/bpf.h | 13 ++++ kernel/bpf/helpers.c | 159 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 143 insertions(+), 29 deletions(-)
include/uapi/linux/bpf.h +13 -0
@@ -7469,6 +7469,19 @@ enum { BPF_F_TIMER_CPU_PIN = (1ULL << 1), }; +/* Extra flags to control bpf_timer_init() behaviour, in addition to CLOCK_*. +* - BPF_F_TIMER_SLEEPABLE: Timer will run in a workqueue in a sleepable +* context, with no guarantees of ordering nor timing (consider this as +* being just offloaded immediately). +*/ +enum { +/* CLOCK_* are using bits 0 to 3 */ +BPF_F_TIMER_SLEEPABLE = (1ULL <<4 ), + __MAX_BPF_F_TIMER_INIT, +}; + +#define MAX_BPF_F_TIMER_INIT __MAX_BPF_F_TIMER_INIT +/* BPF numbers iterator state */ struct bpf_iter_num {/* opaque iterator state; having __u64 here allows to preserve correct
kernel/bpf/helpers.c +130 -29
245 lines not on this page · raw message
Signature · 1 line
2.44.0
Eduard Zingerman
maintainerRe: [PATCH RFC bpf-next v6 1/6] bpf/helpers: introduce sleepable bpf_timers
On Mon, 2024-04-08 at 10:09 +0200, Benjamin Tissoires wrote:
[...]
Quoted 11 lines
kernel/bpf/helpers.c +2
-1@@ -1096,12 +1096,19 @@ const struct bpf_func_proto bpf_snprintf_proto = {* freeing the timers when inner map is replaced or deleted by user space. */ struct bpf_hrtimer { -struct hrtimer timer; + union { + struct hrtimer timer;
kernel/bpf/helpers.c const struct bpf_func_proto bpf_snprintf_proto = { · line 1109
Quoted 10 lines
kernel/bpf/helpers.c +5
-1+struct work_struct work; + };struct bpf_map *map;struct bpf_prog *prog;void __rcu *callback_fn;void *value; -struct rcu_head rcu; +union { +struct rcu_head rcu; +struct work_struct sync_work;
Nit:
I find this name very confusing, the field is used to cancel timer execution, is it a convention to call such things '...sync...'?
Quoted 4 lines
kernel/bpf/helpers.c +2
-0+ }; +u64 flags; };
[...]
kernel/bpf/helpers.c struct bpf_timer_kern { · line 1173
Quoted 7 lines
kernel/bpf/helpers.c +7
-0+static void bpf_timer_sync_work_cb (struct work_struct *work) +{ + struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, sync_work); + +cancel_work_sync (&t->work); + +kfree_rcu (t, rcu);
Sorry, I might be wrong, but this looks suspicious. The 'rcu' field of 'bpf_hrtimer' is defined as follows:
struct bpf_hrtimer {
...
union {
struct rcu_head rcu;
struct work_struct sync_work;
};
...
};And for sleepable timers the 'sync_work' field is set as follows:
BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map,
u64, flags)
{
...
INIT_WORK(&t->sync_work, bpf_timer_sync_work_cb);
...
}So, it looks like 'kfree_rcu' would be called for a non-rcu pointer.
Quoted 2 lines
kernel/bpf/helpers.c +2
-0+} +
Re: [PATCH RFC bpf-next v6 1/6] bpf/helpers: introduce sleepable bpf_timers
Quoted 61 lines · Eduard Zingerman
On Mon, 2024-04-08 at 10:09 +0200, Benjamin Tissoires wrote:
[...]
Quoted 11 lines
kernel/bpf/helpers.c +2
-1@@ -1096,12 +1096,19 @@ const struct bpf_func_proto bpf_snprintf_proto = {* freeing the timers when inner map is replaced or deleted by user space. */ struct bpf_hrtimer { -struct hrtimer timer; +union { +struct hrtimer timer;kernel/bpf/helpers.c const struct bpf_func_proto bpf_snprintf_proto = { · line 1109
Quoted 10 lines
kernel/bpf/helpers.c +5
-1+struct work_struct work; + };struct bpf_map *map;struct bpf_prog *prog;void __rcu *callback_fn;void *value; -struct rcu_head rcu; +union { +struct rcu_head rcu; +struct work_struct sync_work;Nit:
I find this name very confusing, the field is used to cancel timer execution, is it a convention to call such things '...sync...'?Quoted 4 lines
kernel/bpf/helpers.c +2
-0+ }; +u64 flags; };[...]
kernel/bpf/helpers.c struct bpf_timer_kern { · line 1173
Quoted 7 lines
kernel/bpf/helpers.c +7
-0+static void bpf_timer_sync_work_cb (struct work_struct *work) +{ + struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, sync_work); + +cancel_work_sync (&t->work); + +kfree_rcu (t, rcu);Sorry, I might be wrong, but this looks suspicious. The 'rcu' field of '
bpf_hrtimer' is defined as follows:struct bpf_hrtimer { ... union { struct rcu_head rcu; struct work_struct sync_work; }; ... };And for sleepable timers the '
sync_work' field is set as follows:BPF_CALL_3(bpf_timer_init, struct
bpf_timer_kern*, timer, structbpf_map*, map,
Quoted 8 lines
u64, flags) { ... INIT_WORK(&t->sync_work, bpf_timer_sync_work_cb); ... }So, it looks like '
kfree_rcu' would be called for a non-rcu pointer.
That was my initial assumption too, but Alexei told me it was fine. And I think he is correct because kfree_rcu doesn't need the rcu_head to be initialized.
So in the end, we initialize the memory as a work_struct, and when that work kicks in, we reuse that exact same memory as the rcu_head. This is fine because that work will never be reused.
If I understand correctly, this is to save a few bytes as this is a critical struct used in programs with a high rate usage, and every byte counts.
Cheers,
Benjamin
Quoted 5 lines
Quoted 3 lines
kernel/bpf/helpers.c +2
-0+} +
Eduard Zingerman
maintainerRe: [PATCH RFC bpf-next v6 1/6] bpf/helpers: introduce sleepable bpf_timers
On Mon, 2024-04-08 at 19:20 +0200, Benjamin Tissoires wrote:
[...]
Quoted 7 lines
That was my initial assumption too, but Alexei told me it was fine. And I think he is correct because
kfree_rcudoesn't need thercu_headto be initialized.So in the end, we initialize the memory as a
work_struct, and when that work kicks in, we reuse that exact same memory as thercu_head. This is fine because that work will never be reused.
Oh, I get it, thank you for explanation.
Thanks,
Eduard
Re: [PATCH RFC bpf-next v6 1/6] bpf/helpers: introduce sleepable bpf_timers
In reply to Benjamin TissoiresQuoted 76 lines · Benjamin Tissoires
Quoted 61 lines · Eduard Zingerman
On Mon, 2024-04-08 at 10:09 +0200, Benjamin Tissoires wrote:
[...]
Quoted 11 lines
kernel/bpf/helpers.c +2
-1@@ -1096,12 +1096,19 @@ const struct bpf_func_proto bpf_snprintf_proto = {* freeing the timers when inner map is replaced or deleted by user space. */ struct bpf_hrtimer { -struct hrtimer timer; +union { +struct hrtimer timer;kernel/bpf/helpers.c const struct bpf_func_proto bpf_snprintf_proto = { · line 1109
Quoted 10 lines
kernel/bpf/helpers.c +5
-1+struct work_struct work; + };struct bpf_map *map;struct bpf_prog *prog;void __rcu *callback_fn;void *value; -struct rcu_head rcu; +union { +struct rcu_head rcu; +struct work_struct sync_work;Nit:
I find this name very confusing, the field is used to cancel timer execution, is it a convention to call such things '...sync...'?Quoted 4 lines
kernel/bpf/helpers.c +2
-0+ }; +u64 flags; };[...]
kernel/bpf/helpers.c struct bpf_timer_kern { · line 1173
Quoted 7 lines
kernel/bpf/helpers.c +7
-0+static void bpf_timer_sync_work_cb (struct work_struct *work) +{ + struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, sync_work); + +cancel_work_sync (&t->work); + +kfree_rcu (t, rcu);Sorry, I might be wrong, but this looks suspicious. The 'rcu' field of '
bpf_hrtimer' is defined as follows:struct bpf_hrtimer { ... union { struct rcu_head rcu; struct work_struct sync_work; }; ... };And for sleepable timers the '
sync_work' field is set as follows:BPF_CALL_3(bpf_timer_init, struct
bpf_timer_kern*, timer, structbpf_map*, map,Quoted 8 lines
u64, flags) { ... INIT_WORK(&t->sync_work, bpf_timer_sync_work_cb); ... }So, it looks like '
kfree_rcu' would be called for a non-rcu pointer.That was my initial assumption too, but Alexei told me it was fine. And I think he is correct because
kfree_rcudoesn't need thercu_headto be initialized.
Quoted 7 lines
So in the end, we initialize the memory as a
work_struct, and when that work kicks in, we reuse that exact same memory as thercu_head. This is fine because that work will never be reused.If I understand correctly, this is to save a few bytes as this is a critical struct used in programs with a high rate usage, and every byte counts.
Yes. All correct.
Probably makes sense to add a comment before kfree_rcu() line in bpf_timer_sync_work_cb() thatkfree_rcu will wait for bpf_timer_cancel() to finish as was done in
commit 0281b919e175 ("bpf: Fix racing between bpf_timer_cancel_and_free and bpf_timer_cancel").
I suspect that's what confused Eduard.
The patch 1 looks great overall.
If we're going to keep this combined bpf_timer_* api for both wq and timer we'd need to add flags compatibility check
to bpf_timer_start() too.
We can disallow this flag in 'flags' argument and use one from t->flags. Which kinda makes sense to make bpf_timer_start() less verbose. Alternatively we can allow bpf_timer_start() to have it,
but then we'd need to check that it is actually passed.
Either way the patch needs an extra check in bpf_timer_start(). Just ignoring BPF_F_TIMER_SLEEPABLE in bpf_timer_start() doesn't look right.
RFC PATCH v6 2/6 bpf: Add support for KF_ARG_PTR_TO_TIMER
In reply to Benjamin TissoiresFrom: Kumar Kartikeya Dwivedi <[email protected]>
Introduce support for KF_ARG_PTR_TO_TIMER. The kfuncs will use bpf_timer as argument and that will be recognized as timer argument by verifier. bpf_timer_kern casting can happen inside kfunc, but using bpf_timer in argument makes life easier for users who work with non-kern type in BPF progs.
Fix up process_timer_func's meta argument usage (ignore if NULL) so that we can share the same checks for helpers and kfuncs. meta argument is only needed to ensure bpf_timer_init's timer and map arguments are coming from the same map (map_uid logic is necessary for correct inner-map handling).
No such concerns will be necessary for kfuncs as timer initialization happens using helpers, hence pass NULL to process_timer_func from kfunc argument handling code to ignore it.
---
changes in v6:
- used Kumar's version of the patch
- reverted +BTF_ID(struct, bpf_timer_kern)
changes in v5:
- also check for the reg offset
changes in v4:
- enforce KF_ARG_PTR_TO_TIMER to be of type PTR_TO_MAP_VALUE
new in v3 (split from v2 02/10)
--- kernel/bpf/verifier.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
kernel/bpf/verifier.c +25 -0
@@ -7568,12 +7568,16 @@ static int process_timer_func(struct bpf_verifier_env *env, int regno, val + reg->off, map->record->timer_off);return -EINVAL ; } +/* meta is only needed for bpf_timer_init to match timer and map */ +if (!meta) + goto out;if (meta->map_ptr) {verbose (env,"verifier bug. Two map pointers in a timer helper\n" );return -EFAULT ; } meta->map_uid = reg->map_uid; meta->map_ptr = map; +out:return 0 ; }
@@ -10826,6 +10830,7 @@ enum {KF_ARG_LIST_NODE_ID ,KF_ARG_RB_ROOT_ID ,KF_ARG_RB_NODE_ID , +KF_ARG_TIMER_ID , };BTF_ID_LIST (kf_arg_btf_ids)
@@ -10834,6 +10839,7 @@ BTF_ID(struct, bpf_list_head) BTF_ID(struct, bpf_list_node) BTF_ID(struct, bpf_rb_root) BTF_ID(struct, bpf_rb_node) +BTF_ID(struct, bpf_timer_kern)static bool __is_kfunc_ptr_arg_type (const struct btf *btf,const struct btf_param *arg,int type)
@@ -10877,6 +10883,11 @@ static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_parreturn __is_kfunc_ptr_arg_type (btf, arg,KF_ARG_RB_NODE_ID ); } +static bool is_kfunc_arg_timer (const struct btf *btf,const struct btf_param *arg) +{ +return __is_kfunc_ptr_arg_type (btf, arg,KF_ARG_TIMER_ID ); +} +static bool is_kfunc_arg_callback (struct bpf_verifier_env *env,const struct btf *btf,const struct btf_param *arg) {
@@ -10946,6 +10957,7 @@ enum kfunc_ptr_arg_type {KF_ARG_PTR_TO_NULL ,KF_ARG_PTR_TO_CONST_STR ,KF_ARG_PTR_TO_MAP , + KF_ARG_PTR_TO_TIMER, }; enum special_kfunc_type {
@@ -11102,6 +11114,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (is_kfunc_arg_map (meta->btf, &args[argno]))return KF_ARG_PTR_TO_MAP ; +if (is_kfunc_arg_timer (meta->btf, &args[argno])) +return KF_ARG_PTR_TO_TIMER ; +if ((base_type (reg->type) ==PTR_TO_BTF_ID || reg2btf_ids[base_type (reg->type)])) {if (!btf_type_is_struct (ref_t)) { verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n",
@@ -11735,6 +11750,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_case KF_ARG_PTR_TO_CALLBACK :case KF_ARG_PTR_TO_REFCOUNTED_KPTR :case KF_ARG_PTR_TO_CONST_STR : +case KF_ARG_PTR_TO_TIMER :/* Trusted by default */ break ;default :
@@ -12021,6 +12037,15 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_if (ret)return ret;break ; +case KF_ARG_PTR_TO_TIMER : +if (reg->type !=PTR_TO_MAP_VALUE ) { +verbose (env,"arg#%d doesn't point to a map value\n" , i); +return -EINVAL ; + } + ret =process_timer_func (env, regno,NULL ); +if (ret <0 ) +return ret; +break ; } }
Signature · 1 line
2.44.0
Re: [PATCH RFC bpf-next v6 2/6] bpf: Add support for KF_ARG_PTR_TO_TIMER
Quoted 14 lines · On Mon, 2024-04-08 at 10:09 +0200, [email protected] wrote:
From: Kumar Kartikeya Dwivedi <[email protected]>
Introduce support for
KF_ARG_PTR_TO_TIMER. The kfuncs will usebpf_timeras argument and that will be recognized as timer argument by verifier.bpf_timer_kerncasting can happen inside kfunc, but usingbpf_timerin argument makes life easier for users who work with non-kern type in BPF progs.Fix up process_timer_func's meta argument usage (ignore if NULL) so that we can share the same checks for helpers and kfuncs. meta argument is only needed to ensure bpf_timer_init's timer and map arguments are coming from the same map (
map_uidlogic is necessary for correct inner-map handling).
Quoted 8 lines
No such concerns will be necessary for kfuncs as timer initialization happens using helpers, hence pass NULL to
process_timer_funcfrom kfunc argument handling code to ignore it.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>Signed-off-by Benjamin Tissoires <[email protected]>---
Re: [PATCH RFC bpf-next v6 2/6] bpf: Add support for KF_ARG_PTR_TO_TIMER
In reply to [email protected]Quoted 18 lines · On Apr 08 2024, [email protected] wrote:
From: Kumar Kartikeya Dwivedi <[email protected]>
Introduce support for
KF_ARG_PTR_TO_TIMER. The kfuncs will usebpf_timeras argument and that will be recognized as timer argument by verifier.bpf_timer_kerncasting can happen inside kfunc, but usingbpf_timerin argument makes life easier for users who work with non-kern type in BPF progs.Fix up process_timer_func's meta argument usage (ignore if NULL) so that we can share the same checks for helpers and kfuncs. meta argument is only needed to ensure bpf_timer_init's timer and map arguments are coming from the same map (
map_uidlogic is necessary for correct inner-map handling).No such concerns will be necessary for kfuncs as timer initialization happens using helpers, hence pass NULL to
process_timer_funcfrom kfunc argument handling code to ignore it.
Quoted 8 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>Signed-off-by Benjamin Tissoires <[email protected]>---
changes in v6:
- used Kumar's version of the patch
- reverted+BTF_ID(struct, bpf_timer_kern)
My bad. While working on bpf_wq I realized I shouldn't have touched this part. See below:
Quoted 42 lines
changes in v5:
- also check for the reg offsetchanges in v4:
- enforceKF_ARG_PTR_TO_TIMERto be of typePTR_TO_MAP_VALUEnew in v3 (split from v2 02/10)
--- kernel/bpf/verifier.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)kernel/bpf/verifier.c +5
-0@@ -7568,12 +7568,16 @@ static int process_timer_func(struct bpf_verifier_env *env, int regno, val + reg->off, map->record->timer_off);return -EINVAL ; } +/* meta is only needed for bpf_timer_init to match timer and map */ +if (!meta) + goto out;if (meta->map_ptr) {verbose (env,"verifier bug. Two map pointers in a timer helper\n" );return -EFAULT ; } meta->map_uid = reg->map_uid; meta->map_ptr = map; +out:return 0 ; }@@ -10826,6 +10830,7 @@ enum {KF_ARG_LIST_NODE_ID ,KF_ARG_RB_ROOT_ID ,KF_ARG_RB_NODE_ID , +KF_ARG_TIMER_ID , };BTF_ID_LIST (kf_arg_btf_ids)
kernel/bpf/verifier.c BTF_ID(struct, bpf_list_head) · line 10842
Quoted 4 lines
kernel/bpf/verifier.c +1
-0@@ -10834,6 +10839,7 @@ BTF_ID(struct, bpf_list_head) BTF_ID(struct, bpf_list_node) BTF_ID(struct, bpf_rb_root) BTF_ID(struct, bpf_rb_node) +BTF_ID (struct, bpf_timer_kern)
As Kumar originally put, this should be BTF_ID(struct, bpf_timer), and he explained everything in the commit message. I was just too dumb to understand it properly.
(Adding a comment here in case we want to extend bpf_timer API in the future, and so this patch will be useful).
Cheers,
Benjamin
Quoted 61 lines
kernel/bpf/verifier.c +19
-0static bool __is_kfunc_ptr_arg_type (const struct btf *btf,const struct btf_param *arg,int type)@@ -10877,6 +10883,11 @@ static bool is_kfunc_arg_rbtree_node(const struct btf *btf, const struct btf_parreturn __is_kfunc_ptr_arg_type (btf, arg,KF_ARG_RB_NODE_ID ); } +static bool is_kfunc_arg_timer (const struct btf *btf,const struct btf_param *arg) +{ +return __is_kfunc_ptr_arg_type (btf, arg,KF_ARG_TIMER_ID ); +} +static bool is_kfunc_arg_callback (struct bpf_verifier_env *env,const struct btf *btf,const struct btf_param *arg) {@@ -10946,6 +10957,7 @@ enum kfunc_ptr_arg_type {KF_ARG_PTR_TO_NULL ,KF_ARG_PTR_TO_CONST_STR ,KF_ARG_PTR_TO_MAP , + KF_ARG_PTR_TO_TIMER, }; enum special_kfunc_type {@@ -11102,6 +11114,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (is_kfunc_arg_map (meta->btf, &args[argno]))return KF_ARG_PTR_TO_MAP ; +if (is_kfunc_arg_timer (meta->btf, &args[argno])) +return KF_ARG_PTR_TO_TIMER ; +if ((base_type (reg->type) ==PTR_TO_BTF_ID || reg2btf_ids[base_type (reg->type)])) {if (!btf_type_is_struct (ref_t)) { verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n",@@ -11735,6 +11750,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_case KF_ARG_PTR_TO_CALLBACK :case KF_ARG_PTR_TO_REFCOUNTED_KPTR :case KF_ARG_PTR_TO_CONST_STR : +case KF_ARG_PTR_TO_TIMER :/* Trusted by default */ break ;default :@@ -12021,6 +12037,15 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_if (ret)return ret;break ; +case KF_ARG_PTR_TO_TIMER : +if (reg->type !=PTR_TO_MAP_VALUE ) { +verbose (env,"arg#%d doesn't point to a map value\n" , i); +return -EINVAL ; + } + ret =process_timer_func (env, regno,NULL ); +if (ret <0 ) +return ret; +break ; } }Signature · 2 lines
-- 2.44.0
RFC PATCH v6 3/6 bpf/helpers: introduce bpf_timer_set_sleepable_cb() kfunc
In reply to Benjamin TissoiresIn this patch, bpf_timer_set_sleepable_cb() is functionally equivalent to bpf_timer_set_callback(), to the exception that it enforces the timer to be started with BPF_F_TIMER_SLEEPABLE.
But given that bpf_timer_set_callback() is a helper when bpf_timer_set_sleepable_cb() is a kfunc, we need to teach the verifier about its attached callback.
Marking that callback as sleepable will be done in a separate patch
---
changes in v6:
- adapted for flags being set during timer_init
changes in v5:
- enforced sleepable timers to only have BPF_F_TIMER_SLEEPABLE
- use is_bpf_timer_set_sleepable_cb_impl_kfunc() instead of generic is_async_cb()
changes in v4:
- added a new (ignored) argument to the kfunc so that we do not need to wlak the stack
new in v3 (split from v2 02/10)
--- kernel/bpf/helpers.c | 43 ++++++++++++++++++++++++++++++++++-- kernel/bpf/verifier.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +41 -2
@@ -1291,8 +1291,8 @@ static const struct bpf_func_proto bpf_timer_init_proto = { .arg3_type = ARG_ANYTHING, }; -BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn, - struct bpf_prog_aux *, aux) +static int __bpf_timer_set_callback (struct bpf_timer_kern *timer,void *callback_fn, +struct bpf_prog_aux *aux,bool is_sleepable) {struct bpf_prog *prev, *prog = aux->prog;struct bpf_hrtimer *t;
@@ -1306,6 +1306,10 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callb ret = -EINVAL ; goto out; } +if (!!(t->flags &BPF_F_TIMER_SLEEPABLE ) != is_sleepable) { + ret = -EINVAL ; + goto out; + }if (!atomic64_read (&t->map->usercnt)) {/* maps with timers must be either held by user space * or pinned in bpffs. Otherwise timer might still be
@@ -1336,6 +1340,12 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callbreturn ret; } +BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn, + struct bpf_prog_aux *, aux) +{ +return __bpf_timer_set_callback (timer, callback_fn, aux, false); +} +static const struct bpf_func_proto bpf_timer_set_callback_proto = { .func = bpf_timer_set_callback, .gpl_only = true,
@@ -2650,6 +2660,34 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } +/** +* bpf_timer_set_sleepable_cb_impl() - Configure the timer to call %callback_fn +* static function in a sleepable context. +* @timer: The bpf_timer that needs to be configured +* @callback_fn: a static bpf function +* +* @returns %0 on success. %-EINVAL if %timer was not initialized with +* bpf_timer_init() earlier. %-EPERM if %timer is in a map that doesn't +* have any user references. +* The user space should either hold a file descriptor to a map with timers +* or pin such map in bpffs. When map is unpinned or file descriptor is +* closed all timers in the map will be cancelled and freed. +* +* This kfunc is equivalent to %bpf_timer_set_callback except that it tells +* the verifier that the target callback is run in a sleepable context. +*/ +__bpf_kfunc int bpf_timer_set_sleepable_cb_impl(struct bpf_timer_kern *timer, +int (callback_fn )(void *map,int *key,struct bpf_timer *timer), +void *aux__ign) +{ +struct bpf_prog_aux *aux = (struct bpf_prog_aux *)aux__ign; + +if (!aux) +return -EINVAL ; + +return __bpf_timer_set_callback (timer, (void *)callback_fn, aux, true); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (generic_btf_ids)
@@ -2726,6 +2764,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) +BTF_ID_FLAGS(func, bpf_timer_set_sleepable_cb_impl)BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +58 -2
122 lines not on this page · raw message
Signature · 1 line
2.44.0
Eduard Zingerman
maintainerRe: [PATCH RFC bpf-next v6 3/6] bpf/helpers: introduce bpf_timer_set_sleepable_cb() kfunc
On Mon, 2024-04-08 at 10:09 +0200, Benjamin Tissoires wrote:
[...]
Quoted 4 lines
kernel/bpf/helpers.c +0
-0
[...]
kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_dynptr_is_null) · line 2767
Quoted 5 lines
kernel/bpf/helpers.c +1
-0@@ -2726,6 +2764,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) +BTF_ID_FLAGS (func, bpf_timer_set_sleepable_cb_impl)
Note:
this hunk does not apply cleanly on top of current master. The line 'BTF_ID_FLAGS(func, bpf_modify_return_test_tp)' was added to the list since last time current patch-set was merged.
Quoted 3 lines
kernel/bpf/helpers.c +0
-0BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
Re: [PATCH RFC bpf-next v6 3/6] bpf/helpers: introduce bpf_timer_set_sleepable_cb() kfunc
Quoted 11 lines · Eduard Zingerman
On Mon, 2024-04-08 at 10:09 +0200, Benjamin Tissoires wrote:
[...]Quoted 4 lines
kernel/bpf/helpers.c +0
-0[...]
Quoted 10 lines
kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_dynptr_is_null) · line 2767
Quoted 5 lines
kernel/bpf/helpers.c +1
-0@@ -2726,6 +2764,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) +BTF_ID_FLAGS (func, bpf_timer_set_sleepable_cb_impl)Note:
this hunk does not apply cleanly on top of current master. The line 'BTF_ID_FLAGS(func, bpf_modify_return_test_tp)' was added to the list since last time current patch-set was merged.
Oops, thanks for the update.
Just to be clear, I already mentioned it in the cover letter, but this series is not intended to be merged just now (thus RFC again). The plan is to add a new bpf_wq API on the side, and compare it with this v6 to see which one is best, because I am trying to force the workqueue API into a timer, when it's getting further and further away from each other.
Cheers,
Benjamin
Quoted 6 lines
Quoted 5 lines
kernel/bpf/helpers.c +0
-0BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
RFC PATCH v6 4/6 bpf/helpers: mark the callback of bpf_timer_set_sleepable_cb() as sleepable
In reply to Benjamin TissoiresNow that we have bpf_timer_set_sleepable_cb() available and working, we can tag the attached callback as sleepable, and let the verifier check in the correct context the calls and kfuncs.
---
no changes in v6
no changes in v5
changes in v4:
- use a function parameter to forward the sleepable information
new in v3 (split from v2 02/10)
--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
include/linux/bpf_verifier.h +1 -0
@@ -426,6 +426,7 @@ struct bpf_verifier_state {* while they are still in use. */ bool used_as_loop_entry; +bool in_sleepable;/* first and last insn idx of this verifier state */ u32 first_insn_idx;
kernel/bpf/verifier.c +10 -3
@@ -1434,6 +1434,7 @@ static int copy_verifier_state(struct bpf_verifier_state *dst_state, } dst_state->speculative = src->speculative; dst_state->active_rcu_lock = src->active_rcu_lock; + dst_state->in_sleepable = src->in_sleepable; dst_state->curframe = src->curframe; dst_state->active_lock.ptr = src->active_lock.ptr; dst_state->active_lock.id = src->active_lock.id;
@@ -2407,7 +2408,7 @@ static void init_func_state(struct bpf_verifier_env *env,/* Similar to push_stack(), but for async callbacks */ static struct bpf_verifier_state *push_async_cb (struct bpf_verifier_env *env,int insn_idx,int prev_insn_idx, -int subprog) +int subprog,bool is_sleepable) {struct bpf_verifier_stack_elem *elem;struct bpf_func_state *frame;
@@ -2434,6 +2435,7 @@ static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env,* Initialize it similar to do_check_common(). */ elem->st.branches =1 ; + elem->st.in_sleepable = is_sleepable; frame =kzalloc (sizeof (*frame),GFP_KERNEL );if (!frame) goto err;
@@ -5279,7 +5281,8 @@ static int map_kptr_match_type(struct bpf_verifier_env *env,static bool in_sleepable (struct bpf_verifier_env *env) { -return env->prog->sleepable; +return env->prog->sleepable || + (env->cur_state && env->cur_state->in_sleepable); }/* The non-sleepable programs and sleepable programs with explicit bpf_rcu_read_lock()
@@ -9497,7 +9500,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins/* there is no real recursion here. timer callbacks are async */ env->subprog_info[subprog].is_async_cb = true; async_cb =push_async_cb (env, env->subprog_info[subprog].start, - insn_idx, subprog); + insn_idx, subprog, +is_bpf_timer_set_sleepable_cb_impl_kfunc (insn->imm));if (!async_cb)return -EFAULT ; callee = async_cb->frame[0 ];
@@ -16947,6 +16951,9 @@ static bool states_equal(struct bpf_verifier_env *env,if (old->active_rcu_lock != cur->active_rcu_lock)return false; +if (old->in_sleepable != cur->in_sleepable) +return false; +/* for states to be equal callsites have to be the same * and all frame states need to be equivalent */
Signature · 1 line
2.44.0
Re: [PATCH RFC bpf-next v6 4/6] bpf/helpers: mark the callback of bpf_timer_set_sleepable_cb() as sleepable
Quoted 7 lines · Benjamin Tissoires
Now that we have
bpf_timer_set_sleepable_cb()available and working, we can tag the attached callback as sleepable, and let the verifier check in the correct context the calls and kfuncs.Signed-off-by Benjamin Tissoires <[email protected]>---
I think this patch is fine with one nit regarding in_sleepable().
kernel/bpf/verifier.c static int map_kptr_match_type(struct bpf_verifier_env *env, · line 5286
Quoted 8 lines
kernel/bpf/verifier.c +2
-1@@ -5279,7 +5281,8 @@ static int map_kptr_match_type(struct bpf_verifier_env *env,static bool in_sleepable (struct bpf_verifier_env *env) { -return env->prog->sleepable; +return env->prog->sleepable || + (env->cur_state && env->cur_state->in_sleepable); }
Sorry, I already raised this before.
As far as I understand the 'env->cur_state' check is needed because this function is used from do_misc_fixups():
if (is_storage_get_function(insn->imm)) {
if (!in_sleepable(env) ||
env->insn_aux_data[i + delta].storage_get_func_atomic)
insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC);
else
insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL);
insn_buf[1] = *insn;
cnt = 2;
...
}For a timer callback function 'env->prog->sleepable' would be false. Which means that inside sleepable callback function GFP_ATOMIC would be used in cases where GFP_KERNEL would be sufficient.
An alternative would be to check (and set) sleepable flag not for a full program but for a subprogram.
Whether or not this is something worth addressing I don't know.
[...]
Re: [PATCH RFC bpf-next v6 4/6] bpf/helpers: mark the callback of bpf_timer_set_sleepable_cb() as sleepable
Quoted 37 lines · Eduard Zingerman
Quoted 7 lines · Benjamin Tissoires
Now that we have
bpf_timer_set_sleepable_cb()available and working, we can tag the attached callback as sleepable, and let the verifier check in the correct context the calls and kfuncs.Signed-off-by Benjamin Tissoires <[email protected]>---
I think this patch is fine with one nit regarding
in_sleepable().Acked-by Eduard Zingerman <[email protected]>kernel/bpf/verifier.c static int map_kptr_match_type(struct bpf_verifier_env *env, · line 5286
Quoted 8 lines
kernel/bpf/verifier.c +2
-1@@ -5279,7 +5281,8 @@ static int map_kptr_match_type(struct bpf_verifier_env *env,static bool in_sleepable (struct bpf_verifier_env *env) { -return env->prog->sleepable; +return env->prog->sleepable || + (env->cur_state && env->cur_state->in_sleepable); }Sorry, I already raised this before.
As far as I understand the 'env->cur_state' check is needed because this function is used fromdo_misc_fixups():if (is_storage_get_function(insn->imm)) { if (!in_sleepable(env) || env->insn_aux_data[i + delta].storage_get_func_atomic) insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC); else insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL); insn_buf[1] = *insn; cnt = 2; ... }
Quoted 5 lines
For a timer callback function '
env->prog->sleepable' would be false. Which means that inside sleepable callback functionGFP_ATOMICwould be used in cases whereGFP_KERNELwould be sufficient.
An alternative would be to check (and set) sleepable flag not for a full program but for a subprogram.
At this point all subprograms are still part of the main program. jit_subprogs() hasn't been called yet.
So there is only one 'prog' object.
So cannot really set prog->sleepable for callback subprog.
But you've raised a good point.
We can remove "!in_sleepable(env)" part in do_misc_fixups() with:
- if (in_sleepable(env) && is_storage_get_function(func_id))
-
env->insn_aux_data[insn_idx].storage_get_func_atomic = true;
+ if (is_storage_get_function(func_id))
+ env->insn_aux_data[insn_idx].storage_get_func_atomic = !in_sleepable(env);
RFC PATCH v6 5/6 tools: sync include/uapi/linux/bpf.h
In reply to Benjamin Tissoirescp include/uapi/linux/bpf.h tools/include/uapi/linux/bpf.h
---
updated in v6
no changes in v5
new in v4
--- tools/include/uapi/linux/bpf.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
tools/include/uapi/linux/bpf.h +17 -3
@@ -1662,9 +1662,10 @@ union bpf_attr { } query;struct {/* anonymous struct used by BPF_RAW_TRACEPOINT_OPEN command */ -__u64 name; -__u32 prog_fd; -__aligned_u64 cookie; +__u64 name; +__u32 prog_fd; + __u32 :32; +__aligned_u64 cookie; } raw_tracepoint;struct {/* anonymous struct for BPF_BTF_LOAD */
@@ -7468,6 +7469,19 @@ enum { BPF_F_TIMER_CPU_PIN = (1ULL << 1), }; +/* Extra flags to control bpf_timer_init() behaviour, in addition to CLOCK_*. +* - BPF_F_TIMER_SLEEPABLE: Timer will run in a workqueue in a sleepable +* context, with no guarantees of ordering nor timing (consider this as +* being just offloaded immediately). +*/ +enum { +/* CLOCK_* are using bits 0 to 3 */ +BPF_F_TIMER_SLEEPABLE = (1ULL <<4 ), + __MAX_BPF_F_TIMER_INIT, +}; + +#define MAX_BPF_F_TIMER_INIT __MAX_BPF_F_TIMER_INIT +/* BPF numbers iterator state */ struct bpf_iter_num {/* opaque iterator state; having __u64 here allows to preserve correct
Signature · 1 line
2.44.0
Re: [PATCH RFC bpf-next v6 5/6] tools: sync include/uapi/linux/bpf.h
Quoted 22 lines · Benjamin Tissoires
cp
include/uapi/linux/bpf.htools/include/uapi/linux/bpf.hSigned-off-by Benjamin Tissoires <[email protected]>---
updated in v6
no changes in v5
new in v4
--- tools/include/uapi/linux/bpf.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)tools/include/uapi/linux/bpf.h +0
-0@@ -1662,9 +1662,10 @@ union bpf_attr { } query;
tools/include/uapi/linux/bpf.h union bpf_attr { · line 1669
Quoted 10 lines
tools/include/uapi/linux/bpf.h +4
-3struct {/* anonymous struct used by BPF_RAW_TRACEPOINT_OPEN command */ -__u64 name; -__u32 prog_fd; -__aligned_u64 cookie; +__u64 name; +__u32 prog_fd; + __u32 :32; +__aligned_u64 cookie; } raw_tracepoint;
something wrong with your tree.
there is no such issue in bpf-next.
RFC PATCH v6 6/6 selftests/bpf: add sleepable timer tests
In reply to Benjamin Tissoiresbpf_experimental.h and ../bpf_testmod/bpf_testmod_kfunc.h are both including vmlinux.h, which is not compatible with including time.h or bpf_tcp_helpers.h.
So keep sleepable tests in a separate bpf source file.
The first correct test is run twice for convenience:
- first through RUN_TESTS
- then we ensure that the timer was actually executed, in a manual load/attach/run
---
changes in v6:
- use of new _init API with BPF_F_TIMER_SLEEPABLE
changes in v5b:
- do not use SEC(syscall) as they are run in a sleepable context already
- duplicate test_call_sleepable() so we can test if the callback gets called
changes in v5:
- keep sleepable test separate
new in v4
--- tools/testing/selftests/bpf/bpf_experimental.h | 5 + .../selftests/bpf/bpf_testmod/bpf_testmod.c | 5 + .../selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + tools/testing/selftests/bpf/prog_tests/timer.c | 34 ++++ .../testing/selftests/bpf/progs/timer_sleepable.c | 213 +++++++++++++++++++++ 5 files changed, 258 insertions(+)
tools/testing/selftests/bpf/bpf_experimental.h +5 -0
@@ -459,4 +459,9 @@ extern int bpf_iter_css_new(struct bpf_iter_css *it,extern struct cgroup_subsys_state *bpf_iter_css_next (struct bpf_iter_css *it)__weak __ksym; extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym; +extern int bpf_timer_set_sleepable_cb_impl(struct bpf_timer *timer, + int (callback_fn)(void *map, int *key, struct bpf_timer *timer), + void *aux__ign) __ksym; +#define bpf_timer_set_sleepable_cb (timer, cb) \ + bpf_timer_set_sleepable_cb_impl(timer, cb, NULL)#endif
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +5 -0
@@ -494,6 +494,10 @@ __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unusedreturn arg; } +__bpf_kfunc void bpf_kfunc_call_test_sleepable(void) +{ +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -520,6 +524,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU) BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg) BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset) +BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE)BTF_KFUNCS_END (bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +1 -0
@@ -96,6 +96,7 @@ void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; void bpf_kfunc_call_test_destructive(void) __ksym; +void bpf_kfunc_call_test_sleepable(void) __ksym;void bpf_kfunc_call_test_offset (struct prog_test_ref_kfunc *p);struct prog_test_member *bpf_kfunc_call_memb_acquire (void );
tools/testing/selftests/bpf/prog_tests/timer.c +34 -0
@@ -3,6 +3,7 @@#include <test_progs.h> #include "timer.skel.h" #include "timer_failure.skel.h" +#include "timer_sleepable.skel.h" #define NUM_THR 8
@@ -95,3 +96,36 @@ void serial_test_timer(void)RUN_TESTS (timer_failure); } + +/* isolate sleepable tests from main timer tests +* because if test_timer fails, it spews the console +* with 10000 * 5 "spin_lock_thread:PASS:test_run_opts retval 0 nsec" +*/ +void serial_test_sleepable_timer (void ) +{ +struct timer_sleepable *timer_sleepable_skel =NULL ; +int err, prog_fd; + +LIBBPF_OPTS (bpf_test_run_opts, topts); + +RUN_TESTS (timer_sleepable); + +/* re-run the success test to check if the timer was actually executed */ + + timer_sleepable_skel =timer_sleepable__open_and_load (); +if (!ASSERT_OK_PTR (timer_sleepable_skel,"timer_sleepable_skel_load" )) +return ; + + err =timer_sleepable__attach (timer_sleepable_skel); +if (!ASSERT_OK (err,"timer_sleepable_attach" )) +return ; + + prog_fd =bpf_program__fd (timer_sleepable_skel->progs.test_syscall_sleepable); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (topts.retval,0 ,"test_run" ); + +usleep (50 );/* 10 usecs should be enough, but give it extra */ + +ASSERT_EQ (timer_sleepable_skel->bss->ok_sleepable,1 ,"ok_sleepable" ); +}
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +213 -0
213 lines not on this page · raw message
Signature · 1 line
2.44.0
Re: [PATCH RFC bpf-next v6 6/6] selftests/bpf: add sleepable timer tests
Quoted 6 lines · Benjamin Tissoires
bpf_experimental.hand../bpf_testmod/bpf_testmod_kfunc.hare both includingvmlinux.h, which is not compatible with includingtime.horbpf_tcp_helpers.h.So keep sleepable tests in a separate bpf source file.
Quoted 8 lines
The first correct test is run twice for convenience:
- first throughRUN_TESTS
- then we ensure that the timer was actually executed, in a manual load/attach/runSigned-off-by Benjamin Tissoires <[email protected]>---
(With a few nitpicks)
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/timer.c +0
-0
[...]
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/timer.c +8
-0+void serial_test_sleepable_timer (void ) +{ +struct timer_sleepable *timer_sleepable_skel =NULL ; +int err, prog_fd; + +LIBBPF_OPTS (bpf_test_run_opts, topts); + +RUN_TESTS (timer_sleepable);
tools/testing/selftests/bpf/prog_tests/timer.c void serial_test_timer(void) · line 121
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/timer.c +10
-0+ +/* re-run the success test to check if the timer was actually executed */ + + timer_sleepable_skel =timer_sleepable__open_and_load (); +if (!ASSERT_OK_PTR (timer_sleepable_skel,"timer_sleepable_skel_load" )) +return ; + + err =timer_sleepable__attach (timer_sleepable_skel); +if (!ASSERT_OK (err,"timer_sleepable_attach" )) +return ;
Nit: this should call timer_sleepable__destroy();
tools/testing/selftests/bpf/prog_tests/timer.c void serial_test_timer(void) · line 130
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/timer.c +9
-0+ + prog_fd =bpf_program__fd (timer_sleepable_skel->progs.test_syscall_sleepable); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (topts.retval,0 ,"test_run" ); + +usleep (50 );/* 10 usecs should be enough, but give it extra */ + +ASSERT_EQ (timer_sleepable_skel->bss->ok_sleepable,1 ,"ok_sleepable" );
Nit: same as above.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/timer.c +1
-0+}tools/testing/selftests/bpf/progs/timer_sleepable.c new file +0
-0
[...]
Quoted 24 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +24
-0+SEC("tc") +/* check that calling bpf_timer_start() with BPF_F_TIMER_SLEEPABLE on a sleepable +* callback works +*/ +__retval(0) +long test_call_sleepable (void *ctx) +{ +int key =0 ; +struct bpf_timer *timer; + +if (ok_sleepable &1 ) +return -1 ; + + timer =bpf_map_lookup_elem (&timer_map, &key); +if (timer) { +if (bpf_timer_init (timer, &timer_map,CLOCK_MONOTONIC |BPF_F_TIMER_SLEEPABLE ) !=0 ) +return -2 ; +bpf_timer_set_sleepable_cb (timer, timer_cb_sleepable); +if (bpf_timer_start (timer,0 ,0 )) +return -3 ; + }else { +return -4 ; + } +
tools/testing/selftests/bpf/progs/timer_sleepable.c line 69
Quoted 10 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +10
-0+return 0 ; +} + +SEC ("syscall" ) +/* check that calling bpf_timer_start() with BPF_F_TIMER_SLEEPABLE on a sleepable +* callback works. + */ +__retval(0) +long test_syscall_sleepable (void *ctx) +{
Nit: the body of this function is the same as in test_call_sleepable(),
maybe factor it out as an auxiliary static function? (also, should these tests use different 'key' ?)
Quoted 19 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +19
-0+int key =0 ; +struct bpf_timer *timer; + +if (ok_sleepable &1 ) +return -1 ; + + timer =bpf_map_lookup_elem (&timer_map, &key); +if (timer) { +if (bpf_timer_init (timer, &timer_map,CLOCK_MONOTONIC |BPF_F_TIMER_SLEEPABLE ) !=0 ) +return -2 ; +bpf_timer_set_sleepable_cb (timer, timer_cb_sleepable); +if (bpf_timer_start (timer,0 ,0 )) +return -3 ; + }else { +return -4 ; + } + +return 0 ; +}
[...]
Quoted 11 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +11
-0+SEC("tc") +/* check that calling bpf_timer_start() with a delay on a sleepable +* callback is returning -EINVAL +*/ +__retval(-22) +long test_call_sleepable_delay (void *ctx) +{ +int key =2 ; +struct bpf_timer *timer; + + timer =bpf_map_lookup_elem (&timer_map, &key);
tools/testing/selftests/bpf/progs/timer_sleepable.c line 158
Quoted 10 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +10
-0+if (!timer) +return 1 ; + +if (bpf_timer_init (timer, &timer_map,CLOCK_MONOTONIC |BPF_F_TIMER_SLEEPABLE )) +return 2 ; + +if (bpf_timer_set_sleepable_cb (timer, timer_cb_sleepable)) +return 3 ; + +return bpf_timer_start (timer,1 ,0 );
Q: should verifier statically check that 3rd parameter is zero for sleepable timers?
(same question for call to bpf_timer_set_sleepable_cb() with non-sleepable map)
[...]
Re: [PATCH RFC bpf-next v6 6/6] selftests/bpf: add sleepable timer tests
Quoted 17 lines · Eduard Zingerman
Quoted 17 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +16
-0+SEC("tc") +/* check that calling bpf_timer_start() with a delay on a sleepable +* callback is returning -EINVAL +*/ +__retval(-22) +long test_call_sleepable_delay (void *ctx) +{ +int key =2 ; +struct bpf_timer *timer; + + timer =bpf_map_lookup_elem (&timer_map, &key); +if (!timer) +return 1 ; + +if (bpf_timer_init (timer, &timer_map,CLOCK_MONOTONIC |BPF_F_TIMER_SLEEPABLE )) +return 2 ;
Quoted 8 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c line 158
Quoted 5 lines
tools/testing/selftests/bpf/progs/timer_sleepable.c new file +5
-0+ +if (bpf_timer_set_sleepable_cb (timer, timer_cb_sleepable)) +return 3 ; + +return bpf_timer_start (timer,1 ,0 );Q: should verifier statically check that 3rd parameter is zero for sleepable timers?
(same question for call tobpf_timer_set_sleepable_cb()with non-sleepable map)
It can, but that sounds like more work for the verifier. Which gives more reasons to use new kfuncs and clean start with bpf_wq.
Quoted 8 lines · Benjamin Tissoires
New version of the sleepable
bpf_timercode.I'm posting this as this is the result of the previous review, so we can have a baseline to compare to.
The plan is now to introduce a new user API struct
bpf_wq, as the timer API working on softIRQ seems to be quite far away from a wq.
Looking forward. I think it's almost there.