From: Stefan Hajnoczi <stefanha@redhat.com>
To: Albert Esteve <aesteve@redhat.com>
Cc: qemu-devel@nongnu.org, stevensd@chromium.org, dbassey@redhat.com,
"Laurent Vivier" <lvivier@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
mst@redhat.com, slp@redhat.com, hi@alyssa.is,
"Fabiano Rosas" <farosas@suse.de>,
"Stefano Garzarella" <sgarzare@redhat.com>,
jasowang@redhat.com, david@redhat.com,
"Alex Bennée" <alex.bennee@linaro.org>
Subject: Re: [PATCH v8 7/7] vhost-user-device: Add shared memory BAR
Date: Tue, 14 Oct 2025 11:18:02 -0400 [thread overview]
Message-ID: <20251014151802.GA11115@fedora> (raw)
In-Reply-To: <CADSE00KBfyW=bTaNjvr8xgTevzndsk=Rps6gvfE8yH4sYqHqgA@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 6218 bytes --]
On Mon, Oct 13, 2025 at 05:37:52PM +0200, Albert Esteve wrote:
> On Mon, Sep 29, 2025 at 9:00 PM Stefan Hajnoczi <stefanha@redhat.com> wrote:
> >
> > On Wed, Sep 10, 2025 at 01:54:20PM +0200, Albert Esteve wrote:
> > > Add shared memory BAR support to vhost-user-device-pci
> > > to enable direct file mapping for VIRTIO Shared
> > > Memory Regions.
> > >
> > > The implementation creates a consolidated shared
> > > memory BAR that contains all VIRTIO Shared
> > > Memory Regions as subregions. Each region is
> > > configured with its proper shmid, size, and
> > > offset within the BAR. The number and size of
> > > regions are retrieved via VHOST_USER_GET_SHMEM_CONFIG
> > > message sent by vhost-user-base during realization
> > > after virtio_init().
> > >
> > > Specifiically, it uses BAR 3 to avoid conflicts, as
> > > it is currently unused.
> > >
> > > The shared memory BAR is only created when the
> > > backend supports VHOST_USER_PROTOCOL_F_SHMEM and
> > > has configured shared memory regions. This maintains
> > > backward compatibility with backends that do not
> > > support shared memory functionality.
> > >
> > > Signed-off-by: Albert Esteve <aesteve@redhat.com>
> > > ---
> > > hw/virtio/vhost-user-base.c | 47 +++++++++++++++++++++++++++++--
> > > hw/virtio/vhost-user-device-pci.c | 34 ++++++++++++++++++++--
> > > 2 files changed, 76 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/hw/virtio/vhost-user-base.c b/hw/virtio/vhost-user-base.c
> > > index ff67a020b4..82f49500e4 100644
> > > --- a/hw/virtio/vhost-user-base.c
> > > +++ b/hw/virtio/vhost-user-base.c
> > > @@ -16,6 +16,7 @@
> > > #include "hw/virtio/virtio-bus.h"
> > > #include "hw/virtio/vhost-user-base.h"
> > > #include "qemu/error-report.h"
> > > +#include "migration/blocker.h"
> > >
> > > static void vub_start(VirtIODevice *vdev)
> > > {
> > > @@ -276,7 +277,9 @@ static void vub_device_realize(DeviceState *dev, Error **errp)
> > > {
> > > VirtIODevice *vdev = VIRTIO_DEVICE(dev);
> > > VHostUserBase *vub = VHOST_USER_BASE(dev);
> > > - int ret;
> > > + uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS];
> > > + g_autofree char *name = NULL;
> > > + int i, ret, nregions, regions_processed = 0;
> > >
> > > if (!vub->chardev.chr) {
> > > error_setg(errp, "vhost-user-base: missing chardev");
> > > @@ -319,7 +322,7 @@ static void vub_device_realize(DeviceState *dev, Error **errp)
> > >
> > > /* Allocate queues */
> > > vub->vqs = g_ptr_array_sized_new(vub->num_vqs);
> > > - for (int i = 0; i < vub->num_vqs; i++) {
> > > + for (i = 0; i < vub->num_vqs; i++) {
> > > g_ptr_array_add(vub->vqs,
> > > virtio_add_queue(vdev, vub->vq_size,
> > > vub_handle_output));
> > > @@ -333,11 +336,49 @@ static void vub_device_realize(DeviceState *dev, Error **errp)
> > > VHOST_BACKEND_TYPE_USER, 0, errp);
> > >
> > > if (ret < 0) {
> > > - do_vhost_user_cleanup(vdev, vub);
> > > + goto err;
> > > + }
> > > +
> > > + ret = vub->vhost_dev.vhost_ops->vhost_get_shmem_config(&vub->vhost_dev,
> > > + &nregions,
> > > + memory_sizes,
> > > + errp);
> > > +
> > > + if (ret < 0) {
> > > + goto err;
> > > + }
> > > +
> > > + for (i = 0; i < VIRTIO_MAX_SHMEM_REGIONS && regions_processed < nregions; i++) {
> > > + if (memory_sizes[i]) {
> > > + regions_processed++;
> > > + if (vub->vhost_dev.migration_blocker == NULL) {
> > > + error_setg(&vub->vhost_dev.migration_blocker,
> > > + "Migration disabled: devices with VIRTIO Shared Memory "
> > > + "Regions do not support migration yet.");
> > > + ret = migrate_add_blocker_normal(
> > > + &vub->vhost_dev.migration_blocker,
> > > + errp);
> > > +
> > > + if (ret < 0) {
> > > + goto err;
> > > + }
> > > + }
> > > +
> > > + if (memory_sizes[i] % qemu_real_host_page_size() != 0) {
> > > + error_setg(errp, "Shared memory %d size must be a power of 2 "
> > > + "no smaller than the page size", i);
> > > + goto err;
> > > + }
> > > +
> > > + virtio_new_shmem_region(vdev, i, memory_sizes[i]);
> > > + }
> > > }
> > >
> > > qemu_chr_fe_set_handlers(&vub->chardev, NULL, NULL, vub_event, NULL,
> > > dev, NULL, true);
> > > + return;
> > > +err:
> > > + do_vhost_user_cleanup(vdev, vub);
> > > }
> > >
> > > static void vub_device_unrealize(DeviceState *dev)
> > > diff --git a/hw/virtio/vhost-user-device-pci.c b/hw/virtio/vhost-user-device-pci.c
> > > index f10bac874e..bac99e7c60 100644
> > > --- a/hw/virtio/vhost-user-device-pci.c
> > > +++ b/hw/virtio/vhost-user-device-pci.c
> > > @@ -8,14 +8,18 @@
> > > */
> > >
> > > #include "qemu/osdep.h"
> > > +#include "qapi/error.h"
> > > #include "hw/qdev-properties.h"
> > > #include "hw/virtio/vhost-user-base.h"
> > > #include "hw/virtio/virtio-pci.h"
> > >
> > > +#define VIRTIO_DEVICE_PCI_SHMEM_BAR 3
> >
> > 64-bit PCI memory BARs require two contiguous 32-bit BARs. Choosing BAR
> > 3 won't work because modern_mem_bar_idx = 4 (and 5) and there is a
> > collision.
> >
> > virtio-gpu-hostmem uses BAR 2 and 3 for VIRTIO Shared Memory Regions. In
> > order to do this, it rearranges the other VIRTIO PCI BARs in
> > virtio_vga_base_realize(). Following that approach should be safe.
>
> I see. But doing the same strategy would collide with
> virtio-gpu-hostmem if both devices are used simultaneously? Would that
> be ok?
virtio-gpu-* and vhost-user-device-pci are separate PCI devices with
their own BARs, how would they conflict?
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2025-10-14 15:18 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-10 11:54 [PATCH v8 0/7] vhost-user: Add SHMEM_MAP/UNMAP requests Albert Esteve
2025-09-10 11:54 ` [PATCH v8 1/7] vhost-user: Add VirtIO Shared Memory map request Albert Esteve
2025-09-29 18:39 ` Stefan Hajnoczi
2025-10-14 13:52 ` Albert Esteve
2025-10-14 15:50 ` Stefan Hajnoczi
2025-10-15 6:51 ` Albert Esteve
2025-09-29 19:15 ` Stefan Hajnoczi
2025-09-10 11:54 ` [PATCH v8 2/7] vhost_user.rst: Align VhostUserMsg excerpt members Albert Esteve
2025-09-10 11:54 ` [PATCH v8 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec Albert Esteve
2025-09-29 18:48 ` Stefan Hajnoczi
2025-09-10 11:54 ` [PATCH v8 4/7] vhost_user: Add frontend get_shmem_config command Albert Esteve
2025-09-29 18:50 ` Stefan Hajnoczi
2025-09-10 11:54 ` [PATCH v8 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message Albert Esteve
2025-09-10 11:54 ` [PATCH v8 6/7] qmp: add shmem feature map Albert Esteve
2025-09-10 11:54 ` [PATCH v8 7/7] vhost-user-device: Add shared memory BAR Albert Esteve
2025-09-29 19:00 ` Stefan Hajnoczi
2025-10-13 15:37 ` Albert Esteve
2025-10-14 15:18 ` Stefan Hajnoczi [this message]
2025-10-15 6:46 ` Albert Esteve
2025-09-26 8:28 ` [PATCH v8 0/7] vhost-user: Add SHMEM_MAP/UNMAP requests Albert Esteve
2025-09-29 19:15 ` Stefan Hajnoczi
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=20251014151802.GA11115@fedora \
--to=stefanha@redhat.com \
--cc=aesteve@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=david@redhat.com \
--cc=dbassey@redhat.com \
--cc=farosas@suse.de \
--cc=hi@alyssa.is \
--cc=jasowang@redhat.com \
--cc=lvivier@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
--cc=slp@redhat.com \
--cc=stevensd@chromium.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).