Skip to contentSkip to conversation

xen/pdx: fix off-by-one index in offset mask calculation

PATCH In review Roger Pau Monne 3 participants 6 messages Jun 9

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/pdx: fix off-by-one index in offset mask calculation

Adjust the mask calculation in case the last range is merged with the previous one, as then the mask must be calculated from the previous range, which the current one has been merged into.

Instead of fixing the off-by-one in place, move the calculation of the bit change mask to the next loop, after the ranges have been merged. This simplifies the logic by consolidating mask calculation in a single place, possibly making it less error prone in the future.

Also add a test case that triggers the bug being fixed by this commit.

Fixes c5c45bcbd6a1 ("pdx: introduce a new compression algorithm based on region offsets")
---
 tools/tests/pdx/test-pdx.c | 14 ++++++++++++++
 xen/common/pdx.c           | 13 ++++++-------
 2 files changed, 20 insertions(+), 7 deletions(-)
tools/tests/pdx/test-pdx.c +14 -0
index d783186577ef..ba57f1793011 100644
@@ -191,6 +191,20 @@ int main(int argc, char **argv)
             },
             .compress = false,
         },
+        /*
+         * 2s Dell R740, merging of ranges causes mask differences in PDX
+         * offset mode.  Useful for checking mask calculations.
+         */
+        {
+            .ranges = {
+                { .start = 0x0000000UL, .end = 0x0080000UL },
+                { .start = 0x0100000UL, .end = 0x3070000UL },
+                { .start = 0x3070000UL, .end = 0x3870000UL },
+                { .start = 0x3870000UL, .end = 0x6870000UL },
+                { .start = 0x6870000UL, .end = 0x7070000UL },
+            },
+            .compress = false,
+        },
     };
     int ret_code = EXIT_SUCCESS;
 
xen/common/pdx.c +6 -7
index 7e070ff962e8..a84c7d19ade4 100644
@@ -391,10 +391,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
         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;
@@ -402,19 +399,21 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
         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
-     * to calculate the maximum PFN shift to use as the lookup table index.
+     * Populate two masks: one with the non-equal bits of the different ranges,
+     * another with the bits that change inside regions. Do this to calculate
+     * the maximum PFN shift to use as the lookup table index.
      */
     for ( i = 0; i < nr_ranges; i++ )
+    {
+        mask |= pdx_region_mask(ranges[i].base_pfn, ranges[i].pages);
         for ( unsigned int j = i + 1; j < nr_ranges; j++ )
             idx_mask |= ranges[i].base_pfn ^ ranges[j].base_pfn;
+    }
     /* Mask out the bits that change inside of ranges. */
     idx_mask &= ~mask;
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 63 lines · Roger Pau Monne

Adjust the mask calculation in case the last range is merged with the previous one, as then the mask must be calculated from the previous range, which the current one has been merged into.

Instead of fixing the off-by-one in place, move the calculation of the bit change mask to the next loop, after the ranges have been merged. This simplifies the logic by consolidating mask calculation in a single place, possibly making it less error prone in the future.

Also add a test case that triggers the bug being fixed by this commit.

Fixes c5c45bcbd6a1 ("pdx: introduce a new compression algorithm based on region offsets")
---
 tools/tests/pdx/test-pdx.c | 14 ++++++++++++++
 xen/common/pdx.c           | 13 ++++++-------
 2 files changed, 20 insertions(+), 7 deletions(-)
tools/tests/pdx/test-pdx.c +14 -0
index d783186577ef..ba57f1793011 100644
@@ -191,6 +191,20 @@ int main(int argc, char **argv)
             },
             .compress = false,
         },
+        /*
+         * 2s Dell R740, merging of ranges causes mask differences in PDX
+         * offset mode.  Useful for checking mask calculations.
+         */
+        {
+            .ranges = {
+                { .start = 0x0000000UL, .end = 0x0080000UL },
+                { .start = 0x0100000UL, .end = 0x3070000UL },
+                { .start = 0x3070000UL, .end = 0x3870000UL },
+                { .start = 0x3870000UL, .end = 0x6870000UL },
+                { .start = 0x6870000UL, .end = 0x7070000UL },
+            },
+            .compress = false,
+        },
     };
     int ret_code = EXIT_SUCCESS;
 
xen/common/pdx.c +0 -4
index 7e070ff962e8..a84c7d19ade4 100644
@@ -391,10 +391,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
         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;
@@ -402,19 +399,21 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
         if ( i + 1 < nr_ranges )
             memmove(&ranges[i], &ranges[i + 1],
                     (nr_ranges - (i + 1)) * sizeof(ranges[0]));
-        else /* last range */

xen/common/pdx.c bool __init pfn_pdx_compression_setup(paddr_t base) · line 408

Quoted 10 lines
xen/common/pdx.c +2 -3
-            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
-     * to calculate the maximum PFN shift to use as the lookup table index.
+     * Populate two masks: one with the non-equal bits of the different ranges,
+     * another with the bits that change inside regions. Do this to calculate

