[PATCH v1 1/2] smb: client: drop dentries for dying cached dirs during unmount
From: Siddharth Chintamaneni
Date: Tue Nov 17 2026 - 19:25:40 EST
During unmount, cifs_kill_sb() calls close_all_cached_dirs() to release
all cached directory dentries and flush cfid_put_wq before calling
kill_anon_super(). Two races allow a cached_fid's dentry reference to
outlive close_all_cached_dirs() and trigger a busy-dentry panic in
generic_shutdown_super():
1. When invalidate_all_cached_dirs() or
cifs_mark_open_handles_for_deleted_file() runs concurrently, it moves
cached_fid entries from cfids->entries onto cfids->dying under
cfids->cfid_list_lock, then drops cfid_list_lock before laundromat_work
runs on cfid_put_wq. If close_all_cached_dirs() runs in that window, it
only walks cfids->entries (missing cfids->dying) and flushes cfid_put_wq
before laundromat_work is queued, leaving cfid->dentry held:
Thread 1 (umount) Thread 2 (reconnect / open_dirs)
----------------- --------------------------------
invalidate_all_cached_dirs()
spin_lock(&cfid_list_lock)
list_move(&cfid->entry, &cfids->dying)
spin_unlock(&cfid_list_lock)
cifs_kill_sb()
close_all_cached_dirs()
spin_lock(&cfid_list_lock)
walks cfids->entries (empty)
spin_unlock(&cfid_list_lock)
flush_workqueue(cfid_put_wq)
kill_anon_super(sb)
-> generic_shutdown_super()
BUG: Dentry still in use (1)
VFS: Busy inodes after unmount!
mod_delayed_work(cfid_put_wq,
&cfids->laundromat_work, 0)
dput(cfid->dentry) [too late]
2. When two mounts share a cifs_tcon and drop_cached_dir_by_name() (for
example via rmdir on the sibling mount) drops the final reference in
close_cached_dir() -> smb2_close_cached_fid(), it deletes cfid from
cfids->entries under cfids->cfid_list_lock, unlocks cfid_list_lock, and
calls dput(cfid->dentry) synchronously outside cfid_put_wq. A
concurrent unmount of the superblock owning cfid->dentry can finish
close_all_cached_dirs() and reach kill_anon_super() before that dput()
completes.
Fix the first race by also walking cfids->dying under
cfids->cfid_list_lock in close_all_cached_dirs() and clearing cfid->dentry
under the lock. Fix the second race by detaching cfid->dentry under
cfids->cfid_list_lock in smb2_close_cached_fid() and holding
cifs_sb->outstanding_rreq across dput(dentry) so cifs_kill_sb() waits for
the in-flight dput() before calling kill_anon_super().
Fixes: 3fa640d035e5 ("smb: During unmount, ensure all cached dir instances drop their dentry")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@xxxxxxxxx>
---
fs/smb/client/cached_dir.c | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c
index 50217f47a0ed1..e5c924409078c 100644
--- a/fs/smb/client/cached_dir.c
+++ b/fs/smb/client/cached_dir.c
@@ -467,6 +467,8 @@ __releases(&cfid->cfids->cfid_list_lock)
{
struct cached_fid *cfid = container_of(ref, struct cached_fid,
refcount);
+ struct cifs_sb_info *cifs_sb = NULL;
+ struct dentry *dentry;
int rc;
lockdep_assert_held(&cfid->cfids->cfid_list_lock);
@@ -476,10 +478,19 @@ __releases(&cfid->cfids->cfid_list_lock)
cfid->on_list = false;
cfid->cfids->num_entries--;
}
+ dentry = cfid->dentry;
+ cfid->dentry = NULL;
+ if (dentry) {
+ cifs_sb = CIFS_SB(dentry->d_sb);
+ atomic_inc(&cifs_sb->outstanding_rreq);
+ }
spin_unlock(&cfid->cfids->cfid_list_lock);
- dput(cfid->dentry);
- cfid->dentry = NULL;
+ if (dentry) {
+ dput(dentry);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
+ }
if (cfid->is_open) {
rc = SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
@@ -594,6 +605,22 @@ void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
list_add_tail(&tmp_list->entry, &entry);
}
+ list_for_each_entry(cfid, &cfids->dying, entry) {
+ if (!cfid->dentry)
+ continue;
+ tmp_list = kmalloc_obj(*tmp_list, GFP_ATOMIC);
+ if (!tmp_list) {
+ cifs_tcon_dbg(VFS, "Out of memory while dropping dentries\n");
+ spin_unlock(&cfids->cfid_list_lock);
+ spin_unlock(&cifs_sb->tlink_tree_lock);
+ goto done;
+ }
+
+ tmp_list->dentry = cfid->dentry;
+ cfid->dentry = NULL;
+
+ list_add_tail(&tmp_list->entry, &entry);
+ }
spin_unlock(&cfids->cfid_list_lock);
}
spin_unlock(&cifs_sb->tlink_tree_lock);
--
2.43.0