Skip to content

Commit

Permalink
block: push down BKL into .open and .release
Browse files Browse the repository at this point in the history
The open and release block_device_operations are currently
called with the BKL held. In order to change that, we must
first make sure that all drivers that currently rely
on this have no regressions.

This blindly pushes the BKL into all .open and .release
operations for all block drivers to prepare for the
next step. The drivers can subsequently replace the BKL
with their own locks or remove it completely when it can
be shown that it is not needed.

The functions blkdev_get and blkdev_put are the only
remaining users of the big kernel lock in the block
layer, besides a few uses in the ioctl code, none
of which need to serialize with blkdev_{get,put}.

Most of these two functions is also under the protection
of bdev->bd_mutex, including the actual calls to
->open and ->release, and the common code does not
access any global data structures that need the BKL.

Signed-off-by: Arnd Bergmann <[email protected]>
Acked-by: Christoph Hellwig <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
arndb authored and Jens Axboe committed Aug 7, 2010
1 parent 8a6cfeb commit 6e9624b
Show file tree
Hide file tree
Showing 39 changed files with 334 additions and 48 deletions.
7 changes: 6 additions & 1 deletion arch/um/drivers/ubd_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "linux/mm.h"
#include "linux/slab.h"
#include "linux/vmalloc.h"
#include "linux/smp_lock.h"
#include "linux/blkpg.h"
#include "linux/genhd.h"
#include "linux/spinlock.h"
Expand Down Expand Up @@ -1098,6 +1099,7 @@ static int ubd_open(struct block_device *bdev, fmode_t mode)
struct ubd *ubd_dev = disk->private_data;
int err = 0;

lock_kernel();
if(ubd_dev->count == 0){
err = ubd_open_dev(ubd_dev);
if(err){
Expand All @@ -1115,16 +1117,19 @@ static int ubd_open(struct block_device *bdev, fmode_t mode)
if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
err = -EROFS;
}*/
out:
out:
unlock_kernel();
return err;
}

static int ubd_release(struct gendisk *disk, fmode_t mode)
{
struct ubd *ubd_dev = disk->private_data;

lock_kernel();
if(--ubd_dev->count == 0)
ubd_close_dev(ubd_dev);
unlock_kernel();
return 0;
}

Expand Down
13 changes: 9 additions & 4 deletions drivers/block/DAC960.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,28 @@ static int DAC960_open(struct block_device *bdev, fmode_t mode)
struct gendisk *disk = bdev->bd_disk;
DAC960_Controller_T *p = disk->queue->queuedata;
int drive_nr = (long)disk->private_data;
int ret = -ENXIO;

lock_kernel();
if (p->FirmwareType == DAC960_V1_Controller) {
if (p->V1.LogicalDriveInformation[drive_nr].
LogicalDriveState == DAC960_V1_LogicalDrive_Offline)
return -ENXIO;
goto out;
} else {
DAC960_V2_LogicalDeviceInfo_T *i =
p->V2.LogicalDeviceInformation[drive_nr];
if (!i || i->LogicalDeviceState == DAC960_V2_LogicalDevice_Offline)
return -ENXIO;
goto out;
}

check_disk_change(bdev);

if (!get_capacity(p->disks[drive_nr]))
return -ENXIO;
return 0;
goto out;
ret = 0;
out:
unlock_kernel();
return ret;
}

static int DAC960_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Expand Down
12 changes: 10 additions & 2 deletions drivers/block/amiflop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,10 +1555,13 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
int old_dev;
unsigned long flags;

lock_kernel();
old_dev = fd_device[drive];

if (fd_ref[drive] && old_dev != system)
if (fd_ref[drive] && old_dev != system) {
unlock_kernel();
return -EBUSY;
}

if (mode & (FMODE_READ|FMODE_WRITE)) {
check_disk_change(bdev);
Expand All @@ -1571,8 +1574,10 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
fd_deselect (drive);
rel_fdc();

if (wrprot)
if (wrprot) {
unlock_kernel();
return -EROFS;
}
}
}

Expand All @@ -1589,6 +1594,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
unit[drive].type->name, data_types[system].name);

unlock_kernel();
return 0;
}

Expand All @@ -1597,6 +1603,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
struct amiga_floppy_struct *p = disk->private_data;
int drive = p - unit;

