Skip to contentSkip to conversation

mm, page_alloc: several stable fixes

PATCH In review Vlastimil Babka (SUSE) 2 participants 9 messages Nov 3 – Nov 4

Subsystems (4)
lore
Share & tools
lore

Vlastimil Babka (SUSE)

author maintainer
#0 lore raw

v2 cover mm, page_alloc: several stable fixes

A bunch of fixes from Josef, filtered for page allocator by David.

I have tweaked some commit logs and a comment in one case.

In v2 some were dropped as they were merged already, the last one was changed to fix things differently. "mm/compaction: trust zone->contiguous only in the span" was dropped due to lots of sashiko feedback. compaction (as a pfn scanner) needs much more thought

Based on page_alloc/for-next-fixes, to be applied there as well.

---
Changes in v2:
- rebased on page_alloc/for-next-fixes
- dropped 3 patches that were already merged from Gregory
- dropped "mm/compaction: trust zone->contiguous only in the span" as it
needs more thougt
- in the last patch, remove the VM_BUG_ON_PAGE completely
- Link to v1: https://patch.msgid.link/[email protected]

---
Josef Bacik (4):
mm/page_alloc: clear ->private of deferred free pages
mm: page_frag: refill the cache for large fragments
mm: page_isolation: do not take slab pages for free mm/memory_hotplug: resize zones under their seqlock

Vlastimil Babka (SUSE) (1):
mm/page_alloc: remove the zone_spans_pfn() check from get_pfnblock_bitmap_bitidx()

 mm/memory_hotplug.c  | 51 +++++++++++++++++++++++++++++++++++----------------
 mm/page_alloc.c      |  3 ++-
 mm/page_frag_cache.c | 21 +++++++++++++--------
 mm/page_isolation.c  |  7 +++++++
 4 files changed, 57 insertions(+), 25 deletions(-)

---
base-commit: 743a99d7e339e81acaf76f3c902b23abb29abc15
change-id: 20261027-pgalloc-ai-hotfixes-59bcd873a355

author maintainer
#1 lore raw

PATCH v2 1/5 mm/page_alloc: clear ->private of deferred free pages

From: Josef Bacik <[email protected]>

free_pages_nolock() and free_frozen_pages_nolock() put the page on zone->trylock_free_pages if they get neither the pcp lock nor zone->lock. add_page_to_zone_llist() keeps the order of the page in page->private. The next free_one_page() which does get zone->lock passes these pages to split_large_buddy() with ->private still set.

__free_one_page() writes ->private of the head of the resulting free page only. If the deferred page is merged with its lower buddy, it becomes a tail of that free page and keeps the order in ->private.

All other free pages which are not the head of a buddy have a ->private of zero, and since commit 819bfbd47e49 ("mm/page_alloc: remove set_page_private() in prep_compound_tail()") the allocator relies on it. When the stale page is allocated again as a tail of a larger page:

- prep_compound_tail() hits VM_WARN_ON_ONCE(tail->private);
- with the page checks enabled, __free_pages_prepare() reports "BUG: Bad page state", "nonzero private", and leaks the whole page;
- without them the value is seen by whoever uses that page next, e.g. as folio->private of a folio which is split off a large folio.

Only pages of an order above zero are affected. Those can be freed through _nolock() by the stack depot (order 2) and by SLUB (a new slab which cannot be added to the partial list by kmalloc_nolock()).

Clear ->private when the page is taken off the list.

Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
Assisted-by LLM
---
 mm/page_alloc.c | 2 ++
 1 file changed, 2 insertions(+)
mm/page_alloc.c +2 -0
index 55bd558306f5..41fd80c0c762 100644
@@ -1562,6 +1562,8 @@ static void free_one_page(struct zone *zone, struct page *page,
 		llist_for_each_entry_safe(p, tmp, llnode, pcp_llist) {
 			unsigned int p_order = p->private;
 
+			/* It may end up as a tail of the merged free page. */
+			p->private = 0;
 			split_large_buddy(zone, p, page_to_pfn(p), p_order, fpi_flags);
 			__count_vm_events(PGFREE, 1 << p_order);
 		}
Signature · 1 line
2.56.0
reviewer Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] mm/page_alloc: clear ->private of deferred free pages

Quoted 29 lines · Vlastimil Babka (SUSE)

From: Josef Bacik <[email protected]>

free_pages_nolock() and free_frozen_pages_nolock() put the page on zone->trylock_free_pages if they get neither the pcp lock nor zone->lock. add_page_to_zone_llist() keeps the order of the page in page->private. The next free_one_page() which does get zone->lock passes these pages to split_large_buddy() with ->private still set.

__free_one_page() writes ->private of the head of the resulting free page only. If the deferred page is merged with its lower buddy, it becomes a tail of that free page and keeps the order in ->private.

