mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stefan Metzmacher <metze@samba.org>
To: linux-cifs@vger.kernel.org, samba-technical@lists.samba.org
Cc: metze@samba.org, stable@vger.kernel.org,
	Eduard Zateev <hackerowskiy@gmail.com>,
	Paulo Alcantara <pc@manguebit.org>,
	Namjae Jeon <linkinjeon@kernel.org>,
	Ronnie Sahlberg <ronniesahlberg@gmail.com>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Tom Talpey <tom@talpey.com>, Bharath SM <bharathsm@microsoft.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 02/12] smb: client: use kref for the TCP_Server_Info object references
Date: Fri,  4 Dec 2026 14:33:27 +0100	[thread overview]
Message-ID: <48acfa2b06c4630abc76831111019e0ae132ea1e.1796391093.git.metze@samba.org> (raw)
In-Reply-To: <cover.1796391093.git.metze@samba.org>

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@vger.kernel.org
Cc: Eduard Zateev <hackerowskiy@gmail.com>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 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


  parent reply	other threads:[~2026-12-04 13:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 13:33 [PATCH 0/12] smb: client: don't sleep through reconnects and umount Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 01/12] smb: client: don't add a decrypted mid back to an exiting server Stefan Metzmacher
2026-12-04 13:33 ` Stefan Metzmacher [this message]
2026-12-04 13:33 ` [PATCH v2 03/12] smb: client: let in-flight requests hold a TCP_Server_Info reference Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 04/12] smb: client: let the decrypt offload work " Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 05/12] smb: client: let channels hold a reference of the primary server struct Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 06/12] smb: client: stop waiting between reconnect attempts once the server exits Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 07/12] smb: client: wait for pending mids instead of sleeping 46 seconds Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 08/12] smb: client: add cifs_server_wait_for_exit_ms() Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 09/12] smb: client: use cifs_server_wait_for_exit_ms() for the replay back-off Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 10/12] smb: smbdirect: fail a connect from another address family Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 11/12] smb: client: let SMB Direct connections use the srcaddr= mount option Stefan Metzmacher
2026-12-04 13:33 ` [PATCH v2 12/12] smb: client: soft mounts only wait until a reconnect attempt failed locally Stefan Metzmacher

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=48acfa2b06c4630abc76831111019e0ae132ea1e.1796391093.git.metze@samba.org \
    --to=metze@samba.org \
    --cc=bharathsm@microsoft.com \
    --cc=hackerowskiy@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pc@manguebit.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=samba-technical@lists.samba.org \
    --cc=sprasad@microsoft.com \
    --cc=stable@vger.kernel.org \
    --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®