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 1CB87606A336 for ; Thu, 3 Dec 2026 11:38:40 +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=FgMILf7c Received: by kanga.kvack.org (Postfix) id 46A0D6B05BB; Thu, 3 Dec 2026 06:38:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 419FD6B05BC; Thu, 3 Dec 2026 06:38:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30A2A6B05BD; Thu, 3 Dec 2026 06:38:39 -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 C529D6B05BB for ; Thu, 3 Dec 2026 06:38:38 -0500 (EST) Received: from smtpin14.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6A40614065B for ; Thu, 3 Dec 2026 11:38:38 +0000 (UTC) X-FDA: 85502474316.14.B9B0EFA Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf29.hostedemail.com (Postfix) with ESMTP id 97889120005 for ; Thu, 3 Dec 2026 11:38:36 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=FgMILf7c; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf29.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=1796297916; b=8jhk6T2pssWSsRk/QEGBhTHS3c6Fh4Vp1EnnOD9NNYqz5lhOUhuUWYpQCI23rh0P2M8pu7 UwYDkw7LfgVqcMEf2+1vrcihvucYYnKr3EsLi8IWLLmjYnr3/NXxSpF1m06aTKsuQoP82x phRqRXccraSIv3f0FMdujjeZQBR9FDE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796297916; 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=BSPgBs8S/zQ/pxQNGEPzWCqLOya1J6mnqRCa3ARJ5TY=; b=Uyj/UaX8bBCFicc1tYTqg9fJ6uU87Gcpk+A469JyBaIVhn80WFjhf4LQLZ7p6hfX7kcUE0 xCqRWrimBX4AhktEnjknX+BDELFG6HXG6LeSbIQjhTw8c7bqmC2chNPe2RrYIgSwnR/Mwc QxOy5aj+/WR+emQWCB3bVnCAKfCMsr4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=FgMILf7c; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf29.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 16E1B4395E; Thu, 3 Dec 2026 11:38:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2D4D1F0089B; Thu, 3 Dec 2026 11:38:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796297915; bh=BSPgBs8S/zQ/pxQNGEPzWCqLOya1J6mnqRCa3ARJ5TY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=FgMILf7c78rR0biO7qSNJ9CKYPkb3cZ5CIfgbV+xCRDYQPPP+q4zmiXwVpF0Lfsns nt1HQzlx4t7MytpT9PYsewLuHlA2vXU8zN18EHU+KIz9dn5eWv/BmGGX+MW3GYjbBm 4bkfkw0HZUDMoRDcA10mUWJPRKv+nu/lFPF56l4Dj4WhD05yz+rNTv2KYM4tAe/dC0 aIBd/UHCIbgVIHA+P1+NNoHg6BsehNRX2Vm1YWXA3aS9rdC74yF/qWmNviTSAEJs24 sU56sd6zE3cc0niIJ2O2iUyF2DvYyU0b+tPNq5hxNBltcAz1uc4pJDvKg+pGEOISt2 uarFEyaLUeYDQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:26 +0100 Subject: [PATCH v2 03/35] mm/hugetlb: restore the reservation when racing with another reservation 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-3-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=2414; i=david@kernel.org; h=from:subject:message-id; bh=pxNsqloSG98PWvSaa6Wyaqe1KRe3EKh+NPzClYlloCo=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6q8vqTeLld7PJ8xytvr1vHtJ5gZFtWpvcw3DdYUz /vFXyjeUcrCIMbBICumyBIUfN/g5ct1tdVufMwwc1iZQIYwcHEKwETEchn+KXRIPvh1k/PaFSX2 2Y4f3rNcZE3Q3+/tWvcgIkioRljMmeF/qluL6ya3ytlrvGe9ZbX58f3Cxmmxl9fb902afKr6wdW 33AA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 97889120005 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-Stat-Signature: 1zw4tc93p4nohr43jhebb97quh6sa7mn X-Rspam-User: X-HE-Tag: 1796297916-778183 X-HE-Meta: U2FsdGVkX1+p+Ao92TP0STA+rYlajmM4jgN/a+u/ZMzkPkZ2tGg6+ExpqZO+96jWjVdHfgGshBHqQmm0msQEhm9XL1T3AdUgdJ0/GbO6bQDlzEWGfZcGy70VrFXNqPUm5vez8hdMqmDysxOlY6lNQTDCDTOAkHdhY5JQfiGuB/lV/PqZ5QfZZl4WcZa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uAP+l1+Vs7eNAEZCeSi7jyH0zC/TTD1EahOYxQocm2qthy8Er44f58PAfteBa6bKfkeXihSS5dowV3HFgP4FvVpPWsLPzy7qltLgUyjaemfxIcpUFjYCg27/Bo/wqRnQuN+GHWlHOgGwixpcCcI10fGWX6/Fqy2PRXOwlgAhpYgmgzc6/5sFyrsJ9CFyefzNbdjpVqYUomtQ9fOev+O/5qtOr1K8SOKWiD0F5/U9zL6B/FibeqHE8tMYozHoWAOUv6JRvt8EkQEVxYwhMLtSykJHyp9Kc= 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, alloc_hugetlb_folio() can race with hugetlb_reserve_pages() reserving that index, for example, because another process mmap()s the same file without MAP_NORESERVE. vma_commit_reservation() will then find the racing reservation in the reserve map after allocating the folio, and we consume that reservation instead: we return our subpool charge, drop a global reservation and uncharge the cgroup reservation of the folio. However, we don't set HPG_restore_reserve on the folio. If we then hit an error before the folio gets added to the page cache, restore_reserve_on_error() assumes that alloc_hugetlb_folio() performed the reservation in the reserve map and removes it, and free_huge_folio() doesn't restore the global reservation. The reservation of the racing mmap() is then gone, and a later fault at that index can fail with SIGBUS. Reproduced by artificially delaying alloc_hugetlb_folio() right before vma_commit_reservation() during a failing UFFDIO_COPY, while another process mmap()s the same file: once all unreserved huge pages are in use, that process gets SIGBUS when touching its page. Let's fix it by setting HPG_restore_reserve, so the error path keeps the reservation in the reserve map and free_huge_folio() restores the global reservation. On success, adding the folio to the page cache clears the flag. This has low urgency, as it requires an error after racing with another mapping reserving the same index. Fixes: 33039678c8da ("mm/hugetlb: handle races in alloc_huge_page and hugetlb_reserve_pages") Reported-by: Sashiko Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index bf2265bc6eb71..fbe79ae4c0f3c 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3222,6 +3222,11 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma, hugetlb_cgroup_uncharge_folio_rsvd( hstate_index(h), pages_per_huge_page(h), folio); spin_unlock_irq(&hugetlb_lock); + /* + * We consumed that existing reservation and must + * restore it when freeing the folio due to errors. + */ + folio_set_hugetlb_restore_reserve(folio); } } -- 2.56.1