Skip to content

Commit fe8a45d

Browse files
committed
Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull scheduler fixes from Ingo Molnar: "Four bugfixes and one performance fix" * 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: sched/fair: Avoid integer overflow sched: Optimize task_sched_runtime() sched/numa: Cure update_numa_stats() vs. hotplug sched/numa: Fix NULL pointer dereference in task_numa_migrate() sched: Fix endless sync_sched/rcu() loop inside _cpu_down()
2 parents 5e30025 + 85b088e commit fe8a45d

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎kernel/cpu.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
306306
__func__, cpu);
307307
goto out_release;
308308
}
309-
smpboot_park_threads(cpu);
310309

311310
/*
312311
* By now we've cleared cpu_active_mask, wait for all preempt-disabled
@@ -315,12 +314,16 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
315314
*
316315
* For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
317316
* not imply sync_sched(), so explicitly call both.
317+
*
318+
* Do sync before park smpboot threads to take care the rcu boost case.
318319
*/
319320
#ifdef CONFIG_PREEMPT
320321
synchronize_sched();
321322
#endif
322323
synchronize_rcu();
323324

325+
smpboot_park_threads(cpu);
326+
324327
/*
325328
* So now all preempt/rcu users must observe !cpu_active().
326329
*/

‎kernel/sched/core.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,20 @@ unsigned long long task_sched_runtime(struct task_struct *p)
22532253
struct rq *rq;
22542254
u64 ns = 0;
22552255

2256+
#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2257+
/*
2258+
* 64-bit doesn't need locks to atomically read a 64bit value.
2259+
* So we have a optimization chance when the task's delta_exec is 0.
2260+
* Reading ->on_cpu is racy, but this is ok.
2261+
*
2262+
* If we race with it leaving cpu, we'll take a lock. So we're correct.
2263+
* If we race with it entering cpu, unaccounted time is 0. This is
2264+
* indistinguishable from the read occurring a few cycles earlier.
2265+
*/
2266+
if (!p->on_cpu)
2267+
return p->se.sum_exec_runtime;
2268+
#endif
2269+
22562270
rq = task_rq_lock(p, &flags);
22572271
ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
22582272
task_rq_unlock(rq, p, &flags);

‎kernel/sched/fair.c‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ struct numa_stats {
10001000
*/
10011001
static void update_numa_stats(struct numa_stats *ns, int nid)
10021002
{
1003-
int cpu;
1003+
int cpu, cpus = 0;
10041004

10051005
memset(ns, 0, sizeof(*ns));
10061006
for_each_cpu(cpu, cpumask_of_node(nid)) {
@@ -1009,8 +1009,21 @@ static void update_numa_stats(struct numa_stats *ns, int nid)
10091009
ns->nr_running += rq->nr_running;
10101010
ns->load += weighted_cpuload(cpu);
10111011
ns->power += power_of(cpu);
1012+
1013+
cpus++;
10121014
}
10131015

1016+
/*
1017+
* If we raced with hotplug and there are no CPUs left in our mask
1018+
* the @ns structure is NULL'ed and task_numa_compare() will
1019+
* not find this node attractive.
1020+
*
1021+
* We'll either bail at !has_capacity, or we'll detect a huge imbalance
1022+
* and bail there.
1023+
*/
1024+
if (!cpus)
1025+
return;
1026+
10141027
ns->load = (ns->load * SCHED_POWER_SCALE) / ns->power;
10151028
ns->capacity = DIV_ROUND_CLOSEST(ns->power, SCHED_POWER_SCALE);
10161029
ns->has_capacity = (ns->nr_running < ns->capacity);
@@ -1201,9 +1214,21 @@ static int task_numa_migrate(struct task_struct *p)
12011214
*/
12021215
rcu_read_lock();
12031216
sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
1204-
env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
1217+
if (sd)
1218+
env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
12051219
rcu_read_unlock();
12061220

1221+
/*
1222+
* Cpusets can break the scheduler domain tree into smaller
1223+
* balance domains, some of which do not cross NUMA boundaries.
1224+
* Tasks that are "trapped" in such domains cannot be migrated
1225+
* elsewhere, so there is no point in (re)trying.
1226+
*/
1227+
if (unlikely(!sd)) {
1228+
p->numa_preferred_nid = cpu_to_node(task_cpu(p));
1229+
return -EINVAL;
1230+
}
1231+
12071232
taskweight = task_weight(p, env.src_nid);
12081233
groupweight = group_weight(p, env.src_nid);
12091234
update_numa_stats(&env.src_stats, env.src_nid);
@@ -2153,7 +2178,7 @@ static inline void __update_tg_runnable_avg(struct sched_avg *sa,
21532178
long contrib;
21542179

21552180
/* The fraction of a cpu used by this cfs_rq */
2156-
contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
2181+
contrib = div_u64((u64)sa->runnable_avg_sum << NICE_0_SHIFT,
21572182
sa->runnable_avg_period + 1);
21582183
contrib -= cfs_rq->tg_runnable_contrib;
21592184

0 commit comments

Comments
 (0)