Skip to contentSkip to conversation

block: first batch of add_disk() error handling conversions

PATCH Applied Luis Chamberlain 3 participants 10 messages Sep 27, 2021 – Oct 6, 2021

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

v4 cover block: first batch of add_disk() error handling conversions

This is the first set of driver conversions to use the add_disk() error handling. You can find these changes along with the entire 7th set of driver conversions on my 20210927-for-axboe-add-disk-error-handling branch [0].

Changes on this v4 since the last v3:

- Rebased onto linux-next tag 20210927
- I drop the patches already merged
- Fix the scsi/sd, scsi/sr as noted by Ming Lei
- md: rebased in light of "md: fix a lock order reversal in md_alloc"
- Adds Reviewed / Acked by tags where appropriate. I didn't keep the review by Hannes on the scsi patches as those patches are now slightly fixed

[0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux-next.git/log/?h=20210927-for-axboe-add-disk-error-handling

Luis Chamberlain (6):
scsi/sd: add error handling support for add_disk() scsi/sr: add error handling support for add_disk()
md: add error handling support for add_disk()
dm: add add_disk() error handling
loop: add error handling support for add_disk()
nbd: add error handling support for add_disk()

 drivers/block/loop.c | 8 +++++++-
 drivers/block/nbd.c  | 6 +++++-
 drivers/md/dm.c      | 4 +++-
 drivers/md/md.c      | 6 +++++-
 drivers/scsi/sd.c    | 8 +++++++-
 drivers/scsi/sr.c    | 7 ++++++-
 6 files changed, 33 insertions(+), 6 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v4 1/6 scsi/sd: add error handling support for add_disk()

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

As with the error handling for device_add() we follow the same logic and just put the device so that cleanup is done via the scsi_disk_release().

---
 drivers/scsi/sd.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +7 -1
index 523bf2fdc253..3a101ad4d16e 100644
@@ -3449,7 +3449,13 @@ static int sd_probe(struct device *dev)
 		pm_runtime_set_autosuspend_delay(dev,
 			sdp->host->hostt->rpm_autosuspend_delay);
 	}
-	device_add_disk(dev, gd, NULL);
+
+	error = device_add_disk(dev, gd, NULL);
+	if (error) {
+		put_device(&sdkp->dev);
+		goto out;
+	}
+
 	if (sdkp->capacity)
 		sd_dif_config_host(sdkp);
 
Signature · 1 line
2.30.2
#2 lore raw

PATCH v4 2/6 scsi/sr: add error handling support for add_disk()

In reply to Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

Just put the cdrom kref and have the unwinding be done by sr_kref_release().

---
 drivers/scsi/sr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/scsi/sr.c +6 -1
index 8b17b35283aa..d769057b25a0 100644
@@ -727,7 +727,12 @@ static int sr_probe(struct device *dev)
 	dev_set_drvdata(dev, cd);
 	disk->flags |= GENHD_FL_REMOVABLE;
 	sr_revalidate_disk(cd);
