[PATCH] sched: print parent comm in sched_show_task()

Tio Zhang posted 1 patch 2 years, 8 months ago
There is a newer version of this series
kernel/sched/core.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
[PATCH] sched: print parent comm in sched_show_task()
Posted by Tio Zhang 2 years, 8 months ago
Knowing who the parent is might be useful for debugging.
For example, we can sometimes resolve kernel hung tasks by stopping
the person who begins those hung tasks.
With the parent's name printed in sched_show_task(),
it might be helpful to let people know which "service" should be operated.

Signed-off-by: Tio Zhang <tiozhang@didiglobal.com>
---
 kernel/sched/core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index cb2aa2b54c7a..6f4aef0fed58 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -8854,6 +8854,7 @@ void sched_show_task(struct task_struct *p)
 {
 	unsigned long free = 0;
 	int ppid;
+	char pcomm[TASK_COMM_LEN];
 
 	if (!try_get_task_stack(p))
 		return;
@@ -8867,11 +8868,13 @@ void sched_show_task(struct task_struct *p)
 #endif
 	ppid = 0;
 	rcu_read_lock();
-	if (pid_alive(p))
+	if (pid_alive(p)) {
 		ppid = task_pid_nr(rcu_dereference(p->real_parent));
+		get_task_comm(pcomm, rcu_dereference(p->real_parent));
+	}
 	rcu_read_unlock();
-	pr_cont(" stack:%-5lu pid:%-5d ppid:%-6d flags:0x%08lx\n",
-		free, task_pid_nr(p), ppid,
+	pr_cont(" stack:%-5lu pid:%-5d ppid:%-6d parent:%-15.15s flags:0x%08lx\n",
+		free, task_pid_nr(p), ppid, pcomm,
 		read_task_thread_flags(p));
 
 	print_worker_info(KERN_INFO, p);
-- 
2.17.1
Re: [PATCH] sched: print parent comm in sched_show_task()
Posted by Petr Mladek 2 years, 8 months ago
On Wed 2022-12-28 00:14:00, Tio Zhang wrote:
> Knowing who the parent is might be useful for debugging.
> For example, we can sometimes resolve kernel hung tasks by stopping
> the person who begins those hung tasks.
> With the parent's name printed in sched_show_task(),
> it might be helpful to let people know which "service" should be operated.

> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -8854,6 +8854,7 @@ void sched_show_task(struct task_struct *p)
>  {
>  	unsigned long free = 0;
>  	int ppid;
> +	char pcomm[TASK_COMM_LEN];
>  
>  	if (!try_get_task_stack(p))
>  		return;
> @@ -8867,11 +8868,13 @@ void sched_show_task(struct task_struct *p)
>  #endif
>  	ppid = 0;

We need to intialized pcomm here:

	pcomm[0] = '\0';

Otherwise, it would include a garbage when pid_alive(p) returns false below..

>  	rcu_read_lock();
> -	if (pid_alive(p))
> +	if (pid_alive(p)) {
>  		ppid = task_pid_nr(rcu_dereference(p->real_parent));
> +		get_task_comm(pcomm, rcu_dereference(p->real_parent));
> +	}
>  	rcu_read_unlock();
> -	pr_cont(" stack:%-5lu pid:%-5d ppid:%-6d flags:0x%08lx\n",
> -		free, task_pid_nr(p), ppid,
> +	pr_cont(" stack:%-5lu pid:%-5d ppid:%-6d parent:%-15.15s
> flags:0x%08lx\n",

It would print:  .... parent:xxx flags:0000

Some people might be confused whether the flags are from
the task or from the parent.

A solution would be to move the parent value to another line.
It would even better solve the situation when the task
is not alive and we could not get information about the parent:

	if (pid_alive(p)) {
		struct parent = rcu_dereference(p->real_parent);

		pr_info("parent:%-15.15s ppid:%-6d\n",
			parent->comm, task_pid_nr(parent));
	}

> +		free, task_pid_nr(p), ppid, pcomm,
>  		read_task_thread_flags(p));
>  
>  	print_worker_info(KERN_INFO, p);

Best Regards,
Petr
Re: [PATCH] sched: print parent comm in sched_show_task()
Posted by Chen Yu 2 years, 8 months ago
On 2022-12-28 at 00:14:00 +0800, Tio Zhang wrote:
> Knowing who the parent is might be useful for debugging.
> For example, we can sometimes resolve kernel hung tasks by stopping
> the person who begins those hung tasks.
> With the parent's name printed in sched_show_task(),
> it might be helpful to let people know which "service" should be operated.
> 
> Signed-off-by: Tio Zhang <tiozhang@didiglobal.com>
> ---
>  kernel/sched/core.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index cb2aa2b54c7a..6f4aef0fed58 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -8854,6 +8854,7 @@ void sched_show_task(struct task_struct *p)
>  {
>  	unsigned long free = 0;
>  	int ppid;
> +	char pcomm[TASK_COMM_LEN];
>  
>  	if (!try_get_task_stack(p))
>  		return;
> @@ -8867,11 +8868,13 @@ void sched_show_task(struct task_struct *p)
>  #endif
>  	ppid = 0;
>  	rcu_read_lock();
> -	if (pid_alive(p))
> +	if (pid_alive(p)) {
>  		ppid = task_pid_nr(rcu_dereference(p->real_parent));
> +		get_task_comm(pcomm, rcu_dereference(p->real_parent));
Maybe struct task_struct *parent = rcu_dereference(p->real_parent);
and use parent directly to get its pid and comm?
Maybe off-topic, what if the parent is a kernel thread/worker? It might have extra
name information such as kthread->full_name or worker->desc according to proc_task_name().

thanks,