SRCU-protected uretprobes hot path
Subsystems (4)
Andrii Nakryiko
maintainerv3 cover SRCU-protected uretprobes hot path
Recently landed changes make uprobe entry hot code path makes use of RCU Tasks Trace to avoid touching uprobe refcount, which at high frequency of uprobe triggering leads to excessive cache line bouncing and limited scalability with increased number of CPUs that simultaneously execute uprobe handlers.
This patch set adds return uprobe (uretprobe) side of this, this time utilizing SRCU for the same reasons. Given the time between entry uprobe activation (at which point uretprobe code hijacks user-space stack to get activated on user function return) and uretprobe activation can be arbitrarily long and is completely under control of user code, we need to protect ourselves from too long or unbounded SRCU grace periods.
To that end we keep SRCU protection only for a limited time, and if user space code takes longer to return, pending uretprobe instances are "downgraded" to refcounted ones. This gives us best scalability and performance for high-frequency uretprobes, and keeps upper bound on SRCU grace period duration for low frequency uretprobes.
There are a bunch of synchronization issues between timer callback running in IRQ handler and current thread executing uretprobe handlers, which is abstracted away behind "hybrid lifetime uprobe" (hprobe) wrapper around uprobe instance itself.
There is now a speculative try_get_uprobe() and, possibly, a compensating put_uprobe() being done from the timer thread (softirq), so we need to make sure that put_uprobe() is working well from any context. This is what patch #1 does, employing deferred work callback, and shifting all the locking to it.
v2->v3:
- rebased onto peterz/queue.git's perf/core on top of Jiri's changes;
- simplify hprobe states by utilizing HPROBE_GONE for NULL uprobe (Peter);
- hprobe_expire() can return uprobe with refcount, if requested (Peter);
- keep hprobe_init_leased() and hprobe_init_stable() to a) avoid srcu_idx bikeshedding dependency and b) leased constructor shouldn't accept NULL uprobe, so it's nice to be able to easily express and enforce that;
- patch #1 stays the same, we'll work on uprobe_delayed_lock separately;v1->v2:
- dropped single-stepped uprobes changes to make this change a bit more palatable to Oleg and get some good will from him :)
- fixed the bug with not calling __srcu_read_unlock when "expiring" leased
uprobe, but failing to get refcount;
- switched hprobe implementation to an explicit state machine, which seems to make logic more straightforward, evidenced by this allowing me to spot the above subtle LEASED -> GONE transition bug;
- re-ran uprobe-stress many-many times, it was instrumental for getting confidence in implementation and spotting subtle bugs (including the above one, once I modified timer logic to ran at fixed interval to increase the probability of races with the normal uretprobe consumer code);rfc->v1:
- made put_uprobe() work in any context, not just user context (Oleg);
- changed to unconditional mod_timer() usage to avoid races (Oleg).
- I kept single-stepped uprobe changes, as they have a simple use of all the hprobe functionality developed in patch #1.
Andrii Nakryiko (2):
uprobes: allow put_uprobe() from non-sleepable softirq context
uprobes: SRCU-protect uretprobe lifetime (with timeout)
include/linux/uprobes.h | 54 ++++++- kernel/events/uprobes.c | 309 +++++++++++++++++++++++++++++++++++----- 2 files changed, 322 insertions(+), 41 deletions(-)
Signature · 1 line
2.43.5
Andrii Nakryiko
maintainerPATCH v3 1/2 uprobes: allow put_uprobe() from non-sleepable softirq context
Currently put_uprobe() might trigger mutex_lock()/mutex_unlock(), which makes it unsuitable to be called from more restricted context like softirq.
Let's make put_uprobe() agnostic to the context in which it is called, and use work queue to defer the mutex-protected clean up steps.
RB tree removal step is also moved into work-deferred callback to avoid potential deadlock between softirq-based timer callback, added in the next patch, and the rest of uprobe code.
We can rework locking altogher as a follow up, but that's significantly more tricky, so warrants its own patch set. For now, we need to make sure that changes in the next patch that add timer thread work correctly with existing approach, while concentrating on SRCU + timeout logic.
--- kernel/events/uprobes.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/events/uprobes.c +16 -4
@@ -27,6 +27,7 @@#include <linux/shmem_fs.h> #include <linux/khugepaged.h> #include <linux/rcupdate_trace.h> +#include <linux/workqueue.h> #include <linux/uprobes.h>
@@ -61,7 +62,10 @@ struct uprobe {struct list_head pending_list;struct list_head consumers;struct inode *inode;/* Also hold a ref to inode */ -struct rcu_head rcu; +union { +struct rcu_head rcu; +struct work_struct work; + };loff_t offset;loff_t ref_ctr_offset;unsigned long flags;/* "unsigned long" so bitops work */
@@ -625,10 +629,9 @@ static void uprobe_free_rcu(struct rcu_head *rcu)kfree (uprobe); } -static void put_uprobe (struct uprobe *uprobe) +static void uprobe_free_deferred (struct work_struct *work) { -if (!refcount_dec_and_test (&uprobe->ref)) -return ; + struct uprobe *uprobe = container_of(work, struct uprobe, work);write_lock (&uprobes_treelock);
@@ -652,6 +655,15 @@ static void put_uprobe(struct uprobe *uprobe)call_rcu_tasks_trace (&uprobe->rcu, uprobe_free_rcu); } +static void put_uprobe (struct uprobe *uprobe) +{ +if (!refcount_dec_and_test (&uprobe->ref)) +return ; + +INIT_WORK (&uprobe->work, uprobe_free_deferred); +schedule_work (&uprobe->work); +} +static __always_inline int uprobe_cmp(const struct inode *l_inode, const loff_t l_offset,const struct uprobe *r)
Signature · 1 line
2.43.5
[tip: perf/core] uprobes: allow put_uprobe() from non-sleepable softirq context
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 2bf8e5aceff899f5117f14c73e869a61c44d8a69 Gitweb: https://git.kernel.org/tip/2bf8e5aceff899f5117f14c73e869a61c44d8a69 Author: Andrii Nakryiko <[email protected]> AuthorDate: Wed, 23 Oct 2024 21:41:58 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 30 Oct 2024 22:42:19 +01:00
uprobes: allow put_uprobe() from non-sleepable softirq context
Currently put_uprobe() might trigger mutex_lock()/mutex_unlock(), which makes it unsuitable to be called from more restricted context like softirq.
Let's make put_uprobe() agnostic to the context in which it is called, and use work queue to defer the mutex-protected clean up steps.
RB tree removal step is also moved into work-deferred callback to avoid potential deadlock between softirq-based timer callback, added in the next patch, and the rest of uprobe code.
We can rework locking altogher as a follow up, but that's significantly more tricky, so warrants its own patch set. For now, we need to make sure that changes in the next patch that add timer thread work correctly with existing approach, while concentrating on SRCU + timeout logic.
--- kernel/events/uprobes.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/events/uprobes.c +16 -4
@@ -27,6 +27,7 @@#include <linux/shmem_fs.h> #include <linux/khugepaged.h> #include <linux/rcupdate_trace.h> +#include <linux/workqueue.h> #include <linux/uprobes.h>
@@ -61,7 +62,10 @@ struct uprobe {struct list_head pending_list;struct list_head consumers;struct inode *inode;/* Also hold a ref to inode */ -struct rcu_head rcu; +union { +struct rcu_head rcu; +struct work_struct work; + };loff_t offset;loff_t ref_ctr_offset;unsigned long flags;/* "unsigned long" so bitops work */
@@ -625,10 +629,9 @@ static void uprobe_free_rcu(struct rcu_head *rcu)kfree (uprobe); } -static void put_uprobe (struct uprobe *uprobe) +static void uprobe_free_deferred (struct work_struct *work) { -if (!refcount_dec_and_test (&uprobe->ref)) -return ; + struct uprobe *uprobe = container_of(work, struct uprobe, work);write_lock (&uprobes_treelock);
@@ -652,6 +655,15 @@ static void put_uprobe(struct uprobe *uprobe)call_rcu_tasks_trace (&uprobe->rcu, uprobe_free_rcu); } +static void put_uprobe (struct uprobe *uprobe) +{ +if (!refcount_dec_and_test (&uprobe->ref)) +return ; + +INIT_WORK (&uprobe->work, uprobe_free_deferred); +schedule_work (&uprobe->work); +} +static __always_inline int uprobe_cmp(const struct inode *l_inode, const loff_t l_offset,const struct uprobe *r)
Andrii Nakryiko
maintainerPATCH v3 2/2 uprobes: SRCU-protect uretprobe lifetime (with timeout)
In reply to Andrii NakryikoAvoid taking refcount on uprobe in prepare_uretprobe(), instead take uretprobe-specific SRCU lock and keep it active as kernel transfers control back to user space.
Given we can't rely on user space returning from traced function within reasonable time period, we need to make sure not to keep SRCU lock active for too long, though. To that effect, we employ a timer callback which is meant to terminate SRCU lock region after predefined timeout (currently set to 100ms), and instead transfer underlying struct uprobe's lifetime protection to refcounting.
This fallback to less scalable refcounting after 100ms is a fine tradeoff from uretprobe's scalability and performance perspective, because uretprobing *long running* user functions inherently doesn't run into scalability issues (there is just not enough frequency to cause noticeable issues with either performance or scalability).
The overall trick is in ensuring synchronization between current thread and timer's callback fired on some other thread. To cope with that with minimal logic complications, we add hprobe wrapper which is used to contain all the synchronization related issues behind a small number of basic helpers: hprobe_expire() for "downgrading" uprobe from SRCU-protected state to refcounted state, and a hprobe_consume() and hprobe_finalize() pair of single-use consuming helpers. Other than that, whatever current thread's logic is there stays the same, as timer thread cannot modify return_instance state (or add new/remove old return_instances). It only takes care of SRCU unlock and uprobe refcounting, which is hidden from the higher-level uretprobe handling logic.
We use atomic xchg() in hprobe_consume(), which is called from performance critical handle_uretprobe_chain() function run in the current context. When uncontended, this xchg() doesn't seem to hurt performance as there are no other competing CPUs fighting for the same cache line. We also mark struct return_instance as ____cacheline_aligned to ensure no false sharing can happen.
Another technical moment. We need to make sure that the list of return instances can be safely traversed under RCU from timer callback, so we delay return_instance freeing with kfree_rcu() and make sure that list modifications use RCU-aware operations.
Also, given SRCU lock survives transition from kernel to user space and back we need to use lower-level __srcu_read_lock() and __srcu_read_unlock() to avoid lockdep complaining.
Just to give an impression of a kind of performance improvements this change brings, below are benchmarking results with and without these SRCU changes, assuming other uprobe optimizations (mainly RCU Tasks Trace for entry uprobes, lockless RB-tree lookup, and lockless VMA to uprobe lookup) are left intact:
WITHOUT SRCU for uretprobes
===========================
uretprobe-nop ( 1 cpus): 2.197 ± 0.002M/s ( 2.197M/s/cpu) uretprobe-nop ( 2 cpus): 3.325 ± 0.001M/s ( 1.662M/s/cpu) uretprobe-nop ( 3 cpus): 4.129 ± 0.002M/s ( 1.376M/s/cpu) uretprobe-nop ( 4 cpus): 6.180 ± 0.003M/s ( 1.545M/s/cpu) uretprobe-nop ( 8 cpus): 7.323 ± 0.005M/s ( 0.915M/s/cpu) uretprobe-nop (16 cpus): 6.943 ± 0.005M/s ( 0.434M/s/cpu) uretprobe-nop (32 cpus): 5.931 ± 0.014M/s ( 0.185M/s/cpu) uretprobe-nop (64 cpus): 5.145 ± 0.003M/s ( 0.080M/s/cpu) uretprobe-nop (80 cpus): 4.925 ± 0.005M/s ( 0.062M/s/cpu)
WITH SRCU for uretprobes
========================
uretprobe-nop ( 1 cpus): 1.968 ± 0.001M/s ( 1.968M/s/cpu) uretprobe-nop ( 2 cpus): 3.739 ± 0.003M/s ( 1.869M/s/cpu) uretprobe-nop ( 3 cpus): 5.616 ± 0.003M/s ( 1.872M/s/cpu) uretprobe-nop ( 4 cpus): 7.286 ± 0.002M/s ( 1.822M/s/cpu) uretprobe-nop ( 8 cpus): 13.657 ± 0.007M/s ( 1.707M/s/cpu) uretprobe-nop (32 cpus): 45.305 ± 0.066M/s ( 1.416M/s/cpu) uretprobe-nop (64 cpus): 42.390 ± 0.922M/s ( 0.662M/s/cpu) uretprobe-nop (80 cpus): 47.554 ± 2.411M/s ( 0.594M/s/cpu)
--- include/linux/uprobes.h | 54 +++++++- kernel/events/uprobes.c | 289 +++++++++++++++++++++++++++++++++++----- 2 files changed, 306 insertions(+), 37 deletions(-)
include/linux/uprobes.h +52 -2
@@ -15,6 +15,7 @@#include <linux/rbtree.h> #include <linux/types.h> #include <linux/wait.h> +#include <linux/timer.h> struct uprobe ;struct vm_area_struct ;
@@ -67,6 +68,53 @@ enum uprobe_task_state { UTASK_SSTEP_TRAPPED, }; +/* The state of hybrid-lifetime uprobe inside struct return_instance */ +enum hprobe_state { +HPROBE_LEASED ,/* uretprobes_srcu-protected uprobe */ +HPROBE_STABLE ,/* refcounted uprobe */ +HPROBE_GONE ,/* NULL uprobe, SRCU expired, refcount failed */ +HPROBE_CONSUMED ,/* uprobe "consumed" by uretprobe handler */ +}; + +/* +* Hybrid lifetime uprobe. Represents a uprobe instance that could be either +* SRCU protected (with SRCU protection eventually potentially timing out), +* refcounted using uprobe->ref, or there could be no valid uprobe (NULL). +* +* hprobe's internal state is setup such that background timer thread can +* atomically "downgrade" temporarily RCU-protected uprobe into refcounted one +* (or no uprobe, if refcounting failed). +* +* *stable* pointer always point to the uprobe (or could be NULL if there is +* was no valid underlying uprobe to begin with). +* +* *leased* pointer is the key to achieving race-free atomic lifetime state +* transition and can have three possible states: +* - either the same non-NULL value as *stable*, in which case uprobe is +* SRCU-protected; +* - NULL, in which case uprobe (if there is any) is refcounted; +* - special __UPROBE_DEAD value, which represents an uprobe that was SRCU +* protected initially, but SRCU period timed out and we attempted to +* convert it to refcounted, but refcount_inc_not_zero() failed, because +* uprobe effectively went away (the last consumer unsubscribed). In this +* case it's important to know that *stable* pointer (which still has +* non-NULL uprobe pointer) shouldn't be used, because lifetime of +* underlying uprobe is not guaranteed anymore. __UPROBE_DEAD is just an +* internal marker and is handled transparently by hprobe_fetch() helper. +* +* When uprobe is SRCU-protected, we also record srcu_idx value, necessary for +* SRCU unlocking. +* +* See hprobe_expire() and hprobe_fetch() for details of race-free uprobe +* state transitioning details. It all hinges on atomic xchg() over *leaded* +* pointer. *stable* pointer, once initially set, is not modified concurrently. +*/ +struct hprobe { +enum hprobe_state state; +int srcu_idx; +struct uprobe *uprobe; +}; +/* * uprobe_task: Metadata of a task while it singlesteps. */
@@ -86,6 +134,7 @@ struct uprobe_task { };struct uprobe *active_uprobe; +struct timer_list ri_timer;unsigned long xol_vaddr;struct arch_uprobe *auprobe;
@@ -100,7 +149,7 @@ struct return_consumer { };struct return_instance { -struct uprobe *uprobe; +struct hprobe hprobe;unsigned long func;unsigned long stack;/* stack pointer */ unsigned long orig_ret_vaddr;/* original return address */
@@ -108,9 +157,10 @@ struct return_instance {int consumers_cnt;struct return_instance *next;/* keep as stack */ +struct rcu_head rcu; struct return_consumer consumers[] __counted_by(consumers_cnt); -}; +}____cacheline_aligned ;enum rp_check {RP_CHECK_CALL ,
kernel/events/uprobes.c +254 -35
452 lines not on this page · raw message
Signature · 1 line
2.43.5
[tip: perf/core] uprobes: SRCU-protect uretprobe lifetime (with timeout)
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: dd1a7567784e2b1f80258be04f57bcfa82c997eb Gitweb: https://git.kernel.org/tip/dd1a7567784e2b1f80258be04f57bcfa82c997eb Author: Andrii Nakryiko <[email protected]> AuthorDate: Wed, 23 Oct 2024 21:41:59 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 30 Oct 2024 22:42:19 +01:00
uprobes: SRCU-protect uretprobe lifetime (with timeout)
Avoid taking refcount on uprobe in prepare_uretprobe(), instead take uretprobe-specific SRCU lock and keep it active as kernel transfers control back to user space.
Given we can't rely on user space returning from traced function within reasonable time period, we need to make sure not to keep SRCU lock active for too long, though. To that effect, we employ a timer callback which is meant to terminate SRCU lock region after predefined timeout (currently set to 100ms), and instead transfer underlying struct uprobe's lifetime protection to refcounting.
This fallback to less scalable refcounting after 100ms is a fine tradeoff from uretprobe's scalability and performance perspective, because uretprobing *long running* user functions inherently doesn't run into scalability issues (there is just not enough frequency to cause noticeable issues with either performance or scalability).
The overall trick is in ensuring synchronization between current thread and timer's callback fired on some other thread. To cope with that with minimal logic complications, we add hprobe wrapper which is used to contain all the synchronization related issues behind a small number of basic helpers: hprobe_expire() for "downgrading" uprobe from SRCU-protected state to refcounted state, and a hprobe_consume() and hprobe_finalize() pair of single-use consuming helpers. Other than that, whatever current thread's logic is there stays the same, as timer thread cannot modify return_instance state (or add new/remove old return_instances). It only takes care of SRCU unlock and uprobe refcounting, which is hidden from the higher-level uretprobe handling logic.
We use atomic xchg() in hprobe_consume(), which is called from performance critical handle_uretprobe_chain() function run in the current context. When uncontended, this xchg() doesn't seem to hurt performance as there are no other competing CPUs fighting for the same cache line. We also mark struct return_instance as ____cacheline_aligned to ensure no false sharing can happen.
Another technical moment. We need to make sure that the list of return instances can be safely traversed under RCU from timer callback, so we delay return_instance freeing with kfree_rcu() and make sure that list modifications use RCU-aware operations.
Also, given SRCU lock survives transition from kernel to user space and back we need to use lower-level __srcu_read_lock() and __srcu_read_unlock() to avoid lockdep complaining.
Just to give an impression of a kind of performance improvements this change brings, below are benchmarking results with and without these SRCU changes, assuming other uprobe optimizations (mainly RCU Tasks Trace for entry uprobes, lockless RB-tree lookup, and lockless VMA to uprobe lookup) are left intact:
WITHOUT SRCU for uretprobes
===========================
uretprobe-nop ( 1 cpus): 2.197 ± 0.002M/s ( 2.197M/s/cpu) uretprobe-nop ( 2 cpus): 3.325 ± 0.001M/s ( 1.662M/s/cpu) uretprobe-nop ( 3 cpus): 4.129 ± 0.002M/s ( 1.376M/s/cpu) uretprobe-nop ( 4 cpus): 6.180 ± 0.003M/s ( 1.545M/s/cpu) uretprobe-nop ( 8 cpus): 7.323 ± 0.005M/s ( 0.915M/s/cpu) uretprobe-nop (16 cpus): 6.943 ± 0.005M/s ( 0.434M/s/cpu) uretprobe-nop (32 cpus): 5.931 ± 0.014M/s ( 0.185M/s/cpu) uretprobe-nop (64 cpus): 5.145 ± 0.003M/s ( 0.080M/s/cpu) uretprobe-nop (80 cpus): 4.925 ± 0.005M/s ( 0.062M/s/cpu)
WITH SRCU for uretprobes
========================
uretprobe-nop ( 1 cpus): 1.968 ± 0.001M/s ( 1.968M/s/cpu) uretprobe-nop ( 2 cpus): 3.739 ± 0.003M/s ( 1.869M/s/cpu) uretprobe-nop ( 3 cpus): 5.616 ± 0.003M/s ( 1.872M/s/cpu) uretprobe-nop ( 4 cpus): 7.286 ± 0.002M/s ( 1.822M/s/cpu) uretprobe-nop ( 8 cpus): 13.657 ± 0.007M/s ( 1.707M/s/cpu) uretprobe-nop (32 cpus): 45.305 ± 0.066M/s ( 1.416M/s/cpu) uretprobe-nop (64 cpus): 42.390 ± 0.922M/s ( 0.662M/s/cpu) uretprobe-nop (80 cpus): 47.554 ± 2.411M/s ( 0.594M/s/cpu)
--- include/linux/uprobes.h | 54 ++++++- kernel/events/uprobes.c | 287 ++++++++++++++++++++++++++++++++++----- 2 files changed, 304 insertions(+), 37 deletions(-)
include/linux/uprobes.h +52 -2
@@ -15,6 +15,7 @@#include <linux/rbtree.h> #include <linux/types.h> #include <linux/wait.h> +#include <linux/timer.h> struct uprobe ;struct vm_area_struct ;
@@ -67,6 +68,53 @@ enum uprobe_task_state { UTASK_SSTEP_TRAPPED, }; +/* The state of hybrid-lifetime uprobe inside struct return_instance */ +enum hprobe_state { +HPROBE_LEASED ,/* uretprobes_srcu-protected uprobe */ +HPROBE_STABLE ,/* refcounted uprobe */ +HPROBE_GONE ,/* NULL uprobe, SRCU expired, refcount failed */ +HPROBE_CONSUMED ,/* uprobe "consumed" by uretprobe handler */ +}; + +/* +* Hybrid lifetime uprobe. Represents a uprobe instance that could be either +* SRCU protected (with SRCU protection eventually potentially timing out), +* refcounted using uprobe->ref, or there could be no valid uprobe (NULL). +* +* hprobe's internal state is setup such that background timer thread can +* atomically "downgrade" temporarily RCU-protected uprobe into refcounted one +* (or no uprobe, if refcounting failed). +* +* *stable* pointer always point to the uprobe (or could be NULL if there is +* was no valid underlying uprobe to begin with). +* +* *leased* pointer is the key to achieving race-free atomic lifetime state +* transition and can have three possible states: +* - either the same non-NULL value as *stable*, in which case uprobe is +* SRCU-protected; +* - NULL, in which case uprobe (if there is any) is refcounted; +* - special __UPROBE_DEAD value, which represents an uprobe that was SRCU +* protected initially, but SRCU period timed out and we attempted to +* convert it to refcounted, but refcount_inc_not_zero() failed, because +* uprobe effectively went away (the last consumer unsubscribed). In this +* case it's important to know that *stable* pointer (which still has +* non-NULL uprobe pointer) shouldn't be used, because lifetime of +* underlying uprobe is not guaranteed anymore. __UPROBE_DEAD is just an +* internal marker and is handled transparently by hprobe_fetch() helper. +* +* When uprobe is SRCU-protected, we also record srcu_idx value, necessary for +* SRCU unlocking. +* +* See hprobe_expire() and hprobe_fetch() for details of race-free uprobe +* state transitioning details. It all hinges on atomic xchg() over *leaded* +* pointer. *stable* pointer, once initially set, is not modified concurrently. +*/ +struct hprobe { +enum hprobe_state state; +int srcu_idx; +struct uprobe *uprobe; +}; +/* * uprobe_task: Metadata of a task while it singlesteps. */
@@ -86,6 +134,7 @@ struct uprobe_task { };struct uprobe *active_uprobe; +struct timer_list ri_timer;unsigned long xol_vaddr;struct arch_uprobe *auprobe;
@@ -100,7 +149,7 @@ struct return_consumer { };struct return_instance { -struct uprobe *uprobe; +struct hprobe hprobe;unsigned long func;unsigned long stack;/* stack pointer */ unsigned long orig_ret_vaddr;/* original return address */
@@ -108,9 +157,10 @@ struct return_instance {int consumers_cnt;struct return_instance *next;/* keep as stack */ +struct rcu_head rcu; struct return_consumer consumers[] __counted_by(consumers_cnt); -}; +}____cacheline_aligned ;enum rp_check {RP_CHECK_CALL ,
kernel/events/uprobes.c +252 -35
450 lines not on this page · raw message
Re: [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout)
In reply to Andrii NakryikoHello Andrii,
kernel/events/uprobes.c static void put_uprobe(struct uprobe *uprobe) · line 770
Quoted 10 lines · Andrii Nakryiko
kernel/events/uprobes.c +9
-0+static struct uprobe *hprobe_expire (struct hprobe *hprobe,bool get) +{ +enum hprobe_state hstate; + +/* +* return_instance's hprobe is protected by RCU. +* Underlying uprobe is itself protected from reuse by SRCU. +*/ +lockdep_assert (rcu_read_lock_held () &&srcu_read_lock_held (&uretprobes_srcu));
I am hitting this warning in d082ecbc71e9e ("Linux 6.14-rc4") on aarch64. I suppose this might happen on x86 as well, but I haven't tested.
WARNING: CPU: 28 PID: 158906 at kernel/events/uprobes.c:768 hprobe_expire (kernel/events/uprobes.c:825) Call trace: hprobe_expire (kernel/events/uprobes.c:825) (P) uprobe_copy_process (kernel/events/uprobes.c:691 kernel/events/uprobes.c:2103 kernel/events/uprobes.c:2142) copy_process (kernel/fork.c:2636) kernel_clone (kernel/fork.c:2815) __arm64_sys_clone (kernel/fork.c:? kernel/fork.c:2926 kernel/fork.c:2926) invoke_syscall (arch/arm64/kernel/syscall.c:35 arch/arm64/kernel/syscall.c:49) do_el0_svc (arch/arm64/kernel/syscall.c:139 arch/arm64/kernel/syscall.c:151) el0_svc (arch/arm64/kernel/entry-common.c:165 arch/arm64/kernel/entry-common.c:178 arch/arm64/kernel/entry-common.c:745) el0t_64_sync_handler (arch/arm64/kernel/entry-common.c:797) el0t_64_sync (arch/arm64/kernel/entry.S:600)
I broke down that warning, and the problem is on related to rcu_read_lock_held(), since RCU read lock does not seem to be held in this path.
Reading this code, RCU read lock seems to protect old hprobe, which doesn't seem so.
I am wondering if we need to protect it properly, something as:
@@ -2089,7 +2092,9 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) return -ENOMEM; /* if uprobe is non-NULL, we'll have an extra refcount for uprobe */ + rcu_read_lock(); uprobe = hprobe_expire(&o->hprobe, true); + rcu_write_lock(); /* * New utask will have stable properly refcounted uprobe or
Re: [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout)
Quoted 42 lines · Breno Leitao
Hello Andrii,
kernel/events/uprobes.c static void put_uprobe(struct uprobe *uprobe) · line 770
Quoted 10 lines · Andrii Nakryiko
kernel/events/uprobes.c +9
-0+static struct uprobe *hprobe_expire (struct hprobe *hprobe,bool get) +{ +enum hprobe_state hstate; + +/* +* return_instance's hprobe is protected by RCU. +* Underlying uprobe is itself protected from reuse by SRCU. +*/ +lockdep_assert (rcu_read_lock_held () &&srcu_read_lock_held (&uretprobes_srcu));I am hitting this warning in
d082ecbc71e9e("Linux 6.14-rc4") on aarch64. I suppose this might happen on x86 as well, but I haven't tested.WARNING: CPU: 28 PID: 158906 at kernel/events/uprobes.c:768 hprobe_expire (kernel/events/uprobes.c:825) Call trace: hprobe_expire (kernel/events/uprobes.c:825) (P) uprobe_copy_process (kernel/events/uprobes.c:691 kernel/events/uprobes.c:2103 kernel/events/uprobes.c:2142) copy_process (kernel/fork.c:2636) kernel_clone (kernel/fork.c:2815) __arm64_sys_clone (kernel/fork.c:? kernel/fork.c:2926 kernel/fork.c:2926) invoke_syscall (arch/arm64/kernel/syscall.c:35 arch/arm64/kernel/syscall.c:49) do_el0_svc (arch/arm64/kernel/syscall.c:139 arch/arm64/kernel/syscall.c:151) el0_svc (arch/arm64/kernel/entry-common.c:165 arch/arm64/kernel/entry-common.c:178 arch/arm64/kernel/entry-common.c:745) el0t_64_sync_handler (arch/arm64/kernel/entry-common.c:797) el0t_64_sync (arch/arm64/kernel/entry.S:600)I broke down that warning, and the problem is on related to
rcu_read_lock_held(), since RCU read lock does not seem to be held in this path.Reading this code, RCU read lock seems to protect old hprobe, which doesn't seem so.
I am wondering if we need to protect it properly, something as:
Quoted 8 lines
@@ -2089,7 +2092,9 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
return -ENOMEM;
/* if uprobe is non-NULL, we'll have an extra refcount for uprobe */
+ rcu_read_lock();
uprobe = hprobe_expire(&o->hprobe, true);
+ rcu_write_lock();I think this is not good enough. rcu_read_lock/unlock should be around the entire for loop, because, technically, that return_instance can be freed before we even get to hprobe_expire.
So, just like we have guard(srcu)(&uretprobes_srcu); we should have guard(rcu)();
Except, there is that kmemdup() hidden inside dup_return_instance(), so we can't really do that.
So that leaves us with an option to split memory allocation and initialization. Number of return instances can't grow (but can shrink), so we can first count them, allocate memory. Then do another iteration to do hprobe_expire().
I'll try to send a patch some time in the next day or two, but maybe someone has some better ideas meanwhile.
Quoted 2 lines
/*
* New utask will have stable properly refcounted uprobe or
Re: [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout)
Hello Andrii,
Quoted 52 lines · Andrii Nakryiko
Quoted 42 lines · Breno Leitao
Hello Andrii,
kernel/events/uprobes.c static void put_uprobe(struct uprobe *uprobe) · line 770
Quoted 10 lines · Andrii Nakryiko
kernel/events/uprobes.c +9
-0+static struct uprobe *hprobe_expire (struct hprobe *hprobe,bool get) +{ +enum hprobe_state hstate; + +/* +* return_instance's hprobe is protected by RCU. +* Underlying uprobe is itself protected from reuse by SRCU. +*/ +lockdep_assert (rcu_read_lock_held () &&srcu_read_lock_held (&uretprobes_srcu));I am hitting this warning in
d082ecbc71e9e("Linux 6.14-rc4") on aarch64. I suppose this might happen on x86 as well, but I haven't tested.WARNING: CPU: 28 PID: 158906 at kernel/events/uprobes.c:768 hprobe_expire (kernel/events/uprobes.c:825) Call trace: hprobe_expire (kernel/events/uprobes.c:825) (P) uprobe_copy_process (kernel/events/uprobes.c:691 kernel/events/uprobes.c:2103 kernel/events/uprobes.c:2142) copy_process (kernel/fork.c:2636) kernel_clone (kernel/fork.c:2815) __arm64_sys_clone (kernel/fork.c:? kernel/fork.c:2926 kernel/fork.c:2926) invoke_syscall (arch/arm64/kernel/syscall.c:35 arch/arm64/kernel/syscall.c:49) do_el0_svc (arch/arm64/kernel/syscall.c:139 arch/arm64/kernel/syscall.c:151) el0_svc (arch/arm64/kernel/entry-common.c:165 arch/arm64/kernel/entry-common.c:178 arch/arm64/kernel/entry-common.c:745) el0t_64_sync_handler (arch/arm64/kernel/entry-common.c:797) el0t_64_sync (arch/arm64/kernel/entry.S:600)I broke down that warning, and the problem is on related to
rcu_read_lock_held(), since RCU read lock does not seem to be held in this path.Reading this code, RCU read lock seems to protect old hprobe, which doesn't seem so.
I am wondering if we need to protect it properly, something as:
Quoted 8 lines
@@ -2089,7 +2092,9 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) return -ENOMEM; /* if uprobe is non-NULL, we'll have an extra refcount for uprobe */ + rcu_read_lock(); uprobe = hprobe_expire(&o->hprobe, true); + rcu_write_lock();
Quoted 3 lines
I think this is not good enough. rcu_read_lock/unlock should be around the entire for loop, because, technically, that
return_instancecan be freed before we even get tohprobe_expire.
re you suggesting that we should use an RCU read lock to protect the "traversal" of return_instances? In other words, is it currently being traversed unsafely, given that return_instance can be freed at any time?
Quoted 5 lines
So, just like we have guard(srcu)(&uretprobes_srcu); we should have guard(rcu)();
Except, there is that
kmemdup()hidden insidedup_return_instance(), so we can't really do that.
Right. kmemdup() is using GFP_KERNEL, which might sleep, so, it cannot be called using rcu read lock.
Thanks
--breno
Re: [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout)
Quoted 63 lines · Breno Leitao
Hello Andrii,
Quoted 52 lines · Andrii Nakryiko
Quoted 42 lines · Breno Leitao
Hello Andrii,
kernel/events/uprobes.c static void put_uprobe(struct uprobe *uprobe) · line 770
Quoted 10 lines · Andrii Nakryiko
kernel/events/uprobes.c +9
-0+static struct uprobe *hprobe_expire (struct hprobe *hprobe,bool get) +{ +enum hprobe_state hstate; + +/* +* return_instance's hprobe is protected by RCU. +* Underlying uprobe is itself protected from reuse by SRCU. +*/ +lockdep_assert (rcu_read_lock_held () &&srcu_read_lock_held (&uretprobes_srcu));I am hitting this warning in
d082ecbc71e9e("Linux 6.14-rc4") on aarch64. I suppose this might happen on x86 as well, but I haven't tested.WARNING: CPU: 28 PID: 158906 at kernel/events/uprobes.c:768 hprobe_expire (kernel/events/uprobes.c:825) Call trace: hprobe_expire (kernel/events/uprobes.c:825) (P) uprobe_copy_process (kernel/events/uprobes.c:691 kernel/events/uprobes.c:2103 kernel/events/uprobes.c:2142) copy_process (kernel/fork.c:2636) kernel_clone (kernel/fork.c:2815) __arm64_sys_clone (kernel/fork.c:? kernel/fork.c:2926 kernel/fork.c:2926) invoke_syscall (arch/arm64/kernel/syscall.c:35 arch/arm64/kernel/syscall.c:49) do_el0_svc (arch/arm64/kernel/syscall.c:139 arch/arm64/kernel/syscall.c:151) el0_svc (arch/arm64/kernel/entry-common.c:165 arch/arm64/kernel/entry-common.c:178 arch/arm64/kernel/entry-common.c:745) el0t_64_sync_handler (arch/arm64/kernel/entry-common.c:797) el0t_64_sync (arch/arm64/kernel/entry.S:600)I broke down that warning, and the problem is on related to
rcu_read_lock_held(), since RCU read lock does not seem to be held in this path.Reading this code, RCU read lock seems to protect old hprobe, which doesn't seem so.
I am wondering if we need to protect it properly, something as:
Quoted 8 lines
@@ -2089,7 +2092,9 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) return -ENOMEM; /* if uprobe is non-NULL, we'll have an extra refcount for uprobe */ + rcu_read_lock(); uprobe = hprobe_expire(&o->hprobe, true); + rcu_write_lock();Quoted 3 lines
I think this is not good enough. rcu_read_lock/unlock should be around the entire for loop, because, technically, that
return_instancecan be freed before we even get tohprobe_expire.re you suggesting that we should use an RCU read lock to protect the "traversal" of
return_instances? In other words, is it currently being traversed unsafely, given thatreturn_instancecan be freed at any time?
Quoted 8 lines
Quoted 5 lines
So, just like we have guard(srcu)(&uretprobes_srcu); we should have guard(rcu)();
Except, there is that
kmemdup()hidden insidedup_return_instance(), so we can't really do that.Right.
kmemdup()is usingGFP_KERNEL, which might sleep, so, it cannot be called using rcu read lock.
There is always SRCU?
Thanx, Paul
Re: [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout)
In reply to Breno LeitaoQuoted 56 lines · Breno Leitao
Hello Andrii,
Quoted 52 lines · Andrii Nakryiko
Quoted 50 lines · Breno Leitao
Hello Andrii,
kernel/events/uprobes.c static void put_uprobe(struct uprobe *uprobe) · line 770
Quoted 10 lines · Andrii Nakryiko
kernel/events/uprobes.c +9
-0+static struct uprobe *hprobe_expire (struct hprobe *hprobe,bool get) +{ +enum hprobe_state hstate; + +/* +* return_instance's hprobe is protected by RCU. +* Underlying uprobe is itself protected from reuse by SRCU. +*/ +lockdep_assert (rcu_read_lock_held () &&srcu_read_lock_held (&uretprobes_srcu));I am hitting this warning in
d082ecbc71e9e("Linux 6.14-rc4") on aarch64. I suppose this might happen on x86 as well, but I haven't tested.WARNING: CPU: 28 PID: 158906 at kernel/events/uprobes.c:768 hprobe_expire (kernel/events/uprobes.c:825) Call trace: hprobe_expire (kernel/events/uprobes.c:825) (P) uprobe_copy_process (kernel/events/uprobes.c:691 kernel/events/uprobes.c:2103 kernel/events/uprobes.c:2142) copy_process (kernel/fork.c:2636) kernel_clone (kernel/fork.c:2815) __arm64_sys_clone (kernel/fork.c:? kernel/fork.c:2926 kernel/fork.c:2926) invoke_syscall (arch/arm64/kernel/syscall.c:35 arch/arm64/kernel/syscall.c:49) do_el0_svc (arch/arm64/kernel/syscall.c:139 arch/arm64/kernel/syscall.c:151) el0_svc (arch/arm64/kernel/entry-common.c:165 arch/arm64/kernel/entry-common.c:178 arch/arm64/kernel/entry-common.c:745) el0t_64_sync_handler (arch/arm64/kernel/entry-common.c:797) el0t_64_sync (arch/arm64/kernel/entry.S:600)I broke down that warning, and the problem is on related to
rcu_read_lock_held(), since RCU read lock does not seem to be held in this path.Reading this code, RCU read lock seems to protect old hprobe, which doesn't seem so.
I am wondering if we need to protect it properly, something as:
@@ -2089,7 +2092,9 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) return -ENOMEM; /* if uprobe is non-NULL, we'll have an extra refcount for uprobe */ + rcu_read_lock(); uprobe = hprobe_expire(&o->hprobe, true); + rcu_write_lock();
Quoted 7 lines
Quoted 3 lines
I think this is not good enough. rcu_read_lock/unlock should be around the entire for loop, because, technically, that
return_instancecan be freed before we even get tohprobe_expire.re you suggesting that we should use an RCU read lock to protect the "traversal" of
return_instances? In other words, is it currently being traversed unsafely, given thatreturn_instancecan be freed at any time?
Yes, that's what I was suggesting initially.
But reading through all this again and paging in all the context I had when I was implementing this, I think this lockdep_assert(rcu_read_lock_held()) is a false positive.
For timer thread, yes, we need to keep rcu_read_lock(), because timer thread doesn't own return_instance, so it has to guarantee that memory won't go away.
But dup_utask() is called for current thread's return_instances, so they can't go anywhere and RCU read lock is not necessary here.
So I'm going to send a patch removing part of this lockdep_assert() and will expand the comment with the actual lifetime rules.
Quoted 12 lines
Quoted 6 lines
So, just like we have guard(srcu)(&uretprobes_srcu); we should have guard(rcu)();
Except, there is that
kmemdup()hidden insidedup_return_instance(), so we can't really do that.Right.
kmemdup()is usingGFP_KERNEL, which might sleep, so, it cannot be called using rcu read lock.Thanks
--breno