mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1] xfs: stop buftarg shrinkers before draining buffers on teardown
@ 2026-12-05 13:25 Jiaming Zhang
  0 siblings, 0 replies; only message in thread
From: Jiaming Zhang @ 2026-12-05 13:25 UTC (permalink / raw)
  To: Carlos Maiolino, linux-xfs
  Cc: Darrick J. Wong, Dave Chinner, linux-kernel, Jiaming Zhang, stable

In XFS, every cached metadata buffer holds a reference (b_pag) to its
allocation group until xfs_buf_destroy() frees the buffer.  During
unmount and failed-mount cleanup, XFS drains the buffer LRU list via
xfs_buftarg_drain() before freeing per-AG objects, assuming all cached
buffers are gone once bt_lru is empty:

	/* loop until there is nothing left on the lru list. */
	while (list_lru_count(&btp->bt_lru)) {
		list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
			      &dispose, LONG_MAX);

Since commit 430cbeb86fdc ("xfs: add a lru to the XFS buffer cache"),
each buffer target also registers a slab shrinker that stays active
until xfs_mount_free() runs after xfs_unmountfs().  When reclaiming
buffers, xfs_buftarg_shrink_scan() moves them from bt_lru to a local
dispose list before calling xfs_buf_destroy() outside the LRU lock.

If a shrinker scan moves buffers onto dispose, xfs_buftarg_drain() sees
an empty bt_lru and returns while those buffers are still alive.  Then
xfs_free_perag_range() logs a false corruption error for the non-zero
refcount and frees the per-AG object via RCU.  If RCU finishes before
xfs_buf_destroy() runs, xfs_group_put() writes to freed slab memory.

This corrupts slab memory and triggers a KASAN slab-use-after-free
report when unmount or a failed mount races with slab reclaim under
memory pressure or /proc/sys/vm/drop_caches.

Stop and wait on bt_shrinker via a new xfs_buftarg_stop_shrinker()
helper before xfs_buftarg_drain() in xfs_unmountfs() and mount error
cleanup, rather than inside xfs_buftarg_drain() which also runs on live
mounts.  Keep the call in xfs_destroy_buftarg() (a no-op once NULL) to
cover in-memory targets and early mount failures.

Fixes: 430cbeb86fdc ("xfs: add a lru to the XFS buffer cache")
Closes: https://lore.kernel.org/lkml/CANypQFY-+TvJ3Q47ExEwwAP1m9JJn5FFVmkA4-_M02oZRw6Xaw@mail.gmail.com/
Cc: stable@vger.kernel.org
Assisted-by: Antigravity:Gemini
Signed-off-by: Jiaming Zhang <r772577952@gmail.com>
---
 fs/xfs/xfs_buf.c   |  8 +++++++-
 fs/xfs/xfs_buf.h   |  1 +
 fs/xfs/xfs_mount.c | 12 ++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c
index 6c93b4f5629c..12ed5015197d 100644
--- a/fs/xfs/xfs_buf.c
+++ b/fs/xfs/xfs_buf.c
@@ -1633,11 +1633,17 @@ xfs_buftarg_shrink_count(
 	return list_lru_shrink_count(&btp->bt_lru, sc);
 }
 
+void xfs_buftarg_stop_shrinker(struct xfs_buftarg *btp)
+{
+	shrinker_free(btp->bt_shrinker);
+	btp->bt_shrinker = NULL;
+}
+
 void
 xfs_destroy_buftarg(
 	struct xfs_buftarg	*btp)
 {
-	shrinker_free(btp->bt_shrinker);
+	xfs_buftarg_stop_shrinker(btp);
 	ASSERT(percpu_counter_sum(&btp->bt_readahead_count) == 0);
 	percpu_counter_destroy(&btp->bt_readahead_count);
 	list_lru_destroy(&btp->bt_lru);
diff --git a/fs/xfs/xfs_buf.h b/fs/xfs/xfs_buf.h
index a4729253b56f..e509c1858b24 100644
--- a/fs/xfs/xfs_buf.h
+++ b/fs/xfs/xfs_buf.h
@@ -356,6 +356,7 @@ xfs_buf_update_cksum(struct xfs_buf *bp, unsigned long cksum_offset)
 struct xfs_buftarg *xfs_alloc_buftarg(struct xfs_mount *mp,
 		struct file *bdev_file);
 extern void xfs_free_buftarg(struct xfs_buftarg *);
+void xfs_buftarg_stop_shrinker(struct xfs_buftarg *btp);
 extern void xfs_buftarg_wait(struct xfs_buftarg *);
 extern void xfs_buftarg_drain(struct xfs_buftarg *);
 int xfs_configure_buftarg(struct xfs_buftarg *btp, unsigned int sectorsize,
diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index 60931cb00817..427ec42cd324 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -842,6 +842,16 @@ xfs_rtbtree_compute_maxlevels(
 				      mp->m_rtrefc_maxlevels);
 }
 
+static void xfs_stop_buftarg_shrinkers(struct xfs_mount *mp)
+{
+	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
+		xfs_buftarg_stop_shrinker(mp->m_logdev_targp);
+	if (mp->m_rtdev_targp && mp->m_rtdev_targp != mp->m_ddev_targp)
+		xfs_buftarg_stop_shrinker(mp->m_rtdev_targp);
+	if (mp->m_ddev_targp)
+		xfs_buftarg_stop_shrinker(mp->m_ddev_targp);
+}
+
 /*
  * This function does the following on an initial mount of a file system:
  *	- reads the superblock from disk and init the mount struct
@@ -1297,6 +1307,7 @@ xfs_mountfs(
  out_inodegc_shrinker:
 	shrinker_free(mp->m_inodegc_shrinker);
  out_fail_wait:
+	xfs_stop_buftarg_shrinkers(mp);
 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
 		xfs_buftarg_drain(mp->m_logdev_targp);
 	xfs_buftarg_drain(mp->m_ddev_targp);
@@ -1379,6 +1390,7 @@ xfs_unmountfs(
 	 * the log and writing the unmount record.
 	 */
 	xfs_set_done_with_log_incompat(mp);
+	xfs_stop_buftarg_shrinkers(mp);
 	xfs_log_unmount(mp);
 	xfs_da_unmount(mp);
 	xfs_uuid_unmount(mp);

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-05 13:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05 13:25 [PATCH v1] xfs: stop buftarg shrinkers before draining buffers on teardown Jiaming Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®