Skip to contentSkip to conversation

cifs: Corrections to lock ordering notes

PATCH In review [email protected] 2 participants 3 messages Jan 31

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

PATCH v2 1/2 cifs: Corrections to lock ordering notes

From: Shyam Prasad N <[email protected]>

There were a couple of discrepencies in lock ordering for the locks that were specified in the lock ordering notes. Did an analysis of the current codebase (using LLM) and found two pairs whose ordering in these notes were wrong. It also found one lock that was recently removed, and a few locks that weren't documented here before.

---
 fs/smb/client/cifsglob.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/smb/client/cifsglob.h +11 -4
index 3eca5bfb70303..d797b953b6cf6 100644
@@ -1943,6 +1943,8 @@ require use of the stronger protocol */
  */
 
 /****************************************************************************
+ * LOCK ORDERING NOTES:
+ ****************************************************************************
  * Here are all the locks (spinlock, mutex, semaphore) in cifs.ko, arranged according
  * to the locking order. i.e. if two locks are to be held together, the lock that
  * appears higher in this list needs to be taken before the other.
@@ -1971,18 +1973,21 @@ require use of the stronger protocol */
  * =====================================================================================
  * Lock				Protects			Initialization fn
  * =====================================================================================
+ * cifs_mount_mutex		mount/unmount operations
  * vol_list_lock
  * vol_info->ctx_lock		vol_info->ctx
  * cifs_sb_info->tlink_tree_lock	cifs_sb_info->tlink_tree	cifs_setup_cifs_sb
  * TCP_Server_Info->		TCP_Server_Info			cifs_get_tcp_session
  * reconnect_mutex
- * TCP_Server_Info->srv_mutex	TCP_Server_Info			cifs_get_tcp_session
  * cifs_ses->session_mutex	cifs_ses			sesInfoAlloc
+ * TCP_Server_Info->_srv_mutex	TCP_Server_Info			cifs_get_tcp_session
+ * cifs_tcp_ses_lock		cifs_tcp_ses_list		sesInfoAlloc
  * cifs_tcon->open_file_lock	cifs_tcon->openFileList		tconInfoAlloc
  *				cifs_tcon->pending_opens
  * cifs_tcon->stat_lock		cifs_tcon->bytes_read		tconInfoAlloc
  *				cifs_tcon->bytes_written
- * cifs_tcp_ses_lock		cifs_tcp_ses_list		sesInfoAlloc
+ * cifs_tcon->fscache_lock	cifs_tcon->fscache		tconInfoAlloc
+ * cifs_tcon->sb_list_lock	cifs_tcon->cifs_sb_list		tconInfoAlloc
  * GlobalMid_Lock		GlobalMaxActiveXid		init_cifs
  *				GlobalCurrentXid
  *				GlobalTotalActiveXid
@@ -2005,6 +2010,8 @@ require use of the stronger protocol */
  *				->chans_in_reconnect
  * cifs_tcon->tc_lock		(anything that is not protected by another lock and can change)
  *								tcon_info_alloc
+ * cifs_swnreg_idr_mutex	cifs_swnreg_idr			cifs_swn.c
+ *				(witness service registration, accesses tcon fields under tc_lock)
  * inode->i_rwsem, taken by fs/netfs/locking.c e.g. should be taken before cifsInodeInfo locks
  * cifsInodeInfo->open_file_lock	cifsInodeInfo->openFileList	cifs_alloc_inode
  * cifsInodeInfo->writers_lock	cifsInodeInfo->writers		cifsInodeInfo_alloc
@@ -2012,12 +2019,12 @@ require use of the stronger protocol */
  *				->can_cache_brlcks
  * cifsInodeInfo->deferred_lock	cifsInodeInfo->deferred_closes	cifsInodeInfo_alloc
  * cached_fids->cfid_list_lock	cifs_tcon->cfids->entries	init_cached_dirs
