Skip to contentSkip to conversation

Fixes for common mistakes w/ for_each_process and task->mm

PATCH Superseded Anton Vorontsov 3 participants 18 messages Feb 7, 2012 – Feb 10, 2012

Subsystems (6)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fixes for common mistakes w/ for_each_process and task->mm

Hi all,

While working on lowmemorykiller driver, I stumbled upon several places where we traverse the tasklist in an unsafe manner, plus there are a few cases of unsafe access to task->mm.

Note that some patches were not tested (e.g. sh, blackfin), so please take a closer look if there's silly mistakes. Also special attention needed for patch 7/8 (UML specific).

Oleg,

For sysrq case I kept the force_sig() usage, this is because in sysrq case I belive we do want to kill PID namespace init processes. If using force_sig() is still a bad idea, I guess we should fix it somehow else.

Thanks.

Signature · 2 lines
Anton Vorontsov
Email: [email protected]
#1 lore raw

PATCH 1/8 sysrq: Fix possible race with exiting task

sysrq should grab the tasklist lock, otherwise calling force_sig() is not safe, as it might race with exiting task, which ->sighand might be set to NULL already.

---
 drivers/tty/sysrq.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/tty/sysrq.c +2 -0
index 7867b7c..a1bcad7 100644
@@ -322,11 +322,13 @@ static void send_sig_all(int sig)
 {
 	struct task_struct *p;
 
+	read_lock(&tasklist_lock);
 	for_each_process(p) {
 		if (p->mm && !is_global_init(p))
 			/* Not swapper, init nor kernel thread */
 			force_sig(sig, p);
 	}
+	read_unlock(&tasklist_lock);
 }
 
 static void sysrq_handle_term(int key)
Signature · 1 line
1.7.7.6
#3 lore raw

PATCH 2/8 sysrq: Properly check for kernel threads

In reply to Anton Vorontsov

There's a real possibility of killing kernel threads that might have issued use_mm(), so kthread's mm might become non-NULL.

This patch fixes the issue by checking for PF_KTHREAD (just as get_task_mm()).

---
 drivers/tty/sysrq.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
drivers/tty/sysrq.c +6 -3
index a1bcad7..8db9125 100644
@@ -324,9 +324,12 @@ static void send_sig_all(int sig)
 
 	read_lock(&tasklist_lock);
 	for_each_process(p) {
-		if (p->mm && !is_global_init(p))
-			/* Not swapper, init nor kernel thread */
-			force_sig(sig, p);
+		if (p->flags & PF_KTHREAD)
+			continue;
+		if (is_global_init(p))
+			continue;
+
+		force_sig(sig, p);
 	}
 	read_unlock(&tasklist_lock);
 }
Signature · 1 line
1.7.7.6
#5 lore raw

PATCH 3/8 arm: Fix possible race on task->mm

In reply to Anton Vorontsov

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.

---
 arch/arm/kernel/smp.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/arm/kernel/smp.c +2 -0
index 57db122..85db3f2 100644
@@ -168,8 +168,10 @@ int __cpu_disable(void)
 
 	read_lock(&tasklist_lock);
 	for_each_process(p) {
+		task_lock(p);
 		if (p->mm)
 			cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
+		task_unlock(p);
 	}
 	read_unlock(&tasklist_lock);
 
Signature · 1 line
1.7.7.6
#11 lore raw

PATCH 4/8 powerpc/mm: Fix possible race on task->mm

In reply to Anton Vorontsov

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.

---
 arch/powerpc/mm/mmu_context_nohash.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/powerpc/mm/mmu_context_nohash.c +3 -0
index 5b63bd3..13ec484 100644
@@ -43,6 +43,7 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/init.h>
+#include <linux/sched.h>
 #include <linux/spinlock.h>
 #include <linux/bootmem.h>
 #include <linux/notifier.h>
