Skip to contentSkip to conversation

misc struct block_device related driver cleanups

PATCH In review Christoph Hellwig 2 participants 12 messages Nov 16, 2020 – Nov 17, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi Jens, Minchan and Mike,

this series cleans up a few interactions of driver with struct block_device, in preparation for big changes to struct block_device that I plan to send soon.

Diffstat:

 block/loop.c              |    3 -
 block/mtip32xx/mtip32xx.c |   15 --------
 block/mtip32xx/mtip32xx.h |    2 -
 block/zram/zram_drv.c     |   83 ++++++++--------------------------------------
 block/zram/zram_drv.h     |    1 
 md/dm-core.h              |    2 -
 md/dm.c                   |   31 ++++++-----------
 7 files changed, 27 insertions(+), 110 deletions(-)
#1 lore raw

PATCH 1/6 mtip32xx: remove the call to fsync_bdev on removal

del_gendisk already calls fsync_bdev for every partition, no need to do this twice.

---
 drivers/block/mtip32xx/mtip32xx.c | 15 ---------------
 drivers/block/mtip32xx/mtip32xx.h |  2 --
 2 files changed, 17 deletions(-)
drivers/block/mtip32xx/mtip32xx.c +0 -15
index 153e2cdecb4d40..53ac59d19ae530 100644
@@ -3687,7 +3687,6 @@ static int mtip_block_initialize(struct driver_data *dd)
 	/* Enable the block device and add it to /dev */
 	device_add_disk(&dd->pdev->dev, dd->disk, NULL);
 
-	dd->bdev = bdget_disk(dd->disk, 0);
 	/*
 	 * Now that the disk is active, initialize any sysfs attributes
 	 * managed by the protocol layer.
@@ -3721,9 +3720,6 @@ static int mtip_block_initialize(struct driver_data *dd)
 	return rv;
 
 kthread_run_error:
-	bdput(dd->bdev);
-	dd->bdev = NULL;
-
 	/* Delete our gendisk. This also removes the device from /dev */
 	del_gendisk(dd->disk);
 
@@ -3804,14 +3800,6 @@ static int mtip_block_remove(struct driver_data *dd)
 	blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
 	blk_mq_unquiesce_queue(dd->queue);
 
-	/*
-	 * Delete our gendisk structure. This also removes the device
-	 * from /dev
-	 */
-	if (dd->bdev) {
-		bdput(dd->bdev);
-		dd->bdev = NULL;
-	}
 	if (dd->disk) {
 		if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
 			del_gendisk(dd->disk);
@@ -4206,9 +4194,6 @@ static void mtip_pci_remove(struct pci_dev *pdev)
 	} while (atomic_read(&dd->irq_workers_active) != 0 &&
 		time_before(jiffies, to));
 
