From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oo2-f43.google.com (mail-oo2-f43.google.com [74.125.231.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF729353A70 for ; Fri, 11 Sep 2026 14:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.231.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789137913; cv=none; b=IAk7JWIOnWn9NS2NwsrYC8OOwF+UyxWIrSCE+cAIMpUIgz4rrOlPOx8xsocSIMYuY8we5/ogYkV9g81HxiKuOss7MeySIszaB3dzgtTYwG8Yu5uBk6tJagINLXAAkPzGzkANbrVPfd9m9KHVJc4bf00Py6wUB+mQENGzv//Iqvg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789137913; c=relaxed/simple; bh=W20x7rVTdBJtL+P5aYShncumThcpv9eugB/ou+B1ipQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nY3D54f3IVpF9xlyAc1nvEF7RQUVGFRKbu4FrEdew+UQs7AqSETt5ocbJA/DB9ZhIxm6Mvw7KIuCNZQO0dzbA8Ad2vrxXBMmH741xOirs7KKTXn+h+e2aDlDrHNjS03V5vVf+dG/OQYsWPO8l4f1MOv2IOmJgrRLxcSLobqQmHs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EDDJAxlO; arc=none smtp.client-ip=74.125.231.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EDDJAxlO" Received: by mail-oo2-f43.google.com with SMTP id 46e09a7af769-7f65ebea805so586293a34.2 for ; Fri, 11 Sep 2026 07:45:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789137911; x=1789742711; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aHTYcbYj4j1qZKzZt30zdIUqIM0GkVQjFda8W78rUTs=; b=EDDJAxlO7Co5wek+UHN0iVqznHqZpw22C9Ofbhwm215XgyQh55Pze/fqUvyKhUPq0O ylZNGrfdHifZdarYn3daH8Y9vCGgEDYAD7PAwhIVPtM5qXYF6pzF6kZRqJD6/Q1P6zsU nJLRAQjPUD5w7olkudzW20RHjv3so5mCLzg1ZrfRO69YTLUxD/Aauc05ZOy1UN2KobwP 3/bFFAtG9Zx4HDf+wlNPIxjrvb6+dbIUBQEanudrILZKYVMpoFwPIiX/a1DzeF41FdWZ FDz+1lubMgIOQSGILpqBQ3aMKdG4Cn/Rh0Egou4l7qjBcmXzRGjDVGbNUOu3FGPiriu9 l8xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789137911; x=1789742711; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=aHTYcbYj4j1qZKzZt30zdIUqIM0GkVQjFda8W78rUTs=; b=cA0YhX1CBJRzww0jtuho0OLQo6sFElnG24D+kG94FSjlz/txWQFk59bKsH5MUVOEAK q3Jm9lAM50LJCghsYAA3fC6For+qdtFYKbC/igFMwRK6MEBxF8OGMhvRHQKb1hXRFu7a VWiLjvrPjs06cDWWO0QB5NfFUCuQcEBh0ECrJYK1iOFipZ3kDVm6DX9jALM2c9QnChYc SYMGjwDUWt+ZcNSaK6RU0rBzMbkdSR3aiKFwlfeJUVhT6QpJ/EPXq6ISKC54tjQSUEv8 OfUbDQDqUY8hx/aC345udyYmnrJdFDGxiR9qgTyA63AAKp8bFiCHndjvS3iLKUuFq3+w bcKA== X-Forwarded-Encrypted: i=1; AKwUvByzuEj0T2sLNl722zhxpAKvuVy+DnIZw3dgZBfLiDMT0f+tgxWBwKdw62vweEXHx4eECnkoMUK3DQs=@vger.kernel.org X-Gm-Message-State: AFuF++k4bTyfGDizPKOlBvms4lwGVIvWuFNb5vz9cPyYhk/8BldJjE31 XbyinfE2ztHyOXP5ccZN8e3da4327u5YBxDn0pRf/RhdvIfyM+Vta0fh X-Gm-Gg: AYBFou1+pPkAK4Cbn6y/eB4LeOGIMCWn0aMvF/cup/cSHa2tavXKn3EcAq4JVymYPKW pc6m5raEeSsbhXLQF8zqF6eWAhjq1MGnTkjZNSNkvAsAOrqNKKR6mo7eElCUS55ZNt7KIeoFWiJ Ks+pItKCpRqpHQ9vvnn7DXltfG13CuKrrz7AkVKht+uV6bYkWlO2FtGL4zGCKo+qsnSXzLDZdg5 jlawh1QWp2YAIkZ0tYMiQdv+RrfLmTx/Amzm5dofhxuV0OePfQG8rTvjwIVMifqnuXC9ABiV7Px BvyzVWG923xK/2YMSiaOReQyyNeWrlD1eAuECWFgrhq620+tML0jBh95ht2rQ2azVihO4sp3o1/ tx0VLe3MANeeGydjolB+q4lut9agEROUexlUmq/MBRGX0Dh52APaENbnSf0D2K49+Fst8xeMb4w l/TAilXmsTSrIl4ETO/S1HdCKymCXYly9F+9kBE9iCaclIbdKsgG2lS0DLakTIYDSXwZ3Yo25BB grQ7gTecx5K4XYKw1gLkJGdyOYu X-Received: by 2002:a05:6830:448e:b0:802:faf1:3919 with SMTP id 46e09a7af769-803fc316ec8mr7479732a34.3.1789137910671; Fri, 11 Sep 2026 07:45:10 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:6::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-803f60fe54asm2969111a34.15.2026.09.11.07.45.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 07:45:10 -0700 (PDT) From: Joshua Hahn To: Ackerley Tng via B4 Relay Cc: Andrew Morton , David Hildenbrand , Dongliang Mu , Hongxiang Lou , Johannes Weiner , Jonathan Corbet , "Liam R. Howlett" , Lorenzo Stoakes , Miaohe Lin , Michal Hocko , Mike Rapoport , Muchun Song , Nhat Pham , Oscar Salvador , Peter Xu , Randy Dunlap , Roman Gushchin , Shakeel Butt , Shuah Khan , Suren Baghdasaryan , Usama Arif , Vlastimil Babka , Wupeng Ma , Yanteng Si , fvdl@google.com, jthoughton@google.com, rientjes@google.com, vannapurve@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ackerley Tng , stable@vger.kernel.org Subject: Re: [PATCH v2 3/4] mm: hugetlb: Fix subpool usage leak on allocation failure Date: Fri, 11 Sep 2026 07:45:08 -0700 Message-ID: <20260911144509.3792211-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260909-hugetlb-subpool-always-track-used-v2-3-30c5d83b572a@google.com> References: Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Wed, 09 Sep 2026 14:49:28 -0700 Ackerley Tng via B4 Relay wrote: > From: Ackerley Tng > > When folio allocation fails early (e.g. buddy allocation failure or > cgroup charging failure) and a reservation was not used (meaning an > unreserved global page was needed), the subpool page acquired during the > allocation attempt must still be returned. > > Currently, the subpool cleanup error path only returns the page to the > subpool if a reservation was used. If no reservation was used, it skips > releasing the page back to the subpool, permanently leaking the subpool's > used pages counter. > > With subpools now always tracking used pages, always release the page > back to the subpool whenever a subpool page was acquired. > > Opportunistically rename the local variables tracking global reservations > needed and global reservations returned. This clarifies the accounting: > a value of zero for needed global reservations indicates an existing > reservation satisfies the allocation, while a non-zero value indicates > new global pages are required. > > Adjust global reservations using the difference between reservations > needed and reservations returned to properly handle races where concurrent > threads interact with the same subpool. > > Fixes: a833a693a490 ("mm: hugetlb: fix incorrect fallback for subpool") > Signed-off-by: Ackerley Tng > Cc: stable@vger.kernel.org LGTM, Reviewed-by: Joshua Hahn I think that we get parity in the accounting logic between alloc_hugetlb_folio and hugetlb_reserve_pages is a nice plus as a result of these two fixes. > --- > mm/hugetlb.c | 23 ++++++++++------------- > 1 file changed, 10 insertions(+), 13 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 9eb9f3442574c..652cfb55c6e6e 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -2957,7 +2957,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, > struct hugepage_subpool *spool = subpool_vma(vma); > struct hstate *h = hstate_vma(vma); > struct folio *folio; > - long retval, gbl_chg, gbl_reserve; > + long retval, gbl_resv_get; > map_chg_state map_chg; > struct mempolicy_interpreted mpoli; > gfp_t gfp = htlb_alloc_mask(h); > @@ -2996,8 +2996,8 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, > * Or if it can get one from the pool reservation directly. > */ > if (map_chg) { > - gbl_chg = hugepage_subpool_get_pages(spool, 1); > - if (gbl_chg < 0) { > + gbl_resv_get = hugepage_subpool_get_pages(spool, 1); > + if (gbl_resv_get < 0) { > ret = -ENOSPC; > goto out_end_reservation; > } > @@ -3006,7 +3006,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, > * If we have the vma reservation ready, no need for extra > * global reservation. > */ > - gbl_chg = 0; > + gbl_resv_get = 0; > } > > /* > @@ -3017,10 +3017,10 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, > alloc_flags |= HUGETLB_ALLOC_CHARG_CGROUP_RSVD; > > /* > - * gbl_chg == 0 indicates a reservation exists for this > + * gbl_resv_get == 0 indicates a reservation exists for this > * allocation, so try to use it. > */ > - if (gbl_chg == 0) > + if (gbl_resv_get == 0) > alloc_flags |= HUGETLB_ALLOC_USE_GLOBAL_RESERVATIONS; > > /* Takes reference on mpol. */ > @@ -3074,13 +3074,10 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, > return folio; > > out_subpool_put: > - /* > - * put page to subpool iff the quota of subpool's rsv_hpages is used > - * during hugepage_subpool_get_pages. > - */ > - if (map_chg && !gbl_chg) { > - gbl_reserve = hugepage_subpool_put_pages(spool, 1); > - hugetlb_acct_memory(h, -gbl_reserve); > + if (map_chg) { > + long gbl_resv_put = hugepage_subpool_put_pages(spool, 1); > + > + hugetlb_acct_memory(h, gbl_resv_get - gbl_resv_put); > } > > out_end_reservation: > > -- > 2.55.0.1007.g17ff1f9808-goog