@@ -360,8 +361,10 @@ static int __cpuinit mmu_context_cpu_notify(struct notifier_block *self,
 		/* We also clear the cpu_vm_mask bits of CPUs going away */
 		read_lock(&tasklist_lock);
 		for_each_process(p) {
+			task_lock(p);
 			if (p->mm)
 				cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
+			task_unlock(p);
 		}
 		read_unlock(&tasklist_lock);
 	break;
Signature · 1 line
1.7.7.6
#12 lore raw

PATCH 5/8 sh: Fix possible race on task->mm

In reply to Anton Vorontsov

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.

---
 arch/sh/kernel/smp.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/sh/kernel/smp.c +4 -1
index 3147a9a..11d29dc 100644
@@ -154,9 +154,12 @@ int __cpu_disable(void)
 	local_flush_tlb_all();
 
 	read_lock(&tasklist_lock);
-	for_each_process(p)
+	for_each_process(p) {
+		task_lock(p);
 		if (p->mm)
 			cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
+		task_unlock(p);
+	}
 	read_unlock(&tasklist_lock);
 
 	return 0;
Signature · 1 line
1.7.7.6
#13 lore raw

PATCH 6/8 blackfin: Fix possible race on task->mm

In reply to Anton Vorontsov

Even when in atomic context, grabbing irqsave variant of tasklist lock is not enough to protect task->mm from disappearing on SMP machines. Instead, we have to grab the task lock.

---

p.s. I don't have blackfin toolchain handy, so I did not build-test the patch.

 arch/blackfin/kernel/trace.c |   21 +++++++++------------
 1 files changed, 9 insertions(+), 12 deletions(-)
arch/blackfin/kernel/trace.c +9 -12
index 050db44..19bc02a 100644
@@ -10,6 +10,7 @@
 #include <linux/hardirq.h>
 #include <linux/thread_info.h>
 #include <linux/mm.h>
+#include <linux/sched.h>
 #include <linux/uaccess.h>
 #include <linux/module.h>
 #include <linux/kallsyms.h>
@@ -27,7 +28,6 @@ void decode_address(char *buf, unsigned long address)
 	struct task_struct *p;
 	struct mm_struct *mm;
 	unsigned long flags, offset;
-	unsigned char in_atomic = (bfin_read_IPEND() & 0x10) || in_atomic();
 	struct rb_node *n;
 
 #ifdef CONFIG_KALLSYMS
@@ -113,15 +113,13 @@ void decode_address(char *buf, unsigned long address)
 	 */
 	write_lock_irqsave(&tasklist_lock, flags);
 	for_each_process(p) {
-		mm = (in_atomic ? p->mm : get_task_mm(p));
+		task_lock(p);
+		mm = p->mm;
 		if (!mm)
-			continue;
+			goto __continue;
 
-		if (!down_read_trylock(&mm->mmap_sem)) {
-			if (!in_atomic)
-				mmput(mm);
-			continue;
-		}
+		if (!down_read_trylock(&mm->mmap_sem))
+			goto __continue;
 
 		for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
 			struct vm_area_struct *vma;
@@ -163,8 +161,7 @@ void decode_address(char *buf, unsigned long address)
 						name, vma->vm_start, vma->vm_end);
 
 				up_read(&mm->mmap_sem);
-				if (!in_atomic)
-					mmput(mm);
+				task_unlock(p);
 
 				if (buf[0] == '\0')
 					sprintf(buf, "[ %s ] dynamic memory", name);
@@ -174,8 +171,8 @@ void decode_address(char *buf, unsigned long address)
 		}
 
 		up_read(&mm->mmap_sem);
-		if (!in_atomic)
-			mmput(mm);
+__continue:
+		task_unlock(p);
 	}
 
 	/*
Signature · 1 line
1.7.7.6
#15 lore raw

PATCH 7/8 um: Should hold tasklist_lock while traversing processes

In reply to Anton Vorontsov

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.

 arch/um/kernel/reboot.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/um/kernel/reboot.c +3 -0
index 4d93dff..66d754c 100644
@@ -4,6 +4,7 @@
  */
 
 #include "linux/sched.h"
+#include "linux/spinlock.h"
 #include "linux/slab.h"
 #include "kern_util.h"
 #include "os.h"
@@ -22,6 +23,7 @@ static void kill_off_processes(void)
 		struct task_struct *p;
 		int pid;
 
