Skip to contentSkip to conversation

exit: move and extend sched_process_exit() tracepoint

PATCH Applied Andrii Nakryiko 6 participants 9 messages Apr 2, 2025 – Apr 4, 2025

Subsystems (2)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH v2 1/1 exit: move and extend sched_process_exit() tracepoint

It is useful to be able to access current->mm at task exit to, say, record a bunch of VMA information right before the task exits (e.g., for stack symbolization reasons when dealing with short-lived processes that exit in the middle of profiling session). Currently, trace_sched_process_exit() is triggered after exit_mm() which resets current->mm to NULL making this tracepoint unsuitable for inspecting and recording task's mm_struct-related data when tracing process lifetimes.

There is a particularly suitable place, though, right after taskstats_exit() is called, but before we do exit_mm() and other exit_*() resource teardowns. taskstats performs a similar kind of accounting that some applications do with BPF, and so co-locating them seems like a good fit. So that's where trace_sched_process_exit() is moved with this patch.

Also, existing trace_sched_process_exit() tracepoint is notoriously missing group_dead flag that is certainly useful in practice and some of our production applications have to work around this. So plumb group_dead through while at it, to have a richer and more complete tracepoint.

Note that we can't use sched_process_template anymore, and so we use TRACE_EVENT()-based tracepoint definition. But all the field names and order, as well as assign and output logic remain intact. We just add one extra field at the end in backwards-compatible way.

---
 include/trace/events/sched.h | 28 +++++++++++++++++++++++++---
 kernel/exit.c                |  2 +-
 2 files changed, 26 insertions(+), 4 deletions(-)
