Skip to content

Commit 0e2d2aa

Browse files
Peter Zijlstraingomolnar
authored andcommitted
sched/fair: Rewrite PELT migration propagation
When an entity migrates in (or out) of a runqueue, we need to add (or remove) its contribution from the entire PELT hierarchy, because even non-runnable entities are included in the load average sums. In order to do this we have some propagation logic that updates the PELT tree, however the way it 'propagates' the runnable (or load) change is (more or less): tg->weight * grq->avg.load_avg ge->avg.load_avg = ------------------------------ tg->load_avg But that is the expression for ge->weight, and per the definition of load_avg: ge->avg.load_avg := ge->weight * ge->avg.runnable_avg That destroys the runnable_avg (by setting it to 1) we wanted to propagate. Instead directly propagate runnable_sum. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 2a2f5d4 commit 0e2d2aa

3 files changed

Lines changed: 112 additions & 85 deletions

File tree

‎kernel/sched/debug.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
568568
cfs_rq->removed.load_avg);
569569
SEQ_printf(m, " .%-30s: %ld\n", "removed.util_avg",
570570
cfs_rq->removed.util_avg);
571+
SEQ_printf(m, " .%-30s: %ld\n", "removed.runnable_sum",
572+
cfs_rq->removed.runnable_sum);
571573
#ifdef CONFIG_FAIR_GROUP_SCHED
572574
SEQ_printf(m, " .%-30s: %lu\n", "tg_load_avg_contrib",
573575
cfs_rq->tg_load_avg_contrib);

‎kernel/sched/fair.c‎

Lines changed: 105 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,11 +3319,77 @@ void set_task_rq_fair(struct sched_entity *se,
33193319
se->avg.last_update_time = n_last_update_time;
33203320
}
33213321

3322-
/* Take into account change of utilization of a child task group */
3322+
3323+
/*
3324+
* When on migration a sched_entity joins/leaves the PELT hierarchy, we need to
3325+
* propagate its contribution. The key to this propagation is the invariant
3326+
* that for each group:
3327+
*
3328+
* ge->avg == grq->avg (1)
3329+
*
3330+
* _IFF_ we look at the pure running and runnable sums. Because they
3331+
* represent the very same entity, just at different points in the hierarchy.
3332+
*
3333+
*
3334+
* Per the above update_tg_cfs_util() is trivial (and still 'wrong') and
3335+
* simply copies the running sum over.
3336+
*
3337+
* However, update_tg_cfs_runnable() is more complex. So we have:
3338+
*
3339+
* ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
3340+
*
3341+
* And since, like util, the runnable part should be directly transferable,
3342+
* the following would _appear_ to be the straight forward approach:
3343+
*
3344+
* grq->avg.load_avg = grq->load.weight * grq->avg.running_avg (3)
3345+
*
3346+
* And per (1) we have:
3347+
*
3348+
* ge->avg.running_avg == grq->avg.running_avg
3349+
*
3350+
* Which gives:
3351+
*
3352+
* ge->load.weight * grq->avg.load_avg
3353+
* ge->avg.load_avg = ----------------------------------- (4)
3354+
* grq->load.weight
3355+
*
3356+
* Except that is wrong!
3357+
*
3358+
* Because while for entities historical weight is not important and we
3359+
* really only care about our future and therefore can consider a pure
3360+
* runnable sum, runqueues can NOT do this.
3361+
*
3362+
* We specifically want runqueues to have a load_avg that includes
3363+
* historical weights. Those represent the blocked load, the load we expect
3364+
* to (shortly) return to us. This only works by keeping the weights as
3365+
* integral part of the sum. We therefore cannot decompose as per (3).
3366+
*
3367+
* OK, so what then?
3368+
*
3369+
*
3370+
* Another way to look at things is:
3371+
*
3372+
* grq->avg.load_avg = \Sum se->avg.load_avg
3373+
*
3374+
* Therefore, per (2):
3375+
*
3376+
* grq->avg.load_avg = \Sum se->load.weight * se->avg.runnable_avg
3377+
*
3378+
* And the very thing we're propagating is a change in that sum (someone
3379+
* joined/left). So we can easily know the runnable change, which would be, per
3380+
* (2) the already tracked se->load_avg divided by the corresponding
3381+
* se->weight.
3382+
*
3383+
* Basically (4) but in differential form:
3384+
*
3385+
* d(runnable_avg) += se->avg.load_avg / se->load.weight
3386+
* (5)
3387+
* ge->avg.load_avg += ge->load.weight * d(runnable_avg)
3388+
*/
3389+
33233390
static inline void
3324-
update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se)
3391+
update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
33253392
{
3326-
struct cfs_rq *gcfs_rq = group_cfs_rq(se);
33273393
long delta = gcfs_rq->avg.util_avg - se->avg.util_avg;
33283394

33293395
/* Nothing to update */
@@ -3339,102 +3405,59 @@ update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se)
33393405
cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX;
33403406
}
33413407

