hw/i386/amd_iommu: fix incorrect page_size hugepages
cover hw/i386/amd_iommu: fix incorrect page_size hugepages
I have observed the IOMMU using the page size for the next level instead of the current level for hugepages, e.g. using 4K for what should be 2M pages.
The issue can be observed under Linux, but only with specific settings as Linux tends to set NextLevel=7, which directly sets the page size after the walk.
David Hoppenbrouwers (2):
hw/i386/amd_iommu.c: trace page walk in fetch_pte() hw/i386/amd_iommu.c: fix incorrect page_size for hugepages
hw/i386/amd_iommu.c | 21 ++++++++++++++++++--- hw/i386/trace-events | 4 ++++ 2 files changed, 22 insertions(+), 3 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/2 hw/i386/amd_iommu.c: trace page walk in fetch_pte()
This is necessary to demonstrate the issue in the next commit. It is also useful when developing drivers.
--- hw/i386/amd_iommu.c | 8 ++++++++ hw/i386/trace-events | 4 ++++ 2 files changed, 12 insertions(+)
hw/i386/amd_iommu.c +8 -0
@@ -667,6 +667,8 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,uint8_t level, mode;uint64_t pte_addr; +trace_amdvi_fetch_pte_translate (address); + *pte = dte; *page_size =0 ;
@@ -691,7 +693,11 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,return -AMDVI_FR_PT_ROOT_INV ; } +trace_amdvi_fetch_pte_root (level, *pte); + do { + trace_amdvi_fetch_pte_walk(level, *pte, PTE_NEXT_LEVEL(*pte), *page_size); + level -= 1; /* Update the page_size */
@@ -750,6 +756,8 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, *page_size =large_pte_page_size (*pte); } +trace_amdvi_fetch_pte_found (level, *pte,PTE_NEXT_LEVEL (*pte), *page_size); +return 0 ; }
hw/i386/trace-events +4 -0
@@ -118,6 +118,10 @@ amdvi_ir_intctl(uint8_t val) "int_ctl 0x%"PRIx8 amdvi_ir_target_abort(const char *str) "%s" amdvi_ir_delivery_mode(const char *str) "%s" amdvi_ir_irte_ga_val(uint64_t hi, uint64_t lo) "hi 0x%"PRIx64" lo 0x%"PRIx64 +amdvi_fetch_pte_translate(uint64_t address) "0x%016"PRIx64 +amdvi_fetch_pte_root(uint8_t level, uint64_t pte) "level=%d pte=%016"PRIx64 +amdvi_fetch_pte_walk(uint8_t level, uint64_t pte, uint8_t nextlevel, uint64_t page_size) "level=%d pte=%016"PRIx64" NextLevel=%d page_size=0x%"PRIx64 +amdvi_fetch_pte_found(uint8_t level, uint64_t pte, uint8_t nextlevel, uint64_t page_size) "level=%d pte=%016"PRIx64" NextLevel=%d page_size=0x%"PRIx64 # vmport.c vmport_register(unsigned char command, void *func, void *opaque) "command: 0x%02x func: %p opaque: %p"
Signature · 1 line
2.52.0
PATCH 2/2 hw/i386/amd_iommu.c: fix incorrect page_size for hugepages
In reply to David Hoppenbrouwersfetch_pte() incorrectly calculated the page_size for the next level before checking whether the entry at the current level is a leaf.
The incorrect behavior can be observed with a nested Linux guest under specific conditions (using Alpine with linux-stable kernel):
1. Define the first guest as such:
/path/to/qemu-system-x86_64 \
-M q35 \
-enable-kvm \
-cpu host \
-m 8G \
-device amd-iommu,dma-remap=on \
-device virtio-blk,drive=boot,bootindex=1 \
-device nvme,drive=vfio,bootindex=2,serial=nvme-vfio-0 \
-device virtio-net-pci,netdev=net \
-netdev user,id=net,hostfwd=tcp::2223-:22 \
-serial unix:/tmp/genesys.unix \
-drive if=none,id=boot,file=alpine-vm.qcow2,format=qcow2 \
-drive if=none,id=vfio,file=vfio.qcow2,format=qcow2 \
-display none \
-monitor stdio \
-trace 'pci*' \
-trace 'amdvi_fetch_pte*'Add -cdrom path-to-alpine.iso for the first run to install Alpine.
2. In /etc/update-extlinux.conf, add the following to default_kernel_opts:
iommu.strict=1 amd_iommu_dump=1 amd_iommu=v2_pgsizes_only,pgtbl_v2
v2_pgsizes_only is important to coerce Linux into using NextLevel=0 for 2M pages.
3. Add /etc/modprobe.d/vfio.conf with the following content:
/etc/modprobe.d/vfio.conf
options vfio-pci ids=1234:11e8,1b36:0010 options vfio_iommu_type1 allow_unsafe_interrupts=1 softdep igb pre: vfio-pci
4. In /etc/mkinitfs/mkinitfs.conf, add vfio and hugetlbfs to features.
5. In /etc/sysctl.conf, add vm.nr_hugepages=512
6. mkdir /hugepages
7. In /etc/fstab, add hugetlbfs:
hugetlbfs /hugepages hugetlbfs defaults 0 0
8. Reboot
9. Define the second, nested guest as such:
qemu-system-x86_64 \
-M q35 \
-enable-kvm \
-cpu host \
-m 512M \
-mem-prealloc \
-mem-path /hugepages/qemu \
-display none \
-serial stdio \
-device vfio-pci,host=0000:00:04.0 \
-cdrom path-to-alpine.iso
scp the original ISO into the guest.
Doublecheck -device with lspci -nn.If you launch the second guest inside the first guest without this patch, you will observe that booting gets stuck / takes a very long time:
ISOLINUX 6.04 6.04-pre1 Copyright (C) 1994-2015 H. Peter Anvin et al
boot:The following trace can be observed:
amdvi_fetch_pte_translate 0x0000000002867000
amdvi_fetch_pte_root level=3 pte=6000000103629603
amdvi_fetch_pte_walk level=3 pte=6000000103629603 NextLevel=3 page_size=0x40000000
amdvi_fetch_pte_walk level=2 pte=600000010362c401 NextLevel=2 page_size=0x200000
amdvi_fetch_pte_walk level=1 pte=700000016b400001 NextLevel=0 page_size=0x1000
amdvi_fetch_pte_found level=0 pte=700000016b400001 NextLevel=0 page_size=0x1000
amdvi_fetch_pte_translate 0x0000000002e0e000
amdvi_fetch_pte_root level=3 pte=6000000103629603
amdvi_fetch_pte_walk level=3 pte=6000000103629603 NextLevel=3 page_size=0x40000000
amdvi_fetch_pte_walk level=2 pte=600000010362c401 NextLevel=2 page_size=0x200000
amdvi_fetch_pte_walk level=1 pte=700000016b200001 NextLevel=0 page_size=0x1000
amdvi_fetch_pte_found level=0 pte=700000016b200001 NextLevel=0 page_size=0x1000Note that NextLevel skips from 2 to 0, indicating a hugepage. However, it incorrectly determined the page_size to be 0x1000 when it should be 0x200000. It doesn't seem the "host" (first guest) observes this mismatch, but the second guest is clearly affected.
I have observed it booting eventually, but I don't remember how long it took. If/when it does, run setup-alpine. When it asks about which disk to use it will be missing the NVMe drive.
If you apply this patch for the first guest then the second guest will boot much faster and see the NVMe drive. The trace will be longer and look like this:
...
amdvi_fetch_pte_translate 0x000000001ffd8000
amdvi_fetch_pte_root level=3 pte=600000010373e603
amdvi_fetch_pte_walk level=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000
amdvi_fetch_pte_walk level=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000
amdvi_fetch_pte_walk level=1 pte=700000014b800001 NextLevel=0 page_size=0x200000
amdvi_fetch_pte_found level=0 pte=700000014b800001 NextLevel=0 page_size=0x200000
amdvi_fetch_pte_translate 0x0000000000007c00
amdvi_fetch_pte_root level=3 pte=600000010373e603
amdvi_fetch_pte_walk level=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000
amdvi_fetch_pte_walk level=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000
amdvi_fetch_pte_walk level=1 pte=600000010366c201 NextLevel=1 page_size=0x200000
amdvi_fetch_pte_found level=0 pte=700000016b807001 NextLevel=0 page_size=0x1000
amdvi_fetch_pte_translate 0x000000001ffdc000
amdvi_fetch_pte_root level=3 pte=600000010373e603
amdvi_fetch_pte_walk level=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000
amdvi_fetch_pte_walk level=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000
amdvi_fetch_pte_walk level=1 pte=700000014b800001 NextLevel=0 page_size=0x200000
amdvi_fetch_pte_found level=0 pte=700000014b800001 NextLevel=0 page_size=0x200000
...--- hw/i386/amd_iommu.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
hw/i386/amd_iommu.c +10 -3
@@ -684,6 +684,13 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, level = mode =get_pte_translation_mode (dte);assert (mode >0 && mode <7 ); +/* +* TODO what is the actual behavior if NextLevel=0 or 7 in the root? +* For now, set the page_size for the root to be consistent with earlier +* QEMU versions, +*/ + *page_size =PTE_LEVEL_PAGE_SIZE (level); +/* * If IOVA is larger than the max supported by the current pgtable level, * there is nothing to do.
@@ -700,9 +707,6 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, level -=1 ; -/* Update the page_size */ - *page_size =PTE_LEVEL_PAGE_SIZE (level); -/* Permission bits are ANDed at every level, including the DTE */ perms &=amdvi_get_perms (*pte);if (perms ==IOMMU_NONE ) {
@@ -720,6 +724,9 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,break ; } +/* Update the page_size */ + *page_size =PTE_LEVEL_PAGE_SIZE (level); +/* * Index the pgtable using the IOVA bits corresponding to current level * and walk down to the lower level.
Signature · 1 line
2.52.0
Re: [PATCH 2/2] hw/i386/amd_iommu.c: fix incorrect page_size for hugepages
Quoted 119 lines · David Hoppenbrouwers
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
fetch_pte()incorrectly calculated thepage_sizefor the next level before checking whether the entry at the current level is a leaf.The incorrect behavior can be observed with a nested Linux guest under specific conditions (using Alpine with linux-stable kernel):
1. Define the first guest as such:
/path/to/qemu-system-x86_64 \ -M q35 \ -enable-kvm \ -cpu host \ -m 8G \ -device amd-iommu,dma-remap=on \ -device virtio-blk,drive=boot,bootindex=1 \ -device nvme,drive=vfio,bootindex=2,serial=nvme-vfio-0 \ -device virtio-net-pci,netdev=net \ -netdev user,id=net,hostfwd=tcp::2223-:22 \ -serial unix:/tmp/genesys.unix \ -drive if=none,id=boot,file=alpine-vm.qcow2,format=qcow2 \ -drive if=none,id=vfio,file=vfio.qcow2,format=qcow2 \ -display none \ -monitor stdio \ -trace 'pci*' \ -trace 'amdvi_fetch_pte*' Add -cdrom path-to-alpine.iso for the first run to install Alpine.2. In
/etc/update-extlinux.conf, add the following todefault_kernel_opts:iommu.strict=1 amd_iommu_dump=1 amd_iommu=v2_pgsizes_only,pgtbl_v2
v2_pgsizes_onlyis important to coerce Linux into using NextLevel=0 for 2M pages.3. Add
/etc/modprobe.d/vfio.confwith the following content:
/etc/modprobe.d/vfio.conf
options vfio-pci ids=1234:11e8,1b36:0010 optionsvfio_iommu_type1allow_unsafe_interrupts=1 softdep igb pre: vfio-pci4. In
/etc/mkinitfs/mkinitfs.conf, add vfio and hugetlbfs to features.5. In
/etc/sysctl.conf, add vm.nr_hugepages=5126. mkdir /hugepages
7. In
/etc/fstab, add hugetlbfs:hugetlbfs /hugepages hugetlbfs defaults 0 0
8. Reboot
9. Define the second, nested guest as such:
qemu-system-x86_64 \ -M q35 \ -enable-kvm \ -cpu host \ -m 512M \ -mem-prealloc \ -mem-path /hugepages/qemu \ -display none \ -serial stdio \ -device vfio-pci,host=0000:00:04.0 \ -cdrom path-to-alpine.iso scp the original ISO into the guest. Doublecheck -device with lspci -nn.If you launch the second guest inside the first guest without this patch, you will observe that booting gets stuck / takes a very long time:
ISOLINUX 6.04 6.04-pre1 Copyright (C) 1994-2015 H. Peter Anvin et al boot:The following trace can be observed:
amdvi_fetch_pte_translate 0x0000000002867000 amdvi_fetch_pte_root level=3 pte=6000000103629603 amdvi_fetch_pte_walk level=3 pte=6000000103629603 NextLevel=3 page_size=0x40000000 amdvi_fetch_pte_walk level=2 pte=600000010362c401 NextLevel=2 page_size=0x200000 amdvi_fetch_pte_walk level=1 pte=700000016b400001 NextLevel=0 page_size=0x1000 amdvi_fetch_pte_found level=0 pte=700000016b400001 NextLevel=0 page_size=0x1000 amdvi_fetch_pte_translate 0x0000000002e0e000 amdvi_fetch_pte_root level=3 pte=6000000103629603 amdvi_fetch_pte_walk level=3 pte=6000000103629603 NextLevel=3 page_size=0x40000000 amdvi_fetch_pte_walk level=2 pte=600000010362c401 NextLevel=2 page_size=0x200000 amdvi_fetch_pte_walk level=1 pte=700000016b200001 NextLevel=0 page_size=0x1000 amdvi_fetch_pte_found level=0 pte=700000016b200001 NextLevel=0 page_size=0x1000Note that NextLevel skips from 2 to 0, indicating a hugepage. However, it incorrectly determined the
page_sizeto be0x1000when it should be0x200000. It doesn't seem the "host" (first guest) observes this mismatch, but the second guest is clearly affected.I have observed it booting eventually, but I don't remember how long it took. If/when it does, run setup-alpine. When it asks about which disk to use it will be missing the NVMe drive.
If you apply this patch for the first guest then the second guest will boot much faster and see the NVMe drive. The trace will be longer and look like this:
... amdvi_fetch_pte_translate 0x000000001ffd8000 amdvi_fetch_pte_root level=3 pte=600000010373e603 amdvi_fetch_pte_walk level=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000 amdvi_fetch_pte_walk level=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000 amdvi_fetch_pte_walk level=1 pte=700000014b800001 NextLevel=0 page_size=0x200000 amdvi_fetch_pte_found level=0 pte=700000014b800001 NextLevel=0 page_size=0x200000 amdvi_fetch_pte_translate 0x0000000000007c00 amdvi_fetch_pte_root level=3 pte=600000010373e603 amdvi_fetch_pte_walk level=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000 amdvi_fetch_pte_walk level=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000 amdvi_fetch_pte_walk level=1 pte=600000010366c201 NextLevel=1 page_size=0x200000
Quoted 8 lines
amdvi_fetch_pte_found level=0 pte=700000016b807001 NextLevel=0 page_size=0x1000
amdvi_fetch_pte_translate 0x000000001ffdc000
amdvi_fetch_pte_root level=3 pte=600000010373e603
amdvi_fetch_pte_walk level=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000
amdvi_fetch_pte_walk level=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000
amdvi_fetch_pte_walk level=1 pte=700000014b800001 NextLevel=0 page_size=0x200000
amdvi_fetch_pte_found level=0 pte=700000014b800001 NextLevel=0 page_size=0x200000
...Hi David,
Thanks for the fix, here are few nits,
Please move the description and reproduction steps of the bug to the cover letter and include only necessary information about the bug and what patch is doing to resolve it. Also keep the subsystem name as "amd_iommu" in the commit message.
Please see [1] for more information about commit message
[1] https://www.qemu.org/docs/master/devel/submitting-a-patch.html#write-a-meaningful-commit-message
Quoted 45 lines
Signed-off-by David Hoppenbrouwers <[email protected]>--- hw/i386/amd_iommu.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)hw/i386/amd_iommu.c +10
-4@@ -684,6 +684,13 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, level = mode =get_pte_translation_mode (dte);assert (mode >0 && mode <7 ); +/* +* TODO what is the actual behavior if NextLevel=0 or 7 in the root? +* For now, set the page_size for the root to be consistent with earlier +* QEMU versions, +*/ + *page_size =PTE_LEVEL_PAGE_SIZE (level); +/* * If IOVA is larger than the max supported by the current pgtable level, * there is nothing to do. @@ -700,9 +707,6 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, level -=1 ; -/* Update the page_size */ - *page_size =PTE_LEVEL_PAGE_SIZE (level); -/* Permission bits are ANDed at every level, including the DTE */ perms &=amdvi_get_perms (*pte);if (perms ==IOMMU_NONE ) {@@ -720,6 +724,9 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,break ; } +/* Update the page_size */ + *page_size =PTE_LEVEL_PAGE_SIZE (level); +/* * Index the pgtable using the IOVA bits corresponding to current level * and walk down to the lower level. --
Quoted 1 line
2.52.0
Looking at the fetch_pte code, it was harder to read as it treats DTE as regular PTE and creates confusion about how levels are managed.
I propose following patch (tested with FIO) to resolve this bug...
hw/i386/amd_iommu.c +0 -0
@@ -658,13 +658,16 @@ static uint64_t large_pte_page_size(uint64_t pte)
* -AMDVI_FR_PT_ENTRY_INV: A PTE could not be read from guest memory during a * page table walk. This means that the DTE has valid data, but one of the * lower level entries in the Page Table could not be read.
+ *
+ * NOTE: fetch_pte assumes that the provided DTE is valid, i.e. caller has
+ * passed dte with bits 0 and 1 set
*/
static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, uint64_t *pte, hwaddr *page_size)
{
IOMMUAccessFlags perms = amdvi_get_perms(dte);
- uint8_t level, mode;
+ uint8_t level, mode, next_level;
uint64_t pte_addr;
*pte = dte;
@@ -691,51 +694,47 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, return -AMDVI_FR_PT_ROOT_INV;
}
- do {
- level -= 1;
-
- /* Update the page_size */
- *page_size = PTE_LEVEL_PAGE_SIZE(level);
-
- /* Permission bits are ANDed at every level, including the DTE */ - perms &= amdvi_get_perms(*pte);
- if (perms == IOMMU_NONE) {
- return 0;
- }
-
- /* Not Present */
- if (!IOMMU_PTE_PRESENT(*pte)) {
- return 0;
- }
+ *page_size = PTE_LEVEL_PAGE_SIZE(level);
- /* Large or Leaf PTE found */
- if (PTE_NEXT_LEVEL(*pte) == 7 || PTE_NEXT_LEVEL(*pte) == 0) { - /* Leaf PTE found */
- break;
- }
-
- /*
- * Index the pgtable using the IOVA bits corresponding to current level - * and walk down to the lower level.
- */
+ do {
pte_addr = NEXT_PTE_ADDR(*pte, level, address);
*pte = amdvi_get_pte_entry(as->iommu_state, pte_addr, as->devfn);
- if (*pte == (uint64_t)-1) {
+ if (*pte == (uint64_t) -1) {
/*
* A returned PTE of -1 indicates a failure to read the page table - * entry from guest memory.
+ * entry from guest memory
*/
if (level == mode - 1) {
/* Failure to retrieve the Page Table from Root Pointer */ *page_size = 0;
return -AMDVI_FR_PT_ROOT_INV;
} else {
- /* Failure to read PTE. Page walk skips a page_size chunk */ return -AMDVI_FR_PT_ENTRY_INV;
}
}
- } while (level > 0);
+
+ next_level = PTE_NEXT_LEVEL(*pte);
+ *page_size = PTE_LEVEL_PAGE_SIZE(level - 1);
+
+ /* Permission bits are ANDed at every level, including the DTE */ + perms &= amdvi_get_perms(*pte);
+
+ if (!IOMMU_PTE_PRESENT(*pte) || perms == IOMMU_NONE) {
+ return 0;
+ }
+
+ /* Large or Leaf PTE found */
+ if (next_level == 0 || next_level == 7) {
+ break;
+ }
+
+ /* AMD IOMMU supports level skip hence assign next_level to level + * instead of decrementing it
+ */
+ level = next_level;
+ } while(level > 0);
+
assert(PTE_NEXT_LEVEL(*pte) == 0 || PTE_NEXT_LEVEL(*pte) == 7 ||
level == 0);
@@ -745,7 +744,7 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte,
* the range starts in the middle of a contiguous page, the returned PTE
* must be the first PTE of the series.
*/
- if (PTE_NEXT_LEVEL(*pte) == 7) {
+ if (next_level == 7) {
/* Update page_size with the large PTE page size */
*page_size = large_pte_page_size(*pte);
}
@@ -840,6 +839,12 @@ static void amdvi_sync_shadow_page_table_range(AMDVIAddressSpace *as, uint64_t pte;
int ret;
+ /* Return if DTE is not valid */
+ if (!(dte[0] & AMDVI_DEV_TRANSLATION_VALID) || + !(dte[0] & AMDVI_DEV_VALID)) {
+ return;
+ }
+
while (iova < end) {
ret = fetch_pte(as, iova, dte[0], &pte, &pagesize);
hw/i386/amd_iommu.h +0 -0
@@ -190,7 +190,7 @@
#define PT_LEVEL_SHIFT(level) (12 + ((level) * 9))
/* Return IOVA bit group used to index the Page Table at specific level */ -#define PT_LEVEL_INDEX(level, iova) (((iova) >> PT_LEVEL_SHIFT(level)) & \
+#define PT_LEVEL_INDEX(level, iova) (((iova) >> PT_LEVEL_SHIFT(level - 1)) & \
GENMASK64(8, 0))
/* Return the max address for a specified level i.e. max_oaddr */ --
@David Could you please test above patch ?
I have rearraged some part of code. Along with it I have added a requirement that caller must validate DTE before calling fetch_pte. This requirement simplifies the loop.
If @David and @Alejandro are okay with this change, I can prepare and send official patch.
Thanks
Sairaj
Re: [PATCH 2/2] hw/i386/amd_iommu.c: fix incorrect page_size for hugepages
Quoted 348 lines · Sairaj Kodilkar
Quoted 119 lines · David Hoppenbrouwers
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
fetch_pte()incorrectly calculated thepage_sizefor the next level before checking whether the entry at the current level is a leaf.The incorrect behavior can be observed with a nested Linux guest under specific conditions (using Alpine with linux-stable kernel):
1. Define the first guest as such:
/path/to/qemu-system-x86_64 \
-M q35 \
-enable-kvm \
-cpu host \
-m 8G \
-device amd-iommu,dma-remap=on \
-device virtio-blk,drive=boot,bootindex=1 \
-device nvme,drive=vfio,bootindex=2,serial=nvme-vfio-0 \
-device virtio-net-pci,netdev=net \
-netdev user,id=net,hostfwd=tcp::2223-:22 \
-serial unix:/tmp/genesys.unix \
-drive if=none,id=boot,file=alpine-vm.qcow2,format=qcow2 \
-drive if=none,id=vfio,file=vfio.qcow2,format=qcow2 \
-display none \
-monitor stdio \
-trace 'pci*' \
-trace 'amdvi_fetch_pte*'Add -cdrom path-to-alpine.iso for the first run to install Alpine.
2. In
/etc/update-extlinux.conf, add the following todefault_kernel_opts:iommu.strict=1 amd_iommu_dump=1 amd_iommu=v2_pgsizes_only,pgtbl_v2
v2_pgsizes_onlyis important to coerce Linux into using NextLevel=0 for 2M pages.3. Add
/etc/modprobe.d/vfio.confwith the following content:
/etc/modprobe.d/vfio.conf
options vfio-pci ids=1234:11e8,1b36:0010 optionsvfio_iommu_type1allow_unsafe_interrupts=1 softdep igb pre: vfio-pci4. In
/etc/mkinitfs/mkinitfs.conf, add vfio and hugetlbfs to features.5. In
/etc/sysctl.conf, add vm.nr_hugepages=5126. mkdir /hugepages
7. In
/etc/fstab, add hugetlbfs:hugetlbfs /hugepages hugetlbfs defaults 0 0
8. Reboot
9. Define the second, nested guest as such:
qemu-system-x86_64 \
-M q35 \
-enable-kvm \
-cpu host \
-m 512M \
-mem-prealloc \
-mem-path /hugepages/qemu \
-display none \
-serial stdio \
-device vfio-pci,host=0000:00:04.0 \
-cdrom path-to-alpine.isoscp the original ISO into the guest.
Doublecheck -device with lspci -nn.If you launch the second guest inside the first guest without this patch, you will observe that booting gets stuck / takes a very long time:
ISOLINUX 6.04 6.04-pre1 Copyright (C) 1994-2015 H. Peter Anvin et al boot:
The following trace can be observed:
amdvi_fetch_pte_translate0x0000000002867000
amdvi_fetch_pte_rootlevel=3 pte=6000000103629603amdvi_fetch_pte_walklevel=3 pte=6000000103629603 NextLevel=3 page_size=0x40000000amdvi_fetch_pte_walklevel=2 pte=600000010362c401 NextLevel=2 page_size=0x200000amdvi_fetch_pte_walklevel=1 pte=700000016b400001 NextLevel=0 page_size=0x1000amdvi_fetch_pte_foundlevel=0 pte=700000016b400001 NextLevel=0 page_size=0x1000amdvi_fetch_pte_translate0x0000000002e0e000
amdvi_fetch_pte_rootlevel=3 pte=6000000103629603amdvi_fetch_pte_walklevel=3 pte=6000000103629603 NextLevel=3 page_size=0x40000000amdvi_fetch_pte_walklevel=2 pte=600000010362c401 NextLevel=2 page_size=0x200000amdvi_fetch_pte_walklevel=1 pte=700000016b200001 NextLevel=0 page_size=0x1000amdvi_fetch_pte_foundlevel=0 pte=700000016b200001 NextLevel=0 page_size=0x1000Note that NextLevel skips from 2 to 0, indicating a hugepage. However, it incorrectly determined the
page_sizeto be0x1000when it should be0x200000. It doesn't seem the "host" (first guest) observes this mismatch, but the second guest is clearly affected.I have observed it booting eventually, but I don't remember how long it took. If/when it does, run setup-alpine. When it asks about which disk to use it will be missing the NVMe drive.
If you apply this patch for the first guest then the second guest will boot much faster and see the NVMe drive. The trace will be longer and look like this:
...
amdvi_fetch_pte_translate0x000000001ffd8000
amdvi_fetch_pte_rootlevel=3 pte=600000010373e603
amdvi_fetch_pte_walklevel=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000amdvi_fetch_pte_walklevel=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000amdvi_fetch_pte_walklevel=1 pte=700000014b800001 NextLevel=0 page_size=0x200000amdvi_fetch_pte_foundlevel=0 pte=700000014b800001 NextLevel=0 page_size=0x200000amdvi_fetch_pte_translate0x0000000000007c00
amdvi_fetch_pte_rootlevel=3 pte=600000010373e603
amdvi_fetch_pte_walklevel=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000amdvi_fetch_pte_walklevel=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000amdvi_fetch_pte_walklevel=1 pte=600000010366c201 NextLevel=1 page_size=0x200000Quoted 8 lines
amdvi_fetch_pte_foundlevel=0 pte=700000016b807001 NextLevel=0 page_size=0x1000amdvi_fetch_pte_translate0x000000001ffdc000
amdvi_fetch_pte_rootlevel=3 pte=600000010373e603
amdvi_fetch_pte_walklevel=3 pte=600000010373e603 NextLevel=3 page_size=0x8000000000amdvi_fetch_pte_walklevel=2 pte=600000010370b401 NextLevel=2 page_size=0x40000000amdvi_fetch_pte_walklevel=1 pte=700000014b800001 NextLevel=0 page_size=0x200000amdvi_fetch_pte_foundlevel=0 pte=700000014b800001 NextLevel=0 page_size=0x200000
...Hi David,
Thanks for the fix, here are few nits,
Please move the description and reproduction steps of the bug to the cover letter and include only necessary information about the bug and what patch is doing to resolve it. Also keep the subsystem name as "
amd_iommu" in the commit message.Please see [1] for more information about commit message
[1] https://www.qemu.org/docs/master/devel/submitting-a- patch.html#write-a-meaningful-commit-messageQuoted 47 lines
Signed-off-by David Hoppenbrouwers <[email protected]>---
hw/i386/amd_iommu.c| 13 ++++++++++---1 file changed, 10 insertions(+), 3 deletions(-)hw/i386/amd_iommu.c +3
-0@@ -684,6 +684,13 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, level = mode = get_pte_translation_mode(dte); assert(mode > 0 && mode < 7); + /* +* TODO what is the actual behavior if NextLevel=0 or 7 in the root? +* For now, set the page_size for the root to be consistent with earlier
+ * QEMU versions,
+ */
+*page_size=PTE_LEVEL_PAGE_SIZE(level);
+
/*
* If IOVA is larger than the max supported by the current pgtable level,
* there is nothing to do.hw/i386/amd_iommu.c +0
-3@@ -700,9 +707,6 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, level -= 1; - /* Update the page_size */ - *page_size = PTE_LEVEL_PAGE_SIZE(level); - /* Permission bits are ANDed at every level, including the DTE */ perms &= amdvi_get_perms(*pte);if (perms ==
IOMMU_NONE) {hw/i386/amd_iommu.c +3
-0@@ -720,6 +724,9 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, break; } + /* Update the page_size */ + *page_size = PTE_LEVEL_PAGE_SIZE(level); + /** Index the pgtable using the IOVA bits corresponding to current level Quoted 3 lines
* and walk down to the lower level.
Signature · 1 line
2.52.0Looking at the
fetch_ptecode, it was harder to read as it treats DTE as regular PTE and creates confusion about how levels are managed.I propose following patch (tested with FIO) to resolve this bug...
hw/i386/amd_iommu.c +0
-0@@ -658,13 +658,16 @@ static uint64_t large_pte_page_size(uint64_t pte) * -AMDVI_FR_PT_ENTRY_INV: A PTE could not be read from guest memoryduring a
* page table walk. This means that the DTE has valid data, but
one of the
* lower level entries in the Page Table could not be read.
+ *
+ * NOTE:fetch_pteassumes that the provided DTE is valid, i.e. caller has
+ * passed dte with bits 0 and 1 set
*/
staticuint64_tfetch_pte(AMDVIAddressSpace *as, hwaddr address,uint64_tdte,
uint64_t*pte, hwaddr*page_size) {
IOMMUAccessFlags perms =amdvi_get_perms(dte);-
uint8_tlevel, mode;
+uint8_tlevel, mode,next_level;
uint64_tpte_addr;*pte = dte;
hw/i386/amd_iommu.c +1
-24@@ -691,51 +694,47 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, return -AMDVI_FR_PT_ROOT_INV; } - do { - level -= 1; - - /* Update the page_size */ - *page_size = PTE_LEVEL_PAGE_SIZE(level); - - /* Permission bits are ANDed at every level, including the DTE */ - perms &= amdvi_get_perms(*pte); - if (perms == IOMMU_NONE) { - return 0; - } - - /* Not Present */ - if (!IOMMU_PTE_PRESENT(*pte)) { - return 0; - } + *page_size = PTE_LEVEL_PAGE_SIZE(level); - /* Large or Leaf PTE found */ - if (PTE_NEXT_LEVEL(*pte) == 7 || PTE_NEXT_LEVEL(*pte) == 0) { - /* Leaf PTE found */ - break; - } - - /* - * Index the pgtable using the IOVA bits corresponding tocurrent level
- * and walk down to the lower level.
- */
+ do {
pte_addr=NEXT_PTE_ADDR(*pte, level, address);
*pte =amdvi_get_pte_entry(as->iommu_state, pte_addr, as->devfn);- if (*pte == (uint64_t)-1) {
+ if (*pte == (uint64_t) -1) {
/*
* A returned PTE of -1 indicates a failure to read the
page table
- * entry from guest memory.
+ * entry from guest memory
*/
if (level == mode - 1) {
/* Failure to retrieve the Page Table from Root
Pointer */
*page_size= 0;
return -AMDVI_FR_PT_ROOT_INV;
} else {
- /* Failure to read PTE. Page walk skips apage_sizechunk */
return -AMDVI_FR_PT_ENTRY_INV;
}
}
- } while (level > 0);hw/i386/amd_iommu.c +22
-0+ + next_level = PTE_NEXT_LEVEL(*pte); + *page_size = PTE_LEVEL_PAGE_SIZE(level - 1); + + /* Permission bits are ANDed at every level, including the DTE */ + perms &= amdvi_get_perms(*pte); + + if (!IOMMU_PTE_PRESENT(*pte) || perms == IOMMU_NONE) { + return 0; + } + + /* Large or Leaf PTE found */ + if (next_level == 0 || next_level == 7) { + break; + } + + /* AMD IOMMU supports level skip hence assign next_level to level + * instead of decrementing it + */ + level = next_level; + } while(level > 0); + assert(PTE_NEXT_LEVEL(*pte) == 0 || PTE_NEXT_LEVEL(*pte) == 7 || level == 0);@@ -745,7 +744,7 @@ static uint64_t fetch_pte(AMDVIAddressSpace *as, hwaddr address, uint64_t dte, * the range starts in the middle of a contiguous page, thereturned PTE
* must be the first PTE of the series.
*/
- if (PTE_NEXT_LEVEL(*pte)== 7) {
+ if (next_level== 7) {
/* Updatepage_sizewith the large PTE page size */*page_size=large_pte_page_size(*pte);
}hw/i386/amd_iommu.c +6
-0@@ -840,6 +839,12 @@ static void amdvi_sync_shadow_page_table_range(AMDVIAddressSpace *as, uint64_t pte; int ret; + /* Return if DTE is not valid */ + if (!(dte[0] & AMDVI_DEV_TRANSLATION_VALID) || + !(dte[0] & AMDVI_DEV_VALID)) { + return; + } + while (iova < end) {ret =
fetch_pte(as, iova, dte[0], &pte, &pagesize);hw/i386/amd_iommu.h +0
-0@@ -190,7 +190,7 @@ #define PT_LEVEL_SHIFT(level) (12 + ((level) * 9)) /* Return IOVA bit group used to index the Page Table at specificlevel */
-#definePT_LEVEL_INDEX(level, iova)(((iova) >>PT_LEVEL_SHIFT(level)) & \
+#definePT_LEVEL_INDEX(level, iova)(((iova) >>PT_LEVEL_SHIFT(level - 1)) & \
GENMASK64(8, 0))
Quoted 4 lines
/* Return the max address for a specified level i.e.
max_oaddr*/Signature · 1 line
@David Could you please test above patch ?
It appears to work fine with both the Linux VM and my own kernel project.
Quoted 4 lines
I have rearraged some part of code. Along with it I have added a requirement that caller must validate DTE before calling
fetch_pte. This requirement simplifies the loop.If @David and @Alejandro are okay with this change, I can prepare and send official patch.
It is fine with me. I don't need to send a v2?
Quoted 2 lines
Thanks
Sairaj
Re: [PATCH 2/2] hw/i386/amd_iommu.c: fix incorrect page_size for hugepages
Quoted 10 lines · David Hoppenbrouwers
Quoted 6 lines · Sairaj Kodilkar
On 2/25/2026 8:28 PM, David Hoppenbrouwers wrote:
@David Could you please test above patch ?
It appears to work fine with both the Linux VM and my own kernel project.
Thank you for reporting and testing this bug fix David. Like Sairaj I have not been able to reproduce it but it is clear there is an issue.
Quoted 10 lines
Quoted 7 lines
I have rearraged some part of code. Along with it I have added a requirement that caller
must validate DTE before callingfetch_pte. This requirement simplifies the loop.If @David and @Alejandro are okay with this change, I can prepare and send official patch.
It is fine with me. I don't need to send a v2?
No need for now, though I would appreciate your review and testing on the next round of patches. I am working on additional changes, essentially adding to Sairaj's observation/approach that we are better off following the Page Table Root pointer outside of the loop, so that we end up iterating simply on PDEs/PTE. I've been delayed by other work, but I wanted to ack your question.
Thank you,
Alejandro
Quoted 3 lines
Quoted 2 lines
Thanks
Sairaj