From patchwork Tue Dec 6 02:13:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 30065 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2586940wrr; Mon, 5 Dec 2022 18:18:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf6IcIEF8NaUvrOwhnJXXQRAHkIyEGz09SKBvg0r+Sp0WYIzrj3Ngx9bf6pfysIzfILBzgk1 X-Received: by 2002:a17:90b:388b:b0:219:d52c:31a6 with SMTP id mu11-20020a17090b388b00b00219d52c31a6mr7947187pjb.117.1670293085526; Mon, 05 Dec 2022 18:18:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670293085; cv=none; d=google.com; s=arc-20160816; b=jRzCwNsaSQhaALTFQjA+9ZgZUDMSGG3Ga0k3S8yWajod2HOOLOJ5U0pZGlJPYX3uPn kIGjD1XuerhSqBzpV0o3DpWLWefPU2Eik/5A8Szjlm/3Vk1g4/Uo5hgDDwo7XzPG5t/i fjJ+ahqr+Y5fQXB2wroTY1ZFw6J+WEZ8gUph7OWKEKHE5V7ZAwq5zx9cZm0l9Cox0eXr 7QFt2QEXkN/i6Qq7qJTEfaXqTRTPoyoLcRQTobSYXD34X/5YinTon0udr0QiMwG7XCco 19VsgtUW2MiVMpPTnjTfuilzRV3e1GsN6X8U0sVoEC7j/FNucEwA4bbTkn+9g9N/H1yD Fr3w== 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=YEVF1Q8u88ta96iBRolW+Cr5uBo3UBX08hZUI67pf4E=; b=Z+QKRBQjJoD/nykC2f3O3s+jVm2FgI90N8gRQHv+WDcHBdBeqVnEVRhpo8mz2CBQGV C9qljUe9K6x4/zy2OF8HTgrDN679pVt43sY4QtLlHx5BUkBfFsg4CrFspNsAU/RixvQ3 gyuboP51SD5kShRjusfDD6ZrOwqEmOUTsMD13K7+X0OxqIQ66S2akEBXBrFgAkZuZAFd cgx9Q0IApl6/nk1GWrTOlwF1iosq8VYJ8dcjCamZA8ZkZAJ/3/UM5Csj9PCXZiB+b4Be qhL280IhQO2jtDwvhyZAdyDGm7LViRGNZJxbh+qyb3DC4nIFcOoyRRmEnNit5Nd9q8BA LggA== 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 oa3-20020a17090b1bc300b001fb706e96afsi19029122pjb.182.2022.12.05.18.17.52; Mon, 05 Dec 2022 18:18:05 -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 S233008AbiLFCNl (ORCPT + 99 others); Mon, 5 Dec 2022 21:13:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233799AbiLFCN3 (ORCPT ); Mon, 5 Dec 2022 21:13:29 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C87F1D0DA for ; Mon, 5 Dec 2022 18:13:28 -0800 (PST) Received: from kwepemi100025.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4NR3pb1MqSz15N4y; Tue, 6 Dec 2022 10:12:39 +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.31; Tue, 6 Dec 2022 10:13:25 +0800 From: "Longpeng(Mike)" To: , , , , CC: , , , , , , Longpeng Subject: [PATCH v2] vp_vdpa: harden the logic of set status Date: Tue, 6 Dec 2022 10:13:21 +0800 Message-ID: <20221206021321.2400-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: dggems704-chm.china.huawei.com (10.3.19.181) 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?1751429242618441166?= X-GMAIL-MSGID: =?utf-8?q?1751429242618441166?= 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 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, 22 insertions(+) diff --git a/drivers/vdpa/virtio_pci/vp_vdpa.c b/drivers/vdpa/virtio_pci/vp_vdpa.c index 13701c2a1963..a2d3b13ac646 100644 --- a/drivers/vdpa/virtio_pci/vp_vdpa.c +++ b/drivers/vdpa/virtio_pci/vp_vdpa.c @@ -214,6 +214,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); @@ -222,14 +225,33 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) vp_modern_set_status(mdev, status); } +#define VP_VDPA_RESET_TMOUT_MS 5000 /* 5s */ + static int vp_vdpa_reset(struct vdpa_device *vdpa, bool clear) { 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); + unsigned long timeout; vp_modern_set_status(mdev, 0); + /* + * As the virtio v1.1 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 keep us here forever, we only wait for 5 seconds. + */ + timeout = jiffies + msecs_to_jiffies(VP_VDPA_RESET_TMOUT_MS); + while (vp_modern_get_status(mdev)) { + usleep_range(1000, 1500); + if (time_after(jiffies, timeout)) { + dev_err(&mdev->pci_dev->dev, + "vp_vdpa: fail to reset device\n"); + return -ETIMEDOUT; + } + } + if (s & VIRTIO_CONFIG_S_DRIVER_OK) vp_vdpa_free_irq(vp_vdpa);