Skip to contentSkip to conversation

scsi: add non-sleeping variant of scsi_device_put() and use it in alua

PATCH Superseded [email protected] 2 participants 3 messages Jan 24, 2023 SCSI

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: add non-sleeping variant of scsi_device_put() and use it in alua

From: Martin Wilck <[email protected]>

Since the might_sleep() annotation was added in scsi_device_put() and alua_rtpg_queue(), we have seen repeated reports of "BUG: sleeping function called from invalid context" [1], [2]. alua_rtpg_queue() is always called from contexts where the caller must hold at least one reference to the scsi device in question. This means that the reference taken by alua_rtpg_queue() itself can't be the last one, and thus can be dropped without entering the code path in which scsi_device_put() might actually sleep.

Add a new helper function, scsi_device_put_nosleep() for cases like this, where a device reference is put from atomic context, and at the same time it is certain that this reference is not the last one, and use it from alua_rtpg_queue().

[1] https://lore.kernel.org/linux-scsi/[email protected]/
[2] https://lore.kernel.org/linux-scsi/[email protected]/

Fixes f93ed747e2c7 ("scsi: core: Release SCSI devices synchronously")
Cc Bart Van Assche <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Sachin Sant <[email protected]>
Cc Benjamin Block <[email protected]>
Cc Dan Carpenter <[email protected]>
---
 drivers/scsi/device_handler/scsi_dh_alua.c |  4 +---
 drivers/scsi/scsi.c                        | 23 ++++++++++++++++++----
 include/scsi/scsi_device.h                 |  1 +
 3 files changed, 21 insertions(+), 7 deletions(-)
drivers/scsi/device_handler/scsi_dh_alua.c +1 -3
index 49cc18a87473..bdfcea1c16cb 100644
@@ -989,8 +989,6 @@ static bool alua_rtpg_queue(struct alua_port_group *pg,
 	int start_queue = 0;
 	unsigned long flags;
 
-	might_sleep();
-
 	if (WARN_ON_ONCE(!pg) || scsi_device_get(sdev))
 		return false;
 
@@ -1031,7 +1029,7 @@ static bool alua_rtpg_queue(struct alua_port_group *pg,
 			kref_put(&pg->kref, release_port_group);
 	}
 	if (sdev)
-		scsi_device_put(sdev);
+		scsi_device_put_nosleep(sdev);
 
 	return true;
 }
drivers/scsi/scsi.c +19 -4
index 1426b9b03612..eec52bb298a7 100644
@@ -576,6 +576,24 @@ int scsi_device_get(struct scsi_device *sdev)
 }
 EXPORT_SYMBOL(scsi_device_get);
 
