Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3081804rwb; Mon, 15 Aug 2022 17:40:43 -0700 (PDT) X-Google-Smtp-Source: AA6agR4P3cSlq4p7jKmmM+a52cRu76xGDTla8+uSiUFBDc9j/e0M+BdJfk45W1nusWJDmg7Zrto4 X-Received: by 2002:a05:6402:270e:b0:43d:e3e1:847a with SMTP id y14-20020a056402270e00b0043de3e1847amr16706364edd.130.1660610443318; Mon, 15 Aug 2022 17:40:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660610443; cv=none; d=google.com; s=arc-20160816; b=FTI8iC6WteLZhueoZ/3NssmQq2AlZB2+fQixXGdDzypyXE966kprUqm2f4jhd2aNz+ 1rybcLRuGjrrCsZsJ//mAUMNlk1usCw0Y3AGjGbnjS2ZjfgGyv5lQQY8VO9x3TXKYL24 zfWmr0XrO0g9WNiquYA4NRP2eUd3tqvuEyE5iumtwW5Bg8T9/Z1SKLCJJVmGBp8MO/B2 nZqeqcgDQHRtUUdOlXQcnnncFnG6bbLgan107Chj0gckdfWC98hmKNLlDx0SjTdMfbC0 Dhnd/0Msvk3USizKLntwq0MbqKQGf8vtWXGNb30DT4rowx1NGICKfUnzK84HvhYAjhiU 2t7A== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nhoC+5QAF9xoSOL9nNFhKJ21JLtg2PGIgnjMzuJhTUo=; b=tUjo79YDC5vLx0UYtMVdW3O/iCoH3ZjMoYMaYtMShuaTa5LT02B/ZWOZQhnP6I3Z7B U9o1RtSDHrqQO6SDw6WX8gQryoDP0QvqfgbSWLmnTtZqEdX5KLMEeGrlWckkEAVPaOD8 n2Le6dbFdBvMveV7NHtjpEO5Pk6uu5DCoEWUJiHOtWjlB+iDzXLng+1i636Jdt6HC7+O 9KNNCHkTigjbLtL13DJNm2ACMOJk17h+eXX+vJBZMXDFeHhk9CwDtytPrhV7FX8Ypt7v UAZruqPJs+jOK/7S3hAQEsEOjcL3vI04N3jCnW4+m3hQE1BhaJGVdTizIK5UqB0xZ15v YsQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y9Fd99E4; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa42-20020a17090786aa00b00734be0ce0e5si9275744ejc.311.2022.08.15.17.40.17; Mon, 15 Aug 2022 17:40:43 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y9Fd99E4; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347984AbiHOXCi (ORCPT + 99 others); Mon, 15 Aug 2022 19:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352794AbiHOXBI (ORCPT ); Mon, 15 Aug 2022 19:01:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 463415D120; Mon, 15 Aug 2022 12:57:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 06ACB6113B; Mon, 15 Aug 2022 19:57:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17BEEC433C1; Mon, 15 Aug 2022 19:57:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660593465; bh=5T4neKr3Jd7AuPZGVA14c6D8Q45msYSSscOIE8m2h88=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y9Fd99E4ToYyTfqcuSOan+9UggqRPLwoYUctFPKj9AYfqIertvlhwqB3irmnUu+V6 9c84OhqhGkikivImG5FYn4eBJGBdQ22IxGms7BVB2h+oqGhSS9xSG3guKylnLd4LSN hKWlEcjK9TeMcSYbT8xan9vN0umSJw4iRWGLYSS4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Valentin Schneider , Tianchen Ding , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 5.18 0950/1095] sched: Fix the check of nr_running at queue wakelist Date: Mon, 15 Aug 2022 20:05:49 +0200 Message-Id: <20220815180508.412896351@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Tianchen Ding [ Upstream commit 28156108fecb1f808b21d216e8ea8f0d205a530c ] 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 Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Valentin Schneider Link: https://lore.kernel.org/r/20220608233412.327341-2-dtcccc@linux.alibaba.com Signed-off-by: Sasha Levin --- 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 b0f3ded147cb..4e99f0cf727a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3832,8 +3832,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.35.1