From nobody Mon Oct 6 03:12:39 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 F20CF28688C for ; Sat, 26 Jul 2025 14:12:17 +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=1753539138; cv=none; b=huHK3hiLJ2T2Wj5WknR9bnc6PQzbYhC8sNkEG0OhDjEE6IopWFKeumCDjIj7OL1bycSMmjOZ+kyw3GltnQRQ5NWNSPOezr/cfTBNOZNdJkPI/Jx5UJFEUrp4ztebYJG3VJajGma3kpH61MycdY3laBFtGsHJgDd6qVboc4t98Nw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753539138; c=relaxed/simple; bh=TZpjkXBbQZ0/7HMBCrfXxzxFf0zNLQMS0dsRZI7G/40=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=K+tUnka6tsftzBunM/9oRkWnCUuhcTLLFxDTIKYx3nR/J3CLD/kLgggU6jGzYK+G1wZ0iUjT1j+b3Q5jN5b/X7R/XehZBB52PuY18UrL9Cs6v6DQPoqAcHQWPwTNKRvBM4p6ATC/rSOxUPG9hKsfzOwiRFF+57vhwrWmgfg0PPw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gBserE0q; 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="gBserE0q" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D05EBC4CEF6; Sat, 26 Jul 2025 14:12:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753539137; bh=TZpjkXBbQZ0/7HMBCrfXxzxFf0zNLQMS0dsRZI7G/40=; h=Date:From:To:Cc:Subject:References:From; b=gBserE0qjIuMoap6TaoVC2lXIpgtZ2ZrcLuFmDykwMli4XaIf7hy28sNROcwwdHOe wirPb02es16diVd43tzmP4x5wrdMc8MqF0Q9EfrGm+ytGx7KLgeJlQjL5TuK1lS8OP Dh9tYD+z5bbyZQJx/hsxKZggvUsegUhZ64xOAT6bNwRrt2H4QD7/NddeEBc/kVELdA SAdC2CENCuoh26N00CMbgoKjDkJnHz140/3pfLH2B/V7aXbtDnUar6SPMYw1wrA7fp cnE4BvdWX57FBIpyYIU0nD8wd0/YII6SRia15XDLs96mIl1Gjq480+Tq776svForKe 2erLEWL7LG1FA== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uffdR-00000001sg7-2bUM; Sat, 26 Jul 2025 10:12:25 -0400 Message-ID: <20250726141225.470646928@kernel.org> User-Agent: quilt/0.68 Date: Sat, 26 Jul 2025 10:07:14 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Peter Zijlstra , Linus Torvalds , Ingo Molnar , Josh Poimboeuf , Jiri Olsa , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Andrii Nakryiko , Indu Bhagat , "Jose E. Marchesi" , Beau Belgrave , Jens Remus , Jens Axboe , Florian Weimer , Sam James Subject: [for-next][PATCH 10/10] unwind: Finish up unwind when a task exits References: <20250726140704.560579628@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. Cc: Masami Hiramatsu Cc: Mathieu Desnoyers Cc: Josh Poimboeuf Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Jiri Olsa Cc: Arnaldo Carvalho de Melo Cc: Namhyung Kim Cc: Thomas Gleixner Cc: Andrii Nakryiko Cc: Indu Bhagat Cc: "Jose E. Marchesi" Cc: Beau Belgrave Cc: Jens Remus Cc: Linus Torvalds Cc: Andrew Morton Cc: Jens Axboe Cc: Florian Weimer Cc: Sam James Link: https://lore.kernel.org/20250725185740.748555530@kernel.org 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 2efbda01e959..26122d00708a 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; @@ -71,6 +73,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 a5ef1c1f915e..dc6040aae3ee 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -114,7 +114,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) { @@ -147,9 +147,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; @@ -186,6 +186,23 @@ static void unwind_deferred_task_work(struct callback_= head *head) } } =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 kernel exit * @work: Unwind descriptor requesting the trace --=20 2.47.2