From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 98BF2606A322 for ; Thu, 3 Dec 2026 11:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Bhj5zK+l Received: by kanga.kvack.org (Postfix) id BE61C6B05BD; Thu, 3 Dec 2026 06:38:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B954B6B05BE; Thu, 3 Dec 2026 06:38:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A839F6B05BF; Thu, 3 Dec 2026 06:38:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 726AF6B05BD for ; Thu, 3 Dec 2026 06:38:53 -0500 (EST) Received: from smtpin18.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 14ACD1C3DB6 for ; Thu, 3 Dec 2026 11:38:53 +0000 (UTC) X-FDA: 85502474946.18.BFD4D37 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf09.hostedemail.com (Postfix) with ESMTP id 40A69140003 for ; Thu, 3 Dec 2026 11:38:51 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Bhj5zK+l; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796297931; b=VkuPuFAy8cGGa9s9ROGD12bkHGaNt+9L3WVzO+t1p8ERbd2c/grQSLCIeZO5SS2/2mERbc IovISVRN0oJCoBhUBWNDDxHa0lkkcKf+z7YdM+N8eXC7XQklJGQatL0c9gP69bLCML1Fdi BAdcho8DqkdotnqDmvxXsxydA3rczO0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796297931; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SJsU1bOUzt+YsVESQvQbLGMrKW/Fii2kv8ZPCYbi0q4=; b=xmBTbwPSBJdppmH1H7M8SjlkoANrTKa0r0A/xH3ZLgAtT3v6PZf4EXsAeJb2JLyH8DKtAU FTDahvETIm8baEKP9I3vqfX4k7Pi8uNF4KmkWi8D6/lcLIz7PBd2P7sdmoY2KTAsRfkwWt GAGliak/qsI3PDpavDog2rHisKRqUkY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Bhj5zK+l; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 87EF843AA4; Thu, 3 Dec 2026 11:38:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8663E1F000C1; Thu, 3 Dec 2026 11:38:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796297930; bh=SJsU1bOUzt+YsVESQvQbLGMrKW/Fii2kv8ZPCYbi0q4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Bhj5zK+lTT7LP1YT3TaA8iKFoDR+JgKFDgqXPdrjucuyJf9jZTlxJBMsd8g7b0cxq r6C1u83NBPijlimjBxXefYb8Q/vRWHRD2OvRCos4+BR2iWjfxLQrfarIOz3QKSZvm8 GDaTZCYIQhPAwk7qjnrJRmWP8UQq8+d1VZr3+9df4mpy7e8hiwXU8Xo+fzmy3AH3/U vaAb4AC4f1mcbEnzojuyDorKeDGcqGcbedG8yfvH26ASyQSgYbYB777m6IDtftRQZF ENzJ3Sj+VurhVbzOXGzTg3YDSuxoEumXe/oLyWXl+6qMj43NWAxwY/cbTC+LVjX2HH yIj5Do1nMgSDg== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:27 +0100 Subject: [PATCH v2 04/35] mm/hugetlb: return the reservation to the subpool on error X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-4-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , Sashiko , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4816; i=david@kernel.org; h=from:subject:message-id; bh=kqd2PNPya5CwzSgL0g8RYUQ/zHdNuWyR40cjBMBGDSc=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6rUvfoPqznkBUTNTeMzd7zBn9bQolImsMJ+XmDR1 1+GVw52lLIwiHEwyIopsgQF3zd4+XJdbbUbHzPMHFYmkCEMXJwCMJGJwgz/lPdvfGGhl+rsZnJU nCnESjQu6tKeNyW+a5qSNwhOexAsysjwLjJsURVvCFtTTOCXGrbwt3wp+nd6zTjdzz7UnpF4t5M FAA== X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-Stat-Signature: ip4mnfjm17usesczcohk8ker1c5skhgq X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 40A69140003 X-Rspam-User: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796297931-744583 X-HE-Meta: U2FsdGVkX18kuGMKCRwW/s42ommuey9V7W8Wn4ZESVjqqmJEeuIllsBnng/eXU5m2UaC3MqGTe6c1TjnMFXMcdfzuDlrt+bIhfSHMRB49bpU56fbY7smVdhC7wk0qROE1jGXnYYnJBvBOy23MywPtEDtODQbIxHw/O9gWZTsqIHQMdhdF32UJ+rUeaXSHav73FRhJOgh3F8YU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zB3mp66eWb1JyI9GaM6Nb8PYixY+rTY0XK/CqHePwlAGyPJ4M+pJhSADZCnub3/FFjQyomIJQ1YGDM4yjFVy3gO+qLNmEMiDJ25K1GvvhhEuPfvnZzAzyvf4ZZUoz8bhMrKtRB5Ybp4yZzq1TT6hSw8mQztscanEFARP3hBcByeStfcEBt84PMukj9tEW+SP8cvN41ql4V3LfvLp6/GAppufQX9OIUwS5/BpdLHmWMh04YvwPJ4ZHF0zspAVSuFaL3BOBbmhkaJTkgKGU= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet on a min_size= mount, alloc_hugetlb_folio() can take the reservation from the subpool minimum, and we perform the reservation for the folio's index in the reserve map. If something goes wrong before adding the folio to the page cache, restore_reserve_on_error() is supposed to undo the reservation in the reserve map, and free_huge_folio() is supposed to return the reservation to the subpool, like we do for CoW mappings already. However, as the folio consumed a global reservation, it has HPG_restore_reserve set. So restore_reserve_on_error() keeps the reservation in the reserve map, and free_huge_folio() restores the global reservation instead of returning it to the subpool. Consequently, the reservation from the subpool minimum is stuck at that index until truncation. As vma_commit_reservation() does not charge the hugetlb cgroup reservation, a later fault at that index will consume it without a cgroup charge. Reproduced with a failing UFFDIO_COPY on a min_size= mount, followed by a fault at the same index, inside a hugetlb cgroup: hugetlb.2MB.rsvd.current: 0 instead of 2097152 Let's fix it by clearing HPG_restore_reserve in shared mappings as well. On success, adding the folio to the page cache clears the flag. Note that we have to clear the flag before vma_commit_reservation() now, such that the race handling can set it again when we raced with another reservation. This has low urgency, as it only affects the subpool minimum and the hugetlb cgroup reservation accounting on min_size= mounts. Fixes: 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools") Reported-by: Sashiko Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand (Arm) --- Documentation/mm/hugetlbfs_reserv.rst | 4 ++-- mm/hugetlb.c | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Documentation/mm/hugetlbfs_reserv.rst b/Documentation/mm/hugetlbfs_reserv.rst index 4711029bd067d..7c13f79603eee 100644 --- a/Documentation/mm/hugetlbfs_reserv.rst +++ b/Documentation/mm/hugetlbfs_reserv.rst @@ -257,8 +257,8 @@ compensate. See the section :ref:`Reservation Map Helper Routines ` for more information on these routines. -For CoW mappings, alloc_hugetlb_folio() calls -folio_clear_hugetlb_restore_reserve() if the reservation came from the subpool. +alloc_hugetlb_folio() calls folio_clear_hugetlb_restore_reserve() if the +reservation came from the subpool. This makes free_huge_folio() return the reservation to the subpool on error. diff --git a/mm/hugetlb.c b/mm/hugetlb.c index fbe79ae4c0f3c..025aae6d0b72e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2722,7 +2722,9 @@ static long vma_del_reservation(struct hstate *h, * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio. * * For CoW mappings, allocations using a subpool reservation leave the - * flag clear and the reserve map unchanged. free_huge_folio returns the + * flag clear and the reserve map unchanged. The same applies to shared + * mappings, except that case 2 removes the reservation from the reserve map, + * unless updating the reserve map fails. free_huge_folio returns the * reservation to the subpool. */ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, @@ -3200,6 +3202,18 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma, hugetlb_set_folio_subpool(folio, spool); + /* + * On error, let folio freeing code return the reservation to the + * subpool, unless we consumed a reservation from the reserve map. + * + * In private mappings, the reserve map might not exist or already + * account for another folio at this index. In shared mappings, + * restore_reserve_on_error() will remove the reservation we are about + * to add to the reserve map, unless updating the reserve map fails. + */ + if (map_chg != MAP_CHG_REUSE) + folio_clear_hugetlb_restore_reserve(folio); + if (map_chg != MAP_CHG_ENFORCED) { /* commit() is only needed if the map_chg is not enforced */ retval = vma_commit_reservation(h, vma, addr); @@ -3230,14 +3244,6 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma, } } - /* - * Return subpool reservations to the subpool on error. A CoW mapping - * might not have a reserve map, or it might already account for - * another folio at this index. - */ - if (map_chg != MAP_CHG_REUSE && vma_is_cow_mapping(vma)) - folio_clear_hugetlb_restore_reserve(folio); - return folio; out_subpool_put: -- 2.56.1