]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm: call ->free_folio() directly in folio_unmap_invalidate()
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 13 Apr 2026 18:43:11 +0000 (19:43 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 19 Apr 2026 06:24:27 +0000 (23:24 -0700)
We can only call filemap_free_folio() if we have a reference to (or hold a
lock on) the mapping.  Otherwise, we've already removed the folio from the
mapping so it no longer pins the mapping and the mapping can be removed,
causing a use-after-free when accessing mapping->a_ops.

Follow the same pattern as __remove_mapping() and load the free_folio
function pointer before dropping the lock on the mapping.  That lets us
make filemap_free_folio() static as this was the only caller outside
filemap.c.

Link: https://lore.kernel.org/20260413184314.3419945-1-willy@infradead.org
Fixes: fb7d3bc41493 ("mm/filemap: drop streaming/uncached pages when writeback completes")
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reported-by: Google Big Sleep <big-sleep-vuln-reports+bigsleep-501448199@google.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Jan Kara <jack@suse.cz>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/filemap.c
mm/internal.h
mm/truncate.c

index 3c1e785542dde0764e44c6c8e80ef341a005980e..793bf4816ea38f09839cccdf7a42246401f52944 100644 (file)
@@ -228,7 +228,8 @@ 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)
+static void filemap_free_folio(const struct address_space *mapping,
+               struct folio *folio)
 {
        void (*free_folio)(struct folio *);
 
index cb0af847d7d99d81e4c363ba76b54697cb242ee3..546114d3ee448785884bc2cc081cfd201b0d812e 100644 (file)
@@ -540,7 +540,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);
index 12467c1bd711eb2761eeabf3b48f561f114b4d2d..8617a12cb169564f076fb42539463efb6baa02cf 100644 (file)
@@ -622,6 +622,7 @@ static int folio_launder(struct address_space *mapping, struct folio *folio)
 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio,
                           gfp_t gfp)
 {
+       void (*free_folio)(struct folio *);
        int ret;
 
        VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
@@ -648,9 +649,12 @@ int folio_unmap_invalidate(struct address_space *mapping, 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));
        return 1;
 failed:
        xa_unlock_irq(&mapping->i_pages);