Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp866024pxb; Fri, 22 Apr 2022 13:00:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz08vd98SqmsY8GZPTJRWJ7iHz1z1rtYu7LGhgHx0Q9GAtqFnm4ZpAwnoA887sVqXEVRIjC X-Received: by 2002:a17:90b:4c0c:b0:1d2:7cd4:20a6 with SMTP id na12-20020a17090b4c0c00b001d27cd420a6mr18084545pjb.110.1650657620972; Fri, 22 Apr 2022 13:00:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650657620; cv=none; d=google.com; s=arc-20160816; b=i0wQzQ9btJiKaFPIRKIGdaZ7UmF7Vp4FRirS6Ngo3nFIJnYleJL4qVI6eHcN3h15s0 yzvqvrxh1BOOZezb+iYdrIl9mS1vAOK0GAjtSZd4zU05CqfXv9m3fVYWrWmfyVpbS3xD s/QvUvp19B0j7Acu5PCdl2hiRCZk7wtYhexH/Z2CuDJsk01V0niYgmLpAnGl8lqG0IyD pXaC6sreUtgzm8IhpYeFM0UqUUM6+D7NBwRaADmSRA3nCRSNuX4XIueoWzW5CcyIbx6o hmX+Q8MGQFLDs6JpJwM6+rlvyJYVUVl2DiDS5kcwLTxHYnfp8+mt+xDiZdaVandrGwlo nQog== 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:dkim-signature:date; bh=+CdXSQ8TwyT27irzS0Uas8QgJTdWTejo8X7ExtkpCB8=; b=SwdEOGSR0sxOxb8PtUfvQKIEhRg9nNuCkc5k4IcwkXebEIMZbYhXsa0TVnRYX6UAZF u8HqJXTkML4o2wHtq9tNZcbimnl6KwRteDZ0HlLPklZgg8m5tmnJKNtoQ6xbeXjryKrA +107JjfhWtmgLG8kabAko4fSDbWBndbp/9Vv7KsFat3PD8G2vZsa8XVWbRKRZxv459rL aJt8sMAMXX97Y+U9R285yfZp9NhM7XXftW4XtTQwe2pNMHz/RQNt9XGt/B0pvpWxoGiv QwPrC+Rzd1Gmf0f2AgFZrhpaxA4oGZZowxtzSe/ZwaXPUpkpjbDLCPvzXwwyss7qovUu vEGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=b9b8FOXq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id o21-20020a056a0015d500b00508372bac17si9852153pfu.305.2022.04.22.13.00.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 13:00:20 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=b9b8FOXq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D6E92163D34; Fri, 22 Apr 2022 11:58:40 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1449305AbiDVPRS (ORCPT + 99 others); Fri, 22 Apr 2022 11:17:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1449320AbiDVPRQ (ORCPT ); Fri, 22 Apr 2022 11:17:16 -0400 Received: from out0.migadu.com (out0.migadu.com [IPv6:2001:41d0:2:267::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D39375DA01 for ; Fri, 22 Apr 2022 08:14:21 -0700 (PDT) Date: Fri, 22 Apr 2022 23:14:58 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1650640460; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+CdXSQ8TwyT27irzS0Uas8QgJTdWTejo8X7ExtkpCB8=; b=b9b8FOXqU3HsITX4epha6H3gjGUMSlWrUqCAOwx9V6i6OQw8vo54yWnxNtcbr/ceEoJj3y zccQy27UdGS3Co71vD0oNSEgkGN4rcpSl51YYJyGJQWdmOqd0Yu6X2u1Xqds74VKM90Q9W q904cjP6ba0UOcSkxdHvxW/RA80YwjY= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Tao Zhou To: Vincent Donnefort , Tao Zhou Cc: peterz@infradead.org, mingo@redhat.com, vincent.guittot@linaro.org, linux-kernel@vger.kernel.org, dietmar.eggemann@arm.com, morten.rasmussen@arm.com, chris.redpath@arm.com, qperret@google.com Subject: Re: [PATCH v4 2/7] sched/fair: Decay task PELT values during wakeup migration Message-ID: References: <20220412134220.1588482-1-vincent.donnefort@arm.com> <20220412134220.1588482-3-vincent.donnefort@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220412134220.1588482-3-vincent.donnefort@arm.com> X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=no 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 On Tue, Apr 12, 2022 at 02:42:15PM +0100, Vincent Donnefort wrote: > Before being migrated to a new CPU, a task sees its PELT values > synchronized with rq last_update_time. Once done, that same task will also > have its sched_avg last_update_time reset. This means the time between > the migration and the last clock update (B) will not be accounted for in > util_avg and a discontinuity will appear. This issue is amplified by the > PELT clock scaling. If the clock hasn't been updated while the CPU is > idle, clock_pelt will not be aligned with clock_task and that time (A) > will be also lost. > > ---------|----- A -----|-----------|------- B -----|> > clock_pelt clock_task clock now > > This is especially problematic for asymmetric CPU capacity systems which > need stable util_avg signals for task placement and energy estimation. > > Ideally, this problem would be solved by updating the runqueue clocks > before the migration. But that would require taking the runqueue lock > which is quite expensive [1]. Instead estimate the missing time and update > the task util_avg with that value: > > A + B = clock_task - clock_pelt + sched_clock_cpu() - clock > > Neither clock_task, clock_pelt nor clock can be accessed without the > runqueue lock. The new cfs_rq last_update_lag is therefore created and > contains those three values when the last_update_time value for that very > same cfs_rq is updated. > > last_update_lag = clock - clock_task + clock_pelt > > And we can then write the missing time as follow: > > A + B = sched_clock_cpu() - last_update_lag > > The B. part of the missing time is however an estimation that doesn't take > into account IRQ and Paravirt time. > > Now we have an estimation for A + B, we can create an estimator for the > PELT value at the time of the migration. We need for this purpose to > inject last_update_time which is a combination of both clock_pelt and > lost_idle_time. The latter is a time value which is completely lost from a > PELT point of view and must be ignored. And finally, we can write: > > now = last_update_time + A + B > = last_update_time + sched_clock_cpu() - last_update_lag > > This estimation has a cost, mostly due to sched_clock_cpu(). Limit the > usage to the case where the source CPU is idle as we know this is when the > clock is having the biggest risk of being outdated. > > [1] https://lore.kernel.org/all/20190709115759.10451-1-chris.redpath@arm.com/ > > Signed-off-by: Vincent Donnefort > > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index 5dd38c9df0cc..e234d015657f 100644 > --- a/kernel/sched/fair.c > +++ b/kernel/sched/fair.c > @@ -3694,6 +3694,57 @@ static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum > > #endif /* CONFIG_FAIR_GROUP_SCHED */ > > +#ifdef CONFIG_NO_HZ_COMMON > +static inline void update_cfs_rq_lag(struct cfs_rq *cfs_rq) > +{ > + struct rq *rq = rq_of(cfs_rq); > + > + u64_u32_store(cfs_rq->last_update_lag, > +#ifdef CONFIG_CFS_BANDWIDTH > + /* Timer stopped by throttling */ > + unlikely(cfs_rq->throttle_count) ? U64_MAX : > +#endif > + rq->clock - rq->clock_task + rq->clock_pelt); > +} > + > +static inline void migrate_se_pelt_lag(struct sched_entity *se) > +{ > + u64 now, last_update_lag; > + struct cfs_rq *cfs_rq; > + struct rq *rq; > + bool is_idle; > + > + cfs_rq = cfs_rq_of(se); > + rq = rq_of(cfs_rq); > + > + rcu_read_lock(); > + is_idle = is_idle_task(rcu_dereference(rq->curr)); > + rcu_read_unlock(); > + > + /* > + * The lag estimation comes with a cost we don't want to pay all the > + * time. Hence, limiting to the case where the source CPU is idle and > + * we know we are at the greatest risk to have an outdated clock. > + */ > + if (!is_idle) > + return; > + > + last_update_lag = u64_u32_load(cfs_rq->last_update_lag); If CPU is idle, clock_pelt is equal to clock_task, A part is disappeared. > + /* The clock has been stopped for throttling */ > + if (last_update_lag == U64_MAX) > + return; > + > + now = se->avg.last_update_time - last_update_lag + > + sched_clock_cpu(cpu_of(rq)); > + > + __update_load_avg_blocked_se(now, se); > +} > +#else > +static void update_cfs_rq_lag(struct cfs_rq *cfs_rq) {} > +static void migrate_se_pelt_lag(struct sched_entity *se) {} > +#endif > + > /** > * update_cfs_rq_load_avg - update the cfs_rq's load/util averages > * @now: current time, as per cfs_rq_clock_pelt() > @@ -3774,6 +3825,7 @@ update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) > cfs_rq->last_update_time_copy, > sa->last_update_time); > #endif > + update_cfs_rq_lag(cfs_rq); > > return decayed; > } > @@ -6946,6 +6998,8 @@ static void detach_entity_cfs_rq(struct sched_entity *se); > */ > static void migrate_task_rq_fair(struct task_struct *p, int new_cpu) > { > + struct sched_entity *se = &p->se; > + > /* > * As blocked tasks retain absolute vruntime the migration needs to > * deal with this by subtracting the old and adding the new > @@ -6953,7 +7007,6 @@ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu) > * the task on the new runqueue. > */ > if (READ_ONCE(p->__state) == TASK_WAKING) { > - struct sched_entity *se = &p->se; > struct cfs_rq *cfs_rq = cfs_rq_of(se); > > se->vruntime -= u64_u32_load(cfs_rq->min_vruntime); > @@ -6965,25 +7018,28 @@ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu) > * rq->lock and can modify state directly. > */ > lockdep_assert_rq_held(task_rq(p)); > - detach_entity_cfs_rq(&p->se); > + detach_entity_cfs_rq(se); > > } else { > + remove_entity_load_avg(se); > + > /* > - * We are supposed to update the task to "current" time, then > - * its up to date and ready to go to new CPU/cfs_rq. But we > - * have difficulty in getting what current time is, so simply > - * throw away the out-of-date time. This will result in the > - * wakee task is less decayed, but giving the wakee more load > - * sounds not bad. > + * Here, the task's PELT values have been updated according to > + * the current rq's clock. But if that clock hasn't been > + * updated in a while, a substantial idle time will be missed, > + * leading to an inflation after wake-up on the new rq. > + * > + * Estimate the missing time from the rq clock and update > + * sched_avg to improve the PELT continuity after migration. > */ > - remove_entity_load_avg(&p->se); > + migrate_se_pelt_lag(se); > } > > /* Tell new CPU we are migrated */ > - p->se.avg.last_update_time = 0; > + se->avg.last_update_time = 0; > > /* We have migrated, no longer consider this task hot */ > - p->se.exec_start = 0; > + se->exec_start = 0; > > update_scan_period(p, new_cpu); > } > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h > index e2cf6e48b165..2f6446295e7d 100644 > --- a/kernel/sched/sched.h > +++ b/kernel/sched/sched.h > @@ -593,6 +593,12 @@ struct cfs_rq { > struct sched_avg avg; > #ifndef CONFIG_64BIT > u64 last_update_time_copy; > +#endif > +#ifdef CONFIG_NO_HZ_COMMON > + u64 last_update_lag; > +#ifndef CONFIG_64BIT > + u64 last_update_lag_copy; > +#endif > #endif > struct { > raw_spinlock_t lock ____cacheline_aligned; > -- > 2.25.1 >