Skip to content

Commit 3a101d0

Browse files
committed
sched: adjust when cpu_active and cpuset configurations are updated during cpu on/offlining
Currently, when a cpu goes down, cpu_active is cleared before CPU_DOWN_PREPARE starts and cpuset configuration is updated from a default priority cpu notifier. When a cpu is coming up, it's set before CPU_ONLINE but cpuset configuration again is updated from the same cpu notifier. For cpu notifiers, this presents an inconsistent state. Threads which a CPU_DOWN_PREPARE notifier expects to be bound to the CPU can be migrated to other cpus because the cpu is no more inactive. Fix it by updating cpu_active in the highest priority cpu notifier and cpuset configuration in the second highest when a cpu is coming up. Down path is updated similarly. This guarantees that all other cpu notifiers see consistent cpu_active and cpuset configuration. cpuset_track_online_cpus() notifier is converted to cpuset_update_active_cpus() which just updates the configuration and now called from cpuset_cpu_[in]active() notifiers registered from sched_init_smp(). If cpuset is disabled, cpuset_update_active_cpus() degenerates into partition_sched_domains() making separate notifier for !CONFIG_CPUSETS unnecessary. This problem is triggered by cmwq. During CPU_DOWN_PREPARE, hotplug callback creates a kthread and kthread_bind()s it to the target cpu, and the thread is expected to run on that cpu. * Ingo's test discovered __cpuinit/exit markups were incorrect. Fixed. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Ingo Molnar <mingo@elte.hu> Cc: Paul Menage <menage@google.com>
1 parent 50a323b commit 3a101d0

5 files changed

Lines changed: 74 additions & 42 deletions

File tree

