From patchwork Sat Mar 25 01:17:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ye Bin X-Patchwork-Id: 667153 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B35AC761AF for ; Sat, 25 Mar 2023 01:18:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231984AbjCYBSp (ORCPT ); Fri, 24 Mar 2023 21:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229920AbjCYBSk (ORCPT ); Fri, 24 Mar 2023 21:18:40 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C71531A64D; Fri, 24 Mar 2023 18:18:38 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Pk1Rt1zqmz4f3v4j; Sat, 25 Mar 2023 09:18:34 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP3 (Coremail) with SMTP id _Ch0CgCnUiDpSx5kmy0NFg--.49234S5; Sat, 25 Mar 2023 09:18:35 +0800 (CST) From: Ye Bin To: jejb@linux.ibm.com, martin.petersen@oracle.com, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ye Bin Subject: [PATCH 1/5] scsi: fix switch host state race between by sysfs and others Date: Sat, 25 Mar 2023 09:17:30 +0800 Message-Id: <20230325011734.507453-2-yebin@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230325011734.507453-1-yebin@huaweicloud.com> References: <20230325011734.507453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4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnU UI43ZEXa7IU14CJJUUUUU== X-CM-SenderInfo: p1hex046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ye Bin 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 --- 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; }