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 E597024729F; Tue, 29 Jul 2025 18:23:51 +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=1753813432; cv=none; b=ZxXvKgCe0oboONYC288jPizh0XhERJF6BJS6Z6KrFyZUzYkf54pDi+s4RMfj3FQ2sZhAqMMMA2IJS2gEiI97rWGDfeB2E8ChNPB80B23MLyICISwB4T5p4bQ8+swm/fuj0NBlGJLAL3MjLarcOFm7uLHEfhzk1WQZegxhuELp14= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753813432; c=relaxed/simple; bh=SlunTMynvCXTvjDf/X2VPHqD6dfEMvytmKxKKnIYpHk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=lwghbfzIbdmt797ie+SzytJsRxBUDpUajSVhMNJfhoDs2zLhEXLDVUNBmu/9NniSEFPC+yw0SDToAgJ1TrI8WjfgREVkJ0pYSLxBDklyi4bwcZvyKq0RaQD9411UF2c89LhFiG899KD69YlSMbMznROcB3GveSFWrJ38+gAMOQc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E/WElp4P; 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="E/WElp4P" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5F0DC4CEF8; Tue, 29 Jul 2025 18:23:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753813431; bh=SlunTMynvCXTvjDf/X2VPHqD6dfEMvytmKxKKnIYpHk=; h=Date:From:To:Cc:Subject:References:From; b=E/WElp4PsYdIpatkV7ZP0/ObR8Adhhr+9IQMuimOIxfi7VDyTDSzmCVm0CqLIl1oI vNhLNr2n1zj+cShp+T4vd+aJ7r7okeU1dDTHsRW18J/c4zJbVfphCgctgaNo3q/uox POtekt7+tqC3RMzTXyJF4AXjLiJcgA5/Bpuyb72AkC148/WJciXVm+KYBgeZ5XjF3s RI8ub7dq5/ODC7524c8bN2Av00GHd44Pw4aUonWjQ2fKfTGl6qZ7Kp0pw0Lc9AQO7d GPZndUrScH8gy+Er1MriHu2Sd40vtpgniqGNS2EBt6H3rVlbqPIuaO72bPvetzSfwl 0jMdgj2AeEv+g== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1ugoze-0000000553T-2kBk; Tue, 29 Jul 2025 14:24:06 -0400 Message-ID: <20250729182406.504259474@kernel.org> User-Agent: quilt/0.68 Date: Tue, 29 Jul 2025 14:23:14 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.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 v16 10/10] unwind: Finish up unwind when a task exits References: <20250729182304.965835871@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