Re: [RFC] sched: add sched_show_task() variant that accepts log level

From: Tomasz Figa
Date: Wed Mar 12 2025 - 10:52:03 EST


Hi Sergey,

On Mon, Mar 10, 2025 at 5:25 PM Sergey Senozhatsky
<senozhatsky@xxxxxxxxxxxx> wrote:
>
> Hi,
>
> A request for comments, not a formal patch.
>
> Is there any reason why hung-task watchdog prints error header
> with KERN_ERR log level while task's backtrace is printed with
> KERN_INFO? Will it make sense to unify log levels and introduce
> sched_show_task() variant that accept log level param so that
> everything that hung-task watchdog prints becomes KERN_ERR?

Thanks a lot for looking into this.

Let me just add that I've been looking into ways to automatically
analyze kernel crash reports and I noticed that when I filter out
KERN_INFO and higher log levels, I end up losing useful information
for hung-task watchdog-kind of problems. This change would greatly
help in filtering out unnecessary noise from the logs.

By the way, if having it as KERN_INFO by default would still be
desirable, I suppose we could add a KConfig option to set the desired
log level?

Best regards,
Tomasz

>
> Something like this:
>
> ---
> include/linux/sched/debug.h | 1 +
> kernel/hung_task.c | 4 ++--
> kernel/sched/core.c | 19 +++++++++++++------
> 3 files changed, 16 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/sched/debug.h b/include/linux/sched/debug.h
> index b5035afa2396..8e46854d16fc 100644
> --- a/include/linux/sched/debug.h
> +++ b/include/linux/sched/debug.h
> @@ -34,6 +34,7 @@ extern void show_stack(struct task_struct *task, unsigned long *sp,
> const char *loglvl);
>
> extern void sched_show_task(struct task_struct *p);
> +extern void sched_show_task_log_lvl(struct task_struct *p, const char *lvl);
>
> #ifdef CONFIG_SCHED_DEBUG
> struct seq_file;
> diff --git a/kernel/hung_task.c b/kernel/hung_task.c
> index ccd7217fcec1..23dec32a05f3 100644
> --- a/kernel/hung_task.c
> +++ b/kernel/hung_task.c
> @@ -119,7 +119,7 @@ static void debug_show_blocker(struct task_struct *task)
> if ((unsigned long)t == owner) {
> pr_err("INFO: task %s:%d is blocked on a mutex likely owned by task %s:%d.\n",
> task->comm, task->pid, t->comm, t->pid);
> - sched_show_task(t);
> + sched_show_task_log_lvl(t, KERN_ERR);
> return;
> }
> }
> @@ -186,7 +186,7 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)
> pr_err(" Blocked by coredump.\n");
> pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
> " disables this message.\n");
> - sched_show_task(t);
> + sched_show_task_log_lvl(t, KERN_ERR);
> debug_show_blocker(t);
> hung_task_show_lock = true;
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 00ea6f31973c..b6a5a41fbbfc 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -7687,7 +7687,7 @@ void __sched io_schedule(void)
> }
> EXPORT_SYMBOL(io_schedule);
>
> -void sched_show_task(struct task_struct *p)
> +void sched_show_task_log_lvl(struct task_struct *p, const char *lvl)
> {
> unsigned long free;
> int ppid;
> @@ -7695,7 +7695,8 @@ void sched_show_task(struct task_struct *p)
> if (!try_get_task_stack(p))
> return;
>
> - pr_info("task:%-15.15s state:%c", p->comm, task_state_to_char(p));
> + printk("%stask:%-15.15s state:%c", lvl,
> + p->comm, task_state_to_char(p));
>
> if (task_is_running(p))
> pr_cont(" running task ");
> @@ -7709,12 +7710,18 @@ void sched_show_task(struct task_struct *p)
> free, task_pid_nr(p), task_tgid_nr(p),
> ppid, p->flags, read_task_thread_flags(p));
>
> - print_worker_info(KERN_INFO, p);
> - print_stop_info(KERN_INFO, p);
> - print_scx_info(KERN_INFO, p);
> - show_stack(p, NULL, KERN_INFO);
> + print_worker_info(lvl, p);
> + print_stop_info(lvl, p);
> + print_scx_info(lvl, p);
> + show_stack(p, NULL, lvl);
> put_task_stack(p);
> }
> +EXPORT_SYMBOL_GPL(sched_show_task_log_lvl);
> +
> +void sched_show_task(struct task_struct *p)
> +{
> + sched_show_task_log_lvl(p, KERN_INFO);
> +}
> EXPORT_SYMBOL_GPL(sched_show_task);
>
> static inline bool
> --
> 2.49.0.rc0.332.g42c0ae87b1-goog
>