add a ->free_disk block_device_operation v3
Subsystems (5)
Hi Jens,
this series adds a ->free_disk method to struct block_device_operation so that drivers can defer freeing their disk private data until the gendisk goes away and don't need to play games with the validity of ->private_data.
This also converts three simple drivers over as example, but eventually I imagine that all drivers with private data will use it.
Changes since v2:
- only call ->free_disk after add_disk has returned to simplify probe error handling
Changes since v1:
- fix a tag_set use after free in virtio_blk
Diffstat:
block/genhd.c | 5 ++ drivers/block/virtio_blk.c | 66 +++++++----------------------------- drivers/memstick/core/ms_block.c | 64 ++++++++-------------------------- drivers/memstick/core/ms_block.h | 1 drivers/memstick/core/mspro_block.c | 57 +++++-------------------------- include/linux/blkdev.h | 2 + 6 files changed, 46 insertions(+), 149 deletions(-)
PATCH 1/5 block: add a ->free_disk method
Add a method to notify the driver that the gendisk is about to be freed. This allows drivers to tie the lifetime of their private data to that of the gendisk and thus deal with device removal races without expensive synchronization and boilerplate code.
A new flag is added so that ->free_disk is only called after a successful call to add_disk, which significantly simplifies the error handling path during probing.
--- block/genhd.c | 5 +++++ include/linux/blkdev.h | 2 ++ 2 files changed, 7 insertions(+)
block/genhd.c +5 -0
@@ -526,6 +526,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,disk_update_readahead (disk);disk_add_events (disk); +set_bit (GD_ADDED , &disk->state);return 0 ; out_unregister_bdi:
@@ -1119,6 +1120,10 @@ static void disk_release(struct device *dev)xa_destroy (&disk->part_tbl); disk->queue->disk =NULL ;blk_put_queue (disk->queue); + +if (test_bit (GD_ADDED , &disk->state) && disk->fops->free_disk) + disk->fops->free_disk (disk); +iput (disk->part0->bd_inode);/* frees the disk */ }
include/linux/blkdev.h +2 -0
@@ -146,6 +146,7 @@ struct gendisk {#define GD_READ_ONLY 1#define GD_DEAD 2#define GD_NATIVE_CAPACITY 3 +#define GD_ADDED 4struct mutex open_mutex;/* open/close mutex */ unsigned open_partitions;/* number of open partitions */
@@ -1464,6 +1465,7 @@ struct block_device_operations {void (*unlock_native_capacity) (struct gendisk *);int (*getgeo)(struct block_device *,struct hd_geometry *);int (*set_read_only)(struct block_device *bdev,bool ro); +void (*free_disk)(struct gendisk *disk);/* this callback is with swap_lock and sometimes page table lock held */ void (*swap_slot_free_notify) (struct block_device *,unsigned long ); int (*report_zones)(struct gendisk *, sector_t sector,
Signature · 1 line
2.30.2
PATCH 2/5 memstick/ms_block: simplify refcounting
In reply to Christoph HellwigImplement the ->free_disk method to free the msb_data structure only once the last gendisk reference goes away instead of keeping a local refcount.
--- drivers/memstick/core/ms_block.c | 64 ++++++++------------------------ drivers/memstick/core/ms_block.h | 1 - 2 files changed, 15 insertions(+), 50 deletions(-)
drivers/memstick/core/ms_block.c +15 -49
@@ -1943,22 +1943,6 @@ static void msb_io_work(struct work_struct *work)static DEFINE_IDR (msb_disk_idr);/*set of used disk numbers */ static DEFINE_MUTEX (msb_disk_lock);/* protects against races in open/release */ -static int msb_bd_open (struct block_device *bdev,fmode_t mode) -{ -struct gendisk *disk = bdev->bd_disk; -struct msb_data *msb = disk->private_data; - -dbg_verbose ("block device open" ); - -mutex_lock (&msb_disk_lock); - -if (msb && msb->card) - msb->usage_count++; - -mutex_unlock (&msb_disk_lock); -return 0 ; -} -static void msb_data_clear (struct msb_data *msb) {kfree (msb->boot_page);
@@ -1968,33 +1952,6 @@ static void msb_data_clear(struct msb_data *msb) msb->card =NULL ; } -static int msb_disk_release (struct gendisk *disk) -{ -struct msb_data *msb = disk->private_data; - -dbg_verbose ("block device release" ); -mutex_lock (&msb_disk_lock); - -if (msb) { -if (msb->usage_count) - msb->usage_count--; - -if (!msb->usage_count) { - disk->private_data =NULL ; -idr_remove (&msb_disk_idr, msb->disk_id); -put_disk (disk); -kfree (msb); - } - } -mutex_unlock (&msb_disk_lock); -return 0 ; -} - -static void msb_bd_release (struct gendisk *disk,fmode_t mode) -{ -msb_disk_release (disk); -} -static int msb_bd_getgeo (struct block_device *bdev,struct hd_geometry *geo) {
@@ -2003,6 +1960,17 @@ static int msb_bd_getgeo(struct block_device *bdev,return 0 ; } +static void msb_bd_free_disk (struct gendisk *disk) +{ +struct msb_data *msb = disk->private_data; + +mutex_lock (&msb_disk_lock); +idr_remove (&msb_disk_idr, msb->disk_id); +mutex_unlock (&msb_disk_lock); + +kfree (msb); +} +static blk_status_t msb_queue_rq (struct blk_mq_hw_ctx *hctx,const struct blk_mq_queue_data *bd) {
@@ -2096,10 +2064,9 @@ static void msb_start(struct memstick_dev *card) }static const struct block_device_operations msb_bdops = { - .open = msb_bd_open, - .release = msb_bd_release, - .getgeo = msb_bd_getgeo, - .owner =THIS_MODULE + .owner =THIS_MODULE , + .getgeo = msb_bd_getgeo, + .free_disk = msb_bd_free_disk, };static const struct blk_mq_ops msb_mq_ops = {
@@ -2147,7 +2114,6 @@ static int msb_init_disk(struct memstick_dev *card)set_capacity (msb->disk, capacity);dbg ("Set total disk size to %lu sectors" , capacity); - msb->usage_count =1 ; msb->io_queue =alloc_ordered_workqueue ("ms_block" ,WQ_MEM_RECLAIM );INIT_WORK (&msb->io_work, msb_io_work);sg_init_table (msb->prealloc_sg,MS_BLOCK_MAX_SEGS +1 );
@@ -2229,7 +2195,7 @@ static void msb_remove(struct memstick_dev *card)msb_data_clear (msb);mutex_unlock (&msb_disk_lock); -msb_disk_release (msb->disk); +put_disk (msb->disk);memstick_set_drvdata (card,NULL ); }
drivers/memstick/core/ms_block.h +0 -1
@@ -143,7 +143,6 @@ struct ms_boot_page { } __packed;struct msb_data { -unsigned int usage_count;struct memstick_dev *card;struct gendisk *disk;struct request_queue *queue;
Signature · 6 lines
2.30.2 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH 3/5 memstick/mspro_block: fix handling of read-only devices
In reply to Christoph HellwigUse set_disk_ro to propagate the read-only state to the block layer instead of checking for it in ->open and leaking a reference in case of a read-only device.
--- drivers/memstick/core/mspro_block.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/memstick/core/mspro_block.c +4 -6
@@ -186,13 +186,8 @@ static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)mutex_lock (&mspro_block_disk_lock); -if (msb && msb->card) { +if (msb && msb->card) msb->usage_count++; -if ((mode &FMODE_WRITE ) && msb->read_only) - rc = -EROFS ; -else - rc =0 ; - }mutex_unlock (&mspro_block_disk_lock);
@@ -1239,6 +1234,9 @@ static int mspro_block_init_disk(struct memstick_dev *card)set_capacity (msb->disk, capacity);dev_dbg (&card->dev,"capacity set %ld\n" , capacity); +if (msb->read_only) +set_disk_ro (msb->disk, true); + rc =device_add_disk (&card->dev, msb->disk,NULL );if (rc) goto out_cleanup_disk;
Signature · 1 line
2.30.2
PATCH 4/5 memstick/mspro_block: simplify refcounting
In reply to Christoph HellwigImplement the ->free_disk method to free the msb_data structure only once the last gendisk reference goes away instead of keeping a local refcount.
--- drivers/memstick/core/mspro_block.c | 49 +++++------------------------ 1 file changed, 7 insertions(+), 42 deletions(-)
drivers/memstick/core/mspro_block.c +7 -42
@@ -133,7 +133,6 @@ struct mspro_devinfo {struct mspro_block_data {struct memstick_dev *card; -unsigned int usage_count;unsigned int caps;struct gendisk *disk;struct request_queue *queue;
@@ -178,48 +177,16 @@ static int mspro_block_complete_req(struct memstick_dev *card, int error);/*** Block device ***/ -static int mspro_block_bd_open (struct block_device *bdev,fmode_t mode) -{ -struct gendisk *disk = bdev->bd_disk; -struct mspro_block_data *msb = disk->private_data; -int rc = -ENXIO ; - -mutex_lock (&mspro_block_disk_lock); - -if (msb && msb->card) - msb->usage_count++; - -mutex_unlock (&mspro_block_disk_lock); - -return rc; -} - - -static void mspro_block_disk_release (struct gendisk *disk) +static void mspro_block_bd_free_disk (struct gendisk *disk) {struct mspro_block_data *msb = disk->private_data;int disk_id =MINOR (disk_devt (disk)) >>MSPRO_BLOCK_PART_SHIFT ;mutex_lock (&mspro_block_disk_lock); - -if (msb) { -if (msb->usage_count) - msb->usage_count--; - -if (!msb->usage_count) { -kfree (msb); - disk->private_data =NULL ; -idr_remove (&mspro_block_disk_idr, disk_id); -put_disk (disk); - } - } - +idr_remove (&mspro_block_disk_idr, disk_id);mutex_unlock (&mspro_block_disk_lock); -} -static void mspro_block_bd_release (struct gendisk *disk,fmode_t mode) -{ -mspro_block_disk_release (disk); +kfree (msb); } static int mspro_block_bd_getgeo(struct block_device *bdev,
@@ -235,10 +202,9 @@ static int mspro_block_bd_getgeo(struct block_device *bdev, }static const struct block_device_operations ms_block_bdops = { - .open = mspro_block_bd_open, - .release = mspro_block_bd_release, - .getgeo = mspro_block_bd_getgeo, - .owner =THIS_MODULE + .owner =THIS_MODULE , + .getgeo = mspro_block_bd_getgeo, + .free_disk = mspro_block_bd_free_disk, };/*** Information ***/
@@ -1221,7 +1187,6 @@ static int mspro_block_init_disk(struct memstick_dev *card) msb->disk->first_minor = disk_id <<MSPRO_BLOCK_PART_SHIFT ; msb->disk->minors =1 <<MSPRO_BLOCK_PART_SHIFT ; msb->disk->fops = &ms_block_bdops; - msb->usage_count =1 ; msb->disk->private_data = msb;sprintf (msb->disk->disk_name,"mspblk%d" , disk_id);
@@ -1339,7 +1304,7 @@ static void mspro_block_remove(struct memstick_dev *card)mspro_block_data_clear (msb);mutex_unlock (&mspro_block_disk_lock); -mspro_block_disk_release (msb->disk); +put_disk (msb->disk);memstick_set_drvdata (card,NULL ); }
Signature · 1 line
2.30.2
PATCH 5/5 virtio_blk: simplify refcounting
In reply to Christoph HellwigImplement the ->free_disk method to free the virtio_blk structure only once the last gendisk reference goes away instead of keeping a local refcount.
--- drivers/block/virtio_blk.c | 66 ++++++++------------------------------ 1 file changed, 14 insertions(+), 52 deletions(-)
drivers/block/virtio_blk.c +14 -52
@@ -69,13 +69,6 @@ struct virtio_blk {/* Process context for config space updates */ struct work_struct config_work; -/* -* Tracks references from block_device_operations open/release and -* virtio_driver probe/remove so this object can be freed once no -* longer in use. -*/ -refcount_t refs; -/* What host tells us, plus 2 for header & tailer. */ unsigned int sg_elems;
@@ -391,43 +384,6 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)return err; } -static void virtblk_get (struct virtio_blk *vblk) -{ -refcount_inc (&vblk->refs); -} - -static void virtblk_put (struct virtio_blk *vblk) -{ -if (refcount_dec_and_test (&vblk->refs)) { -ida_simple_remove (&vd_index_ida, vblk->index); -mutex_destroy (&vblk->vdev_mutex); -kfree (vblk); - } -} - -static int virtblk_open (struct block_device *bd,fmode_t mode) -{ -struct virtio_blk *vblk = bd->bd_disk->private_data; -int ret =0 ; - -mutex_lock (&vblk->vdev_mutex); - -if (vblk->vdev) -virtblk_get (vblk); -else - ret = -ENXIO ; - -mutex_unlock (&vblk->vdev_mutex); -return ret; -} - -static void virtblk_release (struct gendisk *disk,fmode_t mode) -{ -struct virtio_blk *vblk = disk->private_data; - -virtblk_put (vblk); -} -/* We provide getgeo only to please some old bootloader/partitioning tools */ static int virtblk_getgeo (struct block_device *bd,struct hd_geometry *geo) {
@@ -460,11 +416,19 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)return ret; } +static void virtblk_free_disk (struct gendisk *disk) +{ +struct virtio_blk *vblk = disk->private_data; + +ida_simple_remove (&vd_index_ida, vblk->index); +mutex_destroy (&vblk->vdev_mutex); +kfree (vblk); +} +static const struct block_device_operations virtblk_fops = { - .owner =THIS_MODULE , - .open = virtblk_open, - .release = virtblk_release, - .getgeo = virtblk_getgeo, + .owner =THIS_MODULE , + .getgeo = virtblk_getgeo, + .free_disk = virtblk_free_disk, };static int index_to_minor (int index)
@@ -791,8 +755,6 @@ static int virtblk_probe(struct virtio_device *vdev) goto out_free_index; } -/* This reference is dropped in virtblk_remove(). */ -refcount_set (&vblk->refs,1 );mutex_init (&vblk->vdev_mutex); vblk->vdev = vdev;
@@ -970,7 +932,7 @@ static void virtblk_remove(struct virtio_device *vdev)flush_work (&vblk->config_work);del_gendisk (vblk->disk); -blk_cleanup_disk (vblk->disk); +blk_cleanup_queue (vblk->disk->queue);blk_mq_free_tag_set (&vblk->tag_set);mutex_lock (&vblk->vdev_mutex);
@@ -986,7 +948,7 @@ static void virtblk_remove(struct virtio_device *vdev)mutex_unlock (&vblk->vdev_mutex); -virtblk_put (vblk); +put_disk (vblk->disk); }#ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.30.2
Quoted 8 lines · Christoph Hellwig
this series adds a ->free_disk method to struct
block_device_operationso that drivers can defer freeing their disk private data until the gendisk goes away and don't need to play games with the validity of ->private_data.This also converts three simple drivers over as example, but eventually I imagine that all drivers with private data will use it.
[...]
Applied, thanks!
[1/5] block: add a ->free_disk method
commit: 76792055c4c8b2472ca1ae48e0ddaf8497529f08
[2/5] memstick/ms_block: simplify refcounting
commit: e2efa0796607efe60c708271be483c3a2b0128de
[3/5] memstick/mspro_block: fix handling of read-only devices
commit: 6dab421bfe06a59bf8f212a72e34673e8acf2018
[4/5] memstick/mspro_block: simplify refcounting
commit: 185ed423d1898ead071c18f6161959cd3cab2dde
[5/5] virtio_blk: simplify refcounting
commit: 24b45e6c25173abcf8d5e82285212b47f2b0f86bBest regards,
Signature · 1 line
Jens Axboe