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 4EC5C606A32D for ; Thu, 3 Dec 2026 11:38:24 +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 kanga.kvack.org (Postfix) id 332306B05B9; Thu, 3 Dec 2026 06:38:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E2376B05BA; Thu, 3 Dec 2026 06:38:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D0AC6B05BB; Thu, 3 Dec 2026 06:38:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DC3946B05B9 for ; Thu, 3 Dec 2026 06:38:22 -0500 (EST) Received: from smtpin30.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C63EF8061D for ; Thu, 3 Dec 2026 11:38:21 +0000 (UTC) X-FDA: 85502473602.30.4AE3140 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf18.hostedemail.com (Postfix) with ESMTP id 045ED1C000E for ; Thu, 3 Dec 2026 11:38:19 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=fLNZbebe; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of david@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=david@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796297900; b=lYPjUVWPXx9/8Vlz9C5I5nR3W3okK42GhQg+NCYl8J/MU+ax6PAc+S8oSs74qsK7F1Mkw8 D6eLPFWcvow3TOswxnuxuZFU/CmrKLFRwZhIUHUm/9zisL5eEbBJ9Gsspno6cHuypExXSY oIdHtBOsbBEVmm4Z0onaK9RKQI2HC40= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796297900; 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=y9qTPRSS4g0mV5iF+gVfKVgE1rz7x/Es091wK/poFHo=; b=SuAXkiV1fPQVacecTstN0hMqRhEVeVmNY/QvmzSAjXbubmbneUgX4/5NWRUdVJmcijSisD hP0hQGNncoX6DIycWUF8UqrJeP0kv2UmfC/90KDA1jyWUgZL1xC7FqqiXnxBLTDu5yVbFO v41wcYaLVuV+a/RY5+vhdxPvTOs9fSA= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=fLNZbebe; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of david@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=david@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 8562D621E1; Thu, 3 Dec 2026 11:38:19 +0000 (UTC) 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-Archived-By: 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 X-Stat-Signature: 51cpojbci9yjeybjuxzhewyjhtnpobjs X-Rspamd-Queue-Id: 045ED1C000E X-Rspamd-Server: rspam10 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-Rspam-User: X-HE-Tag: 1796297899-524558 X-HE-Meta: U2FsdGVkX19crJz+fqGzo7Os2RzY+SzgOmDLju5RqSSWOiPwHyrgc0j9jeyYfv+PKt0/FcUyMZT5XoAg/nf6r+SQjG7mXe6PAulmby3Lyn51eXdkXKSj9xBKs9q7WMskOld2sUoG0AyhuFL46b3ivIoVosE6dHwqiQ8idplle2h7wA75UZn08Tm5GhyIOslDiybS9iR30juk+/qlUt5LAnd2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8BkgcKhAHrVXxFXgMuj/O1RWqUPlt4yk/ImGu5HcnfkZZVPx6f29kYVyIYJUFFhjW8jJSgaXHh0yzSLbP9SO1t7qnCf+7h6CcuD0yasTxKsBLfB6ogKVRjSCBR2bCM7jsGCfsQXpJemTCvdDsFOh/QVicQDUFH6mNO5KGcbochXVdmP8q3/dbjveFN1ehCuUDm13eImI8WwzLXxry3iO+Yj+uG6B0SiXti3H83SgCki06RerRYY0LphBTVi6mKVYel1XXn0IsJoYyCArSf3dL0XMQ== 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, 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