Skip to contentSkip to conversation

x86/iommu: improve setup time of hwdom IOMMU

PATCH Superseded Roger Pau Monne 4 participants 19 messages Jan 24, 2024 – Jan 29, 2024

lore
Share & tools
lore
#0 lore raw

v5 cover x86/iommu: improve setup time of hwdom IOMMU

Hello,

The aim of the series is to reduce boot time setup of IOMMU page tables for dom0.

This patches rework the hardware domain IOMMU setup to use a rangeset instead of iterating over all addresses up to the max RAM page. See patch 2/3 for performance figures.

Jan: I've kept your RB in patch 1 with the off-by-one adjustments, and dropped it for patch 2 with the addition of the default label in the switch over the e820 ranges. Such default label is required to continue to deal equally with the ACPI and NVS ranges.

Thanks, Roger.

Roger Pau Monne (3):
x86/iommu: remove regions not to be mapped
x86/iommu: switch hwdom IOMMU to use a rangeset
x86/iommu: cleanup unused functions

 CHANGELOG.md                        |   2 +
 xen/arch/x86/hvm/io.c               |  15 ++-
 xen/arch/x86/include/asm/hvm/io.h   |   4 +-
 xen/arch/x86/include/asm/setup.h    |   2 +-
 xen/arch/x86/setup.c                |  81 ++++++------
 xen/arch/x86/tboot.c                |   2 +-
 xen/drivers/passthrough/x86/iommu.c | 196 ++++++++++++----------------
 7 files changed, 146 insertions(+), 156 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/3 x86/iommu: remove regions not to be mapped

Introduce the code to remove regions not to be mapped from the rangeset that will be used to setup the IOMMU page tables for the hardware domain.

This change also introduces two new functions: remove_xen_ranges() and vpci_subtract_mmcfg() that copy the logic in xen_in_range() and vpci_is_mmcfg_address() respectively and remove the ranges that would otherwise be intercepted by the original functions.

Note that the rangeset is still not populated.

---
Changes since v4:
- Fix off-by-one when removing the Xen used ranges, as the rangesets are inclusive.

Changes since v3:
- Remove unnecessary line wrapping.

Changes since v1:
- Split from bigger patch.

---
 xen/arch/x86/hvm/io.c               | 16 ++++++++
 xen/arch/x86/include/asm/hvm/io.h   |  3 ++
 xen/arch/x86/include/asm/setup.h    |  1 +
 xen/arch/x86/setup.c                | 48 +++++++++++++++++++++++
 xen/drivers/passthrough/x86/iommu.c | 61 +++++++++++++++++++++++++++++
 5 files changed, 129 insertions(+)
xen/arch/x86/hvm/io.c +16 -0
index d75af83ad01f..a42854c52b65 100644
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));
+
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
+}
+
 static unsigned int vpci_mmcfg_decode_addr(const struct hvm_mmcfg *mmcfg,
                                            paddr_t addr, pci_sbdf_t *sbdf)
 {
xen/arch/x86/include/asm/hvm/io.h +3 -0
index a97731657801..e1e5e6fe7491 100644
@@ -156,6 +156,9 @@ void destroy_vpci_mmcfg(struct domain *d);
 /* Check if an address is between a MMCFG region for a domain. */
 bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr);
 
+/* Remove MMCFG regions from a given rangeset. */
+int vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/arch/x86/include/asm/setup.h +1 -0
index 9a460e4db8f4..cd07d98101d8 100644
@@ -37,6 +37,7 @@ void discard_initial_images(void);
 void *bootstrap_map(const module_t *mod);
 
 int xen_in_range(unsigned long mfn);
+int remove_xen_ranges(struct rangeset *r);
 
 extern uint8_t kbd_shift_flags;
 
xen/arch/x86/setup.c +48 -0

54 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +61 -0

73 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

Jan

#3 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 35 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

Quoted 4 lines
                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Thanks, Roger.

#4 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 36 lines · Roger Pau Monné
Quoted 35 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

Quoted 8 lines
Quoted 4 lines
                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Hmm, no, for me neither trampoline_end nor trampoline_start are page aligned. While bootsym_phys(trampoline_start) is, I don't think bootsym_phys(trampoline_end) normally would be (it might only be by coincidence).

Jan

#5 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 42 lines · Jan Beulich
Quoted 41 lines · Roger Pau Monné
Quoted 39 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

Quoted 8 lines
Quoted 3 lines

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Hmm, no, for me neither trampoline_end nor trampoline_start are page aligned. While bootsym_phys(trampoline_start) is, I don't think bootsym_phys(trampoline_end) normally would be (it might only be by coincidence).

Oh, so it had been a coincidence of the build I was using I guess then.

Thanks, Roger.

#6 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 40 lines · Roger Pau Monné
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Hmm, no, for me neither trampoline_end nor trampoline_start are page aligned. While bootsym_phys(trampoline_start) is, I don't think bootsym_phys(trampoline_end) normally would be (it might only be by coincidence).

Oh, so it had been a coincidence of the build I was using I guess then.

trampoline_start has to be page aligned because of constraints from SIPI and S3 (cant remember which one is the 4k constraint, but it's in the comments).

On APs (and indeed, in Xen's pagetables), the trampoline is only a single 4k page.

However, trampoline_end is quite a lot longer because there's various things that get done on the BSP only, including recovering the E820 map, EDID/etc in 16bit mode.

That said, we don't edit the trampoline very often, so if it happened to work for you first time around, it probably hasn't changed since.

~Andrew

#7 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 48 lines · Andrew Cooper
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

Quoted 7 lines
Quoted 4 lines
                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Hmm, no, for me neither trampoline_end nor trampoline_start are page aligned. While bootsym_phys(trampoline_start) is, I don't think bootsym_phys(trampoline_end) normally would be (it might only be by coincidence).

Quoted 5 lines
Quoted 1 line

Oh, so it had been a coincidence of the build I was using I guess then.

trampoline_start has to be page aligned because of constraints from SIPI and S3 (cant remember which one is the 4k constraint, but it's in the comments).

What you're talking about is the copy of the trampoline code/data in low memory. trampoline_{start,end} themselves point into the Xen image.

Quoted 6 lines

On APs (and indeed, in Xen's pagetables), the trampoline is only a single 4k page.

However, trampoline_end is quite a lot longer because there's various things that get done on the BSP only, including recovering the E820 map, EDID/etc in 16bit mode.

And this BSP-only part really wouldn't need removing here, I think. The issue is that the BSP-only and also-AP plus S3-wakeup parts aren't properly delimited (hmm, maybe wakeup_stack can be used for this purpose). But if, as you say, we map only a single page, we could as well limit logic here to just that.

Jan

Quoted 4 lines

That said, we don't edit the trampoline very often, so if it happened to work for you first time around, it probably hasn't changed since.

~Andrew

#8 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 49 lines · Jan Beulich
Quoted 48 lines · Andrew Cooper
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

Quoted 7 lines
Quoted 4 lines
                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Hmm, no, for me neither trampoline_end nor trampoline_start are page aligned. While bootsym_phys(trampoline_start) is, I don't think bootsym_phys(trampoline_end) normally would be (it might only be by coincidence).

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Oh, so it had been a coincidence of the build I was using I guess then.

trampoline_start has to be page aligned because of constraints from SIPI and S3 (cant remember which one is the 4k constraint, but it's in the comments).

What you're talking about is the copy of the trampoline code/data in low memory. trampoline_{start,end} themselves point into the Xen image.

True, but we're operating on bootsym_phys(trampoline_start) which had better be aligned.

We hard-code (by virtue of only filling in 1 single 4k PTE in the pagetables) that the AP trampoline is 4k.

The range here should be 4k only too, or we're (falsely) marking lowmem adjacent to the AP trampoline as a Xen range when it's not.

~Andrew

#9 lore raw

Re: [PATCH v5 1/3] x86/iommu: remove regions not to be mapped

Quoted 57 lines · Andrew Cooper
Quoted 49 lines · Jan Beulich
Quoted 48 lines · Andrew Cooper
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +0 -0
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);

xen/arch/x86/hvm/io.c bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr) · line 379

Quoted 10 lines
xen/arch/x86/hvm/io.c +8 -0
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));

Along the lines of this, ...

Quoted 4 lines
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end)) - 1);

... did you perhaps mean

Quoted 7 lines
Quoted 4 lines
                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

