From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796477135; b=By+w+iqntBHKHzc6K7whxrPHoS8exaF8DWdzTznm967ZB90kQRu2QBmWRFxhkyUgztQU b336AREzJ+ih2QL7Nl1kPKhJ+JbqfS0CiNcvo2hVzxWz3PNs50FDrEVzfDoDNSgD5Qwtj xr/hC7vgsfFwOxUD6WFMJgbp3gmkoV+fZQTIS5TumcBFpQg47RYAcT/mt70hoX8RPCTri +vVzoz1NKa+p0IbEbcIyH+FtQf/H17t3BU9OOZe0sDfdiyVcQ8hUKsjsjSNetqomXRLHF moJC4a1f8GFXDFKryTmWUnkTPMKimM6QKDPip2OJ4lRu2vxunZnVMclOueLFULl9IRQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477135; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=6z61zASaNaHGf1YkHKEsgPbSy4wkBNIsS8PKI4iXfKQ=; b=wjQHrhXQ+tb43gHl8uQxfKXgMVOSTVhSRlWLYqINyM/gSvj9/KI74W5MrEkIju1B2s8O KCG7rhPjLmDk6xdgXxNnMsha1uKA8GPtVSj+3d68Q+c1bw2G/+RZYoflAEaJMatBJyrR1 xR2gD6UhiUwOOpoX7FBXwZX7dmnl6V9dMyJGzmmjSg8eR0qSznmkOl8rn1glZiQXqz/FM rerm+ZdUdIn0wbr2S5P3R5Cw5UQJ2pHKicXBmBdntEkG6WQtgX21dPamX+d7u4pns4Qlo s+TShyKn412JDsq8+M5Tsb4DCBlbaaKTqVzPcK2GD0sWuelt0kna3WVa1zV9JY+qB2g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=W5CJOiuj; arc=none smtp.remote-ip=209.85.215.171 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE1AB606A32F for ; Sat, 5 Dec 2026 13:25:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AE1AB606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=W5CJOiuj Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-cde0daf3a2aso782578a12.3 for ; Sat, 05 Dec 2026 05:25:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796477134; x=1797081934; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=6z61zASaNaHGf1YkHKEsgPbSy4wkBNIsS8PKI4iXfKQ=; b=W5CJOiujtK81xLnutm4j5ETcPQWDPZTxWLOJKv4/BWG9C5GWA3BX5FAWpi2gn+APvh jm0td31C76tWxRPX1r821nA49+h/h4zlXi/Uhe9w2cDOv3dRLsOuj4tH4w3NH03qzcJ/ d4VJ/M8coobrHPfVGbStKvZ8tVG2qbts/mPoVTUfk+0uQLbLqlUT+tqV42fzD/86hFXV cetgcKbxMosQVucE82SJskzxicssaY8Bpl2YHuoDmATVjwQIOsQpPyDU4xP4nTcHFUcr uGiTfrKvSgcsznAOyPvZF5yCUQcp0oz77OaJsJEk1d9jT+r+SkwIAIAs7WxnbcgRhpPv JAHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796477134; x=1797081934; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6z61zASaNaHGf1YkHKEsgPbSy4wkBNIsS8PKI4iXfKQ=; b=o3AlQ+230lH8gTlomi3neEzbcmgTh5OuihZxpCLK4B3WXc57oQkrxJSkElnAOVIlun ceshPlXvYRORylV+fd7XLMf0am78kFZjZbMh4kylQPnQ4pMxQakusqB9RZo/5qFlJKIj /JAl6nfdTxltt+tyNv1H0npiGCV4B+z0WKEJgfzT/eRxohctdwKqUgvCmwXnIsZvMwEr HhptMfsWRnl/NrpgLvX7NQagk1opinceRbew3Kpot0XcLSF3Wc7Jqne3kKtCGREF7/DQ ZNLCs6FV+Bdlv/FSJGymcTswo84IGKdIGTWBYnsmodgPJytRly5hkuP92XAFBvFod5MD TcMA== X-Forwarded-Encrypted: i=1; AL+hErGmPDq5ikQQima0pLgjH2JMY3yUMcgPng1tFm66mXieYCo9FBHUW/CczpYNyhOZnBhpVw7ASEnj1Uz+NMQ=@vger.kernel.org X-Gm-Message-State: AB4q+uFzDX6VaSk6YMhe0hV5It6jUTOxMqlIET0pkgv5WitzBBZlYJIY YbglCAPUyZXx9h+WKSutthipXgD4w6aRziYxjJKKOSz+E8Y8l3CnNrmS X-Gm-Gg: AZFdTZd0DONWeJ75gjlo/MxybIjx9Ava1rjyHORBNy4Vwl+ZETaaFTdWGQE/cUGWV5y tr8xyuV4GMJasso/4B+GvgRggNWRNzm2dRwl3o3uoRGjpcwDLFW5EsgxEJ1PAcVj7hVXsNP6YFK NFp95+8uEAMKIovlOvtzR/XKLSJ9STMx+8ta2eDcrdYTkha5C8arDy9pXpjIj4zgVB+taVcGwlV EgLzgVhdpFEKmOxyg8L+BCZPQeWm1fCYRu6MlQ1SxBxwE42h9X9iQn1XT0AD6ek3IVXugbzQQlD jwWKSDh9fwlKv873Nkck1Sfqph7QMrEXoRpKEZN+j/wzAsWjh3fDgpXDvy+IEzvdZY9Vy/SZGRm zMmjEGVZVplS/7r50WYahuOuHp0Rgx92cHtQPdC2zwB/zAjYwR4pvjV4vZf1Qfmp5dw7rHjLExp xVevVdjl53Pw/g8j5Jrw0hxWthvH+aUzosT8wEn0J1p8UL9JISAHMxNAGBIfOwOctS6eKRjx0zJ 6RwbH+JRpSqXRlliu2ccLt0R8vEiibL97CAS0u3PyxQcx4kJ1jWAvWI9UNxtXkMa94FLmaFtrMh /BRzPponYPH+vsd0FPF0GQdvbUDpuPO78899f6zg2j+g X-Received: by 2002:a17:90b:3806:b0:3ad:3e8:12 with SMTP id 98e67ed59e1d1-3b5e064ab74mr3887200a91.48.1796477133652; Sat, 05 Dec 2026 05:25:33 -0800 (PST) Received: from ustb520lab-MS-7E07.. ([123.124.147.27]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5ddb881bfsm8685034a91.1.2026.12.05.05.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 05:25:33 -0800 (PST) From: Jiaming Zhang To: Carlos Maiolino , linux-xfs@vger.kernel.org Cc: "Darrick J. Wong" , Dave Chinner , linux-kernel@vger.kernel.org, Jiaming Zhang , stable@vger.kernel.org Subject: [PATCH v1] xfs: stop buftarg shrinkers before draining buffers on teardown Date: Sat, 5 Dec 2026 21:25:24 +0800 Message-ID: <20261205132524.3510711-1-r772577952@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@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 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 --- 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