Skip to content

Commit 0a2966b

Browse files
Christoph LameterLinus Torvalds
authored andcommitted
[PATCH] Fix longstanding load balancing bug in the scheduler
The scheduler will stop load balancing if the most busy processor contains processes pinned via processor affinity. The scheduler currently only does one search for busiest cpu. If it cannot pull any tasks away from the busiest cpu because they were pinned then the scheduler goes into a corner and sulks leaving the idle processors idle. F.e. If you have processor 0 busy running four tasks pinned via taskset, there are none on processor 1 and one just started two processes on processor 2 then the scheduler will not move one of the two processes away from processor 2. This patch fixes that issue by forcing the scheduler to come out of its corner and retrying the load balancing by considering other processors for load balancing. This patch was originally developed by John Hawkes and discussed at http://marc.theaimsgroup.com/?l=linux-kernel&m=113901368523205&w=2. I have removed extraneous material and gone back to equipping struct rq with the cpu the queue is associated with since this makes the patch much easier and it is likely that others in the future will have the same difficulty of figuring out which processor owns which runqueue. The overhead added through these patches is a single word on the stack if the kernel is configured to support 32 cpus or less (32 bit). For 32 bit environments the maximum number of cpus that can be configued is 255 which would result in the use of 32 bytes additional on the stack. On IA64 up to 1k cpus can be configured which will result in the use of 128 additional bytes on the stack. The maximum additional cache footprint is one cacheline. Typically memory use will be much less than a cacheline and the additional cpumask will be placed on the stack in a cacheline that already contains other local variable. Signed-off-by: Christoph Lameter <clameter@sgi.com> Cc: John Hawkes <hawkes@sgi.com> Cc: "Siddha, Suresh B" <suresh.b.siddha@intel.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Nick Piggin <nickpiggin@yahoo.com.au> Cc: Peter Williams <pwil3058@bigpond.net.au> Cc: <stable@kernel.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 656ddf7 commit 0a2966b

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

