kernfs: drop splice_write support
syzbot
syzbotA circular locking dependency was detected between &pipe->mutex (in struct pipe_inode_info), &of->mutex (in struct kernfs_open_file), and &sbi->pipe_mutex (in struct autofs_sb_info).
The cycle arises from two conflicting execution paths:
First, splicing data from a pipe into a kernfs file (such as a sysfs attribute) invokes iter_file_splice_write(), which locks the pipe via pipe_lock(pipe) (&pipe->mutex) and then calls kernfs_fop_write_iter(), acquiring mutex_lock(&of->mutex). This establishes the dependency &pipe->mutex -> &of->mutex.
Second, a regular write to a kernfs attribute (such as /sys/power/resume) acquires mutex_lock(&of->mutex) in kernfs_fop_write_iter() and invokes the attribute's store handler (resume_store()). In resume_store(), path resolution via lookup_bdev() and kern_path() can traverse an automount point managed by autofs. This triggers autofs_d_automount() -> autofs_wait() -> autofs_notify_daemon(), which acquires mutex_lock(&sbi->pipe_mutex) and writes a notification packet to the automount daemon pipe via __kernel_write(). That write enters anon_pipe_write() -> anon_pipe_prefill_and_lock() and acquires mutex_lock(&pipe->mutex). This establishes the dependency &of->mutex -> &sbi->pipe_mutex -> &pipe->mutex.
Because all anonymous pipes share the same lockdep lock class (&pipe->mutex), this creates an ABBA circular locking dependency:
======================================================
WARNING: possible circular locking dependency detected
======================================================
task is trying to acquire lock:ffff888113c20c60 (&pipe->mutex){+.+.}-{4:4}, at:anon_pipe_write+0x28f/0x1a30 fs/pipe.c:534
but task is already holding lock:ffff888115813938 (&sbi->pipe_mutex){+.+.}-{4:4}, at:autofs_notify_daemon+0x72a/0xe80 fs/autofs/waitq.c:164
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #2 (&sbi->pipe_mutex){+.+.}-{4:4}:
__mutex_lock+0x19d/0x1550 kernel/locking/mutex.c:821
autofs_notify_daemon+0x72a/0xe80 fs/autofs/waitq.c:164
autofs_wait+0x11bd/0x1880 fs/autofs/waitq.c:426
autofs_d_automount+0x64e/0x810 fs/autofs/root.c:410
kern_path+0x3d/0x150 fs/namei.c:3036
lookup_bdev+0xc8/0x2b0 block/bdev.c:1268
resume_store+0x16f/0x4f0 kernel/power/hibernate.c:1278
kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
vfs_write+0x612/0xba0 fs/read_write.c:687
ksys_write+0x150/0x270 fs/read_write.c:739-> #1 (&of->mutex){+.+.}-{4:4}:
__mutex_lock+0x19d/0x1550 kernel/locking/mutex.c:821
kernfs_fop_write_iter+0x1d8/0x540 fs/kernfs/file.c:336
iter_file_splice_write+0xa31/0x1240 fs/splice.c:736
do_splice+0xc3e/0x16a0 fs/splice.c:1349
__do_sys_splice fs/splice.c:1634 [inline]-> #0 (&pipe->mutex){+.+.}-{4:4}:
__mutex_lock+0x19d/0x1550 kernel/locking/mutex.c:821
anon_pipe_write+0x28f/0x1a30 fs/pipe.c:534
__kernel_write+0x106/0x170 fs/read_write.c:641
autofs_notify_daemon+0x73d/0xe80 fs/autofs/waitq.c:164
autofs_wait+0x11bd/0x1880 fs/autofs/waitq.c:426
autofs_d_automount+0x64e/0x810 fs/autofs/root.c:410
kern_path+0x3d/0x150 fs/namei.c:3036
lookup_bdev+0xc8/0x2b0 block/bdev.c:1268
resume_store+0x16f/0x4f0 kernel/power/hibernate.c:1278
kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
vfs_write+0x612/0xba0 fs/read_write.c:687
ksys_write+0x150/0x270 fs/read_write.c:739Fix this by removing .splice_write = iter_file_splice_write from kernfs_file_fops. Kernfs attributes (sysfs, cgroupfs) are control interfaces designed for small configuration strings and numbers, rather than high-throughput data streams. Kernfs does not do page-cache zero-copy writes; kernfs_fop_write_iter() allocates a kernel buffer and copies data from the iterator via copy_from_iter(), so splice_write provides no benefit over write(). In addition, kernfs write callbacks invoke arbitrary subsystem callbacks across the kernel, and holding a generic VFS lock like pipe->mutex across them breaks subsystem layering and creates lock inversion hazards whenever an attribute callback interacts with pipes, usermode helpers, or automounts. Dropping .splice_write aligns kernfs with other pseudo-filesystems such as procfs, debugfs, tracefs, and configfs, and causes splice() into a kernfs file to return -EINVAL.
To: <[email protected]>
To: "Greg Kroah-Hartman" <[email protected]>
To: "Tejun Heo" <[email protected]>
To: "Christoph Hellwig" <[email protected]>
Cc: <[email protected]>
---
fs/kernfs/file.c +0 -1
@@ -1015,7 +1015,6 @@ const struct file_operations kernfs_file_fops = { .poll = kernfs_fop_poll, .fsync = noop_fsync, .splice_read = copy_splice_read, - .splice_write = iter_file_splice_write, };/**
base-commit: 93f51579e7df248780214094418f205253383cc5
Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].Quoted 117 lines · syzbot
A circular locking dependency was detected between
&pipe->mutex(in structpipe_inode_info),&of->mutex(in structkernfs_open_file), and&sbi->pipe_mutex(in structautofs_sb_info).The cycle arises from two conflicting execution paths:
First, splicing data from a pipe into a kernfs file (such as a sysfs attribute) invokes
iter_file_splice_write(), which locks the pipe viapipe_lock(pipe)(&pipe->mutex) and then callskernfs_fop_write_iter(), acquiringmutex_lock(&of->mutex). This establishes the dependency&pipe->mutex->&of->mutex.Second, a regular write to a kernfs attribute (such as
/sys/power/resume) acquiresmutex_lock(&of->mutex)inkernfs_fop_write_iter()and invokes the attribute's store handler (resume_store()). Inresume_store(), path resolution vialookup_bdev()andkern_path()can traverse an automount point managed by autofs. This triggersautofs_d_automount()->autofs_wait()->autofs_notify_daemon(), which acquiresmutex_lock(&sbi->pipe_mutex)and writes a notification packet to the automount daemon pipe via__kernel_write(). That write entersanon_pipe_write()->anon_pipe_prefill_and_lock()and acquiresmutex_lock(&pipe->mutex). This establishes the dependency&of->mutex->&sbi->pipe_mutex->&pipe->mutex.Because all anonymous pipes share the same lockdep lock class (
&pipe->mutex), this creates an ABBA circular locking dependency:======================================================
WARNING: possible circular locking dependency detected
======================================================
task is trying to acquire lock:ffff888113c20c60(&pipe->mutex){+.+.}-{4:4}, at:anon_pipe_write+0x28f/0x1a30fs/pipe.c:534but task is already holding lock:
ffff888115813938(&sbi->pipe_mutex){+.+.}-{4:4}, at:autofs_notify_daemon+0x72a/0xe80fs/autofs/waitq.c:164which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #2 (&sbi->pipe_mutex){+.+.}-{4:4}:
__mutex_lock+0x19d/0x1550 kernel/locking/mutex.c:821 autofs_notify_daemon+0x72a/0xe80 fs/autofs/waitq.c:164 autofs_wait+0x11bd/0x1880 fs/autofs/waitq.c:426 autofs_d_automount+0x64e/0x810 fs/autofs/root.c:410 kern_path+0x3d/0x150 fs/namei.c:3036 lookup_bdev+0xc8/0x2b0 block/bdev.c:1268 resume_store+0x16f/0x4f0 kernel/power/hibernate.c:1278 kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345 vfs_write+0x612/0xba0 fs/read_write.c:687 ksys_write+0x150/0x270 fs/read_write.c:739-> #1 (&of->mutex){+.+.}-{4:4}:
__mutex_lock+0x19d/0x1550 kernel/locking/mutex.c:821 kernfs_fop_write_iter+0x1d8/0x540 fs/kernfs/file.c:336 iter_file_splice_write+0xa31/0x1240 fs/splice.c:736 do_splice+0xc3e/0x16a0 fs/splice.c:1349 __do_sys_splice fs/splice.c:1634 [inline]-> #0 (&pipe->mutex){+.+.}-{4:4}:
__mutex_lock+0x19d/0x1550 kernel/locking/mutex.c:821 anon_pipe_write+0x28f/0x1a30 fs/pipe.c:534 __kernel_write+0x106/0x170 fs/read_write.c:641 autofs_notify_daemon+0x73d/0xe80 fs/autofs/waitq.c:164 autofs_wait+0x11bd/0x1880 fs/autofs/waitq.c:426 autofs_d_automount+0x64e/0x810 fs/autofs/root.c:410 kern_path+0x3d/0x150 fs/namei.c:3036 lookup_bdev+0xc8/0x2b0 block/bdev.c:1268 resume_store+0x16f/0x4f0 kernel/power/hibernate.c:1278 kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345 vfs_write+0x612/0xba0 fs/read_write.c:687 ksys_write+0x150/0x270 fs/read_write.c:739Fix this by removing .splice_write =
iter_file_splice_writefromkernfs_file_fops. Kernfs attributes (sysfs, cgroupfs) are control interfaces designed for small configuration strings and numbers, rather than high-throughput data streams. Kernfs does not do page-cache zero-copy writes;kernfs_fop_write_iter()allocates a kernel buffer and copies data from the iterator viacopy_from_iter(), sosplice_writeprovides no benefit overwrite(). In addition, kernfs write callbacks invoke arbitrary subsystem callbacks across the kernel, and holding a generic VFS lock likepipe->mutexacross them breaks subsystem layering and creates lock inversion hazards whenever an attribute callback interacts with pipes, usermode helpers, or automounts. Dropping .splice_write aligns kernfs with other pseudo-filesystems such as procfs, debugfs, tracefs, and configfs, and causessplice()into a kernfs file to return-EINVAL.Fixes f2d6c2708bd8 ("kernfs: wire up ->splice_read and ->splice_write")Fixes f2d6c2708bd8 ("kernfs: wire up ->splice_read and ->splice_write")Assisted-by Gemini:gemini-3.8-flash Gemini:gemini-3.1-pro-preview syzbotReported-by [email protected]To: <[email protected]>
To: "Greg Kroah-Hartman" <[email protected]>
To: "Tejun Heo" <[email protected]>
To: "Christoph Hellwig" <[email protected]>
Cc: <[email protected]>---
fs/kernfs/file.c +0
-1@@ -1015,7 +1015,6 @@ const struct file_operations kernfs_file_fops = { .poll = kernfs_fop_poll, .fsync = noop_fsync, .splice_read = copy_splice_read, - .splice_write = iter_file_splice_write, };/**
Quoted 1 line
base-commit:
93f51579e7df248780214094418f205253383cc5
Please keep .splice_write in kernfs_file_fops and fix the lock dependency inside kernfs instead, by not holding of->mutex while the ->store() callback of a regular sysfs attribute runs.
Removing .splice_write does not remove the dependency that lockdep reported. splice_file_to_pipe() also holds the pipe locked while copy_splice_read() calls ->read_iter(), and kernfs_seq_start() or kernfs_file_read_iter() then takes of->mutex. For a file opened with O_RDWR this is the same lockdep class that /sys/power/resume uses when it is written. A splice from such a file into a pipe, followed by the write to /sys/power/resume, records the same cycle through &of->mutex and &sbi->pipe_mutex, and with one shared open file of /sys/power/resume and the autofs daemon pipe two tasks can still deadlock. The change also makes splice() and sendfile() into every kernfs file fail with -EINVAL, for sysfs text and binary attributes and for cgroup files. Commit f2d6c2708bd8 restored exactly this support after users reported that sendfile() and splice() into sysfs files had stopped working, so it has to stay.
The dependency that has to go is the one from of->mutex to whatever ->store() takes or waits for. sysfs_kf_write() calls ops->store(kobj, attr, buf, count) and does not pass the open file, so a ->store() callback keeps no state per open file, and two opens of the same attribute already run it concurrently. Add a bool to struct kernfs_ops, for example write_unlocked, for ->write() callbacks that do not need to be serialized per open file. Copy it into struct kernfs_open_file in kernfs_fop_open() next to atomic_write_len, because the ops table may only be read with an active reference and of->mutex nests outside it. In kernfs_fop_write_iter() take of->mutex only if the cached flag is not set, release it under the same condition, and make sure that the path on which kernfs_get_active_of() fails does not unlock a mutex that was never taken. Set the flag in the four sysfs kernfs_ops tables that use sysfs_kf_write(): sysfs_file_kfops_wo, sysfs_file_kfops_rw, sysfs_prealloc_kfops_wo and sysfs_prealloc_kfops_rw. Leave of->prealloc_mutex as it is, it protects the preallocated buffer. Keep of->mutex for the read side and for cgroup, resctrl and binary sysfs attributes, whose callbacks receive the open file, and keep both splice methods. Do not change autofs, splice or the pipe code. Document the new flag at its declaration, say at sysfs_kf_write() why it is called without of->mutex, and add to the comment about the lockdep keys in kernfs_fop_open() that regular sysfs attributes no longer run ->store() under of->mutex.
The subject and the commit message have to follow the new change, for example "kernfs: do not hold of->mutex across sysfs ->store()". Quote the chain of the report in one line (&pipe->mutex --> &of->mutex --> &sbi->pipe_mutex) and explain its two halves: splice calls ->write_iter() or ->read_iter() of the kernfs file with the pipe locked, and resume_store() resolves the written path under of->mutex, where an autofs mount writes its request to the daemon pipe. Say that ->store() no longer runs under of->mutex and why that is safe, that other kernfs users keep the serialization per open file, and that attributes with a preallocated buffer still run ->store() under of->prealloc_mutex. Use a single Fixes: 1e8c813b083c ("PM: hibernate: don't use early_lookup_bdev in resume_store"), the commit that made resume_store() resolve a path, and remove both Fixes: lines for f2d6c2708bd8. Do not claim that every cycle is gone. A pipe that is spliced into such an attribute stays locked while ->store() resolves the path, so lockdep can still report that pipe mutex against the autofs locks without of->mutex. A callback that holds a lock of its own across the lookup and takes it in ->show() as well, such as a module parameter whose setter opens a path, keeps a dependency through that lock. Keep the Reported-by, Closes, Link and Assisted-by tags.
While rewriting the message, please also keep it short. The lockdep trace of more than 40 lines is not needed, the one line with the chain is enough. Leave out the comparison with procfs, because /proc/sys files do provide .splice_write.
Signature · 1 line
Krystian Kaniewski