Skip to contentSkip to conversation

pdx: cast PAGE_SIZE value ahead of shifting

PATCH In review Roger Pau Monne 3 participants 10 messages Aug 13, 2025 – Aug 14, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pdx: cast PAGE_SIZE value ahead of shifting

Coverity complains that:

277    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
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.

Cast PAGE_SIZE to paddr_t so it has the right width.

Coverity ID: 1662707

Fixes bac2000063ba ('x86-64: reduce range spanned by 1:1 mapping and frame table indexes')
---
 xen/common/pdx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/pdx.c +1 -1
index 7e070ff962e8..5f54dd18f90d 100644
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;
     pfn_top_mask        = ~(pfn_pdx_bottom_mask | pfn_hole_mask);
     ma_top_mask         = pfn_top_mask << PAGE_SHIFT;
Signature · 1 line
2.49.0
#1 lore raw
Quoted 3 lines · Roger Pau Monne

Coverity complains that:

277    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;

I'd perhaps drop this line from the commit message.  The sentence below is fine, and there's only a single hunk so it's obvious what is being referred to.

Quoted 9 lines

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.

Cast PAGE_SIZE to paddr_t so it has the right width.

Coverity ID: 1662707

Fixes bac2000063ba ('x86-64: reduce range spanned by 1:1 mapping and frame table indexes')

Formatting aside, Reviewed-by: Andrew Cooper <[email protected]>

#2 lore raw
In reply to Roger Pau Monne

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

Quoted 8 lines · Roger Pau Monne
xen/common/pdx.c +1 -1
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;

Given

#define PAGE_SIZE (_AC(1,L) << PAGE_SHIFT)

this shouldn't be needed, except maybe for Arm32. There, however, ...

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

Quoted 1 line
xen/common/pdx.c +0 -0
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;

... this and the shift immediately ahead would also be a problem afaict, which makes me conclude this isn't what Coverity has looked at. I expect the problem is with the toolstack side definition of PAGE_SIZE, which imo would rather be addressed there. (And yes, I'm pretty averse to arbitrary casts like this being introduced.)

Jan

#3 lore raw
Quoted 16 lines · Jan Beulich

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

Quoted 8 lines · Roger Pau Monne
xen/common/pdx.c +1 -1
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;

Given

#define PAGE_SIZE (_AC(1,L) << PAGE_SHIFT)

this shouldn't be needed, except maybe for Arm32. There, however, ...

Quoted 7 lines

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

Quoted 1 line
xen/common/pdx.c +0 -0
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;

... this and the shift immediately ahead would also be a problem afaict, which makes me conclude this isn't what Coverity has looked at. I expect the problem is with the toolstack side definition of PAGE_SIZE, which imo would rather be addressed there. (And yes, I'm pretty averse to arbitrary casts like this being introduced.)

As I've realized while looking at this, wouldn't ma_va_bottom_mask also better be of type paddr_t, since it's not operating on pfns, but physical addresses. I didn't adjust the type of ma_va_bottom_mask, but I would be happy to do it if you agree.

Thanks, roger.

#4 lore raw
Quoted 25 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich

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

Quoted 8 lines · Roger Pau Monne
xen/common/pdx.c +1 -1
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;

Given

#define PAGE_SIZE (_AC(1,L) << PAGE_SHIFT)

this shouldn't be needed, except maybe for Arm32. There, however, ...

Quoted 7 lines

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

Quoted 1 line
xen/common/pdx.c +0 -0
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;

... this and the shift immediately ahead would also be a problem afaict, which makes me conclude this isn't what Coverity has looked at. I expect the problem is with the toolstack side definition of PAGE_SIZE, which imo would rather be addressed there. (And yes, I'm pretty averse to arbitrary casts like this being introduced.)

Quoted 4 lines

As I've realized while looking at this, wouldn't ma_va_bottom_mask also better be of type paddr_t, since it's not operating on pfns, but physical addresses. I didn't adjust the type of ma_va_bottom_mask, but I would be happy to do it if you agree.

No, as its name says it's also used on virtual addresses (really: offsets into the direct map). It hence would better not have any bits set outside of the range that VAs can cover. With that, imo the cast (if any) also should have been to unsigned long, not paddr_t. Yet as said, im the cast would better not be there in the first place. Just that meanwhile I've learned that this was committed already.

Jan

#5 lore raw
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich

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

Quoted 8 lines · Roger Pau Monne
xen/common/pdx.c +1 -1
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;