-	device_add_disk(&sdev->sdev_gendev, disk, NULL);
+
+	error = device_add_disk(&sdev->sdev_gendev, disk, NULL);
+	if (error) {
+		kref_put(&cd->kref, sr_kref_release);
+		goto fail;
+	}
 
 	sdev_printk(KERN_DEBUG, sdev,
 		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
Signature · 1 line
2.30.2
#3 lore raw

PATCH v4 3/6 md: add error handling support for add_disk()

In reply to Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

We just do the unwinding of what was not done before, and are sure to unlock prior to bailing.

---
 drivers/md/md.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/md/md.c +5 -1
index 6c0c3d0d905a..6bd5ad3c30b4 100644
@@ -5700,7 +5700,11 @@ static int md_alloc(dev_t dev, char *name)
 	disk->flags |= GENHD_FL_EXT_DEVT;
 	disk->events |= DISK_EVENT_MEDIA_CHANGE;
 	mddev->gendisk = disk;
-	add_disk(disk);
+	error = add_disk(disk);
+	if (error) {
+		blk_cleanup_disk(disk);
+		goto abort;
+	}
 
 	error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
 	if (error) {
Signature · 1 line
2.30.2
#4 lore raw

PATCH v4 4/6 dm: add add_disk() error handling

In reply to Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

There are two calls to dm_setup_md_queue() which can fail then, one on dm_early_create() and we can easily see that the error path there calls dm_destroy in the error path. The other use case is on the ioctl table_load case. If that fails userspace needs to call the DM_DEV_REMOVE_CMD to cleanup the state - similar to any other failure.

---
 drivers/md/dm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm.c +3 -1
index a011d09cb0fa..b83aab8507c2 100644
@@ -2083,7 +2083,9 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
 	if (r)
 		return r;
 
-	add_disk(md->disk);
+	r = add_disk(md->disk);
+	if (r)
+		return r;
 
 	r = dm_sysfs_init(md);
 	if (r) {
Signature · 6 lines
2.30.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Reviewed-by on v4 4/6
#5 lore raw

Re: [PATCH v4 4/6] dm: add add_disk() error handling

Quoted 11 lines · Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

There are two calls to dm_setup_md_queue() which can fail then, one on dm_early_create() and we can easily see that the error path there calls dm_destroy in the error path. The other use case is on the ioctl table_load case. If that fails userspace needs to call the DM_DEV_REMOVE_CMD to cleanup the state - similar to any other failure.

Quoted 2 lines
#6 lore raw

PATCH v4 5/6 loop: add error handling support for add_disk()

In reply to Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

---
 drivers/block/loop.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/loop.c +7 -1
index 7bf4686af774..00ee365ed5e0 100644
@@ -2394,13 +2394,19 @@ static int loop_add(int i)
 	disk->event_flags	= DISK_EVENT_FLAG_UEVENT;
 	sprintf(disk->disk_name, "loop%d", i);
 	/* Make this loop device reachable from pathname. */
-	add_disk(disk);
+	err = add_disk(disk);
+	if (err)
+		goto out_cleanup_disk;
+
 	/* Show this loop device. */
 	mutex_lock(&loop_ctl_mutex);
 	lo->idr_visible = true;
 	mutex_unlock(&loop_ctl_mutex);
+
 	return i;
 
+out_cleanup_disk:
+	blk_cleanup_disk(disk);
 out_cleanup_tags:
 	blk_mq_free_tag_set(&lo->tag_set);
 out_free_idr:
Signature · 7 lines
2.30.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer Applied
#7 lore raw

Re: [PATCH v4 5/6] loop: add error handling support for add_disk()

Quoted 3 lines · Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

Applied, thanks.

Signature · 1 line
Jens Axboe
#8 lore raw

PATCH v4 6/6 nbd: add error handling support for add_disk()

In reply to Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

---
 drivers/block/nbd.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/block/nbd.c +5 -1
index 5170a630778d..741365295157 100644
@@ -1757,7 +1757,9 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
 	disk->fops = &nbd_fops;
 	disk->private_data = nbd;
 	sprintf(disk->disk_name, "nbd%d", index);
-	add_disk(disk);
+	err = add_disk(disk);
+	if (err)
+		goto out_err_disk;
 
 	/*
 	 * Now publish the device.
@@ -1766,6 +1768,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
 	nbd_total_devices++;
 	return nbd;
 
+out_err_disk:
+	blk_cleanup_disk(disk);
 out_free_idr:
 	mutex_lock(&nbd_index_mutex);
 	idr_remove(&nbd_index_idr, index);
Signature · 1 line
2.30.2
maintainer Applied
#9 lore raw

Re: [PATCH v4 6/6] nbd: add error handling support for add_disk()

Quoted 3 lines · Luis Chamberlain

We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling.

Applied, thanks.

Signature · 1 line
Jens Axboe