mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] wchan: Fix get_wchan() when task in schedule
@ 2023-03-30 12:12 Chen Zhongjin
  2023-03-30 13:53 ` kernel test robot
  2023-03-30 16:58 ` kernel test robot
  0 siblings, 2 replies; 4+ messages in thread
From: Chen Zhongjin @ 2023-03-30 12:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: chenzhongjin, mingo, peterz, juri.lelli, vincent.guittot,
	dietmar.eggemann, rostedt, bsegall, mgorman, bristot, vschneid,
	rmk+kernel, geert, keescook

get_wchan() check task to unwind is not running or going to run by:
state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq

However this cannot detect task which is going to be scheduled out.
For example, in this path:

  __wait_for_common(x, schedule_timeout, timeout, TASK_UNINTERRUPTIBLE)
  do_wait_for_common() // state == TASK_UNINTERRUPTIBLE
  schedule_timeout()
  __schedule()
    deactivate_task() // on_rq = 0

After this point get_wchan() can be run on the task but it is still
running actually, and p->pi_lock doesn't work for this case.

It can trigger some warning when running stacktrace on a running task.
Also check p->on_cpu to promise task is really switched out can prevent
this.

Fixes: 42a20f86dc19 ("sched: Add wrapper for get_wchan() to keep task blocked")
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
 kernel/sched/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 0d18c3969f90..2071d1c0eaca 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2041,7 +2041,8 @@ unsigned long get_wchan(struct task_struct *p)
 	raw_spin_lock_irq(&p->pi_lock);
 	state = READ_ONCE(p->__state);
 	smp_rmb(); /* see try_to_wake_up() */
-	if (state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq)
+	if (state != TASK_RUNNING && state != TASK_WAKING &&
+	    !p->on_rq && !p->on_cpu)
 		ip = __get_wchan(p);
 	raw_spin_unlock_irq(&p->pi_lock);
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-31  1:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30 12:12 [PATCH] wchan: Fix get_wchan() when task in schedule Chen Zhongjin
2023-03-30 13:53 ` kernel test robot
2023-03-31  1:08   ` Chen Zhongjin
2023-03-30 16:58 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®