Skip to content

Commit 190a7f9

Browse files
deggemangregkh
authored andcommitted
sched/fair: Fix util_est UTIL_AVG_UNCHANGED handling
commit 68d7a19 upstream. 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 92a801e ("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: b89997a sched/pelt: Fix task util_est update filtering Signed-off-by: Dietmar Eggemann <dietmar.eggemann@arm.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Xuewen Yan <xuewen.yan@unisoc.com> Reviewed-by: Vincent Donnefort <vincent.donnefort@arm.com> Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org> Link: https://lore.kernel.org/r/20210602145808.1562603-1-dietmar.eggemann@arm.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 32e22db commit 190a7f9

4 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,19 @@ struct load_weight {
348348
* Only for tasks we track a moving average of the past instantaneous
349349
* estimated utilization. This allows to absorb sporadic drops in utilization
350350
* of an otherwise almost periodic task.
351+
*
352+
* The UTIL_AVG_UNCHANGED flag is used to synchronize util_est with util_avg
353+
* updates. When a task is dequeued, its util_est should not be updated if its
354+
* util_avg has not been updated in the meantime.
355+
* This information is mapped into the MSB bit of util_est.enqueued at dequeue
356+
* time. Since max value of util_est.enqueued for a task is 1024 (PELT util_avg
357+
* for a task) it is safe to use MSB.
351358
*/
352359
struct util_est {
353360
unsigned int enqueued;
354361
unsigned int ewma;
355362
#define UTIL_EST_WEIGHT_SHIFT 2
363+
#define UTIL_AVG_UNCHANGED 0x80000000
356364
} __attribute__((__aligned__(sizeof(u64))));
357365

358366
/*

‎kernel/sched/debug.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ __initcall(init_sched_debug_procfs);
888888
#define __PS(S, F) SEQ_printf(m, "%-45s:%21Ld\n", S, (long long)(F))
889889
#define __P(F) __PS(#F, F)
890890
#define P(F) __PS(#F, p->F)
891+
#define PM(F, M) __PS(#F, p->F & (M))
891892
#define __PSN(S, F) SEQ_printf(m, "%-45s:%14Ld.%06ld\n", S, SPLIT_NS((long long)(F)))
892893
#define __PN(F) __PSN(#F, F)
893894
#define PN(F) __PSN(#F, p->F)
@@ -1014,7 +1015,7 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
10141015
P(se.avg.util_avg);
10151016
P(se.avg.last_update_time);
10161017
P(se.avg.util_est.ewma);
1017-
P(se.avg.util_est.enqueued);
1018+
PM(se.avg.util_est.enqueued, ~UTIL_AVG_UNCHANGED);
10181019
#endif
10191020
#ifdef CONFIG_UCLAMP_TASK
10201021
__PS("uclamp.min", p->uclamp_req[UCLAMP_MIN].value);

‎kernel/sched/fair.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,7 +3903,7 @@ static inline unsigned long _task_util_est(struct task_struct *p)
39033903
{
39043904
struct util_est ue = READ_ONCE(p->se.avg.util_est);
39053905

3906-
return (max(ue.ewma, ue.enqueued) | UTIL_AVG_UNCHANGED);
3906+
return max(ue.ewma, (ue.enqueued & ~UTIL_AVG_UNCHANGED));
39073907
}
39083908

39093909
static inline unsigned long task_util_est(struct task_struct *p)
@@ -4003,7 +4003,7 @@ static inline void util_est_update(struct cfs_rq *cfs_rq,
40034003
* Reset EWMA on utilization increases, the moving average is used only
40044004
* to smooth utilization decreases.
40054005
*/
4006-
ue.enqueued = (task_util(p) | UTIL_AVG_UNCHANGED);
4006+
ue.enqueued = task_util(p);
40074007
if (sched_feat(UTIL_EST_FASTUP)) {
40084008
if (ue.ewma < ue.enqueued) {
40094009
ue.ewma = ue.enqueued;
@@ -4052,6 +4052,7 @@ static inline void util_est_update(struct cfs_rq *cfs_rq,
40524052
ue.ewma += last_ewma_diff;
40534053
ue.ewma >>= UTIL_EST_WEIGHT_SHIFT;
40544054
done:
4055+
ue.enqueued |= UTIL_AVG_UNCHANGED;
40554056
WRITE_ONCE(p->se.avg.util_est, ue);
40564057

40574058
trace_sched_util_est_se_tp(&p->se);

‎kernel/sched/pelt.h‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,14 @@ static inline u32 get_pelt_divider(struct sched_avg *avg)
4242
return LOAD_AVG_MAX - 1024 + avg->period_contrib;
4343
}
4444

45-
/*
46-
* When a task is dequeued, its estimated utilization should not be update if
47-
* its util_avg has not been updated at least once.
48-
* This flag is used to synchronize util_avg updates with util_est updates.
49-
* We map this information into the LSB bit of the utilization saved at
50-
* dequeue time (i.e. util_est.dequeued).
51-
*/
52-
#define UTIL_AVG_UNCHANGED 0x1
53-
5445
static inline void cfs_se_util_change(struct sched_avg *avg)
5546
{
5647
unsigned int enqueued;
5748

5849
if (!sched_feat(UTIL_EST))
5950
return;
6051

61-
/* Avoid store if the flag has been already set */
52+
/* Avoid store if the flag has been already reset */
6253
enqueued = avg->util_est.enqueued;
6354
if (!(enqueued & UTIL_AVG_UNCHANGED))
6455
return;

0 commit comments

Comments
 (0)