Skip to content

Commit 2a2f5d4

Browse files
Peter Zijlstraingomolnar
authored andcommitted
sched/fair: Rewrite cfs_rq->removed_*avg
Since on wakeup migration we don't hold the rq->lock for the old CPU we cannot update its state. Instead we add the removed 'load' to an atomic variable and have the next update on that CPU collect and process it. Currently we have 2 atomic variables; which already have the issue that they can be read out-of-sync. Also, two atomic ops on a single cacheline is already more expensive than an uncontended lock. Since we want to add more, convert the thing over to an explicit cacheline with a lock in. 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 9059393 commit 2a2f5d4

3 files changed

Lines changed: 48 additions & 24 deletions

File tree

‎kernel/sched/debug.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,10 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
564564
cfs_rq->runnable_load_avg);
565565
SEQ_printf(m, " .%-30s: %lu\n", "util_avg",
566566
cfs_rq->avg.util_avg);
567-
SEQ_printf(m, " .%-30s: %ld\n", "removed_load_avg",
568-
atomic_long_read(&cfs_rq->removed_load_avg));
569-
SEQ_printf(m, " .%-30s: %ld\n", "removed_util_avg",
570-
atomic_long_read(&cfs_rq->removed_util_avg));
567+
SEQ_printf(m, " .%-30s: %ld\n", "removed.load_avg",
568+
cfs_rq->removed.load_avg);
569+
SEQ_printf(m, " .%-30s: %ld\n", "removed.util_avg",
570+
cfs_rq->removed.util_avg);
571571
#ifdef CONFIG_FAIR_GROUP_SCHED
572572
SEQ_printf(m, " .%-30s: %lu\n", "tg_load_avg_contrib",
573573
cfs_rq->tg_load_avg_contrib);

‎kernel/sched/fair.c‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,36 +3501,47 @@ static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
35013501
static inline int
35023502
update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
35033503
{
3504+
unsigned long removed_load = 0, removed_util = 0;
35043505
struct sched_avg *sa = &cfs_rq->avg;
3505-
int decayed, removed_load = 0, removed_util = 0;
3506+
int decayed = 0;
35063507

3507-
if (atomic_long_read(&cfs_rq->removed_load_avg)) {
3508-
s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0);
3508+
if (cfs_rq->removed.nr) {
3509+
unsigned long r;
3510+
3511+
raw_spin_lock(&cfs_rq->removed.lock);
3512+
swap(cfs_rq->removed.util_avg, removed_util);
3513+
swap(cfs_rq->removed.load_avg, removed_load);
3514+
cfs_rq->removed.nr = 0;
3515+
raw_spin_unlock(&cfs_rq->removed.lock);
3516+
3517+
/*
3518+
* The LOAD_AVG_MAX for _sum is a slight over-estimate,
3519+
* which is safe due to sub_positive() clipping at 0.
3520+
*/
3521+
r = removed_load;
35093522
sub_positive(&sa->load_avg, r);
35103523
sub_positive(&sa->load_sum, r * LOAD_AVG_MAX);
3511-
removed_load = 1;
3512-
set_tg_cfs_propagate(cfs_rq);
3513-
}
35143524

3515-
if (atomic_long_read(&cfs_rq->removed_util_avg)) {
3516-
long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0);
3525+
r = removed_util;
35173526
sub_positive(&sa->util_avg, r);
35183527
sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
3519-
removed_util = 1;
3528+
35203529
set_tg_cfs_propagate(cfs_rq);
3530+
3531+
decayed = 1;
35213532
}
35223533

3523-
decayed = __update_load_avg_cfs_rq(now, cpu_of(rq_of(cfs_rq)), cfs_rq);
3534+
decayed |= __update_load_avg_cfs_rq(now, cpu_of(rq_of(cfs_rq)), cfs_rq);
35243535

35253536
#ifndef CONFIG_64BIT
35263537
smp_wmb();
35273538
cfs_rq->load_last_update_time_copy = sa->last_update_time;
35283539
#endif
35293540

3530-
if (decayed || removed_util)
3541+
if (decayed)
35313542
cfs_rq_util_change(cfs_rq);
35323543

3533-
return decayed || removed_load;
3544+
return decayed;
35343545
}
35353546

35363547
/**
@@ -3645,6 +3656,7 @@ void sync_entity_load_avg(struct sched_entity *se)
36453656
void remove_entity_load_avg(struct sched_entity *se)
36463657
{
36473658
struct cfs_rq *cfs_rq = cfs_rq_of(se);
3659+
unsigned long flags;
36483660

36493661
/*
36503662
* tasks cannot exit without having gone through wake_up_new_task() ->
@@ -3654,11 +3666,19 @@ void remove_entity_load_avg(struct sched_entity *se)
36543666
* Similarly for groups, they will have passed through
36553667
* post_init_entity_util_avg() before unregister_sched_fair_group()
36563668
* calls this.
3669+
*
3670+
* XXX in case entity_is_task(se) && task_of(se)->on_rq == MIGRATING
3671+
* we could actually get the right time, since we're called with
3672+
* rq->lock held, see detach_task().
36573673
*/
36583674

36593675
sync_entity_load_avg(se);
3660-
atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg);
3661-
atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg);
3676+
3677+
raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags);
3678+
++cfs_rq->removed.nr;
3679+
cfs_rq->removed.util_avg += se->avg.util_avg;
3680+
cfs_rq->removed.load_avg += se->avg.load_avg;
3681+
raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags);
36623682
}
36633683

36643684
static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
@@ -9449,8 +9469,7 @@ void init_cfs_rq(struct cfs_rq *cfs_rq)
94499469
#ifdef CONFIG_FAIR_GROUP_SCHED
94509470
cfs_rq->propagate_avg = 0;
94519471
#endif
9452-
atomic_long_set(&cfs_rq->removed_load_avg, 0);
9453-
atomic_long_set(&cfs_rq->removed_util_avg, 0);
9472+
raw_spin_lock_init(&cfs_rq->removed.lock);
94549473
#endif
94559474
}
94569475

‎kernel/sched/sched.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,19 @@ struct cfs_rq {
445445
struct sched_avg avg;
446446
u64 runnable_load_sum;
447447
unsigned long runnable_load_avg;
448+
#ifndef CONFIG_64BIT
449+
u64 load_last_update_time_copy;
450+
#endif
448451
#ifdef CONFIG_FAIR_GROUP_SCHED
449452
unsigned long tg_load_avg_contrib;
450453
unsigned long propagate_avg;
451454
#endif
452-
atomic_long_t removed_load_avg, removed_util_avg;
453-
#ifndef CONFIG_64BIT
454-
u64 load_last_update_time_copy;
455-
#endif
455+
struct {
456+
raw_spinlock_t lock ____cacheline_aligned;
457+
int nr;
458+
unsigned long load_avg;
459+
unsigned long util_avg;
460+
} removed;
456461

457462
#ifdef CONFIG_FAIR_GROUP_SCHED
458463
/*

0 commit comments

Comments
 (0)