pdx: introduce a new compression algorithm
v4 cover pdx: introduce a new compression algorithm
Hello,
This series implements a new PDX compression algorithm to cope with the spare memory maps found on the Intel Sierra Forest and Granite Rapids.
Patches 1 to 6 prepare the existing code to make it easier to introduce a new PDX compression, including generalizing the initialization and setup functions and adding a unit test for PDX compression.
Patch 7 introduce the new compression. The new compression is only enabled by default on x86, other architectures are left with their previous defaults.
Finally patch 8 optimizes one x86 loop that was iterating over pfn ranges to instead use pdx values.
Thanks, Roger.
Roger Pau Monne (8):
kconfig: turn PDX compression into a choice
pdx: provide a unified set of unit functions
pdx: introduce command line compression toggle
pdx: allow per-arch optimization of PDX conversion helpers test/pdx: add PDX compression unit tests
pdx: move some helpers in preparation for new compression
pdx: introduce a new compression algorithm based on region offsets x86/mm: adjust loop in arch_init_memory() to iterate over the PDX
space
CHANGELOG.md | 3 + docs/misc/xen-command-line.pandoc | 9 + tools/tests/Makefile | 1 + tools/tests/pdx/.gitignore | 3 + tools/tests/pdx/Makefile | 50 +++ tools/tests/pdx/harness.h | 98 ++++++ tools/tests/pdx/test-pdx.c | 271 ++++++++++++++++ xen/arch/arm/setup.c | 36 +-- xen/arch/x86/include/asm/cpufeatures.h | 1 + xen/arch/x86/include/asm/pdx.h | 71 ++++ xen/arch/x86/mm.c | 17 +- xen/arch/x86/srat.c | 30 +- xen/common/Kconfig | 37 ++- xen/common/pdx.c | 432 +++++++++++++++++++++++-- xen/include/xen/pdx.h | 213 ++++++++---- 15 files changed, 1139 insertions(+), 133 deletions(-) create mode 100644 tools/tests/pdx/.gitignore create mode 100644 tools/tests/pdx/Makefile create mode 100644 tools/tests/pdx/harness.h create mode 100644 tools/tests/pdx/test-pdx.c create mode 100644 xen/arch/x86/include/asm/pdx.h
Signature · 1 line
2.49.0
PATCH v4 1/8 kconfig: turn PDX compression into a choice
Rename the current CONFIG_PDX_COMPRESSION to CONFIG_PDX_MASK_COMPRESSION, and make it part of the PDX compression choice block, in preparation for adding further PDX compression algorithms.
The PDX compression defaults should still be the same for all architectures, however the choice block cannot be protected under EXPERT and still have a default choice being unconditionally selected. As a result, the new "PDX (Page inDeX) compression" item will be unconditionally visible in Kconfig, even on architectures like x86 that previously had no way to enable PDX compression.
As part of this preparation work to introduce new PDX compressions, adjust some of the comments on pdx.h to note they apply to a specific PDX compression. Also shuffle function prototypes and dummy implementations around to make it easier to introduce a new PDX compression. Note all PDX compression implementations are expected to provide a pdx_is_region_compressible() that takes the same set of arguments.
--- xen/arch/arm/setup.c | 2 +- xen/common/Kconfig | 18 +++++++++++++++--- xen/common/pdx.c | 4 ++-- xen/include/xen/pdx.h | 32 +++++++++++++++++++------------- 4 files changed, 37 insertions(+), 19 deletions(-)
xen/arch/arm/setup.c +1 -1
@@ -258,7 +258,7 @@ void __init init_pdx(void)paddr_t bank_start, bank_size, bank_end, ram_end =0 ;int bank; -#ifdef CONFIG_PDX_COMPRESSION +#ifndef CONFIG_PDX_NONE /* * Arm does not have any restrictions on the bits to compress. Pass 0 to * let the common code further restrict the mask.
xen/common/Kconfig +15 -3
@@ -57,9 +57,10 @@ config EVTCHN_FIFO If unsure, say Y. -config PDX_COMPRESSION - bool "PDX (Page inDeX) compression" if EXPERT && !X86 && !RISCV - default ARM || PPC +choice + prompt "PDX (Page inDeX) compression" + default PDX_MASK_COMPRESSION if !X86 && !RISCV + default PDX_NONE help PDX compression is a technique designed to reduce the memory overhead of physical memory management on platforms with sparse RAM
@@ -72,6 +73,17 @@ config PDX_COMPRESSION If your platform does not have sparse RAM banks, do not enable PDX compression. +config PDX_MASK_COMPRESSION + bool "Mask compression" + help + Compression relying on all RAM addresses sharing a zeroed bit region. + +config PDX_NONE + bool "None" + help + No compression +endchoice + config ALTERNATIVE_CALL bool
xen/common/pdx.c +2 -2
@@ -34,7 +34,7 @@ bool __mfn_valid(unsigned long mfn) {bool invalid = mfn >= max_page; -#ifdef CONFIG_PDX_COMPRESSION +#ifdef CONFIG_PDX_MASK_COMPRESSION invalid |= mfn & pfn_hole_mask;#endif
@@ -55,7 +55,7 @@ void set_pdx_range(unsigned long smfn, unsigned long emfn)__set_bit (idx, pdx_group_valid); } -#ifdef CONFIG_PDX_COMPRESSION +#ifdef CONFIG_PDX_MASK_COMPRESSION /* * Diagram to make sense of the following variables. The masks and shifts
xen/include/xen/pdx.h +19 -13
@@ -25,7 +25,7 @@* this by keeping a bitmap of the ranges in the frame table containing * invalid entries and not allocating backing memory for them. * -* ## PDX compression +* ## PDX mask compression * * This is a technique to avoid wasting memory on machines known to have * split their machine address space in several big discontinuous and highly
@@ -101,22 +101,13 @@ bool __mfn_valid(unsigned long mfn);#define paddr_to_pdx (pa) pfn_to_pdx(paddr_to_pfn(pa))#define pdx_to_paddr (px) pfn_to_paddr(pdx_to_pfn(px)) -#ifdef CONFIG_PDX_COMPRESSION +#ifdef CONFIG_PDX_MASK_COMPRESSION extern unsigned long pfn_pdx_bottom_mask, ma_va_bottom_mask;extern unsigned int pfn_pdx_hole_shift;extern unsigned long pfn_hole_mask;extern unsigned long pfn_top_mask, ma_top_mask; -/** -* Validate a region's compatibility with the current compression runtime -* -* @param base Base address of the region -* @param npages Number of PAGE_SIZE-sized pages in the region -* @return True iff the region can be used with the current compression -*/ -bool pdx_is_region_compressible (paddr_t base,unsigned long npages); -/** * Calculates a mask covering "moving" bits of all addresses of a region *
@@ -209,7 +200,9 @@ static inline paddr_t directmapoff_to_maddr(unsigned long offset)*/ void pfn_pdx_hole_setup (unsigned long mask); -#else /* !CONFIG_PDX_COMPRESSION */ +#endif /* CONFIG_PDX_MASK_COMPRESSION */ + +#ifdef CONFIG_PDX_NONE /* Without PDX compression we can skip some computations */
@@ -241,7 +234,20 @@ static inline void pfn_pdx_hole_setup(unsigned long mask) { } -#endif /* CONFIG_PDX_COMPRESSION */ +#else /* !CONFIG_PDX_NONE */ + +/* Shared functions implemented by all PDX compressions. */ + +/** +* Validate a region's compatibility with the current compression runtime +* +* @param base Base address of the region +* @param npages Number of PAGE_SIZE-sized pages in the region +* @return True iff the region can be used with the current compression +*/ +bool pdx_is_region_compressible (paddr_t base,unsigned long npages); + +#endif /* !CONFIG_PDX_NONE */ #endif /* __XEN_PDX_H__ */ /*
Signature · 1 line
2.49.0
Re: [PATCH v4 1/8] kconfig: turn PDX compression into a choice
Hi Roger,
Quoted 18 lines · Roger Pau Monne
Rename the current
CONFIG_PDX_COMPRESSIONtoCONFIG_PDX_MASK_COMPRESSION, and make it part of the PDX compression choice block, in preparation for adding further PDX compression algorithms.The PDX compression defaults should still be the same for all architectures, however the choice block cannot be protected under EXPERT and still have a default choice being unconditionally selected. As a result, the new "PDX (Page inDeX) compression" item will be unconditionally visible in
Kconfig, even on architectures like x86 that previously had no way to enable PDX compression.As part of this preparation work to introduce new PDX compressions, adjust some of the comments on
pdx.hto note they apply to a specific PDX compression. Also shuffle function prototypes and dummy implementations around to make it easier to introduce a new PDX compression. Note all PDX compression implementations are expected to provide apdx_is_region_compressible()that takes the same set of arguments.
Quoted 2 lines
Signed-off-by Roger Pau Monné <[email protected]>Acked-by Jan Beulich <[email protected]>
Cheers,
Signature · 1 line
Julien Grall
PATCH v4 2/8 pdx: provide a unified set of unit functions
In reply to Roger Pau MonneThe current setup (pdx_init_mask() and pdx_region_mask()) and init (pfn_pdx_hole_setup()) PDX compression functions are tailored to the existing PDX compression algorithm.
In preparation for introducing a new compression algorithm convert the setup and init functions to more generic interfaces that aren't tied to the compression in-use. To accomplish this introduce a function that registers all the PFN RAM ranges, plus an init function.
This has the downside of requiring a static array to store such ranges ahead of being processed by the setup function, however it's the only way to pass all the possible information to the different compression setup functions without using per-compression specific setup functions. Per-arch compression setup also need to be adjusted to use the new interface. There's a slight ordering adjustment, in that after PDX compression setup the caller will check whether all the RAM regions are properly covered by the newly setup compression, otherwise compression is disabled by resetting to the initial values.
No functional change intended in the resulting PDX compression values.
---
Changes since v3:
- Rename base to base_pfn and size to pages.
Changes since v2:
- Fix indentation.
Changes since v1:
- s/nr/nr_ranges/
--- xen/arch/arm/setup.c | 34 ++++++------ xen/arch/x86/srat.c | 28 ++++++---- xen/common/pdx.c | 122 ++++++++++++++++++++++++++++++++++++------ xen/include/xen/pdx.h | 73 +++++++++---------------- 4 files changed, 166 insertions(+), 91 deletions(-)
xen/arch/arm/setup.c +17 -17
@@ -256,9 +256,11 @@ void __init init_pdx(void) {const struct membanks *mem =bootinfo_get_mem ();paddr_t bank_start, bank_size, bank_end, ram_end =0 ; -int bank; +unsigned int bank;#ifndef CONFIG_PDX_NONE +for ( bank =0 ; bank < mem->nr_banks; bank++ ) +pfn_pdx_add_region (mem->bank[bank].start, mem->bank[bank].size);/* * Arm does not have any restrictions on the bits to compress. Pass 0 to * let the common code further restrict the mask.
@@ -266,26 +268,24 @@ void __init init_pdx(void)* If the logic changes in pfn_pdx_hole_setup we might have to * update this function too. */ -uint64_t mask =pdx_init_mask (0x0 ); - -for ( bank =0 ; bank < mem->nr_banks; bank++ ) - { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - - mask |= bank_start |pdx_region_mask (bank_start, bank_size); - } +pfn_pdx_compression_setup (0 );for ( bank =0 ; bank < mem->nr_banks; bank++ ) { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - -if (~mask &pdx_region_mask (bank_start, bank_size)) - mask =0 ; +if ( !pdx_is_region_compressible ( + mem->bank[bank].start, +PFN_UP (mem->bank[bank].start + mem->bank[bank].size) - +PFN_DOWN (mem->bank[bank].start)) ) + { +pfn_pdx_compression_reset (); +printk (XENLOG_WARNING +"PFN compression disabled, RAM region [%#" PRIpaddr", %#" + PRIpaddr"] not covered\n" , + mem->bank[bank].start, + mem->bank[bank].start + mem->bank[bank].size -1 ); +break ; + } } - -pfn_pdx_hole_setup (mask >>PAGE_SHIFT );#endif for ( bank =0 ; bank < mem->nr_banks; bank++ )
xen/arch/x86/srat.c +17 -11
@@ -261,8 +261,6 @@ acpi_numa_memory_affinity_init(const struct acpi_srat_mem_affinity *ma)void __init acpi_numa_arch_fixup (void ) {} -static uint64_t __initdata srat_region_mask; - static int __init cf_check srat_parse_region(struct acpi_subtable_header *header,const unsigned long end) {
@@ -282,15 +280,13 @@ static int __init cf_check srat_parse_region(printk (KERN_INFO "SRAT: %013" PRIx64"-%013" PRIx64"\n" , ma->base_address, ma->base_address + ma->length -1 ); - srat_region_mask |= ma->base_address | -pdx_region_mask (ma->base_address, ma->length); +pfn_pdx_add_region (ma->base_address, ma->length);return 0 ; }void __init srat_parse_regions (paddr_t addr) { -u64 mask;unsigned int i; if (acpi_disabled || acpi_numa < 0 ||
@@ -299,19 +295,29 @@ void __init srat_parse_regions(paddr_t addr)/* Set "PXM" as early as feasible. */ numa_fw_nid_name ="PXM" ; - srat_region_mask =pdx_init_mask (addr);acpi_table_parse_srat (ACPI_SRAT_TYPE_MEMORY_AFFINITY , srat_parse_region,0 ); -for (mask = srat_region_mask, i =0 ; mask && i < e820.nr_map; i++) { +pfn_pdx_compression_setup (addr); + +/* Ensure all RAM ranges in the e820 are covered. */ +for (i =0 ; i < e820.nr_map; i++) {if (e820.map[i].type !=E820_RAM )continue ; -if (~mask &pdx_region_mask (e820.map[i].addr, e820.map[i].size)) - mask =0 ; +if (!pdx_is_region_compressible ( + e820.map[i].addr, +PFN_UP (e820.map[i].addr + e820.map[i].size) - +PFN_DOWN (e820.map[i].addr))) { +pfn_pdx_compression_reset (); +printk (XENLOG_WARNING +"PFN compression disabled, RAM region [%#" PRIx64 +", %#" PRIx64"] not covered\n" , + e820.map[i].addr, + e820.map[i].addr + e820.map[i].size -1 ); +return ; + } } - -pfn_pdx_hole_setup (mask >>PAGE_SHIFT ); }unsigned int numa_node_to_arch_nid (nodeid_t n)
xen/common/pdx.c +107 -15
154 lines not on this page · raw message
xen/include/xen/pdx.h +25 -48
103 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v4 2/8] pdx: provide a unified set of unit functions
Hi Roger,
Quoted 33 lines · Roger Pau Monne
xen/arch/arm/setup.c +4
-10@@ -256,9 +256,11 @@ void __init init_pdx(void) {const struct membanks *mem =bootinfo_get_mem ();paddr_t bank_start, bank_size, bank_end, ram_end =0 ; -int bank; +unsigned int bank;#ifndef CONFIG_PDX_NONE +for ( bank =0 ; bank < mem->nr_banks; bank++ ) +pfn_pdx_add_region (mem->bank[bank].start, mem->bank[bank].size);/* * Arm does not have any restrictions on the bits to compress. Pass 0 to * let the common code further restrict the mask. @@ -266,26 +268,24 @@ void __init init_pdx(void)* If the logic changes in pfn_pdx_hole_setup we might have to * update this function too. */ -uint64_t mask =pdx_init_mask (0x0 ); - -for ( bank =0 ; bank < mem->nr_banks; bank++ ) - { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - - mask |= bank_start |pdx_region_mask (bank_start, bank_size); - } +pfn_pdx_compression_setup (0 );for ( bank =0 ; bank < mem->nr_banks; bank++ )
xen/arch/arm/setup.c void __init init_pdx(void) · line 278
Quoted 10 lines
xen/arch/arm/setup.c +4
-5{ - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - -if (~mask &pdx_region_mask (bank_start, bank_size)) - mask =0 ; +if ( !pdx_is_region_compressible ( + mem->bank[bank].start, +PFN_UP (mem->bank[bank].start + mem->bank[bank].size) - +PFN_DOWN (mem->bank[bank].start)) )
This code is a bit too verbose. Can we at least introduce "bank = &mem->bank[bank]" to reduce a bit the verbosity?
The rest of the logic looks fine. So:
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v4 2/8] pdx: provide a unified set of unit functions
Quoted 39 lines · Julien Grall
Hi Roger,
Quoted 36 lines · Roger Pau Monne
xen/arch/arm/setup.c +4
-14@@ -256,9 +256,11 @@ void __init init_pdx(void) {const struct membanks *mem =bootinfo_get_mem ();paddr_t bank_start, bank_size, bank_end, ram_end =0 ; -int bank; +unsigned int bank;#ifndef CONFIG_PDX_NONE +for ( bank =0 ; bank < mem->nr_banks; bank++ ) +pfn_pdx_add_region (mem->bank[bank].start, mem->bank[bank].size);/* * Arm does not have any restrictions on the bits to compress. Pass 0 to * let the common code further restrict the mask. @@ -266,26 +268,24 @@ void __init init_pdx(void)* If the logic changes in pfn_pdx_hole_setup we might have to * update this function too. */ -uint64_t mask =pdx_init_mask (0x0 ); - -for ( bank =0 ; bank < mem->nr_banks; bank++ ) - { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - - mask |= bank_start |pdx_region_mask (bank_start, bank_size); - } +pfn_pdx_compression_setup (0 );for ( bank =0 ; bank < mem->nr_banks; bank++ ) { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - -if (~mask &pdx_region_mask (bank_start, bank_size))
Quoted 8 lines
xen/arch/arm/setup.c void __init init_pdx(void) · line 278
Quoted 5 lines
xen/arch/arm/setup.c +4
-1- mask =0 ; +if ( !pdx_is_region_compressible ( + mem->bank[bank].start, +PFN_UP (mem->bank[bank].start + mem->bank[bank].size) - +PFN_DOWN (mem->bank[bank].start)) )This code is a bit too verbose. Can we at least introduce "bank = &mem->bank[bank]" to reduce a bit the verbosity?
I cannot introduce a bank local variable as it's already used as the index cursor for the loop. Would you be fine with:
for ( bank = 0 ; bank < mem->nr_banks; bank++ )
{
const struct membank *m = &mem->bank[bank];
if ( !pdx_is_region_compressible(m->start,
PFN_UP(m->start + m->size) -
PFN_DOWN(m->start)) )
{
pfn_pdx_compression_reset();
printk(XENLOG_WARNING
"PFN compression disabled, RAM region [%#" PRIpaddr ", %#"
PRIpaddr "] not covered\n",
m->start, m->start + m->size - 1);
break;
}
}Quoted 3 lines
The rest of the logic looks fine. So:
Thanks, Roger.
Re: [PATCH v4 2/8] pdx: provide a unified set of unit functions
Hi Roger,
Quoted 49 lines · Roger Pau Monné
Quoted 39 lines · Julien Grall
Hi Roger,
Quoted 36 lines · Roger Pau Monne
xen/arch/arm/setup.c +4
-14@@ -256,9 +256,11 @@ void __init init_pdx(void) {const struct membanks *mem =bootinfo_get_mem ();paddr_t bank_start, bank_size, bank_end, ram_end =0 ; -int bank; +unsigned int bank;#ifndef CONFIG_PDX_NONE +for ( bank =0 ; bank < mem->nr_banks; bank++ ) +pfn_pdx_add_region (mem->bank[bank].start, mem->bank[bank].size);/* * Arm does not have any restrictions on the bits to compress. Pass 0 to * let the common code further restrict the mask. @@ -266,26 +268,24 @@ void __init init_pdx(void)* If the logic changes in pfn_pdx_hole_setup we might have to * update this function too. */ -uint64_t mask =pdx_init_mask (0x0 ); - -for ( bank =0 ; bank < mem->nr_banks; bank++ ) - { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - - mask |= bank_start |pdx_region_mask (bank_start, bank_size); - } +pfn_pdx_compression_setup (0 );for ( bank =0 ; bank < mem->nr_banks; bank++ ) { - bank_start = mem->bank[bank].start; - bank_size = mem->bank[bank].size; - -if (~mask &pdx_region_mask (bank_start, bank_size))Quoted 8 lines
xen/arch/arm/setup.c void __init init_pdx(void) · line 278
Quoted 5 lines
xen/arch/arm/setup.c +4
-1- mask =0 ; +if ( !pdx_is_region_compressible ( + mem->bank[bank].start, +PFN_UP (mem->bank[bank].start + mem->bank[bank].size) - +PFN_DOWN (mem->bank[bank].start)) )This code is a bit too verbose. Can we at least introduce "bank = &mem->bank[bank]" to reduce a bit the verbosity?
Quoted 2 lines
I cannot introduce a
banklocal variable as it's already used as the index cursor for the loop. Would you be fine with:
Ah yes. I am fine with the logic below. I am happy if you want to commit message without resending the series.
Cheers,
Signature · 1 line
Julien Grall
PATCH v4 3/8 pdx: introduce command line compression toggle
In reply to Roger Pau MonneIntroduce a command line option to allow disabling PDX compression. The disabling is done by turning pfn_pdx_add_region() into a no-op, so when attempting to initialize the selected compression algorithm the array of ranges to compress is empty.
---
Changes since v1:
- New in this version.
--- docs/misc/xen-command-line.pandoc | 9 +++++++++ xen/common/pdx.c | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.pandoc +9 -0
@@ -2072,6 +2072,15 @@ for all of them (`true`), only for those subject to XPTI (`xpti`) or for those not subject to XPTI (`no-xpti`). The feature is used only in case INVPCID is supported and not disabled via `invpcid=false`. +### pdx-compress +> `= <boolean>` + +> Default: `true` if CONFIG_PDX_NONE is unset + +Only relevant when the hypervisor is build with PFN PDX compression. Controls +whether Xen will engage in PFN compression. The algorithm used for PFN +compression is selected at build time from Kconfig. + ### ple_gap > `= <integer>`
xen/common/pdx.c +13 -1
@@ -19,6 +19,7 @@#include <xen/mm.h> #include <xen/bitops.h> #include <xen/nospec.h> +#include <xen/param.h> #include <xen/pfn.h> #include <xen/sections.h>
@@ -76,9 +77,13 @@ static struct pfn_range { } ranges[MAX_PFN_RANGES ]__initdata ;static unsigned int __initdata nr_ranges; +static bool __initdata pdx_compress = true; +boolean_param ("pdx-compress" , pdx_compress); +void __init pfn_pdx_add_region (paddr_t base,paddr_t size) { -if ( !size ) +/* Without ranges there's no PFN compression. */ +if ( !size || !pdx_compress )return ;if ( nr_ranges >=ARRAY_SIZE (ranges) )
@@ -215,6 +220,13 @@ void __init pfn_pdx_compression_setup(paddr_t base)unsigned int i, j, bottom_shift =0 , hole_shift =0 ;unsigned long mask =pdx_init_mask (base) >>PAGE_SHIFT ; +if ( !nr_ranges ) + { +printk (XENLOG_DEBUG "PFN compression disabled%s\n" , + pdx_compress ?": no ranges provided" :"" ); +return ; + } +if ( nr_ranges >ARRAY_SIZE (ranges) ) { printk(XENLOG_WARNING
Signature · 1 line
2.49.0
PATCH v4 4/8 pdx: allow per-arch optimization of PDX conversion helpers
In reply to Roger Pau MonneThere are four performance critical PDX conversion helpers that do the PFN to/from PDX and the physical addresses to/from directmap offsets translations.
In the absence of an active PDX compression, those functions would still do the calculations needed, just to return the same input value as no translation is in place and hence PFN and PDX spaces are identity mapped.
To reduce the overhead of having to do the pointless calculations allow architectures to implement the translation helpers in a per-arch header. Rename the existing conversion functions to add a trailing _xlate suffix, so that the per-arch headers can define the non suffixed versions.
Currently only x86 implements meaningful custom handlers to short circuit the translation when not active, using asm goto. Other architectures use generic macros that map the non-xlate to the xlate variants to keep the previous behavior.
---
Changes since v3:
- Use has_include.
Changes since v2:
- Consume the skip label as parameter in the PDX optimize macro.
Changes since v1:
- Pull return out of OPTIMIZE_PDX macro.
- undef OPTIMIZE_PDX.
---
Would it make sense to move the x86 implementation to the common pdx.h header and let architectures define PDX_ASM_GOTO_SKIP instead?
--- xen/arch/x86/include/asm/cpufeatures.h | 1 + xen/arch/x86/include/asm/pdx.h | 71 ++++++++++++++++++++++++++ xen/arch/x86/srat.c | 6 ++- xen/common/pdx.c | 10 ++-- xen/include/xen/pdx.h | 29 ++++++++--- 5 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 xen/arch/x86/include/asm/pdx.h
xen/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -42,6 +42,7 @@ XEN_CPUFEATURE(XEN_IBT, X86_SYNTH(27)) /* Xen uses CET Indirect Branch XEN_CPUFEATURE(IBPB_ENTRY_PV, X86_SYNTH(28)) /* MSR_PRED_CMD used by Xen for PV */ XEN_CPUFEATURE(IBPB_ENTRY_HVM, X86_SYNTH(29)) /* MSR_PRED_CMD used by Xen for HVM */ XEN_CPUFEATURE(USE_VMCALL, X86_SYNTH(30)) /* Use VMCALL instead of VMMCALL */ +XEN_CPUFEATURE (PDX_COMPRESSION ,X86_SYNTH (31 ))/* PDX compression */ /* Bug words follow the synthetic words. */#define X86_NR_BUG 1
xen/arch/x86/include/asm/pdx.h new file +71 -0
71 lines not on this page · raw message
xen/arch/x86/srat.c +5 -1
@@ -298,7 +298,8 @@ void __init srat_parse_regions(paddr_t addr)acpi_table_parse_srat (ACPI_SRAT_TYPE_MEMORY_AFFINITY , srat_parse_region,0 ); -pfn_pdx_compression_setup (addr); +if (!pfn_pdx_compression_setup (addr)) +return ;/* Ensure all RAM ranges in the e820 are covered. */ for (i =0 ; i < e820.nr_map; i++) {
@@ -318,6 +319,9 @@ void __init srat_parse_regions(paddr_t addr)return ; } } + +/* If we got this far compression is working as expected. */ +setup_force_cpu_cap (X86_FEATURE_PDX_COMPRESSION ); }unsigned int numa_node_to_arch_nid (nodeid_t n)
xen/common/pdx.c +6 -4
@@ -215,7 +215,7 @@ static uint64_t __init pdx_init_mask(uint64_t base_addr) (uint64_t)1 << (MAX_ORDER + PAGE_SHIFT)) - 1); } -void __init pfn_pdx_compression_setup (paddr_t base) +bool __init pfn_pdx_compression_setup (paddr_t base) {unsigned int i, j, bottom_shift =0 , hole_shift =0 ;unsigned long mask =pdx_init_mask (base) >>PAGE_SHIFT ;
@@ -224,7 +224,7 @@ void __init pfn_pdx_compression_setup(paddr_t base) {printk (XENLOG_DEBUG "PFN compression disabled%s\n" , pdx_compress ?": no ranges provided" :"" ); -return ; +return false; }if ( nr_ranges >ARRAY_SIZE (ranges) )
@@ -232,7 +232,7 @@ void __init pfn_pdx_compression_setup(paddr_t base)printk (XENLOG_WARNING "Too many PFN ranges (%u > %zu), not attempting PFN compression\n" , nr_ranges,ARRAY_SIZE (ranges)); -return ; +return false; }for ( i =0 ; i < nr_ranges; i++ )
@@ -263,7 +263,7 @@ void __init pfn_pdx_compression_setup(paddr_t base) } }if ( !hole_shift ) -return ; +return false;printk (KERN_INFO "PFN compression on bits %u...%u\n" , bottom_shift, bottom_shift + hole_shift -1 );
@@ -274,6 +274,8 @@ void __init pfn_pdx_compression_setup(paddr_t base) pfn_hole_mask = ((1UL << hole_shift) -1 ) << bottom_shift; pfn_top_mask = ~(pfn_pdx_bottom_mask | pfn_hole_mask); ma_top_mask = pfn_top_mask <<PAGE_SHIFT ; + +return true; }void __init pfn_pdx_compression_reset (void )
xen/include/xen/pdx.h +23 -6
@@ -114,7 +114,7 @@ extern unsigned long pfn_top_mask, ma_top_mask;* @param pfn Frame number * @return Obtained pdx after compressing the pfn */ -static inline unsigned long pfn_to_pdx (unsigned long pfn) +static inline unsigned long pfn_to_pdx_xlate (unsigned long pfn) {return (pfn & pfn_pdx_bottom_mask) | ((pfn & pfn_top_mask) >> pfn_pdx_hole_shift);
@@ -126,7 +126,7 @@ static inline unsigned long pfn_to_pdx(unsigned long pfn)* @param pdx Page index * @return Obtained pfn after decompressing the pdx */ -static inline unsigned long pdx_to_pfn (unsigned long pdx) +static inline unsigned long pdx_to_pfn_xlate (unsigned long pdx) {return (pdx & pfn_pdx_bottom_mask) | ((pdx << pfn_pdx_hole_shift) & pfn_top_mask);
@@ -139,7 +139,7 @@ static inline unsigned long pdx_to_pfn(unsigned long pdx)* @return Offset on the direct map where that * machine address can be accessed */ -static inline unsigned long maddr_to_directmapoff (paddr_t ma) +static inline unsigned long maddr_to_directmapoff_xlate (paddr_t ma) {return (((ma & ma_top_mask) >> pfn_pdx_hole_shift) | (ma & ma_va_bottom_mask));
@@ -151,7 +151,7 @@ static inline unsigned long maddr_to_directmapoff(paddr_t ma)* @param offset Offset into the direct map * @return Corresponding machine address of that virtual location */ -static inline paddr_t directmapoff_to_maddr (unsigned long offset) +static inline paddr_t directmapoff_to_maddr_xlate (unsigned long offset) {return ((((paddr_t )offset << pfn_pdx_hole_shift) & ma_top_mask) | (offset & ma_va_bottom_mask));
@@ -181,8 +181,9 @@ static inline void pfn_pdx_add_region(paddr_t base, paddr_t size) { } -static inline void pfn_pdx_compression_setup (paddr_t base) +static inline bool pfn_pdx_compression_setup (paddr_t base) { +return false; }static inline void pfn_pdx_compression_reset (void )
@@ -191,6 +192,21 @@ static inline void pfn_pdx_compression_reset(void)#else /* !CONFIG_PDX_NONE */ +/* +* Allow each architecture to define its (possibly optimized) versions of the +* translation functions. +* +* Do not use _xlate suffixed functions, always use the non _xlate variants. +*/ +#if __has_include(<asm/pdx.h>) +# include <asm/pdx.h> +#else +# define pdx_to_pfn pdx_to_pfn_xlate +# define pfn_to_pdx pfn_to_pdx_xlate +# define maddr_to_directmapoff maddr_to_directmapoff_xlate +# define directmapoff_to_maddr directmapoff_to_maddr_xlate +#endif +/* Shared functions implemented by all PDX compressions. */ /**
@@ -215,8 +231,9 @@ void pfn_pdx_add_region(paddr_t base, paddr_t size);* range of the current memory regions. * * @param base address to start compression from. +* @return True if PDX compression has been enabled. */ -void pfn_pdx_compression_setup (paddr_t base); +bool pfn_pdx_compression_setup (paddr_t base);/** * Reset the global variables to it's default values, thus disabling PFN
Signature · 1 line
2.49.0
Re: [PATCH v4 4/8] pdx: allow per-arch optimization of PDX conversion helpers
Quoted 13 lines · Roger Pau Monne
There are four performance critical PDX conversion helpers that do the PFN to/from PDX and the physical addresses to/from directmap offsets translations.
In the absence of an active PDX compression, those functions would still do the calculations needed, just to return the same input value as no translation is in place and hence PFN and PDX spaces are identity mapped.
To reduce the overhead of having to do the pointless calculations allow architectures to implement the translation helpers in a per-arch header. Rename the existing conversion functions to add a trailing
_xlatesuffix, so that the per-arch headers can define the non suffixed versions.
Quoted 6 lines
Currently only x86 implements meaningful custom handlers to short circuit the translation when not active, using asm goto. Other architectures use generic macros that map the non-xlate to the xlate variants to keep the previous behavior.
Signed-off-by Roger Pau Monné <[email protected]>
Once again:
Jan
Re: [PATCH v4 4/8] pdx: allow per-arch optimization of PDX conversion helpers
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monne
There are four performance critical PDX conversion helpers that do the PFN to/from PDX and the physical addresses to/from directmap offsets translations.
In the absence of an active PDX compression, those functions would still do the calculations needed, just to return the same input value as no translation is in place and hence PFN and PDX spaces are identity mapped.
To reduce the overhead of having to do the pointless calculations allow architectures to implement the translation helpers in a per-arch header. Rename the existing conversion functions to add a trailing
_xlatesuffix, so that the per-arch headers can define the non suffixed versions.Currently only x86 implements meaningful custom handlers to short circuit the translation when not active, using asm goto. Other architectures use generic macros that map the non-xlate to the xlate variants to keep the previous behavior.
Quoted 4 lines
Quoted 1 line
Signed-off-by Roger Pau Monné <[email protected]>Once again:
Reviewed-by Jan Beulich <[email protected]>
Thanks, I didn't carry your RB due to the changes requested by Andrew, that was a bit too much to carry a RB after those.
Roger.
Re: [PATCH v4 4/8] pdx: allow per-arch optimization of PDX conversion helpers
Quoted 20 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monne
There are four performance critical PDX conversion helpers that do the PFN to/from PDX and the physical addresses to/from directmap offsets translations.
In the absence of an active PDX compression, those functions would still do the calculations needed, just to return the same input value as no translation is in place and hence PFN and PDX spaces are identity mapped.
To reduce the overhead of having to do the pointless calculations allow architectures to implement the translation helpers in a per-arch header. Rename the existing conversion functions to add a trailing
_xlatesuffix, so that the per-arch headers can define the non suffixed versions.Currently only x86 implements meaningful custom handlers to short circuit the translation when not active, using asm goto. Other architectures use generic macros that map the non-xlate to the xlate variants to keep the previous behavior.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Roger Pau Monné <[email protected]>Once again:
Reviewed-by Jan Beulich <[email protected]>Thanks, I didn't carry your RB due to the changes requested by Andrew, that was a bit too much to carry a RB after those.
Of course, and I didn't mean to suggest you should have kept it. All I wanted to indicate is that I'm as okay withe change as I was before.
Jan
PATCH v4 5/8 test/pdx: add PDX compression unit tests
In reply to Roger Pau MonneIntroduce a set of unit tests for PDX compression. The unit tests contains both real and crafted memory maps that are then compressed using the selected PDX algorithm. Note the build system for the unit tests has been done in a way to support adding new compression algorithms easily. That requires generating a new test-pdx-<compress> executable that's build with the selected PDX compression enabled.
Currently the only generated executable is test-pdx-mask that tests PDX mask compression.
---
Changes since v2:
- Use set -e when running the tests.
- Drop stray removal of pdx.c.
- Use * instead of ? for header blank space capture.
- Add some more memory maps.
Changes since v1:
- New in this version (partially pulled out from a different patch).
--- tools/tests/Makefile | 1 + tools/tests/pdx/.gitignore | 2 + tools/tests/pdx/Makefile | 49 +++++++ tools/tests/pdx/harness.h | 84 ++++++++++++ tools/tests/pdx/test-pdx.c | 267 +++++++++++++++++++++++++++++++++++++ xen/common/pdx.c | 4 + 6 files changed, 407 insertions(+) create mode 100644 tools/tests/pdx/.gitignore create mode 100644 tools/tests/pdx/Makefile create mode 100644 tools/tests/pdx/harness.h create mode 100644 tools/tests/pdx/test-pdx.c
tools/tests/Makefile +1 -0
@@ -9,6 +9,7 @@ ifneq ($(clang),y) SUBDIRS-$(CONFIG_X86) += x86_emulator endif SUBDIRS-y += xenstore +SUBDIRS-y += pdx SUBDIRS-y += rangeset SUBDIRS-y += vpci SUBDIRS-y += paging-mempool
tools/tests/pdx/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +/pdx.h +/test-pdx-mask
tools/tests/pdx/Makefile new file +49 -0
49 lines not on this page · raw message
tools/tests/pdx/harness.h new file +84 -0
84 lines not on this page · raw message
tools/tests/pdx/test-pdx.c new file +267 -0
267 lines not on this page · raw message
xen/common/pdx.c +4 -0
@@ -15,6 +15,8 @@* along with this program; If not, see <http://www.gnu.org/licenses/>. */ +/* Trim content when built for the test harness. */ +#ifdef __XEN__#include <xen/init.h> #include <xen/mm.h> #include <xen/bitops.h>
@@ -57,6 +59,8 @@ void set_pdx_range(unsigned long smfn, unsigned long emfn) __set_bit(idx, pdx_group_valid); } +#endif /* __XEN__ */ +#ifndef CONFIG_PDX_NONE #ifdef CONFIG_X86
Signature · 1 line
2.49.0
Re: [PATCH v4 5/8] test/pdx: add PDX compression unit tests
Quoted 11 lines · Roger Pau Monne
Introduce a set of unit tests for PDX compression. The unit tests contains both real and crafted memory maps that are then compressed using the selected PDX algorithm. Note the build system for the unit tests has been done in a way to support adding new compression algorithms easily. That requires generating a new test-pdx-<compress> executable that's build with the selected PDX compression enabled.
Currently the only generated executable is test-pdx-mask that tests PDX mask compression.
Signed-off-by Roger Pau Monné <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD
PATCH v4 6/8 pdx: move some helpers in preparation for new compression
In reply to Roger Pau MonneMove fill_mask(), pdx_region_mask() and pdx_init_mask() to the !CONFIG_PDX_NONE section in preparation of them also being used by a newly added PDX compression.
No functional change intended.
---
git is not very helpful when generating the diff here, and it ends up moving everything around the functions instead of the functions themselves.
--- xen/common/pdx.c | 118 +++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 59 deletions(-)
xen/common/pdx.c +59 -59
@@ -101,59 +101,6 @@ void __init pfn_pdx_add_region(paddr_t base, paddr_t size) ranges[nr_ranges++].pages =PFN_UP (base + size) -PFN_DOWN (base); } -#endif /* !CONFIG_PDX_NONE */ - -#ifdef CONFIG_PDX_MASK_COMPRESSION - -/* -* Diagram to make sense of the following variables. The masks and shifts -* are done on mfn values in order to convert to/from pdx: -* -* pfn_hole_mask -* pfn_pdx_hole_shift (mask bitsize) -* | -* |---------| -* | | -* V V -* -------------------------- -* |HHHHHHH|000000000|LLLLLL| <--- mfn -* -------------------------- -* ^ ^ ^ ^ -* | | |------| -* | | | -* | | pfn_pdx_bottom_mask -* | | -* |-------| -* | -* pfn_top_mask -* -* ma_{top,va_bottom}_mask is simply a shifted pfn_{top,pdx_bottom}_mask, -* where ma_top_mask has zeroes shifted in while ma_va_bottom_mask has -* ones. -*/ - -/** Mask for the lower non-compressible bits of an mfn */ -unsigned long __ro_after_init pfn_pdx_bottom_mask = ~0UL ; - -/** Mask for the lower non-compressible bits of an maddr or vaddr */ -unsigned long __ro_after_init ma_va_bottom_mask = ~0UL ; - -/** Mask for the higher non-compressible bits of an mfn */ -unsigned long __ro_after_init pfn_top_mask =0 ; - -/** Mask for the higher non-compressible bits of an maddr or vaddr */ -unsigned long __ro_after_init ma_top_mask =0 ; - -/** -* Mask for a pdx compression bit slice. -* -* Invariant: valid(mfn) implies (mfn & pfn_hole_mask) == 0 -*/ -unsigned long __ro_after_init pfn_hole_mask =0 ; - -/** Number of bits of the "compressible" bit slice of an mfn */ -unsigned int __ro_after_init pfn_pdx_hole_shift =0 ; -/* Sets all bits from the most-significant 1-bit down to the LSB */ static uint64_t fill_mask (uint64_t mask) {
@@ -196,12 +143,6 @@ static uint64_t pdx_region_mask(uint64_t base, uint64_t len)return fill_mask (base ^ (base + len -1 )); } -bool pdx_is_region_compressible (paddr_t base,unsigned long npages) -{ -return !(paddr_to_pfn (base) & pfn_hole_mask) && - !(pdx_region_mask (base, npages *PAGE_SIZE ) & ~ma_va_bottom_mask); -} -/** * Creates the mask to start from when calculating non-compressible bits *
@@ -219,6 +160,65 @@ static uint64_t __init pdx_init_mask(uint64_t base_addr) (uint64_t)1 << (MAX_ORDER + PAGE_SHIFT)) - 1); } +#endif /* !CONFIG_PDX_NONE */ + +#ifdef CONFIG_PDX_MASK_COMPRESSION + +/* +* Diagram to make sense of the following variables. The masks and shifts +* are done on mfn values in order to convert to/from pdx: +* +* pfn_hole_mask +* pfn_pdx_hole_shift (mask bitsize) +* | +* |---------| +* | | +* V V +* -------------------------- +* |HHHHHHH|000000000|LLLLLL| <--- mfn +* -------------------------- +* ^ ^ ^ ^ +* | | |------| +* | | | +* | | pfn_pdx_bottom_mask +* | | +* |-------| +* | +* pfn_top_mask +* +* ma_{top,va_bottom}_mask is simply a shifted pfn_{top,pdx_bottom}_mask, +* where ma_top_mask has zeroes shifted in while ma_va_bottom_mask has +* ones. +*/ + +/** Mask for the lower non-compressible bits of an mfn */ +unsigned long __ro_after_init pfn_pdx_bottom_mask = ~0UL ; + +/** Mask for the lower non-compressible bits of an maddr or vaddr */ +unsigned long __ro_after_init ma_va_bottom_mask = ~0UL ; + +/** Mask for the higher non-compressible bits of an mfn */ +unsigned long __ro_after_init pfn_top_mask =0 ; + +/** Mask for the higher non-compressible bits of an maddr or vaddr */ +unsigned long __ro_after_init ma_top_mask =0 ; + +/** +* Mask for a pdx compression bit slice. +* +* Invariant: valid(mfn) implies (mfn & pfn_hole_mask) == 0 +*/ +unsigned long __ro_after_init pfn_hole_mask =0 ; + +/** Number of bits of the "compressible" bit slice of an mfn */ +unsigned int __ro_after_init pfn_pdx_hole_shift =0 ; + +bool pdx_is_region_compressible (paddr_t base,unsigned long npages) +{ +return !(paddr_to_pfn (base) & pfn_hole_mask) && + !(pdx_region_mask (base, npages *PAGE_SIZE ) & ~ma_va_bottom_mask); +} +bool __init pfn_pdx_compression_setup (paddr_t base) {unsigned int i, j, bottom_shift =0 , hole_shift =0 ;
Signature · 1 line
2.49.0
PATCH v4 7/8 pdx: introduce a new compression algorithm based on region offsets
In reply to Roger Pau MonneWith the appearance of Intel Sierra Forest and Granite Rapids it's now possible to get a production x86 host with the following memory map:
SRAT: Node 0 PXM 0 [0000000000000000, 000000007fffffff]
SRAT: Node 0 PXM 0 [0000000100000000, 000000807fffffff]
SRAT: Node 1 PXM 1 [0000063e80000000, 000006be7fffffff]
SRAT: Node 2 PXM 2 [00000c7e80000000, 00000cfe7fffffff]
SRAT: Node 3 PXM 3 [000012be80000000, 0000133e7fffffff]
This is from a four socket Granite Rapids system, with each node having 512GB of memory. The total amount of RAM on the system is 2TB, but without enabling CONFIG_BIGMEM the last range is not accessible, as it's above the 16TB boundary covered by the frame table. Sierra Forest and Granite Rapids are socket compatible, however Sierra Forest only supports 2 socket configurations, while Granite Rapids can go up to 8 sockets.
Note that while the memory map is very sparse, it couldn't be compressed using the current PDX_MASK compression algorithm, which relies on all ranges having a shared zeroed region of bits that can be removed.
The memory map presented above has the property of all regions being similarly spaced between each other, and all having also a similar size. Use a lookup table to store the offsets to translate from/to PFN and PDX spaces. Such table is indexed based on the input PFN or PDX to translated. The example PFN layout about would get compressed using the following:
PFN compression using PFN lookup table shift 29 and PDX region size 0x10000000
range 0 [0000000000000, 0x0000807ffff] PFN IDX 0 : 0000000000000 range 1 [0x00063e80000, 0x0006be7ffff] PFN IDX 3 : 0x00053e80000 range 2 [0x000c7e80000, 0x000cfe7ffff] PFN IDX 6 : 0x000a7e80000 range 3 [0x0012be80000, 0x00133e7ffff] PFN IDX 9 : 0x000fbe80000
Note how the tow ranges belonging to node 0 get merged into a single PDX region by the compression algorithm.
The default size of lookup tables currently set in Kconfig is 64 entries, and the example memory map consumes 10 entries. Such memory map is from a 4 socket Granite Rapids host, which in theory supports up to 8 sockets according to Intel documentation. Assuming the layout of a 8 socket system is similar to the 4 socket one, it would require 21 lookup table entries to support it, way below the current default of 64 entries.
The valid range of lookup table size is currently restricted from 1 to 512 elements in Kconfig.
An extra array is used to keep track of the base PFN for each translated range. Non used slots are set to ~0UL, so that in mfn_valid() the mfn < base check always fails, thus reporting the mfn as invalid.
Introduce __init_or_pdx_mask and use it on some shared functions between PDX mask and offset compression, as otherwise some code becomes unreachable after boot if PDX offset compression is used. Mark the code as __init in that case, so it's pruned after boot.
---
Changes since v3:
- Rename __init_or_pdx_mask.
- Reorder logic in pfn_offset_sanitize_ranges() to reduce code.
- Print a message if the table index is truncated from what would be required to represent all input ranges independently.
- Cast size to paddr_t for pdx_init_mask() call.
Changes since v2:
- s/PDX_OFFSET_TLB_ORDER/PDX_OFFSET_TBL_ORDER/.
- Fix changelog mention of Sapphire Rapids.
- Misc fixes in the test harness.
- Use SWAP() macro.
- Introduce an extra table to keep the bases of the valid ranges.
Changes since v1:
- Use a lookup table with the offsets.
- Split the adding of the test to a pre-patch.
- Amend diagram to also show possible padding after compression.
--- CHANGELOG.md | 3 + tools/tests/pdx/.gitignore | 1 + tools/tests/pdx/Makefile | 3 +- tools/tests/pdx/harness.h | 14 ++ tools/tests/pdx/test-pdx.c | 4 + xen/common/Kconfig | 21 ++- xen/common/pdx.c | 258 ++++++++++++++++++++++++++++++++++++- xen/include/xen/pdx.h | 87 ++++++++++++- 8 files changed, 385 insertions(+), 6 deletions(-)
CHANGELOG.md +3 -0
@@ -20,6 +20,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) grant table or foreign memory. ### Added + - Introduce new PDX compression algorithm to cope with Intel Sierra Forest and + Granite Rapids having sparse memory maps. + - On x86: - Option to attempt to fixup p2m page-faults on PVH dom0. - Resizable BARs is supported for PVH dom0.
tools/tests/pdx/.gitignore +1 -0
@@ -1,2 +1,3 @@ /pdx.h /test-pdx-mask +/test-pdx-offset
tools/tests/pdx/Makefile +2 -1
@@ -1,7 +1,7 @@ XEN_ROOT=$(CURDIR)/../../.. include $(XEN_ROOT)/tools/Rules.mk -TARGETS := test-pdx-mask +TARGETS := test-pdx-mask test-pdx-offset .PHONY: all all: $(TARGETS)
@@ -42,6 +42,7 @@ CFLAGS += $(APPEND_CFLAGS) CFLAGS += $(CFLAGS_xeninclude) test-pdx-mask: CFLAGS += -DCONFIG_PDX_MASK_COMPRESSION +test-pdx-offset: CFLAGS += -DCONFIG_PDX_OFFSET_COMPRESSION test-pdx-%: test-pdx.c pdx.h $(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_$*.o) -o $@ $< $(APPEND_CFLAGS)
tools/tests/pdx/harness.h +14 -0
@@ -44,8 +44,10 @@#define MAX_RANGES 16#define MAX_PFN_RANGES MAX_RANGES +#define CONFIG_PDX_OFFSET_TBL_ORDER 6#define ASSERT assert +#define ASSERT_UNREACHABLE () assert(0)#define CONFIG_DEBUG
@@ -66,10 +68,22 @@ static inline unsigned int find_next(#define find_next_zero_bit (a, s, o) find_next(a, s, o, false)#define find_next_bit (a, s, o) find_next(a, s, o, true) +#define flsl (x) ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0) +#define ffsl (x) __builtin_ffsl(x) +#define boolean_param (name, func)typedef uint64_t paddr_t ; +#define SWAP (a, b) \ + do { typeof(a) t_ = (a); (a) = (b); (b) = t_; } while ( 0 ) + +#define sort (elem, nr, size, cmp, swp) ({ \ +/* Consume swp() so compiler doesn't complain it's unused. */ \ + (void )swp; \ +qsort (elem, nr, size, cmp); \ +}) +#include "pdx.h" #endif
tools/tests/pdx/test-pdx.c +4 -0
@@ -51,7 +51,11 @@ int main(int argc, char **argv) { .start = 0xc7e80000UL, .end = 0xcfe80000UL }, { .start = 0x12be80000UL, .end = 0x133e80000UL }, }, +#ifdef CONFIG_PDX_OFFSET_COMPRESSION + .compress = true, +#else .compress = false, +#endif }, /* Simple hole. */ {
xen/common/Kconfig +20 -1
@@ -59,7 +59,8 @@ config EVTCHN_FIFO choice prompt "PDX (Page inDeX) compression" - default PDX_MASK_COMPRESSION if !X86 && !RISCV + default PDX_OFFSET_COMPRESSION if X86 + default PDX_MASK_COMPRESSION if !RISCV default PDX_NONE help PDX compression is a technique designed to reduce the memory
@@ -78,12 +79,30 @@ config PDX_MASK_COMPRESSION help Compression relying on all RAM addresses sharing a zeroed bit region. +config PDX_OFFSET_COMPRESSION + bool "Offset compression" + help + Compression relying on size and distance between RAM regions being + compressible using an offset lookup table. + config PDX_NONE bool "None" help No compression endchoice +config PDX_OFFSET_TBL_ORDER + int "PDX offset compression lookup table order" if EXPERT + depends on PDX_OFFSET_COMPRESSION + default 6 + range 0 9 + help + Order of the PFN to PDX and PDX to PFN translation lookup tables. + Number of table entries is calculated as 2^N. + + Size of the tables can be adjusted from 1 entry (order 0) to 512 + entries (order 9). + config ALTERNATIVE_CALL bool
xen/common/pdx.c +255 -3
294 lines not on this page · raw message
xen/include/xen/pdx.h +86 -1
99 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v4 7/8] pdx: introduce a new compression algorithm based on region offsets
Quoted 11 lines · Roger Pau Monne
xen/common/pdx.c +1
-0@@ -24,6 +24,7 @@#include <xen/param.h> #include <xen/pfn.h> #include <xen/sections.h> +#include <xen/sort.h> /** * Maximum (non-inclusive) usable pdx. Must be
xen/common/pdx.c bool __mfn_valid(unsigned long mfn) · line 50
Quoted 10 lines
xen/common/pdx.c +6
-0@@ -40,6 +41,12 @@ bool __mfn_valid(unsigned long mfn)#ifdef CONFIG_PDX_MASK_COMPRESSION invalid |= mfn & pfn_hole_mask; +#elif defined(CONFIG_PDX_OFFSET_COMPRESSION ) +{ +unsigned long base = pfn_bases[PFN_TBL_IDX (mfn)]; + + invalid |= mfn < base || mfn >= base + pdx_region_size; +}#endif
Hmm, didn't notice this earlier on: Brace placement looks odd here. I think they want to be indented by one level, as they aren't starting a function body.
Quoted 4 lines
xen/common/pdx.c +0
-0@@ -294,7 +308,245 @@ void __init pfn_pdx_compression_reset(void) nr_ranges =0 ; }
xen/common/pdx.c void __init pfn_pdx_compression_reset(void) · line 319
Quoted 10 lines
xen/common/pdx.c +9
-1-#endif /* CONFIG_PDX_COMPRESSION */ +#elif defined(CONFIG_PDX_OFFSET_COMPRESSION )/* CONFIG_PDX_MASK_COMPRESSION */ + +unsigned int __ro_after_init pfn_index_shift; +unsigned int __ro_after_init pdx_index_shift; + +unsigned long __ro_after_init pfn_pdx_lookup[CONFIG_PDX_NR_LOOKUP ]; +unsigned long __ro_after_init pdx_pfn_lookup[CONFIG_PDX_NR_LOOKUP ]; +unsigned long __ro_after_init pfn_bases[CONFIG_PDX_NR_LOOKUP ]; +unsigned long __ro_after_init pdx_region_size = ~0UL ;
For cache locality, might this last one better also move ahead of the arrays?
Quoted 16 lines
xen/common/pdx.c +16
-0+bool pdx_is_region_compressible (paddr_t base,unsigned long npages) +{ +unsigned long pfn =PFN_DOWN (base); +unsigned long pfn_base = pfn_bases[PFN_TBL_IDX (pfn)]; + +return pfn >= pfn_base && + pfn + npages <= pfn_base + pdx_region_size; +} + +static int __init cf_check cmp_node(const void *a, const void *b) +{ +const struct pfn_range *l = a; +const struct pfn_range *r = b; + +if ( l->base_pfn > r->base_pfn ) +return 1 ;
xen/common/pdx.c void __init pfn_pdx_compression_reset(void) · line 346
Quoted 10 lines
xen/common/pdx.c +10
-0+if ( l->base_pfn < r->base_pfn ) +return -1 ; + +return 0 ; +} + +static void __init cf_check swp_node(void *a, void *b) +{ +SWAP (a, b); +}
This hasn't changed from v3, and still looks wrong to me.
Quoted 60 lines
xen/common/pdx.c +60
-0+bool __init pfn_pdx_compression_setup (paddr_t base) +{ +unsigned long mask =PFN_DOWN (pdx_init_mask (base)), idx_mask =0 ; +unsigned long pages =0 ; +unsigned int i; + +if ( !nr_ranges ) + { +printk (XENLOG_DEBUG "PFN compression disabled%s\n" , + pdx_compress ?": no ranges provided" :"" ); +return false; + } + +if ( nr_ranges >ARRAY_SIZE (ranges) ) + { +printk (XENLOG_WARNING +"Too many PFN ranges (%u > %zu), not attempting PFN compression\n" , + nr_ranges,ARRAY_SIZE (ranges)); +return false; + } + +/* Sort ranges by start address. */ +sort (ranges, nr_ranges,sizeof (*ranges), cmp_node, swp_node); + +for ( i =0 ; i < nr_ranges; i++ ) + { +unsigned long start = ranges[i].base_pfn; + +/* +* Align range base to MAX_ORDER. This is required so the PDX offset +* for the bits below MAX_ORDER matches the MFN offset, and pages +* greater than the minimal order can be used to populate the +* directmap. +*/ + ranges[i].base_pfn = start & ~((1UL <<MAX_ORDER ) -1 ); + ranges[i].pages = start + ranges[i].pages - ranges[i].base_pfn; + +/* +* Only merge overlapped regions now, leave adjacent regions separated. +* They would be merged later if both use the same index into the +* lookup table. +*/ +if ( !i || + ranges[i].base_pfn >= + (ranges[i -1 ].base_pfn + ranges[i -1 ].pages) ) + { + mask |=pdx_region_mask (ranges[i].base_pfn, ranges[i].pages); +continue ; + } + + ranges[i -1 ].pages = ranges[i].base_pfn + ranges[i].pages - + ranges[i -1 ].base_pfn; + +if ( i +1 < nr_ranges ) +memmove (&ranges[i], &ranges[i +1 ], + (nr_ranges - (i +1 )) *sizeof (ranges[0 ])); +else /* last range */ + mask |=pdx_region_mask (ranges[i].base_pfn, ranges[i].pages); + nr_ranges--; + i--;
xen/common/pdx.c void __init pfn_pdx_compression_reset(void) · line 459
Quoted 10 lines
xen/common/pdx.c +10
-0+ } + +/* +* Populate a mask with the non-equal bits of the different ranges, do this +* to calculate the maximum PFN shift to use as the lookup table index. +*/ +for ( i =0 ; i < nr_ranges; i++ ) +for (unsigned int j =0 ; j < nr_ranges; j++ ) + idx_mask |= (ranges[i].base_pfn & ~mask) ^ + (ranges[j].base_pfn & ~mask);
"mask" is loop invariant - can't the AND-ing be pulled out, after the loop? Further, isn't it sufficient for the inner loop to start from i + 1?
Jan
Roger Pau Monné
AppliedRe: [PATCH v4 7/8] pdx: introduce a new compression algorithm based on region offsets
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne
xen/common/pdx.c +1
-0@@ -24,6 +24,7 @@#include <xen/param.h> #include <xen/pfn.h> #include <xen/sections.h> +#include <xen/sort.h> /** * Maximum (non-inclusive) usable pdx. Must be @@ -40,6 +41,12 @@ bool __mfn_valid(unsigned long mfn)
Quoted 13 lines
xen/common/pdx.c bool __mfn_valid(unsigned long mfn) · line 50
Quoted 9 lines
xen/common/pdx.c +6
-0#ifdef CONFIG_PDX_MASK_COMPRESSION invalid |= mfn & pfn_hole_mask; +#elif defined(CONFIG_PDX_OFFSET_COMPRESSION ) +{ +unsigned long base = pfn_bases[PFN_TBL_IDX (mfn)]; + + invalid |= mfn < base || mfn >= base + pdx_region_size; +}#endif Hmm, didn't notice this earlier on: Brace placement looks odd here. I think they want to be indented by one level, as they aren't starting a function body.
Right, I can adjust. Since they are inside of the ifdef block it did look kind of OK to me, and avoided having to indent the content one extra level.
Quoted 4 lines
Quoted 4 lines
xen/common/pdx.c +0
-0@@ -294,7 +308,245 @@ void __init pfn_pdx_compression_reset(void) nr_ranges =0 ; }
Quoted 12 lines
xen/common/pdx.c void __init pfn_pdx_compression_reset(void) · line 319
Quoted 10 lines
xen/common/pdx.c +9
-1-#endif /* CONFIG_PDX_COMPRESSION */ +#elif defined(CONFIG_PDX_OFFSET_COMPRESSION )/* CONFIG_PDX_MASK_COMPRESSION */ + +unsigned int __ro_after_init pfn_index_shift; +unsigned int __ro_after_init pdx_index_shift; + +unsigned long __ro_after_init pfn_pdx_lookup[CONFIG_PDX_NR_LOOKUP ]; +unsigned long __ro_after_init pdx_pfn_lookup[CONFIG_PDX_NR_LOOKUP ]; +unsigned long __ro_after_init pfn_bases[CONFIG_PDX_NR_LOOKUP ]; +unsigned long __ro_after_init pdx_region_size = ~0UL ;For cache locality, might this last one better also move ahead of the arrays?
Oh, yes, this was a late addition and I clearly didn't place enough attention when adding it.
Quoted 20 lines
Quoted 20 lines
xen/common/pdx.c +20
-0+bool pdx_is_region_compressible (paddr_t base,unsigned long npages) +{ +unsigned long pfn =PFN_DOWN (base); +unsigned long pfn_base = pfn_bases[PFN_TBL_IDX (pfn)]; + +return pfn >= pfn_base && + pfn + npages <= pfn_base + pdx_region_size; +} + +static int __init cf_check cmp_node(const void *a, const void *b) +{ +const struct pfn_range *l = a; +const struct pfn_range *r = b; + +if ( l->base_pfn > r->base_pfn ) +return 1 ; +if ( l->base_pfn < r->base_pfn ) +return -1 ; + +return 0 ;
Quoted 8 lines
xen/common/pdx.c void __init pfn_pdx_compression_reset(void) · line 346
Quoted 6 lines
xen/common/pdx.c +6
-0+} + +static void __init cf_check swp_node(void *a, void *b) +{ +SWAP (a, b); +}This hasn't changed from v3, and still looks wrong to me.
Oh, I did recall a comment to that regard, but somehow forgot to apply it, I'm sorry, I've now fixed it.
Quoted 64 lines
Quoted 64 lines
xen/common/pdx.c +64
-0+bool __init pfn_pdx_compression_setup (paddr_t base) +{ +unsigned long mask =PFN_DOWN (pdx_init_mask (base)), idx_mask =0 ; +unsigned long pages =0 ; +unsigned int i; + +if ( !nr_ranges ) + { +printk (XENLOG_DEBUG "PFN compression disabled%s\n" , + pdx_compress ?": no ranges provided" :"" ); +return false; + } + +if ( nr_ranges >ARRAY_SIZE (ranges) ) + { +printk (XENLOG_WARNING +"Too many PFN ranges (%u > %zu), not attempting PFN compression\n" , + nr_ranges,ARRAY_SIZE (ranges)); +return false; + } + +/* Sort ranges by start address. */ +sort (ranges, nr_ranges,sizeof (*ranges), cmp_node, swp_node); + +for ( i =0 ; i < nr_ranges; i++ ) + { +unsigned long start = ranges[i].base_pfn; + +/* +* Align range base to MAX_ORDER. This is required so the PDX offset +* for the bits below MAX_ORDER matches the MFN offset, and pages +* greater than the minimal order can be used to populate the +* directmap. +*/ + ranges[i].base_pfn = start & ~((1UL <<MAX_ORDER ) -1 ); + ranges[i].pages = start + ranges[i].pages - ranges[i].base_pfn; + +/* +* Only merge overlapped regions now, leave adjacent regions separated. +* They would be merged later if both use the same index into the +* lookup table. +*/ +if ( !i || + ranges[i].base_pfn >= + (ranges[i -1 ].base_pfn + ranges[i -1 ].pages) ) + { + mask |=pdx_region_mask (ranges[i].base_pfn, ranges[i].pages); +continue ; + } + + ranges[i -1 ].pages = ranges[i].base_pfn + ranges[i].pages - + ranges[i -1 ].base_pfn; + +if ( i +1 < nr_ranges ) +memmove (&ranges[i], &ranges[i +1 ], + (nr_ranges - (i +1 )) *sizeof (ranges[0 ])); +else /* last range */ + mask |=pdx_region_mask (ranges[i].base_pfn, ranges[i].pages); + nr_ranges--; + i--; + } + +/* +* Populate a mask with the non-equal bits of the different ranges, do this
Quoted 8 lines
xen/common/pdx.c void __init pfn_pdx_compression_reset(void) · line 459
Quoted 6 lines
xen/common/pdx.c +6
-0+* to calculate the maximum PFN shift to use as the lookup table index. +*/ +for ( i =0 ; i < nr_ranges; i++ ) +for (unsigned int j =0 ; j < nr_ranges; j++ ) + idx_mask |= (ranges[i].base_pfn & ~mask) ^ + (ranges[j].base_pfn & ~mask);"mask" is loop invariant - can't the AND-ing be pulled out, after the loop?
I've applied both of the above, thanks for the help.
Regards, Roger.
PATCH v4 8/8 x86/mm: adjust loop in arch_init_memory() to iterate over the PDX space
In reply to Roger Pau MonneThere's a loop in arch_init_memory() that iterates over holes and non-RAM regions to possibly mark any page_info structures matching those addresses as IO. The looping there is done over the PFN space.
PFNs not covered by the PDX space will always fail the mfn_valid() check, hence re-write the loop to iterate over the PDX space and avoid checking any holes that are not covered by the PDX translation.
On a system with a ~6TiB hole this change together with using PDX compression reduces boot time in approximately 20 seconds. Xen boot time without the change is ~50s, with the change it's ~30s.
---
Changes since v3:
- Ensure parameter to pfn_to_pdx() is always RAM.
Changes since v2:
- New in this version.
--- xen/arch/x86/mm.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
xen/arch/x86/mm.c +14 -3
@@ -275,7 +275,7 @@ static void __init assign_io_page(struct page_info *page)void __init arch_init_memory (void ) { -unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn; +unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn, pdx;/* * Basic guest-accessible flags:
@@ -328,9 +328,20 @@ void __init arch_init_memory(void)destroy_xen_mappings ((unsigned long )mfn_to_virt (iostart_pfn), (unsigned long )mfn_to_virt (ioend_pfn)); -/* Mark as I/O up to next RAM region. */ -for ( ; pfn < rstart_pfn; pfn++ ) +/* +* Mark as I/O up to next RAM region. Iterate over the PDX space to +* skip holes which would always fail the mfn_valid() check. +* +* pfn_to_pdx() requires a valid (iow: RAM) PFN to convert to PDX, +* hence provide pfn - 1, which is the tailing PFN from the last RAM +* range, or pdx 0 if the input pfn is 0. +*/ +for ( pdx = pfn ?pfn_to_pdx (pfn -1 ) +1 :0 ; + pdx <pfn_to_pdx (rstart_pfn); + pdx++ ) { + pfn =pdx_to_pfn (pdx); +if ( !mfn_valid (_mfn (pfn)) )continue ;
Signature · 1 line
2.49.0
Re: [PATCH v4 8/8] x86/mm: adjust loop in arch_init_memory() to iterate over the PDX space
Quoted 24 lines · Roger Pau Monne
xen/arch/x86/mm.c +8
-3@@ -275,7 +275,7 @@ static void __init assign_io_page(struct page_info *page)void __init arch_init_memory (void ) { -unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn; +unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn, pdx;/* * Basic guest-accessible flags: @@ -328,9 +328,20 @@ void __init arch_init_memory(void)destroy_xen_mappings ((unsigned long )mfn_to_virt (iostart_pfn), (unsigned long )mfn_to_virt (ioend_pfn)); -/* Mark as I/O up to next RAM region. */ -for ( ; pfn < rstart_pfn; pfn++ ) +/* +* Mark as I/O up to next RAM region. Iterate over the PDX space to +* skip holes which would always fail the mfn_valid() check. +* +* pfn_to_pdx() requires a valid (iow: RAM) PFN to convert to PDX, +* hence provide pfn - 1, which is the tailing PFN from the last RAM +* range, or pdx 0 if the input pfn is 0.
xen/arch/x86/mm.c void __init arch_init_memory(void) · line 347
Quoted 10 lines
xen/arch/x86/mm.c +6
-0+*/ +for ( pdx = pfn ?pfn_to_pdx (pfn -1 ) +1 :0 ; + pdx <pfn_to_pdx (rstart_pfn); + pdx++ ) { + pfn =pdx_to_pfn (pdx); +if ( !mfn_valid (_mfn (pfn)) )continue ;
As much as I would have liked to ack this, I fear there's another caveat here:
At the top of the loop we check not only for RAM, but also for UNUSABLE. The latter, like RAM, shouldn't be marked I/O, but we also can't use PFN <-> PDX transformations on any such page.
Jan
Re: [PATCH v4 8/8] x86/mm: adjust loop in arch_init_memory() to iterate over the PDX space
Quoted 32 lines · Jan Beulich
Quoted 31 lines · Roger Pau Monne
xen/arch/x86/mm.c +14
-3@@ -275,7 +275,7 @@ static void __init assign_io_page(struct page_info *page)void __init arch_init_memory (void ) { -unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn; +unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn, pdx;/* * Basic guest-accessible flags: @@ -328,9 +328,20 @@ void __init arch_init_memory(void)destroy_xen_mappings ((unsigned long )mfn_to_virt (iostart_pfn), (unsigned long )mfn_to_virt (ioend_pfn)); -/* Mark as I/O up to next RAM region. */ -for ( ; pfn < rstart_pfn; pfn++ ) +/* +* Mark as I/O up to next RAM region. Iterate over the PDX space to +* skip holes which would always fail the mfn_valid() check. +* +* pfn_to_pdx() requires a valid (iow: RAM) PFN to convert to PDX, +* hence provide pfn - 1, which is the tailing PFN from the last RAM +* range, or pdx 0 if the input pfn is 0. +*/ +for ( pdx = pfn ?pfn_to_pdx (pfn -1 ) +1 :0 ; + pdx <pfn_to_pdx (rstart_pfn); + pdx++ ) { + pfn =pdx_to_pfn (pdx); +
Quoted 8 lines
xen/arch/x86/mm.c void __init arch_init_memory(void) · line 347
Quoted 3 lines
xen/arch/x86/mm.c +0
-0if ( !mfn_valid (_mfn (pfn)) )continue ;As much as I would have liked to ack this, I fear there's another caveat here:
At the top of the loop we check not only for RAM, but also for UNUSABLE. The latter, like RAM, shouldn't be marked I/O, but we also can't use PFN <-> PDX transformations on any such page.
Right you are. I'm not sure however why we do this - won't we want the mappings of UNUSABLE regions also be removed from the Xen page-tables? (but not marked as IO)
I could do something like:
/* Mark as I/O up to next RAM or UNUSABLE region. */
if ( (!pfn || pdx_is_region_compressible(pfn_to_paddr(pfn - 1), 1)) &&
pdx_is_region_compressible(pfn_to_paddr(rstart_pfn), 1) )
{
/*
* Iterate over the PDX space to skip holes which would always fail
* the mfn_valid() check.
*
* pfn_to_pdx() requires a valid (iow: RAM) PFN to convert to PDX,
* hence provide pfn - 1, which is the tailing PFN from the last
* RAM range, or pdx 0 if the input pfn is 0.
*/
for ( pdx = pfn ? pfn_to_pdx(pfn - 1) + 1 : 0;
pdx < pfn_to_pdx(rstart_pfn);
pdx++ )
{
pfn = pdx_to_pfn(pdx);
if ( !mfn_valid(_mfn(pfn)) )
continue;
assign_io_page(mfn_to_page(_mfn(pfn)));
}
}
else
{
/* Slow path, iterate over the PFN space. */
for ( ; pfn < rstart_pfn; pfn++ )
{
if ( !mfn_valid(_mfn(pfn)) )
continue;
assign_io_page(mfn_to_page(_mfn(pfn)));
}
}But I find it a bit ugly - I might send v5 without this final patch while I see if I can find a better alternative.
Thanks, Roger.
Re: [PATCH v4 8/8] x86/mm: adjust loop in arch_init_memory() to iterate over the PDX space
Quoted 42 lines · Roger Pau Monné
Quoted 32 lines · Jan Beulich
Quoted 31 lines · Roger Pau Monne
xen/arch/x86/mm.c +14
-3@@ -275,7 +275,7 @@ static void __init assign_io_page(struct page_info *page)void __init arch_init_memory (void ) { -unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn; +unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn, pdx;/* * Basic guest-accessible flags: @@ -328,9 +328,20 @@ void __init arch_init_memory(void)destroy_xen_mappings ((unsigned long )mfn_to_virt (iostart_pfn), (unsigned long )mfn_to_virt (ioend_pfn)); -/* Mark as I/O up to next RAM region. */ -for ( ; pfn < rstart_pfn; pfn++ ) +/* +* Mark as I/O up to next RAM region. Iterate over the PDX space to +* skip holes which would always fail the mfn_valid() check. +* +* pfn_to_pdx() requires a valid (iow: RAM) PFN to convert to PDX, +* hence provide pfn - 1, which is the tailing PFN from the last RAM +* range, or pdx 0 if the input pfn is 0. +*/ +for ( pdx = pfn ?pfn_to_pdx (pfn -1 ) +1 :0 ; + pdx <pfn_to_pdx (rstart_pfn); + pdx++ ) { + pfn =pdx_to_pfn (pdx); +Quoted 8 lines
xen/arch/x86/mm.c void __init arch_init_memory(void) · line 347
Quoted 3 lines
xen/arch/x86/mm.c +0
-0if ( !mfn_valid (_mfn (pfn)) )continue ;As much as I would have liked to ack this, I fear there's another caveat here:
At the top of the loop we check not only for RAM, but also for UNUSABLE. The latter, like RAM, shouldn't be marked I/O, but we also can't use PFN <-> PDX transformations on any such page.
Quoted 3 lines
Right you are. I'm not sure however why we do this - won't we want the mappings of UNUSABLE regions also be removed from the Xen page-tables? (but not marked as IO)
Yes, I think this is a flaw in current code. Perhaps it was (wrongly) assumed that no UNUSABLE regions would ever exist this low in a memory map? Imo we want to deal with this in two steps - first sort the UNUSABLE issue, then improve the dealing with what is passed to assign_io_page().
While there we may also want to find a way to tie together the 16Mb boundary checks - the 16UL isn't properly connected to the BOOTSTRAP_MAP_BASE definition in setup.c. Yet then: Am I overlooking something, or is the 16Mb boundary not really special anymore? I.e. could e.g. BOOTSTRAP_MAP_BASE perhaps be moved (at least in principle), either almost arbitrarily up (within the low 4Gb), or down as much as to the 2Mb boundary? The relevant aspect here would be that the comment saying "the statically-initialised 1-16MB mapping area" looks to be stale, as of 7cd7f2f5e116 ("x86/boot: Remove the preconstructed low 16M superpage mappings"). If there are excess mappings to worry about, those may nowadays well live above the 16Mb boundary (because of it being 2Mb mappings that head.S inserts into l2_directmap[]).
If I was to sensibly make changes to that code, I guess I first ought to find a system which actually surfaces any (ideally "interesting") UNUSABLE ranges. In any event, I think the cleanest thing we can do is split the loop in two, one to deal with the removal of mappings between RAM regions up to the 4Gb boundary, and the other to invoke assign_io_page() for holes between RAM || UNUSABLE ones. The latter would then be subject to your optimization. The former could ignore the PDX aspect due to being limited to the low 4Gb, where we never compress.
Jan
Re: [PATCH v4 8/8] x86/mm: adjust loop in arch_init_memory() to iterate over the PDX space
Quoted 52 lines · Jan Beulich
Quoted 42 lines · Roger Pau Monné
Quoted 32 lines · Jan Beulich
Quoted 31 lines · Roger Pau Monne
xen/arch/x86/mm.c +14
-3@@ -275,7 +275,7 @@ static void __init assign_io_page(struct page_info *page)void __init arch_init_memory (void ) { -unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn; +unsigned long i, pfn, rstart_pfn, rend_pfn, iostart_pfn, ioend_pfn, pdx;/* * Basic guest-accessible flags: @@ -328,9 +328,20 @@ void __init arch_init_memory(void)destroy_xen_mappings ((unsigned long )mfn_to_virt (iostart_pfn), (unsigned long )mfn_to_virt (ioend_pfn)); -/* Mark as I/O up to next RAM region. */ -for ( ; pfn < rstart_pfn; pfn++ ) +/* +* Mark as I/O up to next RAM region. Iterate over the PDX space to +* skip holes which would always fail the mfn_valid() check. +* +* pfn_to_pdx() requires a valid (iow: RAM) PFN to convert to PDX, +* hence provide pfn - 1, which is the tailing PFN from the last RAM +* range, or pdx 0 if the input pfn is 0. +*/ +for ( pdx = pfn ?pfn_to_pdx (pfn -1 ) +1 :0 ; + pdx <pfn_to_pdx (rstart_pfn); + pdx++ ) { + pfn =pdx_to_pfn (pdx); +Quoted 8 lines
xen/arch/x86/mm.c void __init arch_init_memory(void) · line 347
Quoted 3 lines
xen/arch/x86/mm.c +0
-0if ( !mfn_valid (_mfn (pfn)) )continue ;As much as I would have liked to ack this, I fear there's another caveat here:
At the top of the loop we check not only for RAM, but also for UNUSABLE. The latter, like RAM, shouldn't be marked I/O, but we also can't use PFN <-> PDX transformations on any such page.Quoted 3 lines
Right you are. I'm not sure however why we do this - won't we want the mappings of UNUSABLE regions also be removed from the Xen page-tables? (but not marked as IO)
Yes, I think this is a flaw in current code. Perhaps it was (wrongly) assumed that no UNUSABLE regions would ever exist this low in a memory map? Imo we want to deal with this in two steps - first sort the UNUSABLE issue, then improve the dealing with what is passed to
assign_io_page().
Quoted 11 lines
While there we may also want to find a way to tie together the 16Mb boundary checks - the 16UL isn't properly connected to the
BOOTSTRAP_MAP_BASEdefinition insetup.c. Yet then: Am I overlooking something, or is the 16Mb boundary not really special anymore? I.e. could e.g.BOOTSTRAP_MAP_BASEperhaps be moved (at least in principle), either almost arbitrarily up (within the low 4Gb), or down as much as to the 2Mb boundary? The relevant aspect here would be that the comment saying "the statically-initialised 1-16MB mapping area" looks to be stale, as of7cd7f2f5e116("x86/boot: Remove the preconstructed low 16M superpage mappings"). If there are excess mappings to worry about, those may nowadays well live above the 16Mb boundary (because of it being 2Mb mappings thathead.Sinserts into l2_directmap[]).
Hmm, extending this to beyond 16M collides with mappings done by acpi_dmar_init(), erst_init(), and acpi_hest_init(). Luckily efi_init_memory() runs only afterwards. While I guess I could limit this to the space 2M mappings were done for in head.S, theoretically there could still be a collision afterwards. So I think we need to either somehow exclude such mappings (might end up fragile), or stop (ab)using the directmap there. Thoughts?
Jan