3342-
/* Take into account change of load of a child task group */
33433408
static inline void
3344-
update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se)
3409+
update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
33453410
{
3346-
struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3347-
long delta, load = gcfs_rq->avg.load_avg;
3411+
long runnable_sum = gcfs_rq->prop_runnable_sum;
3412+
long load_avg;
3413+
s64 load_sum;
33483414

3349-
/*
3350-
* If the load of group cfs_rq is null, the load of the
3351-
* sched_entity will also be null so we can skip the formula
3352-
*/
3353-
if (load) {
3354-
long tg_load;
3355-
3356-
/* Get tg's load and ensure tg_load > 0 */
3357-
tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1;
3358-
3359-
/* Ensure tg_load >= load and updated with current load*/
3360-
tg_load -= gcfs_rq->tg_load_avg_contrib;
3361-
tg_load += load;
3362-
3363-
/*
3364-
* We need to compute a correction term in the case that the
3365-
* task group is consuming more CPU than a task of equal
3366-
* weight. A task with a weight equals to tg->shares will have
3367-
* a load less or equal to scale_load_down(tg->shares).
3368-
* Similarly, the sched_entities that represent the task group
3369-
* at parent level, can't have a load higher than
3370-
* scale_load_down(tg->shares). And the Sum of sched_entities'
3371-
* load must be <= scale_load_down(tg->shares).
3372-
*/
3373-
if (tg_load > scale_load_down(gcfs_rq->tg->shares)) {
3374-
/* scale gcfs_rq's load into tg's shares*/
3375-
load *= scale_load_down(gcfs_rq->tg->shares);
3376-
load /= tg_load;
3377-
}
3378-
}
3415+
if (!runnable_sum)
3416+
return;
33793417

3380-
delta = load - se->avg.load_avg;
3418+
gcfs_rq->prop_runnable_sum = 0;
33813419

3382-
/* Nothing to update */
3383-
if (!delta)
3384-
return;
3420+
load_sum = (s64)se_weight(se) * runnable_sum;
3421+
load_avg = div_s64(load_sum, LOAD_AVG_MAX);
33853422

3386-
/* Set new sched_entity's load */
3387-
se->avg.load_avg = load;
3388-
se->avg.load_sum = LOAD_AVG_MAX;
3423+
add_positive(&se->avg.load_sum, runnable_sum);
3424+
add_positive(&se->avg.load_avg, load_avg);
33893425

3390-
/* Update parent cfs_rq load */
3391-
add_positive(&cfs_rq->avg.load_avg, delta);
3392-
cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX;
3426+
add_positive(&cfs_rq->avg.load_avg, load_avg);
3427+
add_positive(&cfs_rq->avg.load_sum, load_sum);
33933428

3394-
/*
3395-
* If the sched_entity is already enqueued, we also have to update the
3396-
* runnable load avg.
3397-
*/
33983429
if (se->on_rq) {
3399-
/* Update parent cfs_rq runnable_load_avg */
3400-
add_positive(&cfs_rq->runnable_load_avg, delta);
3401-
cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX;
3430+
add_positive(&cfs_rq->runnable_load_avg, load_avg);
3431+
add_positive(&cfs_rq->runnable_load_sum, load_sum);
34023432
}
34033433
}
34043434

3405-
static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq)
3435+
static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum)
34063436
{
3407-
cfs_rq->propagate_avg = 1;
3408-
}
3409-
3410-
static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se)
3411-
{
3412-
struct cfs_rq *cfs_rq = group_cfs_rq(se);
3413-
3414-
if (!cfs_rq->propagate_avg)
3415-
return 0;
3416-
3417-
cfs_rq->propagate_avg = 0;
3418-
return 1;
3437+
cfs_rq->propagate = 1;
3438+
cfs_rq->prop_runnable_sum += runnable_sum;
34193439
}
34203440

34213441
/* Update task and its cfs_rq load average */
34223442
static inline int propagate_entity_load_avg(struct sched_entity *se)
34233443
{
3424-
struct cfs_rq *cfs_rq;
3444+
struct cfs_rq *cfs_rq, *gcfs_rq;
34253445

34263446
if (entity_is_task(se))
34273447
return 0;
34283448

3429-
if (!test_and_clear_tg_cfs_propagate(se))
3449+
gcfs_rq = group_cfs_rq(se);
3450+
if (!gcfs_rq->propagate)
34303451
return 0;
34313452

3453+
gcfs_rq->propagate = 0;
3454+
34323455
cfs_rq = cfs_rq_of(se);
34333456

3434-
set_tg_cfs_propagate(cfs_rq);
3457+
add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum);
34353458

