kernfs: do not hold of->mutex across sysfs ->store()
Subsystems (2)
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):&pipe->mutex --> &of->mutex --> &sbi->pipe_mutex
The cycle arises from two conflicting execution paths: splicing data from or to a pipe holds the pipe lock (&pipe->mutex) while invoking ->write_iter() or ->read_iter() on the kernfs file, acquiring of->mutex (&pipe->mutex -> &of->mutex). Conversely, a write to a sysfs file such as /sys/power/resume acquires of->mutex and calls resume_store(), which resolves paths via lookup_bdev() and kern_path(). Traversing an autofs mount during lookup invokes autofs_notify_daemon(), which acquires sbi->pipe_mutex and writes to the daemon pipe via __kernel_write(), acquiring pipe->mutex (&of->mutex -> &sbi->pipe_mutex -> &pipe->mutex).
To address this, do not hold of->mutex across sysfs ->store() callbacks by introducing a write_unlocked flag in struct kernfs_ops and caching it in struct kernfs_open_file. Running ->store() without of->mutex is safe because sysfs ->store() maintains no per-open-file state, and concurrent opens of the same file already execute ->store() concurrently without serialization. Other kernfs users retain per-open-file serialization under of->mutex, and preallocated sysfs attributes still serialize buffer access via of->prealloc_mutex.
Note that not every cycle is eliminated: a pipe spliced into such an attribute stays locked while ->store() resolves paths, and callbacks holding their own locks across lookup and ->show() retain dependencies.
To: "Danilo Krummrich" <[email protected]>
To: <[email protected]>
To: "Greg Kroah-Hartman" <[email protected]>
To: "Rafael J. Wysocki" <[email protected]>
To: "Tejun Heo" <[email protected]>
To: "Christoph Hellwig" <[email protected]>
Cc: <[email protected]>
---
v2:
- Avoid dropping splice_write support in kernfs_file_fops.
- Introduce write_unlocked flag in struct kernfs_ops and struct kernfs_open_file to run sysfs ->store() without of->mutex.
- Update commit subject and description to reflect the new locking approach.
v1:
https://lore.kernel.org/all/[email protected]/T/
---
fs/kernfs/file.c +12 -5
@@ -333,9 +333,11 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)* @of->mutex nests outside active ref and is used both to ensure that * the ops aren't called concurrently for the same open file. */ -mutex_lock (&of->mutex); +if (!of->write_unlocked) +mutex_lock (&of->mutex);if (!kernfs_get_active_of (of)) { -mutex_unlock (&of->mutex); +if (!of->write_unlocked) +mutex_unlock (&of->mutex); len = -ENODEV ; goto out_free; }
@@ -347,7 +349,8 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter) len = -EINVAL ;kernfs_put_active_of (of); -mutex_unlock (&of->mutex); +if (!of->write_unlocked) +mutex_unlock (&of->mutex);if (len >0 ) iocb->ki_pos += len;
@@ -651,6 +654,8 @@ static int kernfs_fop_open(struct inode *inode, struct file *file)* lockdep key, because the writable file /sys/power/resume may call vfs * lookup helpers for arbitrary paths and readonly files can be read by * overlayfs from vfs helpers when sysfs is a lower layer of overalyfs. +* (Note that regular sysfs attributes no longer run ->store() under +* @of->mutex, but other kernfs users still do.) * * All three cases look the same. They're supposed to * look that way and give @of->mutex different static lockdep keys.
@@ -666,10 +671,12 @@ static int kernfs_fop_open(struct inode *inode, struct file *file) of->file = file;/* -* Write path needs to atomic_write_len outside active reference. -* Cache it in open_file. See kernfs_fop_write_iter() for details. +* Write path needs atomic_write_len and write_unlocked outside +* active reference. Cache them in open_file. See +* kernfs_fop_write_iter() for details. */ of->atomic_write_len = ops->atomic_write_len; + of->write_unlocked = ops->write_unlocked; error = -EINVAL ;/*
fs/sysfs/file.c +15 -1
@@ -132,7 +132,17 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,return min_t (ssize_t, count, len); } -/* kernfs write callback for regular sysfs files */ +/* +* kernfs write callback for regular sysfs files. +* +* Regular sysfs files set .write_unlocked = true and are called without +* holding of->mutex. Sysfs attributes do not rely on per-open-file +* serialization for writes, and calling ->store() without of->mutex avoids +* circular locking dependencies (such as &pipe->mutex -> &of->mutex -> +* &sbi->pipe_mutex -> &pipe->mutex) when ->store() performs operations like +* path lookups or automounts while a generic VFS lock is held. Preallocated +* attributes are still serialized by of->prealloc_mutex. +*/ static ssize_t sysfs_kf_write (struct kernfs_open_file *of,char *buf,size_t count,loff_t pos) {
@@ -229,11 +239,13 @@ static const struct kernfs_ops sysfs_file_kfops_ro = {static const struct kernfs_ops sysfs_file_kfops_wo = { .write = sysfs_kf_write, + .write_unlocked = true, };static const struct kernfs_ops sysfs_file_kfops_rw = { .seq_show = sysfs_kf_seq_show, .write = sysfs_kf_write, + .write_unlocked = true, };static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
@@ -244,12 +256,14 @@ static const struct kernfs_ops sysfs_prealloc_kfops_ro = {static const struct kernfs_ops sysfs_prealloc_kfops_wo = { .write = sysfs_kf_write, .prealloc = true, + .write_unlocked = true, };static const struct kernfs_ops sysfs_prealloc_kfops_rw = { .read = sysfs_kf_read, .write = sysfs_kf_write, .prealloc = true, + .write_unlocked = true, };static const struct kernfs_ops sysfs_bin_kfops_ro = {
include/linux/kernfs.h +6 -0
@@ -274,6 +274,7 @@ struct kernfs_open_file {size_t atomic_write_len; bool mmapped:1; bool released:1; + bool write_unlocked:1;const struct vm_operations_struct *vm_ops; };
@@ -320,6 +321,11 @@ struct kernfs_ops {* ->prealloc. Provide ->read and ->write with ->prealloc. */ bool prealloc; +/* +* "write_unlocked" indicates that ->write() does not require +* per-open-file serialization with of->mutex. +*/ +bool write_unlocked;ssize_t (*write)(struct kernfs_open_file *of,char *buf,size_t bytes,loff_t off);
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 182 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):&pipe->mutex-->&of->mutex-->&sbi->pipe_mutexThe cycle arises from two conflicting execution paths: splicing data from or to a pipe holds the pipe lock (
&pipe->mutex) while invoking ->write_iter() or ->read_iter() on the kernfs file, acquiringof->mutex(&pipe->mutex->&of->mutex). Conversely, a write to a sysfs file such as/sys/power/resumeacquiresof->mutexand callsresume_store(), which resolves paths vialookup_bdev()andkern_path(). Traversing an autofs mount during lookup invokesautofs_notify_daemon(), which acquiressbi->pipe_mutexand writes to the daemon pipe via__kernel_write(), acquiringpipe->mutex(&of->mutex->&sbi->pipe_mutex->&pipe->mutex).To address this, do not hold
of->mutexacross sysfs ->store() callbacks by introducing awrite_unlockedflag in structkernfs_opsand caching it in structkernfs_open_file. Running ->store() withoutof->mutexis safe because sysfs ->store() maintains no per-open-file state, and concurrent opens of the same file already execute ->store() concurrently without serialization. Other kernfs users retain per-open-file serialization underof->mutex, and preallocated sysfs attributes still serialize buffer access viaof->prealloc_mutex.Note that not every cycle is eliminated: a pipe spliced into such an attribute stays locked while ->store() resolves paths, and callbacks holding their own locks across lookup and ->show() retain dependencies.
Fixes f2d6c2708bd8 ("kernfs: wire up ->splice_read and ->splice_write")Fixes 1e8c813b083c ("PM: hibernate: don't use early_lookup_bdev in resume_store")Assisted-by Gemini:gemini-3.8-flash syzbotReported-by [email protected]To: "Danilo Krummrich" <[email protected]>
To: <[email protected]>
To: "Greg Kroah-Hartman" <[email protected]>
To: "Rafael J. Wysocki" <[email protected]>
To: "Tejun Heo" <[email protected]>
To: "Christoph Hellwig" <[email protected]>
Cc: <[email protected]>---
v2:
- Avoid droppingsplice_writesupport inkernfs_file_fops.
- Introducewrite_unlockedflag in structkernfs_opsand structkernfs_open_fileto run sysfs ->store() withoutof->mutex.
- Update commit subject and description to reflect the new locking approach.v1:
https://lore.kernel.org/all/[email protected]/T/
---fs/kernfs/file.c +12
-5@@ -333,9 +333,11 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)* @of->mutex nests outside active ref and is used both to ensure that * the ops aren't called concurrently for the same open file. */ -mutex_lock (&of->mutex); +if (!of->write_unlocked) +mutex_lock (&of->mutex);if (!kernfs_get_active_of (of)) { -mutex_unlock (&of->mutex); +if (!of->write_unlocked) +mutex_unlock (&of->mutex); len = -ENODEV ; goto out_free; }@@ -347,7 +349,8 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter) len = -EINVAL ;kernfs_put_active_of (of); -mutex_unlock (&of->mutex); +if (!of->write_unlocked) +mutex_unlock (&of->mutex);if (len >0 ) iocb->ki_pos += len;@@ -651,6 +654,8 @@ static int kernfs_fop_open(struct inode *inode, struct file *file)* lockdep key, because the writable file /sys/power/resume may call vfs * lookup helpers for arbitrary paths and readonly files can be read by * overlayfs from vfs helpers when sysfs is a lower layer of overalyfs. +* (Note that regular sysfs attributes no longer run ->store() under +* @of->mutex, but other kernfs users still do.) * * All three cases look the same. They're supposed to * look that way and give @of->mutex different static lockdep keys. @@ -666,10 +671,12 @@ static int kernfs_fop_open(struct inode *inode, struct file *file) of->file = file;/* -* Write path needs to atomic_write_len outside active reference. -* Cache it in open_file. See kernfs_fop_write_iter() for details. +* Write path needs atomic_write_len and write_unlocked outside +* active reference. Cache them in open_file. See +* kernfs_fop_write_iter() for details. */ of->atomic_write_len = ops->atomic_write_len; + of->write_unlocked = ops->write_unlocked; error = -EINVAL ;/* fs/sysfs/file.c +15
-1@@ -132,7 +132,17 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,return min_t (ssize_t, count, len); } -/* kernfs write callback for regular sysfs files */ +/* +* kernfs write callback for regular sysfs files. +* +* Regular sysfs files set .write_unlocked = true and are called without +* holding of->mutex. Sysfs attributes do not rely on per-open-file +* serialization for writes, and calling ->store() without of->mutex avoids +* circular locking dependencies (such as &pipe->mutex -> &of->mutex -> +* &sbi->pipe_mutex -> &pipe->mutex) when ->store() performs operations like +* path lookups or automounts while a generic VFS lock is held. Preallocated +* attributes are still serialized by of->prealloc_mutex. +*/ static ssize_t sysfs_kf_write (struct kernfs_open_file *of,char *buf,size_t count,loff_t pos) {@@ -229,11 +239,13 @@ static const struct kernfs_ops sysfs_file_kfops_ro = {static const struct kernfs_ops sysfs_file_kfops_wo = { .write = sysfs_kf_write, + .write_unlocked = true, };static const struct kernfs_ops sysfs_file_kfops_rw = { .seq_show = sysfs_kf_seq_show, .write = sysfs_kf_write, + .write_unlocked = true, };static const struct kernfs_ops sysfs_prealloc_kfops_ro = {@@ -244,12 +256,14 @@ static const struct kernfs_ops sysfs_prealloc_kfops_ro = {static const struct kernfs_ops sysfs_prealloc_kfops_wo = { .write = sysfs_kf_write, .prealloc = true, + .write_unlocked = true, };static const struct kernfs_ops sysfs_prealloc_kfops_rw = { .read = sysfs_kf_read, .write = sysfs_kf_write, .prealloc = true, + .write_unlocked = true, };static const struct kernfs_ops sysfs_bin_kfops_ro = {include/linux/kernfs.h +6
-0@@ -274,6 +274,7 @@ struct kernfs_open_file {size_t atomic_write_len; bool mmapped:1; bool released:1; + bool write_unlocked:1;const struct vm_operations_struct *vm_ops; };@@ -320,6 +321,11 @@ struct kernfs_ops {* ->prealloc. Provide ->read and ->write with ->prealloc. */ bool prealloc; +/* +* "write_unlocked" indicates that ->write() does not require +* per-open-file serialization with of->mutex. +*/ +bool write_unlocked;ssize_t (*write)(struct kernfs_open_file *of,char *buf,size_t bytes,loff_t off);
Quoted 1 line
base-commit:
93f51579e7df248780214094418f205253383cc5
Change the cached write_unlocked field in struct kernfs_open_file to a standalone bool, placed after atomic_write_len and before mmapped and released. The write path reads it before taking of->mutex or an active reference, including for files where the flag is false. The neighboring bitfields are updated at run time under of->mutex or the node mutex and share one C memory location with the added bitfield. A standalone bool keeps the cached flag separate from those mutable fields.
Keep the current write_unlocked approach and the same four regular sysfs write tables that opt out of of->mutex. Preserve both splice methods, active-reference handling, prealloc_mutex and the existing serialization for reads, binary sysfs attributes, cgroup and resctrl. Keep the explicit statement that existing autofs and callback-owned lock dependencies remain outside this narrow change. Preserve the Reported-by, Closes, Link and Assisted-by tags.
Also use only Fixes: 1e8c813b083c ("PM: hibernate: don't use early_lookup_bdev in resume_store"), removing the additional f2d6c2708bd8 tag. Update the write-path comment and the flag documentation to describe the concurrent-callback contract. Shorten the repeated lock-chain explanation while retaining the buffer protection and known limits. Keep the current balanced error-path unlock.
Signature · 1 line
Krystian Kaniewski