‎include/linux/cpu.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ struct notifier_block;
5252
* CPU notifier priorities.
5353
*/
5454
enum {
55+
/*
56+
* SCHED_ACTIVE marks a cpu which is coming up active during
57+
* CPU_ONLINE and CPU_DOWN_FAILED and must be the first
58+
* notifier. CPUSET_ACTIVE adjusts cpuset according to
59+
* cpu_active mask right after SCHED_ACTIVE. During
60+
* CPU_DOWN_PREPARE, SCHED_INACTIVE and CPUSET_INACTIVE are
61+
* ordered in the similar way.
62+
*
63+
* This ordering guarantees consistent cpu_active mask and
64+
* migration behavior to all cpu notifiers.
65+
*/
66+
CPU_PRI_SCHED_ACTIVE = INT_MAX,
67+
CPU_PRI_CPUSET_ACTIVE = INT_MAX - 1,
68+
CPU_PRI_SCHED_INACTIVE = INT_MIN + 1,
69+
CPU_PRI_CPUSET_INACTIVE = INT_MIN,
70+
5571
/* migration should happen before other stuff but after perf */
5672
CPU_PRI_PERF = 20,
5773
CPU_PRI_MIGRATION = 10,

‎include/linux/cpuset.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern int number_of_cpusets; /* How many cpusets are defined in system? */
2020

2121
extern int cpuset_init(void);
2222
extern void cpuset_init_smp(void);
23+
extern void cpuset_update_active_cpus(void);
2324
extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
2425
extern int cpuset_cpus_allowed_fallback(struct task_struct *p);
2526
extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
@@ -132,6 +133,11 @@ static inline void set_mems_allowed(nodemask_t nodemask)
132133
static inline int cpuset_init(void) { return 0; }
133134
static inline void cpuset_init_smp(void) {}
134135

136+
static inline void cpuset_update_active_cpus(void)
137+
{
138+
partition_sched_domains(1, NULL, NULL);
139+
}
140+
135141
static inline void cpuset_cpus_allowed(struct task_struct *p,
136142
struct cpumask *mask)
137143
{

‎kernel/cpu.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,8 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
235235
return -EINVAL;
236236

237237
cpu_hotplug_begin();
238-
set_cpu_active(cpu, false);
239238
err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
240239
if (err) {
241-
set_cpu_active(cpu, true);
242-
243240
nr_calls--;
244241
__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
245242
printk("%s: attempt to take down CPU %u failed\n",
@@ -249,7 +246,6 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
249246

250247
err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
251248
if (err) {
252-
set_cpu_active(cpu, true);
253249
/* CPU didn't die: tell everyone. Can't complain. */
254250
cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
255251

@@ -321,8 +317,6 @@ static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
321317
goto out_notify;
322318
BUG_ON(!cpu_online(cpu));
323319

324-
set_cpu_active(cpu, true);
325-
326320
/* Now call notifier in preparation. */
327321
cpu_notify(CPU_ONLINE | mod, hcpu);
328322

‎kernel/cpuset.c‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,31 +2113,17 @@ static void scan_for_empty_cpusets(struct cpuset *root)
21132113
* but making no active use of cpusets.
21142114
*
21152115
* This routine ensures that top_cpuset.cpus_allowed tracks
2116-
* cpu_online_map on each CPU hotplug (cpuhp) event.
2116+
* cpu_active_mask on each CPU hotplug (cpuhp) event.
21172117
*
21182118
* Called within get_online_cpus(). Needs to call cgroup_lock()
21192119
* before calling generate_sched_domains().
21202120
*/
2121-
static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
2122-
unsigned long phase, void *unused_cpu)
2121+
void __cpuexit cpuset_update_active_cpus(void)
21232122
{
21242123
struct sched_domain_attr *attr;
21252124
cpumask_var_t *doms;
21262125
int ndoms;
21272126

2128-
switch (phase) {
2129-
case CPU_ONLINE:
2130-
case CPU_ONLINE_FROZEN:
2131-
case CPU_DOWN_PREPARE:
2132-
case CPU_DOWN_PREPARE_FROZEN:
2133-
case CPU_DOWN_FAILED:
2134-
case CPU_DOWN_FAILED_FROZEN:
2135-
break;
2136-
2137-
default:
2138-
return NOTIFY_DONE;
2139-
}
2140-
21412127
cgroup_lock();
21422128
mutex_lock(&callback_mutex);
21432129
cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
@@ -2148,8 +2134,6 @@ static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
21482134

21492135
/* Have scheduler rebuild the domains */
21502136
partition_sched_domains(ndoms, doms, attr);
2151-
2152-
return NOTIFY_OK;
21532137
}
21542138

21552139
#ifdef CONFIG_MEMORY_HOTPLUG
@@ -2203,7 +2187,6 @@ void __init cpuset_init_smp(void)
22032187
cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
22042188
top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
22052189

2206-
hotcpu_notifier(cpuset_track_online_cpus, 0);
22072190
hotplug_memory_notifier(cpuset_track_online_nodes, 10);
22082191

22092192
cpuset_wq = create_singlethread_workqueue("cpuset");

‎kernel/sched.c‎

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,17 +5804,46 @@ static struct notifier_block __cpuinitdata migration_notifier = {
58045804
.priority = CPU_PRI_MIGRATION,
58055805
};
58065806

5807+
static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
5808+
unsigned long action, void *hcpu)
5809+
{
5810+
switch (action & ~CPU_TASKS_FROZEN) {
5811+
case CPU_ONLINE:
5812+
case CPU_DOWN_FAILED:
5813+
set_cpu_active((long)hcpu, true);
5814+
return NOTIFY_OK;
5815+
default:
5816+
return NOTIFY_DONE;
5817+
}
5818+
}
5819+
5820+
static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
5821+
unsigned long action, void *hcpu)
5822+
{
5823+
switch (action & ~CPU_TASKS_FROZEN) {
5824+
case CPU_DOWN_PREPARE:
5825+
set_cpu_active((long)hcpu, false);
5826+
return NOTIFY_OK;
5827+
default:
5828+
return NOTIFY_DONE;
5829+
}
5830+
}
5831+
58075832
static int __init migration_init(void)
58085833
{
58095834
void *cpu = (void *)(long)smp_processor_id();
58105835
int err;
58115836

5812-
/* Start one for the boot CPU: */
5837+
/* Initialize migration for the boot CPU */
58135838
err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
58145839
BUG_ON(err == NOTIFY_BAD);
58155840
migration_call(&migration_notifier, CPU_ONLINE, cpu);
58165841
register_cpu_notifier(&migration_notifier);
58175842

5843+
/* Register cpu active notifiers */
5844+
cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5845+
cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5846+
58185847
return 0;
58195848
}
58205849
early_initcall(migration_init);
@@ -7273,29 +7302,35 @@ int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
72737302
}
72747303
#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
72757304

7276-
#ifndef CONFIG_CPUSETS
72777305
/*
7278-
* Add online and remove offline CPUs from the scheduler domains.
7279-
* When cpusets are enabled they take over this function.
7306+
* Update cpusets according to cpu_active mask. If cpusets are
7307+
* disabled, cpuset_update_active_cpus() becomes a simple wrapper
7308+
* around partition_sched_domains().
72807309
*/
7281-
static int update_sched_domains(struct notifier_block *nfb,
7282-
unsigned long action, void *hcpu)
7310+
static int __cpuexit cpuset_cpu_active(struct notifier_block *nfb,
7311+
unsigned long action, void *hcpu)
72837312
{
7284-
switch (action) {
7313+
switch (action & ~CPU_TASKS_FROZEN) {
72857314
case CPU_ONLINE:
7286-
case CPU_ONLINE_FROZEN:
7287-
case CPU_DOWN_PREPARE:
7288-
case CPU_DOWN_PREPARE_FROZEN:
72897315
case CPU_DOWN_FAILED:
7290-
case CPU_DOWN_FAILED_FROZEN:
7291-
partition_sched_domains(1, NULL, NULL);
7316+
cpuset_update_active_cpus();
72927317
return NOTIFY_OK;
7318+
default:
7319+
return NOTIFY_DONE;
7320+
}
7321+
}
72937322

7323+
static int __cpuexit cpuset_cpu_inactive(struct notifier_block *nfb,
7324+
unsigned long action, void *hcpu)
7325+
{
7326+
switch (action & ~CPU_TASKS_FROZEN) {
7327+
case CPU_DOWN_PREPARE:
7328+
cpuset_update_active_cpus();
7329+
return NOTIFY_OK;
72947330
default:
72957331
return NOTIFY_DONE;
72967332
}
72977333
}
7298-
#endif
72997334

73007335
static int update_runtime(struct notifier_block *nfb,
73017336
unsigned long action, void *hcpu)
@@ -7341,10 +7376,8 @@ void __init sched_init_smp(void)
73417376
mutex_unlock(&sched_domains_mutex);
73427377
put_online_cpus();
73437378

7344-
#ifndef CONFIG_CPUSETS
7345-
/* XXX: Theoretical race here - CPU may be hotplugged now */
7346-
hotcpu_notifier(update_sched_domains, 0);
7347-
#endif
7379+
hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7380+
hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
73487381

73497382
/* RT runtime code needs to handle some hotplug events */
73507383
hotcpu_notifier(update_runtime, 0);

0 commit comments

Comments
 (0)