mm/page_alloc: fixes for free_pages_nolock() on RT/UP
Subsystems (3)
cover mm/page_alloc: fixes for free_pages_nolock() on RT/UP
First patch was originally part of a different series but Andrew asked for it to be separate:
https://lore.kernel.org/all/[email protected]/
Pre-existing bugs found by Sashiko during review of this other series:
https://lore.kernel.org/all/[email protected]/
I have not reproduced these bugs, and I suspect there is no real-world user that is affected by them.
---
Brendan Jackman (2):
mm/page_alloc: don't spin_trylock() in NMI on UP mm/page_alloc: don't spin_trylock() when disallowed in free_one_page()
mm/page_alloc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
---
base-commit: 83c85fdab41ef98198bdbd731cebc9a15a185dcc
change-id: 20260714-alloc-nolock-fixes-e30618048b99
Best regards,
Signature · 1 line
Brendan Jackman <[email protected]>
PATCH 1/2 mm/page_alloc: don't spin_trylock() in NMI on UP
As noted in can_spin_trylock(), using this is unsafe in this context. commit 620b46ed6ae17 ("mm/page_alloc: return NULL early from alloc_frozen_pages_nolock() in NMI on UP") fixed this on the alloc side but missed the free side.
Impact: If BPF programs using these features in NMI (probably tracing) are present on non-SMP builds this might crash the kernel and is probably exploitable by local attackers for privilege escalation.
--- mm/page_alloc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
mm/page_alloc.c +1 -2
@@ -2979,8 +2979,7 @@ static void __free_frozen_pages(struct page *page, unsigned int order, migratetype =MIGRATE_MOVABLE ; } -if (unlikely ((fpi_flags &FPI_TRYLOCK ) &&IS_ENABLED (CONFIG_PREEMPT_RT ) - && (in_nmi () ||in_hardirq ()))) { +if (unlikely ((fpi_flags &FPI_TRYLOCK ) && !can_spin_trylock ())) {add_page_to_zone_llist (zone, page, order);return ; }
Signature · 1 line
2.54.0
Re: [PATCH 1/2] mm/page_alloc: don't spin_trylock() in NMI on UP
Quoted 11 lines · Brendan Jackman
As noted in
can_spin_trylock(), using this is unsafe in this context. commit620b46ed6ae17("mm/page_alloc: return NULL early fromalloc_frozen_pages_nolock()in NMI on UP") fixed this on the alloc side but missed the free side.Impact: If BPF programs using these features in NMI (probably tracing) are present on non-SMP builds this might crash the kernel and is probably exploitable by local attackers for privilege escalation.
Should be "Closes:"?
(Andrew can fix up locally)
Quoted 3 lines
Fixes 8c57b687e8331 ("mm, bpf: Introduce free_pages_nolock()")Signed-off-by Brendan Jackman <[email protected]>
Quoted 19 lines
--- mm/page_alloc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)mm/page_alloc.c +1
-2@@ -2979,8 +2979,7 @@ static void __free_frozen_pages(struct page *page, unsigned int order, migratetype =MIGRATE_MOVABLE ; } -if (unlikely ((fpi_flags &FPI_TRYLOCK ) &&IS_ENABLED (CONFIG_PREEMPT_RT ) - && (in_nmi () ||in_hardirq ()))) { +if (unlikely ((fpi_flags &FPI_TRYLOCK ) && !can_spin_trylock ())) {add_page_to_zone_llist (zone, page, order);return ; }
Re: [PATCH 1/2] mm/page_alloc: don't spin_trylock() in NMI on UP
In reply to Brendan JackmanQuoted 9 lines · Brendan Jackman
As noted in
can_spin_trylock(), using this is unsafe in this context. commit620b46ed6ae17("mm/page_alloc: return NULL early fromalloc_frozen_pages_nolock()in NMI on UP") fixed this on the alloc side but missed the free side.Impact: If BPF programs using these features in NMI (probably tracing) are present on non-SMP builds this might crash the kernel and is probably exploitable by local attackers for privilege escalation.
Quoted 6 lines
Reported-by [email protected]Fixes 8c57b687e8331 ("mm, bpf: Introduce free_pages_nolock()")Signed-off-by Brendan Jackman <[email protected]>---
Looks good to me, so:
Signature · 2 lines
Cheers, Harry / Hyeonggon
PATCH 2/2 mm/page_alloc: don't spin_trylock() when disallowed in free_one_page()
In reply to Brendan Jackman__free_frozen_pages() checks can_spin_trylock() before entering into the main free_frozen_page_commit()/free_one_page() path, but before this it can get to free_one_page() via the !pcp_allowed_order() and MIGRATE_ISOLATE paths.
The !pcp_allowed_order() path depends on usage by callers so might not be possible in practice. The MIGRATE_ISOLATE path probably means kernel crashes and privilege escalation if anyone ever did memory hotplug and BPF tracing on a PREEMPT_RT or !SMP build.
--- mm/page_alloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
@@ -1559,7 +1559,7 @@ static void free_one_page(struct zone *zone, struct page *page,unsigned long flags;if (unlikely (fpi_flags &FPI_TRYLOCK )) { -if (!spin_trylock_irqsave (&zone->lock, flags)) { +if (!can_spin_trylock () || !spin_trylock_irqsave (&zone->lock, flags)) {add_page_to_zone_llist (zone, page, order);return ; }
Signature · 1 line
2.54.0
Re: [PATCH 2/2] mm/page_alloc: don't spin_trylock() when disallowed in free_one_page()
Quoted 10 lines · Brendan Jackman
__free_frozen_pages()checkscan_spin_trylock()before entering into the main free_frozen_page_commit()/free_one_page() path, but before this it can get tofree_one_page()via the !pcp_allowed_order() andMIGRATE_ISOLATEpaths.The !pcp_allowed_order() path depends on usage by callers so might not be possible in practice. The
MIGRATE_ISOLATEpath probably means kernel crashes and privilege escalation if anyone ever did memory hotplug and BPF tracing on aPREEMPT_RTor !SMP build.
Quoted 4 lines
Fixes 8c57b687e8331 ("mm, bpf: Introduce free_pages_nolock()")Reported-by [email protected]
Quoted 1 line
Signed-off-by Brendan Jackman <[email protected]>
Quoted 18 lines
--- mm/page_alloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/page_alloc.c +1
-1@@ -1559,7 +1559,7 @@ static void free_one_page(struct zone *zone, struct page *page,unsigned long flags;if (unlikely (fpi_flags &FPI_TRYLOCK )) { -if (!spin_trylock_irqsave (&zone->lock, flags)) { +if (!can_spin_trylock () || !spin_trylock_irqsave (&zone->lock, flags)) {add_page_to_zone_llist (zone, page, order);return ; }
Re: [PATCH 2/2] mm/page_alloc: don't spin_trylock() when disallowed in free_one_page()
In reply to Brendan JackmanQuoted 10 lines · Brendan Jackman
__free_frozen_pages()checkscan_spin_trylock()before entering into the main free_frozen_page_commit()/free_one_page() path, but before this it can get tofree_one_page()via the !pcp_allowed_order() andMIGRATE_ISOLATEpaths.The !pcp_allowed_order() path depends on usage by callers so might not be possible in practice. The
MIGRATE_ISOLATEpath probably means kernel crashes and privilege escalation if anyone ever did memory hotplug and BPF tracing on aPREEMPT_RTor !SMP build.
Quoted 6 lines
Fixes 8c57b687e8331 ("mm, bpf: Introduce free_pages_nolock()")Reported-by [email protected]Link https://sashiko.dev/#/patchset/20260710-spin-trylock-followup-v1-0-affb5fe5ed00%40google.com?part=2Signed-off-by Brendan Jackman <[email protected]>---
Looks good to me, so:
Signature · 2 lines
Cheers, Harry / Hyeonggon
Andrew Morton
maintainerQuoted 9 lines · On Wed, 15 Jul 2026 09:03:57 +0000 Brendan Jackman <[email protected]> wrote:
First patch was originally part of a different series but Andrew asked for it to be separate:
https://lore.kernel.org/all/[email protected]/Pre-existing bugs found by Sashiko during review of this other series:
https://lore.kernel.org/all/[email protected]/I have not reproduced these bugs, and I suspect there is no real-world user that is affected by them.
Right, thanks. I'll stage these for the next merge window - no need to rush these fixes into mainline.
And I staged them ahead of your "mm/page_alloc: rename FPI_TRYLOCK -> FPI_NOLOCK", to lessen backporting hassles.