[patch] mm: fix locking, inotify_read's ev_mutex vs do_page_fault's mmap_sem...

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Ingo Molnar
Date: Sunday, September 14, 2008 - 1:06 am

* Andrew Morton <akpm@linux-foundation.org> wrote:


ah, thx. I picked up the patch into tip/out-of-tree. (see below for a 
tided up changelog) Please queue it up as v2.6.27 material. (i'll report 
it if anything breaks due to the patch)

	Ingo

--------------->
From 1eb0a42e4eb3283521ee1de99adbf567874b622f Mon Sep 17 00:00:00 2001
From: Nick Piggin <nickpiggin@yahoo.com.au>
Date: Thu, 11 Sep 2008 06:12:51 +1000
Subject: [PATCH] mm: fix locking, inotify_read's ev_mutex vs do_page_fault's mmap_sem...

Daniel J Blueman wrote:

Fix inotify lock order reversal with mmap_sem due to holding locks over
copy_to_user.

Reported-by: "Daniel J Blueman" <daniel.blueman@gmail.com>
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
---
 fs/inotify_user.c            |   27 ++++++++++++++++++++-------
 include/asm-x86/uaccess_64.h |    1 +
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/fs/inotify_user.c b/fs/inotify_user.c
index 6024942..d85c7d9 100644
--- a/fs/inotify_user.c
+++ b/fs/inotify_user.c
@@ -323,7 +323,7 @@ out:
 }
 
 /*
- * remove_kevent - cleans up and ultimately frees the given kevent
+ * remove_kevent - cleans up the given kevent
  *
  * Caller must hold dev->ev_mutex.
  */
@@ -334,7 +334,13 @@ static void remove_kevent(struct inotify_device *dev,
 
 	dev->event_count--;
 	dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len;
+}
 
+/*
+ * free_kevent - frees the given kevent.
+ */
+static void free_kevent(struct inotify_kernel_event *kevent)
+{
 	kfree(kevent->name);
 	kmem_cache_free(event_cachep, kevent);
 }
@@ -350,6 +356,7 @@ static void inotify_dev_event_dequeue(struct inotify_device *dev)
 		struct inotify_kernel_event *kevent;
 		kevent = inotify_dev_get_event(dev);
 		remove_kevent(dev, kevent);
+		free_kevent(kevent);
 	}
 }
 
@@ -433,17 +440,15 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 	dev = file->private_data;
 
 	while (1) {
-		int events;
 
 		prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
 
 		mutex_lock(&dev->ev_mutex);
-		events = !list_empty(&dev->events);
-		mutex_unlock(&dev->ev_mutex);
-		if (events) {
+		if (!list_empty(&dev->events)) {
 			ret = 0;
 			break;
 		}
+		mutex_unlock(&dev->ev_mutex);
 
 		if (file->f_flags & O_NONBLOCK) {
 			ret = -EAGAIN;
@@ -462,7 +467,6 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 	if (ret)
 		return ret;
 
-	mutex_lock(&dev->ev_mutex);
 	while (1) {
 		struct inotify_kernel_event *kevent;
 
@@ -481,6 +485,13 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 			}
 			break;
 		}
+		remove_kevent(dev, kevent);
+
+		/*
+		 * Must perform the copy_to_user outside the mutex in order
+		 * to avoid a lock order reversal with mmap_sem.
+		 */
+		mutex_unlock(&dev->ev_mutex);
 
 		if (copy_to_user(buf, &kevent->event, event_size)) {
 			ret = -EFAULT;
@@ -498,7 +509,9 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 			count -= kevent->event.len;
 		}
 
-		remove_kevent(dev, kevent);
+		free_kevent(kevent);
+
+		mutex_lock(&dev->ev_mutex);
 	}
 	mutex_unlock(&dev->ev_mutex);
 
diff --git a/include/asm-x86/uaccess_64.h b/include/asm-x86/uaccess_64.h
index 515d4dc..45806d6 100644
--- a/include/asm-x86/uaccess_64.h
+++ b/include/asm-x86/uaccess_64.h
@@ -7,6 +7,7 @@
 #include <linux/compiler.h>
 #include <linux/errno.h>
 #include <linux/prefetch.h>
+#include <linux/lockdep.h>
 #include <asm/page.h>
 
 /*
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[patch] x86: some lock annotations for user copy paths, Nick Piggin, (Wed Sep 10, 4:37 am)
Re: [patch] x86: some lock annotations for user copy paths, Peter Zijlstra, (Wed Sep 10, 4:41 am)
Re: [patch] x86: some lock annotations for user copy paths, Peter Zijlstra, (Wed Sep 10, 8:01 am)
[PATCH] sysfs: fix deadlock, Ingo Molnar, (Fri Sep 12, 2:24 am)
[patch] mm: fix locking, inotify_read's ev_mutex vs do_pag ..., Ingo Molnar, (Sun Sep 14, 1:06 am)
Re: [PATCH] sysfs: fix deadlock, Nick Piggin, (Sun Sep 14, 3:02 pm)
[patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Nick Piggin, (Sun Sep 14, 3:12 pm)
Re: [PATCH] sysfs: fix deadlock, Peter Zijlstra, (Mon Sep 15, 2:15 am)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Andrew Morton, (Wed Sep 17, 1:14 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Matt Mackall, (Wed Sep 17, 1:46 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Jeremy Fitzhardinge, (Thu Sep 18, 12:29 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Matt Mackall, (Thu Sep 18, 2:11 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Hugh Dickins, (Sat Sep 20, 9:12 am)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, David Howells, (Mon Sep 22, 7:54 am)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Nick Piggin, (Mon Sep 22, 10:32 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, David Howells, (Wed Sep 24, 11:18 am)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Matt Mackall, (Wed Sep 24, 11:29 am)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, David Howells, (Wed Sep 24, 11:56 am)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Matt Mackall, (Wed Sep 24, 12:11 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, David Howells, (Wed Sep 24, 12:26 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Hugh Dickins, (Wed Sep 24, 12:29 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Hugh Dickins, (Wed Sep 24, 12:41 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Andrew Morton, (Wed Sep 24, 12:47 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, David Howells, (Wed Sep 24, 12:59 pm)
Re: [patch] mm: tiny-shmem fix lor, mmap_sem vs i_mutex, Hugh Dickins, (Wed Sep 24, 4:43 pm)