amd_iommu: Bugfix and Cleanup for IO Page Fault
v2 cover amd_iommu: Bugfix and Cleanup for IO Page Fault
v1: https://lore.kernel.org/qemu-devel/[email protected]
v1->v2:
- Add Fixes tag.
- Rename amdvi_as_devid() to amdvi_get_as_devid().
- Cleanup and add macro to avoid hardcoding.
- Make AMD IOMMU extended feature register read-only.
- Drop the patch for enabling I/O page fault delivery when xtsup=on.
Recently, I tried using QEMU IOMMU emulation to study PCI device IOMMU page faults.
It works well with intel-iommu.
[ 28.819456] DMAR: DRHD: handling fault status reg 2 [ 28.820403] DMAR: [DMA Write NO_PASID] Request device [01:00.0] fault addr 0xc0000000 [fault reason 0x05] PTE Write access is not set [ 28.822325] DMAR: Dump dmar0 table entries for IOVA 0xc0000000 [ 28.823289] DMAR: root entry: 0x00000001014ef001 [ 28.823291] DMAR: context entry: hi 0x0000000000000602, low 0x00000001014ee001 [ 28.825266] DMAR: pte level: 4, pte value: 0x000000010208f403 [ 28.826211] DMAR: pte level: 3, pte value: 0x00000001020c8403 [ 28.827158] DMAR: pte level: 2, pte value: 0x0000000000000000 [ 28.828109] DMAR: page table not present at level 1
However, there are some issues with amd-iommu. This patchset aims to address these issues through bugfixes and cleanup.
- Fix duplicate MSI capabilities for the IOMMU PCI device.
- Fix an invalid PCI address reported in I/O page fault events.
- Fix excessive GA log messages causing endless guest dmesg output.
The QEMU test patch I used to inject IOMMU IO page faults has been appended to the end of this email. To execute "info version" HMP command will be able to trigger an IO page fault.
The following is the QEMU command line. I have tested these changes with both 7.1 and v6.4 guest kernels. I have also tested with both xtsup=off and xtsup=on.
qemu-system-x86_64 \
-machine q35,accel=kvm,kernel-irqchip=split \
-smp 4 -m 8G \
-cpu host \
-hda ol94.qcow2 \
-device pcie-root-port,io-reserve=0,pref64-reserve=32M,bus=pcie.0,chassis=40,id=pcie-root-port.1,addr=05.00,multifunction=on \
-device virtio-net-pci,netdev=tapnet,bus=pcie-root-port.1,mq=true,vectors=9,mac=52:54:11:22:14:47,disable-legacy=on,iommu_platform=true,packed=true \
-netdev tap,id=tapnet,script=qemu-ifup,downscript=no,queues=4,vhost=off \
-monitor stdio -vnc :18 \
-device amd-iommu,dma-remap=true,xtsup=off \
-kernel mainline-linux/arch/x86_64/boot/bzImage \
-append "root=/dev/sda1 init=/sbin/init text loglevel=7 console=ttyS0 clocksource=tsc iommu=force iommu.passthrough=0 amd_iommu=force_isolation iommu.strict=1"
Dongli Zhang(5):
amd_iommu: Do not create duplicate MSI capability
amd_iommu: Use full BDF when reporting page faults
amd_iommu: Define MMIO register masks
amd_iommu: Make extended feature register read-only
amd_iommu: Do not latch unsupported GA log status bits
hw/i386/amd_iommu.c | 34 +++++++++++++++++++++------------- hw/i386/amd_iommu.h | 25 +++++++++++++++++++++---- 2 files changed, 42 insertions(+), 17 deletions(-)
Here is the QEMU test patch to inject IO page fault.
hw/virtio/virtio.c +9 -0
@@ -1877,6 +1877,8 @@ err_undo_map: goto done; } +bool testsys_debug = false; +static void *virtqueue_packed_pop (VirtQueue *vq,size_t sz) {unsigned int i, max;
@@ -1951,6 +1953,13 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) do { bool map_ok; + if (testsys_debug && (desc.addr & 0xff000000) == 0xff000000) { + testsys_debug = false; + printf("fault: virtqueue_packed_pop() desc.addr=0x%lx\n", desc.addr); + desc.addr = 0xc0000000; + desc.len = 1; + } + if (desc.flags & VRING_DESC_F_WRITE) { map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num, iov + out_num,
monitor/hmp-cmds.c +5 -0
@@ -114,6 +114,8 @@ void hmp_info_name(Monitor *mon, const QDict *qdict)qapi_free_NameInfo (info); } +extern bool testsys_debug; +void hmp_info_version (Monitor *mon,const QDict *qdict) {VersionInfo *info;
@@ -125,6 +127,9 @@ void hmp_info_version(Monitor *mon, const QDict *qdict) info->package);qapi_free_VersionInfo (info); + +printf ("fault: enable one-time debug\n" ); + testsys_debug = true; }void hmp_quit (Monitor *mon,const QDict *qdict)
Thank you very much!
Dongli Zhang
PATCH v2 1/5 amd_iommu: Do not create duplicate MSI capability
amdvi_pci_realize() manually adds an MSI capability before calling msi_init(). msi_init() creates the MSI capability itself, so the device can expose duplicate MSI capabilities to the guest.
Drop the manual MSI capability and let msi_init() create the single MSI capability used by QEMU.
Otherwise, there are duplicate MSI capabilities in guest VM lspci.
[root@vm ~]# lspci -vvv -s 00:02.0
00:02.0 IOMMU: Advanced Micro Devices, Inc. [AMD] Family 15h (Models 10h-1fh) I/O Memory Management Unit
Subsystem: Red Hat, Inc. Device 1100
Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx+
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
Interrupt: pin ? routed to IRQ 24
Capabilities: [60] MSI: Enable+ Count=1/1 Maskable- 64bit+
Address: 00000000fee02000 Data: 0021
Capabilities: [5c] HyperTransport: Slave or Primary Interface
Command: BaseUnitID=0 UnitCnt=0 MastHost- DefDir-
Link Control 0: CFlE- CST+ CFE- <LkFail- Init- EOC- TXO- <CRCErr=c
Link Config 0: MLWI=16bit MLWO=8bit LWI=8bit LWO=8bit
Link Control 1: CFlE- CST- CFE- <LkFail- Init- EOC- TXO- <CRCErr=0
Link Config 1: MLWI=8bit MLWO=[6] LWI=[6] LWO=N/C
Revision ID: 0.00
Capabilities: [58] MSI: Enable- Count=1/1 Maskable- 64bit-
Address: 00005808 Data: 5c05
Capabilities: [40] Secure device <?>
---v1->v2:
- Add Fixes tag.
hw/i386/amd_iommu.c | 5 ----- 1 file changed, 5 deletions(-)
hw/i386/amd_iommu.c +0 -5
@@ -2567,11 +2567,6 @@ static void amdvi_pci_realize(PCIDevice *pdev, Error **errp) } s->capab_offset = ret; - ret =pci_add_capability (pdev,PCI_CAP_ID_MSI ,0 , -AMDVI_CAPAB_REG_SIZE , errp); -if (ret <0 ) { -return ; - } ret =pci_add_capability (pdev,PCI_CAP_ID_HT ,0 ,AMDVI_CAPAB_REG_SIZE , errp);if (ret <0 ) {
Signature · 1 line
2.39.3
PATCH v2 2/5 amd_iommu: Use full BDF when reporting page faults
In reply to Dongli ZhangAMDVIAddressSpace->devfn only contains the PCI device/function number. AMD-Vi event log entries require the full DeviceID, including the PCI bus number.
When reporting IO page faults for devices behind a pcie-root-port, QEMU logs the fault against the wrong requester. For example, a fault from 01:00.0 is reported as 00:00.0 because only devfn is passed to amdvi_page_fault().
[root@vm ~]# lspci
... ...
01:00.0 Ethernet controller: Red Hat, Inc. Virtio 1.0 network device (rev 01)
[ 58.154050] pci 0000:00:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x000a] [ 58.156968] pci 0000:00:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x000a]
Use the full BDF consistently for AMD-Vi address spaces and pass it to page fault and page-table access error reporting.
---v1->v2:
- Add Fixes tag.
- Rename amdvi_as_devid() to amdvi_get_as_devid().
- Introduce amdvi_get_devid().
- Rebase to call amdvi_get_devid() at amdvi_find_as_by_devid().
hw/i386/amd_iommu.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
hw/i386/amd_iommu.c +18 -6
@@ -65,6 +65,16 @@ typedef struct AMDVIIOTLBEntry {uint64_t page_mask;/* physical page size */ } AMDVIIOTLBEntry; +static uint16_t amdvi_get_devid (PCIBus *bus,uint8_t devfn) +{ +return PCI_BUILD_BDF (pci_bus_num (bus), devfn); +} + +static uint16_t amdvi_get_as_devid (AMDVIAddressSpace *as) +{ +return amdvi_get_devid (as->bus, as->devfn); +} +/* * These 'fault' reasons have an overloaded meaning since they are not only * intended for describing reasons that generate an IO_PAGE_FAULT as per the AMD
@@ -459,7 +469,7 @@ static gboolean amdvi_find_as_by_devid(gpointer key, gpointer value,const AMDVIAsKey *as = key;const uint16_t *devidp = user_data; -return *devidp ==PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +return *devidp ==amdvi_get_devid (as->bus, as->devfn); }static AMDVIAddressSpace *amdvi_get_as_by_devid (AMDVIState *s,uint16_t devid)
@@ -664,7 +674,7 @@ static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,static int amdvi_as_to_dte (AMDVIAddressSpace *as,uint64_t *dte) { -uint16_t devid =PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +uint16_t devid =amdvi_get_as_devid (as);AMDVIState *s = as->iommu_state;if (!amdvi_get_dte (s, devid, dte)) {
@@ -763,6 +773,7 @@ static int fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,uint8_t pt_level, next_pt_level;IOMMUAccessFlags perms;int ret; +uint16_t devid =amdvi_get_as_devid (as); *page_size =0 ;
@@ -780,7 +791,7 @@ static int fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,* Pointer and indexing the top level table using the IOVA from the request. */ pte_addr =NEXT_PTE_ADDR (dte, pt_level, address); - *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, as->devfn); + *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, devid);if (*pte == (uint64_t )-1 ) {/*
@@ -836,7 +847,7 @@ static int fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, *page_size =PTE_LEVEL_PAGE_SIZE (pt_level); pte_addr =NEXT_PTE_ADDR (*pte, pt_level, address); - *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, as->devfn); + *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, devid);if (*pte == (uint64_t )-1 ) {/* Failure to read PTE. Page walk skips a page_size chunk */
@@ -1897,6 +1908,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,uint8_t mode;uint64_t pte;int fetch_ret; +uint16_t devid =amdvi_get_as_devid (as);/* make sure the DTE has TV = 1 */ if (!(dte[0 ] &AMDVI_DEV_TRANSLATION_VALID )) {
@@ -1930,7 +1942,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,if (fetch_ret <0 || !IOMMU_PTE_PRESENT (pte) || perms != (perms &amdvi_get_perms (pte))) { -amdvi_page_fault (as->iommu_state, as->devfn, addr, perms); +amdvi_page_fault (as->iommu_state, devid, addr, perms);trace_amdvi_page_fault (addr);return ; }
@@ -1957,7 +1969,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr, bool is_write, IOMMUTLBEntry *ret) { AMDVIState *s = as->iommu_state; -uint16_t devid =PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +uint16_t devid =amdvi_get_as_devid (as);AMDVIIOTLBEntry *iotlb_entry =amdvi_iotlb_lookup (s, addr, devid);uint64_t entry[4 ];int dte_ret;
Signature · 1 line
2.39.3
Re: [PATCH v2 2/5] amd_iommu: Use full BDF when reporting page faults
Quoted 114 lines · Dongli Zhang
AMDVIAddressSpace->devfnonly contains the PCI device/function number. AMD-Vi event log entries require the full DeviceID, including the PCI bus number.When reporting IO page faults for devices behind a pcie-root-port, QEMU logs the fault against the wrong requester. For example, a fault from 01:00.0 is reported as 00:00.0 because only devfn is passed to
amdvi_page_fault().[root@vm ~]# lspci
... ...
01:00.0 Ethernet controller: Red Hat, Inc. Virtio 1.0 network device (rev 01)[ 58.154050] pci 0000:00:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x000a] [ 58.156968] pci 0000:00:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x000a]Use the full BDF consistently for AMD-Vi address spaces and pass it to page fault and page-table access error reporting.
Fixes d29a09ca6842 ("hw/i386: Introduce AMD IOMMU")Signed-off-by Dongli Zhang <[email protected]>---
v1->v2:
- Add Fixes tag.
- Renameamdvi_as_devid()toamdvi_get_as_devid().
- Introduceamdvi_get_devid().
- Rebase to callamdvi_get_devid()atamdvi_find_as_by_devid().hw/i386/amd_iommu.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)hw/i386/amd_iommu.c +17
-5@@ -65,6 +65,16 @@ typedef struct AMDVIIOTLBEntry {uint64_t page_mask;/* physical page size */ } AMDVIIOTLBEntry; +static uint16_t amdvi_get_devid (PCIBus *bus,uint8_t devfn) +{ +return PCI_BUILD_BDF (pci_bus_num (bus), devfn); +} + +static uint16_t amdvi_get_as_devid (AMDVIAddressSpace *as) +{ +return amdvi_get_devid (as->bus, as->devfn); +} +/* * These 'fault' reasons have an overloaded meaning since they are not only * intended for describing reasons that generate an IO_PAGE_FAULT as per the AMD @@ -459,7 +469,7 @@ static gboolean amdvi_find_as_by_devid(gpointer key, gpointer value,const AMDVIAsKey *as = key;const uint16_t *devidp = user_data; -return *devidp ==PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +return *devidp ==amdvi_get_devid (as->bus, as->devfn); }static AMDVIAddressSpace *amdvi_get_as_by_devid (AMDVIState *s,uint16_t devid)@@ -664,7 +674,7 @@ static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,static int amdvi_as_to_dte (AMDVIAddressSpace *as,uint64_t *dte) { -uint16_t devid =PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +uint16_t devid =amdvi_get_as_devid (as);AMDVIState *s = as->iommu_state;if (!amdvi_get_dte (s, devid, dte)) {@@ -763,6 +773,7 @@ static int fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,uint8_t pt_level, next_pt_level;IOMMUAccessFlags perms;int ret; +uint16_t devid =amdvi_get_as_devid (as); *page_size =0 ;@@ -780,7 +791,7 @@ static int fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,* Pointer and indexing the top level table using the IOVA from the request. */ pte_addr =NEXT_PTE_ADDR (dte, pt_level, address); - *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, as->devfn); + *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, devid);if (*pte == (uint64_t )-1 ) {/* @@ -836,7 +847,7 @@ static int fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, *page_size =PTE_LEVEL_PAGE_SIZE (pt_level); pte_addr =NEXT_PTE_ADDR (*pte, pt_level, address); - *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, as->devfn); + *pte =amdvi_get_pte_entry (as->iommu_state, pte_addr, devid);if (*pte == (uint64_t )-1 ) {/* Failure to read PTE. Page walk skips a page_size chunk */ @@ -1897,6 +1908,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,uint8_t mode;uint64_t pte;int fetch_ret; +uint16_t devid =amdvi_get_as_devid (as);/* make sure the DTE has TV = 1 */ if (!(dte[0 ] &AMDVI_DEV_TRANSLATION_VALID )) {@@ -1930,7 +1942,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,if (fetch_ret <0 || !IOMMU_PTE_PRESENT (pte) || perms != (perms &amdvi_get_perms (pte))) { -amdvi_page_fault (as->iommu_state, as->devfn, addr, perms); +amdvi_page_fault (as->iommu_state, devid, addr, perms);trace_amdvi_page_fault (addr);return ; }
hw/i386/amd_iommu.c static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr, · line 1975
Quoted 8 lines
hw/i386/amd_iommu.c +1
-1@@ -1957,7 +1969,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr, bool is_write, IOMMUTLBEntry *ret) { AMDVIState *s = as->iommu_state; -uint16_t devid =PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +uint16_t devid =amdvi_get_as_devid (as);AMDVIIOTLBEntry *iotlb_entry =amdvi_iotlb_lookup (s, addr, devid);uint64_t entry[4 ];int dte_ret;
Thanks
Sairaj
PATCH v2 3/5 amd_iommu: Define MMIO register masks
In reply to Dongli ZhangThe MMIO read-only and write-1-to-clear masks used by amdvi_init() are currently hardcoded. The hardcoded values look ambiguous.
Define named masks for the AMD-Vi MMIO registers to make the code easier to read and understand. Use status bit names that match the Linux AMD IOMMU driver definitions.
This is a cleanup.
--- hw/i386/amd_iommu.c | 5 +++-- hw/i386/amd_iommu.h | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-)
hw/i386/amd_iommu.c +3 -2
@@ -2563,8 +2563,9 @@ static void amdvi_init(AMDVIState *s)memset (s->mmior,0 ,AMDVI_MMIO_SIZE );amdvi_set_quad (s,AMDVI_MMIO_EXT_FEATURES ,amdvi_extended_feature_register (s), -0xffffffffffffffef ,0 ); -amdvi_set_quad (s,AMDVI_MMIO_STATUS ,0 ,0x98 ,0x67 ); +AMDVI_MMIO_EXT_FEATURE_RO_MASK ,0 ); +amdvi_set_quad (s,AMDVI_MMIO_STATUS ,0 ,AMDVI_MMIO_STATUS_RO_MASK , +AMDVI_MMIO_STATUS_W1C_MASK ); }static void amdvi_pci_realize (PCIDevice *pdev,Error **errp)
hw/i386/amd_iommu.h +20 -4
@@ -107,11 +107,25 @@#define AMDVI_MMIO_CONTROL_INTCAPXTEN (1ULL << 51)/* MMIO status register bits */ -#define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4) -#define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3) -#define AMDVI_MMIO_STATUS_COMP_INT (1 << 2) -#define AMDVI_MMIO_STATUS_EVENT_INT (1 << 1)#define AMDVI_MMIO_STATUS_EVT_OVF (1 << 0) +#define AMDVI_MMIO_STATUS_EVENT_INT (1 << 1) +#define AMDVI_MMIO_STATUS_COMP_INT (1 << 2) +#define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3) +#define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4) +#define AMDVI_MMIO_STATUS_PPR_OVF (1 << 5) +#define AMDVI_MMIO_STATUS_PPR_INT (1 << 6) +#define AMDVI_MMIO_STATUS_PPR_RUN (1 << 7) +#define AMDVI_MMIO_STATUS_GALOG_RUN (1 << 8) +#define AMDVI_MMIO_STATUS_GALOG_OVF (1 << 9) +#define AMDVI_MMIO_STATUS_GALOG_INT (1 << 10) + +#define AMDVI_MMIO_STATUS_RO_MASK \ + (AMDVI_MMIO_STATUS_EVT_RUN | AMDVI_MMIO_STATUS_CMDBUF_RUN | \ + AMDVI_MMIO_STATUS_PPR_RUN) +#define AMDVI_MMIO_STATUS_W1C_MASK \ + (AMDVI_MMIO_STATUS_EVT_OVF | AMDVI_MMIO_STATUS_EVENT_INT | \ + AMDVI_MMIO_STATUS_COMP_INT | AMDVI_MMIO_STATUS_PPR_OVF | \ + AMDVI_MMIO_STATUS_PPR_INT)#define AMDVI_CMDBUF_ID_BYTE 0x07#define AMDVI_CMDBUF_ID_RSHIFT 4
@@ -227,6 +241,8 @@ AMDVI_FEATURE_IA | AMDVI_FEATURE_GT | AMDVI_FEATURE_HE | \ AMDVI_GATS_MODE | AMDVI_HATS_MODE | AMDVI_FEATURE_GA) +#define AMDVI_MMIO_EXT_FEATURE_RO_MASK 0xffffffffffffffefULL +/* capabilities header */ #define AMDVI_CAPAB_FEATURES (AMDVI_CAPAB_FLAT_EXT | \ AMDVI_CAPAB_FLAG_NPCACHE | AMDVI_CAPAB_FLAG_IOTLBSUP \
Signature · 1 line
2.39.3
Re: [PATCH v2 3/5] amd_iommu: Define MMIO register masks
Quoted 8 lines · Dongli Zhang
The MMIO read-only and write-1-to-clear masks used by
amdvi_init()are currently hardcoded. The hardcoded values look ambiguous.Define named masks for the AMD-Vi MMIO registers to make the code easier to read and understand. Use status bit names that match the Linux AMD IOMMU driver definitions.
This is a cleanup.
Instead of "This is a cleanup", we usually use "No functional change is intended"
Quoted 38 lines
Suggested-by Sairaj Kodilkar <[email protected]>Signed-off-by Dongli Zhang <[email protected]>--- hw/i386/amd_iommu.c | 5 +++-- hw/i386/amd_iommu.h | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-)hw/i386/amd_iommu.c +3
-2@@ -2563,8 +2563,9 @@ static void amdvi_init(AMDVIState *s)memset (s->mmior,0 ,AMDVI_MMIO_SIZE );amdvi_set_quad (s,AMDVI_MMIO_EXT_FEATURES ,amdvi_extended_feature_register (s), -0xffffffffffffffef ,0 ); -amdvi_set_quad (s,AMDVI_MMIO_STATUS ,0 ,0x98 ,0x67 ); +AMDVI_MMIO_EXT_FEATURE_RO_MASK ,0 ); +amdvi_set_quad (s,AMDVI_MMIO_STATUS ,0 ,AMDVI_MMIO_STATUS_RO_MASK , +AMDVI_MMIO_STATUS_W1C_MASK ); }static void amdvi_pci_realize (PCIDevice *pdev,Error **errp)hw/i386/amd_iommu.h +1
-4@@ -107,11 +107,25 @@#define AMDVI_MMIO_CONTROL_INTCAPXTEN (1ULL << 51)/* MMIO status register bits */ -#define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4) -#define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3) -#define AMDVI_MMIO_STATUS_COMP_INT (1 << 2) -#define AMDVI_MMIO_STATUS_EVENT_INT (1 << 1)#define AMDVI_MMIO_STATUS_EVT_OVF (1 << 0) +#define AMDVI_MMIO_STATUS_EVENT_INT (1 << 1)
hw/i386/amd_iommu.h line 121
Quoted 10 lines
hw/i386/amd_iommu.h +10
-0+#define AMDVI_MMIO_STATUS_COMP_INT (1 << 2) +#define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3) +#define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4) +#define AMDVI_MMIO_STATUS_PPR_OVF (1 << 5) +#define AMDVI_MMIO_STATUS_PPR_INT (1 << 6) +#define AMDVI_MMIO_STATUS_PPR_RUN (1 << 7) +#define AMDVI_MMIO_STATUS_GALOG_RUN (1 << 8) +#define AMDVI_MMIO_STATUS_GALOG_OVF (1 << 9) +#define AMDVI_MMIO_STATUS_GALOG_INT (1 << 10) +
Please make macro names consistent, here you are defining event and ppr log related macros without "LOG" suffix but using "GALOG" for ga log. Either one is fine but make sure that it is consistent.
Other that this,
Quoted 21 lines
hw/i386/amd_iommu.h +7
-0+#define AMDVI_MMIO_STATUS_RO_MASK \ + (AMDVI_MMIO_STATUS_EVT_RUN | AMDVI_MMIO_STATUS_CMDBUF_RUN | \ + AMDVI_MMIO_STATUS_PPR_RUN) +#define AMDVI_MMIO_STATUS_W1C_MASK \ + (AMDVI_MMIO_STATUS_EVT_OVF | AMDVI_MMIO_STATUS_EVENT_INT | \ + AMDVI_MMIO_STATUS_COMP_INT | AMDVI_MMIO_STATUS_PPR_OVF | \ + AMDVI_MMIO_STATUS_PPR_INT)#define
AMDVI_CMDBUF_ID_BYTE0x07
#defineAMDVI_CMDBUF_ID_RSHIFT4hw/i386/amd_iommu.h +2
-0@@ -227,6 +241,8 @@ AMDVI_FEATURE_IA | AMDVI_FEATURE_GT | AMDVI_FEATURE_HE | \ AMDVI_GATS_MODE | AMDVI_HATS_MODE | AMDVI_FEATURE_GA) +#define AMDVI_MMIO_EXT_FEATURE_RO_MASK 0xffffffffffffffefULL +/* capabilities header */ #define AMDVI_CAPAB_FEATURES (AMDVI_CAPAB_FLAT_EXT | \ AMDVI_CAPAB_FLAG_NPCACHE | AMDVI_CAPAB_FLAG_IOTLBSUP \
PATCH v2 4/5 amd_iommu: Make extended feature register read-only
In reply to Dongli ZhangAMDVI_MMIO_EXT_FEATURES is populated from QEMU's advertised AMD-Vi extended feature set during reset. Guest writes to this register do not enable or disable any corresponding QEMU emulation behavior.
The current mask leaves bit 4 writable, which only allows the guest to modify the stored feature value without changing the device model. Make the register fully read-only so the visible feature bits remain controlled by QEMU.
According to the AMD IOMMU SDM, bits 25:24 (DualPprLogSup), 29:28 (DualEventLogSup), and 37 (USSup) are not specified as RO. This commit modifies only bit 4 (GTSup), while leaving the rest as they are.
--- hw/i386/amd_iommu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/amd_iommu.h +1 -1
@@ -241,7 +241,7 @@ AMDVI_FEATURE_IA | AMDVI_FEATURE_GT | AMDVI_FEATURE_HE | \ AMDVI_GATS_MODE | AMDVI_HATS_MODE | AMDVI_FEATURE_GA) -#define AMDVI_MMIO_EXT_FEATURE_RO_MASK 0xffffffffffffffefULL +#define AMDVI_MMIO_EXT_FEATURE_RO_MASK (~0ULL)/* capabilities header */ #define AMDVI_CAPAB_FEATURES (AMDVI_CAPAB_FLAT_EXT | \
Signature · 1 line
2.39.3
Re: [PATCH v2 4/5] amd_iommu: Make extended feature register read-only
Quoted 25 lines · Dongli Zhang
AMDVI_MMIO_EXT_FEATURESis populated from QEMU's advertised AMD-Vi extended feature set during reset. Guest writes to this register do not enable or disable any corresponding QEMU emulation behavior.The current mask leaves bit 4 writable, which only allows the guest to modify the stored feature value without changing the device model. Make the register fully read-only so the visible feature bits remain controlled by QEMU.
According to the AMD IOMMU SDM, bits 25:24 (DualPprLogSup), 29:28 (DualEventLogSup), and 37 (USSup) are not specified as RO. This commit modifies only bit 4 (GTSup), while leaving the rest as they are.
Suggested-by Sairaj Kodilkar <[email protected]>Signed-off-by Dongli Zhang <[email protected]>--- hw/i386/amd_iommu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i386/amd_iommu.h +0
-0
hw/i386/amd_iommu.h line 244
Quoted 5 lines
hw/i386/amd_iommu.h +1
-1@@ -241,7 +241,7 @@ AMDVI_FEATURE_IA | AMDVI_FEATURE_GT | AMDVI_FEATURE_HE | \ AMDVI_GATS_MODE | AMDVI_HATS_MODE | AMDVI_FEATURE_GA) -#define AMDVI_MMIO_EXT_FEATURE_RO_MASK 0xffffffffffffffefULL +#define AMDVI_MMIO_EXT_FEATURE_RO_MASK (~0ULL)
Thanks
Sairaj
Quoted 3 lines
hw/i386/amd_iommu.h +0
-0/* capabilities header */ #define AMDVI_CAPAB_FEATURES (AMDVI_CAPAB_FLAT_EXT | \
PATCH v2 5/5 amd_iommu: Do not latch unsupported GA log status bits
In reply to Dongli ZhangWhen QEMU records an AMD-Vi event-log entry for an IO page fault, it sets the event interrupt status bit and injects the event interrupt.
Linux kernel prior to commit 2379f3485239 ("iommu/amd: Refactor IOMMU interrupt handling logic for Event, PPR, and GA logs") clears the status register with a combined mask AMD_IOMMU_INT_MASK when handling any IOMMU interrupt. That mask includes the
GA log interrupt (MMIO_STATUS_GALOG_INT_MASK) and
overflow (MMIO_STATUS_GALOG_OVERFLOW_MASK) bits, even when the interrupt being handled is only an event-log interrupt.
QEMU does not produce GA log entries or set GA log status. However, the emulated status register still accepts guest writes to the GA log status bits as normal writes. As a result, a guest status clear write that includes those bits can latch a spurious GA log overflow. The guest then observes GA log status that QEMU did not generate and repeatedly restarts the GA log.
The below guest dmesg logs are from v6.4 Linux kernel.
[ 57.818224] virtio-pci 0000:01:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x0009] [ 57.821130] virtio-pci 0000:01:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x0009] [ 57.824003] AMD-Vi: IOMMU GA Log overflow [ 57.825099] AMD-Vi: IOMMU GA Log restarting [ 57.826338] AMD-Vi: IOMMU GA Log overflow [ 57.827425] AMD-Vi: IOMMU GA Log restarting [ 57.828657] AMD-Vi: IOMMU GA Log overflow [ 57.829758] AMD-Vi: IOMMU GA Log restarting [ 57.830986] AMD-Vi: IOMMU GA Log overflow [ 57.832063] AMD-Vi: IOMMU GA Log restarting
Mainline Linux commit 2379f3485239 ("iommu/amd: Refactor IOMMU interrupt handling logic for Event, PPR, and GA logs") avoids triggering this by chance by clearing only the status bits for the log being handled. However, older guests still use the combined clear mask.
Add the GA log interrupt and overflow bits to the AMDVI_MMIO_STATUS w1cmask so guest writes cannot create unsupported GA log status. This does not implement GA log production. It only prevents guest status clear writes from creating GA log status that QEMU did not generate to emulated IO page fault for guest Linux kernel.
---v1->v2:
- As the previous commit replaced the hardcoded value 0x67 with a macro, update the macro instead of the hardcoded value used by amdvi_init().
hw/i386/amd_iommu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/i386/amd_iommu.h +2 -1
@@ -125,7 +125,8 @@#define AMDVI_MMIO_STATUS_W1C_MASK \ (AMDVI_MMIO_STATUS_EVT_OVF | AMDVI_MMIO_STATUS_EVENT_INT | \ AMDVI_MMIO_STATUS_COMP_INT | AMDVI_MMIO_STATUS_PPR_OVF | \ - AMDVI_MMIO_STATUS_PPR_INT) + AMDVI_MMIO_STATUS_PPR_INT | AMDVI_MMIO_STATUS_GALOG_OVF | \ + AMDVI_MMIO_STATUS_GALOG_INT)#define AMDVI_CMDBUF_ID_BYTE 0x07#define AMDVI_CMDBUF_ID_RSHIFT 4
Signature · 1 line
2.39.3
Re: [PATCH v2 5/5] amd_iommu: Do not latch unsupported GA log status bits
Quoted 57 lines · Dongli Zhang
When QEMU records an AMD-Vi event-log entry for an IO page fault, it sets the event interrupt status bit and injects the event interrupt.
Linux kernel prior to commit
2379f3485239("iommu/amd: Refactor IOMMU interrupt handling logic for Event, PPR, and GA logs") clears the status register with a combined maskAMD_IOMMU_INT_MASKwhen handling any IOMMU interrupt. That mask includes the
GA log interrupt (MMIO_STATUS_GALOG_INT_MASK) and
overflow (MMIO_STATUS_GALOG_OVERFLOW_MASK) bits, even when the interrupt being handled is only an event-log interrupt.QEMU does not produce GA log entries or set GA log status. However, the emulated status register still accepts guest writes to the GA log status bits as normal writes. As a result, a guest status clear write that includes those bits can latch a spurious GA log overflow. The guest then observes GA log status that QEMU did not generate and repeatedly restarts the GA log.
The below guest dmesg logs are from v6.4 Linux kernel.
[ 57.818224] virtio-pci 0000:01:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x0009] [ 57.821130] virtio-pci 0000:01:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0xc0000000 flags=0x0009] [ 57.824003] AMD-Vi: IOMMU GA Log overflow [ 57.825099] AMD-Vi: IOMMU GA Log restarting [ 57.826338] AMD-Vi: IOMMU GA Log overflow [ 57.827425] AMD-Vi: IOMMU GA Log restarting [ 57.828657] AMD-Vi: IOMMU GA Log overflow [ 57.829758] AMD-Vi: IOMMU GA Log restarting [ 57.830986] AMD-Vi: IOMMU GA Log overflow [ 57.832063] AMD-Vi: IOMMU GA Log restartingMainline Linux commit
2379f3485239("iommu/amd: Refactor IOMMU interrupt handling logic for Event, PPR, and GA logs") avoids triggering this by chance by clearing only the status bits for the log being handled. However, older guests still use the combined clear mask.Add the GA log interrupt and overflow bits to the
AMDVI_MMIO_STATUSw1cmask so guest writes cannot create unsupported GA log status. This does not implement GA log production. It only prevents guest status clear writes from creating GA log status that QEMU did not generate to emulated IO page fault for guest Linux kernel.Fixes 12499b23315b ("x86_iommu/amd: Enable Guest virtual APIC support")Signed-off-by Dongli Zhang <[email protected]>---
v1->v2:
- As the previous commit replaced the hardcoded value0x67with a macro, update the macro instead of the hardcoded value used byamdvi_init().hw/i386/amd_iommu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/i386/amd_iommu.h +0
-0
hw/i386/amd_iommu.h line 130
Quoted 7 lines
hw/i386/amd_iommu.h +2
-1@@ -125,7 +125,8 @@#define AMDVI_MMIO_STATUS_W1C_MASK \ (AMDVI_MMIO_STATUS_EVT_OVF | AMDVI_MMIO_STATUS_EVENT_INT | \ AMDVI_MMIO_STATUS_COMP_INT | AMDVI_MMIO_STATUS_PPR_OVF | \ - AMDVI_MMIO_STATUS_PPR_INT) + AMDVI_MMIO_STATUS_PPR_INT | AMDVI_MMIO_STATUS_GALOG_OVF | \ + AMDVI_MMIO_STATUS_GALOG_INT)
Please add AMDVI_MMIO_STATUS_GALOG_RUN to AMDVI_MMIO_STATUS_RO_MASK.
Thanks
Sairaj
Quoted 2 lines
hw/i386/amd_iommu.h +0
-0#define AMDVI_CMDBUF_ID_BYTE 0x07#define AMDVI_CMDBUF_ID_RSHIFT 4
Ping?
Thank you very much!
Dongli Zhang
Quoted 124 lines · Dongli Zhang
v1: https://lore.kernel.org/qemu-devel/[email protected]
v1->v2:
- Add Fixes tag.
- Renameamdvi_as_devid()toamdvi_get_as_devid().
- Cleanup and add macro to avoid hardcoding.
- Make AMD IOMMU extended feature register read-only.
- Drop the patch for enabling I/O page fault delivery when xtsup=on.Recently, I tried using QEMU IOMMU emulation to study PCI device IOMMU page faults.
It works well with intel-iommu.
[ 28.819456] DMAR: DRHD: handling fault status reg 2 [ 28.820403] DMAR: [DMA Write NO_PASID] Request device [01:00.0] fault addr 0xc0000000 [fault reason 0x05] PTE Write access is not set [ 28.822325] DMAR: Dump dmar0 table entries for IOVA 0xc0000000 [ 28.823289] DMAR: root entry: 0x00000001014ef001 [ 28.823291] DMAR: context entry: hi 0x0000000000000602, low 0x00000001014ee001 [ 28.825266] DMAR: pte level: 4, pte value: 0x000000010208f403 [ 28.826211] DMAR: pte level: 3, pte value: 0x00000001020c8403 [ 28.827158] DMAR: pte level: 2, pte value: 0x0000000000000000 [ 28.828109] DMAR: page table not present at level 1However, there are some issues with amd-iommu. This patchset aims to address these issues through bugfixes and cleanup.
- Fix duplicate MSI capabilities for the IOMMU PCI device.
- Fix an invalid PCI address reported in I/O page fault events.
- Fix excessive GA log messages causing endless guest dmesg output.The QEMU test patch I used to inject IOMMU IO page faults has been appended to the end of this email. To execute "info version" HMP command will be able to trigger an IO page fault.
The following is the QEMU command line. I have tested these changes with both 7.1 and v6.4 guest kernels. I have also tested with both xtsup=off and xtsup=on.
qemu-system-x86_64 \
(quoted diff) +0
-11-machine q35,accel=kvm,kernel-irqchip=split \ -smp 4 -m 8G \ -cpu host \ -hda ol94.qcow2 \ -device pcie-root-port,io-reserve=0,pref64-reserve=32M,bus=pcie.0,chassis=40,id=pcie-root-port.1,addr=05.00,multifunction=on \ -device virtio-net-pci,netdev=tapnet,bus=pcie-root-port.1,mq=true,vectors=9,mac=52:54:11:22:14:47,disable-legacy=on,iommu_platform=true,packed=true \ -netdev tap,id=tapnet,script=qemu-ifup,downscript=no,queues=4,vhost=off \ -monitor stdio -vnc :18 \ -device amd-iommu,dma-remap=true,xtsup=off \ -kernel mainline-linux/arch/x86_64/boot/bzImage \ -append "root=/dev/sda1 init=/sbin/init text loglevel=7 console=ttyS0 clocksource=tsc iommu=force iommu.passthrough=0 amd_iommu=force_isolation iommu.strict=1"Dongli Zhang(5):
amd_iommu: Do not create duplicate MSI capability
amd_iommu: Use full BDF when reporting page faults
amd_iommu: Define MMIO register masks
amd_iommu: Make extended feature register read-only
amd_iommu: Do not latch unsupported GA log status bitshw/i386/amd_iommu.c | 34 +++++++++++++++++++++------------- hw/i386/amd_iommu.h | 25 +++++++++++++++++++++---- 2 files changed, 42 insertions(+), 17 deletions(-)Here is the QEMU test patch to inject IO page fault.
hw/virtio/virtio.c +9
-0@@ -1877,6 +1877,8 @@ err_undo_map: goto done; } +bool testsys_debug = false; +static void *virtqueue_packed_pop (VirtQueue *vq,size_t sz) {unsigned int i, max;@@ -1951,6 +1953,13 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) do { bool map_ok; + if (testsys_debug && (desc.addr & 0xff000000) == 0xff000000) { + testsys_debug = false; + printf("fault: virtqueue_packed_pop() desc.addr=0x%lx\n", desc.addr); + desc.addr = 0xc0000000; + desc.len = 1; + } + if (desc.flags & VRING_DESC_F_WRITE) { map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num, iov + out_num,monitor/hmp-cmds.c +5
-0@@ -114,6 +114,8 @@ void hmp_info_name(Monitor *mon, const QDict *qdict)qapi_free_NameInfo (info); } +extern bool testsys_debug; +void hmp_info_version (Monitor *mon,const QDict *qdict) {VersionInfo *info;@@ -125,6 +127,9 @@ void hmp_info_version(Monitor *mon, const QDict *qdict) info->package);qapi_free_VersionInfo (info); + +printf ("fault: enable one-time debug\n" ); + testsys_debug = true; }void hmp_quit (Monitor *mon,const QDict *qdict)Thank you very much!
Dongli Zhang