vfio queue
Subsystems (2)
The following changes since commit cf3e71d8fc8ba681266759bb6cb2e45a45983e3e:
Merge tag 'single-binary-20260112' of https://github.com/philmd/qemu into staging (2026-01-13 11:51:18 +1100)
are available in the Git repository at:
https://github.com/legoater/qemu/ tags/pull-vfio-20260113
for you to fetch changes up to 7a05be8c70bb789c23076b1ca2563ed7d87c6fb8:
tests/rcutorture: Fix build error (2026-01-13 08:29:59 +0100)
----------------------------------------------------------------
vfio queue:
* Resolves build errors with gcc 16
* Adjusts the Linux headers for s390x and mshv
* Fixes endianness issue in the VFIO helper functions
* Adds support for live migration with vIOMMU when using IOMMU dirty tracking
* Implements a migration blocker to prevent failures when VM memory is too large
* Corrects an unmap_bitmap failure in the legacy VFIO backend
* Addresses a workaround for an Intel IOMMU errata.
* Implements Intel IOMMU first stage translation for passthrough device. Also a prerequisite work for vSVA.
* Updates documentation
----------------------------------------------------------------
Cédric Le Goater (6):
include/hw/hyperv: Remove unused 'struct mshv_vp_registers' definition
update-linux-headers: Remove "asm-s390/unistd_32.h" ppc/vof: Fix build error
target/riscv: Fix build errors
tests/qtest: Fix build error
tests/rcutorture: Fix build error
Farhan Ali (1):
util/vfio-helper: Fix endianness in PCI config read/write functions
Joao Martins (1):
vfio: Add a backend_flag parameter to vfio_container_query_dirty_bitmap()
Philippe Mathieu-Daudé (1):
hw/vfio/migration: Check base architecture at runtime
Yi Liu (3):
intel_iommu_accel: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is changed
intel_iommu: Replay pasid bindings after context cache invalidation
Zhenzhong Duan (29):
docs/devel: Remove stale comments related to iommufd dirty tracking
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry
intel_iommu: Delete RPS capability related supporting code
intel_iommu: Update terminology to match VTD spec
hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool
hw/pci: Introduce pci_device_get_viommu_flags()
intel_iommu: Implement get_viommu_flags() callback
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
vfio/iommufd: Force creating nesting parent HWPT
intel_iommu_accel: Check for compatibility with IOMMUFD backed device when x-flts=on
intel_iommu_accel: Fail passthrough device under PCI bridge if x-flts=on
intel_iommu_accel: Stick to system MR for IOMMUFD backed host device when x-flts=on
intel_iommu: Add some macros and inline functions
intel_iommu_accel: Bind/unbind guest page table to host
intel_iommu: Add migration support with x-flts=on
intel_iommu: Enable host device when x-flts=on in scalable mode
docs/devel: Add IOMMUFD nesting documentation
vfio/iommufd: Add framework code to support getting dirty bitmap before unmap vfio/iommufd: Query dirty bitmap before DMA unmap
vfio/container-legacy: rename vfio_dma_unmap_bitmap() to vfio_legacy_dma_unmap_get_dirty_bitmap() vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support
intel_iommu: Fix unmap_bitmap failure with legacy VFIO backend
vfio/listener: Add missing dirty tracking in region_del
vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure vfio/migration: Allow live migration with vIOMMU without VFs using device dirty tracking hw/pci: Introduce pci_device_get_host_iommu_quirks()
intel_iommu_accel: Implement get_host_iommu_quirks() callback
vfio/listener: Bypass readonly region for dirty tracking
Workaround for ERRATA_772415_SPR17
MAINTAINERS | 2 + docs/devel/vfio-iommufd.rst | 38 ++- hw/i386/intel_iommu_accel.h | 56 ++++ hw/i386/intel_iommu_internal.h | 155 +++++++---- hw/vfio/vfio-helpers.h | 2 - include/hw/core/iommu.h | 30 ++ include/hw/hyperv/hvgdk_mini.h | 7 - include/hw/i386/intel_iommu.h | 6 +- include/hw/pci/pci.h | 55 ++++ include/hw/vfio/vfio-container.h | 9 +- include/hw/vfio/vfio-device.h | 15 + include/system/iommufd.h | 2 +- backends/iommufd.c | 5 +- hw/i386/intel_iommu.c | 572 +++++++++++++++++++++++---------------- hw/i386/intel_iommu_accel.c | 272 +++++++++++++++++++ hw/pci/pci.c | 35 ++- hw/ppc/vof.c | 5 +- hw/vfio-user/container.c | 5 +- hw/vfio/container-legacy.c | 15 +- hw/vfio/container.c | 20 +- hw/vfio/device.c | 32 +++ hw/vfio/helpers.c | 17 -- hw/vfio/iommufd.c | 71 ++++- hw/vfio/listener.c | 70 ++++- hw/vfio/migration-multifd.c | 12 +- hw/vfio/migration.c | 40 ++- target/riscv/cpu.c | 2 +- tests/qtest/intel-iommu-test.c | 4 +- tests/qtest/libqtest.c | 3 +- tests/unit/rcutorture.c | 2 +- util/vfio-helpers.c | 15 +- backends/trace-events | 2 +- hw/i386/Kconfig | 5 + hw/i386/meson.build | 1 + hw/i386/trace-events | 4 + hw/vfio/trace-events | 3 +- scripts/update-linux-headers.sh | 1 - 37 files changed, 1207 insertions(+), 383 deletions(-) create mode 100644 hw/i386/intel_iommu_accel.h create mode 100644 include/hw/core/iommu.h create mode 100644 hw/i386/intel_iommu_accel.c
[PULL 01/41] hw/vfio/migration: Check base architecture at runtime
From: Philippe Mathieu-Daudé <[email protected]>
Inline vfio_arch_wants_loading_config_after_iter() and replace the compile time check of the TARGET_ARM definition by a runtime call to target_base_arm().
--- hw/vfio/vfio-helpers.h | 2 -- hw/vfio/helpers.c | 17 ----------------- hw/vfio/migration-multifd.c | 12 +++++++++++- 3 files changed, 11 insertions(+), 20 deletions(-)
hw/vfio/vfio-helpers.h +0 -2
@@ -32,6 +32,4 @@ struct vfio_device_info *vfio_get_device_info(int fd);int vfio_kvm_device_add_fd (int fd,Error **errp);int vfio_kvm_device_del_fd (int fd,Error **errp); -bool vfio_arch_wants_loading_config_after_iter (void ); -#endif /* HW_VFIO_VFIO_HELPERS_H */
hw/vfio/helpers.c +0 -17
@@ -210,20 +210,3 @@ retry:return info; } - -bool vfio_arch_wants_loading_config_after_iter (void ) -{ -/* -* Starting the config load only after all iterables were loaded (during -* non-iterables loading phase) is required for ARM64 due to this platform -* VFIO dependency on interrupt controller being loaded first. -* -* See commit d329f5032e17 ("vfio: Move the saving of the config space to -* the right place in VFIO migration"). -*/ -#if defined(TARGET_ARM ) -return true; -#else -return false; -#endif -}
hw/vfio/migration-multifd.c +11 -1
@@ -16,6 +16,7 @@#include "qemu/error-report.h" #include "qemu/lockable.h" #include "qemu/main-loop.h" +#include "qemu/target-info.h" #include "qemu/thread.h" #include "io/channel-buffer.h" #include "migration/qemu-file.h"
@@ -44,7 +45,16 @@ bool vfio_load_config_after_iter(VFIODevice *vbasedev) }assert (vbasedev->migration_load_config_after_iter ==ON_OFF_AUTO_AUTO ); -return vfio_arch_wants_loading_config_after_iter (); + +/* +* Starting the config load only after all iterables were loaded (during +* non-iterables loading phase) is required for ARM64 due to this platform +* VFIO dependency on interrupt controller being loaded first. +* +* See commit d329f5032e17 ("vfio: Move the saving of the config space to +* the right place in VFIO migration"). +*/ +return target_base_arm (); }/* type safety */
Signature · 1 line
2.52.0
[PULL 02/41] docs/devel: Remove stale comments related to iommufd dirty tracking
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
IOMMUFD dirty tracking support had be merged in merge commit dd4bc5f1cfe9, the stale comments could be dropped.
--- docs/devel/vfio-iommufd.rst | 12 ------------ 1 file changed, 12 deletions(-)
docs/devel/vfio-iommufd.rst +0 -12
@@ -127,18 +127,6 @@ Supports x86, ARM and s390x currently. Caveats ======= -Dirty page sync ---------------- - -Dirty page sync with iommufd backend is unsupported yet, live migration is -disabled by default. But it can be force enabled like below, low efficient -though. - -.. code-block:: bash - - -object iommufd,id=iommufd0 - -device vfio-pci,host=0000:02:00.0,iommufd=iommufd0,enable-migration=on - P2P DMA -------
Signature · 1 line
2.52.0
[PULL 04/41] intel_iommu: Delete RPS capability related supporting code
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
RID-PASID Support(RPS) is not set in vIOMMU ECAP register, the supporting code is there but never takes effect.
Meanwhile, according to VTD spec section 3.4.3:
"Implementations not supporting RID_PASID capability (ECAP_REG.RPS is 0b), use a PASID value of 0 to perform address translation for requests without PASID."
We should delete the supporting code which fetches RID_PASID field from scalable context entry and use 0 as RID_PASID directly, because RID_PASID field is ignored if no RPS support according to spec.
This simplifies the code and doesn't bring any penalty.
--- hw/i386/intel_iommu_internal.h | 2 +- hw/i386/intel_iommu.c | 89 +++++++++++----------------------- 2 files changed, 28 insertions(+), 63 deletions(-)
hw/i386/intel_iommu_internal.h +1 -1
@@ -609,7 +609,7 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CTX_ENTRY_LEGACY_SIZE 16#define VTD_CTX_ENTRY_SCALABLE_SIZE 32 -#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff +#define PASID_0 0#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0 (aw) (0x1e0ULL | ~VTD_HAW_MASK(aw))#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL#define VTD_SM_CONTEXT_ENTRY_PRE 0x10ULL
hw/i386/intel_iommu.c +27 -62
175 lines not on this page · raw message
Signature · 1 line
2.52.0
[PULL 03/41] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
In early days vtd_ce_get_rid2pasid_entry() was used to get pasid entry of rid2pasid, then it was extended to get any pasid entry. So a new name vtd_ce_get_pasid_entry is better to match what it actually does.
No functional change intended.
--- hw/i386/intel_iommu.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
hw/i386/intel_iommu.c +10 -12
@@ -954,10 +954,8 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,return 0 ; } -static int vtd_ce_get_rid2pasid_entry (IntelIOMMUState *s, -VTDContextEntry *ce, -VTDPASIDEntry *pe, -uint32_t pasid) +static int vtd_ce_get_pasid_entry (IntelIOMMUState *s,VTDContextEntry *ce, +VTDPASIDEntry *pe,uint32_t pasid) {dma_addr_t pasid_dir_base;int ret =0 ;
@@ -1035,7 +1033,7 @@ static uint32_t vtd_get_iova_level(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (s->flts) {return VTD_PE_GET_FL_LEVEL (&pe); }else {
@@ -1058,7 +1056,7 @@ static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);return 30 + ((pe.val[0 ] >>2 ) &VTD_SM_PASID_ENTRY_AW ) *9 ; }
@@ -1126,7 +1124,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (s->flts) {return pe.val[2 ] &VTD_SM_PASID_ENTRY_FLPTPTR ; }else {
@@ -1532,7 +1530,7 @@ static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,* has valid rid2pasid setting, which includes valid * rid2pasid field and corresponding pasid entry setting */ -return vtd_ce_get_rid2pasid_entry (s, ce, &pe,PCI_NO_PASID ); +return vtd_ce_get_pasid_entry (s, ce, &pe,PCI_NO_PASID ); }/* Map a device to its corresponding domain (context-entry) */
@@ -1621,7 +1619,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);return VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); }
@@ -1697,7 +1695,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,int ret;if (s->root_scalable) { - ret =vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); + ret =vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (ret) {/* * This error is guest triggerable. We should assumt PT
@@ -3083,7 +3081,7 @@ static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,return ret; } -return vtd_ce_get_rid2pasid_entry (s, &ce, pe, vtd_as->pasid); +return vtd_ce_get_pasid_entry (s, &ce, pe, vtd_as->pasid); }static int vtd_pasid_entry_compare (VTDPASIDEntry *p1,VTDPASIDEntry *p2)
@@ -5161,7 +5159,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,if (ret) {return -EINVAL ; } - ret =vtd_ce_get_rid2pasid_entry (s, &ce, &pe, vtd_as->pasid); + ret =vtd_ce_get_pasid_entry (s, &ce, &pe, vtd_as->pasid);if (ret) {return -EINVAL ; }
Signature · 1 line
2.52.0
[PULL 05/41] intel_iommu: Update terminology to match VTD spec
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
VTD spec revision 3.4 released in December 2021 renamed "First-level" to "First-stage" and "Second-level" to "Second-stage".
Do the same in intel_iommu code to match spec, change all existing "fl/sl/FL/SL/first level/second level/stage-1/stage-2" terminology to "fs/ss/FS/SS/first stage/second stage".
Opportunistically fix a error print of "flts=on" with "x-flts=on".
No functional changes intended.
--- hw/i386/intel_iommu_internal.h | 85 ++++++------ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 247 +++++++++++++++++---------------- tests/qtest/intel-iommu-test.c | 4 +- 4 files changed, 170 insertions(+), 168 deletions(-)
hw/i386/intel_iommu_internal.h +43 -42
@@ -196,8 +196,8 @@#define VTD_ECAP_PSS (7ULL << 35)/* limit: MemTxAttrs::pid */ #define VTD_ECAP_PASID (1ULL << 40)#define VTD_ECAP_SMTS (1ULL << 43) -#define VTD_ECAP_SLTS (1ULL << 46) -#define VTD_ECAP_FLTS (1ULL << 47) +#define VTD_ECAP_SSTS (1ULL << 46) +#define VTD_ECAP_FSTS (1ULL << 47)/* CAP_REG */ /* (offset >> 4) << 24 */
@@ -211,7 +211,7 @@#define VTD_MAMV 18ULL#define VTD_CAP_MAMV (VTD_MAMV << 48)#define VTD_CAP_PSI (1ULL << 39) -#define VTD_CAP_SLLPS ((1ULL << 34) | (1ULL << 35)) +#define VTD_CAP_SSLPS ((1ULL << 34) | (1ULL << 35))#define VTD_CAP_DRAIN_WRITE (1ULL << 54)#define VTD_CAP_DRAIN_READ (1ULL << 55)#define VTD_CAP_FS1GP (1ULL << 56)
@@ -284,7 +284,7 @@ typedef enum VTDFaultReason {VTD_FR_ADDR_BEYOND_MGAW ,/* Input-address above (2^x-1) */ VTD_FR_WRITE ,/* No write permission */ VTD_FR_READ ,/* No read permission */ -/* Fail to access a second-level paging entry (not SL_PML4E) */ +/* Fail to access a second-stage paging entry (not SS_PML4E) */ VTD_FR_PAGING_ENTRY_INV ,VTD_FR_ROOT_TABLE_INV ,/* Fail to access a root-entry */ VTD_FR_CONTEXT_TABLE_INV, /* Fail to access a context-entry */
@@ -292,7 +292,8 @@ typedef enum VTDFaultReason {VTD_FR_ROOT_ENTRY_RSVD ,/* Non-zero reserved field in a present context-entry */ VTD_FR_CONTEXT_ENTRY_RSVD , -/* Non-zero reserved field in a second-level paging entry with at lease one +/* +* Non-zero reserved field in a second-stage paging entry with at lease one * Read(R) and Write(W) or Execute(E) field is Set. */ VTD_FR_PAGING_ENTRY_RSVD,
@@ -329,7 +330,7 @@ typedef enum VTDFaultReason {VTD_FR_PASID_ENTRY_P =0x59 ,VTD_FR_PASID_TABLE_ENTRY_INV =0x5b ,/*Invalid PASID table entry */ -/* Fail to access a first-level paging entry (not FS_PML4E) */ +/* Fail to access a first-stage paging entry (not FS_PML4E) */ VTD_FR_FS_PAGING_ENTRY_INV =0x70 , VTD_FR_FS_PAGING_ENTRY_P = 0x71,/* Non-zero reserved field in present first-stage paging entry */
@@ -473,23 +474,23 @@ typedef union VTDPRDesc VTDPRDesc;#define VTD_SPTE_PAGE_L1_RSVD_MASK (aw, stale_tm) \ stale_tm ? \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \ + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_PAGE_L2_RSVD_MASK (aw) \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_PAGE_L3_RSVD_MASK (aw) \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_PAGE_L4_RSVD_MASK (aw) \ - (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_LPAGE_L2_RSVD_MASK (aw, stale_tm) \ stale_tm ? \ - (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \ - (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \ + (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_LPAGE_L3_RSVD_MASK (aw, stale_tm) \ stale_tm ? \ - (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \ - (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \ + (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))/* Rsvd field masks for fpte */ #define VTD_FS_UPPER_IGNORED 0xfff0000000000000ULL
@@ -596,8 +597,8 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CONTEXT_TT_MULTI_LEVEL 0#define VTD_CONTEXT_TT_DEV_IOTLB (1ULL << 2)#define VTD_CONTEXT_TT_PASS_THROUGH (2ULL << 2) -/* Second Level Page Translation Pointer*/ -#define VTD_CONTEXT_ENTRY_SLPTPTR (~0xfffULL) +/* Second Stage Page Translation Pointer*/ +#define VTD_CONTEXT_ENTRY_SSPTPTR (~0xfffULL)#define VTD_CONTEXT_ENTRY_RSVD_LO (aw) (0xff0ULL | ~VTD_HAW_MASK(aw))/* hi */ #define VTD_CONTEXT_ENTRY_AW 7ULL/* Adjusted guest-address-width */
@@ -635,37 +636,37 @@ typedef struct VTDPASIDCacheInfo {/* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULL#define VTD_SM_PASID_ENTRY_PGTT (7ULL << 6) -#define VTD_SM_PASID_ENTRY_FLT (1ULL << 6) -#define VTD_SM_PASID_ENTRY_SLT (2ULL << 6) +#define VTD_SM_PASID_ENTRY_FST (1ULL << 6) +#define VTD_SM_PASID_ENTRY_SST (2ULL << 6)#define VTD_SM_PASID_ENTRY_NESTED (3ULL << 6)#define VTD_SM_PASID_ENTRY_PT (4ULL << 6)#define VTD_SM_PASID_ENTRY_AW 7ULL/* Adjusted guest-address-width */ #define VTD_SM_PASID_ENTRY_DID (val) ((val) & VTD_DOMAIN_ID_MASK) -#define VTD_SM_PASID_ENTRY_FLPM 3ULL -#define VTD_SM_PASID_ENTRY_FLPTPTR (~0xfffULL) - -/* First Level Paging Structure */ -/* Masks for First Level Paging Entry */ -#define VTD_FL_P 1ULL -#define VTD_FL_RW (1ULL << 1) -#define VTD_FL_US (1ULL << 2) -#define VTD_FL_A (1ULL << 5) -#define VTD_FL_D (1ULL << 6) - -/* Second Level Page Translation Pointer*/ -#define VTD_SM_PASID_ENTRY_SLPTPTR (~0xfffULL) - -/* Second Level Paging Structure */ -/* Masks for Second Level Paging Entry */ -#define VTD_SL_RW_MASK 3ULL -#define VTD_SL_R 1ULL -#define VTD_SL_W (1ULL << 1) -#define VTD_SL_IGN_COM 0xbff0000000000000ULL -#define VTD_SL_TM (1ULL << 62) - -/* Common for both First Level and Second Level */ +#define VTD_SM_PASID_ENTRY_FSPM 3ULL +#define VTD_SM_PASID_ENTRY_FSPTPTR (~0xfffULL) + +/* First Stage Paging Structure */ +/* Masks for First Stage Paging Entry */ +#define VTD_FS_P 1ULL +#define VTD_FS_RW (1ULL << 1) +#define VTD_FS_US (1ULL << 2) +#define VTD_FS_A (1ULL << 5) +#define VTD_FS_D (1ULL << 6) + +/* Second Stage Page Translation Pointer*/ +#define VTD_SM_PASID_ENTRY_SSPTPTR (~0xfffULL) + +/* Second Stage Paging Structure */ +/* Masks for Second Stage Paging Entry */ +#define VTD_SS_RW_MASK 3ULL +#define VTD_SS_R 1ULL +#define VTD_SS_W (1ULL << 1) +#define VTD_SS_IGN_COM 0xbff0000000000000ULL +#define VTD_SS_TM (1ULL << 62) + +/* Common for both First Stage and Second Stage */ #define VTD_PML4_LEVEL 4#define VTD_PDP_LEVEL 3#define VTD_PD_LEVEL 2
include/hw/i386/intel_iommu.h +1 -1
@@ -271,7 +271,7 @@ struct IntelIOMMUState {bool caching_mode;/* RO - is cap CM enabled? */ bool scalable_mode;/* RO - is Scalable Mode supported? */ -bool flts;/* RO - is stage-1 translation supported? */ +bool fsts;/* RO - is first stage translation supported? */ bool snoop_control;/* RO - is SNP filed supported? */ dma_addr_t root;/* Current root table pointer */
hw/i386/intel_iommu.c +124 -123
611 lines not on this page · raw message
tests/qtest/intel-iommu-test.c +2 -2
@@ -13,9 +13,9 @@#include "hw/i386/intel_iommu_internal.h" #define CAP_STAGE_1_FIXED1 (VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | \ - VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS) + VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SSLPS)#define ECAP_STAGE_1_FIXED1 (VTD_ECAP_QI | VTD_ECAP_IR | VTD_ECAP_IRO | \ - VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FLTS) + VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FSTS)static inline uint64_t vtd_reg_readq (QTestState *s,uint64_t offset) {
Signature · 1 line
2.52.0
[PULL 06/41] hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Returns true if PCI device is aliased or false otherwise. This will be used in following patch to determine if a PCI device is under a PCI bridge.
--- include/hw/pci/pci.h | 2 ++ hw/pci/pci.c | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +2 -0
@@ -637,6 +637,8 @@ typedef struct PCIIOMMUOps { bool is_write); } PCIIOMMUOps; +bool pci_device_get_iommu_bus_devfn (PCIDevice *dev,PCIBus **piommu_bus, +PCIBus **aliased_bus,int *aliased_devfn);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);bool pci_device_set_iommu_device (PCIDevice *dev,HostIOMMUDevice *hiod,Error **errp);
hw/pci/pci.c +8 -4
@@ -2869,20 +2869,21 @@ static void pci_device_class_base_init(ObjectClass *klass, const void *data)* For call sites which don't need aliased BDF, passing NULL to * aliased_[bus|devfn] is allowed. * +* Returns true if PCI device RID is aliased or false otherwise. +* * @piommu_bus: return root #PCIBus backed by an IOMMU for the PCI device. * * @aliased_bus: return aliased #PCIBus of the PCI device, optional. * * @aliased_devfn: return aliased devfn of the PCI device, optional. */ -static void pci_device_get_iommu_bus_devfn (PCIDevice *dev, -PCIBus **piommu_bus, -PCIBus **aliased_bus, -int *aliased_devfn) +bool pci_device_get_iommu_bus_devfn (PCIDevice *dev,PCIBus **piommu_bus, +PCIBus **aliased_bus,int *aliased_devfn) {PCIBus *bus =pci_get_bus (dev);PCIBus *iommu_bus = bus;int devfn = dev->devfn; +bool aliased = false;while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) {PCIBus *parent_bus =pci_get_bus (iommu_bus->parent_dev);
@@ -2919,6 +2920,7 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev, devfn = parent->devfn; bus = parent_bus; } + aliased = true; }/*
@@ -2953,6 +2955,8 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,if (aliased_devfn) { *aliased_devfn = devfn; } + +return aliased; }AddressSpace *pci_device_iommu_address_space (PCIDevice *dev)
Signature · 1 line
2.52.0
[PULL 10/41] vfio/iommufd: Force creating nesting parent HWPT
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Call pci_device_get_viommu_flags() to get if vIOMMU supports VIOMMU_FLAG_WANT_NESTING_PARENT.
If yes, create a nesting parent HWPT and add it to the container's hwpt_list, letting this parent HWPT cover the entire second stage mappings (GPA=>HPA).
This allows a VFIO passthrough device to directly attach to this default HWPT and then to use the system address space and its listener.
Introduce a vfio_device_get_viommu_flags_want_nesting() helper to facilitate this implementation.
It is safe to do so because a vIOMMU will be able to fail in set_iommu_device() call, if something else related to the VFIO device or vIOMMU isn't compatible.
--- include/hw/vfio/vfio-device.h | 2 ++ hw/vfio/device.c | 12 ++++++++++++ hw/vfio/iommufd.c | 9 +++++++++ 3 files changed, 23 insertions(+)
include/hw/vfio/vfio-device.h +2 -0
@@ -257,6 +257,8 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,void vfio_device_unprepare (VFIODevice *vbasedev); +bool vfio_device_get_viommu_flags_want_nesting (VFIODevice *vbasedev); +int vfio_device_get_region_info (VFIODevice *vbasedev,int index,struct vfio_region_info **info); int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
hw/vfio/device.c +12 -0
@@ -23,6 +23,7 @@#include "hw/vfio/vfio-device.h" #include "hw/vfio/pci.h" +#include "hw/core/iommu.h" #include "hw/core/hw-error.h" #include "trace.h" #include "qapi/error.h"
@@ -515,6 +516,17 @@ void vfio_device_unprepare(VFIODevice *vbasedev) vbasedev->bcontainer =NULL ; } +bool vfio_device_get_viommu_flags_want_nesting (VFIODevice *vbasedev) +{ +VFIOPCIDevice *vdev =vfio_pci_from_vfio_device (vbasedev); + +if (vdev) { +return !!(pci_device_get_viommu_flags (PCI_DEVICE (vdev)) & +VIOMMU_FLAG_WANT_NESTING_PARENT ); + } +return false; +} +/* * Traditional ioctl() based io */
hw/vfio/iommufd.c +9 -0
@@ -361,6 +361,15 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +/* +* If vIOMMU requests VFIO's cooperation to create nesting parent HWPT, +* force to create it so that it could be reused by vIOMMU to create +* nested HWPT. +*/ +if (vfio_device_get_viommu_flags_want_nesting (vbasedev)) { + flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + } +if (cpr_is_incoming ()) { hwpt_id = vbasedev->cpr.hwpt_id; goto skip_alloc;
Signature · 1 line
2.52.0
[PULL 07/41] hw/pci: Introduce pci_device_get_viommu_flags()
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Introduce a new PCIIOMMUOps optional callback, get_viommu_flags() which allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is VIOMMU_FLAG_WANT_NESTING_PARENT that advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.
pci_device_get_viommu_flags() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_flags() is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:
1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.
Note that this op will be invoked at the attach_device() stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU through the set_iommu_device() callback that will be after the attach_device().
See below sequence:
vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags() for HW nesting cap create a nesting parent HWPT
attach device to the HWPT vfio_device_hiod_create_and_realize() creating hiod
...
pci_device_set_iommu_device(hiod)
--- MAINTAINERS | 1 + include/hw/core/iommu.h | 25 +++++++++++++++++++++++++ include/hw/pci/pci.h | 22 ++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 59 insertions(+) create mode 100644 include/hw/core/iommu.h
MAINTAINERS +1 -0
@@ -2368,6 +2368,7 @@ F: include/system/iommufd.h F: backends/host_iommu_device.c F: include/system/host_iommu_device.h F: include/qemu/chardev_open.h +F: include/hw/core/iommu.h F: util/chardev_open.c F: docs/devel/vfio-iommufd.rst
include/hw/core/iommu.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* General vIOMMU flags +* +* Copyright (C) 2026 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H + +#include "qemu/bitops.h" + +/* +* Theoretical vIOMMU flags. Only determined by the vIOMMU device properties and +* independent on the actual host IOMMU capabilities they may depend on. Each +* flag can be an expectation or request to other sub-system or just a pure +* vIOMMU capability. vIOMMU can choose which flags to expose. +*/ +enum viommu_flags { +/* vIOMMU needs nesting parent HWPT to create nested HWPT */ +VIOMMU_FLAG_WANT_NESTING_PARENT =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +22 -0
@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn); +/** +* @get_viommu_flags: get vIOMMU flags +* +* Optional callback, if not implemented, then vIOMMU doesn't support +* exposing flags to other sub-system, e.g., VFIO. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: bitmap with each bit representing a vIOMMU flag defined in +* enum viommu_flags. +*/ +uint64_t (*get_viommu_flags)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. *
@@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, Error **errp);void pci_device_unset_iommu_device (PCIDevice *dev); +/** +* pci_device_get_viommu_flags: get vIOMMU flags. +* +* Returns: bitmap with each bit representing a vIOMMU flag defined in +* enum viommu_flags. Or 0 if vIOMMU doesn't report any. +* +* @dev: PCI device pointer. +*/ +uint64_t pci_device_get_viommu_flags (PCIDevice *dev); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB.
hw/pci/pci.c +11 -0
@@ -3021,6 +3021,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev) } } +uint64_t pci_device_get_viommu_flags (PCIDevice *dev) +{ +PCIBus *iommu_bus; + +pci_device_get_iommu_bus_devfn (dev, &iommu_bus,NULL ,NULL ); +if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) { +return iommu_bus->iommu_ops->get_viommu_flags (iommu_bus->iommu_opaque); + } +return 0 ; +} +int pci_pri_request_page (PCIDevice *dev,uint32_t pasid,bool priv_req,bool exec_req,hwaddr addr,bool lpig,uint16_t prgi,bool is_read,bool is_write)
Signature · 1 line
2.52.0
[PULL 12/41] intel_iommu_accel: Fail passthrough device under PCI bridge if x-flts=on
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Currently we don't support nested translation for passthrough device with emulated device under same PCI bridge, because they require different address space when x-flts=on.
In theory, we do support if devices under same PCI bridge are all passthrough devices. But emulated device can be hotplugged under same bridge. To simplify, just forbid passthrough device under PCI bridge no matter if there is, or will be emulated devices under same bridge. This is acceptable because PCIE bridge is more popular than PCI bridge now.
--- hw/i386/intel_iommu_accel.h | 4 ++-- hw/i386/intel_iommu.c | 7 ++++--- hw/i386/intel_iommu_accel.c | 12 +++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-)
hw/i386/intel_iommu_accel.h +2 -2
@@ -13,11 +13,11 @@#include CONFIG_DEVICES #ifdef CONFIG_VTD_ACCEL -bool vtd_check_hiod_accel (IntelIOMMUState *s,HostIOMMUDevice *hiod, +bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp);#else static inline bool vtd_check_hiod_accel (IntelIOMMUState *s, -HostIOMMUDevice *hiod, +VTDHostIOMMUDevice *vtd_hiod,Error **errp) {error_setg (errp,"host IOMMU cannot be checked!" );
hw/i386/intel_iommu.c +4 -3
@@ -4570,9 +4570,10 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,return vtd_dev_as; } -static bool vtd_check_hiod (IntelIOMMUState *s,HostIOMMUDevice *hiod, +static bool vtd_check_hiod (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp) { +HostIOMMUDevice *hiod = vtd_hiod->hiod;HostIOMMUDeviceClass *hiodc =HOST_IOMMU_DEVICE_GET_CLASS (hiod);int ret;
@@ -4596,7 +4597,7 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } -return vtd_check_hiod_accel (s, hiod, errp); +return vtd_check_hiod_accel (s, vtd_hiod, errp); } static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
@@ -4632,7 +4633,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, vtd_hiod->iommu_state = s; vtd_hiod->hiod = hiod; -if (!vtd_check_hiod (s, hiod, errp)) { +if (!vtd_check_hiod (s, vtd_hiod, errp)) {g_free (vtd_hiod);vtd_iommu_unlock (s);return false;
hw/i386/intel_iommu_accel.c +11 -1
@@ -12,12 +12,16 @@#include "system/iommufd.h" #include "intel_iommu_internal.h" #include "intel_iommu_accel.h" +#include "hw/pci/pci_bus.h" -bool vtd_check_hiod_accel (IntelIOMMUState *s,HostIOMMUDevice *hiod, +bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp) { +HostIOMMUDevice *hiod = vtd_hiod->hiod;struct HostIOMMUDeviceCaps *caps = &hiod->caps;struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd; +PCIBus *bus = vtd_hiod->bus; +PCIDevice *pdev = bus->devices[vtd_hiod->devfn];if (!object_dynamic_cast (OBJECT (hiod),TYPE_HOST_IOMMU_DEVICE_IOMMUFD )) {error_setg (errp,"Need IOMMUFD backend when x-flts=on" );
@@ -36,6 +40,12 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, HostIOMMUDevice *hiod,return false; } +if (pci_device_get_iommu_bus_devfn (pdev, &bus,NULL ,NULL )) { +error_setg (errp,"Host device downstream to a PCI bridge is " +"unsupported when x-flts=on" ); +return false; + } +error_setg (errp,"host IOMMU is incompatible with guest first stage translation" );return false;
Signature · 1 line
2.52.0
[PULL 20/41] intel_iommu: Enable host device when x-flts=on in scalable mode
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Now that all infrastructures of supporting passthrough device running with first stage translation are there, enable it now.
--- hw/i386/intel_iommu_accel.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/i386/intel_iommu_accel.c +1 -3
@@ -47,9 +47,7 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,return false; } -error_setg (errp, -"host IOMMU is incompatible with guest first stage translation" ); -return false; +return true; }VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as)
Signature · 1 line
2.52.0
[PULL 14/41] intel_iommu: Add some macros and inline functions
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Add some macros and inline functions that will be used by following patch.
This patch also make a cleanup to change below macros to use extract64() just like what smmu does, because they are either used in following patches or used indirectly by new introduced inline functions.
VTD_INV_DESC_PIOTLB_IH
VTD_SM_PASID_ENTRY_PGTT
VTD_SM_PASID_ENTRY_DID
VTD_SM_PASID_ENTRY_FSPM
VTD_SM_PASID_ENTRY_FSPTPTR
But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.
--- hw/i386/intel_iommu_internal.h | 50 ++++++++++++++++++++++++++++------ hw/i386/intel_iommu.c | 27 +++++++++--------- 2 files changed, 55 insertions(+), 22 deletions(-)
hw/i386/intel_iommu_internal.h +41 -9
@@ -516,7 +516,7 @@ typedef union VTDPRDesc VTDPRDesc;#define VTD_INV_DESC_PIOTLB_DID (val) (((val) >> 16) & VTD_DOMAIN_ID_MASK)#define VTD_INV_DESC_PIOTLB_PASID (val) (((val) >> 32) & 0xfffffULL)#define VTD_INV_DESC_PIOTLB_AM (val) ((val) & 0x3fULL) -#define VTD_INV_DESC_PIOTLB_IH (val) (((val) >> 6) & 0x1) +#define VTD_INV_DESC_PIOTLB_IH (x) extract64((x)->val[1], 6, 1)#define VTD_INV_DESC_PIOTLB_ADDR (val) ((val) & ~0xfffULL)#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000f1c0ULL#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL
@@ -636,17 +636,20 @@ typedef struct VTDPASIDCacheInfo {/* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULL -#define VTD_SM_PASID_ENTRY_PGTT (7ULL << 6) -#define VTD_SM_PASID_ENTRY_FST (1ULL << 6) -#define VTD_SM_PASID_ENTRY_SST (2ULL << 6) -#define VTD_SM_PASID_ENTRY_NESTED (3ULL << 6) -#define VTD_SM_PASID_ENTRY_PT (4ULL << 6) +#define VTD_SM_PASID_ENTRY_PGTT (x) extract64((x)->val[0], 6, 3) +#define VTD_SM_PASID_ENTRY_FST 1 +#define VTD_SM_PASID_ENTRY_SST 2 +#define VTD_SM_PASID_ENTRY_NESTED 3 +#define VTD_SM_PASID_ENTRY_PT 4#define VTD_SM_PASID_ENTRY_AW 7ULL/* Adjusted guest-address-width */ -#define VTD_SM_PASID_ENTRY_DID (val) ((val) & VTD_DOMAIN_ID_MASK) +#define VTD_SM_PASID_ENTRY_DID (x) extract64((x)->val[1], 0, 16) -#define VTD_SM_PASID_ENTRY_FSPM 3ULL -#define VTD_SM_PASID_ENTRY_FSPTPTR (~0xfffULL) +#define VTD_SM_PASID_ENTRY_SRE (x) extract64((x)->val[2], 0, 1) +#define VTD_SM_PASID_ENTRY_FSPM (x) extract64((x)->val[2], 2, 2) +#define VTD_SM_PASID_ENTRY_WPE (x) extract64((x)->val[2], 4, 1) +#define VTD_SM_PASID_ENTRY_EAFE (x) extract64((x)->val[2], 7, 1) +#define VTD_SM_PASID_ENTRY_FSPTPFN (x) extract64((x)->val[2], 12, 52)/* First Stage Paging Structure */ /* Masks for First Stage Paging Entry */
@@ -696,4 +699,33 @@ struct vtd_as_key {uint8_t devfn; uint32_t pasid; }; + +static inline dma_addr_t vtd_pe_get_fspt_base (VTDPASIDEntry *pe) +{ +return VTD_SM_PASID_ENTRY_FSPTPFN (pe) <<VTD_PAGE_SHIFT ; +} + +/* +* First stage IOVA address width: 48 bits for 4-level paging(FSPM=00) +* 57 bits for 5-level paging(FSPM=01) +*/ +static inline uint32_t vtd_pe_get_fs_aw (VTDPASIDEntry *pe) +{ +/* +* Paging mode for first-stage translation (VTD spec Figure 9-6) +* 00: 4-level paging, 01: 5-level paging +*/ +return VTD_HOST_AW_48BIT +VTD_SM_PASID_ENTRY_FSPM (pe) *9 ; +} + +static inline bool vtd_pe_pgtt_is_pt (VTDPASIDEntry *pe) +{ +return (VTD_SM_PASID_ENTRY_PGTT (pe) ==VTD_SM_PASID_ENTRY_PT ); +} + +/* check if PGTT is first stage translation */ +static inline bool vtd_pe_pgtt_is_fst (VTDPASIDEntry *pe) +{ +return (VTD_SM_PASID_ENTRY_PGTT (pe) ==VTD_SM_PASID_ENTRY_FST ); +}#endif
hw/i386/intel_iommu.c +14 -13
@@ -48,10 +48,11 @@#define VTD_CE_GET_PRE (ce) \ ((ce)->val[0] & VTD_SM_CONTEXT_ENTRY_PRE) -/* pe operations */ -#define VTD_PE_GET_TYPE (pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT) -#define VTD_PE_GET_FS_LEVEL (pe) \ - (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM)) +/* +* Paging mode for first-stage translation (VTD spec Figure 9-6) +* 00: 4-level paging, 01: 5-level paging +*/ +#define VTD_PE_GET_FS_LEVEL (pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)#define VTD_PE_GET_SS_LEVEL (pe) \ (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
@@ -807,7 +808,7 @@ static inline bool vtd_is_fs_level_supported(IntelIOMMUState *s, uint32_t level)/* Return true if check passed, otherwise false */ static inline bool vtd_pe_type_check (IntelIOMMUState *s,VTDPASIDEntry *pe) { -switch (VTD_PE_GET_TYPE (pe)) { +switch (VTD_SM_PASID_ENTRY_PGTT (pe)) {case VTD_SM_PASID_ENTRY_FST :return !!(s->ecap &VTD_ECAP_FSTS );case VTD_SM_PASID_ENTRY_SST :
@@ -882,7 +883,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,return -VTD_FR_PASID_TABLE_ENTRY_INV ; } - pgtt =VTD_PE_GET_TYPE (pe); + pgtt =VTD_SM_PASID_ENTRY_PGTT (pe);if (pgtt ==VTD_SM_PASID_ENTRY_SST && !vtd_is_ss_level_supported (s,VTD_PE_GET_SS_LEVEL (pe))) {return -VTD_FR_PASID_TABLE_ENTRY_INV ;
@@ -1116,7 +1117,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,if (s->root_scalable) {vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (s->fsts) { -return pe.val[2 ] &VTD_SM_PASID_ENTRY_FSPTPTR ; +return vtd_pe_get_fspt_base (&pe); }else {return pe.val[0 ] &VTD_SM_PASID_ENTRY_SSPTPTR ; }
@@ -1605,7 +1606,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,if (s->root_scalable) {vtd_ce_get_pasid_entry (s, ce, &pe, pasid); -return VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); +return VTD_SM_PASID_ENTRY_DID (&pe); }return VTD_CONTEXT_ENTRY_DID (ce->hi);
@@ -1687,7 +1688,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,*/ return false; } -return (VTD_PE_GET_TYPE (&pe) ==VTD_SM_PASID_ENTRY_PT ); +return vtd_pe_pgtt_is_pt (&pe); }return (vtd_ce_get_type (ce) ==VTD_CONTEXT_TT_PASS_THROUGH );
@@ -3108,9 +3109,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,/* Fall through */ case VTD_INV_DESC_PASIDC_G_DSI :if (pc_entry->valid) { - did =VTD_SM_PASID_ENTRY_DID (pc_entry->pasid_entry.val[1 ]); + did =VTD_SM_PASID_ENTRY_DID (&pc_entry->pasid_entry); }else { - did =VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); + did =VTD_SM_PASID_ENTRY_DID (&pe); }if (pc_info->did != did) {return ;
@@ -5154,8 +5155,8 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,if (ret) {return -EINVAL ; } - pgtt =VTD_PE_GET_TYPE (&pe); - domain_id =VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); + pgtt =VTD_SM_PASID_ENTRY_PGTT (&pe); + domain_id =VTD_SM_PASID_ENTRY_DID (&pe); ret =0 ;switch (pgtt) {case VTD_SM_PASID_ENTRY_FST :
Signature · 1 line
2.52.0
[PULL 18/41] intel_iommu: Replay pasid bindings after context cache invalidation
In reply to Cédric Le GoaterFrom: Yi Liu <[email protected]>
This replays guest pasid bindings after context cache invalidation. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing context cache invalidation.
We see old linux such as 6.7.0-rc2 not following the spec, it sends pasid cache invalidation before context cache invalidation, then QEMU depends on context cache invalidation to get pasid entry and setup binding.
--- hw/i386/intel_iommu.c | 42 ++++++++++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 + 2 files changed, 43 insertions(+)
hw/i386/intel_iommu.c +42 -0
@@ -77,6 +77,8 @@ struct vtd_iotlb_key {static void vtd_address_space_refresh_all (IntelIOMMUState *s);static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n);static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s); +static void vtd_pasid_cache_sync_locked (gpointer key,gpointer value, +gpointer user_data);static void vtd_pasid_cache_reset_locked (IntelIOMMUState *s) {
@@ -2322,6 +2324,37 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)* VT-d emulation codes. */ vtd_iommu_replay_all (s); +/* +* Same for pasid cache invalidation, per VT-d spec 6.5.2.1, a global +* context cache invalidation should be followed by global PASID cache +* invalidation. In order to work with guest not following spec, +* handle global PASID cache invalidation here. +*/ +vtd_replay_pasid_bindings_all (s); +} + +static void vtd_pasid_cache_devsi (VTDAddressSpace *vtd_as) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +PCIBus *bus = vtd_as->bus; +uint8_t devfn = vtd_as->devfn; +struct vtd_as_key key = { + .bus = bus, + .devfn = devfn, + .pasid = vtd_as->pasid, + }; +VTDPASIDCacheInfo pc_info; + +if (!s->fsts || !s->root_scalable || !s->dmar_enabled) { +return ; + } + +trace_vtd_pasid_cache_devsi (pci_bus_num (bus), +VTD_PCI_SLOT (devfn),VTD_PCI_FUNC (devfn)); + +/* We fake to be global invalidation just to bypass all checks */ + pc_info.type =VTD_INV_DESC_PASIDC_G_GLOBAL ; +vtd_pasid_cache_sync_locked (&key, vtd_as, &pc_info); }/* Do a context-cache device-selective invalidation.
@@ -2382,6 +2415,15 @@ static void vtd_context_device_invalidate(IntelIOMMUState *s,* happened. */ vtd_address_space_sync (vtd_as); +/* +* Per spec 6.5.2.1, context flush should be followed by PASID +* cache and iotlb flush. In order to work with a guest which does +* not follow spec and missed PASID cache flush, e.g., linux +* 6.7.0-rc2, we have vtd_pasid_cache_devsi() to invalidate PASID +* cache of passthrough device. Host iommu driver would flush +* piotlb when a pasid unbind is passed down to it. +*/ +vtd_pasid_cache_devsi (vtd_as); } } }
hw/i386/trace-events +1 -0
@@ -28,6 +28,7 @@ vtd_pasid_cache_reset(void) "" vtd_inv_desc_pasid_cache_gsi(void) "" vtd_inv_desc_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16 vtd_inv_desc_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32 +vtd_pasid_cache_devsi(uint8_t bus, uint8_t dev, uint8_t fn) "Dev selective PC invalidation dev: %02"PRIx8":%02"PRIx8".%02"PRIx8 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present" vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present" vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
Signature · 1 line
2.52.0
[PULL 08/41] intel_iommu: Implement get_viommu_flags() callback
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Implement get_viommu_flags() callback and expose a request for nesting parent HWPT for now.
VFIO uses it to create nesting parent HWPT which is further used to create nested HWPT in vIOMMU. All these will be implemented in following patches.
--- hw/i386/intel_iommu.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/i386/intel_iommu.c +12 -0
@@ -24,6 +24,7 @@#include "qemu/main-loop.h" #include "qapi/error.h" #include "hw/core/sysbus.h" +#include "hw/core/iommu.h" #include "intel_iommu_internal.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bus.h"
@@ -4659,6 +4660,16 @@ static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)vtd_iommu_unlock (s); } +static uint64_t vtd_get_viommu_flags (void *opaque) +{ +IntelIOMMUState *s = opaque; +uint64_t flags; + + flags = s->fsts ?VIOMMU_FLAG_WANT_NESTING_PARENT :0 ; + +return flags; +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {
@@ -5312,6 +5323,7 @@ static PCIIOMMUOps vtd_iommu_ops = { .pri_register_notifier = vtd_pri_register_notifier, .pri_unregister_notifier = vtd_pri_unregister_notifier, .pri_request_page = vtd_pri_request_page, + .get_viommu_flags = vtd_get_viommu_flags, };static bool vtd_decide_config (IntelIOMMUState *s,Error **errp)
Signature · 1 line
2.52.0
[PULL 16/41] intel_iommu_accel: Propagate PASID-based iotlb invalidation to host
In reply to Cédric Le GoaterFrom: Yi Liu <[email protected]>
This traps the guest PASID-based iotlb invalidation request and propagate it to host.
Intel VT-d 3.0 supports nested translation in PASID granularity. Guest SVA support could be implemented by configuring nested translation on specific pasid. This is also known as dual stage DMA translation.
Under such configuration, guest owns the GVA->GPA translation which is configured as first stage page table on host side for a specific pasid, and host owns GPA->HPA translation. As guest owns first stage translation table, piotlb invalidation should be propagated to host since host IOMMU will cache first level page table related mappings during DMA address translation.
--- hw/i386/intel_iommu_accel.h | 10 +++++ hw/i386/intel_iommu_internal.h | 6 +++ hw/i386/intel_iommu.c | 11 ++++-- hw/i386/intel_iommu_accel.c | 69 ++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_accel.h +10 -0
@@ -17,6 +17,9 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, Error **errp);VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as);bool vtd_propagate_guest_pasid (VTDAddressSpace *vtd_as,Error **errp); +void vtd_flush_host_piotlb_all_locked (IntelIOMMUState *s,uint16_t domain_id, +uint32_t pasid,hwaddr addr, +uint64_t npages,bool ih);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -37,5 +40,12 @@ static inline bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, {return true; } + +static inline void vtd_flush_host_piotlb_all_locked (IntelIOMMUState *s, +uint16_t domain_id, +uint32_t pasid,hwaddr addr, +uint64_t npages,bool ih) +{ +}#endif #endif
hw/i386/intel_iommu_internal.h +6 -0
@@ -622,6 +622,12 @@ typedef struct VTDPASIDCacheInfo {uint32_t pasid; } VTDPASIDCacheInfo; +typedef struct VTDPIOTLBInvInfo { +uint16_t domain_id; +uint32_t pasid; +struct iommu_hwpt_vtd_s1_invalidate *inv_data; +}VTDPIOTLBInvInfo ; +/* PASID Table Related Definitions */ #define VTD_PASID_DIR_BASE_ADDR_MASK (~0xfffULL)#define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
hw/i386/intel_iommu.c +8 -3
@@ -2967,6 +2967,8 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,vtd_iommu_lock (s);g_hash_table_foreach_remove (s->iotlb, vtd_hash_remove_by_pasid, &info); +vtd_flush_host_piotlb_all_locked (s, domain_id, pasid,0 , (uint64_t )-1 , + false);vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
@@ -2986,7 +2988,8 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s, }static void vtd_piotlb_page_invalidate (IntelIOMMUState *s,uint16_t domain_id, -uint32_t pasid,hwaddr addr,uint8_t am) +uint32_t pasid,hwaddr addr,uint8_t am, +bool ih) {VTDIOTLBPageInvInfo info;
@@ -2998,6 +3001,7 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,vtd_iommu_lock (s);g_hash_table_foreach_remove (s->iotlb, vtd_hash_remove_by_page_piotlb, &info); +vtd_flush_host_piotlb_all_locked (s, domain_id, pasid, addr,1 << am, ih);vtd_iommu_unlock (s);vtd_iotlb_page_invalidate_notify (s, domain_id, addr, am, pasid);
@@ -3029,7 +3033,8 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,case VTD_INV_DESC_PIOTLB_PSI_IN_PASID : am =VTD_INV_DESC_PIOTLB_AM (inv_desc->val[1 ]); addr = (hwaddr )VTD_INV_DESC_PIOTLB_ADDR (inv_desc->val[1 ]); -vtd_piotlb_page_invalidate (s, domain_id, pasid, addr, am); +vtd_piotlb_page_invalidate (s, domain_id, pasid, addr, am, +VTD_INV_DESC_PIOTLB_IH (inv_desc));break ;default :
@@ -5176,7 +5181,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, ret =0 ;switch (pgtt) {case VTD_SM_PASID_ENTRY_FST : -vtd_piotlb_page_invalidate (s, domain_id, vtd_as->pasid, addr,0 ); +vtd_piotlb_page_invalidate (s, domain_id, vtd_as->pasid, addr,0 , false);break ;/* Room for other pgtt values */ default :
hw/i386/intel_iommu_accel.c +69 -0
72 lines not on this page · raw message
Signature · 1 line
2.52.0
[PULL 11/41] intel_iommu_accel: Check for compatibility with IOMMUFD backed device when x-flts=on
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
When vIOMMU is configured x-flts=on in scalable mode, first stage page table is passed to host to construct nested page table for passthrough devices.
We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest first stage page table could be used by host.
For instance, vIOMMU supports first stage 1GB large page mapping, but host does not, then this IOMMUFD backed device should fail.
Even of the checks pass, for now we willingly reject the association because all the bits are not there yet, it will be relaxed in the end of this series.
Note vIOMMU has exposed VIOMMU_FLAG_WANT_NESTING_PARENT flag to force VFIO core to create nesting parent HWPT, if host doesn't support nested translation, the creation will fail. So no need to check nested capability here.
[ clg: - hw/i386/intel_iommu_accel.[hc]: Changed Copyright date 2025 -> 2026
- in commit log :
IOMMU_HWPT_ALLOC_NEST_PARENT -> VIOMMU_FLAG_WANT_NESTING_PARENT ]
--- MAINTAINERS | 1 + hw/i386/intel_iommu_accel.h | 28 +++++++++++++++++++++++++ hw/i386/intel_iommu.c | 5 ++--- hw/i386/intel_iommu_accel.c | 42 +++++++++++++++++++++++++++++++++++++ hw/i386/Kconfig | 5 +++++ hw/i386/meson.build | 1 + 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 hw/i386/intel_iommu_accel.h create mode 100644 hw/i386/intel_iommu_accel.c
MAINTAINERS +1 -0
@@ -3961,6 +3961,7 @@ R: Clément Mathieu--Drif <[email protected]> S: Supported F: hw/i386/intel_iommu.c F: hw/i386/intel_iommu_internal.h +F: hw/i386/intel_iommu_accel.* F: include/hw/i386/intel_iommu.h F: tests/functional/x86_64/test_intel_iommu.py F: tests/qtest/intel-iommu-test.c
hw/i386/intel_iommu_accel.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Intel IOMMU acceleration with nested translation +* +* Copyright (C) 2026 Intel Corporation. +* +* Authors: Zhenzhong Duan <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_I386_INTEL_IOMMU_ACCEL_H +#define HW_I386_INTEL_IOMMU_ACCEL_H +#include CONFIG_DEVICES + +#ifdef CONFIG_VTD_ACCEL +bool vtd_check_hiod_accel (IntelIOMMUState *s,HostIOMMUDevice *hiod, +Error **errp); +#else +static inline bool vtd_check_hiod_accel (IntelIOMMUState *s, +HostIOMMUDevice *hiod, +Error **errp) +{ +error_setg (errp,"host IOMMU cannot be checked!" ); +error_append_hint (errp,"CONFIG_VTD_ACCEL is not enabled" ); +return false; +} +#endif +#endif
hw/i386/intel_iommu.c +2 -3
@@ -26,6 +26,7 @@#include "hw/core/sysbus.h" #include "hw/core/iommu.h" #include "intel_iommu_internal.h" +#include "intel_iommu_accel.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bus.h" #include "hw/core/qdev-properties.h"
@@ -4595,9 +4596,7 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } -error_setg (errp, -"host device is uncompatible with first stage translation" ); -return false; +return vtd_check_hiod_accel (s, hiod, errp); } static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
hw/i386/intel_iommu_accel.c new file +42 -0
42 lines not on this page · raw message
hw/i386/Kconfig +5 -0
@@ -150,8 +150,13 @@ config X86_IOMMU config VTD bool + imply VTD_ACCEL select X86_IOMMU +config VTD_ACCEL + bool + depends on VTD && IOMMUFD + config AMD_IOMMU bool select X86_IOMMU
hw/i386/meson.build +1 -0
@@ -21,6 +21,7 @@ i386_ss.add(when: 'CONFIG_Q35', if_true: files('pc_q35.c')) i386_ss.add(when: 'CONFIG_VMMOUSE', if_true: files('vmmouse.c')) i386_ss.add(when: 'CONFIG_VMPORT', if_true: files('vmport.c')) i386_ss.add(when: 'CONFIG_VTD', if_true: files('intel_iommu.c')) +i386_ss.add(when: 'CONFIG_VTD_ACCEL', if_true: files('intel_iommu_accel.c')) i386_ss.add(when: 'CONFIG_SGX', if_true: files('sgx-epc.c','sgx.c'), if_false: files('sgx-stub.c'))
Signature · 1 line
2.52.0
[PULL 22/41] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Currently we support device and iommu dirty tracking, device dirty tracking is preferred.
Add the framework code in iommufd_cdev_unmap() to choose either device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().
--- hw/vfio/iommufd.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)
hw/vfio/iommufd.c +31 -3
@@ -61,14 +61,42 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer, IOMMUTLBEntry *iotlb, bool unmap_all) {const VFIOIOMMUFDContainer *container =VFIO_IOMMU_IOMMUFD (bcontainer); +IOMMUFDBackend *be = container->be; +uint32_t ioas_id = container->ioas_id; +bool need_dirty_sync = false; +Error *local_err =NULL ; +int ret;if (unmap_all) { size =UINT64_MAX ; } -/* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */ -return iommufd_backend_unmap_dma (container->be, - container->ioas_id, iova, size); +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) { +if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && + bcontainer->dirty_pages_supported) { +/* TODO: query dirty bitmap before DMA unmap */ +return iommufd_backend_unmap_dma (be, ioas_id, iova, size); + } + + need_dirty_sync = true; + } + + ret =iommufd_backend_unmap_dma (be, ioas_id, iova, size); +if (ret) { +return ret; + } + +if (need_dirty_sync) { + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + iotlb->translated_addr, + &local_err); +if (ret) { +error_report_err (local_err); +return ret; + } + } + +return 0 ; }static bool iommufd_cdev_kvm_device_add (VFIODevice *vbasedev,Error **errp)
Signature · 1 line
2.52.0
[PULL 09/41] intel_iommu: Introduce a new structure VTDHostIOMMUDevice
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Introduce a new structure VTDHostIOMMUDevice which replaces HostIOMMUDevice to be stored in hash table.
It includes a reference to HostIOMMUDevice and IntelIOMMUState, also includes BDF information which will be used in future patches.
--- hw/i386/intel_iommu_internal.h | 7 +++++++ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +7 -0
@@ -28,6 +28,7 @@#ifndef HW_I386_INTEL_IOMMU_INTERNAL_H #define HW_I386_INTEL_IOMMU_INTERNAL_H #include "hw/i386/intel_iommu.h" +#include "system/host_iommu_device.h" /* * Intel IOMMU register specification
@@ -678,4 +679,10 @@ typedef struct VTDPASIDCacheInfo {/* Bits to decide the offset for each level */ #define VTD_LEVEL_BITS 9 +typedef struct VTDHostIOMMUDevice { +IntelIOMMUState *iommu_state; +PCIBus *bus; +uint8_t devfn; +HostIOMMUDevice *hiod; +}VTDHostIOMMUDevice ;#endif
include/hw/i386/intel_iommu.h +1 -1
@@ -302,7 +302,7 @@ struct IntelIOMMUState {/* list of registered notifiers */ QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers; -GHashTable *vtd_host_iommu_dev;/* HostIOMMUDevice */ +GHashTable *vtd_host_iommu_dev;/* VTDHostIOMMUDevice */ /* interrupt remapping */ bool intr_enabled;/* Whether guest enabled IR */
hw/i386/intel_iommu.c +13 -2
@@ -288,7 +288,10 @@ static gboolean vtd_hiod_equal(gconstpointer v1, gconstpointer v2)static void vtd_hiod_destroy (gpointer v) { -object_unref (v); +VTDHostIOMMUDevice *vtd_hiod = v; + +object_unref (vtd_hiod->hiod); +g_free (vtd_hiod); } static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
@@ -4601,6 +4604,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, HostIOMMUDevice *hiod, Error **errp) {IntelIOMMUState *s = opaque; +VTDHostIOMMUDevice *vtd_hiod;struct vtd_as_key key = { .bus = bus, .devfn = devfn,
@@ -4623,7 +4627,14 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,return false; } + vtd_hiod =g_malloc0 (sizeof (VTDHostIOMMUDevice)); + vtd_hiod->bus = bus; + vtd_hiod->devfn = (uint8_t )devfn; + vtd_hiod->iommu_state = s; + vtd_hiod->hiod = hiod; +if (!vtd_check_hiod (s, hiod, errp)) { +g_free (vtd_hiod);vtd_iommu_unlock (s);return false; }
@@ -4633,7 +4644,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, new_key->devfn = devfn;object_ref (hiod); -g_hash_table_insert (s->vtd_host_iommu_dev, new_key, hiod); +g_hash_table_insert (s->vtd_host_iommu_dev, new_key, vtd_hiod);vtd_iommu_unlock (s);
Signature · 1 line
2.52.0
[PULL 25/41] vfio: Add a backend_flag parameter to vfio_container_query_dirty_bitmap()
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
This new parameter will be used in following patch, currently 0 is passed.
--- include/hw/vfio/vfio-container.h | 8 ++++++-- hw/vfio-user/container.c | 5 +++-- hw/vfio/container-legacy.c | 5 +++-- hw/vfio/container.c | 15 +++++++++------ hw/vfio/iommufd.c | 7 ++++--- hw/vfio/listener.c | 6 +++--- hw/vfio/trace-events | 2 +- 7 files changed, 29 insertions(+), 19 deletions(-)
include/hw/vfio/vfio-container.h +6 -2
@@ -99,7 +99,9 @@ bool vfio_container_devices_dirty_tracking_is_supported( const VFIOContainer *bcontainer);int vfio_container_query_dirty_bitmap (const VFIOContainer *bcontainer,uint64_t iova,uint64_t size, -hwaddr translated_addr,Error **errp); +uint64_t backend_flag, +hwaddr translated_addr, +Error **errp);GList *vfio_container_get_iova_ranges (const VFIOContainer *bcontainer);
@@ -253,12 +255,14 @@ struct VFIOIOMMUClass {* @vbmap: #VFIOBitmap internal bitmap structure * @iova: iova base address * @size: size of iova range +* @backend_flag: flags for backend, opaque to upper layer container * @errp: pointer to Error*, to store an error if it happens. * * Returns zero to indicate success and negative for error. */ int (*query_dirty_bitmap)(const VFIOContainer *bcontainer, -VFIOBitmap *vbmap,hwaddr iova,hwaddr size,Error **errp); +VFIOBitmap *vbmap,hwaddr iova,hwaddr size, +uint64_t backend_flag,Error **errp);/* PCI specific */ int (*pci_hot_reset)(VFIODevice *vbasedev,bool single);
hw/vfio-user/container.c +3 -2
@@ -163,8 +163,9 @@ vfio_user_set_dirty_page_tracking(const VFIOContainer *bcontainer, }static int vfio_user_query_dirty_bitmap (const VFIOContainer *bcontainer, -VFIOBitmap *vbmap,hwaddr iova, -hwaddr size,Error **errp) +VFIOBitmap *vbmap,hwaddr iova, +hwaddr size,uint64_t backend_flag, +Error **errp) {error_setg_errno (errp,ENOTSUP ,"Not supported" );return -ENOTSUP ;
hw/vfio/container-legacy.c +3 -2
@@ -154,7 +154,7 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container, }if (need_dirty_sync) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , iotlb->translated_addr, &local_err);if (ret) {error_report_err (local_err);
@@ -255,7 +255,8 @@ vfio_legacy_set_dirty_page_tracking(const VFIOContainer *bcontainer, }static int vfio_legacy_query_dirty_bitmap (const VFIOContainer *bcontainer, -VFIOBitmap *vbmap,hwaddr iova,hwaddr size,Error **errp) +VFIOBitmap *vbmap,hwaddr iova,hwaddr size, +uint64_t backend_flag,Error **errp) {const VFIOLegacyContainer *container =VFIO_IOMMU_LEGACY (bcontainer);struct vfio_iommu_type1_dirty_bitmap *dbitmap;
hw/vfio/container.c +9 -6
@@ -213,13 +213,13 @@ static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,static int vfio_container_iommu_query_dirty_bitmap (const VFIOContainer *bcontainer,VFIOBitmap *vbmap,hwaddr iova, -hwaddr size,Error **errp) +hwaddr size,uint64_t backend_flag,Error **errp) {VFIOIOMMUClass *vioc =VFIO_IOMMU_GET_CLASS (bcontainer);g_assert (vioc->query_dirty_bitmap);return vioc->query_dirty_bitmap (bcontainer, vbmap, iova, size, - errp); + backend_flag, errp); } static int vfio_container_devices_query_dirty_bitmap(
@@ -247,7 +247,9 @@ static int vfio_container_devices_query_dirty_bitmap(int vfio_container_query_dirty_bitmap (const VFIOContainer *bcontainer,uint64_t iova,uint64_t size, -hwaddr translated_addr,Error **errp) +uint64_t backend_flag, +hwaddr translated_addr, +Error **errp) {bool all_device_dirty_tracking =vfio_container_devices_dirty_tracking_is_supported (bcontainer);
@@ -274,7 +276,7 @@ int vfio_container_query_dirty_bitmap(const VFIOContainer *bcontainer, errp); }else { ret =vfio_container_iommu_query_dirty_bitmap (bcontainer, &vbmap, iova, size, - errp); + backend_flag, errp); }if (ret) {
@@ -285,8 +287,9 @@ int vfio_container_query_dirty_bitmap(const VFIOContainer *bcontainer, translated_addr, vbmap.pages); -trace_vfio_container_query_dirty_bitmap (iova, size, vbmap.size, - translated_addr, dirty_pages); +trace_vfio_container_query_dirty_bitmap (iova, size, backend_flag, + vbmap.size, translated_addr, + dirty_pages); out:g_free (vbmap.bitmap);
hw/vfio/iommufd.c +4 -3
@@ -74,7 +74,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , iotlb->translated_addr, &local_err);if (ret) {
@@ -100,7 +100,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer, }if (need_dirty_sync) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , iotlb->translated_addr, &local_err);if (ret) {
@@ -216,7 +216,8 @@ err:static int iommufd_query_dirty_bitmap (const VFIOContainer *bcontainer,VFIOBitmap *vbmap,hwaddr iova, -hwaddr size,Error **errp) +hwaddr size,uint64_t backend_flag, +Error **errp) {VFIOIOMMUFDContainer *container =VFIO_IOMMU_IOMMUFD (bcontainer);unsigned long page_size =qemu_real_host_page_size ();
hw/vfio/listener.c +3 -3
@@ -1083,7 +1083,7 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) translated_addr =memory_region_get_ram_addr (mr) + xlat; ret =vfio_container_query_dirty_bitmap (bcontainer, iova, iotlb->addr_mask +1 , - translated_addr, &local_err); +0 , translated_addr, &local_err);if (ret) {error_prepend (&local_err, "vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
@@ -1119,7 +1119,7 @@ static int vfio_ram_discard_query_dirty_bitmap(MemoryRegionSection *section,* Sync the whole mapped region (spanning multiple individual mappings) * in one go. */ - ret =vfio_container_query_dirty_bitmap (vrdl->bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (vrdl->bcontainer, iova, size,0 , translated_addr, &local_err);if (ret) {error_report_err (local_err);
@@ -1204,7 +1204,7 @@ static int vfio_sync_dirty_bitmap(VFIOContainer *bcontainer,return vfio_container_query_dirty_bitmap (bcontainer,REAL_HOST_PAGE_ALIGN (section->offset_within_address_space), -int128_get64 (section->size), translated_addr, errp); +int128_get64 (section->size),0 , translated_addr, errp); } static void vfio_listener_log_sync(MemoryListener *listener,
hw/vfio/trace-events +1 -1
@@ -105,7 +105,7 @@ vfio_device_dirty_tracking_start(int nr_ranges, uint64_t min32, uint64_t max32, vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 # container.c -vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t translated_addr, uint64_t dirty_pages) "iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" gpa=0x%"PRIx64" dirty_pages=%"PRIu64 +vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t backend_flag, uint64_t bitmap_size, uint64_t translated_addr, uint64_t dirty_pages) "iova=0x%"PRIx64" size=0x%"PRIx64" backend_flag=0x%"PRIx64" bitmap_size=0x%"PRIx64" gpa=0x%"PRIx64" dirty_pages=%"PRIu64 # container-legacy.c vfio_container_disconnect(int fd) "close container->fd=%d"
Signature · 1 line
2.52.0
[PULL 15/41] intel_iommu_accel: Bind/unbind guest page table to host
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU PGTT configuration.
When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.
--- hw/i386/intel_iommu_accel.h | 7 +++ include/hw/i386/intel_iommu.h | 2 + hw/i386/intel_iommu.c | 22 ++++++- hw/i386/intel_iommu_accel.c | 114 ++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 3 + 5 files changed, 145 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_accel.h +7 -0
@@ -16,6 +16,7 @@bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp);VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as); +bool vtd_propagate_guest_pasid (VTDAddressSpace *vtd_as,Error **errp);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -30,5 +31,11 @@ static inline VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) {return NULL ; } + +static inline bool vtd_propagate_guest_pasid (VTDAddressSpace *vtd_as, +Error **errp) +{ +return true; +}#endif #endif
include/hw/i386/intel_iommu.h +2 -0
@@ -154,6 +154,8 @@ struct VTDAddressSpace {* with the guest IOMMU pgtables for a device. */ IOVATree *iova_tree; + +uint32_t fs_hwpt_id; };struct VTDIOTLBEntry {
hw/i386/intel_iommu.c +19 -3
@@ -87,7 +87,11 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)g_hash_table_iter_init (&as_it, s->vtd_address_spaces);while (g_hash_table_iter_next (&as_it,NULL , (void **)&vtd_as)) {VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry; - pc_entry->valid = false; +if (pc_entry->valid) { + pc_entry->valid = false; +/* It's fatal to get failure during reset */ +vtd_propagate_guest_pasid (vtd_as, &error_fatal); + } } }
@@ -3073,6 +3077,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,VTDPASIDEntry pe;IOMMUNotifier *n;uint16_t did; +const char *err_prefix ="Attaching to HWPT failed: " ; +Error *local_err =NULL ;if (vtd_dev_get_pe_from_pasid (vtd_as, &pe)) {if (!pc_entry->valid) {
@@ -3093,7 +3099,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,vtd_address_space_unmap (vtd_as, n); }vtd_switch_address_space (vtd_as); -return ; + + err_prefix ="Detaching from HWPT failed: " ; + goto do_bind_unbind; }/*
@@ -3121,12 +3129,20 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,if (!pc_entry->valid) { pc_entry->pasid_entry = pe; pc_entry->valid = true; - }else if (!vtd_pasid_entry_compare (&pe, &pc_entry->pasid_entry)) { + }else if (vtd_pasid_entry_compare (&pe, &pc_entry->pasid_entry)) { + err_prefix ="Replacing HWPT attachment failed: " ; + }else {return ; }vtd_switch_address_space (vtd_as);vtd_address_space_sync (vtd_as); + +do_bind_unbind: +/* TODO: Fault event injection into guest, report error to QEMU for now */ +if (!vtd_propagate_guest_pasid (vtd_as, &local_err)) { +error_reportf_err (local_err,"%s" , err_prefix); + } }static void vtd_pasid_cache_sync (IntelIOMMUState *s,VTDPASIDCacheInfo *pc_info)
hw/i386/intel_iommu_accel.c +114 -0
123 lines not on this page · raw message
hw/i386/trace-events +3 -0
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)" vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)" vtd_reset_exit(void) "" +vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" +vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" +vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
Signature · 1 line
2.52.0
[PULL 29/41] vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
With default config, kernel VFIO IOMMU type1 driver limits dirty bitmap to 256MB for unmap_bitmap ioctl so the maximum guest memory region is no more than 8TB size for the ioctl to succeed.
Be conservative here to limit total guest memory to max value supported by unmap_bitmap ioctl or else add a migration blocker. IOMMUFD backend doesn't have such limit, one can use it if there is a need to migrate such large VM.
--- hw/vfio/migration.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
hw/vfio/migration.c +34 -0
@@ -16,6 +16,7 @@#include <sys/ioctl.h> #include "system/runstate.h" +#include "hw/core/boards.h" #include "hw/vfio/vfio-device.h" #include "hw/vfio/vfio-migration.h" #include "migration/misc.h"
@@ -1152,6 +1153,32 @@ static bool vfio_viommu_preset(VFIODevice *vbasedev)return vbasedev->bcontainer->space->as != &address_space_memory; } +static bool vfio_dirty_tracking_exceed_limit (VFIODevice *vbasedev) +{ +VFIOContainer *bcontainer = vbasedev->bcontainer; +uint64_t max_size, page_size; + +if (!bcontainer->dirty_pages_supported) { +return false; + } + +/* +* VFIO IOMMU type1 driver has limitation of bitmap size on unmap_bitmap +* ioctl(), calculate the limit and compare with guest memory size to +* catch dirty tracking failure early. +* +* This limit is 8TB with default kernel and QEMU config, we are a bit +* conservative here as VM memory layout may be nonconsecutive or VM +* can run with vIOMMU enabled so the limitation could be relaxed. One +* can also switch to use IOMMUFD backend if there is a need to migrate +* large VM. +*/ + page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size; + +return current_machine->ram_size > max_size; +} +/* * Return true when either migration initialized or blocker registered. * Currently only return false when adding blocker fails which will
@@ -1193,6 +1220,13 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp) goto add_blocker; } +if (vfio_dirty_tracking_exceed_limit (vbasedev)) { +error_setg (&err,"%s: Migration is currently not supported with " +"large memory VM due to dirty tracking limitation in " +"backend" , vbasedev->name); + goto add_blocker; + } +warn_report ("%s: VFIO device doesn't support device and " "IOMMU dirty tracking" , vbasedev->name); }
Signature · 1 line
2.52.0
[PULL 31/41] hw/pci: Introduce pci_device_get_host_iommu_quirks()
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to retrieve this raw data in VFIO core.
Introduce a new PCIIOMMUOps optional callback, get_host_iommu_quirk() which allows to retrieve the vendor specific hardware information data and convert it into bitmaps defined with enum host_iommu_quirks.
pci_device_get_host_iommu_quirks() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
--- include/hw/core/iommu.h | 5 +++++ include/hw/pci/pci.h | 31 +++++++++++++++++++++++++++++++ hw/pci/pci.c | 12 ++++++++++++ 3 files changed, 48 insertions(+)
include/hw/core/iommu.h +5 -0
@@ -22,4 +22,9 @@ enum viommu_flags { VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0), }; +/* Host IOMMU quirks. Extracted from host IOMMU capabilities */ +enum host_iommu_quirks { +HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO =BIT_ULL (0 ), +}; +#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +31 -0
@@ -474,6 +474,23 @@ typedef struct PCIIOMMUOps {* enum viommu_flags. */ uint64_t (*get_viommu_flags)(void *opaque); +/** +* @get_host_iommu_quirks: get host IOMMU quirks +* +* Optional callback, if not implemented, then vIOMMU doesn't support +* converting @type specific hardware information data into a standard +* bitmap format. +* +* @type: IOMMU hardware info type +* +* @caps: IOMMU @type specific hardware information data +* +* @size: size of @caps +* +* Returns: bitmap with each bit representing a host IOMMU quirk defined in +* enum host_iommu_quirks +*/ +uint64_t (*get_host_iommu_quirks)(uint32_t type,void *caps,uint32_t size);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. *
@@ -666,6 +683,20 @@ void pci_device_unset_iommu_device(PCIDevice *dev);*/ uint64_t pci_device_get_viommu_flags (PCIDevice *dev); +/** +* pci_device_get_host_iommu_quirks: get host IOMMU quirks. +* +* Returns: bitmap with each bit representing a host IOMMU quirk defined in +* enum host_iommu_quirks. Or 0 if vIOMMU doesn't convert any. +* +* @dev: PCI device pointer. +* @type: IOMMU hardware info type +* @caps: IOMMU @type specific hardware information data +* @size: size of @caps +*/ +uint64_t pci_device_get_host_iommu_quirks (PCIDevice *dev,uint32_t type, +void *caps,uint32_t size); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB.
hw/pci/pci.c +12 -0
@@ -3032,6 +3032,18 @@ uint64_t pci_device_get_viommu_flags(PCIDevice *dev)return 0 ; } +uint64_t pci_device_get_host_iommu_quirks (PCIDevice *dev,uint32_t type, +void *caps,uint32_t size) +{ +PCIBus *iommu_bus; + +pci_device_get_iommu_bus_devfn (dev, &iommu_bus,NULL ,NULL ); +if (iommu_bus && iommu_bus->iommu_ops->get_host_iommu_quirks) { +return iommu_bus->iommu_ops->get_host_iommu_quirks (type, caps, size); + } +return 0 ; +} +int pci_pri_request_page (PCIDevice *dev,uint32_t pasid,bool priv_req,bool exec_req,hwaddr addr,bool lpig,uint16_t prgi,bool is_read,bool is_write)
Signature · 1 line
2.52.0
[PULL 17/41] intel_iommu: Replay all pasid bindings when either SRTP or TE bit is changed
In reply to Cédric Le GoaterFrom: Yi Liu <[email protected]>
When either 'Set Root Table Pointer' or 'Translation Enable' bit is changed, all pasid bindings on host side become stale and need to be updated.
Introduce a helper function vtd_replay_pasid_bindings_all() to go through all pasid entries in all passthrough devices to update host side bindings.
--- hw/i386/intel_iommu.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
hw/i386/intel_iommu.c +10 -0
@@ -76,6 +76,7 @@ struct vtd_iotlb_key {static void vtd_address_space_refresh_all (IntelIOMMUState *s);static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n); +static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s);static void vtd_pasid_cache_reset_locked (IntelIOMMUState *s) {
@@ -2629,6 +2630,7 @@ static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)vtd_set_clear_mask_long (s,DMAR_GSTS_REG ,0 ,VTD_GSTS_RTPS );vtd_reset_caches (s);vtd_address_space_refresh_all (s); +vtd_replay_pasid_bindings_all (s); }/* Set Interrupt Remap Table Pointer */
@@ -2663,6 +2665,7 @@ static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)vtd_reset_caches (s);vtd_address_space_refresh_all (s); +vtd_replay_pasid_bindings_all (s); }/* Handle Interrupt Remap Enable/Disable */
@@ -3162,6 +3165,13 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)vtd_iommu_unlock (s); } +static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s) +{ +VTDPASIDCacheInfo pc_info = { .type =VTD_INV_DESC_PASIDC_G_GLOBAL }; + +vtd_pasid_cache_sync (s, &pc_info); +} +static bool vtd_process_pasid_desc (IntelIOMMUState *s,VTDInvDesc *inv_desc) {
Signature · 1 line
2.52.0
[PULL 26/41] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the last dirty bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.
--- include/system/iommufd.h | 2 +- backends/iommufd.c | 5 +++-- hw/vfio/iommufd.c | 5 +++-- backends/trace-events | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-)
include/system/iommufd.h +1 -1
@@ -64,7 +64,7 @@ bool iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,bool iommufd_backend_get_dirty_bitmap (IOMMUFDBackend *be,uint32_t hwpt_id,uint64_t iova,ram_addr_t size,uint64_t page_size,uint64_t *data, -Error **errp); +uint64_t flags,Error **errp); bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id, uint32_t data_type, uint32_t entry_len, uint32_t *entry_num, void *data,
backends/iommufd.c +3 -2
@@ -361,7 +361,7 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,uint32_t hwpt_id, uint64_t iova, ram_addr_t size, uint64_t page_size, uint64_t *data, - Error **errp) + uint64_t flags, Error **errp) { int ret; struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {
@@ -371,11 +371,12 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, .length = size, .page_size = page_size, .data = (uintptr_t)data, + .flags = flags, }; ret =ioctl (be->fd,IOMMU_HWPT_GET_DIRTY_BITMAP , &get_dirty_bitmap);trace_iommufd_backend_get_dirty_bitmap (be->fd, hwpt_id, iova, size, - page_size, ret ? errno :0 ); + flags, page_size, ret ? errno :0 );if (ret) {error_setg_errno (errp, errno, "IOMMU_HWPT_GET_DIRTY_BITMAP (iova: 0x%"HWADDR_PRIx
hw/vfio/iommufd.c +3 -2
@@ -74,7 +74,8 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, +IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR , iotlb->translated_addr, &local_err);if (ret) {
@@ -231,7 +232,7 @@ static int iommufd_query_dirty_bitmap(const VFIOContainer *bcontainer,if (!iommufd_backend_get_dirty_bitmap (container->be, hwpt->hwpt_id, iova, size, page_size, (uint64_t *)vbmap->bitmap, - errp)) { + backend_flag, errp)) {return -EINVAL ; } }
backends/trace-events +1 -1
@@ -19,5 +19,5 @@ iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas) " iommufd=%d ioas=%d" iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_t flags, uint32_t hwpt_type, uint32_t len, uint64_t data_ptr, uint32_t out_hwpt_id, int ret) " iommufd=%d dev_id=%u pt_id=%u flags=0x%x hwpt_type=%u len=%u data_ptr=0x%"PRIx64" out_hwpt=%u (%d)" iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d id=%d (%d)" iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret) " iommufd=%d hwpt=%u enable=%d (%d)" -iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" page_size=0x%"PRIx64" (%d)" +iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t flags, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" flags=0x%"PRIx64" page_size=0x%"PRIx64" (%d)" iommufd_backend_invalidate_cache(int iommufd, uint32_t id, uint32_t data_type, uint32_t entry_len, uint32_t entry_num, uint32_t done_num, uint64_t data_ptr, int ret) " iommufd=%d id=%u data_type=%u entry_len=%u entry_num=%u done_num=%u data_ptr=0x%"PRIx64" (%d)"
Signature · 1 line
2.52.0
[PULL 34/41] Workaround for ERRATA_772415_SPR17
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
On a system influenced by ERRATA_772415, IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 is repored by IOMMU_DEVICE_GET_HW_INFO. Due to this errata, even the readonly range mapped on second stage page table could still be written.
Reference from 4th Gen Intel Xeon Processor Scalable Family Specification
Update, Errata Details, SPR17.
Link https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/
Backup https://cdrdv2.intel.com/v1/dl/getContent/772415
Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.
Implication: Due to this erratum, pages mapped as Read-only in second-stage page-tables may be modified by remapping hardware Access/Dirty bit updates.
Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."
Introduce a helper vfio_device_get_host_iommu_quirk_bypass_ro to check if readonly mappings should be bypassed.
--- docs/devel/vfio-iommufd.rst | 9 +++++++++ include/hw/vfio/vfio-container.h | 1 + include/hw/vfio/vfio-device.h | 3 +++ hw/vfio/device.c | 14 ++++++++++++++ hw/vfio/iommufd.c | 9 ++++++++- hw/vfio/listener.c | 6 ++++-- 6 files changed, 39 insertions(+), 3 deletions(-)
docs/devel/vfio-iommufd.rst +9 -0
@@ -169,3 +169,12 @@ otherwise below error shows: .. code-block:: none qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0,bus=bridge1,iommufd=iommufd0: vfio 0000:02:00.0: Failed to set vIOMMU: Host device downstream to a PCI bridge is unsupported when x-flts=on + +If host IOMMU has ERRATA_772415_SPR17, running guest with "intel_iommu=on,sm_off" +is unsupported, kexec or reboot guest from "intel_iommu=on,sm_on" to +"intel_iommu=on,sm_off" is also unsupported. Configure scalable mode off as +below if it's not needed by guest: + +.. code-block:: bash + + -device intel-iommu,x-scalable-mode=off
include/hw/vfio/vfio-container.h +1 -0
@@ -52,6 +52,7 @@ struct VFIOContainer { QLIST_HEAD(, VFIODevice) device_list;GList *iova_ranges;NotifierWithReturn cpr_reboot_notifier; +bool bypass_ro; };#define TYPE_VFIO_IOMMU "vfio-iommu"
include/hw/vfio/vfio-device.h +3 -0
@@ -268,6 +268,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,void vfio_device_unprepare (VFIODevice *vbasedev);bool vfio_device_get_viommu_flags_want_nesting (VFIODevice *vbasedev); +bool vfio_device_get_host_iommu_quirk_bypass_ro (VFIODevice *vbasedev, +uint32_t type,void *caps, +uint32_t size);int vfio_device_get_region_info (VFIODevice *vbasedev,int index,struct vfio_region_info **info);
hw/vfio/device.c +14 -0
@@ -533,6 +533,20 @@ bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev)return false; } +bool vfio_device_get_host_iommu_quirk_bypass_ro (VFIODevice *vbasedev, +uint32_t type,void *caps, +uint32_t size) +{ +VFIOPCIDevice *vdev =vfio_pci_from_vfio_device (vbasedev); + +if (vdev) { +return !!(pci_device_get_host_iommu_quirks (PCI_DEVICE (vdev), type, + caps, size) & +HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO ); + } +return false; +} +/* * Traditional ioctl() based io */
hw/vfio/iommufd.c +8 -1
@@ -351,6 +351,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,VFIOContainer *bcontainer =VFIO_IOMMU (container);uint32_t type, flags =0 ;uint64_t hw_caps; +VendorCaps caps;VFIOIOASHwpt *hwpt;uint32_t hwpt_id;int ret;
@@ -396,7 +397,8 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,* instead. */ if (!iommufd_backend_get_device_info (vbasedev->iommufd, vbasedev->devid, - &type,NULL ,0 , &hw_caps, errp)) { + &type, &caps,sizeof (caps), &hw_caps, + errp)) {return false; }
@@ -411,6 +413,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,*/ if (vfio_device_get_viommu_flags_want_nesting (vbasedev)) { flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + +if (vfio_device_get_host_iommu_quirk_bypass_ro (vbasedev, type, + &caps,sizeof (caps))) { + bcontainer->bypass_ro = true; + } }if (cpr_is_incoming ()) {
hw/vfio/listener.c +4 -2
@@ -502,7 +502,8 @@ void vfio_container_region_add(VFIOContainer *bcontainer,int ret;Error *err =NULL ; -if (!vfio_listener_valid_section (section, false,"region_add" )) { +if (!vfio_listener_valid_section (section, bcontainer->bypass_ro, +"region_add" )) {return ; }
@@ -668,7 +669,8 @@ static void vfio_listener_region_del(MemoryListener *listener,int ret;bool try_unmap = true; -if (!vfio_listener_valid_section (section, false,"region_del" )) { +if (!vfio_listener_valid_section (section, bcontainer->bypass_ro, +"region_del" )) {return ; }
Signature · 1 line
2.52.0
[PULL 23/41] vfio/iommufd: Query dirty bitmap before DMA unmap
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
When an existing mapping is unmapped, there could already be dirty bits which need to be recorded before unmap.
If query dirty bitmap fails, we still need to do unmapping or else there is stale mapping and it's risky to guest.
--- hw/vfio/iommufd.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
hw/vfio/iommufd.c +16 -3
@@ -65,7 +65,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,uint32_t ioas_id = container->ioas_id;bool need_dirty_sync = false;Error *local_err =NULL ; -int ret; +int ret, unmap_ret;if (unmap_all) { size =UINT64_MAX ;
@@ -74,8 +74,21 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { -/* TODO: query dirty bitmap before DMA unmap */ -return iommufd_backend_unmap_dma (be, ioas_id, iova, size); + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + iotlb->translated_addr, + &local_err); +if (ret) { +error_report_err (local_err); + } +/* Unmap stale mapping even if query dirty bitmap fails */ + unmap_ret =iommufd_backend_unmap_dma (be, ioas_id, iova, size); + +/* +* If dirty tracking fails, return the failure to VFIO core to +* fail the migration, or else there will be dirty pages missed +* to be migrated. +*/ +return unmap_ret ? : ret; } need_dirty_sync = true;
Signature · 1 line
2.52.0
[PULL 35/41] util/vfio-helper: Fix endianness in PCI config read/write functions
In reply to Cédric Le GoaterFrom: Farhan Ali <[email protected]>
The VFIO pread/pwrite functions use little-endian data format. Currently, the qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config() don't correctly convert from CPU native endian format to little-endian (and vice versa) when using the pread/pwrite functions. Fix this by limiting read/write to 32 bits and handling endian conversion in qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config().
--- util/vfio-helpers.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
util/vfio-helpers.c +10 -5
@@ -233,31 +233,36 @@ int qemu_vfio_pci_init_irq(QEMUVFIOState *s, EventNotifier *e,return 0 ; } -static int qemu_vfio_pci_read_config (QEMUVFIOState *s,void *buf, +static int qemu_vfio_pci_read_config (QEMUVFIOState *s,uint32_t *buf,int size,int ofs) {int ret; +uint32_t val_le;trace_qemu_vfio_pci_read_config (buf, ofs, size, s->config_region_info.offset, s->config_region_info.size);assert (QEMU_IS_ALIGNED (s->config_region_info.offset + ofs, size)); ret =RETRY_ON_EINTR ( -pread (s->device, buf, size, s->config_region_info.offset + ofs) +pread (s->device, &val_le, size, s->config_region_info.offset + ofs) ); + + *buf =le32_to_cpu (val_le);return ret == size ?0 : -errno; } -static int qemu_vfio_pci_write_config (QEMUVFIOState *s,void *buf,int size,int ofs) +static int qemu_vfio_pci_write_config (QEMUVFIOState *s,uint32_t *buf,int size,int ofs) {int ret; +uint32_t val_le; + val_le =cpu_to_le32 (*buf);trace_qemu_vfio_pci_write_config (buf, ofs, size, s->config_region_info.offset, s->config_region_info.size);assert (QEMU_IS_ALIGNED (s->config_region_info.offset + ofs, size)); ret =RETRY_ON_EINTR ( -pwrite (s->device, buf, size, s->config_region_info.offset + ofs) +pwrite (s->device, &val_le, size, s->config_region_info.offset + ofs) );return ret == size ?0 : -errno; }
@@ -296,7 +301,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, const char *device, {int ret;int i; -uint16_t pci_cmd; +uint32_t pci_cmd;struct vfio_group_status group_status = { .argsz =sizeof (group_status) };struct vfio_iommu_type1_info *iommu_info =NULL ;size_t iommu_info_size =sizeof (*iommu_info);
Signature · 1 line
2.52.0
[PULL 24/41] vfio/container-legacy: rename vfio_dma_unmap_bitmap() to vfio_legacy_dma_unmap_get_dirty_bitmap()
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
This is to follow naming style in container-legacy.c to have low level functions with vfio_legacy_ prefix.
No functional changes.
--- hw/vfio/container-legacy.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio/container-legacy.c +6 -4
@@ -68,9 +68,10 @@ static int vfio_ram_block_discard_disable(VFIOLegacyContainer *container, } } -static int vfio_dma_unmap_bitmap (const VFIOLegacyContainer *container, -hwaddr iova,uint64_t size, -IOMMUTLBEntry *iotlb) +static int +vfio_legacy_dma_unmap_get_dirty_bitmap (const VFIOLegacyContainer *container, +hwaddr iova,uint64_t size, +IOMMUTLBEntry *iotlb) {const VFIOContainer *bcontainer =VFIO_IOMMU (container);struct vfio_iommu_type1_dma_unmap *unmap;
@@ -141,7 +142,8 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { -return vfio_dma_unmap_bitmap (container, iova, size, iotlb); +return vfio_legacy_dma_unmap_get_dirty_bitmap (container, iova, size, + iotlb); } need_dirty_sync = true;
Signature · 1 line
2.52.0
[PULL 30/41] vfio/migration: Allow live migration with vIOMMU without VFs using device dirty tracking
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Commit e46883204c38 ("vfio/migration: Block migration with vIOMMU") introduces a migration blocker when vIOMMU is enabled, because we need to calculate the IOVA ranges for device dirty tracking. But this is unnecessary for iommu dirty tracking.
Limit the vfio_viommu_preset() check to those devices which use device dirty tracking. This allows live migration with VFIO devices which use iommu dirty tracking.
--- include/hw/vfio/vfio-device.h | 10 ++++++++++ hw/vfio/container.c | 5 +---- hw/vfio/device.c | 6 ++++++ hw/vfio/migration.c | 6 +++--- 4 files changed, 20 insertions(+), 7 deletions(-)
include/hw/vfio/vfio-device.h +10 -0
@@ -148,6 +148,16 @@ bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindexvoid vfio_device_reset_handler (void *opaque);bool vfio_device_is_mdev (VFIODevice *vbasedev); +/** +* vfio_device_dirty_pages_disabled: Check if device dirty tracking will be +* used for a VFIO device +* +* @vbasedev: The VFIODevice to transform +* +* Return: true if either @vbasedev doesn't support device dirty tracking or +* is forcedly disabled from command line, otherwise false. +*/ +bool vfio_device_dirty_pages_disabled (VFIODevice *vbasedev);bool vfio_device_hiod_create_and_realize (VFIODevice *vbasedev,const char *typename,Error **errp); bool vfio_device_attach(char *name, VFIODevice *vbasedev,
hw/vfio/container.c +1 -4
@@ -178,10 +178,7 @@ bool vfio_container_devices_dirty_tracking_is_supported(VFIODevice *vbasedev; QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) { -if (vbasedev->device_dirty_page_tracking ==ON_OFF_AUTO_OFF ) { -return false; - } -if (!vbasedev->dirty_pages_supported) { +if (vfio_device_dirty_pages_disabled (vbasedev)) {return false; } }
hw/vfio/device.c +6 -0
@@ -412,6 +412,12 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev)return subsys && (strcmp (subsys,"/sys/bus/mdev" ) ==0 ); } +bool vfio_device_dirty_pages_disabled (VFIODevice *vbasedev) +{ +return (!vbasedev->dirty_pages_supported || + vbasedev->device_dirty_page_tracking ==ON_OFF_AUTO_OFF ); +} +bool vfio_device_hiod_create_and_realize (VFIODevice *vbasedev,const char *typename,Error **errp) {
hw/vfio/migration.c +3 -3
@@ -1210,8 +1210,7 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)return !vfio_block_migration (vbasedev, err, errp); } -if ((!vbasedev->dirty_pages_supported || - vbasedev->device_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && +if (vfio_device_dirty_pages_disabled (vbasedev) && !vbasedev->iommu_dirty_tracking) {if (vbasedev->enable_migration ==ON_OFF_AUTO_AUTO ) { error_setg(&err,
@@ -1236,7 +1235,8 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp) goto out_deinit; } -if (vfio_viommu_preset (vbasedev)) { +if (!vfio_device_dirty_pages_disabled (vbasedev) && +vfio_viommu_preset (vbasedev)) {error_setg (&err,"%s: Migration is currently not supported " "with vIOMMU enabled" , vbasedev->name); goto add_blocker;
Signature · 1 line
2.52.0
[PULL 40/41] tests/qtest: Fix build error
In reply to Cédric Le GoaterNewer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC)) detects an unused variable error:
../tests/qtest/libqtest.c: In function ‘qtest_qom_has_concrete_type’:
../tests/qtest/libqtest.c:1044:9: error: variable ‘idx’ set but not used [-Werror=unused-but-set-variable=]
Remove idx.
--- tests/qtest/libqtest.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tests/qtest/libqtest.c +1 -2
@@ -1042,7 +1042,6 @@ static bool qtest_qom_has_concrete_type(const char *parent_typename,QObject *qobj;QString *qstr;QDict *devinfo; -int idx;if (!list) {QDict *resp;
@@ -1067,7 +1066,7 @@ static bool qtest_qom_has_concrete_type(const char *parent_typename, } } -for (p =qlist_first (list), idx =0 ; p; p =qlist_next (p), idx++) { +for (p =qlist_first (list); p; p =qlist_next (p)) { devinfo =qobject_to (QDict,qlist_entry_obj (p));g_assert (devinfo);
Signature · 1 line
2.52.0
[PULL 27/41] intel_iommu: Fix unmap_bitmap failure with legacy VFIO backend
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from IOMMU domain to block domain, vtd_address_space_unmap() is called to unmap whole address space.
If that happens during migration, migration fails with legacy VFIO backend as below:
Status: failed (vfio_container_dma_unmap(0x561bbbd92d90, 0x100000000000, 0x100000000000) = -7 (Argument list too long))
Because legacy VFIO limits maximum bitmap size to 256MB which maps to 8TB on 4K page system, when 16TB sized UNMAP notification is sent, unmap_bitmap ioctl fails. Normally such large UNMAP notification come from IOVA range rather than system memory.
Apart from that, vtd_address_space_unmap() sends UNMAP notification with translated_addr = 0, because there is no valid translated_addr for unmapping a whole iommu memory region. This breaks dirty tracking no matter which VFIO backend is used.
Fix them all by iterating over DMAMap list to unmap each range with active mapping when global_dirty_tracking is active. global_dirty_tracking is protected by BQL, so it's safe to reference it directly. If it's not active, unmapping the whole address space in one go is optimal.
--- hw/i386/intel_iommu.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +42 -0
@@ -4764,6 +4764,43 @@ static uint64_t vtd_get_viommu_flags(void *opaque)return flags; } +/* +* There is no valid translated_addr for unmapping a whole iommu memory region. +* When dirty tracking is enabled, we need it to set dirty bitmaps. Iterate +* over DMAMap list to unmap each range with active mapping and translated_addr +* value. +*/ +static void vtd_address_space_unmap_in_dirty_tracking (VTDAddressSpace *as, +IOMMUNotifier *n) +{ +const DMAMap *map; +const DMAMap target = { + .iova = n->start, + .size = n->end, + }; +IOVATree *tree = as->iova_tree; + +/* +* DMAMap is created during IOMMU page table sync, it's either 4KB or huge +* page size and always a power of 2 in size. So the range of DMAMap could +* be used for UNMAP notification directly. +*/ +while ((map =iova_tree_find (tree, &target))) { +IOMMUTLBEvent event; + + event.type =IOMMU_NOTIFIER_UNMAP ; + event.entry.iova = map->iova; + event.entry.addr_mask = map->size; + event.entry.target_as = &address_space_memory; + event.entry.perm =IOMMU_NONE ; +/* This field is needed to set dirty bigmap */ + event.entry.translated_addr = map->translated_addr; +memory_region_notify_iommu_one (n, &event); + +iova_tree_remove (tree, *map); + } +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {
@@ -4773,6 +4810,11 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)IntelIOMMUState *s = as->iommu_state;DMAMap map; +if (global_dirty_tracking) { +vtd_address_space_unmap_in_dirty_tracking (as, n); +return ; + } +/* * Note: all the codes in this function has a assumption that IOVA * bits are no more than VTD_MGAW bits (which is restricted by
Signature · 1 line
2.52.0
[PULL 38/41] ppc/vof: Fix build error
In reply to Cédric Le GoaterNewer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC)) detects an unused variable error:
../hw/ppc/vof.c: In function ‘vof_dt_memory_available’:../hw/ppc/vof.c:642:12: error: variable ‘n’ set but not used [-Werror=unused-but-set-variable=]
Remove 'n'.
--- hw/ppc/vof.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
hw/ppc/vof.c +2 -3
@@ -639,7 +639,7 @@ static gint of_claimed_compare_func(gconstpointer a, gconstpointer b)static void vof_dt_memory_available (void *fdt,GArray *claimed,uint64_t base) { -int i, n, offset, proplen =0 , sc, ac; +int i, offset, proplen =0 , sc, ac;target_ulong mem0_end;const uint8_t *mem0_reg; g_autofree uint8_t *avail = NULL;
@@ -677,7 +677,7 @@ static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base)g_assert (claimed->len && (g_array_index (claimed, OfClaimed,0 ).start ==0 )); avail =g_malloc0 (sizeof (uint32_t ) * (ac + sc) * claimed->len); -for (i =0 , n =0 , availcur = avail; i < claimed->len; ++i) { +for (i =0 , availcur = avail; i < claimed->len; ++i) {OfClaimed c =g_array_index (claimed, OfClaimed, i);uint64_t start, size;
@@ -705,7 +705,6 @@ static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base)if (size) {trace_vof_avail (c.start + c.size, c.start + c.size + size, size); - ++n; } }_FDT ((fdt_setprop (fdt, offset,"available" , avail, availcur - avail)));
Signature · 1 line
2.52.0
[PULL 28/41] vfio/listener: Add missing dirty tracking in region_del
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a NULL iotlb entry to unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.
By constructing an iotlb entry with iova = gpa for unmap_bitmap, it can set dirty bits correctly.
For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g., vtd_address_space_unmap_in_dirty_tracking().
--- hw/vfio/listener.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
hw/vfio/listener.c +21 -1
@@ -713,14 +713,34 @@ static void vfio_listener_region_del(MemoryListener *listener,if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking when switch out of PT domain. In fact, in unmap_bitmap, +* only translated_addr field is used to set dirty bitmap. +* +* Note: When switch into PT domain from DMA domain, the whole IOMMU +* MR is deleted without iotlb, before that happen, we depend on +* vIOMMU to send unmap notification with accurate iotlb entry to +* VFIO. See vtd_address_space_unmap_in_dirty_tracking() for example, +* it is triggered during switching to block domain because vtd does +* not support direct switching from DMA to PT domain. +*/ +if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova; + iotlb = &entry; + } + ret =vfio_container_dma_unmap (bcontainer, iova,int128_get64 (llsize), -NULL , unmap_all); + iotlb, unmap_all);if (ret) { error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") = %d (%s)",
Signature · 1 line
2.52.0
Re: [PULL 28/41] vfio/listener: Add missing dirty tracking in region_del
Hello Zhenzhong,
Quoted 44 lines · Cédric Le Goater
From: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a NULL iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_dirty_tracking().Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>Reviewed-by Yi Liu <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/listener.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)hw/vfio/listener.c +7
-0@@ -713,14 +713,34 @@ static void vfio_listener_region_del(MemoryListener *listener,if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking when switch out of PT domain. In fact, in unmap_bitmap, +* only translated_addr field is used to set dirty bitmap. +*
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 738
Quoted 10 lines
hw/vfio/listener.c +10
-0+* Note: When switch into PT domain from DMA domain, the whole IOMMU +* MR is deleted without iotlb, before that happen, we depend on +* vIOMMU to send unmap notification with accurate iotlb entry to +* VFIO. See vtd_address_space_unmap_in_dirty_tracking() for example, +* it is triggered during switching to block domain because vtd does +* not support direct switching from DMA to PT domain. +*/ +if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova;
In an experiment involving a nested setup with L1 + intel_iommu, an L2 VM with an assigned igb VF can trigger the following QEMU segv :
bitmap_set_atomic(map=NULL, start=1, nr=1) physical_memory_set_dirty_range(start=0x380004040000, length=4096) physical_memory_set_dirty_lebitmap(start=0x380004040000, pages=3) vfio_container_query_dirty_bitmap(translated_addr=0x380004040000) vfio_legacy_dma_unmap_one(iova=0x380004040000, size=12288) vfio_listener_region_del()
The VT-d interrupt-remapping table is mapped pretty high and translated_addr ends up as a very large value. It seems that the correct value should be :
translated_addr = memory_region_get_ram_addr(section->mr) +
section->offset_within_region;
as found in vfio_iommu_map_dirty_notify() and
and vfio_ram_discard_query_dirty_bitmap().
Is that correct ?
Thanks,
C.
Quoted 9 lines
hw/vfio/listener.c +4
-1+ iotlb = &entry; + } + ret =vfio_container_dma_unmap (bcontainer, iova,int128_get64 (llsize), -NULL , unmap_all); + iotlb, unmap_all);if (ret) { error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") = %d (%s)",
RE: [PULL 28/41] vfio/listener: Add missing dirty tracking in region_del
Hi Cédric,
Quoted 71 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PULL 28/41] vfio/listener: Add missing dirty tracking inregion_delHello Zhenzhong,
Quoted 10 lines · Cédric Le Goater
From: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a NULL iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.Quoted 8 lines
For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_dirty_tracking().Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>Reviewed-by Yi Liu <[email protected]>Quoted 7 lines
Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/listener.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0index
62699cb772d786c1510318dff73973ef4d297177..813621f22f8b5ec284388f9c5f719525ec5f282c100644hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener
Quoted 3 lines
hw/vfio/listener.c +0
-0@@ -713,14 +713,34 @@ static void vfio_listener_region_del(MemoryListener*listener,
Quoted 7 lines
hw/vfio/listener.c +1
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova);hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener · line 728
Quoted 10 lines
hw/vfio/listener.c +7
-0unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking when switch out of PT domain. In fact, in unmap_bitmap, +* only translated_addr field is used to set dirty bitmap. +* +* Note: When switch into PT domain from DMA domain, the whole IOMMU
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener · line 737
Quoted 9 lines
hw/vfio/listener.c +9
-0+* MR is deleted without iotlb, before that happen, we depend on +* vIOMMU to send unmap notification with accurate iotlb entry to +* VFIO. See vtd_address_space_unmap_in_dirty_tracking() for example, +* it is triggered during switching to block domain because vtd does +* not support direct switching from DMA to PT domain. +*/ +if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova;In an experiment involving a nested setup with L1 +
intel_iommu, an L2 VM with an assigned igb VF can trigger the following QEMU segv :
Quoted 6 lines
bitmap_set_atomic(map=NULL, start=1, nr=1)physical_memory_set_dirty_range(start=0x380004040000, length=4096)physical_memory_set_dirty_lebitmap(start=0x380004040000, pages=3)vfio_container_query_dirty_bitmap(translated_addr=0x380004040000)vfio_legacy_dma_unmap_one(iova=0x380004040000, size=12288)vfio_listener_region_del()
Hmm, looks a range in PCI space, I suspect it's a PCI bar but it has 3 pages. Is igb VF a real hw passthrough to L1, then L1 pass it to L2? Seems L2 crash, not L1?
Quoted 5 lines
The VT-d interrupt-remapping table is mapped pretty high and
translated_addrends up as a very large value. It seems that the correct value should be :
Quoted 7 lines
translated_addr=memory_region_get_ram_addr(section->mr)+section->offset_within_region;as found in
vfio_iommu_map_dirty_notify()and
andvfio_ram_discard_query_dirty_bitmap().Is that correct ?
Good catch, it looks we lack a conversion from iotlb->translated_addr to dirty_block offset just like in vfio_iommu_map_dirty_notify(). iotlb->translated_addr is designed to hold translation result from vIOMMU. How about below(untested)?
hw/vfio/container-legacy.c +16 -5
@@ -71,7 +71,7 @@ static int vfio_ram_block_discard_disable(VFIOLegacyContainer *container,static int vfio_legacy_dma_unmap_get_dirty_bitmap (const VFIOLegacyContainer *container,hwaddr iova,uint64_t size, -IOMMUTLBEntry *iotlb) +hwaddr translated_addr) {const VFIOContainer *bcontainer =VFIO_IOMMU (container);struct vfio_iommu_type1_dma_unmap *unmap;
@@ -109,8 +109,8 @@ vfio_legacy_dma_unmap_get_dirty_bitmap(const VFIOLegacyContainer *container, ret =ioctl (container->fd,VFIO_IOMMU_UNMAP_DMA , unmap);if (!ret) { -physical_memory_set_dirty_lebitmap (vbmap.bitmap, - iotlb->translated_addr, vbmap.pages); +physical_memory_set_dirty_lebitmap (vbmap.bitmap, translated_addr, + vbmap.pages); }else {error_report ("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m" ); }
@@ -134,16 +134,27 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container, .size = size, };bool need_dirty_sync = false; +MemoryRegion *mr; +hwaddr translated_addr, xlat;int ret;Error *local_err =NULL ;g_assert (!cpr_is_incoming ()); +RCU_READ_LOCK_GUARD (); + + mr =vfio_translate_iotlb (iotlb, &xlat, &local_err); +if (!mr) { +return -EINVAL ; + } + + translated_addr =memory_region_get_ram_addr (mr) + xlat; +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) {return vfio_legacy_dma_unmap_get_dirty_bitmap (container, iova, size, - iotlb); + translated_addr); } need_dirty_sync = true;
@@ -155,7 +166,7 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container,if (need_dirty_sync) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , - iotlb->translated_addr, &local_err); + translated_addr, &local_err);if (ret) {error_report_err (local_err);return ret;
hw/vfio/iommufd.c +13 -2
@@ -65,6 +65,8 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,IOMMUFDBackend *be = container->be;uint32_t ioas_id = container->ioas_id;bool need_dirty_sync = false; +hwaddr translated_addr, xlat; +MemoryRegion *mr;Error *local_err =NULL ;int ret, unmap_ret;
@@ -72,12 +74,21 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer, size =UINT64_MAX ; } +RCU_READ_LOCK_GUARD (); + + mr =vfio_translate_iotlb (iotlb, &xlat, &local_err); +if (!mr) { +return -EINVAL ; + } + + translated_addr =memory_region_get_ram_addr (mr) + xlat; +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR , - iotlb->translated_addr, + translated_addr, &local_err);if (ret) {error_report_err (local_err);
@@ -103,7 +114,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (need_dirty_sync) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , - iotlb->translated_addr, + translated_addr, &local_err);if (ret) {error_report_err (local_err);
hw/vfio/listener.c +2 -2
@@ -97,8 +97,8 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section,* Called with rcu_read_lock held. * The returned MemoryRegion must not be accessed after calling rcu_read_unlock. */ -static MemoryRegion *vfio_translate_iotlb (IOMMUTLBEntry *iotlb,hwaddr *xlat_p, -Error **errp) +MemoryRegion *vfio_translate_iotlb (IOMMUTLBEntry *iotlb,hwaddr *xlat_p, +Error **errp) {MemoryRegion *mr;
Thanks
Zhenzhong
Re: [PULL 28/41] vfio/listener: Add missing dirty tracking in region_del
Quoted 73 lines · Zhenzhong
Hi Cédric,
Quoted 71 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PULL 28/41] vfio/listener: Add missing dirty tracking inregion_delHello Zhenzhong,
Quoted 10 lines · Cédric Le Goater
From: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a NULL iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.Quoted 8 lines
For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_dirty_tracking().Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>Reviewed-by Yi Liu <[email protected]>Quoted 7 lines
Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/listener.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0index
62699cb772d786c1510318dff73973ef4d297177..813621f22f8b5ec284388f9c5f719525ec5f282c100644hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener
Quoted 3 lines
hw/vfio/listener.c +0
-0@@ -713,14 +713,34 @@ static void vfio_listener_region_del(MemoryListener*listener,
Quoted 7 lines
hw/vfio/listener.c +1
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova);hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener · line 728
Quoted 10 lines
hw/vfio/listener.c +7
-0unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking when switch out of PT domain. In fact, in unmap_bitmap, +* only translated_addr field is used to set dirty bitmap. +* +* Note: When switch into PT domain from DMA domain, the whole IOMMU
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener · line 737
Quoted 9 lines
hw/vfio/listener.c +9
-0+* MR is deleted without iotlb, before that happen, we depend on +* vIOMMU to send unmap notification with accurate iotlb entry to +* VFIO. See vtd_address_space_unmap_in_dirty_tracking() for example, +* it is triggered during switching to block domain because vtd does +* not support direct switching from DMA to PT domain. +*/ +if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova;In an experiment involving a nested setup with L1 +
intel_iommu, an L2 VM with an assigned igb VF can trigger the following QEMU segv :
Quoted 8 lines
Quoted 6 lines
bitmap_set_atomic(map=NULL, start=1, nr=1) physical_memory_set_dirty_range(start=0x380004040000, length=4096) physical_memory_set_dirty_lebitmap(start=0x380004040000, pages=3) vfio_container_query_dirty_bitmap(translated_addr=0x380004040000) vfio_legacy_dma_unmap_one(iova=0x380004040000, size=12288) vfio_listener_region_del()Hmm, looks a range in PCI space, I suspect it's a PCI bar but it has 3 pages.
It's the nested VT-d interrupt remapping table.
Quoted 1 line
Is igb VF a real hw passthrough to L1, then L1 pass it to L2?
Iit's an L1 emulated igb VF assigned to an L2.
Quoted 1 line
Seems L2 crash, not L1?
yes. It occured a few times and I can't reproduce anymore. hmm.
That seemed to fix it
- entry.translated_addr = iova;
+ entry.translated_addr = memory_region_get_ram_addr(section->mr) +
+ section->offset_within_region;
Thanks,
C.
Quoted 138 lines
Quoted 5 lines
The VT-d interrupt-remapping table is mapped pretty high and
translated_addrends up as a very large value. It seems that the correct value should be :Quoted 7 lines
translated_addr = memory_region_get_ram_addr(section->mr) + section->offset_within_region;as found in
vfio_iommu_map_dirty_notify()and
andvfio_ram_discard_query_dirty_bitmap().Is that correct ?
Good catch, it looks we lack a conversion from
iotlb->translated_addrtodirty_blockoffset just like invfio_iommu_map_dirty_notify().iotlb->translated_addris designed to hold translation result from vIOMMU. How about below(untested)?hw/vfio/container-legacy.c +16
-5@@ -71,7 +71,7 @@ static int vfio_ram_block_discard_disable(VFIOLegacyContainer *container,static int vfio_legacy_dma_unmap_get_dirty_bitmap (const VFIOLegacyContainer *container,hwaddr iova,uint64_t size, -IOMMUTLBEntry *iotlb) +hwaddr translated_addr) {const VFIOContainer *bcontainer =VFIO_IOMMU (container);struct vfio_iommu_type1_dma_unmap *unmap;@@ -109,8 +109,8 @@ vfio_legacy_dma_unmap_get_dirty_bitmap(const VFIOLegacyContainer *container, ret =ioctl (container->fd,VFIO_IOMMU_UNMAP_DMA , unmap);if (!ret) { -physical_memory_set_dirty_lebitmap (vbmap.bitmap, - iotlb->translated_addr, vbmap.pages); +physical_memory_set_dirty_lebitmap (vbmap.bitmap, translated_addr, + vbmap.pages); }else {error_report ("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m" ); }@@ -134,16 +134,27 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container, .size = size, };bool need_dirty_sync = false; +MemoryRegion *mr; +hwaddr translated_addr, xlat;int ret;Error *local_err =NULL ;g_assert (!cpr_is_incoming ()); +RCU_READ_LOCK_GUARD (); + + mr =vfio_translate_iotlb (iotlb, &xlat, &local_err); +if (!mr) { +return -EINVAL ; + } + + translated_addr =memory_region_get_ram_addr (mr) + xlat; +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) {return vfio_legacy_dma_unmap_get_dirty_bitmap (container, iova, size, - iotlb); + translated_addr); } need_dirty_sync = true;@@ -155,7 +166,7 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container,if (need_dirty_sync) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , - iotlb->translated_addr, &local_err); + translated_addr, &local_err);if (ret) {error_report_err (local_err);return ret;hw/vfio/iommufd.c +13
-2@@ -65,6 +65,8 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,IOMMUFDBackend *be = container->be;uint32_t ioas_id = container->ioas_id;bool need_dirty_sync = false; +hwaddr translated_addr, xlat; +MemoryRegion *mr;Error *local_err =NULL ;int ret, unmap_ret;@@ -72,12 +74,21 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer, size =UINT64_MAX ; } +RCU_READ_LOCK_GUARD (); + + mr =vfio_translate_iotlb (iotlb, &xlat, &local_err); +if (!mr) { +return -EINVAL ; + } + + translated_addr =memory_region_get_ram_addr (mr) + xlat; +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR , - iotlb->translated_addr, + translated_addr, &local_err);if (ret) {error_report_err (local_err);@@ -103,7 +114,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (need_dirty_sync) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , - iotlb->translated_addr, + translated_addr, &local_err);if (ret) {error_report_err (local_err);hw/vfio/listener.c +2
-2@@ -97,8 +97,8 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section,* Called with rcu_read_lock held. * The returned MemoryRegion must not be accessed after calling rcu_read_unlock. */ -static MemoryRegion *vfio_translate_iotlb (IOMMUTLBEntry *iotlb,hwaddr *xlat_p, -Error **errp) +MemoryRegion *vfio_translate_iotlb (IOMMUTLBEntry *iotlb,hwaddr *xlat_p, +Error **errp) {MemoryRegion *mr;Thanks
Zhenzhong
RE: [PULL 28/41] vfio/listener: Add missing dirty tracking in region_del
Quoted 92 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PULL 28/41] vfio/listener: Add missing dirty tracking inregion_delQuoted 13 lines · Zhenzhong
Hi Cédric,
Quoted 11 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PULL 28/41] vfio/listener: Add missing dirty tracking inregion_delHello Zhenzhong,
Quoted 4 lines · Cédric Le Goater
From: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a
NULL
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.Quoted 8 lines
For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_dirty_tracking().Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>Reviewed-by Yi Liu <[email protected]>Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/listener.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0index
62699cb772d786c1510318dff73973ef4d297177..813621f22f8b5ec284388f9c5f71Quoted 4 lines
Quoted 4 lines
9525ec5f282c100644hw/vfio/listener.c static void
Quoted 3 lines
hw/vfio/listener.c +0
-0@@ -713,14 +713,34 @@ static voidvfio_listener_region_del(MemoryListener
Quoted 33 lines
Quoted 33 lines
*listener,
Quoted 7 lines
hw/vfio/listener.c +1
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova);hw/vfio/listener.c static void · line 728
Quoted 10 lines
hw/vfio/listener.c +7
-0unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking when switch out of PT domain. In fact, in unmap_bitmap, +* only translated_addr field is used to set dirty bitmap. +* +* Note: When switch into PT domain from DMA domain, the whole IOMMU
hw/vfio/listener.c static void · line 737
Quoted 9 lines
hw/vfio/listener.c +9
-0+* MR is deleted without iotlb, before that happen, we depend on +* vIOMMU to send unmap notification with accurate iotlb entry to +* VFIO. See vtd_address_space_unmap_in_dirty_tracking() for example, +* it is triggered during switching to block domain because vtd does +* not support direct switching from DMA to PT domain. +*/ +if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova;In an experiment involving a nested setup with L1 +
intel_iommu, an L2 VM with an assigned igb VF can trigger the following QEMU segv :Quoted 8 lines
Quoted 6 lines
bitmap_set_atomic(map=NULL, start=1, nr=1) physical_memory_set_dirty_range(start=0x380004040000, length=4096) physical_memory_set_dirty_lebitmap(start=0x380004040000, pages=3) vfio_container_query_dirty_bitmap(translated_addr=0x380004040000) vfio_legacy_dma_unmap_one(iova=0x380004040000, size=12288) vfio_listener_region_del()Hmm, looks a range in PCI space, I suspect it's a PCI bar but it has 3 pages.
Quoted 8 lines
It's the nested VT-d interrupt remapping table.
Quoted 1 line
Is igb VF a real hw passthrough to L1, then L1 pass it to L2?
Iit's an L1 emulated igb VF assigned to an L2.
Quoted 1 line
Seems L2 crash, not L1?
yes. It occured a few times and I can't reproduce anymore. hmm.
It can be reproduced with below steps:
1. Start guest with VFIO device in legacy iommu mode, then start dirty rate tracking.
(qemu) calc_dirty_rate -b 60
2. disable VFIO device bar region by clear bit1 in COMMAND register in guest.
# setpci -s 01:00.0 COMMAND=0005
3. below trace triggers:
vfio_listener_region_del region_del 0xe0400020000 - 0xe0400021fff
Thread 10 "CPU 6/KVM" received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7ffde5dfb640 (LWP 217374)]0x000055555613c8a0 in bitmap_set_atomic (map=0x0, start=32, nr=1) at ../util/bitmap.c:213
213 qatomic_or(p, mask_to_set);
(gdb) bt
#0 0x000055555613c8a0 in bitmap_set_atomic (map=0x0, start=32, nr=1) at ../util/bitmap.c:213 #1 0x0000555555d6a0e2 in physical_memory_set_dirty_range (start=15410342789120, length=4096, mask=5 '\005') at ../system/physmem.c:1042 #2 0x0000555555d6b2c9 in physical_memory_set_dirty_lebitmap (bitmap=0x7ffdc80ac690, start=15410342789120, pages=2) at ../system/physmem.c:1305 #3 0x0000555555ca97ee in vfio_legacy_dma_unmap_get_dirty_bitmap (container=0x5555576fc980, iova=15410342789120, size=8192, iotlb=0x7ffde5df9e20) at ../hw/vfio/container-legacy.c:112 #4 0x0000555555ca9927 in vfio_legacy_dma_unmap_one (container=0x5555576fc980, iova=15410342789120, size=8192, flags=0, iotlb=0x7ffde5df9e20) at ../hw/vfio/container-legacy.c:145 #5 0x0000555555ca9aba in vfio_legacy_dma_unmap (bcontainer=0x5555576fc980, iova=15410342789120, size=8192, iotlb=0x7ffde5df9e20, unmap_all=false) at ../hw/vfio/container-legacy.c:196 #6 0x0000555555ca84b5 in vfio_container_dma_unmap (bcontainer=0x5555576fc980, iova=15410342789120, size=8192, iotlb=0x7ffde5df9e20, unmap_all=false) at ../hw/vfio/container.c:131 #7 0x0000555555ca68cd in vfio_listener_region_del (listener=0x5555576fc9b0, section=0x7ffde5df9ed0) at ../hw/vfio/listener.c:753 #8 0x0000555555d5d9a9 in address_space_update_topology_pass (as=0x5555574c1880 <address_space_memory>, old_view=0x7ffd7c001580, new_view=0x7ffdc8000c00, adding=false)
Review
Quoted 6 lines
That seemed to fix it
(quoted diff) +2
-1- entry.translated_addr = iova; + entry.translated_addr = memory_region_get_ram_addr(section->mr) + + section->offset_within_region;
I verified this works.
Quoted 144 lines
Thanks,
C.
Quoted 8 lines
Quoted 8 lines
The VT-d interrupt-remapping table is mapped pretty high and
translated_addrends up as a very large value. It seems that the correct value should be :translated_addr = memory_region_get_ram_addr(section->mr) + section->offset_within_region;Quoted 6 lines
Quoted 4 lines
as found in
vfio_iommu_map_dirty_notify()and
andvfio_ram_discard_query_dirty_bitmap().Is that correct ?
Good catch, it looks we lack a conversion from
iotlb->translated_addrto
dirty_blockoffset just like invfio_iommu_map_dirty_notify().Quoted 1 line
iotlb->translated_addris designed to hold translation result from vIOMMU. Howabout below(untested)?
hw/vfio/container-legacy.c static int
Quoted 4 lines
hw/vfio/container-legacy.c +0
-0@@ -71,7 +71,7 @@ static intvfio_ram_block_discard_disable(VFIOLegacyContainer *container,
hw/vfio/container-legacy.c static int · line 72
Quoted 2 lines
hw/vfio/container-legacy.c +0
-0static int vfio_legacy_dma_unmap_get_dirty_bitmap(const VFIOLegacyContainer*container,
hw/vfio/container-legacy.c vfio_legacy_dma_unmap_get_dirty_bitmap(const
Quoted 7 lines
hw/vfio/container-legacy.c +1
-1hwaddr iova, uint64_t size, - IOMMUTLBEntry *iotlb) + hwaddr translated_addr) { const VFIOContainer *bcontainer = VFIO_IOMMU(container);struct vfio_iommu_type1_dma_unmap *unmap;@@ -109,8 +109,8 @@ vfio_legacy_dma_unmap_get_dirty_bitmap(constVFIOLegacyContainer *container,
hw/vfio/container-legacy.c static int vfio_legacy_dma_unmap_one(const
Quoted 11 lines
hw/vfio/container-legacy.c +2
-2ret =ioctl (container->fd,VFIO_IOMMU_UNMAP_DMA , unmap);if (!ret) { -physical_memory_set_dirty_lebitmap (vbmap.bitmap, - iotlb->translated_addr, vbmap.pages); +physical_memory_set_dirty_lebitmap (vbmap.bitmap, translated_addr, + vbmap.pages); }else {error_report ("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m" ); }@@ -134,16 +134,27 @@ static int vfio_legacy_dma_unmap_one(constVFIOLegacyContainer *container,
hw/vfio/container-legacy.c static int vfio_legacy_dma_unmap_one(const
Quoted 29 lines
hw/vfio/container-legacy.c +12
-1.size = size, };bool need_dirty_sync = false; +MemoryRegion *mr; +hwaddr translated_addr, xlat;int ret;Error *local_err =NULL ;g_assert (!cpr_is_incoming ()); +RCU_READ_LOCK_GUARD (); + + mr =vfio_translate_iotlb (iotlb, &xlat, &local_err); +if (!mr) { +return -EINVAL ; + } + + translated_addr =memory_region_get_ram_addr (mr) + xlat; +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) {return vfio_legacy_dma_unmap_get_dirty_bitmap (container, iova, size, - iotlb); + translated_addr); } need_dirty_sync = true;@@ -155,7 +166,7 @@ static int vfio_legacy_dma_unmap_one(constVFIOLegacyContainer *container,
hw/vfio/iommufd.c static int iommufd_cdev_unmap(const VFIOContainer
Quoted 13 lines
hw/vfio/container-legacy.c +1
-1if (need_dirty_sync) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , - iotlb->translated_addr, &local_err); + translated_addr, &local_err);if (ret) {error_report_err (local_err);return ret;hw/vfio/iommufd.c +0
-0@@ -65,6 +65,8 @@ static int iommufd_cdev_unmap(const VFIOContainer*bcontainer,
hw/vfio/iommufd.c static int iommufd_cdev_unmap(const VFIOContainer
Quoted 9 lines
hw/vfio/iommufd.c +2
-0IOMMUFDBackend *be = container->be;uint32_t ioas_id = container->ioas_id;bool need_dirty_sync = false; +hwaddr translated_addr, xlat; +MemoryRegion *mr;Error *local_err =NULL ;int ret, unmap_ret;@@ -72,12 +74,21 @@ static int iommufd_cdev_unmap(const VFIOContainer*bcontainer,
hw/vfio/iommufd.c static int iommufd_cdev_unmap(const VFIOContainer
Quoted 23 lines
hw/vfio/iommufd.c +10
-1size =UINT64_MAX ; } +RCU_READ_LOCK_GUARD (); + + mr =vfio_translate_iotlb (iotlb, &xlat, &local_err); +if (!mr) { +return -EINVAL ; + } + + translated_addr =memory_region_get_ram_addr (mr) + xlat; +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR , - iotlb->translated_addr, + translated_addr, &local_err);if (ret) {error_report_err (local_err);@@ -103,7 +114,7 @@ static int iommufd_cdev_unmap(const VFIOContainer*bcontainer,
hw/vfio/listener.c static bool
Quoted 13 lines
hw/vfio/iommufd.c +1
-1if (need_dirty_sync) { ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , - iotlb->translated_addr, + translated_addr, &local_err);if (ret) {error_report_err (local_err);hw/vfio/listener.c +0
-0@@ -97,8 +97,8 @@ static boolvfio_listener_skipped_section(MemoryRegionSection *section,
Quoted 11 lines
hw/vfio/listener.c static bool · line 98
Quoted 2 lines
hw/vfio/listener.c +0
-0* Called with rcu_read_lock held. * The returned MemoryRegion must not be accessed after calling
rcu_read_unlock.hw/vfio/listener.c static bool · line 100
Quoted 2 lines
hw/vfio/listener.c +0
-1*/ -static MemoryRegion *vfio_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr
*xlat_p,Quoted 5 lines
hw/vfio/listener.c +2
-1- Error **errp) +MemoryRegion *vfio_translate_iotlb (IOMMUTLBEntry *iotlb,hwaddr *xlat_p, +Error **errp) {MemoryRegion *mr;
Above change needs below tweak to work:
@@ -134,7 +134,6 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container,
.size = size,
};
bool need_dirty_sync = false;
- MemoryRegion *mr;
hwaddr translated_addr, xlat;
int ret;
Error *local_err = NULL;@@ -143,14 +142,15 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container,
RCU_READ_LOCK_GUARD();
- mr = vfio_translate_iotlb(iotlb, &xlat, &local_err);
- if (!mr) {
- return -EINVAL;
- }
+ if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
+ MemoryRegion *mr = vfio_translate_iotlb(iotlb, &xlat, &local_err);
- translated_addr = memory_region_get_ram_addr(mr) + xlat;
+ if (!mr) {
+ return -EINVAL;
+ }
+
+ translated_addr = memory_region_get_ram_addr(mr) + xlat;
- if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
if (!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&
bcontainer->dirty_pages_supported) {
return vfio_legacy_dma_unmap_get_dirty_bitmap(container, iova, size,Thanks
Zhenzhong
Re: [PULL 28/41] vfio/listener: Add missing dirty tracking in region_del
Quoted 131 lines · Zhenzhong
Quoted 92 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PULL 28/41] vfio/listener: Add missing dirty tracking inregion_delQuoted 13 lines · Zhenzhong
Hi Cédric,
Quoted 11 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PULL 28/41] vfio/listener: Add missing dirty tracking inregion_delHello Zhenzhong,
Quoted 4 lines · Cédric Le Goater
From: Zhenzhong Duan <[email protected]>
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a
NULL
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.Quoted 8 lines
For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_dirty_tracking().Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>Reviewed-by Yi Liu <[email protected]>Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/listener.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0index
62699cb772d786c1510318dff73973ef4d297177..813621f22f8b5ec284388f9c5f71Quoted 4 lines
Quoted 4 lines
9525ec5f282c100644hw/vfio/listener.c static void
Quoted 3 lines
hw/vfio/listener.c +0
-0@@ -713,14 +713,34 @@ static voidvfio_listener_region_del(MemoryListener
Quoted 33 lines
Quoted 33 lines
*listener,
Quoted 7 lines
hw/vfio/listener.c +1
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova);hw/vfio/listener.c static void · line 728
Quoted 10 lines
hw/vfio/listener.c +7
-0unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking when switch out of PT domain. In fact, in unmap_bitmap, +* only translated_addr field is used to set dirty bitmap. +* +* Note: When switch into PT domain from DMA domain, the whole IOMMU
hw/vfio/listener.c static void · line 737
Quoted 9 lines
hw/vfio/listener.c +9
-0+* MR is deleted without iotlb, before that happen, we depend on +* vIOMMU to send unmap notification with accurate iotlb entry to +* VFIO. See vtd_address_space_unmap_in_dirty_tracking() for example, +* it is triggered during switching to block domain because vtd does +* not support direct switching from DMA to PT domain. +*/ +if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova;In an experiment involving a nested setup with L1 +
intel_iommu, an L2 VM with an assigned igb VF can trigger the following QEMU segv :Quoted 8 lines
Quoted 6 lines
bitmap_set_atomic(map=NULL, start=1, nr=1) physical_memory_set_dirty_range(start=0x380004040000, length=4096) physical_memory_set_dirty_lebitmap(start=0x380004040000, pages=3) vfio_container_query_dirty_bitmap(translated_addr=0x380004040000) vfio_legacy_dma_unmap_one(iova=0x380004040000, size=12288) vfio_listener_region_del()Hmm, looks a range in PCI space, I suspect it's a PCI bar but it has 3 pages.
Quoted 8 lines
It's the nested VT-d interrupt remapping table.
Quoted 1 line
Is igb VF a real hw passthrough to L1, then L1 pass it to L2?
Iit's an L1 emulated igb VF assigned to an L2.
Quoted 1 line
Seems L2 crash, not L1?
yes. It occured a few times and I can't reproduce anymore. hmm.
It can be reproduced with below steps:
1. Start guest with VFIO device in legacy iommu mode, then start dirty rate tracking.
(qemu)
calc_dirty_rate-b 602. disable VFIO device bar region by clear bit1 in COMMAND register in guest.
# setpci -s 01:00.0 COMMAND=00053. below trace triggers:
vfio_listener_region_delregion_del0xe0400020000-0xe0400021fffThread 10 "CPU 6/KVM" received signal SIGSEGV, Segmentation fault. [Switching to Thread
0x7ffde5dfb640(LWP 217374)]0x000055555613c8a0inbitmap_set_atomic(map=0x0, start=32, nr=1) at../util/bitmap.c:213
213qatomic_or(p, mask_to_set);
(gdb) bt#0 0x000055555613c8a0 in bitmap_set_atomic (map=0x0, start=32, nr=1) at ../util/bitmap.c:213 #1 0x0000555555d6a0e2 in physical_memory_set_dirty_range (start=15410342789120, length=4096, mask=5 '\005') at ../system/physmem.c:1042 #2 0x0000555555d6b2c9 in physical_memory_set_dirty_lebitmap (bitmap=0x7ffdc80ac690, start=15410342789120, pages=2) at ../system/physmem.c:1305 #3 0x0000555555ca97ee in vfio_legacy_dma_unmap_get_dirty_bitmap (container=0x5555576fc980, iova=15410342789120, size=8192, iotlb=0x7ffde5df9e20) at ../hw/vfio/container-legacy.c:112 #4 0x0000555555ca9927 in vfio_legacy_dma_unmap_one (container=0x5555576fc980, iova=15410342789120, size=8192, flags=0, iotlb=0x7ffde5df9e20) at ../hw/vfio/container-legacy.c:145 #5 0x0000555555ca9aba in vfio_legacy_dma_unmap (bcontainer=0x5555576fc980, iova=15410342789120, size=8192, iotlb=0x7ffde5df9e20, unmap_all=false) at ../hw/vfio/container-legacy.c:196 #6 0x0000555555ca84b5 in vfio_container_dma_unmap (bcontainer=0x5555576fc980, iova=15410342789120, size=8192, iotlb=0x7ffde5df9e20, unmap_all=false) at ../hw/vfio/container.c:131 #7 0x0000555555ca68cd in vfio_listener_region_del (listener=0x5555576fc9b0, section=0x7ffde5df9ed0) at ../hw/vfio/listener.c:753 #8 0x0000555555d5d9a9 in address_space_update_topology_pass (as=0x5555574c1880 <address_space_memory>, old_view=0x7ffd7c001580, new_view=0x7ffdc8000c00, adding=false)Quoted 1 line
Quoted 7 lines
Review
Quoted 5 lines
That seemed to fix it
(quoted diff) +2
-1- entry.translated_addr = iova; + entry.translated_addr = memory_region_get_ram_addr(section->mr) + + section->offset_within_region;I verified this works.
Thanks,
I will send a patch.
C.
[PULL 33/41] vfio/listener: Bypass readonly region for dirty tracking
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
When doing dirty tracking or calculating dirty tracking range, readonly regions can be bypassed, because corresponding DMA mappings are readonly and never become dirty.
This can optimize dirty tracking a bit for passthrough device.
--- hw/vfio/listener.c | 48 ++++++++++++++++++++++++++++++++++---------- hw/vfio/trace-events | 1 + 2 files changed, 38 insertions(+), 11 deletions(-)
hw/vfio/listener.c +37 -11
@@ -76,8 +76,13 @@ static bool vfio_log_sync_needed(const VFIOContainer *bcontainer)return true; } -static bool vfio_listener_skipped_section (MemoryRegionSection *section) +static bool vfio_listener_skipped_section (MemoryRegionSection *section, +bool bypass_ro) { +if (bypass_ro && section->readonly) { +return true; + } +return (!memory_region_is_ram (section->mr) && !memory_region_is_iommu (section->mr)) || memory_region_is_protected(section->mr) ||
@@ -368,9 +373,9 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section) }static bool vfio_listener_valid_section (MemoryRegionSection *section, -const char *name) +bool bypass_ro,const char *name) { -if (vfio_listener_skipped_section (section)) { +if (vfio_listener_skipped_section (section, bypass_ro)) { trace_vfio_listener_region_skip(name, section->offset_within_address_space, section->offset_within_address_space +
@@ -497,7 +502,7 @@ void vfio_container_region_add(VFIOContainer *bcontainer,int ret;Error *err =NULL ; -if (!vfio_listener_valid_section (section,"region_add" )) { +if (!vfio_listener_valid_section (section, false,"region_add" )) {return ; }
@@ -663,7 +668,7 @@ static void vfio_listener_region_del(MemoryListener *listener,int ret;bool try_unmap = true; -if (!vfio_listener_valid_section (section,"region_del" )) { +if (!vfio_listener_valid_section (section, false,"region_del" )) {return ; }
@@ -722,9 +727,10 @@ static void vfio_listener_region_del(MemoryListener *listener, }/* -* Fake an IOTLB entry for identity mapping which is needed by dirty -* tracking when switch out of PT domain. In fact, in unmap_bitmap, -* only translated_addr field is used to set dirty bitmap. +* Fake an IOTLB entry for writable identity mapping which is needed +* by dirty tracking when switch out of PT domain. In fact, in +* unmap_bitmap, only translated_addr field is used to set dirty +* bitmap. * * Note: When switch into PT domain from DMA domain, the whole IOMMU * MR is deleted without iotlb, before that happen, we depend on
@@ -733,7 +739,8 @@ static void vfio_listener_region_del(MemoryListener *listener,* it is triggered during switching to block domain because vtd does * not support direct switching from DMA to PT domain. */ -if (global_dirty_tracking &&memory_region_is_ram (section->mr)) { +if (global_dirty_tracking &&memory_region_is_ram (section->mr) && + !section->readonly) { entry.iova = iova; entry.translated_addr = iova; iotlb = &entry;
@@ -841,7 +848,8 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,container_of (listener, VFIODirtyRangesListener, listener);hwaddr iova, end; -if (!vfio_listener_valid_section (section,"tracking_update" ) || +/* Bypass readonly section as it never becomes dirty */ +if (!vfio_listener_valid_section (section, true,"tracking_update" ) || !vfio_get_section_iova_range (dirty->bcontainer, section, &iova, &end,NULL )) {return ;
@@ -1100,6 +1108,19 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)if (!mr) { goto out_unlock; } + +/* +* The mapping is readonly when either it's a readonly mapping in guest +* or mapped target is readonly, bypass it for dirty tracking as it +* never becomes dirty. +*/ +if (!(iotlb->perm &IOMMU_WO ) || mr->readonly) { +trace_vfio_iommu_map_dirty_notify_skip_ro (iova, + iova + iotlb->addr_mask); +rcu_read_unlock (); +return ; + } + translated_addr =memory_region_get_ram_addr (mr) + xlat; ret = vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
@@ -1235,7 +1256,12 @@ static void vfio_listener_log_sync(MemoryListener *listener,int ret;Error *local_err =NULL ; -if (vfio_listener_skipped_section (section)) { +/* +* Bypass readonly section as it never becomes dirty, iommu memory section +* is RW and never bypassed. The readonly mappings in iommu MR are bypassed +* in vfio_iommu_map_dirty_notify(). +*/ +if (vfio_listener_skipped_section (section, true)) {return ; }
hw/vfio/trace-events +1 -0
@@ -103,6 +103,7 @@ vfio_listener_region_del(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - vfio_device_dirty_tracking_update(uint64_t start, uint64_t end, uint64_t min, uint64_t max) "section 0x%"PRIx64" - 0x%"PRIx64" -> update [0x%"PRIx64" - 0x%"PRIx64"]" vfio_device_dirty_tracking_start(int nr_ranges, uint64_t min32, uint64_t max32, uint64_t min64, uint64_t max64, uint64_t minpci, uint64_t maxpci) "nr_ranges %d 32:[0x%"PRIx64" - 0x%"PRIx64"], 64:[0x%"PRIx64" - 0x%"PRIx64"], pci64:[0x%"PRIx64" - 0x%"PRIx64"]" vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 +vfio_iommu_map_dirty_notify_skip_ro(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 # container.c vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t backend_flag, uint64_t bitmap_size, uint64_t translated_addr, uint64_t dirty_pages) "iova=0x%"PRIx64" size=0x%"PRIx64" backend_flag=0x%"PRIx64" bitmap_size=0x%"PRIx64" gpa=0x%"PRIx64" dirty_pages=%"PRIu64
Signature · 1 line
2.52.0
[PULL 39/41] target/riscv: Fix build errors
In reply to Cédric Le GoaterNewer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC)) detects a truncation error:
../target/riscv/cpu.c: In function ‘riscv_isa_write_fdt’:
../target/riscv/cpu.c:2916:35: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 5 [-Werror=format-truncation=]
2916 | snprintf(isa_base, maxlen, "rv%di", xlen);
| ^~
../target/riscv/cpu.c:2916:32: note: directive argument in the range [-2147483648, 2147483632]
2916 | snprintf(isa_base, maxlen, "rv%di", xlen);
| ^~~~~~~
Since the xlen variable represents the register width (32, 64, 128) in the RISC-V base ISA name, mask its value with a 8-bit bitmask to satisfy the size constraints on the snprintf output.
--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu.c +1 -1
@@ -2913,7 +2913,7 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename) riscv_isa =riscv_isa_string (cpu);qemu_fdt_setprop_string (fdt, nodename,"riscv,isa" , riscv_isa); -snprintf (isa_base, maxlen,"rv%di" , xlen); +snprintf (isa_base, maxlen,"rv%di" , xlen &0xFF );qemu_fdt_setprop_string (fdt, nodename,"riscv,isa-base" , isa_base); isa_extensions =riscv_isa_extensions_list (cpu, &count);
Signature · 1 line
2.52.0
[PULL 21/41] docs/devel: Add IOMMUFD nesting documentation
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Add documentation about using IOMMUFD backed VFIO device with intel_iommu with x-flts=on.
--- docs/devel/vfio-iommufd.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
docs/devel/vfio-iommufd.rst +17 -0
@@ -152,3 +152,20 @@ RAM discarding for mdev. ``vfio-ap`` and ``vfio-ccw`` devices don't have same issue as their backend devices are always mdev and RAM discarding is force enabled. + +Usage with intel_iommu featuring x-flts=on +------------------------------------------ + +Only IOMMUFD backed VFIO device is supported when intel_iommu is configured +with x-flts=on, for legacy container backed VFIO device, below error shows: + +.. code-block:: none + + qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0: vfio 0000:02:00.0: Failed to set vIOMMU: Need IOMMUFD backend when x-flts=on + +VFIO device under PCI bridge is unsupported, use PCIE bridge if necessary, +otherwise below error shows: + +.. code-block:: none + + qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0,bus=bridge1,iommufd=iommufd0: vfio 0000:02:00.0: Failed to set vIOMMU: Host device downstream to a PCI bridge is unsupported when x-flts=on
Signature · 1 line
2.52.0
[PULL 13/41] intel_iommu_accel: Stick to system MR for IOMMUFD backed host device when x-flts=on
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
When guest enables scalable mode and setup first stage page table, we don't want to use IOMMU MR but rather continue using the system MR for IOMMUFD backed host device.
Then default HWPT in VFIO contains GPA->HPA mappings which could be reused as nesting parent HWPT to construct nested HWPT in vIOMMU.
Move vtd_as_key into intel_iommu_internal.h as it's also used by accel code.
--- hw/i386/intel_iommu_accel.h | 6 ++++++ hw/i386/intel_iommu_internal.h | 11 +++++++++++ hw/i386/intel_iommu.c | 28 +++++++++++++++------------- hw/i386/intel_iommu_accel.c | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 13 deletions(-)
hw/i386/intel_iommu_accel.h +6 -0
@@ -15,6 +15,7 @@#ifdef CONFIG_VTD_ACCEL bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp); +VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -24,5 +25,10 @@ static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,error_append_hint (errp,"CONFIG_VTD_ACCEL is not enabled" );return false; } + +static inline VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as) +{ +return NULL ; +}#endif #endif
hw/i386/intel_iommu_internal.h +11 -0
@@ -685,4 +685,15 @@ typedef struct VTDHostIOMMUDevice {uint8_t devfn; HostIOMMUDevice *hiod; } VTDHostIOMMUDevice; + +/* +* PCI bus number (or SID) is not reliable since the device is usaully +* initialized before guest can configure the PCI bridge +* (SECONDARY_BUS_NUMBER). +*/ +struct vtd_as_key { +PCIBus *bus; +uint8_t devfn; +uint32_t pasid; +};#endif
hw/i386/intel_iommu.c +15 -13
@@ -55,17 +55,6 @@#define VTD_PE_GET_SS_LEVEL (pe) \ (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW)) -/* -* PCI bus number (or SID) is not reliable since the device is usaully -* initialized before guest can configure the PCI bridge -* (SECONDARY_BUS_NUMBER). -*/ -struct vtd_as_key { -PCIBus *bus; -uint8_t devfn; -uint32_t pasid; -}; -/* bus/devfn is PCI device's real BDF not the aliased one */ struct vtd_hiod_key {PCIBus *bus;
@@ -1730,12 +1719,25 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)/* Return whether the device is using IOMMU translation. */ static bool vtd_switch_address_space (VTDAddressSpace *as) { +IntelIOMMUState *s;bool use_iommu, pt;assert (as); - use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled (as); - pt = as->iommu_state->dmar_enabled &&vtd_as_pt_enabled (as); + s = as->iommu_state; + use_iommu = s->dmar_enabled && !vtd_as_pt_enabled (as); + pt = s->dmar_enabled &&vtd_as_pt_enabled (as); + +/* +* When guest enables scalable mode and sets up first stage page table, +* we stick to system MR for IOMMUFD backed host device. Then its +* default hwpt contains GPA->HPA mappings which is used directly if +* PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall back +* to original processing. +*/ +if (s->root_scalable && s->fsts &&vtd_find_hiod_iommufd (as)) { + use_iommu = false; + } trace_vtd_switch_address_space(pci_bus_num(as->bus), VTD_PCI_SLOT(as->devfn),
hw/i386/intel_iommu_accel.c +18 -0
@@ -50,3 +50,21 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, "host IOMMU is incompatible with guest first stage translation");return false; } + +VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as) +{ +IntelIOMMUState *s = as->iommu_state; +struct vtd_as_key key = { + .bus = as->bus, + .devfn = as->devfn, + }; +VTDHostIOMMUDevice *vtd_hiod =g_hash_table_lookup (s->vtd_host_iommu_dev, + &key); + +if (vtd_hiod && vtd_hiod->hiod && +object_dynamic_cast (OBJECT (vtd_hiod->hiod), +TYPE_HOST_IOMMU_DEVICE_IOMMUFD )) { +return vtd_hiod; + } +return NULL ; +}
Signature · 1 line
2.52.0
[PULL 41/41] tests/rcutorture: Fix build error
In reply to Cédric Le GoaterNewer gcc compiler (version 16.0.0 20260103 (Red Hat 16.0.0-0) (GCC)) detects an unused variable error:
../tests/unit/rcutorture.c: In function ‘rcu_read_stress_test’:
../tests/unit/rcutorture.c:251:18: error: variable ‘garbage’ set but not used [-Werror=unused-but-set-variable=]
251 | volatile int garbage = 0;
| ^~~~~~~Since the 'garbage' variable is used to generate memory reads from the CPU while holding the RCU lock, it can not be removed. Tag it as ((unused)) instead to silence the compiler warnings/errors.
--- tests/unit/rcutorture.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/unit/rcutorture.c +1 -1
@@ -248,7 +248,7 @@ static void *rcu_read_stress_test(void *arg)int pc;long long n_reads_local =0 ;long long rcu_stress_local[RCU_STRESS_PIPE_LEN +1 ] = {0 }; -volatile int garbage =0 ; + volatile int garbage __attribute__ ((unused)) = 0;rcu_register_thread ();
Signature · 1 line
2.52.0
[PULL 19/41] intel_iommu: Add migration support with x-flts=on
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
When x-flts=on, we set up bindings to nested HWPT in host, after migration, VFIO device binds to nesting parent HWPT by default. We need to re-establish the bindings to nested HWPT, or else device DMA will break.
--- hw/i386/intel_iommu.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +7 -0
@@ -4090,6 +4090,13 @@ static int vtd_post_load(void *opaque, int version_id)*/ vtd_switch_address_space_all (iommu); +/* +* Bindings to nested HWPT in host is set up dynamically depending +* on pasid entry configuration from guest. After migration, we +* need to re-establish the bindings before restoring device's DMA. +*/ +vtd_replay_pasid_bindings_all (iommu); +return 0 ; }
Signature · 1 line
2.52.0
[PULL 36/41] include/hw/hyperv: Remove unused 'struct mshv_vp_registers' definition
In reply to Cédric Le GoaterThe 'struct mshv_vp_registers' definition in hvgdk_mini.h is unused in QEMU and conflicts with the canonical definition in linux-headers/linux/mshv.h.
Remove the duplicate definition to avoid build conflicts when the Linux headers are updated.
--- include/hw/hyperv/hvgdk_mini.h | 7 ------- 1 file changed, 7 deletions(-)
include/hw/hyperv/hvgdk_mini.h +0 -7
@@ -450,13 +450,6 @@ typedef struct hv_input_set_vp_registers {struct hv_register_assoc elements[]; } hv_input_set_vp_registers; -#define MSHV_VP_MAX_REGISTERS 128 - -struct mshv_vp_registers { -int count;/* at most MSHV_VP_MAX_REGISTERS */ -struct hv_register_assoc *regs; -}; -union hv_interrupt_control {uint64_t as_uint64;struct {
Signature · 1 line
2.52.0
[PULL 37/41] update-linux-headers: Remove "asm-s390/unistd_32.h"
In reply to Cédric Le GoaterThe "asm/unistd_32.h" file was generated for the 31-bit compatibility mode on the s390 architecture and support was removed in v6.19-rc1, commit 4ac286c4a8d9 ("s390/syscalls: Switch to generic system call table generation")
unistd_32.h is no longer generated when running make header_install. Remove it.
--- scripts/update-linux-headers.sh | 1 - 1 file changed, 1 deletion(-)
scripts/update-linux-headers.sh +0 -1
@@ -154,7 +154,6 @@ EOF mkdir -p "$output/include/standard-headers/asm-$arch" if [ $arch = s390 ]; then cp_portable "$hdrdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/" - cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-s390/" cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/" fi if [ $arch = arm64 ]; then
Signature · 1 line
2.52.0
[PULL 32/41] intel_iommu_accel: Implement get_host_iommu_quirks() callback
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
Implement get_host_iommu_quirks() callback to retrieve the vendor specific hardware information data and convert it into bitmaps defined with enum host_iommu_quirks. It will be used by VFIO in subsequent patch.
--- hw/i386/intel_iommu_accel.h | 5 +++++ hw/i386/intel_iommu.c | 2 ++ hw/i386/intel_iommu_accel.c | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+)
hw/i386/intel_iommu_accel.h +5 -0
@@ -20,6 +20,7 @@ bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);void vtd_flush_host_piotlb_all_locked (IntelIOMMUState *s,uint16_t domain_id,uint32_t pasid,hwaddr addr,uint64_t npages,bool ih); +void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -47,5 +48,9 @@ static inline void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint64_t npages, bool ih) { } + +static inline void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) +{ +}#endif #endif
hw/i386/intel_iommu.c +2 -0
@@ -5590,6 +5590,8 @@ static void vtd_class_init(ObjectClass *klass, const void *data) x86_class->int_remap = vtd_int_remap;set_bit (DEVICE_CATEGORY_MISC , dc->categories); dc->desc ="Intel IOMMU (VT-d) DMA Remapping device" ; + +vtd_iommu_ops_update_accel (&vtd_iommu_ops); }static const TypeInfo vtd_info = {
hw/i386/intel_iommu_accel.c +21 -0
@@ -12,6 +12,7 @@#include "system/iommufd.h" #include "intel_iommu_internal.h" #include "intel_iommu_accel.h" +#include "hw/core/iommu.h" #include "hw/pci/pci_bus.h" #include "trace.h"
@@ -249,3 +250,23 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,g_hash_table_foreach (s->vtd_address_spaces, vtd_flush_host_piotlb_locked, &piotlb_info); } + +static uint64_t vtd_get_host_iommu_quirks (uint32_t type, +void *caps,uint32_t size) +{ +struct iommu_hw_info_vtd *vtd = caps; +uint64_t quirks =0 ; + +if (type ==IOMMU_HW_INFO_TYPE_INTEL_VTD && +sizeof (struct iommu_hw_info_vtd ) <= size && + vtd->flags &IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 ) { + quirks |=HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO ; + } + +return quirks; +} + +void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) +{ + ops->get_host_iommu_quirks = vtd_get_host_iommu_quirks; +}
Signature · 1 line
2.52.0
Richard Henderson
AppliedQuoted 15 lines · Cédric Le Goater
The following changes since commit
cf3e71d8fc8ba681266759bb6cb2e45a45983e3e:Merge tag 'single-binary-20260112' ofhttps://github.com/philmd/qemu into staging (2026-01-13 11:51:18 +1100)are available in the Git repository at:
https://github.com/legoater/qemu/ tags/pull-vfio-20260113for you to fetch changes up to
7a05be8c70bb789c23076b1ca2563ed7d87c6fb8:tests/rcutorture: Fix build error (2026-01-13 08:29:59 +0100)----------------------------------------------------------------
vfio queue:
Quoted 12 lines
* Resolves build errors with gcc 16
* Adjusts the Linux headers for s390x and mshv
* Fixes endianness issue in the VFIO helper functions
* Adds support for live migration with vIOMMU when using IOMMU dirty tracking
* Implements a migration blocker to prevent failures when VM memory is too large
* Corrects anunmap_bitmapfailure in the legacy VFIO backend
* Addresses a workaround for an Intel IOMMU errata.
* Implements Intel IOMMU first stage translation for passthrough device. Also a prerequisite work for vSVA.
* Updates documentation
Applied, thanks. Please update https://wiki.qemu.org/ChangeLog/11.0 as appropriate.
r~