From nobody Wed Apr 29 00:33:15 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 E49E1C433EF for ; Thu, 26 May 2022 07:14:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346336AbiEZHO1 (ORCPT ); Thu, 26 May 2022 03:14:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235634AbiEZHOV (ORCPT ); Thu, 26 May 2022 03:14:21 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0ECFDD11E for ; Thu, 26 May 2022 00:14:20 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id pq9-20020a17090b3d8900b001df622bf81dso999461pjb.3 for ; Thu, 26 May 2022 00:14:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PxeZkmwYfV96KCMYuxRmijxISPSq2e0oV0kOeMtlrUM=; b=RSm+3ev0c3wi9ngyYDMgJli7UkvMbmE6FFkhQSsQUyFTvykUs+C5q6Tac/DHMEf2KZ kgEX4VjJwLNYKAIuyHnNg/MPpiSLE7aSFS3VVw1SGwhidK9xXWUXukCM7URlV2LzxEe4 0sm/F6WGfPht++jaqf0W6bbRjIW1Mze7+56AGJOt/FiFNtYNvOEYgOFxLfAZNj2Tu7on D2eLBRnk2d6zgr86gSDVncka3t5BF/+rdmC/guKUX6nneWOhqH1kjRSdl8UmPABEBqEF oUopp47EqnACLWmhFumAVfrY+77Gd63dWjcci7jxCiGaRwe5HBQ1BX0aMJGhqhUqOziY Sx2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PxeZkmwYfV96KCMYuxRmijxISPSq2e0oV0kOeMtlrUM=; b=LRfHY2a7lz7H2iJ7dFc76B5Z/gTvcxceGktHOsrJ4G6V3FF3NxxmUdLlrVRe/qyqa9 Vb7yPbLzmn3J5tNztMmdWXRKkvEuLDgMVDIAUPtFXnkmJsvSEpWgn8QAipyHP89Yx+sL ZjDBVAMvr+MW354m9nSn0Ooet2DUF4UirmaQTyjhW3mYaZ+5d10GySEK04i2rYFVrUm3 I2vky7KtSCie7VHnhvzgDfaNN3xnB41nniMbAZ+wbWLftY5x/GmcETs3V7DO8NaXoZcE +ihYIE+zsd4TdqyHXskljzNPuw3AIX9hn4+06TBB75Eb3z1GuE30ZNKaHaTxwhafYNBG tBAQ== X-Gm-Message-State: AOAM5307HJ0Y+O45q4Tn7YOdimieM7SSpyY+lhqhLXbqcLqypbYMXQgT D+IcbFlzDpDVcgmIkzq14UFPww== X-Google-Smtp-Source: ABdhPJzN3uazxjTkw1IzQGoEhkLS0CkfbimZC7+yY8XiBel/Cgw+lhFkiNk+YouHfW+2SYFyHhi29g== X-Received: by 2002:a17:90b:3a85:b0:1e0:548:2bf7 with SMTP id om5-20020a17090b3a8500b001e005482bf7mr1152303pjb.55.1653549259596; Thu, 26 May 2022 00:14:19 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id e24-20020aa78c58000000b005184031963bsm640629pfd.85.2022.05.26.00.14.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 May 2022 00:14:19 -0700 (PDT) From: Chengming Zhou To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, odin@uged.al Cc: linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v2 1/2] sched/fair: fix propagate during synchronous attach/detach Date: Thu, 26 May 2022 15:13:53 +0800 Message-Id: <20220526071354.6426-2-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220526071354.6426-1-zhouchengming@bytedance.com> References: <20220526071354.6426-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When a task moves from/to a cfs_rq, we first detach/attach the load_avg of se from/to that cfs_rq, then propagate the changes across the tg tree to make it visible to the root, which did in update_load_avg(). But the current code will break when encountering a on_list cfs_rq, can't propagate up to the root cfs_rq, that also mismatch with the comment of propagate_entity_cfs_rq(), which says "Propagate the changes of the sched_entity across the tg tree to make it visible to the root". The second problem is that it won't update_load_avg() for throttled cfs_rq, cause the load changes can't be propagated upwards. A | B --> throttled cfs_rq / C The prop_runnable_sum of C won't be propagated to B, so won't be propagated to A. Fixes: 0258bdfaff5b ("sched/fair: Fix unfairness caused by missing load dec= ay") Signed-off-by: Chengming Zhou --- kernel/sched/fair.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 523e548c8fdd..5276d05692e0 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -11303,14 +11303,8 @@ static void propagate_entity_cfs_rq(struct sched_e= ntity *se) for_each_sched_entity(se) { cfs_rq =3D cfs_rq_of(se); =20 - if (!cfs_rq_throttled(cfs_rq)){ - update_load_avg(cfs_rq, se, UPDATE_TG); - list_add_leaf_cfs_rq(cfs_rq); - continue; - } - - if (list_add_leaf_cfs_rq(cfs_rq)) - break; + update_load_avg(cfs_rq, se, UPDATE_TG); + list_add_leaf_cfs_rq(cfs_rq); } } #else --=20 2.36.1 From nobody Wed Apr 29 00:33:15 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 EF740C433EF for ; Thu, 26 May 2022 07:14:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346369AbiEZHOa (ORCPT ); Thu, 26 May 2022 03:14:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346341AbiEZHO1 (ORCPT ); Thu, 26 May 2022 03:14:27 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB68A3969C for ; Thu, 26 May 2022 00:14:25 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id cs3-20020a17090af50300b001e0808b5838so1022587pjb.1 for ; Thu, 26 May 2022 00:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fnvSBK00HYkhNihK4hUnHuTK0u4mSGiraqCdnpeu1iI=; b=LEs7yybWWw4H+KEp7l+qyYBrZDtmgqpUaifQnnQsILUShrLGXoOIG+ies4ZUpBNr0X OUSpyhHCWMW2wL1Bs6SlZc5oxJlCKc1Ub/q6cVP6m2kz/01Tz1n9ZBi0ipXXSgtcZ3C6 fNKl1PJyGc9TaSarr48WiKXbI7fGMY0HHXBbEJkbwaI9QGeeac7xw9lIS5oBy04aJYfL ox8dk6Vm0JQNkKvtIH2pzPcLwUJvP0E4KoLMxwLsi2R/IFxL704JcC6Meu8FfH3Nxa+b 3/pkAqF5m2fh/xnIrAG+rnPjKj9si5Jd3CDGnyOMoM7/xX01+ObEVpi3TXvOd9sqAjJL GKvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fnvSBK00HYkhNihK4hUnHuTK0u4mSGiraqCdnpeu1iI=; b=nI/UcxZA7eeevmO2g5e10yibjfLH6/746moS7tRC/MzJ3ypi4cF5gOZVYUCGv35fGV 2fl5Cy7uagvz74PExEv1Duw0OXj2R7+94yyciG9mMJEwAl4bf1vDrRe+BUH7n0KawlLJ W5zkBQ0DgApUmM754vz9nydwga32Vrn4EMGiELRefnV0AL726nBF7wjy0SeYFw0pCek2 P2uepv2OxZudYRqT5i2z4OYX82E+pCSIEoQ9IrOcqfRqy3sXTCs3R4Svs8gF2P08jL5r v/8CoY/61ze3W1wGBTkrFE7YmODOxBkxRj+7mazoGuWGHYXjY1VcO1s1hlslbL6JUrQy UbmQ== X-Gm-Message-State: AOAM530V2kunlMLX0DgHQgWyAXYhQAoUieCcdZ+m0X/VgwoOOyOCkFr0 vxM7HzwiVJslJkb/GEe3xZacoA== X-Google-Smtp-Source: ABdhPJyjokAorAGLaFhyF0hLz72hfUPeBbqTarn063k4Q5VxmiWa08W3+mMgeGocLQh0nOlaeWI6Iw== X-Received: by 2002:a17:90b:23c4:b0:1e0:9e72:e226 with SMTP id md4-20020a17090b23c400b001e09e72e226mr1182976pjb.136.1653549264915; Thu, 26 May 2022 00:14:24 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id e24-20020aa78c58000000b005184031963bsm640629pfd.85.2022.05.26.00.14.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 May 2022 00:14:24 -0700 (PDT) From: Chengming Zhou To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, odin@uged.al Cc: linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v2 2/2] sched/fair: optimize and simplify rq leaf_cfs_rq_list Date: Thu, 26 May 2022 15:13:54 +0800 Message-Id: <20220526071354.6426-3-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220526071354.6426-1-zhouchengming@bytedance.com> References: <20220526071354.6426-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We notice the rq leaf_cfs_rq_list has two problems when do bugfix backports and some test profiling. 1. cfs_rqs under throttled subtree could be added to the list, and make their fully decayed ancestors on the list, even though not needed. 2. #1 also make the leaf_cfs_rq_list management complex and error prone, this is the list of related bugfix so far: commit 31bc6aeaab1d ("sched/fair: Optimize update_blocked_averages()") commit fe61468b2cbc ("sched/fair: Fix enqueue_task_fair warning") commit b34cb07dde7c ("sched/fair: Fix enqueue_task_fair() warning some m= ore") commit 39f23ce07b93 ("sched/fair: Fix unthrottle_cfs_rq() for leaf_cfs_r= q list") commit 0258bdfaff5b ("sched/fair: Fix unfairness caused by missing load = decay") commit a7b359fc6a37 ("sched/fair: Correctly insert cfs_rq's to list on u= nthrottle") commit fdaba61ef8a2 ("sched/fair: Ensure that the CFS parent is added af= ter unthrottling") commit 2630cde26711 ("sched/fair: Add ancestors of unthrottled undecayed= cfs_rq") commit 31bc6aeaab1d ("sched/fair: Optimize update_blocked_averages()") delete every cfs_rq under throttled subtree from rq->leaf_cfs_rq_list, and delete the throttled_hierarchy() test in update_blocked_averages(), which optimized update_blocked_averages(). But those later bugfix add cfs_rqs under throttled subtree back to rq->leaf_cfs_rq_list again, with their fully decayed ancestors, for the integrity of rq->leaf_cfs_rq_list. This patch takes another method, skip all cfs_rqs under throttled hierarchy when list_add_leaf_cfs_rq(), to completely make cfs_rqs under throttled subtree off the leaf_cfs_rq_list. So we don't need to consider throttled related things in enqueue_entity(), unthrottle_cfs_rq() and enqueue_task_fair(), which simplify the code a lot. Also optimize update_blocked_averages() since cfs_rqs under throttled hierarchy and their ancestors won't be on the leaf_cfs_rq_list. Signed-off-by: Chengming Zhou Reported-by: kernel test robot --- v2: - move throttled_hierarchy() outside list_add_leaf_cfs_rq(), suggested by Vincent. --- kernel/sched/fair.c | 77 ++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 57 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 5276d05692e0..7438c1865109 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4374,16 +4374,11 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_= entity *se, int flags) __enqueue_entity(cfs_rq, se); se->on_rq =3D 1; =20 - /* - * When bandwidth control is enabled, cfs might have been removed - * because of a parent been throttled but cfs->nr_running > 1. Try to - * add it unconditionally. - */ - if (cfs_rq->nr_running =3D=3D 1 || cfs_bandwidth_used()) - list_add_leaf_cfs_rq(cfs_rq); - - if (cfs_rq->nr_running =3D=3D 1) + if (cfs_rq->nr_running =3D=3D 1) { check_enqueue_throttle(cfs_rq); + if (!throttled_hierarchy(cfs_rq)) + list_add_leaf_cfs_rq(cfs_rq); + } } =20 static void __clear_buddies_last(struct sched_entity *se) @@ -4998,11 +4993,18 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) /* update hierarchical throttle state */ walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); =20 - /* Nothing to run but something to decay (on_list)? Complete the branch */ if (!cfs_rq->load.weight) { - if (cfs_rq->on_list) - goto unthrottle_throttle; - return; + if (!cfs_rq->on_list) + return; + /* + * Nothing to run but something to decay (on_list)? + * Complete the branch. + */ + for_each_sched_entity(se) { + if (list_add_leaf_cfs_rq(cfs_rq_of(se))) + break; + } + goto unthrottle_throttle; } =20 task_delta =3D cfs_rq->h_nr_running; @@ -5040,31 +5042,12 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) /* end evaluation on encountering a throttled cfs_rq */ if (cfs_rq_throttled(qcfs_rq)) goto unthrottle_throttle; - - /* - * One parent has been throttled and cfs_rq removed from the - * list. Add it back to not break the leaf list. - */ - if (throttled_hierarchy(qcfs_rq)) - list_add_leaf_cfs_rq(qcfs_rq); } =20 /* At this point se is NULL and we are at root level*/ add_nr_running(rq, task_delta); =20 unthrottle_throttle: - /* - * The cfs_rq_throttled() breaks in the above iteration can result in - * incomplete leaf list maintenance, resulting in triggering the - * assertion below. - */ - for_each_sched_entity(se) { - struct cfs_rq *qcfs_rq =3D cfs_rq_of(se); - - if (list_add_leaf_cfs_rq(qcfs_rq)) - break; - } - assert_list_leaf_cfs_rq(rq); =20 /* Determine whether we need to wake up potentially idle CPU: */ @@ -5719,13 +5702,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct = *p, int flags) /* end evaluation on encountering a throttled cfs_rq */ if (cfs_rq_throttled(cfs_rq)) goto enqueue_throttle; - - /* - * One parent has been throttled and cfs_rq removed from the - * list. Add it back to not break the leaf list. - */ - if (throttled_hierarchy(cfs_rq)) - list_add_leaf_cfs_rq(cfs_rq); } =20 /* At this point se is NULL and we are at root level*/ @@ -5749,21 +5725,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct = *p, int flags) update_overutilized_status(rq); =20 enqueue_throttle: - if (cfs_bandwidth_used()) { - /* - * When bandwidth control is enabled; the cfs_rq_throttled() - * breaks in the above iteration can result in incomplete - * leaf list maintenance, resulting in triggering the assertion - * below. - */ - for_each_sched_entity(se) { - cfs_rq =3D cfs_rq_of(se); - - if (list_add_leaf_cfs_rq(cfs_rq)) - break; - } - } - assert_list_leaf_cfs_rq(rq); =20 hrtick_update(rq); @@ -11293,9 +11254,10 @@ static inline bool vruntime_normalized(struct task= _struct *p) */ static void propagate_entity_cfs_rq(struct sched_entity *se) { - struct cfs_rq *cfs_rq; + struct cfs_rq *cfs_rq =3D cfs_rq_of(se); =20 - list_add_leaf_cfs_rq(cfs_rq_of(se)); + if (!throttled_hierarchy(cfs_rq)) + list_add_leaf_cfs_rq(cfs_rq); =20 /* Start to propagate at parent */ se =3D se->parent; @@ -11304,7 +11266,8 @@ static void propagate_entity_cfs_rq(struct sched_en= tity *se) cfs_rq =3D cfs_rq_of(se); =20 update_load_avg(cfs_rq, se, UPDATE_TG); - list_add_leaf_cfs_rq(cfs_rq); + if (!throttled_hierarchy(cfs_rq)) + list_add_leaf_cfs_rq(cfs_rq); } } #else --=20 2.36.1