Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp573785pxj; Fri, 14 May 2021 10:14:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztuiMreOH6N1o43Yh6Qv/BMIkYGwTXaHEQVcDYKya5JAB2EIq6CS+yvOsOy7XHuYYwlLXB X-Received: by 2002:a92:2004:: with SMTP id j4mr5120969ile.53.1621012455465; Fri, 14 May 2021 10:14:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621012455; cv=none; d=google.com; s=arc-20160816; b=mvFuBtplwdQjJ9kGHmIQhdDi/3Cpfe/lJG38lYZ8HTlDEYWKdjo+Z1Efoy7Io1qJUh Zz9c/IfRrEuh0docWBfA0RAdboEYP5k42PZkZ093ruWpMGDd+sbFtahRAq+D+PbldCPh XR4kbZbfonMz6DcQMOBlnz8JeB0a2dCl0HtXrc1RCHocUJmX1nFoWYwxhmrltivrc/NH 1JNu4VtFe0COI1x8T+quXMpJz4iReGMvtVF8KwGxZjeyisyFoa0MtMAI/ynmkb0AJFB4 YQIvG2QleJmM+Reu3KrGlajdbNQs5F1O0vBV0SjiVSFN8nG9LHWacCRL1QdxFGrhYp40 qq6Q== 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 :message-id:date:subject:cc:to:from; bh=vzNEA1ap2kqGCs+E/tWqWDyKlG0jkQOgCHT1pmKkwpM=; b=d6kCOn1OzZMHYdjbRCu/jmkolwcWLyiqnugBkPgPhYmSWAhi6ItTWu4YvTP6wmBqFr UuEKDwSKKNUtVHIBZo70tbO17EdrnHKUg1EbBTEMqrMtPXUqPd3KgDuZJL0kIJ4yBMi/ FW6Bq2HyIDL9Ktr4jIXpUQt3HGJ6aq9/cfqd5x/5sE3X/IofWbNbFnLBsYXhIAu9rZsa 4+g0k7aRVAD7e8OgD+tpc9ypXCHjcCyHu+Ev8YYhT/gwuK0PSR1EJBqQkGqzHrxtkga6 T3gSyYP0CWewvFe519kZfNayDHQAvY/2VqlTMQqdKZw2jX7O6y9yeHr4WP+4SkjMNCC0 heuA== 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 o14si9501681iow.26.2021.05.14.10.14.02; Fri, 14 May 2021 10:14: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; 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 S230403AbhENKjV (ORCPT + 99 others); Fri, 14 May 2021 06:39:21 -0400 Received: from foss.arm.com ([217.140.110.172]:46938 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbhENKjU (ORCPT ); Fri, 14 May 2021 06:39:20 -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 21F251713; Fri, 14 May 2021 03:38:09 -0700 (PDT) Received: from e125579.fritz.box (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 6E10C3F719; Fri, 14 May 2021 03:38:07 -0700 (PDT) From: Dietmar Eggemann To: Ingo Molnar , Peter Zijlstra , Xuewen Yan , Vincent Donnefort Cc: Juri Lelli , Vincent Guittot , Steven Rostedt , Patrick Bellasi , Quentin Perret , linux-kernel@vger.kernel.org Subject: [PATCH] sched/fair: Fix util_est UTIL_AVG_UNCHANGED handling Date: Fri, 14 May 2021 12:37:48 +0200 Message-Id: <20210514103748.737809-1-dietmar.eggemann@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The util_est internal UTIL_AVG_UNCHANGED flag which is used to prevent unnecessary util_est updates uses the LSB of util_est.enqueued. It is exposed via _task_util_est() (and task_util_est()). Commit 92a801e5d5b7 ("sched/fair: Mask UTIL_AVG_UNCHANGED usages") mentions that the LSB is lost for util_est resolution but find_energy_efficient_cpu() checks if task_util_est() returns 0 to return prev_cpu early. _task_util_est() returns the max value of util_est.ewma and util_est.enqueued or'ed w/ UTIL_AVG_UNCHANGED. So task_util_est() returning the max of task_util() and _task_util_est() will never return 0 under the default SCHED_FEAT(UTIL_EST, true). To fix this use the MSB of util_est.enqueued instead and keep the flag util_est internal, i.e. don't export it via _task_util_est(). The maximal possible util_avg value for a task is 1024 so the MSB of 'unsigned int util_est.enqueued' isn't used to store a util value. As a caveat the code behind the util_est_se trace point has to filter UTIL_AVG_UNCHANGED to see the real util_est.enqueued value which should be easy to do. This also fixes an issue report by Xuewen Yan that util_est_update() only used UTIL_AVG_UNCHANGED for the subtrahend of the equation: last_enqueued_diff = ue.enqueued - (task_util() | UTIL_AVG_UNCHANGED) Fixes: b89997aa88f0b sched/pelt: Fix task util_est update filtering Signed-off-by: Dietmar Eggemann --- kernel/sched/fair.c | 5 +++-- kernel/sched/pelt.h | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 161b92aa1c79..0150d440b0a2 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -3856,7 +3856,7 @@ static inline unsigned long _task_util_est(struct task_struct *p) { struct util_est ue = READ_ONCE(p->se.avg.util_est); - return (max(ue.ewma, ue.enqueued) | UTIL_AVG_UNCHANGED); + return max(ue.ewma, (ue.enqueued & ~UTIL_AVG_UNCHANGED)); } static inline unsigned long task_util_est(struct task_struct *p) @@ -3956,7 +3956,7 @@ static inline void util_est_update(struct cfs_rq *cfs_rq, * Reset EWMA on utilization increases, the moving average is used only * to smooth utilization decreases. */ - ue.enqueued = (task_util(p) | UTIL_AVG_UNCHANGED); + ue.enqueued = task_util(p); if (sched_feat(UTIL_EST_FASTUP)) { if (ue.ewma < ue.enqueued) { ue.ewma = ue.enqueued; @@ -4005,6 +4005,7 @@ static inline void util_est_update(struct cfs_rq *cfs_rq, ue.ewma += last_ewma_diff; ue.ewma >>= UTIL_EST_WEIGHT_SHIFT; done: + ue.enqueued |= UTIL_AVG_UNCHANGED; WRITE_ONCE(p->se.avg.util_est, ue); trace_sched_util_est_se_tp(&p->se); diff --git a/kernel/sched/pelt.h b/kernel/sched/pelt.h index 9ed6d8c414ad..178290a8d150 100644 --- a/kernel/sched/pelt.h +++ b/kernel/sched/pelt.h @@ -43,13 +43,14 @@ static inline u32 get_pelt_divider(struct sched_avg *avg) } /* - * When a task is dequeued, its estimated utilization should not be update if - * its util_avg has not been updated at least once. + * When a task is dequeued, its estimated utilization should not be updated if + * its util_avg has not been updated in the meantime. * This flag is used to synchronize util_avg updates with util_est updates. - * We map this information into the LSB bit of the utilization saved at - * dequeue time (i.e. util_est.dequeued). + * We map this information into the MSB bit of util_est.enqueued at dequeue + * time. Since max value of util_est.enqueued for a task is 1024 (PELT + * util_avg for a task) it is safe to use MSB here. */ -#define UTIL_AVG_UNCHANGED 0x1 +#define UTIL_AVG_UNCHANGED 0x80000000 static inline void cfs_se_util_change(struct sched_avg *avg) { @@ -58,7 +59,7 @@ static inline void cfs_se_util_change(struct sched_avg *avg) if (!sched_feat(UTIL_EST)) return; - /* Avoid store if the flag has been already set */ + /* Avoid store if the flag has been already reset */ enqueued = avg->util_est.enqueued; if (!(enqueued & UTIL_AVG_UNCHANGED)) return; -- 2.25.1