Skip to contentSkip to conversation

mm/page_alloc: fixes for free_pages_nolock() on RT/UP

PATCH In review Brendan Jackman 4 participants 8 messages Jul 15 – Jul 16

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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]>
#1 lore raw

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
index af63558391345..5f9873dfccc5a 100644
@@ -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
maintainer Reviewed-by on v1 1/2
#2 lore raw

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. 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.

Should be "Closes:"?
(Andrew can fix up locally)

Quoted 3 lines
Fixes 8c57b687e8331 ("mm, bpf: Introduce free_pages_nolock()")
Quoted 19 lines
---
 mm/page_alloc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/page_alloc.c +1 -2
index af63558391345..5f9873dfccc5a 100644
@@ -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;
 	}
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] mm/page_alloc: don't spin_trylock() in NMI on UP

In reply to Brendan Jackman
Quoted 9 lines · Brendan Jackman

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.

Quoted 6 lines

---

Looks good to me, so:

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#4 lore raw

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
index 5f9873dfccc5a..46e5ea59c71df 100644
@@ -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
maintainer Reviewed-by on v1 2/2
#5 lore raw

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() 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.

Quoted 4 lines
Closes also
Quoted 1 line
Quoted 18 lines
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index 5f9873dfccc5a..46e5ea59c71df 100644
@@ -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;
 		}
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] mm/page_alloc: don't spin_trylock() when disallowed in free_one_page()

In reply to Brendan Jackman
Quoted 10 lines · 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.

Quoted 6 lines

---

Looks good to me, so:

Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer
#7 lore raw
In reply to Brendan Jackman
Quoted 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.