FAILED: patch "[PATCH] mm/filemap: read mapping->a_ops->free_folio under i_lock" failed to apply to 6.6-stable tree
Subsystems (3)
Sasha Levin
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x dc24e273244e9d712257d5a6c20a32c7f5ca84e6
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From dc24e273244e9d712257d5a6c20a32c7f5ca84e6 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Sat, 31 Oct 2026 10:39:35 +0100
Subject: [PATCH] mm/filemap: read mapping->a_ops->free_folio under i_lock
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.
--- mm/filemap.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
mm/filemap.c +14 -15
@@ -227,18 +227,6 @@ void __filemap_remove_folio(struct folio *folio, void *shadow)page_cache_delete (mapping, folio, shadow); } -static void filemap_free_folio (const struct address_space *mapping, -struct folio *folio) -{ -void (*free_folio)(struct folio *); - - free_folio = mapping->a_ops->free_folio; -if (free_folio) -free_folio (folio); - -folio_put_refs (folio,folio_nr_pages (folio)); -} -/** * filemap_remove_folio - Remove folio from page cache. * @folio: The folio.
@@ -250,6 +238,7 @@ static void filemap_free_folio(const struct address_space *mapping,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);
@@ -258,9 +247,12 @@ void filemap_remove_folio(struct folio *folio)xa_unlock_irq (&mapping->i_pages);if (mapping_shrinkable (mapping))inode_lru_list_add (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)); }/*
@@ -320,6 +312,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))
@@ -337,10 +330,16 @@ void delete_from_page_cache_batch(struct address_space *mapping,xa_unlock_irq (&mapping->i_pages);if (mapping_shrinkable (mapping))inode_lru_list_add (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)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/1 mm/filemap: read mapping->a_ops->free_folio under i_lock
From: Josef Bacik <[email protected]>
[ 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.
--- mm/filemap.c | 37 ++++++++++++++++++++----------------- mm/internal.h | 1 - mm/truncate.c | 10 +++++++++- 3 files changed, 29 insertions(+), 19 deletions(-)
mm/filemap.c +20 -17
@@ -225,20 +225,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.
@@ -250,6 +236,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);
@@ -258,9 +246,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); }/*
@@ -320,6 +313,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))
@@ -337,10 +331,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)
mm/internal.h +0 -1
@@ -180,7 +180,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);
mm/truncate.c +9 -1
@@ -594,6 +594,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 ;
@@ -610,9 +613,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);
Signature · 1 line
2.53.0