fprobe, rcu/tasks, rhashtable: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU
Subsystems (7)
Masami Hiramatsu (Google)
maintainercover fprobe, rcu/tasks, rhashtable: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU
Hi,
Here is an RFC patch series which removes standard RCU read lock (guard(rcu) and rcu_read_lock()) from fprobe callback paths by switching fprobe and BPF multi-kprobe to Tasks-Rude RCU.
Motivation & Problem
====================
Currently, fprobe entry and exit callbacks (fprobe_fgraph_entry, fprobe_return, and fprobe_ftrace_entry) execute in the tracing hot path where preemption is disabled.
However, fprobe was forced to wrap hash lookups with guard(rcu)() and rcu_read_lock() because:
- rhashtable defers bucket table deallocation using standard RCU
- unregister_fprobe() and BPF waited for a standard RCU grace period
Taking standard RCU read locks in the tracing fast path introduces several drawbacks:
1. Unnecessary Runtime Overhead:
Every probe hit manipulates current->rcu_read_lock_nesting with memory barriers, and rcu_read_unlock() adds conditional branches to check for special quiescent processing.
On debug kernels with CONFIG_PROVE_RCU=y or CONFIG_LOCKDEP=y, this additionally acquires and releases lockdep maps on every hit, introducing severe lockdep hashing overhead and tracer recursion risks.
2. Fragile Dependency on rcu_is_watching():
Standard RCU treats idle CPUs (and user-space on nohz_full CPUs) as Extended Quiescent States (EQS). If a function is traced while rcu_is_watching() is false, standard RCU is blind to the read-side critical section. In such contexts, synchronize_rcu() does not wait for the reader (risking Use-After-Free), and lockdep emits an "RCU-illegal: rcu_read_lock() used while not watching!" warning. To avoid this, ftrace callbacks normally require FTRACE_OPS_FL_RCU, adding extra trampoline check overhead.
3. Asymmetric Synchronization with fprobe_return():
fprobe_return() executes under preempt_disable_notrace() without holding rcu_read_lock(). Prior to this series, unregister_fprobe() only waited on synchronize_rcu(), which does NOT wait for pure preempt-disabled sections, leaving a potential Use-After-Free window during probe unregistration.
I've tried to fix the last UAF with simply introducing guard(rcu)()[1] but Sashiko found the 2nd problem [2]. So I decided to implement this series.
[1] https://lore.kernel.org/all/179055575009.241711.6358052647499787191.stgit@devnote2/
[2] https://lore.kernel.org/all/[email protected]/
Solution: Tasks-Rude RCU
========================
Because fprobe callbacks already run strictly within preempt-disabled contexts, we can transition fprobe and its deferred table reclamation to Tasks-Rude RCU:
- Tasks-Rude RCU detects grace periods via schedule_on_each_cpu(), forcing a schedule on every online CPU. This guarantees that all preempt-disabled sections that began prior to the grace period have completed before memory is reclaimed.
- Unlike standard RCU, Tasks-Rude RCU does not rely on dyntick-idle / EQS tracking. It does NOT require rcu_is_watching() to be true and does not trigger lockdep warnings in pre-RCU/idle execution paths.
- Within fprobe, guard(rcu)() is replaced with guard(rcu_sched_notrace)(), reducing the lookup lock to pure, non-tracing preempt counter increments without lockdep or RCU state manipulation.
Feedback and suggestions from RCU, BPF, and tracing maintainers are welcome!
Thank you,
---
base-commit: 5bfa9f1a9dcb6ecb607adbc1c0226605c972935b
Masami Hiramatsu (Google) (3):
rcu/tasks: Export call_rcu_tasks_rude()
rhashtable: Add use_tasks_rude parameter to defer bucket table free
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU
include/linux/rcupdate.h | 6 +++ include/linux/rhashtable-types.h | 2 + kernel/bpf/syscall.c | 2 + kernel/rcu/tasks.h | 8 ++--- kernel/trace/fprobe.c | 66 +++++++++++++++++++++----------------- lib/rhashtable.c | 5 ++- 6 files changed, 54 insertions(+), 35 deletions(-)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRFC PATCH 1/3 rcu/tasks: Export call_rcu_tasks_rude()
From: Masami Hiramatsu (Google) <[email protected]>
Currently call_rcu_tasks_rude() is static and private to kernel/rcu/tasks.h, while synchronize_rcu_tasks_rude() is exported. Subsystems that execute handlers under preempt_disable() (such as fprobe and BPF kprobe-multi) cannot safely wait on synchronize_rcu_tasks_rude() in atomic or asynchronous contexts without an asynchronous call_rcu_tasks_rude() variant.
Make call_rcu_tasks_rude() non-static, declare it in include/linux/rcupdate.h, and export it via EXPORT_SYMBOL_GPL(). Also provide fallbacks to call_rcu when Tasks Rude RCU is not configured.
--- include/linux/rcupdate.h | 6 ++++++ kernel/rcu/tasks.h | 8 +++----- 2 files changed, 9 insertions(+), 5 deletions(-)
include/linux/rcupdate.h +6 -0
@@ -198,7 +198,11 @@ void rcu_tasks_torture_stats_print(char *tt, char *tf);# ifdef CONFIG_TASKS_RUDE_RCU void synchronize_rcu_tasks_rude (void ); +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func);void rcu_tasks_rude_torture_stats_print (char *tt,char *tf); +# else +# define call_rcu_tasks_rude call_rcu +# define synchronize_rcu_tasks_rude synchronize_rcu# endif #define rcu_note_voluntary_context_switch (t) rcu_tasks_qs(t, false)
@@ -210,6 +214,8 @@ void exit_tasks_rcu_finish(void);#define rcu_note_voluntary_context_switch (t) do { } while (0)#define call_rcu_tasks call_rcu#define synchronize_rcu_tasks synchronize_rcu +#define call_rcu_tasks_rude call_rcu +#define synchronize_rcu_tasks_rude synchronize_rcustatic inline void exit_tasks_rcu_start (void ) { }static inline void exit_tasks_rcu_finish (void ) { }#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
kernel/rcu/tasks.h +3 -5
@@ -1355,7 +1355,7 @@ static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)schedule_on_each_cpu (rcu_tasks_be_rude); } -static void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func); +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func);DEFINE_RCU_TASKS (rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,"RCU Tasks Rude" );
@@ -1376,14 +1376,12 @@ DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,* * See the description of call_rcu() for more detailed information on * memory ordering guarantees. -* -* This is no longer exported, and is instead reserved for use by -* synchronize_rcu_tasks_rude(). */ -static void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func) +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func) {call_rcu_tasks_generic (rhp, func, &rcu_tasks_rude); } +EXPORT_SYMBOL_GPL (call_rcu_tasks_rude);/** * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
[email protected]
AI reviewRe: [RFC PATCH 1/3] rcu/tasks: Export call_rcu_tasks_rude()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] rcu: missing rcu_barrier_tasks_rude() causes UAF on module unload --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rcu: missing rcu_barrier_tasks_rude() causes UAF on module unload --
commit ba4806c534fb61d71e190abff5c0d237c0eb2648
Author: Masami Hiramatsu (Google) <[email protected]>
rcu/tasks: Export call_rcu_tasks_rude()
This commit exports call_rcu_tasks_rude() so that subsystems executing handlers under preempt_disable() can safely use an asynchronous variant. It also provides fallbacks to call_rcu when Tasks Rude RCU is not configured.
include/linux/rcupdate.h void rcu_tasks_torture_stats_print(char *tt, char *tf); · line 201
Quoted 8 lines
include/linux/rcupdate.h +1
-0@@ -198,7 +198,11 @@ void rcu_tasks_torture_stats_print(char *tt, char *tf);# ifdef CONFIG_TASKS_RUDE_RCU void synchronize_rcu_tasks_rude (void ); +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func);
[Severity: High]
Since the asynchronous callback API is being exposed for external use, does there also need to be an rcu_barrier_tasks_rude() declared so that modules can safely tear down and wait for pending callbacks?
include/linux/rcupdate.h void rcu_tasks_torture_stats_print(char *tt, char *tf); · line 205
Quoted 4 lines
include/linux/rcupdate.h +3
-0void rcu_tasks_rude_torture_stats_print (char *tt,char *tf); +# else +# define call_rcu_tasks_rude call_rcu +# define synchronize_rcu_tasks_rude synchronize_rcu
[Severity: High]
If a barrier function is introduced, will this fallback block also need a corresponding fallback definition like
#define rcu_barrier_tasks_rude rcu_barrier?
Quoted 1 line
include/linux/rcupdate.h +0
-0# endif
[ ... ]
Quoted 3 lines
kernel/rcu/tasks.h +0
-0
[ ... ]
kernel/rcu/tasks.h DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude, · line 1384
Quoted 6 lines
kernel/rcu/tasks.h +2
-1-static void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func) +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func) {call_rcu_tasks_generic (rhp, func, &rcu_tasks_rude); } +EXPORT_SYMBOL_GPL (call_rcu_tasks_rude);
[Severity: High]
If a loadable module uses this API with a callback pointing into its own text section, how does its module_exit function wait for the callback to finish executing before the module unloads? Without an exported barrier function, could this lead to a use-after-free or invalid instruction execution when the pending callback is invoked in freed or unmapped memory?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/179060575297.372125.10581329083761385407.stgit@devnote2?part=1
[email protected]
AI reviewRe: [RFC PATCH 1/3] rcu/tasks: Export call_rcu_tasks_rude()
In reply to Masami Hiramatsu (Google)Do call_rcu_tasks_rude() and synchronize_rcu_tasks_rude() actually provide the same grace period guarantee?
Quoted 16 lines
include/linux/rcupdate.h +4
-0@@ -198,7 +198,11 @@ void rcu_tasks_torture_stats_print(char *tt, char *tf);# ifdef CONFIG_TASKS_RUDE_RCU void synchronize_rcu_tasks_rude (void ); +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func);void rcu_tasks_rude_torture_stats_print (char *tt,char *tf); +# else +# define call_rcu_tasks_rude call_rcu +# define synchronize_rcu_tasks_rude synchronize_rcu# endif #define rcu_note_voluntary_context_switch (t) rcu_tasks_qs(t, false)
[ ... ]
Quoted 4 lines
kernel/rcu/tasks.h +0
-0
[ ... ]
Quoted 4 lines
kernel/rcu/tasks.h +0
-0@@ -1376,14 +1376,12 @@ DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,* * See the description of call_rcu() for more detailed information on * memory ordering guarantees.
kernel/rcu/tasks.h DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude, · line 1384
Quoted 10 lines
kernel/rcu/tasks.h +2
-4-* -* This is no longer exported, and is instead reserved for use by -* synchronize_rcu_tasks_rude(). */ -static void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func) +void call_rcu_tasks_rude (struct rcu_head *rhp,rcu_callback_t func) {call_rcu_tasks_generic (rhp, func, &rcu_tasks_rude); } +EXPORT_SYMBOL_GPL (call_rcu_tasks_rude);
Do call_rcu_tasks_rude() and synchronize_rcu_tasks_rude() actually provide the same grace period guarantee?
On architectures that select ARCH_WANTS_NO_INSTR (x86, arm64, riscv, s390, loongarch), synchronize_rcu_tasks_rude() returns without waiting at all unless CONFIG_FORCE_TASKS_RUDE_RCU is set:
kernel/rcu/tasks.h:synchronize_rcu_tasks_rude() {
if (!IS_ENABLED(CONFIG_ARCH_WANTS_NO_INSTR) ||
IS_ENABLED(CONFIG_FORCE_TASKS_RUDE_RCU))
synchronize_rcu_tasks_generic(&rcu_tasks_rude);
}That option depends on RCU_EXPERT and defaults to n.
But call_rcu_tasks_rude() never takes that shortcut. Every callback goes through this path:
call_rcu_tasks_rude()
-> call_rcu_tasks_generic()
-> rcu_tasks_one_gp()
-> rcu_tasks_rude_wait_gp()
-> schedule_on_each_cpu()which always waits.
The rest of the series relies on these two functions being equivalent. Commit f0b3c7fc5f82 ("fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU") replaces synchronize_rcu() with synchronize_rcu_tasks_rude() in unregister_fprobe() and in the error path of register_fprobe_ips().
On a default x86 or arm64 build (FPROBE selects FUNCTION_GRAPH_TRACER, which depends on FUNCTION_TRACER, which selects TASKS_RUDE_RCU), unregister_fprobe() then returns without waiting for handlers that are still running. Example path:
trace_fprobe_release()
-> unregister_trace_fprobe()
-> __unregister_trace_fprobe()
-> unregister_fprobe(&tf->fp)
-> unregister_fprobe_async() + synchronize_rcu_tasks_rude() [no-op]
-> free_trace_fprobe(tf)
-> kfree(tf)Meanwhile another CPU can be inside fprobe_ftrace_entry() or fprobe_return(), holding fp from READ_ONCE(node->fp) or from the shadow stack, and calling fp->entry_handler / fp->exit_handler, which uses container_of() to reach the freed trace_fprobe. The same thing happens when the samples/fprobe module unloads after unregister_fprobe().
Before the series, synchronize_rcu() waited for these preempt-disabled sections, because consolidated RCU waits for preempt-disabled regions where RCU is watching.
Other points:
(1) The commit message says callers "cannot safely wait on synchronize_rcu_tasks_rude()" and need an async variant. On these architectures that wait is a no-op, so the premise is wrong, and the synchronize_rcu_tasks_rude() kerneldoc ("Control will return to the caller some time after a rude rcu-tasks grace period has elapsed") does not match its implementation.
(2) Every grace period needed by a call_rcu_tasks_rude() callback now queues work on and context-switches every online CPU, including idle and nohz_full CPUs. For the synchronous API that cost was deliberately removed on these architectures. Once the series moves fprobe/kprobe-multi teardown and rhashtable resizing onto this API, the cost is paid on every teardown and resize.
(3) The removed comment ("This is no longer exported, and is instead reserved for use by synchronize_rcu_tasks_rude()") recorded a deliberate decision to retire the async Rude API. The changelog does not explain why that decision is being reversed.
Options include making call_rcu_tasks_rude() match synchronize_rcu_tasks_rude(), for example falling back to call_rcu() under the same ARCH_WANTS_NO_INSTR condition. Alternatively the series could keep using call_rcu()/synchronize_rcu(): these already cover preempt-disabled readers on noinstr architectures, and rcu_read_lock_sched_notrace() can replace rcu_read_lock() in the handlers.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/36438713281
Masami Hiramatsu (Google)
maintainerRFC PATCH 2/3 rhashtable: Add use_tasks_rude parameter to defer bucket table free
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Currently rhashtable always uses call_rcu() to free old bucket tables upon table resizing. However, some callers (such as fprobe) operate under preempt_disable() without holding rcu_read_lock(), relying on Tasks Rude RCU grace periods instead of standard RCU.
Add a use_tasks_rude boolean flag to struct rhashtable_params. When enabled, rhashtable_rehash_table() frees old bucket tables using call_rcu_tasks_rude() instead of call_rcu(). Existing callers continue to default to call_rcu().
--- include/linux/rhashtable-types.h | 2 ++ lib/rhashtable.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/rhashtable-types.h +2 -0
@@ -52,6 +52,7 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,* @min_size: Minimum size while shrinking * @insecure_elasticity: Set to true to disable chain length checks * @automatic_shrinking: Enable automatic shrinking of tables +* @use_tasks_rude: Use call_rcu_tasks_rude() to free bucket tables * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) * @obj_hashfn: Function to hash object * @obj_cmpfn: Function to compare key with object
@@ -65,6 +66,7 @@ struct rhashtable_params {u16 min_size;bool insecure_elasticity;bool automatic_shrinking; +bool use_tasks_rude;rht_hashfn_t hashfn;rht_obj_hashfn_t obj_hashfn;rht_obj_cmpfn_t obj_cmpfn;
lib/rhashtable.c +4 -1
@@ -359,7 +359,10 @@ static int rhashtable_rehash_table(struct rhashtable *ht)* rhashtable_walk_stop() can use rcu_head_after_call_rcu() * to check if it should not re-link the table. */ -call_rcu (&old_tbl->rcu, bucket_table_free_rcu); +if (ht->p.use_tasks_rude) +call_rcu_tasks_rude (&old_tbl->rcu, bucket_table_free_rcu); +else +call_rcu (&old_tbl->rcu, bucket_table_free_rcu);spin_unlock (&ht->lock);return rht_dereference (new_tbl->future_tbl, ht) ? -EAGAIN :0 ;
[email protected]
AI reviewRe: [RFC PATCH 2/3] rhashtable: Add use_tasks_rude parameter to defer bucket table free
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] rhashtable: UAF in standard walkers and inserts due to Tasks Rude RCU mismatch --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rhashtable: UAF in standard walkers and inserts due to Tasks Rude RCU mismatch --
commit 72dce4af7330e16ac9fafbde35eaffd5f5d340ca
Author: Masami Hiramatsu (Google) <[email protected]>
rhashtable: Add use_tasks_rude parameter to defer bucket table free
This commit adds a use_tasks_rude boolean flag to struct rhashtable_params. When enabled, old bucket tables are freed using call_rcu_tasks_rude() instead of call_rcu() to support callers that operate under preempt_disable() without holding rcu_read_lock().
lib/rhashtable.c static int rhashtable_rehash_table(struct rhashtable *ht) · line 363
Quoted 11 lines
lib/rhashtable.c +2
-1@@ -359,7 +359,10 @@ static int rhashtable_rehash_table(struct rhashtable *ht)* rhashtable_walk_stop() can use rcu_head_after_call_rcu() * to check if it should not re-link the table. */ -call_rcu (&old_tbl->rcu, bucket_table_free_rcu); +if (ht->p.use_tasks_rude) +call_rcu_tasks_rude (&old_tbl->rcu, bucket_table_free_rcu);
[Severity: High]
Does this create a use-after-free for standard readers when CONFIG_PREEMPT_RCU=y is enabled?
Standard rhashtable operations like __rhashtable_insert_fast() and rhashtable_walk_start_check() acquire standard RCU read locks:
lib/rhashtable.c:rhashtable_walk_start_check() {
...
rcu_read_lock();
iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
...
}include/linux/rhashtable.h:__rhashtable_insert_fast() {
...
rcu_read_lock();
tbl = rht_dereference_rcu(ht->tbl, ht);
...
}Under CONFIG_PREEMPT_RCU, rcu_read_lock() does not disable preemption. If a task is preempted during an insert or walk, a Tasks Rude RCU grace period can complete while the preempted task is still accessing the old bucket table. Since Tasks Rude RCU only waits for preempt-disabled regions and not standard RCU read-side critical sections, the bucket table could be freed before the preempted standard reader finishes.
Quoted 5 lines
lib/rhashtable.c +2
-0+else +call_rcu (&old_tbl->rcu, bucket_table_free_rcu);spin_unlock (&ht->lock);return rht_dereference (new_tbl->future_tbl, ht) ? -EAGAIN :0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/179060575297.372125.10581329083761385407.stgit@devnote2?part=2
Masami Hiramatsu (Google)
maintainerRFC PATCH 3/3 fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Currently, fprobe entry and exit callbacks are called from the tracing path where preemption is disabled. However, because rhashtable deferred bucket table reclamation using standard RCU and unregister_fprobe() waited for standard RCU grace periods, guard(rcu)() and rcu_read_lock() were used around hash lookups. Calling rcu_read_lock() in the trace path introduces unnecessary overhead and potential recursion risks.
Furthermore, BPF_LINK_TYPE_KPROBE_MULTI attaches to fprobe via register_fprobe_ips() and unregisters it asynchronously via unregister_fprobe_async(), relying on bpf_link_free() to wait for an RCU grace period before freeing the link structure.
If fprobe switches to Tasks-Rude RCU without simultaneously updating BPF, a Use-After-Free race window opens during bpf_link_free() because standard RCU grace periods do not wait for pure preempt-disabled execution contexts to complete.
Atomically switch both fprobe and BPF kprobe-multi to Tasks-Rude RCU.
--- kernel/bpf/syscall.c | 2 + kernel/trace/fprobe.c | 66 +++++++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 29 deletions(-)
kernel/bpf/syscall.c +2 -0
@@ -3350,6 +3350,8 @@ static void bpf_link_free(struct bpf_link *link)/* We need to do a SRCU grace period wait for non-faultable tracepoint BPF links. */ else if (bpf_link_is_tracepoint(link)) call_tracepoint_unregister_atomic(&link->rcu, bpf_link_defer_dealloc_rcu_gp); + else if (link->type == BPF_LINK_TYPE_KPROBE_MULTI) + call_rcu_tasks_rude(&link->rcu, bpf_link_defer_dealloc_rcu_gp); else call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp); }else if (ops->dealloc) {
kernel/trace/fprobe.c +37 -29
145 lines not on this page · raw message
[email protected]
AI reviewRe: [RFC PATCH 3/3] fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] trace: Use-After-Free in fprobe_module_callback during rhashtable walk - [Low] trace: sparse…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] trace: Use-After-Free in fprobe_module_callback during rhashtable walk
- [Low] trace: sparse context imbalance due to missing RCU_SCHED annotation --
commit b3e36a52547a2b577250e858535265a49a8ab8b5
Author: Masami Hiramatsu (Google) <[email protected]>
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCU
This commit switches fprobe entry/exit callbacks and BPF kprobe-multi asynchronous unregistration from standard RCU to Tasks-Rude RCU. This prevents potential use-after-free races during RCU grace periods by waiting for preempt-disabled execution contexts to complete.
Quoted 4 lines
kernel/trace/fprobe.c +0
-0
[ ... ]
kernel/trace/fprobe.c static const struct rhashtable_params fprobe_rht_params = { · line 79
Quoted 5 lines
kernel/trace/fprobe.c +1
-0@@ -76,8 +76,14 @@ static const struct rhashtable_params fprobe_rht_params = { .obj_hashfn = fprobe_node_obj_hashfn, .obj_cmpfn = fprobe_node_cmp, .automatic_shrinking = true, + .use_tasks_rude = true,
[Severity: High]
Does setting .use_tasks_rude = true introduce a use-after-free race during module unload?
Since deferred bucket table reclamation now uses call_rcu_tasks_rude(), the table walk in fprobe_module_callback() might not have adequate reader protection:
kernel/trace/fprobe.c:fprobe_module_callback() {
...
rhashtable_walk_start(&iter);
while ((node = rhashtable_walk_next(&iter)) && !IS_ERR(node))
if (fprobe_remove_node_in_module(mod, node, &alist) < 0) {
...
}rhashtable_walk_start() solely relies on rcu_read_lock(). Under CONFIG_PREEMPT_RCU, rcu_read_lock() does not disable preemption, meaning a preemption during the walk satisfies the Tasks-Rude RCU quiescent state.
If the walker is preempted, could a concurrent background resize complete its grace period and free the bucket table, causing rhashtable_walk_next() to dereference freed memory when the walker is scheduled back?
kernel/trace/fprobe.c static const struct rhashtable_params fprobe_rht_params = { · line 85
Quoted 6 lines
kernel/trace/fprobe.c +4
-0}; +DEFINE_LOCK_GUARD_0 (rcu_sched_notrace,rcu_read_lock_sched_notrace (), + rcu_read_unlock_sched_notrace()) +DECLARE_LOCK_GUARD_0_ATTRS (rcu_sched_notrace,__acquires_shared (RCU ), +__releases_shared (RCU ))
[Severity: Low]
Does the rcu_sched_notrace lock guard declaration omit the RCU_SCHED context?
rcu_read_lock_sched_notrace() acquires both RCU and RCU_SCHED, but this DECLARE_LOCK_GUARD_0_ATTRS macro only declares __acquires_shared(RCU) and __releases_shared(RCU).
Could this cause sparse context imbalance warnings due to the missing __acquires_shared(RCU_SCHED) and __releases_shared(RCU_SCHED) annotations?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/179060575297.372125.10581329083761385407.stgit@devnote2?part=3
Paul E. McKenney
maintainerQuoted 47 lines · Masami Hiramatsu (Google)
Hi,
Here is an RFC patch series which removes standard RCU read lock (guard(rcu) and
rcu_read_lock()) from fprobe callback paths by switching fprobe and BPF multi-kprobe to Tasks-Rude RCU.Motivation & Problem
====================Currently, fprobe entry and exit callbacks (
fprobe_fgraph_entry,fprobe_return, andfprobe_ftrace_entry) execute in the tracing hot path where preemption is disabled.However, fprobe was forced to wrap hash lookups with guard(rcu)() and
rcu_read_lock()because:- rhashtable defers bucket table deallocation using standard RCU
-unregister_fprobe()and BPF waited for a standard RCU grace periodTaking standard RCU read locks in the tracing fast path introduces several drawbacks:
1. Unnecessary Runtime Overhead:
Every probe hit manipulatescurrent->rcu_read_lock_nestingwith memory barriers, andrcu_read_unlock()adds conditional branches to check for special quiescent processing.
On debug kernels withCONFIG_PROVE_RCU=yorCONFIG_LOCKDEP=y, this additionally acquires and releases lockdep maps on every hit, introducing severe lockdep hashing overhead and tracer recursion risks.2. Fragile Dependency on
rcu_is_watching():
Standard RCU treats idle CPUs (and user-space onnohz_fullCPUs) as Extended Quiescent States (EQS). If a function is traced whilercu_is_watching()is false, standard RCU is blind to the read-side critical section. In such contexts,synchronize_rcu()does not wait for the reader (risking Use-After-Free), and lockdep emits an "RCU-illegal:rcu_read_lock()used while not watching!" warning. To avoid this, ftrace callbacks normally requireFTRACE_OPS_FL_RCU, adding extra trampoline check overhead.3. Asymmetric Synchronization with
fprobe_return():
fprobe_return()executes underpreempt_disable_notrace()without holdingrcu_read_lock(). Prior to this series,unregister_fprobe()only waited onsynchronize_rcu(), which does NOT wait for pure preempt-disabled sections, leaving a potential Use-After-Free window during probe unregistration.
Quoted 3 lines
I've tried to fix the last UAF with simply introducing guard(rcu)()[1] but Sashiko found the 2nd problem [2]. So I decided to implement this series.
Unless I am missing something subtle, Sashiko needs to be taught a little bit more about RCU. Preemption-disabled regions of code really are valid RCU readers. If you have a reproducer showing that this is not the case in some situation, that would be a bug in RCU.
Adding Roman Gushchin and Chris Mason on CC for their thoughts.
Thanx, Paul
Quoted 45 lines
[1] https://lore.kernel.org/all/179055575009.241711.6358052647499787191.stgit@devnote2/
[2] https://lore.kernel.org/all/[email protected]/Solution: Tasks-Rude RCU
========================Because fprobe callbacks already run strictly within preempt-disabled contexts, we can transition fprobe and its deferred table reclamation to Tasks-Rude RCU:
- Tasks-Rude RCU detects grace periods via
schedule_on_each_cpu(), forcing a schedule on every online CPU. This guarantees that all preempt-disabled sections that began prior to the grace period have completed before memory is reclaimed.
- Unlike standard RCU, Tasks-Rude RCU does not rely on dyntick-idle / EQS tracking. It does NOT requirercu_is_watching()to be true and does not trigger lockdep warnings in pre-RCU/idle execution paths.
- Within fprobe, guard(rcu)() is replaced with guard(rcu_sched_notrace)(), reducing the lookup lock to pure, non-tracing preempt counter increments without lockdep or RCU state manipulation.Feedback and suggestions from RCU, BPF, and tracing maintainers are welcome!
Thank you,
---
base-commit:5bfa9f1a9dcb6ecb607adbc1c0226605c972935bMasami Hiramatsu (Google) (3):
rcu/tasks: Exportcall_rcu_tasks_rude()
rhashtable: Adduse_tasks_rudeparameter to defer bucket table free
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCUinclude/linux/rcupdate.h | 6 +++ include/linux/rhashtable-types.h | 2 + kernel/bpf/syscall.c | 2 + kernel/rcu/tasks.h | 8 ++--- kernel/trace/fprobe.c | 66 +++++++++++++++++++++----------------- lib/rhashtable.c | 5 ++- 6 files changed, 54 insertions(+), 35 deletions(-)Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerQuoted 48 lines · On Mon, 28 Sep 2026 09:25:02 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 47 lines · Masami Hiramatsu (Google)
Hi,
Here is an RFC patch series which removes standard RCU read lock (guard(rcu) and
rcu_read_lock()) from fprobe callback paths by switching fprobe and BPF multi-kprobe to Tasks-Rude RCU.Motivation & Problem
====================Currently, fprobe entry and exit callbacks (
fprobe_fgraph_entry,fprobe_return, andfprobe_ftrace_entry) execute in the tracing hot path where preemption is disabled.However, fprobe was forced to wrap hash lookups with guard(rcu)() and
rcu_read_lock()because:- rhashtable defers bucket table deallocation using standard RCU
-unregister_fprobe()and BPF waited for a standard RCU grace periodTaking standard RCU read locks in the tracing fast path introduces several drawbacks:
1. Unnecessary Runtime Overhead:
Every probe hit manipulatescurrent->rcu_read_lock_nestingwith memory barriers, andrcu_read_unlock()adds conditional branches to check for special quiescent processing.
On debug kernels withCONFIG_PROVE_RCU=yorCONFIG_LOCKDEP=y, this additionally acquires and releases lockdep maps on every hit, introducing severe lockdep hashing overhead and tracer recursion risks.2. Fragile Dependency on
rcu_is_watching():
Standard RCU treats idle CPUs (and user-space onnohz_fullCPUs) as Extended Quiescent States (EQS). If a function is traced whilercu_is_watching()is false, standard RCU is blind to the read-side critical section. In such contexts,synchronize_rcu()does not wait for the reader (risking Use-After-Free), and lockdep emits an "RCU-illegal:rcu_read_lock()used while not watching!" warning. To avoid this, ftrace callbacks normally requireFTRACE_OPS_FL_RCU, adding extra trampoline check overhead.3. Asymmetric Synchronization with
fprobe_return():
fprobe_return()executes underpreempt_disable_notrace()without holdingrcu_read_lock(). Prior to this series,unregister_fprobe()only waited onsynchronize_rcu(), which does NOT wait for pure preempt-disabled sections, leaving a potential Use-After-Free window during probe unregistration.
Quoted 8 lines
Quoted 3 lines
I've tried to fix the last UAF with simply introducing guard(rcu)()[1] but Sashiko found the 2nd problem [2]. So I decided to implement this series.
Unless I am missing something subtle, Sashiko needs to be taught a little bit more about RCU. Preemption-disabled regions of code really are valid RCU readers. If you have a reproducer showing that this is not the case in some situation, that would be a bug in RCU.
Thanks for the clarification!
So I think we can replace rcu_read_lock() with rcu_read_lock_sched_notrace()? (also, maybe we need to add rcu_is_watching() check)
Current code may involves lockdep in fprobe, that is not good.
BTW, if we want to trace some code in extended quiescent state, do we still need this Tasks-Rude RCU?
Thank you,
Quoted 50 lines
Adding Roman Gushchin and Chris Mason on CC for their thoughts.
Thanx, Paul
Quoted 45 lines
[1] https://lore.kernel.org/all/179055575009.241711.6358052647499787191.stgit@devnote2/
[2] https://lore.kernel.org/all/[email protected]/Solution: Tasks-Rude RCU
========================Because fprobe callbacks already run strictly within preempt-disabled contexts, we can transition fprobe and its deferred table reclamation to Tasks-Rude RCU:
- Tasks-Rude RCU detects grace periods via
schedule_on_each_cpu(), forcing a schedule on every online CPU. This guarantees that all preempt-disabled sections that began prior to the grace period have completed before memory is reclaimed.
- Unlike standard RCU, Tasks-Rude RCU does not rely on dyntick-idle / EQS tracking. It does NOT requirercu_is_watching()to be true and does not trigger lockdep warnings in pre-RCU/idle execution paths.
- Within fprobe, guard(rcu)() is replaced with guard(rcu_sched_notrace)(), reducing the lookup lock to pure, non-tracing preempt counter increments without lockdep or RCU state manipulation.Feedback and suggestions from RCU, BPF, and tracing maintainers are welcome!
Thank you,
---
base-commit:5bfa9f1a9dcb6ecb607adbc1c0226605c972935bMasami Hiramatsu (Google) (3):
rcu/tasks: Exportcall_rcu_tasks_rude()
rhashtable: Adduse_tasks_rudeparameter to defer bucket table free
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCUinclude/linux/rcupdate.h | 6 +++ include/linux/rhashtable-types.h | 2 + kernel/bpf/syscall.c | 2 + kernel/rcu/tasks.h | 8 ++--- kernel/trace/fprobe.c | 66 +++++++++++++++++++++----------------- lib/rhashtable.c | 5 ++- 6 files changed, 54 insertions(+), 35 deletions(-)Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Paul E. McKenney
maintainerQuoted 55 lines · Masami Hiramatsu
Quoted 52 lines · On Mon, 28 Sep 2026 09:25:02 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 50 lines · Masami Hiramatsu (Google)
Hi,
Here is an RFC patch series which removes standard RCU read lock (guard(rcu) and
rcu_read_lock()) from fprobe callback paths by switching fprobe and BPF multi-kprobe to Tasks-Rude RCU.Motivation & Problem
====================Currently, fprobe entry and exit callbacks (
fprobe_fgraph_entry,fprobe_return, andfprobe_ftrace_entry) execute in the tracing hot path where preemption is disabled.However, fprobe was forced to wrap hash lookups with guard(rcu)() and
rcu_read_lock()because:- rhashtable defers bucket table deallocation using standard RCU
-unregister_fprobe()and BPF waited for a standard RCU grace periodTaking standard RCU read locks in the tracing fast path introduces several drawbacks:
1. Unnecessary Runtime Overhead:
Every probe hit manipulatescurrent->rcu_read_lock_nestingwith memory barriers, andrcu_read_unlock()adds conditional branches to check for special quiescent processing.
On debug kernels withCONFIG_PROVE_RCU=yorCONFIG_LOCKDEP=y, this additionally acquires and releases lockdep maps on every hit, introducing severe lockdep hashing overhead and tracer recursion risks.2. Fragile Dependency on
rcu_is_watching():
Standard RCU treats idle CPUs (and user-space onnohz_fullCPUs) as Extended Quiescent States (EQS). If a function is traced whilercu_is_watching()is false, standard RCU is blind to the read-side critical section. In such contexts,synchronize_rcu()does not wait for the reader (risking Use-After-Free), and lockdep emits an "RCU-illegal:rcu_read_lock()used while not watching!" warning. To avoid this, ftrace callbacks normally requireFTRACE_OPS_FL_RCU, adding extra trampoline check overhead.3. Asymmetric Synchronization with
fprobe_return():
fprobe_return()executes underpreempt_disable_notrace()without holdingrcu_read_lock(). Prior to this series,unregister_fprobe()only waited onsynchronize_rcu(), which does NOT wait for pure preempt-disabled sections, leaving a potential Use-After-Free window during probe unregistration.I've tried to fix the last UAF with simply introducing guard(rcu)()[1] but Sashiko found the 2nd problem [2]. So I decided to implement this series.
Quoted 8 lines
Quoted 4 lines
Unless I am missing something subtle, Sashiko needs to be taught a little bit more about RCU. Preemption-disabled regions of code really are valid RCU readers. If you have a reproducer showing that this is not the case in some situation, that would be a bug in RCU.
Thanks for the clarification!
So I think we can replacercu_read_lock()withrcu_read_lock_sched_notrace()? (also, maybe we need to addrcu_is_watching()check)
That would work, and yes, you are quite right that if !rcu_is_watching(), RCU is studiously ignoring you completely, no matter what your preemption state is.
Quoted 1 line
Current code may involves lockdep in fprobe, that is not good.
Agreed, that could be problematic in any number of contexts. ;-)
Quoted 2 lines
BTW, if we want to trace some code in extended quiescent state, do we still need this Tasks-Rude RCU?
Well, you need something other than vanilla RCU, anyway. And Tasks Rude RCU is one candidate, though it is also a candidate that might annoy people wishing to minimize IPIs.
Thanx, Paul
Quoted 56 lines
Thank you,
Quoted 50 lines
Adding Roman Gushchin and Chris Mason on CC for their thoughts.
Thanx, Paul
Quoted 45 lines
[1] https://lore.kernel.org/all/179055575009.241711.6358052647499787191.stgit@devnote2/
[2] https://lore.kernel.org/all/[email protected]/Solution: Tasks-Rude RCU
========================Because fprobe callbacks already run strictly within preempt-disabled contexts, we can transition fprobe and its deferred table reclamation to Tasks-Rude RCU:
- Tasks-Rude RCU detects grace periods via
schedule_on_each_cpu(), forcing a schedule on every online CPU. This guarantees that all preempt-disabled sections that began prior to the grace period have completed before memory is reclaimed.
- Unlike standard RCU, Tasks-Rude RCU does not rely on dyntick-idle / EQS tracking. It does NOT requirercu_is_watching()to be true and does not trigger lockdep warnings in pre-RCU/idle execution paths.
- Within fprobe, guard(rcu)() is replaced with guard(rcu_sched_notrace)(), reducing the lookup lock to pure, non-tracing preempt counter increments without lockdep or RCU state manipulation.Feedback and suggestions from RCU, BPF, and tracing maintainers are welcome!
Thank you,
---
base-commit:5bfa9f1a9dcb6ecb607adbc1c0226605c972935bMasami Hiramatsu (Google) (3):
rcu/tasks: Exportcall_rcu_tasks_rude()
rhashtable: Adduse_tasks_rudeparameter to defer bucket table free
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCUinclude/linux/rcupdate.h | 6 +++ include/linux/rhashtable-types.h | 2 + kernel/bpf/syscall.c | 2 + kernel/rcu/tasks.h | 8 ++--- kernel/trace/fprobe.c | 66 +++++++++++++++++++++----------------- lib/rhashtable.c | 5 ++- 6 files changed, 54 insertions(+), 35 deletions(-)Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 72 lines · Paul E. McKenney
Quoted 55 lines · Masami Hiramatsu
Quoted 52 lines · On Mon, 28 Sep 2026 09:25:02 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 50 lines · Masami Hiramatsu (Google)
Hi,
Here is an RFC patch series which removes standard RCU read lock (guard(rcu) and
rcu_read_lock()) from fprobe callback paths by switching fprobe and BPF multi-kprobe to Tasks-Rude RCU.Motivation & Problem
====================Currently, fprobe entry and exit callbacks (
fprobe_fgraph_entry,fprobe_return, andfprobe_ftrace_entry) execute in the tracing hot path where preemption is disabled.However, fprobe was forced to wrap hash lookups with guard(rcu)() and
rcu_read_lock()because:- rhashtable defers bucket table deallocation using standard RCU
-unregister_fprobe()and BPF waited for a standard RCU grace periodTaking standard RCU read locks in the tracing fast path introduces several drawbacks:
1. Unnecessary Runtime Overhead:
Every probe hit manipulatescurrent->rcu_read_lock_nestingwith memory barriers, andrcu_read_unlock()adds conditional branches to check for special quiescent processing.
On debug kernels withCONFIG_PROVE_RCU=yorCONFIG_LOCKDEP=y, this additionally acquires and releases lockdep maps on every hit, introducing severe lockdep hashing overhead and tracer recursion risks.2. Fragile Dependency on
rcu_is_watching():
Standard RCU treats idle CPUs (and user-space onnohz_fullCPUs) as Extended Quiescent States (EQS). If a function is traced whilercu_is_watching()is false, standard RCU is blind to the read-side critical section. In such contexts,synchronize_rcu()does not wait for the reader (risking Use-After-Free), and lockdep emits an "RCU-illegal:rcu_read_lock()used while not watching!" warning. To avoid this, ftrace callbacks normally requireFTRACE_OPS_FL_RCU, adding extra trampoline check overhead.3. Asymmetric Synchronization with
fprobe_return():
fprobe_return()executes underpreempt_disable_notrace()without holdingrcu_read_lock(). Prior to this series,unregister_fprobe()only waited onsynchronize_rcu(), which does NOT wait for pure preempt-disabled sections, leaving a potential Use-After-Free window during probe unregistration.I've tried to fix the last UAF with simply introducing guard(rcu)()[1] but Sashiko found the 2nd problem [2]. So I decided to implement this series.
Quoted 8 lines
Quoted 4 lines
Unless I am missing something subtle, Sashiko needs to be taught a little bit more about RCU. Preemption-disabled regions of code really are valid RCU readers. If you have a reproducer showing that this is not the case in some situation, that would be a bug in RCU.
Thanks for the clarification!
So I think we can replacercu_read_lock()withrcu_read_lock_sched_notrace()? (also, maybe we need to addrcu_is_watching()check)That would work, and yes, you are quite right that if !rcu_is_watching(), RCU is studiously ignoring you completely, no matter what your preemption state is.
Quoted 1 line
Current code may involves lockdep in fprobe, that is not good.
Quoted 9 lines
Agreed, that could be problematic in any number of contexts. ;-)
Quoted 2 lines
BTW, if we want to trace some code in extended quiescent state, do we still need this Tasks-Rude RCU?
Well, you need something other than vanilla RCU, anyway. And Tasks Rude RCU is one candidate, though it is also a candidate that might annoy people wishing to minimize IPIs.
Would RCU Tasks Trace be a better candidate given uprobes and sleepable BPF depends on it? Would it work here?
Quoted 58 lines
Thanx, Paul
Quoted 56 lines
Thank you,
Quoted 50 lines
Adding Roman Gushchin and Chris Mason on CC for their thoughts.
Thanx, Paul
Quoted 45 lines
[1] https://lore.kernel.org/all/179055575009.241711.6358052647499787191.stgit@devnote2/
[2] https://lore.kernel.org/all/[email protected]/Solution: Tasks-Rude RCU
========================Because fprobe callbacks already run strictly within preempt-disabled contexts, we can transition fprobe and its deferred table reclamation to Tasks-Rude RCU:
- Tasks-Rude RCU detects grace periods via
schedule_on_each_cpu(), forcing a schedule on every online CPU. This guarantees that all preempt-disabled sections that began prior to the grace period have completed before memory is reclaimed.
- Unlike standard RCU, Tasks-Rude RCU does not rely on dyntick-idle / EQS tracking. It does NOT requirercu_is_watching()to be true and does not trigger lockdep warnings in pre-RCU/idle execution paths.
- Within fprobe, guard(rcu)() is replaced with guard(rcu_sched_notrace)(), reducing the lookup lock to pure, non-tracing preempt counter increments without lockdep or RCU state manipulation.Feedback and suggestions from RCU, BPF, and tracing maintainers are welcome!
Thank you,
---
base-commit:5bfa9f1a9dcb6ecb607adbc1c0226605c972935bMasami Hiramatsu (Google) (3):
rcu/tasks: Exportcall_rcu_tasks_rude()
rhashtable: Adduse_tasks_rudeparameter to defer bucket table free
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCUinclude/linux/rcupdate.h | 6 +++ include/linux/rhashtable-types.h | 2 + kernel/bpf/syscall.c | 2 + kernel/rcu/tasks.h | 8 ++--- kernel/trace/fprobe.c | 66 +++++++++++++++++++++----------------- lib/rhashtable.c | 5 ++- 6 files changed, 54 insertions(+), 35 deletions(-)--
Masami Hiramatsu (Google) <[email protected]>--
Masami Hiramatsu (Google) <[email protected]>
Paul E. McKenney
maintainerQuoted 78 lines · Andrii Nakryiko
Quoted 77 lines · Paul E. McKenney
Quoted 55 lines · Masami Hiramatsu
Quoted 52 lines · On Mon, 28 Sep 2026 09:25:02 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 50 lines · Masami Hiramatsu (Google)
Hi,
Here is an RFC patch series which removes standard RCU read lock (guard(rcu) and
rcu_read_lock()) from fprobe callback paths by switching fprobe and BPF multi-kprobe to Tasks-Rude RCU.Motivation & Problem
====================Currently, fprobe entry and exit callbacks (
fprobe_fgraph_entry,fprobe_return, andfprobe_ftrace_entry) execute in the tracing hot path where preemption is disabled.However, fprobe was forced to wrap hash lookups with guard(rcu)() and
rcu_read_lock()because:- rhashtable defers bucket table deallocation using standard RCU
-unregister_fprobe()and BPF waited for a standard RCU grace periodTaking standard RCU read locks in the tracing fast path introduces several drawbacks:
1. Unnecessary Runtime Overhead:
Every probe hit manipulatescurrent->rcu_read_lock_nestingwith memory barriers, andrcu_read_unlock()adds conditional branches to check for special quiescent processing.
On debug kernels withCONFIG_PROVE_RCU=yorCONFIG_LOCKDEP=y, this additionally acquires and releases lockdep maps on every hit, introducing severe lockdep hashing overhead and tracer recursion risks.2. Fragile Dependency on
rcu_is_watching():
Standard RCU treats idle CPUs (and user-space onnohz_fullCPUs) as Extended Quiescent States (EQS). If a function is traced whilercu_is_watching()is false, standard RCU is blind to the read-side critical section. In such contexts,synchronize_rcu()does not wait for the reader (risking Use-After-Free), and lockdep emits an "RCU-illegal:rcu_read_lock()used while not watching!" warning. To avoid this, ftrace callbacks normally requireFTRACE_OPS_FL_RCU, adding extra trampoline check overhead.3. Asymmetric Synchronization with
fprobe_return():
fprobe_return()executes underpreempt_disable_notrace()without holdingrcu_read_lock(). Prior to this series,unregister_fprobe()only waited onsynchronize_rcu(), which does NOT wait for pure preempt-disabled sections, leaving a potential Use-After-Free window during probe unregistration.I've tried to fix the last UAF with simply introducing guard(rcu)()[1] but Sashiko found the 2nd problem [2]. So I decided to implement this series.
Quoted 8 lines
Quoted 4 lines
Unless I am missing something subtle, Sashiko needs to be taught a little bit more about RCU. Preemption-disabled regions of code really are valid RCU readers. If you have a reproducer showing that this is not the case in some situation, that would be a bug in RCU.
Thanks for the clarification!
So I think we can replacercu_read_lock()withrcu_read_lock_sched_notrace()? (also, maybe we need to addrcu_is_watching()check)That would work, and yes, you are quite right that if !rcu_is_watching(), RCU is studiously ignoring you completely, no matter what your preemption state is.
Quoted 1 line
Current code may involves lockdep in fprobe, that is not good.
Agreed, that could be problematic in any number of contexts. ;-)
Quoted 2 lines
BTW, if we want to trace some code in extended quiescent state, do we still need this Tasks-Rude RCU?
Quoted 6 lines
Quoted 3 lines
Well, you need something other than vanilla RCU, anyway. And Tasks Rude RCU is one candidate, though it is also a candidate that might annoy people wishing to minimize IPIs.
Would RCU Tasks Trace be a better candidate given uprobes and sleepable BPF depends on it? Would it work here?
Quite possibly! Of course, there might be corner-case ftrace/BPF issues that you and Masami would know better than would I.
David Woodhouse suggests the new Atomic SRCU for a vaguely similar purpose:
https://lore.kernel.org/all/[email protected]/
Now that Tasks Trace RCU is implemented in terms of SRCU-fast, there might not be all that much air between David's suggestion and yours.
Thanx, Paul
Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Thank you,
Quoted 50 lines
Adding Roman Gushchin and Chris Mason on CC for their thoughts.
Thanx, Paul
Quoted 45 lines
[1] https://lore.kernel.org/all/179055575009.241711.6358052647499787191.stgit@devnote2/
[2] https://lore.kernel.org/all/[email protected]/Solution: Tasks-Rude RCU
========================Because fprobe callbacks already run strictly within preempt-disabled contexts, we can transition fprobe and its deferred table reclamation to Tasks-Rude RCU:
- Tasks-Rude RCU detects grace periods via
schedule_on_each_cpu(), forcing a schedule on every online CPU. This guarantees that all preempt-disabled sections that began prior to the grace period have completed before memory is reclaimed.
- Unlike standard RCU, Tasks-Rude RCU does not rely on dyntick-idle / EQS tracking. It does NOT requirercu_is_watching()to be true and does not trigger lockdep warnings in pre-RCU/idle execution paths.
- Within fprobe, guard(rcu)() is replaced with guard(rcu_sched_notrace)(), reducing the lookup lock to pure, non-tracing preempt counter increments without lockdep or RCU state manipulation.Feedback and suggestions from RCU, BPF, and tracing maintainers are welcome!
Thank you,
---
base-commit:5bfa9f1a9dcb6ecb607adbc1c0226605c972935bMasami Hiramatsu (Google) (3):
rcu/tasks: Exportcall_rcu_tasks_rude()
rhashtable: Adduse_tasks_rudeparameter to defer bucket table free
fprobe: Switch fprobe and BPF kprobe-multi to Tasks-Rude RCUinclude/linux/rcupdate.h | 6 +++ include/linux/rhashtable-types.h | 2 + kernel/bpf/syscall.c | 2 + kernel/rcu/tasks.h | 8 ++--- kernel/trace/fprobe.c | 66 +++++++++++++++++++++----------------- lib/rhashtable.c | 5 ++- 6 files changed, 54 insertions(+), 35 deletions(-)--
Masami Hiramatsu (Google) <[email protected]>--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerQuoted 15 lines · On Mon, 28 Sep 2026 17:41:57 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 15 lines
Quoted 15 lines
Quoted 3 lines
Thanks for the clarification!
So I think we can replacercu_read_lock()withrcu_read_lock_sched_notrace()? (also, maybe we need to addrcu_is_watching()check)That would work, and yes, you are quite right that if !rcu_is_watching(), RCU is studiously ignoring you completely, no matter what your preemption state is.
Quoted 1 line
Current code may involves lockdep in fprobe, that is not good.
Agreed, that could be problematic in any number of contexts. ;-)
Quoted 2 lines
BTW, if we want to trace some code in extended quiescent state, do we still need this Tasks-Rude RCU?
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Well, you need something other than vanilla RCU, anyway. And Tasks Rude RCU is one candidate, though it is also a candidate that might annoy people wishing to minimize IPIs.
Would RCU Tasks Trace be a better candidate given uprobes and sleepable BPF depends on it? Would it work here?
Hmm, those can be sleepable, but this is not. fprobe is based on ftrace so the callbacks are non preemptive.
Quoted 11 lines
Quite possibly! Of course, there might be corner-case ftrace/BPF issues that you and Masami would know better than would I.
David Woodhouse suggests the new Atomic SRCU for a vaguely similar purpose:
https://lore.kernel.org/all/[email protected]/
Now that Tasks Trace RCU is implemented in terms of SRCU-fast, there might not be all that much air between David's suggestion and yours.
OK, let me check it. But at a glance, this atomic SRCU is still a bit heavier than rcu_sched_notrace, for fprobe. It involves smp_mb and lockdep.
So if all of these rcu_read_lock() in fprobe are replaced with rcu_read_lock_sched_notrace(), isn't it enough to switch the synchronize_rcu() to synchronize_rcu_tasks_rude() for avoiding if (!rcu_is_watching()) return; ?
(Note, most of replacement will be done by [1])
[1] https://lore.kernel.org/all/179064115227.394389.16910234241400391996.stgit@devnote2/
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Paul E. McKenney
maintainerQuoted 35 lines · Masami Hiramatsu
Quoted 15 lines · On Mon, 28 Sep 2026 17:41:57 -0700 "Paul E. McKenney" <[email protected]>…
Quoted 15 lines
Quoted 15 lines
Quoted 3 lines
Thanks for the clarification!
So I think we can replacercu_read_lock()withrcu_read_lock_sched_notrace()? (also, maybe we need to addrcu_is_watching()check)That would work, and yes, you are quite right that if !rcu_is_watching(), RCU is studiously ignoring you completely, no matter what your preemption state is.
Quoted 1 line
Current code may involves lockdep in fprobe, that is not good.
Agreed, that could be problematic in any number of contexts. ;-)
Quoted 2 lines
BTW, if we want to trace some code in extended quiescent state, do we still need this Tasks-Rude RCU?
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Well, you need something other than vanilla RCU, anyway. And Tasks Rude RCU is one candidate, though it is also a candidate that might annoy people wishing to minimize IPIs.
Would RCU Tasks Trace be a better candidate given uprobes and sleepable BPF depends on it? Would it work here?
Hmm, those can be sleepable, but this is not. fprobe is based on ftrace so the callbacks are non preemptive.
Quoted 7 lines
Quite possibly! Of course, there might be corner-case ftrace/BPF issues that you and Masami would know better than would I.
David Woodhouse suggests the new Atomic SRCU for a vaguely similar purpose:
Quoted 7 lines
Quoted 4 lines
https://lore.kernel.org/all/[email protected]/
Now that Tasks Trace RCU is implemented in terms of SRCU-fast, there might not be all that much air between David's suggestion and yours.
OK, let me check it. But at a glance, this atomic SRCU is still a bit heavier than
rcu_sched_notrace, for fprobe. It involvessmp_mband lockdep.
True, it is heavier weight.
Quoted 4 lines
So if all of these
rcu_read_lock()in fprobe are replaced withrcu_read_lock_sched_notrace(), isn't it enough to switch thesynchronize_rcu()tosynchronize_rcu_tasks_rude()for avoidingif (!rcu_is_watching()) return;?
Yes, for all fprobes where rcu_is_watching(), agreed.
Thanx, Paul
Quoted 7 lines
(Note, most of replacement will be done by [1])
[1] https://lore.kernel.org/all/179064115227.394389.16910234241400391996.stgit@devnote2/Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>