From nobody Thu Apr 2 15:44:02 2026 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1772643218; cv=none; d=zohomail.com; s=zohoarc; b=Yw+xe0hUzjhgqycww2afrvQYQr3/h06C943+0RJazFKSU6ycdSjJBvq2T2QQAOZk4XM8s5UlKX0TxOTsR6LRBmtlcOVS4/uXYryUpu7idjSlLVdo7Vl7K0ini3ySQxKfHmx6sp/wc/N94ytofA0XE+aUy9nHSIofBIXUVJKXgss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1772643218; 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=/2K0d0yMI2FB9ttaLek/P9Lpa6KPNcXaq5k+29l39xE=; b=muDBG2xzf38R1ljYUiXpsDcMtw5tLyuHOkA9esti7QHtu00K1I/WEIc5CRoh1aBD5XmWIqzIXNVn/jLFaiEuujxVHDGlfiIh6mDY4Vn7sBo4WMhtdxdqByncgbj0M/kyKUFXMmTi/fgvSakSXhtEzSv8ESM+IiemlcM+rgDCjp8= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1772643218063594.720889992501; Wed, 4 Mar 2026 08:53:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vxpTZ-0003Ex-Ae; Wed, 04 Mar 2026 11:53:34 -0500 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 1vxpTS-00032s-SY for qemu-devel@nongnu.org; Wed, 04 Mar 2026 11:53:27 -0500 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 1vxpTR-0002kg-7d for qemu-devel@nongnu.org; Wed, 04 Mar 2026 11:53:26 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-453-_DGIH1RvP7uFLp9MmyfThg-1; Wed, 04 Mar 2026 11:53:20 -0500 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B0AA51956053; Wed, 4 Mar 2026 16:53:18 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.32.26]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D8D2F18005AE; Wed, 4 Mar 2026 16:53:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772643204; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/2K0d0yMI2FB9ttaLek/P9Lpa6KPNcXaq5k+29l39xE=; b=EuAY10shiCo1G2skKhOVPBqn29kXPIZ9EXQ5onCCqVfBRH80rFgEblDcWyb81rdnM240aB 7k/6IibCWuk3gs46EzDTx6FM98qnfkRQcKlzubPOX2D9lp7IZZPyDbokbutsJqmRMUrctO 2mr90Ed5VqI6C97ngT+WUbCoaLMVALU= X-MC-Unique: _DGIH1RvP7uFLp9MmyfThg-1 X-Mimecast-MFC-AGG-ID: _DGIH1RvP7uFLp9MmyfThg_1772643198 From: Albert Esteve To: qemu-devel@nongnu.org Cc: mst@redhat.com, Stefano Garzarella , manos.pitsidianakis@linaro.org, slp@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , stefanha@redhat.com, stevensd@chromium.org, Laurent Vivier , jasowang@redhat.com, Peter Xu , hi@alyssa.is, Pierrick Bouvier , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Fabiano Rosas , dbassey@redhat.com, Paolo Bonzini Subject: [PATCH v14 7/7] vhost-user-device: Add shared memory BAR Date: Wed, 4 Mar 2026 17:52:23 +0100 Message-ID: <20260304165223.2166175-8-aesteve@redhat.com> In-Reply-To: <20260304165223.2166175-1-aesteve@redhat.com> References: <20260304165223.2166175-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 33 X-Spam_score: 3.3 X-Spam_bar: +++ X-Spam_report: (3.3 / 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.703, RCVD_IN_VALIDITY_SAFE_BLOCKED=1.386, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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: 1772643221132154100 Content-Type: text/plain; charset="utf-8" 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(). Specifically, it uses BAR 4 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. Reviewed-by: Stefano Garzarella Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve --- hw/virtio/vhost-user-base.c | 46 ++++++++++++++++++++++++-- hw/virtio/vhost-user-test-device-pci.c | 42 +++++++++++++++++++++-- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-user-base.c b/hw/virtio/vhost-user-base.c index 01ab9ca56b..de42ea46e5 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" =20 static void vub_start(VirtIODevice *vdev) { @@ -276,7 +277,8 @@ static void vub_device_realize(DeviceState *dev, Error = **errp) { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VHostUserBase *vub =3D VHOST_USER_BASE(dev); - int ret; + uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS]; + int i, ret, nregions, regions_processed =3D 0; =20 if (!vub->chardev.chr) { error_setg(errp, "vhost-user-base: missing chardev"); @@ -319,7 +321,7 @@ static void vub_device_realize(DeviceState *dev, Error = **errp) =20 /* Allocate queues */ vub->vqs =3D g_ptr_array_sized_new(vub->num_vqs); - for (int i =3D 0; i < vub->num_vqs; i++) { + for (i =3D 0; i < vub->num_vqs; i++) { g_ptr_array_add(vub->vqs, virtio_add_queue(vdev, vub->vq_size, vub_handle_output)); @@ -333,11 +335,49 @@ static void vub_device_realize(DeviceState *dev, Erro= r **errp) VHOST_BACKEND_TYPE_USER, 0, errp); =20 if (ret < 0) { - do_vhost_user_cleanup(vdev, vub); + goto err; + } + + ret =3D vub->vhost_dev.vhost_ops->vhost_get_shmem_config(&vub->vhost_d= ev, + &nregions, + memory_sizes, + errp); + + if (ret < 0) { + goto err; + } + + for (i =3D 0; i < VIRTIO_MAX_SHMEM_REGIONS && regions_processed < nreg= ions; i++) { + if (memory_sizes[i]) { + regions_processed++; + if (vub->vhost_dev.migration_blocker =3D=3D NULL) { + error_setg(&vub->vhost_dev.migration_blocker, + "Migration disabled: devices with VIRTIO Shared Mem= ory " + "Regions do not support migration yet."); + ret =3D migrate_add_blocker_normal( + &vub->vhost_dev.migration_blocker, + errp); + + if (ret < 0) { + goto err; + } + } + + if (memory_sizes[i] % qemu_real_host_page_size() !=3D 0) { + error_setg(errp, "Shared memory %d size must be a multiple= of " + "the host page size", i); + goto err; + } + + virtio_new_shmem_region(vdev, i, memory_sizes[i]); + } } =20 qemu_chr_fe_set_handlers(&vub->chardev, NULL, NULL, vub_event, NULL, dev, NULL, true); + return; +err: + do_vhost_user_cleanup(vdev, vub); } =20 static void vub_device_unrealize(DeviceState *dev) diff --git a/hw/virtio/vhost-user-test-device-pci.c b/hw/virtio/vhost-user-= test-device-pci.c index 7f6d751690..2106557ed8 100644 --- a/hw/virtio/vhost-user-test-device-pci.c +++ b/hw/virtio/vhost-user-test-device-pci.c @@ -12,10 +12,13 @@ #include "hw/virtio/vhost-user-base.h" #include "hw/virtio/virtio-pci.h" =20 +#define VIRTIO_DEVICE_PCI_SHMEM_BAR 4 + struct VHostUserDevicePCI { VirtIOPCIProxy parent_obj; =20 VHostUserBase vub; + MemoryRegion shmembar; }; =20 #define TYPE_VHOST_USER_TEST_DEVICE_PCI "vhost-user-test-device-pci-base" @@ -25,10 +28,45 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_US= ER_TEST_DEVICE_PCI) static void vhost_user_device_pci_realize(VirtIOPCIProxy *vpci_dev, Error = **errp) { VHostUserDevicePCI *dev =3D VHOST_USER_TEST_DEVICE_PCI(vpci_dev); - DeviceState *vdev =3D DEVICE(&dev->vub); + DeviceState *dev_state =3D DEVICE(&dev->vub); + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev_state); + VirtioSharedMemory *shmem; + uint64_t offset =3D 0, shmem_size =3D 0; =20 + /* Keep modern 64-bit BARs (2 slots) away from the shared memory BAR. = */ + vpci_dev->modern_mem_bar_idx =3D 2; vpci_dev->nvectors =3D 1; - qdev_realize(vdev, BUS(&vpci_dev->bus), errp); + if (!qdev_realize(dev_state, BUS(&vpci_dev->bus), errp)) { + return; + } + + QSIMPLEQ_FOREACH(shmem, &vdev->shmem_list, entry) { + if (memory_region_size(&shmem->mr) > UINT64_MAX - shmem_size) { + error_setg(errp, "Total shared memory required overflow"); + return; + } + shmem_size =3D shmem_size + memory_region_size(&shmem->mr); + } + if (shmem_size) { + if (vpci_dev->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY) { + error_setg(errp, "modern-pio-notify is not supported due to PC= I BAR layout limitations"); + return; + } + memory_region_init(&dev->shmembar, OBJECT(vpci_dev), + "vhost-device-pci-shmembar", shmem_size); + QSIMPLEQ_FOREACH(shmem, &vdev->shmem_list, entry) { + memory_region_add_subregion(&dev->shmembar, offset, &shmem->mr= ); + virtio_pci_add_shm_cap(vpci_dev, VIRTIO_DEVICE_PCI_SHMEM_BAR, + offset, memory_region_size(&shmem->mr), + shmem->shmid); + offset =3D offset + memory_region_size(&shmem->mr); + } + pci_register_bar(&vpci_dev->pci_dev, VIRTIO_DEVICE_PCI_SHMEM_BAR, + PCI_BASE_ADDRESS_SPACE_MEMORY | + PCI_BASE_ADDRESS_MEM_PREFETCH | + PCI_BASE_ADDRESS_MEM_TYPE_64, + &dev->shmembar); + } } =20 static void vhost_user_device_pci_class_init(ObjectClass *klass, --=20 2.52.0