+		read_lock(&tasklist_lock);
 		for_each_process(p) {
 			if (p->mm == NULL)
 				continue;
@@ -29,6 +31,7 @@ static void kill_off_processes(void)
 			pid = p->mm->context.id.u.pid;
 			os_kill_ptraced_process(pid, 1);
 		}
+		read_unlock(&tasklist_lock);
 	}
 }
 
Signature · 1 line
1.7.7.6
#16 lore raw

PATCH 8/8 um: Fix possible race on task->mm

In reply to Anton Vorontsov

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.

---
 arch/um/kernel/reboot.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
arch/um/kernel/reboot.c +5 -2
index 66d754c..1411f4e 100644
@@ -25,10 +25,13 @@ static void kill_off_processes(void)
 
 		read_lock(&tasklist_lock);
 		for_each_process(p) {
-			if (p->mm == NULL)
+			task_lock(p);
+			if (!p->mm) {
+				task_unlock(p);
 				continue;
-
+			}
 			pid = p->mm->context.id.u.pid;
+			task_unlock(p);
 			os_kill_ptraced_process(pid, 1);
 		}
 		read_unlock(&tasklist_lock);
Signature · 1 line
1.7.7.6
#17 lore raw
In reply to Anton Vorontsov
Quoted 3 lines · Anton Vorontsov

For sysrq case I kept the force_sig() usage, this is because in sysrq case I belive we do want to kill PID namespace init processes.

Agreed,

Quoted 3 lines

If using
force_sig() is still a bad idea, I guess we should fix it somehow else.

Yes, I think it is simply wrong here. And I think that force_* should not take the "struct task_struct *" argument, it should be used for synchronous signals only.

I am thinking about the patch below. With this patch send_sig_all() can use send_sig_info(SEND_SIG_FORCED). And probably send_sig_all() doesn't need tasklist in this case.

I'll recheck this but I need to disappear until Friday, sorry.

Oleg.

kernel/signal.c +8 -8
@@ -58,21 +58,20 @@ static int sig_handler_ignored(void __us
 		(handler == SIG_DFL && sig_kernel_ignore(sig));
 }
 
-static int sig_task_ignored(struct task_struct *t, int sig,
-		int from_ancestor_ns)
+static int sig_task_ignored(struct task_struct *t, int sig, bool force)
 {
 	void __user *handler;
 
 	handler = sig_handler(t, sig);
 
 	if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
-			handler == SIG_DFL && !from_ancestor_ns)
+			handler == SIG_DFL && !force)
 		return 1;
 
 	return sig_handler_ignored(handler, sig);
 }
 