‎kernel/sched.c‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ struct rq {
238238
/* For active balancing */
239239
int active_balance;
240240
int push_cpu;
241+
int cpu; /* cpu of this runqueue */
241242

242243
struct task_struct *migration_thread;
243244
struct list_head migration_queue;
@@ -267,6 +268,15 @@ struct rq {
267268

268269
static DEFINE_PER_CPU(struct rq, runqueues);
269270

271+
static inline int cpu_of(struct rq *rq)
272+
{
273+
#ifdef CONFIG_SMP
274+
return rq->cpu;
275+
#else
276+
return 0;
277+
#endif
278+
}
279+
270280
/*
271281
* The domain tree (rq->sd) is protected by RCU's quiescent state transition.
272282
* See detach_destroy_domains: synchronize_sched for details.
@@ -2211,7 +2221,8 @@ static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
22112221
*/
22122222
static struct sched_group *
22132223
find_busiest_group(struct sched_domain *sd, int this_cpu,
2214-
unsigned long *imbalance, enum idle_type idle, int *sd_idle)
2224+
unsigned long *imbalance, enum idle_type idle, int *sd_idle,
2225+
cpumask_t *cpus)
22152226
{
22162227
struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
22172228
unsigned long max_load, avg_load, total_load, this_load, total_pwr;
@@ -2248,7 +2259,12 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
22482259
sum_weighted_load = sum_nr_running = avg_load = 0;
22492260

22502261
for_each_cpu_mask(i, group->cpumask) {
2251-
struct rq *rq = cpu_rq(i);
2262+
struct rq *rq;
2263+
2264+
if (!cpu_isset(i, *cpus))
2265+
continue;
2266+
2267+
rq = cpu_rq(i);
22522268

22532269
if (*sd_idle && !idle_cpu(i))
22542270
*sd_idle = 0;
@@ -2466,13 +2482,17 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
24662482
*/
24672483
static struct rq *
24682484
find_busiest_queue(struct sched_group *group, enum idle_type idle,
2469-
unsigned long imbalance)
2485+
unsigned long imbalance, cpumask_t *cpus)
24702486
{
24712487
struct rq *busiest = NULL, *rq;
24722488
unsigned long max_load = 0;
24732489
int i;
24742490

24752491
for_each_cpu_mask(i, group->cpumask) {
2492+
2493+
if (!cpu_isset(i, *cpus))
2494+
continue;
2495+
24762496
rq = cpu_rq(i);
24772497

24782498
if (rq->nr_running == 1 && rq->raw_weighted_load > imbalance)
@@ -2511,20 +2531,23 @@ static int load_balance(int this_cpu, struct rq *this_rq,
25112531
struct sched_group *group;
25122532
unsigned long imbalance;
25132533
struct rq *busiest;
2534+
cpumask_t cpus = CPU_MASK_ALL;
25142535

25152536
if (idle != NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
25162537
!sched_smt_power_savings)
25172538
sd_idle = 1;
25182539

25192540
schedstat_inc(sd, lb_cnt[idle]);
25202541

2521-
group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle);
2542+
redo:
2543+
group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
2544+
&cpus);
25222545
if (!group) {
25232546
schedstat_inc(sd, lb_nobusyg[idle]);
25242547
goto out_balanced;
25252548
}
25262549

2527-
busiest = find_busiest_queue(group, idle, imbalance);
2550+
busiest = find_busiest_queue(group, idle, imbalance, &cpus);
25282551
if (!busiest) {
25292552
schedstat_inc(sd, lb_nobusyq[idle]);
25302553
goto out_balanced;
@@ -2549,8 +2572,12 @@ static int load_balance(int this_cpu, struct rq *this_rq,
25492572
double_rq_unlock(this_rq, busiest);
25502573

25512574
/* All tasks on this runqueue were pinned by CPU affinity */
2552-
if (unlikely(all_pinned))
2575+
if (unlikely(all_pinned)) {
2576+
cpu_clear(cpu_of(busiest), cpus);
2577+
if (!cpus_empty(cpus))
2578+
goto redo;
25532579
goto out_balanced;
2580+
}
25542581
}
25552582

25562583
if (!nr_moved) {
@@ -2639,18 +2666,22 @@ load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
26392666
unsigned long imbalance;
26402667
int nr_moved = 0;
26412668
int sd_idle = 0;
2669+
cpumask_t cpus = CPU_MASK_ALL;
26422670

26432671
if (sd->flags & SD_SHARE_CPUPOWER && !sched_smt_power_savings)
26442672
sd_idle = 1;
26452673

26462674
schedstat_inc(sd, lb_cnt[NEWLY_IDLE]);
2647-
group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE, &sd_idle);
2675+
redo:
2676+
group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE,
2677+
&sd_idle, &cpus);
26482678
if (!group) {
26492679
schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]);
26502680
goto out_balanced;
26512681
}
26522682

2653-
busiest = find_busiest_queue(group, NEWLY_IDLE, imbalance);
2683+
busiest = find_busiest_queue(group, NEWLY_IDLE, imbalance,
2684+
&cpus);
26542685
if (!busiest) {
26552686
schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]);
26562687
goto out_balanced;
@@ -2668,6 +2699,12 @@ load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
26682699
minus_1_or_zero(busiest->nr_running),
26692700
imbalance, sd, NEWLY_IDLE, NULL);
26702701
spin_unlock(&busiest->lock);
2702+
2703+
if (!nr_moved) {
2704+
cpu_clear(cpu_of(busiest), cpus);
2705+
if (!cpus_empty(cpus))
2706+
goto redo;
2707+
}
26712708
}
26722709

26732710
if (!nr_moved) {
@@ -6747,6 +6784,7 @@ void __init sched_init(void)
67476784
rq->cpu_load[j] = 0;
67486785
rq->active_balance = 0;
67496786
rq->push_cpu = 0;
6787+
rq->cpu = i;
67506788
rq->migration_thread = NULL;
67516789
INIT_LIST_HEAD(&rq->migration_queue);
67526790
#endif

0 commit comments

Comments
 (0)