include/trace/events/sched.h +25 -3
index 8994e97d86c1..05a14f2b35c3 100644
@@ -328,9 +328,31 @@ DEFINE_EVENT(sched_process_template, sched_process_free,
 /*
  * Tracepoint for a task exiting:
  */
-DEFINE_EVENT(sched_process_template, sched_process_exit,
-	     TP_PROTO(struct task_struct *p),
-	     TP_ARGS(p));
+TRACE_EVENT(sched_process_exit,
+
+	TP_PROTO(struct task_struct *p, bool group_dead),
+
+	TP_ARGS(p, group_dead),
+
+	TP_STRUCT__entry(
+		__array(	char,	comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	pid			)
+		__field(	int,	prio			)
+		__field(	bool,	group_dead		)
+	),
+
+	TP_fast_assign(
+		memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
+		__entry->pid		= p->pid;
+		__entry->prio		= p->prio; /* XXX SCHED_DEADLINE */
+		__entry->group_dead	= group_dead;
+	),
+
+	TP_printk("comm=%s pid=%d prio=%d group_dead=%s",
+		  __entry->comm, __entry->pid, __entry->prio,
+		  __entry->group_dead ? "true" : "false"
+	)
+);
 
 /*
  * Tracepoint for waiting on task to unschedule:
kernel/exit.c +1 -1
index c2e6c7b7779f..4abd307b1586 100644
@@ -937,12 +937,12 @@ void __noreturn do_exit(long code)
 
 	tsk->exit_code = code;
 	taskstats_exit(tsk, group_dead);
+	trace_sched_process_exit(tsk, group_dead);
 
 	exit_mm();
 
 	if (group_dead)
 		acct_process();
-	trace_sched_process_exit(tsk);
 
 	exit_sem(tsk);
 	exit_shm(tsk);
Signature · 1 line
2.47.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 27 lines · On Wed, 2 Apr 2025 11:09:25 -0700 Andrii Nakryiko <[email protected]> wrote:

It is useful to be able to access current->mm at task exit to, say, record a bunch of VMA information right before the task exits (e.g., for stack symbolization reasons when dealing with short-lived processes that exit in the middle of profiling session). Currently, trace_sched_process_exit() is triggered after exit_mm() which resets current->mm to NULL making this tracepoint unsuitable for inspecting and recording task's mm_struct-related data when tracing process lifetimes.

There is a particularly suitable place, though, right after taskstats_exit() is called, but before we do exit_mm() and other exit_*() resource teardowns. taskstats performs a similar kind of accounting that some applications do with BPF, and so co-locating them seems like a good fit. So that's where trace_sched_process_exit() is moved with this patch.

Also, existing trace_sched_process_exit() tracepoint is notoriously missing group_dead flag that is certainly useful in practice and some of our production applications have to work around this. So plumb group_dead through while at it, to have a richer and more complete tracepoint.

Note that we can't use sched_process_template anymore, and so we use TRACE_EVENT()-based tracepoint definition. But all the field names and order, as well as assign and output logic remain intact. We just add one extra field at the end in backwards-compatible way.

Quoted 5 lines
---
 include/trace/events/sched.h | 28 +++++++++++++++++++++++++---
 kernel/exit.c                |  2 +-
 2 files changed, 26 insertions(+), 4 deletions(-)

-- Steve

Acked-by on v2 1/1
#2 lore raw
In reply to Andrii Nakryiko
Quoted 4 lines · Andrii Nakryiko

kernel/exit.c void __noreturn do_exit(long code) · line 945

Quoted 10 lines
kernel/exit.c +1 -1
@@ -937,12 +937,12 @@ void __noreturn do_exit(long code)
 
 	tsk->exit_code = code;
 	taskstats_exit(tsk, group_dead);
+	trace_sched_process_exit(tsk, group_dead);
 
 	exit_mm();
 
 	if (group_dead)
 		acct_process();
-	trace_sched_process_exit(tsk);

I see nothing wrong in this change.

(and I think that the current placement of trace_sched_process_exit() was
more or less "random").

#3 lore raw
In reply to Andrii Nakryiko
Quoted 16 lines · Andrii Nakryiko

It is useful to be able to access current->mm at task exit to, say, record a bunch of VMA information right before the task exits (e.g., for stack symbolization reasons when dealing with short-lived processes that exit in the middle of profiling session). Currently, trace_sched_process_exit() is triggered after exit_mm() which resets current->mm to NULL making this tracepoint unsuitable for inspecting and recording task's mm_struct-related data when tracing process lifetimes.

There is a particularly suitable place, though, right after taskstats_exit() is called, but before we do exit_mm() and other exit_*() resource teardowns. taskstats performs a similar kind of accounting that some applications do with BPF, and so co-locating them seems like a good fit. So that's where trace_sched_process_exit() is moved with this patch.

Quoted 8 lines

Also, existing trace_sched_process_exit() tracepoint is notoriously missing group_dead flag that is certainly useful in practice and some of our production applications have to work around this. So plumb group_dead through while at it, to have a richer and more complete tracepoint.

Note that we can't use sched_process_template anymore, and so we use TRACE_EVENT()-based tracepoint definition.

But all the field names and

Quoted 34 lines

order, as well as assign and output logic remain intact. We just add one extra field at the end in backwards-compatible way.

---
 include/trace/events/sched.h | 28 +++++++++++++++++++++++++---
 kernel/exit.c                |  2 +-
 2 files changed, 26 insertions(+), 4 deletions(-)
include/trace/events/sched.h +14 -3
index 8994e97d86c1..05a14f2b35c3 100644
@@ -328,9 +328,31 @@ DEFINE_EVENT(sched_process_template, sched_process_free,
 /*
  * Tracepoint for a task exiting:
  */
-DEFINE_EVENT(sched_process_template, sched_process_exit,
-	     TP_PROTO(struct task_struct *p),
-	     TP_ARGS(p));
+TRACE_EVENT(sched_process_exit,
+
+	TP_PROTO(struct task_struct *p, bool group_dead),
+
+	TP_ARGS(p, group_dead),
+
+	TP_STRUCT__entry(
+		__array(	char,	comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	pid			)
+		__field(	int,	prio			)
+		__field(	bool,	group_dead		)
+	),
+
+	TP_fast_assign(

include/trace/events/sched.h DEFINE_EVENT(sched_process_template, sched_process_free, · line 354

Quoted 10 lines
include/trace/events/sched.h +10 -0
+		memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
+		__entry->pid		= p->pid;
+		__entry->prio		= p->prio; /* XXX SCHED_DEADLINE */
+		__entry->group_dead	= group_dead;
+	),
+
+	TP_printk("comm=%s pid=%d prio=%d group_dead=%s",
+		  __entry->comm, __entry->pid, __entry->prio,
+		  __entry->group_dead ? "true" : "false"
+	)

This feels really fragile, could you please at least add a comment that points out that this is basically an extension of sched_process_template, and that it should remain a subset of it, or something to that end?

Thanks,

Ingo

maintainer
#4 lore raw
Quoted 4 lines · On Thu, 3 Apr 2025 15:54:22 +0200 Ingo Molnar <[email protected]> wrote:

This feels really fragile, could you please at least add a comment that points out that this is basically an extension of sched_process_template, and that it should remain a subset of it, or something to that end?

Is there any dependency on this?

The reason to use the templates is because it saves memory. Each TRACE_EVENT() can add ~5k (which a TRACE_EVENT() is really just a DECLARE_EVENT_CLASS() + DEFINE_EVENT() for a single event).

Each DEFINE_EVENT() just adds around 250 bytes. Hence, if you have multiple events that share the same fields and output, it's much more memory efficient to use the CLASS and EVENT logic then making each their own TRACE_EVENT().

I don't know of any other dependency to why this was a template other than to save memory.

-- Steve

#5 lore raw
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Thu, 3 Apr 2025 15:54:22 +0200 Ingo Molnar <[email protected]> wrote:

This feels really fragile, could you please at least add a comment that points out that this is basically an extension of sched_process_template, and that it should remain a subset of it, or something to that end?

Quoted 4 lines

Is there any dependency on this?

I don't know of any other dependency to why this was a template other than to save memory.

Uhm, to state the obvious: to not replicate the same definitions over and over again three times times, for 3 scheduler tracepoints that share the record format?

Removing just a single sched_process_template use bloats the source and adds in potential fragility:

 2 files changed, 26 insertions(+), 4 deletions(-)

So my request is to please at least add a comment that points the reader to the shared record format between sched_process_exit and the other two tracepoints.

Thanks,

Ingo

#6 lore raw
Quoted 21 lines · Ingo Molnar
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Thu, 3 Apr 2025 15:54:22 +0200 Ingo Molnar <[email protected]> wrote:

This feels really fragile, could you please at least add a comment that points out that this is basically an extension of sched_process_template, and that it should remain a subset of it, or something to that end?

Quoted 4 lines

Is there any dependency on this?

I don't know of any other dependency to why this was a template other than to save memory.

Uhm, to state the obvious: to not replicate the same definitions over and over again three times times, for 3 scheduler tracepoints that share the record format?

Quoted 8 lines

Removing just a single sched_process_template use bloats the source and adds in potential fragility:

 2 files changed, 26 insertions(+), 4 deletions(-)

So my request is to please at least add a comment that points the reader to the shared record format between sched_process_exit and the other two tracepoints.

Sounds good, no problem. I'll send a follow up patch which Andrew can fold, if he prefers.

Quoted 4 lines

Thanks,

Ingo

#7 lore raw

[tip: sched/core] sched/tracepoints: Move and extend the sched_process_exit() tracepoint

In reply to Andrii Nakryiko
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     3e816361e94a0e79b1aabf44abec552e9698b196
Gitweb:        https://git.kernel.org/tip/3e816361e94a0e79b1aabf44abec552e9698b196
Author:        Andrii Nakryiko <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 11:09:25 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 04 Apr 2025 10:30:19 +02:00

sched/tracepoints: Move and extend the sched_process_exit() tracepoint

It is useful to be able to access current->mm at task exit to, say, record a bunch of VMA information right before the task exits (e.g., for stack symbolization reasons when dealing with short-lived processes that exit in the middle of profiling session). Currently, trace_sched_process_exit() is triggered after exit_mm() which resets current->mm to NULL making this tracepoint unsuitable for inspecting and recording task's mm_struct-related data when tracing process lifetimes.

There is a particularly suitable place, though, right after taskstats_exit() is called, but before we do exit_mm() and other exit_*() resource teardowns. taskstats performs a similar kind of accounting that some applications do with BPF, and so co-locating them seems like a good fit. So that's where trace_sched_process_exit() is moved with this patch.

Also, existing trace_sched_process_exit() tracepoint is notoriously missing group_dead flag that is certainly useful in practice and some of our production applications have to work around this. So plumb group_dead through while at it, to have a richer and more complete tracepoint.

Note that we can't use sched_process_template anymore, and so we use TRACE_EVENT()-based tracepoint definition. But all the field names and order, as well as assign and output logic remain intact. We just add one extra field at the end in backwards-compatible way.

Document the dependency to sched_process_template anyway.

---
 include/trace/events/sched.h | 34 ++++++++++++++++++++++++++++++----
 kernel/exit.c                |  2 +-
 2 files changed, 31 insertions(+), 5 deletions(-)
include/trace/events/sched.h +30 -4
index 8994e97..3bec9fb 100644
@@ -326,11 +326,37 @@ DEFINE_EVENT(sched_process_template, sched_process_free,
 	     TP_ARGS(p));
 
 /*
- * Tracepoint for a task exiting:
+ * Tracepoint for a task exiting.
+ * Note, it's a superset of sched_process_template and should be kept
+ * compatible as much as possible. sched_process_exits has an extra
+ * `group_dead` argument, so sched_process_template can't be used,
+ * unfortunately, just like sched_migrate_task above.
  */
-DEFINE_EVENT(sched_process_template, sched_process_exit,
-	     TP_PROTO(struct task_struct *p),
-	     TP_ARGS(p));
+TRACE_EVENT(sched_process_exit,
+
+	TP_PROTO(struct task_struct *p, bool group_dead),
+
+	TP_ARGS(p, group_dead),
+
+	TP_STRUCT__entry(
+		__array(	char,	comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	pid			)
+		__field(	int,	prio			)
+		__field(	bool,	group_dead		)
+	),
+
+	TP_fast_assign(
+		memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
+		__entry->pid		= p->pid;
+		__entry->prio		= p->prio; /* XXX SCHED_DEADLINE */
+		__entry->group_dead	= group_dead;
+	),
+
+	TP_printk("comm=%s pid=%d prio=%d group_dead=%s",
+		  __entry->comm, __entry->pid, __entry->prio,
+		  __entry->group_dead ? "true" : "false"
+	)
+);
 
 /*
  * Tracepoint for waiting on task to unschedule:
kernel/exit.c +1 -1
index 1b51dc0..f1db86d 100644
@@ -936,12 +936,12 @@ void __noreturn do_exit(long code)
 
 	tsk->exit_code = code;
 	taskstats_exit(tsk, group_dead);
+	trace_sched_process_exit(tsk, group_dead);
 
 	exit_mm();
 
 	if (group_dead)
 		acct_process();
-	trace_sched_process_exit(tsk);
 
 	exit_sem(tsk);
 	exit_shm(tsk);
#8 lore raw

Re: [tip: sched/core] sched/tracepoints: Move and extend the sched_process_exit() tracepoint

Quoted 39 lines · On Fri, Apr 4, 2025 at 1:37 AM tip-bot2 for Andrii Nakryiko…

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     3e816361e94a0e79b1aabf44abec552e9698b196
Gitweb:        https://git.kernel.org/tip/3e816361e94a0e79b1aabf44abec552e9698b196
Author:        Andrii Nakryiko <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 11:09:25 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 04 Apr 2025 10:30:19 +02:00

sched/tracepoints: Move and extend the sched_process_exit() tracepoint

It is useful to be able to access current->mm at task exit to, say, record a bunch of VMA information right before the task exits (e.g., for stack symbolization reasons when dealing with short-lived processes that exit in the middle of profiling session). Currently, trace_sched_process_exit() is triggered after exit_mm() which resets current->mm to NULL making this tracepoint unsuitable for inspecting and recording task's mm_struct-related data when tracing process lifetimes.

There is a particularly suitable place, though, right after taskstats_exit() is called, but before we do exit_mm() and other exit_*() resource teardowns. taskstats performs a similar kind of accounting that some applications do with BPF, and so co-locating them seems like a good fit. So that's where trace_sched_process_exit() is moved with this patch.

Also, existing trace_sched_process_exit() tracepoint is notoriously missing group_dead flag that is certainly useful in practice and some of our production applications have to work around this. So plumb group_dead through while at it, to have a richer and more complete tracepoint.

Note that we can't use sched_process_template anymore, and so we use TRACE_EVENT()-based tracepoint definition. But all the field names and order, as well as assign and output logic remain intact. We just add one extra field at the end in backwards-compatible way.

Quoted 4 lines

Document the dependency to sched_process_template anyway.

Adding Andrew.

Seems like my patch was applied both by Andrew ([0], [1]) and Ingo. Andew, would it be possible to drop those from your tree and keep the one in Ingo's tip/sched/core? Thanks!

[0] https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/exit-move-and-extend-sched_process_exit-tracepoint.patch
[1] https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/exit-move-and-extend-sched_process_exit-tracepoint-fix.patch

Quoted 73 lines
---
 include/trace/events/sched.h | 34 ++++++++++++++++++++++++++++++----
 kernel/exit.c                |  2 +-
 2 files changed, 31 insertions(+), 5 deletions(-)
include/trace/events/sched.h +30 -4
index 8994e97..3bec9fb 100644
@@ -326,11 +326,37 @@ DEFINE_EVENT(sched_process_template, sched_process_free,
             TP_ARGS(p));
 
 /*
- * Tracepoint for a task exiting:
+ * Tracepoint for a task exiting.
+ * Note, it's a superset of sched_process_template and should be kept
+ * compatible as much as possible. sched_process_exits has an extra
+ * `group_dead` argument, so sched_process_template can't be used,
+ * unfortunately, just like sched_migrate_task above.
  */
-DEFINE_EVENT(sched_process_template, sched_process_exit,
-            TP_PROTO(struct task_struct *p),
-            TP_ARGS(p));
+TRACE_EVENT(sched_process_exit,
+
+       TP_PROTO(struct task_struct *p, bool group_dead),
+
+       TP_ARGS(p, group_dead),
+
+       TP_STRUCT__entry(
+               __array(        char,   comm,   TASK_COMM_LEN   )
+               __field(        pid_t,  pid                     )
+               __field(        int,    prio                    )
+               __field(        bool,   group_dead              )
+       ),
+
+       TP_fast_assign(
+               memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
+               __entry->pid            = p->pid;
+               __entry->prio           = p->prio; /* XXX SCHED_DEADLINE */
+               __entry->group_dead     = group_dead;
+       ),
+
+       TP_printk("comm=%s pid=%d prio=%d group_dead=%s",
+                 __entry->comm, __entry->pid, __entry->prio,
+                 __entry->group_dead ? "true" : "false"
+       )
+);
 
 /*
  * Tracepoint for waiting on task to unschedule:
kernel/exit.c +1 -1
index 1b51dc0..f1db86d 100644
@@ -936,12 +936,12 @@ void __noreturn do_exit(long code)
 
        tsk->exit_code = code;
        taskstats_exit(tsk, group_dead);
+       trace_sched_process_exit(tsk, group_dead);
 
        exit_mm();
 
        if (group_dead)
                acct_process();
-       trace_sched_process_exit(tsk);
 
        exit_sem(tsk);
        exit_shm(tsk);