Skip to contentSkip to conversation

smb: client: fix unmount busy dentry/inode races on openFileList

PATCH Applied Siddharth Chintamaneni 2 participants 4 messages Nov 17 – Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover smb: client: fix unmount busy dentry/inode races on openFileList

This series fixes two unmount races in cifs_kill_sb() where background paths walk tcon->openFileList while wait_var_event() is waiting on cifs_sb->outstanding_rreq and take new cifsFileInfo references that outlive the wait, causing busy dentry/inode warnings and panics in generic_shutdown_super():

- Patch 1 counts queued oplock/lease breaks in cifs_sb->outstanding_rreq from cifs_queue_oplock_break() until cifs_oplock_break() completes.
- Patch 2 counts persistent handle reopens in cifs_sb->outstanding_rreq in cifs_reopen_persistent_handles() until cifsFileInfo_put() drops the handle reference.

Siddharth Chintamaneni (2):
smb: client: fix busy dentry and inode on unmount from late oplock
break
smb: client: fix busy dentry on unmount from persistent handle reopen

 fs/smb/client/cifsfs.c | 10 +++++-----
 fs/smb/client/file.c   |  9 +++++++++
 fs/smb/client/misc.c   |  6 +++++-
 3 files changed, 19 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 smb: client: fix busy dentry and inode on unmount from late oplock break

During unmount, cifs_kill_sb() flushes cifsoplockd_wq and deferredclose_wq before waiting on cifs_sb->outstanding_rreq for in-flight netfs I/O requests to finish their cleanup_work in cifs_free_request().

While cifs_kill_sb() is waiting on outstanding_rreq, any cifsFileInfo referenced by an in-flight request (req->cfile) still has cfile->count >= 1 and remains on tcon->openFileList. If the SMB server sends a lease or oplock break for that file during this window, the demultiplex thread (cifsd) finds cfile on tcon->openFileList and calls cifs_queue_oplock_break(cfile), which takes a new reference on cfile and queues cfile->oplock_break onto cifsoplockd_wq after cifsoplockd_wq was already flushed:

  Thread 1 (umount)      Thread 2 (cifsd)       Thread 3 (workers)
  -----------------      ----------------       ------------------
  cifs_kill_sb()
  flush(cifsoplockd_wq)
  flush(deferredclose_wq)
  wait(outstanding_rreq)
    [blocks: rreq == 1]
                         SMB2 Lease Break
                         finds cfile on list
                         cifs_queue_oplock_break()
                           queue(cifsoplockd_wq)
                           cifsFileInfo_get(cfile)
                           [cfile->count: 1 -> 2]
                                                cifs_oplock_break()
                                                  ihold(inode)
                                                cifs_free_request()
                                                  cifsFileInfo_put()
                                                  [cfile->count: 2 -> 1]
                                                  dec(outstanding_rreq)
    [wakes: rreq == 0]

flush(serverclose_wq)
flush(fileinfo_put_wq)
kill_anon_super(sb)
-> generic_shutdown_super()
BUG: Dentry still in use (1)
VFS: Busy inodes after unmount!

Furthermore, cifs_oplock_break() itself holds the inode via ihold() after dropping cfile and may initiate writeback via filemap_fdatawrite(), spawning new netfs I/O requests.

Fix this by counting queued oplock breaks in cifs_sb->outstanding_rreq from when cifs_queue_oplock_break() queues the work until cifs_oplock_break() finishes, so wait_var_event() in cifs_kill_sb() waits for both in-flight I/O requests and any late oplock breaks to complete.

Fixes 340cea84f691 ("cifs: open files should not hold ref on superblock")
---
 fs/smb/client/cifsfs.c | 10 +++++-----
 fs/smb/client/file.c   |  2 ++
 fs/smb/client/misc.c   |  6 +++++-
 3 files changed, 12 insertions(+), 6 deletions(-)
fs/smb/client/cifsfs.c +5 -5
index 8cff7857b4509..9faba158f8b6a 100644
@@ -418,11 +418,11 @@ static void cifs_kill_sb(struct super_block *sb)
 		flush_workqueue(deferredclose_wq);
 
 		/*
-		 * Wait for all in-flight netfs I/O requests to finish their
-		 * cleanup_work so that any cifsFileInfo final puts they queue
-		 * to fileinfo_put_wq/serverclose_wq have been queued, then
-		 * drain the workqueue so the cfile dentry refs are dropped to
-		 * avoid the busy dentry warning.
+		 * Wait for all in-flight netfs I/O requests and oplock breaks
+		 * to finish so that any cifsFileInfo final puts they queue to
+		 * fileinfo_put_wq/serverclose_wq have been queued, then drain
+		 * the workqueue so the cfile dentry refs are dropped to avoid
+		 * the busy dentry warning.
 		 */
 		wait_var_event(&cifs_sb->outstanding_rreq,
 			       !atomic_read(&cifs_sb->outstanding_rreq));
fs/smb/client/file.c +2 -0
index f51653cb6432a..146ff2adfa244 100644
@@ -4023,6 +4023,8 @@ void cifs_oplock_break(struct work_struct *work)
 out:
 	cifs_done_oplock_break(cinode);
 	iput(inode);
+	if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+		wake_up_var(&cifs_sb->outstanding_rreq);
 }
 
 #ifdef CONFIG_SWAP