+/**
+ * scsi_device_put_nosleep  -  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. This function may only be called from
+ * a call context where it is certain that the reference dropped is not the
+ * last one.
+ */
+void scsi_device_put_nosleep(struct scsi_device *sdev)
+{
+	struct module *mod = sdev->host->hostt->module;
+
+	put_device(&sdev->sdev_gendev);
+	module_put(mod);
+}
+EXPORT_SYMBOL(scsi_device_put);
+
 /**
  * scsi_device_put  -  release a reference to a scsi_device
  * @sdev:	device to release a reference on.
@@ -586,12 +604,9 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
-	struct module *mod = sdev->host->hostt->module;
-
 	might_sleep();
 
-	put_device(&sdev->sdev_gendev);
-	module_put(mod);
+	scsi_device_put_nosleep(sdev);
 }
 EXPORT_SYMBOL(scsi_device_put);
 
include/scsi/scsi_device.h +1 -0
index 3642b8e3928b..85cffa07f2c0 100644
@@ -365,6 +365,7 @@ void scsi_attach_vpd(struct scsi_device *sdev);
 
 extern struct scsi_device *scsi_device_from_queue(struct request_queue *q);
 extern int __must_check scsi_device_get(struct scsi_device *);
+extern void scsi_device_put_nosleep(struct scsi_device *);
 extern void scsi_device_put(struct scsi_device *);
 extern struct scsi_device *scsi_device_lookup(struct Scsi_Host *,
 					      uint, uint, u64);
Signature · 1 line
2.39.0
#1 lore raw
Quoted 38 lines · On 1/24/23 13:07, [email protected] wrote:

From: Martin Wilck <[email protected]>

Since the might_sleep() annotation was added in scsi_device_put() and alua_rtpg_queue(), we have seen repeated reports of "BUG: sleeping function called from invalid context" [1], [2]. alua_rtpg_queue() is always called from contexts where the caller must hold at least one reference to the scsi device in question. This means that the reference taken by alua_rtpg_queue() itself can't be the last one, and thus can be dropped without entering the code path in which scsi_device_put() might actually sleep.

Add a new helper function, scsi_device_put_nosleep() for cases like this, where a device reference is put from atomic context, and at the same time it is certain that this reference is not the last one, and use it from alua_rtpg_queue().

[1] https://lore.kernel.org/linux-scsi/[email protected]/
[2] https://lore.kernel.org/linux-scsi/[email protected]/

Fixes f93ed747e2c7 ("scsi: core: Release SCSI devices synchronously")
Cc Bart Van Assche <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Sachin Sant <[email protected]>
Cc Benjamin Block <[email protected]>
Cc Dan Carpenter <[email protected]>
---
  drivers/scsi/device_handler/scsi_dh_alua.c |  4 +---
  drivers/scsi/scsi.c                        | 23 ++++++++++++++++++----
  include/scsi/scsi_device.h                 |  1 +
  3 files changed, 21 insertions(+), 7 deletions(-)
drivers/scsi/device_handler/scsi_dh_alua.c +0 -0
index 49cc18a87473..bdfcea1c16cb 100644

drivers/scsi/device_handler/scsi_dh_alua.c static bool alua_rtpg_queue(struct alua_port_group *pg, · line 994

Quoted 8 lines
drivers/scsi/device_handler/scsi_dh_alua.c +0 -2
@@ -989,8 +989,6 @@ static bool alua_rtpg_queue(struct alua_port_group *pg,
  	int start_queue = 0;
  	unsigned long flags;
 
-	might_sleep();
-
  	if (WARN_ON_ONCE(!pg) || scsi_device_get(sdev))
  		return false;
 

I think Martin and James have already picked this hunk from Bart's last fixup [1] and sent a pull request including it to Linus [2].

At least, your patch did not apply cleanly for me, as I also had removed those two lines in my tree already.
Am applying the remaining parts to see how it performs in our CI (although this seems to occur seldomly so it'll be hard to tell whether it fixes the "problem").

[1] https://lore.kernel.org/linux-scsi/[email protected]/
[2] https://lore.kernel.org/linux-scsi/87b5e16ec007de3523fd78534a48d6244bda3f46.camel@HansenPartnership.com/

Signature · 11 lines
Mit freundlichen Gruessen / Kind regards
Steffen Maier

Linux on IBM Z and LinuxONE

https://www.ibm.com/privacy/us/en/
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Gregor Pillen
Geschaeftsfuehrung: David Faller
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294
#2 lore raw
In reply to [email protected]
Quoted 15 lines · On 1/24/23 13:07, [email protected] wrote:
drivers/scsi/scsi.c +7 -0
index 1426b9b03612..eec52bb298a7 100644
@@ -576,6 +576,24 @@ int scsi_device_get(struct scsi_device *sdev)
  }
  EXPORT_SYMBOL(scsi_device_get);
 
+/**
+ * scsi_device_put_nosleep  -  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. This function may only be called from
+ * a call context where it is certain that the reference dropped is not the

drivers/scsi/scsi.c int scsi_device_get(struct scsi_device *sdev) · line 595

Quoted 10 lines
drivers/scsi/scsi.c +10 -0
+ * last one.
+ */
+void scsi_device_put_nosleep(struct scsi_device *sdev)
+{
+	struct module *mod = sdev->host->hostt->module;
+
+	put_device(&sdev->sdev_gendev);
+	module_put(mod);
+}
+EXPORT_SYMBOL(scsi_device_put);

