Merge tag 'pm-4.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[sfrench/cifs-2.6.git] / kernel / sched / fair.c
index d378d02fdfcb1eb14dd5036ee61b1bc37e300727..8bc0a883d19045145ad7b839c23ca2ec1dc96bfe 100644 (file)
@@ -806,7 +806,7 @@ void post_init_entity_util_avg(struct sched_entity *se)
                        /*
                         * For !fair tasks do:
                         *
-                       update_cfs_rq_load_avg(now, cfs_rq, false);
+                       update_cfs_rq_load_avg(now, cfs_rq);
                        attach_entity_load_avg(cfs_rq, se);
                        switched_from_fair(rq, p);
                         *
@@ -1071,6 +1071,29 @@ unsigned int sysctl_numa_balancing_scan_size = 256;
 /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
 unsigned int sysctl_numa_balancing_scan_delay = 1000;
 
+struct numa_group {
+       atomic_t refcount;
+
+       spinlock_t lock; /* nr_tasks, tasks */
+       int nr_tasks;
+       pid_t gid;
+       int active_nodes;
+
+       struct rcu_head rcu;
+       unsigned long total_faults;
+       unsigned long max_faults_cpu;
+       /*
+        * Faults_cpu is used to decide whether memory should move
+        * towards the CPU. As a consequence, these stats are weighted
+        * more by CPU use than by memory faults.
+        */
+       unsigned long *faults_cpu;
+       unsigned long faults[0];
+};
+
+static inline unsigned long group_faults_priv(struct numa_group *ng);
+static inline unsigned long group_faults_shared(struct numa_group *ng);
+
 static unsigned int task_nr_scan_windows(struct task_struct *p)
 {
        unsigned long rss = 0;
@@ -1107,13 +1130,47 @@ static unsigned int task_scan_min(struct task_struct *p)
        return max_t(unsigned int, floor, scan);
 }
 
+static unsigned int task_scan_start(struct task_struct *p)
+{
+       unsigned long smin = task_scan_min(p);
+       unsigned long period = smin;
+
+       /* Scale the maximum scan period with the amount of shared memory. */
+       if (p->numa_group) {
+               struct numa_group *ng = p->numa_group;
+               unsigned long shared = group_faults_shared(ng);
+               unsigned long private = group_faults_priv(ng);
+
+               period *= atomic_read(&ng->refcount);
+               period *= shared + 1;
+               period /= private + shared + 1;
+       }
+
+       return max(smin, period);
+}
+
 static unsigned int task_scan_max(struct task_struct *p)
 {
-       unsigned int smin = task_scan_min(p);
-       unsigned int smax;
+       unsigned long smin = task_scan_min(p);
+       unsigned long smax;
 
        /* Watch for min being lower than max due to floor calculations */
        smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
+
+       /* Scale the maximum scan period with the amount of shared memory. */
+       if (p->numa_group) {
+               struct numa_group *ng = p->numa_group;
+               unsigned long shared = group_faults_shared(ng);
+               unsigned long private = group_faults_priv(ng);
+               unsigned long period = smax;
+
+               period *= atomic_read(&ng->refcount);
+               period *= shared + 1;
+               period /= private + shared + 1;
+
+               smax = max(smax, period);
+       }
+
        return max(smin, smax);
 }
 
@@ -1129,26 +1186,6 @@ static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
        rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
 }
 
-struct numa_group {
-       atomic_t refcount;
-
-       spinlock_t lock; /* nr_tasks, tasks */
-       int nr_tasks;
-       pid_t gid;
-       int active_nodes;
-
-       struct rcu_head rcu;
-       unsigned long total_faults;
-       unsigned long max_faults_cpu;
-       /*
-        * Faults_cpu is used to decide whether memory should move
-        * towards the CPU. As a consequence, these stats are weighted
-        * more by CPU use than by memory faults.
-        */
-       unsigned long *faults_cpu;
-       unsigned long faults[0];
-};
-
 /* Shared or private faults. */
 #define NR_NUMA_HINT_FAULT_TYPES 2
 