-	if (!dd->sr)
-		fsync_bdev(dd->bdev);
-
 	if (atomic_read(&dd->irq_workers_active) != 0) {
 		dev_warn(&dd->pdev->dev,
 			"Completion workers still active!\n");
drivers/block/mtip32xx/mtip32xx.h +0 -2
index e22a7f0523bf30..88f4206310e4c8 100644
@@ -463,8 +463,6 @@ struct driver_data {
 
 	int isr_binding;
 
-	struct block_device *bdev;
-
 	struct list_head online_list; /* linkage for online list */
 
 	struct list_head remove_list; /* linkage for removing list */
Signature · 1 line
2.29.2
#2 lore raw

PATCH 2/6 zram: remove the claim mechanism

In reply to Christoph Hellwig

The zram claim mechanism was added to ensure no new opens come in during teardown. But the proper way to archive that is to call del_gendisk first, which takes care of all that. Once del_gendisk is called in the right place, the reset side can also be simplified as no I/O can be outstanding on a block device that is not open.

---
 drivers/block/zram/zram_drv.c | 72 ++++++++---------------------------
 1 file changed, 15 insertions(+), 57 deletions(-)
drivers/block/zram/zram_drv.c +15 -57
index 6d15d51cee2b7e..2e6d75ec1afddb 100644
@@ -1756,64 +1756,33 @@ static ssize_t disksize_store(struct device *dev,
 static ssize_t reset_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t len)
 {
-	int ret;
-	unsigned short do_reset;
-	struct zram *zram;
+	struct zram *zram = dev_to_zram(dev);
 	struct block_device *bdev;
+	unsigned short do_reset;
+	int ret = 0;
 
 	ret = kstrtou16(buf, 10, &do_reset);
 	if (ret)
 		return ret;
-
 	if (!do_reset)
 		return -EINVAL;
 
-	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 	if (!bdev)
 		return -ENOMEM;
 
 	mutex_lock(&bdev->bd_mutex);
-	/* Do not reset an active device or claimed device */
-	if (bdev->bd_openers || zram->claim) {
-		mutex_unlock(&bdev->bd_mutex);
-		bdput(bdev);
-		return -EBUSY;
-	}
-
-	/* From now on, anyone can't open /dev/zram[0-9] */
-	zram->claim = true;
+	if (bdev->bd_openers)
+		ret = -EBUSY;
+	else
+		zram_reset_device(zram);
 	mutex_unlock(&bdev->bd_mutex);
-
-	/* Make sure all the pending I/O are finished */
-	fsync_bdev(bdev);
-	zram_reset_device(zram);
 	bdput(bdev);
 
-	mutex_lock(&bdev->bd_mutex);
-	zram->claim = false;
-	mutex_unlock(&bdev->bd_mutex);
-
-	return len;
-}
-
-static int zram_open(struct block_device *bdev, fmode_t mode)
-{
-	int ret = 0;
-	struct zram *zram;
-
-	WARN_ON(!mutex_is_locked(&bdev->bd_mutex));
-
-	zram = bdev->bd_disk->private_data;
-	/* zram was claimed to reset so open request fails */
-	if (zram->claim)
-		ret = -EBUSY;
-
-	return ret;
+	return ret ? ret : len;
 }
 
 static const struct block_device_operations zram_devops = {
-	.open = zram_open,
 	.submit_bio = zram_submit_bio,
 	.swap_slot_free_notify = zram_slot_free_notify,
 	.rw_page = zram_rw_page,
@@ -1821,7 +1790,6 @@ static const struct block_device_operations zram_devops = {
 };
 
 static const struct block_device_operations zram_wb_devops = {
-	.open = zram_open,
 	.submit_bio = zram_submit_bio,
 	.swap_slot_free_notify = zram_slot_free_notify,
 	.owner = THIS_MODULE
@@ -1974,32 +1942,22 @@ static int zram_add(void)
 
 static int zram_remove(struct zram *zram)
 {
-	struct block_device *bdev;
-
-	bdev = bdget_disk(zram->disk, 0);
-	if (!bdev)
-		return -ENOMEM;
+	struct block_device *bdev = bdget_disk(zram->disk, 0);
 
-	mutex_lock(&bdev->bd_mutex);
-	if (bdev->bd_openers || zram->claim) {
-		mutex_unlock(&bdev->bd_mutex);
+	if (bdev) {
+		if (bdev->bd_openers) {
+			bdput(bdev);
+			return -EBUSY;
+		}
 		bdput(bdev);
-		return -EBUSY;
 	}
 
-	zram->claim = true;
-	mutex_unlock(&bdev->bd_mutex);
-
+	del_gendisk(zram->disk);
 	zram_debugfs_unregister(zram);
-
-	/* Make sure all the pending I/O are finished */
-	fsync_bdev(bdev);
 	zram_reset_device(zram);
-	bdput(bdev);
 
 	pr_info("Removed device: %s\n", zram->disk->disk_name);
 
-	del_gendisk(zram->disk);
 	blk_cleanup_queue(zram->disk->queue);
 	put_disk(zram->disk);
 	kfree(zram);
Signature · 1 line
2.29.2
#3 lore raw

PATCH 3/6 zram: do not call set_blocksize

In reply to Christoph Hellwig

set_blocksize is used by file systems to use their preferred buffer cache block size. Block drivers should not set it.

---
 drivers/block/zram/zram_drv.c | 11 +----------
 drivers/block/zram/zram_drv.h |  1 -
 2 files changed, 1 insertion(+), 11 deletions(-)
drivers/block/zram/zram_drv.c +1 -10
index 2e6d75ec1afddb..88baa6158eaee1 100644
@@ -403,13 +403,10 @@ static void reset_bdev(struct zram *zram)
 		return;
 
 	bdev = zram->bdev;
-	if (zram->old_block_size)
-		set_blocksize(bdev, zram->old_block_size);
 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
 	/* hope filp_close flush all of IO */
 	filp_close(zram->backing_dev, NULL);
 	zram->backing_dev = NULL;
-	zram->old_block_size = 0;
 	zram->bdev = NULL;
 	zram->disk->fops = &zram_devops;
 	kvfree(zram->bitmap);
@@ -454,7 +451,7 @@ static ssize_t backing_dev_store(struct device *dev,
 	struct file *backing_dev = NULL;
 	struct inode *inode;
 	struct address_space *mapping;
-	unsigned int bitmap_sz, old_block_size = 0;
+	unsigned int bitmap_sz;
 	unsigned long nr_pages, *bitmap = NULL;
 	struct block_device *bdev = NULL;
 	int err;
@@ -509,14 +506,8 @@ static ssize_t backing_dev_store(struct device *dev,
 		goto out;
 	}
 
-	old_block_size = block_size(bdev);
-	err = set_blocksize(bdev, PAGE_SIZE);
-	if (err)
-		goto out;
-
 	reset_bdev(zram);
 
-	zram->old_block_size = old_block_size;
 	zram->bdev = bdev;
 	zram->backing_dev = backing_dev;
 	zram->bitmap = bitmap;
drivers/block/zram/zram_drv.h +0 -1
index f2fd46daa76045..712354a4207c77 100644
@@ -118,7 +118,6 @@ struct zram {
 	bool wb_limit_enable;
 	u64 bd_wb_limit;
 	struct block_device *bdev;
-	unsigned int old_block_size;
 	unsigned long *bitmap;
 	unsigned long nr_pages;
 #endif
Signature · 1 line
2.29.2
#4 lore raw

PATCH 4/6 loop: do not call set_blocksize

In reply to Christoph Hellwig

set_blocksize is used by file systems to use their preferred buffer cache block size. Block drivers should not set it.

---
 drivers/block/loop.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/block/loop.c +0 -3
index 9a27d4f1c08aac..b42c728620c9e4 100644
@@ -1164,9 +1164,6 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
 	size = get_loop_size(lo, file);
 	loop_set_size(lo, size);
 
-	set_blocksize(bdev, S_ISBLK(inode->i_mode) ?
-		      block_size(inode->i_bdev) : PAGE_SIZE);
-
 	lo->lo_state = Lo_bound;
 	if (part_shift)
 		lo->lo_flags |= LO_FLAGS_PARTSCAN;
Signature · 1 line
2.29.2
#5 lore raw

PATCH 5/6 dm: simplify flush_bio initialization in __send_empty_flush

In reply to Christoph Hellwig

We don't really need the struct block_device to initialize a bio. So switch from using bio_set_dev to manually setting up bi_disk (bi_partno will always be zero and has been cleared by bio_init already).

---
 drivers/md/dm.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/md/dm.c +3 -9
index 54739f1b579bc8..6d7eb72d41f9ea 100644
@@ -1422,18 +1422,12 @@ static int __send_empty_flush(struct clone_info *ci)
 	 */
 	bio_init(&flush_bio, NULL, 0);
 	flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
+	flush_bio.bi_disk = ci->io->md->disk;
+	bio_associate_blkg(&flush_bio);
+
 	ci->bio = &flush_bio;
 	ci->sector_count = 0;
 
-	/*
-	 * Empty flush uses a statically initialized bio, as the base for
-	 * cloning.  However, blkg association requires that a bdev is
-	 * associated with a gendisk, which doesn't happen until the bdev is
-	 * opened.  So, blkg association is done at issue time of the flush
-	 * rather than when the device is created in alloc_dev().
-	 */
-	bio_set_dev(ci->bio, ci->io->md->bdev);
-
 	BUG_ON(bio_has_data(ci->bio));
 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Signature · 1 line
2.29.2
Acked-by on v1 5/6
#6 lore raw

Re: [PATCH 5/6] dm: simplify flush_bio initialization in __send_empty_flush

Quoted 5 lines · Christoph Hellwig

We don't really need the struct block_device to initialize a bio. So switch from using bio_set_dev to manually setting up bi_disk (bi_partno will always be zero and has been cleared by bio_init already).

#7 lore raw

PATCH 6/6 dm: remove the block_device reference in struct mapped_device

In reply to Christoph Hellwig

Get rid of the long-lasting struct block_device reference in struct mapped_device. The only remaining user is the freeze code, where we can trivially look up the block device at freeze time and release the reference at thaw time.

---
 drivers/md/dm-core.h |  2 --
 drivers/md/dm.c      | 19 ++++++++-----------
 2 files changed, 8 insertions(+), 13 deletions(-)
drivers/md/dm-core.h +0 -2
index d522093cb39dda..b1b400ed76fe90 100644
@@ -107,8 +107,6 @@ struct mapped_device {
 	/* kobject and completion */
 	struct dm_kobject_holder kobj_holder;
 
-	struct block_device *bdev;
-
 	struct dm_stats stats;
 
 	/* for blk-mq request-based DM support */
drivers/md/dm.c +8 -11
index 6d7eb72d41f9ea..83fe1e7f13e6b0 100644
@@ -1744,11 +1744,6 @@ static void cleanup_mapped_device(struct mapped_device *md)
 
 	cleanup_srcu_struct(&md->io_barrier);
 
-	if (md->bdev) {
-		bdput(md->bdev);
-		md->bdev = NULL;
-	}
-
 	mutex_destroy(&md->suspend_lock);
 	mutex_destroy(&md->type_lock);
 	mutex_destroy(&md->table_devices_lock);
@@ -1840,10 +1835,6 @@ static struct mapped_device *alloc_dev(int minor)
 	if (!md->wq)
 		goto bad;
 
-	md->bdev = bdget_disk(md->disk, 0);
-	if (!md->bdev)
-		goto bad;
-
 	dm_stats_init(&md->stats);
 
 	/* Populate the mapping, nobody knows we exist yet */
@@ -2384,12 +2375,17 @@ struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  */
 static int lock_fs(struct mapped_device *md)
 {
+	struct block_device *bdev;
 	int r;
 
 	WARN_ON(md->frozen_sb);
 
-	md->frozen_sb = freeze_bdev(md->bdev);
+	bdev = bdget_disk(md->disk, 0);
+	if (!bdev)
+		return -ENOMEM;
+	md->frozen_sb = freeze_bdev(bdev);
 	if (IS_ERR(md->frozen_sb)) {
+		bdput(bdev);
 		r = PTR_ERR(md->frozen_sb);
 		md->frozen_sb = NULL;
 		return r;
@@ -2405,7 +2401,8 @@ static void unlock_fs(struct mapped_device *md)
 	if (!test_bit(DMF_FROZEN, &md->flags))
 		return;
 
-	thaw_bdev(md->bdev, md->frozen_sb);
+	thaw_bdev(md->frozen_sb->s_bdev, md->frozen_sb);
+	bdput(md->frozen_sb->s_bdev);
 	md->frozen_sb = NULL;
 	clear_bit(DMF_FROZEN, &md->flags);
 }
Signature · 1 line
2.29.2
Acked-by on v1 6/6
#8 lore raw

Re: [PATCH 6/6] dm: remove the block_device reference in struct mapped_device

Quoted 6 lines · Christoph Hellwig

Get rid of the long-lasting struct block_device reference in struct mapped_device. The only remaining user is the freeze code, where we can trivially look up the block device at freeze time and release the reference at thaw time.

#9 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

Hi Jens, Minchan and Mike,

this series cleans up a few interactions of driver with struct block_device, in preparation for big changes to struct block_device that I plan to send soon.

Thanks, I've picked up 5 and 6 for 5.11.

#10 lore raw
Quoted 10 lines · Mike Snitzer
Quoted 5 lines · Christoph Hellwig

Hi Jens, Minchan and Mike,

this series cleans up a few interactions of driver with struct block_device, in preparation for big changes to struct block_device that I plan to send soon.

Thanks, I've picked up 5 and 6 for 5.11.

I actually need them in Jens' for-5.11/block tree for my next series..

#11 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Mike Snitzer
Quoted 2 lines · Christoph Hellwig

Hi Jens, Minchan and Mike,

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

this series cleans up a few interactions of driver with struct block_device, in preparation for big changes to struct block_device that I plan to send soon.

Thanks, I've picked up 5 and 6 for 5.11.

I actually need them in Jens' for-5.11/block tree for my next series..

Ah, OK.. no problem.

I'll reply with my Acked-by:s