3436-
update_tg_cfs_util(cfs_rq, se);
3437-
update_tg_cfs_load(cfs_rq, se);
3459+
update_tg_cfs_util(cfs_rq, se, gcfs_rq);
3460+
update_tg_cfs_runnable(cfs_rq, se, gcfs_rq);
34383461

34393462
return 1;
34403463
}
@@ -3458,7 +3481,7 @@ static inline bool skip_blocked_update(struct sched_entity *se)
34583481
* If there is a pending propagation, we have to update the load and
34593482
* the utilization of the sched_entity:
34603483
*/
3461-
if (gcfs_rq->propagate_avg)
3484+
if (gcfs_rq->propagate)
34623485
return false;
34633486

34643487
/*
@@ -3478,7 +3501,7 @@ static inline int propagate_entity_load_avg(struct sched_entity *se)
34783501
return 0;
34793502
}
34803503

3481-
static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
3504+
static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {}
34823505

34833506
#endif /* CONFIG_FAIR_GROUP_SCHED */
34843507

@@ -3501,7 +3524,7 @@ static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
35013524
static inline int
35023525
update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
35033526
{
3504-
unsigned long removed_load = 0, removed_util = 0;
3527+
unsigned long removed_load = 0, removed_util = 0, removed_runnable_sum = 0;
35053528
struct sched_avg *sa = &cfs_rq->avg;
35063529
int decayed = 0;
35073530

@@ -3511,6 +3534,7 @@ update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
35113534
raw_spin_lock(&cfs_rq->removed.lock);
35123535
swap(cfs_rq->removed.util_avg, removed_util);
35133536
swap(cfs_rq->removed.load_avg, removed_load);
3537+
swap(cfs_rq->removed.runnable_sum, removed_runnable_sum);
35143538
cfs_rq->removed.nr = 0;
35153539
raw_spin_unlock(&cfs_rq->removed.lock);
35163540

@@ -3526,7 +3550,7 @@ update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
35263550
sub_positive(&sa->util_avg, r);
35273551
sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
35283552

3529-
set_tg_cfs_propagate(cfs_rq);
3553+
add_tg_cfs_propagate(cfs_rq, -(long)removed_runnable_sum);
35303554

35313555
decayed = 1;
35323556
}
@@ -3558,7 +3582,8 @@ static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s
35583582
enqueue_load_avg(cfs_rq, se);
35593583
cfs_rq->avg.util_avg += se->avg.util_avg;
35603584
cfs_rq->avg.util_sum += se->avg.util_sum;
3561-
set_tg_cfs_propagate(cfs_rq);
3585+
3586+
add_tg_cfs_propagate(cfs_rq, se->avg.load_sum);
35623587

35633588
cfs_rq_util_change(cfs_rq);
35643589
}
@@ -3576,7 +3601,8 @@ static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s
35763601
dequeue_load_avg(cfs_rq, se);
35773602
sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
35783603
sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
3579-
set_tg_cfs_propagate(cfs_rq);
3604+
3605+
add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum);
35803606

35813607
cfs_rq_util_change(cfs_rq);
35823608
}
@@ -3678,6 +3704,7 @@ void remove_entity_load_avg(struct sched_entity *se)
36783704
++cfs_rq->removed.nr;
36793705
cfs_rq->removed.util_avg += se->avg.util_avg;
36803706
cfs_rq->removed.load_avg += se->avg.load_avg;
3707+
cfs_rq->removed.runnable_sum += se->avg.load_sum; /* == runnable_sum */
36813708
raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags);
36823709
}
36833710

@@ -9466,9 +9493,6 @@ void init_cfs_rq(struct cfs_rq *cfs_rq)
94669493
cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
94679494
#endif
94689495
#ifdef CONFIG_SMP
9469-
#ifdef CONFIG_FAIR_GROUP_SCHED
9470-
cfs_rq->propagate_avg = 0;
9471-
#endif
94729496
raw_spin_lock_init(&cfs_rq->removed.lock);
94739497
#endif
94749498
}

‎kernel/sched/sched.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,20 @@ struct cfs_rq {
447447
unsigned long runnable_load_avg;
448448
#ifndef CONFIG_64BIT
449449
u64 load_last_update_time_copy;
450-
#endif
451-
#ifdef CONFIG_FAIR_GROUP_SCHED
452-
unsigned long tg_load_avg_contrib;
453-
unsigned long propagate_avg;
454450
#endif
455451
struct {
456452
raw_spinlock_t lock ____cacheline_aligned;
457453
int nr;
458454
unsigned long load_avg;
459455
unsigned long util_avg;
456+
unsigned long runnable_sum;
460457
} removed;
461458

462459
#ifdef CONFIG_FAIR_GROUP_SCHED
460+
unsigned long tg_load_avg_contrib;
461+
long propagate;
462+
long prop_runnable_sum;
463+
463464
/*
464465
* h_load = weight * f(tg)
465466
*

0 commit comments

Comments
 (0)