Re: [Linux Kernel Bug] INFO: task hung in btrfs_writepages

From: Qu Wenruo

Date: Sun Dec 06 2026 - 01:33:22 EST




在 2026/12/6 16:50, Jiaming Zhang 写道:
Hi Linux kernel developers and maintainers,

We are writing to report a hung-task issue in the Btrfs subsystem,
reproducible on the latest version of Linux (v7.4-rc5, commit
fe6ff8d5d675).

It is triggered by mounting a Btrfs image with zstd compression
(requiring CAP_SYS_ADMIN to mount) when memory pressure causes workspace
preallocation to fail,

Test again on the latest for-next.

There is a patch changed the zstd workspace behavior:

btrfs: zstd: use a dedicated hidden max level workspace

then writing and syncing a file while workspace
allocation fails again. Below is the kernel report:

INFO: task kworker/u10:0:28 blocked in I/O wait for more than 5 seconds.
Not tainted 7.4.0-rc5 #62
Workqueue: writeback wb_workfn (flush-btrfs-78)
Call Trace:
<TASK>
context_switch kernel/sched/core.c:5615 [inline]
__schedule+0x17c5/0x5b30 kernel/sched/core.c:7437
__schedule_loop kernel/sched/core.c:7514 [inline]
schedule+0x165/0x2b0 kernel/sched/core.c:7529
io_schedule+0x80/0xd0 kernel/sched/core.c:8201
folio_wait_bit_common+0x6cf/0xce0 mm/filemap.c:1327
folio_lock include/linux/pagemap.h:1216 [inline]
extent_write_cache_pages fs/btrfs/extent_io.c:2786 [inline]
btrfs_writepages+0x96f/0x2540 fs/btrfs/extent_io.c:2965
do_writepages+0x32e/0x550 mm/page-writeback.c:2562
__writeback_single_inode+0x130/0x1210 fs/fs-writeback.c:1809
writeback_sb_inodes+0x93f/0x1860 fs/fs-writeback.c:2120
__writeback_inodes_wb+0x111/0x240 fs/fs-writeback.c:2196
wb_writeback+0x43f/0xae0 fs/fs-writeback.c:2307
wb_check_start_all fs/fs-writeback.c:2433 [inline]
wb_do_writeback fs/fs-writeback.c:2480 [inline]
wb_workfn+0x983/0x1060 fs/fs-writeback.c:2518
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xca5/0x1900 kernel/workqueue.c:3580
worker_thread+0x8a3/0xda0 kernel/workqueue.c:3661
kthread+0x38c/0x480 kernel/kthread.c:436
ret_from_fork+0x509/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
</TASK>
INFO: task repro.out:9443 blocked for more than 5 seconds.
Not tainted 7.4.0-rc5 #62
Call Trace:
<TASK>
context_switch kernel/sched/core.c:5615 [inline]
__schedule+0x17c5/0x5b30 kernel/sched/core.c:7437
__schedule_loop kernel/sched/core.c:7514 [inline]
schedule+0x165/0x2b0 kernel/sched/core.c:7529
zstd_get_workspace+0x25a/0x3a0 fs/btrfs/zstd.c:317
get_workspace fs/btrfs/compression.c:876 [inline]
btrfs_compress_bio+0x2de/0x470 fs/btrfs/compression.c:1021
run_delalloc_inline fs/btrfs/inode.c:2323 [inline]
btrfs_run_delalloc_range+0x198f/0x2080 fs/btrfs/inode.c:2410
writepage_delalloc+0xeee/0x1990 fs/btrfs/extent_io.c:1685
extent_writepage fs/btrfs/extent_io.c:2081 [inline]
extent_write_cache_pages fs/btrfs/extent_io.c:2834 [inline]
btrfs_writepages+0x1363/0x2540 fs/btrfs/extent_io.c:2965
do_writepages+0x32e/0x550 mm/page-writeback.c:2562
filemap_writeback mm/filemap.c:386 [inline]
filemap_fdatawrite_range+0x1a5/0x250 mm/filemap.c:411
btrfs_fdatawrite_range fs/btrfs/file.c:3872 [inline]
start_ordered_ops fs/btrfs/file.c:1492 [inline]
btrfs_sync_file+0x377/0x1240 fs/btrfs/file.c:1574
vfs_fsync_range fs/sync.c:186 [inline]
vfs_fsync fs/sync.c:200 [inline]
do_fsync fs/sync.c:211 [inline]
__do_sys_fsync fs/sync.c:216 [inline]
__se_sys_fsync fs/sync.c:214 [inline]
__x64_sys_fsync+0x12a/0x180 fs/sync.c:214
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x17b/0x560 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
</TASK>
INFO: task repro.out:10074 blocked for more than 5 seconds.
Not tainted 7.4.0-rc5 #62
Call Trace:
<TASK>
context_switch kernel/sched/core.c:5615 [inline]
__schedule+0x17c5/0x5b30 kernel/sched/core.c:7437
__schedule_loop kernel/sched/core.c:7514 [inline]
schedule+0x165/0x2b0 kernel/sched/core.c:7529
wb_wait_for_completion+0x405/0x7b0 fs/fs-writeback.c:227
sync_inodes_sb+0x224/0xbc0 fs/fs-writeback.c:3072
__iterate_supers+0x24e/0x3d0 fs/super.c:953
ksys_sync fs/sync.c:102 [inline]
__ia32_sys_sync+0x94/0x100 fs/sync.c:111
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x17b/0x560 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
</TASK>

