Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1913323rwl; Fri, 24 Mar 2023 18:19:16 -0700 (PDT) X-Google-Smtp-Source: AK7set/kINd5k0BrUw0QvDwKOq2Y/8yn5G2CGNgi5DTuQg44D5rOMapXy/FGma1Vkt7rM8lnsacC X-Received: by 2002:a05:6a21:3286:b0:da:cde6:ad75 with SMTP id yt6-20020a056a21328600b000dacde6ad75mr8424857pzb.22.1679707156003; Fri, 24 Mar 2023 18:19:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679707155; cv=none; d=google.com; s=arc-20160816; b=j20TfcpwZNpBRycouFkxQ/9AF8zXuapA6QUORYAH38DWB0slsBb55WBqN2D6EY6CdI DKTHwAMvN92Ezs2EECEvkcmIBnJb4pDRlBIan83vO3/zYrjYqTFNZ8LMOE1rwnf2uLkE 2bBFqQe1EE97mX/93H8JUJn5qvPP+f88QeCmTJYv6kyI47VE7d9YL7SXvCL/khQGszlY rBo53MTpJqramq1OM41l4h7yrlAV233hA2hAa0tYHhUKYpg1rGFybkKDy4EqoQN9gYwA XjdDThv7hG4+tSprMDCJlqVYIwN33id1srClGioR+FVncpRENpnLVvZueX5ffo4G9jVa /njg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=vi542iX6hrQ2Lrfh0b71xX+tOq3WDYP9v1njrLx2sqk=; b=HAvbwvB7YEflvfanrRWFygr6dse3mNehrUDLpdriepJL7sNn3PkzG6fakxA9R5ezcF vDkfMMqomZl5zRZ5hd7E37cl4YRVsiC+aUs9CpjX/GLhrqbFSSFXDOSYmhEqaJ8XTUyS kiZo1vNmHLwtkgJN7loSdzQ7pxhJSHMxqb+ZB72f5zay6lfOUKMrrhKVwLXyu2yd0Q8F S2Btoe2qu48ykz9nmZOQJZKBkViKGrF+n/KunDZfVtBgEfurozxBOdrnphqCWSmH/NWm YeA4WwjqumQtj7kCoN/3v8H7HejGWCa5atCgE1gM5en/BuxSaWnJRwtVSREDocC5SuDY XZ0Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h187-20020a636cc4000000b0050bf68736dbsi22278177pgc.615.2023.03.24.18.19.03; Fri, 24 Mar 2023 18:19:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232148AbjCYBSy (ORCPT + 99 others); Fri, 24 Mar 2023 21:18:54 -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-1-yebin@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _Ch0CgCnUiDpSx5kmy0NFg--.49234S5 X-Coremail-Antispam: 1UD129KBjvJXoWrtrykGFWxZw15JF4kur17Wrg_yoW8Jry8pF WDJa4a9w4Uuws2gwsI9r45Xr9xK34kGrnFgFWIgwn5AFy8J3y5Kws5Ca4UXay5JFWxWFs7 JF4Dtr98CayUtF7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvmb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_Jrv_JF4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV WxJVW8Jr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx 0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWU JVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJV W8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF 1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6x IIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvE x4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnU UI43ZEXa7IU14CJJUUUUU== X-CM-SenderInfo: p1hex046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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; } -- 2.31.1