Skip to contentSkip to conversation

scsi: fix hang when device state is set via sysfs

PATCH Applied Mike Christie 5 participants 11 messages Nov 5, 2021 – Nov 19, 2021

Subsystems (2)
lore
Share & tools
lore

Mike Christie

author maintainer
#0 lore raw

v2 cover scsi: fix hang when device state is set via sysfs

The following patches fix an issue where during iscsi recovery we deadlock due to iscsid calling into sysfs to set the device state to running, but the scsi error handler is also just finishing up.

My first attempt was here:

https://patchwork.kernel.org/project/linux-scsi/patch/[email protected]/

But the problem with that patch is that iscsid can still get stuck.

This version 2 of the patchset allows the transport class to always set the device state to running after the iscsi layer has done a relogin. It then relies on a check in the second patch where if the transport class has already set the state to running, iscsid write to the state sysfs file returns without doing a rescan. I think this is hacky, but I'm not sure how else to fix the issue without breaking iscsid or the users of the new rescan behavior.

author maintainer
#1 lore raw

PATCH v2 1/2 iscsi: Unblock session then wake up error handler

We can race where iscsi_session_recovery_timedout has woken up the error handler thread and it's now setting the devices to offline, and session_recovery_timedout's call to scsi_target_unblock is also trying to set the device's state to transport-offline. We can then get a mix of states.

For the case where we can't relogin we want the devices to be in transport-offline so when we have repaired the connection __iscsi_unblock_session can set the state back to running. So this patch has us set the device state then call into libiscsi to wake up the error handler.

---
 drivers/scsi/scsi_transport_iscsi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/scsi_transport_iscsi.c +3 -3
index 78343d3f9385..554b6f784223 100644
@@ -1899,12 +1899,12 @@ static void session_recovery_timedout(struct work_struct *work)
 	}
 	spin_unlock_irqrestore(&session->lock, flags);
 
-	if (session->transport->session_recovery_timedout)
-		session->transport->session_recovery_timedout(session);
-
 	ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
 	scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
 	ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
+
+	if (session->transport->session_recovery_timedout)
+		session->transport->session_recovery_timedout(session);
 }
 
 static void __iscsi_unblock_session(struct work_struct *work)
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH 1/2] iscsi: Unblock session then wake up error handler

Quoted 28 lines · Mike Christie

We can race where iscsi_session_recovery_timedout has woken up the error handler thread and it's now setting the devices to offline, and session_recovery_timedout's call to scsi_target_unblock is also trying to set the device's state to transport-offline. We can then get a mix of states.

For the case where we can't relogin we want the devices to be in transport-offline so when we have repaired the connection __iscsi_unblock_session can set the state back to running. So this patch has us set the device state then call into libiscsi to wake up the error handler.

---
 drivers/scsi/scsi_transport_iscsi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/scsi_transport_iscsi.c +0 -2
index 78343d3f9385..554b6f784223 100644
@@ -1899,12 +1899,12 @@ static void session_recovery_timedout(struct work_struct *work)
 	}
 	spin_unlock_irqrestore(&session->lock, flags);
 
-	if (session->transport->session_recovery_timedout)
-		session->transport->session_recovery_timedout(session);

drivers/scsi/scsi_transport_iscsi.c static void session_recovery_timedout(struct work_struct *work) · line 1910

Quoted 10 lines
drivers/scsi/scsi_transport_iscsi.c +3 -1
-
 	ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
 	scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
 	ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
+
+	if (session->transport->session_recovery_timedout)
+		session->transport->session_recovery_timedout(session);
 }
 
 static void __iscsi_unblock_session(struct work_struct *work)
#3 lore raw

Re: [PATCH 1/2] iscsi: Unblock session then wake up error handler

In reply to Mike Christie
Quoted 6 lines · Mike Christie

We can race where iscsi_session_recovery_timedout has woken up the error handler thread and it's now setting the devices to offline, and session_recovery_timedout's call to scsi_target_unblock is also trying to set the device's state to transport-offline. We can then get a mix of states.

Quoted 7 lines

