make the hugetlb migration strategy consistent
Subsystems (13)
Baolin Wang
reviewerv2 cover make the hugetlb migration strategy consistent
Hi,
As discussed in previous thread [1], there is an inconsistency when handling hugetlb migration. When handling the migration of freed hugetlb, it prevents fallback to other NUMA nodes in alloc_and_dissolve_hugetlb_folio(). However, when dealing with in-use hugetlb, it allows fallback to other NUMA nodes in alloc_hugetlb_folio_nodemask(), which can break the per-node hugetlb pool and might result in unexpected failures when node bound workloads doesn't get what is asssumed available.
This patch set tries to make the hugetlb migration strategy more clear and consistent. Please find details in each patch.
Changes from v1:
- Add reviewed tag from Oscar.
- Move the gfp_mask modification into a new helper called by alloc_migration_target().
Changes from RFC:
- Move the gfp modification into alloc_migrate_hugetlb_folio().
- Add more comments.
Baolin Wang (3):
mm: record the migration reason for struct migration_target_control
mm: hugetlb: make the hugetlb migration strategy consistent
docs: hugetlbpage.rst: add hugetlb migration description
Documentation/admin-guide/mm/hugetlbpage.rst | 7 ++++ include/linux/hugetlb.h | 35 ++++++++++++++++++-- mm/gup.c | 1 + mm/hugetlb.c | 14 ++++++-- mm/internal.h | 1 + mm/memory-failure.c | 1 + mm/memory_hotplug.c | 1 + mm/mempolicy.c | 4 ++- mm/migrate.c | 4 ++- mm/page_alloc.c | 1 + mm/vmscan.c | 3 +- 11 files changed, 65 insertions(+), 7 deletions(-)
Signature · 1 line
2.39.3
Baolin Wang
reviewerPATCH v2 1/3 mm: record the migration reason for struct migration_target_control
To support different hugetlb allocation strategies during hugetlb migration based on various migration reasons, record the migration reason in the migration_target_control structure as a preparation.
--- mm/gup.c | 1 + mm/internal.h | 1 + mm/memory-failure.c | 1 + mm/memory_hotplug.c | 1 + mm/mempolicy.c | 1 + mm/migrate.c | 1 + mm/page_alloc.c | 1 + mm/vmscan.c | 3 ++- 8 files changed, 9 insertions(+), 1 deletion(-)
mm/gup.c +1 -0
@@ -2132,6 +2132,7 @@ static int migrate_longterm_unpinnable_pages(struct migration_target_control mtc = { .nid =NUMA_NO_NODE , .gfp_mask =GFP_USER | __GFP_NOWARN, + .reason =MR_LONGTERM_PIN , }; if (migrate_pages(movable_page_list, alloc_migration_target,
mm/internal.h +1 -0
@@ -959,6 +959,7 @@ struct migration_target_control {int nid;/* preferred node id */ nodemask_t *nmask;gfp_t gfp_mask; +enum migrate_reason reason; };/*
mm/memory-failure.c +1 -0
@@ -2666,6 +2666,7 @@ static int soft_offline_in_use_page(struct page *page)struct migration_target_control mtc = { .nid =NUMA_NO_NODE , .gfp_mask =GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, + .reason =MR_MEMORY_FAILURE , };if (!huge &&folio_test_large (folio)) {
mm/memory_hotplug.c +1 -0
@@ -1841,6 +1841,7 @@ static void do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)struct migration_target_control mtc = { .nmask = &nmask, .gfp_mask =GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, + .reason =MR_MEMORY_HOTPLUG , };int ret;
mm/mempolicy.c +1 -0
@@ -1070,6 +1070,7 @@ static long migrate_to_node(struct mm_struct *mm, int source, int dest,struct migration_target_control mtc = { .nid = dest, .gfp_mask =GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, + .reason =MR_SYSCALL , };nodes_clear (nmask);
mm/migrate.c +1 -0
@@ -2060,6 +2060,7 @@ static int do_move_pages_to_node(struct list_head *pagelist, int node)struct migration_target_control mtc = { .nid = node, .gfp_mask =GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, + .reason =MR_SYSCALL , }; err = migrate_pages(pagelist, alloc_migration_target, NULL,
mm/page_alloc.c +1 -0
@@ -6266,6 +6266,7 @@ int __alloc_contig_migrate_range(struct compact_control *cc,struct migration_target_control mtc = { .nid =zone_to_nid (cc->zone), .gfp_mask =GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, + .reason =MR_CONTIG_RANGE , };lru_cache_disable ();
mm/vmscan.c +2 -1
@@ -978,7 +978,8 @@ static unsigned int demote_folio_list(struct list_head *demote_folios, .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | __GFP_NOWARN | __GFP_NOMEMALLOC | GFP_NOWAIT, .nid = target_nid, - .nmask = &allowed_mask + .nmask = &allowed_mask, + .reason = MR_DEMOTION, };if (list_empty (demote_folios))
Signature · 1 line
2.39.3
Baolin Wang
reviewerPATCH v2 2/3 mm: hugetlb: make the hugetlb migration strategy consistent
In reply to Baolin WangAs discussed in previous thread [1], there is an inconsistency when handing hugetlb migration. When handling the migration of freed hugetlb, it prevents fallback to other NUMA nodes in alloc_and_dissolve_hugetlb_folio(). However, when dealing with in-use hugetlb, it allows fallback to other NUMA nodes in alloc_hugetlb_folio_nodemask(), which can break the per-node hugetlb pool and might result in unexpected failures when node bound workloads doesn't get what is asssumed available.
To make hugetlb migration strategy more clear, we should list all the scenarios of hugetlb migration and analyze whether allocation fallback is permitted:
1) Memory offline: will call dissolve_free_huge_pages() to free the freed hugetlb, and call do_migrate_range() to migrate the in-use hugetlb. Both can break the per-node hugetlb pool, but as this is an explicit offlining operation, no better choice. So should allow the hugetlb allocation fallback.
2) Memory failure: same as memory offline. Should allow fallback to a different node might be the only option to handle it, otherwise the impact of poisoned memory can be amplified.
3) Longterm pinning: will call migrate_longterm_unpinnable_pages() to migrate in-use and not-longterm-pinnable hugetlb, which can break the per-node pool. But we should fail to longterm pinning if can not allocate on current node to avoid breaking the per-node pool.
4) Syscalls (mbind, migrate_pages, move_pages): these are explicit users operation to move pages to other nodes, so fallback to other nodes should not be prohibited.
5) alloc_contig_range: used by CMA allocation and virtio-mem fake-offline to allocate given range of pages. Now the freed hugetlb migration is not allowed to fallback, to keep consistency, the in-use hugetlb migration should be also not allowed to fallback.
6) alloc_contig_pages: used by kfence, pgtable_debug etc. The strategy should be consistent with that of alloc_contig_range().
Based on the analysis of the various scenarios above, introducing a new helper to determine whether fallback is permitted according to the migration reason..
--- include/linux/hugetlb.h | 35 +++++++++++++++++++++++++++++++++-- mm/hugetlb.c | 14 ++++++++++++-- mm/mempolicy.c | 3 ++- mm/migrate.c | 3 ++- 4 files changed, 49 insertions(+), 6 deletions(-)
include/linux/hugetlb.h +33 -2
@@ -747,7 +747,8 @@ int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list);struct folio *alloc_hugetlb_folio (struct vm_area_struct *vma,unsigned long addr,int avoid_reserve);struct folio *alloc_hugetlb_folio_nodemask (struct hstate *h,int preferred_nid, -nodemask_t *nmask,gfp_t gfp_mask); +nodemask_t *nmask,gfp_t gfp_mask, +bool allow_alloc_fallback);int hugetlb_add_to_page_cache (struct folio *folio,struct address_space *mapping,pgoff_t idx); void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
@@ -970,6 +971,30 @@ static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)return modified_mask; } +static inline bool htlb_allow_alloc_fallback (int reason) +{ +bool allowed_fallback = false; + +/* +* Note: the memory offline, memory failure and migration syscalls will +* be allowed to fallback to other nodes due to lack of a better chioce, +* that might break the per-node hugetlb pool. While other cases will +* set the __GFP_THISNODE to avoid breaking the per-node hugetlb pool. +*/ +switch (reason) { +case MR_MEMORY_HOTPLUG : +case MR_MEMORY_FAILURE : +case MR_SYSCALL : +case MR_MEMPOLICY_MBIND : + allowed_fallback = true; +break ; +default : +break ; + } + +return allowed_fallback; +} +static inline spinlock_t *huge_pte_lockptr (struct hstate *h,struct mm_struct *mm,pte_t *pte) {
@@ -1065,7 +1090,8 @@ static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,static inline struct folio *alloc_hugetlb_folio_nodemask (struct hstate *h,int preferred_nid, -nodemask_t *nmask,gfp_t gfp_mask) +nodemask_t *nmask,gfp_t gfp_mask, +bool allow_alloc_fallback) {return NULL ; }
@@ -1181,6 +1207,11 @@ static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)return 0 ; } +static inline bool htlb_allow_alloc_fallback (int reason) +{ +return false; +} +static inline spinlock_t *huge_pte_lockptr (struct hstate *h,struct mm_struct *mm,pte_t *pte) {
mm/hugetlb.c +12 -2
@@ -2621,7 +2621,7 @@ struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h,/* folio migration callback function */ struct folio *alloc_hugetlb_folio_nodemask (struct hstate *h,int preferred_nid, -nodemask_t *nmask,gfp_t gfp_mask) +nodemask_t *nmask,gfp_t gfp_mask,bool allow_alloc_fallback) {spin_lock_irq (&hugetlb_lock);if (available_huge_pages (h)) {
@@ -2636,6 +2636,10 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, }spin_unlock_irq (&hugetlb_lock); +/* We cannot fallback to other nodes, as we could break the per-node pool. */ +if (!allow_alloc_fallback) + gfp_mask |= __GFP_THISNODE; +return alloc_migrate_hugetlb_folio (h, gfp_mask, preferred_nid, nmask); }
@@ -6653,7 +6657,13 @@ static struct folio *alloc_hugetlb_folio_vma(struct hstate *h, gfp_mask =htlb_alloc_mask (h); node =huge_node (vma, address, gfp_mask, &mpol, &nodemask); - folio =alloc_hugetlb_folio_nodemask (h, node, nodemask, gfp_mask); +/* +* This is used to allocate a temporary hugetlb to hold the copied +* content, which will then be copied again to the final hugetlb +* consuming a reservation. Set the alloc_fallback to false to indicate +* that breaking the per-node hugetlb pool is not allowed in this case. +*/ + folio =alloc_hugetlb_folio_nodemask (h, node, nodemask, gfp_mask, false);mpol_cond_put (mpol);return folio;
mm/mempolicy.c +2 -1
@@ -1228,7 +1228,8 @@ static struct folio *alloc_migration_target_by_mpol(struct folio *src, h =folio_hstate (src); gfp =htlb_alloc_mask (h); nodemask =policy_nodemask (gfp, pol, ilx, &nid); -return alloc_hugetlb_folio_nodemask (h, nid, nodemask, gfp); +return alloc_hugetlb_folio_nodemask (h, nid, nodemask, gfp, +htlb_allow_alloc_fallback (MR_MEMPOLICY_MBIND )); }if (folio_test_large (src))
mm/migrate.c +2 -1
@@ -2022,7 +2022,8 @@ struct folio *alloc_migration_target(struct folio *src, unsigned long private) gfp_mask =htlb_modify_alloc_mask (h, gfp_mask);return alloc_hugetlb_folio_nodemask (h, nid, - mtc->nmask, gfp_mask); + mtc->nmask, gfp_mask, +htlb_allow_alloc_fallback (mtc->reason)); }if (folio_test_large (src)) {
Signature · 1 line
2.39.3
Baolin Wang
reviewerPATCH v2 3/3 docs: hugetlbpage.rst: add hugetlb migration description
In reply to Baolin WangAdd some description of the hugetlb migration strategy.
--- Documentation/admin-guide/mm/hugetlbpage.rst | 7 +++++++ 1 file changed, 7 insertions(+)
Documentation/admin-guide/mm/hugetlbpage.rst +7 -0
@@ -376,6 +376,13 @@ Note that the number of overcommit and reserve pages remain global quantities, as we don't know until fault time, when the faulting task's mempolicy is applied, from which node the huge page allocation will be attempted. +The hugetlb may be migrated between the per-node hugepages pool in the following +scenarios: memory offline, memory failure, longterm pinning, syscalls(mbind, +migrate_pages and move_pages), alloc_contig_range() and alloc_contig_pages(). +Now only memory offline, memory failure and syscalls allow fallbacking to allocate +a new hugetlb on a different node if the current node is unable to allocate during +hugetlb migration, that means these 3 cases can break the per-node hugepages pool. + .. _using_huge_pages: Using Huge Pages
Signature · 1 line
2.39.3
Re: [PATCH v2 2/3] mm: hugetlb: make the hugetlb migration strategy consistent
In reply to Baolin WangQuoted 29 lines · Baolin Wang
As discussed in previous thread [1], there is an inconsistency when handing hugetlb migration. When handling the migration of freed hugetlb, it prevents fallback to other NUMA nodes in
alloc_and_dissolve_hugetlb_folio(). However, when dealing with in-use hugetlb, it allows fallback to other NUMA nodes inalloc_hugetlb_folio_nodemask(), which can break the per-node hugetlb pool and might result in unexpected failures when node bound workloads doesn't get what is asssumed available.To make hugetlb migration strategy more clear, we should list all the scenarios of hugetlb migration and analyze whether allocation fallback is permitted:
1) Memory offline: will calldissolve_free_huge_pages()to free the freed hugetlb, and calldo_migrate_range()to migrate the in-use hugetlb. Both can break the per-node hugetlb pool, but as this is an explicit offlining operation, no better choice. So should allow the hugetlb allocation fallback.
2) Memory failure: same as memory offline. Should allow fallback to a different node might be the only option to handle it, otherwise the impact of poisoned memory can be amplified.
3) Longterm pinning: will callmigrate_longterm_unpinnable_pages()to migrate in-use and not-longterm-pinnable hugetlb, which can break the per-node pool. But we should fail to longterm pinning if can not allocate on current node to avoid breaking the per-node pool.
4) Syscalls (mbind,migrate_pages,move_pages): these are explicit users operation to move pages to other nodes, so fallback to other nodes should not be prohibited.
5)alloc_contig_range: used by CMA allocation and virtio-mem fake-offline to allocate given range of pages. Now the freed hugetlb migration is not allowed to fallback, to keep consistency, the in-use hugetlb migration should be also not allowed to fallback.
6)alloc_contig_pages: used by kfence,pgtable_debugetc. The strategy should be consistent with that ofalloc_contig_range().
Quoted 5 lines
Based on the analysis of the various scenarios above, introducing a new helper to determine whether fallback is permitted according to the migration reason..
Signed-off-by Baolin Wang <[email protected]>
include/linux/hugetlb.h static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) · line 980
Quoted 7 lines
include/linux/hugetlb.h +7
-0+static inline bool htlb_allow_alloc_fallback (int reason) +{ +bool allowed_fallback = false; + +/* +* Note: the memory offline, memory failure and migration syscalls will +* be allowed to fallback to other nodes due to lack of a better chioce,
^ choice
Signature · 2 lines
Oscar Salvador SUSE Labs