Skip to contentSkip to conversation

Avoid that sd_shutdown() triggers a kernel warning

PATCH In review Bart Van Assche 5 participants 12 messages Jan 7, 2015 – Jan 26, 2015 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Avoid that sd_shutdown() triggers a kernel warning

Since kernel v3.19-rc1 module_refcount() returns 1 instead of 0 when called from inside module_exit(). This breaks the module_refcount() test in scsi_device_put() and hence causes the following kernel warning to be reported when unloading the ib_srp kernel module:

WARNING: CPU: 5 PID: 228 at kernel/module.c:954 module_put+0x207/0x220()

Call Trace:
 [<ffffffff814d1fcf>] dump_stack+0x4c/0x65
 [<ffffffff81053ada>] warn_slowpath_common+0x8a/0xc0
 [<ffffffff81053bca>] warn_slowpath_null+0x1a/0x20
 [<ffffffff810d0507>] module_put+0x207/0x220
 [<ffffffffa000bea8>] scsi_device_put+0x48/0x50 [scsi_mod]
 [<ffffffffa03676d2>] scsi_disk_put+0x32/0x50 [sd_mod]
 [<ffffffffa0368d4c>] sd_shutdown+0x8c/0x150 [sd_mod]
 [<ffffffffa0368e79>] sd_remove+0x69/0xc0 [sd_mod]
 [<ffffffff813457ef>] __device_release_driver+0x7f/0xf0
 [<ffffffff81345885>] device_release_driver+0x25/0x40
 [<ffffffff81345134>] bus_remove_device+0x124/0x1b0
 [<ffffffff8134189e>] device_del+0x13e/0x250
 [<ffffffffa001cdcd>] __scsi_remove_device+0xcd/0xe0 [scsi_mod]
 [<ffffffffa001b39f>] scsi_forget_host+0x6f/0x80 [scsi_mod]
 [<ffffffffa000d5f6>] scsi_remove_host+0x86/0x140 [scsi_mod]
 [<ffffffffa07d5c0b>] srp_remove_work+0x9b/0x210 [ib_srp]
 [<ffffffff8106fd28>] process_one_work+0x1d8/0x780
 [<ffffffff810703eb>] worker_thread+0x11b/0x4a0
 [<ffffffff81075a6f>] kthread+0xef/0x110
 [<ffffffff814dad6c>] ret_from_fork+0x7c/0xb0

See also patch "module: Remove stop_machine from module unloading" (Masami Hiramatsu; commit e513cc1c07e2; kernel v3.19-rc1).

Cc Christoph Hellwig <[email protected]>
Cc Hannes Reinecke <[email protected]>
---
 drivers/scsi/scsi.c        | 63 ++++++++++++++++++++++++++++++++--------------
 drivers/scsi/sd.c          | 44 +++++++++++++++++---------------
 include/scsi/scsi_device.h |  2 ++
 3 files changed, 70 insertions(+), 39 deletions(-)
drivers/scsi/scsi.c +44 -19
index e028854..2cae46b 100644
@@ -973,30 +973,63 @@ int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
 EXPORT_SYMBOL(scsi_report_opcode);
 
 /**
- * scsi_device_get  -  get an additional reference to a scsi_device
+ * scsi_dev_get - get an additional reference to a scsi_device
  * @sdev:	device to get a reference to
+ * @get_lld:    whether or not to increase the LLD kernel module refcount
  *
- * Description: Gets a reference to the scsi_device and increments the use count
- * of the underlying LLDD module.  You must hold host_lock of the
- * parent Scsi_Host or already have a reference when calling this.
+ * Description: Gets a reference to the scsi_device and optionally increments
+ * the use count of the associated LLDD module. You must hold host_lock of
+ * the parent Scsi_Host or already have a reference when calling this.
  */
-int scsi_device_get(struct scsi_device *sdev)
+int scsi_dev_get(struct scsi_device *sdev, bool get_lld)
 {
 	if (sdev->sdev_state == SDEV_DEL)
 		return -ENXIO;
 	if (!get_device(&sdev->sdev_gendev))
 		return -ENXIO;
-	/* We can fail this if we're doing SCSI operations
-	 * from module exit (like cache flush) */
-	try_module_get(sdev->host->hostt->module);
+	/* Can fail if invoked during module exit (like cache flush) */
+	if (get_lld && !try_module_get(sdev->host->hostt->module)) {
+		put_device(&sdev->sdev_gendev);
+		return -ENXIO;
+	}
 
 	return 0;
 }
