From patchwork Wed Jan 4 04:25:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 38734 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4961993wrt; Tue, 3 Jan 2023 20:30:03 -0800 (PST) X-Google-Smtp-Source: AMrXdXv0urnRszHGkoYAbsTvR4xsmla6VPeaXk7kpQNp67Q2QIUyMrnITaCxldkBe0CsA6uOC3yk X-Received: by 2002:a17:902:f80c:b0:189:ea4c:e414 with SMTP id ix12-20020a170902f80c00b00189ea4ce414mr51619614plb.61.1672806602872; Tue, 03 Jan 2023 20:30:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672806602; cv=none; d=google.com; s=arc-20160816; b=q/6Mr++1lSC3XQcBxdiU1ExlnIowSf3ye8Xbfw43cwnn/fKkB5MS2cJdYbH1dy/UEE KaRMTEWVQ+663xmLct0kKpYMlkNNofUemePLX2J8qQ/OGShwfxW7w8orXGXqmH2ufIdx gm0s+Us8bFGPFuZQOwzg+JzC6R8mKu+tbOSg1F8mEuPS/q5rLnDDSWW59wCByWyK40WX b7qZH9hqMiCgkWK6a5JBicxrRroxhD8AQTDUuZbXON6LRuRvY9oD/omcOYzmmzYiAeVJ IwWIBr/BemqxXaYqCB9XI12ziUk4PJUdxPo8hN9K2d11bvLaOsUn2AurCEm1U9E7R6pt rLvw== 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 :message-id:date:subject:cc:to:from; bh=TrtSXVMph8Xi/cgDDs+Pxu1gCmLWOTdmflFznOLQsYU=; b=rswbCPZTYm0Hv29Z6HAmhV2WbEZU4A0IRFO/c4X5AMvsPGxt4gertXfi67tPS2o4AJ uLFW6du2XZZs5ukKt8xSbkjti9lk1G5BkEmX0pYpFfAsXJplw7asnrk/mde6+O1tYy8E magglXpLuWq+WeZL1Tg03U26Q/ZaHhHlQiZxfVQIMnOuNFd6M6aNjeJIadMoUEeJhwbD Dqo4gS/Vf0S2l57UFCEcHkUK83Y+guT+n8CDQodLoxeAn5XQc4uGsZyiPHqK+9OW2jVg cswbajQcwPY4Pq3JO0oPc3nsk5ip8F1y8n7fHPhgireCMulzQfI5PI9lj7qqgpxvMbHh WlpA== 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w7-20020a170902904700b0017123d2abd5si32162113plz.457.2023.01.03.20.29.45; Tue, 03 Jan 2023 20:30:02 -0800 (PST) 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230352AbjADEZc (ORCPT + 99 others); Tue, 3 Jan 2023 23:25:32 -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 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 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754064856597396299?= X-GMAIL-MSGID: =?utf-8?q?1754064856597396299?= 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 @@ #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 */ 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 = vp_vdpa_to_mdev(vp_vdpa); u8 s = vp_vdpa_get_status(vdpa); + /* We should never be setting status to 0. */ + WARN_ON(status == 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 = vdpa_to_vp(vdpa); struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa); - u8 s = vp_vdpa_get_status(vdpa); + u8 tmp, s = vp_vdpa_get_status(vdpa); + int ret; vp_modern_set_status(mdev, 0); + /* + * 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 = readx_poll_timeout(vp_ioread8, &mdev->common->device_status, tmp, + tmp == 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);