Skip to contentSkip to conversation

block: cope with gendisk's 'queue' being added later

PATCH In review Mike Snitzer 2 participants 6 messages Jan 9, 2018 – Jan 10, 2018

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

cover block: cope with gendisk's 'queue' being added later

Hi Jens,

Please consider PATCH 1/2 for 4.16 inclusion. I've included PATCH 2/2 to show the DM changes needed in order to make use of the block changes.

I think, all things considered, this moves DM's interface with block core in a better direction for the long-term but I obviously welcome any critical feedback.

Thanks!
Mike

Mike Snitzer (2):
block: cope with gendisk's 'queue' being added later
dm: fix awkward request_queue initialization

 block/blk-sysfs.c     | 12 ++++++++++
 block/genhd.c         | 28 +++++++---------------
 drivers/md/dm-core.h  |  2 --
 drivers/md/dm-ioctl.c |  7 ++----
 drivers/md/dm-rq.c    | 13 +---------
 drivers/md/dm-table.c |  2 +-
 drivers/md/dm.c       | 66 +++++++++++++++++++++++++++++++--------------------
 7 files changed, 64 insertions(+), 66 deletions(-)
Signature · 1 line
2.15.0
#1 lore raw

PATCH 1/2 block: cope with gendisk's 'queue' being added later

Since I can remember DM has forced the block layer to allow the allocation and initialization of the request_queue to be distinct operations. Reason for this was block/genhd.c:add_disk() has required that the request_queue (and associated bdi) be tied to the gendisk before add_disk() is called -- because add_disk() also deals with exposing the request_queue via blk_register_queue().

DM's dynamic creation of arbitrary device types (and associated request_queue types) requires the DM device's gendisk be available so that DM table loads can establish a master/slave relationship with subordinate devices that are referenced by loaded DM tables -- using bd_link_disk_holder(). But until these DM tables, and their associated subordinate devices, are known DM cannot know what type of request_queue it needs -- nor what its queue_limits should be.

This chicken and egg scenario has created all manner of problems for DM and, at times, the block layer.

Summary of changes:

- Adjust device_add_disk() so that that it can cope with the gendisk _not_ having its 'queue' established yet.

- Remove del_gendisk()'s WARN_ON() if disk->queue is NULL

- Move "bdi" symlink creation from register_disk() to the end of blk_register_queue() -- it is more logical in that the bdi is part of the request_queue.

- Move extra request_queue reference count (on behalf of gendisk) from device_add_disk() to end of blk_register_queue().

- Make device_add_disk()'s calls to bdi_register_owner() and blk_register_queue() conditional on disk->queue not being NULL.

- Export blk_register_queue()

These changes allow DM to use device_add_disk() to anchor its gendisk as the "master" for master/slave relationships DM must establish with subordinate devices referenced in DM tables that get loaded. Once all "slave" devices for a DM device are known a request_queue can be properly initialized and then advertised via sysfs -- important improvement being that no request_queue resource initialization is missed.

These changes have been tested to work without any IO races because the request_queue and associated bdi don't even exist at the time that the gendisk's "struct device"s are established by device_add_disk(). I've been mindful of historic bugs, and haven't experienced them with DM, e.g.: https://bugzilla.kernel.org/show_bug.cgi?id=16312 (fixed by commit 01ea5063 "block: Fix race during disk initialization")

---
 block/blk-sysfs.c | 12 ++++++++++++
 block/genhd.c     | 28 ++++++++--------------------
 2 files changed, 20 insertions(+), 20 deletions(-)
block/blk-sysfs.c +12 -0
index 870484eaed1f..0b0dda8e2420 100644
@@ -919,8 +919,20 @@ int blk_register_queue(struct gendisk *disk)
 	ret = 0;
 unlock:
 	mutex_unlock(&q->sysfs_lock);
+
+	/*
+	 * Take an extra ref on queue which will be put on disk_release()
+	 * so that it sticks around as long as @disk is there.
+	 */
+	WARN_ON_ONCE(!blk_get_queue(q));
+
+	WARN_ON(sysfs_create_link(&dev->kobj,
+				  &q->backing_dev_info->dev->kobj,
+				  "bdi"));
+
 	return ret;
 }