+EXPORT_SYMBOL(scsi_dev_get);
+
+/**
+ * scsi_dev_put - release a reference to a scsi_device
+ * @sdev:	device to release a reference on
+ * @put_lld:    whether or not to decrease the LLD kernel module refcount
+ *
+ * Description: Release a reference to the scsi_device. The device is freed
+ * once the last user vanishes.
+ */
+void scsi_dev_put(struct scsi_device *sdev, bool put_lld)
+{
+	if (put_lld)
+		module_put(sdev->host->hostt->module);
+	put_device(&sdev->sdev_gendev);
+}
+EXPORT_SYMBOL(scsi_dev_put);
+
+/**
+ * scsi_device_get - get an additional reference to a scsi_device
+ * @sdev:	device to get a reference to
+ *
+ * Description: Gets a reference to the scsi_device and increments the use count
+ * of the underlying LLDD module.  You must hold host_lock of the
+ * parent Scsi_Host or already have a reference when calling this.
+ */
+int scsi_device_get(struct scsi_device *sdev)
+{
+	return scsi_dev_get(sdev, true);
+}
 EXPORT_SYMBOL(scsi_device_get);
 
 /**
- * scsi_device_put  -  release a reference to a scsi_device
- * @sdev:	device to release a reference on.
+ * scsi_device_put - release a reference to a scsi_device
+ * @sdev:	device to release a reference on
  *
  * Description: Release a reference to the scsi_device and decrements the use
  * count of the underlying LLDD module.  The device is freed once the last
@@ -1004,15 +1037,7 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-#ifdef CONFIG_MODULE_UNLOAD
-	struct module *module = sdev->host->hostt->module;
-
-	/* The module refcount will be zero if scsi_device_get()
-	 * was called from a module removal routine */
-	if (module && module_refcount(module) != 0)
-		module_put(module);
-#endif
-	put_device(&sdev->sdev_gendev);
+	scsi_dev_put(sdev, true);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/sd.c +24 -20

148 lines not on this page · raw message

include/scsi/scsi_device.h +2 -0
index 3a4edd1..a4cb852 100644
@@ -330,6 +330,8 @@ extern void scsi_remove_device(struct scsi_device *);
 extern int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh);
 void scsi_attach_vpd(struct scsi_device *sdev);
 
+extern int scsi_dev_get(struct scsi_device *, bool get_lld);
+extern void scsi_dev_put(struct scsi_device *, bool put_lld);
 extern int scsi_device_get(struct scsi_device *);
 extern void scsi_device_put(struct scsi_device *);
 extern struct scsi_device *scsi_device_lookup(struct Scsi_Host *,
Signature · 1 line
2.1.2
#1 lore raw
Quoted 5 lines · Bart Van Assche

Since kernel v3.19-rc1 module_refcount() returns 1 instead of 0 when called from inside module_exit(). This breaks the module_refcount() test in scsi_device_put() and hence causes the following kernel warning to be reported when unloading the ib_srp kernel module:

This is getting better, but I still think we need to sort out the root cause.

The problemt started with commit 39b7f1e25 ("[SCSI] sd: Fix refcounting"), which added the calls to scsi_device_get in various struct device_driver/scsi_driver methods. From the BZ it seems like the rationale was to protect against races between ->rescan and ->remove, but instead of doing that using refcounting we better ensure that in the SCSI midlayer by taking scan_mutex around calls to ->rescan. The first attached patch does that, which allows us to functionally revert 39b7f1e25, which then also allows to revert 85b6c720 ("[SCSI] sd: fix cache flushing on module removal (and individual device removal)").

See the attached series to do that. Warnings: so far it only got minimal testing.

Quoted 1 line

From f92b9687a9663d4bf304303658acfe03167e0667 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Thu, 8 Jan 2015 11:59:56 +0100
Subject: scsi: serialize ->rescan against ->remove

Take the scan_mutex when rescanning a device to protect against a concurrent device removal.

---
 drivers/scsi/scsi_scan.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +5 -3
index 0deb385..7d926cd 100644
@@ -1569,16 +1569,18 @@ EXPORT_SYMBOL(scsi_add_device);
 
 void scsi_rescan_device(struct device *dev)
 {
-	if (!dev->driver)
-		return;
+	struct scsi_device *sdev = to_scsi_device(dev);
+	struct Scsi_Host *shost = sdev->host;
 
-	if (try_module_get(dev->driver->owner)) {
+	mutex_lock(&shost->scan_mutex);
+	if (dev->driver && try_module_get(dev->driver->owner)) {
 		struct scsi_driver *drv = to_scsi_driver(dev->driver);
 
 		if (drv->rescan)
 			drv->rescan(dev);
 		module_put(dev->driver->owner);
 	}
+	mutex_unlock(&shost->scan_mutex);
 }
 EXPORT_SYMBOL(scsi_rescan_device);
 
Signature · 1 line
1.9.1
Quoted 1 line

From c056e4f177617d095eca495f94753f16043eb38b Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Thu, 8 Jan 2015 12:07:05 +0100
Subject: sd: don't grab reference device references from driver methods

The device model already takes care of races from ->remove and ->shutdown vs its other methods, and we now take care for the scsi-specific methods as well.

This is a partial, manual revert of commit 39b7f1e25 ("[SCSI] sd: Fix refcounting").

---
 drivers/scsi/sd.c | 55 +++++++++++--------------------------------------------
 1 file changed, 11 insertions(+), 44 deletions(-)
drivers/scsi/sd.c +11 -44

120 lines not on this page · raw message

Signature · 1 line
1.9.1
Quoted 1 line

From 53378be96577c3fdb31dc1bdc0a73ec49171c763 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Thu, 8 Jan 2015 12:10:29 +0100
Subject: Revert "[SCSI] sd: fix cache flushing on module removal (and
individual device removal)"

This reverts commit 85b6c720b0931101c8bcc3a5abdc2b8514b0fb4b.

We now never call scsi_device_get from the shutdown path, and this workaround turned out to create more problems than it solves. Move back to properly checking the device state and carefully handle module refcounting including checking the return value.

---
 drivers/scsi/scsi.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/scsi/scsi.c +6 -13
index 08c90a7..ff8f888 100644
@@ -975,14 +975,14 @@ EXPORT_SYMBOL(scsi_report_opcode);
  */
 int scsi_device_get(struct scsi_device *sdev)
 {
-	if (sdev->sdev_state == SDEV_DEL)
+	if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
 		return -ENXIO;
 	if (!get_device(&sdev->sdev_gendev))
 		return -ENXIO;
-	/* We can fail this if we're doing SCSI operations
-	 * from module exit (like cache flush) */
-	try_module_get(sdev->host->hostt->module);
-
+	if (!try_module_get(sdev->host->hostt->module)) {
+		put_device(&sdev->sdev_gendev);
+		return -ENXIO;
+	}
 	return 0;
 }
 EXPORT_SYMBOL(scsi_device_get);
@@ -997,14 +997,7 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-#ifdef CONFIG_MODULE_UNLOAD
-	struct module *module = sdev->host->hostt->module;
-
-	/* The module refcount will be zero if scsi_device_get()
-	 * was called from a module removal routine */
-	if (module && module_refcount(module) != 0)
-		module_put(module);
-#endif
+	module_put(sdev->host->hostt->module);
 	put_device(&sdev->sdev_gendev);
 }
 EXPORT_SYMBOL(scsi_device_put);
Signature · 1 line
1.9.1
#2 lore raw
Quoted 21 lines · Christoph Hellwig
Quoted 5 lines · Bart Van Assche

Since kernel v3.19-rc1 module_refcount() returns 1 instead of 0 when called from inside module_exit(). This breaks the module_refcount() test in scsi_device_put() and hence causes the following kernel warning to be reported when unloading the ib_srp kernel module:

This is getting better, but I still think we need to sort out the root cause.

The problemt started with commit 39b7f1e25 ("[SCSI] sd: Fix refcounting"), which added the calls to scsi_device_get in various struct device_driver/scsi_driver methods. From the BZ it seems like the rationale was to protect against races between ->rescan and ->remove, but instead of doing that using refcounting we better ensure that in the SCSI midlayer by taking scan_mutex around calls to ->rescan. The first attached patch does that, which allows us to functionally revert 39b7f1e25, which then also allows to revert 85b6c720 ("[SCSI] sd: fix cache flushing on module removal (and individual device removal)").

Quoted 2 lines

See the attached series to do that. Warnings: so far it only got minimal testing.

This seems like a good idea and the obvious (once it has been pointed out!) approach.

Perhaps not directly related to the issue at hand is this question: In scsi_rescan_device() we will now have:

	mutex_lock(&shost->scan_mutex);
	if (dev->driver && try_module_get(dev->driver->owner)) {
		struct scsi_driver *drv = to_scsi_driver(dev->driver);

		if (drv->rescan)
			drv->rescan(dev);
		module_put(dev->driver->owner);
	}
	mutex_unlock(&shost->scan_mutex);

