From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E10492F5473; Wed, 25 Jun 2025 22:57:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=216.40.44.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750892227; cv=none; b=HT/HAAYJNhyY2iIGJlnaZbX0LCfwSiDZEdWoUPKgCd/wu4h3ffD3SW3tDEm1YTLwJ5tFsOTL3OqXkOLWZ0ydw7wjkQ7Dgydi+QMq5pq1Qtuu9vV1V8HOfWkeY0ym/OAY2kvhhe5o3a2TB++rcuyKvlcIkxuktkUc8Sj5jldmbQw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750892227; c=relaxed/simple; bh=52zC1lsx1UYNw91cJfuuL4EeK9nBfr1g4uCuitlGZwM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=mgK/8ylYYJO/BWh2mKsPJI5VtKv/Detd6jhgBpULcvhIVuEWwtTrGw4FDliYLjkNu2v/2w3ngZpDC7Nx6rDLpK1Y9hL4TOuGRhtYfZWRvXQ59+uAvTF+AXSvl0OH8YMcSiIgIgyS5dFtzLbywChDVukmG4tuj2yfGhv/fGi6QiA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=goodmis.org; spf=pass smtp.mailfrom=goodmis.org; arc=none smtp.client-ip=216.40.44.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=goodmis.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=goodmis.org Received: from omf04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 56FB7BB4FF; Wed, 25 Jun 2025 22:56:53 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: nevets@goodmis.org) by omf04.hostedemail.com (Postfix) with ESMTPA id 1941F20023; Wed, 25 Jun 2025 22:56:50 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uUZ3L-000000043dp-1EmZ; Wed, 25 Jun 2025 18:57:15 -0400 Message-ID: <20250625225715.146840700@goodmis.org> User-Agent: quilt/0.68 Date: Wed, 25 Jun 2025 18:56:02 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, x86@kernel.org Cc: Masami Hiramatsu , Mathieu Desnoyers , Josh Poimboeuf , Peter Zijlstra , Ingo Molnar , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Andrii Nakryiko , Indu Bhagat , "Jose E. Marchesi" , Beau Belgrave , Jens Remus , Linus Torvalds , Andrew Morton , Jens Axboe Subject: [PATCH v11 02/14] unwind_user: Add frame pointer support References: <20250625225600.555017347@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Stat-Signature: gnuexgfyo3zmx8bn8jdh79767aw3c9ax X-Rspamd-Server: rspamout01 X-Rspamd-Queue-Id: 1941F20023 X-Session-Marker: 6E657665747340676F6F646D69732E6F7267 X-Session-ID: U2FsdGVkX18zTXika3H2Hb11Twt+Y8TYiE8U87EcB0w= X-HE-Tag: 1750892210-444175 X-HE-Meta: U2FsdGVkX1+UeYFRlLQ2ltMXZSYjfpTt6ouE8y4AqQzljeqCdrD1ACTNLIXPMKiwqfAy5STfr8NdnwX982rDAJTnXuccBVsy8iCjiM/H4Ow+7PS94lnXbSsBgByWuyoeHwp5u0tu43CyRiPskPr11KfCHsBRV3hcjCskiEfXzoHLEgu6uYPvhQu49fQ3I5jyXVGkmMKExC2sVjnuy/V5yffnx/o99KawEhHnu6rgC6wrq1+lvPgLPz91rcBfXXnmoeboUMb0DnonhZQ+kPIzYDDsAqyf9QiAxuhoSjfkctltXjpMluPu4CYxrsR2kIzMD/QkBCFwkukmrn652Q7r4eEgUbAtNJyR/44Hj9a+/s0L3Y0HzkH6N3M18UpUbbopsxhbKpWb45zpLS3jokzAhUMl7y7xIWp7KlNSD1TRCYc= From: Josh Poimboeuf Add optional support for user space frame pointer unwinding. If supported, the arch needs to enable CONFIG_HAVE_UNWIND_USER_FP and define ARCH_INIT_USER_FP_FRAME. By encoding the frame offsets in struct unwind_user_frame, much of this code can also be reused for future unwinder implementations like sframe. Signed-off-by: Josh Poimboeuf Co-developed-by: Steven Rostedt (Google) Signed-off-by: Steven Rostedt (Google) --- Changes since v10: https://lore.kernel.org/20250611010428.092934995@goodmis.org - Rename "the_end" label to "done" to be more consistent to what the kernel uses. (Peter Zijlstra) - Add a comment about what cfa and ra stand for. (Peter Zijlstra) arch/Kconfig | 4 +++ include/asm-generic/Kbuild | 1 + include/asm-generic/unwind_user.h | 5 +++ include/linux/unwind_user.h | 5 +++ include/linux/unwind_user_types.h | 1 + kernel/unwind/user.c | 51 +++++++++++++++++++++++++++++-- 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 include/asm-generic/unwind_user.h diff --git a/arch/Kconfig b/arch/Kconfig index ea59e5d7cc69..8e3fd723bd74 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -438,6 +438,10 @@ config HAVE_HARDLOCKUP_DETECTOR_ARCH config UNWIND_USER bool +config HAVE_UNWIND_USER_FP + bool + select UNWIND_USER + config HAVE_PERF_REGS bool help diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild index 8675b7b4ad23..295c94a3ccc1 100644 --- a/include/asm-generic/Kbuild +++ b/include/asm-generic/Kbuild @@ -59,6 +59,7 @@ mandatory-y += tlbflush.h mandatory-y += topology.h mandatory-y += trace_clock.h mandatory-y += uaccess.h +mandatory-y += unwind_user.h mandatory-y += vermagic.h mandatory-y += vga.h mandatory-y += video.h diff --git a/include/asm-generic/unwind_user.h b/include/asm-generic/unwind_user.h new file mode 100644 index 000000000000..b8882b909944 --- /dev/null +++ b/include/asm-generic/unwind_user.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_UNWIND_USER_H +#define _ASM_GENERIC_UNWIND_USER_H + +#endif /* _ASM_GENERIC_UNWIND_USER_H */ diff --git a/include/linux/unwind_user.h b/include/linux/unwind_user.h index aa7923c1384f..a405111c41b0 100644 --- a/include/linux/unwind_user.h +++ b/include/linux/unwind_user.h @@ -3,6 +3,11 @@ #define _LINUX_UNWIND_USER_H #include +#include + +#ifndef ARCH_INIT_USER_FP_FRAME + #define ARCH_INIT_USER_FP_FRAME +#endif int unwind_user_start(struct unwind_user_state *state); int unwind_user_next(struct unwind_user_state *state); diff --git a/include/linux/unwind_user_types.h b/include/linux/unwind_user_types.h index 6ed1b4ae74e1..65bd070eb6b0 100644 --- a/include/linux/unwind_user_types.h +++ b/include/linux/unwind_user_types.h @@ -6,6 +6,7 @@ enum unwind_user_type { UNWIND_USER_TYPE_NONE, + UNWIND_USER_TYPE_FP, }; struct unwind_stacktrace { diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c index d30449328981..1201d655654a 100644 --- a/kernel/unwind/user.c +++ b/kernel/unwind/user.c @@ -6,10 +6,54 @@ #include #include #include +#include + +static struct unwind_user_frame fp_frame = { + ARCH_INIT_USER_FP_FRAME +}; + +static inline bool fp_state(struct unwind_user_state *state) +{ + return IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP) && + state->type == UNWIND_USER_TYPE_FP; +} int unwind_user_next(struct unwind_user_state *state) { - /* no implementation yet */ + struct unwind_user_frame *frame; + unsigned long cfa = 0, fp, ra = 0; + + if (state->done) + return -EINVAL; + + if (fp_state(state)) + frame = &fp_frame; + else + goto done; + + /* Get the Canonical Frame Address (CFA) */ + cfa = (frame->use_fp ? state->fp : state->sp) + frame->cfa_off; + + /* stack going in wrong direction? */ + if (cfa <= state->sp) + goto done; + + /* Find the Return Address (RA) */ + if (get_user(ra, (unsigned long *)(cfa + frame->ra_off))) + goto done; + + if (frame->fp_off && get_user(fp, (unsigned long __user *)(cfa + frame->fp_off))) + goto done; + + state->ip = ra; + state->sp = cfa; + if (frame->fp_off) + state->fp = fp; + + return 0; + +done: + state->done = true; return -EINVAL; } @@ -24,7 +68,10 @@ int unwind_user_start(struct unwind_user_state *state) return -EINVAL; } - state->type = UNWIND_USER_TYPE_NONE; + if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP)) + state->type = UNWIND_USER_TYPE_FP; + else + state->type = UNWIND_USER_TYPE_NONE; state->ip = instruction_pointer(regs); state->sp = user_stack_pointer(regs); -- 2.47.2