Skip to content

Commit bf14ed8

Browse files
yangge1116-cpuakpm00
authored andcommitted
mm/page_alloc: Separate THP PCP into movable and non-movable categories
Since commit 5d0a661 ("mm/page_alloc: use only one PCP list for THP-sized allocations") no longer differentiates the migration type of pages in THP-sized PCP list, it's possible that non-movable allocation requests may get a CMA page from the list, in some cases, it's not acceptable. If a large number of CMA memory are configured in system (for example, the CMA memory accounts for 50% of the system memory), starting a virtual machine with device passthrough will get stuck. During starting the virtual machine, it will call pin_user_pages_remote(..., FOLL_LONGTERM, ...) to pin memory. Normally if a page is present and in CMA area, pin_user_pages_remote() will migrate the page from CMA area to non-CMA area because of FOLL_LONGTERM flag. But if non-movable allocation requests return CMA memory, migrate_longterm_unpinnable_pages() will migrate a CMA page to another CMA page, which will fail to pass the check in check_and_migrate_movable_pages() and cause migration endless. Call trace: pin_user_pages_remote --__gup_longterm_locked // endless loops in this function ----_get_user_pages_locked ----check_and_migrate_movable_pages ------migrate_longterm_unpinnable_pages --------alloc_migration_target This problem will also have a negative impact on CMA itself. For example, when CMA is borrowed by THP, and we need to reclaim it through cma_alloc() or dma_alloc_coherent(), we must move those pages out to ensure CMA's users can retrieve that contigous memory. Currently, CMA's memory is occupied by non-movable pages, meaning we can't relocate them. As a result, cma_alloc() is more likely to fail. To fix the problem above, we add one PCP list for THP, which will not introduce a new cacheline for struct per_cpu_pages. THP will have 2 PCP lists, one PCP list is used by MOVABLE allocation, and the other PCP list is used by UNMOVABLE allocation. MOVABLE allocation contains GPF_MOVABLE, and UNMOVABLE allocation contains GFP_UNMOVABLE and GFP_RECLAIMABLE. Link: https://lkml.kernel.org/r/[email protected] Fixes: 5d0a661 ("mm/page_alloc: use only one PCP list for THP-sized allocations") Signed-off-by: yangge <[email protected]> Cc: Baolin Wang <[email protected]> Cc: Barry Song <[email protected]> Cc: Mel Gorman <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 54e7d59 commit bf14ed8

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

include/linux/mmzone.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,13 +654,12 @@ enum zone_watermarks {
654654
};
655655

656656
/*
657-
* One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. One additional list
658-
* for THP which will usually be GFP_MOVABLE. Even if it is another type,
659-
* it should not contribute to serious fragmentation causing THP allocation
660-
* failures.
657+
* One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. Two additional lists
658+
* are added for THP. One PCP list is used by GPF_MOVABLE, and the other PCP list
659+
* is used by GFP_UNMOVABLE and GFP_RECLAIMABLE.
661660
*/
662661
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
663-
#define NR_PCP_THP 1
662+
#define NR_PCP_THP 2
664663
#else
665664
#define NR_PCP_THP 0
666665
#endif

mm/page_alloc.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,15 @@ static void bad_page(struct page *page, const char *reason)
504504

505505
static inline unsigned int order_to_pindex(int migratetype, int order)
506506
{
507+
bool __maybe_unused movable;
508+
507509
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
508510
if (order > PAGE_ALLOC_COSTLY_ORDER) {
509511
VM_BUG_ON(order != HPAGE_PMD_ORDER);
510-
return NR_LOWORDER_PCP_LISTS;
512+
513+
movable = migratetype == MIGRATE_MOVABLE;
514+
515+
return NR_LOWORDER_PCP_LISTS + movable;
511516
}
512517
#else
513518
VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);
@@ -521,7 +526,7 @@ static inline int pindex_to_order(unsigned int pindex)
521526
int order = pindex / MIGRATE_PCPTYPES;
522527

523528
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
524-
if (pindex == NR_LOWORDER_PCP_LISTS)
529+
if (pindex >= NR_LOWORDER_PCP_LISTS)
525530
order = HPAGE_PMD_ORDER;
526531
#else
527532
VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);

0 commit comments

Comments
 (0)