From nobody Fri Nov 14 21:05:49 2025 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=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1760615125; cv=none; d=zohomail.com; s=zohoarc; b=gM2zuBDnH4upYzgmsdwrqRliKfdnTak710EwicXWfKtEMCgnH1S59kJso8O/DMFF7MJMT2mj7BjNZJN+27yuQY98RAcnh1hW+L+vvQVV5OiV3c0CcGY8831tz41yJD7fY9pdswgzxHpUElBxrtjQxD+2+TpSlP94ISUsyBVpdMw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760615125; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4cgovY7tyDau9EmQRY+aKBap0SqRZ3wu+IcsVV5ZIew=; b=SEX+lDtaJzK3XhO98H9dG3pMe27ipTYTRkmRvQ2x2EchCTZr0aYaDKonsBpV8Nk1dlrr0BlfepcwWHqPrt7ZI5hfLrWUP0z9CYEBhG189r4Y03fqWhQUBV9YVYqGaxuFUZmCcQZPvMUPiVYcEY9VfzuGH9hly0iyec3jCjhnkBU= 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 1760615125224474.3223560073129; Thu, 16 Oct 2025 04:45:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9MNi-0007Xj-6D; Thu, 16 Oct 2025 07:42:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v9MN8-0007Et-Ff; Thu, 16 Oct 2025 07:42:21 -0400 Received: from forwardcorp1a.mail.yandex.net ([178.154.239.72]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v9MMp-0003WD-EU; Thu, 16 Oct 2025 07:42:13 -0400 Received: from mail-nwsmtp-smtp-corp-main-69.vla.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-69.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:3a87:0:640:845c:0]) by forwardcorp1a.mail.yandex.net (Yandex) with ESMTPS id 200DEC01BE; Thu, 16 Oct 2025 14:41:38 +0300 (MSK) Received: from vsementsov-lin.. (unknown [2a02:6bf:8080:a8c::1:19]) by mail-nwsmtp-smtp-corp-main-69.vla.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id LfP2M73FEmI0-jabrmu5C; Thu, 16 Oct 2025 14:41:37 +0300 Precedence: bulk X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1760614897; bh=4cgovY7tyDau9EmQRY+aKBap0SqRZ3wu+IcsVV5ZIew=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=OrheK5kWODMj9Sm46WDHPjCXdSUUw773g/qtifCblTi3dJZ/aIUb5UrRZCV3YRCRz QAIhycvDA9+Gu9Rop2ssBVP2TwoNAn5o/s7HKFCRs0zah1HSuInZ7xbiYSrNm7dJuf 0UXSFQ6rtSJWL1TYLpNWsU4nTKOppKZoQxdVXJr0= Authentication-Results: mail-nwsmtp-smtp-corp-main-69.vla.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: raphael@enfabrica.net, pbonzini@redhat.com, farosas@suse.de Cc: mst@redhat.com, sgarzare@redhat.com, marcandre.lureau@redhat.com, kwolf@redhat.com, hreitz@redhat.com, berrange@redhat.com, eblake@redhat.com, armbru@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org, steven.sistare@oracle.com, vsementsov@yandex-team.ru, yc-core@yandex-team.ru, d-tatianin@yandex-team.ru, jasowang@redhat.com Subject: [PATCH v2 19/25] vhost: support backend-transfer migration Date: Thu, 16 Oct 2025 14:40:56 +0300 Message-ID: <20251016114104.1384675-20-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20251016114104.1384675-1-vsementsov@yandex-team.ru> References: <20251016114104.1384675-1-vsementsov@yandex-team.ru> MIME-Version: 1.0 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=178.154.239.72; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp1a.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1760615128287158500 Content-Type: text/plain; charset="utf-8" Introduce vhost_dev.backend_transfer field, Signed-off-by: Vladimir Sementsov-Ogievskiy Acked-by: Raphael Norwitz --- hw/virtio/vhost.c | 121 +++++++++++++++++++++++++++++++++----- include/hw/virtio/vhost.h | 7 +++ 2 files changed, 113 insertions(+), 15 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 63036f8214..c46203eb9c 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1325,6 +1325,8 @@ out: return ret; } =20 +static void vhost_virtqueue_error_notifier(EventNotifier *n); + int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, struct vhost_virtqueue *vq, @@ -1350,7 +1352,13 @@ int vhost_virtqueue_start(struct vhost_dev *dev, return r; } =20 - vq->num =3D state.num =3D virtio_queue_get_num(vdev, idx); + vq->num =3D virtio_queue_get_num(vdev, idx); + + if (dev->backend_transfer) { + return 0; + } + + state.num =3D vq->num; r =3D dev->vhost_ops->vhost_set_vring_num(dev, &state); if (r) { VHOST_OPS_DEBUG(r, "vhost_set_vring_num failed"); @@ -1428,6 +1436,10 @@ static int do_vhost_virtqueue_stop(struct vhost_dev = *dev, =20 trace_vhost_virtque_stop_in(dev, vdev->name, idx); =20 + if (dev->backend_transfer) { + return 0; + } + if (virtio_queue_get_desc_addr(vdev, idx) =3D=3D 0) { /* Don't stop the virtqueue which might have not been started */ return 0; @@ -1565,10 +1577,14 @@ fail_call: =20 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq) { - event_notifier_cleanup(&vq->masked_notifier); + if (!vq->dev->backend_transfer) { + event_notifier_cleanup(&vq->masked_notifier); + } if (vq->dev->vhost_ops->vhost_set_vring_err) { event_notifier_set_handler(&vq->error_notifier, NULL); - event_notifier_cleanup(&vq->error_notifier); + if (!vq->dev->backend_transfer) { + event_notifier_cleanup(&vq->error_notifier); + } } } =20 @@ -1635,6 +1651,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, =20 hdev->vdev =3D NULL; hdev->migration_blocker =3D NULL; + hdev->_features_wait_incoming =3D true; hdev->busyloop_timeout =3D busyloop_timeout; =20 for (i =3D 0; i < hdev->nvqs; ++i) { @@ -1717,6 +1734,8 @@ int vhost_dev_connect(struct vhost_dev *hdev, Error *= *errp) goto fail; } =20 + hdev->_features_wait_incoming =3D false; + for (i =3D 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) { r =3D vhost_virtqueue_connect(hdev->vqs + i, hdev->vq_index + i); if (r < 0) { @@ -1808,8 +1827,11 @@ void vhost_dev_disable_notifiers_nvqs(struct vhost_d= ev *hdev, */ memory_region_transaction_commit(); =20 - for (i =3D 0; i < nvqs; ++i) { - virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index = + i); + if (!hdev->backend_transfer) { + for (i =3D 0; i < nvqs; ++i) { + virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), + hdev->vq_index + i); + } } virtio_device_release_ioeventfd(vdev); } @@ -1967,6 +1989,11 @@ void vhost_get_features_ex(struct vhost_dev *hdev, { const int *bit =3D feature_bits; =20 + if (hdev->_features_wait_incoming) { + /* Excessive set is enough for early initialization. */ + return; + } + while (*bit !=3D VHOST_INVALID_FEATURE_BIT) { if (!vhost_dev_has_feature_ex(hdev, *bit)) { virtio_clear_feature_ex(features, *bit); @@ -2001,6 +2028,54 @@ const VMStateDescription vmstate_backend_transfer_vh= ost_inflight =3D { } }; =20 +const VMStateDescription vmstate_vhost_virtqueue =3D { + .name =3D "vhost-virtqueue", + .fields =3D (const VMStateField[]) { + VMSTATE_EVENT_NOTIFIER(error_notifier, struct vhost_virtqueue), + VMSTATE_EVENT_NOTIFIER(masked_notifier, struct vhost_virtqueue), + VMSTATE_END_OF_LIST() + }, +}; + +static int vhost_dev_post_load(void *opaque, int version_id) +{ + struct vhost_dev *hdev =3D opaque; + Error *err =3D NULL; + int i; + + if (!check_memslots(hdev, &err)) { + error_report_err(err); + return -EINVAL; + } + + hdev->_features_wait_incoming =3D false; + + if (hdev->vhost_ops->vhost_set_vring_err) { + for (i =3D 0; i < hdev->nvqs; ++i) { + event_notifier_set_handler(&hdev->vqs[i].error_notifier, + vhost_virtqueue_error_notifier); + } + } + + + return 0; +} + +const VMStateDescription vmstate_vhost_dev =3D { + .name =3D "vhost-dev", + .post_load =3D vhost_dev_post_load, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT64(_features, struct vhost_dev), + VMSTATE_UINT64(max_queues, struct vhost_dev), + VMSTATE_UINT32_EQUAL(nvqs, struct vhost_dev, NULL), + VMSTATE_STRUCT_VARRAY_POINTER_UINT32(vqs, struct vhost_dev, + nvqs, + vmstate_vhost_virtqueue, + struct vhost_virtqueue), + VMSTATE_END_OF_LIST() + }, +}; + void vhost_ack_features_ex(struct vhost_dev *hdev, const int *feature_bits, const uint64_t *features) { @@ -2127,19 +2202,24 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIOD= evice *vdev, bool vrings) hdev->started =3D true; hdev->vdev =3D vdev; =20 - r =3D vhost_dev_set_features(hdev, hdev->log_enabled); - if (r < 0) { - goto fail_features; + if (!hdev->backend_transfer) { + r =3D vhost_dev_set_features(hdev, hdev->log_enabled); + if (r < 0) { + warn_report("%s %d", __func__, __LINE__); + goto fail_features; + } } =20 if (vhost_dev_has_iommu(hdev)) { memory_listener_register(&hdev->iommu_listener, vdev->dma_as); } =20 - r =3D hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem); - if (r < 0) { - VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed"); - goto fail_mem; + if (!hdev->backend_transfer) { + r =3D hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem); + if (r < 0) { + VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed"); + goto fail_mem; + } } for (i =3D 0; i < hdev->nvqs; ++i) { r =3D vhost_virtqueue_start(hdev, @@ -2179,13 +2259,13 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIOD= evice *vdev, bool vrings) } vhost_dev_elect_mem_logger(hdev, true); } - if (vrings) { + if (vrings && !hdev->backend_transfer) { r =3D vhost_dev_set_vring_enable(hdev, true); if (r) { goto fail_log; } } - if (hdev->vhost_ops->vhost_dev_start) { + if (hdev->vhost_ops->vhost_dev_start && !hdev->backend_transfer) { r =3D hdev->vhost_ops->vhost_dev_start(hdev, true); if (r) { goto fail_start; @@ -2207,6 +2287,8 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODev= ice *vdev, bool vrings) } vhost_start_config_intr(hdev); =20 + hdev->backend_transfer =3D false; + trace_vhost_dev_start_out(hdev, vdev->name); return 0; fail_iotlb: @@ -2262,9 +2344,18 @@ static int do_vhost_dev_stop(struct vhost_dev *hdev,= VirtIODevice *vdev, if (hdev->vhost_ops->vhost_dev_start) { hdev->vhost_ops->vhost_dev_start(hdev, false); } - if (vrings) { + if (vrings && !hdev->backend_transfer) { vhost_dev_set_vring_enable(hdev, false); } + + if (hdev->backend_transfer) { + for (i =3D 0; i < hdev->nvqs; ++i) { + struct vhost_virtqueue *vq =3D hdev->vqs + i; + + event_notifier_set_handler(&vq->error_notifier, NULL); + } + } + for (i =3D 0; i < hdev->nvqs; ++i) { rc |=3D do_vhost_virtqueue_stop(hdev, vdev, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 94a0c75fc8..55ad822848 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -105,6 +105,9 @@ struct vhost_dev { VIRTIO_DECLARE_FEATURES(_features); VIRTIO_DECLARE_FEATURES(acked_features); =20 + bool _features_wait_incoming; + bool backend_transfer; + uint32_t busyloop_timeout; uint64_t max_queues; uint64_t backend_cap; @@ -592,4 +595,8 @@ extern const VMStateDescription vmstate_backend_transfe= r_vhost_inflight; VMSTATE_STRUCT_POINTER(_field, _state, vmstate_inflight, \ struct vhost_inflight) =20 +extern const VMStateDescription vmstate_vhost_dev; +#define VMSTATE_BACKEND_TRANSFER_VHOST(_field, _state) \ + VMSTATE_STRUCT(_field, _state, 0, vmstate_vhost_dev, struct vhost_dev) + #endif --=20 2.48.1