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=1796298213; b=yfC91oHZK2e+gtxT1WWkjGHnXMLt0E5abjsm4/ParkZgjvaOMmIXeur1UCbcgqkGgkQN hi1Px2vWjPeOpb3czyasoxPypkgMwgaotIFrjPj7c0Awv+i0Ssg88wLjrTRmsbQxyXZ7y mgtz8xkK1oa/GR/weVbdUDkZcEsMIvn9COtT1+kILQWdsgcgZ0eEHa3UvwrKoOKYTTfBi sqQG5EL+cKmPPXsKai9HxFt2yRt/8wJC1ihJjWjmQyiHCqfXDtTCk/pDC2DVgr7CY4JCm hRIxVdvqHvexpBRYJYS0zhOb4UQ6cpx8kWqaGpw+qLrDjkWueLZMh1N0/wPsU0vLs/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298213; 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=jIS4A4m/+l4GLxKvteYs88qc43Ql4IlqOsgZqRYDFsc=; b=GYeLoCroa8P/hGIVQZq6Za0V2Jf/Iv3xVtv+bW1koGKjRJxo/6pDQvQ6/iNMhwRTjEQP Miuqq4BScKCO3TSlLp3+20Z1iJD1ggOAIMBPnOPOacMfeiokKGfo02qXMZgtlCQkbZZiz c3CHbhCHXpNqbCPxap15BiQOHtWTK0FRWFCkHZ8x747YmPFmeEyn7ocIV3+lENFQQEfBl BMqFQU5j6m9/cOO/Ts/T1AggGCLz0pHrp10t4aRQgjEOajcgsPEyun31saD50pvaNeyYn 7CBdQGESZAMw5sXQ5LCUdwRfSlKQSivA6HVyEPr1+JxLf1nkh5k57SF6ndij+yWpekQ== 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=FcqZY+zi; 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 0721E606A322; Thu, 3 Dec 2026 11:43:33 +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=FcqZY+zi Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CD281F00893; Thu, 3 Dec 2026 11:43:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298212; bh=jIS4A4m/+l4GLxKvteYs88qc43Ql4IlqOsgZqRYDFsc=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=FcqZY+zi8JnP5IlxpnQg8SGAXl7Ytl3EMX+RDtrf90rpkz4Zl2W4xoKnYFFRXxZe6 3Lk4clxAYgiUxuCKZK39goyfB/f33EOx6Kf2YVj4XhKVdbRPgPTfH3+Rxi/z4ujZY5 cIvPSP3yUtIGxZRdZfbEyqNVnx7MoGY5TI+xr6mgJkF2OPGKaOnVot99aL3J54z4De YIkPvl5wSglinNc82SbEe1apz6cspqPbWkNI4r40Ewbl0NGp9CXqpNFeoWFk8Oodgv iO050dvmHzWJn0LwCWxeKoxjghAt5L0K3MHnS7a/47Xqx8FIgKPqb4JvgJzWKS3EVa X031il16A05zQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:46 +0100 Subject: [PATCH v2 23/35] mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON 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-23-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 , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3854; i=david@kernel.org; h=from:subject:message-id; bh=Oqf255v2yjn28iz7x2qDa9mcSbQKpNXiSR9XwAQSr/k=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ2recDqceqcn65x487yVpt/0ntr7ht/8YwUmOMTd6 Irh4xTrKGVhEONgkBVTZAkKvm/w8uW62mo3PmaYOaxMIEMYuDgFYCL/Chn+cCyXiq9suur3MSW8 KUsquqtuwqLpJ76fu9J6RXx/0/Ztwgz/S65wudQcf3bH3zgtICFo5emtKzmUztu5rrr99rfZ+v0 HuQE= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A When UFFDIO_COPY places a new folio into a MAP_SHARED hugetlb mapping, hugetlb_mfill_atomic_pte() checks EOF under filemap_invalidate_lock_shared() and holds it until the folio is mapped, such that a concurrent truncation either unmaps the new folio, or we see the new i_size. For other file-backed mappings, mfill_file_over_size() checks EOF for all userfaultfd operations. However, we don't check EOF for UFFDIO_COPY into MAP_PRIVATE mappings, where the new folio does not go into the page cache, and we don't check it for UFFDIO_POISON at all. Consequently, both can install an anonymous folio or a poison marker beyond EOF, even without racing with truncation. Accessing that range then maps the stale folio or reports memory poison instead of raising SIGBUS, and once the file is extended again, the stale folio or poison marker remains instead of a fresh folio getting faulted in. Reproduced by truncating a file and using UFFDIO_COPY in a private mapping or UFFDIO_POISON on the truncated range: both succeed, and after extending the file again, accessing the page returns the copied data or raises SIGBUS. Further, artificially delaying hugetlb_mfill_atomic_pte() before allocating the folio, while another thread truncates the file, leaves the page beyond EOF mapped. Let's fix it by checking EOF under the invalidate lock for all UFFDIO_COPY and UFFDIO_POISON, and holding it until we installed the PTE. UFFDIO_CONTINUE maps an existing page cache folio under the fault mutex, which serializes against its removal on truncation. This has low urgency, as it requires UFFDIO_COPY into a private mapping or UFFDIO_POISON beyond EOF. No need to CC stable. Fixes: fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races") Fixes: 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs") Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 105cc2840f988..a23d36e7b8e93 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6559,13 +6559,31 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, bool folio_in_pagecache = false; pte_t dst_ptep; + /* + * Check EOF and keep truncation out until we mapped the new folio or + * installed the poison marker: truncation unmaps or removes it + * afterwards. + */ + if (!is_continue) { + filemap_invalidate_lock_shared(mapping); + if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) { + if (*foliop) { + folio_put(*foliop); + *foliop = NULL; + } + ret = -EFAULT; + goto out; + } + } + if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { ptl = huge_pte_lock(h, dst_mm, dst_pte); /* Don't overwrite any existing PTEs (even markers) */ if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) { spin_unlock(ptl); - return -EEXIST; + ret = -EEXIST; + goto out; } VM_WARN_ON_ONCE(size == PMD_SIZE && hugetlb_pmd_shared(dst_pte)); @@ -6576,19 +6594,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, update_mmu_cache(dst_vma, dst_addr, dst_pte); spin_unlock(ptl); - return 0; - } - - if (alloc_pagecache_folio) { - filemap_invalidate_lock_shared(mapping); - if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) { - if (*foliop) { - folio_put(*foliop); - *foliop = NULL; - } - ret = -EFAULT; - goto out; - } + ret = 0; + goto out; } if (is_continue) { @@ -6757,7 +6764,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, spin_unlock(ptl); ret = 0; out: - if (alloc_pagecache_folio) + if (!is_continue) filemap_invalidate_unlock_shared(mapping); return ret; out_release_unlock: -- 2.56.1