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=1794631570; b=l9FL59npw2v1dkSliNElEk937bMr/uF0f7QHhtmTj7m4c6fSOZn40p3hkjJY9C+g2pPk 2tK1cxpAwaMwNUK1wGc3ewS/YC8qRjInRHBlVFYaBP6zbKyApolrKEzHLqwrdB/lQNlDi BMPLH8m2ocegixkvf6ga2GILSXDcv3otfhdj+tf7h8zup+gX8Grl6jDGg7EYmeCacLPIL FH6hqFuHshBNlmIrVgJcd6H0Q7oK0wgyNp/NBnPe5zLc+U1q2UVBkijJ0hJeS5bToQu9E pOIIQWMxdsCr74dL5I4f19cvLxDbLNrkf4d9fDiX7YJ55MhO3V8SFiCoBNHtRxMSlmA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794631570; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Yti36HqVRybzVq+dQbPwiEsZsL/TlW5Vyd2EZrAhbWQ=; b=huApBSd7SbPOgAKsothh+5x5nCCe4LBjYNuI+cnzm6vnHUQoFDoXWNUGtLaLb57hjlfa rMochl06sWYXheB0MDnxz1AYkfS7yJ0AFAySeP1yeU1PPCsfZ/DfkXsy5ucPiyDpgVBxb cSNydbQZ7MUVdEBQsdq+kuf/g5AT3MqN+0kUWdM3CcA228w2JZ2rrONERey03DmnGGCov HfdFhuLHaJf0Sa61kBuBBpjJ+duoZcsTvJnA0ljavPnscmugkVGraO2u665XiULPsQ2c6 Ee51Fj/cNHo+zUI5tuT+Q3qFCkriaFemfHfirwQB9+tW3HhisGevB3vfr9C84rod5Pw== 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=XuqDt+Pu; 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 0489E606A330 for ; Sat, 14 Nov 2026 04:46:09 +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=XuqDt+Pu Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3E541F000C4; Sat, 14 Nov 2026 04:46:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794631569; bh=Yti36HqVRybzVq+dQbPwiEsZsL/TlW5Vyd2EZrAhbWQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XuqDt+PuqkbrCXsL47NKVbvrEK02XSmYpel5QavW2KqcosvONceu6ayGCwzkj485C 9QfQgS5JjsZDkSIix5uBJS1Ilvkj96/KIyeo1SXhjeVDyfefuwQBR2WL9uzpDptGgk GuYi9I6aDL1p72iFqkLGWtWLxKw2XnibmSBfihcbZ/pb5yG1K6GFZx8j1vjc/ULrwz 9WxUKkxf0ghV9cq+9R2XRz6sipZXmBoXYWq468QfNCi10yuLx69EBrZPMwMEXpqprN C83MTOXNhW5OzTQ3zxZh/Vxa8YZAMn94XWWI2BvLaQmv7VKqOsO8OJNGK8qz2VLq1T WtBBsIA4xOjjg== 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.1.y] mm/filemap: read mapping->a_ops->free_folio under i_lock Date: Fri, 13 Nov 2026 23:46:07 -0500 Message-ID: <20261114044555.2958175-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261109201558-failed-6.1-dc24e273244e@kernel.org> References: <20261109201558-failed-6.1-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: preserved hugetlb-aware reference counting in the inlined cleanup, including the additional invalidate_complete_folio2() caller ] Signed-off-by: Sasha Levin --- mm/filemap.c | 37 ++++++++++++++++++++----------------- mm/internal.h | 1 - mm/truncate.c | 10 +++++++++- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 7c996dc4dad28..61581b59d0ff3 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -223,20 +223,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) && !folio_test_hugetlb(folio)) - refs = folio_nr_pages(folio); - folio_put_refs(folio, refs); -} - /** * filemap_remove_folio - Remove folio from page cache. * @folio: The folio. @@ -248,6 +234,8 @@ 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); + int refs = 1; BUG_ON(!folio_test_locked(folio)); spin_lock(&mapping->host->i_lock); @@ -256,9 +244,14 @@ 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); + if (folio_test_large(folio) && !folio_test_hugetlb(folio)) + refs = folio_nr_pages(folio); + folio_put_refs(folio, refs); } /* @@ -318,6 +311,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)) @@ -335,10 +329,19 @@ 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]; + int refs = 1; + + if (free_folio) + free_folio(folio); + if (folio_test_large(folio) && !folio_test_hugetlb(folio)) + refs = folio_nr_pages(folio); + folio_put_refs(folio, refs); + } } int filemap_check_errors(struct address_space *mapping) diff --git a/mm/internal.h b/mm/internal.h index f99d9812c48d7..dd27162f310c1 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -129,7 +129,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 cd6768ca5b975..785bfc4147e8b 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -597,6 +597,9 @@ EXPORT_SYMBOL(invalidate_mapping_pages); static int invalidate_complete_folio2(struct address_space *mapping, struct folio *folio) { + void (*free_folio)(struct folio *folio); + int refs = 1; + if (folio->mapping != mapping) return 0; @@ -613,9 +616,14 @@ 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); + if (folio_test_large(folio) && !folio_test_hugetlb(folio)) + refs = folio_nr_pages(folio); + folio_put_refs(folio, refs); return 1; failed: xa_unlock_irq(&mapping->i_pages); -- 2.53.0