From nobody Tue Feb 10 03:45:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1A29C7619A for ; Fri, 31 Mar 2023 01:43:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229588AbjCaBnE (ORCPT ); Thu, 30 Mar 2023 21:43:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbjCaBnC (ORCPT ); Thu, 30 Mar 2023 21:43:02 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C3A549EA for ; Thu, 30 Mar 2023 18:42:59 -0700 (PDT) Received: from dggpemm500013.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4PnjfS4HZrzKx3C; Fri, 31 Mar 2023 09:40:32 +0800 (CST) Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Fri, 31 Mar 2023 09:42:56 +0800 From: Chen Zhongjin To: CC: , , , , , , , , , , , , , , Subject: [PATCH v2] wchan: Fix get_wchan() when task in schedule Date: Fri, 31 Mar 2023 09:41:12 +0800 Message-ID: <20230331014112.193144-1-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" get_wchan() check task to unwind is not running or going to run by: state !=3D TASK_RUNNING && state !=3D 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 =3D=3D TASK_UNINTERRUPTIBLE schedule_timeout() __schedule() deactivate_task() // on_rq =3D 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 block= ed") Signed-off-by: Chen Zhongjin --- 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 =3D 0; unsigned int state; + int on_cpu =3D 0; =20 if (!p || p =3D=3D current) return 0; @@ -2041,7 +2042,12 @@ unsigned long get_wchan(struct task_struct *p) raw_spin_lock_irq(&p->pi_lock); state =3D READ_ONCE(p->__state); smp_rmb(); /* see try_to_wake_up() */ - if (state !=3D TASK_RUNNING && state !=3D TASK_WAKING && !p->on_rq) + +#ifdef CONFIG_SMP + on_cpu =3D p->on_cpu; +#endif + if (state !=3D TASK_RUNNING && state !=3D TASK_WAKING && + !p->on_rq && !on_cpu) ip =3D __get_wchan(p); raw_spin_unlock_irq(&p->pi_lock); =20 --=20 2.17.1