-static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
+static int sig_ignored(struct task_struct *t, int sig, bool force)
 {
 	/*
 	 * Blocked signals are never ignored, since the
@@ -82,7 +81,7 @@ static int sig_ignored(struct task_struc
 	if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
 		return 0;
 
-	if (!sig_task_ignored(t, sig, from_ancestor_ns))
+	if (!sig_task_ignored(t, sig, force))
 		return 0;
 
 	/*
@@ -855,7 +854,7 @@ static void ptrace_trap_notify(struct ta
  * Returns true if the signal should be actually delivered, otherwise
  * it should be dropped.
  */
-static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
+static int prepare_signal(int sig, struct task_struct *p, bool force)
 {
 	struct signal_struct *signal = p->signal;
 	struct task_struct *t;
@@ -915,7 +914,7 @@ static int prepare_signal(int sig, struc
 		}
 	}
 
-	return !sig_ignored(p, sig, from_ancestor_ns);
+	return !sig_ignored(p, sig, force);
 }
 
 /*
@@ -1059,7 +1058,8 @@ static int __send_signal(int sig, struct
 
 	assert_spin_locked(&t->sighand->siglock);
 
-	if (!prepare_signal(sig, t, from_ancestor_ns))
+	if (!prepare_signal(sig, t,
+			from_ancestor_ns || (info == SEND_SIG_FORCED)))
 		return 0;
 
 	pending = group ? &t->signal->shared_pending : &t->pending;
#6 lore raw

Re: [PATCH 3/8] arm: Fix possible race on task->mm

In reply to Anton Vorontsov
Quoted 5 lines · Anton Vorontsov

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,

Yes, but

Quoted 1 line

so let's take the task lock while we care about its mm.

it seems that this needs find_lock_task_mm() too ?

the same for the rest patches in this series.

Oleg.

#14 lore raw

Re: [PATCH 6/8] blackfin: Fix possible race on task->mm

In reply to Anton Vorontsov
Quoted 4 lines · Anton Vorontsov

Even when in atomic context, grabbing irqsave variant of tasklist lock is not enough to protect task->mm from disappearing on SMP machines. Instead, we have to grab the task lock.

Yes, but afaics there is no reason for write_lock_irqsave(tasklist). read_lock() should be enough.

I know nothing about arch/blackfin/ but in fact this looks simply wrong.

For example. 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).

Mike?

Oleg.

Acked-by on v1 2/8
#4 lore raw

Re: [PATCH 2/8] sysrq: Properly check for kernel threads

In reply to Anton Vorontsov
Quoted 8 lines · Anton Vorontsov

There's a real possibility of killing kernel threads that might have issued use_mm(), so kthread's mm might become non-NULL.

This patch fixes the issue by checking for PF_KTHREAD (just as get_task_mm()).

Definitely needed.

Acked-by on v1 3/8
#10 lore raw

Re: [PATCH 3/8] arm: Fix possible race on task->mm

In reply to Anton Vorontsov
Quoted 7 lines · Anton Vorontsov

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.

#7 lore raw

Re: [PATCH 3/8] arm: Fix possible race on task->mm

In reply to Oleg Nesterov
Quoted 7 lines · Oleg Nesterov
Quoted 5 lines · Anton Vorontsov

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,

Quoted 7 lines

Yes, but

Quoted 1 line

so let's take the task lock while we care about its mm.

it seems that this needs find_lock_task_mm() too ?

the same for the rest patches in this series.

Yep, I think you're right, will add this change.

Signature · 2 lines
Anton Vorontsov
Email: [email protected]
#8 lore raw

Re: [PATCH 3/8] arm: Fix possible race on task->mm

Quoted 10 lines · Anton Vorontsov
Quoted 9 lines · Oleg Nesterov
Quoted 5 lines · Anton Vorontsov

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,

Yes, but

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

so let's take the task lock while we care about its mm.

it seems that this needs find_lock_task_mm() too ?

the same for the rest patches in this series.

Yep, I think you're right, will add this change.

Thinking about it more... making the code use find_lock_task_mm would be a behaviour change. Sure, in trivial cases like ARM this looks like a 100% safe thing to do, but in e.g. UML case, I wouldn't bet much money on that 'mm->context.id.u.pid' would be still meaningful.

So, I'd rather do it in a separate change, so this can be easily reverted.

Signature · 2 lines
Anton Vorontsov
Email: [email protected]
#9 lore raw

Re: [PATCH 3/8] arm: Fix possible race on task->mm

Quoted 18 lines · Anton Vorontsov
Quoted 16 lines · Anton Vorontsov
Quoted 14 lines · Oleg Nesterov
Quoted 5 lines · Anton Vorontsov

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,

Yes, but

Quoted 1 line

so let's take the task lock while we care about its mm.

it seems that this needs find_lock_task_mm() too ?

the same for the rest patches in this series.

Quoted 7 lines
Quoted 1 line

Yep, I think you're right, will add this change.

Thinking about it more... making the code use find_lock_task_mm would be a behaviour change. Sure, in trivial cases like ARM this looks like a 100% safe thing to do, but in e.g. UML case, I wouldn't bet much money on that 'mm->context.id.u.pid' would be still meaningful.

OK, perhaps UML differs. I don't know what context.id.u.pid means. Although at first glance it would be meaningful anyway...

Quoted 2 lines

So, I'd rather do it in a separate change, so this can be easily reverted.

In the !UML case find_lock_task_mm() "obviously looks like the right thing...

But I won't argue, up to you.

Oleg.