Skip to content

Commit a830c2d

Browse files
Wupeng Masmb49
authored andcommitted
mm: hugetlb: fix incorrect fallback for subpool
BugLink: https://bugs.launchpad.net/bugs/2115266 commit a833a693a490ecff8ba377654c6d4d333718b6b1 upstream. 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/[email protected] Fixes: 1c5ecae ("hugetlbfs: add minimum size accounting to subpools") Signed-off-by: Wupeng Ma <[email protected]> Tested-by: Joshua Hahn <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Ma Wupeng <[email protected]> Cc: Muchun Song <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Manuel Diewald <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 8fdfdcd commit a830c2d

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
@@ -2987,7 +2987,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
29872987
struct hugepage_subpool *spool = subpool_vma(vma);
29882988
struct hstate *h = hstate_vma(vma);
29892989
struct folio *folio;
2990-
long retval, gbl_chg;
2990+
long retval, gbl_chg, gbl_reserve;
29912991
map_chg_state map_chg;
29922992
int ret, idx;
29932993
struct hugetlb_cgroup *h_cg = NULL;
@@ -3140,8 +3140,16 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
31403140
hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
31413141
h_cg);
31423142
out_subpool_put:
3143-
if (map_chg)
3144-
hugepage_subpool_put_pages(spool, 1);
3143+
/*
3144+
* put page to subpool iff the quota of subpool's rsv_hpages is used
3145+
* during hugepage_subpool_get_pages.
3146+
*/
3147+
if (map_chg && !gbl_chg) {
3148+
gbl_reserve = hugepage_subpool_put_pages(spool, 1);
3149+
hugetlb_acct_memory(h, -gbl_reserve);
3150+
}
3151+
3152+
31453153
out_end_reservation:
31463154
if (map_chg != MAP_CHG_ENFORCED)
31473155
vma_end_reservation(h, vma, addr);
@@ -6949,7 +6957,7 @@ bool hugetlb_reserve_pages(struct inode *inode,
69496957
struct vm_area_struct *vma,
69506958
vm_flags_t vm_flags)
69516959
{
6952-
long chg = -1, add = -1;
6960+
long chg = -1, add = -1, spool_resv, gbl_resv;
69536961
struct hstate *h = hstate_inode(inode);
69546962
struct hugepage_subpool *spool = subpool_inode(inode);
69556963
struct resv_map *resv_map;
@@ -7084,8 +7092,16 @@ bool hugetlb_reserve_pages(struct inode *inode,
70847092
return true;
70857093

70867094
out_put_pages:
7087-
/* put back original number of pages, chg */
7088-
(void)hugepage_subpool_put_pages(spool, chg);
7095+
spool_resv = chg - gbl_reserve;
7096+
if (spool_resv) {
7097+
/* put sub pool's reservation back, chg - gbl_reserve */
7098+
gbl_resv = hugepage_subpool_put_pages(spool, spool_resv);
7099+
/*
7100+
* subpool's reserved pages can not be put back due to race,
7101+
* return to hstate.
7102+
*/
7103+
hugetlb_acct_memory(h, -gbl_resv);
7104+
}
70897105
out_uncharge_cgroup:
70907106
hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
70917107
chg * pages_per_huge_page(h), h_cg);

0 commit comments

Comments
 (0)