All other free pages which are not the head of a buddy have a ->private of zero, and since commit 819bfbd47e49 ("mm/page_alloc: remove set_page_private() in prep_compound_tail()") the allocator relies on it. When the stale page is allocated again as a tail of a larger page:

- prep_compound_tail() hits VM_WARN_ON_ONCE(tail->private);
- with the page checks enabled, __free_pages_prepare() reports "BUG: Bad page state", "nonzero private", and leaks the whole page;
- without them the value is seen by whoever uses that page next, e.g. as folio->private of a folio which is split off a large folio.

Only pages of an order above zero are affected. Those can be freed through _nolock() by the stack depot (order 2) and by SLUB (a new slab which cannot be added to the partial list by kmalloc_nolock()).

Clear ->private when the page is taken off the list.

Quoted 7 lines
Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
Assisted-by LLM
author maintainer
#3 lore raw

PATCH v2 2/5 mm: page_frag: refill the cache for large fragments

In reply to Vlastimil Babka (SUSE)

From: Josef Bacik <[email protected]>

A fragment which is larger than PAGE_SIZE can only be served from the 32K page of a page_frag_cache. If memory is low and the cache had to fall back to a single page, __page_frag_alloc_align() returns NULL and keeps that page.

Since the offset counts up, this check comes first and is made whenever the fragment does not fit behind nc->offset, no matter how big the page of the cache is. So the cache is never refilled on behalf of a large
fragment: with fragsz = 10000 the first three allocations succeed, the fourth one and all the following ones fail although memory is plentiful. nc->offset does not move either, so this lasts until smaller fragments have used up the rest of the page, which never happens if the cache has no other user.

The nfp driver allocates its receive buffers with napi_alloc_frag() and netdev_alloc_frag(). They are larger than a page with a jumbo MTU.

Only give up if the fragment does not fit into the page at all, which is what the comment describes. The fourth allocation in the scenario above with a 32k page will thus now proceed to refill the cache.

Fixes 8218f62c9c9b ("mm: page_frag: use initial zero offset for page_frag_alloc_align()")
Assisted-by LLM
[[email protected]: make the comment more obvious]
---
 mm/page_frag_cache.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