Given

#define PAGE_SIZE (_AC(1,L) << PAGE_SHIFT)

this shouldn't be needed, except maybe for Arm32. There, however, ...

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

Quoted 1 line
xen/common/pdx.c +0 -0
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;

... this and the shift immediately ahead would also be a problem afaict, which makes me conclude this isn't what Coverity has looked at. I expect the problem is with the toolstack side definition of PAGE_SIZE, which imo would rather be addressed there. (And yes, I'm pretty averse to arbitrary casts like this being introduced.)

Quoted 8 lines
Quoted 4 lines

As I've realized while looking at this, wouldn't ma_va_bottom_mask also better be of type paddr_t, since it's not operating on pfns, but physical addresses. I didn't adjust the type of ma_va_bottom_mask, but I would be happy to do it if you agree.

No, as its name says it's also used on virtual addresses (really: offsets into the direct map). It hence would better not have any bits set outside of the range that VAs can cover.

It's confusing that it's sometimes used against a paddr_t or an unsigned long type. The logic itself already limits the shift so it's below the width of unsigned long AFAICT.

Quoted 4 lines

With that, imo the cast (if any) also
should have been to unsigned long, not paddr_t. Yet as said, im the cast would better not be there in the first place. Just that meanwhile I've learned that this was committed already.

Sorry, I should have waited for your opinion.

I think you would prefer the patch below. I can send this formally, not sure whether you would prefer a formal revert of the previous patch, plus the new fix applied, or doing the revert in the new patc (like below) is fine.

Thanks, Roger.
---

tools/tests/pdx/harness.h +1 -1
index 5bef7df650d2..a0fe33b4f1e0 100644
@@ -33,7 +33,7 @@
 #define PAGE_SHIFT    12
 /* Some libcs define PAGE_SIZE in limits.h. */
 #undef  PAGE_SIZE
-#define PAGE_SIZE     (1 << PAGE_SHIFT)
+#define PAGE_SIZE     (1UL << PAGE_SHIFT)
 #define MAX_ORDER     18 /* 2 * PAGETABLE_ORDER (9) */
 
 #define PFN_DOWN(x)   ((x) >> PAGE_SHIFT)
xen/common/pdx.c +1 -1
index 06536cc639f3..9e6b36086fbd 100644
@@ -274,7 +274,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;
     pfn_top_mask        = ~(pfn_pdx_bottom_mask | pfn_hole_mask);
     ma_top_mask         = pfn_top_mask << PAGE_SHIFT;
Acked-by
#6 lore raw
Quoted 32 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich

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

Quoted 8 lines · Roger Pau Monne
xen/common/pdx.c +1 -1
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;

Given

#define PAGE_SIZE (_AC(1,L) << PAGE_SHIFT)

this shouldn't be needed, except maybe for Arm32. There, however, ...

Quoted 7 lines

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

Quoted 1 line
xen/common/pdx.c +0 -0
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;

... this and the shift immediately ahead would also be a problem afaict, which makes me conclude this isn't what Coverity has looked at. I expect the problem is with the toolstack side definition of PAGE_SIZE, which imo would rather be addressed there. (And yes, I'm pretty averse to arbitrary casts like this being introduced.)

As I've realized while looking at this, wouldn't ma_va_bottom_mask also better be of type paddr_t, since it's not operating on pfns, but physical addresses. I didn't adjust the type of ma_va_bottom_mask, but I would be happy to do it if you agree.

Quoted 7 lines
Quoted 3 lines

No, as its name says it's also used on virtual addresses (really: offsets into the direct map). It hence would better not have any bits set outside of the range that VAs can cover.

It's confusing that it's sometimes used against a paddr_t or an unsigned long type. The logic itself already limits the shift so it's below the width of unsigned long AFAICT.

Well, the variable simply doesn't need to be wider than the narrowest type it's used with.

Quoted 8 lines
Quoted 4 lines

With that, imo the cast (if any) also
should have been to unsigned long, not paddr_t. Yet as said, im the cast would better not be there in the first place. Just that meanwhile I've learned that this was committed already.

Sorry, I should have waited for your opinion.

I think you would prefer the patch below.

Yes.

Quoted 4 lines

I can send this formally,
not sure whether you would prefer a formal revert of the previous patch, plus the new fix applied, or doing the revert in the new patc (like below) is fine.

I don't see a strong need for an outright revert.

Jan

