page_pool: Fix PP_MAGIC_MASK to avoid crashing on some 32-bit arches
Subsystems (6)
PATCH v2 1/1 page_pool: Fix PP_MAGIC_MASK to avoid crashing on some 32-bit arches
Helge reported that the introduction of PP_MAGIC_MASK let to crashes on boot on his 32-bit parisc machine. The cause of this is the mask is set too wide, so the page_pool_page_is_pp() incurs false positives which crashes the machine.
Just disabling the check in page_pool_is_pp() will lead to the page_pool code itself malfunctioning; so instead of doing this, this patch changes the define for PP_DMA_INDEX_BITS to avoid mistaking arbitrary kernel pointers for page_pool-tagged pages.
The fix relies on the kernel pointers that alias with the pp_magic field always being above PAGE_OFFSET. With this assumption, we can use the lowest bit of the value of PAGE_OFFSET as the upper bound of the PP_DMA_INDEX_MASK, which should avoid the false positives.
Because we cannot rely on PAGE_OFFSET always being a compile-time constant, nor on it always being >0, we fall back to disabling the dma_index storage when there are not enough bits available. This leaves us in the situation we were in before the patch in the Fixes tag, but only on a subset of architecture configurations. This seems to be the best we can do until the transition to page types in complete for page_pool pages.
v2:
- Make sure there's at least 8 bits available and that the PAGE_OFFSET bit calculation doesn't wrap
--- include/linux/mm.h | 22 +++++++------ net/core/page_pool.c | 76 ++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 32 deletions(-)
include/linux/mm.h +13 -9
@@ -4159,14 +4159,13 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);* since this value becomes part of PP_SIGNATURE; meaning we can just use the * space between the PP_SIGNATURE value (without POISON_POINTER_DELTA), and the * lowest bits of POISON_POINTER_DELTA. On arches where POISON_POINTER_DELTA is -* 0, we make sure that we leave the two topmost bits empty, as that guarantees -* we won't mistake a valid kernel pointer for a value we set, regardless of the -* VMSPLIT setting. +* 0, we use the lowest bit of PAGE_OFFSET as the boundary if that value is +* known at compile-time. * -* Altogether, this means that the number of bits available is constrained by -* the size of an unsigned long (at the upper end, subtracting two bits per the -* above), and the definition of PP_SIGNATURE (with or without -* POISON_POINTER_DELTA). +* If the value of PAGE_OFFSET is not known at compile time, or if it is too +* small to leave at least 8 bits available above PP_SIGNATURE, we define the +* number of bits to be 0, which turns off the DMA index tracking altogether +* (see page_pool_register_dma_index()). */ #define PP_DMA_INDEX_SHIFT (1 + __fls(PP_SIGNATURE - POISON_POINTER_DELTA))#if POISON_POINTER_DELTA >0
@@ -4175,8 +4174,13 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);*/ #define PP_DMA_INDEX_BITS MIN(32, __ffs(POISON_POINTER_DELTA) - PP_DMA_INDEX_SHIFT)#else -/* Always leave out the topmost two; see above. */ -#define PP_DMA_INDEX_BITS MIN(32, BITS_PER_LONG - PP_DMA_INDEX_SHIFT - 2) +/* Use the lowest bit of PAGE_OFFSET if there's at least 8 bits available; see above */ +#define PP_DMA_INDEX_MIN_OFFSET (1 << (PP_DMA_INDEX_SHIFT + 8)) +#define PP_DMA_INDEX_BITS ((__builtin_constant_p(PAGE_OFFSET) && \ + PAGE_OFFSET >= PP_DMA_INDEX_MIN_OFFSET && \ + !(PAGE_OFFSET & (PP_DMA_INDEX_MIN_OFFSET - 1))) ? \ + MIN(32, __ffs(PAGE_OFFSET) - PP_DMA_INDEX_SHIFT) : 0) +#endif #define PP_DMA_INDEX_MASK GENMASK(PP_DMA_INDEX_BITS + PP_DMA_INDEX_SHIFT - 1, \
net/core/page_pool.c +53 -23
112 lines not on this page · raw message
Signature · 1 line
2.51.0
Quoted 67 lines · Toke Høiland-Jørgensen
Helge reported that the introduction of
PP_MAGIC_MASKlet to crashes on boot on his 32-bit parisc machine. The cause of this is the mask is set too wide, so thepage_pool_page_is_pp()incurs false positives which crashes the machine.Just disabling the check in
page_pool_is_pp()will lead to thepage_poolcode itself malfunctioning; so instead of doing this, this patch changes the define forPP_DMA_INDEX_BITSto avoid mistaking arbitrary kernel pointers for page_pool-tagged pages.The fix relies on the kernel pointers that alias with the
pp_magicfield always being abovePAGE_OFFSET. With this assumption, we can use the lowest bit of the value ofPAGE_OFFSETas the upper bound of thePP_DMA_INDEX_MASK, which should avoid the false positives.Because we cannot rely on
PAGE_OFFSETalways being a compile-time constant, nor on it always being >0, we fall back to disabling thedma_indexstorage when there are not enough bits available. This leaves us in the situation we were in before the patch in the Fixes tag, but only on a subset of architecture configurations. This seems to be the best we can do until the transition to page types in complete forpage_poolpages.v2:
- Make sure there's at least 8 bits available and that thePAGE_OFFSETbit calculation doesn't wrapFixes ee62ce7a1d90 ("page_pool: Track DMA-mapped pages and unmap them when destroying the pool")Cc [email protected] # 6.15+Tested-by Helge Deller <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- include/linux/mm.h | 22 +++++++------ net/core/page_pool.c | 76 ++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 32 deletions(-)include/linux/mm.h +6
-7@@ -4159,14 +4159,13 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);* since this value becomes part of PP_SIGNATURE; meaning we can just use the * space between the PP_SIGNATURE value (without POISON_POINTER_DELTA), and the * lowest bits of POISON_POINTER_DELTA. On arches where POISON_POINTER_DELTA is -* 0, we make sure that we leave the two topmost bits empty, as that guarantees -* we won't mistake a valid kernel pointer for a value we set, regardless of the -* VMSPLIT setting. +* 0, we use the lowest bit of PAGE_OFFSET as the boundary if that value is +* known at compile-time. * -* Altogether, this means that the number of bits available is constrained by -* the size of an unsigned long (at the upper end, subtracting two bits per the -* above), and the definition of PP_SIGNATURE (with or without -* POISON_POINTER_DELTA). +* If the value of PAGE_OFFSET is not known at compile time, or if it is too +* small to leave at least 8 bits available above PP_SIGNATURE, we define the +* number of bits to be 0, which turns off the DMA index tracking altogether +* (see page_pool_register_dma_index()). */ #define PP_DMA_INDEX_SHIFT (1 + __fls(PP_SIGNATURE - POISON_POINTER_DELTA))#if POISON_POINTER_DELTA >0 @@ -4175,8 +4174,13 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);*/ #define PP_DMA_INDEX_BITS MIN(32, __ffs(POISON_POINTER_DELTA) - PP_DMA_INDEX_SHIFT)#else
include/linux/mm.h int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); · line 4184
Quoted 10 lines
include/linux/mm.h +7
-2-/* Always leave out the topmost two; see above. */ -#define PP_DMA_INDEX_BITS MIN(32, BITS_PER_LONG - PP_DMA_INDEX_SHIFT - 2) +/* Use the lowest bit of PAGE_OFFSET if there's at least 8 bits available; see above */ +#define PP_DMA_INDEX_MIN_OFFSET (1 << (PP_DMA_INDEX_SHIFT + 8)) +#define PP_DMA_INDEX_BITS ((__builtin_constant_p(PAGE_OFFSET) && \ + PAGE_OFFSET >= PP_DMA_INDEX_MIN_OFFSET && \ + !(PAGE_OFFSET & (PP_DMA_INDEX_MIN_OFFSET - 1))) ? \ + MIN(32, __ffs(PAGE_OFFSET) - PP_DMA_INDEX_SHIFT) : 0) +#endif
It took some staring at, but I think I understand this code and it is correct. This is the critical check, it's making sure that the bits used by PAGE_OFFSET are not shared with the bits used for the dma-index:
include/linux/mm.h int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); · line 4181
Quoted 1 line
include/linux/mm.h +1
-0+ !(PAGE_OFFSET & (PP_DMA_INDEX_MIN_OFFSET - 1))) ? \
The following check confused me for a while, but I think I figured it out. It's checking that the bits used for PAGE_OFFSET are 'higher' than the bits used for PP_DMA_INDEX:
include/linux/mm.h int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); · line 4180
Quoted 1 line
include/linux/mm.h +1
-0+ PAGE_OFFSET >= PP_DMA_INDEX_MIN_OFFSET && \
And finally this calculation should indeed be the bits we can use (the empty space between the lsb set by PAGE_OFFSET and the msb set by the pp magic:
Quoted 1 line
+
MIN(32, __ffs(PAGE_OFFSET) - PP_DMA_INDEX_SHIFT): 0)
AFAIU we should not need the MIN anymore, since that subtraction is guaranteed to be positive, but that's a nit.
Signature · 2 lines
Thanks, Mina
Quoted 90 lines · Mina Almasry
Quoted 67 lines · Toke Høiland-Jørgensen
Helge reported that the introduction of
PP_MAGIC_MASKlet to crashes on boot on his 32-bit parisc machine. The cause of this is the mask is set too wide, so thepage_pool_page_is_pp()incurs false positives which crashes the machine.Just disabling the check in
page_pool_is_pp()will lead to thepage_poolcode itself malfunctioning; so instead of doing this, this patch changes the define forPP_DMA_INDEX_BITSto avoid mistaking arbitrary kernel pointers for page_pool-tagged pages.The fix relies on the kernel pointers that alias with the
pp_magicfield always being abovePAGE_OFFSET. With this assumption, we can use the lowest bit of the value ofPAGE_OFFSETas the upper bound of thePP_DMA_INDEX_MASK, which should avoid the false positives.Because we cannot rely on
PAGE_OFFSETalways being a compile-time constant, nor on it always being >0, we fall back to disabling thedma_indexstorage when there are not enough bits available. This leaves us in the situation we were in before the patch in the Fixes tag, but only on a subset of architecture configurations. This seems to be the best we can do until the transition to page types in complete forpage_poolpages.v2:
- Make sure there's at least 8 bits available and that thePAGE_OFFSETbit calculation doesn't wrapFixes ee62ce7a1d90 ("page_pool: Track DMA-mapped pages and unmap them when destroying the pool")Cc [email protected] # 6.15+Tested-by Helge Deller <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- include/linux/mm.h | 22 +++++++------ net/core/page_pool.c | 76 ++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 32 deletions(-)include/linux/mm.h +6
-7@@ -4159,14 +4159,13 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);* since this value becomes part of PP_SIGNATURE; meaning we can just use the * space between the PP_SIGNATURE value (without POISON_POINTER_DELTA), and the * lowest bits of POISON_POINTER_DELTA. On arches where POISON_POINTER_DELTA is -* 0, we make sure that we leave the two topmost bits empty, as that guarantees -* we won't mistake a valid kernel pointer for a value we set, regardless of the -* VMSPLIT setting. +* 0, we use the lowest bit of PAGE_OFFSET as the boundary if that value is +* known at compile-time. * -* Altogether, this means that the number of bits available is constrained by -* the size of an unsigned long (at the upper end, subtracting two bits per the -* above), and the definition of PP_SIGNATURE (with or without -* POISON_POINTER_DELTA). +* If the value of PAGE_OFFSET is not known at compile time, or if it is too +* small to leave at least 8 bits available above PP_SIGNATURE, we define the +* number of bits to be 0, which turns off the DMA index tracking altogether +* (see page_pool_register_dma_index()). */ #define PP_DMA_INDEX_SHIFT (1 + __fls(PP_SIGNATURE - POISON_POINTER_DELTA))#if POISON_POINTER_DELTA >0 @@ -4175,8 +4174,13 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);*/ #define PP_DMA_INDEX_BITS MIN(32, __ffs(POISON_POINTER_DELTA) - PP_DMA_INDEX_SHIFT)#else include/linux/mm.h int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); · line 4184
Quoted 10 lines
include/linux/mm.h +7
-2-/* Always leave out the topmost two; see above. */ -#define PP_DMA_INDEX_BITS MIN(32, BITS_PER_LONG - PP_DMA_INDEX_SHIFT - 2) +/* Use the lowest bit of PAGE_OFFSET if there's at least 8 bits available; see above */ +#define PP_DMA_INDEX_MIN_OFFSET (1 << (PP_DMA_INDEX_SHIFT + 8)) +#define PP_DMA_INDEX_BITS ((__builtin_constant_p(PAGE_OFFSET) && \ + PAGE_OFFSET >= PP_DMA_INDEX_MIN_OFFSET && \ + !(PAGE_OFFSET & (PP_DMA_INDEX_MIN_OFFSET - 1))) ? \ + MIN(32, __ffs(PAGE_OFFSET) - PP_DMA_INDEX_SHIFT) : 0) +#endif It took some staring at, but I think I understand this code and it is correct. This is the critical check, it's making sure that the bits used by
PAGE_OFFSETare not shared with the bits used for the dma-index:include/linux/mm.h int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); · line 4181
Quoted 1 line
include/linux/mm.h +1
-0+ !(PAGE_OFFSET & (PP_DMA_INDEX_MIN_OFFSET - 1))) ? \The following check confused me for a while, but I think I figured it out. It's checking that the bits used for
PAGE_OFFSETare 'higher' than the bits used forPP_DMA_INDEX:
Quoted 7 lines
include/linux/mm.h int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); · line 4180
Quoted 1 line
include/linux/mm.h +1
-0+ PAGE_OFFSET >= PP_DMA_INDEX_MIN_OFFSET && \And finally this calculation should indeed be the bits we can use (the empty space between the lsb set by
PAGE_OFFSETand the msb set by the pp magic:Quoted 1 line
+
MIN(32, __ffs(PAGE_OFFSET) - PP_DMA_INDEX_SHIFT): 0)
Yup, exactly! Thanks for walking through it and confirming that the logic is sound :)
Quoted 2 lines
AFAIU we should not need the MIN anymore, since that subtraction is guaranteed to be positive, but that's a nit.
The MIN was originally there to limit how many bits we use for 64-bit systems that don't set POISON_POINTER_DELTA, since xarray uses a u32 for the size of the limits. Not sure if such a combination exists in the real world, but I figure that having it there doesn't hurt in any case.
Quoted 1 line
Reviewed-by Mina Almasry <[email protected]>
Thanks!
-Toke
Quoted 32 lines · Toke Høiland-Jørgensen
Helge reported that the introduction of
PP_MAGIC_MASKlet to crashes on boot on his 32-bit parisc machine. The cause of this is the mask is set too wide, so thepage_pool_page_is_pp()incurs false positives which crashes the machine.Just disabling the check in
page_pool_is_pp()will lead to thepage_poolcode itself malfunctioning; so instead of doing this, this patch changes the define forPP_DMA_INDEX_BITSto avoid mistaking arbitrary kernel pointers for page_pool-tagged pages.The fix relies on the kernel pointers that alias with the
pp_magicfield always being abovePAGE_OFFSET. With this assumption, we can use the lowest bit of the value ofPAGE_OFFSETas the upper bound of thePP_DMA_INDEX_MASK, which should avoid the false positives.Because we cannot rely on
PAGE_OFFSETalways being a compile-time constant, nor on it always being >0, we fall back to disabling thedma_indexstorage when there are not enough bits available. This leaves us in the situation we were in before the patch in the Fixes tag, but only on a subset of architecture configurations. This seems to be the best we can do until the transition to page types in complete forpage_poolpages.v2:
- Make sure there's at least 8 bits available and that thePAGE_OFFSETbit calculation doesn't wrapFixes ee62ce7a1d90 ("page_pool: Track DMA-mapped pages and unmap them when destroying the pool")Cc [email protected] # 6.15+Tested-by Helge Deller <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>
Quoted 4 lines
--- include/linux/mm.h | 22 +++++++------ net/core/page_pool.c | 76 ++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 32 deletions(-)
I tested this v2 patch (the former tested-by was for v1), and v2 works too:
Thanks!
Helge
Quoted 38 lines · Helge Deller
Quoted 32 lines · Toke Høiland-Jørgensen
Helge reported that the introduction of
PP_MAGIC_MASKlet to crashes on boot on his 32-bit parisc machine. The cause of this is the mask is set too wide, so thepage_pool_page_is_pp()incurs false positives which crashes the machine.Just disabling the check in
page_pool_is_pp()will lead to thepage_poolcode itself malfunctioning; so instead of doing this, this patch changes the define forPP_DMA_INDEX_BITSto avoid mistaking arbitrary kernel pointers for page_pool-tagged pages.The fix relies on the kernel pointers that alias with the
pp_magicfield always being abovePAGE_OFFSET. With this assumption, we can use the lowest bit of the value ofPAGE_OFFSETas the upper bound of thePP_DMA_INDEX_MASK, which should avoid the false positives.Because we cannot rely on
PAGE_OFFSETalways being a compile-time constant, nor on it always being >0, we fall back to disabling thedma_indexstorage when there are not enough bits available. This leaves us in the situation we were in before the patch in the Fixes tag, but only on a subset of architecture configurations. This seems to be the best we can do until the transition to page types in complete forpage_poolpages.v2:
- Make sure there's at least 8 bits available and that thePAGE_OFFSETbit calculation doesn't wrapFixes ee62ce7a1d90 ("page_pool: Track DMA-mapped pages and unmap them when destroying the pool")Cc [email protected] # 6.15+Tested-by Helge Deller <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>Quoted 4 lines
--- include/linux/mm.h | 22 +++++++------ net/core/page_pool.c | 76 ++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 32 deletions(-)
Quoted 4 lines
I tested this v2 patch (the former tested-by was for v1), and v2 works too:
Tested-by Helge Deller <[email protected]>
Great, thank you for re-testing! :)
-Toke
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Tue, 30 Sep 2025 13:43:29 +0200 you wrote:
Helge reported that the introduction of
PP_MAGIC_MASKlet to crashes on boot on his 32-bit parisc machine. The cause of this is the mask is set too wide, so thepage_pool_page_is_pp()incurs false positives which crashes the machine.Just disabling the check in
page_pool_is_pp()will lead to thepage_poolcode itself malfunctioning; so instead of doing this, this patch changes the define forPP_DMA_INDEX_BITSto avoid mistaking arbitrary kernel pointers for page_pool-tagged pages.[...]
Here is the summary with links:
- [net,v2] page_pool: Fix PP_MAGIC_MASK to avoid crashing on some 32-bit arches
https://git.kernel.org/netdev/net/c/95920c2ed02b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html