bpf: Add bpf_call_rcu() and bpf_call_rcu_tasks_trace()
Subsystems (6)
cover bpf: Add bpf_call_rcu() and bpf_call_rcu_tasks_trace()
BPF programs that manage their own objects have no way to run their own logic once an RCU grace period has elapsed. bpf_obj_drop() defers a free, but returning an index to an allocator or unpinning a resource once readers are done has no equivalent. sched_ext's BPF library works around this today by pushing freed nodes onto a list and having a userspace thread call membarrier(MEMBARRIER_CMD_GLOBAL) and then run a BPF program to reclaim them; it is the first intended user.
Add:
int bpf_call_rcu(struct bpf_rcu_head *rh, void *map,
int (*callback)(struct bpf_map *map, void *key,
void *value));
and bpf_call_rcu_tasks_trace(), same signature, which also waits for sleepable programs.
@rh is a struct bpf_rcu_head embedded in a value of @map, so the callback runs as callback(map, key, value) for the element it lives in and needs no cookie. The field is only accepted in BPF_MAP_TYPE_ARRAY, and arming holds a reference on the calling program until the callback has run. Patch 1 covers the lifetime rules.
This needs
https://lore.kernel.org/all/[email protected]/
for call_rcu() and call_srcu() to be safe from the contexts a BPF program can be called in.
Puranjay Mohan (4):
bpf: Add bpf_call_rcu() kfunc
selftests/bpf: Add tests for bpf_call_rcu()
bpf: Add bpf_call_rcu_tasks_trace() kfunc selftests/bpf: Add a test for bpf_call_rcu_tasks_trace()
include/linux/bpf.h | 9 + include/uapi/linux/bpf.h | 4 + kernel/bpf/btf.c | 7 + kernel/bpf/helpers.c | 104 ++++++++++ kernel/bpf/map_in_map.c | 4 + kernel/bpf/map_iter.c | 6 + kernel/bpf/syscall.c | 11 +- kernel/bpf/verifier.c | 86 +++++++- tools/include/uapi/linux/bpf.h | 4 + .../selftests/bpf/prog_tests/call_rcu.c | 191 ++++++++++++++++++ tools/testing/selftests/bpf/progs/call_rcu.c | 89 ++++++++ .../selftests/bpf/progs/call_rcu_fail.c | 114 +++++++++++ 12 files changed, 626 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/call_rcu.c create mode 100644 tools/testing/selftests/bpf/progs/call_rcu.c create mode 100644 tools/testing/selftests/bpf/progs/call_rcu_fail.c
base-commit: 1b7415bf70be95b9a1e7e87d544867881065613f
Signature · 1 line
2.53.0-Meta
PATCH 1/4 bpf: Add bpf_call_rcu() kfunc
BPF programs that manage their own objects have no way to run their own logic once an RCU grace period has elapsed. bpf_obj_drop() defers a free, but returning an index to an allocator or unpinning a resource once readers are done has no equivalent. sched_ext's BPF library works around this today by pushing freed nodes onto a list and having a userspace thread call membarrier(MEMBARRIER_CMD_GLOBAL) and then run a BPF program to reclaim them.
Add:
int bpf_call_rcu(struct bpf_rcu_head *rh, void *map,
int (*callback)(struct bpf_map *map, void *key,
void *value));
@rh is a struct bpf_rcu_head embedded in a value of @map, so the callback runs as callback(map, key, value) for the element it lives in and needs no cookie. A head can only be armed once, which bounds outstanding work by the number of elements.
An RCU callback cannot be cancelled, so everything it touches has to stay alive until it runs:
- The callback is the program's text, so arming takes a program reference as bpf_timer, bpf_wq and bpf_task_work do, dropped once the callback returns. bpf_prog_inc_not_zero() also fails the arm with -ENOENT once the program is dying.
- The map is held by that reference through used_maps. An inner map is not, so bpf_rcu_head is rejected in one.
- The field is only accepted in BPF_MAP_TYPE_ARRAY, whose elements are never freed individually.
- The head is disarmed before the callback runs so it can be armed again from there, which takes a new program reference before the running callback drops its own. Arming therefore fails with -EPERM once the map is held by neither a process nor bpffs, the same policy bpf_timer and bpf_task_work apply.
bpf_iter hands a program a writable pointer to the live element, which would let it overwrite a queued head, so bpf_iter_attach_map() rejects maps carrying one.
The callback is verified non-sleepable even when the caller is sleepable, and RCU invokes it with BH disabled.
--- include/linux/bpf.h | 9 ++++ include/uapi/linux/bpf.h | 4 ++ kernel/bpf/btf.c | 7 +++ kernel/bpf/helpers.c | 77 +++++++++++++++++++++++++++++++ kernel/bpf/map_in_map.c | 4 ++ kernel/bpf/map_iter.c | 6 +++ kernel/bpf/syscall.c | 11 ++++- kernel/bpf/verifier.c | 83 +++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 4 ++ 9 files changed, 202 insertions(+), 3 deletions(-)
include/linux/bpf.h +9 -0
@@ -214,6 +214,7 @@ enum btf_field_type {BPF_UPTR = (1 <<11 ),BPF_RES_SPIN_LOCK = (1 <<12 ),BPF_TASK_WORK = (1 <<13 ), + BPF_RCU_HEAD = (1 << 14), }; enum bpf_cgroup_storage_type {
@@ -268,6 +269,7 @@ struct btf_record {int wq_off;int refcount_off;int task_work_off; +int rcu_head_off;struct btf_field fields[]; };
@@ -373,6 +375,8 @@ static inline const char *btf_field_type_name(enum btf_field_type type)return "bpf_refcount" ;case BPF_TASK_WORK :return "bpf_task_work" ; +case BPF_RCU_HEAD : +return "bpf_rcu_head" ;default :WARN_ON_ONCE (1 );return "unknown" ;
@@ -413,6 +417,8 @@ static inline u32 btf_field_type_size(enum btf_field_type type)return sizeof (struct bpf_refcount );case BPF_TASK_WORK :return sizeof (struct bpf_task_work ); +case BPF_RCU_HEAD : +return sizeof (struct bpf_rcu_head );default :WARN_ON_ONCE (1 );return 0 ;
@@ -447,6 +453,8 @@ static inline u32 btf_field_type_align(enum btf_field_type type)return __alignof__(struct bpf_refcount );case BPF_TASK_WORK :return __alignof__(struct bpf_task_work ); +case BPF_RCU_HEAD : +return __alignof__(struct bpf_rcu_head );default :WARN_ON_ONCE (1 );return 0 ;
@@ -479,6 +487,7 @@ static inline void bpf_obj_init_field(const struct btf_field *field, void *addr)case BPF_KPTR_PERCPU :case BPF_UPTR :case BPF_TASK_WORK : +case BPF_RCU_HEAD :break ;default :WARN_ON_ONCE (1 );
include/uapi/linux/bpf.h +4 -0
@@ -7600,6 +7600,10 @@ struct bpf_task_work {__u64 __opaque; } __attribute__((aligned(8))); +struct bpf_rcu_head { +__u64 __opaque[8 ]; +} __attribute__((aligned (8 ))); +struct bpf_wq {__u64 __opaque[2 ]; } __attribute__((aligned (8 )));
kernel/bpf/btf.c +7 -0
@@ -3695,6 +3695,7 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_ { BPF_TIMER, "bpf_timer", true }, { BPF_WORKQUEUE, "bpf_wq", true }, { BPF_TASK_WORK, "bpf_task_work", true }, + { BPF_RCU_HEAD, "bpf_rcu_head", true }, { BPF_LIST_HEAD, "bpf_list_head", false }, { BPF_LIST_NODE, "bpf_list_node", false }, { BPF_RB_ROOT, "bpf_rb_root", false },
@@ -3880,6 +3881,7 @@ static int btf_find_field_one(const struct btf *btf,case BPF_RB_NODE :case BPF_REFCOUNT :case BPF_TASK_WORK : +case BPF_RCU_HEAD : ret =btf_find_struct (btf, var_type, off, sz, field_type, info_cnt ? &info[0 ] : &tmp);if (ret <0 )
@@ -4175,6 +4177,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type rec->wq_off = -EINVAL ; rec->refcount_off = -EINVAL ; rec->task_work_off = -EINVAL ; + rec->rcu_head_off = -EINVAL ;for (i =0 ; i < cnt; i++) { field_type_size =btf_field_type_size (info_arr[i].type);if (info_arr[i].off + field_type_size > value_size) {
@@ -4218,6 +4221,10 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_typeWARN_ON_ONCE (rec->task_work_off >=0 ); rec->task_work_off = rec->fields[i].offset;break ; +case BPF_RCU_HEAD : +WARN_ON_ONCE (rec->rcu_head_off >=0 ); + rec->rcu_head_off = rec->fields[i].offset; +break ;case BPF_REFCOUNT :WARN_ON_ONCE (rec->refcount_off >=0 );/* Cache offset for faster lookup at runtime */
kernel/bpf/helpers.c +77 -0
89 lines not on this page · raw message
kernel/bpf/map_in_map.c +4 -0
@@ -25,6 +25,10 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)if (!inner_map->ops->map_meta_equal)return ERR_PTR (-ENOTSUPP ); +/* An inner map has no used_maps reference to hold it under a queued callback. */ +if (btf_record_has_field (inner_map->record,BPF_RCU_HEAD )) +return ERR_PTR (-EOPNOTSUPP ); + inner_map_meta_size =sizeof (*inner_map_meta);/* In some cases verifier needs to access beyond just base map. */ if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops)
kernel/bpf/map_iter.c +6 -0
@@ -117,6 +117,12 @@ static int bpf_iter_attach_map(struct bpf_prog *prog, goto put_map; } +/* The value ctx arg is writable and, for a non-percpu map, aliases the live element. */ +if (btf_record_has_field (map->record,BPF_RCU_HEAD )) { + err = -EOPNOTSUPP ; + goto put_map; + } +if (map->map_type ==BPF_MAP_TYPE_PERCPU_HASH || map->map_type ==BPF_MAP_TYPE_LRU_PERCPU_HASH || map->map_type ==BPF_MAP_TYPE_PERCPU_ARRAY )
kernel/bpf/syscall.c +10 -1
@@ -687,6 +687,7 @@ void btf_record_free(struct btf_record *rec)case BPF_REFCOUNT :case BPF_WORKQUEUE :case BPF_TASK_WORK : +case BPF_RCU_HEAD :/* Nothing to release */ break ;default :
@@ -741,6 +742,7 @@ struct btf_record *btf_record_dup(const struct btf_record *rec)case BPF_REFCOUNT :case BPF_WORKQUEUE :case BPF_TASK_WORK : +case BPF_RCU_HEAD :/* Nothing to acquire */ break ;default :
@@ -874,6 +876,7 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)case BPF_LIST_NODE :case BPF_RB_NODE :case BPF_REFCOUNT : +case BPF_RCU_HEAD :break ;default :WARN_ON_ONCE (1 );
@@ -1277,7 +1280,7 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token, map->record =btf_parse_fields (btf, value_type,BPF_SPIN_LOCK |BPF_RES_SPIN_LOCK |BPF_TIMER |BPF_KPTR |BPF_LIST_HEAD |BPF_RB_ROOT |BPF_REFCOUNT |BPF_WORKQUEUE |BPF_UPTR | -BPF_TASK_WORK , +BPF_TASK_WORK |BPF_RCU_HEAD , map->value_size);if (!IS_ERR_OR_NULL (map->record)) {int i;
@@ -1319,6 +1322,12 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token, goto free_map_tab; }break ; +case BPF_RCU_HEAD : +if (map->map_type !=BPF_MAP_TYPE_ARRAY ) { + ret = -EOPNOTSUPP ; + goto free_map_tab; + } +break ;case BPF_KPTR_UNREF :case BPF_KPTR_REF :case BPF_KPTR_PERCPU :
kernel/bpf/verifier.c +81 -2
185 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +4 -0
@@ -7600,6 +7600,10 @@ struct bpf_task_work {__u64 __opaque; } __attribute__((aligned(8))); +struct bpf_rcu_head { +__u64 __opaque[8 ]; +} __attribute__((aligned (8 ))); +struct bpf_wq {__u64 __opaque[2 ]; } __attribute__((aligned (8 )));
Signature · 1 line
2.53.0-Meta
PATCH 2/4 selftests/bpf: Add tests for bpf_call_rcu()
In reply to Puranjay MohanCover the callback running after a grace period with the right map, key and value, -EBUSY on a second arm, reuse of the head once disarmed, a callback arming itself again, and teardown with a callback queued.
struct bpf_rcu_head is not the first member of the map value, so the callback's recovery of the value from the head is exercised. arm() wraps both arms in an RCU read section, otherwise a grace period may elapse between them and the second one legitimately succeeds.
Negative tests: a hash map created with the same BTF, the map used as an inner map, and an iterator attach, all checked for -EOPNOTSUPP; plus verifier rejection of a mismatched map, a map with no bpf_rcu_head, a head at the wrong offset, a head on the stack, and a sleepable callback.
--- .../selftests/bpf/prog_tests/call_rcu.c | 187 ++++++++++++++++++ tools/testing/selftests/bpf/progs/call_rcu.c | 72 +++++++ .../selftests/bpf/progs/call_rcu_fail.c | 114 +++++++++++ 3 files changed, 373 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/call_rcu.c create mode 100644 tools/testing/selftests/bpf/progs/call_rcu.c create mode 100644 tools/testing/selftests/bpf/progs/call_rcu_fail.c
tools/testing/selftests/bpf/prog_tests/call_rcu.c new file +187 -0
@@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ +#include <test_progs.h> +#include "call_rcu.skel.h" +#include "call_rcu_fail.skel.h" + +struct elem { +__u64 pad; +struct bpf_rcu_head rh; +__u64 val; +}; + +/* call_rcu() is lazy on a CONFIG_RCU_LAZY kernel, so allow well over one grace period. */ +static bool wait_for_callbacks (struct call_rcu *skel,int expected) +{ +int i; + +for (i =0 ; i <3000 ; i++) { +if (READ_ONCE (skel->bss->callbacks) >= expected) +return true; +usleep (10000 ); + } +fprintf (stderr,"callbacks: got %d want %d\n" ,READ_ONCE (skel->bss->callbacks), expected); +return false; +} + +static void test_call_rcu_run (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct call_rcu *skel; +struct elem elem; +__u32 key =1 ; +int err; + + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts); +if (!ASSERT_OK (err,"test_run" ) || !ASSERT_EQ (opts.retval,0 ,"retval" )) + goto out; + +ASSERT_EQ (skel->bss->arm_err,0 ,"arm_err" ); +ASSERT_EQ (skel->bss->busy_err, -EBUSY ,"busy_err" ); + +if (!ASSERT_TRUE (wait_for_callbacks (skel,1 ),"callback_ran" )) + goto out; + +ASSERT_EQ (skel->bss->cb_key, key,"cb_key" ); +ASSERT_EQ (skel->bss->cb_val,0xdeadbeef ,"cb_val" ); +ASSERT_EQ (skel->bss->cb_max_entries,bpf_map__max_entries (skel->maps.arr),"cb_map" ); + + err =bpf_map__lookup_elem (skel->maps.arr, &key,sizeof (key), &elem,sizeof (elem),0 ); +if (ASSERT_OK (err,"lookup" )) +ASSERT_EQ (elem.val,0 ,"value_cleared" ); + +/* The head is disarmed before the callback runs, so it can be reused. */ + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts); +if (!ASSERT_OK (err,"test_run_again" )) + goto out; +ASSERT_EQ (skel->bss->arm_err,0 ,"rearm_err" ); +ASSERT_TRUE (wait_for_callbacks (skel,2 ),"callback_ran_again" ); +out: +call_rcu__destroy (skel); +} + +static void test_call_rcu_chain (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct call_rcu *skel; + + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + + skel->bss->chain =1 ; +if (!ASSERT_OK (bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts),"test_run" )) + goto out; + +ASSERT_TRUE (wait_for_callbacks (skel,2 ),"chained_callback_ran" ); +ASSERT_EQ (skel->bss->chain_err,0 ,"chain_err" ); +out: +call_rcu__destroy (skel); +} + +/* Tear down the map and the program while a callback is still queued. */ +static void test_call_rcu_teardown (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct call_rcu *skel; + + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + +/* The callback re-arms after the last user reference is gone; that must be refused. */ + skel->bss->chain =1 ; +ASSERT_OK (bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts),"test_run" ); +call_rcu__destroy (skel); +} + +static void test_call_rcu_bad_map (void ) +{ +LIBBPF_OPTS (bpf_map_create_opts, opts); +struct call_rcu *skel; +int fd; + + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + + opts.btf_fd =bpf_object__btf_fd (skel->obj); + opts.btf_key_type_id =bpf_map__btf_key_type_id (skel->maps.arr); + opts.btf_value_type_id =bpf_map__btf_value_type_id (skel->maps.arr); + + fd =bpf_map_create (BPF_MAP_TYPE_HASH ,"rcu_hash" ,sizeof (__u32), +bpf_map__value_size (skel->maps.arr),1 , &opts); +if (ASSERT_LT (fd,0 ,"hash_rejected" )) +ASSERT_EQ (fd, -EOPNOTSUPP ,"hash_errno" ); +else +close (fd); + +call_rcu__destroy (skel); +} + +/* Iterating would hand the program a writable pointer to the head. */ +static void test_call_rcu_iter (void ) +{ +LIBBPF_OPTS (bpf_iter_attach_opts, opts); +union bpf_iter_link_info linfo = {}; +struct bpf_link *link; +struct call_rcu *skel; + + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + + linfo.map.map_fd =bpf_map__fd (skel->maps.arr); + opts.link_info = &linfo; + opts.link_info_len =sizeof (linfo); + + link =bpf_program__attach_iter (skel->progs.dump, &opts); +if (!ASSERT_ERR_PTR (link,"iter_rejected" )) +bpf_link__destroy (link); +else +ASSERT_EQ (libbpf_get_error (link), -EOPNOTSUPP ,"iter_errno" ); + +call_rcu__destroy (skel); +} + +static void test_call_rcu_inner_map (void ) +{ +LIBBPF_OPTS (bpf_map_create_opts, opts); +struct call_rcu *skel; +int fd; + + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + + opts.inner_map_fd =bpf_map__fd (skel->maps.arr); + fd =bpf_map_create (BPF_MAP_TYPE_ARRAY_OF_MAPS ,"rcu_outer" , +sizeof (__u32),sizeof (__u32),1 , &opts); +if (ASSERT_LT (fd,0 ,"inner_map_rejected" )) +ASSERT_EQ (fd, -EOPNOTSUPP ,"inner_map_errno" ); +else +close (fd); + +call_rcu__destroy (skel); +} + +void test_call_rcu (void ) +{ +if (test__start_subtest ("run" )) +test_call_rcu_run (); +if (test__start_subtest ("chain" )) +test_call_rcu_chain (); +if (test__start_subtest ("teardown" )) +test_call_rcu_teardown (); +if (test__start_subtest ("hash_map" )) +test_call_rcu_bad_map (); +if (test__start_subtest ("iter" )) +test_call_rcu_iter (); +if (test__start_subtest ("inner_map" )) +test_call_rcu_inner_map (); +RUN_TESTS (call_rcu_fail); +}
tools/testing/selftests/bpf/progs/call_rcu.c new file +72 -0
72 lines not on this page · raw message
tools/testing/selftests/bpf/progs/call_rcu_fail.c new file +114 -0
114 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH 3/4 bpf: Add bpf_call_rcu_tasks_trace() kfunc
In reply to Puranjay MohanSame as bpf_call_rcu(), but waits for sleepable BPF programs too. call_rcu_tasks_trace() is call_srcu() on rcu_tasks_trace_srcu_struct and SRCU invokes callbacks with BH disabled, so the callback is still not sleepable. It does run from a kworker rather than softirq or the rcuc/rcuo kthread, so current is a kworker.
Only the queueing call differs, so the two share struct bpf_rcu_head and all of the verifier plumbing.
--- kernel/bpf/helpers.c | 57 +++++++++++++++++++++++++++++++------------ kernel/bpf/verifier.c | 5 +++- 2 files changed, 46 insertions(+), 16 deletions(-)
kernel/bpf/helpers.c +42 -15
@@ -4711,21 +4711,10 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)bpf_prog_put (prog); } -/** -* bpf_call_rcu - Invoke a BPF callback after an RCU grace period -* @rh: struct bpf_rcu_head in a BPF map value -* @map__const_map: bpf_map that embeds struct bpf_rcu_head in the values -* @callback: BPF subprogram, invoked as callback(map, key, value) for the value holding @rh -* @aux: bpf_prog_aux of the caller, implicitly set by the verifier -* -* Return: 0, -EBUSY if @rh is already queued, -EPERM if @map is held by neither a process -* nor bpffs, or -ENOENT if the calling program is going away. -*/ -__bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, -bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +static int __bpf_call_rcu (struct bpf_rcu_head *rh,struct bpf_map *map,void *callback, +struct bpf_prog_aux *aux,bool trace) {struct bpf_rcu_head_kern *rhk = (void *)rh; -struct bpf_map *map = map__const_map;struct bpf_prog *prog;BUILD_BUG_ON (sizeof (struct bpf_rcu_head_kern ) >sizeof (struct bpf_rcu_head ));
@@ -4745,13 +4734,50 @@ __bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map,return PTR_ERR (prog); } - rhk->callback_fn = (bpf_callback_t )(void *)callback; + rhk->callback_fn = (bpf_callback_t )callback; rhk->map = map; rhk->prog = prog; -call_rcu (&rhk->rcu, bpf_rcu_run_callback); +if (trace) +call_rcu_tasks_trace (&rhk->rcu, bpf_rcu_run_callback); +else +call_rcu (&rhk->rcu, bpf_rcu_run_callback);return 0 ; } +/** +* bpf_call_rcu - Invoke a BPF callback after an RCU grace period +* @rh: struct bpf_rcu_head in a BPF map value +* @map__const_map: bpf_map that embeds struct bpf_rcu_head in the values +* @callback: BPF subprogram, invoked as callback(map, key, value) for the value holding @rh +* @aux: bpf_prog_aux of the caller, implicitly set by the verifier +* +* Return: 0, -EBUSY if @rh is already queued, -EPERM if @map is held by neither a process +* nor bpffs, or -ENOENT if the calling program is going away. +*/ +__bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, +bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +{ +return __bpf_call_rcu (rh, map__const_map, callback, aux, false); +} + +/** +* bpf_call_rcu_tasks_trace - Invoke a BPF callback after an RCU tasks trace grace period +* @rh: struct bpf_rcu_head in a BPF map value +* @map__const_map: bpf_map that embeds struct bpf_rcu_head in the values +* @callback: BPF subprogram, invoked as callback(map, key, value) for the value holding @rh +* @aux: bpf_prog_aux of the caller, implicitly set by the verifier +* +* Waits for sleepable BPF programs too. The callback itself is not sleepable either way. +* +* Return: 0, -EBUSY if @rh is already queued, -EPERM if @map is held by neither a process +* nor bpffs, or -ENOENT if the calling program is going away. +*/ +__bpf_kfunc int bpf_call_rcu_tasks_trace(struct bpf_rcu_head *rh, void *map__const_map, +bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +{ +return __bpf_call_rcu (rh, map__const_map, callback, aux, true); +} +static int make_file_dynptr (struct file *file,u32 flags,bool may_sleep,struct bpf_dynptr_kern *ptr) {
@@ -5046,6 +5072,7 @@ BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE) BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_call_rcu, KF_IMPLICIT_ARGS) +BTF_ID_FLAGS(func, bpf_call_rcu_tasks_trace, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_dynptr_from_file) BTF_ID_FLAGS(func, bpf_dynptr_file_discard, KF_RELEASE)BTF_ID_FLAGS (func, bpf_timer_cancel_async)
kernel/bpf/verifier.c +4 -1
@@ -12014,6 +12014,7 @@ enum special_kfunc_type { KF_bpf_task_work_schedule_signal, KF_bpf_task_work_schedule_resume, KF_bpf_call_rcu, + KF_bpf_call_rcu_tasks_trace, KF_bpf_arena_alloc_pages, KF_bpf_arena_free_pages, KF_bpf_session_is_return,
@@ -12105,6 +12106,7 @@ BTF_ID(func, __bpf_trap) BTF_ID(func, bpf_task_work_schedule_signal) BTF_ID(func, bpf_task_work_schedule_resume) BTF_ID(func, bpf_call_rcu) +BTF_ID(func, bpf_call_rcu_tasks_trace) BTF_ID(func, bpf_arena_alloc_pages) BTF_ID(func, bpf_arena_free_pages)#ifdef CONFIG_BPF_EVENTS
@@ -12160,7 +12162,8 @@ static bool is_bpf_rbtree_add_kfunc(u32 func_id)static bool is_call_rcu_kfunc (u32 func_id) { -return func_id == special_kfunc_list[KF_bpf_call_rcu]; +return func_id == special_kfunc_list[KF_bpf_call_rcu] || + func_id == special_kfunc_list[KF_bpf_call_rcu_tasks_trace]; }static bool is_task_work_add_kfunc (u32 func_id)
Signature · 1 line
2.53.0-Meta
PATCH 4/4 selftests/bpf: Add a test for bpf_call_rcu_tasks_trace()
In reply to Puranjay MohanRun the same functional test against the tasks trace flavour.
--- .../testing/selftests/bpf/prog_tests/call_rcu.c | 12 ++++++++---- tools/testing/selftests/bpf/progs/call_rcu.c | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/call_rcu.c +8 -4
@@ -24,9 +24,10 @@ static bool wait_for_callbacks(struct call_rcu *skel, int expected)return false; } -static void test_call_rcu_run (void ) +static void test_call_rcu_run (bool trace) {LIBBPF_OPTS (bpf_test_run_opts, opts); +struct bpf_program *prog;struct call_rcu *skel;struct elem elem;__u32 key =1 ;
@@ -36,7 +37,8 @@ static void test_call_rcu_run(void)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ; - err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts); + prog = trace ? skel->progs.arm_trace : skel->progs.arm; + err =bpf_prog_test_run_opts (bpf_program__fd (prog), &opts);if (!ASSERT_OK (err,"test_run" ) || !ASSERT_EQ (opts.retval,0 ,"retval" )) goto out;
@@ -55,7 +57,7 @@ static void test_call_rcu_run(void)ASSERT_EQ (elem.val,0 ,"value_cleared" );/* The head is disarmed before the callback runs, so it can be reused. */ - err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts); + err =bpf_prog_test_run_opts (bpf_program__fd (prog), &opts);if (!ASSERT_OK (err,"test_run_again" )) goto out;ASSERT_EQ (skel->bss->arm_err,0 ,"rearm_err" );
@@ -172,7 +174,9 @@ static void test_call_rcu_inner_map(void)void test_call_rcu (void ) {if (test__start_subtest ("run" )) -test_call_rcu_run (); +test_call_rcu_run (false); +if (test__start_subtest ("run_tasks_trace" )) +test_call_rcu_run (true);if (test__start_subtest ("chain" ))test_call_rcu_chain ();if (test__start_subtest ("teardown" ))
tools/testing/selftests/bpf/progs/call_rcu.c +17 -0
@@ -65,6 +65,23 @@ int arm(void *ctx)return 0 ; } +SEC("syscall") +int arm_trace (void *ctx) +{ +__u32 key =1 ; +struct elem *e; + + e =bpf_map_lookup_elem (&arr, &key); +if (!e) +return 1 ; + + e->val =0xdeadbeef ; +/* No lock needed: the enclosing rcu_read_lock_trace() already blocks the grace period. */ + arm_err =bpf_call_rcu_tasks_trace (&e->rh, &arr, reclaim); + busy_err =bpf_call_rcu_tasks_trace (&e->rh, &arr, reclaim); +return 0 ; +} + SEC("iter/bpf_map_elem")int dump (struct bpf_iter__bpf_map_elem *ctx) {
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH bpf-next 2/4] selftests/bpf: Add tests for bpf_call_rcu()
In reply to Puranjay MohanThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incomplete test validation in `test_call_rcu_teardown`. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete test validation in test_call_rcu_teardown.
--
commit 7d7060669166bdbb9948b95d35813ccc322df204
Author: Puranjay Mohan <[email protected]>
selftests/bpf: Add tests for bpf_call_rcu()
This commit adds tests for bpf_call_rcu() to cover the callback running after a grace period, checking -EBUSY on second arm, head reuse, and teardown. It also includes negative tests for invalid map types and verifier rejections.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/call_rcu.c new file +0
-0
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/call_rcu.c new file +5
-0+/* Tear down the map and the program while a callback is still queued. */ +static void test_call_rcu_teardown (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct call_rcu *skel;
tools/testing/selftests/bpf/prog_tests/call_rcu.c line 100
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/call_rcu.c new file +10
-0+ + skel =call_rcu__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + +/* The callback re-arms after the last user reference is gone; that must be refused. */ + skel->bss->chain =1 ; +ASSERT_OK (bpf_prog_test_run_opts (bpf_program__fd (skel->progs.arm), &opts),"test_run" ); +call_rcu__destroy (skel); +}
[Severity: Medium]
Does this test actually verify that the re-arming was refused?
It looks like we set chain to 1, run the program, and then immediately destroy the skeleton without waiting for the callback to run or asserting the value of chain_err.
If a kernel regression allowed the re-arm to succeed, would this test just silently pass without catching the failure?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH bpf-next 1/4] bpf: Add bpf_call_rcu() kfunc
In reply to Puranjay MohanThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Calling `bpf_call_rcu` from NMI context causes a raw spinlock deadlock because `call_rcu()` is not…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Calling bpf_call_rcu from NMI context causes a raw spinlock deadlock because call_rcu() is not NMI-safe. --
commit 3385037afe11a72dffbb6db99b4174626596c231
Author: Puranjay Mohan <[email protected]>
bpf: Add bpf_call_rcu() kfunc
This commit adds a new bpf_call_rcu kfunc that allows BPF programs to defer operations using RCU grace periods.
Quoted 3 lines
kernel/bpf/helpers.c +0
-0
[ ... ]
Quoted 3 lines
kernel/bpf/helpers.c +3
-0+__bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, +bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +{
[ ... ]
kernel/bpf/helpers.c __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b · line 4751
Quoted 4 lines
kernel/bpf/helpers.c +4
-0+ rhk->callback_fn = (bpf_callback_t )(void *)callback; + rhk->map = map; + rhk->prog = prog; +call_rcu (&rhk->rcu, bpf_rcu_run_callback);
[Severity: High]
Does this code risk a raw spinlock deadlock if called from an NMI context?
Because bpf_call_rcu is exposed to tracing programs, it can be invoked from NMI contexts, such as when a tracing program is attached to a perf event. However, call_rcu acquires rcu_node raw spinlocks and is not NMI-safe. If the NMI interrupts a path that already holds the same rcu_node lock, the system will attempt to re-acquire the lock, resulting in a deadlock.
Should the verifier restrict this kfunc from being called in NMI contexts, similar to how it uses btf_record_has_nmi_unsafe_fields for bpf_obj_drop?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH bpf-next 1/4] bpf: Add bpf_call_rcu() kfunc
Quoted 29 lines · On Mon, Sep 7, 2026 at 3:06 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Callingbpf_call_rcufrom NMI context causes a raw spinlock deadlock becausecall_rcu()is not NMI-safe. --commit
3385037afe11a72dffbb6db99b4174626596c231
Author: Puranjay Mohan <[email protected]>bpf: Add
bpf_call_rcu()kfuncThis commit adds a new
bpf_call_rcukfunc that allows BPF programs to defer operations using RCU grace periods.Quoted 3 lines
kernel/bpf/helpers.c +0
-0[ ... ]
Quoted 3 lines
kernel/bpf/helpers.c +3
-0+__bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, +bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +{[ ... ]
kernel/bpf/helpers.c __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b · line 4751
Quoted 4 lines
kernel/bpf/helpers.c +4
-0+ rhk->callback_fn = (bpf_callback_t )(void *)callback; + rhk->map = map; + rhk->prog = prog; +call_rcu (&rhk->rcu, bpf_rcu_run_callback);[Severity: High]
Does this code risk a raw spinlock deadlock if called from an NMI context?
Quoted 8 lines
Because
bpf_call_rcuis exposed to tracing programs, it can be invoked from NMI contexts, such as when a tracing program is attached to a perf event. However,call_rcuacquiresrcu_noderaw spinlocks and is not NMI-safe. If the NMI interrupts a path that already holds the samercu_nodelock, the system will attempt to re-acquire the lock, resulting in a deadlock.Should the verifier restrict this kfunc from being called in NMI contexts, similar to how it uses
btf_record_has_nmi_unsafe_fieldsforbpf_obj_drop?
This set is supposed to land after the rcu changes that allow call_rcu to work from NMI have landed. Please see the cover letter for the link to that patchset.
Re: [PATCH bpf-next 1/4] bpf: Add bpf_call_rcu() kfunc
Quoted 37 lines · Puranjay Mohan
Quoted 35 lines · On Mon, Sep 7, 2026 at 3:06 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Callingbpf_call_rcufrom NMI context causes a raw spinlock deadlock becausecall_rcu()is not NMI-safe. --commit
3385037afe11a72dffbb6db99b4174626596c231
Author: Puranjay Mohan <[email protected]>bpf: Add
bpf_call_rcu()kfuncThis commit adds a new
bpf_call_rcukfunc that allows BPF programs to defer operations using RCU grace periods.Quoted 3 lines
kernel/bpf/helpers.c +0
-0[ ... ]
Quoted 3 lines
kernel/bpf/helpers.c +3
-0+__bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, +bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +{[ ... ]
kernel/bpf/helpers.c __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b · line 4751
Quoted 4 lines
kernel/bpf/helpers.c +4
-0+ rhk->callback_fn = (bpf_callback_t )(void *)callback; + rhk->map = map; + rhk->prog = prog; +call_rcu (&rhk->rcu, bpf_rcu_run_callback);[Severity: High]
Does this code risk a raw spinlock deadlock if called from an NMI context?Because
bpf_call_rcuis exposed to tracing programs, it can be invoked from NMI contexts, such as when a tracing program is attached to a perf event. However,call_rcuacquiresrcu_noderaw spinlocks and is not NMI-safe. If the NMI interrupts a path that already holds the samercu_nodelock, the system will attempt to re-acquire the lock, resulting in a deadlock.
Quoted 6 lines
Quoted 2 lines
Should the verifier restrict this kfunc from being called in NMI contexts, similar to how it uses
btf_record_has_nmi_unsafe_fieldsforbpf_obj_drop?This set is supposed to land after the rcu changes that allow
call_rcuto work from NMI have landed. Please see the cover letter for the link to that patchset.
The patch would need to be updated as s/call_rcu/call_rcu_nolock/ ? or more will be required?
If the former then we can land it now.
Also notice how your LLM doesn't know about your earlier work ;)
+ rcu_read_lock();
+ migrate_disable();
that should have been ...
pw-bot: cr
Re: [PATCH bpf-next 1/4] bpf: Add bpf_call_rcu() kfunc
Quoted 42 lines · Alexei Starovoitov
Quoted 40 lines · Puranjay Mohan
Quoted 37 lines · On Mon, Sep 7, 2026 at 3:06 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Callingbpf_call_rcufrom NMI context causes a raw spinlock deadlock becausecall_rcu()is not NMI-safe. --commit
3385037afe11a72dffbb6db99b4174626596c231
Author: Puranjay Mohan <[email protected]>bpf: Add
bpf_call_rcu()kfuncThis commit adds a new
bpf_call_rcukfunc that allows BPF programs to defer operations using RCU grace periods.Quoted 3 lines
kernel/bpf/helpers.c +0
-0[ ... ]
Quoted 3 lines
kernel/bpf/helpers.c +3
-0+__bpf_kfunc int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, +bpf_rcu_callback_t callback,struct bpf_prog_aux *aux) +{[ ... ]
kernel/bpf/helpers.c __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b · line 4751
Quoted 4 lines
kernel/bpf/helpers.c +4
-0+ rhk->callback_fn = (bpf_callback_t )(void *)callback; + rhk->map = map; + rhk->prog = prog; +call_rcu (&rhk->rcu, bpf_rcu_run_callback);[Severity: High]
Does this code risk a raw spinlock deadlock if called from an NMI context?Because
bpf_call_rcuis exposed to tracing programs, it can be invoked from NMI contexts, such as when a tracing program is attached to a perf event. However,call_rcuacquiresrcu_noderaw spinlocks and is not NMI-safe. If the NMI interrupts a path that already holds the samercu_nodelock, the system will attempt to re-acquire the lock, resulting in a deadlock.Should the verifier restrict this kfunc from being called in NMI contexts, similar to how it uses
btf_record_has_nmi_unsafe_fieldsforbpf_obj_drop?
Quoted 7 lines
Quoted 3 lines
This set is supposed to land after the rcu changes that allow
call_rcuto work from NMI have landed. Please see the cover letter for the link to that patchset.The patch would need to be updated as s/call_rcu/call_rcu_nolock/ ? or more will be required?
If the former then we can land it now.
There will be no call_rcu_nolock(), we decided to make the standard call_rcu() be safe to be called from anywhere, so no change is required to this set. But it needs to land after call_rcu() changes land, or maybe rcu and bpf tree can co-ordinate somehow?
Quoted 6 lines
Also notice how your LLM doesn't know about your earlier work ;)
+rcu_read_lock();
+migrate_disable();that should have been ...
You mean rcu_read_lock_dont_migrate(); right ? Will change in v2.