Skip to contentSkip to conversation

xfs: wait for buffer unpinning before shutdown completion

PATCH In review Andrea Parri 3 participants 4 messages Nov 26 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: wait for buffer unpinning before shutdown completion

Changing an XFS filesystem label while forcing log shutdown can trigger a use-after-free in the CIL push worker, as reported by syzbot:

    BUG: KASAN: slab-use-after-free in __list_splice include/linux/list.h:577 [inline]
    BUG: KASAN: slab-use-after-free in list_splice_init include/linux/list.h:619 [inline]
    BUG: KASAN: slab-use-after-free in xlog_cil_push_pcp_aggregate fs/xfs/xfs_log_cil.c:137 [inline]
    BUG: KASAN: slab-use-after-free in xlog_cil_push_work+0x986/0x2a90 fs/xfs/xfs_log_cil.c:1417
    Write of size 8 at addr ffff88803ac995b0 by task kworker/u8:8/1160

    Allocated by task 6137:
     xfs_buf_item_init+0x6a/0x6e0 fs/xfs/xfs_buf_item.c:872
     _xfs_trans_bjoin+0x46/0x110 fs/xfs/xfs_trans_buf.c:75
     __xfs_trans_getsb+0x65/0x4a0 fs/xfs/xfs_trans_buf.c:191
     xfs_log_sb+0x43/0x200 fs/xfs/libxfs/xfs_sb.c:1327
     xfs_sync_sb_buf+0xc2/0x290 fs/xfs/libxfs/xfs_sb.c:1479
     xfs_ioc_setlabel+0x1d7/0x340 fs/xfs/xfs_ioctl.c:1081

    Freed by task 6137:
     xfs_buf_ioend+0x235/0x790 fs/xfs/xfs_buf.c:1167
     xfs_buf_iowait+0x133/0x420 fs/xfs/xfs_buf.c:1336
     xfs_bwrite+0x5f/0xe0 fs/xfs/xfs_buf.c:1239
     xfs_sync_sb_buf+0x172/0x290 fs/xfs/libxfs/xfs_sb.c:1489
     xfs_ioc_setlabel+0x1d7/0x340 fs/xfs/xfs_ioctl.c:1081

xfs_buf_submit() completes a buffer immediately when the log is shut down. It bypasses xfs_buf_wait_unpin(), so xfs_buf_item_done() can free the buffer log item while the CIL still owns its pinning reference. A later CIL push or checkpoint abort then accesses the freed item.

Quotacheck hits the same problem at mount time:
xfs_buf_delwri_submit() passes pinned buffers to xfs_buf_submit() and relies on it to wait for them to be unpinned.

This became a use-after-free with commit d2fe5c4c8d25 ("xfs: rearrange code in xfs_buf_item.c"). Before that commit, xfs_buf_item_relse() returned early while bli_refcount was non-zero, leaving the buffer log item to the CIL abort. It now asserts that the count is zero and frees the item.

Wait for write buffers to become unpinned before checking log shutdown. xfs_buf_item_unpin() drops b_pin_count and wakes waiters before it takes the buffer lock to fail the buffer, so waiting with the buffer locked does not deadlock against the CIL abort.

On a shut down log the CIL is aborted, and the buffer unpinned, by the next log force; the periodic log worker issues one if nothing else does. Pinned buffers on a live log are already waited for in the same way.

syzbot's C reproducer ran under a KVM guest with KASAN, PREEMPT_RT, four CPUs and five workers. With this change it completed 191 seconds of active workload and 2,208 XFS mounts without a KASAN report. With the change reverted, KASAN reported the UAF after 74 seconds and 664 mounts.

OpenAI Codex analyzed the reports, implemented the suggested change, drafted this changelog, and ran the tests described above. Claude Code reviewed the change and revised this changelog.

---

Jeffin, Cc'ing you because this touches the BLI lifetime that your log item reference counting conversion reworks. This is the short-term xfs_buf_submit() fix Dave described in the thread linked above. It is meant to be small enough to backport and does not replace that work.

Processing of emailed #syz test commands is currently disabled (see
https://github.com/google/syzkaller/issues/8122), so the functional test results come only from local runs of syzbot's C reproducer. Three later 10-minute runs of the unpatched kernel did not reproduce the UAF, so the 191-second fixed-kernel run provides only weak evidence that the race is fixed.

 fs/xfs/xfs_buf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/xfs_buf.c +3 -3