lock_kernel();
if (unit[drive].dirty == 1) {
del_timer (flush_track_timer + drive);
non_int_flush_track (drive);
Expand All @@ -1610,6 +1617,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
/* the mod_use counter is handled this way */
floppy_off (drive | 0x40000000);
#endif
unlock_kernel();
return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions drivers/block/aoe/aoeblk.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <linux/slab.h>
#include <linux/genhd.h>
#include <linux/netdevice.h>
#include <linux/smp_lock.h>
#include "aoe.h"

static struct kmem_cache *buf_pool_cache;
Expand Down Expand Up @@ -124,13 +125,16 @@ aoeblk_open(struct block_device *bdev, fmode_t mode)
struct aoedev *d = bdev->bd_disk->private_data;
ulong flags;

lock_kernel();
spin_lock_irqsave(&d->lock, flags);
if (d->flags & DEVFL_UP) {
d->nopen++;
spin_unlock_irqrestore(&d->lock, flags);
unlock_kernel();
return 0;
}
spin_unlock_irqrestore(&d->lock, flags);
unlock_kernel();
return -ENODEV;
}

Expand Down
14 changes: 13 additions & 1 deletion drivers/block/ataflop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1850,22 +1850,34 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
return 0;
}

static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
{
int ret;

lock_kernel();
ret = floppy_open(bdev, mode);
unlock_kernel();

return ret;
}

static int floppy_release(struct gendisk *disk, fmode_t mode)
{
struct atari_floppy_struct *p = disk->private_data;
lock_kernel();
if (p->ref < 0)
p->ref = 0;
else if (!p->ref--) {
printk(KERN_ERR "floppy_release with fd_ref == 0");
p->ref = 0;
}
unlock_kernel();
return 0;
}

