Skip to contentSkip to conversation

limit set the host state by sysfs

PATCH Superseded Ye Bin 2 participants 10 messages Mar 25, 2023 – Mar 27, 2023 SCSI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

cover limit set the host state by sysfs

From: Ye Bin <[email protected]>

Now, we can set the host state by sysfs with any value. Actually, it doesn't make sense. May cause some functional issues.
This patchset introduce 'blocked' state to blocking IO, we can use this state for testing. Perhaps we can use this to do some fault recovery or firmware upgrades, as long as the driver support is good, it may be insensitive to the upper layer.

Ye Bin (5):
scsi: fix switch host state race between by sysfs and others
scsi: introduce SHOST_BLOCKED state to support blocking IO
scsi: limit to set the host state
scsi: blocking IO when host is blocked
scsi: run queue after set host state from blocked to running

 drivers/scsi/hosts.c      | 11 +++++++++++
 drivers/scsi/scsi_lib.c   |  4 ++++
 drivers/scsi/scsi_sysfs.c | 18 +++++++++++++++++-
 include/scsi/scsi_host.h  |  6 ++++++
 4 files changed, 38 insertions(+), 1 deletion(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/5 scsi: fix switch host state race between by sysfs and others

From: Ye Bin <[email protected]>

Now, switch host state by sysfs isn't hold 'shost->host_lock' lock. It may race with other process, lead to host mixed state.

---
 drivers/scsi/scsi_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/scsi_sysfs.c +7 -1
index ee28f73af4d4..cc0ae5e3def3 100644
@@ -202,6 +202,7 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
 	int i;
 	struct Scsi_Host *shost = class_to_shost(dev);
 	enum scsi_host_state state = 0;
+	unsigned long flags;
 
 	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
 		const int len = strlen(shost_states[i].name);
@@ -214,8 +215,13 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
 	if (!state)
 		return -EINVAL;
 
-	if (scsi_host_set_state(shost, state))
+	spin_lock_irqsave(shost->host_lock, flags);
+	if (scsi_host_set_state(shost, state)) {
+		spin_unlock_irqrestore(shost->host_lock, flags);
 		return -EINVAL;
+	}
+	spin_unlock_irqrestore(shost->host_lock, flags);
+
 	return count;
 }
 
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 1/5] scsi: fix switch host state race between by sysfs and others

Quoted 26 lines · Ye Bin

From: Ye Bin <[email protected]>

Now, switch host state by sysfs isn't hold 'shost->host_lock' lock. It may race with other process, lead to host mixed state.

---
  drivers/scsi/scsi_sysfs.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/scsi_sysfs.c +1 -0
index ee28f73af4d4..cc0ae5e3def3 100644
@@ -202,6 +202,7 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
  	int i;
  	struct Scsi_Host *shost = class_to_shost(dev);
  	enum scsi_host_state state = 0;
+	unsigned long flags;
 
  	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  		const int len = strlen(shost_states[i].name);
@@ -214,8 +215,13 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
  	if (!state)
  		return -EINVAL;
 

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

Quoted 10 lines
drivers/scsi/scsi_sysfs.c +6 -1
-	if (scsi_host_set_state(shost, state))
+	spin_lock_irqsave(shost->host_lock, flags);
+	if (scsi_host_set_state(shost, state)) {
+		spin_unlock_irqrestore(shost->host_lock, flags);
  		return -EINVAL;
+	}
+	spin_unlock_irqrestore(shost->host_lock, flags);
+
  	return count;
  }

Please make sure that there is only one spin_unlock_irqrestore() call in this function.

Thanks,

Bart.

author
#3 lore raw

PATCH 2/5 scsi: introduce SHOST_BLOCKED state to support blocking IO

In reply to Ye Bin

From: Ye Bin <[email protected]>

SHOST_BLOCKED state to blocking io in block layer. This state use for test, Only running state and blocked state can be switched to each other.

---
 drivers/scsi/hosts.c      | 11 +++++++++++
 drivers/scsi/scsi_sysfs.c |  1 +
 include/scsi/scsi_host.h  |  1 +
 3 files changed, 13 insertions(+)
drivers/scsi/hosts.c +11 -0
index 9b6fbbe15d92..3b497fd4d329 100644
@@ -90,6 +90,7 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
 		switch (oldstate) {
 		case SHOST_CREATED:
 		case SHOST_RECOVERY:
+		case SHOST_BLOCKED:
 			break;
 		default:
 			goto illegal;
@@ -99,6 +100,7 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
 	case SHOST_RECOVERY:
 		switch (oldstate) {
 		case SHOST_RUNNING:
+		case SHOST_BLOCKED:
 			break;
 		default:
 			goto illegal;
@@ -109,6 +111,7 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
 		switch (oldstate) {
 		case SHOST_CREATED:
 		case SHOST_RUNNING:
+		case SHOST_BLOCKED:
 		case SHOST_CANCEL_RECOVERY:
 			break;
 		default:
@@ -144,6 +147,14 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
 			goto illegal;
 		}
 		break;
+	case SHOST_BLOCKED:
+		switch (oldstate) {
+		case SHOST_RUNNING:
+			break;
+		default:
+			goto illegal;
+		}
+		break;
 	}
 	shost->shost_state = state;
 	return 0;
drivers/scsi/scsi_sysfs.c +1 -0
index cc0ae5e3def3..b14f95ac594e 100644
@@ -69,6 +69,7 @@ static const struct {
 	{ SHOST_RECOVERY, "recovery" },
 	{ SHOST_CANCEL_RECOVERY, "cancel/recovery" },
 	{ SHOST_DEL_RECOVERY, "deleted/recovery", },
+	{ SHOST_BLOCKED, "blocked", },
 };
 const char *scsi_host_state_name(enum scsi_host_state state)
 {
include/scsi/scsi_host.h +1 -0
index 587cc767bb67..9e99317b11fa 100644
@@ -527,6 +527,7 @@ enum scsi_host_state {
 	SHOST_RECOVERY,
 	SHOST_CANCEL_RECOVERY,
 	SHOST_DEL_RECOVERY,
+	SHOST_BLOCKED,
 };
 
 struct Scsi_Host {
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH 2/5] scsi: introduce SHOST_BLOCKED state to support blocking IO

Quoted 31 lines · Ye Bin
drivers/scsi/hosts.c +3 -0
index 9b6fbbe15d92..3b497fd4d329 100644
@@ -90,6 +90,7 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
  		switch (oldstate) {
  		case SHOST_CREATED:
  		case SHOST_RECOVERY:
+		case SHOST_BLOCKED:
  			break;
  		default:
  			goto illegal;
@@ -99,6 +100,7 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
  	case SHOST_RECOVERY:
  		switch (oldstate) {
  		case SHOST_RUNNING:
+		case SHOST_BLOCKED:
  			break;
  		default:
  			goto illegal;
@@ -109,6 +111,7 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
  		switch (oldstate) {
  		case SHOST_CREATED:
  		case SHOST_RUNNING:
+		case SHOST_BLOCKED:
  		case SHOST_CANCEL_RECOVERY:
  			break;
  		default:
@@ -144,6 +147,14 @@ int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
  			goto illegal;
  		}

drivers/scsi/hosts.c int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state) · line 158

Quoted 10 lines
drivers/scsi/hosts.c +8 -0
  		break;
+	case SHOST_BLOCKED:
+		switch (oldstate) {
+		case SHOST_RUNNING:
+			break;
+		default:
+			goto illegal;
+		}
+		break;
  	}

If a host is blocked, error recovery happens and completes, the host will be unblocked. I don't think that is acceptable.

The "blocked" property is orthogonal to the host state so a new boolean member variable should be introduced in struct Scsi_Host instead of introducing a new SCSI host state.

Thanks,

Bart.

author
#5 lore raw

PATCH 3/5 scsi: limit to set the host state

In reply to Ye Bin

From: Ye Bin <[email protected]>

Now, we can set the host state with any value. Actually, it doesn't make sense. As previous patch introduce SHOST_BLOCKED state, set this state, it will blocking IO. So this patch limit to set the host with running/blocked state.

---
 drivers/scsi/scsi_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/scsi_sysfs.c +7 -1
index b14f95ac594e..42c5936c7711 100644
@@ -203,6 +203,7 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
 	int i;
 	struct Scsi_Host *shost = class_to_shost(dev);
 	enum scsi_host_state state = 0;
+	enum scsi_host_state old_state;
 	unsigned long flags;
 
 	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
@@ -216,8 +217,13 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
 	if (!state)
 		return -EINVAL;
 
+	if (state != SHOST_RUNNING && state != SHOST_BLOCKED)
+		return -EINVAL;
+
 	spin_lock_irqsave(shost->host_lock, flags);
-	if (scsi_host_set_state(shost, state)) {
+	old_state = shost->shost_state;
+	if ((old_state != SHOST_RUNNING && old_state != SHOST_BLOCKED) ||
+	    scsi_host_set_state(shost, state)) {
 		spin_unlock_irqrestore(shost->host_lock, flags);
 		return -EINVAL;
 	}
Signature · 1 line
2.31.1
#6 lore raw

Re: [PATCH 3/5] scsi: limit to set the host state

Quoted 29 lines · Ye Bin

From: Ye Bin <[email protected]>

Now, we can set the host state with any value. Actually, it doesn't make sense. As previous patch introduce SHOST_BLOCKED state, set this state, it will blocking IO. So this patch limit to set the host with running/blocked state.

---
  drivers/scsi/scsi_sysfs.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/scsi_sysfs.c +2 -0
index b14f95ac594e..42c5936c7711 100644
@@ -203,6 +203,7 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
  	int i;
  	struct Scsi_Host *shost = class_to_shost(dev);
  	enum scsi_host_state state = 0;
+	enum scsi_host_state old_state;
  	unsigned long flags;
 
  	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
@@ -216,8 +217,13 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
  	if (!state)
  		return -EINVAL;
 
+	if (state != SHOST_RUNNING && state != SHOST_BLOCKED)

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

Quoted 10 lines
drivers/scsi/scsi_sysfs.c +5 -1
+		return -EINVAL;
+
  	spin_lock_irqsave(shost->host_lock, flags);
-	if (scsi_host_set_state(shost, state)) {
+	old_state = shost->shost_state;
+	if ((old_state != SHOST_RUNNING && old_state != SHOST_BLOCKED) ||
+	    scsi_host_set_state(shost, state)) {
  		spin_unlock_irqrestore(shost->host_lock, flags);
  		return -EINVAL;
  	}

Please make sure that the "state != SHOST_RUNNING && state != SHOST_BLOCKED" check occurs only once and also that there is one spin_lock_irqsave() call in this function and only one spin_unlock_irqrestore() call.

Thanks,

Bart.

author
#7 lore raw

PATCH 4/5 scsi: blocking IO when host is blocked

In reply to Ye Bin

From: Ye Bin <[email protected]>

Unlike recovery state may block other process, blocking IO only when host is blocked.

---
 drivers/scsi/scsi_lib.c  | 4 ++++
 include/scsi/scsi_host.h | 5 +++++
 2 files changed, 9 insertions(+)
drivers/scsi/scsi_lib.c +4 -0
index b7c569a42aa4..492487717f41 100644
@@ -1731,6 +1731,10 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
 			ret = BLK_STS_OFFLINE;
 		goto out_dec_target_busy;
 	}
+
+	if (unlikely(scsi_host_blocked(shost)))
+		goto out_dec_target_busy;
+
 	if (!scsi_host_queue_ready(q, shost, sdev, cmd))
 		goto out_dec_target_busy;
 
include/scsi/scsi_host.h +5 -0
index 9e99317b11fa..571321bbb706 100644
@@ -745,6 +745,11 @@ static inline int scsi_host_in_recovery(struct Scsi_Host *shost)
 		shost->tmf_in_progress;
 }
 
+static inline int scsi_host_blocked(struct Scsi_Host *shost)
+{
+	return shost->shost_state == SHOST_BLOCKED;
+}
+
 extern int scsi_queue_work(struct Scsi_Host *, struct work_struct *);
 extern void scsi_flush_work(struct Scsi_Host *);
 
Signature · 1 line
2.31.1
#8 lore raw

Re: [PATCH 4/5] scsi: blocking IO when host is blocked

drivers/scsi/scsi_lib.c static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, · line 1736

Quoted 2 lines · Ye Bin
drivers/scsi/scsi_lib.c +2 -0
+	if (unlikely(scsi_host_blocked(shost)))
+		goto out_dec_target_busy;

I this check would be moved earlier then the atomic_inc() and atomic_dec() of scsi_target(sdev)->target_busy could be skipped.

Thanks,

Bart.

author
#9 lore raw

PATCH 5/5 scsi: run queue after set host state from blocked to running

In reply to Ye Bin

From: Ye Bin <[email protected]>

As when host is blocked, all request is blocked. After set the host state with running, e will need to ensure that these requests are started.

---
 drivers/scsi/scsi_sysfs.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/scsi/scsi_sysfs.c +3 -0
index 42c5936c7711..202d58f4f267 100644
@@ -229,6 +229,9 @@ store_shost_state(struct device *dev, struct device_attribute *attr,
 	}
 	spin_unlock_irqrestore(shost->host_lock, flags);
 
+	if (old_state == SHOST_BLOCKED && state == SHOST_RUNNING)
+		scsi_run_host_queues(shost);
+
 	return count;
 }
 
Signature · 1 line
2.31.1