Skip to content

Commit 003af99

Browse files
aristeuakpm00
authored andcommitted
hugetlb: force allocating surplus hugepages on mempolicy allowed nodes
When trying to allocate a hugepage with no reserved ones free, it may be allowed in case a number of overcommit hugepages was configured (using /proc/sys/vm/nr_overcommit_hugepages) and that number wasn't reached. This allows for a behavior of having extra hugepages allocated dynamically, if there're resources for it. Some sysadmins even prefer not reserving any hugepages and setting a big number of overcommit hugepages. But while attempting to allocate overcommit hugepages in a multi node system (either NUMA or mempolicy/cpuset) said allocations might randomly fail even when there're resources available for the allocation. This happens due to allowed_mems_nr() only accounting for the number of free hugepages in the nodes the current process belongs to and the surplus hugepage allocation is done so it can be allocated in any node. In case one or more of the requested surplus hugepages are allocated in a different node, the whole allocation will fail due allowed_mems_nr() returning a lower value. So allocate surplus hugepages in one of the nodes the current process belongs to. Easy way to reproduce this issue is to use a 2+ NUMA nodes system: # echo 0 >/proc/sys/vm/nr_hugepages # echo 1 >/proc/sys/vm/nr_overcommit_hugepages # numactl -m0 ./tools/testing/selftests/mm/map_hugetlb 2 Repeating the execution of map_hugetlb test application will eventually fail when the hugepage ends up allocated in a different node. [[email protected]: v2] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Aristeu Rozanski <[email protected]> Cc: Muchun Song <[email protected]> Cc: Aristeu Rozanski <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Vishal Moola <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 64548bc commit 003af99

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

mm/hugetlb.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,6 +2620,23 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
26202620
return alloc_migrate_hugetlb_folio(h, gfp_mask, preferred_nid, nmask);
26212621
}
26222622

2623+
static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
2624+
{
2625+
#ifdef CONFIG_NUMA
2626+
struct mempolicy *mpol = get_task_policy(current);
2627+
2628+
/*
2629+
* Only enforce MPOL_BIND policy which overlaps with cpuset policy
2630+
* (from policy_nodemask) specifically for hugetlb case
2631+
*/
2632+
if (mpol->mode == MPOL_BIND &&
2633+
(apply_policy_zone(mpol, gfp_zone(gfp)) &&
2634+
cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
2635+
return &mpol->nodes;
2636+
#endif
2637+
return NULL;
2638+
}
2639+
26232640
/*
26242641
* Increase the hugetlb pool such that it can accommodate a reservation
26252642
* of size 'delta'.
@@ -2633,6 +2650,8 @@ static int gather_surplus_pages(struct hstate *h, long delta)
26332650
long i;
26342651
long needed, allocated;
26352652
bool alloc_ok = true;
2653+
int node;
2654+
nodemask_t *mbind_nodemask = policy_mbind_nodemask(htlb_alloc_mask(h));
26362655

26372656
lockdep_assert_held(&hugetlb_lock);
26382657
needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
@@ -2647,8 +2666,15 @@ static int gather_surplus_pages(struct hstate *h, long delta)
26472666
retry:
26482667
spin_unlock_irq(&hugetlb_lock);
26492668
for (i = 0; i < needed; i++) {
2650-
folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
2651-
NUMA_NO_NODE, NULL);
2669+
folio = NULL;
2670+
for_each_node_mask(node, cpuset_current_mems_allowed) {
2671+
if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) {
2672+
folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
2673+
node, NULL);
2674+
if (folio)
2675+
break;
2676+
}
2677+
}
26522678
if (!folio) {
26532679
alloc_ok = false;
26542680
break;
@@ -4878,23 +4904,6 @@ static int __init default_hugepagesz_setup(char *s)
48784904
}
48794905
__setup("default_hugepagesz=", default_hugepagesz_setup);
48804906

4881-
static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
4882-
{
4883-
#ifdef CONFIG_NUMA
4884-
struct mempolicy *mpol = get_task_policy(current);
4885-
4886-
/*
4887-
* Only enforce MPOL_BIND policy which overlaps with cpuset policy
4888-
* (from policy_nodemask) specifically for hugetlb case
4889-
*/
4890-
if (mpol->mode == MPOL_BIND &&
4891-
(apply_policy_zone(mpol, gfp_zone(gfp)) &&
4892-
cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
4893-
return &mpol->nodes;
4894-
#endif
4895-
return NULL;
4896-
}
4897-
48984907
static unsigned int allowed_mems_nr(struct hstate *h)
48994908
{
49004909
int node;

0 commit comments

Comments
 (0)