linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/compaction: Fix bug in hugetlb handling pathway
@ 2025-04-01  2:10 Vishal Moola (Oracle)
  2025-04-01  2:10 ` [PATCH] mm/compaction: Use folio in hugetlb pathway Vishal Moola (Oracle)
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Vishal Moola (Oracle) @ 2025-04-01  2:10 UTC (permalink / raw)
  To: linux-mm
  Cc: linux-kernel, akpm, muchun.song, Vishal Moola (Oracle),
	Miaohe Lin, Oscar Salvador

The compaction code doesn't take references on pages until we're certain
we should attempt to handle it.

In the hugetlb case, isolate_or_dissolve_huge_page() may return -EBUSY
without taking a reference to the folio associated with our pfn. If our
folio's refcount drops to 0, compound_nr() becomes unpredictable, making
low_pfn and nr_scanned unreliable.
The user-visible effect is minimal - this should rarely happen (if ever).

Fix this by storing the folio statistics earlier on the stack (just like
the THP and Buddy cases).

Also revert commit 66fe1cf7f581 ("mm: compaction: use helper compound_nr
in isolate_migratepages_block")
to make backporting easier.

Fixes: 369fa227c219 ("mm: make alloc_contig_range handle free hugetlb pages")
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Oscar Salvador <osalvador@suse.de>
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
---
 mm/compaction.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/compaction.c b/mm/compaction.c
index 139f00c0308a..ca71fd3c3181 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -981,13 +981,13 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		}
 
 		if (PageHuge(page)) {
+			const unsigned int order = compound_order(page);
 			/*
 			 * skip hugetlbfs if we are not compacting for pages
 			 * bigger than its order. THPs and other compound pages
 			 * are handled below.
 			 */
 			if (!cc->alloc_contig) {
-				const unsigned int order = compound_order(page);
 
 				if (order <= MAX_PAGE_ORDER) {
 					low_pfn += (1UL << order) - 1;
@@ -1011,8 +1011,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 				 /* Do not report -EBUSY down the chain */
 				if (ret == -EBUSY)
 					ret = 0;
-				low_pfn += compound_nr(page) - 1;
-				nr_scanned += compound_nr(page) - 1;
+				low_pfn += (1UL << order) - 1;
+				nr_scanned += (1UL << order) - 1;
 				goto isolate_fail;
 			}
 
-- 
2.48.1



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-04-01 18:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-01  2:10 [PATCH] mm/compaction: Fix bug in hugetlb handling pathway Vishal Moola (Oracle)
2025-04-01  2:10 ` [PATCH] mm/compaction: Use folio in hugetlb pathway Vishal Moola (Oracle)
2025-04-01 15:04   ` Oscar Salvador
2025-04-01 15:23   ` Zi Yan
2025-04-01 14:59 ` [PATCH] mm/compaction: Fix bug in hugetlb handling pathway Oscar Salvador
2025-04-01 17:55   ` Vishal Moola (Oracle)
2025-04-01 15:20 ` Zi Yan
2025-04-01 18:09   ` Vishal Moola (Oracle)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox