Skip to contentSkip to conversation

x86/boot: Improvements to move_memory()

PATCH In review Andrew Cooper 2 participants 7 messages Oct 22, 2024 – Oct 23, 2024

lore
Share & tools
lore
#0 lore raw

cover x86/boot: Improvements to move_memory()

Strip module_t out of move_memory() and simplify the logic.

This is to allow yet-more cleanup with the hyperlaunch series. See patch 1 for most of the discussion.

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1507885241

Andrew Cooper (3):
x86/boot: Convert move_memory() to use bootstrap_map_addr() x86/boot: Simplify address calculations in move_memory() x86/boot: Simplify size calculations in move_memory()

 xen/arch/x86/setup.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/3 x86/boot: Convert move_memory() to use bootstrap_map_addr()

move_memory() is very complicated, and buggy. In order to fix the latter, we have to address the former.

Given prior cleanup, bootstrap_map() is now implemented in terms of bootstrap_map_addr(), meaining that it is counterproductive to plumb the mapping through module_t.

Delete mod, and introduce two same-sized/named fields. At this point in boot, neither fields have their named purpose, so indicate the purpose in comments.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

This is broken out both to ease review (I recommend git diff --color-words), and for bisection purposes given how many times I've failed to edit this logic correctly.

This does not conflict with anything in Hyperlaunch v7, but it does remove the only callers of bootstrap_map() remaining by the end of the series, allowing for even more cleanup.

In terms of bugs. The ones I've spotted so far are:

* The blksz calculation depends on the previous thing having been unmapped first.

* The calculation halving blksz based on BOOTSTRAP_MAP_BASE is bogus. It might plausibly be a remnent of 32bit Xen with the absence of highmem.

* The caller of move_memory() is strictly moving modules forward in memory (dst > src) with a possibility of partial overlap. The while loop maps dst and src, blksz chunks at a time, ascending, with s < d. This results in memmove() doing an unconditional backwards copy.

I don't see how this logic could ever have worked for an overlapping move.

* The caller of move_memory() already has a good mapping for dst, so we don't even need to split BOOTSTRAP_MAP_LIMIT in half to map both parts.

---
 xen/arch/x86/setup.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
xen/arch/x86/setup.c +19 -16
index 059b597331a7..46a0b3093c2c 100644
@@ -492,26 +492,29 @@ static void __init noinline move_memory(
 
     while ( size )
     {
-        module_t mod;
+        unsigned int start /* frame */;
+        unsigned int end   /* mapsz */;
         unsigned int soffs = src & mask;
         unsigned int doffs = dst & mask;
         unsigned int sz;
         void *d, *s;
 
-        mod.mod_start = (src - soffs) >> PAGE_SHIFT;
-        mod.mod_end = soffs + size;
-        if ( mod.mod_end > blksz )
-            mod.mod_end = blksz;
-        sz = mod.mod_end - soffs;
-        s = bootstrap_map(&mod);
-
-        mod.mod_start = (dst - doffs) >> PAGE_SHIFT;
-        mod.mod_end = doffs + size;
-        if ( mod.mod_end > blksz )
-            mod.mod_end = blksz;
-        if ( sz > mod.mod_end - doffs )
-            sz = mod.mod_end - doffs;
-        d = bootstrap_map(&mod);
+        start = (src - soffs) >> PAGE_SHIFT;
+        end = soffs + size;
+        if ( end > blksz )
+            end = blksz;
+        sz = end - soffs;
+        s = bootstrap_map_addr(pfn_to_paddr(start),
+                               pfn_to_paddr(start) + end);
+
+        start = (dst - doffs) >> PAGE_SHIFT;
+        end = doffs + size;
+        if ( end > blksz )
+            end = blksz;
+        if ( sz > end - doffs )
+            sz = end - doffs;
+        d = bootstrap_map_addr(pfn_to_paddr(start),
+                               pfn_to_paddr(start) + end);
 
         memmove(d + doffs, s + soffs, sz);
 
@@ -519,7 +522,7 @@ static void __init noinline move_memory(
         src += sz;
         size -= sz;
 
-        bootstrap_map(NULL);
+        bootstrap_map_addr(0, 0);
     }
 }
 
Signature · 1 line
2.39.5
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] x86/boot: Convert move_memory() to use bootstrap_map_addr()

Quoted 10 lines · Andrew Cooper

move_memory() is very complicated, and buggy. In order to fix the latter, we have to address the former.

Given prior cleanup, bootstrap_map() is now implemented in terms of bootstrap_map_addr(), meaining that it is counterproductive to plumb the mapping through module_t.

Delete mod, and introduce two same-sized/named fields. At this point in boot, neither fields have their named purpose, so indicate the purpose in comments.

Quoted 8 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

#3 lore raw

PATCH 2/3 x86/boot: Simplify address calculations in move_memory()

In reply to Andrew Cooper

