From mboxrd@z Thu Jan 1 00:00:00 1970 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 14A4D25B305; Fri, 25 Jul 2025 18:57:35 +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=1753469855; cv=none; b=oyv85wQSH6rxizb0X2xbfSBHQMATwba0hJfMekazPRO6qEGnO/m7uSyDdyKHo94v9/Izx0IY//ZgpStdfOohLgJk7HNps9kz0wzhgraIZ8+37k9M69ZXiMpcwAXD3m9bjGPYURPokNn26wPsSAAoPdEve8W9LgKalrkYDi/HWkk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753469855; c=relaxed/simple; bh=SlunTMynvCXTvjDf/X2VPHqD6dfEMvytmKxKKnIYpHk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=ekasy9mQbzcoQbUdYB7Gn5Ry4rB1LwWl7otmP9FRWvbjZ989xfjF6P83/tmkEBDlnLqsV76b7PZ4xlUTcveyMY7Sk3u4J0tNB3Jl1F7zKFmON+sDf75VyEMF0qJebe4X46xe1T0KTiZ3xGuHLe1K8wvuoY00KwS/DLeE5rO/bDs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p9vgtE0X; 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="p9vgtE0X" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4736C4CEF9; Fri, 25 Jul 2025 18:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753469855; bh=SlunTMynvCXTvjDf/X2VPHqD6dfEMvytmKxKKnIYpHk=; h=Date:From:To:Cc:Subject:References:From; b=p9vgtE0XjqOERWZAY+A7yGhrUg5aGIESjUlGKJ0qtXUz5+Ed9cPCPaintMbDAULiH KRS/lK1crLni2oqaMC9KejP4ZIrZ0ttMs1P83FV3l8xjtakboJ4Dr9hWUsXypWbKGw PfkLBpTlyJMW+bSG9NWmVjMc+8pUny/fdW0jkIdWTLiX5zW5SduzP1UTNolXxr3oTt rWS/HM2WnEc0R8lLT+ZD9qkQsGFRYvC0oBt9QG2sfp6U7VhCf1O8ylz11WuImm8QAP A4IQp8JQFuzA82PHJovnzI+pE2O2ju2IS2W9AsWTUjmUpSnkDFsMmq1vLmVl+E31/c r1o83jNbThddw== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1ufNbw-00000001N7w-3kf8; Fri, 25 Jul 2025 14:57:40 -0400 Message-ID: <20250725185740.748555530@kernel.org> User-Agent: quilt/0.68 Date: Fri, 25 Jul 2025 14:55:22 -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 , Arnaldo Carvalho de Melo , 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 v15 10/10] unwind: Finish up unwind when a task exits References: <20250725185512.673587297@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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_deferred.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); +void unwind_deferred_task_exit(struct task_struct *task); + static __always_inline void unwind_reset_info(void) { struct unwind_task_info *info = ¤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 *timestamp) { return -ENOSYS; } static inline void unwind_deferred_cancel(struct unwind_work *work) {} +static inline void unwind_deferred_task_exit(struct task_struct *task) {} static inline void unwind_reset_info(void) {} #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 @@ -938,6 +939,7 @@ void __noreturn do_exit(long code) tsk->exit_code = code; taskstats_exit(tsk, group_dead); + unwind_deferred_task_exit(tsk); trace_sched_process_exit(tsk, group_dead); /* 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 *trace) /* Should always be called from faultable context */ might_fault(); - if (current->flags & PF_EXITING) + if (!current->mm) return -EINVAL; if (!info->cache) { @@ -147,9 +147,9 @@ int unwind_user_faultable(struct unwind_stacktrace *trace) return 0; } -static void unwind_deferred_task_work(struct callback_head *head) +static void process_unwind_deferred(struct task_struct *task) { - struct unwind_task_info *info = container_of(head, struct unwind_task_info, work); + struct unwind_task_info *info = &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) } } +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 = ¤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 -- 2.47.2