Skip to contentSkip to conversation

pdx: introduce a new compression algorithm based on region offsets

PATCH In review Roger Pau Monne 3 participants 5 messages Aug 12, 2025 – Aug 18, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 pdx: introduce a new compression algorithm based on region offsets

With 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 v4:
- Fold pfn_offset_sanitize_ranges() into pfn_pdx_compression_setup().
- Fix usage of SWAP().
- Indented code block added to mfn_valid().
- Optimize a loop.
- Move definition of pdx_region_size.

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           | 241 ++++++++++++++++++++++++++++++++++++-
 xen/include/xen/pdx.h      |  87 ++++++++++++-
 8 files changed, 368 insertions(+), 6 deletions(-)
CHANGELOG.md +3 -0
index 5f31ca08fe3f..f9ef893f4851 100644
@@ -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
index a32c7db4de79..1202a531a7fd 100644
@@ -1,2 +1,3 @@
 /pdx.h
 /test-pdx-mask
+/test-pdx-offset
tools/tests/pdx/Makefile +2 -1
index b3734afde686..10b354f0cefd 100644
@@ -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
index 5bef7df650d2..7c1c5a246868 100644
@@ -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
index 0798ccee359b..eefd54c76815 100644
@@ -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
index 8dad0c923a9d..76f9ce705f7a 100644
@@ -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 +238 -3

277 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
#1 lore raw
Quoted 1 line · Roger Pau Monne

Not this patch, but this is probably the best place to report it.

https://scan5.scan.coverity.com/#/project-view/30554/10426?selectedIssue=1662707

Something you did in the series made enough changes in pfn_pdx_compression_setup() for Coverity to start reporting an issue in some decade-old code.

The complaint is on line:

277    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1; CID 1662707: (#1 of 1): Bad bit shift operation (BAD_SHIFT)

34. large_shift: In expression 0x1000 << bottom_shift, left shifting by more than 31 bits has undefined behavior. The shift amount, bottom_shift, is as much as 63.

The relevant part of earlier analysis seems to be the "i >= BITS_PER_LONG" check in the order loop, causing Coverity to think that i can be up to 63.

~Andrew

#2 lore raw
Quoted 16 lines · Andrew Cooper
Quoted 1 line · Roger Pau Monne

Not this patch, but this is probably the best place to report it.

https://scan5.scan.coverity.com/#/project-view/30554/10426?selectedIssue=1662707

Something you did in the series made enough changes in pfn_pdx_compression_setup() for Coverity to start reporting an issue in some decade-old code.

The complaint is on line:

277    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1; CID 1662707: (#1 of 1): Bad bit shift operation (BAD_SHIFT)

Quoted 7 lines

34. large_shift: In expression 0x1000 << bottom_shift, left shifting by more than 31 bits has undefined behavior. The shift amount, bottom_shift, is as much as 63.

The relevant part of earlier analysis seems to be the "i >= BITS_PER_LONG" check in the order loop, causing Coverity to think that i can be up to 63.

Yes, I've also got the email. This is existing code, so the issue wasn't introduced by the previous patches, it's probably result of the code movement that Coverity picked it up.

I will send a patch to fix this.

Thanks, Roger.

#3 lore raw
Quoted 25 lines · Roger Pau Monné
Quoted 16 lines · Andrew Cooper
Quoted 1 line · Roger Pau Monne

Not this patch, but this is probably the best place to report it.

https://scan5.scan.coverity.com/#/project-view/30554/10426?selectedIssue=1662707

Something you did in the series made enough changes in pfn_pdx_compression_setup() for Coverity to start reporting an issue in some decade-old code.

The complaint is on line:

277    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1; CID 1662707: (#1 of 1): Bad bit shift operation (BAD_SHIFT)

Quoted 7 lines

34. large_shift: In expression 0x1000 << bottom_shift, left shifting by more than 31 bits has undefined behavior. The shift amount, bottom_shift, is as much as 63.

The relevant part of earlier analysis seems to be the "i >= BITS_PER_LONG" check in the order loop, causing Coverity to think that i can be up to 63.

Quoted 5 lines

Yes, I've also got the email. This is existing code, so the issue wasn't introduced by the previous patches, it's probably result of the code movement that Coverity picked it up.

I will send a patch to fix this.

Isn't it simply because this file is now also built for a toolstack side test?

Jan

Reviewed-by on v5 1/1
#4 lore raw
In reply to Roger Pau Monne
Quoted 49 lines · Roger Pau Monne

With 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.

Quoted 6 lines

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.

with one cosmetic remark (sorry for not spotting this earlier):

Quoted 20 lines
tools/tests/pdx/harness.h +5 -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)
+

tools/tests/pdx/harness.h static inline unsigned int find_next( · line 83

Quoted 10 lines
tools/tests/pdx/harness.h +6 -0
 #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;                                                          \

It generally shouldn't matter here, yet maybe still better to parenthesize swp.

Jan