Skip to content

Commit b3b9cb1

Browse files
committed
unwind: Finish up unwind when a task exits
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 <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Indu Bhagat <[email protected]> Cc: "Jose E. Marchesi" <[email protected]> Cc: Beau Belgrave <[email protected]> Cc: Jens Remus <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Florian Weimer <[email protected]> Cc: Sam James <[email protected]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 357eda2 commit b3b9cb1

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

include/linux/unwind_deferred.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func);
3939
int unwind_deferred_request(struct unwind_work *work, u64 *cookie);
4040
void unwind_deferred_cancel(struct unwind_work *work);
4141

42+
void unwind_deferred_task_exit(struct task_struct *task);
43+
4244
static __always_inline void unwind_reset_info(void)
4345
{
4446
struct unwind_task_info *info = &current->unwind_info;
@@ -71,6 +73,7 @@ static inline int unwind_deferred_init(struct unwind_work *work, unwind_callback
7173
static inline int unwind_deferred_request(struct unwind_work *work, u64 *timestamp) { return -ENOSYS; }
7274
static inline void unwind_deferred_cancel(struct unwind_work *work) {}
7375

76+
static inline void unwind_deferred_task_exit(struct task_struct *task) {}
7477
static inline void unwind_reset_info(void) {}
7578

7679
#endif /* !CONFIG_UNWIND_USER */

kernel/exit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include <linux/rethook.h>
6969
#include <linux/sysfs.h>
7070
#include <linux/user_events.h>
71+
#include <linux/unwind_deferred.h>
7172
#include <linux/uaccess.h>
7273
#include <linux/pidfs.h>
7374

@@ -938,6 +939,7 @@ void __noreturn do_exit(long code)
938939

939940
tsk->exit_code = code;
940941
taskstats_exit(tsk, group_dead);
942+
unwind_deferred_task_exit(tsk);
941943
trace_sched_process_exit(tsk, group_dead);
942944

943945
/*

kernel/unwind/deferred.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ int unwind_user_faultable(struct unwind_stacktrace *trace)
114114
/* Should always be called from faultable context */
115115
might_fault();
116116

117-
if (current->flags & PF_EXITING)
117+
if (!current->mm)
118118
return -EINVAL;
119119

120120
if (!info->cache) {
@@ -147,9 +147,9 @@ int unwind_user_faultable(struct unwind_stacktrace *trace)
147147
return 0;
148148
}
149149

150-
static void unwind_deferred_task_work(struct callback_head *head)
150+
static void process_unwind_deferred(struct task_struct *task)
151151
{
152-
struct unwind_task_info *info = container_of(head, struct unwind_task_info, work);
152+
struct unwind_task_info *info = &task->unwind_info;
153153
struct unwind_stacktrace trace;
154154
struct unwind_work *work;
155155
unsigned long bits;
@@ -186,6 +186,23 @@ static void unwind_deferred_task_work(struct callback_head *head)
186186
}
187187
}
188188

189+
static void unwind_deferred_task_work(struct callback_head *head)
190+
{
191+
process_unwind_deferred(current);
192+
}
193+
194+
void unwind_deferred_task_exit(struct task_struct *task)
195+
{
196+
struct unwind_task_info *info = &current->unwind_info;
197+
198+
if (!unwind_pending(info))
199+
return;
200+
201+
process_unwind_deferred(task);
202+
203+
task_work_cancel(task, &info->work);
204+
}
205+
189206
/**
190207
* unwind_deferred_request - Request a user stacktrace on task kernel exit
191208
* @work: Unwind descriptor requesting the trace

0 commit comments

Comments
 (0)