From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=144.76.82.148 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796391237; b=Ro65txTohwV6k6GB3878SqsOcotu42giG1wPCXBuC3DrBPmjrbZI84rNzzqaBq/j3u0b GlKGFpUORALB9t11hgU2kmaTbcLcv2vCx04BnxWZAmziTRQ1R4WBJHzfbcmNkEFgE3DQW IfBr7K10kxRQoCWejwOFztcRdhPj0ekcB+cIPH/2BP/PdE7jtx6WhooXe32cCVOscKTC4 B2JdYTv680XnU1B3hnQXooHPjx5Ey+altVcnEF8nghQxPbP0R4wMWEuFO2a7hC2uMEa2z x1P7+vEpXLCPpPYLs9z/vezJ4n4Zcu0eixvZQ2/Mdv55GD96lcMt1w9beRWPoL6WV9A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796391237; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=646asQQOkO5epO9cDYaLFt7l70JqH6ldFAi8I3/CvTY=; b=e7EdZ9viFdI+6lMs+iLbdwvwDxLXmDn84/NLD0Mvk7IfXfdUkh0UartSo86QCZ0KNidx UUESNZFEg89S1irGhBUm2tlVeUhgOrw5qKktZkrgXJtQFcSvblSU7Blzcbn+IMJnQIwsd EXBblh6Tlm9xWWMNTZknl0gov2Jwgs6P8vTpZnSWgzCzeUtrmtNbSVsEzuKGpbN3ju19c BSwnpjN+yy6027wfr6i+vcXjT0HQ4aN592LtnjVxoaTU8+2Y7bhZfo8+9TpIwXvIEtXns Bg6olw/JO6M84rrCpr2SEi8CwH7dQP0t7CSJwwJL182JPRkOWPVDosr1SWrhpiZZ0jA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=samba.org; spf=pass smtp.mailfrom=samba.org; dkim=pass header.d=samba.org header.i=@samba.org header.a=rsa-sha256 header.s=42 header.b=HEKChkZv; arc=none smtp.remote-ip=144.76.82.148 Received: from hr2.samba.org (hr2.samba.org [144.76.82.148]) (using TLSv1.3 with cipher TLS_CHACHA20_POLY1305_SHA256 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD74E606A333; Fri, 4 Dec 2026 13:33:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CD74E606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=samba.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=samba.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (3072-bit key) header.d=samba.org header.i=@samba.org header.a=rsa-sha256 header.s=42 header.b=HEKChkZv DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=samba.org; s=42; h=Message-ID:Date:Cc:To:From; bh=646asQQOkO5epO9cDYaLFt7l70JqH6ldFAi8I3/CvTY=; b=HEKChkZv2dWeOLXecI4347PQkW Ki1GbmKjTpVZiFySYkxkLE6ik+92l9u6YT0PDtg91iejFEU+AH/Y8Z/Irb4aegeUvSHKWwldzR+5F zkErstVhfYgrNKT5COqVhlf9f+xaUu+aEDamDA18KdHXv6FWfHjFvdRnGdHi/7pLbzGFi33CTZ/8Y Io7QazaANMt84UV4JFthMf1jeJUohDvWuLZMR810+phlvs9u4XK04wTXFJnIm+5tdh/f2P2Ts3xFW QV+VpWVJzu07bt/Q7YoLQ7Osxb2qxYq4MVCER2aN5C8TrzczzDJOkX/8Fq8wVpiOZAkdvQYMYnfFx +AknDGKtL/KGL+k4m0MzJLLTQ9B4g4kQ2ascoPTXEjwR3+RAMH7PYp3PvdSXxSHcKBb803ThenQpm /jXnEAffxDgGkfho1PlBhZhbzW34RFubVHg4LKuG6LpbemHcNJbNLV5Mp21RAs6bpcOcae+sJmpmJ 8Gpch1lIWmhRb6jy5kK3J7HO; Received: from [127.0.0.2] (localhost [127.0.0.1]) by hr2.samba.org with esmtpsa (TLS1.3:ECDHE_SECP256R1__ECDSA_SECP256R1_SHA256__CHACHA20_POLY1305:256) (Exim) id 1xZTQ9-00000000I8a-0JAf; Fri, 04 Dec 2026 13:33:53 +0000 From: Stefan Metzmacher To: linux-cifs@vger.kernel.org, samba-technical@lists.samba.org Cc: metze@samba.org, stable@vger.kernel.org, Eduard Zateev , Paulo Alcantara , Namjae Jeon , Ronnie Sahlberg , Shyam Prasad N , Tom Talpey , Bharath SM , 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 Message-ID: <48acfa2b06c4630abc76831111019e0ae132ea1e.1796391093.git.metze@samba.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- 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 #include #include +#include #include #include #include @@ -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