Skip to contentSkip to conversation

sd async probing

PATCH In review Hannes Reinecke 3 participants 13 messages Dec 12, 2017 – Dec 15, 2017

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

cover sd async probing

Hi all,

here's a patchset to facilitate full async scsi disk probing. The current approach has the problem of calling async_synchronize_full_domain() during sd_remove(), which is required to wait for _all_ current asynchronous probes to complete.
When running under iSER sd_remove() is called from the RX thread directly, ie sd_remove() has to complete before RX processing can continue. But as probing has to issue I/O any response will be stuck in the RX queue, resulting in a deadlock.

This patchset breaks up the synchronization point into a per-device cookie, thus removing the global synchronisation and breaking the deadlock. But during testing some more deficiencies up and down the stack had been revealed, so we'll need the entire patchset to make it work.

As usual, comments and reviews are welcome.

Hannes Reinecke (4):
block: check for GENHD_FL_UP in del_gendisk()
sd: Check if parent is still present before proceeding with probing
scsi: add missing get_device() return value checks
sd: use async_probe cookie to avoid deadlocks

 block/genhd.c            |  3 +++
 drivers/scsi/scsi_scan.c | 14 ++++++++++++--
 drivers/scsi/sd.c        | 19 +++++++++++++------
 drivers/scsi/sd.h        |  3 +++
 4 files changed, 31 insertions(+), 8 deletions(-)
Signature · 1 line
1.8.5.6
#1 lore raw

PATCH 1/4 block: check for GENHD_FL_UP in del_gendisk()

From: Hannes Reinecke <[email protected]>

When a device is probed asynchronously del_gendisk() might be called before the async probing was run, causing del_gendisk() to crash due to uninitialized sysfs objects.

---
 block/genhd.c | 3 +++
 1 file changed, 3 insertions(+)
block/genhd.c +3 -0
index c2223f1..cc40d95 100644
@@ -697,6 +697,9 @@ void del_gendisk(struct gendisk *disk)
 	struct disk_part_iter piter;
 	struct hd_struct *part;
 
+	if (!(disk->flags & GENHD_FL_UP))
+		return;
+
 	blk_integrity_del(disk);
 	disk_del_events(disk);
 
Signature · 1 line
1.8.5.6
#5 lore raw

PATCH 2/4 sd: Check if parent is still present before proceeding with probing

In reply to Hannes Reinecke

When a new SCSI disk is created we should be checking if the parent device is actually present before proceeding with probing.

---
 drivers/scsi/sd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +4 -1
index ab75ebd..228b0b62 100644
@@ -3399,6 +3399,10 @@ static int sd_probe(struct device *dev)
 	}
 
 	device_initialize(&sdkp->dev);
+
+	if (!get_device(dev))
+		goto out_free_index;
+
 	sdkp->dev.parent = dev;
 	sdkp->dev.class = &sd_disk_class;
 	dev_set_name(&sdkp->dev, "%s", dev_name(dev));
@@ -3407,7 +3411,6 @@ static int sd_probe(struct device *dev)
 	if (error)
 		goto out_free_index;
 
-	get_device(dev);
 	dev_set_drvdata(dev, sdkp);
 
 	get_device(&sdkp->dev);	/* prevent release before async_schedule */
Signature · 1 line
1.8.5.6
#7 lore raw

PATCH 3/4 scsi: add missing get_device() return value checks

In reply to Hannes Reinecke

We need to validate that get_device() succeeded, otherwise we'll end up working with invalid devices.

---
 drivers/scsi/scsi_scan.c | 14 ++++++++++++--
 drivers/scsi/sd.c        |  8 +++++---
 2 files changed, 17 insertions(+), 5 deletions(-)
drivers/scsi/scsi_scan.c +12 -2
index be5e919..18edfd7 100644
@@ -245,6 +245,10 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
 	INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue);
 
 	sdev->sdev_gendev.parent = get_device(&starget->dev);
+	if (!sdev->sdev_gendev.parent) {
+		kfree(sdev);
+		return NULL;
+	}
 	sdev->sdev_target = starget;
 
 	/* usually NULL and set by ->slave_alloc instead */
@@ -366,7 +370,8 @@ static struct scsi_target *__scsi_find_target(struct device *parent,
 		}
 	}
 	if (found_starget)
-		get_device(&found_starget->dev);
+		if (!get_device(&found_starget->dev))
+			found_starget = NULL;
 
 	return found_starget;
 }
@@ -436,6 +441,10 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
 	device_initialize(dev);
 	kref_init(&starget->reap_ref);
 	dev->parent = get_device(parent);
