From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647279439; cv=none; d=zohomail.com; s=zohoarc; b=Vf/Zj21pcpi52jkNmkYCF7Eek43l8HMjp2fmYByonMeFWz83RgQyE4AeLnjmbdwVOEUXAUoH+TQYTXg3H46GwBu/9VPbhMCTtMtZteE7R/mnh2I/izd6dfhOPNiZm3YDr9rsn4VfyMs5N3A89VhD5+YZknc8r1MUwkFAe6NXPec= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647279439; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HT1J9o8e8hMWr+fuenoBd2CTP0mmy5j6F2gk8Fio8t8=; b=iZ3LQIWNbfi5g67qlSs2NJi0wIAc+gkknnx6lbb8fd2zCkJsWuJEvwHP/L6fK79XHBcOk8UKP47e0eHb8DpGGcZz03VsKRFoWGN75RKhjZy0ZFL1Q/NoFDCwtdCtheXiEZiwZE5pCI7qEoFK9Yr690WJn8gvzRE/HVrh1V5djCY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 164727943989642.23673992619638; Mon, 14 Mar 2022 10:37:19 -0700 (PDT) Received: from localhost ([::1]:52756 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTodC-00005u-7n for importer@patchew.org; Mon, 14 Mar 2022 13:37:18 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38738) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobJ-0005hz-1F for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:50567) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobB-0007XF-DQ for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:15 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-433-HeU3uavsP_mVZnv7w90KeQ-1; Mon, 14 Mar 2022 13:35:01 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 95D862999B2C for ; Mon, 14 Mar 2022 17:35:00 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id BE62C492C14; Mon, 14 Mar 2022 17:34:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279302; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HT1J9o8e8hMWr+fuenoBd2CTP0mmy5j6F2gk8Fio8t8=; b=GXgocwW0IYz1gcpUDRZyU86e+isQRnF4sw3XIIcthwIl6Ctfh5ih9DoUCFer2IO41Pob+P fDxPwfN4haSYJcx4L9eIumVbt4kcTmLkfnJA2BLQ1rHY0LGQYhBtknLV4EBV/Tp5v1tMSL 6OCfs3pyUOty+F/bTbYgTdEDBaM/f4Q= X-MC-Unique: HeU3uavsP_mVZnv7w90KeQ-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 01/15] vhost: Add VhostShadowVirtqueue Date: Mon, 14 Mar 2022 18:34:41 +0100 Message-Id: <20220314173455.200342-2-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647279441098100001 Vhost shadow virtqueue (SVQ) is an intermediate jump for virtqueue notifications and buffers, allowing qemu to track them. While qemu is forwarding the buffers and virtqueue changes, it is able to commit the memory it's being dirtied, the same way regular qemu's VirtIO devices do. This commit only exposes basic SVQ allocation and free. Next patches of the series add functionality like notifications and buffers forwarding. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 28 ++++++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 62 ++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-shadow-virtqueue.h create mode 100644 hw/virtio/vhost-shadow-virtqueue.c diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h new file mode 100644 index 0000000000..f1519e3c7b --- /dev/null +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -0,0 +1,28 @@ +/* + * vhost shadow virtqueue + * + * SPDX-FileCopyrightText: Red Hat, Inc. 2021 + * SPDX-FileContributor: Author: Eugenio P=C3=A9rez + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef VHOST_SHADOW_VIRTQUEUE_H +#define VHOST_SHADOW_VIRTQUEUE_H + +#include "qemu/event_notifier.h" + +/* Shadow virtqueue to relay notifications */ +typedef struct VhostShadowVirtqueue { + /* Shadow kick notifier, sent to vhost */ + EventNotifier hdev_kick; + /* Shadow call notifier, sent to vhost */ + EventNotifier hdev_call; +} VhostShadowVirtqueue; + +VhostShadowVirtqueue *vhost_svq_new(void); + +void vhost_svq_free(gpointer vq); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostShadowVirtqueue, vhost_svq_free); + +#endif diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c new file mode 100644 index 0000000000..c1db02c53e --- /dev/null +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -0,0 +1,62 @@ +/* + * vhost shadow virtqueue + * + * SPDX-FileCopyrightText: Red Hat, Inc. 2021 + * SPDX-FileContributor: Author: Eugenio P=C3=A9rez + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" + +#include "qemu/error-report.h" + +/** + * Creates vhost shadow virtqueue, and instructs the vhost device to use t= he + * shadow methods and file descriptors. + * + * Returns the new virtqueue or NULL. + * + * In case of error, reason is reported through error_report. + */ +VhostShadowVirtqueue *vhost_svq_new(void) +{ + g_autofree VhostShadowVirtqueue *svq =3D g_new0(VhostShadowVirtqueue, = 1); + int r; + + r =3D event_notifier_init(&svq->hdev_kick, 0); + if (r !=3D 0) { + error_report("Couldn't create kick event notifier: %s (%d)", + g_strerror(errno), errno); + goto err_init_hdev_kick; + } + + r =3D event_notifier_init(&svq->hdev_call, 0); + if (r !=3D 0) { + error_report("Couldn't create call event notifier: %s (%d)", + g_strerror(errno), errno); + goto err_init_hdev_call; + } + + return g_steal_pointer(&svq); + +err_init_hdev_call: + event_notifier_cleanup(&svq->hdev_kick); + +err_init_hdev_kick: + return NULL; +} + +/** + * Free the resources of the shadow virtqueue. + * + * @pvq: gpointer to SVQ so it can be used by autofree functions. + */ +void vhost_svq_free(gpointer pvq) +{ + VhostShadowVirtqueue *vq =3D pvq; + event_notifier_cleanup(&vq->hdev_kick); + event_notifier_cleanup(&vq->hdev_call); + g_free(vq); +} diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 521f7d64a8..2dc87613bc 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-= stub.c')) =20 virtio_ss =3D ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backe= nd.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backe= nd.c', 'vhost-shadow-virtqueue.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloo= n.c')) --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647279783; cv=none; d=zohomail.com; s=zohoarc; b=DQqvGI71jVHYPPL+Zgoi7qWMfeHfE/Xj4OK5mnw72g2WWyMs4wbqE7Fhw2sHcRffaW0xk4hASlRv1+94U/tU0f8anV27Dgd6IaI2bqsivJ69hVAqoHzcERdqw9i8bG5gB2nZ71OnWvXXTC1S5jeIcaf2nZn0CnSijZqiCmP0GsY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647279783; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3V4tkmM9/rK3dllItlBvbFFgHBWQs+y5F9ByohU0QKM=; b=fS3Plyh+LiT1lVbHR1cZ0XeOqLPXBiqPznig17/CmkLiuZELnAp50TbzssJyYxAT+WIgSPpTTZDDP9aDo1j0nd5Hn41iP73WRJ6YWsgmZkMs5DXey+gh89OEwq21SoecaeYNGKmuPLvYXnpGuThnqfafWUViRHJ4y4Zh25l9qQQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647279783868433.34852146522314; Mon, 14 Mar 2022 10:43:03 -0700 (PDT) Received: from localhost ([::1]:33266 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nToik-0006Fn-Ds for importer@patchew.org; Mon, 14 Mar 2022 13:43:02 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38736) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobJ-0005hy-1S for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:34059) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobB-0007cL-D5 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:15 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-471-yvv1P-d1PA2jC_JRN19ilg-1; Mon, 14 Mar 2022 13:35:01 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A7B963C01BB2 for ; Mon, 14 Mar 2022 17:35:01 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id E93864B8D5B; Mon, 14 Mar 2022 17:35:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279303; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3V4tkmM9/rK3dllItlBvbFFgHBWQs+y5F9ByohU0QKM=; b=JkHtBgC2gU5G3j0+WmpSW8bHLgLMJ5XUtSpm+2MJ2sxCKOhm+zwf1TCu3epLB8ePJx5QFq yPPctLy5ctQ1pZ+YQSmMsm7NW5vrbk2Gbvhv3CDCVP2+svUgyh8tCjvZcogfRzuc1cj19z wGn/NYR9wBNZliauABjyMk1XN7pOdB0= X-MC-Unique: yvv1P-d1PA2jC_JRN19ilg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 02/15] vhost: Add Shadow VirtQueue kick forwarding capabilities Date: Mon, 14 Mar 2022 18:34:42 +0100 Message-Id: <20220314173455.200342-3-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647279786134100001 At this mode no buffer forwarding will be performed in SVQ mode: Qemu will just forward the guest's kicks to the device. Host memory notifiers regions are left out for simplicity, and they will not be addressed in this series. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 14 +++ include/hw/virtio/vhost-vdpa.h | 4 + hw/virtio/vhost-shadow-virtqueue.c | 55 +++++++++++ hw/virtio/vhost-vdpa.c | 144 ++++++++++++++++++++++++++++- 4 files changed, 215 insertions(+), 2 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index f1519e3c7b..1cbc87d5d8 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -18,8 +18,22 @@ typedef struct VhostShadowVirtqueue { EventNotifier hdev_kick; /* Shadow call notifier, sent to vhost */ EventNotifier hdev_call; + + /* + * Borrowed virtqueue's guest to host notifier. To borrow it in this e= vent + * notifier allows to recover the VhostShadowVirtqueue from the event = loop + * easily. If we use the VirtQueue's one, we don't have an easy way to + * retrieve VhostShadowVirtqueue. + * + * So shadow virtqueue must not clean it, or we would lose VirtQueue o= ne. + */ + EventNotifier svq_kick; } VhostShadowVirtqueue; =20 +void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd); + +void vhost_svq_stop(VhostShadowVirtqueue *svq); + VhostShadowVirtqueue *vhost_svq_new(void); =20 void vhost_svq_free(gpointer vq); diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h index 3ce79a646d..009a9f3b6b 100644 --- a/include/hw/virtio/vhost-vdpa.h +++ b/include/hw/virtio/vhost-vdpa.h @@ -12,6 +12,8 @@ #ifndef HW_VIRTIO_VHOST_VDPA_H #define HW_VIRTIO_VHOST_VDPA_H =20 +#include + #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" =20 @@ -27,6 +29,8 @@ typedef struct vhost_vdpa { bool iotlb_batch_begin_sent; MemoryListener listener; struct vhost_vdpa_iova_range iova_range; + bool shadow_vqs_enabled; + GPtrArray *shadow_vqs; struct vhost_dev *dev; VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX]; } VhostVDPA; diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index c1db02c53e..e5da907b8e 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -11,6 +11,59 @@ #include "hw/virtio/vhost-shadow-virtqueue.h" =20 #include "qemu/error-report.h" +#include "qemu/main-loop.h" +#include "linux-headers/linux/vhost.h" + +/** + * Forward guest notifications. + * + * @n: guest kick event notifier, the one that guest set to notify svq. + */ +static void vhost_handle_guest_kick(EventNotifier *n) +{ + VhostShadowVirtqueue *svq =3D container_of(n, VhostShadowVirtqueue, sv= q_kick); + event_notifier_test_and_clear(n); + event_notifier_set(&svq->hdev_kick); +} + +/** + * Set a new file descriptor for the guest to kick the SVQ and notify for = avail + * + * @svq: The svq + * @svq_kick_fd: The svq kick fd + * + * Note that the SVQ will never close the old file descriptor. + */ +void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) +{ + EventNotifier *svq_kick =3D &svq->svq_kick; + bool poll_stop =3D VHOST_FILE_UNBIND !=3D event_notifier_get_fd(svq_ki= ck); + bool poll_start =3D svq_kick_fd !=3D VHOST_FILE_UNBIND; + + if (poll_stop) { + event_notifier_set_handler(svq_kick, NULL); + } + + /* + * event_notifier_set_handler already checks for guest's notifications= if + * they arrive at the new file descriptor in the switch, so there is no + * need to explicitly check for them. + */ + if (poll_start) { + event_notifier_init_fd(svq_kick, svq_kick_fd); + event_notifier_set(svq_kick); + event_notifier_set_handler(svq_kick, vhost_handle_guest_kick); + } +} + +/** + * Stop the shadow virtqueue operation. + * @svq: Shadow Virtqueue + */ +void vhost_svq_stop(VhostShadowVirtqueue *svq) +{ + event_notifier_set_handler(&svq->svq_kick, NULL); +} =20 /** * Creates vhost shadow virtqueue, and instructs the vhost device to use t= he @@ -39,6 +92,7 @@ VhostShadowVirtqueue *vhost_svq_new(void) goto err_init_hdev_call; } =20 + event_notifier_init_fd(&svq->svq_kick, VHOST_FILE_UNBIND); return g_steal_pointer(&svq); =20 err_init_hdev_call: @@ -56,6 +110,7 @@ err_init_hdev_kick: void vhost_svq_free(gpointer pvq) { VhostShadowVirtqueue *vq =3D pvq; + vhost_svq_stop(vq); event_notifier_cleanup(&vq->hdev_kick); event_notifier_cleanup(&vq->hdev_call); g_free(vq); diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 6c67d5f034..9c7cb32bdd 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -17,12 +17,14 @@ #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "hw/virtio/virtio-net.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/vhost-vdpa.h" #include "exec/address-spaces.h" #include "qemu/main-loop.h" #include "cpu.h" #include "trace.h" #include "qemu-common.h" +#include "qapi/error.h" =20 /* * Return one past the end of the end of section. Be careful with uint64_t @@ -342,6 +344,30 @@ static bool vhost_vdpa_one_time_request(struct vhost_d= ev *dev) return v->index !=3D 0; } =20 +static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *= v, + Error **errp) +{ + g_autoptr(GPtrArray) shadow_vqs =3D NULL; + + if (!v->shadow_vqs_enabled) { + return 0; + } + + shadow_vqs =3D g_ptr_array_new_full(hdev->nvqs, vhost_svq_free); + for (unsigned n =3D 0; n < hdev->nvqs; ++n) { + g_autoptr(VhostShadowVirtqueue) svq =3D vhost_svq_new(); + + if (unlikely(!svq)) { + error_setg(errp, "Cannot create svq %u", n); + return -1; + } + g_ptr_array_add(shadow_vqs, g_steal_pointer(&svq)); + } + + v->shadow_vqs =3D g_steal_pointer(&shadow_vqs); + return 0; +} + static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **er= rp) { struct vhost_vdpa *v; @@ -364,6 +390,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void= *opaque, Error **errp) dev->opaque =3D opaque ; v->listener =3D vhost_vdpa_memory_listener; v->msg_type =3D VHOST_IOTLB_MSG_V2; + ret =3D vhost_vdpa_init_svq(dev, v, errp); + if (ret) { + goto err; + } =20 vhost_vdpa_get_iova_range(v); =20 @@ -375,6 +405,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void= *opaque, Error **errp) VIRTIO_CONFIG_S_DRIVER); =20 return 0; + +err: + ram_block_discard_disable(false); + return ret; } =20 static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev, @@ -445,8 +479,14 @@ static void vhost_vdpa_host_notifiers_uninit(struct vh= ost_dev *dev, int n) =20 static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev) { + struct vhost_vdpa *v =3D dev->opaque; int i; =20 + if (v->shadow_vqs_enabled) { + /* FIXME SVQ is not compatible with host notifiers mr */ + return; + } + for (i =3D dev->vq_index; i < dev->vq_index + dev->nvqs; i++) { if (vhost_vdpa_host_notifier_init(dev, i)) { goto err; @@ -460,6 +500,21 @@ err: return; } =20 +static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev) +{ + struct vhost_vdpa *v =3D dev->opaque; + size_t idx; + + if (!v->shadow_vqs) { + return; + } + + for (idx =3D 0; idx < v->shadow_vqs->len; ++idx) { + vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, idx)); + } + g_ptr_array_free(v->shadow_vqs, true); +} + static int vhost_vdpa_cleanup(struct vhost_dev *dev) { struct vhost_vdpa *v; @@ -468,6 +523,7 @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev) trace_vhost_vdpa_cleanup(dev, v); vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); memory_listener_unregister(&v->listener); + vhost_vdpa_svq_cleanup(dev); =20 dev->opaque =3D NULL; ram_block_discard_disable(false); @@ -559,11 +615,26 @@ static int vhost_vdpa_get_device_id(struct vhost_dev = *dev, return ret; } =20 +static void vhost_vdpa_reset_svq(struct vhost_vdpa *v) +{ + if (!v->shadow_vqs_enabled) { + return; + } + + for (unsigned i =3D 0; i < v->shadow_vqs->len; ++i) { + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, i); + vhost_svq_stop(svq); + } +} + static int vhost_vdpa_reset_device(struct vhost_dev *dev) { + struct vhost_vdpa *v =3D dev->opaque; int ret; uint8_t status =3D 0; =20 + vhost_vdpa_reset_svq(v); + ret =3D vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status); trace_vhost_vdpa_reset_device(dev, status); return ret; @@ -647,13 +718,74 @@ static int vhost_vdpa_get_config(struct vhost_dev *de= v, uint8_t *config, return ret; } =20 +static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev, + struct vhost_vring_file *file) +{ + trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd); + return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file); +} + +/** + * Set the shadow virtqueue descriptors to the device + * + * @dev: The vhost device model + * @svq: The shadow virtqueue + * @idx: The index of the virtqueue in the vhost device + * @errp: Error + */ +static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, + VhostShadowVirtqueue *svq, unsigned idx, + Error **errp) +{ + struct vhost_vring_file file =3D { + .index =3D dev->vq_index + idx, + }; + const EventNotifier *event_notifier =3D &svq->hdev_kick; + int r; + + file.fd =3D event_notifier_get_fd(event_notifier); + r =3D vhost_vdpa_set_vring_dev_kick(dev, &file); + if (unlikely(r !=3D 0)) { + error_setg_errno(errp, -r, "Can't set device kick fd"); + } + + return r =3D=3D 0; +} + +static bool vhost_vdpa_svqs_start(struct vhost_dev *dev) +{ + struct vhost_vdpa *v =3D dev->opaque; + Error *err =3D NULL; + unsigned i; + + if (!v->shadow_vqs) { + return true; + } + + for (i =3D 0; i < v->shadow_vqs->len; ++i) { + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, i); + bool ok =3D vhost_vdpa_svq_setup(dev, svq, i, &err); + if (unlikely(!ok)) { + error_reportf_err(err, "Cannot setup SVQ %u: ", i); + return false; + } + } + + return true; +} + static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) { struct vhost_vdpa *v =3D dev->opaque; + bool ok; trace_vhost_vdpa_dev_start(dev, started); =20 if (started) { vhost_vdpa_host_notifiers_init(dev); + ok =3D vhost_vdpa_svqs_start(dev); + if (unlikely(!ok)) { + return -1; + } vhost_vdpa_set_vring_ready(dev); } else { vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); @@ -725,8 +857,16 @@ static int vhost_vdpa_get_vring_base(struct vhost_dev = *dev, static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, struct vhost_vring_file *file) { - trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd); - return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file); + struct vhost_vdpa *v =3D dev->opaque; + int vdpa_idx =3D file->index - dev->vq_index; + + if (v->shadow_vqs_enabled) { + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, vdp= a_idx); + vhost_svq_set_svq_kick_fd(svq, file->fd); + return 0; + } else { + return vhost_vdpa_set_vring_dev_kick(dev, file); + } } =20 static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647279448; cv=none; d=zohomail.com; s=zohoarc; b=ZgOlcCh8r8VhQCBafS1oZOpr4KN1lHnfCRo50l6D0MMHBLY09USHZvEjQeiI49/tURpegEOA3y8R1XkhQy0gZTr1EFdoer9ZGiF23vYG3VsVupNLHGOlD22lkFQj7RCu/9sl5Bob8F2qelxFHkFToZHazGDwFSEGakKhRapWjyU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647279448; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=1EYVTjUuxcLCcbCFmpfWWv7kJgwg22IYEU22FJ3m1h8=; b=ear6sXEj/IiLe60jEDZ9MN9kaVgLty1rKM8Te1g1KAF0HvzNQ09WBhLg5QAnXYp0zsAegWBS/NDqDQt1QMdOYT5Zr5BHir0GXpcY/LTpU8+b5Ydr+0sx3Wtlaa/nvJdZcTwecotK5cKa97j4Xat1XrQ246qKm/o5YDPp1oVx2Fg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647279448794791.6642321951673; Mon, 14 Mar 2022 10:37:28 -0700 (PDT) Received: from localhost ([::1]:52798 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTodL-00007n-7j for importer@patchew.org; Mon, 14 Mar 2022 13:37:28 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38786) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobK-0005iK-R6 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:58649) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobD-0007fF-GW for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:21 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-67-x9aoygyLMOeWDCH_t9QIYg-1; Mon, 14 Mar 2022 13:35:03 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BA14E185A79C for ; Mon, 14 Mar 2022 17:35:02 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF2D14B8D5A; Mon, 14 Mar 2022 17:35:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279304; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1EYVTjUuxcLCcbCFmpfWWv7kJgwg22IYEU22FJ3m1h8=; b=WElrXu5uegnFIw+Xda9jQr29PZ8q52UNtzDlnh2RkTA5q3+knufnWsvd5sXHbgrVguFGJ9 5XAELxjMN+txKHolPWexZCBIdJoY7sG9+K/lCJh5fqjR8/Aij33VRF/45Znv1cwCnLx8bz rIs3apvQ24juWJ5qJC6NIPp2rP9AO3I= X-MC-Unique: x9aoygyLMOeWDCH_t9QIYg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 03/15] vhost: Add Shadow VirtQueue call forwarding capabilities Date: Mon, 14 Mar 2022 18:34:43 +0100 Message-Id: <20220314173455.200342-4-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647279450902100001 This will make qemu aware of the device used buffers, allowing it to write the guest memory with its contents if needed. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 4 ++++ hw/virtio/vhost-shadow-virtqueue.c | 38 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 31 ++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index 1cbc87d5d8..cbc5213579 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -28,9 +28,13 @@ typedef struct VhostShadowVirtqueue { * So shadow virtqueue must not clean it, or we would lose VirtQueue o= ne. */ EventNotifier svq_kick; + + /* Guest's call notifier, where the SVQ calls guest. */ + EventNotifier svq_call; } VhostShadowVirtqueue; =20 void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd); +void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd); =20 void vhost_svq_stop(VhostShadowVirtqueue *svq); =20 diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index e5da907b8e..55cb5414ef 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -26,6 +26,42 @@ static void vhost_handle_guest_kick(EventNotifier *n) event_notifier_set(&svq->hdev_kick); } =20 +/** + * Forward vhost notifications + * + * @n: hdev call event notifier, the one that device set to notify svq. + */ +static void vhost_svq_handle_call(EventNotifier *n) +{ + VhostShadowVirtqueue *svq =3D container_of(n, VhostShadowVirtqueue, + hdev_call); + event_notifier_test_and_clear(n); + event_notifier_set(&svq->svq_call); +} + +/** + * Set the call notifier for the SVQ to call the guest + * + * @svq: Shadow virtqueue + * @call_fd: call notifier + * + * Called on BQL context. + */ +void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd) +{ + if (call_fd =3D=3D VHOST_FILE_UNBIND) { + /* + * Fail event_notifier_set if called handling device call. + * + * SVQ still needs device notifications, since it needs to keep + * forwarding used buffers even with the unbind. + */ + memset(&svq->svq_call, 0, sizeof(svq->svq_call)); + } else { + event_notifier_init_fd(&svq->svq_call, call_fd); + } +} + /** * Set a new file descriptor for the guest to kick the SVQ and notify for = avail * @@ -93,6 +129,7 @@ VhostShadowVirtqueue *vhost_svq_new(void) } =20 event_notifier_init_fd(&svq->svq_kick, VHOST_FILE_UNBIND); + event_notifier_set_handler(&svq->hdev_call, vhost_svq_handle_call); return g_steal_pointer(&svq); =20 err_init_hdev_call: @@ -112,6 +149,7 @@ void vhost_svq_free(gpointer pvq) VhostShadowVirtqueue *vq =3D pvq; vhost_svq_stop(vq); event_notifier_cleanup(&vq->hdev_kick); + event_notifier_set_handler(&vq->hdev_call, NULL); event_notifier_cleanup(&vq->hdev_call); g_free(vq); } diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 9c7cb32bdd..9f4333cff5 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -725,6 +725,13 @@ static int vhost_vdpa_set_vring_dev_kick(struct vhost_= dev *dev, return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file); } =20 +static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev, + struct vhost_vring_file *file) +{ + trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd); + return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file); +} + /** * Set the shadow virtqueue descriptors to the device * @@ -732,6 +739,9 @@ static int vhost_vdpa_set_vring_dev_kick(struct vhost_d= ev *dev, * @svq: The shadow virtqueue * @idx: The index of the virtqueue in the vhost device * @errp: Error + * + * Note that this function does not rewind kick file descriptor if cannot = set + * call one. */ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, VhostShadowVirtqueue *svq, unsigned idx, @@ -747,6 +757,14 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, r =3D vhost_vdpa_set_vring_dev_kick(dev, &file); if (unlikely(r !=3D 0)) { error_setg_errno(errp, -r, "Can't set device kick fd"); + return false; + } + + event_notifier =3D &svq->hdev_call; + file.fd =3D event_notifier_get_fd(event_notifier); + r =3D vhost_vdpa_set_vring_dev_call(dev, &file); + if (unlikely(r !=3D 0)) { + error_setg_errno(errp, -r, "Can't set device call fd"); } =20 return r =3D=3D 0; @@ -872,8 +890,17 @@ static int vhost_vdpa_set_vring_kick(struct vhost_dev = *dev, static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, struct vhost_vring_file *file) { - trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd); - return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file); + struct vhost_vdpa *v =3D dev->opaque; + + if (v->shadow_vqs_enabled) { + int vdpa_idx =3D file->index - dev->vq_index; + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, vdp= a_idx); + + vhost_svq_set_svq_call_fd(svq, file->fd); + return 0; + } else { + return vhost_vdpa_set_vring_dev_call(dev, file); + } } =20 static int vhost_vdpa_get_features(struct vhost_dev *dev, --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280361; cv=none; d=zohomail.com; s=zohoarc; b=F2IFH2jG4w7n9Vg8PvMX3XI629hGL9iOY4MMUiHt5eblA/vZpbMBnFv4ZDJ2QWDeRSWXZNqqIoMgetjeGRLq4rE4m8bpiUJaOyxX2DQx4iN/Agw21vV32s70udEtg05MfddmDI3aJFgNZ+EYYMcvWcWUBY66gGCqHwMsBtaKfSg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280361; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QhP6ilN/CIRoJS3IXwP4c2NYQfFyVXK3gqQa2RpNgnA=; b=hzN9BsKxUGTn92QrYwXHZsBuOjp7ghAl4S7//XMcVOw4rt8BREMKGd4m0exVUs6iu0Qy1EqAzBX5WVkiVdeg7vL143cdQz3hZaAwtgncvo7qQHb4+OO/g19bXcs4vrRg79wFxHiCoY+fZkvg+8EpSPfzE/n0KftDjPErKMQ1QSI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280361751748.4167075361167; Mon, 14 Mar 2022 10:52:41 -0700 (PDT) Received: from localhost ([::1]:49192 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTos4-0000QU-H8 for importer@patchew.org; Mon, 14 Mar 2022 13:52:40 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38930) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobN-0005l6-Mc for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:22010) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007fL-2V for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:24 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-522-dDGM-5dUP82ObvF41moUtg-1; Mon, 14 Mar 2022 13:35:04 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BFB453806729 for ; Mon, 14 Mar 2022 17:35:03 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 18B04492C14; Mon, 14 Mar 2022 17:35:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279305; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QhP6ilN/CIRoJS3IXwP4c2NYQfFyVXK3gqQa2RpNgnA=; b=iS3JUwydk5En1C5LJrj4wPmpqL6ccG/TdghPhTEPRCpB4bX+X1UMl8PWBInjmVDtVcbmZS paj07edvJtYfiNqy3YUuCVVbHgWRQvJbjX+UilpvP0kGf0ngwk25xUP3jwq1ARoYq2Y3SK YNt6CR/uqq9x8aJYrTHPg4VrfEYQjDs= X-MC-Unique: dDGM-5dUP82ObvF41moUtg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 04/15] vhost: Add vhost_svq_valid_features to shadow vq Date: Mon, 14 Mar 2022 18:34:44 +0100 Message-Id: <20220314173455.200342-5-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280363880100003 This allows SVQ to negotiate features with the guest and the device. For the device, SVQ is a driver. While this function bypasses all non-transport features, it needs to disable the features that SVQ does not support when forwarding buffers. This includes packed vq layout, indirect descriptors or event idx. Future changes can add support to offer more features to the guest, since the use of VirtQueue gives this for free. This is left out at the moment for simplicity. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 15 ++++++++++ 3 files changed, 61 insertions(+) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index cbc5213579..9e12f77201 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -33,6 +33,8 @@ typedef struct VhostShadowVirtqueue { EventNotifier svq_call; } VhostShadowVirtqueue; =20 +bool vhost_svq_valid_features(uint64_t features, Error **errp); + void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd); void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd); =20 diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index 55cb5414ef..519328445c 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -11,9 +11,53 @@ #include "hw/virtio/vhost-shadow-virtqueue.h" =20 #include "qemu/error-report.h" +#include "qapi/error.h" #include "qemu/main-loop.h" #include "linux-headers/linux/vhost.h" =20 +/** + * Validate the transport device features that both guests can use with th= e SVQ + * and SVQs can use with the device. + * + * @dev_features: The features + * @errp: Error pointer + */ +bool vhost_svq_valid_features(uint64_t features, Error **errp) +{ + bool ok =3D true; + uint64_t svq_features =3D features; + + for (uint64_t b =3D VIRTIO_TRANSPORT_F_START; b <=3D VIRTIO_TRANSPORT_= F_END; + ++b) { + switch (b) { + case VIRTIO_F_ANY_LAYOUT: + continue; + + case VIRTIO_F_ACCESS_PLATFORM: + /* SVQ trust in the host's IOMMU to translate addresses */ + case VIRTIO_F_VERSION_1: + /* SVQ trust that the guest vring is little endian */ + if (!(svq_features & BIT_ULL(b))) { + svq_features |=3D BIT_ULL(b); + ok =3D false; + } + continue; + + default: + if (svq_features & BIT_ULL(b)) { + svq_features &=3D ~BIT_ULL(b); + ok =3D false; + } + } + } + + if (!ok) { + error_setg(errp, "SVQ Invalid device feature flags, offer: 0x%"PRI= x64 + ", ok: 0x%"PRIx64, features, svq_features); + } + return ok; +} + /** * Forward guest notifications. * diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 9f4333cff5..4e1deb3d04 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -348,11 +348,26 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev= , struct vhost_vdpa *v, Error **errp) { g_autoptr(GPtrArray) shadow_vqs =3D NULL; + uint64_t dev_features, svq_features; + int r; + bool ok; =20 if (!v->shadow_vqs_enabled) { return 0; } =20 + r =3D hdev->vhost_ops->vhost_get_features(hdev, &dev_features); + if (r !=3D 0) { + error_setg_errno(errp, -r, "Can't get vdpa device features"); + return r; + } + + svq_features =3D dev_features; + ok =3D vhost_svq_valid_features(svq_features, errp); + if (unlikely(!ok)) { + return -1; + } + shadow_vqs =3D g_ptr_array_new_full(hdev->nvqs, vhost_svq_free); for (unsigned n =3D 0; n < hdev->nvqs; ++n) { g_autoptr(VhostShadowVirtqueue) svq =3D vhost_svq_new(); --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647279451; cv=none; d=zohomail.com; s=zohoarc; b=IxL2D8K4nJeECL6O3bLLkZCRms5WYcetD8atPyFQzaQPnEbZeEfX9rD+0Nrd8yhesJbNyzKtF0ApTUAIFDauaOFzD6GdSa3u162GPboQC5w4h/84jJ0LZ22U4nYhVTGItOTJkQwiqvsTzBdQHjr8iadO0er99BldclcXGDJ7PTw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647279451; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BknqXWrmLkkz7oLRe8+EL4KF0342SkeEtELRBYQHio4=; b=Yp/Q3f6klVxZy2yl7ZGO1agHaWpDaV7U0JS1sGWVe0x0GT0/f40BEKlcI2RDQN1cs5ClWKtA4j52Whs+wgb4WHSBJ+rE51sAitPCq8hSORZSkB4kblYFjz2Pn2D/gfRFmlwylsiffM6pBJTqBXCrPz/z/tfna5y2F1jCWkUwcos= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647279451780205.06724411265998; Mon, 14 Mar 2022 10:37:31 -0700 (PDT) Received: from localhost ([::1]:53032 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTodO-0000JS-IO for importer@patchew.org; Mon, 14 Mar 2022 13:37:30 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38932) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobN-0005l7-P6 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:24424) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007fa-Hp for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-201-W9VZALz3MCmfAcIAmaZX9w-1; Mon, 14 Mar 2022 13:35:05 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C64D3811E9B for ; Mon, 14 Mar 2022 17:35:04 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C964492C14; Mon, 14 Mar 2022 17:35:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279306; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BknqXWrmLkkz7oLRe8+EL4KF0342SkeEtELRBYQHio4=; b=Nxnl0VoOdLUVI1F7ggUvQ2YNGrJU4AOoaZc/wzq7CskQXMPWGOhNtm40spJpOCfkawPONi oyLPDM3JIifxME0oFVtS9A5fGBgfg3/NEOKhwE1Cb/UIrVa8KNXOu8gztJ1riQxmeicMMf 44mLz5I29PBcaA/0TXiWQfhW7E7BbOA= X-MC-Unique: W9VZALz3MCmfAcIAmaZX9w-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 05/15] virtio: Add vhost_svq_get_vring_addr Date: Mon, 14 Mar 2022 18:34:45 +0100 Message-Id: <20220314173455.200342-6-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647279452868100003 It reports the shadow virtqueue address from qemu virtual address space. Since this will be different from the guest's vaddr, but the device can access it, SVQ takes special care about its alignment & lack of garbage data. It assumes that IOMMU will work in host_page_size ranges for that. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 9 +++++++++ hw/virtio/vhost-shadow-virtqueue.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index 9e12f77201..82cea1c3fa 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -11,9 +11,14 @@ #define VHOST_SHADOW_VIRTQUEUE_H =20 #include "qemu/event_notifier.h" +#include "hw/virtio/virtio.h" +#include "standard-headers/linux/vhost_types.h" =20 /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue { + /* Shadow vring */ + struct vring vring; + /* Shadow kick notifier, sent to vhost */ EventNotifier hdev_kick; /* Shadow call notifier, sent to vhost */ @@ -37,6 +42,10 @@ bool vhost_svq_valid_features(uint64_t features, Error *= *errp); =20 void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd); void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd); +void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr); +size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq); +size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq); =20 void vhost_svq_stop(VhostShadowVirtqueue *svq); =20 diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index 519328445c..573ac0d9cf 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -106,6 +106,35 @@ void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *s= vq, int call_fd) } } =20 +/** + * Get the shadow vq vring address. + * @svq: Shadow virtqueue + * @addr: Destination to store address + */ +void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr) +{ + addr->desc_user_addr =3D (uint64_t)(intptr_t)svq->vring.desc; + addr->avail_user_addr =3D (uint64_t)(intptr_t)svq->vring.avail; + addr->used_user_addr =3D (uint64_t)(intptr_t)svq->vring.used; +} + +size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq) +{ + size_t desc_size =3D sizeof(vring_desc_t) * svq->vring.num; + size_t avail_size =3D offsetof(vring_avail_t, ring) + + sizeof(uint16_t) * svq->vring= .num; + + return ROUND_UP(desc_size + avail_size, qemu_real_host_page_size); +} + +size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq) +{ + size_t used_size =3D offsetof(vring_used_t, ring) + + sizeof(vring_used_elem_t) * svq->vring= .num; + return ROUND_UP(used_size, qemu_real_host_page_size); +} + /** * Set a new file descriptor for the guest to kick the SVQ and notify for = avail * --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647279977; cv=none; d=zohomail.com; s=zohoarc; b=CHr2/fGWlBgc4+rSnWsKVA5FZ2vxH9sa/V8JSWATIZL9870bgAOHx072HJh4NwNeeYA9mhAtZL1iN6+EDRQGf+zxNd4YPu7zdQ3fAilBcBQigGTCaCQoJsy8IEre5vGUS2ee7MGK9E/dVi5Qx+ueamd1KNQ26iiCsvbJ7NoWfvA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647279977; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=a14QYI742zsn+kZZ3QWn225DvimwAo3Em4vVOBdC/lU=; b=nFFwWkmi2KlJwDZTrHaKcVcKYPZH466H5sYlV+7OWwxZJeAPhgWAACIkN7DVRodehDRkd+sqWB8vvMnY9HVgGfLtbDoZSq+4W5Beo1yUfZakRmjBT4yGvKngBzV6vCQopZB54Tzhw0KzZGzryxtPjlWayfdfXRPiI//OzG6sdho= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647279977930195.19152582182323; Mon, 14 Mar 2022 10:46:17 -0700 (PDT) Received: from localhost ([::1]:36306 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTolp-0008Kc-2I for importer@patchew.org; Mon, 14 Mar 2022 13:46:13 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38928) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobN-0005l3-Lx for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:60215) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007fj-HW for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:24 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-235-Hk2uwjcDNzm3YIT1y8bXcg-1; Mon, 14 Mar 2022 13:35:06 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0B7AC38035A1 for ; Mon, 14 Mar 2022 17:35:06 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25A774B8D5A; Mon, 14 Mar 2022 17:35:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279307; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a14QYI742zsn+kZZ3QWn225DvimwAo3Em4vVOBdC/lU=; b=diQba7tLKRT8X6JMATMLj2LtqAJRPxmcjdQpwwpmKsuS2EtilSMV6kC9UxDODz75Qzczkj l47qyfnIJqOgRK2nS4D0SExu+0Ty7Jw6AljfNsMCBB5nVyQpmff21XLK5Bwq9GcGdDURDC 7Yh67QkpAUAV7CogpBfki28Aak58uGg= X-MC-Unique: Hk2uwjcDNzm3YIT1y8bXcg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 06/15] vdpa: adapt vhost_ops callbacks to svq Date: Mon, 14 Mar 2022 18:34:46 +0100 Message-Id: <20220314173455.200342-7-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647279979906100001 First half of the buffers forwarding part, preparing vhost-vdpa callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so this is effectively dead code at the moment, but it helps to reduce patch size. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-vdpa.c | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 4e1deb3d04..3e2c181d2b 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -733,6 +733,13 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev= , uint8_t *config, return ret; } =20 +static int vhost_vdpa_set_dev_vring_base(struct vhost_dev *dev, + struct vhost_vring_state *ring) +{ + trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num); + return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring); +} + static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev, struct vhost_vring_file *file) { @@ -747,6 +754,18 @@ static int vhost_vdpa_set_vring_dev_call(struct vhost_= dev *dev, return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file); } =20 +static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev, + struct vhost_vring_addr *addr) +{ + trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags, + addr->desc_user_addr, addr->used_user_addr, + addr->avail_user_addr, + addr->log_guest_addr); + + return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr); + +} + /** * Set the shadow virtqueue descriptors to the device * @@ -856,11 +875,17 @@ static int vhost_vdpa_set_log_base(struct vhost_dev *= dev, uint64_t base, static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev, struct vhost_vring_addr *addr) { - trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags, - addr->desc_user_addr, addr->used_user_= addr, - addr->avail_user_addr, - addr->log_guest_addr); - return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr); + struct vhost_vdpa *v =3D dev->opaque; + + if (v->shadow_vqs_enabled) { + /* + * Device vring addr was set at device start. SVQ base is handled = by + * VirtQueue code. + */ + return 0; + } + + return vhost_vdpa_set_vring_dev_addr(dev, addr); } =20 static int vhost_vdpa_set_vring_num(struct vhost_dev *dev, @@ -873,8 +898,17 @@ static int vhost_vdpa_set_vring_num(struct vhost_dev *= dev, static int vhost_vdpa_set_vring_base(struct vhost_dev *dev, struct vhost_vring_state *ring) { - trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num); - return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring); + struct vhost_vdpa *v =3D dev->opaque; + + if (v->shadow_vqs_enabled) { + /* + * Device vring base was set at device start. SVQ base is handled = by + * VirtQueue code. + */ + return 0; + } + + return vhost_vdpa_set_dev_vring_base(dev, ring); } =20 static int vhost_vdpa_get_vring_base(struct vhost_dev *dev, --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280460; cv=none; d=zohomail.com; s=zohoarc; b=XvJQZDiXxKshd2TqGtsN7Ed5OchGlRnUz2K4C/eQ4mKMgyggzf9csGxQkbJV2wxhtgXkmTP49bG8NvXTFnTeZfyZAfz5KlXxqTEeVWz/gpilmY5PVJM8eOhEXiF7jAxLx9j2NxhwzV4FsWOGCMOt0sXLhFhNMlqpEiHzix74zx8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280460; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fQVkGaJbBOuOHBJdPYJ/ZNTGKGSGRbiVKnb8rNDP3Ms=; b=SFYVcOhMaZ5lirAUG3/6U0UAvz+lhWJ/HLjF9Dh3UI5yKT6NzsgBBOHC/iRKqOUc2duKhgPzoKnhGZzg82Hg2Iof/AYvOG2xxXyCZPQDiyY69lHfWD04sue8oVMQodqgbvZ/r3bQxffDlqoq1UdGThWs27nV7c5foqqmhLQJED8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 164728046008417.738782668934732; Mon, 14 Mar 2022 10:54:20 -0700 (PDT) Received: from localhost ([::1]:55604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTote-0004hD-ND for importer@patchew.org; Mon, 14 Mar 2022 13:54:18 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38938) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobO-0005mZ-3N for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:21193) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007fp-Hd for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-407-c6S4J4f6M0eFOjEKdkV5hw-1; Mon, 14 Mar 2022 13:35:07 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2E9861C01E98 for ; Mon, 14 Mar 2022 17:35:07 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6A4D0492C14; Mon, 14 Mar 2022 17:35:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279308; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fQVkGaJbBOuOHBJdPYJ/ZNTGKGSGRbiVKnb8rNDP3Ms=; b=Q24aJpwowTS4Ob0fSwjjWvDNaKj/ykCFNRZE5TfS7OGV4j4BMKSdJn0uTv+QBWuUUXIoy6 JEZV7AU0CwEJ8XQOLr0EqkTG7W2ly66UXNkBCubPGe/0Ceeh+3XNAqM7ozGd55J6YLFrWv PieLC5F0pB2EQXYZDKrnW0Leo7zoL98= X-MC-Unique: c6S4J4f6M0eFOjEKdkV5hw-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 07/15] vhost: Shadow virtqueue buffers forwarding Date: Mon, 14 Mar 2022 18:34:47 +0100 Message-Id: <20220314173455.200342-8-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280460856100001 Initial version of shadow virtqueue that actually forward buffers. There is no iommu support at the moment, and that will be addressed in future patches of this series. Since all vhost-vdpa devices use forced IOMMU, this means that SVQ is not usable at this point of the series on any device. For simplicity it only supports modern devices, that expects vring in little endian, with split ring and no event idx or indirect descriptors. Support for them will not be added in this series. It reuses the VirtQueue code for the device part. The driver part is based on Linux's virtio_ring driver, but with stripped functionality and optimizations so it's easier to review. However, forwarding buffers have some particular pieces: One of the most unexpected ones is that a guest's buffer can expand through more than one descriptor in SVQ. While this is handled gracefully by qemu's emulated virtio devices, it may cause unexpected SVQ queue full. This patch also solves it by checking for this condition at both guest's kicks and device's calls. The code may be more elegant in the future if SVQ code runs in its own iocontext. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 26 +++ hw/virtio/vhost-shadow-virtqueue.c | 352 ++++++++++++++++++++++++++++- hw/virtio/vhost-vdpa.c | 155 ++++++++++++- 3 files changed, 522 insertions(+), 11 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index 82cea1c3fa..38b3b91ca7 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -36,6 +36,30 @@ typedef struct VhostShadowVirtqueue { =20 /* Guest's call notifier, where the SVQ calls guest. */ EventNotifier svq_call; + + /* Virtio queue shadowing */ + VirtQueue *vq; + + /* Virtio device */ + VirtIODevice *vdev; + + /* Map for use the guest's descriptors */ + VirtQueueElement **ring_id_maps; + + /* Next VirtQueue element that guest made available */ + VirtQueueElement *next_guest_avail_elem; + + /* Next head to expose to the device */ + uint16_t shadow_avail_idx; + + /* Next free descriptor */ + uint16_t free_head; + + /* Last seen used idx */ + uint16_t shadow_used_idx; + + /* Next head to consume from the device */ + uint16_t last_used_idx; } VhostShadowVirtqueue; =20 bool vhost_svq_valid_features(uint64_t features, Error **errp); @@ -47,6 +71,8 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue = *svq, size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq); size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq); =20 +void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, + VirtQueue *vq); void vhost_svq_stop(VhostShadowVirtqueue *svq); =20 VhostShadowVirtqueue *vhost_svq_new(void); diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index 573ac0d9cf..ece50b8ece 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -13,6 +13,8 @@ #include "qemu/error-report.h" #include "qapi/error.h" #include "qemu/main-loop.h" +#include "qemu/log.h" +#include "qemu/memalign.h" #include "linux-headers/linux/vhost.h" =20 /** @@ -59,28 +61,307 @@ bool vhost_svq_valid_features(uint64_t features, Error= **errp) } =20 /** - * Forward guest notifications. + * Number of descriptors that the SVQ can make available from the guest. + * + * @svq: The svq + */ +static uint16_t vhost_svq_available_slots(const VhostShadowVirtqueue *svq) +{ + return svq->vring.num - (svq->shadow_avail_idx - svq->shadow_used_idx); +} + +static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, + const struct iovec *iovec, size_t num, + bool more_descs, bool write) +{ + uint16_t i =3D svq->free_head, last =3D svq->free_head; + unsigned n; + uint16_t flags =3D write ? cpu_to_le16(VRING_DESC_F_WRITE) : 0; + vring_desc_t *descs =3D svq->vring.desc; + + if (num =3D=3D 0) { + return; + } + + for (n =3D 0; n < num; n++) { + if (more_descs || (n + 1 < num)) { + descs[i].flags =3D flags | cpu_to_le16(VRING_DESC_F_NEXT); + } else { + descs[i].flags =3D flags; + } + descs[i].addr =3D cpu_to_le64((hwaddr)(intptr_t)iovec[n].iov_base); + descs[i].len =3D cpu_to_le32(iovec[n].iov_len); + + last =3D i; + i =3D cpu_to_le16(descs[i].next); + } + + svq->free_head =3D le16_to_cpu(descs[last].next); +} + +static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, + VirtQueueElement *elem, unsigned *head) +{ + unsigned avail_idx; + vring_avail_t *avail =3D svq->vring.avail; + + *head =3D svq->free_head; + + /* We need some descriptors here */ + if (unlikely(!elem->out_num && !elem->in_num)) { + qemu_log_mask(LOG_GUEST_ERROR, + "Guest provided element with no descriptors"); + return false; + } + + vhost_vring_write_descs(svq, elem->out_sg, elem->out_num, elem->in_num= > 0, + false); + vhost_vring_write_descs(svq, elem->in_sg, elem->in_num, false, true); + + /* + * Put the entry in the available array (but don't update avail->idx u= ntil + * they do sync). + */ + avail_idx =3D svq->shadow_avail_idx & (svq->vring.num - 1); + avail->ring[avail_idx] =3D cpu_to_le16(*head); + svq->shadow_avail_idx++; + + /* Update the avail index after write the descriptor */ + smp_wmb(); + avail->idx =3D cpu_to_le16(svq->shadow_avail_idx); + + return true; +} + +static bool vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *ele= m) +{ + unsigned qemu_head; + bool ok =3D vhost_svq_add_split(svq, elem, &qemu_head); + if (unlikely(!ok)) { + return false; + } + + svq->ring_id_maps[qemu_head] =3D elem; + return true; +} + +static void vhost_svq_kick(VhostShadowVirtqueue *svq) +{ + /* + * We need to expose the available array entries before checking the u= sed + * flags + */ + smp_mb(); + if (svq->vring.used->flags & VRING_USED_F_NO_NOTIFY) { + return; + } + + event_notifier_set(&svq->hdev_kick); +} + +/** + * Forward available buffers. + * + * @svq: Shadow VirtQueue + * + * Note that this function does not guarantee that all guest's available + * buffers are available to the device in SVQ avail ring. The guest may ha= ve + * exposed a GPA / GIOVA contiguous buffer, but it may not be contiguous in + * qemu vaddr. + * + * If that happens, guest's kick notifications will be disabled until the + * device uses some buffers. + */ +static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq) +{ + /* Clear event notifier */ + event_notifier_test_and_clear(&svq->svq_kick); + + /* Forward to the device as many available buffers as possible */ + do { + virtio_queue_set_notification(svq->vq, false); + + while (true) { + VirtQueueElement *elem; + bool ok; + + if (svq->next_guest_avail_elem) { + elem =3D g_steal_pointer(&svq->next_guest_avail_elem); + } else { + elem =3D virtqueue_pop(svq->vq, sizeof(*elem)); + } + + if (!elem) { + break; + } + + if (elem->out_num + elem->in_num > vhost_svq_available_slots(s= vq)) { + /* + * This condition is possible since a contiguous buffer in= GPA + * does not imply a contiguous buffer in qemu's VA + * scatter-gather segments. If that happens, the buffer ex= posed + * to the device needs to be a chain of descriptors at this + * moment. + * + * SVQ cannot hold more available buffers if we are here: + * queue the current guest descriptor and ignore further k= icks + * until some elements are used. + */ + svq->next_guest_avail_elem =3D elem; + return; + } + + ok =3D vhost_svq_add(svq, elem); + if (unlikely(!ok)) { + /* VQ is broken, just return and ignore any other kicks */ + return; + } + vhost_svq_kick(svq); + } + + virtio_queue_set_notification(svq->vq, true); + } while (!virtio_queue_empty(svq->vq)); +} + +/** + * Handle guest's kick. * * @n: guest kick event notifier, the one that guest set to notify svq. */ -static void vhost_handle_guest_kick(EventNotifier *n) +static void vhost_handle_guest_kick_notifier(EventNotifier *n) { VhostShadowVirtqueue *svq =3D container_of(n, VhostShadowVirtqueue, sv= q_kick); event_notifier_test_and_clear(n); - event_notifier_set(&svq->hdev_kick); + vhost_handle_guest_kick(svq); +} + +static bool vhost_svq_more_used(VhostShadowVirtqueue *svq) +{ + if (svq->last_used_idx !=3D svq->shadow_used_idx) { + return true; + } + + svq->shadow_used_idx =3D cpu_to_le16(svq->vring.used->idx); + + return svq->last_used_idx !=3D svq->shadow_used_idx; } =20 /** - * Forward vhost notifications + * Enable vhost device calls after disable them. + * + * @svq: The svq + * + * It returns false if there are pending used buffers from the vhost devic= e, + * avoiding the possible races between SVQ checking for more work and enab= ling + * callbacks. True if SVQ used vring has no more pending buffers. + */ +static bool vhost_svq_enable_notification(VhostShadowVirtqueue *svq) +{ + svq->vring.avail->flags &=3D ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT); + /* Make sure the flag is written before the read of used_idx */ + smp_mb(); + return !vhost_svq_more_used(svq); +} + +static void vhost_svq_disable_notification(VhostShadowVirtqueue *svq) +{ + svq->vring.avail->flags |=3D cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT); +} + +static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq, + uint32_t *len) +{ + vring_desc_t *descs =3D svq->vring.desc; + const vring_used_t *used =3D svq->vring.used; + vring_used_elem_t used_elem; + uint16_t last_used; + + if (!vhost_svq_more_used(svq)) { + return NULL; + } + + /* Only get used array entries after they have been exposed by dev */ + smp_rmb(); + last_used =3D svq->last_used_idx & (svq->vring.num - 1); + used_elem.id =3D le32_to_cpu(used->ring[last_used].id); + used_elem.len =3D le32_to_cpu(used->ring[last_used].len); + + svq->last_used_idx++; + if (unlikely(used_elem.id >=3D svq->vring.num)) { + qemu_log_mask(LOG_GUEST_ERROR, "Device %s says index %u is used", + svq->vdev->name, used_elem.id); + return NULL; + } + + if (unlikely(!svq->ring_id_maps[used_elem.id])) { + qemu_log_mask(LOG_GUEST_ERROR, + "Device %s says index %u is used, but it was not available", + svq->vdev->name, used_elem.id); + return NULL; + } + + descs[used_elem.id].next =3D svq->free_head; + svq->free_head =3D used_elem.id; + + *len =3D used_elem.len; + return g_steal_pointer(&svq->ring_id_maps[used_elem.id]); +} + +static void vhost_svq_flush(VhostShadowVirtqueue *svq, + bool check_for_avail_queue) +{ + VirtQueue *vq =3D svq->vq; + + /* Forward as many used buffers as possible. */ + do { + unsigned i =3D 0; + + vhost_svq_disable_notification(svq); + while (true) { + uint32_t len; + g_autofree VirtQueueElement *elem =3D vhost_svq_get_buf(svq, &= len); + if (!elem) { + break; + } + + if (unlikely(i >=3D svq->vring.num)) { + qemu_log_mask(LOG_GUEST_ERROR, + "More than %u used buffers obtained in a %u size = SVQ", + i, svq->vring.num); + virtqueue_fill(vq, elem, len, i); + virtqueue_flush(vq, i); + return; + } + virtqueue_fill(vq, elem, len, i++); + } + + virtqueue_flush(vq, i); + event_notifier_set(&svq->svq_call); + + if (check_for_avail_queue && svq->next_guest_avail_elem) { + /* + * Avail ring was full when vhost_svq_flush was called, so it'= s a + * good moment to make more descriptors available if possible. + */ + vhost_handle_guest_kick(svq); + } + } while (!vhost_svq_enable_notification(svq)); +} + +/** + * Forward used buffers. * * @n: hdev call event notifier, the one that device set to notify svq. + * + * Note that we are not making any buffers available in the loop, there is= no + * way that it runs more than virtqueue size times. */ static void vhost_svq_handle_call(EventNotifier *n) { VhostShadowVirtqueue *svq =3D container_of(n, VhostShadowVirtqueue, hdev_call); event_notifier_test_and_clear(n); - event_notifier_set(&svq->svq_call); + vhost_svq_flush(svq, true); } =20 /** @@ -161,7 +442,41 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *s= vq, int svq_kick_fd) if (poll_start) { event_notifier_init_fd(svq_kick, svq_kick_fd); event_notifier_set(svq_kick); - event_notifier_set_handler(svq_kick, vhost_handle_guest_kick); + event_notifier_set_handler(svq_kick, vhost_handle_guest_kick_notif= ier); + } +} + +/** + * Start the shadow virtqueue operation. + * + * @svq: Shadow Virtqueue + * @vdev: VirtIO device + * @vq: Virtqueue to shadow + */ +void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, + VirtQueue *vq) +{ + size_t desc_size, driver_size, device_size; + + svq->next_guest_avail_elem =3D NULL; + svq->shadow_avail_idx =3D 0; + svq->shadow_used_idx =3D 0; + svq->last_used_idx =3D 0; + svq->vdev =3D vdev; + svq->vq =3D vq; + + svq->vring.num =3D virtio_queue_get_num(vdev, virtio_get_queue_index(v= q)); + driver_size =3D vhost_svq_driver_area_size(svq); + device_size =3D vhost_svq_device_area_size(svq); + svq->vring.desc =3D qemu_memalign(qemu_real_host_page_size, driver_siz= e); + desc_size =3D sizeof(vring_desc_t) * svq->vring.num; + svq->vring.avail =3D (void *)((char *)svq->vring.desc + desc_size); + memset(svq->vring.desc, 0, driver_size); + svq->vring.used =3D qemu_memalign(qemu_real_host_page_size, device_siz= e); + memset(svq->vring.used, 0, device_size); + svq->ring_id_maps =3D g_new0(VirtQueueElement *, svq->vring.num); + for (unsigned i =3D 0; i < svq->vring.num - 1; i++) { + svq->vring.desc[i].next =3D cpu_to_le16(i + 1); } } =20 @@ -172,6 +487,31 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *s= vq, int svq_kick_fd) void vhost_svq_stop(VhostShadowVirtqueue *svq) { event_notifier_set_handler(&svq->svq_kick, NULL); + g_autofree VirtQueueElement *next_avail_elem =3D NULL; + + if (!svq->vq) { + return; + } + + /* Send all pending used descriptors to guest */ + vhost_svq_flush(svq, false); + + for (unsigned i =3D 0; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem =3D NULL; + elem =3D g_steal_pointer(&svq->ring_id_maps[i]); + if (elem) { + virtqueue_detach_element(svq->vq, elem, 0); + } + } + + next_avail_elem =3D g_steal_pointer(&svq->next_guest_avail_elem); + if (next_avail_elem) { + virtqueue_detach_element(svq->vq, next_avail_elem, 0); + } + svq->vq =3D NULL; + g_free(svq->ring_id_maps); + qemu_vfree(svq->vring.desc); + qemu_vfree(svq->vring.used); } =20 /** diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 3e2c181d2b..297505f331 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -777,9 +777,9 @@ static int vhost_vdpa_set_vring_dev_addr(struct vhost_d= ev *dev, * Note that this function does not rewind kick file descriptor if cannot = set * call one. */ -static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, - VhostShadowVirtqueue *svq, unsigned idx, - Error **errp) +static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev, + VhostShadowVirtqueue *svq, unsigned idx, + Error **errp) { struct vhost_vring_file file =3D { .index =3D dev->vq_index + idx, @@ -791,7 +791,7 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, r =3D vhost_vdpa_set_vring_dev_kick(dev, &file); if (unlikely(r !=3D 0)) { error_setg_errno(errp, -r, "Can't set device kick fd"); - return false; + return r; } =20 event_notifier =3D &svq->hdev_call; @@ -801,6 +801,95 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, error_setg_errno(errp, -r, "Can't set device call fd"); } =20 + return r; +} + +/** + * Unmap a SVQ area in the device + */ +static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr iova, + hwaddr size) +{ + int r; + + size =3D ROUND_UP(size, qemu_real_host_page_size); + r =3D vhost_vdpa_dma_unmap(v, iova, size); + return r =3D=3D 0; +} + +static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, + const VhostShadowVirtqueue *svq) +{ + struct vhost_vdpa *v =3D dev->opaque; + struct vhost_vring_addr svq_addr; + size_t device_size =3D vhost_svq_device_area_size(svq); + size_t driver_size =3D vhost_svq_driver_area_size(svq); + bool ok; + + vhost_svq_get_vring_addr(svq, &svq_addr); + + ok =3D vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr, driver_si= ze); + if (unlikely(!ok)) { + return false; + } + + return vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr, device_si= ze); +} + +/** + * Map the shadow virtqueue rings in the device + * + * @dev: The vhost device + * @svq: The shadow virtqueue + * @addr: Assigned IOVA addresses + * @errp: Error pointer + */ +static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, + const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr, + Error **errp) +{ + struct vhost_vdpa *v =3D dev->opaque; + size_t device_size =3D vhost_svq_device_area_size(svq); + size_t driver_size =3D vhost_svq_driver_area_size(svq); + int r; + + ERRP_GUARD(); + vhost_svq_get_vring_addr(svq, addr); + + r =3D vhost_vdpa_dma_map(v, addr->desc_user_addr, driver_size, + (void *)(uintptr_t)addr->desc_user_addr, true); + if (unlikely(r !=3D 0)) { + error_setg_errno(errp, -r, "Cannot create vq driver region: "); + return false; + } + + r =3D vhost_vdpa_dma_map(v, addr->used_user_addr, device_size, + (void *)(intptr_t)addr->used_user_addr, false); + if (unlikely(r !=3D 0)) { + error_setg_errno(errp, -r, "Cannot create vq device region: "); + } + + return r =3D=3D 0; +} + +static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, + VhostShadowVirtqueue *svq, unsigned idx, + Error **errp) +{ + uint16_t vq_index =3D dev->vq_index + idx; + struct vhost_vring_state s =3D { + .index =3D vq_index, + }; + int r; + + r =3D vhost_vdpa_set_dev_vring_base(dev, &s); + if (unlikely(r)) { + error_setg_errno(errp, -r, "Cannot set vring base"); + return false; + } + + r =3D vhost_vdpa_svq_set_fds(dev, svq, idx, errp); return r =3D=3D 0; } =20 @@ -815,10 +904,62 @@ static bool vhost_vdpa_svqs_start(struct vhost_dev *d= ev) } =20 for (i =3D 0; i < v->shadow_vqs->len; ++i) { + VirtQueue *vq =3D virtio_get_queue(dev->vdev, dev->vq_index + i); VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, i); + struct vhost_vring_addr addr =3D { + .index =3D i, + }; + int r; bool ok =3D vhost_vdpa_svq_setup(dev, svq, i, &err); if (unlikely(!ok)) { - error_reportf_err(err, "Cannot setup SVQ %u: ", i); + goto err; + } + + vhost_svq_start(svq, dev->vdev, vq); + ok =3D vhost_vdpa_svq_map_rings(dev, svq, &addr, &err); + if (unlikely(!ok)) { + goto err_map; + } + + /* Override vring GPA set by vhost subsystem */ + r =3D vhost_vdpa_set_vring_dev_addr(dev, &addr); + if (unlikely(r !=3D 0)) { + error_setg_errno(&err, -r, "Cannot set device address"); + goto err_set_addr; + } + } + + return true; + +err_set_addr: + vhost_vdpa_svq_unmap_rings(dev, g_ptr_array_index(v->shadow_vqs, i)); + +err_map: + vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, i)); + +err: + error_reportf_err(err, "Cannot setup SVQ %u: ", i); + for (unsigned j =3D 0; j < i; ++j) { + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, j); + vhost_vdpa_svq_unmap_rings(dev, svq); + vhost_svq_stop(svq); + } + + return false; +} + +static bool vhost_vdpa_svqs_stop(struct vhost_dev *dev) +{ + struct vhost_vdpa *v =3D dev->opaque; + + if (!v->shadow_vqs) { + return true; + } + + for (unsigned i =3D 0; i < v->shadow_vqs->len; ++i) { + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, i); + bool ok =3D vhost_vdpa_svq_unmap_rings(dev, svq); + if (unlikely(!ok)) { return false; } } @@ -840,6 +981,10 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev,= bool started) } vhost_vdpa_set_vring_ready(dev); } else { + ok =3D vhost_vdpa_svqs_stop(dev); + if (unlikely(!ok)) { + return -1; + } vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); } =20 --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280582; cv=none; d=zohomail.com; s=zohoarc; b=XFof+mSkzSvyOBe+oSYxbTRxqTimKSzRGHoCQCGZlY6HWsdhOghCxUFOZpJ+CldUyRayDLdeHpjBw68LTH1vVeecajwRI+0SqqFBpa21c41T+pte214wrRRXCqKg96QbaBSJcj6GW2e/ZtGUakRd5XnVkzpyWWlMonOqGOx2YiA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280582; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=GxssvPAYO/l7YEZ7M2UAfXAIMq9A6w12FtPcuaDtEUc=; b=OaNUne3i6ZxdiyZ7THAevCjF6iuUdDCHYdogOszfUM9EqtOq9gaJz47WVV681DV4K4UEts0hQ7DN8G58oIxDlzL/O+3l1fuFl45qLaUBBm0ui9pAto4dCnTJoSDaHTrFRKHh0X+kJ39soTWclydWOK8VCPYwiwyrPdneXuFXUdA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280582642878.5327846683604; Mon, 14 Mar 2022 10:56:22 -0700 (PDT) Received: from localhost ([::1]:33818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTovd-0000bm-IA for importer@patchew.org; Mon, 14 Mar 2022 13:56:21 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobN-0005l0-MJ for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43436) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007ft-Gq for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:24 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-622-tKrONgPQM_uLEOXlWEsGGQ-1; Mon, 14 Mar 2022 13:35:08 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 211C1833974 for ; Mon, 14 Mar 2022 17:35:08 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7537D4B8D5B; Mon, 14 Mar 2022 17:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279309; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GxssvPAYO/l7YEZ7M2UAfXAIMq9A6w12FtPcuaDtEUc=; b=MHSy6lRdDCwHuNt5JncrM9xOAMldbjq55YtApP3rJ7LNNF5F/vt34jzit9eQtgqiIH4+H4 t0+2C+MkUb7iJAGepuZFP4HJnJbCjciqr6CMrOFtuJ1zraCY0cbrif7DaYfWVJM6rsrr4a xoPWw0IE/pP8i1st9uXvkBYB+9eyRfY= X-MC-Unique: tKrONgPQM_uLEOXlWEsGGQ-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 08/15] util: Add iova_tree_alloc_map Date: Mon, 14 Mar 2022 18:34:48 +0100 Message-Id: <20220314173455.200342-9-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280583226100001 This iova tree function allows it to look for a hole in allocated regions and return a totally new translation for a given translated address. It's usage is mainly to allow devices to access qemu address space, remapping guest's one into a new iova space where qemu can add chunks of addresses. Signed-off-by: Eugenio P=C3=A9rez Reviewed-by: Peter Xu --- include/qemu/iova-tree.h | 18 ++++++ util/iova-tree.c | 136 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h index 8249edd764..d066400f09 100644 --- a/include/qemu/iova-tree.h +++ b/include/qemu/iova-tree.h @@ -29,6 +29,7 @@ #define IOVA_OK (0) #define IOVA_ERR_INVALID (-1) /* Invalid parameters */ #define IOVA_ERR_OVERLAP (-2) /* IOVA range overlapped */ +#define IOVA_ERR_NOMEM (-3) /* Cannot allocate */ =20 typedef struct IOVATree IOVATree; typedef struct DMAMap { @@ -119,6 +120,23 @@ const DMAMap *iova_tree_find_address(const IOVATree *t= ree, hwaddr iova); */ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator); =20 +/** + * iova_tree_alloc_map: + * + * @tree: the iova tree to allocate from + * @map: the new map (as translated addr & size) to allocate in the iova r= egion + * @iova_begin: the minimum address of the allocation + * @iova_end: the maximum addressable direction of the allocation + * + * Allocates a new region of a given size, between iova_min and iova_max. + * + * Return: Same as iova_tree_insert, but cannot overlap and can return err= or if + * iova tree is out of free contiguous range. The caller gets the assigned= iova + * in map->iova. + */ +int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin, + hwaddr iova_end); + /** * iova_tree_destroy: * diff --git a/util/iova-tree.c b/util/iova-tree.c index 23ea35b7a4..139626b46f 100644 --- a/util/iova-tree.c +++ b/util/iova-tree.c @@ -16,6 +16,40 @@ struct IOVATree { GTree *tree; }; =20 +/* Args to pass to iova_tree_alloc foreach function. */ +struct IOVATreeAllocArgs { + /* Size of the desired allocation */ + size_t new_size; + + /* The minimum address allowed in the allocation */ + hwaddr iova_begin; + + /* Map at the left of the hole, can be NULL if "this" is first one */ + const DMAMap *prev; + + /* Map at the right of the hole, can be NULL if "prev" is the last one= */ + const DMAMap *this; + + /* If found, we fill in the IOVA here */ + hwaddr iova_result; + + /* Whether have we found a valid IOVA */ + bool iova_found; +}; + +/** + * Iterate args to the next hole + * + * @args: The alloc arguments + * @next: The next mapping in the tree. Can be NULL to signal the last one + */ +static void iova_tree_alloc_args_iterate(struct IOVATreeAllocArgs *args, + const DMAMap *next) +{ + args->prev =3D args->this; + args->this =3D next; +} + static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer da= ta) { const DMAMap *m1 =3D a, *m2 =3D b; @@ -107,6 +141,108 @@ int iova_tree_remove(IOVATree *tree, const DMAMap *ma= p) return IOVA_OK; } =20 +/** + * Try to find an unallocated IOVA range between prev and this elements. + * + * @args: Arguments to allocation + * + * Cases: + * + * (1) !prev, !this: No entries allocated, always succeed + * + * (2) !prev, this: We're iterating at the 1st element. + * + * (3) prev, !this: We're iterating at the last element. + * + * (4) prev, this: this is the most common case, we'll try to find a hole + * between "prev" and "this" mapping. + * + * Note that this function assumes the last valid iova is HWADDR_MAX, but = it + * searches linearly so it's easy to discard the result if it's not the ca= se. + */ +static void iova_tree_alloc_map_in_hole(struct IOVATreeAllocArgs *args) +{ + const DMAMap *prev =3D args->prev, *this =3D args->this; + uint64_t hole_start, hole_last; + + if (this && this->iova + this->size < args->iova_begin) { + return; + } + + hole_start =3D MAX(prev ? prev->iova + prev->size + 1 : 0, args->iova_= begin); + hole_last =3D this ? this->iova : HWADDR_MAX; + + if (hole_last - hole_start > args->new_size) { + args->iova_result =3D hole_start; + args->iova_found =3D true; + } +} + +/** + * Foreach dma node in the tree, compare if there is a hole with its previ= ous + * node (or minimum iova address allowed) and the node. + * + * @key: Node iterating + * @value: Node iterating + * @pargs: Struct to communicate with the outside world + * + * Return: false to keep iterating, true if needs break. + */ +static gboolean iova_tree_alloc_traverse(gpointer key, gpointer value, + gpointer pargs) +{ + struct IOVATreeAllocArgs *args =3D pargs; + DMAMap *node =3D value; + + assert(key =3D=3D value); + + iova_tree_alloc_args_iterate(args, node); + iova_tree_alloc_map_in_hole(args); + return args->iova_found; +} + +int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin, + hwaddr iova_last) +{ + struct IOVATreeAllocArgs args =3D { + .new_size =3D map->size, + .iova_begin =3D iova_begin, + }; + + if (unlikely(iova_last < iova_begin)) { + return IOVA_ERR_INVALID; + } + + /* + * Find a valid hole for the mapping + * + * Assuming low iova_begin, so no need to do a binary search to + * locate the first node. + * + * TODO: Replace all this with g_tree_node_first/next/last when availa= ble + * (from glib since 2.68). To do it with g_tree_foreach complicates the + * code a lot. + * + */ + g_tree_foreach(tree->tree, iova_tree_alloc_traverse, &args); + if (!args.iova_found) { + /* + * Either tree is empty or the last hole is still not checked. + * g_tree_foreach does not compare (last, iova_last] range, so we = check + * it here. + */ + iova_tree_alloc_args_iterate(&args, NULL); + iova_tree_alloc_map_in_hole(&args); + } + + if (!args.iova_found || args.iova_result + map->size > iova_last) { + return IOVA_ERR_NOMEM; + } + + map->iova =3D args.iova_result; + return iova_tree_insert(tree, map); +} + void iova_tree_destroy(IOVATree *tree) { g_tree_destroy(tree->tree); --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280359; cv=none; d=zohomail.com; s=zohoarc; b=KM9xOOZDmeC9hQ+Dz3KpjaZizZtGgtF7T/UPvREybMt6J7WlUmpNoZs5XGzizutLxCtv0hgYpJSfRcMFhR+xweXf/BgE99mvB06nEcGuVZ7sogXSJarLaKYmMrOhYdMrcnJ1cikjPUAKYmDVBrxcd5kGvxSgmhuFDeI0J1IcVnI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280359; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=PX6K9wXjW4B0wwCfgUF3MAxn+Ok1Pe8Jolvp9bod53g=; b=Gg0/qQerLzNvwAaLMU7dvTnhHmxk8v6/J5bbJi9UevELAL+UFkpR1hNlF7s6DL9JkgcnBsBCOjHdiAxfCY5Cn7vgwf+UI5MXkv7pdcHYa7Rwks2X+emK0BahNWTaFQmtEnmibXBW0CAT80rb4L1j+CVmZaajMx0BMYcibL3TecU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280359810793.8086913287657; Mon, 14 Mar 2022 10:52:39 -0700 (PDT) Received: from localhost ([::1]:49144 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTos3-0000OQ-VN for importer@patchew.org; Mon, 14 Mar 2022 13:52:39 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38848) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobL-0005il-9T for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23924) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobH-0007fx-U1 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:22 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-170-s9QZaK33NFGV7Qk9Wt0hjA-1; Mon, 14 Mar 2022 13:35:09 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 346783C11A08 for ; Mon, 14 Mar 2022 17:35:09 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7410B492C14; Mon, 14 Mar 2022 17:35:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279310; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PX6K9wXjW4B0wwCfgUF3MAxn+Ok1Pe8Jolvp9bod53g=; b=QE0Wc0s7AyxnrmXAWw2tkCBpyzRWZSrfsdRsaG5Fi9hiQoY3GKpPNekXzD2Po9H9vjQO+y uIMfBTK/KRLpYQKqyqE06o+s8WDJf1y35eopczWYUEusqcucHeU0zzgW4fuoJPjlaa+sAw fYhvzryKup+nm/usxUGZjn/JG/TtFKQ= X-MC-Unique: s9QZaK33NFGV7Qk9Wt0hjA-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 09/15] util: add iova_tree_find_iova Date: Mon, 14 Mar 2022 18:34:49 +0100 Message-Id: <20220314173455.200342-10-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280361901100001 This function does the reverse operation of iova_tree_find: To look for a mapping that match a translated address so we can do the reverse. This have linear complexity instead of logarithmic, but it supports overlapping HVA. Future developments could reduce it. Signed-off-by: Eugenio P=C3=A9rez --- include/qemu/iova-tree.h | 20 +++++++++++++++++++- util/iova-tree.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h index d066400f09..c938fb0793 100644 --- a/include/qemu/iova-tree.h +++ b/include/qemu/iova-tree.h @@ -83,7 +83,7 @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map); * @tree: the iova tree to search from * @map: the mapping to search * - * Search for a mapping in the iova tree that overlaps with the + * Search for a mapping in the iova tree that iova overlaps with the * mapping range specified. Only the first found mapping will be * returned. * @@ -95,6 +95,24 @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map); */ const DMAMap *iova_tree_find(const IOVATree *tree, const DMAMap *map); =20 +/** + * iova_tree_find_iova: + * + * @tree: the iova tree to search from + * @map: the mapping to search + * + * Search for a mapping in the iova tree that translated_addr overlaps wit= h the + * mapping range specified. Only the first found mapping will be + * returned. + * + * Return: DMAMap pointer if found, or NULL if not found. Note that + * the returned DMAMap pointer is maintained internally. User should + * only read the content but never modify or free the content. Also, + * user is responsible to make sure the pointer is valid (say, no + * concurrent deletion in progress). + */ +const DMAMap *iova_tree_find_iova(const IOVATree *tree, const DMAMap *map); + /** * iova_tree_find_address: * diff --git a/util/iova-tree.c b/util/iova-tree.c index 139626b46f..6dff29c1f6 100644 --- a/util/iova-tree.c +++ b/util/iova-tree.c @@ -37,6 +37,11 @@ struct IOVATreeAllocArgs { bool iova_found; }; =20 +typedef struct IOVATreeFindIOVAArgs { + const DMAMap *needle; + const DMAMap *result; +} IOVATreeFindIOVAArgs; + /** * Iterate args to the next hole * @@ -81,6 +86,35 @@ const DMAMap *iova_tree_find(const IOVATree *tree, const= DMAMap *map) return g_tree_lookup(tree->tree, map); } =20 +static gboolean iova_tree_find_address_iterator(gpointer key, gpointer val= ue, + gpointer data) +{ + const DMAMap *map =3D key; + IOVATreeFindIOVAArgs *args =3D data; + const DMAMap *needle; + + g_assert(key =3D=3D value); + + needle =3D args->needle; + if (map->translated_addr + map->size < needle->translated_addr || + needle->translated_addr + needle->size < map->translated_addr) { + return false; + } + + args->result =3D map; + return true; +} + +const DMAMap *iova_tree_find_iova(const IOVATree *tree, const DMAMap *map) +{ + IOVATreeFindIOVAArgs args =3D { + .needle =3D map, + }; + + g_tree_foreach(tree->tree, iova_tree_find_address_iterator, &args); + return args.result; +} + const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova) { const DMAMap map =3D { .iova =3D iova, .size =3D 0 }; --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280222; cv=none; d=zohomail.com; s=zohoarc; b=m7LVJaQMtQUgiyhtQGaCC8CSDGOYfyQ/FS8aOePyEGeHB6psOH/1y/xGtlehgvRWuWqD71cN5jRJZRmAY2oqhwJNLk9miPrG4aI9izSulwx5ig8rZY1WFT3DcCKrQReHrjuVNWgy/bYafEfigNcB82WagHezRQZRqj+CPAJ/yCU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280222; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=MuncSNL5cIyNNFVX3t8rQOhlPMrP64lBXXUP9IMPAtI=; b=jGaTnnqVg0mif/nWSlWAlmKnVa5Kg21QfHGhcNI1z6Lt3SyRdAT8/FhHUYLcNCJ6/oIA8WnxWQIO8jA8j4kzgLWikTedZhyamlMH72ClAo8M2kPsRKe9eQVDf4atMaqzIQht4de7P3CVboefC4A9+rnX53c85zRgHQUW2HN54yE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16472802222201001.9400715617678; Mon, 14 Mar 2022 10:50:22 -0700 (PDT) Received: from localhost ([::1]:41918 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTopq-0003qi-7m for importer@patchew.org; Mon, 14 Mar 2022 13:50:22 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38868) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobL-0005jC-LG for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40359) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007g1-2U for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-258-7SHymmQ_MTGqiPnP2zL-ug-1; Mon, 14 Mar 2022 13:35:10 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2899B833962 for ; Mon, 14 Mar 2022 17:35:10 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 87F34401E40; Mon, 14 Mar 2022 17:35:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MuncSNL5cIyNNFVX3t8rQOhlPMrP64lBXXUP9IMPAtI=; b=AKah9W5IsuMKSxbKqUjsxZXqoupuFU8D99J8XppoRJXe+t/vKJfpAWOniff5yuQ5uHkhCv xJn6EbMVeYVdXnP6Qmmn2tlhFFF4p3LfT2csTdmtH5khvZOz5OFuAlLwWvMkcXySBFMxnV Y1xULLHpeFfP9h0rLrX8oIh5/DFdRhI= X-MC-Unique: 7SHymmQ_MTGqiPnP2zL-ug-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 10/15] vhost: Add VhostIOVATree Date: Mon, 14 Mar 2022 18:34:50 +0100 Message-Id: <20220314173455.200342-11-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280224359100001 This tree is able to look for a translated address from an IOVA address. At first glance it is similar to util/iova-tree. However, SVQ working on devices with limited IOVA space need more capabilities, like allocating IOVA chunks or performing reverse translations (qemu addresses to iova). The allocation capability, as "assign a free IOVA address to this chunk of memory in qemu's address space" allows shadow virtqueue to create a new address space that is not restricted by guest's addressable one, so we can allocate shadow vqs vrings outside of it. It duplicates the tree so it can search efficiently in both directions, and it will signal overlap if iova or the translated address is present in any tree. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-iova-tree.h | 27 +++++++++ hw/virtio/vhost-iova-tree.c | 110 ++++++++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-iova-tree.h create mode 100644 hw/virtio/vhost-iova-tree.c diff --git a/hw/virtio/vhost-iova-tree.h b/hw/virtio/vhost-iova-tree.h new file mode 100644 index 0000000000..6a4f24e0f9 --- /dev/null +++ b/hw/virtio/vhost-iova-tree.h @@ -0,0 +1,27 @@ +/* + * vhost software live migration iova tree + * + * SPDX-FileCopyrightText: Red Hat, Inc. 2021 + * SPDX-FileContributor: Author: Eugenio P=C3=A9rez + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_VIRTIO_VHOST_IOVA_TREE_H +#define HW_VIRTIO_VHOST_IOVA_TREE_H + +#include "qemu/iova-tree.h" +#include "exec/memory.h" + +typedef struct VhostIOVATree VhostIOVATree; + +VhostIOVATree *vhost_iova_tree_new(uint64_t iova_first, uint64_t iova_last= ); +void vhost_iova_tree_delete(VhostIOVATree *iova_tree); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostIOVATree, vhost_iova_tree_delete); + +const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree, + const DMAMap *map); +int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map); +void vhost_iova_tree_remove(VhostIOVATree *iova_tree, const DMAMap *map); + +#endif diff --git a/hw/virtio/vhost-iova-tree.c b/hw/virtio/vhost-iova-tree.c new file mode 100644 index 0000000000..55fed1fefb --- /dev/null +++ b/hw/virtio/vhost-iova-tree.c @@ -0,0 +1,110 @@ +/* + * vhost software live migration iova tree + * + * SPDX-FileCopyrightText: Red Hat, Inc. 2021 + * SPDX-FileContributor: Author: Eugenio P=C3=A9rez + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/iova-tree.h" +#include "vhost-iova-tree.h" + +#define iova_min_addr qemu_real_host_page_size + +/** + * VhostIOVATree, able to: + * - Translate iova address + * - Reverse translate iova address (from translated to iova) + * - Allocate IOVA regions for translated range (linear operation) + */ +struct VhostIOVATree { + /* First addressable iova address in the device */ + uint64_t iova_first; + + /* Last addressable iova address in the device */ + uint64_t iova_last; + + /* IOVA address to qemu memory maps. */ + IOVATree *iova_taddr_map; +}; + +/** + * Create a new IOVA tree + * + * Returns the new IOVA tree + */ +VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) +{ + VhostIOVATree *tree =3D g_new(VhostIOVATree, 1); + + /* Some devices do not like 0 addresses */ + tree->iova_first =3D MAX(iova_first, iova_min_addr); + tree->iova_last =3D iova_last; + + tree->iova_taddr_map =3D iova_tree_new(); + return tree; +} + +/** + * Delete an iova tree + */ +void vhost_iova_tree_delete(VhostIOVATree *iova_tree) +{ + iova_tree_destroy(iova_tree->iova_taddr_map); + g_free(iova_tree); +} + +/** + * Find the IOVA address stored from a memory address + * + * @tree: The iova tree + * @map: The map with the memory address + * + * Return the stored mapping, or NULL if not found. + */ +const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *tree, + const DMAMap *map) +{ + return iova_tree_find_iova(tree->iova_taddr_map, map); +} + +/** + * Allocate a new mapping + * + * @tree: The iova tree + * @map: The iova map + * + * Returns: + * - IOVA_OK if the map fits in the container + * - IOVA_ERR_INVALID if the map does not make sense (like size overflow) + * - IOVA_ERR_NOMEM if tree cannot allocate more space. + * + * It returns assignated iova in map->iova if return value is VHOST_DMA_MA= P_OK. + */ +int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) +{ + /* Some vhost devices do not like addr 0. Skip first page */ + hwaddr iova_first =3D tree->iova_first ?: qemu_real_host_page_size; + + if (map->translated_addr + map->size < map->translated_addr || + map->perm =3D=3D IOMMU_NONE) { + return IOVA_ERR_INVALID; + } + + /* Allocate a node in IOVA address */ + return iova_tree_alloc_map(tree->iova_taddr_map, map, iova_first, + tree->iova_last); +} + +/** + * Remove existing mappings from iova tree + * + * @iova_tree: The vhost iova tree + * @map: The map to remove + */ +void vhost_iova_tree_remove(VhostIOVATree *iova_tree, const DMAMap *map) +{ + iova_tree_remove(iova_tree->iova_taddr_map, map); +} diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 2dc87613bc..6047670804 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-= stub.c')) =20 virtio_ss =3D ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backe= nd.c', 'vhost-shadow-virtqueue.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backe= nd.c', 'vhost-shadow-virtqueue.c', 'vhost-iova-tree.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloo= n.c')) --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647279782; cv=none; d=zohomail.com; s=zohoarc; b=nNG6oQWiLfZWqODEhrGWMHj5m1uQRIFEVngDwkFJfP5foCXn2S8ysZbifrvgcfwauF/zArNKbWDLJ+UCs3G59HCBnm7UYbhuRbhOefp8fvuJ6sMGAqaa8IwuYvMgdDFnape6Txf3vTQ9qrSFJTBJA6IlhHRDwV7D5YMBQzzo08w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647279782; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/IDe233Foscv9ub9CAH79a7KPRsGquChc4hpy/JiFZI=; b=m4Z0B6VkvjP+qOQ8w7Nux9xUIHVVjyFjMvX6lKFAeYN/EP4anF8cjAHjDHEFxDZT1sMhH79vsBXmGBjB9yqkCZGtG/YWhAQ0+q4M70ARQQc3INo6ZQwTvQwCMdajaUkPrsstuZoeKu+/rqJjdfKZXgGdy1bu8xvc4IFknDKus4I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647279782902459.01671226032295; Mon, 14 Mar 2022 10:43:02 -0700 (PDT) Received: from localhost ([::1]:33142 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nToik-00069U-TQ for importer@patchew.org; Mon, 14 Mar 2022 13:43:02 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38804) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobK-0005iW-5W for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:27566) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobC-0007g9-0V for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:21 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-6IwcshBwPXaactNkbeHS-w-1; Mon, 14 Mar 2022 13:35:11 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 258E4811E7A for ; Mon, 14 Mar 2022 17:35:11 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 702D7492C14; Mon, 14 Mar 2022 17:35:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279312; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/IDe233Foscv9ub9CAH79a7KPRsGquChc4hpy/JiFZI=; b=R9VwMn6FNpzM3a2vP4JaoF6WG5CEjvWuc9JdOHxagIsxcdaVn8wvpUIdOvcqRnx0sIxhHt Ma9IJsAnx1QGxHO6HT1u9BkmM4R9LJU1OlbXOyZX2Alf5j1IX9w1zZBxVhsftokj94pAbM osxtilerid9X+eepq90X9nSpjgVIm6Q= X-MC-Unique: 6IwcshBwPXaactNkbeHS-w-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 11/15] vdpa: Add custom IOTLB translations to SVQ Date: Mon, 14 Mar 2022 18:34:51 +0100 Message-Id: <20220314173455.200342-12-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647279788338100001 Use translations added in VhostIOVATree in SVQ. Only introduce usage here, not allocation and deallocation. As with previous patches, we use the dead code paths of shadow_vqs_enabled to avoid commiting too many changes at once. These are impossible to take at the moment. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 6 +- include/hw/virtio/vhost-vdpa.h | 3 + hw/virtio/vhost-shadow-virtqueue.c | 86 +++++++++++++++++--- hw/virtio/vhost-vdpa.c | 122 ++++++++++++++++++++++++----- 4 files changed, 187 insertions(+), 30 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index 38b3b91ca7..e5e24c536d 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -13,6 +13,7 @@ #include "qemu/event_notifier.h" #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" +#include "hw/virtio/vhost-iova-tree.h" =20 /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue { @@ -43,6 +44,9 @@ typedef struct VhostShadowVirtqueue { /* Virtio device */ VirtIODevice *vdev; =20 + /* IOVA mapping */ + VhostIOVATree *iova_tree; + /* Map for use the guest's descriptors */ VirtQueueElement **ring_id_maps; =20 @@ -75,7 +79,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODev= ice *vdev, VirtQueue *vq); void vhost_svq_stop(VhostShadowVirtqueue *svq); =20 -VhostShadowVirtqueue *vhost_svq_new(void); +VhostShadowVirtqueue *vhost_svq_new(VhostIOVATree *iova_tree); =20 void vhost_svq_free(gpointer vq); G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostShadowVirtqueue, vhost_svq_free); diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h index 009a9f3b6b..ee8e939ad0 100644 --- a/include/hw/virtio/vhost-vdpa.h +++ b/include/hw/virtio/vhost-vdpa.h @@ -14,6 +14,7 @@ =20 #include =20 +#include "hw/virtio/vhost-iova-tree.h" #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" =20 @@ -30,6 +31,8 @@ typedef struct vhost_vdpa { MemoryListener listener; struct vhost_vdpa_iova_range iova_range; bool shadow_vqs_enabled; + /* IOVA mapping used by the Shadow Virtqueue */ + VhostIOVATree *iova_tree; GPtrArray *shadow_vqs; struct vhost_dev *dev; VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX]; diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index ece50b8ece..b232803d1b 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -70,7 +70,59 @@ static uint16_t vhost_svq_available_slots(const VhostSha= dowVirtqueue *svq) return svq->vring.num - (svq->shadow_avail_idx - svq->shadow_used_idx); } =20 -static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, +/** + * Translate addresses between the qemu's virtual address and the SVQ IOVA + * + * @svq: Shadow VirtQueue + * @vaddr: Translated IOVA addresses + * @iovec: Source qemu's VA addresses + * @num: Length of iovec and minimum length of vaddr + */ +static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq, + hwaddr *addrs, const struct iovec *io= vec, + size_t num) +{ + if (num =3D=3D 0) { + return true; + } + + for (size_t i =3D 0; i < num; ++i) { + DMAMap needle =3D { + .translated_addr =3D (hwaddr)(uintptr_t)iovec[i].iov_base, + .size =3D iovec[i].iov_len, + }; + Int128 needle_last, map_last; + size_t off; + + const DMAMap *map =3D vhost_iova_tree_find_iova(svq->iova_tree, &n= eedle); + /* + * Map cannot be NULL since iova map contains all guest space and + * qemu already has a physical address mapped + */ + if (unlikely(!map)) { + qemu_log_mask(LOG_GUEST_ERROR, + "Invalid address 0x%"HWADDR_PRIx" given by guest= ", + needle.translated_addr); + return false; + } + + off =3D needle.translated_addr - map->translated_addr; + addrs[i] =3D map->iova + off; + + needle_last =3D int128_add(int128_make64(needle.translated_addr), + int128_make64(iovec[i].iov_len)); + map_last =3D int128_make64(map->translated_addr + map->size); + if (unlikely(int128_gt(needle_last, map_last))) { + qemu_log_mask(LOG_GUEST_ERROR, + "Guest buffer expands over iova range"); + return false; + } + } + + return true; +} + +static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg, const struct iovec *iovec, size_t num, bool more_descs, bool write) { @@ -89,7 +141,7 @@ static void vhost_vring_write_descs(VhostShadowVirtqueue= *svq, } else { descs[i].flags =3D flags; } - descs[i].addr =3D cpu_to_le64((hwaddr)(intptr_t)iovec[n].iov_base); + descs[i].addr =3D cpu_to_le64(sg[n]); descs[i].len =3D cpu_to_le32(iovec[n].iov_len); =20 last =3D i; @@ -104,6 +156,8 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *s= vq, { unsigned avail_idx; vring_avail_t *avail =3D svq->vring.avail; + bool ok; + g_autofree hwaddr *sgs =3D g_new(hwaddr, MAX(elem->out_num, elem->in_n= um)); =20 *head =3D svq->free_head; =20 @@ -114,9 +168,20 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *= svq, return false; } =20 - vhost_vring_write_descs(svq, elem->out_sg, elem->out_num, elem->in_num= > 0, - false); - vhost_vring_write_descs(svq, elem->in_sg, elem->in_num, false, true); + ok =3D vhost_svq_translate_addr(svq, sgs, elem->out_sg, elem->out_num); + if (unlikely(!ok)) { + return false; + } + vhost_vring_write_descs(svq, sgs, elem->out_sg, elem->out_num, + elem->in_num > 0, false); + + + ok =3D vhost_svq_translate_addr(svq, sgs, elem->in_sg, elem->in_num); + if (unlikely(!ok)) { + return false; + } + + vhost_vring_write_descs(svq, sgs, elem->in_sg, elem->in_num, false, tr= ue); =20 /* * Put the entry in the available array (but don't update avail->idx u= ntil @@ -395,9 +460,9 @@ void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *sv= q, int call_fd) void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, struct vhost_vring_addr *addr) { - addr->desc_user_addr =3D (uint64_t)(intptr_t)svq->vring.desc; - addr->avail_user_addr =3D (uint64_t)(intptr_t)svq->vring.avail; - addr->used_user_addr =3D (uint64_t)(intptr_t)svq->vring.used; + addr->desc_user_addr =3D (uint64_t)(uintptr_t)svq->vring.desc; + addr->avail_user_addr =3D (uint64_t)(uintptr_t)svq->vring.avail; + addr->used_user_addr =3D (uint64_t)(uintptr_t)svq->vring.used; } =20 size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq) @@ -518,11 +583,13 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq) * Creates vhost shadow virtqueue, and instructs the vhost device to use t= he * shadow methods and file descriptors. * + * @iova_tree: Tree to perform descriptors translations + * * Returns the new virtqueue or NULL. * * In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new(void) +VhostShadowVirtqueue *vhost_svq_new(VhostIOVATree *iova_tree) { g_autofree VhostShadowVirtqueue *svq =3D g_new0(VhostShadowVirtqueue, = 1); int r; @@ -543,6 +610,7 @@ VhostShadowVirtqueue *vhost_svq_new(void) =20 event_notifier_init_fd(&svq->svq_kick, VHOST_FILE_UNBIND); event_notifier_set_handler(&svq->hdev_call, vhost_svq_handle_call); + svq->iova_tree =3D iova_tree; return g_steal_pointer(&svq); =20 err_init_hdev_call: diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 297505f331..ec38189492 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -209,6 +209,21 @@ static void vhost_vdpa_listener_region_add(MemoryListe= ner *listener, vaddr, section->readonly); =20 llsize =3D int128_sub(llend, int128_make64(iova)); + if (v->shadow_vqs_enabled) { + DMAMap mem_region =3D { + .translated_addr =3D (hwaddr)(uintptr_t)vaddr, + .size =3D int128_get64(llsize) - 1, + .perm =3D IOMMU_ACCESS_FLAG(true, section->readonly), + }; + + int r =3D vhost_iova_tree_map_alloc(v->iova_tree, &mem_region); + if (unlikely(r !=3D IOVA_OK)) { + error_report("Can't allocate a mapping (%d)", r); + goto fail; + } + + iova =3D mem_region.iova; + } =20 vhost_vdpa_iotlb_batch_begin_once(v); ret =3D vhost_vdpa_dma_map(v, iova, int128_get64(llsize), @@ -261,6 +276,20 @@ static void vhost_vdpa_listener_region_del(MemoryListe= ner *listener, =20 llsize =3D int128_sub(llend, int128_make64(iova)); =20 + if (v->shadow_vqs_enabled) { + const DMAMap *result; + const void *vaddr =3D memory_region_get_ram_ptr(section->mr) + + section->offset_within_region + + (iova - section->offset_within_address_space); + DMAMap mem_region =3D { + .translated_addr =3D (hwaddr)(uintptr_t)vaddr, + .size =3D int128_get64(llsize) - 1, + }; + + result =3D vhost_iova_tree_find_iova(v->iova_tree, &mem_region); + iova =3D result->iova; + vhost_iova_tree_remove(v->iova_tree, &mem_region); + } vhost_vdpa_iotlb_batch_begin_once(v); ret =3D vhost_vdpa_dma_unmap(v, iova, int128_get64(llsize)); if (ret) { @@ -370,7 +399,7 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, = struct vhost_vdpa *v, =20 shadow_vqs =3D g_ptr_array_new_full(hdev->nvqs, vhost_svq_free); for (unsigned n =3D 0; n < hdev->nvqs; ++n) { - g_autoptr(VhostShadowVirtqueue) svq =3D vhost_svq_new(); + g_autoptr(VhostShadowVirtqueue) svq =3D vhost_svq_new(v->iova_tree= ); =20 if (unlikely(!svq)) { error_setg(errp, "Cannot create svq %u", n); @@ -807,33 +836,70 @@ static int vhost_vdpa_svq_set_fds(struct vhost_dev *d= ev, /** * Unmap a SVQ area in the device */ -static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr iova, - hwaddr size) +static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, + const DMAMap *needle) { + const DMAMap *result =3D vhost_iova_tree_find_iova(v->iova_tree, needl= e); + hwaddr size; int r; =20 - size =3D ROUND_UP(size, qemu_real_host_page_size); - r =3D vhost_vdpa_dma_unmap(v, iova, size); + if (unlikely(!result)) { + error_report("Unable to find SVQ address to unmap"); + return false; + } + + size =3D ROUND_UP(result->size, qemu_real_host_page_size); + r =3D vhost_vdpa_dma_unmap(v, result->iova, size); return r =3D=3D 0; } =20 static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, const VhostShadowVirtqueue *svq) { + DMAMap needle =3D {}; struct vhost_vdpa *v =3D dev->opaque; struct vhost_vring_addr svq_addr; - size_t device_size =3D vhost_svq_device_area_size(svq); - size_t driver_size =3D vhost_svq_driver_area_size(svq); bool ok; =20 vhost_svq_get_vring_addr(svq, &svq_addr); =20 - ok =3D vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr, driver_si= ze); + needle.translated_addr =3D svq_addr.desc_user_addr; + ok =3D vhost_vdpa_svq_unmap_ring(v, &needle); if (unlikely(!ok)) { return false; } =20 - return vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr, device_si= ze); + needle.translated_addr =3D svq_addr.used_user_addr; + return vhost_vdpa_svq_unmap_ring(v, &needle); +} + +/** + * Map the SVQ area in the device + * + * @v: Vhost-vdpa device + * @needle: The area to search iova + * @errorp: Error pointer + */ +static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, + Error **errp) +{ + int r; + + r =3D vhost_iova_tree_map_alloc(v->iova_tree, needle); + if (unlikely(r !=3D IOVA_OK)) { + error_setg(errp, "Cannot allocate iova (%d)", r); + return false; + } + + r =3D vhost_vdpa_dma_map(v, needle->iova, needle->size + 1, + (void *)(uintptr_t)needle->translated_addr, + needle->perm =3D=3D IOMMU_RO); + if (unlikely(r !=3D 0)) { + error_setg_errno(errp, -r, "Cannot map region to device"); + vhost_iova_tree_remove(v->iova_tree, needle); + } + + return r =3D=3D 0; } =20 /** @@ -849,28 +915,44 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev= *dev, struct vhost_vring_addr *addr, Error **errp) { + DMAMap device_region, driver_region; + struct vhost_vring_addr svq_addr; struct vhost_vdpa *v =3D dev->opaque; size_t device_size =3D vhost_svq_device_area_size(svq); size_t driver_size =3D vhost_svq_driver_area_size(svq); - int r; + size_t avail_offset; + bool ok; =20 ERRP_GUARD(); - vhost_svq_get_vring_addr(svq, addr); + vhost_svq_get_vring_addr(svq, &svq_addr); =20 - r =3D vhost_vdpa_dma_map(v, addr->desc_user_addr, driver_size, - (void *)(uintptr_t)addr->desc_user_addr, true); - if (unlikely(r !=3D 0)) { - error_setg_errno(errp, -r, "Cannot create vq driver region: "); + driver_region =3D (DMAMap) { + .translated_addr =3D svq_addr.desc_user_addr, + .size =3D driver_size - 1, + .perm =3D IOMMU_RO, + }; + ok =3D vhost_vdpa_svq_map_ring(v, &driver_region, errp); + if (unlikely(!ok)) { + error_prepend(errp, "Cannot create vq driver region: "); return false; } + addr->desc_user_addr =3D driver_region.iova; + avail_offset =3D svq_addr.avail_user_addr - svq_addr.desc_user_addr; + addr->avail_user_addr =3D driver_region.iova + avail_offset; =20 - r =3D vhost_vdpa_dma_map(v, addr->used_user_addr, device_size, - (void *)(intptr_t)addr->used_user_addr, false); - if (unlikely(r !=3D 0)) { - error_setg_errno(errp, -r, "Cannot create vq device region: "); + device_region =3D (DMAMap) { + .translated_addr =3D svq_addr.used_user_addr, + .size =3D device_size - 1, + .perm =3D IOMMU_RW, + }; + ok =3D vhost_vdpa_svq_map_ring(v, &device_region, errp); + if (unlikely(!ok)) { + error_prepend(errp, "Cannot create vq device region: "); + vhost_vdpa_svq_unmap_ring(v, &driver_region); } + addr->used_user_addr =3D device_region.iova; =20 - return r =3D=3D 0; + return ok; } =20 static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280291; cv=none; d=zohomail.com; s=zohoarc; b=jt5PO+mU0A7TzBS95eV/9I9BDp4pJPI+2wSer7HaplCuSp00JO8LcJjcv8vcSxS4cPmv+SV8WEM2M8k7oqOSfa3RqQkV7x71iUIwoZGqeei5ZT82RYHWlEorBCycM7/sf/jPCC894pCUMiD6KeASUPhxmvXWiUswCI7ecf1z8Pg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280291; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=awJA20GrHdmE5mx1jebRcOJ+jzW4aMns4g2tdpXeRaI=; b=Ef3MJXLYlSsi2OWEh6ylqXUwzDxPO6RCDv76e/Co20sd2pgIE7a3OMEf6J3h6Lao1uILVPWA/N9uHBmoVHfTEekJ3lt6W7G31Xa58iXmXVazP74FI7x9UZUCa3tb+VcQ5593ihBJeLpNfFDi+FX0zLLKc/8Lssh5uiH+825gRb8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280291507396.9050829278144; Mon, 14 Mar 2022 10:51:31 -0700 (PDT) Received: from localhost ([::1]:44786 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nToqx-0005nv-P2 for importer@patchew.org; Mon, 14 Mar 2022 13:51:31 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38934) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobN-0005lB-NS for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:39969) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobI-0007gL-Ij for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-376-jvonCG3lOKadFXUx7_lPGQ-1; Mon, 14 Mar 2022 13:35:12 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3279D3806706 for ; Mon, 14 Mar 2022 17:35:12 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 70FD2401E3B; Mon, 14 Mar 2022 17:35:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279313; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=awJA20GrHdmE5mx1jebRcOJ+jzW4aMns4g2tdpXeRaI=; b=YqP+/Sq3385mFSSeYzoFB5+MIrrnJbmiT7Z5KvGfAgl7coMXgQlB+F+KKQE8L4rKt0q2iW s2aEjW9622bkxQ6Z7TknXFimaW0crWwKj6WYggYyYcUYyLbN6jv5wHvUiJ7XVu7sB++pay 9IkzJDOdiLlAdw8o60SJo+iigojlYuI= X-MC-Unique: jvonCG3lOKadFXUx7_lPGQ-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 12/15] vdpa: Adapt vhost_vdpa_get_vring_base to SVQ Date: Mon, 14 Mar 2022 18:34:52 +0100 Message-Id: <20220314173455.200342-13-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280293333100001 This is needed to achieve migration, so the destination can restore its index. Setting base as last used idx, so destination will see as available all the entries that the device did not use, including the in-flight processing ones. This is ok for networking, but other kinds of devices might have problems with these retransmissions. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-vdpa.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index ec38189492..93dbe1043c 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1141,8 +1141,25 @@ static int vhost_vdpa_set_vring_base(struct vhost_de= v *dev, static int vhost_vdpa_get_vring_base(struct vhost_dev *dev, struct vhost_vring_state *ring) { + struct vhost_vdpa *v =3D dev->opaque; int ret; =20 + if (v->shadow_vqs_enabled) { + VhostShadowVirtqueue *svq =3D g_ptr_array_index(v->shadow_vqs, + ring->index); + + /* + * Setting base as last used idx, so destination will see as avail= able + * all the entries that the device did not use, including the in-f= light + * processing ones. + * + * TODO: This is ok for networking, but other kinds of devices mig= ht + * have problems with these retransmissions. + */ + ring->num =3D svq->last_used_idx; + return 0; + } + ret =3D vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring); trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num); return ret; --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280391; cv=none; d=zohomail.com; s=zohoarc; b=RGx43duqaWp4uRKs0bpN6IKgXk4VCstaTMBVPDidZaMqU8SNM1PYaorRDsO3m3gy8Xn2sY1JyF4UqtCnwFH4iRmhsom8V15cAEj2HC/FbFuv+olXqbuWvsazwjnT1usMCZnz2QdLZYyYZerWkmn/wasiHccjo0wSk8wGoa35czI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280391; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=svq5CDmPiqJ2rkrBQJKCRCBEIDkD8kuZxACvp9vrFEc=; b=I8Tdhn+9fGREBnwcAO13CFrSwS6I+VnjJHgErhesfb1SKgh5WoJA15NeR4CTbAH6ZPeHco0HGgT5/+AGSXR4Sb0DfX+AviEuT+1LeB0Vdj8Y1KAHAfr3nWs9WQ3wnOA3/hyZUgzhLR6s/DS3c6hhmI1q0PmBAh3it4QadwaPmC0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280391480566.3089987627638; Mon, 14 Mar 2022 10:53:11 -0700 (PDT) Received: from localhost ([::1]:51334 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTosY-0001qM-Fy for importer@patchew.org; Mon, 14 Mar 2022 13:53:10 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38936) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobO-0005m4-19 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:52443) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobJ-0007gn-1r for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:25 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-93-Ofl7KG1dOs2OnRz-xhrLJQ-1; Mon, 14 Mar 2022 13:35:13 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 64F11185A7BA for ; Mon, 14 Mar 2022 17:35:13 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FCCB4B8D5B; Mon, 14 Mar 2022 17:35:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279314; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=svq5CDmPiqJ2rkrBQJKCRCBEIDkD8kuZxACvp9vrFEc=; b=Ozd0bGzZx2tfJatKPv/UpPRulVlverXp3LrBFFfstAYA3eQyvLcW0wGsIP4uucrO9VwMpX gWrII0YweaNUZDiVVk6TR9IlYEkQYptToUu8EeRnisILgjZcYgRRdS1oUDbIaokAW71KU+ 8D9kZ6M5JQyNXkITPnMa6dRsQ+hGIQ4= X-MC-Unique: Ofl7KG1dOs2OnRz-xhrLJQ-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 13/15] vdpa: Never set log_base addr if SVQ is enabled Date: Mon, 14 Mar 2022 18:34:53 +0100 Message-Id: <20220314173455.200342-14-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280392110100001 Setting the log address would make the device start reporting invalid dirty memory because the SVQ vrings are located in qemu's memory. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-vdpa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 93dbe1043c..89d59f86f9 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1090,7 +1090,8 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev= , bool started) static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base, struct vhost_log *log) { - if (vhost_vdpa_one_time_request(dev)) { + struct vhost_vdpa *v =3D dev->opaque; + if (v->shadow_vqs_enabled || vhost_vdpa_one_time_request(dev)) { return 0; } =20 --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280455; cv=none; d=zohomail.com; s=zohoarc; b=gHetGpFeMg+z7maOJbOmJkkdU35G/LklzfU3qYiMFgGS4z+iYKXm3LkaHtdAJQGobI02j7f+WwgpRXUSGFkMoWOH65gtDwozn4PjEpDB+fxwK7mH2JvuBGe5vpdQSpWODC1AlWMmuK+Uaj1tsrWW2pVdxLHH1DTLPDGM1El/Aas= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280455; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=GKjcMAO3iDRmerFDUh8Wh3yafs8xRLGRY3fRz/lK/v0=; b=bIPmYVUw+kXBLJi6gszyoprlKMYxDGtvLIpyubecyF/JBkHjgn3MjVCOlNf6KHcDoTv3cWMTmxpUqsLMlymE9A17CIhgxqPmfmyC1/r7g/z3VfI+hXeXVIYWJNUTwV/YOaiXjwimNjexI/8HPL541OuAZwL0axW1EVJIuxkZFlE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280455400144.732293049928; Mon, 14 Mar 2022 10:54:15 -0700 (PDT) Received: from localhost ([::1]:55492 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTota-0004bw-Am for importer@patchew.org; Mon, 14 Mar 2022 13:54:14 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38846) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobL-0005ii-90 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:59604) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobH-0007h1-Ul for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:22 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-665-GoQ-m5BzOA2ylFt6wpjK5Q-1; Mon, 14 Mar 2022 13:35:14 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4E9541801387 for ; Mon, 14 Mar 2022 17:35:14 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id AC15B492C14; Mon, 14 Mar 2022 17:35:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279315; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GKjcMAO3iDRmerFDUh8Wh3yafs8xRLGRY3fRz/lK/v0=; b=VoJVqQ2Locrsft9sLodT1Dy38XBlopTXM7R838N+pjV+wh0UZm3bg0XmH+fznYwcvQeXpv CINjUXF3T1sr8KFN+c9XRa5UdF0xbjQgNhjB9Bee8MYbXBByVZLFCh7efBru3zJ+KCW5xP 7z9f9pC+gojxvYTGqPAdlM8NAr627Us= X-MC-Unique: GoQ-m5BzOA2ylFt6wpjK5Q-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 14/15] vdpa: Expose VHOST_F_LOG_ALL on SVQ Date: Mon, 14 Mar 2022 18:34:54 +0100 Message-Id: <20220314173455.200342-15-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280456897100001 SVQ is able to log the dirty bits by itself, so let's use it to not block migration. Also, ignore set and clear of VHOST_F_LOG_ALL on set_features if SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region. The log region is still allocated. Future changes might skip that, but this series is already long enough. Signed-off-by: Eugenio P=C3=A9rez --- include/hw/virtio/vhost-vdpa.h | 1 + hw/virtio/vhost-vdpa.c | 39 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h index ee8e939ad0..a29dbb3f53 100644 --- a/include/hw/virtio/vhost-vdpa.h +++ b/include/hw/virtio/vhost-vdpa.h @@ -30,6 +30,7 @@ typedef struct vhost_vdpa { bool iotlb_batch_begin_sent; MemoryListener listener; struct vhost_vdpa_iova_range iova_range; + uint64_t acked_features; bool shadow_vqs_enabled; /* IOVA mapping used by the Shadow Virtqueue */ VhostIOVATree *iova_tree; diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 89d59f86f9..c5ed7a3779 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -373,6 +373,16 @@ static bool vhost_vdpa_one_time_request(struct vhost_d= ev *dev) return v->index !=3D 0; } =20 +static int vhost_vdpa_get_dev_features(struct vhost_dev *dev, + uint64_t *features) +{ + int ret; + + ret =3D vhost_vdpa_call(dev, VHOST_GET_FEATURES, features); + trace_vhost_vdpa_get_features(dev, *features); + return ret; +} + static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *= v, Error **errp) { @@ -385,7 +395,7 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, = struct vhost_vdpa *v, return 0; } =20 - r =3D hdev->vhost_ops->vhost_get_features(hdev, &dev_features); + r =3D vhost_vdpa_get_dev_features(hdev, &dev_features); if (r !=3D 0) { error_setg_errno(errp, -r, "Can't get vdpa device features"); return r; @@ -610,12 +620,29 @@ static int vhost_vdpa_set_mem_table(struct vhost_dev = *dev, static int vhost_vdpa_set_features(struct vhost_dev *dev, uint64_t features) { + struct vhost_vdpa *v =3D dev->opaque; int ret; =20 if (vhost_vdpa_one_time_request(dev)) { return 0; } =20 + if (v->shadow_vqs_enabled) { + if ((v->acked_features ^ features) =3D=3D BIT_ULL(VHOST_F_LOG_ALL)= ) { + /* + * QEMU is just trying to enable or disable logging. SVQ handl= es + * this sepparately, so no need to forward this. + */ + v->acked_features =3D features; + return 0; + } + + v->acked_features =3D features; + + /* We must not ack _F_LOG if SVQ is enabled */ + features &=3D ~BIT_ULL(VHOST_F_LOG_ALL); + } + trace_vhost_vdpa_set_features(dev, features); ret =3D vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features); if (ret) { @@ -1200,10 +1227,14 @@ static int vhost_vdpa_set_vring_call(struct vhost_d= ev *dev, static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features) { - int ret; + struct vhost_vdpa *v =3D dev->opaque; + int ret =3D vhost_vdpa_get_dev_features(dev, features); + + if (ret =3D=3D 0 && v->shadow_vqs_enabled) { + /* Add SVQ logging capabilities */ + *features |=3D BIT_ULL(VHOST_F_LOG_ALL); + } =20 - ret =3D vhost_vdpa_call(dev, VHOST_GET_FEATURES, features); - trace_vhost_vdpa_get_features(dev, *features); return ret; } =20 --=20 2.27.0 From nobody Sun May 12 15:12:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1647280209; cv=none; d=zohomail.com; s=zohoarc; b=Sm5c61w84eVXmhA88W4qSLQ1NUR1zpCW3OIpIkqlZseIO9rcyWP0waI/jsFwbmbr/+Rl15Ran91eENgJ2BVumrBMg3WKBSCHzLEMCyDVpm46J+nrxNTB/JIesiZnQskJCZey9ZtYoLFzFfqTCuN4bCuuYw+afQB62YVz3ds8JSo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647280209; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bZ786sOFfxifS5n262Wr3sDZEi9zsYiqATnXazjh51U=; b=T4vY/1BakHio27bQ1l2CbXi+vvPM4hD5WlJNGXIOEyzpP/p7/eiRusAjDJGR7JgAg/xYszCnKpZY8eE9TLUVxI9AERScW9unxhv5o/bTcMv+A89ayMZhILVD9gl5c49qntUlXsYMs1FPdvPBOc09aXsbrKkaTvedH0MKpKJp5cY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647280209581574.5375979818025; Mon, 14 Mar 2022 10:50:09 -0700 (PDT) Received: from localhost ([::1]:41864 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTopc-0003oL-DL for importer@patchew.org; Mon, 14 Mar 2022 13:50:08 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38844) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobL-0005ih-88 for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:49639) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTobH-0007h7-Vx for qemu-devel@nongnu.org; Mon, 14 Mar 2022 13:35:22 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-501-rn3Z6X3cOXS-RU6gg14MKg-1; Mon, 14 Mar 2022 13:35:16 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 36D6129A9CC9 for ; Mon, 14 Mar 2022 17:35:15 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.194.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 94F07492C14; Mon, 14 Mar 2022 17:35:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647279317; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bZ786sOFfxifS5n262Wr3sDZEi9zsYiqATnXazjh51U=; b=gVOea+x0Nmjl7/WFDDGV55o3W60U4JFf6zKdKM7ywg09KxNcGM+IO4NoF4HlZrIWmbUnFs +oeUhQA7kKmUIKzJW8lyMCSmSnYGFlt4IvEudrgrkzoeOgaaOXqIg5pqgArW5uN4K+cq6J iOW6x7wizR/PJ9pVE1xyUKkd6z69cks= X-MC-Unique: rn3Z6X3cOXS-RU6gg14MKg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH v6 15/15] vdpa: Add x-svq to NetdevVhostVDPAOptions Date: Mon, 14 Mar 2022 18:34:55 +0100 Message-Id: <20220314173455.200342-16-eperezma@redhat.com> In-Reply-To: <20220314173455.200342-1-eperezma@redhat.com> References: <20220314173455.200342-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647280210808100001 Finally offering the possibility to enable SVQ from the command line. Signed-off-by: Eugenio P=C3=A9rez --- qapi/net.json | 8 +++++++- net/vhost-vdpa.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/qapi/net.json b/qapi/net.json index 7fab2e7cd8..271dfa7708 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -445,12 +445,18 @@ # @queues: number of queues to be created for multiqueue vhost-vdpa # (default: 1) # +# @x-svq: Start device with (experimental) shadow virtqueue. (Since 7.1) +# +# Features: +# @unstable: Member @x-svq is experimental. +# # Since: 5.1 ## { 'struct': 'NetdevVhostVDPAOptions', 'data': { '*vhostdev': 'str', - '*queues': 'int' } } + '*queues': 'int', + '*x-svq': {'type': 'bool', 'features' : [ 'unstable'] } } } =20 ## # @NetClientDriver: diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index 1e9fe47c03..def738998b 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -127,7 +127,11 @@ err_init: static void vhost_vdpa_cleanup(NetClientState *nc) { VhostVDPAState *s =3D DO_UPCAST(VhostVDPAState, nc, nc); + struct vhost_dev *dev =3D s->vhost_vdpa.dev; =20 + if (dev && dev->vq_index + dev->nvqs =3D=3D dev->vq_index_end) { + g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete); + } if (s->vhost_net) { vhost_net_cleanup(s->vhost_net); g_free(s->vhost_net); @@ -187,13 +191,23 @@ static NetClientInfo net_vhost_vdpa_info =3D { .check_peer_type =3D vhost_vdpa_check_peer_type, }; =20 +static int vhost_vdpa_get_iova_range(int fd, + struct vhost_vdpa_iova_range *iova_ra= nge) +{ + int ret =3D ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); + + return ret < 0 ? -errno : 0; +} + static NetClientState *net_vhost_vdpa_init(NetClientState *peer, - const char *device, - const char *name, - int vdpa_device_fd, - int queue_pair_index, - int nvqs, - bool is_datapath) + const char *device, + const char *name, + int vdpa_device_fd, + int queue_pair_index, + int nvqs, + bool is_datapath, + bool svq, + VhostIOVATree *iova_tree) { NetClientState *nc =3D NULL; VhostVDPAState *s; @@ -211,6 +225,8 @@ static NetClientState *net_vhost_vdpa_init(NetClientSta= te *peer, =20 s->vhost_vdpa.device_fd =3D vdpa_device_fd; s->vhost_vdpa.index =3D queue_pair_index; + s->vhost_vdpa.shadow_vqs_enabled =3D svq; + s->vhost_vdpa.iova_tree =3D iova_tree; ret =3D vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, n= vqs); if (ret) { qemu_del_net_client(nc); @@ -266,6 +282,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const cha= r *name, g_autofree NetClientState **ncs =3D NULL; NetClientState *nc; int queue_pairs, i, has_cvq =3D 0; + g_autoptr(VhostIOVATree) iova_tree =3D NULL; =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_VHOST_VDPA); opts =3D &netdev->u.vhost_vdpa; @@ -285,29 +302,44 @@ int net_init_vhost_vdpa(const Netdev *netdev, const c= har *name, qemu_close(vdpa_device_fd); return queue_pairs; } + if (opts->x_svq) { + struct vhost_vdpa_iova_range iova_range; + + if (has_cvq) { + error_setg(errp, "vdpa svq does not work with cvq"); + goto err_svq; + } + vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range); + iova_tree =3D vhost_iova_tree_new(iova_range.first, iova_range.las= t); + } =20 ncs =3D g_malloc0(sizeof(*ncs) * queue_pairs); =20 for (i =3D 0; i < queue_pairs; i++) { ncs[i] =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, - vdpa_device_fd, i, 2, true); + vdpa_device_fd, i, 2, true, opts->x_s= vq, + iova_tree); if (!ncs[i]) goto err; } =20 if (has_cvq) { nc =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, - vdpa_device_fd, i, 1, false); + vdpa_device_fd, i, 1, false, opts->x_svq, + iova_tree); if (!nc) goto err; } =20 + iova_tree =3D NULL; return 0; =20 err: if (i) { qemu_del_net_client(ncs[0]); } + +err_svq: qemu_close(vdpa_device_fd); =20 return -1; --=20 2.27.0