* [PATCH v2 1/2] smb: client: drop dentries for dying cached dirs during unmount
2026-11-30 18:11 [PATCH v2 0/2] smb: client: fix cached-dir and deferred-close unmount races Siddharth Chintamaneni
@ 2026-11-30 18:11 ` Siddharth Chintamaneni
2026-11-30 18:11 ` [PATCH v2 2/2] smb: client: fix busy dentry on unmount from cancelled deferred close Siddharth Chintamaneni
1 sibling, 0 replies; 3+ messages in thread
From: Siddharth Chintamaneni @ 2026-11-30 18:11 UTC (permalink / raw)
To: Paulo Alcantara
Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
Allen Pais, Rachel Menge, Hardik Garg, Siddharth Chintamaneni,
Gillian Laming, linux-cifs, linux-kernel
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(). Three 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 cfids_laundromat_worker() 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, close_all_cached_dirs() detaches
cfid->dentry from cfids->entries without checking whether the dentry
belongs to the unmounting superblock. This both clears cfid->dentry
on live cached directories belonging to the sibling mount and allows
cifs_sb1 to steal a dentry belonging to a concurrently unmounting
cifs_sb2, letting cifs_sb2 reach kill_anon_super() before cifs_sb1
calls dput().
3. 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 these by walking both cfids->entries and cfids->dying under
cfids->cfid_list_lock in close_all_cached_dirs() while filtering for
dentries belonging to cifs_sb, and by detaching cfid->dentry under
cfids->cfid_list_lock in smb2_close_cached_fid() while holding
cifs_sb->outstanding_rreq across dput(dentry).
Fixes: 3fa640d035e5 ("smb: During unmount, ensure all cached dir instances drop their dentry")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
---
Changes in v2:
- Filter cfids->entries and cfids->dying in close_all_cached_dirs() for
dentries belonging to cifs_sb.
- Fix function name in commit description.
fs/smb/client/cached_dir.c | 35 +++++++++++++++++++++++++++++++++--
1 file changed, 33 insertions(+), 2 deletions(-)
diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c
index 50217f47a0ed1..2c6243cbe10e4 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,
@@ -576,6 +587,9 @@ void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
continue;
spin_lock(&cfids->cfid_list_lock);
list_for_each_entry(cfid, &cfids->entries, entry) {
+ if (!cfid->dentry ||
+ CIFS_SB(cfid->dentry->d_sb) != cifs_sb)
+ continue;
tmp_list = kmalloc_obj(*tmp_list, GFP_ATOMIC);
if (tmp_list == NULL) {
/*
@@ -594,6 +608,23 @@ 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 ||
+ CIFS_SB(cfid->dentry->d_sb) != cifs_sb)
+ 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
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] smb: client: fix busy dentry on unmount from cancelled deferred close
2026-11-30 18:11 [PATCH v2 0/2] smb: client: fix cached-dir and deferred-close unmount races Siddharth Chintamaneni
2026-11-30 18:11 ` [PATCH v2 1/2] smb: client: drop dentries for dying cached dirs during unmount Siddharth Chintamaneni
@ 2026-11-30 18:11 ` Siddharth Chintamaneni
1 sibling, 0 replies; 3+ messages in thread
From: Siddharth Chintamaneni @ 2026-11-30 18:11 UTC (permalink / raw)
To: Paulo Alcantara
Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
Allen Pais, Rachel Menge, Hardik Garg, Siddharth Chintamaneni,
Gillian Laming, linux-cifs, linux-kernel
When cifs_close_deferred_file(), cifs_close_all_deferred_files(), or
cifs_close_deferred_file_under_dentry() cancels a pending deferred close
via cancel_delayed_work(&cfile->deferred), the cifsFileInfo is moved to
a thread-local stack list (file_head) before open_file_lock is dropped
and _cifsFileInfo_put() is called outside the lock.
Because tcon->openFileList is shared across superblocks using the same
tree connection, a concurrent operation or unmount on a sibling mount
sharing the tcon can cancel a deferred close work item belonging to a
superblock that is concurrently unmounting. Once cancel_delayed_work()
removes the work from deferredclose_wq, cifs_kill_sb() observes neither
a pending delayed work item on deferredclose_wq nor a non-zero
outstanding_rreq counter, and proceeds to kill_anon_super() before
_cifsFileInfo_put() drops cfile->dentry:
Thread 1 (umount sb1) Thread 2 (sibling mount / rename)
--------------------- ---------------------------------
cifs_close_all_deferred_files(tcon)
spin_lock(&tcon->open_file_lock)
cancel_delayed_work(&cfile->deferred)
list_add_tail(..., &file_head)
spin_unlock(&tcon->open_file_lock)
cifs_kill_sb(sb1)
cifs_close_all_deferred_files_sb()
[delayed_work_pending == false]
flush(cifsoplockd_wq)
flush(deferredclose_wq)
wait(outstanding_rreq == 0)
flush(serverclose_wq)
flush(fileinfo_put_wq)
kill_anon_super(sb1)
-> generic_shutdown_super()
BUG: Dentry still in use (1)
VFS: Busy inodes after unmount!
_cifsFileInfo_put(cfile, true, false)
dput(cfile->dentry) [too late]
Fix this by incrementing cifs_sb->outstanding_rreq while still holding
open_file_lock whenever cancel_delayed_work(&cfile->deferred) succeeds,
and decrementing it (waking any waiter in cifs_kill_sb()) after
_cifsFileInfo_put() completes.
Fixes: 340cea84f691 ("cifs: open files should not hold ref on superblock")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
---
Changes in v2:
- Update Fixes: tag to 340cea84f691 ("cifs: open files should not hold
ref on superblock").
fs/smb/client/misc.c | 32 +++++++++++++++++++++++++++++---
1 file changed, 29 insertions(+), 3 deletions(-)
diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c
index 72a986bfaefc4..5072efd0ea568 100644
--- a/fs/smb/client/misc.c
+++ b/fs/smb/client/misc.c
@@ -502,15 +502,19 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
{
struct cifsFileInfo *cfile = NULL, *failed_cfile = NULL;
struct file_list *tmp_list, *tmp_next_list;
+ struct cifs_sb_info *cifs_sb;
LIST_HEAD(file_head);
if (cifs_inode == NULL)
return;
+ cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
+
spin_lock(&cifs_inode->open_file_lock);
list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
if (delayed_work_pending(&cfile->deferred)) {
if (cancel_delayed_work(&cfile->deferred)) {
+ atomic_inc(&cifs_sb->outstanding_rreq);
spin_lock(&cifs_inode->deferred_lock);
cifs_del_deferred_close(cfile);
spin_unlock(&cifs_inode->deferred_lock);
@@ -528,11 +532,16 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
}
spin_unlock(&cifs_inode->open_file_lock);
- if (failed_cfile)
+ if (failed_cfile) {
_cifsFileInfo_put(failed_cfile, false, false);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
+ }
list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
_cifsFileInfo_put(tmp_list->cfile, false, false);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
list_del(&tmp_list->list);
kfree(tmp_list);
}
@@ -543,12 +552,15 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
{
struct cifsFileInfo *cfile, *failed_cfile = NULL;
struct file_list *tmp_list, *tmp_next_list;
+ struct cifs_sb_info *cifs_sb;
LIST_HEAD(file_head);
spin_lock(&tcon->open_file_lock);
list_for_each_entry(cfile, &tcon->openFileList, tlist) {
if (delayed_work_pending(&cfile->deferred)) {
if (cancel_delayed_work(&cfile->deferred)) {
+ cifs_sb = CIFS_SB(d_inode(cfile->dentry)->i_sb);
+ atomic_inc(&cifs_sb->outstanding_rreq);
spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
cifs_del_deferred_close(cfile);
spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
@@ -566,11 +578,18 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
}
spin_unlock(&tcon->open_file_lock);
- if (failed_cfile)
+ if (failed_cfile) {
+ cifs_sb = CIFS_SB(d_inode(failed_cfile->dentry)->i_sb);
_cifsFileInfo_put(failed_cfile, true, false);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
+ }
list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
+ cifs_sb = CIFS_SB(d_inode(tmp_list->cfile->dentry)->i_sb);
_cifsFileInfo_put(tmp_list->cfile, true, false);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
list_del(&tmp_list->list);
kfree(tmp_list);
}
@@ -618,6 +637,7 @@ void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
{
struct file_list *tmp_list, *tmp_next_list;
struct cifsFileInfo *cfile, *failed_cfile = NULL;
+ struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
LIST_HEAD(file_head);
spin_lock(&tcon->open_file_lock);
@@ -625,6 +645,7 @@ void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
if ((cfile->dentry == dentry) &&
delayed_work_pending(&cfile->deferred) &&
cancel_delayed_work(&cfile->deferred)) {
+ atomic_inc(&cifs_sb->outstanding_rreq);
spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
cifs_del_deferred_close(cfile);
spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
@@ -640,11 +661,16 @@ void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
}
spin_unlock(&tcon->open_file_lock);
- if (failed_cfile)
+ if (failed_cfile) {
_cifsFileInfo_put(failed_cfile, true, false);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
+ }
list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
_cifsFileInfo_put(tmp_list->cfile, true, false);
+ if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+ wake_up_var(&cifs_sb->outstanding_rreq);
list_del(&tmp_list->list);
kfree(tmp_list);
}
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread