From patchwork Thu Nov 16 03:52:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yangxingui X-Patchwork-Id: 165687 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp2972884vqg; Wed, 15 Nov 2023 19:55:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IGFcoUO1f3ZHnca3xOObPZTf2HjHJ9AGXYHiXx3wRHAmVVBfeThMCMU4Mbn0GUttgGsGCWr X-Received: by 2002:a17:90b:2dc3:b0:280:47ba:767a with SMTP id sk3-20020a17090b2dc300b0028047ba767amr751565pjb.16.1700106936657; Wed, 15 Nov 2023 19:55:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700106936; cv=none; d=google.com; s=arc-20160816; b=JmlOELyTpOWP7poLfA8RhhWgrjPPG/zglFjQldcP1Una7mPk++90fk3zJ6Q3r1+01X IScz4n5xbzOGibwuY5TP/Gs2RjFX38hRFemzzzuVXGh88TgrxXmXG1k3xn9GaUTG/8yG vq863QDDBFl93dZknR2MkcUEIydKPnBdvC4w4RteAZNMNb6gDZSaU9npNBUVoq+tfGIr 0DhyB1VemVCkHWFlm+L/dH2ePw36beIGde5lmccNrDWYUd3vHdkcbylAZ+OTBiogPRma bB6hNQKTI5lPaEVOD0tpPCdBnxtbPY3fQ71BZkTCKojy+hQ/zUxMqTqAGlnenE14RAjW 9JkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=le1MmTicqMuRvHPq98od0fdjDNtS/FXCc0XvmF5QbCk=; fh=v1i5ALTdSFhoG6gracN8Qjox4iTWalJm97Mg20AUzFM=; b=JlobkcoZS0WXtJiSCloTR+fz/tGTE/AD6lVFHk7wDRwc0j5jr4AdGnLlK7FiHhhIu/ U5Pstq0aKrU2i4CPjEhn9EE2mR1NeLOwtTeunSgAlzJF+Mi8gZnJSYRohjJ/yTjV0WH1 9kGaPWah5coVt1NIahkMkn5C1Xxj2gnZetv3w8wjfDrgR2jfyBC1uQQ8IG6dS0AtfSKu 4rjlcNjheeWFoufHPFa9/H1OiYqqh9xDSSiW9LkNRjyaCltTU416g2R65BknBWqBvttF AzZl4m1MMOub5f6nq8TNdI5kvZhOfgNdj4i66VWwIuNakoioDUHahBVdsmSCwxteX+VM Kq0w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id g189-20020a636bc6000000b005ace065e52csi11622351pgc.336.2023.11.15.19.55.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 19:55:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id C3F6280C9CA6; Wed, 15 Nov 2023 19:55:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344617AbjKPDzc (ORCPT + 28 others); Wed, 15 Nov 2023 22:55:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbjKPDzb (ORCPT ); Wed, 15 Nov 2023 22:55:31 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB4A6192; Wed, 15 Nov 2023 19:55:27 -0800 (PST) Received: from dggpemd100001.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4SW5lT4C3lzWhNr; Thu, 16 Nov 2023 11:55:01 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpemd100001.china.huawei.com (7.185.36.94) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1258.23; Thu, 16 Nov 2023 11:55:25 +0800 From: Xingui Yang To: , , , , CC: , , , , , , Subject: [PATCH v3] scsi: libsas: Fix set zero-address when device-type != NO_DEVICE Date: Thu, 16 Nov 2023 03:52:41 +0000 Message-ID: <20231116035241.13730-1-yangxingui@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemd100001.china.huawei.com (7.185.36.94) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 15 Nov 2023 19:55:35 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782547508088806533 X-GMAIL-MSGID: 1782691330983168069 Firstly, when ex_phy is added to the parent port, ex_phy->port is not set. As a result, sas_port_delete_phy() won't be called in sas_unregister_devs_sas_addr(), and although ex_phy's sas_address is zero, it is not deleted from the parent port's phy_list. Secondly, phy->attached_sas_addr will be set to a zero-address when phy->linkrate < SAS_LINK_RATE_1_5_GBPS and device-type != NO_DEVICE during device registration, such as stp. It will create a new port and all other ex_phys whose addresses are zero will be added to the new port in sas_ex_get_linkrate(), and it may trigger BUG() as follows: [562240.051046] sas: phy19 part of wide port with phy16 [562240.051197] sas: ex 500e004aaaaaaa1f phy19:U:0 attached: 0000000000000000 (no device) [562240.051203] sas: done REVALIDATING DOMAIN on port 0, pid:435909, res 0x0 [562240.062536] sas: ex 500e004aaaaaaa1f phy0 new device attached [562240.062616] sas: ex 500e004aaaaaaa1f phy00:U:5 attached: 0000000000000000 (stp) [562240.062680] port-7:7:0: trying to add phy phy-7:7:19 fails: it's already part of another port [562240.085064] ------------[ cut here ]------------ [562240.096612] kernel BUG at drivers/scsi/scsi_transport_sas.c:1083! [562240.109611] Internal error: Oops - BUG: 0 [#1] SMP [562240.343518] Process kworker/u256:3 (pid: 435909, stack limit = 0x0000000003bcbebf) [562240.421714] Workqueue: 0000:b4:02.0_disco_q sas_revalidate_domain [libsas] [562240.437173] pstate: 40c00009 (nZcv daif +PAN +UAO) [562240.450478] pc : sas_port_add_phy+0x13c/0x168 [scsi_transport_sas] [562240.465283] lr : sas_port_add_phy+0x13c/0x168 [scsi_transport_sas] [562240.479751] sp : ffff0000300cfa70 [562240.674822] Call trace: [562240.682709] sas_port_add_phy+0x13c/0x168 [scsi_transport_sas] [562240.694013] sas_ex_get_linkrate.isra.5+0xcc/0x128 [libsas] [562240.704957] sas_ex_discover_end_dev+0xfc/0x538 [libsas] [562240.715508] sas_ex_discover_dev+0x3cc/0x4b8 [libsas] [562240.725634] sas_ex_discover_devices+0x9c/0x1a8 [libsas] [562240.735855] sas_ex_revalidate_domain+0x2f0/0x450 [libsas] [562240.746123] sas_revalidate_domain+0x158/0x160 [libsas] [562240.756014] process_one_work+0x1b4/0x448 [562240.764548] worker_thread+0x54/0x468 [562240.772562] kthread+0x134/0x138 [562240.779989] ret_from_fork+0x10/0x18 We've done the following to solve this problem: Firstly, set ex_phy->port when ex_phy is added to the parent port. And set ex_dev->parent_port to NULL when the number of PHYs of the parent port becomes 0. Secondly, don't set a zero-address for phy->attached_sas_addr when phy->attached_dev_type != NO_DEVICE. Fixes: 7d1d86518118 ("[SCSI] libsas: fix false positive 'device attached' conditions") Signed-off-by: Xingui Yang --- v2 -> v3: 1. Set ex_dev->parent_port to NULL when the number of PHYs of the parent port becomes 0 2. Update the comments v1 -> v2: 1. Set ex_phy->port with parent_port when ex_phy is added to the parent port 2. Set ex_phy to NULL when free expander 3. Update the comments --- drivers/scsi/libsas/sas_discover.c | 4 +++- drivers/scsi/libsas/sas_expander.c | 8 +++++--- drivers/scsi/libsas/sas_internal.h | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/scsi/libsas/sas_discover.c b/drivers/scsi/libsas/sas_discover.c index 8fb7c41c0962..8eb3888a9e57 100644 --- a/drivers/scsi/libsas/sas_discover.c +++ b/drivers/scsi/libsas/sas_discover.c @@ -296,8 +296,10 @@ void sas_free_device(struct kref *kref) dev->phy = NULL; /* remove the phys and ports, everything else should be gone */ - if (dev_is_expander(dev->dev_type)) + if (dev_is_expander(dev->dev_type)) { kfree(dev->ex_dev.ex_phy); + dev->ex_dev.ex_phy = NULL; + } if (dev_is_sata(dev) && dev->sata_dev.ap) { ata_sas_tport_delete(dev->sata_dev.ap); diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c index a2204674b680..89d44a9dc4e3 100644 --- a/drivers/scsi/libsas/sas_expander.c +++ b/drivers/scsi/libsas/sas_expander.c @@ -239,8 +239,7 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id, /* help some expanders that fail to zero sas_address in the 'no * device' case */ - if (phy->attached_dev_type == SAS_PHY_UNUSED || - phy->linkrate < SAS_LINK_RATE_1_5_GBPS) + if (phy->attached_dev_type == SAS_PHY_UNUSED) memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); else memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE); @@ -1844,9 +1843,12 @@ static void sas_unregister_devs_sas_addr(struct domain_device *parent, if (phy->port) { sas_port_delete_phy(phy->port, phy->phy); sas_device_set_phy(found, phy->port); - if (phy->port->num_phys == 0) + if (phy->port->num_phys == 0) { list_add_tail(&phy->port->del_list, &parent->port->sas_port_del_list); + if (ex_dev->parent_port == phy->port) + ex_dev->parent_port = NULL; + } phy->port = NULL; } } diff --git a/drivers/scsi/libsas/sas_internal.h b/drivers/scsi/libsas/sas_internal.h index 3804aef165ad..e860d5b19880 100644 --- a/drivers/scsi/libsas/sas_internal.h +++ b/drivers/scsi/libsas/sas_internal.h @@ -202,6 +202,7 @@ static inline void sas_add_parent_port(struct domain_device *dev, int phy_id) sas_port_mark_backlink(ex->parent_port); } sas_port_add_phy(ex->parent_port, ex_phy->phy); + ex_phy->port = ex->parent_port; } static inline struct domain_device *sas_alloc_device(void)