[PATCH v2 02/12] smb: client: use kref for the TCP_Server_Info object references

From: Stefan Metzmacher

Date: Fri Dec 04 2026 - 08:34:13 EST


The struct TCP_Server_Info lifetime references (obj_refs) currently
have exactly two holders, the session side and the cifsd thread, both
in connect.c.

Convert obj_refs to struct kref, move the free code into a release
function and add cifs_tcp_server_get() and cifs_tcp_server_put(), so
that the next commit can let in-flight requests hold a reference from
other files.

No functional change.

Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits")
Cc: stable@xxxxxxxxxxxxxxx
Cc: Eduard Zateev <hackerowskiy@xxxxxxxxx>
Cc: Paulo Alcantara <pc@xxxxxxxxxxxxx>
Cc: Namjae Jeon <linkinjeon@xxxxxxxxxx>
Cc: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
Cc: Shyam Prasad N <sprasad@xxxxxxxxxxxxx>
Cc: Tom Talpey <tom@xxxxxxxxxx>
Cc: Bharath SM <bharathsm@xxxxxxxxxxxxx>
Cc: linux-cifs@xxxxxxxxxxxxxxx
Cc: samba-technical@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Signed-off-by: Stefan Metzmacher <metze@xxxxxxxxx>
---
fs/smb/client/cifsglob.h | 3 ++-
fs/smb/client/cifsproto.h | 2 ++
fs/smb/client/connect.c | 27 ++++++++++++++++++++++-----
3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/fs/smb/client/cifsglob.h b/fs/smb/client/cifsglob.h
index 3bfa9b1bacf5..51c987806fd7 100644
--- a/fs/smb/client/cifsglob.h
+++ b/fs/smb/client/cifsglob.h
@@ -16,6 +16,7 @@
#include <linux/scatterlist.h>
#include <linux/mm.h>
#include <linux/mempool.h>
+#include <linux/kref.h>
#include <linux/refcount.h>
#include <linux/workqueue.h>
#include <linux/utsname.h>
@@ -682,7 +683,7 @@ struct TCP_Server_Info {
spinlock_t srv_lock; /* protect anything here that is not protected */
__u64 conn_id; /* connection identifier (useful for debugging) */
int srv_count; /* reference counter */
- refcount_t obj_refs; /* struct lifetime: srv_count users + cifsd */
+ struct kref obj_refs; /* struct lifetime, see cifs_tcp_server_get() */
int rfc1001_sessinit; /* whether to estasblish netbios session */
bool with_rfc1001; /* if netbios session is used */
/* 15 character server name + 0x20 16th byte indicating type = srv */
diff --git a/fs/smb/client/cifsproto.h b/fs/smb/client/cifsproto.h
index 15123c7b9373..f6c95a47525b 100644
--- a/fs/smb/client/cifsproto.h
+++ b/fs/smb/client/cifsproto.h
@@ -278,6 +278,8 @@ void cifs_mark_open_handles_for_deleted_file(struct inode *inode,
struct TCP_Server_Info *cifs_get_tcp_session(struct smb3_fs_context *ctx,
struct TCP_Server_Info *primary_server);
void cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect);
+void cifs_tcp_server_get(struct TCP_Server_Info *server);
+void cifs_tcp_server_put(struct TCP_Server_Info *server);
void cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace);

void cifs_release_automount_timer(void);
diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c
index 064b70bf1fdf..3766f486727a 100644
--- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c
@@ -1084,10 +1084,10 @@ cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
return 0;
}

-static void cifs_tcp_server_put(struct TCP_Server_Info *server)
+static void cifs_tcp_server_release(struct kref *kref)
{
- if (!refcount_dec_and_test(&server->obj_refs))
- return;
+ struct TCP_Server_Info *server =
+ container_of(kref, struct TCP_Server_Info, obj_refs);

cifs_crypto_secmech_release(server);
kfree_sensitive(server->session_key.response);
@@ -1097,6 +1097,23 @@ static void cifs_tcp_server_put(struct TCP_Server_Info *server)
kfree_sensitive(server);
}

+/*
+ * References of the struct TCP_Server_Info memory, unlike srv_count,
+ * which controls the connection and the cifsd thread. Holders are the
+ * session side (dropped by cifs_put_tcp_session()), the cifsd thread
+ * (dropped at the end of clean_demultiplex_info()) and anything else
+ * that may still use the struct without holding srv_count.
+ */
+void cifs_tcp_server_get(struct TCP_Server_Info *server)
+{
+ kref_get(&server->obj_refs);
+}
+
+void cifs_tcp_server_put(struct TCP_Server_Info *server)
+{
+ kref_put(&server->obj_refs, cifs_tcp_server_release);
+}
+
static noinline_for_stack void
clean_demultiplex_info(struct TCP_Server_Info *server)
{
@@ -1796,7 +1813,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
rc = -ENOMEM;
goto out_err;
}
- refcount_set(&tcp_ses->obj_refs, 1);
+ kref_init(&tcp_ses->obj_refs);

tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
if (!tcp_ses->hostname) {
@@ -1939,7 +1956,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
goto out_err_crypto_release;
}
/* Keep the server alive until the demultiplex thread has exited */
- refcount_inc(&tcp_ses->obj_refs);
+ cifs_tcp_server_get(tcp_ses);
/* thread created, put it on the list */
spin_lock(&cifs_tcp_ses_lock);
list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
--
2.43.0