@@ -1198,6 +1235,30 @@ static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
                group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
 }
 
+static inline unsigned long group_faults_priv(struct numa_group *ng)
+{
+       unsigned long faults = 0;
+       int node;
+
+       for_each_online_node(node) {
+               faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)];
+       }
+
+       return faults;
+}
+
+static inline unsigned long group_faults_shared(struct numa_group *ng)
+{
+       unsigned long faults = 0;
+       int node;
+
+       for_each_online_node(node) {
+               faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)];
+       }
+
+       return faults;
+}
+
 /*
  * A node triggering more than 1/3 as many NUMA faults as the maximum is
  * considered part of a numa group's pseudo-interleaving set. Migrations
@@ -1378,7 +1439,7 @@ bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
               group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
 }
 
-static unsigned long weighted_cpuload(const int cpu);
+static unsigned long weighted_cpuload(struct rq *rq);
 static unsigned long source_load(int cpu, int type);
 static unsigned long target_load(int cpu, int type);
 static unsigned long capacity_of(int cpu);
@@ -1409,7 +1470,7 @@ static void update_numa_stats(struct numa_stats *ns, int nid)
                struct rq *rq = cpu_rq(cpu);
 
                ns->nr_running += rq->nr_running;
-               ns->load += weighted_cpuload(cpu);
+               ns->load += weighted_cpuload(rq);
                ns->compute_capacity += capacity_of(cpu);
 
                cpus++;
@@ -1808,7 +1869,7 @@ static int task_numa_migrate(struct task_struct *p)
         * Reset the scan period if the task is being rescheduled on an
         * alternative node to recheck if the tasks is now properly placed.
         */