What prevents the device from being unbound from its driver while the rescan runs? Evaluating the argument to the module_put() would then dereference a NULL pointer.

Unbind events that happen through the normal scsi_remove_host() mechanism are fine, because scsi_remove_host() locks the scan_mutex. But what about writes to the driver's sysfs "unbind" attribute?

Alan Stern

#3 lore raw
Quoted 16 lines · Alan Stern

This seems like a good idea and the obvious (once it has been pointed out!) approach.

Perhaps not directly related to the issue at hand is this question: In scsi_rescan_device() we will now have:

	mutex_lock(&shost->scan_mutex);
	if (dev->driver && try_module_get(dev->driver->owner)) {
		struct scsi_driver *drv = to_scsi_driver(dev->driver);

		if (drv->rescan)
			drv->rescan(dev);
		module_put(dev->driver->owner);
	}
	mutex_unlock(&shost->scan_mutex);
Quoted 7 lines

What prevents the device from being unbound from its driver while the rescan runs? Evaluating the argument to the module_put() would then dereference a NULL pointer.

Unbind events that happen through the normal scsi_remove_host() mechanism are fine, because scsi_remove_host() locks the scan_mutex. But what about writes to the driver's sysfs "unbind" attribute?

Looks like we should still get an unconditional reference to the device using get_device in scsi_rescan_device at least.

But this seems like a more generic problem, and at least a quick glance at the pci_driver methods seems like others don't have a good synchroniation of ->remove against random driver methods.

#4 lore raw
Quoted 21 lines · Christoph Hellwig
Quoted 20 lines · Alan Stern

This seems like a good idea and the obvious (once it has been pointed out!) approach.

Perhaps not directly related to the issue at hand is this question: In scsi_rescan_device() we will now have:

	mutex_lock(&shost->scan_mutex);
	if (dev->driver && try_module_get(dev->driver->owner)) {
		struct scsi_driver *drv = to_scsi_driver(dev->driver);

		if (drv->rescan)
			drv->rescan(dev);
		module_put(dev->driver->owner);
	}
	mutex_unlock(&shost->scan_mutex);

What prevents the device from being unbound from its driver while the rescan runs? Evaluating the argument to the module_put() would then dereference a NULL pointer.

Quoted 6 lines
Quoted 3 lines

Unbind events that happen through the normal scsi_remove_host() mechanism are fine, because scsi_remove_host() locks the scan_mutex. But what about writes to the driver's sysfs "unbind" attribute?

Looks like we should still get an unconditional reference to the device using get_device in scsi_rescan_device at least.

I'm not sure that's necessary. scsi_rescan_device is exposed by sysfs, and the kernfs core insures that the underlying device won't be deallocated while a sysfs method runs.

