mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
To: Paulo Alcantara <pc@manguebit.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Tom Talpey <tom@talpey.com>, Bharath SM <bharathsm@microsoft.com>,
	Allen Pais <apais@microsoft.com>,
	Rachel Menge <rachelmenge@linux.microsoft.com>,
	Hardik Garg <hargar@linux.microsoft.com>,
	Siddharth Chintamaneni <sidchintamaneni@gmail.com>,
	Gillian Laming <glaming@microsoft.com>,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/2] smb: client: drop dentries for dying cached dirs during unmount
Date: Mon, 30 Nov 2026 18:11:58 +0000	[thread overview]
Message-ID: <20261130181159.93456-2-sidchintamaneni@gmail.com> (raw)
In-Reply-To: <20261130181159.93456-1-sidchintamaneni@gmail.com>

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


  reply	other threads:[~2026-11-30 18:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-30 18:11 ` [PATCH v2 2/2] smb: client: fix busy dentry on unmount from cancelled deferred close Siddharth Chintamaneni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130181159.93456-2-sidchintamaneni@gmail.com \
    --to=sidchintamaneni@gmail.com \
    --cc=apais@microsoft.com \
    --cc=bharathsm@microsoft.com \
    --cc=glaming@microsoft.com \
    --cc=hargar@linux.microsoft.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pc@manguebit.org \
    --cc=rachelmenge@linux.microsoft.com \
    --cc=ronniesahlberg@gmail.com \
    --cc=sprasad@microsoft.com \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®