-       p->numa_scan_period = task_scan_min(p);
+       p->numa_scan_period = task_scan_start(p);
 
        if (env.best_task == NULL) {
                ret = migrate_task_to(p, env.best_cpu);
@@ -1892,7 +1953,7 @@ static void update_task_scan_period(struct task_struct *p,
                        unsigned long shared, unsigned long private)
 {
        unsigned int period_slot;
-       int ratio;
+       int lr_ratio, ps_ratio;
        int diff;
 
        unsigned long remote = p->numa_faults_locality[0];
@@ -1922,25 +1983,36 @@ static void update_task_scan_period(struct task_struct *p,
         *       >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
         */
        period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
-       ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
-       if (ratio >= NUMA_PERIOD_THRESHOLD) {
-               int slot = ratio - NUMA_PERIOD_THRESHOLD;
+       lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
+       ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared);
+
+       if (ps_ratio >= NUMA_PERIOD_THRESHOLD) {
+               /*
+                * Most memory accesses are local. There is no need to
+                * do fast NUMA scanning, since memory is already local.
+                */
+               int slot = ps_ratio - NUMA_PERIOD_THRESHOLD;
+               if (!slot)
+                       slot = 1;
+               diff = slot * period_slot;
+       } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) {
+               /*
+                * Most memory accesses are shared with other tasks.
+                * There is no point in continuing fast NUMA scanning,
+                * since other tasks may just move the memory elsewhere.
+                */
+               int slot = lr_ratio - NUMA_PERIOD_THRESHOLD;
                if (!slot)
                        slot = 1;
                diff = slot * period_slot;
        } else {
-               diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
-
                /*
-                * Scale scan rate increases based on sharing. There is an
-                * inverse relationship between the degree of sharing and
-                * the adjustment made to the scanning period. Broadly
-                * speaking the intent is that there is little point
-                * scanning faster if shared accesses dominate as it may
-                * simply bounce migrations uselessly
+                * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS,
+                * yet they are not on the local NUMA node. Speed up
+                * NUMA scanning to get the memory moved over.
                 */
-               ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1));
-               diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
+               int ratio = max(lr_ratio, ps_ratio);
+               diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
        }
 
        p->numa_scan_period = clamp(p->numa_scan_period + diff,
@@ -2448,7 +2520,7 @@ void task_numa_work(struct callback_head *work)
 
        if (p->numa_scan_period == 0) {
                p->numa_scan_period_max = task_scan_max(p);
-               p->numa_scan_period = task_scan_min(p);
+               p->numa_scan_period = task_scan_start(p);
        }
 
        next_scan = now + msecs_to_jiffies(p->numa_scan_period);
@@ -2576,7 +2648,7 @@ void task_tick_numa(struct rq *rq, struct task_struct *curr)
 
        if (now > curr->node_stamp + period) {
                if (!curr->node_stamp)
-                       curr->numa_scan_period = task_scan_min(curr);
+                       curr->numa_scan_period = task_scan_start(curr);
                curr->node_stamp += period;
 
                if (!time_before(jiffies, curr->mm->numa_next_scan)) {
@@ -2586,59 +2658,6 @@ void task_tick_numa(struct rq *rq, struct task_struct *curr)
        }
 }
 
-/*
- * Can a task be moved from prev_cpu to this_cpu without causing a load
- * imbalance that would trigger the load balancer?
- */
-static inline bool numa_wake_affine(struct sched_domain *sd,
-                                   struct task_struct *p, int this_cpu,
-                                   int prev_cpu, int sync)
-{
-       struct numa_stats prev_load, this_load;
-       s64 this_eff_load, prev_eff_load;
-
-       update_numa_stats(&prev_load, cpu_to_node(prev_cpu));
-       update_numa_stats(&this_load, cpu_to_node(this_cpu));
-
-       /*
-        * If sync wakeup then subtract the (maximum possible)
-        * effect of the currently running task from the load
-        * of the current CPU:
-        */
-       if (sync) {
-               unsigned long current_load = task_h_load(current);
-
-               if (this_load.load > current_load)
-                       this_load.load -= current_load;
-               else
-                       this_load.load = 0;
-       }
-
-       /*
-        * In low-load situations, where this_cpu's node is idle due to the
-        * sync cause above having dropped this_load.load to 0, move the task.
-        * Moving to an idle socket will not create a bad imbalance.
-        *
-        * Otherwise check if the nodes are near enough in load to allow this
-        * task to be woken on this_cpu's node.
-        */
-       if (this_load.load > 0) {
-               unsigned long task_load = task_h_load(p);
-
-               this_eff_load = 100;
-               this_eff_load *= prev_load.compute_capacity;
-
-               prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
-               prev_eff_load *= this_load.compute_capacity;
-
-               this_eff_load *= this_load.load + task_load;
-               prev_eff_load *= prev_load.load - task_load;
-
-               return this_eff_load <= prev_eff_load;
-       }
-
-       return true;
-}
 #else
 static void task_tick_numa(struct rq *rq, struct task_struct *curr)
 {
@@ -2652,14 +2671,6 @@ static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
 {
 }
 
-#ifdef CONFIG_SMP
-static inline bool numa_wake_affine(struct sched_domain *sd,
-                                   struct task_struct *p, int this_cpu,
-                                   int prev_cpu, int sync)
-{
-       return true;
-}
-#endif /* !SMP */
 #endif /* CONFIG_NUMA_BALANCING */
 
 static void
@@ -2790,6 +2801,31 @@ static inline void update_cfs_shares(struct sched_entity *se)
 }
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 
+static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
+{
+       struct rq *rq = rq_of(cfs_rq);
+
+       if (&rq->cfs == cfs_rq) {
+               /*
+                * There are a few boundary cases this might miss but it should
+                * get called often enough that that should (hopefully) not be
+                * a real problem -- added to that it only calls on the local
+                * CPU, so if we enqueue remotely we'll miss an update, but
+                * the next tick/schedule should update.
+                *
+                * It will not get called when we go idle, because the idle
+                * thread is a different class (!fair), nor will the utilization
+                * number include things like RT tasks.
+                *
+                * As is, the util number is not freq-invariant (we'd have to
+                * implement arch_scale_freq_capacity() for that).
+                *
+                * See cpu_util().
+                */
+               cpufreq_update_util(rq, 0);
+       }
+}
+
 #ifdef CONFIG_SMP
 /*
  * Approximate:
@@ -2967,6 +3003,18 @@ ___update_load_avg(u64 now, int cpu, struct sched_avg *sa,
 
        sa->last_update_time += delta << 10;
 
+       /*
+        * running is a subset of runnable (weight) so running can't be set if
+        * runnable is clear. But there are some corner cases where the current
+        * se has been already dequeued but cfs_rq->curr still points to it.
+        * This means that weight will be 0 but not running for a sched_entity
+        * but also for a cfs_rq if the latter becomes idle. As an example,
+        * this happens during idle_balance() which calls
+        * update_blocked_averages()
+        */
+       if (!weight)
+               running = 0;
+
        /*
         * Now we know we crossed measurement unit boundaries. The *_avg
         * accrues by two steps:
@@ -3276,31 +3324,6 @@ static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
 
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 
-static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
-{
-       struct rq *rq = rq_of(cfs_rq);
-
-       if (&rq->cfs == cfs_rq) {
-               /*
-                * There are a few boundary cases this might miss but it should
-                * get called often enough that that should (hopefully) not be
-                * a real problem -- added to that it only calls on the local
-                * CPU, so if we enqueue remotely we'll miss an update, but
-                * the next tick/schedule should update.
-                *
-                * It will not get called when we go idle, because the idle
-                * thread is a different class (!fair), nor will the utilization
-                * number include things like RT tasks.
-                *
-                * As is, the util number is not freq-invariant (we'd have to
-                * implement arch_scale_freq_capacity() for that).
-                *
-                * See cpu_util().
-                */
-               cpufreq_update_util(rq, 0);
-       }
-}
-
 /*
  * Unsigned subtract and clamp on underflow.
  *
@@ -3322,7 +3345,6 @@ static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
  * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
  * @now: current time, as per cfs_rq_clock_task()
  * @cfs_rq: cfs_rq to update
- * @update_freq: should we call cfs_rq_util_change() or will the call do so
  *
  * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
  * avg. The immediate corollary is that all (fair) tasks must be attached, see
@@ -3336,7 +3358,7 @@ static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
  * call update_tg_load_avg() when this function returns true.
  */
 static inline int
