]> xenbits.xensource.com Git - people/aperard/xen-unstable.git/commitdiff
x86/boot: Simplify size calculations in move_memory()
authorAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 22 Oct 2024 17:48:30 +0000 (18:48 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 23 Oct 2024 09:27:36 +0000 (10:27 +0100)
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.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Daniel P. Smith <dpsmith@apertussolutions.com>
xen/arch/x86/setup.c

index ce28e77060cfdb9a4b6fc4710a787abe262d2b2a..8ad957f3d141c9f3d2cda53665247ad1e06747dc 100644 (file)
@@ -492,23 +492,22 @@ static void __init 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);