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: 74803 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp133452vqo; Fri, 24 Mar 2023 18:19:30 -0700 (PDT) X-Google-Smtp-Source: AKy350bNtQnoW/R9syJCSkIRlz4pha03P/AfAC/H91bHoZA/LXAr771Nxg4lG9Cr8mqB2qiMKaGa X-Received: by 2002:a17:90a:a66:b0:237:c52f:a54d with SMTP id o93-20020a17090a0a6600b00237c52fa54dmr10805317pjo.21.1679707169959; Fri, 24 Mar 2023 18:19:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679707169; cv=none; d=google.com; s=arc-20160816; b=rHJYgApKr2bmp1zcP9mG8qacPyLfPiafzBMBtt7PQ4CK4mB+ADvz1oNpq31GGI9f/3 GcwlpbhkzgzXLxE3wzp59pSc0q09FR+1bsKllIa9+sOHe1KvhctTQvbmpbTg3d537Jxa Cf7Q/uwcTd2Sob51zuM5DlgSfBJg7ZEqiaCUDGQIydflRwQPSHOXUJjpwF8teIW6K4is fkNSKmj+r9bBIUo6FHkLYMD3Fx3TYCkBAHWNCdbL9xnaCEbjTqwgzrrGJgR7V/bJZ4k1 ubFxYax2O8LkEJNjvd7xVjmH5Hg7r/ZkBtrrK5E5S9UNCnEqheRPhPMnCEtoyMYxOxav FoYw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q65-20020a632a44000000b0050fa6fbde46si11452901pgq.492.2023.03.24.18.19.17; Fri, 24 Mar 2023 18:19:29 -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 S232152AbjCYBSz (ORCPT + 99 others); Fri, 24 Mar 2023 21:18:55 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761300625386000234?= X-GMAIL-MSGID: =?utf-8?q?1761300625386000234?= 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; }