Skip to content

Commit cb79295

Browse files
Anton Vorontsovtorvalds
authored andcommitted
cpu: introduce clear_tasks_mm_cpumask() helper
Many architectures clear tasks' mm_cpumask like this: read_lock(&tasklist_lock); for_each_process(p) { if (p->mm) cpumask_clear_cpu(cpu, mm_cpumask(p->mm)); } read_unlock(&tasklist_lock); Depending on the context, the code above may have several problems, such as: 1. Working with task->mm w/o getting mm or grabing the task lock is dangerous as ->mm might disappear (exit_mm() assigns NULL under task_lock(), so tasklist lock is not enough). 2. Checking for process->mm is not enough because process' main thread may exit or detach its mm via use_mm(), but other threads may still have a valid mm. This patch implements a small helper function that does things correctly, i.e.: 1. We take the task's lock while whe handle its mm (we can't use get_task_mm()/mmput() pair as mmput() might sleep); 2. To catch exited main thread case, we use find_lock_task_mm(), which walks up all threads and returns an appropriate task (with task lock held). Also, Per Peter Zijlstra's idea, now we don't grab tasklist_lock in the new helper, instead we take the rcu read lock. We can do this because the function is called after the cpu is taken down and marked offline, so no new tasks will get this cpu set in their mm mask. Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org> Cc: Richard Weinberger <richard@nod.at> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Russell King <rmk@arm.linux.org.uk> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Mike Frysinger <vapier@gentoo.org> Cc: Paul Mundt <lethal@linux-sh.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f7505d6 commit cb79295

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎include/linux/cpu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ extern void put_online_cpus(void);
177177
#define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
178178
#define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
179179
#define unregister_hotcpu_notifier(nb) unregister_cpu_notifier(nb)
180+
void clear_tasks_mm_cpumask(int cpu);
180181
int cpu_down(unsigned int cpu);
181182

182183
#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE

‎kernel/cpu.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <linux/sched.h>
1111
#include <linux/unistd.h>
1212
#include <linux/cpu.h>
13+
#include <linux/oom.h>
14+
#include <linux/rcupdate.h>
1315
#include <linux/export.h>
1416
#include <linux/kthread.h>
1517
#include <linux/stop_machine.h>
@@ -173,6 +175,30 @@ void __ref unregister_cpu_notifier(struct notifier_block *nb)
173175
}
174176
EXPORT_SYMBOL(unregister_cpu_notifier);
175177

178+
void clear_tasks_mm_cpumask(int cpu)
179+
{
180+
struct task_struct *p;
181+
182+
/*
183+
* This function is called after the cpu is taken down and marked
184+
* offline, so its not like new tasks will ever get this cpu set in
185+
* their mm mask. -- Peter Zijlstra
186+
* Thus, we may use rcu_read_lock() here, instead of grabbing
187+
* full-fledged tasklist_lock.
188+
*/
189+
rcu_read_lock();
190+
for_each_process(p) {
191+
struct task_struct *t;
192+
193+
t = find_lock_task_mm(p);
194+
if (!t)
195+
continue;
196+
cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
197+
task_unlock(t);
198+
}
199+
rcu_read_unlock();
200+
}
201+
176202
static inline void check_for_tasks(int cpu)
177203
{
178204
struct task_struct *p;

0 commit comments

Comments
 (0)