+	if (!dev->parent) {
+		kfree(starget);
+		return NULL;
+	}
 	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
 	dev->bus = &scsi_bus_type;
 	dev->type = &scsi_target_type;
@@ -469,7 +478,8 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
 			return NULL;
 		}
 	}
-	get_device(dev);
+	/* No good way to recover here; keep fingers crossed */
+	WARN_ON(!get_device(dev));
 
 	return starget;
 
drivers/scsi/sd.c +5 -3
index 228b0b62..abbab17 100644
@@ -619,10 +619,12 @@ static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
 
 	if (disk->private_data) {
 		sdkp = scsi_disk(disk);
-		if (scsi_device_get(sdkp->device) == 0)
-			get_device(&sdkp->dev);
-		else
+		if (scsi_device_get(sdkp->device))
+			sdkp = NULL;
+		else if (!get_device(&sdkp->dev)) {
+			scsi_device_put(sdkp->device);
 			sdkp = NULL;
+		}
 	}
 	mutex_unlock(&sd_ref_mutex);
 	return sdkp;
Signature · 1 line
1.8.5.6
#10 lore raw

PATCH 4/4 sd: use async_probe cookie to avoid deadlocks

In reply to Hannes Reinecke

With the current design we're waiting for all async probes to finish when removing any sd device.
This might lead to a livelock where the 'remove' call is blocking for any probe calls to finish, and the probe calls are waiting for a response, which will never be processes as the thread handling the responses is waiting for the remove call to finish. Which is completely pointless as we only _really_ care for the probe on _this_ device to be completed; any other probing can happily continue for all we care.
So save the async probing cookie in the structure and only wait if this specific probe is still active.

---
 drivers/scsi/sd.c | 6 ++++--
 drivers/scsi/sd.h | 3 +++
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/scsi/sd.c +4 -2
index abbab17..7bf20ca 100644
@@ -3416,7 +3416,8 @@ static int sd_probe(struct device *dev)
 	dev_set_drvdata(dev, sdkp);
 
 	get_device(&sdkp->dev);	/* prevent release before async_schedule */
-	async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
+	sdkp->async_probe = async_schedule_domain(sd_probe_async, sdkp,
+						  &scsi_sd_probe_domain);
 
 	return 0;
 
@@ -3454,7 +3455,8 @@ static int sd_remove(struct device *dev)
 	scsi_autopm_get_device(sdkp->device);
 
 	async_synchronize_full_domain(&scsi_sd_pm_domain);
-	async_synchronize_full_domain(&scsi_sd_probe_domain);
+	async_synchronize_cookie_domain(sdkp->async_probe,
+					&scsi_sd_probe_domain);
 	device_del(&sdkp->dev);
 	del_gendisk(sdkp->disk);
 	sd_shutdown(dev);
drivers/scsi/sd.h +3 -0
index 320de75..d8aff29 100644
@@ -2,6 +2,8 @@
 #ifndef _SCSI_DISK_H
 #define _SCSI_DISK_H
 