here (and then similarly below, except there the difference is benign I think, for the labels being page-aligned)?

They are all page aligned, so I didn't care much, but now that you point it might be safer to do the subtraction from the address instead of the frame number, just in case.

Hmm, no, for me neither trampoline_end nor trampoline_start are page aligned. While bootsym_phys(trampoline_start) is, I don't think bootsym_phys(trampoline_end) normally would be (it might only be by coincidence).

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Oh, so it had been a coincidence of the build I was using I guess then.

trampoline_start has to be page aligned because of constraints from SIPI and S3 (cant remember which one is the 4k constraint, but it's in the comments).

What you're talking about is the copy of the trampoline code/data in low memory. trampoline_{start,end} themselves point into the Xen image.

Quoted 8 lines

True, but we're operating on bootsym_phys(trampoline_start) which had better be aligned.

We hard-code (by virtue of only filling in 1 single 4k PTE in the pagetables) that the AP trampoline is 4k.

The range here should be 4k only too, or we're (falsely) marking lowmem adjacent to the AP trampoline as a Xen range when it's not.

Hm, looking at zap_low_mappings() we do seem to possibly map more than one page, in fact on my current build trampoline_end - trampoline_start is 6528.

Thanks, Roger.

#10 lore raw

PATCH v6 1/3 x86/iommu: remove regions not to be mapped

In reply to Roger Pau Monne

Introduce the code to remove regions not to be mapped from the rangeset that will be used to setup the IOMMU page tables for the hardware domain.

This change also introduces two new functions: remove_xen_ranges() and vpci_subtract_mmcfg() that copy the logic in xen_in_range() and vpci_is_mmcfg_address() respectively and remove the ranges that would otherwise be intercepted by the original functions.

Note that the rangeset is still not populated.

---
Changes since v6:
- Fix subtraction to be done against the address, not the mfn.

Changes since v4:
- Fix off-by-one when removing the Xen used ranges, as the rangesets are inclusive.

Changes since v3:
- Remove unnecessary line wrapping.

Changes since v1:
- Split from bigger patch.

---
 xen/arch/x86/hvm/io.c               | 16 ++++++++
 xen/arch/x86/include/asm/hvm/io.h   |  3 ++
 xen/arch/x86/include/asm/setup.h    |  1 +
 xen/arch/x86/setup.c                | 48 +++++++++++++++++++++++
 xen/drivers/passthrough/x86/iommu.c | 61 +++++++++++++++++++++++++++++
 5 files changed, 129 insertions(+)
xen/arch/x86/hvm/io.c +16 -0
index d75af83ad01f..a42854c52b65 100644
@@ -369,6 +369,22 @@ bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
     return vpci_mmcfg_find(d, addr);
 }
 
+int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+    {
+        int rc = rangeset_remove_range(r, PFN_DOWN(mmcfg->addr),
+                                       PFN_DOWN(mmcfg->addr + mmcfg->size - 1));
+
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
+}
+
 static unsigned int vpci_mmcfg_decode_addr(const struct hvm_mmcfg *mmcfg,
                                            paddr_t addr, pci_sbdf_t *sbdf)
 {
xen/arch/x86/include/asm/hvm/io.h +3 -0
index a97731657801..e1e5e6fe7491 100644
@@ -156,6 +156,9 @@ void destroy_vpci_mmcfg(struct domain *d);
 /* Check if an address is between a MMCFG region for a domain. */
 bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr);
 
+/* Remove MMCFG regions from a given rangeset. */
+int vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/arch/x86/include/asm/setup.h +1 -0
index 9a460e4db8f4..cd07d98101d8 100644
@@ -37,6 +37,7 @@ void discard_initial_images(void);
 void *bootstrap_map(const module_t *mod);
 
 int xen_in_range(unsigned long mfn);
+int remove_xen_ranges(struct rangeset *r);
 
 extern uint8_t kbd_shift_flags;
 
xen/arch/x86/setup.c +48 -0

54 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +61 -0

73 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v6 1/3] x86/iommu: remove regions not to be mapped

Quoted 4 lines · Roger Pau Monne
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

As per further v5 comments this wants to be

rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
PFN_DOWN(bootsym_phys(trampoline_start)));

