loop: make partition scanning reliable
Subsystems (2)
RFC PATCH 1/1 loop: make partition scanning reliable
So far, loop-device partition-scanning is skipped with EBUSY if the bd_mutex cannot be locked. This might happen, if someone runs open() / close() / ioctl() in parallel to LOOP_SET_FD/LOOP_CHANGE_FD and friends.
__fput() on open files might get delayed arbitrarily, which means, blkdev_put() might get called at any point in time, locking bd_mutex and thus preventing any possible partition-rescanning in parallel. This basically requires user-space to *always* run BLKRRSCAN after LOOP_SET_FD/LOOP_CHANGE_FD as we cannot know whether someone else is about to close their FD at the same time.
Fix this by running BLKRRPART without holding lo_ctl_mutex, thus, we will no longer deadlock if we lock bd_mutex.
--- block/ioctl.c | 9 ++++++--- drivers/block/loop.c | 49 ++++++++++++++++++++++++++++++------------------- include/linux/fs.h | 1 + 3 files changed, 37 insertions(+), 22 deletions(-)
block/ioctl.c +6 -3
@@ -150,7 +150,7 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user } } -static int blkdev_reread_part (struct block_device *bdev) +int blkdev_reread_part (struct block_device *bdev,int skipbusy) {struct gendisk *disk = bdev->bd_disk;int res;
@@ -159,12 +159,15 @@ static int blkdev_reread_part(struct block_device *bdev)return -EINVAL ;if (!capable (CAP_SYS_ADMIN ))return -EACCES ; -if (!mutex_trylock (&bdev->bd_mutex)) +if (!skipbusy) +mutex_lock (&bdev->bd_mutex); +else if (!mutex_trylock (&bdev->bd_mutex))return -EBUSY ; res =rescan_partitions (disk, bdev);mutex_unlock (&bdev->bd_mutex);return res; } +EXPORT_SYMBOL (blkdev_reread_part);static int blk_ioctl_discard (struct block_device *bdev,uint64_t start,uint64_t len,int secure)
@@ -407,7 +410,7 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, ret =blkpg_ioctl (bdev, (struct blkpg_ioctl_arg __user *) arg);break ;case BLKRRPART : - ret =blkdev_reread_part (bdev); + ret =blkdev_reread_part (bdev,1 );break ;case BLKGETSIZE : size =i_size_read (bdev->bd_inode);
drivers/block/loop.c +30 -19
159 lines not on this page · raw message
include/linux/fs.h +1 -0
@@ -2153,6 +2153,7 @@ extern const struct file_operations bad_sock_fops;#ifdef CONFIG_BLOCK extern int ioctl_by_bdev (struct block_device *,unsigned ,unsigned long );extern int blkdev_ioctl (struct block_device *,fmode_t ,unsigned ,unsigned long ); +extern int blkdev_reread_part (struct block_device *bdev,int skipbusy);extern long compat_blkdev_ioctl (struct file *,unsigned ,unsigned long );extern int blkdev_get (struct block_device *bdev,fmode_t mode,void *holder); extern struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
Signature · 1 line
2.2.2
Hello, David.
Quoted 6 lines · David Herrmann
block/ioctl.c +1
-1-static int blkdev_reread_part (struct block_device *bdev) +int blkdev_reread_part (struct block_device *bdev,int skipbusy) {struct gendisk *disk = bdev->bd_disk;int res;
block/ioctl.c static int blkdev_reread_part(struct block_device *bdev) · line 165
Quoted 8 lines
block/ioctl.c +3
-1@@ -159,12 +159,15 @@ static int blkdev_reread_part(struct block_device *bdev)return -EINVAL ;if (!capable (CAP_SYS_ADMIN ))return -EACCES ; -if (!mutex_trylock (&bdev->bd_mutex)) +if (!skipbusy) +mutex_lock (&bdev->bd_mutex); +else if (!mutex_trylock (&bdev->bd_mutex))return -EBUSY ;
Do we actually need the mutex_trylock() path? Why can't we just always grab the mutex?
...
drivers/block/loop.c out: · line 640
Quoted 10 lines
drivers/block/loop.c +1
-1@@ -637,7 +637,7 @@ out:* new backing store is the same size and type as the old backing store. */ static int loop_change_fd (struct loop_device *lo,struct block_device *bdev, -unsigned int arg) +unsigned int arg,int *rrpart)
bool *rrpart would be better but can't we communicate this through the return value? Wouldn't that be prettier?
Thanks.
Signature · 1 line
tejun
Quoted 4 lines · Tejun Heo
Quoted 4 lines
block/ioctl.c +0
-0@@ -159,12 +159,15 @@ static int blkdev_reread_part(struct block_device *bdev)return -EINVAL ;if (!capable (CAP_SYS_ADMIN ))return -EACCES ;
Quoted 8 lines
block/ioctl.c static int blkdev_reread_part(struct block_device *bdev) · line 165
Quoted 5 lines
block/ioctl.c +3
-1-if (!mutex_trylock (&bdev->bd_mutex)) +if (!skipbusy) +mutex_lock (&bdev->bd_mutex); +else if (!mutex_trylock (&bdev->bd_mutex))return -EBUSY ;Do we actually need the
mutex_trylock()path? Why can't we just always grab the mutex?
I wondered about this, too. The trylock goes all the way back to when Al first factored out the partition re-reading into common code ("[PATCH] partition table flush/read cleanup" in the historic tree converted from bk), and even before that most drivers used a weird dev_lock_part() consruct operating on a kdev_t, wich looked like a trylock.
Given that blkdev_reread_part is invoked directly from the ioctl method without any additional locking it seems fairly pointless for the case where it's issue from userspace. In addition to that the loop driver, nbd and dasd issue it from a driver, as does the root mounting code for md.
I would be surprised if any of these callers needs it, but a careful audit would be useful.
I'd also really prefer a patch that makes loop, nbd and dasd call blkdev_reread_part directly instead of by ioctl_by_bdev as a first stage preparation, followed by the locking changes and the changes to loop.c in separate patch so each does one thing and can be properly documented.