index 6c93b4f5629c1..4acd0c3451da9 100644
@@ -1382,6 +1382,9 @@ xfs_buf_submit(
 
 	ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 
+	if (bp->b_flags & XBF_WRITE)
+		xfs_buf_wait_unpin(bp);
+
 	/*
 	 * On log shutdown we stale and complete the buffer immediately. We can
 	 * be called to read the superblock before the log has been set up, so
@@ -1402,9 +1405,6 @@ xfs_buf_submit(
 		goto ioerror;
 	}
 
-	if (bp->b_flags & XBF_WRITE)
-		xfs_buf_wait_unpin(bp);
-
 	/*
 	 * Make sure we capture only current IO errors rather than stale errors
 	 * left over from previous use of the buffer (e.g. failed readahead).
Signature · 1 line
2.53.0
#1 lore raw
Quoted 2 lines · Andrea Parri

Changing an XFS filesystem label while forcing log shutdown can trigger a use-after-free in the CIL push worker, as reported by syzbot:

I *think* Dave is already working on a proper fix for this on his series, so I'll wait for him to look into this as I didn't have time to go through his series yet.

Besides that...

Quoted 3 lines

OpenAI Codex analyzed the reports, implemented the suggested change, drafted this changelog, and ran the tests described above. Claude Code reviewed the change and revised this changelog.

LLMs references should stay away from commit messages. Doesn't matter what tool you used, the code is still your responsibility.

Quoted 56 lines

---

Jeffin, Cc'ing you because this touches the BLI lifetime that your log item reference counting conversion reworks. This is the short-term xfs_buf_submit() fix Dave described in the thread linked above. It is meant to be small enough to backport and does not replace that work.

Processing of emailed #syz test commands is currently disabled (see
https://github.com/google/syzkaller/issues/8122), so the functional test results come only from local runs of syzbot's C reproducer. Three later 10-minute runs of the unpatched kernel did not reproduce the UAF, so the 191-second fixed-kernel run provides only weak evidence that the race is fixed.

 fs/xfs/xfs_buf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/xfs_buf.c +3 -3
index 6c93b4f5629c1..4acd0c3451da9 100644
@@ -1382,6 +1382,9 @@ xfs_buf_submit(
 
 	ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 
+	if (bp->b_flags & XBF_WRITE)
+		xfs_buf_wait_unpin(bp);
+
 	/*
 	 * On log shutdown we stale and complete the buffer immediately. We can
 	 * be called to read the superblock before the log has been set up, so
@@ -1402,9 +1405,6 @@ xfs_buf_submit(
 		goto ioerror;
 	}
 
-	if (bp->b_flags & XBF_WRITE)
-		xfs_buf_wait_unpin(bp);
-
 	/*
 	 * Make sure we capture only current IO errors rather than stale errors
 	 * left over from previous use of the buffer (e.g. failed readahead).
Signature · 1 line
2.53.0
maintainer
#2 lore raw
Quoted 8 lines · On Thu, Nov 26, 2026 at 06:34:08PM +0100, [email protected] wrote:
Quoted 2 lines · Andrea Parri

Changing an XFS filesystem label while forcing log shutdown can trigger a use-after-free in the CIL push worker, as reported by syzbot:

I *think* Dave is already working on a proper fix for this on his series, so I'll wait for him to look into this as I didn't have time to go through his series yet.

Quoted 8 lines

Besides that...

Quoted 3 lines

OpenAI Codex analyzed the reports, implemented the suggested change, drafted this changelog, and ran the tests described above. Claude Code reviewed the change and revised this changelog.

LLMs references should stay away from commit messages. Doesn't matter what tool you used, the code is still your responsibility.

woops, sorry the bonker email headers.... Different machine, misconfigured MUA...

Quoted 57 lines
Quoted 57 lines

---

Jeffin, Cc'ing you because this touches the BLI lifetime that your log item reference counting conversion reworks. This is the short-term xfs_buf_submit() fix Dave described in the thread linked above. It is meant to be small enough to backport and does not replace that work.

Processing of emailed #syz test commands is currently disabled (see
https://github.com/google/syzkaller/issues/8122), so the functional test results come only from local runs of syzbot's C reproducer. Three later 10-minute runs of the unpatched kernel did not reproduce the UAF, so the 191-second fixed-kernel run provides only weak evidence that the race is fixed.

 fs/xfs/xfs_buf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/xfs_buf.c +3 -3
index 6c93b4f5629c1..4acd0c3451da9 100644
@@ -1382,6 +1382,9 @@ xfs_buf_submit(
 
 	ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
 
+	if (bp->b_flags & XBF_WRITE)
+		xfs_buf_wait_unpin(bp);
+
 	/*
 	 * On log shutdown we stale and complete the buffer immediately. We can
 	 * be called to read the superblock before the log has been set up, so
@@ -1402,9 +1405,6 @@ xfs_buf_submit(
 		goto ioerror;
 	}
 
-	if (bp->b_flags & XBF_WRITE)
-		xfs_buf_wait_unpin(bp);
-
 	/*
 	 * Make sure we capture only current IO errors rather than stale errors
 	 * left over from previous use of the buffer (e.g. failed readahead).
Signature · 1 line
2.53.0
#3 lore raw
In reply to [email protected]
Quoted 3 lines · Carlos Maiolino

I *think* Dave is already working on a proper fix for this on his series, so I'll wait for him to look into this as I didn't have time to go through his series yet.

Thanks. Please drop this patch in any case: Sashiko pointed out that it can deadlock in xfs_buf_delwri_submit(), which keeps every buffer it has submitted locked until all of them have been submitted:

xfs_buf_delwri_submit() CIL abort (xlog_cil_ail_insert)
lock bp1, xfs_buf_submit(bp1)
xfs_buf_wait_unpin(bp1) xfs_buf_item_unpin(bp1, remove)
drop pin, wake waiters
log is shut down, complete bp1 xfs_buf_lock(bp1) <- blocks
lock bp2, xfs_buf_submit(bp2)
xfs_buf_wait_unpin(bp2) <- blocks, bp2 would be unpinned
later in the same abort

This needs bp1 and bp2 to be pinned in the same aborted checkpoint, with the abort reaching bp1 first and bp1's unpin dropping the last BLI reference. That is the quotacheck case the patch claims to fix. Without the patch, xfs_buf_submit() does not wait once the log is shut down, so this is a regression.

I couldn't find Dave's series on lore. Could you point me to it? I'll check whether it covers these syzbot reports before sending anything else.

Quoted 2 lines

LLMs references should stay away from commit messages. Doesn't matter what tool you used, the code is still your responsibility.

Understood, I'll keep them out of the changelog.

Thanks,
Andrea