[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <51855c0902486060cd6e1ccc6b22fd092a2e676d.1737511963.git.jpoimboe@kernel.org>
Date: Tue, 21 Jan 2025 18:31:21 -0800
From: Josh Poimboeuf <jpoimboe@...nel.org>
To: x86@...nel.org
Cc: Peter Zijlstra <peterz@...radead.org>,
Steven Rostedt <rostedt@...dmis.org>,
Ingo Molnar <mingo@...nel.org>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
linux-kernel@...r.kernel.org,
Indu Bhagat <indu.bhagat@...cle.com>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>,
Namhyung Kim <namhyung@...nel.org>,
Ian Rogers <irogers@...gle.com>,
Adrian Hunter <adrian.hunter@...el.com>,
linux-perf-users@...r.kernel.org,
Mark Brown <broonie@...nel.org>,
linux-toolchains@...r.kernel.org,
Jordan Rome <jordalgo@...a.com>,
Sam James <sam@...too.org>,
linux-trace-kernel@...r.kernel.org,
Andrii Nakryiko <andrii.nakryiko@...il.com>,
Jens Remus <jremus@...ux.ibm.com>,
Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
Florian Weimer <fweimer@...hat.com>,
Andy Lutomirski <luto@...nel.org>,
Masami Hiramatsu <mhiramat@...nel.org>,
Weinan Liu <wnliu@...gle.com>
Subject: [PATCH v4 29/39] unwind_user/deferred: Add unwind cache
Cache the results of the unwind to ensure the unwind is only performed
once, even when called by multiple tracers.
Signed-off-by: Josh Poimboeuf <jpoimboe@...nel.org>
---
include/linux/unwind_deferred_types.h | 8 +++++++-
kernel/unwind/deferred.c | 26 ++++++++++++++++++++------
2 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_deferred_types.h
index 9749824aea09..6f71a06329fb 100644
--- a/include/linux/unwind_deferred_types.h
+++ b/include/linux/unwind_deferred_types.h
@@ -2,8 +2,14 @@
#ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H
#define _LINUX_UNWIND_USER_DEFERRED_TYPES_H
-struct unwind_task_info {
+struct unwind_cache {
unsigned long *entries;
+ unsigned int nr_entries;
+ u64 cookie;
+};
+
+struct unwind_task_info {
+ struct unwind_cache cache;
u64 cookie;
};
diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c
index f0dbe4069247..2f38055cce48 100644
--- a/kernel/unwind/deferred.c
+++ b/kernel/unwind/deferred.c
@@ -56,6 +56,7 @@ static void unwind_deferred_task_work(struct callback_head *head)
{
struct unwind_work *work = container_of(head, struct unwind_work, work);
struct unwind_task_info *info = ¤t->unwind_info;
+ struct unwind_cache *cache = &info->cache;
struct unwind_stacktrace trace;
u64 cookie;
@@ -73,17 +74,30 @@ static void unwind_deferred_task_work(struct callback_head *head)
if (!current->mm)
goto do_callback;
- if (!info->entries) {
- info->entries = kmalloc(UNWIND_MAX_ENTRIES * sizeof(long),
- GFP_KERNEL);
- if (!info->entries)
+ if (!cache->entries) {
+ cache->entries = kmalloc(UNWIND_MAX_ENTRIES * sizeof(long),
+ GFP_KERNEL);
+ if (!cache->entries)
goto do_callback;
}
- trace.entries = info->entries;
+ trace.entries = cache->entries;
+
+ if (cookie == cache->cookie) {
+ /*
+ * The user stack has already been previously unwound in this
+ * entry context. Skip the unwind and use the cache.
+ */
+ trace.nr = cache->nr_entries;
+ goto do_callback;
+ }
+
trace.nr = 0;
unwind_user(&trace, UNWIND_MAX_ENTRIES);
+ cache->cookie = cookie;
+ cache->nr_entries = trace.nr;
+
do_callback:
work->func(work, &trace, cookie);
work->pending = 0;
@@ -174,5 +188,5 @@ void unwind_task_free(struct task_struct *task)
{
struct unwind_task_info *info = &task->unwind_info;
- kfree(info->entries);
+ kfree(info->cache.entries);
}
--
2.48.1
Powered by blists - more mailing lists