+#include <linux/async.h>
+
 /*
  * More than enough for everybody ;)  The huge number of majors
  * is a leftover from 16bit dev_t days, we don't really need that
@@ -73,6 +75,7 @@ struct scsi_disk {
 	struct device	dev;
 	struct gendisk	*disk;
 	struct opal_dev *opal_dev;
+	async_cookie_t  async_probe;
 #ifdef CONFIG_BLK_DEV_ZONED
 	unsigned int	nr_zones;
 	unsigned int	zone_blocks;
Signature · 1 line
1.8.5.6
#2 lore raw

Re: [PATCH 1/4] block: check for GENHD_FL_UP in del_gendisk()

In reply to Hannes Reinecke
Quoted 16 lines · Hannes Reinecke

From: Hannes Reinecke <[email protected]>

When a device is probed asynchronously del_gendisk() might be called before the async probing was run, causing del_gendisk() to crash due to uninitialized sysfs objects.

---
 block/genhd.c | 3 +++
 1 file changed, 3 insertions(+)
block/genhd.c +0 -0
index c2223f1..cc40d95 100644

block/genhd.c void del_gendisk(struct gendisk *disk) · line 704

Quoted 8 lines
block/genhd.c +3 -0
@@ -697,6 +697,9 @@ void del_gendisk(struct gendisk *disk)
 	struct disk_part_iter piter;
 	struct hd_struct *part;
 
+	if (!(disk->flags & GENHD_FL_UP))
+		return;
+
 	blk_integrity_del(disk);
 	disk_del_events(disk);

Hello Hannes,

Thank you for having published your approach for increasing disk probing concurrency. Your approach looks interesting to me. However, I don't think that patches 1/4..3/4 are sufficient to avoid races between e.g. device_add_disk() and del_gendisk(). As far as I know no locks are held around the device_add_disk() and del_gendisk() calls. Does that mean that del_gendisk() can call e.g. blk_integrity_del() before device_add_disk() has called blk_integrity_add()?

Bart.

#3 lore raw

Re: [PATCH 1/4] block: check for GENHD_FL_UP in del_gendisk()

Quoted 28 lines · Bart Van Assche
Quoted 16 lines · Hannes Reinecke

From: Hannes Reinecke <[email protected]>

When a device is probed asynchronously del_gendisk() might be called before the async probing was run, causing del_gendisk() to crash due to uninitialized sysfs objects.

---
 block/genhd.c | 3 +++
 1 file changed, 3 insertions(+)
block/genhd.c +0 -0
index c2223f1..cc40d95 100644

block/genhd.c void del_gendisk(struct gendisk *disk) · line 704

Quoted 8 lines
block/genhd.c +3 -0
@@ -697,6 +697,9 @@ void del_gendisk(struct gendisk *disk)
 	struct disk_part_iter piter;
 	struct hd_struct *part;
 
+	if (!(disk->flags & GENHD_FL_UP))
+		return;
+
 	blk_integrity_del(disk);
 	disk_del_events(disk);

Hello Hannes,

Quoted 8 lines

Thank you for having published your approach for increasing disk probing concurrency. Your approach looks interesting to me. However, I don't think that patches 1/4..3/4 are sufficient to avoid races between e.g. device_add_disk() and del_gendisk(). As far as I know no locks are held around the device_add_disk() and del_gendisk() calls. Does that mean that del_gendisk() can call e.g. blk_integrity_del() before device_add_disk() has called blk_integrity_add()?

In principle, yes.
However, the overall idea here is that device_add_disk() and del_gendisk() are enclosed within upper layer procedures, which themselves provide additional locking.
In our case the sd driver provided synchronisation guarantees ensuring that device_add_disk() and del_gendisk() doesn't run concurrently.

if one is really concerned we could convert disk->flags to a bitmask, and use atomic bitmask modification; that should avoid any concurrency issues.
However, I'm not sure if it's worth it.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		               zSeries & Storage
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)
#4 lore raw

Re: [PATCH 1/4] block: check for GENHD_FL_UP in del_gendisk()

Quoted 37 lines · Hannes Reinecke
Quoted 28 lines · Bart Van Assche
Quoted 16 lines · Hannes Reinecke

From: Hannes Reinecke <[email protected]>

When a device is probed asynchronously del_gendisk() might be called before the async probing was run, causing del_gendisk() to crash due to uninitialized sysfs objects.

---
 block/genhd.c | 3 +++
 1 file changed, 3 insertions(+)
block/genhd.c +0 -0
index c2223f1..cc40d95 100644

block/genhd.c void del_gendisk(struct gendisk *disk) · line 704

Quoted 8 lines
block/genhd.c +3 -0
@@ -697,6 +697,9 @@ void del_gendisk(struct gendisk *disk)
 	struct disk_part_iter piter;
 	struct hd_struct *part;
 
+	if (!(disk->flags & GENHD_FL_UP))
+		return;
+
 	blk_integrity_del(disk);
 	disk_del_events(disk);

Hello Hannes,

Quoted 7 lines

Thank you for having published your approach for increasing disk probing concurrency. Your approach looks interesting to me. However, I don't think that patches 1/4..3/4 are sufficient to avoid races between e.g. device_add_disk() and del_gendisk(). As far as I know no locks are held around the device_add_disk() and del_gendisk() calls. Does that mean that del_gendisk() can call e.g. blk_integrity_del() before device_add_disk() has called blk_integrity_add()?

Quoted 8 lines

In principle, yes. However, the overall idea here is that device_add_disk() and del_gendisk() are enclosed within upper layer procedures, which themselves provide additional locking. In our case the sd driver provided synchronisation guarantees ensuring that device_add_disk() and del_gendisk() doesn't run concurrently.

if one is really concerned we could convert disk->flags to a bitmask, and use atomic bitmask modification; that should avoid any concurrency issues.

Hello Hannes,

Regarding the scenario explained in a previous e-mail: what guarantees that the device_add_disk() call in sd_probe_async() does not happen concurrently with the device_unregister() call from __scsi_remove_device()?

Thanks,

Bart.

#6 lore raw

Re: [PATCH 2/4] sd: Check if parent is still present before proceeding with probing

In reply to Hannes Reinecke

drivers/scsi/sd.c static int sd_probe(struct device *dev) · line 3404

Quoted 11 lines · Hannes Reinecke
drivers/scsi/sd.c +3 -0
index ab75ebd..228b0b62 100644
@@ -3399,6 +3399,10 @@ static int sd_probe(struct device *dev)
 	}
 
 	device_initialize(&sdkp->dev);
+
+	if (!get_device(dev))
+		goto out_free_index;

get_device(dev) returns the value of the 'dev' argument. Are you sure the if-test is useful? Did you perhaps want to check sdp->sdev_state like scsi_device_get() does?

drivers/scsi/sd.c static int sd_probe(struct device *dev) · line 3416

Quoted 11 lines
drivers/scsi/sd.c +0 -1
 	sdkp->dev.parent = dev;
 	sdkp->dev.class = &sd_disk_class;
 	dev_set_name(&sdkp->dev, "%s", dev_name(dev));
@@ -3407,7 +3411,6 @@ static int sd_probe(struct device *dev)
 	if (error)
 		goto out_free_index;
 
-	get_device(dev);
 	dev_set_drvdata(dev, sdkp);
 
 	get_device(&sdkp->dev);	/* prevent release before async_schedule */

