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=1793056824; b=NWS92QRbfTHl9NmbofSTc3C6NqHh3VSXOZrX9N6OzxskrOqCexzFrSm2JuAd8ztvia1W WRvYLmxoEfpMn0gHFIUpFFXf11Hi35b5YhCp6RWBeF5QNNY9Lb0+ESjnDmP8EVLEGEC/v nY666kVk0s9bvNyuDVfcAvvxO4dbQE3PCgRglFtBGX6M/rRMoY3ANARf6YRGKrpRgswC3 AfWMjwaL1SOV1Xq2BM1IlT/nfbZeuynytJRLk7SV1aX3ekVbwyNZI9PhTqGH2IHzwWOhX n8ZqG70m3fhBaZ5qkIzf2HHhXi1yI6DsbGV3kg1mXB3/J+gGXBKYipSJyOmuvWdDeWw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793056824; 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=hyfT9UeYB82/Suh3401TnHPdPR4zRtp/VCfqHa3Pal8=; b=jVvhwYUwoQSks02Tpkp/smraoUHEff3caV/jqyQmWUKmyBdrIIe3t1WyY/S0EBIrPy8F ZRD10itSPVtWv27hGtic91KvFxtHGOq8Cqbptm5mzg4NUfEDnxyTaTe5tk80BEAeQlbWR knQECFQFAxPTWOlQLFKR+ihE5kQU1rm3X7QWAPTgZAPpnJh2dmNxdUwuI9h06D2aoMff4 qGRh2P3T4izmd5No/x4cs4cJs//P2IGjXzqGoeUpc9ZcOM4xNUT88sat6+EBEMUcA0RDd SfvG/TamLLQngekpkhTSBEiuV8E/0vhqQRJ4iQsZtTlzbzV64fDUPsY5vIg4a9Yfzkw== 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=U97lsdiu; 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 E60E5606A31A; Mon, 26 Oct 2026 23:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=U97lsdiu Received: by smtp.kernel.org (Postfix) with ESMTPSA id 999B61F01828; Mon, 26 Oct 2026 23:20:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793056823; bh=hyfT9UeYB82/Suh3401TnHPdPR4zRtp/VCfqHa3Pal8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=U97lsdiuuUuPQkUAKKTWJuTgmH9WwQHN0iCh+iz6wbLPc5h+rl05TXy57cN86b+/Q FI4xdeXdn6F4RwcU+GrHn4zbLcRBGAyKA0Lp94FBikfdN8ltNLD6PqHgZ/Qn+xkRM5 ckNapyH5LMmD/Nv7BZRtRCk9aTWJxuFyhfp8YxDlk+ZGHwanXE/Viut+BZGtWwN+Tl C/3i0Eaa6ClOJyTpfYezrhNOGzQTHbcD94eIRkISQHhuJQHkpvFsOdxrAHdvB1oYou ctYbGfccTpd5kBRo6Z34hbpbaAMaG+Ij1gx1yy1hr/uPLT7j/mYnTvcQ+JcEReUht9 s/mnuLs9M7+pA== From: "David Hildenbrand (Arm)" Date: Tue, 27 Oct 2026 00:17:28 +0100 Subject: [PATCH 08/46] mm/filemap: finish fault cleanup before dropping the fault lock X-Mailing-List: linux-kernel@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: <20261027-fixes_core-v1-8-6f74bec9a5bb@kernel.org> References: <20261027-fixes_core-v1-0-6f74bec9a5bb@kernel.org> In-Reply-To: <20261027-fixes_core-v1-0-6f74bec9a5bb@kernel.org> To: Andrew Morton , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Baolin Wang , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kees Cook , Peter Zijlstra , Jann Horn , Pedro Falcato , Hugh Dickins , Pasha Tatashin , Pratyush Yadav , "Matthew Wilcox (Oracle)" , Jan Kara , Zi Yan , Lance Yang , Rik van Riel , Harry Yoo , Alistair Popple , Nico Pache , Ryan Roberts , Dev Jain , Usama Arif , Kiryl Shutsemau , Matthew Brost , John Hubbard , Peter Xu , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Oscar Salvador , Minchan Kim , Muchun Song , Xu Xin , Chengming Zhou , Chris Li , Kemeng Shi , Nhat Pham , Youngjun Park , Gregory Price , Joshua Hahn , Rakie Kim , Ying Huang , Jason Gunthorpe Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-cxl@vger.kernel.org, stable@vger.kernel.org, Chris Mason , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4015; i=david@kernel.org; h=from:subject:message-id; bh=53xEYTkQp6IA0uXj6MOe1vM8W7peAp/MfOvPesJ6Ry0=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLLu358cKuG90E2q9Vcd72l1E0eTh6vv8Uvc2KRaes5xD /e1U5/mdJSyMIhxMMiKKbIEBd83ePlyXW21Gx8zzBxWJpAhDFycAjCRVQkM/0O+p3RP//jQ+0D6 hjiOc2uW3FZ7Y//nAetjz8/HDU8uUpdkZLiUlzPnxGrjWVNvzWdXOsCVUvPzyUpVwdSczgtBD1p zj3ABAA== X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A On a retried fault, a fatal signal interrupting the folio lock wait makes lock_folio_maybe_drop_mmap() drop the fault lock without pinning the file. However, filemap_fault() may still need to release mapping->invalidate_lock. A concurrent munmap() can release the last file reference and free the mapping before that unlock. Defer dropping the fault lock to filemap_fault()'s retry cleanup, after releasing invalidate_lock. If the fault lock was already dropped for I/O, release the existing file reference instead. Keep the fault lock held for FAULT_FLAG_RETRY_NOWAIT as before. Fixes: 730633f0b7f9 ("mm: Protect operations adding pages to page cache with invalidate_lock") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/filemap.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 6b5e1d866a9e0..b04be64a5069f 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3308,11 +3308,11 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, * @folio - the folio to lock. * @fpin - the pointer to the file we may pin (or is already pinned). * - * This works similar to lock_folio_or_retry in that it can drop the - * mmap_lock. It differs in that it actually returns the folio locked - * if it returns 1 and 0 if it couldn't lock the folio. If we did have - * to drop the mmap_lock then fpin will point to the pinned file and - * needs to be fput()'ed at a later point. + * Returns 1 with the folio locked, or 0 if locking failed. + * + * On both entry and return, a non-NULL *fpin means the fault lock has + * already been dropped and the caller must eventually fput() the file. + * Otherwise the fault lock is still held, including on failure. */ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio, struct file **fpin) @@ -3329,21 +3329,9 @@ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio, return 0; *fpin = maybe_unlock_mmap_for_io(vmf, *fpin); - if (vmf->flags & FAULT_FLAG_KILLABLE) { - if (__folio_lock_killable(folio)) { - /* - * We didn't have the right flags to drop the - * fault lock, but all fault_handlers only check - * for fatal signals if we return VM_FAULT_RETRY, - * so we need to drop the fault lock here and - * return 0 if we don't have a fpin. - */ - if (*fpin == NULL) - release_fault_lock(vmf); - return 0; - } - } else - __folio_lock(folio); + if (vmf->flags & FAULT_FLAG_KILLABLE) + return !__folio_lock_killable(folio); + __folio_lock(folio); return 1; } @@ -3572,7 +3560,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) * vma->vm_mm->mmap_lock must be held on entry. * * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock - * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap(). + * may be dropped before doing I/O or when aborting an interrupted wait. * * If our return value does not have VM_FAULT_RETRY set, the mmap_lock * has not been released. @@ -3728,9 +3716,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) out_retry: /* - * We dropped the mmap_lock, we need to return to the fault handler to - * re-find the vma and come back and find our hopefully still populated - * page. + * With fpin set, the fault lock was already dropped. Otherwise it is + * still held. Release invalidate_lock before dropping either protection. */ if (!IS_ERR(folio)) folio_put(folio); @@ -3738,6 +3725,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) filemap_invalidate_unlock_shared(mapping); if (fpin) fput(fpin); + else if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) + release_fault_lock(vmf); return ret | VM_FAULT_RETRY; } EXPORT_SYMBOL(filemap_fault); -- 2.43.0