+EXPORT_SYMBOL(scsi_device_put_nosleep);

otherwise I get:

Quoted 46 lines
drivers/scsi/scsi.c +0 -0
  CC [M]  drivers/scsi/scsi.o
 In file included from ./include/linux/linkage.h:7,
                 from ./include/linux/preempt.h:10,
                 from ./arch/s390/include/asm/timex.h:13,
                 from ./include/linux/timex.h:67,
                 from ./include/linux/time32.h:13,
                 from ./include/linux/time.h:60,
                 from ./include/linux/stat.h:19,
                 from ./include/linux/module.h:13,
                 from drivers/scsi/scsi.c:41:

./include/linux/export.h:57:43: error: redefinition of ‘__ksymtab_scsi_device_put’
57 | static const struct kernel_symbol __ksymtab_##sym \
| ^~~~~~~~~~
./include/linux/export.h:96:9: note: in expansion of macro ‘__KSYMTAB_ENTRY’

   96 |         __KSYMTAB_ENTRY(sym, sec)
      |         ^~~~~~~~~~~~~~~
./include/linux/export.h:140:41: note: in expansion of macro ‘___EXPORT_SYMBOL’
  140 | #define __EXPORT_SYMBOL(sym, sec, ns)   ___EXPORT_SYMBOL(sym, sec, ns)
      |                                         ^~~~~~~~~~~~~~~~
./include/linux/export.h:147:41: note: in expansion of macro ‘__EXPORT_SYMBOL’
  147 | #define _EXPORT_SYMBOL(sym, sec)        __EXPORT_SYMBOL(sym, sec, "")
      |                                         ^~~~~~~~~~~~~~~
./include/linux/export.h:150:41: note: in expansion of macro ‘_EXPORT_SYMBOL’
  150 | #define EXPORT_SYMBOL(sym)              _EXPORT_SYMBOL(sym, "")
      |                                         ^~~~~~~~~~~~~~
drivers/scsi/scsi.c:611:1: note: in expansion of macro ‘EXPORT_SYMBOL’
  611 | EXPORT_SYMBOL(scsi_device_put);
      | ^~~~~~~~~~~~~

./include/linux/export.h:57:43: note: previous definition of ‘__ksymtab_scsi_device_put’ with type ‘const struct kernel_symbol’
57 | static const struct kernel_symbol __ksymtab_##sym \
| ^~~~~~~~~~
./include/linux/export.h:96:9: note: in expansion of macro ‘__KSYMTAB_ENTRY’

   96 |         __KSYMTAB_ENTRY(sym, sec)
      |         ^~~~~~~~~~~~~~~
./include/linux/export.h:140:41: note: in expansion of macro ‘___EXPORT_SYMBOL’
  140 | #define __EXPORT_SYMBOL(sym, sec, ns)   ___EXPORT_SYMBOL(sym, sec, ns)
      |                                         ^~~~~~~~~~~~~~~~
./include/linux/export.h:147:41: note: in expansion of macro ‘__EXPORT_SYMBOL’
  147 | #define _EXPORT_SYMBOL(sym, sec)        __EXPORT_SYMBOL(sym, sec, "")
      |                                         ^~~~~~~~~~~~~~~
./include/linux/export.h:150:41: note: in expansion of macro ‘_EXPORT_SYMBOL’
  150 | #define EXPORT_SYMBOL(sym)              _EXPORT_SYMBOL(sym, "")
      |                                         ^~~~~~~~~~~~~~
drivers/scsi/scsi.c:595:1: note: in expansion of macro ‘EXPORT_SYMBOL’
  595 | EXPORT_SYMBOL(scsi_device_put);
      | ^~~~~~~~~~~~~
Signature · 11 lines
Mit freundlichen Gruessen / Kind regards
Steffen Maier

Linux on IBM Z and LinuxONE

https://www.ibm.com/privacy/us/en/
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Gregor Pillen
Geschaeftsfuehrung: David Faller
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294