From: Anton Vorontsov <hidden> Date: 2012-04-23 07:08:00
Hi all,
This is another resend of several task->mm fixes, the bugs I found
during LMK code audit. Architectures were traverse the tasklist
in an unsafe manner, plus there are a few cases of unsafe access to
task->mm in general.
There were no objections on the previous resend, and the final words
were somewhere along "the patches are fine" line.
In v3:
- Dropped a controversal 'Make find_lock_task_mm() sparse-aware' patch;
- Reword arm and sh commit messages, per Oleg Nesterov's suggestions;
- Added an optimization trick in clear_tasks_mm_cpumask(): take only
the rcu read lock, no need for the whole tasklist_lock.
Suggested by Peter Zijlstra.
In v2:
- introduced a small helper in cpu.c: most arches duplicate the
same [buggy] code snippet, so it's better to fix it and move the
logic into a common function.
Thanks,
--
Anton Vorontsov
Email: cbouatmailru@gmail.com
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:09:26
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 <redacted>
---
include/linux/cpu.h | 1 +
kernel/cpu.c | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:09:47
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.
To fix this we would need to use find_lock_task_mm(), which would
walk up all threads and returns an appropriate task (with task
lock held).
clear_tasks_mm_cpumask() has this issue fixed, so let's use it.
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Anton Vorontsov <redacted>
---
arch/arm/kernel/smp.c | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:09:57
Current CPU hotplug code has some task->mm handling issues:
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).
We can't use get_task_mm()/mmput() pair as mmput() might sleep,
so we must take the task lock while handle its mm.
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.
To fix this we would need to use find_lock_task_mm(), which would
walk up all threads and returns an appropriate task (with task
lock held).
clear_tasks_mm_cpumask() has all the issues fixed, so let's use it.
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Anton Vorontsov <redacted>
---
arch/powerpc/mm/mmu_context_nohash.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
@@ -333,9 +333,7 @@ static int __cpuinit mmu_context_cpu_notify(struct notifier_block *self,unsignedlongaction,void*hcpu){unsignedintcpu=(unsignedint)(long)hcpu;-#ifdef CONFIG_HOTPLUG_CPU-structtask_struct*p;-#endif+/* We don't touch CPU 0 map, it's allocated at aboot and kept*aroundforever*/
@@ -358,12 +356,7 @@ static int __cpuinit mmu_context_cpu_notify(struct notifier_block *self,stale_map[cpu]=NULL;/* We also clear the cpu_vm_mask bits of CPUs going away */-read_lock(&tasklist_lock);-for_each_process(p){-if(p->mm)-cpumask_clear_cpu(cpu,mm_cpumask(p->mm));-}-read_unlock(&tasklist_lock);+clear_tasks_mm_cpumask(cpu);break;#endif /* CONFIG_HOTPLUG_CPU */}
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:10:10
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.
To fix this we would need to use find_lock_task_mm(), which would
walk up all threads and returns an appropriate task (with task
lock held).
clear_tasks_mm_cpumask() has the issue fixed, so let's use it.
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Anton Vorontsov <redacted>
---
arch/sh/kernel/smp.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:10:23
The patch fixes two problems:
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).
We can't use get_task_mm()/mmput() pair as mmput() might sleep,
so we have to take the task lock while handle its mm.
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.
To catch this we use find_lock_task_mm(), which walks up all
threads and returns an appropriate task (with task lock held).
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Anton Vorontsov <redacted>
---
arch/blackfin/kernel/trace.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:10:36
Oleg Nesterov found an interesting deadlock possibility:
sysrq_showregs_othercpus() does smp_call_function(showacpu)
and showacpu() show_stack()->decode_address(). Now suppose that IPI
interrupts the task holding read_lock(tasklist).
To fix this, blackfin should not grab the write_ variant of the
tasklist lock, read_ one is enough.
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Anton Vorontsov <redacted>
---
arch/blackfin/kernel/trace.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:10:45
Traversing the tasks requires holding tasklist_lock, otherwise it
is unsafe.
p.s. However, I'm not sure that calling os_kill_ptraced_process()
in the atomic context is correct. It seem to work, but please
take a closer look.
Signed-off-by: Anton Vorontsov <redacted>
---
arch/um/kernel/reboot.c | 3 +++
1 file changed, 3 insertions(+)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:10:55
Checking for task->mm is dangerous as ->mm might disappear (exit_mm()
assigns NULL under task_lock(), so tasklist lock is not enough).
We can't use get_task_mm()/mmput() pair as mmput() might sleep,
so let's take the task lock while we care about its mm.
Note that we should also use find_lock_task_mm() to check all process'
threads for a valid mm, but for uml we'll do it in a separate patch.
Signed-off-by: Anton Vorontsov <redacted>
---
arch/um/kernel/reboot.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
From: Anton Vorontsov <hidden> Date: 2012-04-23 07:11:05
kill_off_processes() might miss a valid process, this is because
checking for process->mm is not enough. Process' main thread may
exit or detach its mm via use_mm(), but other threads may still
have a valid mm.
To catch this we use find_lock_task_mm(), which walks up all
threads and returns an appropriate task (with task lock held).
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Anton Vorontsov <redacted>
---
arch/um/kernel/reboot.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
From: Richard Weinberger <richard@nod.at> Date: 2012-04-23 14:58:21
On 23.04.2012 09:09, Anton Vorontsov wrote:
Traversing the tasks requires holding tasklist_lock, otherwise it
is unsafe.
p.s. However, I'm not sure that calling os_kill_ptraced_process()
in the atomic context is correct. It seem to work, but please
take a closer look.
Signed-off-by: Anton Vorontsov<redacted>
---
You forgot my Ack and I've already explained why
os_kill_ptraced_process() is fine.
Thanks,
//richard
From: Anton Vorontsov <hidden> Date: 2012-04-23 15:41:47
On Mon, Apr 23, 2012 at 04:57:54PM +0200, Richard Weinberger wrote:
On 23.04.2012 09:09, Anton Vorontsov wrote:
quoted
Traversing the tasks requires holding tasklist_lock, otherwise it
is unsafe.
p.s. However, I'm not sure that calling os_kill_ptraced_process()
in the atomic context is correct. It seem to work, but please
take a closer look.
Signed-off-by: Anton Vorontsov<redacted>
---
You forgot my Ack and I've already explained why
os_kill_ptraced_process() is fine.
Ouch, sorry!
--
Anton Vorontsov
Email: cbouatmailru@gmail.com
From: Andrew Morton <akpm@linux-foundation.org> Date: 2012-04-26 23:59:16
On Mon, 23 Apr 2012 00:07:36 -0700
Anton Vorontsov [off-list ref] wrote:
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.
The operation of this function was presumably obvious to you at the
time you wrote it, but that isn't true of other people at later times.
Please document it?
+{
+ struct task_struct *p;
+
+ /*
+ * This function is called after the cpu is taken down and marked
+ * offline,
hm, well. Who said that this function will only ever be called
after that CPU was taken down? There is nothing in the function name
nor in the (absent) documentation which enforces this precondition.
If someone tries to use this function for a different purpose, or
copies-and-modifies it for a different purpose, we just shot them in
the foot.
They'd be pretty dumb to do that without reading the local comment,
but still...
so its not like new tasks will ever get this cpu set in
+ * their mm mask. -- Peter Zijlstra
+ * Thus, we may use rcu_read_lock() here, instead of grabbing
+ * full-fledged tasklist_lock.
+ */
+ rcu_read_lock();
+ for_each_process(p) {
+ struct task_struct *t;
+
+ t = find_lock_task_mm(p);
+ if (!t)
+ continue;
+ cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
+ task_unlock(t);
+ }
+ rcu_read_unlock();
+}
It is good that this code exists under CONFIG_HOTPLUG_CPU. Did you
check that everything works correctly with CONFIG_HOTPLUG_CPU=n?
From: Peter Zijlstra <hidden> Date: 2012-05-01 10:46:37
On Thu, 2012-04-26 at 16:59 -0700, Andrew Morton wrote:
quoted
+void clear_tasks_mm_cpumask(int cpu)
=20
The operation of this function was presumably obvious to you at the
time you wrote it, but that isn't true of other people at later times.
=20
Please document it?
=20
=20
quoted
+{
+ struct task_struct *p;
+
+ /*
+ * This function is called after the cpu is taken down and marked
+ * offline,
=20
hm, well. Who said that this function will only ever be called
after that CPU was taken down? There is nothing in the function name
nor in the (absent) documentation which enforces this precondition.
=20
If someone tries to use this function for a different purpose, or
copies-and-modifies it for a different purpose, we just shot them in
the foot.
=20
They'd be pretty dumb to do that without reading the local comment,
but still...
Methinks something simple like:
WARN_ON(cpu_online(cpu));
Ought to cure that worry, no? :-)
=20
quoted
so its not like new tasks will ever get this cpu set in
+ * their mm mask. -- Peter Zijlstra
+ * Thus, we may use rcu_read_lock() here, instead of grabbing
+ * full-fledged tasklist_lock.
+ */
+ rcu_read_lock();
+ for_each_process(p) {
+ struct task_struct *t;
+
+ t =3D find_lock_task_mm(p);
+ if (!t)
+ continue;
+ cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
+ task_unlock(t);
+ }
+ rcu_read_unlock();
+}=20
From: Anton Vorontsov <hidden> Date: 2012-05-05 01:48:48
On Thu, Apr 26, 2012 at 04:59:11PM -0700, Andrew Morton wrote:
[...]
quoted
so its not like new tasks will ever get this cpu set in
+ * their mm mask. -- Peter Zijlstra
+ * Thus, we may use rcu_read_lock() here, instead of grabbing
+ * full-fledged tasklist_lock.
+ */
+ rcu_read_lock();
+ for_each_process(p) {
+ struct task_struct *t;
+
+ t = find_lock_task_mm(p);
+ if (!t)
+ continue;
+ cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
+ task_unlock(t);
+ }
+ rcu_read_unlock();
+}
It is good that this code exists under CONFIG_HOTPLUG_CPU. Did you
check that everything works correctly with CONFIG_HOTPLUG_CPU=n?
Yeah, only the code under CONFIG_HOTPLUG_CPU calls the function, so
it should be all fine.
Thanks!
--
Anton Vorontsov
Email: cbouatmailru@gmail.com
From: Anton Vorontsov <hidden> Date: 2012-05-05 01:49:01
This patch adds more comments on clear_tasks_mm_cpumask, plus adds
a runtime check: the function is only suitable for offlined CPUs,
and if called inappropriately, the kernel should scream aloud.
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Suggested-by: Peter Zijlstra <redacted>
Signed-off-by: Anton Vorontsov <redacted>
---
On Tue, May 01, 2012 at 12:45:33PM +0200, Peter Zijlstra wrote:
On Thu, 2012-04-26 at 16:59 -0700, Andrew Morton wrote:
quoted
quoted
+void clear_tasks_mm_cpumask(int cpu)
The operation of this function was presumably obvious to you at the
time you wrote it, but that isn't true of other people at later times.
Please document it?
[...]
quoted
If someone tries to use this function for a different purpose, or
copies-and-modifies it for a different purpose, we just shot them in
the foot.
They'd be pretty dumb to do that without reading the local comment,
but still...
Methinks something simple like:
WARN_ON(cpu_online(cpu));
Ought to cure that worry, no? :-)
Yeah, this is all good ideas, thanks.
How about the following patch?
kernel/cpu.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
From: Mike Frysinger <hidden> Date: 2012-06-01 04:36:55
On Monday 23 April 2012 03:09:01 Anton Vorontsov wrote:
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).
that isn't a problem for this code as it specifically checks if it's in an
atomic section. if it is, then task->mm can't go away on us.
We can't use get_task_mm()/mmput() pair as mmput() might sleep,
so we have to take the task lock while handle its mm.
if we're not in an atomic section, then sleeping is fine.
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.
i don't think it matters for this code (per the reasons above).
To catch this we use find_lock_task_mm(), which walks up all
threads and returns an appropriate task (with task lock held).
certainly fine for the non-atomic code path. i guess we'll notice in crashes
if it causes a problem in atomic code paths as well.
-mike