From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Josh Poimboeuf <jpoimboe@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
x86@kernel.org, Jiri Olsa <jolsa@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Subject: [PATCH v7 12/17] unwind deferred: Use SRCU unwind_deferred_task_work()
Date: Fri, 02 May 2025 12:47:58 -0400 [thread overview]
Message-ID: <20250502165009.406665968@goodmis.org> (raw)
In-Reply-To: 20250502164746.178864972@goodmis.org
From: Steven Rostedt <rostedt@goodmis.org>
Instead of using the callback_mutex to protect the link list of callbacks
in unwind_deferred_task_work(), use SRCU instead. This gets called every
time a task exits that has to record a stack trace that was requested.
This can happen for many tasks on several CPUs at the same time. A mutex
is a bottleneck and can cause a bit of contention and slow down performance.
As the callbacks themselves are allowed to sleep, regular RCU can not be
used to protect the list. Instead use SRCU, as that still allows the
callbacks to sleep and the list can be read without needing to hold the
callback_mutex.
Link: https://lore.kernel.org/all/ca9bd83a-6c80-4ee0-a83c-224b9d60b755@efficios.com/
Suggested-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
kernel/unwind/deferred.c | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c
index 716393dff810..5f98ac5e3a1b 100644
--- a/kernel/unwind/deferred.c
+++ b/kernel/unwind/deferred.c
@@ -23,10 +23,11 @@
*/
static DEFINE_PER_CPU(u64, unwind_ctx_ctr);
-/* Guards adding to and reading the list of callbacks */
+/* Guards adding to or removing from the list of callbacks */
static DEFINE_MUTEX(callback_mutex);
static LIST_HEAD(callbacks);
static unsigned long unwind_mask;
+DEFINE_STATIC_SRCU(unwind_srcu);
/*
* The context cookie is a unique identifier that is assigned to a user
@@ -137,6 +138,7 @@ static void unwind_deferred_task_work(struct callback_head *head)
struct unwind_work *work;
struct task_struct *task = current;
u64 cookie;
+ int idx;
if (WARN_ON_ONCE(!info->pending))
return;
@@ -155,14 +157,16 @@ static void unwind_deferred_task_work(struct callback_head *head)
cookie = get_cookie(info);
- guard(mutex)(&callback_mutex);
- list_for_each_entry(work, &callbacks, list) {
+ idx = srcu_read_lock(&unwind_srcu);
+ list_for_each_entry_srcu(work, &callbacks, list,
+ srcu_read_lock_held(&unwind_srcu)) {
if (task->unwind_mask & (1UL << work->bit)) {
work->func(work, &trace, cookie);
clear_bit(work->bit, ¤t->unwind_mask);
}
}
- barrier();
+ srcu_read_unlock(&unwind_srcu, idx);
+
/* If another task work is pending, reuse the cookie and stack trace */
if (!READ_ONCE(info->pending))
WRITE_ONCE(info->cookie, 0);
@@ -238,6 +242,7 @@ int unwind_deferred_request(struct unwind_work *work, u64 *cookie)
{
struct unwind_task_info *info = ¤t->unwind_info;
int pending;
+ int bit;
int ret;
*cookie = 0;
@@ -249,12 +254,17 @@ int unwind_deferred_request(struct unwind_work *work, u64 *cookie)
if (in_nmi())
return unwind_deferred_request_nmi(work, cookie);
+ /* Do not allow cancelled works to request again */
+ bit = READ_ONCE(work->bit);
+ if (WARN_ON_ONCE(bit < 0))
+ return -EINVAL;
+
guard(irqsave)();
*cookie = get_cookie(info);
/* This is already queued */
- if (current->unwind_mask & (1UL << work->bit))
+ if (current->unwind_mask & (1UL << bit))
return 1;
/* callback already pending? */
@@ -280,19 +290,26 @@ int unwind_deferred_request(struct unwind_work *work, u64 *cookie)
void unwind_deferred_cancel(struct unwind_work *work)
{
struct task_struct *g, *t;
+ int bit;
if (!work)
return;
guard(mutex)(&callback_mutex);
- list_del(&work->list);
+ list_del_rcu(&work->list);
+ bit = work->bit;
+
+ /* Do not allow any more requests and prevent callbacks */
+ work->bit = -1;
+
+ clear_bit(bit, &unwind_mask);
- clear_bit(work->bit, &unwind_mask);
+ synchronize_srcu(&unwind_srcu);
guard(rcu)();
/* Clear this bit from all threads */
for_each_process_thread(g, t) {
- clear_bit(work->bit, &t->unwind_mask);
+ clear_bit(bit, &t->unwind_mask);
}
}
@@ -309,7 +326,7 @@ int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func)
work->bit = ffz(unwind_mask);
unwind_mask |= 1UL << work->bit;
- list_add(&work->list, &callbacks);
+ list_add_rcu(&work->list, &callbacks);
work->func = func;
return 0;
}
--
2.47.2
next prev parent reply other threads:[~2025-05-02 16:50 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-02 16:47 [PATCH v7 00/17] unwind_user: perf: x86: Deferred unwinding infrastructure Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 01/17] unwind_user: Add user space unwinding API Steven Rostedt
2025-05-04 9:30 ` Ingo Molnar
2025-05-04 16:43 ` Steven Rostedt
2025-05-04 17:53 ` Josh Poimboeuf
2025-05-02 16:47 ` [PATCH v7 02/17] unwind_user: Add frame pointer support Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 03/17] unwind_user/x86: Enable frame pointer unwinding on x86 Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 04/17] perf/x86: Rename and move get_segment_base() and make it global Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 05/17] unwind_user: Add compat mode frame pointer support Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 06/17] unwind_user/x86: Enable compat mode frame pointer unwinding on x86 Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 07/17] unwind_user/deferred: Add unwind_deferred_trace() Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 08/17] unwind_user/deferred: Add unwind cache Steven Rostedt
2025-05-04 9:37 ` Ingo Molnar
2025-05-04 16:21 ` Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 09/17] unwind_user/deferred: Add deferred unwinding interface Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 10/17] unwind_user/deferred: Make unwind deferral requests NMI-safe Steven Rostedt
2025-05-02 16:47 ` [PATCH v7 11/17] unwind deferred: Use bitmask to determine which callbacks to call Steven Rostedt
2025-05-09 1:36 ` Steven Rostedt
2025-05-02 16:47 ` Steven Rostedt [this message]
2025-05-02 16:47 ` [PATCH v7 13/17] perf: Remove get_perf_callchain() init_nr argument Steven Rostedt
2025-05-02 16:48 ` [PATCH v7 14/17] perf: Have get_perf_callchain() return NULL if crosstask and user are set Steven Rostedt
2025-05-02 16:48 ` [PATCH v7 15/17] perf: Use current->flags & PF_KTHREAD instead of current->mm == NULL Steven Rostedt
2025-05-02 16:48 ` [PATCH v7 16/17] perf: Simplify get_perf_callchain() user logic Steven Rostedt
2025-05-02 16:48 ` [PATCH v7 17/17] perf: Skip user unwind if the task is a kernel thread Steven Rostedt
2025-05-04 9:41 ` [PATCH v7 00/17] unwind_user: perf: x86: Deferred unwinding infrastructure Ingo Molnar
2025-05-04 16:32 ` Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250502165009.406665968@goodmis.org \
--to=rostedt@goodmis.org \
--cc=jolsa@kernel.org \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.