Skip to contentSkip to conversation

memory: overlapping XENMAPSPACE_gmfn_range requests

PATCH In review Jan Beulich 3 participants 7 messages Dec 22, 2025 – May 14

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 memory: overlapping XENMAPSPACE_gmfn_range requests

Overlapping requests may need processing backwards, or else the intended effect wouldn't be achieved (and instead some pages would be moved more than once).

With some adjustment there this also covers XEN_DMOP_relocate_memory, where the potential issue was first noticed.

Fixes a04811a315e0 ("mm: New XENMEM space, XENMAPSPACE_gmfn_range")

---
Of course an alternative would be to simply reject overlapping requests. Then we should reject all overlaps though, I think. But since the code change didn't end up overly intrusive, I thought I would go the "fix it" route first.
---
v2: Adjust XEN_DMOP_relocate_memory handling for the working-backwards
case.

xen/arch/x86/hvm/dm.c +6 -2
@@ -569,12 +569,16 @@ int dm_op(const struct dmop_args *op_arg
 
         rc = xenmem_add_to_physmap(d, &xatp, 0);
         if ( rc == 0 && data->size != xatp.size )
+        {
             rc = xatp.size;
+            xatp.idx += rc;
+            xatp.gpfn += rc;
+        }
         if ( rc > 0 )
         {
             data->size -= rc;
-            data->src_gfn += rc;
-            data->dst_gfn += rc;
+            data->src_gfn = xatp.idx;
+            data->dst_gfn = xatp.gpfn;
             const_op = false;
             rc = -ERESTART;
         }
xen/common/memory.c +40 -7

83 lines not on this page · raw message

#1 lore raw

cover tests/paging-mempool: Testing for P2M relocation

Andrew Cooper (2):
tests/paging-mempool: Misc cleanup tests/paging-mempool: Extend to test P2M relocation

 tools/tests/paging-mempool/Makefile           |   4 +
 .../paging-mempool/test-paging-mempool.c      | 222 +++++++++++++++++-
 xen/common/memory.c                           |   2 +-
 3 files changed, 223 insertions(+), 5 deletions(-)
Signature · 1 line
2.39.5
#2 lore raw

PATCH 1/2 tests/paging-mempool: Misc cleanup

... ahead of extending it with a second test.

Rename run_tests() to test_paging_mempool_size() and have it return int so the newly wrapping run_tests() can decide whether to run the next or not.

Comment the parameters to xc_domain_populate_physmap_exact() to make it clearer what's going on, and remove the page added to the physmap when the test is done.

No practical change.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
 .../paging-mempool/test-paging-mempool.c      | 25 ++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
tools/tests/paging-mempool/test-paging-mempool.c +22 -3
index 1ebc13455ac2..d1207d26d3fc 100644
@@ -15,6 +15,7 @@ static unsigned int nr_failures;
 ({                                              \
     nr_failures++;                              \
     (void)printf(fmt, ##__VA_ARGS__);           \
+    -1;                                         \
 })
 
 static xc_interface *xch;
@@ -40,7 +41,7 @@ static uint64_t default_mempool_size_bytes =
     16 << 12;
 #endif
 
-static void run_tests(void)
+static int test_paging_mempool_size(void)
 {
     xen_pfn_t physmap[] = { 0 };
     uint64_t size_bytes, old_size_bytes;
@@ -80,7 +81,8 @@ static void run_tests(void)
         return fail("  Fail: setmaxmem: : %d - %s\n",
                     errno, strerror(errno));
 
-    rc = xc_domain_populate_physmap_exact(xch, domid, 1, 0, 0, physmap);
+    rc = xc_domain_populate_physmap_exact(xch, domid, ARRAY_SIZE(physmap),
+                                          0 /* order 4k */, 0 /* flags */, physmap);
     if ( rc )
         return fail("  Fail: populate physmap: %d - %s\n",
                     errno, strerror(errno));
@@ -98,6 +100,11 @@ static void run_tests(void)
         return fail("  Fail: mempool size changed %"PRIu64" => %"PRIu64"\n",
                     old_size_bytes, size_bytes);
 
+    /* We added one 4k page.  Check we can remove it. */
+    rc = xc_domain_remove_from_physmap(xch, domid, physmap[0]);
+    if ( rc )
+        return fail("  Fail: remove from physmap: %d - %s\n",
+                    errno, strerror(errno));
 
 
     printf("Test bad set size\n");
@@ -134,6 +141,19 @@ static void run_tests(void)
     if ( size_bytes != 64 << 20 )
         return fail("  Fail: expected mempool size %u, got %"PRIu64"\n",
                     64 << 20, size_bytes);
+
+    return 0;
+}
+
+static int run_tests(void)
+{
+    int rc;
+
+    rc = test_paging_mempool_size();
+    if ( rc )
+        return rc;
+
+    return 0;
 }
 
 int main(int argc, char **argv)
@@ -143,7 +163,6 @@ int main(int argc, char **argv)
     printf("Paging mempool tests\n");
 
     xch = xc_interface_open(NULL, NULL, 0);
-
     if ( !xch )
         err(1, "xc_interface_open");
 
Signature · 1 line
2.39.5
#3 lore raw

PATCH 2/2 tests/paging-mempool: Extend to test P2M relocation

In reply to Andrew Cooper

XENMAPSPACE_gmfn_range was found to be buggy with overlapping ranges, and the fix is sufficiently far from clear that yours truely decided a test was in order.

XENMAPSPACE_gmfn_range is exposed by xendevicemodel_relocate_memory() so take the opportunity to create a better example for others to copy.

The test works as follows. Populate 2M of RAM in the guest, marking the pages so they can be identified later. Then construct four relocation test cases (overlapping vs non-overlapping, forwards vs backwards) using the marks in the guest pages to confirm that the P2M was adjusted correctly.

The use of XEN_DOMCTL_CDF_iommu forces xenmem_add_to_physmap() to limit the iteration count to a maximum of 16. Use this to force a continuation and exercise more of the logic.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Oleksii Kurochko <[email protected]>

paging-mempool isn't necessary the best named test to put this in, but it's the one with the best structure to fit it. We are going to want to shuffle the testing structure a bit when we've got a couple more examples to draw good lines between.

Without "memory: overlapping XENMAPSPACE_gmfn_range requests", this fails:

...
Test p2m memory relocation
Test forward, no overlap: GFNs [3e0...3ff] -> [400...41f]
Test forward, overlapping: GFNs [3c0...3df] -> [3c1...3e0]
Fail: check mark unable to map 003c1: -22
Fail: check mark unable to map 003c2: -22
Fail: check mark unable to map 003c3: -22
Fail: check mark unable to map 003c4: -22
Fail: check mark unable to map 003c5: -22
Fail: check mark unable to map 003c6: -22
Fail: check mark unable to map 003c7: -22
Fail: check mark unable to map 003c8: -22
Fail: check mark unable to map 003c9: -22
Fail: check mark unable to map 003ca: -22
Fail: check mark unable to map 003cb: -22
Fail: check mark unable to map 003cc: -22
Fail: check mark unable to map 003cd: -22
Fail: check mark unable to map 003ce: -22
Fail: check mark unable to map 003cf: -22
Fail: check mark unable to map 003d0: -22
Fail: check mark unable to map 003d1: -22
Fail: check mark unable to map 003d2: -22
Fail: check mark unable to map 003d3: -22
Fail: check mark unable to map 003d4: -22
Fail: check mark unable to map 003d5: -22
Fail: check mark unable to map 003d6: -22
Fail: check mark unable to map 003d7: -22
Fail: check mark unable to map 003d8: -22
Fail: check mark unable to map 003d9: -22
Fail: check mark unable to map 003da: -22
Fail: check mark unable to map 003db: -22
Fail: check mark unable to map 003dc: -22
Fail: check mark unable to map 003dd: -22
Fail: check mark unable to map 003de: -22
Fail: check mark unable to map 003df: -22
Fail: check mark: gfn 003e0 expecting fffffe20 (479), got fffffe3f (448)

With the fix in place, the test passes:

...
Test p2m memory relocation

    Test forward, no overlap:   GFNs [3e0...3ff] -> [400...41f]
    Test forward, overlapping:  GFNs [3c0...3df] -> [3c1...3e0]
    Test backwards, no overlap: GFNs [200...21f] -> [1e0...1ff]
    Test backwards, no overlap: GFNs [220...23f] -> [21f...23e]
---
 tools/tests/paging-mempool/Makefile           |   4 +
 .../paging-mempool/test-paging-mempool.c      | 197 +++++++++++++++++-
 xen/common/memory.c                           |   2 +-
 3 files changed, 201 insertions(+), 2 deletions(-)
tools/tests/paging-mempool/Makefile +4 -0
index a1e12584ce80..7444d81b8460 100644
@@ -25,9 +25,13 @@ uninstall:
 
 CFLAGS += $(CFLAGS_xeninclude)
 CFLAGS += $(CFLAGS_libxenctrl)
+CFLAGS += $(CFLAGS_libxendevicemodel)
+CFLAGS += $(CFLAGS_libxenforeignmemory)
 CFLAGS += $(APPEND_CFLAGS)
 
 LDFLAGS += $(LDLIBS_libxenctrl)
+LDFLAGS += $(LDLIBS_libxendevicemodel)
+LDFLAGS += $(LDLIBS_libxenforeignmemory)
 LDFLAGS += $(APPEND_LDFLAGS)
 
 %.o: Makefile
tools/tests/paging-mempool/test-paging-mempool.c +196 -1

230 lines not on this page · raw message

xen/common/memory.c +1 -1
index 1ad4b51c5b02..c6109dc7d664 100644
@@ -962,7 +962,7 @@ int xenmem_add_to_physmap(struct domain *d, struct xen_add_to_physmap *xatp,
     unsigned int done = 0;
     long rc = 0;
     union add_to_physmap_extra extra = {};
-    struct page_info *pages[16];
+    struct page_info *pages[16]; /* If changing this, adjust test-paging-mempool too */
 
     if ( !paging_mode_translate(d) )
     {
Signature · 1 line
2.39.5
author Acked-by on the v1 series
#4 lore raw

Re: [PATCH 0/2] tests/paging-mempool: Testing for P2M relocation

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

Andrew Cooper (2):
tests/paging-mempool: Misc cleanup tests/paging-mempool: Extend to test P2M relocation

 tools/tests/paging-mempool/Makefile           |   4 +
 .../paging-mempool/test-paging-mempool.c      | 222 +++++++++++++++++-
 xen/common/memory.c                           |   2 +-
 3 files changed, 223 insertions(+), 5 deletions(-)

ideally with line length of 80 chars not exceeded, as looks to be the case in both of the patches.

Jan

#5 lore raw

Re: [PATCH 0/2] tests/paging-mempool: Testing for P2M relocation

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

Andrew Cooper (2):
tests/paging-mempool: Misc cleanup tests/paging-mempool: Extend to test P2M relocation

  tools/tests/paging-mempool/Makefile           |   4 +
  .../paging-mempool/test-paging-mempool.c      | 222 +++++++++++++++++-
  xen/common/memory.c                           |   2 +-
  3 files changed, 223 insertions(+), 5 deletions(-)

~ Oleksii

Acked-by Tested-by on v2 1/1
#6 lore raw
In reply to Jan Beulich
Quoted 10 lines · Jan Beulich

Overlapping requests may need processing backwards, or else the intended effect wouldn't be achieved (and instead some pages would be moved more than once).

With some adjustment there this also covers XEN_DMOP_relocate_memory, where the potential issue was first noticed.

Fixes a04811a315e0 ("mm: New XENMEM space, XENMAPSPACE_gmfn_range")
Quoted 8 lines

---
Of course an alternative would be to simply reject overlapping requests. Then we should reject all overlaps though, I think. But since the code change didn't end up overly intrusive, I thought I would go the "fix it" route first.
---
v2: Adjust XEN_DMOP_relocate_memory handling for the working-backwards
case.

I can't say that I've understood everything going on here (hence the test case), but the test case says it does fix the bug I reported.

~Andrew