Skip to contentSkip to conversation

scsi: core: put LLD module refcnt after SCSI device is released

PATCH Superseded Ming Lei 3 participants 6 messages Sep 30, 2021 – Oct 8, 2021 SCSI

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v3 1/1 scsi: core: put LLD module refcnt after SCSI device is released

SCSI host release is triggered when SCSI device is freed, and we have to make sure that LLD module won't be unloaded before SCSI host instance is released because shost->hostt is required in host release handler.

So make sure to put LLD module refcnt after SCSI device is released.

Fix one kernel panic of 'BUG: unable to handle page fault for address' reported by Changhui and Yi.

---
 drivers/scsi/scsi.c        |  4 +++-
 drivers/scsi/scsi_sysfs.c  | 12 ++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/scsi.c +3 -1
index b241f9e3885c..291ecc33b1fe 100644
@@ -553,8 +553,10 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-	module_put(sdev->host->hostt->module);
+	struct module *mod = sdev->host->hostt->module;
+
 	put_device(&sdev->sdev_gendev);
+	module_put(mod);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/scsi_sysfs.c +12 -0
index 86793259e541..9ada26814011 100644
@@ -449,9 +449,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
 	struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
 	unsigned long flags;
+	struct module *mod;
+	bool put_mod = false;
 
 	sdev = container_of(work, struct scsi_device, ew.work);
 
+	if (sdev->put_lld_mod_ref) {
+		mod = sdev->host->hostt->module;
+		put_mod = true;
+	}
+
 	scsi_dh_release_device(sdev);
 
 	parent = sdev->sdev_gendev.parent;
@@ -502,11 +509,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 
 	if (parent)
 		put_device(parent);
+	if (put_mod)
+		module_put(mod);
 }
 
 static void scsi_device_dev_release(struct device *dev)
 {
 	struct scsi_device *sdp = to_scsi_device(dev);
+
+	sdp->put_lld_mod_ref = try_module_get(sdp->host->hostt->module);
+
 	execute_in_process_context(scsi_device_dev_release_usercontext,
 				   &sdp->ew);
 }
include/scsi/scsi_device.h +1 -0
index 430b73bd02ac..54b46d590e2d 100644
@@ -206,6 +206,7 @@ struct scsi_device {
 	unsigned rpm_autosuspend:1;	/* Enable runtime autosuspend at device
 					 * creation time */
 	unsigned ignore_media_change:1; /* Ignore MEDIA CHANGE on resume */
+	unsigned put_lld_mod_ref:1;	/* Put LLD module ref in release */
 
 	bool offline_already;		/* Device offline message logged */
 
Signature · 1 line
2.31.1
Tested-by on v3 1/1
#1 lore raw

This patch fixed one panic issue which I found on s390x with blktests srp tests.
https://lore.kernel.org/linux-block/CAHj4cs8XNtkzbbiLnFmVu82wYeQpLkVp6_wCtrnbhODay+OP9w@mail.gmail.com/#t

Quoted 89 lines · Ming Lei

SCSI host release is triggered when SCSI device is freed, and we have to make sure that LLD module won't be unloaded before SCSI host instance is released because shost->hostt is required in host release handler.

So make sure to put LLD module refcnt after SCSI device is released.

Fix one kernel panic of 'BUG: unable to handle page fault for address' reported by Changhui and Yi.

---
 drivers/scsi/scsi.c        |  4 +++-
 drivers/scsi/scsi_sysfs.c  | 12 ++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/scsi.c +3 -1
index b241f9e3885c..291ecc33b1fe 100644
@@ -553,8 +553,10 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-       module_put(sdev->host->hostt->module);
+       struct module *mod = sdev->host->hostt->module;
+
        put_device(&sdev->sdev_gendev);
+       module_put(mod);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/scsi_sysfs.c +12 -0
index 86793259e541..9ada26814011 100644
@@ -449,9 +449,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
        struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
        struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
        unsigned long flags;
+       struct module *mod;
+       bool put_mod = false;
 
        sdev = container_of(work, struct scsi_device, ew.work);
 
+       if (sdev->put_lld_mod_ref) {
+               mod = sdev->host->hostt->module;
+               put_mod = true;
+       }
+
        scsi_dh_release_device(sdev);
 
        parent = sdev->sdev_gendev.parent;
@@ -502,11 +509,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 
        if (parent)
                put_device(parent);
+       if (put_mod)
+               module_put(mod);
 }
 
 static void scsi_device_dev_release(struct device *dev)
 {
        struct scsi_device *sdp = to_scsi_device(dev);
+
+       sdp->put_lld_mod_ref = try_module_get(sdp->host->hostt->module);
+
        execute_in_process_context(scsi_device_dev_release_usercontext,
                                   &sdp->ew);
 }