static const struct block_device_operations floppy_fops = {
.owner = THIS_MODULE,
.open = floppy_open,
.open = floppy_unlocked_open,
.release = floppy_release,
.ioctl = fd_ioctl,
.media_changed = check_floppy_change,
Expand Down
23 changes: 20 additions & 3 deletions drivers/block/cciss.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ static void do_cciss_request(struct request_queue *q);
static irqreturn_t do_cciss_intx(int irq, void *dev_id);
static irqreturn_t do_cciss_msix_intr(int irq, void *dev_id);
static int cciss_open(struct block_device *bdev, fmode_t mode);
static int cciss_unlocked_open(struct block_device *bdev, fmode_t mode);
static int cciss_release(struct gendisk *disk, fmode_t mode);
static int do_ioctl(struct block_device *bdev, fmode_t mode,
unsigned int cmd, unsigned long arg);
Expand Down Expand Up @@ -237,7 +238,7 @@ static int cciss_compat_ioctl(struct block_device *, fmode_t,

static const struct block_device_operations cciss_fops = {
.owner = THIS_MODULE,
.open = cciss_open,
.open = cciss_unlocked_open,
.release = cciss_release,
.ioctl = do_ioctl,
.getgeo = cciss_getgeo,
Expand Down Expand Up @@ -1042,20 +1043,36 @@ static int cciss_open(struct block_device *bdev, fmode_t mode)
return 0;
}

static int cciss_unlocked_open(struct block_device *bdev, fmode_t mode)
{
int ret;

lock_kernel();
ret = cciss_open(bdev, mode);
unlock_kernel();

return ret;
}

/*
* Close. Sync first.
*/
static int cciss_release(struct gendisk *disk, fmode_t mode)
{
ctlr_info_t *host = get_host(disk);
drive_info_struct *drv = get_drv(disk);
ctlr_info_t *host;
drive_info_struct *drv;

lock_kernel();
host = get_host(disk);
drv = get_drv(disk);

#ifdef CCISS_DEBUG
printk(KERN_DEBUG "cciss_release %s\n", disk->disk_name);
#endif /* CCISS_DEBUG */

drv->usage_count--;
host->usage_count--;
unlock_kernel();
return 0;
}

Expand Down
22 changes: 19 additions & 3 deletions drivers/block/cpqarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static int sendcmd(
unsigned int blkcnt,
unsigned int log_unit );

static int ida_open(struct block_device *bdev, fmode_t mode);
static int ida_unlocked_open(struct block_device *bdev, fmode_t mode);
static int ida_release(struct gendisk *disk, fmode_t mode);
static int ida_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg);
static int ida_getgeo(struct block_device *bdev, struct hd_geometry *geo);
Expand Down Expand Up @@ -196,7 +196,7 @@ static inline ctlr_info_t *get_host(struct gendisk *disk)

static const struct block_device_operations ida_fops = {
.owner = THIS_MODULE,
.open = ida_open,
.open = ida_unlocked_open,
.release = ida_release,
.ioctl = ida_ioctl,
.getgeo = ida_getgeo,
Expand Down Expand Up @@ -841,13 +841,29 @@ static int ida_open(struct block_device *bdev, fmode_t mode)
return 0;
}

static int ida_unlocked_open(struct block_device *bdev, fmode_t mode)
{
int ret;

lock_kernel();
ret = ida_open(bdev, mode);
unlock_kernel();

return ret;
}

/*
* Close. Sync first.
*/
static int ida_release(struct gendisk *disk, fmode_t mode)
{
ctlr_info_t *host = get_host(disk);
ctlr_info_t *host;

lock_kernel();
host = get_host(disk);
host->usage_count--;
unlock_kernel();

return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions drivers/block/drbd/drbd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,7 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
unsigned long flags;
int rv = 0;

lock_kernel();
spin_lock_irqsave(&mdev->req_lock, flags);
/* to have a stable mdev->state.role
* and no race with updating open_cnt */
Expand All @@ -2618,14 +2619,17 @@ static int drbd_open(struct block_device *bdev, fmode_t mode)
if (!rv)
mdev->open_cnt++;
spin_unlock_irqrestore(&mdev->req_lock, flags);
unlock_kernel();

return rv;
}

static int drbd_release(struct gendisk *gd, fmode_t mode)
{
struct drbd_conf *mdev = gd->private_data;
lock_kernel();
mdev->open_cnt--;
unlock_kernel();
return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions drivers/block/floppy.c
Original file line number Diff line number Diff line change
Expand Up @@ -3616,6 +3616,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
{
int drive = (long)disk->private_data;

lock_kernel();
mutex_lock(&open_lock);
if (UDRS->fd_ref < 0)
UDRS->fd_ref = 0;
Expand All @@ -3626,6 +3627,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
if (!UDRS->fd_ref)
opened_bdev[drive] = NULL;
mutex_unlock(&open_lock);
unlock_kernel();

return 0;
}
Expand All @@ -3643,6 +3645,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
int res = -EBUSY;
char *tmp;

lock_kernel();
mutex_lock(&open_lock);
old_dev = UDRS->fd_device;
if (opened_bdev[drive] && opened_bdev[drive] != bdev)
Expand Down Expand Up @@ -3719,6 +3722,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
goto out;
}
mutex_unlock(&open_lock);
unlock_kernel();
return 0;
out:
if (UDRS->fd_ref < 0)
Expand All @@ -3729,6 +3733,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
opened_bdev[drive] = NULL;
out2:
mutex_unlock(&open_lock);
unlock_kernel();
return res;
}

Expand Down
5 changes: 5 additions & 0 deletions drivers/block/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <linux/compat.h>
#include <linux/suspend.h>
#include <linux/freezer.h>
#include <linux/smp_lock.h>
#include <linux/writeback.h>
#include <linux/buffer_head.h> /* for invalidate_bdev() */
#include <linux/completion.h>
Expand Down Expand Up @@ -1408,9 +1409,11 @@ static int lo_open(struct block_device *bdev, fmode_t mode)
{
struct loop_device *lo = bdev->bd_disk->private_data;

lock_kernel();
mutex_lock(&lo->lo_ctl_mutex);
lo->lo_refcnt++;
mutex_unlock(&lo->lo_ctl_mutex);
unlock_kernel();

return 0;
}
Expand All @@ -1420,6 +1423,7 @@ static int lo_release(struct gendisk *disk, fmode_t mode)
struct loop_device *lo = disk->private_data;
int err;

lock_kernel();
mutex_lock(&lo->lo_ctl_mutex);

if (--lo->lo_refcnt)
Expand All @@ -1444,6 +1448,7 @@ static int lo_release(struct gendisk *disk, fmode_t mode)
out:
mutex_unlock(&lo->lo_ctl_mutex);
out_unlocked:
lock_kernel();
return 0;
}

Expand Down
Loading

0 comments on commit 6e9624b

Please sign in to comment.