Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2826746pxv; Mon, 12 Jul 2021 02:55:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwX45uS5Ke9XehMFRLshwrpkW4rBrHr8PrWJP/Q/Mp7p0b+t1WjiL865nSLkwPouqYAImyW X-Received: by 2002:a02:c496:: with SMTP id t22mr12184977jam.80.1626083715676; Mon, 12 Jul 2021 02:55:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626083715; cv=none; d=google.com; s=arc-20160816; b=AlBvGeex45Nbycxht6/DTYoCvq0iaEczzDCutIcq2oZ5RAIY8LmHjdLW2y9OgGJ4Fl 0L+Aqe3SQDZyuKCYbnRtOcHgj73tCQDYnnWXww/P8jzDrvNKgUumUvSIe65J7oAsbC9I /V/EvgD3QpkFwNPWdaAHs8Nr1ICOvvA2OIs0ebqqRarOHj8HYDhiInIGkZLBMIVyONuy UeRsEyBNf5XkM4HUqurSJCH76ID1JmNjsIeFdWYOada3Qn2G77aV/a3e5Ifp6QF35wX3 7Qy5CRW/Abj6FkP7Rr8PP/fNW379Q37brJVHlNGMJhg33XRRCjwNEa+Eu619P3in1Y4Y mxcw== 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=jjnfyXZuEPMvmS7zrF4T2YHNS+RaSRgALHBr8LUFNJ4=; b=p50BOUQmubilwa6n5TC8KEIdX9ntPKubdEfQlZPlmYWWD33ui23ntR6gp9edYigqYJ Sy3lEGojDYUouNLWsRDWIbUw4ZKsezoG1903RBJSjmJKJFq/UnEefDX470AhVSHitxf1 H4lK3We95DfHZzOoGyo/nkQaKxQvT5sKPDfMCjmYzgVjwXjwmjF7SF6hvmB45HX2Qgg4 AfmFFAFp3EQSX8Ppqk3+TW/N8GG/gaN5SjyvwfZ5ZO97cSRNLTfkD0dImmvX105P5xbY zlXk6reSBAIrW384EshQYjZu+S6wNXIiyIc/KFQnJ+foBbFcwJzgjuFDnMDPwq4hXvtb 1NNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=R7HxVqwn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n2si18599276jaj.36.2021.07.12.02.55.04; Mon, 12 Jul 2021 02:55:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=R7HxVqwn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S238994AbhGLG5D (ORCPT + 99 others); Mon, 12 Jul 2021 02:57:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:36654 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238558AbhGLGlD (ORCPT ); Mon, 12 Jul 2021 02:41:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B001261108; Mon, 12 Jul 2021 06:38:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626071895; bh=ucOfSracrK7coYzzzsjcUnvqKPtxGBD+Myd+ddIt32E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R7HxVqwndQhmpFr51bKm+ioFWlQ146C3db6Klxetlb7cgnnkKLNWlYRe6Wv27EPl8 3eZDtRzZUXk74lUpidc41W4tNvunpAvL0eRGmv8Vxbexh7tlJOdd1+3rx4n8MwqdqN nfw/TiqLwh+W+ABYGz32UmPnRbjAAtojmwyQGeeQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xuewen Yan , Qais Yousef , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 5.10 266/593] sched/uclamp: Fix uclamp_tg_restrict() Date: Mon, 12 Jul 2021 08:07:06 +0200 Message-Id: <20210712060912.688049530@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060843.180606720@linuxfoundation.org> References: <20210712060843.180606720@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qais Yousef [ Upstream commit 0213b7083e81f4acd69db32cb72eb4e5f220329a ] Now cpu.uclamp.min acts as a protection, we need to make sure that the uclamp request of the task is within the allowed range of the cgroup, that is it is clamp()'ed correctly by tg->uclamp[UCLAMP_MIN] and tg->uclamp[UCLAMP_MAX]. As reported by Xuewen [1] we can have some corner cases where there's inversion between uclamp requested by task (p) and the uclamp values of the taskgroup it's attached to (tg). Following table demonstrates 2 corner cases: | p | tg | effective -----------+-----+------+----------- CASE 1 -----------+-----+------+----------- uclamp_min | 60% | 0% | 60% -----------+-----+------+----------- uclamp_max | 80% | 50% | 50% -----------+-----+------+----------- CASE 2 -----------+-----+------+----------- uclamp_min | 0% | 30% | 30% -----------+-----+------+----------- uclamp_max | 20% | 50% | 20% -----------+-----+------+----------- With this fix we get: | p | tg | effective -----------+-----+------+----------- CASE 1 -----------+-----+------+----------- uclamp_min | 60% | 0% | 50% -----------+-----+------+----------- uclamp_max | 80% | 50% | 50% -----------+-----+------+----------- CASE 2 -----------+-----+------+----------- uclamp_min | 0% | 30% | 30% -----------+-----+------+----------- uclamp_max | 20% | 50% | 30% -----------+-----+------+----------- Additionally uclamp_update_active_tasks() must now unconditionally update both UCLAMP_MIN/MAX because changing the tg's UCLAMP_MAX for instance could have an impact on the effective UCLAMP_MIN of the tasks. | p | tg | effective -----------+-----+------+----------- old -----------+-----+------+----------- uclamp_min | 60% | 0% | 50% -----------+-----+------+----------- uclamp_max | 80% | 50% | 50% -----------+-----+------+----------- *new* -----------+-----+------+----------- uclamp_min | 60% | 0% | *60%* -----------+-----+------+----------- uclamp_max | 80% |*70%* | *70%* -----------+-----+------+----------- [1] https://lore.kernel.org/lkml/CAB8ipk_a6VFNjiEnHRHkUMBKbA+qzPQvhtNjJ_YNzQhqV_o8Zw@mail.gmail.com/ Fixes: 0c18f2ecfcc2 ("sched/uclamp: Fix wrong implementation of cpu.uclamp.min") Reported-by: Xuewen Yan Signed-off-by: Qais Yousef Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20210617165155.3774110-1-qais.yousef@arm.com Signed-off-by: Sasha Levin --- kernel/sched/core.c | 49 +++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index d4bbead59ad2..679562d2f55d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1063,8 +1063,10 @@ static void uclamp_sync_util_min_rt_default(void) static inline struct uclamp_se uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) { + /* Copy by value as we could modify it */ struct uclamp_se uc_req = p->uclamp_req[clamp_id]; #ifdef CONFIG_UCLAMP_TASK_GROUP + unsigned int tg_min, tg_max, value; /* * Tasks in autogroups or root task group will be @@ -1075,23 +1077,11 @@ uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) if (task_group(p) == &root_task_group) return uc_req; - switch (clamp_id) { - case UCLAMP_MIN: { - struct uclamp_se uc_min = task_group(p)->uclamp[clamp_id]; - if (uc_req.value < uc_min.value) - return uc_min; - break; - } - case UCLAMP_MAX: { - struct uclamp_se uc_max = task_group(p)->uclamp[clamp_id]; - if (uc_req.value > uc_max.value) - return uc_max; - break; - } - default: - WARN_ON_ONCE(1); - break; - } + tg_min = task_group(p)->uclamp[UCLAMP_MIN].value; + tg_max = task_group(p)->uclamp[UCLAMP_MAX].value; + value = uc_req.value; + value = clamp(value, tg_min, tg_max); + uclamp_se_set(&uc_req, value, false); #endif return uc_req; @@ -1290,8 +1280,9 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) } static inline void -uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) +uclamp_update_active(struct task_struct *p) { + enum uclamp_id clamp_id; struct rq_flags rf; struct rq *rq; @@ -1311,9 +1302,11 @@ uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) * affecting a valid clamp bucket, the next time it's enqueued, * it will already see the updated clamp bucket value. */ - if (p->uclamp[clamp_id].active) { - uclamp_rq_dec_id(rq, p, clamp_id); - uclamp_rq_inc_id(rq, p, clamp_id); + for_each_clamp_id(clamp_id) { + if (p->uclamp[clamp_id].active) { + uclamp_rq_dec_id(rq, p, clamp_id); + uclamp_rq_inc_id(rq, p, clamp_id); + } } task_rq_unlock(rq, p, &rf); @@ -1321,20 +1314,14 @@ uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) #ifdef CONFIG_UCLAMP_TASK_GROUP static inline void -uclamp_update_active_tasks(struct cgroup_subsys_state *css, - unsigned int clamps) +uclamp_update_active_tasks(struct cgroup_subsys_state *css) { - enum uclamp_id clamp_id; struct css_task_iter it; struct task_struct *p; css_task_iter_start(css, 0, &it); - while ((p = css_task_iter_next(&it))) { - for_each_clamp_id(clamp_id) { - if ((0x1 << clamp_id) & clamps) - uclamp_update_active(p, clamp_id); - } - } + while ((p = css_task_iter_next(&it))) + uclamp_update_active(p); css_task_iter_end(&it); } @@ -7749,7 +7736,7 @@ static void cpu_util_update_eff(struct cgroup_subsys_state *css) } /* Immediately update descendants RUNNABLE tasks */ - uclamp_update_active_tasks(css, clamps); + uclamp_update_active_tasks(css); } } -- 2.30.2