linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anton Vorontsov <anton.vorontsov@linaro.org>
To: Andrew Morton <akpm@linux-foundation.org>,
	Oleg Nesterov <oleg@redhat.com>
Cc: Russell King <linux@arm.linux.org.uk>,
	Mike Frysinger <vapier@gentoo.org>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Richard Weinberger <richard@nod.at>,
	Paul Mundt <lethal@linux-sh.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>,
	John Stultz <john.stultz@linaro.org>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	uclinux-dist-devel@blackfin.uclinux.org,
	linuxppc-dev@lists.ozlabs.org, linux-sh@vger.kernel.org,
	user-mode-linux-devel@lists.sourceforge.net,
	linaro-kernel@lists.linaro.org, patches@linaro.org,
	linux-mm@kvack.org
Subject: [PATCH 5/9] blackfin: A couple of task->mm handling fixes
Date: Mon, 23 Apr 2012 00:09:01 -0700	[thread overview]
Message-ID: <20120423070901.GE30752@lizard> (raw)
In-Reply-To: <20120423070641.GA27702@lizard>

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 <anton.vorontsov@linaro.org>
---
 arch/blackfin/kernel/trace.c |   26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/arch/blackfin/kernel/trace.c b/arch/blackfin/kernel/trace.c
index 44bbf2f..d08f0e3 100644
--- a/arch/blackfin/kernel/trace.c
+++ b/arch/blackfin/kernel/trace.c
@@ -10,6 +10,8 @@
 #include <linux/hardirq.h>
 #include <linux/thread_info.h>
 #include <linux/mm.h>
+#include <linux/oom.h>
+#include <linux/sched.h>
 #include <linux/uaccess.h>
 #include <linux/module.h>
 #include <linux/kallsyms.h>
@@ -28,7 +30,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
@@ -114,15 +115,15 @@ 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));
-		if (!mm)
-			continue;
+		struct task_struct *t;
 
-		if (!down_read_trylock(&mm->mmap_sem)) {
-			if (!in_atomic)
-				mmput(mm);
+		t = find_lock_task_mm(p);
+		if (!t)
 			continue;
-		}
+
+		mm = t->mm;
+		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;
@@ -131,7 +132,7 @@ void decode_address(char *buf, unsigned long address)
 
 			if (address >= vma->vm_start && address < vma->vm_end) {
 				char _tmpbuf[256];
-				char *name = p->comm;
+				char *name = t->comm;
 				struct file *file = vma->vm_file;
 
 				if (file) {
@@ -164,8 +165,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(t);
 
 				if (buf[0] == '\0')
 					sprintf(buf, "[ %s ] dynamic memory", name);
@@ -175,8 +175,8 @@ void decode_address(char *buf, unsigned long address)
 		}
 
 		up_read(&mm->mmap_sem);
-		if (!in_atomic)
-			mmput(mm);
+__continue:
+		task_unlock(t);
 	}
 
 	/*
-- 
1.7.9.2


  parent reply	other threads:[~2012-04-23  7:10 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-23  7:06 [PATCH v3 0/9] Fixes for common mistakes w/ for_each_process and task->mm Anton Vorontsov
2012-04-23  7:07 ` [PATCH 1/9] cpu: Introduce clear_tasks_mm_cpumask() helper Anton Vorontsov
2012-04-26 23:59   ` Andrew Morton
2012-05-01 10:45     ` Peter Zijlstra
2012-05-05  1:47       ` [PATCH] cpu: Document clear_tasks_mm_cpumask() Anton Vorontsov
2012-05-05  1:47     ` [PATCH 1/9] cpu: Introduce clear_tasks_mm_cpumask() helper Anton Vorontsov
2012-04-23  7:08 ` [PATCH 2/9] arm: Use clear_tasks_mm_cpumask() Anton Vorontsov
2012-04-23  7:08 ` [PATCH 3/9] powerpc: " Anton Vorontsov
2012-04-23  7:08 ` [PATCH 4/9] sh: " Anton Vorontsov
2012-04-23  7:09 ` Anton Vorontsov [this message]
2012-06-01  4:36   ` [PATCH 5/9] blackfin: A couple of task->mm handling fixes Mike Frysinger
2012-04-23  7:09 ` [PATCH 6/9] blackfin: Fix possible deadlock in decode_address() Anton Vorontsov
2012-04-23  7:09 ` [PATCH 7/9] um: Should hold tasklist_lock while traversing processes Anton Vorontsov
2012-04-23 14:57   ` Richard Weinberger
2012-04-23 15:40     ` Anton Vorontsov
2012-04-23  7:09 ` [PATCH 8/9] um: Fix possible race on task->mm Anton Vorontsov
2012-04-23  7:09 ` [PATCH 9/9] um: Properly check all process' threads for a live mm Anton Vorontsov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120423070901.GE30752@lizard \
    --to=anton.vorontsov@linaro.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=benh@kernel.crashing.org \
    --cc=john.stultz@linaro.org \
    --cc=kosaki.motohiro@jp.fujitsu.com \
    --cc=lethal@linux-sh.org \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=oleg@redhat.com \
    --cc=patches@linaro.org \
    --cc=richard@nod.at \
    --cc=uclinux-dist-devel@blackfin.uclinux.org \
    --cc=user-mode-linux-devel@lists.sourceforge.net \
    --cc=vapier@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).