-update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
+update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
 {
        struct sched_avg *sa = &cfs_rq->avg;
        int decayed, removed_load = 0, removed_util = 0;
@@ -3364,7 +3386,7 @@ update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
        cfs_rq->load_last_update_time_copy = sa->last_update_time;
 #endif
 
-       if (update_freq && (decayed || removed_util))
+       if (decayed || removed_util)
                cfs_rq_util_change(cfs_rq);
 
        return decayed || removed_load;
@@ -3392,7 +3414,7 @@ static inline void update_load_avg(struct sched_entity *se, int flags)
        if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD))
                __update_load_avg_se(now, cpu, cfs_rq, se);
 
-       decayed  = update_cfs_rq_load_avg(now, cfs_rq, true);
+       decayed  = update_cfs_rq_load_avg(now, cfs_rq);
        decayed |= propagate_entity_load_avg(se);
 
        if (decayed && (flags & UPDATE_TG))
@@ -3536,7 +3558,7 @@ static int idle_balance(struct rq *this_rq, struct rq_flags *rf);
 #else /* CONFIG_SMP */
 
 static inline int
-update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
+update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
 {
        return 0;
 }
@@ -3546,7 +3568,7 @@ update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
 
 static inline void update_load_avg(struct sched_entity *se, int not_used1)
 {
-       cpufreq_update_util(rq_of(cfs_rq_of(se)), 0);
+       cfs_rq_util_change(cfs_rq_of(se));
 }
 
 static inline void
@@ -5127,9 +5149,9 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 }
 
 /* Used instead of source_load when we know the type == 0 */
-static unsigned long weighted_cpuload(const int cpu)
+static unsigned long weighted_cpuload(struct rq *rq)
 {
-       return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs);
+       return cfs_rq_runnable_load_avg(&rq->cfs);
 }
 
 #ifdef CONFIG_NO_HZ_COMMON
