Skip to contentSkip to conversation

scsi: core: Fix the scsi_device_put() might_sleep annotation

PATCH Applied Bart Van Assche 4 participants 5 messages Jan 25, 2023 – Jan 30, 2023 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: core: Fix the scsi_device_put() might_sleep annotation

Although most calls of scsi_device_put() happen from non-atomic context, alua_rtpg_queue() calls this function from atomic context if alua_rtpg_queue() itself is called from atomic context. 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. Hence move the might_sleep() annotation from scsi_device_put() into scsi_device_dev_release().

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

Note: a significant part of the above description was written by Martin Wilck.

Fixes f93ed747e2c7 ("scsi: core: Release SCSI devices synchronously")
Cc Martin Wilck <[email protected]>
Cc Steffen Maier <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Sachin Sant <[email protected]>
Cc Benjamin Block <[email protected]>
---
 drivers/scsi/scsi.c       | 2 --
 drivers/scsi/scsi_sysfs.c | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/scsi.c +0 -2
index 1426b9b03612..9feb0323bc44 100644
@@ -588,8 +588,6 @@ 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);
 }
drivers/scsi/scsi_sysfs.c +2 -0
index 981d1bab2120..8ef9a5494340 100644
@@ -451,6 +451,8 @@ static void scsi_device_dev_release(struct device *dev)
 	struct scsi_vpd *vpd_pgb0 = NULL, *vpd_pgb1 = NULL, *vpd_pgb2 = NULL;
 	unsigned long flags;
 
+	might_sleep();
+
 	scsi_dh_release_device(sdev);
 
 	parent = sdev->sdev_gendev.parent;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · Bart Van Assche

Although most calls of scsi_device_put() happen from non-atomic context,
alua_rtpg_queue() calls this function from atomic context if alua_rtpg_queue() itself is called from atomic context. 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. Hence move the might_sleep() annotation from scsi_device_put() into scsi_device_dev_release().

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

Note: a significant part of the above description was written by Martin
Wilck.

Fixes f93ed747e2c7 ("scsi: core: Release SCSI devices
Quoted 8 lines

synchronously")

Cc Martin Wilck <[email protected]>
Cc Steffen Maier <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Sachin Sant <[email protected]>
Cc Benjamin Block <[email protected]>
#2 lore raw
In reply to Bart Van Assche
Quoted 11 lines · Bart Van Assche

Although most calls of scsi_device_put() happen from non-atomic context, alua_rtpg_queue() calls this function from atomic context if alua_rtpg_queue() itself is called from atomic context. 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. Hence move the might_sleep() annotation from scsi_device_put() into scsi_device_dev_release().

[...]

Applied to 6.2/scsi-fixes, thanks!

[1/1] scsi: core: Fix the scsi_device_put() might_sleep annotation

      https://git.kernel.org/mkp/scsi/c/2542fc9578d4
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#3 lore raw
Quoted 12 lines · Martin K. Petersen
Quoted 10 lines · Bart Van Assche

Although most calls of scsi_device_put() happen from non-atomic context, alua_rtpg_queue() calls this function from atomic context if alua_rtpg_queue() itself is called from atomic context. 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. Hence move the might_sleep() annotation from scsi_device_put() into scsi_device_dev_release().

Quoted 3 lines
Quoted 1 line

[...]

Applied to 6.2/scsi-fixes, thanks!

This is a really bad idea. Instead of actually catching scsi_device_put put from a wrong context all the time, it now limits to the final put and thus making the annotation a lot less useful.

#4 lore raw
Quoted 19 lines · Christoph Hellwig
Quoted 18 lines · Martin K. Petersen
Quoted 16 lines · Bart Van Assche

Although most calls of scsi_device_put() happen from non-atomic context,
alua_rtpg_queue() calls this function from atomic context if alua_rtpg_queue() itself is called from atomic context. 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. Hence move the might_sleep() annotation from scsi_device_put() into scsi_device_dev_release().

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

[...]

Applied to 6.2/scsi-fixes, thanks!

This is a really bad idea.  Instead of actually catching scsi_device_put
put from a wrong context all the time, it now limits to the final put and thus making the annotation a lot less useful.

The other idea that we discussed was this:

https://lore.kernel.org/linux-scsi/[email protected]/T/#t

That approach would still catch other wrong call contexts (i.e. from outside the ALUA code). But it requires adding a new exported function in scsi.c. Would you prefer that, or do you have something completely different in mind?

Idea #3 was to use a deferred call (e.g. a workqueue) for dropping the additional ref that had been takein in alua_rtpg_queue().

Thanks,
Martin