From nobody Sat Feb 7 03:04:08 2026 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 D0E87C4332F for ; Wed, 4 Jan 2023 04:25:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234276AbjADEZe (ORCPT ); Tue, 3 Jan 2023 23:25:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229802AbjADEZ2 (ORCPT ); Tue, 3 Jan 2023 23:25:28 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0AB3102D for ; Tue, 3 Jan 2023 20:25:26 -0800 (PST) Received: from kwepemi100025.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NmxH14VVyzqTLB; Wed, 4 Jan 2023 12:20:45 +0800 (CST) Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by kwepemi100025.china.huawei.com (7.221.188.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 4 Jan 2023 12:25:23 +0800 From: "Longpeng(Mike)" To: , , CC: , , , , , Longpeng Subject: [PATCH v3] vp_vdpa: harden the logic of set status Date: Wed, 4 Jan 2023 12:25:19 +0800 Message-ID: <20230104042519.170-1-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemi100025.china.huawei.com (7.221.188.158) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Longpeng 1. We should not set status to 0 when invoking vp_vdpa_set_status(), trigger a warning in that case. 2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device. But we also don't want to keep us in an infinite loop forever, so wait for 5s if we try to reset the device. Signed-off-by: Longpeng --- Changes v3->v2: - move VP_VDPA_RESET_TIMEOUT_US near the other macros. [Stefano] - refer v1.2 in comments. [Stefano] - s/keep/keeping/ [Jason] - use readx_poll_timeout. [Jason] Changes v1->v2: - use WARN_ON instead of BUG_ON. [Stefano] - use "warning + failed" instead of "infinite loop". [Jason, Stefano] - use usleep_range instead of msleep (checkpatch). [Longpeng] --- drivers/vdpa/virtio_pci/vp_vdpa.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/drivers/vdpa/virtio_pci/vp_vdpa.c b/drivers/vdpa/virtio_pci/vp= _vdpa.c index d448db0c4de3..3fc496aea456 100644 --- a/drivers/vdpa/virtio_pci/vp_vdpa.c +++ b/drivers/vdpa/virtio_pci/vp_vdpa.c @@ -10,6 +10,7 @@ =20 #include #include +#include #include #include #include @@ -22,6 +23,7 @@ #define VP_VDPA_QUEUE_MAX 256 #define VP_VDPA_DRIVER_NAME "vp_vdpa" #define VP_VDPA_NAME_SIZE 256 +#define VP_VDPA_RESET_TIMEOUT_US 5000000 /* 5s */ =20 struct vp_vring { void __iomem *notify; @@ -214,6 +216,9 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa= , u8 status) struct virtio_pci_modern_device *mdev =3D vp_vdpa_to_mdev(vp_vdpa); u8 s =3D vp_vdpa_get_status(vdpa); =20 + /* We should never be setting status to 0. */ + WARN_ON(status =3D=3D 0); + if (status & VIRTIO_CONFIG_S_DRIVER_OK && !(s & VIRTIO_CONFIG_S_DRIVER_OK)) { vp_vdpa_request_irq(vp_vdpa); @@ -226,10 +231,25 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa) { struct vp_vdpa *vp_vdpa =3D vdpa_to_vp(vdpa); struct virtio_pci_modern_device *mdev =3D vp_vdpa_to_mdev(vp_vdpa); - u8 s =3D vp_vdpa_get_status(vdpa); + u8 tmp, s =3D vp_vdpa_get_status(vdpa); + int ret; =20 vp_modern_set_status(mdev, 0); =20 + /* + * As the virtio v1.1/v1.2 spec (4.1.4.3.2) says: After writing 0 to + * device_status, the driver MUST wait for a read of device_status + * to return 0 before reinitializing the device. + * To avoid keeping us here forever, we only wait for 5 seconds. + */ + ret =3D readx_poll_timeout(vp_ioread8, &mdev->common->device_status, tmp, + tmp =3D=3D 0, 1000, VP_VDPA_RESET_TIMEOUT_US); + if (ret) { + dev_err(&mdev->pci_dev->dev, + "vp_vdpa: fail to reset device, %d\n", ret); + return ret; + } + if (s & VIRTIO_CONFIG_S_DRIVER_OK) vp_vdpa_free_irq(vp_vdpa); =20 --=20 2.23.0