include/scsi/scsi_device.h +1 -1
index 430b73bd02ac..54b46d590e2d 100644
@@ -206,6 +206,7 @@ struct scsi_device {
        unsigned rpm_autosuspend:1;     /* Enable runtime autosuspend at device
                                         * creation time */
        unsigned ignore_media_change:1; /* Ignore MEDIA CHANGE on resume */
+       unsigned put_lld_mod_ref:1;     /* Put LLD module ref in release */
 
        bool offline_already;           /* Device offline message logged */
 
--

2.31.1

Signature · 2 lines
Best Regards,
  Yi Zhang
#2 lore raw
In reply to Ming Lei
Quoted 5 lines · Ming Lei

SCSI host release is triggered when SCSI device is freed, and we have to make sure that LLD module won't be unloaded before SCSI host instance is released because shost->hostt is required in host release handler.

So make sure to put LLD module refcnt after SCSI device is released.

What is a "LLD"?

Quoted 34 lines

Fix one kernel panic of 'BUG: unable to handle page fault for address' reported by Changhui and Yi.

---
 drivers/scsi/scsi.c        |  4 +++-
 drivers/scsi/scsi_sysfs.c  | 12 ++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/scsi.c +3 -1
index b241f9e3885c..291ecc33b1fe 100644
@@ -553,8 +553,10 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-	module_put(sdev->host->hostt->module);
+	struct module *mod = sdev->host->hostt->module;
+
 	put_device(&sdev->sdev_gendev);
+	module_put(mod);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/scsi_sysfs.c +0 -0
index 86793259e541..9ada26814011 100644

drivers/scsi/scsi_sysfs.c static void scsi_device_dev_release_usercontext(struct work_struct *work) · line 457

Quoted 9 lines
drivers/scsi/scsi_sysfs.c +3 -0
@@ -449,9 +449,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
 	struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
 	unsigned long flags;
+	struct module *mod;
+	bool put_mod = false;
 
 	sdev = container_of(work, struct scsi_device, ew.work);
 
+	if (sdev->put_lld_mod_ref) {

Why do you need this flag at all?

Shouldn't you just always grab/release the module? Why would you not want to?

thanks,

greg k-h

author
#3 lore raw
Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

SCSI host release is triggered when SCSI device is freed, and we have to make sure that LLD module won't be unloaded before SCSI host instance is released because shost->hostt is required in host release handler.

So make sure to put LLD module refcnt after SCSI device is released.

What is a "LLD"?

Lower level driver, which is used often as one scsi term.

Quoted 41 lines
Quoted 41 lines

Fix one kernel panic of 'BUG: unable to handle page fault for address' reported by Changhui and Yi.

---
 drivers/scsi/scsi.c        |  4 +++-
 drivers/scsi/scsi_sysfs.c  | 12 ++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/scsi.c +3 -1
index b241f9e3885c..291ecc33b1fe 100644
@@ -553,8 +553,10 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-	module_put(sdev->host->hostt->module);
+	struct module *mod = sdev->host->hostt->module;
+
 	put_device(&sdev->sdev_gendev);
+	module_put(mod);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/scsi_sysfs.c +2 -0
index 86793259e541..9ada26814011 100644
@@ -449,9 +449,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
 	struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
 	unsigned long flags;
+	struct module *mod;
+	bool put_mod = false;
 
Quoted 8 lines

drivers/scsi/scsi_sysfs.c static void scsi_device_dev_release_usercontext(struct work_struct *work) · line 457

Quoted 3 lines
drivers/scsi/scsi_sysfs.c +1 -0
 	sdev = container_of(work, struct scsi_device, ew.work);
 
+	if (sdev->put_lld_mod_ref) {

Why do you need this flag at all?

Shouldn't you just always grab/release the module? Why would you not want to?

try_module_get() may fail in scsi_device_dev_release() in case that unloading is started, then we don't need to put it in scsi_device_dev_release_usercontext(), so this flag is required.

thanks,
Ming

#4 lore raw
Quoted 59 lines · Ming Lei
Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

SCSI host release is triggered when SCSI device is freed, and we have to make sure that LLD module won't be unloaded before SCSI host instance is released because shost->hostt is required in host release handler.

So make sure to put LLD module refcnt after SCSI device is released.

What is a "LLD"?

Lower level driver, which is used often as one scsi term.

Quoted 47 lines
Quoted 35 lines

Fix one kernel panic of 'BUG: unable to handle page fault for address' reported by Changhui and Yi.

---
 drivers/scsi/scsi.c        |  4 +++-
 drivers/scsi/scsi_sysfs.c  | 12 ++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/scsi.c +3 -1
index b241f9e3885c..291ecc33b1fe 100644
@@ -553,8 +553,10 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-	module_put(sdev->host->hostt->module);
+	struct module *mod = sdev->host->hostt->module;
+
 	put_device(&sdev->sdev_gendev);
+	module_put(mod);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/scsi_sysfs.c +0 -0
index 86793259e541..9ada26814011 100644

drivers/scsi/scsi_sysfs.c static void scsi_device_dev_release_usercontext(struct work_struct *work) · line 457

Quoted 9 lines
drivers/scsi/scsi_sysfs.c +3 -0
@@ -449,9 +449,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
 	struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
 	unsigned long flags;
+	struct module *mod;
+	bool put_mod = false;
 
 	sdev = container_of(work, struct scsi_device, ew.work);
 
+	if (sdev->put_lld_mod_ref) {

Why do you need this flag at all?

Quoted 6 lines
Quoted 2 lines

Shouldn't you just always grab/release the module? Why would you not want to?

try_module_get() may fail in scsi_device_dev_release() in case that unloading is started, then we don't need to put it in scsi_device_dev_release_usercontext(), so this flag is required.

If grabing the module reference failed then you CAN NOT call into it so you have a problem here that you seem to be papering over. Just set the module pointer to NULL and then you don't need to test it when calling module_put, right?

thanks,

greg k-h

author
#5 lore raw
Quoted 63 lines · Greg Kroah-Hartman
Quoted 62 lines · Ming Lei
Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

SCSI host release is triggered when SCSI device is freed, and we have to make sure that LLD module won't be unloaded before SCSI host instance is released because shost->hostt is required in host release handler.

So make sure to put LLD module refcnt after SCSI device is released.

What is a "LLD"?

Lower level driver, which is used often as one scsi term.

Quoted 49 lines
Quoted 35 lines

Fix one kernel panic of 'BUG: unable to handle page fault for address' reported by Changhui and Yi.

---
 drivers/scsi/scsi.c        |  4 +++-
 drivers/scsi/scsi_sysfs.c  | 12 ++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/scsi.c +3 -1
index b241f9e3885c..291ecc33b1fe 100644
@@ -553,8 +553,10 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-	module_put(sdev->host->hostt->module);
+	struct module *mod = sdev->host->hostt->module;
+
 	put_device(&sdev->sdev_gendev);
+	module_put(mod);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
drivers/scsi/scsi_sysfs.c +0 -0
index 86793259e541..9ada26814011 100644

drivers/scsi/scsi_sysfs.c static void scsi_device_dev_release_usercontext(struct work_struct *work) · line 457

Quoted 9 lines
drivers/scsi/scsi_sysfs.c +3 -0
@@ -449,9 +449,16 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
 	struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
 	unsigned long flags;
+	struct module *mod;
+	bool put_mod = false;
 
 	sdev = container_of(work, struct scsi_device, ew.work);
 
+	if (sdev->put_lld_mod_ref) {

Why do you need this flag at all?

Shouldn't you just always grab/release the module? Why would you not want to?

Quoted 8 lines
Quoted 3 lines

try_module_get() may fail in scsi_device_dev_release() in case that unloading is started, then we don't need to put it in scsi_device_dev_release_usercontext(), so this flag is required.

If grabing the module reference failed then you CAN NOT call into it so you have a problem here that you seem to be papering over. Just set the module pointer to NULL and then you don't need to test it when calling module_put, right?

OK, sounds good, given the failure means that we shouldn't touch it any more.

Thanks,
Ming