mm/page_frag_cache.c +13 -8
index e63efe78b7d4..8914437ddc51 100644
@@ -119,15 +119,20 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc,
 	size = PAGE_SIZE << encoded_page_decode_order(encoded_page);
 	offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask);
 	if (unlikely(offset + fragsz > size)) {
-		if (unlikely(fragsz > PAGE_SIZE)) {
+		if (unlikely(fragsz > size)) {
 			/*
-			 * The caller is trying to allocate a fragment
-			 * with fragsz > PAGE_SIZE but the cache isn't big
-			 * enough to satisfy the request, this may
-			 * happen in low memory conditions.
-			 * We don't release the cache page because
-			 * it could make memory pressure worse
-			 * so we simply return NULL here.
+			 * The caller is trying to allocate a fragment that's
+			 * larger than the whole cache page.
+			 *
+			 * Either fragsz is larger than PAGE_FRAG_CACHE_MAX_SIZE
+			 * so this cannot ever succeed.
+			 *
+			 * Or the refill has fallen back to order-0 due to a low
+			 * memory condition. Then we don't want to release the
+			 * cache page to attempt a new large-size refill because
+			 * it could make memory pressure worse.
+			 *
+			 * So in both cases we simply return NULL here.
 			 */
 			return NULL;
 		}
Signature · 1 line
2.56.0
author maintainer
#4 lore raw

PATCH v2 3/5 mm: page_isolation: do not take slab pages for free

In reply to Vlastimil Babka (SUSE)

From: Josef Bacik <[email protected]>

page_is_unmovable() regards a page with a reference count of zero as free or about to be freed. Since slab allocates frozen pages, they will be wrongly considered to be freed if order 0 (higher-order compound pages are caught earlier).

If such slabs are the only unmovable pages of a memory block, memory offlining now isolates the block, this can lead to offline_pages() looping without progress.

Thus detect the slab pages as removable to return -EBUSY, as would happen before commit 9aec2fb0fd5e ("slab: allocate frozen pages").

Fixes 9aec2fb0fd5e ("slab: allocate frozen pages")
Assisted-by LLM
---
 mm/memory_hotplug.c | 7 +++++++
 mm/page_isolation.c | 7 +++++++
 2 files changed, 14 insertions(+)
mm/memory_hotplug.c +7 -0
index c9cc42e54f05..0ab9a6e09222 100644
@@ -1832,6 +1832,13 @@ static int scan_movable_pages(unsigned long start, unsigned long end,
 		if (PageOffline(page) && page_count(page))
 			return -EBUSY;
 
+		/*
+		 * A page that was allocated, but was not a slab page yet, when
+		 * its pageblock was isolated.
+		 */
+		if (PageSlab(page))
+			return -EBUSY;
+
 		folio = page_folio(page);
 		if (!folio_test_hugetlb(folio))
 			continue;
mm/page_isolation.c +7 -0
index 5aaad037384e..f4f0571607ab 100644
@@ -74,6 +74,13 @@ bool page_is_unmovable(struct zone *zone, struct page *page,
 		return false;
 	}
 
+	/*
+	 * Slab pages are frozen: do not take their reference count of zero
+	 * for that of a free page.
+	 */
+	if (PageSlab(page))
+		return true;
+
 	/*
 	 * We can't use page_count without pin a page
 	 * because another CPU can free compound page.
Signature · 1 line
2.56.0
reviewer Reviewed-by on v2 3/5
#5 lore raw

Re: [PATCH v2 3/5] mm: page_isolation: do not take slab pages for free

Quoted 14 lines · Vlastimil Babka (SUSE)

From: Josef Bacik <[email protected]>

page_is_unmovable() regards a page with a reference count of zero as free or about to be freed. Since slab allocates frozen pages, they will be wrongly considered to be freed if order 0 (higher-order compound pages are caught earlier).

If such slabs are the only unmovable pages of a memory block, memory offlining now isolates the block, this can lead to offline_pages() looping without progress.

Thus detect the slab pages as removable to return -EBUSY, as would happen before commit 9aec2fb0fd5e ("slab: allocate frozen pages").

Quoted 6 lines
Fixes 9aec2fb0fd5e ("slab: allocate frozen pages")
Assisted-by LLM
author maintainer
#6 lore raw

PATCH v2 4/5 mm/memory_hotplug: resize zones under their seqlock

In reply to Vlastimil Babka (SUSE)

From: Josef Bacik <[email protected]>

With CONFIG_DEBUG_VM the page allocator checks that a page which is freed, split or allocated lies within its zone. The check reads zone->zone_start_pfn and zone->spanned_pages under zone->span_seqlock, as memory hotplug can change the two at any time.

Commit 27cacaad16c5 ("mm,memory_hotplug: drop unneeded locking") removed the write side, since all resizing is serialized by the hotplug lock. That leaves the reader without protection. When memory below the start of a zone goes online, the start is lowered first and the size is raised afterwards. When the lowest memory of a zone goes offline, the size is reduced first. In between, the zone seems to end before its last pages. If one of them goes through the allocator at that moment, it is reported as being outside of the zone triggers VM_BUG_ON_PAGE().

Write the two fields in a write section of the seqlock. Interrupts are disabled as pages are freed from interrupts, too, and the reader would wait for the writer which it has interrupted.

Fixes 27cacaad16c5 ("mm,memory_hotplug: drop unneeded locking")
Assisted-by LLM
---
 mm/memory_hotplug.c | 44 ++++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 16 deletions(-)
mm/memory_hotplug.c +28 -16
index 0ab9a6e09222..b9f02bc6ed0d 100644
@@ -461,6 +461,22 @@ static unsigned long find_biggest_section_pfn(struct zone *zone,
 	return 0;
 }
 
+/*
+ * The page allocator checks pages against the span of their zone while the
+ * zone is resized, see page_outside_zone_boundaries(). It must not see the
+ * new value of one field together with the old value of the other one.
+ */
+static void zone_set_span(struct zone *zone, unsigned long start_pfn,
+			  unsigned long spanned_pages)
+{
+	unsigned long flags;
+
+	write_seqlock_irqsave(&zone->span_seqlock, flags);
+	zone->zone_start_pfn = start_pfn;
+	zone->spanned_pages = spanned_pages;
+	write_sequnlock_irqrestore(&zone->span_seqlock, flags);
+}
+
 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
 			     unsigned long end_pfn)
 {
@@ -475,13 +491,10 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
 		 */
 		pfn = find_smallest_section_pfn(zone, end_pfn,
 						zone_end_pfn(zone));
-		if (pfn) {
-			zone->spanned_pages = zone_end_pfn(zone) - pfn;
-			zone->zone_start_pfn = pfn;
-		} else {
-			zone->zone_start_pfn = 0;
-			zone->spanned_pages = 0;
-		}
+		if (pfn)
+			zone_set_span(zone, pfn, zone_end_pfn(zone) - pfn);
+		else
+			zone_set_span(zone, 0, 0);
 	} else if (zone_end_pfn(zone) == end_pfn) {
 		/*
 		 * If the section is biggest section in the zone, it need
@@ -492,11 +505,10 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
 		pfn = find_biggest_section_pfn(zone, zone->zone_start_pfn,
 					       start_pfn);
 		if (pfn)
-			zone->spanned_pages = pfn - zone->zone_start_pfn + 1;
-		else {
-			zone->zone_start_pfn = 0;
-			zone->spanned_pages = 0;
-		}
+			zone_set_span(zone, zone->zone_start_pfn,
+				      pfn - zone->zone_start_pfn + 1);
+		else
+			zone_set_span(zone, 0, 0);
 	}
 }
 
@@ -684,12 +696,12 @@ static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages)
 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
 		unsigned long nr_pages)
 {
-	unsigned long old_end_pfn = zone_end_pfn(zone);
+	unsigned long end_pfn = max(start_pfn + nr_pages, zone_end_pfn(zone));
 
-	if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
-		zone->zone_start_pfn = start_pfn;
+	if (!zone_is_empty(zone) && start_pfn > zone->zone_start_pfn)
+		start_pfn = zone->zone_start_pfn;
 
-	zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
+	zone_set_span(zone, start_pfn, end_pfn - start_pfn);
 }
 
 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
Signature · 1 line
2.56.0
author maintainer
#7 lore raw

PATCH v2 5/5 mm/page_alloc: remove the zone_spans_pfn() check from get_pfnblock_bitmap_bitidx()

In reply to Vlastimil Babka (SUSE)

Whenever the flags of a pageblock are read or written, a kernel with CONFIG_DEBUG_VM makes sure that the pfn lies within the zone of the page. Unlike bad_range(), that check reads zone->zone_start_pfn and zone->spanned_pages without zone->span_seqlock.

Memory hotplug changes the two fields one after the other. If the lower end of a zone moves, the zone seems to end before its last pages in between. A page of that part which is freed at that moment, on another CPU, ends in

  page dumped because: VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn))
  kernel BUG at mm/page_alloc.c:372!
   free_unref_folios+0x55e/0xed0
   folios_put_refs+0x2b5/0x530

An attempt has been made [1] to fix this with the span seqlock check. But that can trigger all sorts of problems in restricted contexts, as noted by sashiko [2].

Since this is just a sanity debug check and anything with BUG_ON is frowned upon anyway, just remove it altogether.

---
 mm/page_alloc.c | 1 -
 1 file changed, 1 deletion(-)
mm/page_alloc.c +0 -1
index 41fd80c0c762..3a7b9b533368 100644
@@ -370,7 +370,6 @@ get_pfnblock_bitmap_bitidx(const struct page *page, unsigned long pfn,
 	BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
 #endif
 	BUILD_BUG_ON(__MIGRATE_TYPE_END > PAGEBLOCK_MIGRATETYPE_MASK);
-	VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
 
 	bitmap = get_pageblock_bitmap(page, pfn);
 	*bitidx = pfn_to_bitidx(page, pfn);
Signature · 1 line
2.56.0
author maintainer
#8 lore raw
In reply to Vlastimil Babka (SUSE)
Quoted 10 lines · Vlastimil Babka (SUSE)

A bunch of fixes from Josef, filtered for page allocator by David.

I have tweaked some commit logs and a comment in one case.

In v2 some were dropped as they were merged already, the last one was changed to fix things differently. "mm/compaction: trust zone->contiguous only in the span" was dropped due to lots of sashiko feedback. compaction (as a pfn scanner) needs much more thought

Based on page_alloc/for-next-fixes, to be applied there as well.

Now done.

Quoted 29 lines

---
Changes in v2:
- rebased on page_alloc/for-next-fixes
- dropped 3 patches that were already merged from Gregory
- dropped "mm/compaction: trust zone->contiguous only in the span" as it
needs more thougt
- in the last patch, remove the VM_BUG_ON_PAGE completely
- Link to v1: https://patch.msgid.link/[email protected]

---
Josef Bacik (4):
mm/page_alloc: clear ->private of deferred free pages
mm: page_frag: refill the cache for large fragments
mm: page_isolation: do not take slab pages for free mm/memory_hotplug: resize zones under their seqlock

Vlastimil Babka (SUSE) (1):
mm/page_alloc: remove the zone_spans_pfn() check from get_pfnblock_bitmap_bitidx()

 mm/memory_hotplug.c  | 51 +++++++++++++++++++++++++++++++++++----------------
 mm/page_alloc.c      |  3 ++-
 mm/page_frag_cache.c | 21 +++++++++++++--------
 mm/page_isolation.c  |  7 +++++++
 4 files changed, 57 insertions(+), 25 deletions(-)

---
base-commit: 743a99d7e339e81acaf76f3c902b23abb29abc15
change-id: 20261027-pgalloc-ai-hotfixes-59bcd873a355