From: Hanna Czenczek <hreitz@redhat.com>
To: Connor Kite <connorkite@gmail.com>, qemu-devel@nongnu.org
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Viresh Kumar" <viresh.kumar@linaro.org>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Mathieu Poirier" <mathieu.poirier@linaro.org>,
"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
"Haixu Cui" <quic_haixcui@quicinc.com>,
"Raphael Norwitz" <rnorwitz@nvidia.com>,
"Kevin Wolf" <kwolf@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Fam Zheng" <fam@euphon.net>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Milan Zamazal" <mzamazal@redhat.com>,
"Akihiko Odaki" <odaki@rsg.ci.i.u-tokyo.ac.jp>,
"Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
qemu-block@nongnu.org, virtio-fs@lists.linux.dev,
"Gonglei (Arei)" <arei.gonglei@huawei.com>,
"zhenwei pi" <zhenwei.pi@linux.dev>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Jason Wang" <jasowangio@gmail.com>,
"Peter Xu" <peterx@redhat.com>,
"Eugenio Pérez" <eperezma@redhat.com>,
"Alyssa Ross" <hi@alyssa.is>,
"Demi Marie Obenour" <demiobenour@gmail.com>
Subject: Re: [PATCH RFC 11/15] hw/virtio/vhost-user: create isolation region
Date: Mon, 3 Aug 2026 15:25:19 +0200 [thread overview]
Message-ID: <b6bc052c-1bf5-449b-85d0-e1c4624a5d99@redhat.com> (raw)
In-Reply-To: <20260723-vhost-user-isolated-memory-v1-11-6b97c439eb28@gmail.com>
On 24.07.26 00:30, Connor Kite wrote:
> If memory isolation mode is active for the vhost-user device adds
> features to:
> - Gather the size required for bounce buffers and vrings in shared
> isolation region
> - Allocate the required space in an anonymous file
> - Create a vhost-iova-tree with space to map entire isolation region
> - Map guest memory regions and shared vrings into the tree
> - Release these resources upon backend cleanup
>
> Signed-off-by: Connor Kite <connorkite@gmail.com>
> ---
> hw/virtio/vhost-user.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 129 insertions(+)
>
> diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
> index f296b63fb9..710cf966f8 100644
> --- a/hw/virtio/vhost-user.c
> +++ b/hw/virtio/vhost-user.c
> @@ -18,6 +18,7 @@
> #include "hw/virtio/vhost-backend.h"
> #include "hw/virtio/virtio.h"
> #include "hw/virtio/virtio-net.h"
> +#include "hw/virtio/vhost-iova-tree.h"
> #include "chardev/char-fe.h"
> #include "io/channel-socket.h"
> #include "system/kvm.h"
> @@ -25,6 +26,7 @@
> #include "qemu/main-loop.h"
> #include "qemu/uuid.h"
> #include "qemu/sockets.h"
> +#include "qemu/memfd.h"
> #include "system/runstate.h"
> #include "system/cryptodev.h"
> #include "migration/postcopy-ram.h"
> @@ -320,6 +322,13 @@ static VhostUserMsg m __attribute__ ((unused));
> /* The version of the protocol we support */
> #define VHOST_USER_VERSION (0x1)
>
> +typedef struct IsolationRegion {
> + uint64_t base_addr;
> + uint64_t vring_base_addr;
> + uint64_t size;
> + int iso_fd;
> +} IsolationRegion;
> +
Going beyond Stefan, I would say please document each field with a
proper doc comment, and ideally the whole struct, too.
> struct vhost_user {
> struct vhost_dev *dev;
> /* Shared between vhost devs of the same virtio device */
> @@ -353,6 +362,10 @@ struct vhost_user {
> * by the backend (see @features).
> */
> uint64_t protocol_features;
> +
> + /* Isolated memory data*/
In general: Please put spaces after the leading /* and before the
trailing */.
> + struct IsolationRegion iso_memory;
> + VhostIOVATree *iso_iova_tree;
Patch 13 adds more variables to this, so I think it may make sense to
have a dedicated struct for these fields.
> };
>
> struct scrub_regions {
> @@ -1109,6 +1122,121 @@ static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
> return 0;
> }
>
> +/* TODO: Is there any notifier cleanup required here?*/
> +static void cleanup_isolation_regions(struct vhost_dev *dev)
> +{
> + struct vhost_user *u = dev->opaque;
> + if (u->iso_memory.base_addr) {
> + vhost_iova_tree_delete(u->iso_iova_tree);
> + u->iso_iova_tree = NULL;
> + memset(&u->iso_memory, 0, sizeof(IsolationRegion));
> + qemu_memfd_free((gpointer) u->iso_memory.base_addr, u->iso_memory.size,
> + u->iso_memory.iso_fd);
As Akihiko said, the memset() should come after this, and also...
> + u->iso_memory.base_addr = 0;
...this is just a subset of the memset().
> + }
> +}
> +
> +__attribute__((unused))
> +static int init_isolation_regions(struct vhost_dev *dev,
> + VhostUserMsg *msg,
> + int *fds, size_t *fd_num)
> +{
> + Error *err = NULL;
> + struct vhost_user *u = dev->opaque;
> + uint32_t nregions = dev->mem->nregions;
> + uint64_t buffer_reg_size = 0;
> + DMAMap newEntry = {
> + .perm = IOMMU_RW
> + };
> + g_autoptr(GArray) buffer_regions =
> + g_array_new(FALSE, TRUE, sizeof(DMAMap));
> +
> + msg->hdr.request = VHOST_USER_SET_MEM_TABLE;
> +
> + /* In case of reset, clear old regions*/
> + if (u->iso_memory.base_addr != 0) {
> + cleanup_isolation_regions(dev);
> + vhost_iova_tree_delete(u->iso_iova_tree);
> + }
(What Akihiko said)
> +
> + /* Gather information for bounce buffers to be mapped */
> + for (int i = 0; i < nregions; i++) {
> + struct vhost_memory_region *dev_region = &dev->mem->regions[i];
> + hwaddr size = ROUND_UP(dev_region->memory_size,
> + qemu_real_host_page_size());
> + newEntry.translated_addr = dev_region->guest_phys_addr;
> + newEntry.size = size - 1;
> + buffer_reg_size += size;
> + g_array_append_val(buffer_regions, newEntry);
> + }
> +
> + int num;
> + size_t desc_size;
> + size_t avail_size;
> + size_t driver_area_size;
> + size_t device_area_size;
> + size_t total_vring_size = 0;
> + size_t total_mmap_size;
Please do not mix variable declarations and normal code (style.rst calls
it “Mixed declarations”).
> +
> + /* Get space required for all vrings */
> + for (int j = 0; j < dev->nvqs; j++) {
Why j here and i above and below?
> + num = virtio_queue_get_num(dev->vdev, dev->vq_index + j);
> + desc_size = sizeof(vring_desc_t) * num;
> + avail_size = offsetof(vring_avail_t, ring[num]) +
> + sizeof(uint16_t);
> + driver_area_size = ROUND_UP(desc_size + avail_size,
> + qemu_real_host_page_size());
> + device_area_size = ROUND_UP(offsetof(vring_used_t, ring[num]) +
> + sizeof(uint16_t),
> + qemu_real_host_page_size());
> + total_vring_size += driver_area_size + device_area_size;
> + }
> +
> + total_mmap_size = buffer_reg_size + total_vring_size;
> +
> + /* Allocate and map an anonymous file to hold the isolation region */
> + u->iso_memory.base_addr = (uint64_t) qemu_memfd_alloc("iso_r",
> + total_mmap_size,
> + F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
> + &u->iso_memory.iso_fd, &err);
> + u->iso_memory.size = total_mmap_size;
> +
> + if (err) {
> + error_report_err(err);
> + cleanup_isolation_regions(dev);
> + return -1;
> + }
> +
> + uint64_t last_addr = int128_get64(int128_add(u->iso_memory.base_addr,
> + total_mmap_size - 1));
I would like a comment why you went for a 128-bit operation here. I
assume it is because it checks for overflow, turning overflow into an
assertion failure, and it can be assumed `qemu_memfd_alloc()` must
naturally return a pointer such that adding the length of the allocated
area to it (minus one) will never overflow?
The question is, do we even need to check for overflow then. Not that I
mind it, in principle, I just find it non-obvious. (The more obvious
check (imho) would be to just do a 64-bit operation and then
assert(last_addr >= u->iso_memory.base_addr).)
> +
> + /*
> + * Instantiates iova tree sized to map bounce buffers and vrings to the
> + * isolation region in host va.
> + */
> + u->iso_iova_tree = vhost_iova_tree_new(u->iso_memory.base_addr, last_addr);
> +
> + assert(&u->iso_memory.iso_fd >= 0);
> + DMAMap *map;
> + DMAMap vring_map = {
> + .perm = IOMMU_RW,
> + .size = total_vring_size - 1,
> + /*vrings are allocated on tree first, so will be assigned base addr*/
> + .translated_addr = u->iso_memory.base_addr
> + };
> +
> + vhost_iova_tree_map_alloc(u->iso_iova_tree, &vring_map,
> + vring_map.translated_addr);
> + u->iso_memory.vring_base_addr = vring_map.iova;
> + for (int i = 0; i < buffer_regions->len; i++) {
> + map = &g_array_index(buffer_regions, DMAMap, i);
God, I *really*, *really* hate this, and find it really disgusting that
the documentation actually recommends doing this (`&g_array_index()`)
instead of just offering a separate macro to get a reference.
And existing qemu code does it all over the place, too.
So I cannot really fault you for it.
Still. Too ugly for me to keep completely silent about it.
</rant>
Hanna
> + vhost_iova_tree_map_alloc_gpa(u->iso_iova_tree, map,
> + map->translated_addr);
> + }
> +
> + return 0;
> +}
> +
> static int vhost_user_set_mem_table(struct vhost_dev *dev,
> struct vhost_memory *mem)
> {
> @@ -2681,6 +2809,7 @@ static int vhost_user_backend_cleanup(struct vhost_dev *dev)
> g_free(u->region_rb_offset);
> u->region_rb_offset = NULL;
> u->region_rb_len = 0;
> + cleanup_isolation_regions(dev);
> g_free(u);
> dev->opaque = 0;
>
>
next prev parent reply other threads:[~2026-08-03 13:25 UTC|newest]
Thread overview: 96+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 22:29 [PATCH RFC 00/15] vhost-user: isolated memory ConKite
2026-07-23 22:30 ` [PATCH RFC 01/15] vhost-user: Consolidate chardev property definitions ConKite
2026-07-24 6:09 ` Markus Armbruster
2026-07-23 22:30 ` [PATCH RFC 02/15] vhost-user: Add memory-isolation qdev property to vhost-user devices ConKite
2026-07-24 10:56 ` Akihiko Odaki
2026-07-28 18:30 ` Connor Kite
2026-08-03 12:21 ` Hanna Czenczek
2026-08-03 20:32 ` Connor Kite
2026-08-04 10:46 ` Hanna Czenczek
2026-08-06 4:28 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 03/15] backends/cryptodev-vhost-user: add memory isolation bool Connor Kite
2026-07-24 6:06 ` Markus Armbruster
2026-07-27 18:43 ` Stefan Hajnoczi
2026-07-28 5:26 ` Connor Kite
2026-07-24 11:03 ` Akihiko Odaki
2026-07-23 22:30 ` [PATCH RFC 04/15] net/vhost-user: add memory isolation Connor Kite
2026-07-27 18:48 ` Stefan Hajnoczi
2026-08-03 12:27 ` Hanna Czenczek
2026-08-05 23:43 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 05/15] vhost-user: add memory_isolation to VhostUserState Connor Kite
2026-07-24 11:09 ` Akihiko Odaki
2026-07-27 19:06 ` Stefan Hajnoczi
2026-07-30 21:28 ` Connor Kite
2026-08-03 12:29 ` Hanna Czenczek
2026-08-05 23:45 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 06/15] util/iova-tree: g_tree_foreach wrapper Connor Kite
2026-07-27 19:07 ` Stefan Hajnoczi
2026-07-28 18:24 ` Connor Kite
2026-08-03 12:36 ` Hanna Czenczek
2026-08-03 21:13 ` Connor Kite
2026-08-04 11:04 ` Hanna Czenczek
2026-07-23 22:30 ` [PATCH RFC 07/15] hw/virtio: iova_tree_foreach wrapper Connor Kite
2026-07-24 11:14 ` Akihiko Odaki
2026-07-31 0:51 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 08/15] hw/virtio/vhost-shadow-virtqueue: used handler Connor Kite
2026-07-24 11:29 ` Akihiko Odaki
2026-07-28 15:06 ` Stefan Hajnoczi
2026-08-03 12:41 ` Hanna Czenczek
2026-08-06 2:50 ` Connor Kite
2026-08-06 10:28 ` Hanna Czenczek
2026-08-06 15:05 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 09/15] hw/virtio/vhost-shadow-virtqueue: specified vring placement Connor Kite
2026-07-24 12:19 ` Akihiko Odaki
2026-08-06 15:29 ` Connor Kite
2026-08-06 15:56 ` Akihiko Odaki
2026-07-28 15:23 ` Stefan Hajnoczi
2026-08-07 19:49 ` Connor Kite
2026-08-03 12:48 ` Hanna Czenczek
2026-08-07 19:49 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 10/15] hw/virtio/vhost-shadow-virtqueue: range boundary in translation Connor Kite
2026-07-24 12:31 ` Akihiko Odaki
2026-07-28 15:34 ` Stefan Hajnoczi
2026-08-03 12:56 ` Hanna Czenczek
2026-08-08 5:52 ` Connor Kite
2026-08-11 11:04 ` Hanna Czenczek
2026-07-23 22:30 ` [PATCH RFC 11/15] hw/virtio/vhost-user: create isolation region Connor Kite
2026-07-24 12:53 ` Akihiko Odaki
2026-08-03 1:28 ` Connor Kite
2026-07-28 17:59 ` Stefan Hajnoczi
2026-08-03 18:26 ` Connor Kite
2026-08-04 18:16 ` Stefan Hajnoczi
2026-08-09 1:12 ` Connor Kite
2026-08-03 13:25 ` Hanna Czenczek [this message]
2026-08-03 19:31 ` Connor Kite
2026-08-04 11:03 ` Hanna Czenczek
2026-08-09 0:36 ` Connor Kite
2026-08-04 18:23 ` Stefan Hajnoczi
2026-07-23 22:30 ` [PATCH RFC 12/15] hw/virtio/vhost-user: send isolation regions to device Connor Kite
2026-07-24 13:33 ` Akihiko Odaki
2026-08-10 17:40 ` Connor Kite
2026-08-10 21:12 ` Connor Kite
2026-07-28 19:16 ` Stefan Hajnoczi
2026-08-10 18:34 ` Connor Kite
2026-08-03 13:46 ` Hanna Czenczek
2026-08-10 22:16 ` Connor Kite
2026-07-23 22:30 ` [PATCH RFC 13/15] hw/virtio/vhost-user: add shadow virtqueues and eventfd intercepts Connor Kite
2026-07-24 13:45 ` Akihiko Odaki
2026-08-10 23:54 ` Connor Kite
2026-07-28 19:41 ` Stefan Hajnoczi
2026-08-11 1:39 ` Connor Kite
2026-08-03 13:52 ` Hanna Czenczek
2026-08-11 2:32 ` Connor Kite
2026-08-11 11:42 ` Hanna Czenczek
2026-07-23 22:30 ` [PATCH RFC 14/15] hw/virtio/vhost-user: handle data movement with shadow vqs Connor Kite
2026-07-24 15:20 ` Akihiko Odaki
2026-08-11 6:28 ` Connor Kite
2026-07-28 20:57 ` Stefan Hajnoczi
2026-08-11 6:28 ` Connor Kite
2026-08-03 14:10 ` Hanna Czenczek
2026-07-23 22:30 ` [PATCH RFC 15/15] hw/virtio/vhost-user: shadow vq cleanup Connor Kite
2026-07-24 15:22 ` Akihiko Odaki
2026-07-25 0:15 ` [PATCH RFC 00/15] vhost-user: isolated memory Demi Marie Obenour
2026-07-25 3:45 ` Akihiko Odaki
2026-07-27 18:23 ` Stefan Hajnoczi
2026-07-28 7:07 ` Demi Marie Obenour
2026-07-28 14:50 ` Connor Kite
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b6bc052c-1bf5-449b-85d0-e1c4624a5d99@redhat.com \
--to=hreitz@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=arei.gonglei@huawei.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=connorkite@gmail.com \
--cc=demiobenour@gmail.com \
--cc=dmitry.osipenko@collabora.com \
--cc=eblake@redhat.com \
--cc=eperezma@redhat.com \
--cc=fam@euphon.net \
--cc=hi@alyssa.is \
--cc=jasowangio@gmail.com \
--cc=kraxel@redhat.com \
--cc=kwolf@redhat.com \
--cc=manos.pitsidianakis@linaro.org \
--cc=marcandre.lureau@redhat.com \
--cc=mathieu.poirier@linaro.org \
--cc=mst@redhat.com \
--cc=mzamazal@redhat.com \
--cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=quic_haixcui@quicinc.com \
--cc=rnorwitz@nvidia.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=viresh.kumar@linaro.org \
--cc=virtio-fs@lists.linux.dev \
--cc=zhenwei.pi@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.