Skip to content

Commit 88ee134

Browse files
x-y-zakpm00
authored andcommitted
mm: fix a potential infinite loop in start_isolate_page_range()
In isolate_single_pageblock() called by start_isolate_page_range(), there are some pageblock isolation issues causing a potential infinite loop when isolating a page range. This is reported by Qian Cai. 1. the pageblock was isolated by just changing pageblock migratetype without checking unmovable pages. Calling set_migratetype_isolate() to isolate pageblock properly. 2. an off-by-one error caused migrating pages unnecessarily, since the page is not crossing pageblock boundary. 3. migrating a compound page across pageblock boundary then splitting the free page later has a small race window that the free page might be allocated again, so that the code will try again, causing an potential infinite loop. Temporarily set the to-be-migrated page's pageblock to MIGRATE_ISOLATE to prevent that and bail out early if no free page is found after page migration. An additional fix to split_free_page() aims to avoid crashing in __free_one_page(). When the free page is split at the specified split_pfn_offset, free_page_order should check both the first bit of free_page_pfn and the last bit of split_pfn_offset and use the smaller one. For example, if free_page_pfn=0x10000, split_pfn_offset=0xc000, free_page_order should first be 0x8000 then 0x4000, instead of 0x4000 then 0x8000, which the original algorithm did. [akpm@linux-foundation.org: suppress min() warning] Link: https://lkml.kernel.org/r/20220524194756.1698351-1-zi.yan@sent.com Fixes: b2c9e2f ("mm: make alloc_contig_range work at pageblock granularity") Signed-off-by: Zi Yan <ziy@nvidia.com> Reported-by: Qian Cai <quic_qiancai@quicinc.com> Cc: Christophe Leroy <christophe.leroy@csgroup.eu> Cc: David Hildenbrand <david@redhat.com> Cc: Eric Ren <renzhengeek@gmail.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Mike Rapoport <rppt@linux.ibm.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Oscar Salvador <osalvador@suse.de> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent bb5ced4 commit 88ee134

2 files changed

Lines changed: 46 additions & 13 deletions

File tree

mm/page_alloc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,18 @@ void split_free_page(struct page *free_page,
11141114
unsigned long flags;
11151115
int free_page_order;
11161116

1117+
if (split_pfn_offset == 0)
1118+
return;
1119+
11171120
spin_lock_irqsave(&zone->lock, flags);
11181121
del_page_from_free_list(free_page, zone, order);
11191122
for (pfn = free_page_pfn;
11201123
pfn < free_page_pfn + (1UL << order);) {
11211124
int mt = get_pfnblock_migratetype(pfn_to_page(pfn), pfn);
11221125

1123-
free_page_order = ffs(split_pfn_offset) - 1;
1126+
free_page_order = min_t(int,
1127+
pfn ? __ffs(pfn) : order,
1128+
__fls(split_pfn_offset));
11241129
__free_one_page(pfn_to_page(pfn), pfn, zone, free_page_order,
11251130
mt, FPI_NONE);
11261131
pfn += 1UL << free_page_order;

mm/page_isolation.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)
283283
* isolate_single_pageblock() -- tries to isolate a pageblock that might be
284284
* within a free or in-use page.
285285
* @boundary_pfn: pageblock-aligned pfn that a page might cross
286+
* @flags: isolation flags
286287
* @gfp_flags: GFP flags used for migrating pages
287288
* @isolate_before: isolate the pageblock before the boundary_pfn
288289
*
@@ -298,14 +299,15 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)
298299
* either. The function handles this by splitting the free page or migrating
299300
* the in-use page then splitting the free page.
300301
*/
301-
static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
302-
bool isolate_before)
302+
static int isolate_single_pageblock(unsigned long boundary_pfn, int flags,
303+
gfp_t gfp_flags, bool isolate_before)
303304
{
304305
unsigned char saved_mt;
305306
unsigned long start_pfn;
306307
unsigned long isolate_pageblock;
307308
unsigned long pfn;
308309
struct zone *zone;
310+
int ret;
309311

310312
VM_BUG_ON(!IS_ALIGNED(boundary_pfn, pageblock_nr_pages));
311313

@@ -325,7 +327,11 @@ static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
325327
zone->zone_start_pfn);
326328

