From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 688DA1E2838; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; cv=none; b=Vl+qTiUEp4z2oBdltZ/OeuOGqO+cyc91qxy8OaPumf3yCGtyJ6tM53JP5KlwEgCegHHkI5y3wwUgVBY8s6lR8fAtyFjGrxf7xzzslnGXvZR8DE6J+7F76zkM09rGePDbaQ4I5o0suzSDJ6HQbQiHsWPkyzmsUNm0Lao3FwSWm0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; c=relaxed/simple; bh=Jou/EsDJ2wcQGX4ZAo7Tpn6oE93Ilh584sRGb96jknA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=VoxKmzNxqx6KLbTWWxs2zfazOXJriDrs01iKDK9xYETMfZuy2WwnO52y5Xt816hwa4YzdrvyzDAtT+WBMSOx4RL7toKfgD2Je/Az47z5b8gqBobWZJapuV2PQ7+qWtteWUHcN81VJ1BCn70dNvV9kOj/nbqPtLFxP7r8obNV+9I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iNxErGid; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iNxErGid" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6E2BC4CEF5; Tue, 8 Jul 2025 01:23:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937837; bh=Jou/EsDJ2wcQGX4ZAo7Tpn6oE93Ilh584sRGb96jknA=; h=Date:From:To:Cc:Subject:References:From; b=iNxErGidMD470AqiPxX9E0nD+uKeLnfxsnD2tKVt3INeXATSVWkQPxK0LwF8+usdb rFi+Xee1lmfqe0mY7toTQXuzbH3eFjd12e3HCZNsj7ZgdebxXzLdGKbeudbyLsnJhf 0I4e3zWPhUJ0dK143k8MVSDs7c4Qs+flWH9iZUDKkL2RhZWFmj6F8COsLvRQXGRly7 8Efmk8YwpqF0O3WI4kK1JtwkDUBLxZ1g23j0+862COscw1KaNxzVbjp0Iil+G5ZMwT GRHeSqsS54vVQJEtIBUEfEFZyUZcrDBK93R08vsQSc0LjtCCFukolVaNJkwlbozuwF NptugoP1UDRSQ== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3t-00000000BpW-42Av; Mon, 07 Jul 2025 21:23:57 -0400 Message-ID: <20250708012357.814042587@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:40 -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 , Florian Weimer , Sam James Subject: [PATCH v13 01/14] unwind_user: Add user space unwinding API References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Introduce a generic API for unwinding user stacks. In order to expand user space unwinding to be able to handle more complex scenarios, such as deferred unwinding and reading user space information, create a generic interface that all architectures can use that support the various unwinding methods. This is an alternative method for handling user space stack traces from the simple stack_trace_save_user() API. This does not replace that interface, but this interface will be used to expand the functionality of user space stack walking. None of the structures introduced will be exposed to user space tooling. Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- Changes since v12: https://lore.kernel.org/20250701005450.721228270@goodmis= .org - Make unwind_user_start() and unwind_user_next() static. There's no reason that they need to be used by other files. - Move for_each_user_frame() macro into user.c - Remove extra parenthesis around start in for_each_user_frame() macro (Mathieu Desnoyers) MAINTAINERS | 8 +++++ arch/Kconfig | 3 ++ include/linux/unwind_user.h | 9 +++++ include/linux/unwind_user_types.h | 31 +++++++++++++++++ kernel/Makefile | 1 + kernel/unwind/Makefile | 1 + kernel/unwind/user.c | 58 +++++++++++++++++++++++++++++++ 7 files changed, 111 insertions(+) create mode 100644 include/linux/unwind_user.h create mode 100644 include/linux/unwind_user_types.h create mode 100644 kernel/unwind/Makefile create mode 100644 kernel/unwind/user.c diff --git a/MAINTAINERS b/MAINTAINERS index fad6cb025a19..370d780fd5f8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -25928,6 +25928,14 @@ F: Documentation/driver-api/uio-howto.rst F: drivers/uio/ F: include/linux/uio_driver.h =20 +USERSPACE STACK UNWINDING +M: Josh Poimboeuf +M: Steven Rostedt +S: Maintained +F: include/linux/unwind*.h +F: kernel/unwind/ + + UTIL-LINUX PACKAGE M: Karel Zak L: util-linux@vger.kernel.org diff --git a/arch/Kconfig b/arch/Kconfig index a3308a220f86..ea59e5d7cc69 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -435,6 +435,9 @@ config HAVE_HARDLOCKUP_DETECTOR_ARCH It uses the same command line parameters, and sysctl interface, as the generic hardlockup detectors. =20 +config UNWIND_USER + bool + config HAVE_PERF_REGS bool help diff --git a/include/linux/unwind_user.h b/include/linux/unwind_user.h new file mode 100644 index 000000000000..1d77bed8de2c --- /dev/null +++ b/include/linux/unwind_user.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_UNWIND_USER_H +#define _LINUX_UNWIND_USER_H + +#include + +int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries); + +#endif /* _LINUX_UNWIND_USER_H */ diff --git a/include/linux/unwind_user_types.h b/include/linux/unwind_user_= types.h new file mode 100644 index 000000000000..6ed1b4ae74e1 --- /dev/null +++ b/include/linux/unwind_user_types.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_UNWIND_USER_TYPES_H +#define _LINUX_UNWIND_USER_TYPES_H + +#include + +enum unwind_user_type { + UNWIND_USER_TYPE_NONE, +}; + +struct unwind_stacktrace { + unsigned int nr; + unsigned long *entries; +}; + +struct unwind_user_frame { + s32 cfa_off; + s32 ra_off; + s32 fp_off; + bool use_fp; +}; + +struct unwind_user_state { + unsigned long ip; + unsigned long sp; + unsigned long fp; + enum unwind_user_type type; + bool done; +}; + +#endif /* _LINUX_UNWIND_USER_TYPES_H */ diff --git a/kernel/Makefile b/kernel/Makefile index 32e80dd626af..541186050251 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -55,6 +55,7 @@ obj-y +=3D rcu/ obj-y +=3D livepatch/ obj-y +=3D dma/ obj-y +=3D entry/ +obj-y +=3D unwind/ obj-$(CONFIG_MODULES) +=3D module/ =20 obj-$(CONFIG_KCMP) +=3D kcmp.o diff --git a/kernel/unwind/Makefile b/kernel/unwind/Makefile new file mode 100644 index 000000000000..349ce3677526 --- /dev/null +++ b/kernel/unwind/Makefile @@ -0,0 +1 @@ + obj-$(CONFIG_UNWIND_USER) +=3D user.o diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c new file mode 100644 index 000000000000..d0cf3ee2706d --- /dev/null +++ b/kernel/unwind/user.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Generic interfaces for unwinding user space +*/ +#include +#include +#include +#include + +#define for_each_user_frame(state) \ + for (unwind_user_start(state); !(state)->done; unwind_user_next(state)) + +static int unwind_user_next(struct unwind_user_state *state) +{ + /* no implementation yet */ + return -EINVAL; +} + +static int unwind_user_start(struct unwind_user_state *state) +{ + struct pt_regs *regs =3D task_pt_regs(current); + + memset(state, 0, sizeof(*state)); + + if ((current->flags & PF_KTHREAD) || !user_mode(regs)) { + state->done =3D true; + return -EINVAL; + } + + state->type =3D UNWIND_USER_TYPE_NONE; + + state->ip =3D instruction_pointer(regs); + state->sp =3D user_stack_pointer(regs); + state->fp =3D frame_pointer(regs); + + return 0; +} + +int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries) +{ + struct unwind_user_state state; + + trace->nr =3D 0; + + if (!max_entries) + return -EINVAL; + + if (current->flags & PF_KTHREAD) + return 0; + + for_each_user_frame(&state) { + trace->entries[trace->nr++] =3D state.ip; + if (trace->nr >=3D max_entries) + break; + } + + return 0; +} --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3211E35893; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; cv=none; b=MRvnhRomKHL7QboTLysNFlVl3nwrHfQD9eoK0KOgH06i5Ja++kDEaIC1w1LPuwRv3NPfXEkBRyeKWJYXU6N58PwVMY9zuy4G1FJKPOyz4GEXH8cHFrOQsuH6AGwFB9pYzQp3C8mxAFCslv67dpJyv1crK3KU3sbJgnJErEoLOwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; c=relaxed/simple; bh=aN8l2NtS9Oba9cJOPW3Q9OnFBUI6l6LjWtk/C1O7k5w=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=a4FzgBqrvrQUZpW63hnaqEs6XLn55pu67ekjkNMJB15zAK2B7XOjBK2s1cUV5P2dfsGLn+GYo0WfWo2NBY6vbWYNPy/NtLyxcIybCqj7SCccKRebvRvhUxbQzHcmFpZJ+rWFUA1NDy8y9hw7RMGe2xpHwj77VpsWFvxjd5yZEdc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ge+s6SrG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ge+s6SrG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6606C4CEF4; Tue, 8 Jul 2025 01:23:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937838; bh=aN8l2NtS9Oba9cJOPW3Q9OnFBUI6l6LjWtk/C1O7k5w=; h=Date:From:To:Cc:Subject:References:From; b=Ge+s6SrGDoYX7jmof8Ey6SY4sX3JaZYAukf71gTKevMu4R3kBDb09MQgv5HgB/9zS DGvuNdEbB1rlqgpyKWCEbB7ZkmDB42D5MRaRTECkzIHKIgS/A/uTMcp07Ye/QJX3Vp gbZD7dj7Ahn5pcybQNFtcmSpxzPuYUgaK6TlUALQrtKliP9y4GWxJLdoxfEvXpwwnN bwC5xjxEJXY/xe4CyEwUJIWrnHtQI5mdpDGJ/WvWezd4A7j7NSROFsnXX4XtZOzpHq +xAduRJm1BCRObcEXL32vJuUAI7FAgsvSjNifHTo3jzgM+h1sy1+QJh+w00pzTtaBz fQrFHqhuawD1g== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3u-00000000Bq0-0Xzw; Mon, 07 Jul 2025 21:23:58 -0400 Message-ID: <20250708012357.982692711@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:41 -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 , Florian Weimer , Sam James Subject: [PATCH v13 02/14] unwind_user: Add frame pointer support References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 v12: https://lore.kernel.org/20250701005450.888492528@goodmis= .org - Added test when use_fp is true to make sure fp < sp (Jens Remus) - Make sure the address read is word aligned (Linus Torvalds) 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 | 65 ++++++++++++++++++++++++++++++- 6 files changed, 79 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 =20 +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 +=3D tlbflush.h mandatory-y +=3D topology.h mandatory-y +=3D trace_clock.h mandatory-y +=3D uaccess.h +mandatory-y +=3D unwind_user.h mandatory-y +=3D vermagic.h mandatory-y +=3D vga.h mandatory-y +=3D 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 1d77bed8de2c..7f7282516bf5 100644 --- a/include/linux/unwind_user.h +++ b/include/linux/unwind_user.h @@ -3,6 +3,11 @@ #define _LINUX_UNWIND_USER_H =20 #include +#include + +#ifndef ARCH_INIT_USER_FP_FRAME + #define ARCH_INIT_USER_FP_FRAME +#endif =20 int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries); =20 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 @@ =20 enum unwind_user_type { UNWIND_USER_TYPE_NONE, + UNWIND_USER_TYPE_FP, }; =20 struct unwind_stacktrace { diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c index d0cf3ee2706d..62b3ef37d71b 100644 --- a/kernel/unwind/user.c +++ b/kernel/unwind/user.c @@ -6,13 +6,71 @@ #include #include #include +#include + +static struct unwind_user_frame fp_frame =3D { + 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 =3D=3D UNWIND_USER_TYPE_FP; +} =20 #define for_each_user_frame(state) \ for (unwind_user_start(state); !(state)->done; unwind_user_next(state)) =20 static int unwind_user_next(struct unwind_user_state *state) { - /* no implementation yet */ + struct unwind_user_frame *frame; + unsigned long cfa =3D 0, fp, ra =3D 0; + unsigned int shift; + + if (state->done) + return -EINVAL; + + if (fp_state(state)) + frame =3D &fp_frame; + else + goto done; + + if (frame->use_fp) { + if (state->fp < state->sp) + goto done; + cfa =3D state->fp; + } else { + cfa =3D state->sp; + } + + /* Get the Canonical Frame Address (CFA) */ + cfa +=3D frame->cfa_off; + + /* stack going in wrong direction? */ + if (cfa <=3D state->sp) + goto done; + + /* Make sure that the address is word aligned */ + shift =3D sizeof(long) =3D=3D 4 ? 2 : 3; + if ((cfa + frame->ra_off) & ((1 << shift) - 1)) + 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->f= p_off))) + goto done; + + state->ip =3D ra; + state->sp =3D cfa; + if (frame->fp_off) + state->fp =3D fp; + + return 0; + +done: + state->done =3D true; return -EINVAL; } =20 @@ -27,7 +85,10 @@ static int unwind_user_start(struct unwind_user_state *s= tate) return -EINVAL; } =20 - state->type =3D UNWIND_USER_TYPE_NONE; + if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP)) + state->type =3D UNWIND_USER_TYPE_FP; + else + state->type =3D UNWIND_USER_TYPE_NONE; =20 state->ip =3D instruction_pointer(regs); state->sp =3D user_stack_pointer(regs); --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 86F1E1EA7C6; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; cv=none; b=PxGxND3uIqJKX53lxQSlv0g0P8LmFfI3cBJZ9hqhm+kSFonPoMbLbYdsfZWn8diVjmybMZ5JnQdh3/o+gYEDALESbKvwdhaHR6CtCmrp79f/EH/9nImJUFfwebML2tnG0qx5F1wzF7RaMBMn/+MKwvWNaA9YyOBrbBN5I1s4KYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; c=relaxed/simple; bh=dfL0gbtknedEceNctRgbDHaSJtmSBoNoNXZNsBMMW1c=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=GUqoueBLA1TYN8iDlH1lq6aqs38UwEpE3RQggagGsSHt8mkK/j6c7PXkcqW3Cff+zvx5yq/ynLYmogI5o3FQpKuZKJM/ILhikp82jVgXV2NbQZwMagOetiZS7L/u/ictNDD44BPrzk7kq6zhzPT5uzR18Q1hJ03QMqnMu8m9gAU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uqpRwcK/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uqpRwcK/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C520C16AAE; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937838; bh=dfL0gbtknedEceNctRgbDHaSJtmSBoNoNXZNsBMMW1c=; h=Date:From:To:Cc:Subject:References:From; b=uqpRwcK/bG9SZp/VXcJjnELvjDQs2ILRMig+MhPpf02DPs1OSEdylEqZAyVHZmp5y r1j96iJnvSRAn5PTBi6gUm8lRJdc+MhVuv8M1haurFp3N/v6/psTM5pPiFbTID1mK2 L/WF9Uv/mLJeXcV/rBhDLksduyLVp/EIJ8RrkvnCDFrynMT6nG4QW0X9VbemV7Vd/+ lwgrtcpPlJ1fSILMhQTZTuizVdERTy/VnSrtYvnql44hBfOzPvlRMjC5jVCNUf553d sC71V8WBZrO2HRss1oP1vpOqQb1LIEB37nKWMEbAEjArfAXjj8wN+k0lt814G2QFzj AMhU/834O6c1Q== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3u-00000000BqU-1Gcz; Mon, 07 Jul 2025 21:23:58 -0400 Message-ID: <20250708012358.152451558@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:42 -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 , Florian Weimer , Sam James Subject: [PATCH v13 03/14] unwind_user: Add compat mode frame pointer support References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Add optional support for user space compat mode frame pointer unwinding. If supported, the arch needs to enable CONFIG_HAVE_UNWIND_USER_COMPAT_FP and define ARCH_INIT_USER_COMPAT_FP_FRAME. Signed-off-by: Josh Poimboeuf Co-developed-by: Steven Rostedt (Google) Signed-off-by: Steven Rostedt (Google) --- Changes since v12: https://lore.kernel.org/20250701005451.055982038@goodmis= .org - With new alignment check, updated to handle compat mode. arch/Kconfig | 4 +++ include/asm-generic/Kbuild | 1 + include/asm-generic/unwind_user_types.h | 5 ++++ include/linux/unwind_user.h | 5 ++++ include/linux/unwind_user_types.h | 7 +++++ kernel/unwind/user.c | 34 +++++++++++++++++++++---- 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 include/asm-generic/unwind_user_types.h diff --git a/arch/Kconfig b/arch/Kconfig index 8e3fd723bd74..2c41d3072910 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -442,6 +442,10 @@ config HAVE_UNWIND_USER_FP bool select UNWIND_USER =20 +config HAVE_UNWIND_USER_COMPAT_FP + bool + depends on HAVE_UNWIND_USER_FP + config HAVE_PERF_REGS bool help diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild index 295c94a3ccc1..b797a2434396 100644 --- a/include/asm-generic/Kbuild +++ b/include/asm-generic/Kbuild @@ -60,6 +60,7 @@ mandatory-y +=3D topology.h mandatory-y +=3D trace_clock.h mandatory-y +=3D uaccess.h mandatory-y +=3D unwind_user.h +mandatory-y +=3D unwind_user_types.h mandatory-y +=3D vermagic.h mandatory-y +=3D vga.h mandatory-y +=3D video.h diff --git a/include/asm-generic/unwind_user_types.h b/include/asm-generic/= unwind_user_types.h new file mode 100644 index 000000000000..f568b82e52cd --- /dev/null +++ b/include/asm-generic/unwind_user_types.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_UNWIND_USER_TYPES_H +#define _ASM_GENERIC_UNWIND_USER_TYPES_H + +#endif /* _ASM_GENERIC_UNWIND_USER_TYPES_H */ diff --git a/include/linux/unwind_user.h b/include/linux/unwind_user.h index 7f7282516bf5..834b643afd3a 100644 --- a/include/linux/unwind_user.h +++ b/include/linux/unwind_user.h @@ -9,6 +9,11 @@ #define ARCH_INIT_USER_FP_FRAME #endif =20 +#ifndef ARCH_INIT_USER_COMPAT_FP_FRAME + #define ARCH_INIT_USER_COMPAT_FP_FRAME + #define in_compat_mode(regs) false +#endif + int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries); =20 #endif /* _LINUX_UNWIND_USER_H */ diff --git a/include/linux/unwind_user_types.h b/include/linux/unwind_user_= types.h index 65bd070eb6b0..0b6563951ca4 100644 --- a/include/linux/unwind_user_types.h +++ b/include/linux/unwind_user_types.h @@ -3,10 +3,16 @@ #define _LINUX_UNWIND_USER_TYPES_H =20 #include +#include + +#ifndef arch_unwind_user_state +struct arch_unwind_user_state {}; +#endif =20 enum unwind_user_type { UNWIND_USER_TYPE_NONE, UNWIND_USER_TYPE_FP, + UNWIND_USER_TYPE_COMPAT_FP, }; =20 struct unwind_stacktrace { @@ -25,6 +31,7 @@ struct unwind_user_state { unsigned long ip; unsigned long sp; unsigned long fp; + struct arch_unwind_user_state arch; enum unwind_user_type type; bool done; }; diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c index 62b3ef37d71b..03775191447c 100644 --- a/kernel/unwind/user.c +++ b/kernel/unwind/user.c @@ -12,6 +12,10 @@ static struct unwind_user_frame fp_frame =3D { ARCH_INIT_USER_FP_FRAME }; =20 +static struct unwind_user_frame compat_fp_frame =3D { + ARCH_INIT_USER_COMPAT_FP_FRAME +}; + static inline bool fp_state(struct unwind_user_state *state) { return IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP) && @@ -21,6 +25,22 @@ static inline bool fp_state(struct unwind_user_state *st= ate) #define for_each_user_frame(state) \ for (unwind_user_start(state); !(state)->done; unwind_user_next(state)) =20 +static inline bool compat_fp_state(struct unwind_user_state *state) +{ + return IS_ENABLED(CONFIG_HAVE_UNWIND_USER_COMPAT_FP) && + state->type =3D=3D UNWIND_USER_TYPE_COMPAT_FP; +} + +#define unwind_get_user_long(to, from, state) \ +({ \ + int __ret; \ + if (compat_fp_state(state)) \ + __ret =3D get_user(to, (u32 __user *)(from)); \ + else \ + __ret =3D get_user(to, (unsigned long __user *)(from)); \ + __ret; \ +}) + static int unwind_user_next(struct unwind_user_state *state) { struct unwind_user_frame *frame; @@ -30,7 +50,9 @@ static int unwind_user_next(struct unwind_user_state *sta= te) if (state->done) return -EINVAL; =20 - if (fp_state(state)) + if (compat_fp_state(state)) + frame =3D &compat_fp_frame; + else if (fp_state(state)) frame =3D &fp_frame; else goto done; @@ -51,15 +73,15 @@ static int unwind_user_next(struct unwind_user_state *s= tate) goto done; =20 /* Make sure that the address is word aligned */ - shift =3D sizeof(long) =3D=3D 4 ? 2 : 3; + shift =3D sizeof(long) =3D=3D 4 || compat_fp_state(state) ? 2 : 3; if ((cfa + frame->ra_off) & ((1 << shift) - 1)) goto done; =20 /* Find the Return Address (RA) */ - if (get_user(ra, (unsigned long *)(cfa + frame->ra_off))) + if (unwind_get_user_long(ra, cfa + frame->ra_off, state)) goto done; =20 - if (frame->fp_off && get_user(fp, (unsigned long __user *)(cfa + frame->f= p_off))) + if (frame->fp_off && unwind_get_user_long(fp, cfa + frame->fp_off, state)) goto done; =20 state->ip =3D ra; @@ -85,7 +107,9 @@ static int unwind_user_start(struct unwind_user_state *s= tate) return -EINVAL; } =20 - if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP)) + if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_COMPAT_FP) && in_compat_mode(regs)) + state->type =3D UNWIND_USER_TYPE_COMPAT_FP; + else if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP)) state->type =3D UNWIND_USER_TYPE_FP; else state->type =3D UNWIND_USER_TYPE_NONE; --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 86E901E9B0D; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; cv=none; b=jURwpy05YWyLsq8ArRZupsE86Ih7Dukjg/HvyFJijZnFKclBWdotPFmS7cIsiCSPcSyq6SEAlsAr9eZHtAxN35wygrwze1ORTjLdNJpqtHBjAaG0Ts1PGSpBzxbLxyjAJK/qLzhGEky4AehQDzOpTQQq9cob12jlRZLL3qwu7qI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; c=relaxed/simple; bh=D6WzydJYTzpIkcTqf6Eb08Cdq6+JZu7JrzcdUkXXQ6w=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=r7VHz0Pm6AvHldfswqZlt/Z9IpOnmsVdvH28bRQQY4fjdvnNcNfzCwhmHqfvbT2uDAV5Hb/Uita8iHGBL6WuGoRFoGESqFlDKVqHkTaqRvdDixHrG7565DLz+DTOkcKMH4tySy2Qp7Undu2mirinbXX2Y8yys+va7x1SqccOUpw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ABDMBJmw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ABDMBJmw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EABBC4CEF8; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937838; bh=D6WzydJYTzpIkcTqf6Eb08Cdq6+JZu7JrzcdUkXXQ6w=; h=Date:From:To:Cc:Subject:References:From; b=ABDMBJmwX4CnEh9EFnL54bZ70qvumIsZ44TiubExWDMncvVtdU11jfS6V7IBWf1VG aCgWwnreX8iQcEqGhsCSmnxhIa6Iex2DwRx0SU3dyNSQoV/uas4kk67yCxNHSg8d6H +4pnHmjKsyhpVGKniuO2oj9eArtc5zjuYQ7Eoi5S+A9gbIWA7Sgrl8v/h1NEgiN5ph yWw/7nSrYPI/Wt71pTrrCgahj+4kvp4kMPX82SaSl1dRS3/91xONcNBEFJwJ9HqXNt 7VPd3Yf/4+k1ZuyREfPyk3Zi9rm5sEQgMKkuRX21Mo3QqjbNfZT+kJbzz+lUUltCcu xV2BSbptYvxKQ== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3u-00000000Bqy-1xvP; Mon, 07 Jul 2025 21:23:58 -0400 Message-ID: <20250708012358.323165125@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:43 -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 , Florian Weimer , Sam James Subject: [PATCH v13 04/14] unwind_user/deferred: Add unwind_user_faultable() References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt Add a new API to retrieve a user space callstack called unwind_user_faultable(). The difference between this user space stack tracer from the current user space stack tracer is that this must be called from faultable context as it may use routines to access user space data that needs to be faulted in. It can be safely called from entering or exiting a system call as the code can still be faulted in there. This code is based on work by Josh Poimboeuf's deferred unwinding code: Link: https://lore.kernel.org/all/6052e8487746603bdb29b65f4033e739092d9925.= 1737511963.git.jpoimboe@kernel.org/ Signed-off-by: Steven Rostedt (Google) --- include/linux/sched.h | 5 +++ include/linux/unwind_deferred.h | 24 +++++++++++ include/linux/unwind_deferred_types.h | 9 ++++ kernel/fork.c | 4 ++ kernel/unwind/Makefile | 2 +- kernel/unwind/deferred.c | 60 +++++++++++++++++++++++++++ 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 include/linux/unwind_deferred.h create mode 100644 include/linux/unwind_deferred_types.h create mode 100644 kernel/unwind/deferred.c diff --git a/include/linux/sched.h b/include/linux/sched.h index 4f78a64beb52..59fdf7d9bb1e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -46,6 +46,7 @@ #include #include #include +#include #include =20 /* task_struct member predeclarations (sorted alphabetically): */ @@ -1654,6 +1655,10 @@ struct task_struct { struct user_event_mm *user_event_mm; #endif =20 +#ifdef CONFIG_UNWIND_USER + struct unwind_task_info unwind_info; +#endif + /* CPU-specific state of this task: */ struct thread_struct thread; =20 diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h new file mode 100644 index 000000000000..a5f6e8f8a1a2 --- /dev/null +++ b/include/linux/unwind_deferred.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_UNWIND_USER_DEFERRED_H +#define _LINUX_UNWIND_USER_DEFERRED_H + +#include +#include + +#ifdef CONFIG_UNWIND_USER + +void unwind_task_init(struct task_struct *task); +void unwind_task_free(struct task_struct *task); + +int unwind_user_faultable(struct unwind_stacktrace *trace); + +#else /* !CONFIG_UNWIND_USER */ + +static inline void unwind_task_init(struct task_struct *task) {} +static inline void unwind_task_free(struct task_struct *task) {} + +static inline int unwind_user_faultable(struct unwind_stacktrace *trace) {= return -ENOSYS; } + +#endif /* !CONFIG_UNWIND_USER */ + +#endif /* _LINUX_UNWIND_USER_DEFERRED_H */ diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h new file mode 100644 index 000000000000..aa32db574e43 --- /dev/null +++ b/include/linux/unwind_deferred_types.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H +#define _LINUX_UNWIND_USER_DEFERRED_TYPES_H + +struct unwind_task_info { + unsigned long *entries; +}; + +#endif /* _LINUX_UNWIND_USER_DEFERRED_TYPES_H */ diff --git a/kernel/fork.c b/kernel/fork.c index 1ee8eb11f38b..3341d50c61f2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -105,6 +105,7 @@ #include #include #include +#include =20 #include #include @@ -732,6 +733,7 @@ void __put_task_struct(struct task_struct *tsk) WARN_ON(refcount_read(&tsk->usage)); WARN_ON(tsk =3D=3D current); =20 + unwind_task_free(tsk); sched_ext_free(tsk); io_uring_free(tsk); cgroup_free(tsk); @@ -2135,6 +2137,8 @@ __latent_entropy struct task_struct *copy_process( p->bpf_ctx =3D NULL; #endif =20 + unwind_task_init(p); + /* Perform scheduler related setup. Assign this task to a CPU. */ retval =3D sched_fork(clone_flags, p); if (retval) diff --git a/kernel/unwind/Makefile b/kernel/unwind/Makefile index 349ce3677526..eae37bea54fd 100644 --- a/kernel/unwind/Makefile +++ b/kernel/unwind/Makefile @@ -1 +1 @@ - obj-$(CONFIG_UNWIND_USER) +=3D user.o + obj-$(CONFIG_UNWIND_USER) +=3D user.o deferred.o diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c new file mode 100644 index 000000000000..a0badbeb3cc1 --- /dev/null +++ b/kernel/unwind/deferred.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Deferred user space unwinding + */ +#include +#include +#include +#include + +#define UNWIND_MAX_ENTRIES 512 + +/** + * unwind_user_faultable - Produce a user stacktrace in faultable context + * @trace: The descriptor that will store the user stacktrace + * + * This must be called in a known faultable context (usually when entering + * or exiting user space). Depending on the available implementations + * the @trace will be loaded with the addresses of the user space stacktra= ce + * if it can be found. + * + * Return: 0 on success and negative on error + * On success @trace will contain the user space stacktrace + */ +int unwind_user_faultable(struct unwind_stacktrace *trace) +{ + struct unwind_task_info *info =3D ¤t->unwind_info; + + /* Should always be called from faultable context */ + might_fault(); + + if (current->flags & PF_EXITING) + return -EINVAL; + + if (!info->entries) { + info->entries =3D kmalloc_array(UNWIND_MAX_ENTRIES, sizeof(long), + GFP_KERNEL); + if (!info->entries) + return -ENOMEM; + } + + trace->nr =3D 0; + trace->entries =3D info->entries; + unwind_user(trace, UNWIND_MAX_ENTRIES); + + return 0; +} + +void unwind_task_init(struct task_struct *task) +{ + struct unwind_task_info *info =3D &task->unwind_info; + + memset(info, 0, sizeof(*info)); +} + +void unwind_task_free(struct task_struct *task) +{ + struct unwind_task_info *info =3D &task->unwind_info; + + kfree(info->entries); +} --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 ADB841F130B; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; cv=none; b=Ry3ShHj01RyeBvEQv5u6V30GMfEkWSX/xODY0EfH9q+y4IweMHiftagwDbu0KzLgMJr+Q85UxfcupplxCsv6Gc2WDFpBdRfyUfXAMnQ8ZSPnX0F5RRfaPa/YBm6Tq4IpVaw/f7yTzOkJPcsRmQB7XDD/Tao78N3TrVKaPvYcbFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; c=relaxed/simple; bh=khOhKXtWFZeIJYQHz9mkKupo2UshQTC5+3EPNwqv7Gk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=PLrM7n/xPHQvHAO7RJeWrmHjc+kVR4FMtWu5jN3XzUI3qh5nxZtv6deRZSvBLH0fGFdaGuwBE1Jw8O2O2JQheX2oxanntgF6R1rHxrmF7D3+pTWfI5HEfy31SgZ8cRSQeDSdCAKk/9dq1AjuMXHqoeElJE6C/C65ymZr5f/y1Qs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rfK1gj6t; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rfK1gj6t" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7FC39C4CEFB; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937838; bh=khOhKXtWFZeIJYQHz9mkKupo2UshQTC5+3EPNwqv7Gk=; h=Date:From:To:Cc:Subject:References:From; b=rfK1gj6tfUrHBMsE3+WEfzYFd1p6L5VA1WBRcCQR8L7xAdjhpYaq2dXT8CrV40dNl iJeAnYPkWtmB3EeleM7CiyLW9ijeuF8/UlWSPdd2K49fNOYci1PP3LlBnX6EuD4GRl MLRKjBJvZnYm6SEr0qObkzpMH6VX51bggZkDyxPEh5Z9jbFO+5CTGcSTsSUfuZuyQB U0QjALtrn3OtMjW72VkU/2/H21XPPx9d8MZLlV4VXyAm0vR0wT2Ws9FZyXoe1eBXBa 9w/BPJ6s16jnQQo5YMEQhrsGD9lYimRycHe5HQsPQ66ew3uf5sqKLD2tfI2DbWO96Z uRVgWBHGjN7Jw== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3u-00000000BrS-2gHI; Mon, 07 Jul 2025 21:23:58 -0400 Message-ID: <20250708012358.490721996@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:44 -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 , Florian Weimer , Sam James Subject: [PATCH v13 05/14] unwind_user/deferred: Add unwind cache References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Cache the results of the unwind to ensure the unwind is only performed once, even when called by multiple tracers. The cache nr_entries gets cleared every time the task exits the kernel. When a stacktrace is requested, nr_entries gets set to the number of entries in the stacktrace. If another stacktrace is requested, if nr_entries is not zero, then it contains the same stacktrace that would be retrieved so it is not processed again and the entries is given to the caller. Co-developed-by: Steven Rostedt (Google) Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- include/linux/entry-common.h | 2 ++ include/linux/unwind_deferred.h | 8 +++++++ include/linux/unwind_deferred_types.h | 7 +++++- kernel/unwind/deferred.c | 31 +++++++++++++++++++++------ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h index f94f3fdf15fc..8908b8eeb99b 100644 --- a/include/linux/entry-common.h +++ b/include/linux/entry-common.h @@ -12,6 +12,7 @@ #include #include #include +#include =20 #include #include @@ -362,6 +363,7 @@ static __always_inline void exit_to_user_mode(void) lockdep_hardirqs_on_prepare(); instrumentation_end(); =20 + unwind_reset_info(); user_enter_irqoff(); arch_exit_to_user_mode(); lockdep_hardirqs_on(CALLER_ADDR0); diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h index a5f6e8f8a1a2..baacf4a1eb4c 100644 --- a/include/linux/unwind_deferred.h +++ b/include/linux/unwind_deferred.h @@ -12,6 +12,12 @@ void unwind_task_free(struct task_struct *task); =20 int unwind_user_faultable(struct unwind_stacktrace *trace); =20 +static __always_inline void unwind_reset_info(void) +{ + if (unlikely(current->unwind_info.cache)) + current->unwind_info.cache->nr_entries =3D 0; +} + #else /* !CONFIG_UNWIND_USER */ =20 static inline void unwind_task_init(struct task_struct *task) {} @@ -19,6 +25,8 @@ static inline void unwind_task_free(struct task_struct *t= ask) {} =20 static inline int unwind_user_faultable(struct unwind_stacktrace *trace) {= return -ENOSYS; } =20 +static inline void unwind_reset_info(void) {} + #endif /* !CONFIG_UNWIND_USER */ =20 #endif /* _LINUX_UNWIND_USER_DEFERRED_H */ diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h index aa32db574e43..db5b54b18828 100644 --- a/include/linux/unwind_deferred_types.h +++ b/include/linux/unwind_deferred_types.h @@ -2,8 +2,13 @@ #ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H #define _LINUX_UNWIND_USER_DEFERRED_TYPES_H =20 +struct unwind_cache { + unsigned int nr_entries; + unsigned long entries[]; +}; + struct unwind_task_info { - unsigned long *entries; + struct unwind_cache *cache; }; =20 #endif /* _LINUX_UNWIND_USER_DEFERRED_TYPES_H */ diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index a0badbeb3cc1..96368a5aa522 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -4,10 +4,13 @@ */ #include #include +#include #include #include =20 -#define UNWIND_MAX_ENTRIES 512 +/* Make the cache fit in a 4K page */ +#define UNWIND_MAX_ENTRIES \ + ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) =20 /** * unwind_user_faultable - Produce a user stacktrace in faultable context @@ -24,6 +27,7 @@ int unwind_user_faultable(struct unwind_stacktrace *trace) { struct unwind_task_info *info =3D ¤t->unwind_info; + struct unwind_cache *cache; =20 /* Should always be called from faultable context */ might_fault(); @@ -31,17 +35,30 @@ int unwind_user_faultable(struct unwind_stacktrace *tra= ce) if (current->flags & PF_EXITING) return -EINVAL; =20 - if (!info->entries) { - info->entries =3D kmalloc_array(UNWIND_MAX_ENTRIES, sizeof(long), - GFP_KERNEL); - if (!info->entries) + if (!info->cache) { + info->cache =3D kzalloc(struct_size(cache, entries, UNWIND_MAX_ENTRIES), + GFP_KERNEL); + if (!info->cache) return -ENOMEM; } =20 + cache =3D info->cache; + trace->entries =3D cache->entries; + + if (cache->nr_entries) { + /* + * The user stack has already been previously unwound in this + * entry context. Skip the unwind and use the cache. + */ + trace->nr =3D cache->nr_entries; + return 0; + } + trace->nr =3D 0; - trace->entries =3D info->entries; unwind_user(trace, UNWIND_MAX_ENTRIES); =20 + cache->nr_entries =3D trace->nr; + return 0; } =20 @@ -56,5 +73,5 @@ void unwind_task_free(struct task_struct *task) { struct unwind_task_info *info =3D &task->unwind_info; =20 - kfree(info->entries); + kfree(info->cache); } --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C29551F91C8; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; cv=none; b=ubugdq/I0AmDVyTQ5BBN2FZj6KO51HKvuRkXrL3ol7Xj76OyEDOcOMGuAQrA4uZnD0RawoC693KW0wggZ71f+ts4+KkL1Wq56kk5pL3ZOpauxTiUOO+ZmKl0FxOnpqB0sMSf6vE8FPp8A6Qs093uRKYn50wet+bxejKtAK/NHyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937838; c=relaxed/simple; bh=qmTEwDoeegk7DD73bMEZwWggpkWe2bUx5X2J9A6ubDE=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=a/8CWOx1v7n+Y50CnA4tglAvUevnyyLPgBnc9LubyJZdmfe7NoBezDbHV2oEXnLB+doNABoy7kAHNG2mxTnKGG4wv19iJNlxv7hYWk4kjrLU5duXg3dbkD32twOpSLvvvYzHG/0x//fRRLUzHQFkmG2PsI5uW1e6obU+XXyw/0M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ABMpsFq5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ABMpsFq5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E4C0C116C6; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937838; bh=qmTEwDoeegk7DD73bMEZwWggpkWe2bUx5X2J9A6ubDE=; h=Date:From:To:Cc:Subject:References:From; b=ABMpsFq5ogBTw2mZdesdOlvilhqxiLOkUmMkmHEynEGEMsNoSxqH8MYB+ci5NBnd1 hylAfBbOO5rhPTnuWb+Ao/PO70RGc+YSEvX5zGmvxfIPdSOouHJ7b8YoBI23ApAVE4 thd5gD0BkW8NmiBbXJ7RS4ZxPaerRr7mbUULBaISKpO8m85XAQUJ5iTNw9rFLkZvMJ hkTO2N5ntt3Xi2hLWhO7Ucidc7d+hiYLyoBq2m3to/fptm92LbeZfiUQbKIqkkQkkY gt51ziYIUHX8uFmKd263Ple9rJRFli9kfZMwGogMObVy10zqsgSGfedtqHsu1NkiJe a7VRJWcDXcjrw== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3u-00000000Brw-3PCt; Mon, 07 Jul 2025 21:23:58 -0400 Message-ID: <20250708012358.659547758@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:45 -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 , Florian Weimer , Sam James Subject: [PATCH v13 06/14] unwind_user/deferred: Add deferred unwinding interface References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Add an interface for scheduling task work to unwind the user space stack before returning to user space. This solves several problems for its callers: - Ensure the unwind happens in task context even if the caller may be running in interrupt context. - Avoid duplicate unwinds, whether called multiple times by the same caller or by different callers. - Create a "context cookie" which allows trace post-processing to correlate kernel unwinds/traces with the user unwind. A concept of a "cookie" is created to detect when the stacktrace is the same. A cookie is generated the first time a user space stacktrace is requested after the task enters the kernel. As the stacktrace is saved on the task_struct while the task is in the kernel, if another request comes in, if the cookie is still the same, it will use the saved stacktrace, and not have to regenerate one. The cookie is passed to the caller on request, and when the stacktrace is generated upon returning to user space, it call the requester's callback with the cookie as well as the stacktrace. The cookie is cleared when it goes back to user space. Note, this currently adds another conditional to the unwind_reset_info() path that is always called returning to user space, but future changes will put this back to a single conditional. A global list is created and protected by a global mutex that holds tracers that register with the unwind infrastructure. The number of registered tracers will be limited in future changes. Each perf program or ftrace instance will register its own descriptor to use for deferred unwind stack traces. Note, in the function unwind_deferred_task_work() that gets called when returning to user space, it uses a global mutex for synchronization which will cause a big bottleneck. This will be replaced by SRCU, but that change adds some complex synchronization that deservers its own commit. Co-developed-by: Steven Rostedt (Google) Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- Changes since v12: https://lore.kernel.org/20250701005451.571473750@goodmis= .org - Replaced the timestamp with the generated cookie logic again. Instead of using a 64 bit word where the CPU part of the cookie is just 12 bits, make it two 32 bit words, where the CPU that the cookie is generated on is one word and the second word is just a per cpu counter. This allows for just using a 32 bit cmpxchg which works on all archs that have safe NMI cmpxchg. include/linux/unwind_deferred.h | 24 ++++ include/linux/unwind_deferred_types.h | 12 ++ kernel/unwind/deferred.c | 159 +++++++++++++++++++++++++- 3 files changed, 194 insertions(+), 1 deletion(-) diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h index baacf4a1eb4c..14efd8c027aa 100644 --- a/include/linux/unwind_deferred.h +++ b/include/linux/unwind_deferred.h @@ -2,9 +2,19 @@ #ifndef _LINUX_UNWIND_USER_DEFERRED_H #define _LINUX_UNWIND_USER_DEFERRED_H =20 +#include #include #include =20 +struct unwind_work; + +typedef void (*unwind_callback_t)(struct unwind_work *work, struct unwind_= stacktrace *trace, u64 cookie); + +struct unwind_work { + struct list_head list; + unwind_callback_t func; +}; + #ifdef CONFIG_UNWIND_USER =20 void unwind_task_init(struct task_struct *task); @@ -12,8 +22,19 @@ void unwind_task_free(struct task_struct *task); =20 int unwind_user_faultable(struct unwind_stacktrace *trace); =20 +int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func); +int unwind_deferred_request(struct unwind_work *work, u64 *cookie); +void unwind_deferred_cancel(struct unwind_work *work); + static __always_inline void unwind_reset_info(void) { + if (unlikely(current->unwind_info.id.id)) + current->unwind_info.id.id =3D 0; + /* + * As unwind_user_faultable() can be called directly and + * depends on nr_entries being cleared on exit to user, + * this needs to be a separate conditional. + */ if (unlikely(current->unwind_info.cache)) current->unwind_info.cache->nr_entries =3D 0; } @@ -24,6 +45,9 @@ static inline void unwind_task_init(struct task_struct *t= ask) {} static inline void unwind_task_free(struct task_struct *task) {} =20 static inline int unwind_user_faultable(struct unwind_stacktrace *trace) {= return -ENOSYS; } +static inline int unwind_deferred_init(struct unwind_work *work, unwind_ca= llback_t func) { return -ENOSYS; } +static inline int unwind_deferred_request(struct unwind_work *work, u64 *t= imestamp) { return -ENOSYS; } +static inline void unwind_deferred_cancel(struct unwind_work *work) {} =20 static inline void unwind_reset_info(void) {} =20 diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h index db5b54b18828..79b4f8cece53 100644 --- a/include/linux/unwind_deferred_types.h +++ b/include/linux/unwind_deferred_types.h @@ -7,8 +7,20 @@ struct unwind_cache { unsigned long entries[]; }; =20 + +union unwind_task_id { + struct { + u32 cpu; + u32 cnt; + }; + u64 id; +}; + struct unwind_task_info { struct unwind_cache *cache; + struct callback_head work; + union unwind_task_id id; + int pending; }; =20 #endif /* _LINUX_UNWIND_USER_DEFERRED_TYPES_H */ diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index 96368a5aa522..b1faaa55e5d5 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -2,16 +2,66 @@ /* * Deferred user space unwinding */ +#include +#include +#include +#include #include #include #include #include -#include +#include =20 /* Make the cache fit in a 4K page */ #define UNWIND_MAX_ENTRIES \ ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) =20 +/* Guards adding to and reading the list of callbacks */ +static DEFINE_MUTEX(callback_mutex); +static LIST_HEAD(callbacks); + +/* + * This is a unique percpu identifier for a given task entry context. + * Conceptually, it's incremented every time the CPU enters the kernel from + * user space, so that each "entry context" on the CPU gets a unique ID. = In + * reality, as an optimization, it's only incremented on demand for the fi= rst + * deferred unwind request after a given entry-from-user. + * + * It's combined with the CPU id to make a systemwide-unique "context cook= ie". + */ +static DEFINE_PER_CPU(u32, unwind_ctx_ctr); + +/* + * The context cookie is a unique identifier that is assigned to a user + * space stacktrace. As the user space stacktrace remains the same while + * the task is in the kernel, the cookie is an identifier for the stacktra= ce. + * Although it is possible for the stacktrace to get another cookie if ano= ther + * request is made after the cookie was cleared and before reentering user + * space. + */ +static u64 get_cookie(struct unwind_task_info *info) +{ + u32 cpu_cnt; + u32 cnt; + u32 old =3D 0; + + if (info->id.cpu) + return info->id.id; + + cpu_cnt =3D __this_cpu_read(unwind_ctx_ctr); + cpu_cnt +=3D 2; + cnt =3D cpu_cnt | 1; /* Always make non zero */ + + if (try_cmpxchg(&info->id.cnt, &old, cnt)) { + /* Update the per cpu counter */ + __this_cpu_write(unwind_ctx_ctr, cpu_cnt); + } + /* Interrupts are disabled, the CPU will always be same */ + info->id.cpu =3D smp_processor_id() + 1; /* Must be non zero */ + + return info->id.id; +} + /** * unwind_user_faultable - Produce a user stacktrace in faultable context * @trace: The descriptor that will store the user stacktrace @@ -62,11 +112,117 @@ int unwind_user_faultable(struct unwind_stacktrace *t= race) return 0; } =20 +static void unwind_deferred_task_work(struct callback_head *head) +{ + struct unwind_task_info *info =3D container_of(head, struct unwind_task_i= nfo, work); + struct unwind_stacktrace trace; + struct unwind_work *work; + u64 cookie; + + if (WARN_ON_ONCE(!info->pending)) + return; + + /* Allow work to come in again */ + WRITE_ONCE(info->pending, 0); + + /* + * From here on out, the callback must always be called, even if it's + * just an empty trace. + */ + trace.nr =3D 0; + trace.entries =3D NULL; + + unwind_user_faultable(&trace); + + cookie =3D info->id.id; + + guard(mutex)(&callback_mutex); + list_for_each_entry(work, &callbacks, list) { + work->func(work, &trace, cookie); + } +} + +/** + * unwind_deferred_request - Request a user stacktrace on task exit + * @work: Unwind descriptor requesting the trace + * @cookie: The cookie of the first request made for this task + * + * Schedule a user space unwind to be done in task work before exiting the + * kernel. + * + * The returned @cookie output is the generated cookie of the very first + * request for a user space stacktrace for this task since it entered the + * kernel. It can be from a request by any caller of this infrastructure. + * Its value will also be passed to the callback function. It can be + * used to stitch kernel and user stack traces together in post-processing. + * + * It's valid to call this function multiple times for the same @work with= in + * the same task entry context. Each call will return the same cookie + * while the task hasn't left the kernel. If the callback is not pending + * because it has already been previously called for the same entry contex= t, + * it will be called again with the same stack trace and cookie. + * + * Return: 1 if the the callback was already queued. + * 0 if the callback successfully was queued. + * Negative if there's an error. + * @cookie holds the cookie of the first request by any user + */ +int unwind_deferred_request(struct unwind_work *work, u64 *cookie) +{ + struct unwind_task_info *info =3D ¤t->unwind_info; + int ret; + + *cookie =3D 0; + + if (WARN_ON_ONCE(in_nmi())) + return -EINVAL; + + if ((current->flags & (PF_KTHREAD | PF_EXITING)) || + !user_mode(task_pt_regs(current))) + return -EINVAL; + + guard(irqsave)(); + + *cookie =3D get_cookie(info); + + /* callback already pending? */ + if (info->pending) + return 1; + + /* The work has been claimed, now schedule it. */ + ret =3D task_work_add(current, &info->work, TWA_RESUME); + if (WARN_ON_ONCE(ret)) + return ret; + + info->pending =3D 1; + return 0; +} + +void unwind_deferred_cancel(struct unwind_work *work) +{ + if (!work) + return; + + guard(mutex)(&callback_mutex); + list_del(&work->list); +} + +int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func) +{ + memset(work, 0, sizeof(*work)); + + guard(mutex)(&callback_mutex); + list_add(&work->list, &callbacks); + work->func =3D func; + return 0; +} + void unwind_task_init(struct task_struct *task) { struct unwind_task_info *info =3D &task->unwind_info; =20 memset(info, 0, sizeof(*info)); + init_task_work(&info->work, unwind_deferred_task_work); } =20 void unwind_task_free(struct task_struct *task) @@ -74,4 +230,5 @@ void unwind_task_free(struct task_struct *task) struct unwind_task_info *info =3D &task->unwind_info; =20 kfree(info->cache); + task_work_cancel(task, &info->work); } --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F047C2036F3; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; cv=none; b=f8BSzxlIWDGiWyxdUCn1fAwZtfB+Ec+OHHw23WtcQBIzCkinJ27wjq/xcJXkzNrXGSm7i+H/ssVBXMm/gKadgeAk0iTvtjqJ1FaUqNqL/1FT4/MSHu1p7lrtlwvBnO3Z8eeypeidjuZb3TcmT6I1XjFkHH1KlJD7XM3Y54/o2gw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; c=relaxed/simple; bh=1P09LovKWNhVgieil2hCUmwed+xCsnmLZJ1706E9lA0=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=iy7576OZfzVUifJnhvUqOcIoDIf605S8ObBwoQmsTw1QGR03LPljjH8WPA6v/30NC3VPxeBGcIF1k9kZ3dNQ5h6wAEr36xX9z8hKQtAOe7wrEbMcNnnxzYJsI7ZUIfmrg0SvLzsZUw58DoFci0eMsWNVkGIrFLsQ+NV9CZNFER8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OXe3hgfs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OXe3hgfs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBDB3C4CEE3; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937838; bh=1P09LovKWNhVgieil2hCUmwed+xCsnmLZJ1706E9lA0=; h=Date:From:To:Cc:Subject:References:From; b=OXe3hgfsZuvdhnassHMoJ1T7gTSmihxgY1mXwtOwqq4q90sWndpex5yibsJPr9MNY /b1RH0Jbx0aK8Mbm+ZUIOgQ1djhS4kmloi86boN+YGEJsFti8Pi9/TfwB2OXAoU95Y 2Nz+6FrLQAmwxZersLr1I08LmIUR2u0PdqA5HhSmDB/luKbGaICknEwskzrDiur+yR 9iR7n2yvhfPKVfKq5vh4PZelz2fW42jz6oLeymqGKV5h7pQZJGvfMpAwODRrhriBN9 t5OW+f/Wtl3Gui0QXOwovLhR1byyTFRM0DbxK3+H7tpS49/UhDTeY95zgKjZulW6Jb u5X1OjJOuiIBQ== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3u-00000000BsQ-47OV; Mon, 07 Jul 2025 21:23:58 -0400 Message-ID: <20250708012358.831631671@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:46 -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 , Florian Weimer , Sam James Subject: [PATCH v13 07/14] unwind_user/deferred: Make unwind deferral requests NMI-safe References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt Make unwind_deferred_request() NMI-safe so tracers in NMI context can call it and safely request a user space stacktrace when the task exits. Note, this is only allowed for architectures that implement a safe cmpxchg. If an architecture requests a deferred stack trace from NMI context that does not support a safe NMI cmpxchg, it will get an -EINVAL. For those architectures, they would need another method (perhaps an irqwork), to request a deferred user space stack trace. That can be dealt with later if one of theses architectures require this feature. Suggested-by: Peter Zijlstra Signed-off-by: Steven Rostedt (Google) --- Changes since v12: https://lore.kernel.org/20250701005451.737614486@goodmis= .org - Now that the timestamp has been replaced by a cookie that uses only a 32 bit cmpxchg(), this code just checks if the architecture has a safe cmpxchg that can be used in NMI and doesn't do the 64 bit check. Only the pending value is converted to local_t. include/linux/unwind_deferred_types.h | 4 +- kernel/unwind/deferred.c | 56 ++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h index 79b4f8cece53..cd95ed1c8610 100644 --- a/include/linux/unwind_deferred_types.h +++ b/include/linux/unwind_deferred_types.h @@ -2,6 +2,8 @@ #ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H #define _LINUX_UNWIND_USER_DEFERRED_TYPES_H =20 +#include + struct unwind_cache { unsigned int nr_entries; unsigned long entries[]; @@ -20,7 +22,7 @@ struct unwind_task_info { struct unwind_cache *cache; struct callback_head work; union unwind_task_id id; - int pending; + local_t pending; }; =20 #endif /* _LINUX_UNWIND_USER_DEFERRED_TYPES_H */ diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index b1faaa55e5d5..2417e4ebbc82 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -12,6 +12,31 @@ #include #include =20 +/* + * For requesting a deferred user space stack trace from NMI context + * the architecture must support a safe cmpxchg in NMI context. + * For those architectures that do not have that, then it cannot ask + * for a deferred user space stack trace from an NMI context. If it + * does, then it will get -EINVAL. + */ +#if defined(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) +# define CAN_USE_IN_NMI 1 +static inline bool try_assign_cnt(struct unwind_task_info *info, u32 cnt) +{ + u32 old =3D 0; + + return try_cmpxchg(&info->id.cnt, &old, cnt); +} +#else +# define CAN_USE_IN_NMI 0 +/* When NMIs are not allowed, this always succeeds */ +static inline bool try_assign_cnt(struct unwind_task_info *info, u32 cnt) +{ + info->id.cnt =3D cnt; + return true; +} +#endif + /* Make the cache fit in a 4K page */ #define UNWIND_MAX_ENTRIES \ ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) @@ -43,7 +68,6 @@ static u64 get_cookie(struct unwind_task_info *info) { u32 cpu_cnt; u32 cnt; - u32 old =3D 0; =20 if (info->id.cpu) return info->id.id; @@ -52,7 +76,7 @@ static u64 get_cookie(struct unwind_task_info *info) cpu_cnt +=3D 2; cnt =3D cpu_cnt | 1; /* Always make non zero */ =20 - if (try_cmpxchg(&info->id.cnt, &old, cnt)) { + if (try_assign_cnt(info, cnt)) { /* Update the per cpu counter */ __this_cpu_write(unwind_ctx_ctr, cpu_cnt); } @@ -119,11 +143,11 @@ static void unwind_deferred_task_work(struct callback= _head *head) struct unwind_work *work; u64 cookie; =20 - if (WARN_ON_ONCE(!info->pending)) + if (WARN_ON_ONCE(!local_read(&info->pending))) return; =20 /* Allow work to come in again */ - WRITE_ONCE(info->pending, 0); + local_set(&info->pending, 0); =20 /* * From here on out, the callback must always be called, even if it's @@ -170,31 +194,43 @@ static void unwind_deferred_task_work(struct callback= _head *head) int unwind_deferred_request(struct unwind_work *work, u64 *cookie) { struct unwind_task_info *info =3D ¤t->unwind_info; + long pending; int ret; =20 *cookie =3D 0; =20 - if (WARN_ON_ONCE(in_nmi())) - return -EINVAL; - if ((current->flags & (PF_KTHREAD | PF_EXITING)) || !user_mode(task_pt_regs(current))) return -EINVAL; =20 + /* NMI requires having safe cmpxchg operations */ + if (!CAN_USE_IN_NMI && in_nmi()) + return -EINVAL; + guard(irqsave)(); =20 *cookie =3D get_cookie(info); =20 /* callback already pending? */ - if (info->pending) + pending =3D local_read(&info->pending); + if (pending) return 1; =20 + if (CAN_USE_IN_NMI) { + /* Claim the work unless an NMI just now swooped in to do so. */ + if (!local_try_cmpxchg(&info->pending, &pending, 1)) + return 1; + } else { + local_set(&info->pending, 1); + } + /* The work has been claimed, now schedule it. */ ret =3D task_work_add(current, &info->work, TWA_RESUME); - if (WARN_ON_ONCE(ret)) + if (WARN_ON_ONCE(ret)) { + local_set(&info->pending, 0); return ret; + } =20 - info->pending =3D 1; return 0; } =20 --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1CEE82066F7; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; cv=none; b=Ke7W5NozCX9k5xTuXqDpo3VgX+/WQQ26bYn8d2mrhbpHGI0Ebdh5uKnBiIh0sotwCDl8Q/Lc8jLP2mAwYE29vFOK8ua2/dBw77zHzT5j5jG8fl5TL9YcV0q4/WZhAsYF5Pn4mhngCT27Yinal0bLqoP/TqyrlT3QtGoJu8OQ1p0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; c=relaxed/simple; bh=1C7WGJCd3Lh5br7vs50VM2T6byvNqeNaV7iCdcxH4vs=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=M8yK2gc6tAgAufVAlgnYOjfau0ZBfQsC0sGjLvWAbbwX0aVq9MN1o+Ygufe7vSxzjw+CtKKr8Z5zt7oH9SffyovM2ig5Q922Uc0mJzNLvMdMsDhswyXqdkhrjv3Q8mreU+diobCfUvLEx3PMu+L+m3IJTWbU5hxDS+a6Eue/Hkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EN+zutg0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EN+zutg0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF0DEC4CEF4; Tue, 8 Jul 2025 01:23:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937839; bh=1C7WGJCd3Lh5br7vs50VM2T6byvNqeNaV7iCdcxH4vs=; h=Date:From:To:Cc:Subject:References:From; b=EN+zutg03MMXCRdIKFYibE2GmhK48PIRjZXJWEjHbBUfreVUqIvtt1ySk8rPmzEPX MBc/hbrIvt8LPNKtdI76CHIpR23GyYjW0k9goZ9qsVmlFlTltCMGAsQ9UXWveXyvTA HNl1RcAuvldIrA30CRHiZ7WzVJ0nQPD8wtcWNW9nsYaasnBf5Yx5ocDQ2LAdElS9Jb SVzCAngVcSa0xjS4H/ZwO9w7CQiv1GeS81GpEW8YcCrUmL7FZujHfDtAWosi3WHh5Z 8TvLM+Npl6WiwipoIhhTPZWWNlFUr2WbihpIgk+QiU9B9vjKdtfxlvIDmjzqTbbjcV ftCCP8pXlchEQ== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3v-00000000Bsw-0dQn; Mon, 07 Jul 2025 21:23:59 -0400 Message-ID: <20250708012359.002924698@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:47 -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 , Florian Weimer , Sam James Subject: [PATCH v13 08/14] unwind deferred: Use bitmask to determine which callbacks to call References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt In order to know which registered callback requested a stacktrace for when the task goes back to user space, add a bitmask to keep track of all registered tracers. The bitmask is the size of long, which means that on a 32 bit machine, it can have at most 32 registered tracers, and on 64 bit, it can have at most 64 registered tracers. This should not be an issue as there should not be more than 10 (unless BPF can abuse this?). When a tracer registers with unwind_deferred_init() it will get a bit number assigned to it. When a tracer requests a stacktrace, it will have its bit set within the task_struct. When the task returns back to user space, it will call the callbacks for all the registered tracers where their bits are set in the task's mask. When a tracer is removed by the unwind_deferred_cancel() all current tasks will clear the associated bit, just in case another tracer gets registered immediately afterward and then gets their callback called unexpectedly. Signed-off-by: Steven Rostedt (Google) --- include/linux/unwind_deferred.h | 1 + include/linux/unwind_deferred_types.h | 1 + kernel/unwind/deferred.c | 36 ++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h index 14efd8c027aa..12bffdb0648e 100644 --- a/include/linux/unwind_deferred.h +++ b/include/linux/unwind_deferred.h @@ -13,6 +13,7 @@ typedef void (*unwind_callback_t)(struct unwind_work *wor= k, struct unwind_stackt struct unwind_work { struct list_head list; unwind_callback_t func; + int bit; }; =20 #ifdef CONFIG_UNWIND_USER diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h index cd95ed1c8610..7a03a8672b0d 100644 --- a/include/linux/unwind_deferred_types.h +++ b/include/linux/unwind_deferred_types.h @@ -21,6 +21,7 @@ union unwind_task_id { struct unwind_task_info { struct unwind_cache *cache; struct callback_head work; + unsigned long unwind_mask; union unwind_task_id id; local_t pending; }; diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index 2417e4ebbc82..5edb648b7de7 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -44,6 +44,7 @@ static inline bool try_assign_cnt(struct unwind_task_info= *info, u32 cnt) /* Guards adding to and reading the list of callbacks */ static DEFINE_MUTEX(callback_mutex); static LIST_HEAD(callbacks); +static unsigned long unwind_mask; =20 /* * This is a unique percpu identifier for a given task entry context. @@ -162,7 +163,10 @@ static void unwind_deferred_task_work(struct callback_= head *head) =20 guard(mutex)(&callback_mutex); list_for_each_entry(work, &callbacks, list) { - work->func(work, &trace, cookie); + if (test_bit(work->bit, &info->unwind_mask)) { + work->func(work, &trace, cookie); + clear_bit(work->bit, &info->unwind_mask); + } } } =20 @@ -211,15 +215,19 @@ int unwind_deferred_request(struct unwind_work *work,= u64 *cookie) =20 *cookie =3D get_cookie(info); =20 + /* This is already queued */ + if (test_bit(work->bit, &info->unwind_mask)) + return 1; + /* callback already pending? */ pending =3D local_read(&info->pending); if (pending) - return 1; + goto out; =20 if (CAN_USE_IN_NMI) { /* Claim the work unless an NMI just now swooped in to do so. */ if (!local_try_cmpxchg(&info->pending, &pending, 1)) - return 1; + goto out; } else { local_set(&info->pending, 1); } @@ -231,16 +239,27 @@ int unwind_deferred_request(struct unwind_work *work,= u64 *cookie) return ret; } =20 - return 0; + out: + return test_and_set_bit(work->bit, &info->unwind_mask); } =20 void unwind_deferred_cancel(struct unwind_work *work) { + struct task_struct *g, *t; + if (!work) return; =20 guard(mutex)(&callback_mutex); list_del(&work->list); + + __clear_bit(work->bit, &unwind_mask); + + guard(rcu)(); + /* Clear this bit from all threads */ + for_each_process_thread(g, t) { + clear_bit(work->bit, &t->unwind_info.unwind_mask); + } } =20 int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func) @@ -248,6 +267,14 @@ int unwind_deferred_init(struct unwind_work *work, unw= ind_callback_t func) memset(work, 0, sizeof(*work)); =20 guard(mutex)(&callback_mutex); + + /* See if there's a bit in the mask available */ + if (unwind_mask =3D=3D ~0UL) + return -EBUSY; + + work->bit =3D ffz(unwind_mask); + __set_bit(work->bit, &unwind_mask); + list_add(&work->list, &callbacks); work->func =3D func; return 0; @@ -259,6 +286,7 @@ void unwind_task_init(struct task_struct *task) =20 memset(info, 0, sizeof(*info)); init_task_work(&info->work, unwind_deferred_task_work); + info->unwind_mask =3D 0; } =20 void unwind_task_free(struct task_struct *task) --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4785220F091; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; cv=none; b=JrYzMkNlehyUWMuCVVInPgG64ZL0FfG0Ve5OEYA/WShKENzuR+ZZFgeesOlpiacBdknn9lXkKIxb/5dS0y8fL+oQpZvxs6XxnnGw5j6i/Ix6cIITxnF2sm/l6uFjWdEuqKiySfLxCtCsBhFTiC5D45P1Dt5JP2+0Q1od7K8HhNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; c=relaxed/simple; bh=Um70FQ+3UzUH2oUUSV3TA33BR6NtC+VENc9gr4wr3zk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=dHNzLAG17EotwoSurO8oo8phHn0q/uA5x3HeDTNP6y+wAz96k3iQFEZcDhi04qyYNdVjKbOq26zs07Giqg4Ts9dEE1nKtvfBVsiVd3fbkGq7VNE6vdRgOZWE2bFnc9rNSts1AKhyxp0KoOysS6JmADCw/4QYK3z22o5O85jZvlc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A8SuujCB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A8SuujCB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26033C4CEE3; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937839; bh=Um70FQ+3UzUH2oUUSV3TA33BR6NtC+VENc9gr4wr3zk=; h=Date:From:To:Cc:Subject:References:From; b=A8SuujCBIQX79rW+cVjEiQLhiRl1La6FPT8ph/oNdB9tt+W7c54pmpuPKRigG1EWw aPYWvex9K/xg7K24qOhF5wILQOHkSE7CE+L8Oz6EBZWD8GYbdpQWHqValsgVoq/E90 gu8VSmbIm0AtW/SC4d6gxFdJNFTAHjTcemvKMzuHmGMxcBGGdaxkaVL4J1Wf/1kT3E Geqz0MGMdiLXZccMhgeWFduq6u57LuqYCVTY14XMsJBXmDkJ/WAVCdNL7D1eT2XNrR 0n2rl7bXayJJnb1CMJrCp7mYssFK4a7OxC4x8xEQMB56LtuOUaFcn66hncAGVTP9Ir EbiW6Rh3oqMyQ== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3v-00000000BtR-1MET; Mon, 07 Jul 2025 21:23:59 -0400 Message-ID: <20250708012359.172959778@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:48 -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 , Florian Weimer , Sam James Subject: [PATCH v13 09/14] unwind deferred: Use SRCU unwind_deferred_task_work() References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt 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 cannot 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@effi= cios.com/ Suggested-by: Mathieu Desnoyers Signed-off-by: Steven Rostedt (Google) --- 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 5edb648b7de7..9aed9866f460 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -41,10 +41,11 @@ static inline bool try_assign_cnt(struct unwind_task_in= fo *info, u32 cnt) #define UNWIND_MAX_ENTRIES \ ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) =20 -/* 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); =20 /* * This is a unique percpu identifier for a given task entry context. @@ -143,6 +144,7 @@ static void unwind_deferred_task_work(struct callback_h= ead *head) struct unwind_stacktrace trace; struct unwind_work *work; u64 cookie; + int idx; =20 if (WARN_ON_ONCE(!local_read(&info->pending))) return; @@ -161,13 +163,15 @@ static void unwind_deferred_task_work(struct callback= _head *head) =20 cookie =3D info->id.id; =20 - guard(mutex)(&callback_mutex); - list_for_each_entry(work, &callbacks, list) { + idx =3D srcu_read_lock(&unwind_srcu); + list_for_each_entry_srcu(work, &callbacks, list, + srcu_read_lock_held(&unwind_srcu)) { if (test_bit(work->bit, &info->unwind_mask)) { work->func(work, &trace, cookie); clear_bit(work->bit, &info->unwind_mask); } } + srcu_read_unlock(&unwind_srcu, idx); } =20 /** @@ -199,6 +203,7 @@ int unwind_deferred_request(struct unwind_work *work, u= 64 *cookie) { struct unwind_task_info *info =3D ¤t->unwind_info; long pending; + int bit; int ret; =20 *cookie =3D 0; @@ -211,12 +216,17 @@ int unwind_deferred_request(struct unwind_work *work,= u64 *cookie) if (!CAN_USE_IN_NMI && in_nmi()) return -EINVAL; =20 + /* Do not allow cancelled works to request again */ + bit =3D READ_ONCE(work->bit); + if (WARN_ON_ONCE(bit < 0)) + return -EINVAL; + guard(irqsave)(); =20 *cookie =3D get_cookie(info); =20 /* This is already queued */ - if (test_bit(work->bit, &info->unwind_mask)) + if (test_bit(bit, &info->unwind_mask)) return 1; =20 /* callback already pending? */ @@ -240,25 +250,32 @@ int unwind_deferred_request(struct unwind_work *work,= u64 *cookie) } =20 out: - return test_and_set_bit(work->bit, &info->unwind_mask); + return test_and_set_bit(bit, &info->unwind_mask); } =20 void unwind_deferred_cancel(struct unwind_work *work) { struct task_struct *g, *t; + int bit; =20 if (!work) return; =20 guard(mutex)(&callback_mutex); - list_del(&work->list); + list_del_rcu(&work->list); + bit =3D work->bit; + + /* Do not allow any more requests and prevent callbacks */ + work->bit =3D -1; + + __clear_bit(bit, &unwind_mask); =20 - __clear_bit(work->bit, &unwind_mask); + synchronize_srcu(&unwind_srcu); =20 guard(rcu)(); /* Clear this bit from all threads */ for_each_process_thread(g, t) { - clear_bit(work->bit, &t->unwind_info.unwind_mask); + clear_bit(bit, &t->unwind_info.unwind_mask); } } =20 @@ -275,7 +292,7 @@ int unwind_deferred_init(struct unwind_work *work, unwi= nd_callback_t func) work->bit =3D ffz(unwind_mask); __set_bit(work->bit, &unwind_mask); =20 - list_add(&work->list, &callbacks); + list_add_rcu(&work->list, &callbacks); work->func =3D func; return 0; } --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 7037921883F; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; cv=none; b=TfmuXDN80sghdZ2HEBc3BA8xlshcW/xPaWPPHnHfu8GvOwuFQkeMLBEZ8aFKjOkgi55DxoOmWMf5MyJf94bKY+XpnOVbqnlI0chConQeNaYvRJ8Pd/koIgK+8JWcq1F9ahB+DEDxi+yYVLULJRE6Q7l0T/HPq2/2T3ghDbYnz9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; c=relaxed/simple; bh=I4Lv6KjOHCwzaJFkkDd/S0x5we6FReZBQbJgHE2LzPg=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=ZeYtdC5DfpnMbS68vPduD+R8MU60S6oNMMooszl4r3/qeMpra978iHhNVFZECFuedACmEwYNBWyS1VGlWFaEcAMtd6ZJH3Av0StDYFex+v5mTVljQInB44XawkVJqVQ1SFk4HXB04Gnjpf2wVJGBXD+3/h1A27BXjvBe88foM3w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dZ+DM3QR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dZ+DM3QR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D999C4CEF7; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937839; bh=I4Lv6KjOHCwzaJFkkDd/S0x5we6FReZBQbJgHE2LzPg=; h=Date:From:To:Cc:Subject:References:From; b=dZ+DM3QR5KwnFgrC6SKF84Su1Qndhzttsdj5F/xvjicbxKb30WOHpt2ijjF639ZqE dTBYxLtKofW4Sj2zRRJ4Vro42IFm8UH/6rPv+YuvhpPXx+clqROpfcoUbAg4jlY2ai DlXE6riNAHOxIK6eT0Y5frOEAcbUX8Hnl2XQXX6RwpxXqnkHcqZEDsL/e5lSKVaRjH 1o+3LuLDQ/7s9c4GVAsx7zTytnYtPiE/UQGUHakkxpfm56BsqUXWWySdxuKsdhfKI8 wVvYi0t6IeBNW4Dt74gch2ysIXvFxGXxAxgiMDhPYVwOe9GrbN7y8zEBM06YabK1Ot UtlnimzaSOJaw== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3v-00000000Btw-253w; Mon, 07 Jul 2025 21:23:59 -0400 Message-ID: <20250708012359.345060579@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:49 -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 , Florian Weimer , Sam James Subject: [PATCH v13 10/14] unwind: Clear unwind_mask on exit back to user space References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt When testing the deferred unwinder by attaching deferred user space stacktraces to events, a live lock happened. This was when the deferred unwinding was added to the irqs_disabled event, which happens after the task_work callbacks are called and before the task goes back to user space. The event callback would be registered when irqs were disabled, the task_work would trigger, call the callback for this work and clear the work's bit. Then before getting back to user space, irqs would be disabled again, the event triggered again, and a new task_work registered. This caused an infinite loop and the system hung. To prevent this, clear the bits at the very last moment before going back to user space and when instrumentation is disabled. That is in unwind_exit_to_user_mode(). Move the pending bit from a value on the task_struct to the most significant bit of the unwind_mask (saves space on the task_struct). This will allow modifying the pending bit along with the work bits atomically. Instead of clearing a work's bit after its callback is called, it is delayed until exit. If the work is requested again, the task_work is not queued again and the work will be notified that the task has already been called (via UNWIND_ALREADY_EXECUTED return value). The pending bit is cleared before calling the callback functions but the current work bits remain. If one of the called works registers again, it will not trigger a task_work if its bit is still present in the task's unwind_mask. If a new work registers, then it will set both the pending bit and its own bit but clear the other work bits so that their callbacks do not get called again. Signed-off-by: Steven Rostedt (Google) --- Changes since v12: https://lore.kernel.org/20250701005452.242933931@goodmis= .org - Removed no longer used local.h headers from unwind_deferred_types.h include/linux/unwind_deferred.h | 25 +++++++-- include/linux/unwind_deferred_types.h | 3 -- kernel/unwind/deferred.c | 76 ++++++++++++++++++--------- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h index 12bffdb0648e..587e120c0fd6 100644 --- a/include/linux/unwind_deferred.h +++ b/include/linux/unwind_deferred.h @@ -18,6 +18,14 @@ struct unwind_work { =20 #ifdef CONFIG_UNWIND_USER =20 +#define UNWIND_PENDING_BIT (BITS_PER_LONG - 1) +#define UNWIND_PENDING BIT(UNWIND_PENDING_BIT) + +enum { + UNWIND_ALREADY_PENDING =3D 1, + UNWIND_ALREADY_EXECUTED =3D 2, +}; + void unwind_task_init(struct task_struct *task); void unwind_task_free(struct task_struct *task); =20 @@ -29,15 +37,26 @@ void unwind_deferred_cancel(struct unwind_work *work); =20 static __always_inline void unwind_reset_info(void) { - if (unlikely(current->unwind_info.id.id)) + struct unwind_task_info *info =3D ¤t->unwind_info; + unsigned long bits; + + /* Was there any unwinding? */ + if (unlikely(info->unwind_mask)) { + bits =3D info->unwind_mask; + do { + /* Is a task_work going to run again before going back */ + if (bits & UNWIND_PENDING) + return; + } while (!try_cmpxchg(&info->unwind_mask, &bits, 0UL)); current->unwind_info.id.id =3D 0; + } /* * As unwind_user_faultable() can be called directly and * depends on nr_entries being cleared on exit to user, * this needs to be a separate conditional. */ - if (unlikely(current->unwind_info.cache)) - current->unwind_info.cache->nr_entries =3D 0; + if (unlikely(info->cache)) + info->cache->nr_entries =3D 0; } =20 #else /* !CONFIG_UNWIND_USER */ diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h index 7a03a8672b0d..db6c65daf185 100644 --- a/include/linux/unwind_deferred_types.h +++ b/include/linux/unwind_deferred_types.h @@ -2,8 +2,6 @@ #ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H #define _LINUX_UNWIND_USER_DEFERRED_TYPES_H =20 -#include - struct unwind_cache { unsigned int nr_entries; unsigned long entries[]; @@ -23,7 +21,6 @@ struct unwind_task_info { struct callback_head work; unsigned long unwind_mask; union unwind_task_id id; - local_t pending; }; =20 #endif /* _LINUX_UNWIND_USER_DEFERRED_TYPES_H */ diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index 9aed9866f460..256458f3eafe 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -47,6 +47,11 @@ static LIST_HEAD(callbacks); static unsigned long unwind_mask; DEFINE_STATIC_SRCU(unwind_srcu); =20 +static inline bool unwind_pending(struct unwind_task_info *info) +{ + return test_bit(UNWIND_PENDING_BIT, &info->unwind_mask); +} + /* * This is a unique percpu identifier for a given task entry context. * Conceptually, it's incremented every time the CPU enters the kernel from @@ -143,14 +148,17 @@ static void unwind_deferred_task_work(struct callback= _head *head) struct unwind_task_info *info =3D container_of(head, struct unwind_task_i= nfo, work); struct unwind_stacktrace trace; struct unwind_work *work; + unsigned long bits; u64 cookie; int idx; =20 - if (WARN_ON_ONCE(!local_read(&info->pending))) + if (WARN_ON_ONCE(!unwind_pending(info))) return; =20 - /* Allow work to come in again */ - local_set(&info->pending, 0); + /* Clear pending bit but make sure to have the current bits */ + bits =3D READ_ONCE(info->unwind_mask); + while (!try_cmpxchg(&info->unwind_mask, &bits, bits & ~UNWIND_PENDING)) + ; =20 /* * From here on out, the callback must always be called, even if it's @@ -166,10 +174,8 @@ static void unwind_deferred_task_work(struct callback_= head *head) idx =3D srcu_read_lock(&unwind_srcu); list_for_each_entry_srcu(work, &callbacks, list, srcu_read_lock_held(&unwind_srcu)) { - if (test_bit(work->bit, &info->unwind_mask)) { + if (test_bit(work->bit, &bits)) work->func(work, &trace, cookie); - clear_bit(work->bit, &info->unwind_mask); - } } srcu_read_unlock(&unwind_srcu, idx); } @@ -194,15 +200,17 @@ static void unwind_deferred_task_work(struct callback= _head *head) * because it has already been previously called for the same entry contex= t, * it will be called again with the same stack trace and cookie. * - * Return: 1 if the the callback was already queued. - * 0 if the callback successfully was queued. + * Return: 0 if the callback successfully was queued. + * UNWIND_ALREADY_PENDING if the the callback was already queued. + * UNWIND_ALREADY_EXECUTED if the callback was already called + * (and will not be called again) * Negative if there's an error. * @cookie holds the cookie of the first request by any user */ int unwind_deferred_request(struct unwind_work *work, u64 *cookie) { struct unwind_task_info *info =3D ¤t->unwind_info; - long pending; + unsigned long old, bits; int bit; int ret; =20 @@ -225,32 +233,52 @@ int unwind_deferred_request(struct unwind_work *work,= u64 *cookie) =20 *cookie =3D get_cookie(info); =20 - /* This is already queued */ - if (test_bit(bit, &info->unwind_mask)) - return 1; + old =3D READ_ONCE(info->unwind_mask); + + /* Is this already queued */ + if (test_bit(bit, &old)) { + /* + * If pending is not set, it means this work's callback + * was already called. + */ + return old & UNWIND_PENDING ? UNWIND_ALREADY_PENDING : + UNWIND_ALREADY_EXECUTED; + } =20 - /* callback already pending? */ - pending =3D local_read(&info->pending); - if (pending) + if (unwind_pending(info)) goto out; =20 + /* + * This is the first to enable another task_work for this task since + * the task entered the kernel, or had already called the callbacks. + * Set only the bit for this work and clear all others as they have + * already had their callbacks called, and do not need to call them + * again because of this work. + */ + bits =3D UNWIND_PENDING | BIT(bit); + + /* + * If the cmpxchg() fails, it means that an NMI came in and set + * the pending bit as well as cleared the other bits. Just + * jump to setting the bit for this work. + */ if (CAN_USE_IN_NMI) { - /* Claim the work unless an NMI just now swooped in to do so. */ - if (!local_try_cmpxchg(&info->pending, &pending, 1)) + if (!try_cmpxchg(&info->unwind_mask, &old, bits)) goto out; } else { - local_set(&info->pending, 1); + info->unwind_mask =3D bits; } =20 /* The work has been claimed, now schedule it. */ ret =3D task_work_add(current, &info->work, TWA_RESUME); - if (WARN_ON_ONCE(ret)) { - local_set(&info->pending, 0); - return ret; - } =20 + if (WARN_ON_ONCE(ret)) + WRITE_ONCE(info->unwind_mask, 0); + + return ret; out: - return test_and_set_bit(bit, &info->unwind_mask); + return test_and_set_bit(bit, &info->unwind_mask) ? + UNWIND_ALREADY_PENDING : 0; } =20 void unwind_deferred_cancel(struct unwind_work *work) @@ -286,7 +314,7 @@ int unwind_deferred_init(struct unwind_work *work, unwi= nd_callback_t func) guard(mutex)(&callback_mutex); =20 /* See if there's a bit in the mask available */ - if (unwind_mask =3D=3D ~0UL) + if (unwind_mask =3D=3D ~(UNWIND_PENDING)) return -EBUSY; =20 work->bit =3D ffz(unwind_mask); --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8F62821A931; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; cv=none; b=kdIsmjNl8skVF4UUekS4RQMek/Qi6CXLsGowpb7dNUrYDdFRxLKE7KZy8b3h+eoclXSXBDeursZlyZhO6uOVQ3uPoza/x0Zj+D6K4jSRpBj7+4rJp6jHAzjtnVcJYaPTXBG5y0h2j7cXu5ouKWv6KCtkBMEpMR4aficKJtf5p6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; c=relaxed/simple; bh=61tDSqBMXQ/82GJZZ8kZgqsfXb9llhFb2V03jv03Tb4=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=pVWCtaCic1IFP66sfL8dK4DS1yKiESZROHoRHPDweCohAkjL1OSogEVaDB8E39uLYkoWvk9TWUOnnh60SZ7z7LCEG8Ic7ngPm5+eTBu8cMF2YUU7VVD5sBaFU12yRWVoQ1lXwRKnb2w4aMChDfV4UuLi1bb8RX0g9X7bDKQGxwQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SZhGGW5O; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SZhGGW5O" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69E7FC4CEF6; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937839; bh=61tDSqBMXQ/82GJZZ8kZgqsfXb9llhFb2V03jv03Tb4=; h=Date:From:To:Cc:Subject:References:From; b=SZhGGW5OTDO7VXypqKj4gzAzA/65ZWOsgQfHurefkqSouy9rZ7baRpIDR3QP4kcfA YahgsCqHJhyxRhuX7Rd1UjjwYUruz+6J8+E/olYANesJKgSptypqXUH7N76oLe0Ll4 0ejXwNA/Wc+IUFEmMv2P7L5lC2TRJ1gwlskoAa+36GdmX1Aa0mxRmJmfqGGhINdjs+ VEjtoww7k5nkN4sb6e5fi80w3wjEgcwP0qp1t6uDP4grPXvgZXuPTst7jrJDYmEwte QY0J9lNhG1z0r8Ne0p4UWFdR0CLGcfsLj43FsgiQya3HK5wZva4DWCCTCIBJUzBTvt JoW4m6eDU8Q6Q== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3v-00000000BuS-2n6L; Mon, 07 Jul 2025 21:23:59 -0400 Message-ID: <20250708012359.516909531@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:50 -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 , Florian Weimer , Sam James Subject: [PATCH v13 11/14] unwind: Add USED bit to only have one conditional on way back to user space References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt On the way back to user space, the function unwind_reset_info() is called unconditionally (but always inlined). It currently has two conditionals. One that checks the unwind_mask which is set whenever a deferred trace is called and is used to know that the mask needs to be cleared. The other checks if the cache has been allocated, and if so, it resets the nr_entries so that the unwinder knows it needs to do the work to get a new user space stack trace again (it only does it once per entering the kernel). Use one of the bits in the unwind mask as a "USED" bit that gets set whenever a trace is created. This will make it possible to only check the unwind_mask in the unwind_reset_info() to know if it needs to do work or not and eliminates a conditional that happens every time the task goes back to user space. Signed-off-by: Steven Rostedt (Google) --- include/linux/unwind_deferred.h | 14 +++++++------- kernel/unwind/deferred.c | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h index 587e120c0fd6..376bfd50ff75 100644 --- a/include/linux/unwind_deferred.h +++ b/include/linux/unwind_deferred.h @@ -21,6 +21,10 @@ struct unwind_work { #define UNWIND_PENDING_BIT (BITS_PER_LONG - 1) #define UNWIND_PENDING BIT(UNWIND_PENDING_BIT) =20 +/* Set if the unwinding was used (directly or deferred) */ +#define UNWIND_USED_BIT (UNWIND_PENDING_BIT - 1) +#define UNWIND_USED BIT(UNWIND_USED_BIT) + enum { UNWIND_ALREADY_PENDING =3D 1, UNWIND_ALREADY_EXECUTED =3D 2, @@ -49,14 +53,10 @@ static __always_inline void unwind_reset_info(void) return; } while (!try_cmpxchg(&info->unwind_mask, &bits, 0UL)); current->unwind_info.id.id =3D 0; + + if (unlikely(info->cache)) + info->cache->nr_entries =3D 0; } - /* - * As unwind_user_faultable() can be called directly and - * depends on nr_entries being cleared on exit to user, - * this needs to be a separate conditional. - */ - if (unlikely(info->cache)) - info->cache->nr_entries =3D 0; } =20 #else /* !CONFIG_UNWIND_USER */ diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index 256458f3eafe..9299974b6562 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -140,6 +140,9 @@ int unwind_user_faultable(struct unwind_stacktrace *tra= ce) =20 cache->nr_entries =3D trace->nr; =20 + /* Clear nr_entries on way back to user space */ + set_bit(UNWIND_USED_BIT, &info->unwind_mask); + return 0; } =20 @@ -314,7 +317,7 @@ int unwind_deferred_init(struct unwind_work *work, unwi= nd_callback_t func) guard(mutex)(&callback_mutex); =20 /* See if there's a bit in the mask available */ - if (unwind_mask =3D=3D ~(UNWIND_PENDING)) + if (unwind_mask =3D=3D ~(UNWIND_PENDING|UNWIND_USED)) return -EBUSY; =20 work->bit =3D ffz(unwind_mask); --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B6F7721D00A; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; cv=none; b=OQEEJC7EfQqNtUoJ4zyoJS0CE8rZ6+Lg6/MCCkPaf20kZbzTBY38MOTb2laovU+65hKljJC7dbqNkWUe/dVFiHz/PVzuTwC59n6A2/a2zV/RBufE/aqr1s9699mg1OiWwkW/um4Hb3G0zdooCGmLoSEFI0cveTh5/A+Ug/xsNrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937839; c=relaxed/simple; bh=1woBRSwvr3+dsgRe82q4LxBYj4eV2D2BoGtGwhUkrU4=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=D9QMQECSgMFe6DHfzs9LNuzJcVZDYQVZfikySfBB/rzO5sluQXiDLEMSb9orflCrxoaJprsPzmpCeqgItl7OufvWKCLf+C3vPBJHj3aprjAAFtbsVWmp+/Oh2wQPaEAsKooR638KrKaAlDrjAZ8CSH/1tG3HTozR40b0iqGr5oA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AKfFHVeo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AKfFHVeo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96B90C4CEE3; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937839; bh=1woBRSwvr3+dsgRe82q4LxBYj4eV2D2BoGtGwhUkrU4=; h=Date:From:To:Cc:Subject:References:From; b=AKfFHVeo+Y0gT7guMc4TDba4/IPgGPWmnsS+HWwrRcSRhRlNDn2tS5Mm3bWksHhqQ VqE3Cx0KZrqR6F3pfG80GoGREcE2tyg13QTuUfrMgJOrGV3UWalNLs/CkQOqBPmwjj HNmxBzAReWa9pn5ScJnuqyHzVCmoEgC7RvmxomiEz8gq8BCpitnYd//mBJNMTPRU2W TSXQ8p2oCn9JxM9fkpo3bEQfnZnriXAYSbUaGMavU83FdiUQWef38Miaeiz0KVLq6x zmDnBsEsMaJYIHHFNM2mhoxrHuvhf8n98ArpW+K4RCFpi0+Dduf1XciPGc5TmPai67 /ZQ95hBcwxx1w== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3v-00000000Bux-3U2D; Mon, 07 Jul 2025 21:23:59 -0400 Message-ID: <20250708012359.685166151@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:51 -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 , Florian Weimer , Sam James Subject: [PATCH v13 12/14] unwind: Finish up unwind when a task exits References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt On do_exit() when a task is exiting, if a unwind is requested and the deferred user stacktrace is deferred via the task_work, the task_work callback is called after exit_mm() is called in do_exit(). This means that the user stack trace will not be retrieved and an empty stack is created. Instead, add a function unwind_deferred_task_exit() and call it just before exit_mm() so that the unwinder can call the requested callbacks with the user space stack. Signed-off-by: Steven Rostedt (Google) --- include/linux/unwind_deferred.h | 3 +++ kernel/exit.c | 2 ++ kernel/unwind/deferred.c | 23 ++++++++++++++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/linux/unwind_deferred.h b/include/linux/unwind_deferre= d.h index 376bfd50ff75..a9d5b100d6b2 100644 --- a/include/linux/unwind_deferred.h +++ b/include/linux/unwind_deferred.h @@ -39,6 +39,8 @@ int unwind_deferred_init(struct unwind_work *work, unwind= _callback_t func); int unwind_deferred_request(struct unwind_work *work, u64 *cookie); void unwind_deferred_cancel(struct unwind_work *work); =20 +void unwind_deferred_task_exit(struct task_struct *task); + static __always_inline void unwind_reset_info(void) { struct unwind_task_info *info =3D ¤t->unwind_info; @@ -69,6 +71,7 @@ static inline int unwind_deferred_init(struct unwind_work= *work, unwind_callback static inline int unwind_deferred_request(struct unwind_work *work, u64 *t= imestamp) { return -ENOSYS; } static inline void unwind_deferred_cancel(struct unwind_work *work) {} =20 +static inline void unwind_deferred_task_exit(struct task_struct *task) {} static inline void unwind_reset_info(void) {} =20 #endif /* !CONFIG_UNWIND_USER */ diff --git a/kernel/exit.c b/kernel/exit.c index bb184a67ac73..1d8c8ac33c4f 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -68,6 +68,7 @@ #include #include #include +#include #include #include =20 @@ -938,6 +939,7 @@ void __noreturn do_exit(long code) =20 tsk->exit_code =3D code; taskstats_exit(tsk, group_dead); + unwind_deferred_task_exit(tsk); trace_sched_process_exit(tsk, group_dead); =20 /* diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index 9299974b6562..039e12700d49 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -113,7 +113,7 @@ int unwind_user_faultable(struct unwind_stacktrace *tra= ce) /* Should always be called from faultable context */ might_fault(); =20 - if (current->flags & PF_EXITING) + if (!current->mm) return -EINVAL; =20 if (!info->cache) { @@ -146,9 +146,9 @@ int unwind_user_faultable(struct unwind_stacktrace *tra= ce) return 0; } =20 -static void unwind_deferred_task_work(struct callback_head *head) +static void process_unwind_deferred(struct task_struct *task) { - struct unwind_task_info *info =3D container_of(head, struct unwind_task_i= nfo, work); + struct unwind_task_info *info =3D &task->unwind_info; struct unwind_stacktrace trace; struct unwind_work *work; unsigned long bits; @@ -183,6 +183,23 @@ static void unwind_deferred_task_work(struct callback_= head *head) srcu_read_unlock(&unwind_srcu, idx); } =20 +static void unwind_deferred_task_work(struct callback_head *head) +{ + process_unwind_deferred(current); +} + +void unwind_deferred_task_exit(struct task_struct *task) +{ + struct unwind_task_info *info =3D ¤t->unwind_info; + + if (!unwind_pending(info)) + return; + + process_unwind_deferred(task); + + task_work_cancel(task, &info->work); +} + /** * unwind_deferred_request - Request a user stacktrace on task exit * @work: Unwind descriptor requesting the trace --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E02EE220F24; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937840; cv=none; b=SBhcbbCJNL6i6HSD6KHXYRhiZLcdV5V80sGv1PLw0PmE7Ebrj6K4JPNOIGsodIXaHWZ9XzKjVr38tmvkvUKgQOJvbVdFpzUt3igde1AGw4zrhsSkEPLabE2jW71Fo0hvP94fkfTOW6sqpcz3G58cEkB6eyfaDLw/ykMD2wyHFIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937840; c=relaxed/simple; bh=q9368biQbKFkdqwUJGMNR4U7z4DEcx2lmBxRY3YPNYQ=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=TRzUJ5syFxmYz7pfb/oYfJLA0nhcKJb+UIfCU8I+u+YjyMvpYfrszDP8uBdCPvtg5QxTus5X+gyOsLJ9DEMLFfqMAS9ffxhsDnX7ZdZ5z3wgDH1K1W5oZJmdjf0yiR8He5z5ZYD+MGMTpv+JLVXjMyYLXr1P2224M5XbBM5jQtE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RrWmDdTd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RrWmDdTd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8D74C4CEF4; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937839; bh=q9368biQbKFkdqwUJGMNR4U7z4DEcx2lmBxRY3YPNYQ=; h=Date:From:To:Cc:Subject:References:From; b=RrWmDdTdfrCR/QR7S1cFvpBywbUn+9TgIi+j7JfOTMojWG99WrGBMxhw0ZwEtPN8b Z4pTIPhJxGhY9UhwUCsO7GavdwJVt4xbIsyvKJQSSaWQVDlYn6GoXicKIcVkBO2Cbx 5U9pnOwMp6RnRKi7FDUV0mqCjqdXADZ6adLUSxWqYc38d6X0tefQYiV8y3PgXBdm8l QJafTD0Xmh/hSOAjXG/4DuojtsuOeLYiQpvzPrvIjzqb4iEcUb1r0TrJXEpuknQBm+ lSxEussR+H2ZhLbdYDCJM+SB8mqBot7pxjDNGINtvpd5gFK2SJG7jSfXkTyxsFKhuq hplvDN8jDzx9w== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3w-00000000BvS-00qE; Mon, 07 Jul 2025 21:24:00 -0400 Message-ID: <20250708012359.853818537@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:52 -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 , Florian Weimer , Sam James Subject: [PATCH v13 13/14] unwind_user/x86: Enable frame pointer unwinding on x86 References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Use ARCH_INIT_USER_FP_FRAME to describe how frame pointers are unwound on x86, and enable CONFIG_HAVE_UNWIND_USER_FP accordingly so the unwind_user interfaces can be used. Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- arch/x86/Kconfig | 1 + arch/x86/include/asm/unwind_user.h | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 arch/x86/include/asm/unwind_user.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 71019b3b54ea..5862433c81e1 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -302,6 +302,7 @@ config X86 select HAVE_SYSCALL_TRACEPOINTS select HAVE_UACCESS_VALIDATION if HAVE_OBJTOOL select HAVE_UNSTABLE_SCHED_CLOCK + select HAVE_UNWIND_USER_FP if X86_64 select HAVE_USER_RETURN_NOTIFIER select HAVE_GENERIC_VDSO select VDSO_GETRANDOM if X86_64 diff --git a/arch/x86/include/asm/unwind_user.h b/arch/x86/include/asm/unwi= nd_user.h new file mode 100644 index 000000000000..8597857bf896 --- /dev/null +++ b/arch/x86/include/asm/unwind_user.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_UNWIND_USER_H +#define _ASM_X86_UNWIND_USER_H + +#define ARCH_INIT_USER_FP_FRAME \ + .cfa_off =3D (s32)sizeof(long) * 2, \ + .ra_off =3D (s32)sizeof(long) * -1, \ + .fp_off =3D (s32)sizeof(long) * -2, \ + .use_fp =3D true, + +#endif /* _ASM_X86_UNWIND_USER_H */ --=20 2.47.2 From nobody Tue Oct 7 18:22:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 13955221FC4; Tue, 8 Jul 2025 01:24:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937840; cv=none; b=uTeSqSfe/tDvTFAzOyDOZeXx70xJBR2Jt5LZw9zvkGZrhIvUDphDrQR0LTkHwB2XihpSmVdNx+RlXX1ReZNeu9ZEZJR3ADSIazY1TzdknE14RjI4hvza91x623fHrsST8wc+6Jy9VTCySoycThDJmESgwovMijWGAt+y2W+M2yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937840; c=relaxed/simple; bh=OTmEBmEDwwr+5i50GFjKWiFV3nOMPMB1tKCDHYcguyM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=CpotaNiH1ihIVN/XWKu1MZW1dwBeWWm1DcXQl7v6tv/u3A0cEs37bBGsggvddVCIl//jBpTNQ+5zHM6ZgKcvnJiQUy8J3C4QUpTZ0ryjzt/acITqe+/Y/41GPBIVFil2X4/3n1tK+pUduKhujVKaOXBa5B72vhzrOBFVgm1CRD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j0lUt9uB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j0lUt9uB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4DC6C4CEF6; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937840; bh=OTmEBmEDwwr+5i50GFjKWiFV3nOMPMB1tKCDHYcguyM=; h=Date:From:To:Cc:Subject:References:From; b=j0lUt9uBCmNalZZgXZn6WUMBYzGKP9DZ90y8WUfzcQ4pA+oQY/jQghRiohCMVKb3k NUjXRT7HRxdlw5ZhIi6KYvsVRd5huvOrY3EXTvQa8pMmv6NsxvKHCRGIeWLrbQ3MS6 bthybsKKnI0m+XCdULIPyohXCbRIT4Rq2KjN4wmM8pc7udhUMDpoGEE8xpIsJlTF5g jy9hjHEV+KfnrN2ksOF7RrI1Wgk4cJe3sh/fH9fikCNg+p7Pr+s1L83qbJywEQuW1+ SVuhg97ikp43TNmAlOM2hZtp/ZV3AeN1Hfpy9ephNxzwWlcOQ2tjXVH22DDyRvIJJs +ho2Yv50vsVcA== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3w-00000000Bvx-0ivC; Mon, 07 Jul 2025 21:24:00 -0400 Message-ID: <20250708012400.023290498@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:53 -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 , Florian Weimer , Sam James Subject: [PATCH v13 14/14] unwind_user/x86: Enable compat mode frame pointer unwinding on x86 References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Use ARCH_INIT_USER_COMPAT_FP_FRAME to describe how frame pointers are unwound on x86, and implement the hooks needed to add the segment base addresses. Enable HAVE_UNWIND_USER_COMPAT_FP if the system has compat mode compiled in. Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- arch/x86/Kconfig | 1 + arch/x86/include/asm/unwind_user.h | 31 ++++++++++++++++++++++++ arch/x86/include/asm/unwind_user_types.h | 17 +++++++++++++ arch/x86/kernel/stacktrace.c | 28 +++++++++++++++++++++ include/linux/unwind_user.h | 20 +++++++++++++++ kernel/unwind/user.c | 4 +++ 6 files changed, 101 insertions(+) create mode 100644 arch/x86/include/asm/unwind_user_types.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 5862433c81e1..17d4094c821b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -302,6 +302,7 @@ config X86 select HAVE_SYSCALL_TRACEPOINTS select HAVE_UACCESS_VALIDATION if HAVE_OBJTOOL select HAVE_UNSTABLE_SCHED_CLOCK + select HAVE_UNWIND_USER_COMPAT_FP if IA32_EMULATION select HAVE_UNWIND_USER_FP if X86_64 select HAVE_USER_RETURN_NOTIFIER select HAVE_GENERIC_VDSO diff --git a/arch/x86/include/asm/unwind_user.h b/arch/x86/include/asm/unwi= nd_user.h index 8597857bf896..19634a73612d 100644 --- a/arch/x86/include/asm/unwind_user.h +++ b/arch/x86/include/asm/unwind_user.h @@ -2,10 +2,41 @@ #ifndef _ASM_X86_UNWIND_USER_H #define _ASM_X86_UNWIND_USER_H =20 +#include + #define ARCH_INIT_USER_FP_FRAME \ .cfa_off =3D (s32)sizeof(long) * 2, \ .ra_off =3D (s32)sizeof(long) * -1, \ .fp_off =3D (s32)sizeof(long) * -2, \ .use_fp =3D true, =20 +#ifdef CONFIG_IA32_EMULATION + +#define ARCH_INIT_USER_COMPAT_FP_FRAME \ + .cfa_off =3D (s32)sizeof(u32) * 2, \ + .ra_off =3D (s32)sizeof(u32) * -1, \ + .fp_off =3D (s32)sizeof(u32) * -2, \ + .use_fp =3D true, + +#define in_compat_mode(regs) !user_64bit_mode(regs) + +void arch_unwind_user_init(struct unwind_user_state *state, + struct pt_regs *regs); + +static inline void arch_unwind_user_next(struct unwind_user_state *state) +{ + if (state->type !=3D UNWIND_USER_TYPE_COMPAT_FP) + return; + + state->ip +=3D state->arch.cs_base; + state->fp +=3D state->arch.ss_base; +} + +#define arch_unwind_user_init arch_unwind_user_init +#define arch_unwind_user_next arch_unwind_user_next + +#endif /* CONFIG_IA32_EMULATION */ + +#include + #endif /* _ASM_X86_UNWIND_USER_H */ diff --git a/arch/x86/include/asm/unwind_user_types.h b/arch/x86/include/as= m/unwind_user_types.h new file mode 100644 index 000000000000..f93d535f900e --- /dev/null +++ b/arch/x86/include/asm/unwind_user_types.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_UNWIND_USER_TYPES_H +#define _ASM_X86_UNWIND_USER_TYPES_H + +#ifdef CONFIG_IA32_EMULATION + +struct arch_unwind_user_state { + unsigned long ss_base; + unsigned long cs_base; +}; +#define arch_unwind_user_state arch_unwind_user_state + +#endif /* CONFIG_IA32_EMULATION */ + +#include + +#endif /* _ASM_UNWIND_USER_TYPES_H */ diff --git a/arch/x86/kernel/stacktrace.c b/arch/x86/kernel/stacktrace.c index ee117fcf46ed..8ef9d8c71df9 100644 --- a/arch/x86/kernel/stacktrace.c +++ b/arch/x86/kernel/stacktrace.c @@ -9,7 +9,10 @@ #include #include #include +#include #include +#include +#include #include =20 void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, @@ -128,3 +131,28 @@ void arch_stack_walk_user(stack_trace_consume_fn consu= me_entry, void *cookie, } } =20 +#ifdef CONFIG_IA32_EMULATION +void arch_unwind_user_init(struct unwind_user_state *state, + struct pt_regs *regs) +{ + unsigned long cs_base, ss_base; + + if (state->type !=3D UNWIND_USER_TYPE_COMPAT_FP) + return; + + cs_base =3D insn_get_seg_base(regs, INAT_SEG_REG_CS); + ss_base =3D insn_get_seg_base(regs, INAT_SEG_REG_SS); + + if (cs_base =3D=3D -1) + cs_base =3D 0; + if (ss_base =3D=3D -1) + ss_base =3D 0; + + state->arch.cs_base =3D cs_base; + state->arch.ss_base =3D ss_base; + + state->ip +=3D cs_base; + state->sp +=3D ss_base; + state->fp +=3D ss_base; +} +#endif /* CONFIG_IA32_EMULATION */ diff --git a/include/linux/unwind_user.h b/include/linux/unwind_user.h index 834b643afd3a..8a4af0214ecb 100644 --- a/include/linux/unwind_user.h +++ b/include/linux/unwind_user.h @@ -14,6 +14,26 @@ #define in_compat_mode(regs) false #endif =20 +/* + * If an architecture needs to initialize the state for a specific + * reason, for example, it may need to do something different + * in compat mode, it can define a macro named arch_unwind_user_init + * with the name of the function that will perform this initialization. + */ +#ifndef arch_unwind_user_init +static inline void arch_unwind_user_init(struct unwind_user_state *state, = struct pt_regs *reg) {} +#endif + +/* + * If an architecture requires some more updates to the state between + * stack frames, it can define a macro named arch_unwind_user_next + * with the name of the function that will update the state between + * reading stack frames during the user space stack walk. + */ +#ifndef arch_unwind_user_next +static inline void arch_unwind_user_next(struct unwind_user_state *state) = {} +#endif + int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries); =20 #endif /* _LINUX_UNWIND_USER_H */ diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c index 03775191447c..249d9e32fad7 100644 --- a/kernel/unwind/user.c +++ b/kernel/unwind/user.c @@ -89,6 +89,8 @@ static int unwind_user_next(struct unwind_user_state *sta= te) if (frame->fp_off) state->fp =3D fp; =20 + arch_unwind_user_next(state); + return 0; =20 done: @@ -118,6 +120,8 @@ static int unwind_user_start(struct unwind_user_state *= state) state->sp =3D user_stack_pointer(regs); state->fp =3D frame_pointer(regs); =20 + arch_unwind_user_init(state, regs); + return 0; } =20 --=20 2.47.2