mm/page_alloc: skip THP-sized PCP list when allocating non-CMA THP-sized page
Subsystems (2)
PATCH 1/1 mm/page_alloc: skip THP-sized PCP list when allocating non-CMA THP-sized page
From: yangge <[email protected]>
Since commit 5d0a661d808f ("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page with PF_MEMALLOC_PIN flag returns a CMA page.
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/page_alloc.c +10 -0
@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) { + page =rmqueue_pcplist (preferred_zone, zone, order, + migratetype, alloc_flags); +if (likely (page)) + goto out; + } +#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags,
Signature · 1 line
2.7.4
Cc Johannes, Zi and Vlastimil.
Quoted 22 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +0
-0
mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2992
Quoted 6 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) {
Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in __rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?
Quoted 14 lines
mm/page_alloc.c +7
-0+ page =rmqueue_pcplist (preferred_zone, zone, order, + migratetype, alloc_flags); +if (likely (page)) + goto out; + } +#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags,
Quoted 20 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 17 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>---
mm/page_alloc.c| 10 ++++++++++1 file changed, 10 insertions(+)
Quoted 12 lines
mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2991
Quoted 10 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone, WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); if (likely(pcp_allowed_order(order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags & ALLOC_CMA || + order != HPAGE_PMD_ORDER) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in __rmqueue_pcplist(), we may need to compare many times because of pcp batch.
Quoted 37 lines · On 2024/6/4 20:36, yangge1116 wrote:
Quoted 20 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 17 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>---
mm/page_alloc.c| 10 ++++++++++1 file changed, 10 insertions(+)Quoted 13 lines
mm/page_alloc.c struct page *rmqueue(struct zone · line 2992
Quoted 11 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone, WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); if (likely(pcp_allowed_order(order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags & ALLOC_CMA || + order != HPAGE_PMD_ORDER) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?
Quoted 1 line
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in
__rmqueue_pcplist(), we may need to compare many times because of pcp batch.
I mean we can only compare once, focusing on CMA pages.
mm/page_alloc.c +5 -0
@@ -2973,6 +2973,11 @@ struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, } page = list_first_entry(list, struct page, pcp_list); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (order ==HPAGE_PMD_ORDER && !is_migrate_movable (migratetype) && +is_migrate_cma (get_pageblock_migratetype (page))) +return NULL ; +#endif list_del (&page->pcp_list); pcp->count -=1 << order; }while (check_new_pages (page, order));
Quoted 40 lines · Baolin Wang
Quoted 37 lines · On 2024/6/4 20:36, yangge1116 wrote:
Quoted 33 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 22 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>---
mm/page_alloc.c| 10 ++++++++++1 file changed, 10 insertions(+)mm/page_alloc.c +0
-0mm/page_alloc.c struct page *rmqueue(struct zone · line 2992
Quoted 6 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone, WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); if (likely(pcp_allowed_order(order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags & ALLOC_CMA || + order != HPAGE_PMD_ORDER) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?
Quoted 3 lines
Quoted 1 line
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in
__rmqueue_pcplist(), we may need to compare many times because of pcp batch.I mean we can only compare once, focusing on CMA pages.
pcp_list may contains CMA and no-CMA pages, why only compare once, just increase one chance of using the pcp_list?
Quoted 19 lines
mm/page_alloc.c +2
-0@@ -2973,6 +2973,11 @@ struct page *__rmqueue_pcplist(struct zone *zone,unsigned int order, } page = list_first_entry(list, struct page, pcp_list); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order == HPAGE_PMD_ORDER &&!is_migrate_movable(migratetype) &&
+ is_migrate_cma(get_pageblock_migratetype(page))) + return NULL;
+#endif
list_del(&page->pcp_list);
pcp->count-= 1 << order;
} while (check_new_pages(page, order));
Quoted 62 lines · Baolin Wang
Quoted 40 lines · On 2024/6/4 20:36, yangge1116 wrote:
Quoted 21 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 18 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)Quoted 15 lines
mm/page_alloc.c struct page *rmqueue(struct zone · line 2992
Quoted 11 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?Quoted 3 lines
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in
__rmqueue_pcplist(), we may need to compare many times because of pcp batch.I mean we can only compare once, focusing on CMA pages.
mm/page_alloc.c +2
-0@@ -2973,6 +2973,11 @@ struct page *__rmqueue_pcplist(struct zone *zone,unsigned int order, } page = list_first_entry(list, struct page, pcp_list); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order == HPAGE_PMD_ORDER &&!is_migrate_movable(migratetype) &&
mm/page_alloc.c struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, · line 2980
Quoted 3 lines
mm/page_alloc.c +3
-0+ is_migrate_cma(get_pageblock_migratetype(page))) +return NULL ; +#endif
This doesn't seem ideal either. It's possible that the PCP still has many non-CMA folios, but due to bad luck, the first entry is "always" CMA. In this case,
allocations with is_migrate_movable(migratetype) == false will always lose the
chance to use the PCP. It also appears to incur a PCP spin lock/unlock.
I don't see an ideal solution unless we bring back the CMA PCP :-)
Quoted 4 lines
list_del(&page->pcp_list);
pcp->count -= 1 << order;
} while (check_new_pages(page, order));Quoted 60 lines · Barry Song
Quoted 58 lines · Baolin Wang
Quoted 40 lines · On 2024/6/4 20:36, yangge1116 wrote:
Quoted 21 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 18 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)Quoted 15 lines
mm/page_alloc.c struct page *rmqueue(struct zone · line 2992
Quoted 11 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?Quoted 3 lines
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in
__rmqueue_pcplist(), we may need to compare many times because of pcp batch.I mean we can only compare once, focusing on CMA pages.
mm/page_alloc.c +0
-0@@ -2973,6 +2973,11 @@ struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, }
Quoted 13 lines
mm/page_alloc.c struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, · line 2980
Quoted 7 lines
mm/page_alloc.c +2
-0page = list_first_entry(list, struct page, pcp_list); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order == HPAGE_PMD_ORDER &&!is_migrate_movable(migratetype) &&
mm/page_alloc.c +3
-0+ is_migrate_cma(get_pageblock_migratetype(page))) +return NULL ; +#endif This doesn't seem ideal either. It's possible that the PCP still has many non-CMA folios, but due to bad luck, the first entry is "always" CMA. In this case,
allocations withis_migrate_movable(migratetype)== false will always lose the
chance to use the PCP. It also appears to incur a PCP spin lock/unlock.
Yes, just some ideas to to mitigate the issue...
Quoted 2 lines
I don't see an ideal solution unless we bring back the CMA PCP :-)
Tend to agree, and adding a CMA PCP seems the overhead can be acceptable?
Quoted 78 lines · Baolin Wang
Quoted 60 lines · Barry Song
Quoted 58 lines · Baolin Wang
Quoted 40 lines · On 2024/6/4 20:36, yangge1116 wrote:
Quoted 21 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 18 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)Quoted 15 lines
mm/page_alloc.c struct page *rmqueue(struct zone · line 2992
Quoted 11 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?Quoted 3 lines
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in
__rmqueue_pcplist(), we may need to compare many times because of pcp batch.I mean we can only compare once, focusing on CMA pages.
mm/page_alloc.c +0
-0@@ -2973,6 +2973,11 @@ struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, }Quoted 13 lines
mm/page_alloc.c struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, · line 2980
Quoted 7 lines
mm/page_alloc.c +2
-0page = list_first_entry(list, struct page, pcp_list); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order == HPAGE_PMD_ORDER &&!is_migrate_movable(migratetype) &&
mm/page_alloc.c +3
-0+ is_migrate_cma(get_pageblock_migratetype(page))) +return NULL ; +#endif This doesn't seem ideal either. It's possible that the PCP still has many non-CMA folios, but due to bad luck, the first entry is "always" CMA. In this case,
allocations withis_migrate_movable(migratetype)== false will always lose the
chance to use the PCP. It also appears to incur a PCP spin lock/unlock.
Quoted 6 lines
Yes, just some ideas to to mitigate the issue...
Quoted 2 lines
I don't see an ideal solution unless we bring back the CMA PCP :-)
Tend to agree, and adding a CMA PCP seems the overhead can be acceptable?
yes. probably. Hi Ge,
Could we printk the size before and after adding 1 to NR_PCP_LISTS? Does it increase one cacheline?
struct per_cpu_pages {spinlock_t lock; /* Protects lists field */
int count; /* number of pages in the list */
int high; /* high watermark, emptying needed */ int high_min; /* min high watermark */
int high_max; /* max high watermark */
int batch; /* chunk size for buddy add/remove */ u8 flags; /* protected by pcp->lock */
u8 alloc_factor; /* batch scaling factor during allocate */ #ifdef CONFIG_NUMA
u8 expire; /* When 0, remote pagesets are drained */ #endif
short free_count; /* consecutive free count */
/* Lists of pages, one per migrate type stored on the pcp-lists */ struct list_head lists[NR_PCP_LISTS];
} ____cacheline_aligned_in_smp;
Quoted 106 lines · Barry Song
Quoted 78 lines · Baolin Wang
Quoted 60 lines · Barry Song
Quoted 58 lines · Baolin Wang
Quoted 40 lines · On 2024/6/4 20:36, yangge1116 wrote:
Quoted 21 lines · Baolin Wang
Cc Johannes, Zi and Vlastimil.
Quoted 18 lines · On 2024/6/4 17:14, [email protected] wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)Quoted 15 lines
mm/page_alloc.c struct page *rmqueue(struct zone · line 2992
Quoted 11 lines
mm/page_alloc.c +3
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) {Seems you will also miss the non-CMA THP from the PCP, so I wonder if we can add a migratetype comparison in
__rmqueue_pcplist(), and if it's not suitable, then fallback to buddy?Quoted 3 lines
Yes, we may miss some non-CMA THPs in the PCP. But, if add a migratetype comparison in
__rmqueue_pcplist(), we may need to compare many times because of pcp batch.I mean we can only compare once, focusing on CMA pages.
mm/page_alloc.c +0
-0@@ -2973,6 +2973,11 @@ struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, }Quoted 13 lines
mm/page_alloc.c struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, · line 2980
Quoted 7 lines
mm/page_alloc.c +2
-0page = list_first_entry(list, struct page, pcp_list); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order == HPAGE_PMD_ORDER &&!is_migrate_movable(migratetype) &&
mm/page_alloc.c +3
-0+ is_migrate_cma(get_pageblock_migratetype(page))) +return NULL ; +#endif This doesn't seem ideal either. It's possible that the PCP still has many non-CMA folios, but due to bad luck, the first entry is "always" CMA. In this case,
allocations withis_migrate_movable(migratetype)== false will always lose the
chance to use the PCP. It also appears to incur a PCP spin lock/unlock.Quoted 6 lines
Yes, just some ideas to to mitigate the issue...
Quoted 2 lines
I don't see an ideal solution unless we bring back the CMA PCP :-)
Tend to agree, and adding a CMA PCP seems the overhead can be acceptable?
yes. probably. Hi Ge,
Could we printk the size before and after adding 1 to
NR_PCP_LISTS? Does it increase one cacheline?struct
per_cpu_pages{spinlock_tlock; /* Protects lists field */
int count; /* number of pages in the list */
int high; /* high watermark, emptying needed */ inthigh_min; /* min high watermark */
inthigh_max; /* max high watermark */
int batch; /* chunk size for buddy add/remove */ u8 flags; /* protected bypcp->lock*/
u8alloc_factor; /* batch scaling factor during allocate */ #ifdefCONFIG_NUMA
u8 expire; /* When 0, remote pagesets are drained */ #endif
shortfree_count; /* consecutive free count */
Quoted 4 lines
/* Lists of pages, one per migrate type stored on the pcp-lists */ struct
list_headlists[NR_PCP_LISTS];
}____cacheline_aligned_in_smp;
OK.
The size of struct per_cpu_pages is 256 bytes in current code containing commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations"). crash> struct per_cpu_pages
struct per_cpu_pages {
spinlock_t lock;
int count;
int high;
int high_min;
int high_max;
int batch;
u8 flags;
u8 alloc_factor;
u8 expire;
short free_count;
struct list_head lists[13];
}SIZE: 256
After revert commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of struct per_cpu_pages is 272 bytes. crash> struct per_cpu_pages
struct per_cpu_pages {
spinlock_t lock;
int count;
int high;
int high_min;
int high_max;
int batch;
u8 flags;
u8 alloc_factor;
u8 expire;
short free_count;
struct list_head lists[15];
}SIZE: 272
Seems commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.
Quoted 11 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log? Is it possible that some CMA memory might be used by non-movable allocation requests? If so, will CMA somehow become unable to migrate, causing cma_alloc() to fail?
Quoted 13 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +0
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));
mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2997
Quoted 10 lines
mm/page_alloc.c +8
-0if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) { + page =rmqueue_pcplist (preferred_zone, zone, order, + migratetype, alloc_flags); +if (likely (page)) + goto out; + }
This seems not ideal, because non-CMA THP gets no chance to use PCP. But it still seems better than causing the failure of CMA allocation.
Is there a possible approach to avoiding adding CMA THP into pcp from the first beginning? Otherwise, we might need a separate PCP for CMA.
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.
Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call pin_user_pages_remote(..., FOLL_LONGTERM, ...) to pin memory. If a page is in CMA area, pin_user_pages_remote() will migrate the page from CMA area to non-CMA area because of FOLL_LONGTERM flag. If non-movable allocation requests return CMA memory, pin_user_pages_remote() will enter endless loops.
backtrace:pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate ------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocation
Quoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 1 line
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?
No, it will cause endless loops in __gup_longterm_locked(). If non-movable allocation requests return CMA memory, migrate_longterm_unpinnable_pages() will migrate a CMA page to another CMA page, which is useless and cause endless loops in __gup_longterm_locked().
backtrace:pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate ------------migrate_longterm_unpinnable_pages
Quoted 45 lines
Quoted 13 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +0
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2997
Quoted 10 lines
mm/page_alloc.c +8
-0if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (!IS_ENABLED (CONFIG_CMA ) || alloc_flags &ALLOC_CMA || + order !=HPAGE_PMD_ORDER ) { + page =rmqueue_pcplist (preferred_zone, zone, order, + migratetype, alloc_flags); +if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it still seems better than causing the failure of CMA allocation.
Is there a possible approach to avoiding adding CMA THP into pcp from the first beginning? Otherwise, we might need a separate PCP for CMA.
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 42 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 1 line
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 35 lines
Quoted 20 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations")Signed-off-by yangge <[email protected]>---
mm/page_alloc.c| 10 ++++++++++1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone, WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); if (likely(pcp_allowed_order(order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=Quoted 8 lines
HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone, order,
+ migratetype,alloc_flags);
+ if (likely(page))
+ goto out;
+ }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it still seems better than causing the failure of CMA allocation.
Is there a possible approach to avoiding adding CMA THP into pcp from the first beginning? Otherwise, we might need a separate PCP for CMA.
The vast majority of THP-sized allocations are GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.
Commit 1d91df85f399 takes a similar approach to filter, and I mainly refer to it.
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
+#else
page =rmqueue_pcplist(preferred_zone, zone, order, migratetype, alloc_flags); if (likely(page))
goto out;
+#endif
}page = rmqueue_buddy(preferred_zone, zone, order,
alloc_flags,Signature · 1 line
2.7.4Thanks
Barry
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?
Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().
This is only one perspective. We also need to consider the 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 contiguous 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.
Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2997
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.
But the majority of movable pages aren't CMA, right? Do we have an estimate for adding back a CMA THP PCP? Will per_cpu_pages introduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?
[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
Quoted 21 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 125 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2997
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.
Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct per_cpu_pages is 256 bytes in current code containing commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations"). crash> struct per_cpu_pages
struct per_cpu_pages {
spinlock_t lock;
int count;
int high;
int high_min;
int high_max;
int batch;
u8 flags;
u8 alloc_factor;
u8 expire;
short free_count;
struct list_head lists[13];
}SIZE: 256
After revert commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of struct per_cpu_pages is 272 bytes. crash> struct per_cpu_pages
struct per_cpu_pages {
spinlock_t lock;
int count;
int high;
int high_min;
int high_max;
int batch;
u8 flags;
u8 alloc_factor;
u8 expire;
short free_count;
struct list_head lists[15];
}SIZE: 272
Seems commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.
Quoted 22 lines
Quoted 22 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 173 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 125 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2997
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct
per_cpu_pagesis 256 bytes in current code containing commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes. crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }
Quoted 4 lines
SIZE: 272
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.
the proposal is not reverting the patch but adding one CMA pcp. so it is "struct list_head lists[14]"; in this case, the size is still 256?
Quoted 24 lines
Quoted 23 lines
Quoted 23 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 176 lines · Barry Song
Quoted 175 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 125 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct
per_cpu_pagesis 256 bytes in current code containing commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes. crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }SIZE: 272
Quoted 7 lines
Quoted 2 lines
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?
Yes, if only add one CMA pcp, the size is still 256. Seems adding one CMA pcp is more reasonable.
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 24 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 176 lines · Barry Song
Quoted 175 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 125 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct
per_cpu_pagesis 256 bytes in current code containing commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes. crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }SIZE: 272
Quoted 7 lines
Quoted 2 lines
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?
Yes, the size is still 256. If add one PCP list, we will have 2 PCP lists for THP. One PCP list is used by MIGRATE_UNMOVABLE, and the other PCP list is used by MIGRATE_MOVABLE and MIGRATE_RECLAIMABLE. Is that right?
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 24 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 183 lines · On Tue, Jun 18, 2024 at 6:56 PM yangge1116 <[email protected]> wrote:
Quoted 179 lines · Barry Song
Quoted 177 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 125 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct
per_cpu_pagesis 256 bytes in current code containing commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes. crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }SIZE: 272
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.
Quoted 8 lines
Quoted 4 lines
the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?Yes, the size is still 256. If add one PCP list, we will have 2 PCP lists for THP. One PCP list is used by
MIGRATE_UNMOVABLE, and the other PCP list is used byMIGRATE_MOVABLEandMIGRATE_RECLAIMABLE. Is that right?
i am not quite sure about MIGRATE_RECLAIMABLE as we want to CMA is only used by movable.
So it might be:
MOVABLE and NON-MOVABLE.
Quoted 27 lines
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 25 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 189 lines · Barry Song
Quoted 188 lines · On Tue, Jun 18, 2024 at 6:56 PM yangge1116 <[email protected]> wrote:
Quoted 183 lines · Barry Song
Quoted 173 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 125 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 47 lines
Quoted 47 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 34 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct
per_cpu_pagesis 256 bytes in current code containing commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes. crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }Quoted 4 lines
SIZE: 272
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?
Quoted 8 lines
Quoted 3 lines
Yes, the size is still 256. If add one PCP list, we will have 2 PCP lists for THP. One PCP list is used by
MIGRATE_UNMOVABLE, and the other PCP list is used byMIGRATE_MOVABLEandMIGRATE_RECLAIMABLE. Is that right?i am not quite sure about
MIGRATE_RECLAIMABLEas we want to CMA is only used by movable.
So it might be:
MOVABLE and NON-MOVABLE.
One PCP list is used by UNMOVABLE pages, and the other PCP list is used by MOVABLE pages, seems it is feasible. UNMOVABLE PCP list contains MIGRATE_UNMOVABLE pages and MIGRATE_RECLAIMABLE pages, and MOVABLE PCP list contains MIGRATE_MOVABLE pages.
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 26 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry
Quoted 200 lines · 在 2024/6/18 15:51, yangge1116 写道:
Quoted 197 lines · Barry Song
Quoted 195 lines · On Tue, Jun 18, 2024 at 6:56 PM yangge1116 <[email protected]> wrote:
Quoted 180 lines · Barry Song
Quoted 179 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 129 lines · Barry Song
Quoted 52 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 48 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue
to migrate ------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 50 lines
Quoted 50 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue
to migrate ------------migrate_longterm_unpinnable_pagesQuoted 36 lines
Quoted 17 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>---
mm/page_alloc.c| 10 ++++++++++1 file changed, 10 insertions(+)mm/page_alloc.c +0
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE((gfp_flags &
__GFP_NOFAIL) && (orderQuoted 1 line
1));
Quoted 14 lines
if (likely(pcp_allowed_order(order))) { +#ifdef
CONFIG_TRANSPARENT_HUGEPAGE
+ if (!IS_ENABLED(CONFIG_CMA) ||alloc_flags&ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);
+ if (likely(page))
+ goto out;
+ }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.Quoted 8 lines
But the majority of movable pages aren't CMA, right?
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
The size of struct
per_cpu_pagesis 256 bytes in current code containing commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations").
crash> structper_cpu_pages
structper_cpu_pages{
spinlock_tlock;
int count;
int high;
inthigh_min;
inthigh_max;
int batch;
u8 flags;
u8alloc_factor;
u8 expire;
shortfree_count;
structlist_headlists[13];
}
SIZE: 256After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes. crash> structper_cpu_pages
structper_cpu_pages{
spinlock_tlock;
int count;
int high;
inthigh_min;
inthigh_max;
int batch;
u8 flags;
u8alloc_factor;
u8 expire;
shortfree_count;
structlist_headlists[15];
}
SIZE: 272Quoted 7 lines
Quoted 2 lines
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?Yes, the size is still 256. If add one PCP list, we will have 2 PCP lists for THP. One PCP list is used by
MIGRATE_UNMOVABLE, and the other PCP list is used byMIGRATE_MOVABLEandMIGRATE_RECLAIMABLE. Is that right?
Quoted 7 lines
Quoted 4 lines
i am not quite sure about
MIGRATE_RECLAIMABLEas we want to CMA is only used by movable.
So it might be:
MOVABLE and NON-MOVABLE.One PCP list is used by UNMOVABLE pages, and the other PCP list is used by MOVABLE pages, seems it is feasible. UNMOVABLE PCP list contains
MIGRATE_UNMOVABLEpages andMIGRATE_RECLAIMABLEpages, and MOVABLE PCP list containsMIGRATE_MOVABLEpages.
Is the following modification feasiable?
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-#define NR_PCP_THP 1
+#define NR_PCP_THP 2
+#define PCP_THP_MOVABLE 0
+#define PCP_THP_UNMOVABLE 1
#else
#define NR_PCP_THP 0
#endif
static inline unsigned int order_to_pindex(int migratetype, int order) {
+ int pcp_type = migratetype;
+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
if (order > PAGE_ALLOC_COSTLY_ORDER) {
VM_BUG_ON(order != HPAGE_PMD_ORDER);- return NR_LOWORDER_PCP_LISTS;
+
+ if (migratetype != MIGRATE_MOVABLE)
+ pcp_type = PCP_THP_UNMOVABLE;
+ else
+ pcp_type = PCP_THP_MOVABLE;
+
+ return NR_LOWORDER_PCP_LISTS + pcp_type;
}
#else
VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);#endif
- return (MIGRATE_PCPTYPES * order) + migratetype;
+ return (MIGRATE_PCPTYPES * order) + pcp_type;
}
@@ -521,7 +529,7 @@ static inline int pindex_to_order(unsigned int pindex)
int order = pindex / MIGRATE_PCPTYPES;
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
- if (pindex == NR_LOWORDER_PCP_LISTS)
+ if (order > PAGE_ALLOC_COSTLY_ORDER)
order = HPAGE_PMD_ORDER;
#else
VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 26 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
+#else
page =rmqueue_pcplist(preferred_zone, zone, order, migratetype, alloc_flags); if (likely(page))
goto out;
+#endif
}page = rmqueue_buddy(preferred_zone, zone, order,
alloc_flags,Signature · 1 line
2.7.4Thanks
Barry
Quoted 263 lines · Ge Yang
Quoted 228 lines · 在 2024/6/18 15:51, yangge1116 写道:
Quoted 220 lines · Barry Song
Quoted 214 lines · On Tue, Jun 18, 2024 at 6:56 PM yangge1116 <[email protected]> wrote:
Quoted 202 lines · Barry Song
Quoted 201 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 146 lines · Barry Song
Quoted 63 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 59 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 14 lines · Barry Song
Quoted 13 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from
the list, in some cases, it's not acceptable, for example, allocating
a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 5 lines
Quoted 3 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized
PCP list to avoid the issue above.Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting
virtual machine with device passthrough will get stuck.During starting virtual machine, it will call pin_user_pages_remote(...,
FOLL_LONGTERM, ...) to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA
area because ofFOLL_LONGTERMflag. If non-movable allocation requests
return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue
to migrate ------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 3 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()
to fail?Quoted 6 lines
Quoted 6 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another
CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 51 lines
Quoted 51 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue
to migrate ------------migrate_longterm_unpinnable_pagesQuoted 37 lines
Quoted 28 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone,
zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP.
But it
still seems better than causing the failure of CMA allocation.Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Is there a possible approach to avoiding adding CMA THP into pcp from
the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.But the majority of movable pages aren't CMA, right?
Quoted 9 lines
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which
the original intention for THP was to avoid by having only one PCP[1]?[1]
https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/The size of struct
per_cpu_pagesis 256 bytes in current code containing
commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized
allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list
for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes.
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }SIZE: 272
Quoted 7 lines
Quoted 2 lines
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?Quoted 4 lines
Yes, the size is still 256. If add one PCP list, we will have 2 PCP lists for THP. One PCP list is used by
MIGRATE_UNMOVABLE, and the other PCP list is used byMIGRATE_MOVABLEandMIGRATE_RECLAIMABLE. Is that right?Quoted 4 lines
i am not quite sure about
MIGRATE_RECLAIMABLEas we want to CMA is only used by movable.
So it might be:
MOVABLE and NON-MOVABLE.Quoted 5 lines
One PCP list is used by UNMOVABLE pages, and the other PCP list is used by MOVABLE pages, seems it is feasible. UNMOVABLE PCP list contains
MIGRATE_UNMOVABLEpages andMIGRATE_RECLAIMABLEpages, and MOVABLE PCP list containsMIGRATE_MOVABLEpages.Is the following modification feasiable?
#ifdef
CONFIG_TRANSPARENT_HUGEPAGE(quoted diff) +10
-2-#define NR_PCP_THP 1 +#define NR_PCP_THP 2 +#define PCP_THP_MOVABLE 0 +#define PCP_THP_UNMOVABLE 1 #else #define NR_PCP_THP 0 #endif static inline unsigned int order_to_pindex(int migratetype, int order) { + int pcp_type = migratetype; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE if (order > PAGE_ALLOC_COSTLY_ORDER) { VM_BUG_ON(order != HPAGE_PMD_ORDER); - return NR_LOWORDER_PCP_LISTS; + + if (migratetype != MIGRATE_MOVABLE) + pcp_type = PCP_THP_UNMOVABLE; + else + pcp_type = PCP_THP_MOVABLE;
Review
Quoted 10 lines
(quoted diff) +3
-1+ + return NR_LOWORDER_PCP_LISTS + pcp_type; } #else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); #endif - return (MIGRATE_PCPTYPES * order) + migratetype; + return (MIGRATE_PCPTYPES * order) + pcp_type; }
a minimum change might be, then you can drop most new code.
mm/page_alloc.c +1 -0
@@ -588,6 +588,7 @@ static void bad_page(struct page *page, const char *reason)static inline unsigned int order_to_pindex (int migratetype,int order) { +bool __maybe_unused movable;#ifdef CONFIG_CMA /* * We shouldn't get here for MIGRATE_CMA if those pages don't
@@ -600,7 +601,8 @@ static inline unsigned int order_to_pindex(int
migratetype, int order)
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
if (order > PAGE_ALLOC_COSTLY_ORDER) {
VM_BUG_ON(order != pageblock_order);
- return NR_LOWORDER_PCP_LISTS;
+ movable = migratetype == MIGRATE_MOVABLE;
+ return NR_LOWORDER_PCP_LISTS + movable;
}
#else
VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);Quoted 46 lines
(quoted diff) +1
-1@@ -521,7 +529,7 @@ static inline int pindex_to_order(unsigned int pindex) int order = pindex / MIGRATE_PCPTYPES; #ifdef CONFIG_TRANSPARENT_HUGEPAGE - if (pindex == NR_LOWORDER_PCP_LISTS) + if (order > PAGE_ALLOC_COSTLY_ORDER) order = HPAGE_PMD_ORDER; #else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 32 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
+#else
page = rmqueue_pcplist(preferred_zone,
zone, order,
migratetype,alloc_flags); if (likely(page)) goto out;Quoted 7 lines
+#endif
}page = rmqueue_buddy(preferred_zone, zone, order,
alloc_flags,
--
2.7.4Thanks
Barry
Quoted 270 lines · Barry Song
Quoted 269 lines · Ge Yang
Quoted 228 lines · 在 2024/6/18 15:51, yangge1116 写道:
Quoted 220 lines · Barry Song
Quoted 214 lines · On Tue, Jun 18, 2024 at 6:56 PM yangge1116 <[email protected]> wrote:
Quoted 202 lines · Barry Song
Quoted 201 lines · On Tue, Jun 18, 2024 at 3:32 PM yangge1116 <[email protected]> wrote:
Quoted 146 lines · Barry Song
Quoted 63 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 59 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 14 lines · Barry Song
Quoted 13 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from
the list, in some cases, it's not acceptable, for example, allocating
a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 5 lines
Quoted 3 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized
PCP list to avoid the issue above.Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting
virtual machine with device passthrough will get stuck.During starting virtual machine, it will call pin_user_pages_remote(...,
FOLL_LONGTERM, ...) to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA
area because ofFOLL_LONGTERMflag. If non-movable allocation requests
return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue
to migrate ------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 3 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()
to fail?Quoted 6 lines
Quoted 6 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another
CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 51 lines
Quoted 51 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue
to migrate ------------migrate_longterm_unpinnable_pagesQuoted 37 lines
Quoted 28 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone,
zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP.
But it
still seems better than causing the failure of CMA allocation.Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Is there a possible approach to avoiding adding CMA THP into pcp from
the first
beginning? Otherwise, we might need a separate PCP for CMA.The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.But the majority of movable pages aren't CMA, right?
Quoted 9 lines
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which
the original intention for THP was to avoid by having only one PCP[1]?[1]
https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/The size of struct
per_cpu_pagesis 256 bytes in current code containing
commit5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized
allocations").
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[13]; }SIZE: 256
After revert commit
5d0a661d808f("mm/page_alloc: use only one PCP list
for THP-sized allocations"), the size of structper_cpu_pagesis 272 bytes.
crash> structper_cpu_pagesstruct per_cpu_pages { spinlock_t lock; int count; int high; int high_min; int high_max; int batch; u8 flags; u8 alloc_factor; u8 expire; short free_count; struct list_head lists[15]; }SIZE: 272
Quoted 7 lines
Quoted 2 lines
Seems commit
5d0a661d808f("mm/page_alloc: use only one PCP list for THP-sized allocations") decrease one cacheline.the proposal is not reverting the patch but adding one CMA pcp. so it is "struct
list_headlists[14]"; in this case, the size is still 256?Quoted 4 lines
Yes, the size is still 256. If add one PCP list, we will have 2 PCP lists for THP. One PCP list is used by
MIGRATE_UNMOVABLE, and the other PCP list is used byMIGRATE_MOVABLEandMIGRATE_RECLAIMABLE. Is that right?Quoted 4 lines
i am not quite sure about
MIGRATE_RECLAIMABLEas we want to CMA is only used by movable.
So it might be:
MOVABLE and NON-MOVABLE.Quoted 5 lines
One PCP list is used by UNMOVABLE pages, and the other PCP list is used by MOVABLE pages, seems it is feasible. UNMOVABLE PCP list contains
MIGRATE_UNMOVABLEpages andMIGRATE_RECLAIMABLEpages, and MOVABLE PCP list containsMIGRATE_MOVABLEpages.Is the following modification feasiable?
#ifdef
CONFIG_TRANSPARENT_HUGEPAGE(quoted diff) +12
-2-#define NR_PCP_THP 1 +#define NR_PCP_THP 2 +#define PCP_THP_MOVABLE 0 +#define PCP_THP_UNMOVABLE 1 #else #define NR_PCP_THP 0 #endif static inline unsigned int order_to_pindex(int migratetype, int order) { + int pcp_type = migratetype; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE if (order > PAGE_ALLOC_COSTLY_ORDER) { VM_BUG_ON(order != HPAGE_PMD_ORDER); - return NR_LOWORDER_PCP_LISTS; + + if (migratetype != MIGRATE_MOVABLE) + pcp_type = PCP_THP_UNMOVABLE; + else + pcp_type = PCP_THP_MOVABLE; + + return NR_LOWORDER_PCP_LISTS + pcp_type; } #else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); #endif
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +1
-1- return (MIGRATE_PCPTYPES * order) + migratetype; + return (MIGRATE_PCPTYPES * order) + pcp_type; }a minimum change might be, then you can drop most new code.
Thanks, I will prepare the V2.
Quoted 71 lines
mm/page_alloc.c +3
-1@@ -588,6 +588,7 @@ static void bad_page(struct page *page, const char *reason)static inline unsigned int order_to_pindex (int migratetype,int order) { +bool __maybe_unused movable;#ifdef CONFIG_CMA /* * We shouldn't get here for MIGRATE_CMA if those pages don't @@ -600,7 +601,8 @@ static inline unsigned int order_to_pindex(int migratetype, int order)#ifdef CONFIG_TRANSPARENT_HUGEPAGE if (order >PAGE_ALLOC_COSTLY_ORDER ) {VM_BUG_ON (order != pageblock_order); -return NR_LOWORDER_PCP_LISTS ; + movable = migratetype ==MIGRATE_MOVABLE ; +return NR_LOWORDER_PCP_LISTS + movable; }#else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);Quoted 46 lines
(quoted diff) +1
-1@@ -521,7 +529,7 @@ static inline int pindex_to_order(unsigned int pindex) int order = pindex / MIGRATE_PCPTYPES; #ifdef CONFIG_TRANSPARENT_HUGEPAGE - if (pindex == NR_LOWORDER_PCP_LISTS) + if (order > PAGE_ALLOC_COSTLY_ORDER) order = HPAGE_PMD_ORDER; #else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 32 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
+#else
page = rmqueue_pcplist(preferred_zone,
zone, order,
migratetype,alloc_flags); if (likely(page)) goto out;Quoted 7 lines
+#endif
}page = rmqueue_buddy(preferred_zone, zone, order,
alloc_flags,
--
2.7.4Thanks
Barry
Quoted 121 lines · Barry Song
Quoted 51 lines · On Tue, Jun 18, 2024 at 9:36 AM yangge1116 <[email protected]> wrote:
Quoted 47 lines · 在 2024/6/17 下午8:47, yangge1116 写道:
Quoted 10 lines · Barry Song
Quoted 9 lines · On Tue, Jun 4, 2024 at 9:15 PM <[email protected]> wrote:
From: yangge <[email protected]>
Since commit
5d0a661d808f("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 to get a CMA page from the list, in some cases, it's not acceptable, for example, allocating a non-CMA page withPF_MEMALLOC_PINflag returns a CMA page.Quoted 4 lines
Quoted 2 lines
The patch forbids allocating non-CMA THP-sized page from THP-sized PCP list to avoid the issue above.
Could you please describe the impact on users in the commit log?
If a large number of CMA memory are configured in the system (for example, the CMA memory accounts for 50% of the system memory), starting virtual machine with device passthrough will get stuck.
During starting virtual machine, it will call
pin_user_pages_remote(..., FOLL_LONGTERM, ...)to pin memory. If a page is in CMA area,pin_user_pages_remote()will migrate the page from CMA area to non-CMA area because ofFOLL_LONGTERMflag. If non-movable allocation requests return CMA memory,pin_user_pages_remote()will enter endless loops.backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pages ----------------alloc_migration_target // non-movable allocationQuoted 2 lines
Is it possible that some CMA memory might be used by non-movable allocation requests?
Yes.
Quoted 2 lines
If so, will CMA somehow become unable to migrate, causing
cma_alloc()to fail?Quoted 5 lines
Quoted 5 lines
No, it will cause endless loops in
__gup_longterm_locked(). If non-movable allocation requests return CMA memory,migrate_longterm_unpinnable_pages()will migrate a CMA page to another CMA page, which is useless and cause endless loops in__gup_longterm_locked().This is only one perspective. We also need to consider the impact on CMA itself. For example,
when CMA is borrowed by THP, and we need to reclaim it throughcma_alloc()ordma_alloc_coherent(),
we must move those pages out to ensure CMA's users can retrieve that contiguous 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.Quoted 52 lines
Quoted 51 lines
backtrace:
pin_user_pages_remote
----__gup_longterm_locked //cause endless loops in this function --------__get_user_pages_locked --------check_and_migrate_movable_pages //always check fail and continue to migrate
------------migrate_longterm_unpinnable_pagesQuoted 38 lines
Quoted 26 lines
Fixes 5d0a661d808f ("mm/page_alloc: use only one PCP list forTHP-sized allocations")
Signed-off-by yangge <[email protected]>--- mm/page_alloc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/page_alloc.c +2
-0@@ -2987,10 +2987,20 @@ struct page *rmqueue(struct zone *preferred_zone,WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 ));if (likely (pcp_allowed_order (order))) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (!IS_ENABLED(CONFIG_CMA) || alloc_flags &
ALLOC_CMA||
+ order !=HPAGE_PMD_ORDER) {
+ page = rmqueue_pcplist(preferred_zone, zone,
order,
+ migratetype,alloc_flags);mm/page_alloc.c struct page *rmqueue(struct zone *preferred_zone, · line 2997
Quoted 3 lines
mm/page_alloc.c +3
-0+if (likely (page)) + goto out; + }This seems not ideal, because non-CMA THP gets no chance to use PCP. But it
still seems better than causing the failure of CMA allocation.Is there a possible approach to avoiding adding CMA THP into pcp from the first
beginning? Otherwise, we might need a separate PCP for CMA.
Quoted 5 lines
Quoted 3 lines
The vast majority of THP-sized allocations are
GFP_MOVABLE, avoiding adding CMA THP into pcp may incur a slight performance penalty.But the majority of movable pages aren't CMA, right?
Yes.
Quoted 28 lines
Do we have an estimate for
adding back a CMA THP PCP? Willper_cpu_pagesintroduce a new cacheline, which the original intention for THP was to avoid by having only one PCP[1]?[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
Quoted 21 lines
Commit
1d91df85f399takes a similar approach to filter, and I mainly refer to it.Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
mm/page_alloc.c +2
-1+#else page =rmqueue_pcplist (preferred_zone, zone, order, migratetype, alloc_flags);if (likely (page)) goto out; +#endif } page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, --2.7.4
Thanks
Barry