@@ -5174,7 +5196,7 @@ static void cpu_load_update_idle(struct rq *this_rq)
        /*
         * bail if there's load or we're actually up-to-date.
         */
-       if (weighted_cpuload(cpu_of(this_rq)))
+       if (weighted_cpuload(this_rq))
                return;
 
        cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
@@ -5195,7 +5217,7 @@ void cpu_load_update_nohz_start(void)
         * concurrently we'll exit nohz. And cpu_load write can race with
         * cpu_load_update_idle() but both updater would be writing the same.
         */
-       this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
+       this_rq->cpu_load[0] = weighted_cpuload(this_rq);
 }
 
 /*
@@ -5211,7 +5233,7 @@ void cpu_load_update_nohz_stop(void)
        if (curr_jiffies == this_rq->last_load_update_tick)
                return;
 
-       load = weighted_cpuload(cpu_of(this_rq));
+       load = weighted_cpuload(this_rq);
        rq_lock(this_rq, &rf);
        update_rq_clock(this_rq);
        cpu_load_update_nohz(this_rq, curr_jiffies, load);
@@ -5237,7 +5259,7 @@ static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
  */
 void cpu_load_update_active(struct rq *this_rq)
 {
-       unsigned long load = weighted_cpuload(cpu_of(this_rq));
+       unsigned long load = weighted_cpuload(this_rq);
 
        if (tick_nohz_tick_stopped())
                cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
@@ -5255,7 +5277,7 @@ void cpu_load_update_active(struct rq *this_rq)
 static unsigned long source_load(int cpu, int type)
 {
        struct rq *rq = cpu_rq(cpu);
-       unsigned long total = weighted_cpuload(cpu);
+       unsigned long total = weighted_cpuload(rq);
 
        if (type == 0 || !sched_feat(LB_BIAS))
                return total;
@@ -5270,7 +5292,7 @@ static unsigned long source_load(int cpu, int type)
 static unsigned long target_load(int cpu, int type)
 {
        struct rq *rq = cpu_rq(cpu);
-       unsigned long total = weighted_cpuload(cpu);
+       unsigned long total = weighted_cpuload(rq);
 
        if (type == 0 || !sched_feat(LB_BIAS))
                return total;
@@ -5292,7 +5314,7 @@ static unsigned long cpu_avg_load_per_task(int cpu)
 {
        struct rq *rq = cpu_rq(cpu);
        unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
-       unsigned long load_avg = weighted_cpuload(cpu);
+       unsigned long load_avg = weighted_cpuload(rq);
 
        if (nr_running)
                return load_avg / nr_running;
@@ -5347,20 +5369,115 @@ static int wake_wide(struct task_struct *p)
        return 1;
 }
 
+struct llc_stats {
+       unsigned long   nr_running;
+       unsigned long   load;
+       unsigned long   capacity;
+       int             has_capacity;
+};
+
+static bool get_llc_stats(struct llc_stats *stats, int cpu)
+{
+       struct sched_domain_shared *sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
+
+       if (!sds)
+               return false;
+
+       stats->nr_running       = READ_ONCE(sds->nr_running);
+       stats->load             = READ_ONCE(sds->load);
+       stats->capacity         = READ_ONCE(sds->capacity);
+       stats->has_capacity     = stats->nr_running < per_cpu(sd_llc_size, cpu);
+
+       return true;
+}
+
+/*
+ * Can a task be moved from prev_cpu to this_cpu without causing a load
+ * imbalance that would trigger the load balancer?
+ *
+ * Since we're running on 'stale' values, we might in fact create an imbalance
+ * but recomputing these values is expensive, as that'd mean iteration 2 cache
+ * domains worth of CPUs.
+ */
+static bool
+wake_affine_llc(struct sched_domain *sd, struct task_struct *p,
+               int this_cpu, int prev_cpu, int sync)
+{
+       struct llc_stats prev_stats, this_stats;
+       s64 this_eff_load, prev_eff_load;
+       unsigned long task_load;
+
+       if (!get_llc_stats(&prev_stats, prev_cpu) ||
+           !get_llc_stats(&this_stats, this_cpu))
+               return false;
+
+       /*
+        * If sync wakeup then subtract the (maximum possible)
+        * effect of the currently running task from the load
+        * of the current LLC.
+        */
+       if (sync) {
+               unsigned long current_load = task_h_load(current);
+
+               /* in this case load hits 0 and this LLC is considered 'idle' */
+               if (current_load > this_stats.load)
+                       return true;
+
+               this_stats.load -= current_load;
+       }
+
+       /*
+        * The has_capacity stuff is not SMT aware, but by trying to balance
+        * the nr_running on both ends we try and fill the domain at equal
+        * rates, thereby first consuming cores before siblings.
+        */
+
+       /* if the old cache has capacity, stay there */
+       if (prev_stats.has_capacity && prev_stats.nr_running < this_stats.nr_running+1)
+               return false;
+
+       /* if this cache has capacity, come here */
+       if (this_stats.has_capacity && this_stats.nr_running < prev_stats.nr_running+1)
+               return true;
+
+       /*
+        * Check to see if we can move the load without causing too much
+        * imbalance.
+        */
+       task_load = task_h_load(p);
+
+       this_eff_load = 100;
+       this_eff_load *= prev_stats.capacity;
+
+       prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
+       prev_eff_load *= this_stats.capacity;
+
+       this_eff_load *= this_stats.load + task_load;
+       prev_eff_load *= prev_stats.load - task_load;
+
+       return this_eff_load <= prev_eff_load;
+}
+
 static int wake_affine(struct sched_domain *sd, struct task_struct *p,
                       int prev_cpu, int sync)
 {
        int this_cpu = smp_processor_id();
-       bool affine = false;
+       bool affine;
 
        /*
-        * Common case: CPUs are in the same socket, and select_idle_sibling()
-        * will do its thing regardless of what we return:
+        * Default to no affine wakeups; wake_affine() should not effect a task
+        * placement the load-balancer feels inclined to undo. The conservative
+        * option is therefore to not move tasks when they wake up.
         */
-       if (cpus_share_cache(prev_cpu, this_cpu))
-               affine = true;
-       else
-               affine = numa_wake_affine(sd, p, this_cpu, prev_cpu, sync);
+       affine = false;
+
+       /*
+        * If the wakeup is across cache domains, try to evaluate if movement
+        * makes sense, otherwise rely on select_idle_siblings() to do
+        * placement inside the cache domain.
+        */
+       if (!cpus_share_cache(prev_cpu, this_cpu))
+               affine = wake_affine_llc(sd, p, this_cpu, prev_cpu, sync);
 
        schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
        if (affine) {
@@ -5552,7 +5669,7 @@ find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
                                shallowest_idle_cpu = i;
                        }
                } else if (shallowest_idle_cpu == -1) {
-                       load = weighted_cpuload(i);
+                       load = weighted_cpuload(cpu_rq(i));
                        if (load < min_load || (load == min_load && i == this_cpu)) {
                                min_load = load;
                                least_loaded_cpu = i;
@@ -6189,10 +6306,10 @@ pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf
        int new_tasks;
 
 again:
-#ifdef CONFIG_FAIR_GROUP_SCHED
        if (!cfs_rq->nr_running)
                goto idle;
 
+#ifdef CONFIG_FAIR_GROUP_SCHED
        if (prev->sched_class != &fair_sched_class)
                goto simple;
 
@@ -6222,11 +6339,17 @@ again:
                        /*
                         * This call to check_cfs_rq_runtime() will do the
                         * throttle and dequeue its entity in the parent(s).
-                        * Therefore the 'simple' nr_running test will indeed
+                        * Therefore the nr_running test will indeed
                         * be correct.
                         */
-                       if (unlikely(check_cfs_rq_runtime(cfs_rq)))
+                       if (unlikely(check_cfs_rq_runtime(cfs_rq))) {
+                               cfs_rq = &rq->cfs;
+
+                               if (!cfs_rq->nr_running)
+                                       goto idle;
+
                                goto simple;
+                       }
                }
 
                se = pick_next_entity(cfs_rq, curr);
@@ -6266,12 +6389,8 @@ again:
 
        return p;
 simple:
-       cfs_rq = &rq->cfs;
 #endif
 
-       if (!cfs_rq->nr_running)
-               goto idle;
-
        put_prev_task(rq, prev);
 
        do {
@@ -6919,7 +7038,7 @@ static void update_blocked_averages(int cpu)
                if (throttled_hierarchy(cfs_rq))
                        continue;
 
-               if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true))
+               if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq))
                        update_tg_load_avg(cfs_rq, 0);
 
                /* Propagate pending load changes to the parent, if any: */
@@ -6992,7 +7111,7 @@ static inline void update_blocked_averages(int cpu)
 
        rq_lock_irqsave(rq, &rf);
        update_rq_clock(rq);
-       update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true);
+       update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq);
        rq_unlock_irqrestore(rq, &rf);
 }
 
