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=1796297899; b=vgJWImOSRl+bWnuoZKpTACIU1mU2AewTKLIdQ0EAX9+eKDVfB5oIsXRjVXmeEbM6hg0W 0S+0tMdExCFdNjyV5jXx7eKtPsrozLKrMRayKYraN43y3l294w98FMETA+p5kafZx7n2Q aRVakk0lnNM5l5vR6dp6vk3lnEGqoG6zudgBQkn3gVyWhD4vjMnvu5R4dXKMJjteO4eod LlI049tNi2jQFcbZLOqoc86plXNNq/9VdQKkkHOyZv4lJyjI/P/J8jTaCsDFw1MpNcwsJ qQvkzVsepim+l3e6Hate9hBrLrwEPqkP+16KFrScotIJrPEmf4WWVAk8UzGQ+iY1shg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297899; 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=y9qTPRSS4g0mV5iF+gVfKVgE1rz7x/Es091wK/poFHo=; b=SBBQcx2zdcR4kiuLJ7fvWcevsT12SdIbvvjTQha9eHQawMNYPB6dG0PvMPBH+EOrJWR7 Dx0NiaWzdr4LnfmgdXQg9M/ryfg8T7liXdRR8ZQXXbuyuJ7BmPSjdlmSaVYYXJwNL+Opu YPuqwHQEggEbpvjUg9wX4dkbIkUWgrYyImE80acVENm/Q/SeYHtP8Fv75Gx9Xv/yFodyT diG71m5Kjoq3osHeY/TNhnf576EFtIdZiLTBb9PlFFXmahIIy8ermfqxBQ+NRQwWy5FmA J5feeDe2DC8jEBNs53E4ffcmSJNeVI8Z3irausuDM0cw2EE6y9eCh61lDZdCIFzYCkw== 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=fLNZbebe; 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 60C60606A32D; Thu, 3 Dec 2026 11:38:19 +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=fLNZbebe Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2A561F00893; Thu, 3 Dec 2026 11:38:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796297899; bh=y9qTPRSS4g0mV5iF+gVfKVgE1rz7x/Es091wK/poFHo=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=fLNZbebexAkAQn5NC59PY89SMNy+gr3qSZieTmPK0Co95RorJlNPenV6IeTY2kivw 2W0lYMIwuoRTgforE1n9lKDstEy9WCGvD0ZkJ0UmnflORqoZm6UuyWrAVl8fQ9hmNp /9ImbuhbfDOl7r+ag5Uo9YkANEE+YGPdRrhBqxp9poEqP1mr2/Y975kVLz+oAWai92 rD75aX44dfQ6APDX3ama7vDeBfQyqfe5TMDxH7SZJzEeQEQA4AHG37P2P2hZadLuGJ qkg9nQN9xcHx00MANZ/YFB7W+8hRM6gpLofPiPRYzFZaB7gfDlBaczum7aN3jpw3HQ oKugdTFxHW1yQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:25 +0100 Subject: [PATCH v2 02/35] mm/hugetlb: restore the reservation if vma_needs_reservation() fails X-Mailing-List: linux-kernel@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-2-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=4250; i=david@kernel.org; h=from:subject:message-id; bh=skjPOgGiuWU2rgxOrjk9lmCuRjO9I0JcWbRMXjNo/gk=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQyqv5u1bvf1s1s+dS3nyotbEevidt0kM3zl5b+2Wc 2YnfSzsOkpZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZjIBS9Ghl4X+aLGdw0be6bd eX3v5d22nLvzZO89lOyYdH5OZL1hlwHD/7Cypg2NK6ofTLt2dFvohZAjLZxeESqSfoHLJy4tz9m TxQcA 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, for example because the mapping was created with MAP_NORESERVE, alloc_hugetlb_folio() is responsible for performing the hugetlb 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() will call vma_needs_reservation(), which can fail to allocate a file_region in corner cases. In that case, we will leave the reserve map unchanged and free the folio without HPG_restore_reserve. Consequently, nothing backs the reservation. The same happens on a min_size= mount if alloc_hugetlb_folio() took the reservation from the subpool minimum: the folio then has HPG_restore_reserve, but we clear it when vma_needs_reservation() fails. When we later try again to allocate+populate the page cache, we will consume the reservation for that index in the reserve map, although we never accounted for a reservation. This will underflow resv_huge_pages and the subpool usage. Reproduced both cases with fault-injection, failing the file_region allocation in restore_reserve_on_error() when the first UFFDIO_COPY attempt into a MAP_SHARED|MAP_NORESERVE mapping fails: (1) memfd, after the retry: HugePages_Rsvd: 0 -> 18446744073709551615 (2) hugetlbfs mount with min_size=, after unmounting: HugePages_Rsvd: 0 -> 18446744073709551615 Let's fix it by setting or keeping HPG_restore_reserve for folios in shared mappings in this unlikely error case, such that free_huge_folio() will restore the reservation. Restructure the code to make it at least a little bit easier to understand. This has low urgency, as the file_region allocation is very unlikely to fail. Fixes: 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality") Reported-by: Sashiko Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f46c85fc0cd0b..bf2265bc6eb71 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2730,21 +2730,27 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, { long rc = vma_needs_reservation(h, vma, address); - if (folio_test_hugetlb_restore_reserve(folio)) { - if (unlikely(rc < 0)) - /* - * Rare out of memory condition in reserve map - * manipulation. Clear hugetlb_restore_reserve so - * that global reserve count will not be incremented - * by free_huge_folio. This will make it appear - * as though the reservation for this folio was - * consumed. This may prevent the task from - * faulting in the folio at a later time. This - * is better than inconsistent global huge page - * accounting of reserve counts. - */ + if (unlikely(rc < 0)) { + /* + * Rare out of memory condition in reserve map manipulation. + * + * In shared mappings, the reserve map has the reservation for + * this index, so let folio freeing code restore it. + * + * Otherwise, don't restore the reservation, making it appear + * as though it was consumed. This may prevent the task from + * faulting in the folio at a later time, but is better than + * inconsistent global huge page accounting of reserve counts. + */ + if (vma->vm_flags & VM_MAYSHARE) + folio_set_hugetlb_restore_reserve(folio); + else folio_clear_hugetlb_restore_reserve(folio); - else if (rc) + return; + } + + if (folio_test_hugetlb_restore_reserve(folio)) { + if (rc) (void)vma_add_reservation(h, vma, address); else vma_end_reservation(h, vma, address); @@ -2769,11 +2775,6 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, * on a subsequent allocation. */ folio_set_hugetlb_restore_reserve(folio); - } else if (rc < 0) { - /* - * Region cache allocation can fail even if an entry exists. - * Leave the flag and reserve map unchanged. - */ } else { /* * No reservation present, do nothing -- 2.56.1