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=1794630960; b=3kzcNnxvANHleDgfh9qMKPKrpZXL3VYk+usTwmvqGwhT1Bdga1+8N22QKFD2gnhkPQD4 gZNUCxT49Wn9jSMWujCkRzZNVgojJu+Ku5m3h/ACUoSkmgd5RMV3g/Ymhyot6ZdpOMRQv wlrCmFERShWQWPXyPSVqeiAUPBVmwbCoOKrsI6Mbsc+1sC4Om0Smo0xxPgfYsses0kR4I fYX1/rMil1EUsGEu/4020FKftP2ejd41dtyBvExiRd37jLEkVjj2haB9wpRcB/IBF3Gf4 TSI3N6z+dj/zyjMbzvtxfZa8bKwivUyQXs69Z6AudaLalrvOydYI2TXevrHivaoh0Ig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794630960; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=ftTfvOjsHib5LsO1tGG0tgMVVCJRSR7z728qBAjvUVc=; b=NZx0e49Ka6SduEEDICYkdbj2T8lBP14PPfIuENAlX2ggqCDgHFgQKyKAcQIpAWsNcRCJ rtf/lfJi7Ouxdp5GKQtNUxWPp8d7a8+3ms3OBtON6O2hI8TrIydTkUL6rpF5SbY22JqrE zJ9zjaIpTD/p11VpzouJMS6X1QTYg9uFA94QopWaHQIEzUYiDd95QTulLf3OLPDIY7pYt xaYRFnYE0CbrIB14SKW6WTe7Hi4SlY892C/kK3EOAzSgr38VRCr+MhgUYTyaOf95MyL65 h9SmcXGncMqF2GutE/dv73nX1aZ4z7RbIQrTDm6WCORSnEANTN9ca6UlZ+yL1ohY9pQ== 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=RW0mzAoa; 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 3100B606A327 for ; Sat, 14 Nov 2026 04:35:59 +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=RW0mzAoa Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0D861F000C4; Sat, 14 Nov 2026 04:35:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794630959; bh=ftTfvOjsHib5LsO1tGG0tgMVVCJRSR7z728qBAjvUVc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RW0mzAoanRPj7XiGP+D6cuZX91hPD9fBcZfAwxHK8WHgfb0suwkaPQVZtzHVkZUfb s9MgaVHnLZMCPduVdkeUbFyqWb05uy/0BKr4myzgl/mxPNcKuOglsfABcllpGyGHih Hg/WhUzoC3qfGScc3s0tsIXwTuWwvCrH4+J58WdKlzlq08/9fk3jwOdnEtVYr5sZJ2 RQPq9/xHf8h2mlGp0Yxjy1L5jluz+l7nvmcqv8w4rzitI1SuuPd9OWLQYmHR50Atg3 EmS/1OKtKEezDbCQA1YbmHu9x6I0i0h3rVsNIWZtEMMFSFUq5oNdML32WLOWcj+wHb gI4U0Riq43kOA== From: Sasha Levin To: stable@vger.kernel.org Cc: Josef Bacik , Rik van Riel , Muhammad Usama Anjum , Zi Yan , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 6.12.y] mm/filemap: read mapping->a_ops->free_folio under i_lock Date: Fri, 13 Nov 2026 23:35:57 -0500 Message-ID: <20261114042915.2896115-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261109200707-failed-6.12-dc24e273244e@kernel.org> References: <20261109200707-failed-6.12-dc24e273244e@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Josef Bacik [ Upstream commit dc24e273244e9d712257d5a6c20a32c7f5ca84e6 ] memory_failure() -> ... -> filemap_remove_folio() can read mapping->a_ops after the inode has been freed. Once the folio is deleted from the pagecache it no longer pins the mapping, so after i_lock is dropped e.g., a concurrent final iput() can evict and free the inode before filemap_free_folio() looks up mapping->a_ops->free_folio. Most callers hold a reference on the inode, but not all. Documentation/filesystems/vfs.rst explicitly states that ->free_folio() must not assume the original address_space mapping still exists. Fix it by loading ->free_folio while i_lock is still held, similar to what folio_unmap_invalidate() does. Inline filemap_free_folio() into its remaining caller such that delete_from_page_cache_batch() also just loads the callback before dropping the i_lock. The bug was found by code inspection and has not been reproduced. Fix generated by an LLM and sent by Josef Bacik in private. Fixes: 59c66c5f8c4f ("mm: factor out page cache page freeing into a separate function") Cc: stable@vger.kernel.org Signed-off-by: Josef Bacik [ david: get rid of filemap_free_folio() entirely; improve patch description ] Reviewed-by: Rik van Riel Tested-by: Muhammad Usama Anjum Reviewed-by: Zi Yan Link: https://patch.msgid.link/20261031-fixes_core-v3-6-d6437171ecc3@kernel.org Signed-off-by: David Hildenbrand (Arm) [ sashal: adapted filemap_free_folio() removal to the additional invalidate_complete_folio2() caller by inlining its cleanup ] Signed-off-by: Sasha Levin --- mm/filemap.c | 31 ++++++++++++++----------------- mm/internal.h | 1 - mm/truncate.c | 7 ++++++- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 602106b329bc8..76efdb1e1b784 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -224,20 +224,6 @@ void __filemap_remove_folio(struct folio *folio, void *shadow) page_cache_delete(mapping, folio, shadow); } -void filemap_free_folio(struct address_space *mapping, struct folio *folio) -{ - void (*free_folio)(struct folio *); - int refs = 1; - - free_folio = mapping->a_ops->free_folio; - if (free_folio) - free_folio(folio); - - if (folio_test_large(folio)) - refs = folio_nr_pages(folio); - folio_put_refs(folio, refs); -} - /** * filemap_remove_folio - Remove folio from page cache. * @folio: The folio. @@ -249,6 +235,7 @@ void filemap_free_folio(struct address_space *mapping, struct folio *folio) void filemap_remove_folio(struct folio *folio) { struct address_space *mapping = folio->mapping; + void (*free_folio)(struct folio *folio); BUG_ON(!folio_test_locked(folio)); spin_lock(&mapping->host->i_lock); @@ -257,9 +244,12 @@ void filemap_remove_folio(struct folio *folio) xa_unlock_irq(&mapping->i_pages); if (mapping_shrinkable(mapping)) inode_add_lru(mapping->host); + free_folio = mapping->a_ops->free_folio; spin_unlock(&mapping->host->i_lock); - filemap_free_folio(mapping, folio); + if (free_folio) + free_folio(folio); + folio_put_refs(folio, folio_nr_pages(folio)); } /* @@ -319,6 +309,7 @@ static void page_cache_delete_batch(struct address_space *mapping, void delete_from_page_cache_batch(struct address_space *mapping, struct folio_batch *fbatch) { + void (*free_folio)(struct folio *folio); int i; if (!folio_batch_count(fbatch)) @@ -336,10 +327,16 @@ void delete_from_page_cache_batch(struct address_space *mapping, xa_unlock_irq(&mapping->i_pages); if (mapping_shrinkable(mapping)) inode_add_lru(mapping->host); + free_folio = mapping->a_ops->free_folio; spin_unlock(&mapping->host->i_lock); - for (i = 0; i < folio_batch_count(fbatch); i++) - filemap_free_folio(mapping, fbatch->folios[i]); + for (i = 0; i < folio_batch_count(fbatch); i++) { + struct folio *folio = fbatch->folios[i]; + + if (free_folio) + free_folio(folio); + folio_put_refs(folio, folio_nr_pages(folio)); + } } int filemap_check_errors(struct address_space *mapping) diff --git a/mm/internal.h b/mm/internal.h index 7aca909c54052..a9f0ed8fa3f2b 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -402,7 +402,6 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); -void filemap_free_folio(struct address_space *mapping, struct folio *folio); int truncate_inode_folio(struct address_space *mapping, struct folio *folio); bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end); diff --git a/mm/truncate.c b/mm/truncate.c index fb5c20b57bd4f..8ed9ae8d6c1ef 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -574,6 +574,8 @@ EXPORT_SYMBOL(invalidate_mapping_pages); static int invalidate_complete_folio2(struct address_space *mapping, struct folio *folio) { + void (*free_folio)(struct folio *folio); + if (folio->mapping != mapping) return 0; @@ -590,9 +592,12 @@ static int invalidate_complete_folio2(struct address_space *mapping, xa_unlock_irq(&mapping->i_pages); if (mapping_shrinkable(mapping)) inode_add_lru(mapping->host); + free_folio = mapping->a_ops->free_folio; spin_unlock(&mapping->host->i_lock); - filemap_free_folio(mapping, folio); + if (free_folio) + free_folio(folio); + folio_put_refs(folio, folio_nr_pages(folio)); return 1; failed: xa_unlock_irq(&mapping->i_pages); -- 2.53.0