@@ -7038,6 +7157,7 @@ struct sg_lb_stats {
 struct sd_lb_stats {
        struct sched_group *busiest;    /* Busiest group in this sd */
        struct sched_group *local;      /* Local group in this sd */
+       unsigned long total_running;
        unsigned long total_load;       /* Total load of all groups in sd */
        unsigned long total_capacity;   /* Total capacity of all groups in sd */
        unsigned long avg_load; /* Average load across all groups in sd */
@@ -7057,6 +7177,7 @@ static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
        *sds = (struct sd_lb_stats){
                .busiest = NULL,
                .local = NULL,
+               .total_running = 0UL,
                .total_load = 0UL,
                .total_capacity = 0UL,
                .busiest_stat = {
@@ -7365,7 +7486,7 @@ static inline void update_sg_lb_stats(struct lb_env *env,
                sgs->nr_numa_running += rq->nr_numa_running;
                sgs->nr_preferred_running += rq->nr_preferred_running;
 #endif
-               sgs->sum_weighted_load += weighted_cpuload(i);
+               sgs->sum_weighted_load += weighted_cpuload(rq);
                /*
                 * No need to call idle_cpu() if nr_running is not 0
                 */
@@ -7492,6 +7613,7 @@ static inline enum fbq_type fbq_classify_rq(struct rq *rq)
  */
 static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
 {
+       struct sched_domain_shared *shared = env->sd->shared;
        struct sched_domain *child = env->sd->child;
        struct sched_group *sg = env->sd->groups;
        struct sg_lb_stats *local = &sds->local_stat;
@@ -7548,6 +7670,7 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
 
 next_group:
                /* Now, start updating sd_lb_stats */
+               sds->total_running += sgs->sum_nr_running;
                sds->total_load += sgs->group_load;
                sds->total_capacity += sgs->group_capacity;
 
@@ -7563,6 +7686,21 @@ next_group:
                        env->dst_rq->rd->overload = overload;
        }
 
+       if (!shared)
+               return;
+
+       /*
+        * Since these are sums over groups they can contain some CPUs
+        * multiple times for the NUMA domains.
+        *
+        * Currently only wake_affine_llc() and find_busiest_group()
+        * uses these numbers, only the last is affected by this problem.
+        *
+        * XXX fix that.
+        */
+       WRITE_ONCE(shared->nr_running,  sds->total_running);
+       WRITE_ONCE(shared->load,        sds->total_load);
+       WRITE_ONCE(shared->capacity,    sds->total_capacity);
 }
 
 /**
@@ -7792,6 +7930,7 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
        if (!sds.busiest || busiest->sum_nr_running == 0)
                goto out_balanced;
 
+       /* XXX broken for overlapping NUMA groups */
        sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
                                                / sds.total_capacity;
 
@@ -7894,7 +8033,7 @@ static struct rq *find_busiest_queue(struct lb_env *env,
 
                capacity = capacity_of(i);
 
-               wl = weighted_cpuload(i);
+               wl = weighted_cpuload(rq);
 
                /*
                 * When comparing with imbalance, use weighted_cpuload()