+EXPORT_SYMBOL_GPL(blk_register_queue);
 
 void blk_unregister_queue(struct gendisk *disk)
 {
block/genhd.c +8 -20
index 96a66f671720..13aa80319b3b 100644
@@ -621,11 +621,6 @@ static void register_disk(struct device *parent, struct gendisk *disk)
 	while ((part = disk_part_iter_next(&piter)))
 		kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
 	disk_part_iter_exit(&piter);
-
-	err = sysfs_create_link(&ddev->kobj,
-				&disk->queue->backing_dev_info->dev->kobj,
-				"bdi");
-	WARN_ON(err);
 }
 
 /**
@@ -671,24 +666,19 @@ void device_add_disk(struct device *parent, struct gendisk *disk)
 		disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
 		disk->flags |= GENHD_FL_NO_PART_SCAN;
 	} else {
-		int ret;
-
-		/* Register BDI before referencing it from bdev */
 		disk_to_dev(disk)->devt = devt;
-		ret = bdi_register_owner(disk->queue->backing_dev_info,
-						disk_to_dev(disk));
-		WARN_ON(ret);
+		/* Register BDI before referencing it from bdev */
+		if (disk->queue) {
+			retval = bdi_register_owner(disk->queue->backing_dev_info,
+						    disk_to_dev(disk));
+			WARN_ON(retval);
+		}
 		blk_register_region(disk_devt(disk), disk->minors, NULL,
 				    exact_match, exact_lock, disk);
 	}
 	register_disk(parent, disk);
-	blk_register_queue(disk);
-
-	/*
-	 * Take an extra ref on queue which will be put on disk_release()
-	 * so that it sticks around as long as @disk is there.
-	 */
-	WARN_ON_ONCE(!blk_get_queue(disk->queue));
+	if (disk->queue)
+		blk_register_queue(disk);
 
 	disk_add_events(disk);
 	blk_integrity_add(disk);
@@ -727,8 +717,6 @@ void del_gendisk(struct gendisk *disk)
 		 */
 		bdi_unregister(disk->queue->backing_dev_info);
 		blk_unregister_queue(disk);
-	} else {
-		WARN_ON(1);
 	}
 
 	if (!(disk->flags & GENHD_FL_HIDDEN))
Signature · 1 line
2.15.0
#2 lore raw
Quoted 12 lines · Mike Snitzer
block/blk-sysfs.c +4 -0
index 870484eaed1f..0b0dda8e2420 100644
@@ -919,8 +919,20 @@ int blk_register_queue(struct gendisk *disk)
 	ret = 0;
 unlock:
 	mutex_unlock(&q->sysfs_lock);
+
+	/*
+	 * Take an extra ref on queue which will be put on disk_release()
+	 * so that it sticks around as long as @disk is there.

block/blk-sysfs.c int blk_register_queue(struct gendisk *disk) · line 935

Quoted 10 lines
block/blk-sysfs.c +8 -0
+	 */
+	WARN_ON_ONCE(!blk_get_queue(q));
+
+	WARN_ON(sysfs_create_link(&dev->kobj,
+				  &q->backing_dev_info->dev->kobj,
+				  "bdi"));
+
 	return ret;
 }
+EXPORT_SYMBOL_GPL(blk_register_queue);

Hello Mike,

So the sysfs_create_link() call is moved from register_disk() into blk_register_queue() but the sysfs_remove_link() call stays in del_gendisk()? Are you sure that you want this asymmetry?

Thanks,

Bart.

#3 lore raw
Quoted 26 lines · Bart Van Assche
Quoted 12 lines · Mike Snitzer
block/blk-sysfs.c +4 -0
index 870484eaed1f..0b0dda8e2420 100644
@@ -919,8 +919,20 @@ int blk_register_queue(struct gendisk *disk)
 	ret = 0;
 unlock:
 	mutex_unlock(&q->sysfs_lock);
+
+	/*
+	 * Take an extra ref on queue which will be put on disk_release()
+	 * so that it sticks around as long as @disk is there.

block/blk-sysfs.c int blk_register_queue(struct gendisk *disk) · line 935

Quoted 10 lines
block/blk-sysfs.c +8 -0
+	 */
+	WARN_ON_ONCE(!blk_get_queue(q));
+
+	WARN_ON(sysfs_create_link(&dev->kobj,
+				  &q->backing_dev_info->dev->kobj,
+				  "bdi"));
+
 	return ret;
 }
