Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp562847pxv; Thu, 1 Jul 2021 04:37:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFDDwaEVvg0x/jhwUPE7ArZDhsnUCH4VNPLcNnEtoy52p2KBHn2kA/rof6E1rLvMQiRaZZ X-Received: by 2002:a17:906:cedd:: with SMTP id si29mr30773932ejb.81.1625139420665; Thu, 01 Jul 2021 04:37:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625139420; cv=none; d=google.com; s=arc-20160816; b=h/cXO4kEVy+a4oBXjH6BXksWwGX/EVUdELHpwoE1ExkdnMTpIJrrYkaVJrVwK7fM7E +QCrGuhlG0W8CfZdHtNiBTQ57WKQJmxS4y3Of3Id9fTOQR96M9A0e3fhp1Vkm+PBWF0T 92htMg2sAz2W2+NP6lWVWUZdddyxaJ4cpdoHfqFSopqUFGyAXZb+CNJaHpSKR+krmCQY 41s7XHTCD6IPGs8TaatDqoz/UDWq6YmVJE1wDjYGLweFAf+RFl2J2NNU/U8k2bWGjCJ1 +/LXfV0W4NRW8mG330UvBtQWc5xkJS71qtSoSU91xc17zxHoW5ZBhIXA2EQHxS/+zFT3 W3hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=wEk0RFENBURpXWA37Ik/O3XRamRGOfUxm1g2FraS5qY=; b=AOZuTDRIGWkKdtvoEP5gHLSrY2BGh/WCn14KN7IQN2j9nqyhJGgb1O+Ov57npk0k2l TFl5F2GAoOmdJo4I28jo3j5IWhe96b1IskCMBjTODRhB74XVm5bwvCy0aMDoJKnQfU18 8R4DL4FvB5vGCCJcqXhyZdtWT+3x8r7Ila18R4B80Bc7mvunQq+y7BYyMKSAkUc2+Arb f+/xGDTEDWcSEZeByJBu9ckD/0vsz+9/Lwg1Ut4SUoSNF3c08atsn7mDCRJagsGIZdzw RTPa+UWokQlL8434nhLZMQJRo7fHVUbdWPd4OZLAdKUNFKHoROXE+7TO6DzkLrs9+lm6 /7mA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si10561930edq.364.2021.07.01.04.36.36; Thu, 01 Jul 2021 04:37:00 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236271AbhGALfH (ORCPT + 99 others); Thu, 1 Jul 2021 07:35:07 -0400 Received: from foss.arm.com ([217.140.110.172]:51996 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236130AbhGALfG (ORCPT ); Thu, 1 Jul 2021 07:35:06 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 289E66D; Thu, 1 Jul 2021 04:32:36 -0700 (PDT) Received: from e107158-lin.cambridge.arm.com (unknown [10.1.195.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5E9EE3F718; Thu, 1 Jul 2021 04:32:34 -0700 (PDT) Date: Thu, 1 Jul 2021 12:32:32 +0100 From: Qais Yousef To: Xuewen Yan Cc: valentin.schneider@arm.com, 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, linux-kernel@vger.kernel.org, patrick.bellasi@matbug.net, qperret@google.com Subject: Re: [PATCH v2] sched/uclamp: Avoid getting unreasonable ucalmp value when rq is idle Message-ID: <20210701113232.quqmxjy22udr2hfb@e107158-lin.cambridge.arm.com> References: <20210630141204.8197-1-xuewen.yan94@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210630141204.8197-1-xuewen.yan94@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 06/30/21 22:12, Xuewen Yan wrote: > From: Xuewen Yan > > Now in uclamp_rq_util_with(), when the task != NULL, the uclamp_max as following: > uc_rq_max = rq->uclamp[UCLAMP_MAX].value; > uc_eff_max = uclamp_eff_value(p, UCLAMP_MAX); > uclamp_max = max{uc_rq_max, uc_eff_max}; > > Consider the following scenario: > (1)the rq is idle, the uc_rq_max is last runnable task's UCLAMP_MAX; > (2)the p's uc_eff_max < uc_rq_max. > > As a result, the uclamp_max = uc_rq_max instead of uc_eff_max, it is unreasonable. > > The scenario often happens in find_energy_efficient_cpu(), when the task has smaller UCLAMP_MAX. > > When rq has UCLAMP_FLAG_IDLE flag, enqueuing the task will lift UCLAMP_FLAG_IDLE > and set the rq clamp as the task's via uclamp_idle_reset(). It doesn't need > to read the rq clamp. And it can also avoid the problems described above. > > Fixes: 9d20ad7dfc9a ("sched/uclamp: Add uclamp_util_with()") > > Signed-off-by: Xuewen Yan > > --- > change v2: > *add Fixes(Valentin Schneider); > *ignore all rq clamp when idle (Valentin Schneider) > --- > kernel/sched/sched.h | 21 ++++++++++++++------- > 1 file changed, 14 insertions(+), 7 deletions(-) > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h > index c80d42e9589b..14a41a243f7b 100644 > --- a/kernel/sched/sched.h > +++ b/kernel/sched/sched.h > @@ -2818,20 +2818,27 @@ static __always_inline > unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util, > struct task_struct *p) > { > - unsigned long min_util; > - unsigned long max_util; > + unsigned long min_util = 0; > + unsigned long max_util = 0; > > if (!static_branch_likely(&sched_uclamp_used)) > return util; > > - min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value); > - max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value); > - > if (p) { > - min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN)); > - max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX)); > + min_util = uclamp_eff_value(p, UCLAMP_MIN); > + max_util = uclamp_eff_value(p, UCLAMP_MAX); > + > + /* > + * Ignore last runnable task's max clamp, as this task will > + * reset it. Similarly, no need to read the rq's min clamp. > + */ > + if (rq->uclamp_flags & UCLAMP_FLAG_IDLE) > + goto out; We read rq->uclamp_flags without locks here. Me thinks this needs READ_ONCE(). But since we care only about a single bit, I can't see any risk for inconsistency, so we're fine. Reviewed-by: Qais Yousef Thanks! -- Qais Yousef > } > > + min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value)); > + max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value)); > +out: > /* > * Since CPU's {min,max}_util clamps are MAX aggregated considering > * RUNNABLE tasks with _different_ clamps, we can end up with an > -- > 2.25.1 >