Skip to content

Commit 1c56343

Browse files
minchankakpm00
authored andcommitted
mm: fix is_pinnable_page against a cma page
Pages in the CMA area could have MIGRATE_ISOLATE as well as MIGRATE_CMA so the current is_pinnable_page() could miss CMA pages which have MIGRATE_ISOLATE. It ends up pinning CMA pages as longterm for the pin_user_pages() API so CMA allocations keep failing until the pin is released. CPU 0 CPU 1 - Task B cma_alloc alloc_contig_range pin_user_pages_fast(FOLL_LONGTERM) change pageblock as MIGRATE_ISOLATE internal_get_user_pages_fast lockless_pages_from_mm gup_pte_range try_grab_folio is_pinnable_page return true; So, pinned the page successfully. page migration failure with pinned page .. .. After 30 sec unpin_user_page(page) CMA allocation succeeded after 30 sec. The CMA allocation path protects the migration type change race using zone->lock but what GUP path need to know is just whether the page is on CMA area or not rather than exact migration type. Thus, we don't need zone->lock but just checks migration type in either of (MIGRATE_ISOLATE and MIGRATE_CMA). Adding the MIGRATE_ISOLATE check in is_pinnable_page could cause rejecting of pinning pages on MIGRATE_ISOLATE pageblocks even though it's neither CMA nor movable zone if the page is temporarily unmovable. However, such a migration failure by unexpected temporal refcount holding is general issue, not only come from MIGRATE_ISOLATE and the MIGRATE_ISOLATE is also transient state like other temporal elevated refcount problem. Link: https://lkml.kernel.org/r/20220524171525.976723-1-minchan@kernel.org Signed-off-by: Minchan Kim <minchan@kernel.org> Reviewed-by: John Hubbard <jhubbard@nvidia.com> Acked-by: Paul E. McKenney <paulmck@kernel.org> Cc: David Hildenbrand <david@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ba6851b commit 1c56343

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

include/linux/mm.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,13 @@ static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma,
15941594
#ifdef CONFIG_MIGRATION
15951595
static inline bool is_pinnable_page(struct page *page)
15961596
{
1597-
return !(is_zone_movable_page(page) || is_migrate_cma_page(page)) ||
1598-
is_zero_pfn(page_to_pfn(page));
1597+
#ifdef CONFIG_CMA
1598+
int mt = get_pageblock_migratetype(page);
1599+
1600+
if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
1601+
return false;
1602+
#endif
1603+
return !(is_zone_movable_page(page) || is_zero_pfn(page_to_pfn(page)));
15991604
}
16001605
#else
16011606
static inline bool is_pinnable_page(struct page *page)

mm/page_alloc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,12 @@ unsigned long __get_pfnblock_flags_mask(const struct page *page,
482482
bitidx = pfn_to_bitidx(page, pfn);
483483
word_bitidx = bitidx / BITS_PER_LONG;
484484
bitidx &= (BITS_PER_LONG-1);
485-
486-
word = bitmap[word_bitidx];
485+
/*
486+
* This races, without locks, with set_pfnblock_flags_mask(). Ensure
487+
* a consistent read of the memory array, so that results, even though
488+
* racy, are not corrupted.
489+
*/
490+
word = READ_ONCE(bitmap[word_bitidx]);
487491
return (word >> bitidx) & mask;
488492
}
489493

0 commit comments

Comments
 (0)