Skip to contentSkip to conversation

scsi: Fix incorrect usage of shost_for_each_device

PATCH Applied Ye Bin 4 participants 4 messages May 18, 2020 – Jun 4, 2020 SCSI

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

PATCH 1/1 scsi: Fix incorrect usage of shost_for_each_device

shost_for_each_device(sdev, shost) \
	for ((sdev) = __scsi_iterate_devices((shost), NULL); \
	     (sdev); \
	     (sdev) = __scsi_iterate_devices((shost), (sdev)))

When terminating shost_for_each_device() iteration with break or return, scsi_device_put() should be used to prevent stale scsi device references from being left behind.

---
 drivers/scsi/scsi_error.c | 2 ++
 drivers/scsi/scsi_lib.c   | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/scsi_error.c +2 -0
index 978be1602f71..927b1e641842 100644
@@ -1412,6 +1412,7 @@ static int scsi_eh_stu(struct Scsi_Host *shost,
 				sdev_printk(KERN_INFO, sdev,
 					    "%s: skip START_UNIT, past eh deadline\n",
 					    current->comm));
+			scsi_device_put(sdev);
 			break;
 		}
 		stu_scmd = NULL;
@@ -1478,6 +1479,7 @@ static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
 				sdev_printk(KERN_INFO, sdev,
 					    "%s: skip BDR, past eh deadline\n",
 					     current->comm));
+			scsi_device_put(sdev);
 			break;
 		}
 		bdr_scmd = NULL;
drivers/scsi/scsi_lib.c +3 -1
index be1a4a9a5fca..173bc7fc2836 100644
@@ -2859,8 +2859,10 @@ scsi_host_unblock(struct Scsi_Host *shost, int new_state)
 
 	shost_for_each_device(sdev, shost) {
 		ret = scsi_internal_device_unblock(sdev, new_state);
-		if (ret)
+		if (ret) {
+			scsi_device_put(sdev);
 			break;
+		}
 	}
 	return ret;
 }
Signature · 1 line
2.21.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Ye Bin
shost_for_each_device(sdev, shost) \
	for ((sdev) = __scsi_iterate_devices((shost), NULL); \
	     (sdev); \
	     (sdev) = __scsi_iterate_devices((shost), (sdev)))

When terminating shost_for_each_device() iteration with break or return, scsi_device_put() should be used to prevent stale scsi device references from being left behind.

#2 lore raw
In reply to Ye Bin
Quoted 8 lines · Ye Bin
shost_for_each_device(sdev, shost) \
	for ((sdev) = __scsi_iterate_devices((shost), NULL); \
	     (sdev); \
	     (sdev) = __scsi_iterate_devices((shost), (sdev)))

When terminating shost_for_each_device() iteration with break or return, scsi_device_put() should be used to prevent stale scsi device references from being left behind.

Applied to 5.8/scsi-queue, thanks!

[1/1] scsi: core: Fix incorrect usage of shost_for_each_device

      https://git.kernel.org/mkp/scsi/c/4dea170f4fb2
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
Reviewed-by on v1 1/1
#3 lore raw
In reply to Ye Bin
Quoted 42 lines · Ye Bin
shost_for_each_device(sdev, shost) \
	for ((sdev) = __scsi_iterate_devices((shost), NULL); \
	     (sdev); \
	     (sdev) = __scsi_iterate_devices((shost), (sdev)))

When terminating shost_for_each_device() iteration with break or return, scsi_device_put() should be used to prevent stale scsi device references from being left behind.

---
  drivers/scsi/scsi_error.c | 2 ++
  drivers/scsi/scsi_lib.c   | 4 +++-
  2 files changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/scsi_error.c +2 -0
index 978be1602f71..927b1e641842 100644
@@ -1412,6 +1412,7 @@ static int scsi_eh_stu(struct Scsi_Host *shost,
  				sdev_printk(KERN_INFO, sdev,
  					    "%s: skip START_UNIT, past eh deadline\n",
  					    current->comm));
+			scsi_device_put(sdev);
  			break;
  		}
  		stu_scmd = NULL;
@@ -1478,6 +1479,7 @@ static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  				sdev_printk(KERN_INFO, sdev,
  					    "%s: skip BDR, past eh deadline\n",
  					     current->comm));
+			scsi_device_put(sdev);
  			break;
  		}
  		bdr_scmd = NULL;
drivers/scsi/scsi_lib.c +0 -0
index be1a4a9a5fca..173bc7fc2836 100644
@@ -2859,8 +2859,10 @@ scsi_host_unblock(struct Scsi_Host *shost, int new_state)
 

drivers/scsi/scsi_lib.c scsi_host_unblock(struct Scsi_Host *shost, int new_state) · line 2868

Quoted 10 lines
drivers/scsi/scsi_lib.c +3 -1
  	shost_for_each_device(sdev, shost) {
  		ret = scsi_internal_device_unblock(sdev, new_state);
-		if (ret)
+		if (ret) {
+			scsi_device_put(sdev);
  			break;
+		}
  	}
  	return ret;
  }

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke            Teamlead Storage & Networking
[email protected]                               +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer