smb: client: fix cached-dir and deferred-close unmount races
Subsystems (2)
cover smb: client: fix cached-dir and deferred-close unmount races
While investigating the cifs_kill_sb() unmount busy-dentry race fixed in "smb: client: fix busy dentry and inode on unmount from late oplock break" [1], I wrote a static analysis tool with AI assistance to scan for the same teardown pattern where objects are moved off shared lists and locks are dropped before their underlying dentry references are released.
That scan identified two more unmount races in cifs_kill_sb(), both of which I reproduced in QEMU where cifs_kill_sb() reaches generic_shutdown_super() while dentries are still in use:
- Patch 1 walks cfids->dying under cfids->cfid_list_lock in close_all_cached_dirs() and holds cifs_sb->outstanding_rreq across dput(dentry) in smb2_close_cached_fid() so dentries for dying or concurrently closed cached directories are dropped before kill_anon_super() runs.
- Patch 2 counts cancelled deferred-close handles in cifs_sb->outstanding_rreq while they sit on a local stack list before _cifsFileInfo_put() completes in cifs_close_deferred_file(), cifs_close_all_deferred_files(), and cifs_close_deferred_file_under_dentry().
[1] https://lore.kernel.org/all/[email protected]/
Siddharth Chintamaneni (2):
smb: client: drop dentries for dying cached dirs during unmount
smb: client: fix busy dentry on unmount from cancelled deferred close
fs/smb/client/cached_dir.c | 31 +++++++++++++++++++++++++++++-- fs/smb/client/misc.c | 32 +++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 smb: client: drop dentries for dying cached dirs during unmount
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().
--- fs/smb/client/cached_dir.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
fs/smb/client/cached_dir.c +29 -2
@@ -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);
Signature · 1 line
2.43.0
PATCH 2/2 smb: client: fix busy dentry on unmount from cancelled deferred close
In reply to Siddharth ChintamaneniWhen 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.
--- fs/smb/client/misc.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)
fs/smb/client/misc.c +29 -3
@@ -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); }
Signature · 1 line
2.43.0