From nobody Sun May 19 02:27:01 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=1678729786; cv=none; d=zohomail.com; s=zohoarc; b=S6sl89EJQIyFWVQv0tMKbEGxOk6aKFAuV0xAkF8BvacwvvppAWS+kLanSJlOUr14DLrN2EQpZMnuE6f+21wezj4qWl+klzaL45ZLh1x88a+rU+Lm4wJl0PkkCAbSd+VrY9QakwiEGfVczgQrU3qSGgikpRSlbbUDQfQ5iDqlxYU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678729786; 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=JntM8S7ZJ8FixQwMO058rCya73BlLwwfTOC+MOej9zg=; b=J0MSp10XHRkuSOs8xTcocjFx8I+TWVaV4HU2i4rLqGNoR0iN3c//7MCVBUwCFcxKiAM/lL7UX/VtPiDhm5FEnD5Id4B/Qc8ClNl6zmil3MIZ4nakhb1MzzWq9Xlfq05+yF4kb7LiTe2R1xpTsFApvQwdFA5eUpXADmMV42JUw9w= 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 1678729786744296.30730631206086; Mon, 13 Mar 2023 10:49:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pbmIJ-0006IO-6L; Mon, 13 Mar 2023 13:49:11 -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 1pbmHs-0006F1-MH for qemu-devel@nongnu.org; Mon, 13 Mar 2023 13:48:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pbmHo-0000jO-TD for qemu-devel@nongnu.org; Mon, 13 Mar 2023 13:48:44 -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-661--zOCFIZlMxanGgPim3Qpyw-1; Mon, 13 Mar 2023 13:48:37 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E2FC5858F0E for ; Mon, 13 Mar 2023 17:48:36 +0000 (UTC) Received: from localhost (unknown [10.39.193.191]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5397EC164E8; Mon, 13 Mar 2023 17:48:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678729719; 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=JntM8S7ZJ8FixQwMO058rCya73BlLwwfTOC+MOej9zg=; b=JaEynZG9uQHJxoAP4yddtMlFK9HhxRXd2sz215behH2/YZjw7M8NMN2eHNTpQfK2hrZmBd 6VQREEFS1R3KTXpCVXkmdoV2eLBNpcS2TjsRPsZys9kpBSBF/xQpuVUG+2RcqLJa+biejs vOYlFT3OcQRtkZvKu/q75oPFBO1NCmc= X-MC-Unique: -zOCFIZlMxanGgPim3Qpyw-1 From: Hanna Czenczek To: qemu-devel@nongnu.org, virtio-fs@redhat.com Cc: Hanna Czenczek , "Michael S . Tsirkin" , Stefan Hajnoczi , "Dr . David Alan Gilbert" , Juan Quintela Subject: [RFC 1/2] vhost-user: Add interface for virtio-fs migration Date: Mon, 13 Mar 2023 18:48:32 +0100 Message-Id: <20230313174833.28790-2-hreitz@redhat.com> In-Reply-To: <20230313174833.28790-1-hreitz@redhat.com> References: <20230313174833.28790-1-hreitz@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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=hreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, DKIMWL_WL_HIGH=-0.001, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678729787990100001 Content-Type: text/plain; charset="utf-8" Add a virtio-fs-specific vhost-user interface to facilitate migrating back-end-internal state. We plan to migrate the internal state simply as a binary blob after the streaming phase, so all we need is a way to transfer such a blob from and to the back-end. We do so by using a dedicated area of shared memory through which the blob is transferred in chunks. This patch adds the following vhost operations (and implements them for vhost-user): - FS_SET_STATE_FD: The front-end passes a dedicated shared memory area to the back-end. This area will be used to transfer state via the other two operations. (After the transfer FS_SET_STATE_FD detaches the shared memory area again.) - FS_GET_STATE: The front-end asks the back-end to place a chunk of internal state into the shared memory area. - FS_SET_STATE: The front-end puts a chunk of internal state into the shared memory area, and asks the back-end to fetch it. On the source side, the back-end is expected to serialize its internal state either when FS_SET_STATE_FD is invoked, or when FS_GET_STATE is invoked the first time. On subsequent FS_GET_STATE calls, it memcpy()s parts of that serialized state into the shared memory area. On the destination side, the back-end is expected to collect the state blob over all FS_SET_STATE calls, and then deserialize and apply it once FS_SET_STATE_FD detaches the shared memory area. Signed-off-by: Hanna Czenczek --- include/hw/virtio/vhost-backend.h | 9 ++ include/hw/virtio/vhost.h | 68 +++++++++++++++ hw/virtio/vhost-user.c | 138 ++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 29 +++++++ 4 files changed, 244 insertions(+) diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index ec3fbae58d..fa3bd19386 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -42,6 +42,12 @@ typedef int (*vhost_backend_init)(struct vhost_dev *dev,= void *opaque, typedef int (*vhost_backend_cleanup)(struct vhost_dev *dev); typedef int (*vhost_backend_memslots_limit)(struct vhost_dev *dev); =20 +typedef ssize_t (*vhost_fs_get_state_op)(struct vhost_dev *dev, + uint64_t state_offset, size_t siz= e); +typedef int (*vhost_fs_set_state_op)(struct vhost_dev *dev, + uint64_t state_offset, size_t size); +typedef int (*vhost_fs_set_state_fd_op)(struct vhost_dev *dev, int memfd, + size_t size); typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev, struct vhost_vring_file *file); typedef int (*vhost_net_set_mtu_op)(struct vhost_dev *dev, uint16_t mtu); @@ -138,6 +144,9 @@ typedef struct VhostOps { vhost_backend_init vhost_backend_init; vhost_backend_cleanup vhost_backend_cleanup; vhost_backend_memslots_limit vhost_backend_memslots_limit; + vhost_fs_get_state_op vhost_fs_get_state; + vhost_fs_set_state_op vhost_fs_set_state; + vhost_fs_set_state_fd_op vhost_fs_set_state_fd; vhost_net_set_backend_op vhost_net_set_backend; vhost_net_set_mtu_op vhost_net_set_mtu; vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint; diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index a52f273347..b1ad9785dd 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -336,4 +336,72 @@ int vhost_dev_set_inflight(struct vhost_dev *dev, struct vhost_inflight *inflight); int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, struct vhost_inflight *inflight); + +/** + * vhost_fs_set_state_fd(): Share memory with a virtio-fs vhost + * back-end for transferring internal state for the purpose of + * migration. Calling this function again will have the back-end + * unregister (free) the previously shared memory area. + * + * @dev: The vhost device + * @memfd: File descriptor associated with the shared memory to share. + * If negative, no memory area is shared, only releasing the + * previously shared area, and announcing the end of transfer + * (which, on the destination side, should lead to the + * back-end deserializing and applying the received state). + * @size: Size of the shared memory area + * + * Returns 0 on success, and -errno on failure. + */ +int vhost_fs_set_state_fd(struct vhost_dev *dev, int memfd, size_t size); + +/** + * vhost_fs_get_state(): Request the virtio-fs vhost back-end to place + * a chunk of migration state into the shared memory area negotiated + * through vhost_fs_set_state_fd(). May only be used for migration, + * and only by the source side. + * + * The back-end-internal migration state is treated as a binary blob, + * which is transferred in chunks to fit into the shared memory area. + * + * @dev: The vhost device + * @state_offset: Offset into the state blob of the first byte to be + * transferred + * @size: Number of bytes to transfer at most; must not exceed the + * size of the shared memory area + * + * On success, returns the number of bytes that remain in the full + * state blob from the beginning of this chunk (i.e. the full size of + * the blob, minus @state_offset). When transferring the final chunk, + * this may be less than @size. The shared memory will contain the + * requested data, starting at offset 0 into the SHM, and counting + * `MIN(@size, returned value)` bytes. + * + * On failure, returns -errno. + */ +ssize_t vhost_fs_get_state(struct vhost_dev *dev, uint64_t state_offset, + uint64_t size); + +/** + * vhost_fs_set_state(): Request the virtio-fs vhost back-end to fetch + * a chunk of migration state from the shared memory area negotiated + * through vhost_fs_set_state_fd(). May only be used for migration, + * and only by the destination side. + * + * The back-end-internal migration state is treated as a binary blob, + * which is transferred in chunks to fit into the shared memory area. + * + * The front-end (i.e. the caller) must transfer the whole state to + * the back-end, without holes. + * + * @vdev: the VirtIODevice structure + * @state_offset: Offset into the state blob of the first byte to be + * transferred + * @size: Length of the chunk to transfer; must not exceed the size of + * the shared memory area + * + * Returns 0 on success, and -errno on failure. + */ +int vhost_fs_set_state(struct vhost_dev *dev, uint64_t state_offset, + uint64_t size); #endif diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index e5285df4ba..7fd1fb1ed3 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -130,6 +130,9 @@ typedef enum VhostUserRequest { VHOST_USER_REM_MEM_REG =3D 38, VHOST_USER_SET_STATUS =3D 39, VHOST_USER_GET_STATUS =3D 40, + VHOST_USER_FS_SET_STATE_FD =3D 41, + VHOST_USER_FS_GET_STATE =3D 42, + VHOST_USER_FS_SET_STATE =3D 43, VHOST_USER_MAX } VhostUserRequest; =20 @@ -210,6 +213,15 @@ typedef struct { uint32_t size; /* the following payload size */ } QEMU_PACKED VhostUserHeader; =20 +/* + * Request and reply payloads of VHOST_USER_FS_GET_STATE, and request + * payload of VHOST_USER_FS_SET_STATE. + */ +typedef struct VhostUserFsState { + uint64_t state_offset; + uint64_t size; +} VhostUserFsState; + typedef union { #define VHOST_USER_VRING_IDX_MASK (0xff) #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8) @@ -224,6 +236,7 @@ typedef union { VhostUserCryptoSession session; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserFsState fs_state; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -2240,6 +2253,128 @@ static int vhost_user_net_set_mtu(struct vhost_dev = *dev, uint16_t mtu) return 0; } =20 +static int vhost_user_fs_set_state_fd(struct vhost_dev *dev, int memfd, + size_t size) +{ + int ret; + bool reply_supported =3D virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_REPLY_= ACK); + VhostUserMsg msg =3D { + .hdr =3D { + .request =3D VHOST_USER_FS_SET_STATE_FD, + .flags =3D VHOST_USER_VERSION, + .size =3D sizeof(msg.payload.u64), + }, + .payload.u64 =3D size, + }; + + if (reply_supported) { + msg.hdr.flags |=3D VHOST_USER_NEED_REPLY_MASK; + } + + if (memfd < 0) { + assert(size =3D=3D 0); + ret =3D vhost_user_write(dev, &msg, NULL, 0); + } else { + ret =3D vhost_user_write(dev, &msg, &memfd, 1); + } + if (ret < 0) { + return ret; + } + + if (reply_supported) { + return process_message_reply(dev, &msg); + } + + return 0; +} + +static ssize_t vhost_user_fs_get_state(struct vhost_dev *dev, + uint64_t state_offset, + size_t size) +{ + int ret; + VhostUserMsg msg =3D { + .hdr =3D { + .request =3D VHOST_USER_FS_GET_STATE, + .flags =3D VHOST_USER_VERSION, + .size =3D sizeof(msg.payload.fs_state), + }, + .payload.fs_state =3D { + .state_offset =3D state_offset, + .size =3D size, + }, + }; + + ret =3D vhost_user_write(dev, &msg, NULL, 0); + if (ret < 0) { + return ret; + } + + ret =3D vhost_user_read(dev, &msg); + if (ret < 0) { + return ret; + } + + if (msg.hdr.request !=3D VHOST_USER_FS_GET_STATE) { + error_report("Received unexpected message type: " + "Expected %d, received %d", + VHOST_USER_FS_GET_STATE, msg.hdr.request); + return -EPROTO; + } + + if (msg.hdr.size !=3D sizeof(VhostUserFsState)) { + error_report("Received unexpected message length: " + "Expected %" PRIu32 ", received %zu", + msg.hdr.size, sizeof(VhostUserFsState)); + return -EPROTO; + } + + if (msg.payload.fs_state.size > SSIZE_MAX) { + error_report("Remaining state size returned by back end is too hig= h: " + "Expected up to %zd, reported %" PRIu64, + SSIZE_MAX, msg.payload.fs_state.size); + return -EPROTO; + } + + return msg.payload.fs_state.size; +} + +static int vhost_user_fs_set_state(struct vhost_dev *dev, + uint64_t state_offset, + size_t size) +{ + int ret; + bool reply_supported =3D virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_REPLY_= ACK); + VhostUserMsg msg =3D { + .hdr =3D { + .request =3D VHOST_USER_FS_SET_STATE, + .flags =3D VHOST_USER_VERSION, + .size =3D sizeof(msg.payload.fs_state), + }, + .payload.fs_state =3D { + .state_offset =3D state_offset, + .size =3D size, + }, + }; + + if (reply_supported) { + msg.hdr.flags |=3D VHOST_USER_NEED_REPLY_MASK; + } + + ret =3D vhost_user_write(dev, &msg, NULL, 0); + if (ret < 0) { + return ret; + } + + if (reply_supported) { + return process_message_reply(dev, &msg); + } + + return 0; +} + static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg) { @@ -2716,4 +2851,7 @@ const VhostOps user_ops =3D { .vhost_get_inflight_fd =3D vhost_user_get_inflight_fd, .vhost_set_inflight_fd =3D vhost_user_set_inflight_fd, .vhost_dev_start =3D vhost_user_dev_start, + .vhost_fs_set_state_fd =3D vhost_user_fs_set_state_fd, + .vhost_fs_get_state =3D vhost_user_fs_get_state, + .vhost_fs_set_state =3D vhost_user_fs_set_state, }; diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index a266396576..ef8252c90e 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -2075,3 +2075,32 @@ int vhost_net_set_backend(struct vhost_dev *hdev, =20 return -ENOSYS; } + +int vhost_fs_set_state_fd(struct vhost_dev *dev, int memfd, size_t size) +{ + if (dev->vhost_ops->vhost_fs_set_state_fd) { + return dev->vhost_ops->vhost_fs_set_state_fd(dev, memfd, size); + } + + return -ENOSYS; +} + +ssize_t vhost_fs_get_state(struct vhost_dev *dev, uint64_t state_offset, + uint64_t size) +{ + if (dev->vhost_ops->vhost_fs_get_state) { + return dev->vhost_ops->vhost_fs_get_state(dev, state_offset, size); + } + + return -ENOSYS; +} + +int vhost_fs_set_state(struct vhost_dev *dev, uint64_t state_offset, + uint64_t size) +{ + if (dev->vhost_ops->vhost_fs_set_state) { + return dev->vhost_ops->vhost_fs_set_state(dev, state_offset, size); + } + + return -ENOSYS; +} --=20 2.39.1 From nobody Sun May 19 02:27:01 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=1678729807; cv=none; d=zohomail.com; s=zohoarc; b=CP2dC3IoCGiK2UJLivzhwl8qlxCkNFXDLBRyWdi8kRuQkVLPqgvqcObHfOs/N9aG1b+VQtOYfHUb/uQleNe7T2PMAr2cdLUUw6Ufhk2/Kjxx6Sq0AydjmfxYxFsJU6RNuPDhHZQ6RadeZgGrup5gvvJKVbj5yHp3DqehpANqs6M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678729807; 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=DX0E4mn4Sb2zZT47K9+1I+S4rAPR9KAy7RT8+k73qvY=; b=V/PWwpEv4QMd+ZradwR+RaNb4t/J10Eef4VrAMj30itkZM9e1L0Yka0V7MZq7HrTV2f62uNKFC0OUmnQnbeGSiu5+Y5KpwLmktrgb1vVrzQ0YuiZ3S1FcLduTQaXguzT01kZ2k/FSPsr7XTb0+DpIG+Pz1ybtMG0chMHjqKxSMY= 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 1678729807637867.4210176908987; Mon, 13 Mar 2023 10:50:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pbmII-0006I7-U2; Mon, 13 Mar 2023 13:49:11 -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 1pbmHq-0006Ez-QD for qemu-devel@nongnu.org; Mon, 13 Mar 2023 13:48:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pbmHp-0000jW-0U for qemu-devel@nongnu.org; Mon, 13 Mar 2023 13:48:42 -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-488-3E1CVelRNDyNuhExoeIpYw-1; Mon, 13 Mar 2023 13:48:39 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A3351185A794 for ; Mon, 13 Mar 2023 17:48:38 +0000 (UTC) Received: from localhost (unknown [10.39.193.191]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 240F12027040; Mon, 13 Mar 2023 17:48:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678729720; 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=DX0E4mn4Sb2zZT47K9+1I+S4rAPR9KAy7RT8+k73qvY=; b=EC/7lyYmqDS6mazMbaZweD0GwLf1IABRIrZMxiNgJ9aw9/kf1aUx5h2O0BX0fXmAjl/8Jo cybwysxEmaTkvow7xlTQNFGX55v7ALRnYXlHKaxxFUz6ZNoY6zCy1iwuwRw3H9g826Xq8N kejkcrfNzAauomYhDZyKtGSvaOGUUoI= X-MC-Unique: 3E1CVelRNDyNuhExoeIpYw-1 From: Hanna Czenczek To: qemu-devel@nongnu.org, virtio-fs@redhat.com Cc: Hanna Czenczek , "Michael S . Tsirkin" , Stefan Hajnoczi , "Dr . David Alan Gilbert" , Juan Quintela Subject: [RFC 2/2] vhost-user-fs: Implement stateful migration Date: Mon, 13 Mar 2023 18:48:33 +0100 Message-Id: <20230313174833.28790-3-hreitz@redhat.com> In-Reply-To: <20230313174833.28790-1-hreitz@redhat.com> References: <20230313174833.28790-1-hreitz@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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=hreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, DKIMWL_WL_HIGH=-0.001, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678729809238100003 Content-Type: text/plain; charset="utf-8" A virtio-fs device's VM state consists of: - the virtio device (vring) state (VMSTATE_VIRTIO_DEVICE) - the back-end's (virtiofsd's) internal state We get/set the latter via the new vhost-user operations FS_SET_STATE_FD, FS_GET_STATE, and FS_SET_STATE. Signed-off-by: Hanna Czenczek --- hw/virtio/vhost-user-fs.c | 171 +++++++++++++++++++++++++++++++++++++- 1 file changed, 170 insertions(+), 1 deletion(-) diff --git a/hw/virtio/vhost-user-fs.c b/hw/virtio/vhost-user-fs.c index 83fc20e49e..df1fb02acc 100644 --- a/hw/virtio/vhost-user-fs.c +++ b/hw/virtio/vhost-user-fs.c @@ -20,8 +20,10 @@ #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" #include "qemu/error-report.h" +#include "qemu/memfd.h" #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-user-fs.h" +#include "migration/qemu-file-types.h" #include "monitor/monitor.h" #include "sysemu/sysemu.h" =20 @@ -298,9 +300,176 @@ static struct vhost_dev *vuf_get_vhost(VirtIODevice *= vdev) return &fs->vhost_dev; } =20 +/** + * Fetch the internal state from the back-end (virtiofsd) and save it + * to `f`. + */ +static int vuf_save_state(QEMUFile *f, void *pv, size_t size, + const VMStateField *field, JSONWriter *vmdesc) +{ + VirtIODevice *vdev =3D pv; + VHostUserFS *fs =3D VHOST_USER_FS(vdev); + int memfd =3D -1; + /* Size of the shared memory through which to transfer the state */ + const size_t chunk_size =3D 4 * 1024 * 1024; + size_t state_offset; + ssize_t remaining; + void *shm_buf; + Error *local_err =3D NULL; + int ret, ret2; + + /* Set up shared memory through which to receive the state from virtio= fsd */ + shm_buf =3D qemu_memfd_alloc("vhost-fs-state", chunk_size, + F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW, + &memfd, &local_err); + if (!shm_buf) { + error_report_err(local_err); + ret =3D -ENOMEM; + goto early_fail; + } + + /* Share the SHM area with virtiofsd */ + ret =3D vhost_fs_set_state_fd(&fs->vhost_dev, memfd, chunk_size); + if (ret < 0) { + goto early_fail; + } + + /* Receive the virtiofsd state in chunks, and write them to `f` */ + state_offset =3D 0; + do { + size_t this_chunk_size; + + remaining =3D vhost_fs_get_state(&fs->vhost_dev, state_offset, + chunk_size); + if (remaining < 0) { + ret =3D remaining; + goto fail; + } + + /* Prefix the whole state by its total length */ + if (state_offset =3D=3D 0) { + qemu_put_be64(f, remaining); + } + + this_chunk_size =3D MIN(remaining, chunk_size); + qemu_put_buffer(f, shm_buf, this_chunk_size); + state_offset +=3D this_chunk_size; + } while (remaining >=3D chunk_size); + + ret =3D 0; +fail: + /* Have virtiofsd close the shared memory */ + ret2 =3D vhost_fs_set_state_fd(&fs->vhost_dev, -1, 0); + if (ret2 < 0) { + error_report("Failed to remove state FD from the vhost-user-fs bac= k " + "end: %s", strerror(-ret)); + if (ret =3D=3D 0) { + ret =3D ret2; + } + } + +early_fail: + if (shm_buf) { + qemu_memfd_free(shm_buf, chunk_size, memfd); + } + + return ret; +} + +/** + * Load the back-end's (virtiofsd's) internal state from `f` and send + * it over to that back-end. + */ +static int vuf_load_state(QEMUFile *f, void *pv, size_t size, + const VMStateField *field) +{ + VirtIODevice *vdev =3D pv; + VHostUserFS *fs =3D VHOST_USER_FS(vdev); + int memfd =3D -1; + /* Size of the shared memory through which to transfer the state */ + const size_t chunk_size =3D 4 * 1024 * 1024; + size_t state_offset; + uint64_t remaining; + void *shm_buf; + Error *local_err =3D NULL; + int ret, ret2; + + /* The state is prefixed by its total length, read that first */ + remaining =3D qemu_get_be64(f); + + /* Set up shared memory through which to send the state to virtiofsd */ + shm_buf =3D qemu_memfd_alloc("vhost-fs-state", chunk_size, + F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW, + &memfd, &local_err); + if (!shm_buf) { + error_report_err(local_err); + ret =3D -ENOMEM; + goto early_fail; + } + + /* Share the SHM area with virtiofsd */ + ret =3D vhost_fs_set_state_fd(&fs->vhost_dev, memfd, chunk_size); + if (ret < 0) { + goto early_fail; + } + + /* + * Read the virtiofsd state in chunks from `f`, and send them over + * to virtiofsd + */ + state_offset =3D 0; + do { + size_t this_chunk_size =3D MIN(remaining, chunk_size); + + if (qemu_get_buffer(f, shm_buf, this_chunk_size) < this_chunk_size= ) { + ret =3D -EINVAL; + goto fail; + } + + ret =3D vhost_fs_set_state(&fs->vhost_dev, state_offset, this_chun= k_size); + if (ret < 0) { + goto fail; + } + + state_offset +=3D this_chunk_size; + remaining -=3D this_chunk_size; + } while (remaining > 0); + + ret =3D 0; +fail: + ret2 =3D vhost_fs_set_state_fd(&fs->vhost_dev, -1, 0); + if (ret2 < 0) { + error_report("Failed to remove state FD from the vhost-user-fs bac= k " + "end -- perhaps it failed to deserialize/apply the st= ate: " + "%s", strerror(-ret2)); + if (ret =3D=3D 0) { + ret =3D ret2; + } + } + +early_fail: + if (shm_buf) { + qemu_memfd_free(shm_buf, chunk_size, memfd); + } + + return ret; +} + static const VMStateDescription vuf_vmstate =3D { .name =3D "vhost-user-fs", - .unmigratable =3D 1, + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_VIRTIO_DEVICE, + { + .name =3D "back-end", + .info =3D &(const VMStateInfo) { + .name =3D "virtio-fs back-end state", + .get =3D vuf_load_state, + .put =3D vuf_save_state, + }, + }, + VMSTATE_END_OF_LIST() + }, }; =20 static Property vuf_properties[] =3D { --=20 2.39.1