Skip to content

Commit 6a1bdc1

Browse files
oleg-nesterovIngo Molnar
authored andcommitted
sched: _cpu_down(): Don't play with current->cpus_allowed
_cpu_down() changes the current task's affinity and then recovers it at the end. The problems are well known: we can't restore old_allowed if it was bound to the now-dead-cpu, and we can race with the userspace which can change cpu-affinity during unplug. _cpu_down() should not play with current->cpus_allowed at all. Instead, take_cpu_down() can migrate the caller of _cpu_down() after __cpu_disable() removes the dying cpu from cpu_online_mask. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Acked-by: Rafael J. Wysocki <rjw@sisk.pl> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <20100315091023.GA9148@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 30da688 commit 6a1bdc1

3 files changed

Lines changed: 8 additions & 13 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,7 @@ extern void sched_clock_idle_sleep_event(void);
18431843
extern void sched_clock_idle_wakeup_event(u64 delta_ns);
18441844

18451845
#ifdef CONFIG_HOTPLUG_CPU
1846+
extern void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p);
18461847
extern void idle_task_exit(void);
18471848
#else
18481849
static inline void idle_task_exit(void) {}

‎kernel/cpu.c‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static inline void check_for_tasks(int cpu)
163163
}
164164

165165
struct take_cpu_down_param {
166+
struct task_struct *caller;
166167
unsigned long mod;
167168
void *hcpu;
168169
};
@@ -171,6 +172,7 @@ struct take_cpu_down_param {
171172
static int __ref take_cpu_down(void *_param)
172173
{
173174
struct take_cpu_down_param *param = _param;
175+
unsigned int cpu = (unsigned long)param->hcpu;
174176
int err;
175177

176178
/* Ensure this CPU doesn't handle any more interrupts. */
@@ -181,6 +183,8 @@ static int __ref take_cpu_down(void *_param)
181183
raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
182184
param->hcpu);
183185

186+
if (task_cpu(param->caller) == cpu)
187+
move_task_off_dead_cpu(cpu, param->caller);
184188
/* Force idle task to run as soon as we yield: it should
185189
immediately notice cpu is offline and die quickly. */
186190
sched_idle_next();
@@ -191,10 +195,10 @@ static int __ref take_cpu_down(void *_param)
191195
static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
192196
{
193197
int err, nr_calls = 0;
194-
cpumask_var_t old_allowed;
195198
void *hcpu = (void *)(long)cpu;
196199
unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
197200
struct take_cpu_down_param tcd_param = {
201+
.caller = current,
198202
.mod = mod,
199203
.hcpu = hcpu,
200204
};
@@ -205,9 +209,6 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
205209
if (!cpu_online(cpu))
206210
return -EINVAL;
207211

208-
if (!alloc_cpumask_var(&old_allowed, GFP_KERNEL))
209-
return -ENOMEM;
210-
211212
cpu_hotplug_begin();
212213
set_cpu_active(cpu, false);
213214
err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
@@ -224,10 +225,6 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
224225
goto out_release;
225226
}
226227

227-
/* Ensure that we are not runnable on dying cpu */
228-
cpumask_copy(old_allowed, &current->cpus_allowed);
229-
set_cpus_allowed_ptr(current, cpu_active_mask);
230-
231228
err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
232229
if (err) {
233230
set_cpu_active(cpu, true);
@@ -236,7 +233,7 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
236233
hcpu) == NOTIFY_BAD)
237234
BUG();
238235

239-
goto out_allowed;
236+
goto out_release;
240237
}
241238
BUG_ON(cpu_online(cpu));
242239

@@ -254,16 +251,13 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
254251

255252
check_for_tasks(cpu);
256253

257-
out_allowed:
258-
set_cpus_allowed_ptr(current, old_allowed);
259254
out_release:
260255
cpu_hotplug_done();
261256
if (!err) {
262257
if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
263258
hcpu) == NOTIFY_BAD)
264259
BUG();
265260
}
266-
free_cpumask_var(old_allowed);
267261
return err;
268262
}
269263

‎kernel/sched.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5442,7 +5442,7 @@ static int migration_thread(void *data)
54425442
/*
54435443
* Figure out where task on dead CPU should go, use force if necessary.
54445444
*/
5445-
static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5445+
void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
54465446
{
54475447
struct rq *rq = cpu_rq(dead_cpu);
54485448
int needs_cpu, uninitialized_var(dest_cpu);

0 commit comments

Comments
 (0)