fs/smb/client/misc.c +5 -1
index 4d0df73d15d7a..72a986bfaefc4 100644
@@ -371,6 +371,8 @@ void cifs_put_writer(struct cifsInodeInfo *cinode)
  */
 void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
 {
+	struct cifs_sb_info *cifs_sb = CIFS_SB(d_inode(cfile->dentry)->i_sb);
+
 	/*
 	 * Bump the handle refcount now while we hold the
 	 * open_file_lock to enforce the validity of it for the oplock
@@ -379,8 +381,10 @@ void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
 	 *
 	 * Only take a reference if the work is actually queued.
 	 */
-	if (queue_work(cifsoplockd_wq, &cfile->oplock_break))
+	if (queue_work(cifsoplockd_wq, &cfile->oplock_break)) {
 		cifsFileInfo_get(cfile);
+		atomic_inc(&cifs_sb->outstanding_rreq);
+	}
 }
 
 void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 smb: client: fix busy dentry on unmount from persistent handle reopen

In reply to Siddharth Chintamaneni

During unmount, cifs_kill_sb() waits on cifs_sb->outstanding_rreq for in-flight netfs I/O requests and oplock breaks to complete before flushing serverclose_wq and fileinfo_put_wq.

While cifs_kill_sb() is waiting on outstanding_rreq, any cifsFileInfo referenced by an in-flight request (req->cfile) remains on tcon->openFileList. On a mount using persistent handles, if a reconnect runs on cifsiod_wq during this window, smb2_reconnect_server() calls cifs_reopen_persistent_handles(), which walks tcon->openFileList and takes a new reference on open_file via cifsFileInfo_get() to reopen the handle over the network:

  Thread 1 (umount)      Thread 2 (cifsiod_wq)    Thread 3 (workers)
  -----------------      ---------------------    ------------------
  cifs_kill_sb()
  wait(outstanding_rreq)
    [blocks: rreq == 1]
                         smb2_reconnect_server()
                         cifs_reopen_persistent_handles()
                           finds open_file on list
                           cifsFileInfo_get(open_file)
                           [cfile->count: 1 -> 2]
                           cifs_reopen_file()
                                                  cifs_free_request()
                                                    cifsFileInfo_put()
                                                    [cfile->count: 2 -> 1]
                                                    dec(outstanding_rreq)
    [wakes: rreq == 0]

flush(serverclose_wq)
flush(fileinfo_put_wq)
kill_anon_super(sb)
-> generic_shutdown_super()
BUG: Dentry still in use (1)
VFS: Busy inodes after unmount!

Fix this by incrementing cifs_sb->outstanding_rreq when cifs_reopen_persistent_handles() takes a reference on open_file and decrementing it after cifsFileInfo_put(open_file), so wait_var_event() in cifs_kill_sb() waits for any in-progress persistent handle reopen to finish and queue its final put before draining fileinfo_put_wq.

Fixes 340cea84f691 ("cifs: open files should not hold ref on superblock")
---
 fs/smb/client/file.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/smb/client/file.c +7 -0
index 146ff2adfa244..e0b56c6c2c743 100644
@@ -2044,6 +2044,7 @@ bool
 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
 {
 	struct cifsFileInfo *open_file, *tmp;
+	struct cifs_sb_info *cifs_sb;
 	LIST_HEAD(tmp_list);
 	bool retry = false;
 	int rc;
@@ -2060,12 +2061,16 @@ cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
 	list_for_each_entry(open_file, &tcon->openFileList, tlist) {
 		if (!open_file->invalidHandle)
 			continue;
+		cifs_sb = CIFS_SB(d_inode(open_file->dentry)->i_sb);
 		cifsFileInfo_get(open_file);
+		atomic_inc(&cifs_sb->outstanding_rreq);
 		list_add_tail(&open_file->rlist, &tmp_list);
 	}
 	spin_unlock(&tcon->open_file_lock);
 
 	list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
+		cifs_sb = CIFS_SB(d_inode(open_file->dentry)->i_sb);
+
 		/* do not flush */
 		rc = cifs_reopen_file(open_file, false,
 				      CIFS_REOPEN_BOUND | CIFS_REOPEN_NOWAIT);
@@ -2075,6 +2080,8 @@ cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
 		}
 		list_del_init(&open_file->rlist);
 		cifsFileInfo_put(open_file);
+		if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
+			wake_up_var(&cifs_sb->outstanding_rreq);
 	}
 	return retry;
 }
Signature · 1 line
2.43.0
maintainer Applied
#3 lore raw
In reply to Siddharth Chintamaneni
Quoted 6 lines · Siddharth Chintamaneni

This series fixes two unmount races in cifs_kill_sb() where background paths walk tcon->openFileList while wait_var_event() is waiting on cifs_sb->outstanding_rreq and take new cifsFileInfo references that outlive the wait, causing busy dentry/inode warnings and panics in generic_shutdown_super():

Quoted 6 lines

- Patch 1 counts queued oplock/lease breaks in cifs_sb->outstanding_rreq from cifs_queue_oplock_break() until cifs_oplock_break() completes.
- Patch 2 counts persistent handle reopens in cifs_sb->outstanding_rreq in cifs_reopen_persistent_handles() until cifsFileInfo_put() drops the handle reference.
...

Applied.