+EXPORT_SYMBOL_GPL(blk_register_queue);

Hello Mike,

Quoted 3 lines

So the sysfs_create_link() call is moved from register_disk() into blk_register_queue() but the sysfs_remove_link() call stays in del_gendisk()? Are you sure that you want this asymmetry?

My focus was on the add_disk() side of things, due to disk->queue possibly being NULL on add. But on remove all was basically left unmodified (aside from removing the WARN_ON).

I dont think the asymmetry is a big deal but I can fix it. I'll wait for more feedback before sending out a v2 though.

Thanks,
Mike

#4 lore raw
Quoted 33 lines · Mike Snitzer
Quoted 26 lines · Bart Van Assche
Quoted 12 lines · Mike Snitzer
block/blk-sysfs.c +4 -0
index 870484eaed1f..0b0dda8e2420 100644
@@ -919,8 +919,20 @@ int blk_register_queue(struct gendisk *disk)
 	ret = 0;
 unlock:
 	mutex_unlock(&q->sysfs_lock);
+
+	/*
+	 * Take an extra ref on queue which will be put on disk_release()
+	 * so that it sticks around as long as @disk is there.

block/blk-sysfs.c int blk_register_queue(struct gendisk *disk) · line 935

Quoted 10 lines
block/blk-sysfs.c +8 -0
+	 */
+	WARN_ON_ONCE(!blk_get_queue(q));
+
+	WARN_ON(sysfs_create_link(&dev->kobj,
+				  &q->backing_dev_info->dev->kobj,
+				  "bdi"));
+
 	return ret;
 }
+EXPORT_SYMBOL_GPL(blk_register_queue);

Hello Mike,

Quoted 3 lines

So the sysfs_create_link() call is moved from register_disk() into blk_register_queue() but the sysfs_remove_link() call stays in del_gendisk()? Are you sure that you want this asymmetry?

Quoted 6 lines

My focus was on the add_disk() side of things, due to disk->queue possibly being NULL on add. But on remove all was basically left unmodified (aside from removing the WARN_ON).

I dont think the asymmetry is a big deal but I can fix it. I'll wait for more feedback before sending out a v2 though.

But while reviewing this asymetry I found that the sysfs_create_link() that I moved to blk_register_queue() needs to be guarded against GENHD_FL_HIDDEN -- I didn't notice the GENHD_FL_HIDDEN early return in register_disk(). I'll get that fixed up.

But unrelated to my patch: I think I found another curious imbalance, in current upstream code, relative to GENHD_FL_HIDDEN. bdi_register_owner() is only called if !GENHD_FL_HIDDEN but bdi_unregister() is called unconditionally. Not sure what is needed to address that issue because I'd have thought that the bdi would be needed regardless of GENHD_FL_HIDDEN. Christoph?

#5 lore raw

PATCH 2/2 dm: fix awkward request_queue initialization

In reply to Mike Snitzer

Fix DM so that it no longer creates a place-holder request_queue that doesn't reflect the actual way the request_queue will ulimately be used, only to have to backfill proper queue and queue_limits initialization. Instead, DM creates a gendisk that initially doesn't have a request_queue at all. This serves to allow DM table loads to own subordinate devices without first needing to know the aggregated capabilities that will be needed of the DM device's request_queue. Once all DM tables are loaded the request_queue is allocated and DM then backfills its gendisk's ->queue member and associated sysfs and bdi initialization via blk_register_queue().

DM is now no longer prone to having its request_queue be improperly initialized.

Summary of changes:

- alloc_dev() doesn't pre-allocate a place-holder request_queue, and now sets md->disk->queue to NULL before calling add_disk().

- dm_setup_md_queue() is updated to allocate and initialize the DM's request_queue (_after_ all table loads have occurred and request_queue type and features and limits are known).

- dm_setup_md_queue() must call bdi_register_owner() and blk_register_queue() because they were not possible at add_disk()-time (due to disk->queue being NULL).

Any future "why is disk->queue NULL!?" awkwardness is DM's to own and shouldn't put more burden on block core (famous last words). So potential issues related to disk->queue NULL pointer dereferences should be for DM maintainer(s) to triage and address.