- * cached_fid->fid_lock		(anything that is not protected by another lock and can change)
- *								init_cached_dir
+ * cached_fid->dirents.de_mutex	cached_fid->dirents		alloc_cached_dir
  * cifsFileInfo->fh_mutex	cifsFileInfo			cifs_new_fileinfo
  * cifsFileInfo->file_info_lock	cifsFileInfo->count		cifs_new_fileinfo
  *				->invalidHandle			initiate_cifs_search
  *				->oplock_break_cancelled
+ * smbdirect_mr->mutex		RDMA memory region management	(SMBDirect only)
  * mid_q_entry->mid_lock	mid_q_entry->callback           alloc_mid
  *								smb2_mid_entry_alloc
  *				(Any fields of mid_q_entry that will need protection)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/2 cifs: Fix locking usage for tcon fields

From: Shyam Prasad N <[email protected]>

We used to use the cifs_tcp_ses_lock to protect a lot of objects that are not just the server, ses or tcon lists. We later introduced srv_lock, ses_lock and tc_lock to protect fields within the corresponding structs. This was done to provide a more granular protection and avoid unnecessary serialization.

There were still a couple of uses of cifs_tcp_ses_lock to provide tcon fields. In this patch, I've replaced them with tc_lock.

---
 fs/smb/client/cached_dir.c | 4 ++--
 fs/smb/client/smb2misc.c   | 6 +++---
 fs/smb/client/smb2ops.c    | 8 +++-----
 fs/smb/client/smb2pdu.c    | 2 ++
 fs/smb/client/trace.h      | 1 +
 5 files changed, 11 insertions(+), 10 deletions(-)
fs/smb/client/cached_dir.c +2 -2
index 1db7ab6c2529c..569030b3e68d4 100644
@@ -788,11 +788,11 @@ static void cfids_laundromat_worker(struct work_struct *work)
 		cfid->dentry = NULL;
 
 		if (cfid->is_open) {
-			spin_lock(&cifs_tcp_ses_lock);
+			spin_lock(&cfid->tcon->tc_lock);
 			++cfid->tcon->tc_count;
 			trace_smb3_tcon_ref(cfid->tcon->debug_id, cfid->tcon->tc_count,
 					    netfs_trace_tcon_ref_get_cached_laundromat);
-			spin_unlock(&cifs_tcp_ses_lock);
+			spin_unlock(&cfid->tcon->tc_lock);
 			queue_work(serverclose_wq, &cfid->close_work);
 		} else
 			/*
fs/smb/client/smb2misc.c +3 -3
index f3cb62d914502..0871b9f1f86a6 100644
@@ -820,14 +820,14 @@ smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
 	int rc;
 
 	cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
-	spin_lock(&cifs_tcp_ses_lock);
+	spin_lock(&tcon->tc_lock);
 	if (tcon->tc_count <= 0) {
 		struct TCP_Server_Info *server = NULL;
 
 		trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
 				    netfs_trace_tcon_ref_see_cancelled_close);
 		WARN_ONCE(tcon->tc_count < 0, "tcon refcount is negative");
-		spin_unlock(&cifs_tcp_ses_lock);
+		spin_unlock(&tcon->tc_lock);
 
 		if (tcon->ses) {
 			server = tcon->ses->server;
@@ -841,7 +841,7 @@ smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
 	tcon->tc_count++;
 	trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
 			    netfs_trace_tcon_ref_get_cancelled_close);
-	spin_unlock(&cifs_tcp_ses_lock);
+	spin_unlock(&tcon->tc_lock);
 
 	rc = __smb2_handle_cancelled_cmd(tcon, SMB2_CLOSE_HE, 0,
 					 persistent_fid, volatile_fid);
fs/smb/client/smb2ops.c +3 -5
index c1aaf77e187b6..0b2abd0fdad11 100644
@@ -3091,7 +3091,9 @@ smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
 						struct cifs_tcon,
 						tcon_list);
 		if (tcon) {
+			spin_lock(&tcon->tc_lock);
 			tcon->tc_count++;
+			spin_unlock(&tcon->tc_lock);
 			trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
 					    netfs_trace_tcon_ref_get_dfs_refer);
 		}
@@ -3160,13 +3162,9 @@ smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
  out:
 	if (tcon && !tcon->ipc) {
 		/* ipc tcons are not refcounted */
