[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20230331014112.193144-1-chenzhongjin@huawei.com>
Date: Fri, 31 Mar 2023 09:41:12 +0800
From: Chen Zhongjin <chenzhongjin@...wei.com>
To: <linux-kernel@...r.kernel.org>
CC: <chenzhongjin@...wei.com>, <mingo@...hat.com>,
<peterz@...radead.org>, <jpoimboe@...nel.org>,
<juri.lelli@...hat.com>, <vincent.guittot@...aro.org>,
<dietmar.eggemann@....com>, <rostedt@...dmis.org>,
<bsegall@...gle.com>, <mgorman@...e.de>, <bristot@...hat.com>,
<vschneid@...hat.com>, <rmk+kernel@...linux.org.uk>,
<keescook@...omium.org>, <geert@...ux-m68k.org>
Subject: [PATCH v2] wchan: Fix get_wchan() when task in schedule
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 to prevent
this.
Fixes: 42a20f86dc19 ("sched: Add wrapper for get_wchan() to keep task blocked")
Signed-off-by: Chen Zhongjin <chenzhongjin@...wei.com>
---
v1 -> v2:
Fix task_struct->on_cpu not exist for !CONFIG_SMP
---
kernel/sched/core.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 0d18c3969f90..94799db69487 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2033,6 +2033,7 @@ unsigned long get_wchan(struct task_struct *p)
{
unsigned long ip = 0;
unsigned int state;
+ int on_cpu = 0;
if (!p || p == current)
return 0;
@@ -2041,7 +2042,12 @@ 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)
+
+#ifdef CONFIG_SMP
+ on_cpu = p->on_cpu;
+#endif
+ if (state != TASK_RUNNING && state != TASK_WAKING &&
+ !p->on_rq && !on_cpu)
ip = __get_wchan(p);
raw_spin_unlock_irq(&p->pi_lock);
--
2.17.1
Powered by blists - more mailing lists