For the case where we can't relogin we want the devices to be in transport-offline so when we have repaired the connection __iscsi_unblock_session can set the state back to running. So this patch has us set the device state then call into libiscsi to wake up the error handler.

[...]

Applied to 5.16/scsi-fixes, thanks!

[1/2] iscsi: Unblock session then wake up error handler

      https://git.kernel.org/mkp/scsi/c/a0c2f8b6709a
[2/2] scsi: Fix hang when device state is set via sysfs
      https://git.kernel.org/mkp/scsi/c/4edd8cd4e86d
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
author maintainer
#4 lore raw

PATCH v2 2/2 scsi: Fix hang when device state is set via sysfs

In reply to Mike Christie

This fixes a regression added with:

commit f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device")

The problem is that after iSCSI recovery, iscsid will call into the kernel to set the dev's state to running, and with that patch we now call scsi_rescan_device with the state_mutex held. If the scsi error handler thread is just starting to test the device in scsi_send_eh_cmnd then it's going to try to grab the state_mutex.

We are then stuck, because when scsi_rescan_device tries to send its IO scsi_queue_rq calls -> scsi_host_queue_ready -> scsi_host_in_recovery which will return true (the host state is still in recovery) and IO will just be requeued. scsi_send_eh_cmnd will then never be able to grab the state_mutex to finish error handling.

To prevent the deadlock this moves the rescan related code to after we drop the state_mutex.

This also adds a check for if we are already in the running state. This prevents extra scans and helps the iscsid case where if the transport class has already onlined the device during it's recovery process then we don't need userspace to do it again plus possibly block that daemon.

Fixes f0f82e2476f6 ("scsi: core: Fix capacity set to zero after

offlinining device")

Cc Bart Van Assche <[email protected]>
Cc lijinlin <[email protected]>
---
 drivers/scsi/scsi_sysfs.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_sysfs.c +19 -11
index a35841b34bfd..53e23a7bc0d3 100644
@@ -797,6 +797,7 @@ store_state_field(struct device *dev, struct device_attribute *attr,
 	int i, ret;
 	struct scsi_device *sdev = to_scsi_device(dev);
 	enum scsi_device_state state = 0;
+	bool rescan_dev = false;
 
 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
 		const int len = strlen(sdev_states[i].name);
@@ -815,20 +816,27 @@ store_state_field(struct device *dev, struct device_attribute *attr,
 	}
 
 	mutex_lock(&sdev->state_mutex);
-	ret = scsi_device_set_state(sdev, state);
-	/*
-	 * If the device state changes to SDEV_RUNNING, we need to
-	 * run the queue to avoid I/O hang, and rescan the device
-	 * to revalidate it. Running the queue first is necessary
-	 * because another thread may be waiting inside
-	 * blk_mq_freeze_queue_wait() and because that call may be
-	 * waiting for pending I/O to finish.
-	 */
-	if (ret == 0 && state == SDEV_RUNNING) {
+	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
+		ret = count;
+	} else {
+		ret = scsi_device_set_state(sdev, state);
+		if (ret == 0 && state == SDEV_RUNNING)
+			rescan_dev = true;
+	}
+	mutex_unlock(&sdev->state_mutex);
+
+	if (rescan_dev) {
+		/*
+		 * If the device state changes to SDEV_RUNNING, we need to
+		 * run the queue to avoid I/O hang, and rescan the device
+		 * to revalidate it. Running the queue first is necessary
+		 * because another thread may be waiting inside
+		 * blk_mq_freeze_queue_wait() and because that call may be
+		 * waiting for pending I/O to finish.
+		 */
 		blk_mq_run_hw_queues(sdev->request_queue, true);
 		scsi_rescan_device(dev);
 	}
-	mutex_unlock(&sdev->state_mutex);
 
 	return ret == 0 ? count : -EINVAL;
 }
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH 2/2] scsi: Fix hang when device state is set via sysfs

Quoted 77 lines · Mike Christie

This fixes a regression added with:

commit f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device")

The problem is that after iSCSI recovery, iscsid will call into the kernel to set the dev's state to running, and with that patch we now call scsi_rescan_device with the state_mutex held. If the scsi error handler thread is just starting to test the device in scsi_send_eh_cmnd then it's going to try to grab the state_mutex.

We are then stuck, because when scsi_rescan_device tries to send its IO scsi_queue_rq calls -> scsi_host_queue_ready -> scsi_host_in_recovery which will return true (the host state is still in recovery) and IO will just be requeued. scsi_send_eh_cmnd will then never be able to grab the state_mutex to finish error handling.

To prevent the deadlock this moves the rescan related code to after we drop the state_mutex.

This also adds a check for if we are already in the running state. This prevents extra scans and helps the iscsid case where if the transport class has already onlined the device during it's recovery process then we don't need userspace to do it again plus possibly block that daemon.

Fixes f0f82e2476f6 ("scsi: core: Fix capacity set to zero after

offlinining device")

Cc Bart Van Assche <[email protected]>
Cc lijinlin <[email protected]>
---
 drivers/scsi/scsi_sysfs.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_sysfs.c +16 -10
index a35841b34bfd..53e23a7bc0d3 100644
@@ -797,6 +797,7 @@ store_state_field(struct device *dev, struct device_attribute *attr,
 	int i, ret;
 	struct scsi_device *sdev = to_scsi_device(dev);
 	enum scsi_device_state state = 0;
+	bool rescan_dev = false;
 
 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
 		const int len = strlen(sdev_states[i].name);
@@ -815,20 +816,27 @@ store_state_field(struct device *dev, struct device_attribute *attr,
 	}
 
 	mutex_lock(&sdev->state_mutex);
-	ret = scsi_device_set_state(sdev, state);
-	/*
-	 * If the device state changes to SDEV_RUNNING, we need to
-	 * run the queue to avoid I/O hang, and rescan the device
-	 * to revalidate it. Running the queue first is necessary
-	 * because another thread may be waiting inside
-	 * blk_mq_freeze_queue_wait() and because that call may be
-	 * waiting for pending I/O to finish.
-	 */
-	if (ret == 0 && state == SDEV_RUNNING) {
+	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
+		ret = count;
+	} else {
+		ret = scsi_device_set_state(sdev, state);
+		if (ret == 0 && state == SDEV_RUNNING)
+			rescan_dev = true;
+	}
+	mutex_unlock(&sdev->state_mutex);
+
+	if (rescan_dev) {
+		/*
+		 * If the device state changes to SDEV_RUNNING, we need to
+		 * run the queue to avoid I/O hang, and rescan the device
+		 * to revalidate it. Running the queue first is necessary
+		 * because another thread may be waiting inside

drivers/scsi/scsi_sysfs.c store_state_field(struct device *dev, struct device_attribute *attr, · line 842

Quoted 10 lines
drivers/scsi/scsi_sysfs.c +3 -1
+		 * blk_mq_freeze_queue_wait() and because that call may be
+		 * waiting for pending I/O to finish.
+		 */
 		blk_mq_run_hw_queues(sdev->request_queue, true);
 		scsi_rescan_device(dev);
 	}
-	mutex_unlock(&sdev->state_mutex);
 
 	return ret == 0 ? count : -EINVAL;
 }
Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH 2/2] scsi: Fix hang when device state is set via sysfs

In reply to Mike Christie
Quoted 77 lines · Mike Christie

This fixes a regression added with:

commit f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device")

The problem is that after iSCSI recovery, iscsid will call into the kernel to set the dev's state to running, and with that patch we now call scsi_rescan_device with the state_mutex held. If the scsi error handler thread is just starting to test the device in scsi_send_eh_cmnd then it's going to try to grab the state_mutex.

We are then stuck, because when scsi_rescan_device tries to send its IO scsi_queue_rq calls -> scsi_host_queue_ready -> scsi_host_in_recovery which will return true (the host state is still in recovery) and IO will just be requeued. scsi_send_eh_cmnd will then never be able to grab the state_mutex to finish error handling.

To prevent the deadlock this moves the rescan related code to after we drop the state_mutex.

This also adds a check for if we are already in the running state. This prevents extra scans and helps the iscsid case where if the transport class has already onlined the device during it's recovery process then we don't need userspace to do it again plus possibly block that daemon.

Fixes f0f82e2476f6 ("scsi: core: Fix capacity set to zero after

offlinining device")

Cc Bart Van Assche <[email protected]>
Cc lijinlin <[email protected]>
---
  drivers/scsi/scsi_sysfs.c | 30 +++++++++++++++++++-----------
  1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_sysfs.c +16 -10
index a35841b34bfd..53e23a7bc0d3 100644
@@ -797,6 +797,7 @@ store_state_field(struct device *dev, struct device_attribute *attr,
  	int i, ret;
  	struct scsi_device *sdev = to_scsi_device(dev);
  	enum scsi_device_state state = 0;
+	bool rescan_dev = false;
 
  	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  		const int len = strlen(sdev_states[i].name);
@@ -815,20 +816,27 @@ store_state_field(struct device *dev, struct device_attribute *attr,
  	}
 
  	mutex_lock(&sdev->state_mutex);
-	ret = scsi_device_set_state(sdev, state);
-	/*
-	 * If the device state changes to SDEV_RUNNING, we need to
-	 * run the queue to avoid I/O hang, and rescan the device
-	 * to revalidate it. Running the queue first is necessary
-	 * because another thread may be waiting inside
-	 * blk_mq_freeze_queue_wait() and because that call may be
-	 * waiting for pending I/O to finish.
-	 */
-	if (ret == 0 && state == SDEV_RUNNING) {
+	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
+		ret = count;
+	} else {
+		ret = scsi_device_set_state(sdev, state);
+		if (ret == 0 && state == SDEV_RUNNING)
+			rescan_dev = true;
+	}
+	mutex_unlock(&sdev->state_mutex);
+
+	if (rescan_dev) {
+		/*
+		 * If the device state changes to SDEV_RUNNING, we need to
+		 * run the queue to avoid I/O hang, and rescan the device
+		 * to revalidate it. Running the queue first is necessary
+		 * because another thread may be waiting inside

drivers/scsi/scsi_sysfs.c store_state_field(struct device *dev, struct device_attribute *attr, · line 842

Quoted 10 lines
drivers/scsi/scsi_sysfs.c +3 -1
+		 * blk_mq_freeze_queue_wait() and because that call may be
+		 * waiting for pending I/O to finish.
+		 */
  		blk_mq_run_hw_queues(sdev->request_queue, true);
  		scsi_rescan_device(dev);
  	}
-	mutex_unlock(&sdev->state_mutex);
 
  	return ret == 0 ? count : -EINVAL;
  }
Signature · 1 line
Wu Bo
#7 lore raw

Re: [PATCH 2/2] scsi: Fix hang when device state is set via sysfs

In reply to Mike Christie
Quoted 65 lines · Mike Christie

This fixes a regression added with:

commit f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device")

The problem is that after iSCSI recovery, iscsid will call into the kernel
to set the dev's state to running, and with that patch we now call scsi_rescan_device with the state_mutex held. If the scsi error handler
thread is just starting to test the device in scsi_send_eh_cmnd then it's
going to try to grab the state_mutex.

We are then stuck, because when scsi_rescan_device tries to send its IO
scsi_queue_rq calls -> scsi_host_queue_ready -> scsi_host_in_recovery which will return true (the host state is still in recovery) and IO will
just be requeued. scsi_send_eh_cmnd will then never be able to grab the
state_mutex to finish error handling.

To prevent the deadlock this moves the rescan related code to after we
drop the state_mutex.

This also adds a check for if we are already in the running state. This
prevents extra scans and helps the iscsid case where if the transport class has already onlined the device during it's recovery process then we
don't need userspace to do it again plus possibly block that daemon.

Fixes f0f82e2476f6 ("scsi: core: Fix capacity set to zero after

offlinining device")

Cc Bart Van Assche <[email protected]>
Cc lijinlin <[email protected]>
---
 drivers/scsi/scsi_sysfs.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_sysfs.c +1 -0
index a35841b34bfd..53e23a7bc0d3 100644
@@ -797,6 +797,7 @@ store_state_field(struct device *dev, struct
 device_attribute *attr,
 	int i, ret;
 	struct scsi_device *sdev = to_scsi_device(dev);
 	enum scsi_device_state state = 0;
+	bool rescan_dev = false;
 
 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
drivers/scsi/scsi_sysfs.c +0 -3
 		const int len = strlen(sdev_states[i].name);
@@ -815,20 +816,27 @@ store_state_field(struct device *dev, struct
 device_attribute *attr,
 	}
 
 	mutex_lock(&sdev->state_mutex);
-	ret = scsi_device_set_state(sdev, state);
-	/*
-	 * If the device state changes to SDEV_RUNNING, we need to

drivers/scsi/scsi_sysfs.c store_state_field(struct device *dev, struct · line 822

Quoted 10 lines
drivers/scsi/scsi_sysfs.c +2 -7
-	 * run the queue to avoid I/O hang, and rescan the device
-	 * to revalidate it. Running the queue first is necessary
-	 * because another thread may be waiting inside
-	 * blk_mq_freeze_queue_wait() and because that call may be
-	 * waiting for pending I/O to finish.
-	 */
-	if (ret == 0 && state == SDEV_RUNNING) {
+	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING)
 {
+		ret = count;

This looks wrong because of this

[...]

drivers/scsi/scsi_sysfs.c store_state_field(struct device *dev, struct device_attribute *attr, · line 841

Quoted 1 line
drivers/scsi/scsi_sysfs.c +0 -0
 	return ret == 0 ? count : -EINVAL;

Don't we now return EINVAL on idempotent set state running because the count is always non-zero?

I think the first statement should be 'ret = 0;' instead to cause idempotent state setting to succeed as a nop.

James

author maintainer
#8 lore raw

Re: [PATCH 2/2] scsi: Fix hang when device state is set via sysfs

Quoted 79 lines · James Bottomley
Quoted 65 lines · Mike Christie

This fixes a regression added with:

commit f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device")

The problem is that after iSCSI recovery, iscsid will call into the kernel
to set the dev's state to running, and with that patch we now call scsi_rescan_device with the state_mutex held. If the scsi error handler
thread is just starting to test the device in scsi_send_eh_cmnd then it's
going to try to grab the state_mutex.

We are then stuck, because when scsi_rescan_device tries to send its IO
scsi_queue_rq calls -> scsi_host_queue_ready -> scsi_host_in_recovery which will return true (the host state is still in recovery) and IO will
just be requeued. scsi_send_eh_cmnd will then never be able to grab the
state_mutex to finish error handling.

To prevent the deadlock this moves the rescan related code to after we
drop the state_mutex.

This also adds a check for if we are already in the running state. This
prevents extra scans and helps the iscsid case where if the transport class has already onlined the device during it's recovery process then we
don't need userspace to do it again plus possibly block that daemon.

Fixes f0f82e2476f6 ("scsi: core: Fix capacity set to zero after

offlinining device")

Cc Bart Van Assche <[email protected]>
Cc lijinlin <[email protected]>
---
 drivers/scsi/scsi_sysfs.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_sysfs.c +1 -0
index a35841b34bfd..53e23a7bc0d3 100644
@@ -797,6 +797,7 @@ store_state_field(struct device *dev, struct
 device_attribute *attr,
 	int i, ret;
 	struct scsi_device *sdev = to_scsi_device(dev);
 	enum scsi_device_state state = 0;
+	bool rescan_dev = false;
 
 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
drivers/scsi/scsi_sysfs.c +0 -3
 		const int len = strlen(sdev_states[i].name);
@@ -815,20 +816,27 @@ store_state_field(struct device *dev, struct
 device_attribute *attr,
 	}
 
 	mutex_lock(&sdev->state_mutex);
-	ret = scsi_device_set_state(sdev, state);
-	/*
-	 * If the device state changes to SDEV_RUNNING, we need to

drivers/scsi/scsi_sysfs.c store_state_field(struct device *dev, struct · line 822

Quoted 10 lines
drivers/scsi/scsi_sysfs.c +2 -7
-	 * run the queue to avoid I/O hang, and rescan the device
-	 * to revalidate it. Running the queue first is necessary
-	 * because another thread may be waiting inside
-	 * blk_mq_freeze_queue_wait() and because that call may be
-	 * waiting for pending I/O to finish.
-	 */
-	if (ret == 0 && state == SDEV_RUNNING) {
+	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING)
 {
+		ret = count;

This looks wrong because of this

Quoted 9 lines

[...]

Quoted 1 line

return ret == 0 ? count : -EINVAL;

Don't we now return EINVAL on idempotent set state running because the count is always non-zero?

I think the first statement should be 'ret = 0;' instead to cause idempotent state setting to succeed as a nop.

You're right.

I'll resend this patchset with James's comment handled.

#9 lore raw

Re: [PATCH 2/2] scsi: Fix hang when device state is set via sysfs

Quoted 81 lines · Mike Christie
Quoted 80 lines · James Bottomley
Quoted 69 lines · Mike Christie

This fixes a regression added with:

commit f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device")

The problem is that after iSCSI recovery, iscsid will call into the kernel to set the dev's state to running, and with that patch we now call scsi_rescan_device with the state_mutex held. If the scsi error handler thread is just starting to test the device in scsi_send_eh_cmnd then it's going to try to grab the state_mutex.

We are then stuck, because when scsi_rescan_device tries to send its IO scsi_queue_rq calls -> scsi_host_queue_ready -> scsi_host_in_recovery which will return true (the host state is still in recovery) and IO will just be requeued. scsi_send_eh_cmnd will then never be able to grab the state_mutex to finish error handling.

To prevent the deadlock this moves the rescan related code to after we drop the state_mutex.

This also adds a check for if we are already in the running state. This prevents extra scans and helps the iscsid case where if the transport class has already onlined the device during it's recovery process then we don't need userspace to do it again plus possibly block that daemon.

Fixes f0f82e2476f6 ("scsi: core: Fix capacity set to zero after

offlinining device")

Cc Bart Van Assche <[email protected]>
Cc lijinlin <[email protected]>
---
 drivers/scsi/scsi_sysfs.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_sysfs.c +1 -0
index a35841b34bfd..53e23a7bc0d3 100644
@@ -797,6 +797,7 @@ store_state_field(struct device *dev, struct
 device_attribute *attr,
 	int i, ret;
 	struct scsi_device *sdev = to_scsi_device(dev);
 	enum scsi_device_state state = 0;
+	bool rescan_dev = false;
 
 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
drivers/scsi/scsi_sysfs.c +0 -0
 		const int len = strlen(sdev_states[i].name);
@@ -815,20 +816,27 @@ store_state_field(struct device *dev,

struct
device_attribute *attr,

drivers/scsi/scsi_sysfs.c +1 -10
 	}
 
 	mutex_lock(&sdev->state_mutex);
-	ret = scsi_device_set_state(sdev, state);
-	/*
-	 * If the device state changes to SDEV_RUNNING, we need to
-	 * run the queue to avoid I/O hang, and rescan the device
-	 * to revalidate it. Running the queue first is necessary
-	 * because another thread may be waiting inside
-	 * blk_mq_freeze_queue_wait() and because that call may be
-	 * waiting for pending I/O to finish.
-	 */
-	if (ret == 0 && state == SDEV_RUNNING) {
+	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING)

{

drivers/scsi/scsi_sysfs.c store_state_field(struct device *dev, struct device_attribute *attr, · line 820

Quoted 1 line
drivers/scsi/scsi_sysfs.c +1 -0
+		ret = count;

This looks wrong because of this

[...]

Quoted 1 line

return ret == 0 ? count : -EINVAL;

Don't we now return EINVAL on idempotent set state running because the count is always non-zero?

Quoted 7 lines
Quoted 3 lines

I think the first statement should be 'ret = 0;' instead to cause idempotent state setting to succeed as a nop.

You're right.

I'll resend this patchset with James's comment handled.

Send an incremental patch because this is already in the queue for Linus and he'd go ballistic if we had to rebase to remove it.

James

#10 lore raw
In reply to Mike Christie

Mike,

Quoted 3 lines

The following patches fix an issue where during iscsi recovery we deadlock due to iscsid calling into sysfs to set the device state to running, but the scsi error handler is also just finishing up.

Applied to 5.16/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering