diff mbox series

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

Message ID 20230325011734.507453-2-yebin@huaweicloud.com
State New
Headers show
Series limit set the host state by sysfs | expand

Commit Message

Ye Bin March 25, 2023, 1:17 a.m. UTC
From: Ye Bin <yebin10@huawei.com>

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

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 drivers/scsi/scsi_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Bart Van Assche March 27, 2023, 9:26 p.m. UTC | #1
On 3/24/23 18:17, Ye Bin wrote:
> From: Ye Bin <yebin10@huawei.com>
> 
> Now, switch host state by sysfs isn't hold 'shost->host_lock' lock.
> It may race with other process, lead to host mixed state.
> 
> Signed-off-by: Ye Bin <yebin10@huawei.com>
> ---
>   drivers/scsi/scsi_sysfs.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
> index ee28f73af4d4..cc0ae5e3def3 100644
> --- a/drivers/scsi/scsi_sysfs.c
> +++ b/drivers/scsi/scsi_sysfs.c
> @@ -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;
>   }

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

Thanks,

Bart.
diff mbox series

Patch

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index ee28f73af4d4..cc0ae5e3def3 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -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;
 }