Given that soffs is the offset into the 2M superpage,

start = (src - soffs) >> PAGE_SIFT

is a complicated expression for the frame address of the containing superpage. Except, start is converted straight back to a byte address to use, so the shifting is unnecessary too.

The only done with the mapped pointer is to have soffs added back on for the memmove() call. bootstrap_map_addr() passes through the offset, so we can pass src directly in and simplify the memmove() call too. For the end mapping address, this simplifies to just src + sz too.

The same reasoning holds for dst and doffs.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

---
 xen/arch/x86/setup.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
xen/arch/x86/setup.c +3 -8
index 46a0b3093c2c..152cd696d6c2 100644
@@ -492,31 +492,26 @@ static void __init noinline move_memory(
 
     while ( size )
     {
-        unsigned int start /* frame */;
         unsigned int end   /* mapsz */;
         unsigned int soffs = src & mask;
         unsigned int doffs = dst & mask;
         unsigned int sz;
         void *d, *s;
 
-        start = (src - soffs) >> PAGE_SHIFT;
         end = soffs + size;
         if ( end > blksz )
             end = blksz;
         sz = end - soffs;
-        s = bootstrap_map_addr(pfn_to_paddr(start),
-                               pfn_to_paddr(start) + end);
+        s = bootstrap_map_addr(src, src + sz);
 
-        start = (dst - doffs) >> PAGE_SHIFT;
         end = doffs + size;
         if ( end > blksz )
             end = blksz;
         if ( sz > end - doffs )
             sz = end - doffs;
-        d = bootstrap_map_addr(pfn_to_paddr(start),
-                               pfn_to_paddr(start) + end);
+        d = bootstrap_map_addr(dst, dst + sz);
 
-        memmove(d + doffs, s + soffs, sz);
+        memmove(d, s, sz);
 
         dst += sz;
         src += sz;
Signature · 1 line
2.39.5
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] x86/boot: Simplify address calculations in move_memory()

Quoted 9 lines · Andrew Cooper

Given that soffs is the offset into the 2M superpage,

start = (src - soffs) >> PAGE_SIFT

is a complicated expression for the frame address of the containing superpage. Except, start is converted straight back to a byte address to use, so the shifting is unnecessary too.

The only done with the mapped pointer is to have soffs added back on for the

nit: think you dropped a word here, "The only done".

Quoted 6 lines

memmove() call. bootstrap_map_addr() passes through the offset, so we can pass src directly in and simplify the memmove() call too. For the end mapping address, this simplifies to just src + sz too.

The same reasoning holds for dst and doffs.

Quoted 7 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

After nit,

#5 lore raw

PATCH 3/3 x86/boot: Simplify size calculations in move_memory()

In reply to Andrew Cooper

While both src and dst are similar, src is mapped only accounting for src's size, while dst is mapped based on the minimum of both. This means that in some cases, an overly large mapping is requested for src.

Rework the sz calcuation to be symmetric, and leave an explanation of how logic works.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

And as if by magic, all transient fields from converting module_t are gone, with bloat-o-meter reporting:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-74 (-74)

  Function                                     old     new   delta
  move_memory                                  265     191     -74

which is all removal of logic inside the while loop.

---
 xen/arch/x86/setup.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
xen/arch/x86/setup.c +10 -11
index 152cd696d6c2..bb525980cdd8 100644
@@ -492,23 +492,22 @@ static void __init noinline move_memory(
 
     while ( size )
     {
-        unsigned int end   /* mapsz */;
         unsigned int soffs = src & mask;
         unsigned int doffs = dst & mask;
         unsigned int sz;
         void *d, *s;
 
-        end = soffs + size;
-        if ( end > blksz )
-            end = blksz;
-        sz = end - soffs;
-        s = bootstrap_map_addr(src, src + sz);
+        /*
+         * We're copying between two arbitrary buffers, as they fall within
+         * 2M-aligned regions with a maximum bound of blksz.
+         *
+         * For [ds]offs + size <= blksz, sz = size.
+         * For [ds]offs + size >  blksz, sz = blksz - [ds]offs.
+         */
+        sz = max(soffs, doffs);
+        sz = min(sz + size, blksz) - sz;
 
-        end = doffs + size;
-        if ( end > blksz )
-            end = blksz;
-        if ( sz > end - doffs )
-            sz = end - doffs;
+        s = bootstrap_map_addr(src, src + sz);
         d = bootstrap_map_addr(dst, dst + sz);
 
         memmove(d, s, sz);
Signature · 1 line
2.39.5
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] x86/boot: Simplify size calculations in move_memory()

Quoted 7 lines · Andrew Cooper

While both src and dst are similar, src is mapped only accounting for src's size, while dst is mapped based on the minimum of both. This means that in some cases, an overly large mapping is requested for src.

Rework the sz calcuation to be symmetric, and leave an explanation of how logic works.

Quoted 7 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>