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=1796297915; b=5c1m6/8bXppQSlmwiUPIJIrAewe0bzCKMPIxGpcreWXtjK/Vpj0E5E+qrRBCGg2elgD4 peNZHRCoDHEX3Lim109rPIb4P33p7E/apY2voXqJ8AFCHJPXbsPct9Y7D1kBMHrr0mm/o 7LMC5DG57AFr3pAZoeD4sSyagszsEJzawIRmt8P06G7aQygWPMN5W1jwPOsVd4/OZ0hnP PtusRWlAxmUQ8jgPQA7lbo7PvWijhR/AoZZmsuGwtCNNUoGoprp+8ruCbMWoSxxv/7xNl YAk5Djtgu+9iD5wb46wFGfA1Utr+a8DEJDvaUarmCsZxA+cwdXPuNeeWlGYBCl95tzQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297915; 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=BSPgBs8S/zQ/pxQNGEPzWCqLOya1J6mnqRCa3ARJ5TY=; b=SpcQ7EBzfRBXCcjOtVo8Nf3MFhvmlr9obvylrNddTImZwXUdR+RM9bH+kw+dOUroG5sK CrET7ujH4/k6P5g5NCPZp+R1PYbv0zSukxU2wVfJVhuIGwwMtXIV8lf9W5Zcxll0fB9Sy /vszi34vyxZ19uq0gvTaPa247m1zIP0I0/6FCuAiiHHz0TPyOhjNfdZAYHnZyOHFgyqsG wdgUQYeQtai8VBCh12BgIkvtPl4c2qsk1ngD1YDJZ1rTgBHTQi6f2jLIHeAUyZMqwImnF nBRe1wYv0MrDGIlNI1B0zzytb6O7tyfHJdXOAjq3XvdH+9j6DLh34yvr1zK2IY4hq+w== 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=FgMILf7c; 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 7413B606A322; Thu, 3 Dec 2026 11:38:35 +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 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-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-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 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