Forgot to refresh the patch before sending, this line should be:

"another with the bits that change inside ranges. Do this to calculate"

IOW: s/regions/ranges/ so that we don't mix "ranges" and "regions" in the same paragraph which is confusing.

Thanks, Roger.

Acked-by on v1 1/1
#2 lore raw
In reply to Roger Pau Monne
Quoted 9 lines · Roger Pau Monne

Adjust the mask calculation in case the last range is merged with the previous one, as then the mask must be calculated from the previous range, which the current one has been merged into.

Instead of fixing the off-by-one in place, move the calculation of the bit change mask to the next loop, after the ranges have been merged. This simplifies the logic by consolidating mask calculation in a single place, possibly making it less error prone in the future.

Quoted 4 lines

Also add a test case that triggers the bug being fixed by this commit.

Fixes c5c45bcbd6a1 ("pdx: introduce a new compression algorithm based on region offsets")

tools/tests/pdx/test-pdx.c int main(int argc, char **argv) · line 196

Quoted 11 lines
tools/tests/pdx/test-pdx.c +3 -0
index d783186577ef..ba57f1793011 100644
@@ -191,6 +191,20 @@ int main(int argc, char **argv)
             },
             .compress = false,
         },
+        /*
+         * 2s Dell R740, merging of ranges causes mask differences in PDX
+         * offset mode.  Useful for checking mask calculations.

What's the 2s here?  If it is what I think it is, I'd suggest "Dell R740, dual socket,"

~Andrew

#3 lore raw
Quoted 17 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne

Adjust the mask calculation in case the last range is merged with the previous one, as then the mask must be calculated from the previous range, which the current one has been merged into.

Instead of fixing the off-by-one in place, move the calculation of the bit change mask to the next loop, after the ranges have been merged. This simplifies the logic by consolidating mask calculation in a single place, possibly making it less error prone in the future.

Quoted 4 lines

Also add a test case that triggers the bug being fixed by this commit.

Fixes c5c45bcbd6a1 ("pdx: introduce a new compression algorithm based on region offsets")
Quoted 14 lines

tools/tests/pdx/test-pdx.c int main(int argc, char **argv) · line 196

Quoted 11 lines
tools/tests/pdx/test-pdx.c +3 -0
index d783186577ef..ba57f1793011 100644
@@ -191,6 +191,20 @@ int main(int argc, char **argv)
             },
             .compress = false,
         },
+        /*
+         * 2s Dell R740, merging of ranges causes mask differences in PDX
+         * offset mode.  Useful for checking mask calculations.

What's the 2s here?  If it is what I think it is, I'd suggest "Dell R740, dual socket,"

Yes, it's what you think it is. I've used "2s" in existing comments, but I'm perfectly fine with spelling it out.

Thanks, Roger.

#4 lore raw
Quoted 30 lines · Roger Pau Monné
Quoted 16 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne

Adjust the mask calculation in case the last range is merged with the previous one, as then the mask must be calculated from the previous range, which the current one has been merged into.

Instead of fixing the off-by-one in place, move the calculation of the bit change mask to the next loop, after the ranges have been merged. This simplifies the logic by consolidating mask calculation in a single place, possibly making it less error prone in the future.

Quoted 4 lines

Also add a test case that triggers the bug being fixed by this commit.

Fixes c5c45bcbd6a1 ("pdx: introduce a new compression algorithm based on region offsets")
Quoted 13 lines

tools/tests/pdx/test-pdx.c int main(int argc, char **argv) · line 196

Quoted 11 lines
tools/tests/pdx/test-pdx.c +3 -0
index d783186577ef..ba57f1793011 100644
@@ -191,6 +191,20 @@ int main(int argc, char **argv)
             },
             .compress = false,
         },
+        /*
+         * 2s Dell R740, merging of ranges causes mask differences in PDX
+         * offset mode.  Useful for checking mask calculations.

What's the 2s here?  If it is what I think it is, I'd suggest "Dell R740, dual socket,"

Quoted 1 line

Yes, it's what you think it is. I've used "2s" in existing comments,

Oh, fine.

~Andrew

#5 lore raw
In reply to Roger Pau Monne
Quoted 9 lines · Roger Pau Monne

Adjust the mask calculation in case the last range is merged with the previous one, as then the mask must be calculated from the previous range, which the current one has been merged into.

Instead of fixing the off-by-one in place, move the calculation of the bit change mask to the next loop, after the ranges have been merged. This simplifies the logic by consolidating mask calculation in a single place, possibly making it less error prone in the future.

Quoted 4 lines

Also add a test case that triggers the bug being fixed by this commit.

Fixes c5c45bcbd6a1 ("pdx: introduce a new compression algorithm based on region offsets")

Thanks.

~ Oleksii