qemu/include/system
Chenyi Qiang 5d6483edaa ram-block-attributes: Introduce RamBlockAttributes to manage RAMBlock with guest_memfd
Commit 852f0048f3 ("RAMBlock: make guest_memfd require uncoordinated
discard") highlighted that subsystems like VFIO may disable RAM block
discard. However, guest_memfd relies on discard operations for page
conversion between private and shared memory, potentially leading to
the stale IOMMU mapping issue when assigning hardware devices to
confidential VMs via shared memory. To address this and allow shared
device assignement, it is crucial to ensure the VFIO system refreshes
its IOMMU mappings.

RamDiscardManager is an existing interface (used by virtio-mem) to
adjust VFIO mappings in relation to VM page assignment. Effectively page
conversion is similar to hot-removing a page in one mode and adding it
back in the other. Therefore, similar actions are required for page
conversion events. Introduce the RamDiscardManager to guest_memfd to
facilitate this process.

Since guest_memfd is not an object, it cannot directly implement the
RamDiscardManager interface. Implementing it in HostMemoryBackend is
not appropriate because guest_memfd is per RAMBlock, and some RAMBlocks
have a memory backend while others do not. Notably, virtual BIOS
RAMBlocks using memory_region_init_ram_guest_memfd() do not have a
backend.

To manage RAMBlocks with guest_memfd, define a new object named
RamBlockAttributes to implement the RamDiscardManager interface. This
object can store the guest_memfd information such as the bitmap for
shared memory and the registered listeners for event notifications. A
new state_change() helper function is provided to notify listeners, such
as VFIO, allowing VFIO to do dynamically DMA map and unmap for the shared
memory according to conversion events. Note that in the current context
of RamDiscardManager for guest_memfd, the shared state is analogous to
being populated, while the private state can be considered discarded for
simplicity. In the future, it would be more complicated if considering
more states like private/shared/discarded at the same time.

In current implementation, memory state tracking is performed at the
host page size granularity, as the minimum conversion size can be one
page per request. Additionally, VFIO expected the DMA mapping for a
specific IOVA to be mapped and unmapped with the same granularity.
Confidential VMs may perform partial conversions, such as conversions on
small regions within a larger one. To prevent such invalid cases and
until support for DMA mapping cut operations is available, all
operations are performed with 4K granularity.

In addition, memory conversion failures cause QEMU to quit rather than
resuming the guest or retrying the operation at present. It would be
future work to add more error handling or rollback mechanisms once
conversion failures are allowed. For example, in-place conversion of
guest_memfd could retry the unmap operation during the conversion from
shared to private. For now, keep the complex error handling out of the
picture as it is not required.

Tested-by: Alexey Kardashevskiy <aik@amd.com>
Reviewed-by: Alexey Kardashevskiy <aik@amd.com>
Reviewed-by: Pankaj Gupta <pankaj.gupta@amd.com>
Signed-off-by: Chenyi Qiang <chenyi.qiang@intel.com>
Link: https://lore.kernel.org/r/20250612082747.51539-5-chenyi.qiang@intel.com
[peterx: squash fixup from Chenyi to fix builds]
Signed-off-by: Peter Xu <peterx@redhat.com>
2025-06-23 16:03:59 -04:00
..
accel-blocker.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
accel-ops.h accel: Forward-declare AccelOpsClass in 'qemu/typedefs.h' 2025-03-06 15:46:18 +01:00
address-spaces.h include/system: Move exec/address-spaces.h to system/address-spaces.h 2025-04-23 14:08:21 -07:00
arch_init.h system: Replace arch_type global by qemu_arch_available() helper 2025-03-11 20:03:26 +01:00
balloon.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
block-backend-common.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
block-backend-global-state.h block: Remove unused blk_op_is_blocked() 2025-03-11 15:49:14 +01:00
block-backend-io.h block: Improve blk_get_attached_dev_id() docstring 2025-02-06 13:59:00 +01:00
block-backend.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
block-ram-registrar.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
blockdev.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
confidential-guest-support.h meson: Only allow CONFIG_USER_ONLY from certain source sets 2025-04-23 14:08:44 -07:00
cpu-throttle.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
cpu-timers-internal.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
cpu-timers.h include/exec: Split out icount.h 2025-04-23 14:08:44 -07:00
cpus.h accel: Forward-declare AccelOpsClass in 'qemu/typedefs.h' 2025-03-06 15:46:18 +01:00
cryptodev-vhost-user.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
cryptodev-vhost.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
cryptodev.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
device_tree.h hw: Centralize handling of -machine dumpdtb option 2025-02-24 15:03:42 +00:00
dirtylimit.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
dirtyrate.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
dma.h include/system: Move exec/address-spaces.h to system/address-spaces.h 2025-04-23 14:08:21 -07:00
dump-arch.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
dump.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
event-loop-base.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
host_iommu_device.h vfio/iommufd: Save vendor specific device info 2025-06-05 10:40:38 +02:00
hostmem.h include/system: Move exec/memory.h to system/memory.h 2025-04-23 14:08:21 -07:00
hvf.h include/system/hvf: missing vaddr include 2025-05-14 15:12:40 +01:00
hvf_int.h accel/hvf: Fix TYPE_HVF_ACCEL instance size 2025-06-10 12:59:09 +02:00
hw_accel.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
iommufd.h vfio/iommufd: Add properties and handlers to TYPE_HOST_IOMMU_DEVICE_IOMMUFD 2025-06-05 10:40:38 +02:00
ioport.h include/system: Move exec/ioport.h to system/ioport.h 2025-04-23 14:08:21 -07:00
iothread.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
kvm.h i386/kvm: Prefault memory on page state change 2025-06-06 14:32:54 +02:00
kvm_int.h include/system: Move exec/memory.h to system/memory.h 2025-04-23 14:08:21 -07:00
kvm_xen.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
memory.h memory: Unify the definiton of ReplayRamPopulate() and ReplayRamDiscard() 2025-06-23 16:03:59 -04:00
memory_mapping.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
numa.h system/numa: Remove unnecessary 'exec/cpu-common.h' header 2024-12-20 17:44:57 +01:00
nvmm.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
os-posix.h os: add an ability to lock memory on_fault 2025-02-12 11:36:01 -05:00
os-wasm.h include/qemu/osdep.h: Add Emscripten-specific OS dependencies 2025-05-06 16:02:04 +02:00
os-win32.h cleanup: Drop pointless return at end of function 2025-04-24 09:33:42 +02:00
qtest.h include/system: Remove ifndef CONFIG_USER_ONLY in qtest.h 2025-04-23 14:08:44 -07:00
ram_addr.h include: Remove 'exec/exec-all.h' 2025-04-30 12:45:05 -07:00
ramblock.h ram-block-attributes: Introduce RamBlockAttributes to manage RAMBlock with guest_memfd 2025-06-23 16:03:59 -04:00
replay.h meson: Only allow CONFIG_USER_ONLY from certain source sets 2025-04-23 14:08:44 -07:00
reset.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
rng-random.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
rng.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
rtc.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
runstate-action.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
runstate.h system/runstate: add VM state change cb with return value 2025-05-14 05:39:14 -04:00
seccomp.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
spdm-socket.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
stats.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
system.h overcommit: introduce mem-lock=on-fault 2025-02-12 11:36:13 -05:00
tcg.h tcg: Move qemu_tcg_mttcg_enabled() to 'system/tcg.h' 2025-04-23 15:07:32 -07:00
tpm.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
tpm_backend.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
tpm_util.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
vhost-user-backend.h vhost-user: return failure if backend crash when live migration 2025-05-14 05:39:15 -04:00
watchdog.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
whpx.h include: Rename sysemu/ -> system/ 2024-12-20 17:44:56 +01:00
xen-mapcache.h system/xen: remove inline stubs 2025-04-23 13:52:37 -07:00
xen.h meson: Only allow CONFIG_USER_ONLY from certain source sets 2025-04-23 14:08:44 -07:00