From: Samuel Cabrero <scabrero@suse.com>
To: linux-cifs@vger.kernel.org
Cc: smfrench@gmail.com, pc@manguebit.com, ronniesahlberg@gmail.com,
sprasad@microsoft.com, tom@talpey.com, bharathsm@microsoft.com,
henrique.carvalho@suse.com, ematsumiya@suse.de,
Samuel Cabrero <scabrero@suse.com>
Subject: [PATCH 16/18] smb:client: Simplify registration creation
Date: Fri, 10 Jul 2026 11:16:35 +0200 [thread overview]
Message-ID: <20260710091637.58873-17-scabrero@suse.com> (raw)
In-Reply-To: <20260710091637.58873-1-scabrero@suse.com>
And switch to GFP_KERNEL since the code path can sleep.
Signed-off-by: Samuel Cabrero <scabrero@suse.com>
---
fs/smb/client/cifs_swn.c | 209 ++++++++++++++++++++-------------------
1 file changed, 106 insertions(+), 103 deletions(-)
diff --git a/fs/smb/client/cifs_swn.c b/fs/smb/client/cifs_swn.c
index 97e415e441e4..3375a65ec32e 100644
--- a/fs/smb/client/cifs_swn.c
+++ b/fs/smb/client/cifs_swn.c
@@ -323,7 +323,8 @@ cifs_swn_reg_get_tcon(const struct cifs_swn_reg *swnreg)
/*
* Sends a register message to the userspace daemon based on the registration.
* The authentication information to connect to the witness service is bundled
- * into the message.
+ * into the message. This function can sleep while allocating the genlmsg so
+ * it must be called after taking a swnreg reference and release the lock.
*/
static int cifs_swn_send_register_message(struct cifs_swn_reg *swnreg)
{
@@ -414,7 +415,9 @@ static int cifs_swn_send_register_message(struct cifs_swn_reg *swnreg)
}
/*
- * Sends an uregister message to the userspace daemon based on the registration
+ * Sends an unregister message to the userspace daemon based on the registration.
+ * This function can sleep while allocating the genlmsg so it must be called after
+ * taking a swnreg reference and release the lock.
*/
static int cifs_swn_send_unregister_message(struct cifs_swn_reg *swnreg)
{
@@ -481,6 +484,11 @@ static int cifs_swn_send_unregister_message(struct cifs_swn_reg *swnreg)
return ret;
}
+/*
+ * Release a registration. Must be called with the last reference dropped (the
+ * refcount has reached zero) and with the registration already removed from the
+ * IDR under cifs_swnreg_idr_mutex, so it is no longer discoverable.
+ */
static void cifs_swn_reg_release(struct cifs_swn_reg *swnreg)
{
int ret;
@@ -614,99 +622,6 @@ static struct cifs_swn_reg *cifs_find_swn_reg(struct cifs_tcon *tcon)
return ERR_PTR(-ENOENT);
}
-/*
- * Get a registration for the tcon's server and share name, allocating a new one if it does not
- * exists
- */
-static struct cifs_swn_reg *cifs_get_swn_reg(struct cifs_tcon *tcon)
-{
- struct cifs_swn_reg *swnreg = NULL;
- int ret;
-
- mutex_lock(&cifs_swnreg_idr_mutex);
-
- /* Check if we are already registered for this network and share names */
- swnreg = cifs_find_swn_reg(tcon);
- if (!IS_ERR(swnreg)) {
- kref_get(&swnreg->ref_count);
- goto unlock;
- } else if (PTR_ERR(swnreg) != -ENOENT) {
- goto unlock;
- }
-
- swnreg = kmalloc_obj(struct cifs_swn_reg, GFP_ATOMIC);
- if (swnreg == NULL) {
- ret = -ENOMEM;
- goto fail_unlock;
- }
-
- kref_init(&swnreg->ref_count);
-
- swnreg->id = idr_alloc(&cifs_swnreg_idr, swnreg, 1, 0, GFP_ATOMIC);
- if (swnreg->id < 0) {
- cifs_dbg(FYI, "%s: failed to allocate registration id\n", __func__);
- ret = swnreg->id;
- goto fail;
- }
-
- swnreg->net_name = extract_hostname(tcon->tree_name);
- if (IS_ERR(swnreg->net_name)) {
- ret = PTR_ERR(swnreg->net_name);
- cifs_dbg(VFS, "%s: failed to extract host name from target: %d\n", __func__, ret);
- goto fail_idr;
- }
-
- swnreg->share_name = extract_sharename(tcon->tree_name);
- if (IS_ERR(swnreg->share_name)) {
- ret = PTR_ERR(swnreg->share_name);
- cifs_dbg(VFS, "%s: failed to extract share name from target: %d\n", __func__, ret);
- goto fail_net_name;
- }
-
- ret = cifs_swn_reg_set_auth(swnreg, tcon);
- if (ret != 0) {
- cifs_dbg(VFS, "%s: failed to set auth info: %d\n", __func__,
- ret);
- goto fail_share_name;
- }
-
- /*
- * If there is an address stored use it instead of the server address, because we are
- * in the process of reconnecting to it after a share has been moved or we have been
- * told to switch to it (client move message). In these cases we unregister from the
- * server address and register to the new address when we receive the notification.
- */
- if (tcon->ses->server->use_swn_dstaddr)
- swnreg->addr = tcon->ses->server->swn_dstaddr;
- else
- swnreg->addr = tcon->ses->server->dstaddr;
-
- swnreg->net_name_notify = true;
- swnreg->share_name_notify =
- (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC);
- swnreg->ip_notify = false;
-
- swnreg->check_interval = tcon->ses->server->echo_interval;
- INIT_DELAYED_WORK(&swnreg->check, cifs_swn_reg_check);
-
- queue_delayed_work(cifsiod_wq, &swnreg->check, swnreg->check_interval);
-unlock:
- mutex_unlock(&cifs_swnreg_idr_mutex);
-
- return swnreg;
-fail_share_name:
- kfree(swnreg->share_name);
-fail_net_name:
- kfree(swnreg->net_name);
-fail_idr:
- idr_remove(&cifs_swnreg_idr, swnreg->id);
-fail:
- kfree(swnreg);
-fail_unlock:
- mutex_unlock(&cifs_swnreg_idr_mutex);
- return ERR_PTR(ret);
-}
-
static void cifs_swn_resource_state_changed(struct cifs_tcon *tcon,
const char *name, int state)
{
@@ -816,8 +731,8 @@ cifs_swn_handle_notification_tcon(const struct cifs_swn_notification *not,
/*
* This function process a notification received for a registration. It
- * searches all matching tcons to deliver it and then unregisters/registers
- * as necessary if the address has changed.
+ * unregisters/registers as necessary and applies the notification to all
+ * matching tcons.
*/
static int cifs_swn_handle_notification(const struct cifs_swn_notification *not)
{
@@ -986,16 +901,104 @@ int cifs_swn_register(struct cifs_tcon *tcon)
struct cifs_swn_reg *swnreg;
int ret;
- swnreg = cifs_get_swn_reg(tcon);
- if (IS_ERR(swnreg))
+ mutex_lock(&cifs_swnreg_idr_mutex);
+
+ swnreg = cifs_find_swn_reg(tcon);
+ if (!IS_ERR(swnreg)) {
+ /*
+ * There is a registration matching this tcon, could be a second mount of
+ * the same share, increment the refcount.
+ */
+ if (kref_get_unless_zero(&swnreg->ref_count)) {
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+ return 0;
+ }
+ /* Else it is being released, allocate new one */
+ } else if (PTR_ERR(swnreg) != -ENOENT) {
+ mutex_unlock(&cifs_swnreg_idr_mutex);
return PTR_ERR(swnreg);
+ }
- ret = cifs_swn_send_register_message(swnreg);
- if (ret < 0) {
- cifs_dbg(VFS, "%s: Failed to send swn register message: %d\n", __func__, ret);
- /* Do not put the swnreg or return error, the check task will retry */
+ /* Allocate new registration */
+ swnreg = kzalloc_obj(struct cifs_swn_reg, GFP_KERNEL);
+ if (swnreg == NULL) {
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+ return -ENOMEM;
}
+ kref_init(&swnreg->ref_count);
+
+ swnreg->id = idr_alloc(&cifs_swnreg_idr, swnreg, 1, 0, GFP_KERNEL);
+ if (swnreg->id < 0) {
+ ret = swnreg->id;
+ cifs_dbg(FYI, "%s: failed to allocate registration id\n",
+ __func__);
+ kfree(swnreg);
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+ return ret;
+ }
+
+ swnreg->net_name = extract_hostname(tcon->tree_name);
+ if (IS_ERR(swnreg->net_name)) {
+ ret = PTR_ERR(swnreg->net_name);
+ cifs_dbg(VFS,
+ "%s: failed to extract host name from target: %d\n",
+ __func__, ret);
+ idr_remove(&cifs_swnreg_idr, swnreg->id);
+ kfree(swnreg);
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+ return ret;
+ }
+
+ swnreg->share_name = extract_sharename(tcon->tree_name);
+ if (IS_ERR(swnreg->share_name)) {
+ ret = PTR_ERR(swnreg->share_name);
+ cifs_dbg(VFS,
+ "%s: failed to extract share name from target: %d\n",
+ __func__, ret);
+ kfree(swnreg->net_name);
+ idr_remove(&cifs_swnreg_idr, swnreg->id);
+ kfree(swnreg);
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+ return ret;
+ }
+
+ ret = cifs_swn_reg_set_auth(swnreg, tcon);
+ if (ret != 0) {
+ cifs_dbg(VFS, "%s: failed to set auth info: %d\n", __func__,
+ ret);
+ kfree(swnreg->net_name);
+ kfree(swnreg->share_name);
+ idr_remove(&cifs_swnreg_idr, swnreg->id);
+ kfree(swnreg);
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+ return ret;
+ }
+
+ /*
+ * If there is an address stored use it instead of the server address, because we are
+ * in the process of reconnecting to it after a share has been moved or we have been
+ * told to switch to it (client move message). In these cases we unregister from the
+ * server address and register to the new address when we receive the notification.
+ */
+ if (tcon->ses->server->use_swn_dstaddr)
+ swnreg->addr = tcon->ses->server->swn_dstaddr;
+ else
+ swnreg->addr = tcon->ses->server->dstaddr;
+
+ swnreg->net_name_notify = true;
+ swnreg->share_name_notify =
+ (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC);
+ swnreg->ip_notify = false;
+
+ swnreg->check_interval = tcon->ses->server->echo_interval;
+ INIT_DELAYED_WORK(&swnreg->check, cifs_swn_reg_check);
+
+ /* Queue a immediate run to send the netlink message */
+ queue_delayed_work(cifsiod_wq, &swnreg->check, 0);
+
+ mutex_unlock(&cifs_swnreg_idr_mutex);
+
return 0;
}
--
2.54.0
next prev parent reply other threads:[~2026-07-10 9:18 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-10 9:16 [PATCH 00/18] SWN: Decouple witness registrations and tcons Samuel Cabrero
2026-07-10 9:16 ` [PATCH 01/18] Revert "smb: client: resolve SWN tcon from live registrations" Samuel Cabrero
2026-07-10 9:16 ` [PATCH 02/18] smb:client: Adjust witness notification request Samuel Cabrero
2026-07-10 9:16 ` [PATCH 03/18] smb:client: Rename variable to match the rest of the code Samuel Cabrero
2026-07-10 9:16 ` [PATCH 04/18] smb:client: Split idr remove and release functions Samuel Cabrero
2026-07-10 9:16 ` [PATCH 05/18] smb:client: Add a specific task to refresh witness registrations Samuel Cabrero
2026-07-10 9:16 ` [PATCH 06/18] smb:client: Store the tcon dstaddr in the witness registration Samuel Cabrero
2026-07-10 9:16 ` [PATCH 07/18] smb:client: Fix printf format string Samuel Cabrero
2026-07-10 9:16 ` [PATCH 08/18] smb:client: Save memory allocations Samuel Cabrero
2026-07-10 9:16 ` [PATCH 09/18] smb:client: Use the correct return code Samuel Cabrero
2026-07-10 9:16 ` [PATCH 10/18] smb:client: Improve tcon matching logic Samuel Cabrero
2026-07-10 9:16 ` [PATCH 11/18] smb:client: Pass the tcon to cifs_swn_send_register_message Samuel Cabrero
2026-07-10 9:16 ` [PATCH 12/18] smb:client: Pass the tcon to notification handlers Samuel Cabrero
2026-07-10 9:16 ` [PATCH 13/18] smb:client: Introduce swn_notification struct Samuel Cabrero
2026-07-10 9:16 ` [PATCH 14/18] smb:client: Cache auth info Samuel Cabrero
2026-07-10 9:16 ` [PATCH 15/18] smb:client: Do not store the tcon reference, find a matching one Samuel Cabrero
2026-07-10 9:16 ` Samuel Cabrero [this message]
2026-07-10 9:16 ` [PATCH 17/18] smb:client: Improve registration network and share names handling Samuel Cabrero
2026-07-10 9:16 ` [PATCH 18/18] smb:client: Avoid witness re-register if the address did not change Samuel Cabrero
2026-07-10 14:16 ` [PATCH 00/18] SWN: Decouple witness registrations and tcons Enzo Matsumiya
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=20260710091637.58873-17-scabrero@suse.com \
--to=scabrero@suse.com \
--cc=bharathsm@microsoft.com \
--cc=ematsumiya@suse.de \
--cc=henrique.carvalho@suse.com \
--cc=linux-cifs@vger.kernel.org \
--cc=pc@manguebit.com \
--cc=ronniesahlberg@gmail.com \
--cc=smfrench@gmail.com \
--cc=sprasad@microsoft.com \
--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