Since the get_device() call has been moved up, are there any error paths that have to be adjusted?

Bart.

#8 lore raw

Re: [PATCH 3/4] scsi: add missing get_device() return value checks

In reply to Hannes Reinecke

drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 251

Quoted 5 lines · Hannes Reinecke
drivers/scsi/scsi_scan.c +4 -0
 	sdev->sdev_gendev.parent = get_device(&starget->dev);
+	if (!sdev->sdev_gendev.parent) {
+		kfree(sdev);
+		return NULL;
+	}

Are you sure that get_device() can return NULL?

Thanks,

Bart.

#11 lore raw

Re: [PATCH 4/4] sd: use async_probe cookie to avoid deadlocks

In reply to Hannes Reinecke
Quoted 11 lines · Hannes Reinecke

With the current design we're waiting for all async probes to finish when removing any sd device.
This might lead to a livelock where the 'remove' call is blocking for any probe calls to finish, and the probe calls are waiting for a response, which will never be processes as the thread handling the responses is waiting for the remove call to finish. Which is completely pointless as we only _really_ care for the probe on _this_ device to be completed; any other probing can happily continue for all we care.
So save the async probing cookie in the structure and only wait if this specific probe is still active.

From async_synchronize_cookie_domain():

	wait_event(async_done, lowest_in_progress(domain) >= cookie);

So async_synchronize_cookie_domain() also waits for multiple asynchronous probes to finish. Does this patch have any advantages over the patch I posted (https://marc.info/?l=linux-scsi&m=151275368714540)?

Thanks,

Bart.

#9 lore raw

Re: [PATCH 3/4] scsi: add missing get_device() return value checks

In reply to Bart Van Assche
Quoted 9 lines · Bart Van Assche

drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 251

Quoted 5 lines · Hannes Reinecke
drivers/scsi/scsi_scan.c +4 -0
 	sdev->sdev_gendev.parent = get_device(&starget->dev);
+	if (!sdev->sdev_gendev.parent) {
+		kfree(sdev);
+		return NULL;
+	}

Are you sure that get_device() can return NULL?

If it can't return NULL, why bother having a return value at all? And this is not the only place where get_device() is checked for a NULL return value.

We _really_ need to come up with a consistent view here.
If get_device() can never return NULL I would strongly argue for making it a void function.
If it _can_ return NULL we need to check for it.

Either way I don't mind; but please stay consistent.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)
#12 lore raw

Re: [PATCH 4/4] sd: use async_probe cookie to avoid deadlocks

In reply to Bart Van Assche
Quoted 13 lines · Bart Van Assche
Quoted 11 lines · Hannes Reinecke

With the current design we're waiting for all async probes to finish when removing any sd device.
This might lead to a livelock where the 'remove' call is blocking for any probe calls to finish, and the probe calls are waiting for a response, which will never be processes as the thread handling the responses is waiting for the remove call to finish. Which is completely pointless as we only _really_ care for the probe on _this_ device to be completed; any other probing can happily continue for all we care.
So save the async probing cookie in the structure and only wait if this specific probe is still active.

Quoted 8 lines

From async_synchronize_cookie_domain():

	wait_event(async_done, lowest_in_progress(domain) >= cookie);

So async_synchronize_cookie_domain() also waits for multiple asynchronous probes to finish. Does this patch have any advantages over the patch I posted (https://marc.info/?l=linux-scsi&m=151275368714540)?

Correct, it waits for all _previous_ entries to complete. But this was precisely the point; previous entries (should) have all necessary information to complete (they probably only have been scheduled out for some reason), so a

The main advantage is that the change to make it work is relatively simple. (And doesn't change the interface; something we as poor distribution developer have to worry about...)

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)