A very welcome side-effect of these changes is DM no longer needs to:
1) backfill the "mq" sysfs entry (because historically DM didn't initialize the request_queue to use blk-mq until _after_ register_queue() was called via add_disk()).
2) call elv_register_queue() to get .request_fn request-based DM device's "queue" exposed in syfs.

These changes also stave off the need to introduce new DM-specific workarounds in block core, e.g. this proposal:
https://patchwork.kernel.org/patch/10067961/

In the end DM devices should be less unicorn in nature (relative to initialization and availability of block core infrastructure).

---
 drivers/md/dm-core.h  |  2 --
 drivers/md/dm-ioctl.c |  7 ++----
 drivers/md/dm-rq.c    | 13 +---------
 drivers/md/dm-table.c |  2 +-
 drivers/md/dm.c       | 66 +++++++++++++++++++++++++++++++--------------------
 5 files changed, 44 insertions(+), 46 deletions(-)
drivers/md/dm-core.h +0 -2
index 124ffa2d6b9a..3222e21cbbf8 100644
@@ -129,8 +129,6 @@ struct mapped_device {
 	struct srcu_struct io_barrier;
 };
 
-void dm_init_md_queue(struct mapped_device *md);
-void dm_init_normal_md_queue(struct mapped_device *md);
 int md_in_flight(struct mapped_device *md);
 void disable_write_same(struct mapped_device *md);
 void disable_write_zeroes(struct mapped_device *md);
drivers/md/dm-ioctl.c +2 -5
index e52676fa9832..cf8908a6fcc9 100644
@@ -1334,13 +1334,10 @@ static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_si
 	}
 
 	if (dm_get_md_type(md) == DM_TYPE_NONE) {
-		/* Initial table load: acquire type of table. */
-		dm_set_md_type(md, dm_table_get_type(t));
-
-		/* setup md->queue to reflect md's type (may block) */
+		/* setup md->type and md->queue to reflect table's type (may block) */
 		r = dm_setup_md_queue(md, t);
 		if (r) {
-			DMWARN("unable to set up device queue for new table.");
+			DMWARN("unable to setup queue for device.");
 			goto err_unlock_md_type;
 		}
 	} else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
drivers/md/dm-rq.c +1 -12
index 9d32f25489c2..ab95cc3f2f29 100644
@@ -56,7 +56,7 @@ static unsigned dm_get_blk_mq_queue_depth(void)
 
 int dm_request_based(struct mapped_device *md)
 {
-	return queue_is_rq_based(md->queue);
+	return md->queue && queue_is_rq_based(md->queue);
 }
 
 static void dm_old_start_queue(struct request_queue *q)
@@ -700,7 +700,6 @@ int dm_old_init_request_queue(struct mapped_device *md, struct dm_table *t)
 	/* disable dm_old_request_fn's merge heuristic by default */
 	md->seq_rq_merge_deadline_usecs = 0;
 
-	dm_init_normal_md_queue(md);
 	blk_queue_softirq_done(md->queue, dm_softirq_done);
 
 	/* Initialize the request-based DM worker thread */
@@ -713,8 +712,6 @@ int dm_old_init_request_queue(struct mapped_device *md, struct dm_table *t)
 		return error;
 	}
 
-	elv_register_queue(md->queue);
-
 	return 0;
 }
 
@@ -810,17 +807,9 @@ int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
 		err = PTR_ERR(q);
 		goto out_tag_set;
 	}
-	dm_init_md_queue(md);
-
-	/* backfill 'mq' sysfs registration normally done in blk_register_queue */
-	err = blk_mq_register_dev(disk_to_dev(md->disk), q);
-	if (err)
-		goto out_cleanup_queue;
 
 	return 0;
 
-out_cleanup_queue:
-	blk_cleanup_queue(q);
 out_tag_set:
 	blk_mq_free_tag_set(md->tag_set);
 out_kfree_tag_set:
drivers/md/dm-table.c +1 -1
index ad4ac294dd57..905e0947325a 100644
@@ -1192,7 +1192,7 @@ static int dm_table_build_index(struct dm_table *t)
 
 static bool integrity_profile_exists(struct gendisk *disk)
 {
-	return !!blk_get_integrity(disk);
+	return disk->queue && !!blk_get_integrity(disk);
 }
 
 /*
drivers/md/dm.c +40 -26

135 lines not on this page · raw message

Signature · 1 line
2.15.0