Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp138679iof; Wed, 8 Jun 2022 17:29:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUaDmjwvSaegWjqFb7iLSzEGyTXWzQvz4vZFSdkQUwPwygMU9uE5eVLiN0kMGD7tcJU+oV X-Received: by 2002:a17:903:18a:b0:166:ba97:8b19 with SMTP id z10-20020a170903018a00b00166ba978b19mr31260776plg.62.1654734566826; Wed, 08 Jun 2022 17:29:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654734566; cv=none; d=google.com; s=arc-20160816; b=avATINzGzlIYDC5RO6kokD7ApXxZJtDF28ZfWitGB65SsHqfK8p9yY++89B5aVTEMz hfllmHQQTau5D2ggz4v+617E7/YPImRW8rUJsV/raWZ1vK/iEqIBm0e+Gyvjbnnf19lb QI7KzAX5vSiJ1BOBhjbbfANclE4VXWmqq03NelEpmKl4/L7XCa4c4BM17MQOAYTaM6B6 ++yGoAgyqwUFM955mAmEeH4AZoOdjD561Hhe0k5Q7A5jqHDCCCAXF7/4NFu0EVO+4yjQ +pC/e2j1GlQMLtlhz4+7aFP43rDOCGs4CJqvktUfeMO/z9ac+pRTA01+9EhS9NqFqH5v vVOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=gOYOMwPXrDrjpqqynMvZvLu9MJ5QJ8CRfUfv/Sa7ZsQ=; b=UypD7w8v5XKXefKNj4IS2B88OkdanA2cDz6TsSv7rON72X7HcsyxaYuJwqBylX0/1U BFuUCMWh4/qEgPZEL4aiofh5tnp7ZgpbGzfU2UPgYCIsSHPnkQDXi0vSBSfcCLwpnA9/ SG7P7f7+Z7xNmFPrZcDyBnuzAAFTp1Xr8NRVRsTt7qgVmFa8UqMd3OtUpm+qamF4Krwz f3xaNF7LW8vC3HW9uyj6xneKUpgw10zQa9Dllz+tbHkhx7Js7O+iaGYyMOC7vyVMDsDb fT3QaRM4QtZPnW4EP8Tth4HNAGCyI3dDxfUgmof7ae7Z2nlz8GF0qpq1pv09RUcNYaLe aouA== 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=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j70-20020a638b49000000b003fe2ae26681si5003266pge.109.2022.06.08.17.29.02; Wed, 08 Jun 2022 17:29:26 -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=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233273AbiFHXei (ORCPT + 99 others); Wed, 8 Jun 2022 19:34:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232971AbiFHXe2 (ORCPT ); Wed, 8 Jun 2022 19:34:28 -0400 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0C7312295F for ; Wed, 8 Jun 2022 16:34:23 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=dtcccc@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0VFoWqnQ_1654731259; Received: from localhost.localdomain(mailfrom:dtcccc@linux.alibaba.com fp:SMTPD_---0VFoWqnQ_1654731259) by smtp.aliyun-inc.com; Thu, 09 Jun 2022 07:34:20 +0800 From: Tianchen Ding To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider Cc: linux-kernel@vger.kernel.org Subject: [PATCH RESEND v4 1/2] sched: Fix the check of nr_running at queue wakelist Date: Thu, 9 Jun 2022 07:34:11 +0800 Message-Id: <20220608233412.327341-2-dtcccc@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220608233412.327341-1-dtcccc@linux.alibaba.com> References: <20220608233412.327341-1-dtcccc@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,HK_RANDOM_ENVFROM,HK_RANDOM_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=no 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 The commit 2ebb17717550 ("sched/core: Offload wakee task activation if it the wakee is descheduling") checked rq->nr_running <= 1 to avoid task stacking when WF_ON_CPU. Per the ordering of writes to p->on_rq and p->on_cpu, observing p->on_cpu (WF_ON_CPU) in ttwu_queue_cond() implies !p->on_rq, IOW p has gone through the deactivate_task() in __schedule(), thus p has been accounted out of rq->nr_running. As such, the task being the only runnable task on the rq implies reading rq->nr_running == 0 at that point. The benchmark result is in [1]. [1] https://lore.kernel.org/all/e34de686-4e85-bde1-9f3c-9bbc86b38627@linux.alibaba.com/ Suggested-by: Valentin Schneider Signed-off-by: Tianchen Ding Reviewed-by: Valentin Schneider --- kernel/sched/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 21db6816a7bd..a4bdb2b95976 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3829,8 +3829,12 @@ static inline bool ttwu_queue_cond(int cpu, int wake_flags) * CPU then use the wakelist to offload the task activation to * the soon-to-be-idle CPU as the current CPU is likely busy. * nr_running is checked to avoid unnecessary task stacking. + * + * Note that we can only get here with (wakee) p->on_rq=0, + * p->on_cpu can be whatever, we've done the dequeue, so + * the wakee has been accounted out of ->nr_running. */ - if ((wake_flags & WF_ON_CPU) && cpu_rq(cpu)->nr_running <= 1) + if ((wake_flags & WF_ON_CPU) && !cpu_rq(cpu)->nr_running) return true; return false; -- 2.27.0