[Linux Kernel Bug] BUG: spinlock bad magic in iomap_clear_range_dirty

From: Jiaming Zhang

Date: Sun Dec 06 2026 - 07:09:21 EST


Hi Linux kernel developers and maintainers,

We are writing to report a spinlock bad magic bug in the GFS2
subsystem, reproducible on the latest version of Linux (v7.4-rc5,
commit fe6ff8d5d675) and linux-gfs2 for-next branch (commit
02b63d3f6c2d).

It is triggered by mounting a crafted GFS2 image whose hidden quota
system inode lacks the journaled-data flag with quotas enabled
(requiring CAP_SYS_ADMIN to mount) and unmounting while syncing a
pending quota change. Below is the kernel report:

BUG: spinlock bad magic on CPU#0, repro.out/9450
lock: 0xffff888012e2ac40, .magic: 00000000, .owner: /0, .owner_cpu: 316845120
CPU: 0 UID: 0 PID: 9450 Comm: repro.out Not tainted 7.4.0-rc5 #62 PREEMPT(full)
Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix,
1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x10e/0x190 lib/dump_stack.c:120
debug_spin_lock_before kernel/locking/spinlock_debug.c:86 [inline]
do_raw_spin_lock+0x1bd/0x260 kernel/locking/spinlock_debug.c:115
__raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:156 [inline]
_raw_spin_lock_irqsave+0x4c/0x60 kernel/locking/spinlock.c:181
ifs_clear_range_dirty fs/iomap/buffered-io.c:203 [inline]
iomap_clear_range_dirty+0x260/0x410 fs/iomap/buffered-io.c:214
iomap_writeback_folio+0x1a3e/0x2210 fs/iomap/buffered-io.c:2071
iomap_writepages+0x15c/0x2d0 fs/iomap/buffered-io.c:2115
gfs2_writepages+0x1ba/0x290 fs/gfs2/aops.c:175
do_writepages+0x32e/0x550 mm/page-writeback.c:2562
filemap_writeback mm/filemap.c:386 [inline]
filemap_fdatawrite_range mm/filemap.c:411 [inline]
filemap_fdatawrite+0x199/0x240 mm/filemap.c:417
gfs2_ordered_write fs/gfs2/log.c:730 [inline]
__gfs2_log_flush+0xc32/0x1d70 fs/gfs2/log.c:1135
gfs2_log_flush+0x34/0x50 fs/gfs2/log.c:1217
do_sync+0x9cc/0xc10 fs/gfs2/quota.c:1000
gfs2_quota_sync+0x359/0x460 fs/gfs2/quota.c:1378
gfs2_sync_fs+0x4c/0xb0 fs/gfs2/super.c:664
sync_filesystem+0xf1/0x240 fs/sync.c:56
generic_shutdown_super+0x7a/0x2d0 fs/super.c:702
kill_block_super+0x44/0xa0 fs/super.c:1852
deactivate_locked_super+0xbe/0x110 fs/super.c:558
cleanup_mnt+0x1ff/0x290 fs/namespace.c:1396
task_work_run+0x1d4/0x260 kernel/task_work.c:233
exit_task_work include/linux/task_work.h:40 [inline]
do_exit+0x6d3/0x2260 kernel/exit.c:980
do_group_exit+0x21c/0x2d0 kernel/exit.c:1123
__do_sys_exit_group kernel/exit.c:1134 [inline]
__se_sys_exit_group kernel/exit.c:1132 [inline]
__x64_sys_exit_group+0x3f/0x40 kernel/exit.c:1132
x64_sys_call+0x18c6/0x18d0 arch/x86/include/generated/asm/syscalls_64.h:232
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>

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 spinlock bad magic bug triggers in iomap_clear_range_dirty()
because a folio of GFS2's hidden quota file holds a struct buffer_head
in folio->private where the iomap writeback path expects a struct
iomap_folio_state and tries to acquire its embedded spinlock.

Since commit 2164f9b91869 ("gfs2: use iomap for buffered I/O in ordered
and writeback mode"), GFS2 selects address-space operations in
gfs2_set_aops() solely from the on-disk GFS2_DIF_JDATA flag: journaled
files use buffer-head helpers, whereas non-journaled files use iomap
helpers that store iomap_folio_state in folio->private.

At mount time, init_inodes() looks up the hidden quota inode without
instantiating it or verifying that it is a regular journaled-data file.
Meanwhile, gfs2_write_buf_to_page() unconditionally treats the quota
inode as buffer-head-backed and attaches buffer heads to folio->private
via create_empty_buffers().

When a crafted image clears GFS2_DIF_JDATA on the quota inode, GFS2
assigns iomap address-space operations to it. During quota sync,
unstuffing queues the inode for ordered writeback while quota writes
attach a buffer_head to folio->private, so the subsequent log flush
runs iomap_writepages() and iomap_clear_range_dirty() on that folio.

Reinterpreting the buffer_head as an iomap_folio_state spinlock and
dirty bitmap corrupts buffer_head fields, triggers a spinlock bad magic
report, and can leave the CPU spinning on the invalid lock state.

To fix this issue, we can instantiate the hidden quota inode under a
shared glock in init_inodes(), verify that it is a regular file with
GFS2_DIF_JDATA set, and reject corrupt images at mount time with
-EINVAL:

diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 6ec47376aecf..62375d367cce 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -837,6 +837,7 @@ static int init_inodes(struct gfs2_sbd *sdp, int undo)
{
int error = 0;
struct inode *master = d_inode(sdp->sd_master_dir);
+ struct gfs2_holder q_gh;

if (undo)
goto fail_qinode;
@@ -862,6 +863,20 @@ static int init_inodes(struct gfs2_sbd *sdp, int undo)
fs_err(sdp, "can't get quota file inode: %d\n", error);
goto fail_rindex;
}
+ error = gfs2_glock_nq_init(gfs2_inode_glock(sdp->sd_quota_inode),
+ LM_ST_SHARED, 0, &q_gh);
+ if (error) {
+ fs_err(sdp, "can't lock quota file inode: %d\n", error);
+ goto fail_qinode;
+ }
+ if (!S_ISREG(sdp->sd_quota_inode->i_mode) ||
+ !gfs2_is_jdata(GFS2_I(sdp->sd_quota_inode))) {
+ fs_err(sdp, "corrupt quota file inode\n");
+ error = -EINVAL;
+ }
+ gfs2_glock_dq_uninit(&q_gh);
+ if (error)
+ goto fail_qinode;
/*
* i_rwsem on quota files is special. Since this inode is hidden system
* file, we are safe to define locking ourselves.

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

Best regards,
Jiaming Zhang