Showing all locks held in the system:
locks held by kworker/u10:0/28: 3, last CPU#1:
#0: ffff8880422a6148 ((wq_completion)writeback){+.+.}-{0:0}, at:
rcu_lock_acquire include/linux/rcupdate.h:309 [inline]
#0: ffff8880422a6148 ((wq_completion)writeback){+.+.}-{0:0}, at:
rcu_read_lock include/linux/rcupdate.h:849 [inline]
#0: ffff8880422a6148 ((wq_completion)writeback){+.+.}-{0:0}, at:
process_one_work kernel/workqueue.c:3462 [inline]
#0: ffff8880422a6148 ((wq_completion)writeback){+.+.}-{0:0}, at:
process_scheduled_works+0x95e/0x1900 kernel/workqueue.c:3580
#1: ffffc900004ffc40
((work_completion)(&(&wb->dwork)->work)){+.+.}-{0:0}, at:
rcu_lock_acquire include/linux/rcupdate.h:309 [inline]
#1: ffffc900004ffc40
((work_completion)(&(&wb->dwork)->work)){+.+.}-{0:0}, at:
rcu_read_lock include/linux/rcupdate.h:849 [inline]
#1: ffffc900004ffc40
((work_completion)(&(&wb->dwork)->work)){+.+.}-{0:0}, at:
process_one_work kernel/workqueue.c:3462 [inline]
#1: ffffc900004ffc40
((work_completion)(&(&wb->dwork)->work)){+.+.}-{0:0}, at:
process_scheduled_works+0x95e/0x1900 kernel/workqueue.c:3580
#2: ffff888020f60100 (&type->s_umount_key#56){++++}-{4:4}, at:
super_trylock_shared+0x23/0xf0 fs/super.c:642
locks held by repro.out/10074: 2, last CPU#1:
#0: ffff888020f60100 (&type->s_umount_key#56){++++}-{4:4}, at:
__super_lock fs/super.c:62 [inline]
#0: ffff888020f60100 (&type->s_umount_key#56){++++}-{4:4}, at:
super_lock+0x2a7/0x3a0 fs/super.c:124
#1: ffff888020f66870 (&bdi->wb_switch_rwsem){++++}-{4:4}, at:
bdi_down_write_wb_switch_rwsem fs/fs-writeback.c:397 [inline]
#1: ffff888020f66870 (&bdi->wb_switch_rwsem){++++}-{4:4}, at:
sync_inodes_sb+0x208/0xbc0 fs/fs-writeback.c:3070

Following is the root cause analysis for this issue. Note that the
analysis was performed with the assistance of an LLM, and we have tried
our best to ensure its accuracy.

The hung-task report triggers because a writeback worker in
btrfs_writepages() waits forever for a folio lock held by an fsync()
task. That fsync() task sleeps permanently in zstd_get_workspace()
waiting for a zstd compression workspace, while a concurrent sync() task
blocks waiting for writeback to complete.

In Btrfs, zstd compression uses scratch memory buffers called workspaces
managed per filesystem by zstd_workspace_manager. Higher compression
levels require larger buffers, so Btrfs protects a single maximum-level
workspace from reclaim as a fallback that any lower-level request can
reuse when allocating a new workspace fails.

Since commit 330f02b136a8 ("btrfs: add workspace manager initialization
for zstd"), zstd_alloc_workspace_manager() preallocates that fallback
workspace at mount time, but if allocation fails it only logs a warning
and still returns 0. The filesystem then mounts with no maximum-level
workspace in the pool.

Later, when zstd_get_workspace() cannot allocate a workspace during
compressed writeback, it puts the task to sleep in TASK_UNINTERRUPTIBLE
on zwsm->wait to wait for the fallback workspace. Because
zstd_put_workspace() only wakes zwsm->wait when a maximum-level
workspace is returned, and none exists, nothing ever wakes the task.

The fsync() task remains asleep forever while holding the folio lock,
even after memory pressure clears. This permanently blocks background
writeback in btrfs_writepages(), sync(), and unmount on that filesystem,
and triggers a hung-task report.

To fix this issue, we can fail zstd_alloc_workspace_manager() and return
the error when preallocating the maximum-level fallback workspace fails,
ensuring every mounted Btrfs filesystem has the fallback workspace that
zstd_get_workspace() relies on:

diff --git a/fs/btrfs/zstd.c b/fs/btrfs/zstd.c
index 588d7af2953f..fe0fe3d79ba4 100644
--- a/fs/btrfs/zstd.c
+++ b/fs/btrfs/zstd.c
@@ -196,15 +196,16 @@ int zstd_alloc_workspace_manager(struct
btrfs_fs_info *fs_info)
INIT_LIST_HEAD(&zwsm->lru_list);
for (int i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++)
INIT_LIST_HEAD(&zwsm->idle_ws[i]);
- fs_info->compr_wsm[BTRFS_COMPRESS_ZSTD] = zwsm;

ws = zstd_alloc_workspace(fs_info, ZSTD_BTRFS_MAX_LEVEL);
if (IS_ERR(ws)) {
- btrfs_warn(NULL, "cannot preallocate zstd compression workspace");
- } else {
- set_bit(ZSTD_BTRFS_MAX_LEVEL - 1, &zwsm->active_map);
- list_add(ws, &zwsm->idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1]);
+ btrfs_err(fs_info, "cannot preallocate zstd compression workspace");
+ kfree(zwsm);
+ return PTR_ERR(ws);
}
+ set_bit(ZSTD_BTRFS_MAX_LEVEL - 1, &zwsm->active_map);
+ list_add(ws, &zwsm->idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1]);
+ fs_info->compr_wsm[BTRFS_COMPRESS_ZSTD] = zwsm;
return 0;
}


The kernel console output, kernel config, and C reproducer are available
on Google Drive:
https://drive.google.com/drive/folders/1Rzi0mqFJKhABYjizpImIgFy182RRwk1T?usp=sharing

Best regards,
Jiaming Zhang