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=1796298199; b=szGrNWDnIRlEXbubCUEOpIDbE6aBIhAT1eqGy62fUpH5m44vAhCHr87FPYk8eL1yJf6l 54fpHewA5VvecDdESCdjj4qkdJmIRkSLG3Y7HoRudUZqu4OJppz7JB0XmMJUF94ry05mn NL+gHBFBfO0AMdBSPwRrv9fYmL9uLht63N2KHF/kTKM2flQvgZUmtZAF/TiPo3xfVZEkA 6hy5RAakJ5i87ZeCwFACFz123dKRuMb5gPYs/S1IJsj0YIowE6UmpMKwc/VBXMdYNtubK L2nKIHY8AYxxs7WW6jdG/fSgKCVrCWEBXQjZ9uFJI3rOvOGEab3H/c/MEJ4m07H1j4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298199; 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=Zpa1XXybLF4DFuVadeV/lBd0Ufvlf7BnR5lR2gRGip0=; b=GL+g0zg7qJ3WfY7ZETelZr+CQZ87+YqQjLi/sW1W+eUtxpO928g8GgCGV6u8PHm/DqTw 6NQIlv1/pBj73B4SGnX5kbNC/bhTG7n/EGynHrPBKVed7gO1vXpUWqe41y0aeWC0IAokp NahvBvpuMhQzUnwIBshTfpeIExBr7FyETz9QuPENuLr2Sh5mA6XcBZHskLfDz840UYRbC pCu1xN85USFc63QnwHEUF5XLqB8Inc7m19Wv8Gv1QwT1kzbn+gZqCkFep2e3UrldhW6UH jiW2onj2r1prHobaXiw+ka3+NUdn82hKKU1ZYpWsCCYKOjTkWMbv1ZEnQHPle5k4n2g== 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="UPMC/U2m"; 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 10286606A322; Thu, 3 Dec 2026 11:43:18 +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=UPMC/U2m Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1074D1F000C1; Thu, 3 Dec 2026 11:43:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298198; bh=Zpa1XXybLF4DFuVadeV/lBd0Ufvlf7BnR5lR2gRGip0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=UPMC/U2mFT0QEw1jSnaMvdMT30ql8dXWJVUGSo06pQCQv7GLHAV5kckinOtVm00Ty qT6yfdTV5/sg4i+EhLsZYLx4i0zu3owrbF5tSLGBoTYrlUh8gMB826lBX4Pro2BiPQ MHhvyjDEzSnFBtAu6ydrDXy9Tf/ZTFDVdkXXEtpznG4jMcJmYmeXvLMsXmCJLJaNyx F8Thm2KY/vYpqecpee5JkqiY7zYOngZdcHqizyTWyNF4GZqPkfrmVzIgTXOTJoFhfG xPoMws/ZWBOakS2997285mDXUaehgCBhhs96z9Z5iNzg7WK0mAmpCmn1g5YlWbLNBE yWleUe+iX5QnQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:45 +0100 Subject: [PATCH v2 22/35] mm/hugetlb: serialize private faults with truncation 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-22-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=4680; i=david@kernel.org; h=from:subject:message-id; bh=+TSH9+OeuCVryc3qC14DJsiEHgRogF0nnN+sFdQjjo4=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ2oELrjeO6E1g9/tiZcZq87kC6yPLyy6zZsiGt+xm efyvJSZHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEyk5BTDX7moXqkiRqMtbod6 ug46bVTo6tm5Yhu/yaLZ26XX+p5icmRk2JX9U+dx7sO0xEvCmuqXt4hMdJvw6s/9Kg6V/8+iNqS p8gMA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A When we fault in a folio in a MAP_PRIVATE hugetlb mapping at an index without a page cache folio, hugetlb_no_page() checks that the index is not beyond EOF, allocates a new anonymous folio and maps it. Truncation first updates i_size under the invalidate lock, and then unmaps the truncated range from all VMAs. For page cache folios, we recently started rechecking EOF under filemap_invalidate_lock_shared() to handle exactly that. However, nothing serializes the allocation of anonymous folios with truncation: if truncation unmaps the range after we checked EOF, but before we mapped the new folio, the folio stays mapped beyond EOF. Consequently, accessing the truncated range does not raise SIGBUS, and the process keeps the folio mapped until it unmaps it. Reproduced by artificially delaying hugetlb_no_page() before allocating the folio, while another thread truncates the file: the page beyond EOF stays mapped. Let's fix it by also rechecking EOF under the invalidate lock for anonymous folios, and holding it until we mapped the folio, such that truncation will unmap it. Like faults in shared mappings, faults in private mappings now briefly take the invalidate lock in read mode, and might have to wait for a concurrent truncation. Which seems to be the right thing to do. This has low urgency, as it requires racing a page fault in a private mapping with truncation. No need to CC stable. Fixes: fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races") Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 25 +++++++++++++++---------- mm/rmap.c | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 1555a326efe4c..105cc2840f988 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6002,6 +6002,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE; u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff); bool folio_locked = true, new_anon_folio = false; + bool invalidate_locked = false; struct vm_area_struct *vma = vmf->vma; struct mm_struct *mm = vma->vm_mm; struct hstate *h = hstate_vma(vma); @@ -6061,15 +6062,19 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, ret = __vmf_anon_prepare(vmf); if (unlikely(ret)) goto out; - } else { - /* Recheck EOF before consuming an inode reservation. */ - filemap_invalidate_lock_shared(mapping); - if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false)) { - filemap_invalidate_unlock_shared(mapping); - goto out; - } + ret = VM_FAULT_SIGBUS; } + /* + * Recheck EOF and keep truncation out until the folio is in the + * page cache, or until we mapped the new anonymous folio: + * truncation unmaps or removes it afterwards. + */ + filemap_invalidate_lock_shared(mapping); + invalidate_locked = true; + if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false)) + goto out; + folio = alloc_hugetlb_folio(vma, vmf->address, false); if (IS_ERR(folio)) { /* @@ -6088,8 +6093,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, ret = vmf_error(PTR_ERR(folio)); else ret = 0; - if (pagecache_folio) - filemap_invalidate_unlock_shared(mapping); goto out; } folio_zero_user(folio, vmf->real_address); @@ -6110,10 +6113,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, folio); folio_put(folio); ret = VM_FAULT_SIGBUS; - filemap_invalidate_unlock_shared(mapping); goto out; } filemap_invalidate_unlock_shared(mapping); + invalidate_locked = false; folio_set_hugetlb_migratable(folio); } else { new_anon_folio = true; @@ -6212,6 +6215,8 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, */ spin_unlock(vmf->ptl); out: + if (invalidate_locked) + filemap_invalidate_unlock_shared(mapping); hugetlb_vma_unlock_read(vma); /* diff --git a/mm/rmap.c b/mm/rmap.c index f35b671d26514..4193793158bc1 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -48,7 +48,7 @@ * hugetlbfs PageHuge() take locks in this order: * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) * vma_lock (hugetlb specific lock for pmd_sharing) - * mapping->invalidate_lock (when allocating page cache folios) + * mapping->invalidate_lock (when allocating folios) * folio_lock * mapping->i_mmap_rwsem (also used for hugetlb pmd sharing) */ -- 2.56.1