From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4440D351C2E for ; Wed, 29 Apr 2026 17:22:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777483353; cv=none; b=rlFDkQQeousUrcbY+pCCucdGlJaDl+zPjnPvQ9WTNAuPY8LtqzanfF3QxGJEge/RzuI8rV8XVQbrgy3VBAgvYw7tU5usR13sbV0lSc9SJcxYqXlL9UMyCJ1F5vo6S4WHGBd6BrAhrLAfqPaIpGMKIN67YVQZKFAobfhVS0RIg9w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777483353; c=relaxed/simple; bh=I2Lzahi4p9chd43/j4jr1ieIgQq9gI0N1B7Hc+W8Dvo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sQx0xT7i0+YEcPneTiewtaW6pTCizzbbiIKb1WsU9de2vzcyBy7AA4690qBZQ07qqnISgW6PE8FCSfvSzPYzIK9c99Shok4FfTE6Dub3fO4Q0wPyH0CvvO/wNzfqlK7sFpsYOUrZYVO5ixgV3l7JIMwDClJoXNyZCiVt0pVqVjo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=rMhejc9B; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="rMhejc9B" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=A6rP5vBvDu723X9ku3riKFaXXtcZY2E5Iv2O76v2kgQ=; b=rMhejc9BD4NBw3iAnczuReCv19 hZH2OLMe/smIdmkpnsZYvrlJAkH+dDJ5t8hNRUSB4PKP7WUVdotVAk7H8T9Aymr1VVL4M0ccfE4jZ dlv3Pq3w1L/DPoH4i3EEd3UfxFkzdGBcO0+lTcHoKPX3csn038wNE9J2W3EQvFyfATv9+8Y7pu96P 2virLBBWcDuH/kVJ+bW5xbwZgIkn9NRQ7mKG6hW1tgLdm/rM50RfWoPiHdcTClfkpRJz+mxGXpqx/ EhjWPyLApkVbzcCifnftPZzhqZPZEepgqCh9wBToZbJPqM1Al1YjoFHzA256f6Ynk2zA+l2aYBTrV LnF6RLig==; Received: from willy by casper.infradead.org with local (Exim 4.98.2 #2 (Red Hat Linux)) id 1wI8cC-00000005seX-3JZv; Wed, 29 Apr 2026 17:22:24 +0000 From: "Matthew Wilcox (Oracle)" To: stable@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , Google Big Sleep , Jens Axboe , Jan Kara , Andrew Morton Subject: [PATCH 6.18.y] mm: call ->free_folio() directly in folio_unmap_invalidate() Date: Wed, 29 Apr 2026 18:22:22 +0100 Message-ID: <20260429172222.1401701-1-willy@infradead.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <2026042002-idealness-evade-7213@gregkh> References: <2026042002-idealness-evade-7213@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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) Reported-by: Google Big Sleep Cc: Jens Axboe Cc: Jan Kara Cc: Signed-off-by: Andrew Morton (cherry picked from commit 615d9bb2ccad42f9e21d837431e401db2e471195) --- mm/filemap.c | 3 ++- mm/internal.h | 1 - mm/truncate.c | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 91dcfe14a67b..e3339cf37a1f 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -227,7 +227,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 *); diff --git a/mm/internal.h b/mm/internal.h index c80c6f566c2d..4cf6a12ff491 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -471,7 +471,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 3c5a50ae3274..4bf64fd610c9 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -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_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.47.3