From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796297930; b=pJqMJssLoWGLcTUwmFiDh0uBLcXbehOhU+l1hVKBCqrWfqJCX33Fm7xxGbRSSrFHDqk8 YuU9GL7jKztxOohlyEnsxhBS9MV4PhSbo2JQ4HiUXXIMNjpwzms3dvga/Y1rUSuWqx2l7 7/YPirekho1jE92XFHHy1boNZlbT2ceapVumey474ULXC1sOd9lJ3Q8Nh0F+aVX1cAfnA BJarGkByosaxZvsn36+1NPcXElJRA8mzgjo7d92EIEnkcKBszjEqosdhAJbc8zLjNfdpF IIgRk7GNE0OnDNUAcfjhaTqU+o11dbJVS4BKr+4pgnBaBYTSnA988m1wgQGIK4YU7VQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297930; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=SJsU1bOUzt+YsVESQvQbLGMrKW/Fii2kv8ZPCYbi0q4=; b=T1CaTA6I3lbYyMC5JPM/yQ8Z4EwnsC8MdF/TgdtOYQzaQgMycmEMlXLVE6Lwmh9qOEPf 6GArJAHBDQB9CD5qsDuhc4+42fjSv1pADCg/ifhwR/xHA800EU+6Jnpc5K5RH8JTDzMH0 6tD0EwZyOeQ5uTD3HLzMTopRQzIY9N4mkk7JnL1BfJKE8An4X/6D4YO2R9kEOusLH6n4b nYAVWf3FYHRf068iyVVGC556qbX9QJS46o6GoQNZEhE+VvYDJla2CB7ByUtkjMKj2m7ZR XhwhKs02RgSNsQtE1y0Ztx78Hq/vZmTvoIPBb2pdtWDYRiP2kDnB16MrHa9WKyp9eTQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Bhj5zK+l; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC576606A322; Thu, 3 Dec 2026 11:38:50 +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 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-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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