-		spin_lock(&cifs_tcp_ses_lock);
-		tcon->tc_count--;
+		cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_dfs_refer);
 		trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
 				    netfs_trace_tcon_ref_dec_dfs_refer);
-		/* tc_count can never go negative */
-		WARN_ON(tcon->tc_count < 0);
-		spin_unlock(&cifs_tcp_ses_lock);
 	}
 	kfree(utf16_path);
 	kfree(dfs_req);
fs/smb/client/smb2pdu.c +2 -0
index 5d57c895ca37a..c7e086dfb1765 100644
@@ -4239,7 +4239,9 @@ void smb2_reconnect_server(struct work_struct *work)
 
 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
 			if (tcon->need_reconnect || tcon->need_reopen_files) {
+				spin_lock(&tcon->tc_lock);
 				tcon->tc_count++;
+				spin_unlock(&tcon->tc_lock);
 				trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
 						    netfs_trace_tcon_ref_get_reconnect_server);
 				list_add_tail(&tcon->rlist, &tmp_list);
fs/smb/client/trace.h +1 -0
index a584a77431132..191f02344dcdd 100644
@@ -189,6 +189,7 @@
 	EM(netfs_trace_tcon_ref_put_cancelled_close_fid, "PUT Cn-Fid") \
 	EM(netfs_trace_tcon_ref_put_cancelled_mid,	"PUT Cn-Mid") \
 	EM(netfs_trace_tcon_ref_put_mnt_ctx,		"PUT MntCtx") \
+	EM(netfs_trace_tcon_ref_put_dfs_refer,		"PUT DfsRfr") \
 	EM(netfs_trace_tcon_ref_put_reconnect_server,	"PUT Reconn") \
 	EM(netfs_trace_tcon_ref_put_tlink,		"PUT Tlink ") \
 	EM(netfs_trace_tcon_ref_see_cancelled_close,	"SEE Cn-Cls") \
Signature · 1 line
2.43.0
#2 lore raw
In reply to [email protected]

Fixed typo and merged these two to cifs-2.6.git for-next

Quoted 30 lines · On Sat, Jan 31, 2026 at 12:52 PM <[email protected]> wrote:

From: Shyam Prasad N <[email protected]>

There were a couple of discrepencies in lock ordering for the locks that were specified in the lock ordering notes. Did an analysis of the current codebase (using LLM) and found two pairs whose ordering in these notes were wrong. It also found one lock that was recently removed, and a few locks that weren't documented here before.

---
 fs/smb/client/cifsglob.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/smb/client/cifsglob.h +2 -0
index 3eca5bfb70303..d797b953b6cf6 100644
@@ -1943,6 +1943,8 @@ require use of the stronger protocol */
  */
 
 /****************************************************************************
+ * LOCK ORDERING NOTES:
+ ****************************************************************************
  * Here are all the locks (spinlock, mutex, semaphore) in cifs.ko, arranged according
  * to the locking order. i.e. if two locks are to be held together, the lock that
  * appears higher in this list needs to be taken before the other.
@@ -1971,18 +1973,21 @@ require use of the stronger protocol */
  * =====================================================================================
  * Lock                                Protects                        Initialization fn

fs/smb/client/cifsglob.h require use of the stronger protocol */ · line 1983

Quoted 10 lines
fs/smb/client/cifsglob.h +2 -1
  * =====================================================================================
