* [PATCH RFC v2] configfs: don't lock the root inode in configfs_depend_item_unlocked()
@ 2026-11-29 12:38 Haruka Amakawa via B4 Relay
2026-11-29 12:49 ` Greg Kroah-Hartman
0 siblings, 1 reply; 2+ messages in thread
From: Haruka Amakawa via B4 Relay @ 2026-11-29 12:38 UTC (permalink / raw)
To: Breno Leitao
Cc: Greg Kroah-Hartman, Andreas Hindborg, Slawomir Stepien,
William Chuang, Roy, linux-fsdevel, linux-usb, target-devel,
linux-kernel, syzbot+8c0a48f6c8357c9da819, Haruka Amakawa
From: Haruka Amakawa <macchacat@pm.me>
configfs_depend_item_unlocked() is meant to be called from configfs
callbacks. Directory operation callbacks such as ->make_group() run
with a directory inode locked by the VFS. When the target item lives in
another subsystem, configfs_depend_item_unlocked() additionally locks
the configfs root inode, so that the target subsystem cannot be
unregistered while it is looked up and the dependency is taken.
From such a callback, that nests the root inode lock inside a directory
inode lock. A rename() whose two parent directories are the configfs
root and a directory below it nests them the other way round:
lock_rename() locks both parents before the last components are looked
up and before vfs_rename() gets to fail, and lock_two_directories()
locks the ancestor first. f_tcm runs into this on mkdir of
target/usb_gadget/<wwn>/tpgt_N: configfs_mkdir() calls the
->make_group() callback, target_fabric_make_tpg(), with the VFS still
holding the <wwn> directory lock, and its usbg_make_tpg() depends on the
tcm function instance under usb_gadget/:
mkdir(.../naa.X/tpgt_1) rename(".../config/a",
".../naa.X/a")
inode_lock(naa.X) [VFS]
lock_two_directories()
inode_lock(root)
inode_lock(naa.X)
usbg_make_tpg()
configfs_depend_item_unlocked()
inode_lock(root)
Sashiko pointed this inversion out in review, and William Chuang
reproduced the hang: one process looping over mkdir/rmdir of tpgt_1 and
another looping over rename() attempts like the one above (the source
does not even exist) both hang within about a second. Giving the root
inode its own lockdep class makes lockdep stop reporting the cycle, but
the deadlock is still there. With that class split applied, the same
two loops stop after the first mkdir of tpgt_1, lockdep reports
nothing, and only the hung task detector notices:
INFO: task rename_vs_mkdir:92 blocked for more than 30 seconds.
down_write+0x1ae/0x1e0
configfs_depend_item_unlocked+0xd8/0x5c0
usbg_make_tpg+0x1f5/0x650
target_fabric_make_tpg+0xa6/0x7f0
configfs_mkdir+0x4ea/0xfe0
vfs_mkdir+0x2b7/0x920
INFO: task rename_vs_mkdir:93 blocked for more than 30 seconds.
down_write_nested+0x1bd/0x1f0
lock_two_directories+0x186/0x330
__start_renaming+0xff/0x4e0
filename_renameat2+0x5bf/0x920
The root inode lock is not needed for what it protects here.
Subsystem directories are added to and removed from the root's
s_children under configfs_dirent_lock. A subsystem's dirent carries
CONFIGFS_USET_CREATING until configfs_register_subsystem() has attached
it, and configfs_unregister_subsystem() marks the subsystem's fragment
dead before it takes the root inode lock, and later gives the dirent
CONFIGFS_USET_DROPPING in configfs_detach_prep() under
configfs_dirent_lock. So look the target subsystem up, check that it is
neither being registered nor being unregistered, and take the
dependency, all in one configfs_dirent_lock section, and don't take the
root inode lock at all.
The one s_type update of a subsystem dirent that was only covered by
the root inode lock, setting CONFIGFS_USET_DIR in
configfs_attach_group(), happens after the dirent is already on the
root's s_children, so move it under configfs_dirent_lock too. frag_dead
is now read without frag_sem here, so write it with WRITE_ONCE() in
configfs_unregister_subsystem().
A depend that used to wait for a concurrent register of the target
subsystem now fails with -ENOENT while the subsystem is not on the
root's s_children yet or still marked CONFIGFS_USET_CREATING; once
configfs_dir_set_ready() has cleared that, it goes on to the usual scan
for the target. One that races with unregister fails with -ENOENT once
it observes the fragment marked dead or the dirent marked
CONFIGFS_USET_DROPPING. As before, unregister marks the fragment dead
before it takes the root inode lock.
configfs_unregister_subsystem() still does not look at
s_dependent_count; that is unchanged.
The same-subsystem case and configfs_depend_item() are unchanged.
Claude Opus 5.5 (Claude Code) was used to analyse the problem and to
write the code, this changelog and the test programs. The result was
reviewed by me and cross-checked with GPT-6.2-Sol and Gemini 4 Carbon.
Fixes: d79d75b5c518 ("fs: configfs: Add unlocked version of configfs_depend_item()")
Reported-by: William Chuang <founder@logarcheon.com>
Closes: https://lore.kernel.org/all/20261117150358.8149-1-founder@logarcheon.com/
Reported-by: syzbot+8c0a48f6c8357c9da819@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8c0a48f6c8357c9da819
Signed-off-by: Haruka Amakawa <macchacat@pm.me>
---
RFC because it changes how a cross-subsystem depend synchronizes with
subsystem (un)registration, and because of the lockdep-class commit
below.
Breno: "configfs: give the root inode its own lockdep class"
(https://patch.msgid.link/20261022134443.1122318-2-sst@poczta.fm) is
queued in configfs-next. Its changelog says that no holder of the root
inode lock waits for a directory inode lock held by a depend caller;
for the rename path above, though, lock_two_directories() can hold the
root inode lock while waiting for the descendant's inode lock, and with
the class split applied lockdep no longer reports that hang. With this
patch the root inode lock is no longer taken under a directory inode
lock on this path, so the class split is not needed for it either. If
this approach looks right, would you consider dropping the queued class
split? I tested this patch both on configfs-next and on configfs-next
with that commit reverted, with the same pass/fail results.
Testing (QEMU/KVM via virtme-ng, x86_64, PROVE_LOCKING + KASAN + hung
task detector at 30s). "Without the patch" is mainline 946d9ed1aa02 and
next-20261127; "with" is configfs-next 2e0fad9d2eae plus this patch:
- 60s loop of mkdir/rmdir of target/usb_gadget/naa.X/tpgt_1 against a
loop of rename("/sys/kernel/config/nonexistent",
".../naa.X/nonexistent"): without the patch both loops stop (on
mainline after 5 successful mkdirs, within the first second; on
next-20261127 after the first one) and the hung task detector fires
twice; with the patch, 5616 successful mkdirs and 17070 rename()
calls (5328 and 21653 with the class split reverted), no lockdep,
hung task or KASAN reports. rmdir of functions/tcm.usb0 still fails
with -EBUSY while tpgt_1 exists, and succeeds after it is removed.
- syzbot C reproducers for 8c0a48f6c8357c9da819 (uvc_func_allow_link)
and 300213c2b5b55c0244f4 (uvcg_extension_bm_controls_store): lockdep
report on mainline, none with the patch (also with the class commit
reverted). I did not add tags for the latter, since usb-next already
breaks that cycle on the UVC side with commit a3f3e1390f47 ("usb:
gadget: uvc: remove su_mutex from configfs _show/_store callbacks",
https://patch.msgid.link/20261119161742.758715-1-timo.joannin@gmail.com).
- tools/testing/selftests/filesystems/configfs: 25/25.
- Not tested: a depend racing with configfs_register_subsystem() or
configfs_unregister_subsystem() of the target subsystem.
Not in this patch, but found while looking at this:
- configfs_depend_item() takes the root inode lock the same way, and at
least vhost-scsi can call it with a mutex held that its ->make_group()
takes under a directory inode lock: vhost_scsi_set_endpoint() holds
vhost_scsi_mutex across target_depend_item(), and
vhost_scsi_make_tpg() takes vhost_scsi_mutex on mkdir of
target/vhost/<wwn>/tpgt_N with the <wwn> directory locked. Together
with an analogous rename() whose destination parent is
target/vhost/<wwn>, that looks like a three-task cycle. I have only
checked this by reading the code, not reproduced it. If this approach
is acceptable I can send the same change for configfs_depend_item() as
a follow-up.
- configfs_depend_item_unlocked() still walks target->ci_group up to the
root without any lock, as before; and the comment in f_tcm's
usbg_make_tpg() about the root inode lock becomes stale with this
patch.
---
Changes in v2:
- Use my real name for the author and Signed-off-by, as Greg asked
(no code changes).
- Link to v1: https://patch.msgid.link/20261129-configfs-depend-root-lock-v1-1-92d4b678e372@pm.me
---
fs/configfs/dir.c | 124 ++++++++++++++++++++++++++++++++----------------------
1 file changed, 74 insertions(+), 50 deletions(-)
diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index c4f570686bd2..d9e08d5e02df 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -907,7 +907,9 @@ static int configfs_attach_group(struct config_item *item,
ret = configfs_attach_item(item, dentry, frag);
if (!ret) {
sd = dentry->d_fsdata;
+ spin_lock(&configfs_dirent_lock);
sd->s_type |= CONFIGFS_USET_DIR;
+ spin_unlock(&configfs_dirent_lock);
/*
* FYI, we're faking mkdir in populate_groups()
@@ -1103,17 +1105,19 @@ static int configfs_depend_prep(struct configfs_dirent *sd,
return ret;
}
-static int configfs_do_depend_item(struct configfs_dirent *subsys_sd,
- struct config_item *target)
+/* Caller must hold configfs_dirent_lock. */
+static int __configfs_do_depend_item(struct configfs_dirent *subsys_sd,
+ struct config_item *target)
{
struct configfs_dirent *p;
int ret;
- spin_lock(&configfs_dirent_lock);
+ lockdep_assert_held(&configfs_dirent_lock);
+
/* Scan the tree, return 0 if found */
ret = configfs_depend_prep(subsys_sd, target);
if (ret)
- goto out_unlock_dirent_lock;
+ return ret;
/*
* We are sure that the item is not about to be removed by rmdir(), and
@@ -1122,27 +1126,46 @@ static int configfs_do_depend_item(struct configfs_dirent *subsys_sd,
p = target->ci_dentry->d_fsdata;
p->s_dependent_count += 1;
-out_unlock_dirent_lock:
+ return 0;
+}
+
+static int configfs_do_depend_item(struct configfs_dirent *subsys_sd,
+ struct config_item *target)
+{
+ int ret;
+
+ spin_lock(&configfs_dirent_lock);
+ ret = __configfs_do_depend_item(subsys_sd, target);
spin_unlock(&configfs_dirent_lock);
return ret;
}
-static inline struct configfs_dirent *
-configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
- struct config_item *subsys_item)
+static struct configfs_dirent *
+__configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
+ struct config_item *subsys_item)
{
struct configfs_dirent *p;
- struct configfs_dirent *ret = NULL;
- spin_lock(&configfs_dirent_lock);
+ lockdep_assert_held(&configfs_dirent_lock);
+
list_for_each_entry(p, &root_sd->s_children, s_sibling) {
if (p->s_type & CONFIGFS_DIR &&
- p->s_element == subsys_item) {
- ret = p;
- break;
- }
+ p->s_element == subsys_item)
+ return p;
}
+
+ return NULL;
+}
+
+static struct configfs_dirent *
+configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
+ struct config_item *subsys_item)
+{
+ struct configfs_dirent *ret;
+
+ spin_lock(&configfs_dirent_lock);
+ ret = __configfs_find_subsys_dentry(root_sd, subsys_item);
spin_unlock(&configfs_dirent_lock);
return ret;
@@ -1224,19 +1247,26 @@ EXPORT_SYMBOL(configfs_undepend_item);
/*
* caller_subsys is a caller's subsystem not target's. This is used to
- * determine if we should lock root and check subsys or not. When we are
- * in the same subsystem as our target there is no need to do locking as
- * we know that subsys is valid and is not unregistered during this function
- * as we are called from callback of one of his children and VFS holds a lock
- * on some inode. Otherwise we have to lock our root to ensure that target's
- * subsystem it is not unregistered during this function.
+ * determine if we have to check that the target's subsystem is still
+ * registered. When we are in the same subsystem as our target there is no
+ * need to, as we know that subsys is valid and is not unregistered during
+ * this function as we are called from callback of one of his children and
+ * VFS holds a lock on some inode.
+ *
+ * Otherwise the target's subsystem is looked up and the dependency is taken
+ * in one configfs_dirent_lock section, so the subsystem cannot be unlinked
+ * from the root in between. This must not take the root inode lock: the
+ * caller may be a directory operation callback such as ->make_group(), which
+ * runs with a directory inode locked by VFS, and a rename() between the root
+ * and a directory below it locks the root inode first (see
+ * lock_two_directories()), so that would be an ABBA deadlock.
*/
int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
struct config_item *target)
{
struct configfs_subsystem *target_subsys;
+ struct configfs_dirent *subsys_sd, *root_sd;
struct config_group *root, *parent;
- struct configfs_dirent *subsys_sd;
int ret = -ENOENT;
/* Disallow this function for configfs root */
@@ -1260,43 +1290,36 @@ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
root = parent;
} else {
target_subsys = parent->cg_subsys;
- /* Find a cofnigfs root as we may need it for locking */
+ /* Find the configfs root to look the subsystem up from */
for (root = parent; !configfs_is_root(&root->cg_item);
root = root->cg_item.ci_group)
;
}
- if (target_subsys != caller_subsys) {
- /*
- * We are in other configfs subsystem, so we have to do
- * additional locking to prevent other subsystem from being
- * unregistered
- */
- inode_lock(d_inode(root->cg_item.ci_dentry));
-
- /*
- * As we are trying to depend item from other subsystem
- * we have to check if this subsystem is still registered
- */
- subsys_sd = configfs_find_subsys_dentry(
- root->cg_item.ci_dentry->d_fsdata,
- &target_subsys->su_group.cg_item);
- if (!subsys_sd)
- goto out_root_unlock;
- } else {
+ if (target_subsys == caller_subsys) {
subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
+ return configfs_do_depend_item(subsys_sd, target);
}
- /* Now we can execute core of depend item */
- ret = configfs_do_depend_item(subsys_sd, target);
+ /*
+ * The target is in another subsystem: check that it is still
+ * registered, and not being registered or unregistered, and take the
+ * dependency without dropping configfs_dirent_lock in between.
+ * configfs_unregister_subsystem() marks the subsystem's fragment dead
+ * before it takes the root inode lock, and the dirent
+ * CONFIGFS_USET_DROPPING later.
+ */
+ root_sd = root->cg_item.ci_dentry->d_fsdata;
- if (target_subsys != caller_subsys)
-out_root_unlock:
- /*
- * We were called from subsystem other than our target so we
- * took some locks so now it's time to release them
- */
- inode_unlock(d_inode(root->cg_item.ci_dentry));
+ spin_lock(&configfs_dirent_lock);
+ subsys_sd = __configfs_find_subsys_dentry(root_sd,
+ &target_subsys->su_group.cg_item);
+ if (subsys_sd &&
+ !(subsys_sd->s_type & (CONFIGFS_USET_CREATING |
+ CONFIGFS_USET_DROPPING)) &&
+ !READ_ONCE(subsys_sd->s_frag->frag_dead))
+ ret = __configfs_do_depend_item(subsys_sd, target);
+ spin_unlock(&configfs_dirent_lock);
return ret;
}
@@ -1963,7 +1986,8 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
}
down_write(&frag->frag_sem);
- frag->frag_dead = true;
+ /* Read locklessly by configfs_depend_item_unlocked() */
+ WRITE_ONCE(frag->frag_dead, true);
up_write(&frag->frag_sem);
inode_lock_nested(d_inode(root),
---
base-commit: 2e0fad9d2eaec7d4ce24ecb087d7f58ed9ad57e2
change-id: 20261129-configfs-depend-root-lock-568b8d6f62df
Best regards,
--
Haruka Amakawa <macchacat@pm.me>
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH RFC v2] configfs: don't lock the root inode in configfs_depend_item_unlocked()
2026-11-29 12:38 [PATCH RFC v2] configfs: don't lock the root inode in configfs_depend_item_unlocked() Haruka Amakawa via B4 Relay
@ 2026-11-29 12:49 ` Greg Kroah-Hartman
0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-29 12:49 UTC (permalink / raw)
To: macchacat
Cc: Breno Leitao, Andreas Hindborg, Slawomir Stepien, William Chuang,
Roy, linux-fsdevel, linux-usb, target-devel, linux-kernel,
syzbot+8c0a48f6c8357c9da819
On Sun, Nov 29, 2026 at 09:38:55PM +0900, Haruka Amakawa via B4 Relay wrote:
> @@ -1103,17 +1105,19 @@ static int configfs_depend_prep(struct configfs_dirent *sd,
> return ret;
> }
>
> -static int configfs_do_depend_item(struct configfs_dirent *subsys_sd,
> - struct config_item *target)
> +/* Caller must hold configfs_dirent_lock. */
> +static int __configfs_do_depend_item(struct configfs_dirent *subsys_sd,
> + struct config_item *target)
Mark this with __must_hold() please.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-29 12:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 12:38 [PATCH RFC v2] configfs: don't lock the root inode in configfs_depend_item_unlocked() Haruka Amakawa via B4 Relay
2026-11-29 12:49 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®