(scsi_rescan_device is also EXPORTed, so in theory it could be called under less safe circumstances. Perhaps then the burden should fall on the caller to guarantee that the device won't be deallocated.)

Quoted 3 lines

But this seems like a more generic problem, and at least a quick glance at the pci_driver methods seems like others don't have a good synchroniation of ->remove against random driver methods.

Can you give one or two examples?

Alan Stern

#5 lore raw
Quoted 2 lines · Alan Stern

and the kernfs core insures that the underlying device won't be deallocated while a sysfs method runs.

It has a reference to keep it from beeing freed, but so far I can't find anything that prevents ->remove from beeing called while we are in or just before a method call.

Quoted 5 lines
Quoted 3 lines

But this seems like a more generic problem, and at least a quick glance at the pci_driver methods seems like others don't have a good synchroniation of ->remove against random driver methods.

Can you give one or two examples?

I look at the sriov_configure PCI method, or the various sub-methods under pci_driver.err_handler.

#6 lore raw

sysfs methods can race with ->remove

Tejun:

The context is that we have been talking about drivers/scsi/scsi_scan.c:scsi_rescan_device(), which is called by the store_rescan_field() sysfs method in scsi_sysfs.c. The problem is
this: What happens in scsi_rescan_device if the device is unbound from its driver before the module_put call? The dev->driver->owner calculation would dereference a NULL pointer.

Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Alan Stern

and the kernfs core insures that the underlying device won't be deallocated while a sysfs method runs.

It has a reference to keep it from beeing freed, but so far I can't find anything that prevents ->remove from beeing called while we are in or just before a method call.

There are two types of methods to think about: Those registered by the subsystem and those registered by the driver.

If a method is registered by the driver, then the driver will unregister it when the ->remove routine runs. I don't know for certain, but I would expect that the sysfs/kernfs core will make sure that any existing method calls complete before unregister returns. This would prevent races.

If a method is registered by the subsystem, and if the method runs entirely within the subsystem's code, then ->remove doesn't matter. The driver could be unbound while the method is running and it would be okay.

The only time we have a problem is when the method is registered by the subsystem and the method calls into the driver. (Note that this is exactly what happens with scsi_rescan_device.)

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

But this seems like a more generic problem, and at least a quick glance at the pci_driver methods seems like others don't have a good synchroniation of ->remove against random driver methods.

Can you give one or two examples?

I look at the sriov_configure PCI method, or the various sub-methods under pci_driver.err_handler.

The sriov_numvfs_store method does have the same problem, and so does the reset_store method (by way of pci_reset_function -> pci_dev_save_and_disable -> pci_reset_notify).

Tejun, is my analysis correct? How should we fix these races?

Alan Stern

#7 lore raw

Re: sysfs methods can race with ->remove

Hello, Alan.

Quoted 4 lines · Alan Stern
Quoted 3 lines

It has a reference to keep it from beeing freed, but so far I can't find anything that prevents ->remove from beeing called while we are in or just before a method call.

Quoted 8 lines

There are two types of methods to think about: Those registered by the subsystem and those registered by the driver.

If a method is registered by the driver, then the driver will unregister it when the ->remove routine runs. I don't know for certain, but I would expect that the sysfs/kernfs core will make sure that any existing method calls complete before unregister returns. This would prevent races.

Yes, attribute deletions are blocked till the on-going sysfs read/write operations are finished and further rw accesses are failed.

Quoted 15 lines

If a method is registered by the subsystem, and if the method runs entirely within the subsystem's code, then ->remove doesn't matter. The driver could be unbound while the method is running and it would be okay.

The only time we have a problem is when the method is registered by the subsystem and the method calls into the driver. (Note that this is exactly what happens with scsi_rescan_device.)

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

But this seems like a more generic problem, and at least a quick glance at the pci_driver methods seems like others don't have a good synchroniation of ->remove against random driver methods.

Can you give one or two examples?

Quoted 8 lines
Quoted 2 lines

I look at the sriov_configure PCI method, or the various sub-methods under pci_driver.err_handler.

The sriov_numvfs_store method does have the same problem, and so does the reset_store method (by way of pci_reset_function -> pci_dev_save_and_disable -> pci_reset_notify).

Tejun, is my analysis correct? How should we fix these races?

I'm not really following what the actual problem case is, so SCSI subsystem store methods are derefing dev->driver without synchronizing against detach events? If that's the case, the solution would be synchronizing against attach/detach events? Sorry if I'm being totally idiotic. I'm having a bit of hard time jumping right in. :)

Thanks.

Signature · 1 line
tejun
#8 lore raw

Re: sysfs methods can race with ->remove

Quoted 8 lines · Tejun Heo
Quoted 5 lines

If a method is registered by the driver, then the driver will unregister it when the ->remove routine runs. I don't know for certain, but I would expect that the sysfs/kernfs core will make sure that any existing method calls complete before unregister returns. This would prevent races.

Yes, attribute deletions are blocked till the on-going sysfs read/write operations are finished and further rw accesses are failed.

Btw, where do we do that? I did a walk through the code starting from device_del, but must have missed the obvious.

Quoted 11 lines
Quoted 5 lines

The sriov_numvfs_store method does have the same problem, and so does the reset_store method (by way of pci_reset_function -> pci_dev_save_and_disable -> pci_reset_notify).

Tejun, is my analysis correct? How should we fix these races?

I'm not really following what the actual problem case is, so SCSI subsystem store methods are derefing dev->driver without synchronizing against detach events? If that's the case, the solution would be synchronizing against attach/detach events? Sorry if I'm being totally idiotic. I'm having a bit of hard time jumping right in. :)

No problem. That's the basic situation we are talking about. I have a serie fixing some long standing issues in the device model integration in SCSI, and pointed out a possible issue in that area.

So what is the proper lock to take to prevent ->remove from beeing called while in such a method? A mentioned about I tried to peel through all the layers of the onion^H^H^H^H^Hdriver core, but so far couldn't find anything obvious.

#9 lore raw

Re: sysfs methods can race with ->remove

Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Tejun Heo
Quoted 5 lines