+ * cifs_mount_mutex            mount/unmount operations
  * vol_list_lock
  * vol_info->ctx_lock          vol_info->ctx
  * cifs_sb_info->tlink_tree_lock       cifs_sb_info->tlink_tree        cifs_setup_cifs_sb
  * TCP_Server_Info->           TCP_Server_Info                 cifs_get_tcp_session
  * reconnect_mutex
- * TCP_Server_Info->srv_mutex  TCP_Server_Info                 cifs_get_tcp_session
  * cifs_ses->session_mutex     cifs_ses                        sesInfoAlloc
+ * TCP_Server_Info->_srv_mutex TCP_Server_Info                 cifs_get_tcp_session

Fixed typo "_srv_mutex" should be "srv_mutex"

fs/smb/client/cifsglob.h require use of the stronger protocol */ · line 1984

Quoted 1 line
fs/smb/client/cifsglob.h +1 -0
+ * cifs_tcp_ses_lock           cifs_tcp_ses_list               sesInfoAlloc

Would it be more logical to fix the code that calls it in the unexpected order or fix the lock
description. The original idea IIRC was to grab TCP session locks before locks on things
that hang off it (like session, and tcon), but in this case it probably is ok but curious if
changing the code would be more intuitive

Quoted 37 lines
fs/smb/client/cifsglob.h +6 -4
  * cifs_tcon->open_file_lock   cifs_tcon->openFileList         tconInfoAlloc
  *                             cifs_tcon->pending_opens
  * cifs_tcon->stat_lock                cifs_tcon->bytes_read           tconInfoAlloc
  *                             cifs_tcon->bytes_written
- * cifs_tcp_ses_lock           cifs_tcp_ses_list               sesInfoAlloc
+ * cifs_tcon->fscache_lock     cifs_tcon->fscache              tconInfoAlloc
+ * cifs_tcon->sb_list_lock     cifs_tcon->cifs_sb_list         tconInfoAlloc
  * GlobalMid_Lock              GlobalMaxActiveXid              init_cifs
  *                             GlobalCurrentXid
  *                             GlobalTotalActiveXid
@@ -2005,6 +2010,8 @@ require use of the stronger protocol */
  *                             ->chans_in_reconnect
  * cifs_tcon->tc_lock          (anything that is not protected by another lock and can change)
  *                                                             tcon_info_alloc
+ * cifs_swnreg_idr_mutex       cifs_swnreg_idr                 cifs_swn.c
+ *                             (witness service registration, accesses tcon fields under tc_lock)
  * inode->i_rwsem, taken by fs/netfs/locking.c e.g. should be taken before cifsInodeInfo locks
  * cifsInodeInfo->open_file_lock       cifsInodeInfo->openFileList     cifs_alloc_inode
  * cifsInodeInfo->writers_lock cifsInodeInfo->writers          cifsInodeInfo_alloc
@@ -2012,12 +2019,12 @@ require use of the stronger protocol */
  *                             ->can_cache_brlcks
  * cifsInodeInfo->deferred_lock        cifsInodeInfo->deferred_closes  cifsInodeInfo_alloc
  * cached_fids->cfid_list_lock cifs_tcon->cfids->entries       init_cached_dirs
- * cached_fid->fid_lock                (anything that is not protected by another lock and can change)
- *                                                             init_cached_dir
+ * cached_fid->dirents.de_mutex        cached_fid->dirents             alloc_cached_dir
  * cifsFileInfo->fh_mutex      cifsFileInfo                    cifs_new_fileinfo
  * cifsFileInfo->file_info_lock        cifsFileInfo->count             cifs_new_fileinfo
  *                             ->invalidHandle                 initiate_cifs_search
  *                             ->oplock_break_cancelled
+ * smbdirect_mr->mutex         RDMA memory region management   (SMBDirect only)
  * mid_q_entry->mid_lock       mid_q_entry->callback           alloc_mid
  *                                                             smb2_mid_entry_alloc
  *                             (Any fields of mid_q_entry that will need protection)
--

2.43.0

Signature · 3 lines
Thanks,

Steve