Skip to content

Commit 1597339

Browse files
aristeugregkh
authored andcommitted
hugetlb: force allocating surplus hugepages on mempolicy allowed nodes
commit 003af99 upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9f5d352 commit 1597339

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

mm/hugetlb.c

+28-19
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,23 @@ struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_struct *v
25182518
return folio;
25192519
}
25202520

2521+
static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
2522+
{
2523+
#ifdef CONFIG_NUMA
2524+
struct mempolicy *mpol = get_task_policy(current);
2525+
2526+
/*
2527+
* Only enforce MPOL_BIND policy which overlaps with cpuset policy
2528+
* (from policy_nodemask) specifically for hugetlb case
2529+
*/
2530+
if (mpol->mode == MPOL_BIND &&
2531+
(apply_policy_zone(mpol, gfp_zone(gfp)) &&
2532+
cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
2533+
return &mpol->nodes;
2534+
#endif
2535+
return NULL;
2536+
}
2537+
25212538
/*
25222539
* Increase the hugetlb pool such that it can accommodate a reservation
25232540
* of size 'delta'.
@@ -2531,6 +2548,8 @@ static int gather_surplus_pages(struct hstate *h, long delta)
25312548
long i;
25322549
long needed, allocated;
25332550
bool alloc_ok = true;
2551+
int node;
2552+
nodemask_t *mbind_nodemask = policy_mbind_nodemask(htlb_alloc_mask(h));
25342553

25352554
lockdep_assert_held(&hugetlb_lock);
25362555
needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
@@ -2545,8 +2564,15 @@ static int gather_surplus_pages(struct hstate *h, long delta)
25452564
retry:
25462565
spin_unlock_irq(&hugetlb_lock);
25472566
for (i = 0; i < needed; i++) {
2548-
folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
2549-
NUMA_NO_NODE, NULL);
2567+
folio = NULL;
2568+
for_each_node_mask(node, cpuset_current_mems_allowed) {
2569+
if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) {
2570+
folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
2571+
node, NULL);
2572+
if (folio)
2573+
break;
2574+
}
2575+
}
25502576
if (!folio) {
25512577
alloc_ok = false;
25522578
break;
@@ -4531,23 +4557,6 @@ static int __init default_hugepagesz_setup(char *s)
45314557
}
45324558
__setup("default_hugepagesz=", default_hugepagesz_setup);
45334559

4534-
static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
4535-
{
4536-
#ifdef CONFIG_NUMA
4537-
struct mempolicy *mpol = get_task_policy(current);
4538-
4539-
/*
4540-
* Only enforce MPOL_BIND policy which overlaps with cpuset policy
4541-
* (from policy_nodemask) specifically for hugetlb case
4542-
*/
4543-
if (mpol->mode == MPOL_BIND &&
4544-
(apply_policy_zone(mpol, gfp_zone(gfp)) &&
4545-
cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
4546-
return &mpol->nodes;
4547-
#endif
4548-
return NULL;
4549-
}
4550-
45514560
static unsigned int allowed_mems_nr(struct hstate *h)
45524561
{
45534562
int node;

0 commit comments

Comments
 (0)