If a method is registered by the driver, then the driver will unregister it when the ->remove routine runs. I don't know for certain, but I would expect that the sysfs/kernfs core will make sure that any existing method calls complete before unregister returns. This would prevent races.

Quoted 5 lines
Quoted 2 lines

Yes, attribute deletions are blocked till the on-going sysfs read/write operations are finished and further rw accesses are failed.

Btw, where do we do that? I did a walk through the code starting from device_del, but must have missed the obvious.

It happens in fs/kernfs/dir.c:kernfs_drain(). That routine is called when a sysfs file is removed, and it waits until all ongoing read/write operations are finished.

Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

The sriov_numvfs_store method does have the same problem, and so does the reset_store method (by way of pci_reset_function -> pci_dev_save_and_disable -> pci_reset_notify).

Tejun, is my analysis correct? How should we fix these races?

I'm not really following what the actual problem case is, so SCSI subsystem store methods are derefing dev->driver without synchronizing against detach events? If that's the case, the solution would be synchronizing against attach/detach events? Sorry if I'm being totally idiotic. I'm having a bit of hard time jumping right in. :)

Quoted 8 lines

No problem. That's the basic situation we are talking about. I have a serie fixing some long standing issues in the device model integration in SCSI, and pointed out a possible issue in that area.

So what is the proper lock to take to prevent ->remove from beeing called while in such a method? A mentioned about I tried to peel through all the layers of the onion^H^H^H^H^Hdriver core, but so far couldn't find anything obvious.

The proper lock is dev->mutex, as I mentioned in an earlier email
(http://marc.info/?l=linux-scsi&m=142176669519982&w=2). That lock is held whenever a ->remove method is called.

Alan Stern

#10 lore raw
In reply to Christoph Hellwig
Quoted 25 lines · Christoph Hellwig
Quoted 16 lines · Alan Stern

This seems like a good idea and the obvious (once it has been pointed out!) approach.

Perhaps not directly related to the issue at hand is this question: In scsi_rescan_device() we will now have:

	mutex_lock(&shost->scan_mutex);
	if (dev->driver && try_module_get(dev->driver->owner)) {
		struct scsi_driver *drv = to_scsi_driver(dev->driver);

		if (drv->rescan)
			drv->rescan(dev);
		module_put(dev->driver->owner);
	}
	mutex_unlock(&shost->scan_mutex);
Quoted 7 lines

What prevents the device from being unbound from its driver while the rescan runs? Evaluating the argument to the module_put() would then dereference a NULL pointer.

Unbind events that happen through the normal scsi_remove_host() mechanism are fine, because scsi_remove_host() locks the scan_mutex. But what about writes to the driver's sysfs "unbind" attribute?

Quoted 6 lines

Looks like we should still get an unconditional reference to the device using get_device in scsi_rescan_device at least.

But this seems like a more generic problem, and at least a quick glance at the pci_driver methods seems like others don't have a good synchroniation of ->remove against random driver methods.

This particular problem comes down to the fact that scsi_rescan_device() accesses dev->driver without appropriate mutual exclusion. SCSI's scan_mutex won't help because it doesn't protect dev->driver. Rather, dev->driver is protected by dev->mutex, and so scsi_rescan_device() needs to use device_lock/unlock.

This suggests that the scan_mutex may not be necessary at all. Historically, it seems to be quite old, predating the device model. Now that we have the device model, maybe scan_mutex simply isn't needed.

Scanning for channels or targets beneath a host should be protected by shost->gendev.mutex. Scanning for logical units beneath a target should be protected by starget->dev.mutex. Scanning for partitions beneath a SCSI drive should be protected by sdev->sdev_gendev.mutex.

James, here's a related question. Suppose userspace writes to the rescan attribute file for a disk drive for sd_probe_async() has started. What will happen? What _ought_ to happen? Do we need to call

	async_synchronize_full_domain(&scsi_sd_probe_domain);

somewhere in this pathway, or will it be okay?

Alan Stern

Tested-by on v2 1/1
#11 lore raw
In reply to Bart Van Assche
Quoted 6 lines · Bart Van Assche

Since kernel v3.19-rc1 module_refcount() returns 1 instead of 0 when called from inside module_exit(). This breaks the module_refcount() test in scsi_device_put() and hence causes the following kernel warning to be reported when unloading the ib_srp kernel module:

This patch fixed module removal crashes I was seeing with the hpsa driver running on 3.19-rc4 kernel. Ran ~200 insmod/rmmod tests with no issues.