Skip to content

Commit a833a69

Browse files
Wupeng Maakpm00
authored andcommitted
mm: hugetlb: fix incorrect fallback for subpool
During our testing with hugetlb subpool enabled, we observe that hstate->resv_huge_pages may underflow into negative values. Root cause analysis reveals a race condition in subpool reservation fallback handling as follow: hugetlb_reserve_pages() /* Attempt subpool reservation */ gbl_reserve = hugepage_subpool_get_pages(spool, chg); /* Global reservation may fail after subpool allocation */ if (hugetlb_acct_memory(h, gbl_reserve) < 0) goto out_put_pages; out_put_pages: /* This incorrectly restores reservation to subpool */ hugepage_subpool_put_pages(spool, chg); When hugetlb_acct_memory() fails after subpool allocation, the current implementation over-commits subpool reservations by returning the full 'chg' value instead of the actual allocated 'gbl_reserve' amount. This discrepancy propagates to global reservations during subsequent releases, eventually causing resv_huge_pages underflow. This problem can be trigger easily with the following steps: 1. reverse hugepage for hugeltb allocation 2. mount hugetlbfs with min_size to enable hugetlb subpool 3. alloc hugepages with two task(make sure the second will fail due to insufficient amount of hugepages) 4. with for a few seconds and repeat step 3 which will make hstate->resv_huge_pages to go below zero. To fix this problem, return corrent amount of pages to subpool during the fallback after hugepage_subpool_get_pages is called. Link: https://lkml.kernel.org/r/20250410062633.3102457-1-mawupeng1@huawei.com Fixes: 1c5ecae ("hugetlbfs: add minimum size accounting to subpools") Signed-off-by: Wupeng Ma <mawupeng1@huawei.com> Tested-by: Joshua Hahn <joshua.hahnjy@gmail.com> Reviewed-by: Oscar Salvador <osalvador@suse.de> Cc: David Hildenbrand <david@redhat.com> Cc: Ma Wupeng <mawupeng1@huawei.com> Cc: Muchun Song <muchun.song@linux.dev> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 82f2b0b commit a833a69

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

mm/hugetlb.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,7 +3010,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
30103010
struct hugepage_subpool *spool = subpool_vma(vma);
30113011
struct hstate *h = hstate_vma(vma);
30123012
struct folio *folio;
3013-
long retval, gbl_chg;
3013+
long retval, gbl_chg, gbl_reserve;
30143014
map_chg_state map_chg;
30153015
int ret, idx;
30163016
struct hugetlb_cgroup *h_cg = NULL;
@@ -3163,8 +3163,16 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
31633163
hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
31643164
h_cg);
31653165
out_subpool_put:
3166-
if (map_chg)
3167-
hugepage_subpool_put_pages(spool, 1);
3166+
/*
3167+
* put page to subpool iff the quota of subpool's rsv_hpages is used
3168+
* during hugepage_subpool_get_pages.
3169+
*/
3170+
if (map_chg && !gbl_chg) {
3171+
gbl_reserve = hugepage_subpool_put_pages(spool, 1);
3172+
hugetlb_acct_memory(h, -gbl_reserve);
3173+
}
3174+
3175+
31683176
out_end_reservation:
31693177
if (map_chg != MAP_CHG_ENFORCED)
31703178
vma_end_reservation(h, vma, addr);
@@ -7239,7 +7247,7 @@ bool hugetlb_reserve_pages(struct inode *inode,
72397247
struct vm_area_struct *vma,
72407248
vm_flags_t vm_flags)
72417249
{
7242-
long chg = -1, add = -1;
7250+
long chg = -1, add = -1, spool_resv, gbl_resv;
72437251
struct hstate *h = hstate_inode(inode);
72447252
struct hugepage_subpool *spool = subpool_inode(inode);
72457253
struct resv_map *resv_map;
@@ -7374,8 +7382,16 @@ bool hugetlb_reserve_pages(struct inode *inode,
73747382
return true;
73757383

73767384
out_put_pages:
7377-
/* put back original number of pages, chg */
7378-
(void)hugepage_subpool_put_pages(spool, chg);
7385+
spool_resv = chg - gbl_reserve;
7386+
if (spool_resv) {
7387+
/* put sub pool's reservation back, chg - gbl_reserve */
7388+
gbl_resv = hugepage_subpool_put_pages(spool, spool_resv);
7389+
/*
7390+
* subpool's reserved pages can not be put back due to race,
7391+
* return to hstate.
7392+
*/
7393+
hugetlb_acct_memory(h, -gbl_resv);
7394+
}
73797395
out_uncharge_cgroup:
73807396
hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
73817397
chg * pages_per_huge_page(h), h_cg);

0 commit comments

Comments
 (0)