327329
saved_mt = get_pageblock_migratetype(pfn_to_page(isolate_pageblock));
328-
set_pageblock_migratetype(pfn_to_page(isolate_pageblock), MIGRATE_ISOLATE);
330+
ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), saved_mt, flags,
331+
isolate_pageblock, isolate_pageblock + pageblock_nr_pages);
332+
333+
if (ret)
334+
return ret;
329335

330336
/*
331337
* Bail out early when the to-be-isolated pageblock does not form
@@ -374,7 +380,7 @@ static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
374380
struct page *head = compound_head(page);
375381
unsigned long head_pfn = page_to_pfn(head);
376382

377-
if (head_pfn + nr_pages < boundary_pfn) {
383+
if (head_pfn + nr_pages <= boundary_pfn) {
378384
pfn = head_pfn + nr_pages;
379385
continue;
380386
}
@@ -386,7 +392,8 @@ static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
386392
if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) {
387393
int order;
388394
unsigned long outer_pfn;
389-
int ret;
395+
int page_mt = get_pageblock_migratetype(page);
396+
bool isolate_page = !is_migrate_isolate_page(page);
390397
struct compact_control cc = {
391398
.nr_migratepages = 0,
392399
.order = -1,
@@ -399,9 +406,31 @@ static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
399406
};
400407
INIT_LIST_HEAD(&cc.migratepages);
401408

409+
/*
410+
* XXX: mark the page as MIGRATE_ISOLATE so that
411+
* no one else can grab the freed page after migration.
412+
* Ideally, the page should be freed as two separate
413+
* pages to be added into separate migratetype free
414+
* lists.
415+
*/
416+
if (isolate_page) {
417+
ret = set_migratetype_isolate(page, page_mt,
418+
flags, head_pfn, head_pfn + nr_pages);
419+
if (ret)
420+
goto failed;
421+
}
422+
402423
ret = __alloc_contig_migrate_range(&cc, head_pfn,
403424
head_pfn + nr_pages);
404425

426+
/*
427+
* restore the page's migratetype so that it can
428+
* be split into separate migratetype free lists
429+
* later.
430+
*/
431+
if (isolate_page)
432+
unset_migratetype_isolate(page, page_mt);
433+
405434
if (ret)
406435
goto failed;
407436
/*
@@ -417,10 +446,9 @@ static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
417446
order = 0;
418447
outer_pfn = pfn;
419448
while (!PageBuddy(pfn_to_page(outer_pfn))) {
420-
if (++order >= MAX_ORDER) {
421-
outer_pfn = pfn;
422-
break;
423-
}
449+
/* stop if we cannot find the free page */
450+
if (++order >= MAX_ORDER)
451+
goto failed;
424452
outer_pfn &= ~0UL << order;
425453
}
426454
pfn = outer_pfn;
@@ -435,7 +463,7 @@ static int isolate_single_pageblock(unsigned long boundary_pfn, gfp_t gfp_flags,
435463
return 0;
436464
failed:
437465
/* restore the original migratetype */
438-
set_pageblock_migratetype(pfn_to_page(isolate_pageblock), saved_mt);
466+
unset_migratetype_isolate(pfn_to_page(isolate_pageblock), saved_mt);
439467
return -EBUSY;
440468
}
441469

@@ -496,12 +524,12 @@ int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
496524
int ret;
497525

498526
/* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
499-
ret = isolate_single_pageblock(isolate_start, gfp_flags, false);
527+
ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false);
500528
if (ret)
501529
return ret;
502530

503531
/* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
504-
ret = isolate_single_pageblock(isolate_end, gfp_flags, true);
532+
ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true);
505533
if (ret) {
506534
unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype);
507535
return ret;

0 commit comments

Comments
 (0)