author Acked-by Jan Beulich
#7 lore raw
Quoted 55 lines · Jan Beulich
Quoted 32 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich

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

Quoted 8 lines · Roger Pau Monne
xen/common/pdx.c +1 -1
@@ -288,7 +288,7 @@ bool __init pfn_pdx_compression_setup(paddr_t base)
 
     pfn_pdx_hole_shift  = hole_shift;
     pfn_pdx_bottom_mask = (1UL << bottom_shift) - 1;
-    ma_va_bottom_mask   = (PAGE_SIZE << bottom_shift) - 1;
+    ma_va_bottom_mask   = ((paddr_t)PAGE_SIZE << bottom_shift) - 1;

Given

#define PAGE_SIZE (_AC(1,L) << PAGE_SHIFT)

this shouldn't be needed, except maybe for Arm32. There, however, ...

Quoted 7 lines

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

Quoted 1 line
xen/common/pdx.c +0 -0
     pfn_hole_mask       = ((1UL << hole_shift) - 1) << bottom_shift;

... this and the shift immediately ahead would also be a problem afaict, which makes me conclude this isn't what Coverity has looked at. I expect the problem is with the toolstack side definition of PAGE_SIZE, which imo would rather be addressed there. (And yes, I'm pretty averse to arbitrary casts like this being introduced.)

As I've realized while looking at this, wouldn't ma_va_bottom_mask also better be of type paddr_t, since it's not operating on pfns, but physical addresses. I didn't adjust the type of ma_va_bottom_mask, but I would be happy to do it if you agree.

Quoted 7 lines
Quoted 3 lines

No, as its name says it's also used on virtual addresses (really: offsets into the direct map). It hence would better not have any bits set outside of the range that VAs can cover.

It's confusing that it's sometimes used against a paddr_t or an unsigned long type. The logic itself already limits the shift so it's below the width of unsigned long AFAICT.

Well, the variable simply doesn't need to be wider than the narrowest type it's used with.

Quoted 8 lines
Quoted 4 lines

With that, imo the cast (if any) also
should have been to unsigned long, not paddr_t. Yet as said, im the cast would better not be there in the first place. Just that meanwhile I've learned that this was committed already.

Sorry, I should have waited for your opinion.

I think you would prefer the patch below.

Yes.

Quoted 8 lines
Quoted 4 lines

I can send this formally,
not sure whether you would prefer a formal revert of the previous patch, plus the new fix applied, or doing the revert in the new patc (like below) is fine.

I don't see a strong need for an outright revert.

I've adjusted UL -> L as requested by Andrew, and added the following commit message:

tests/pdx: define PAGE_SIZE as long

Otherwise Coverity complains about possibly shifting an integer more than 31 bits.

This also reverts the previous attempt to fix this Coverity reported issue, commit 4dd323029094d93dbc8d174fe744fd7f54f0a7a4.

Coverity ID: 1662707

Fixes cb50e4033717 ('test/pdx: add PDX compression unit tests')

Let me know if you are OK with the adjustment and commit message.

Thanks, Roger.

#8 lore raw
Quoted 11 lines · Roger Pau Monné

I've adjusted UL -> L as requested by Andrew, and added the following commit message:

tests/pdx: define PAGE_SIZE as long

Otherwise Coverity complains about possibly shifting an integer more than 31 bits.

This also reverts the previous attempt to fix this Coverity reported issue, commit 4dd323029094d93dbc8d174fe744fd7f54f0a7a4.

Quoted 7 lines

Coverity ID: 1662707

Fixes cb50e4033717 ('test/pdx: add PDX compression unit tests')

Let me know if you are OK with the adjustment and commit message.

Fine with me.

Jan

#9 lore raw
In reply to Roger Pau Monné

tools/tests/pdx/harness.h line 36

Quoted 13 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
Quoted 1 line · Roger Pau Monné

On Thu, Aug 14, 2025 at 09:18:45AM +0200, Jan Beulich wrote:

tools/tests/pdx/harness.h +1 -1
index 5bef7df650d2..a0fe33b4f1e0 100644
@@ -33,7 +33,7 @@
 #define PAGE_SHIFT    12
 /* Some libcs define PAGE_SIZE in limits.h. */
 #undef  PAGE_SIZE
-#define PAGE_SIZE     (1 << PAGE_SHIFT)
+#define PAGE_SIZE     (1UL << PAGE_SHIFT)

It should be 1L to match Xen.

~Andrew