or some such, with suitable cross-referencing comments added here and on the other side as to this only being a single page (unless this is already somehow abstracted; I can't spot any respective assertion in xen.lds.S though, for example).

Jan

Reviewed-by on v6 1/3
#12 lore raw

Re: [PATCH v6 1/3] x86/iommu: remove regions not to be mapped

In reply to Roger Pau Monne
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/setup.c +0 -0
@@ -2138,6 +2138,54 @@ int __hwdom_init xen_in_range(unsigned long mfn)
     return 0;

xen/arch/x86/setup.c int __hwdom_init xen_in_range(unsigned long mfn) · line 2148

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
 }
 
+int __hwdom_init remove_xen_ranges(struct rangeset *r)
+{
+    paddr_t start, end;
+    int rc;
+
+    /* S3 resume code (and other real mode trampoline code) */
+    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
+                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));

With the understanding that we've settled on us not quite being at the point where only one page of the trampoline is kept mapped:

Jan

Reviewed-by on v6 1/3
#13 lore raw

Re: [PATCH v6 1/3] x86/iommu: remove regions not to be mapped

In reply to Roger Pau Monne
Quoted 24 lines · Roger Pau Monne

Introduce the code to remove regions not to be mapped from the rangeset that will be used to setup the IOMMU page tables for the hardware domain.

This change also introduces two new functions: remove_xen_ranges() and vpci_subtract_mmcfg() that copy the logic in xen_in_range() and vpci_is_mmcfg_address() respectively and remove the ranges that would otherwise be intercepted by the original functions.

Note that the rangeset is still not populated.

---
Changes since v6:
- Fix subtraction to be done against the address, not the mfn.

Changes since v4:
- Fix off-by-one when removing the Xen used ranges, as the rangesets are inclusive.

Changes since v3:
- Remove unnecessary line wrapping.

Changes since v1:
- Split from bigger patch.

Quoted 8 lines
---
  xen/arch/x86/hvm/io.c               | 16 ++++++++
  xen/arch/x86/include/asm/hvm/io.h   |  3 ++
  xen/arch/x86/include/asm/setup.h    |  1 +
  xen/arch/x86/setup.c                | 48 +++++++++++++++++++++++
  xen/drivers/passthrough/x86/iommu.c | 61 +++++++++++++++++++++++++++++
  5 files changed, 129 insertions(+)
#14 lore raw

PATCH v5 2/3 x86/iommu: switch hwdom IOMMU to use a rangeset

In reply to Roger Pau Monne

The current loop that iterates from 0 to the maximum RAM address in order to setup the IOMMU mappings is highly inefficient, and it will get worse as the amount of RAM increases. It's also not accounting for any reserved regions past the last RAM address.

Instead of iterating over memory addresses, iterate over the memory map regions and use a rangeset in order to keep track of which ranges need to be identity mapped in the hardware domain physical address space.

On an AMD EPYC 7452 with 512GiB of RAM, the time to execute arch_iommu_hwdom_init() in nanoseconds is:

x old
+ new
N Min Max Median Avg Stddev
x 5 2.2364154e+10 2.338244e+10 2.2474685e+10 2.2622409e+10 4.2949869e+08
+ 5 1025012 1033036 1026188 1028276.2 3623.1194
Difference at 95.0% confidence

        -2.26214e+10 +/- 4.42931e+08
        -99.9955% +/- 9.05152e-05%
        (Student's t, pooled s = 3.03701e+08)

Execution time of arch_iommu_hwdom_init() goes down from ~22s to ~0.001s.

Note there's a change for HVM domains (ie: PVH dom0) that get switched to create the p2m mappings using map_mmio_regions() instead of p2m_add_identity_entry(), so that ranges can be mapped with a single function call if possible. Note that the interface of map_mmio_regions() doesn't allow creating read-only mappings, but so far there are no such mappings created for PVH dom0 in arch_iommu_hwdom_init().

No change intended in the resulting mappings that a hardware domain gets.

---
Changes since v4:
- Add default case to handle ACPI and NVS regions (which are not mapped) unless in the low 4GB and when inclusive mode is set.
- Add changelog entry.
- Dropped Jans RB.

Changes since v3:
- Remove unnecessary line wraps.

Changes since v2:
- Simplify a bit the logic related to inclusive option, at the cost of making some no-op calls on some cases.

Changes since v1:
- Split from bigger patch.
- Remove unneeded default case.

x86/iommu: add CHANGELOG entry for hwdom setup time improvements

---
 CHANGELOG.md                        |   2 +
 xen/drivers/passthrough/x86/iommu.c | 149 ++++++----------------------
 2 files changed, 35 insertions(+), 116 deletions(-)
CHANGELOG.md +2 -0
index 723d06425431..3e8b996e4718 100644
@@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 ### Changed
  - Changed flexible array definitions in public I/O interface headers to not
    use "1" as the number of array elements.
+ - On x86:
+   - Reduce IOMMU setup time for hardware domain.
 
 ### Added
  - On x86:
xen/drivers/passthrough/x86/iommu.c +33 -116

175 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v5 2/3
#15 lore raw

Re: [PATCH v5 2/3] x86/iommu: switch hwdom IOMMU to use a rangeset

Quoted 31 lines · Roger Pau Monne

The current loop that iterates from 0 to the maximum RAM address in order to setup the IOMMU mappings is highly inefficient, and it will get worse as the amount of RAM increases. It's also not accounting for any reserved regions past the last RAM address.

Instead of iterating over memory addresses, iterate over the memory map regions and use a rangeset in order to keep track of which ranges need to be identity mapped in the hardware domain physical address space.

On an AMD EPYC 7452 with 512GiB of RAM, the time to execute arch_iommu_hwdom_init() in nanoseconds is:

x old
+ new
N Min Max Median Avg Stddev
x 5 2.2364154e+10 2.338244e+10 2.2474685e+10 2.2622409e+10 4.2949869e+08
+ 5 1025012 1033036 1026188 1028276.2 3623.1194
Difference at 95.0% confidence

        -2.26214e+10 +/- 4.42931e+08
        -99.9955% +/- 9.05152e-05%
        (Student's t, pooled s = 3.03701e+08)

Execution time of arch_iommu_hwdom_init() goes down from ~22s to ~0.001s.

Note there's a change for HVM domains (ie: PVH dom0) that get switched to create the p2m mappings using map_mmio_regions() instead of p2m_add_identity_entry(), so that ranges can be mapped with a single function call if possible. Note that the interface of map_mmio_regions() doesn't allow creating read-only mappings, but so far there are no such mappings created for PVH dom0 in arch_iommu_hwdom_init().

Quoted 3 lines

No change intended in the resulting mappings that a hardware domain gets.

Reviewed-by on v5 2/3
#16 lore raw

Re: [PATCH v5 2/3] x86/iommu: switch hwdom IOMMU to use a rangeset

In reply to Roger Pau Monne
Quoted 48 lines · Roger Pau Monne

The current loop that iterates from 0 to the maximum RAM address in order to setup the IOMMU mappings is highly inefficient, and it will get worse as the amount of RAM increases. It's also not accounting for any reserved regions past the last RAM address.

Instead of iterating over memory addresses, iterate over the memory map regions and use a rangeset in order to keep track of which ranges need to be identity mapped in the hardware domain physical address space.

On an AMD EPYC 7452 with 512GiB of RAM, the time to execute arch_iommu_hwdom_init() in nanoseconds is:

x old
+ new
N Min Max Median Avg Stddev
x 5 2.2364154e+10 2.338244e+10 2.2474685e+10 2.2622409e+10 4.2949869e+08
+ 5 1025012 1033036 1026188 1028276.2 3623.1194
Difference at 95.0% confidence

         -2.26214e+10 +/- 4.42931e+08
         -99.9955% +/- 9.05152e-05%
         (Student's t, pooled s = 3.03701e+08)

Execution time of arch_iommu_hwdom_init() goes down from ~22s to ~0.001s.

Note there's a change for HVM domains (ie: PVH dom0) that get switched to create the p2m mappings using map_mmio_regions() instead of p2m_add_identity_entry(), so that ranges can be mapped with a single function call if possible. Note that the interface of map_mmio_regions() doesn't allow creating read-only mappings, but so far there are no such mappings created for PVH dom0 in arch_iommu_hwdom_init().

No change intended in the resulting mappings that a hardware domain gets.

---
Changes since v4:
- Add default case to handle ACPI and NVS regions (which are not mapped) unless in the low 4GB and when inclusive mode is set.
- Add changelog entry.
- Dropped Jans RB.

Changes since v3:
- Remove unnecessary line wraps.

Changes since v2:
- Simplify a bit the logic related to inclusive option, at the cost of making some no-op calls on some cases.

Quoted 7 lines

Changes since v1:
- Split from bigger patch.
- Remove unneeded default case.

x86/iommu: add CHANGELOG entry for hwdom setup time improvements

#17 lore raw

PATCH v5 3/3 x86/iommu: cleanup unused functions

In reply to Roger Pau Monne

Remove xen_in_range() and vpci_is_mmcfg_address() now that hey are unused.

Adjust comments to point to the new functions that replace the existing ones.

No functional change.

---
Changes since v2:
- Do remove vpci_is_mmcfg_address().
---
Can be squashed with the previous patch if desired, split as a separate patch for clarity.

---
 xen/arch/x86/hvm/io.c             |  5 ---
 xen/arch/x86/include/asm/hvm/io.h |  3 --
 xen/arch/x86/include/asm/setup.h  |  1 -
 xen/arch/x86/setup.c              | 53 ++-----------------------------
 xen/arch/x86/tboot.c              |  2 +-
 5 files changed, 3 insertions(+), 61 deletions(-)
xen/arch/x86/hvm/io.c +0 -5
index a42854c52b65..06283b41c463 100644
@@ -364,11 +364,6 @@ static const struct hvm_mmcfg *vpci_mmcfg_find(const struct domain *d,
     return NULL;
 }
 
-bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr)
-{
-    return vpci_mmcfg_find(d, addr);
-}
-
 int __hwdom_init vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r)
 {
     const struct hvm_mmcfg *mmcfg;
xen/arch/x86/include/asm/hvm/io.h +0 -3
index e1e5e6fe7491..24d1b6134f02 100644
@@ -153,9 +153,6 @@ int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
 /* Destroy tracked MMCFG areas. */
 void destroy_vpci_mmcfg(struct domain *d);
 
-/* Check if an address is between a MMCFG region for a domain. */
-bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr);
-
 /* Remove MMCFG regions from a given rangeset. */
 int vpci_subtract_mmcfg(const struct domain *d, struct rangeset *r);
 
xen/arch/x86/include/asm/setup.h +0 -1
index cd07d98101d8..1ced1299c77b 100644
@@ -36,7 +36,6 @@ unsigned long initial_images_nrpages(nodeid_t node);
 void discard_initial_images(void);
 void *bootstrap_map(const module_t *mod);
 
-int xen_in_range(unsigned long mfn);
 int remove_xen_ranges(struct rangeset *r);
 
 extern uint8_t kbd_shift_flags;
xen/arch/x86/setup.c +2 -51

71 lines not on this page · raw message

xen/arch/x86/tboot.c +1 -1
index 86c4c22cacb8..4c254b4e34b4 100644
@@ -321,7 +321,7 @@ void tboot_shutdown(uint32_t shutdown_type)
 
         /*
          * Xen regions for tboot to MAC. This needs to remain in sync with
-         * xen_in_range().
+         * remove_xen_ranges().
          */
         g_tboot_shared->num_mac_regions = 3;
         /* S3 resume code (and other real mode trampoline code) */
Signature · 1 line
2.43.0
Reviewed-by on v5 3/3
#18 lore raw

Re: [PATCH v5 3/3] x86/iommu: cleanup unused functions

Quoted 14 lines · Roger Pau Monne

Remove xen_in_range() and vpci_is_mmcfg_address() now that hey are unused.

Adjust comments to point to the new functions that replace the existing ones.

No functional change.

---
Changes since v2:
- Do remove vpci_is_mmcfg_address().
---
Can be squashed with the previous patch if desired, split as a separate patch for clarity.

Quoted 8 lines
---
  xen/arch/x86/hvm/io.c             |  5 ---
  xen/arch/x86/include/asm/hvm/io.h |  3 --
  xen/arch/x86/include/asm/setup.h  |  1 -
  xen/arch/x86/setup.c              | 53 ++-----------------------------
  xen/arch/x86/tboot.c              |  2 +-
  5 files changed, 3 insertions(+), 61 deletions(-)