From patchwork Fri Mar 31 01:41:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 77506 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp256463vqo; Thu, 30 Mar 2023 18:45:50 -0700 (PDT) X-Google-Smtp-Source: AKy350ZLdK1cNbfsWEYNgFh9Ap5aT53eEyhBXR57gp6zEQkfg9WJpZsI7iwY4Ro/q1G+EUh+yVor X-Received: by 2002:a17:906:ef90:b0:8b2:7567:9c30 with SMTP id ze16-20020a170906ef9000b008b275679c30mr30678257ejb.59.1680227149912; Thu, 30 Mar 2023 18:45:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680227149; cv=none; d=google.com; s=arc-20160816; b=NXjpIZsUs8TJ7ADW9E3yxYZWdDRpkO7gNvpvxviaEAOTbjTniQULrFqY4JE1DWK/ar k9WmxoFedlYkVd57cwz9coHcxrp2xM4HOAjc/xh7o+0EShpQK6aGRtl4S2Ut0+9LexUu 4WJ60GmfXYBJ6HJqY0rXSSi84LxVT1bawMmtC5SXvtzksmw/fY5eAlQt5vPQcgmdrlco 1SIX9mc6TbiLb9wYsbtWUcomkzLWRt/g/wUH6wHvNdQF6ySrrxsCwyxPu26GTfO6WEa/ AJ7JU8GZc/BXkRsQyJ+foMTX/gmIhFK1ePQXD3Oeb+Tzj/uc98zIwCcvptE+wBOYpOUT AZ5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=AKa8vmsaagCmBFQTKkT1jhQZuTcYG4Q8G/eT8+1Ea/0=; b=Xqh+FxU/AMR5cztkdD+ui5kHt/94s1oXIn8mPKUw8xKb5ADecIiMRu6lYtUG+BI00f TS24SXhdjp76R4bqZVH7MQmMHhNXLNqC/mUsRPOrORB1VUn7Y3NYnFL7+XaIWRbVe5Cd iE6UztQSoygCTLD1IDVvvHOu5tQ13O3jbIjzWZTtKIMZ4EKbRbmiS1UfS2oL+h7Gc6co WwEZbOlAdJkuGwjvp6rh3DC/jrji4KeqpRCol3qrfkFY9KbaaKWDrNlYCuHHFbjs7pS+ b0I68RNKIzAlrNkCmfhSxLzsM72dqZG2m4noyt9mneKRj3vqGAhJK+9HA4Uwz/xqYd1t qpXw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bh2-20020a170906a0c200b009372a2a6e7fsi890209ejb.563.2023.03.30.18.45.26; Thu, 30 Mar 2023 18:45:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229458AbjCaBnE (ORCPT + 99 others); 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 X-Spam-Status: No, score=-2.3 required=5.0 tests=RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761795528479281274?= X-GMAIL-MSGID: =?utf-8?q?1761845863861437032?= 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 --- 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);