From: Mikulas Patocka <mpatocka@redhat.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: Jeff Chua <jeff.chua.linux@gmail.com>,
Lai Jiangshan <laijs@cn.fujitsu.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Jan Kara <jack@suse.cz>, lkml <linux-kernel@vger.kernel.org>,
linux-fsdevel <linux-fsdevel@vger.kernel.org>
Subject: [PATCH] Introduce a method to catch mmap_region (was: Recent kernel "mount" slow)
Date: Wed, 28 Nov 2012 12:25:43 -0500 (EST) [thread overview]
Message-ID: <Pine.LNX.4.64.1211281222410.11599@file.rdu.redhat.com> (raw)
In-Reply-To: <50B5CC5A.8060607@kernel.dk>
On Wed, 28 Nov 2012, Jens Axboe wrote:
> On 2012-11-28 04:57, Mikulas Patocka wrote:
> >
> > This patch is wrong because you must check if the device is mapped while
> > holding bdev->bd_block_size_semaphore (because
> > bdev->bd_block_size_semaphore prevents new mappings from being created)
>
> No it doesn't. If you read the patch, that was moved to i_mmap_mutex.
Hmm, it was wrong before the patch and it is wrong after the patch too.
The problem is that ->mmap method doesn't do the actual mapping, the
caller of ->mmap (mmap_region) does it. So we must actually catch
mmap_region and protect it with the lock, not ->mmap.
Mikulas
---
Introduce a method to catch mmap_region
For block devices, we must prevent the device from being mapped while
block size is being changed.
This was implemented by catching the mmap method and taking
bd_block_size_semaphore in it.
The problem is that the generic_file_mmap method does almost nothing, it
only sets vma->vm_ops = &generic_file_vm_ops, all the hard work is done
by the caller, mmap_region. Consequently, locking the mmap method is
insufficient, to prevent the race condition. The race condition can
happen for example this way:
process 1:
Starts mapping a block device, it goes to mmap_region, calls
file->f_op->mmap (blkdev_mmap - that acquires and releases
bd_block_size_semaphore), and reschedule happens just after blkdev_mmap
returns.
process 2:
Changes block device size, goes into set_blocksize, acquires
percpu_down_write, calls mapping_mapped to test if the device is mapped
(it still isn't). Then, it reschedules.
process 1:
Continues in mmap_region, successfully finishes the mapping.
process 2:
Continues in set_blocksize, changes the block size while the block
device is mapped. (which is incorrect and may result in a crash or
misbehavior).
To fix this possible race condition, we need to catch the function that
does real mapping - mmap_region. This patch adds a new method,
file_operations->mmap_region. mmap_region calls the method
file_operations->mmap_region if it is non-NULL, otherwise, it calls
generic_mmap_region.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
---
fs/block_dev.c | 12 ++++++++----
include/linux/fs.h | 4 ++++
include/linux/mm.h | 3 +++
mm/mmap.c | 10 ++++++++++
4 files changed, 25 insertions(+), 4 deletions(-)
Index: linux-3.7-rc7/include/linux/fs.h
===================================================================
--- linux-3.7-rc7.orig/include/linux/fs.h 2012-11-28 17:45:55.000000000 +0100
+++ linux-3.7-rc7/include/linux/fs.h 2012-11-28 18:02:45.000000000 +0100
@@ -27,6 +27,7 @@
#include <linux/lockdep.h>
#include <linux/percpu-rwsem.h>
#include <linux/blk_types.h>
+#include <linux/mm_types.h>
#include <asm/byteorder.h>
#include <uapi/linux/fs.h>
@@ -1528,6 +1529,9 @@ struct file_operations {
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
+ unsigned long (*mmap_region)(struct file *, unsigned long,
+ unsigned long, unsigned long, vm_flags_t,
+ unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
Index: linux-3.7-rc7/include/linux/mm.h
===================================================================
--- linux-3.7-rc7.orig/include/linux/mm.h 2012-11-28 17:45:55.000000000 +0100
+++ linux-3.7-rc7/include/linux/mm.h 2012-11-28 17:47:12.000000000 +0100
@@ -1444,6 +1444,9 @@ extern unsigned long get_unmapped_area(s
extern unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, unsigned long flags,
vm_flags_t vm_flags, unsigned long pgoff);
+extern unsigned long generic_mmap_region(struct file *file, unsigned long addr,
+ unsigned long len, unsigned long flags,
+ vm_flags_t vm_flags, unsigned long pgoff);
extern unsigned long do_mmap_pgoff(struct file *, unsigned long,
unsigned long, unsigned long,
unsigned long, unsigned long);
Index: linux-3.7-rc7/mm/mmap.c
===================================================================
--- linux-3.7-rc7.orig/mm/mmap.c 2012-11-28 17:45:55.000000000 +0100
+++ linux-3.7-rc7/mm/mmap.c 2012-11-28 17:57:40.000000000 +0100
@@ -1244,6 +1244,16 @@ unsigned long mmap_region(struct file *f
unsigned long len, unsigned long flags,
vm_flags_t vm_flags, unsigned long pgoff)
{
+ if (file && file->f_op->mmap_region)
+ return file->f_op->mmap_region(file, addr, len, flags, vm_flags,
+ pgoff);
+ return generic_mmap_region(file, addr, len, flags, vm_flags, pgoff);
+}
+
+unsigned long generic_mmap_region(struct file *file, unsigned long addr,
+ unsigned long len, unsigned long flags,
+ vm_flags_t vm_flags, unsigned long pgoff)
+{
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma, *prev;
int correct_wcount = 0;
Index: linux-3.7-rc7/fs/block_dev.c
===================================================================
--- linux-3.7-rc7.orig/fs/block_dev.c 2012-11-28 17:45:56.000000000 +0100
+++ linux-3.7-rc7/fs/block_dev.c 2012-11-28 17:47:12.000000000 +0100
@@ -1658,14 +1658,17 @@ ssize_t blkdev_aio_write(struct kiocb *i
}
EXPORT_SYMBOL_GPL(blkdev_aio_write);
-static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
+static unsigned long blkdev_mmap_region(struct file *file, unsigned long addr,
+ unsigned long len, unsigned long flags,
+ vm_flags_t vm_flags,
+ unsigned long pgoff)
{
- int ret;
+ unsigned long ret;
struct block_device *bdev = I_BDEV(file->f_mapping->host);
percpu_down_read(&bdev->bd_block_size_semaphore);
- ret = generic_file_mmap(file, vma);
+ ret = generic_mmap_region(file, addr, len, flags, vm_flags, pgoff);
percpu_up_read(&bdev->bd_block_size_semaphore);
@@ -1737,7 +1740,8 @@ const struct file_operations def_blk_fop
.write = do_sync_write,
.aio_read = blkdev_aio_read,
.aio_write = blkdev_aio_write,
- .mmap = blkdev_mmap,
+ .mmap_region = blkdev_mmap_region,
+ .mmap = generic_file_mmap,
.fsync = blkdev_fsync,
.unlocked_ioctl = block_ioctl,
#ifdef CONFIG_COMPAT
next prev parent reply other threads:[~2012-11-28 17:26 UTC|newest]
Thread overview: 81+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CAAJw_ZtbhE5Jtd4PsWx8a23QdFTW7aMrKBmRf-bo5Wrean9Xhg@mail.gmail.com>
2012-11-20 18:09 ` Recent kernel "mount" slow Jan Kara
2012-11-21 15:46 ` Jeff Chua
2012-11-22 14:30 ` Jeff Chua
2012-11-22 19:21 ` Linus Torvalds
2012-11-23 13:24 ` Jens Axboe
2012-11-23 22:21 ` Jeff Chua
2012-11-23 23:31 ` Jeff Chua
2012-11-23 23:48 ` Jeff Chua
2012-11-24 21:09 ` Mikulas Patocka
2012-11-24 23:23 ` Jeff Chua
2012-11-27 5:57 ` Jeff Chua
2012-11-27 7:38 ` Jens Axboe
2012-11-27 7:44 ` Jens Axboe
2012-11-27 8:45 ` Jeff Chua
2012-11-27 10:06 ` Jeff Chua
2012-11-27 12:33 ` Jens Axboe
2012-11-28 3:57 ` Mikulas Patocka
2012-11-28 8:33 ` Jens Axboe
2012-11-28 13:05 ` Jeff Chua
2012-11-28 17:25 ` Mikulas Patocka [this message]
2012-11-28 19:15 ` [PATCH] Introduce a method to catch mmap_region (was: Recent kernel "mount" slow) Linus Torvalds
2012-11-28 19:43 ` Al Viro
2012-11-28 19:53 ` Linus Torvalds
2012-11-28 22:01 ` [PATCH v2] Do a proper locking for mmap and block size change Mikulas Patocka
2012-11-29 17:19 ` Linus Torvalds
2012-11-29 18:23 ` Mikulas Patocka
2012-11-29 18:46 ` Linus Torvalds
2012-11-29 19:02 ` Linus Torvalds
2012-11-29 19:15 ` Chris Mason
2012-11-29 19:26 ` Linus Torvalds
2012-11-29 19:48 ` Chris Mason
2012-11-29 19:55 ` Linus Torvalds
2012-11-29 20:10 ` Linus Torvalds
2012-11-29 20:52 ` Linus Torvalds
2012-11-29 21:29 ` Chris Mason
2012-11-29 22:16 ` Linus Torvalds
2012-11-29 22:36 ` Linus Torvalds
2012-11-30 1:16 ` Chris Mason
2012-11-30 2:13 ` Linus Torvalds
2012-11-30 2:27 ` Chris Mason
2012-11-30 2:49 ` Dave Chinner
2012-11-30 14:31 ` Chris Mason
2012-11-30 16:42 ` Linus Torvalds
2012-11-30 16:36 ` Christoph Hellwig
2012-11-30 22:40 ` Dave Chinner
2012-11-30 23:09 ` Christoph Hellwig
2012-11-29 19:50 ` Linus Torvalds
2012-11-28 19:50 ` [PATCH] Introduce a method to catch mmap_region (was: Recent kernel "mount" slow) Mikulas Patocka
2012-11-28 20:03 ` Linus Torvalds
2012-11-28 20:13 ` Linus Torvalds
2012-11-28 20:32 ` Linus Torvalds
2012-11-28 20:47 ` Linus Torvalds
2012-11-28 22:10 ` Mikulas Patocka
2012-11-28 21:29 ` Mikulas Patocka
2012-11-28 22:52 ` Linus Torvalds
2012-11-28 23:13 ` Linus Torvalds
2012-11-29 1:20 ` Mikulas Patocka
2012-11-29 0:38 ` Mikulas Patocka
2012-11-29 2:04 ` Linus Torvalds
2012-11-29 2:58 ` Linus Torvalds
2012-11-29 6:16 ` Linus Torvalds
2012-11-29 6:25 ` Al Viro
2012-11-29 6:30 ` Al Viro
2012-11-29 6:37 ` Linus Torvalds
2012-11-29 6:45 ` Al Viro
2012-11-29 10:57 ` Jeff Chua
2012-11-29 6:33 ` Linus Torvalds
2012-11-29 14:12 ` Chris Mason
2012-11-29 17:26 ` Chris Mason
2012-11-29 17:26 ` Linus Torvalds
2012-11-29 17:51 ` Chris Mason
2012-11-29 18:12 ` Linus Torvalds
2012-11-28 3:59 ` [PATCH 1/2] percpu-rwsem: use synchronize_sched_expedited Mikulas Patocka
2012-11-28 4:01 ` [PATCH 2/2] block_dev: don't take the write lock if block size doesn't change Mikulas Patocka
2012-11-28 14:24 ` Jeff Chua
2012-11-28 22:03 ` Mikulas Patocka
2012-11-28 14:19 ` [PATCH 1/2] percpu-rwsem: use synchronize_sched_expedited Jeff Chua
2012-11-30 0:06 ` Andrew Morton
2012-11-30 3:00 ` Mikulas Patocka
2012-11-30 13:42 ` Paul E. McKenney
2012-11-30 18:57 ` Oleg Nesterov
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=Pine.LNX.4.64.1211281222410.11599@file.rdu.redhat.com \
--to=mpatocka@redhat.com \
--cc=axboe@kernel.dk \
--cc=jack@suse.cz \
--cc=jeff.chua.linux@gmail.com \
--cc=laijs@cn.fujitsu.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.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).