Skip to content

Commit 2273dea

Browse files
Liu Shixinakpm00
authored andcommitted
mm/hugetlb: update nr_huge_pages and surplus_huge_pages together
In alloc_surplus_hugetlb_folio(), we increase nr_huge_pages and surplus_huge_pages separately. In the middle window, if we set nr_hugepages to smaller and satisfy count < persistent_huge_pages(h), the surplus_huge_pages will be increased by adjust_pool_surplus(). After adding delay in the middle window, we can reproduce the problem easily by following step: 1. echo 3 > /proc/sys/vm/nr_overcommit_hugepages 2. mmap two hugepages. When nr_huge_pages=2 and surplus_huge_pages=1, goto step 3. 3. echo 0 > /proc/sys/vm/nr_huge_pages Finally, nr_huge_pages is less than surplus_huge_pages. To fix the problem, call only_alloc_fresh_hugetlb_folio() instead and move down __prep_account_new_huge_page() into the hugetlb_lock. Link: https://lkml.kernel.org/r/20250305035409.2391344-1-liushixin2@huawei.com Fixes: 0c397da ("mm, hugetlb: further simplify hugetlb allocation API") Signed-off-by: Liu Shixin <liushixin2@huawei.com> Acked-by: Peter Xu <peterx@redhat.com> Acked-by: Oscar Salvador <osalvador@suse.de> Cc: David Hildenbrand <david@redhat.com> Cc: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: Liu Shixin <liushixin2@huawei.com> Cc: Muchun Song <muchun.song@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 114b480 commit 2273dea

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

mm/hugetlb.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2259,11 +2259,20 @@ static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h,
22592259
goto out_unlock;
22602260
spin_unlock_irq(&hugetlb_lock);
22612261

2262-
folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask);
2262+
folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
22632263
if (!folio)
22642264
return NULL;
22652265

2266+
hugetlb_vmemmap_optimize_folio(h, folio);
2267+
22662268
spin_lock_irq(&hugetlb_lock);
2269+
/*
2270+
* nr_huge_pages needs to be adjusted within the same lock cycle
2271+
* as surplus_pages, otherwise it might confuse
2272+
* persistent_huge_pages() momentarily.
2273+
*/
2274+
__prep_account_new_huge_page(h, nid);
2275+
22672276
/*
22682277
* We could have raced with the pool size change.
22692278
* Double check that and simply deallocate the new page

0 commit comments

Comments
 (0)