Skip to contentSkip to conversation

sunrpc: Fix potential race conditions in rpc_sysfs_xprt_state_change()

PATCH New [email protected] 1 participant 3 messages Nov 15, 2021

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

PATCH 1/3 sunrpc: Fix potential race conditions in rpc_sysfs_xprt_state_change()

From: Anna Schumaker <[email protected]>

We need to use test_and_set_bit() when changing xprt state flags to avoid potentially getting xps->xps_nactive out of sync.

---
 net/sunrpc/sysfs.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
net/sunrpc/sysfs.c +19 -16
index 77e7d011c1ab..8f309bcdf84f 100644
@@ -309,25 +309,28 @@ static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
 		goto release_tasks;
 	}
 	if (offline) {
-		set_bit(XPRT_OFFLINE, &xprt->state);
-		spin_lock(&xps->xps_lock);
-		xps->xps_nactive--;
-		spin_unlock(&xps->xps_lock);
+		if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
+			spin_lock(&xps->xps_lock);
+			xps->xps_nactive--;
+			spin_unlock(&xps->xps_lock);
+		}
 	} else if (online) {
-		clear_bit(XPRT_OFFLINE, &xprt->state);
-		spin_lock(&xps->xps_lock);
-		xps->xps_nactive++;
-		spin_unlock(&xps->xps_lock);
+		if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
+			spin_lock(&xps->xps_lock);
+			xps->xps_nactive++;
+			spin_unlock(&xps->xps_lock);
+		}
 	} else if (remove) {
 		if (test_bit(XPRT_OFFLINE, &xprt->state)) {
-			set_bit(XPRT_REMOVE, &xprt->state);
-			xprt_force_disconnect(xprt);
-			if (test_bit(XPRT_CONNECTED, &xprt->state)) {
-				if (!xprt->sending.qlen &&
-				    !xprt->pending.qlen &&
-				    !xprt->backlog.qlen &&
-				    !atomic_long_read(&xprt->queuelen))
-					rpc_xprt_switch_remove_xprt(xps, xprt);
+			if (!test_and_set_bit(XPRT_REMOVE, &xprt->state)) {
+				xprt_force_disconnect(xprt);
+				if (test_bit(XPRT_CONNECTED, &xprt->state)) {
+					if (!xprt->sending.qlen &&
+					    !xprt->pending.qlen &&
+					    !xprt->backlog.qlen &&
+					    !atomic_long_read(&xprt->queuelen))
+						rpc_xprt_switch_remove_xprt(xps, xprt);
+				}
 			}
 		} else {
 			count = -EINVAL;
Signature · 1 line
2.33.1
#1 lore raw

PATCH 2/3 sunrpc: Provide a helper function for accessing the number of xprts

From: Anna Schumaker <[email protected]>

---
 include/linux/sunrpc/clnt.h |  1 +
 net/sunrpc/clnt.c           | 10 ++++++++++
 2 files changed, 11 insertions(+)
include/linux/sunrpc/clnt.h +1 -0
index 267b7aeaf1a6..0a94fe1036a8 100644
@@ -238,6 +238,7 @@ const char *rpc_proc_name(const struct rpc_task *task);
 
 void rpc_clnt_xprt_switch_put(struct rpc_clnt *);
 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *, struct rpc_xprt *);
+unsigned int rpc_clnt_xprt_switch_num_xprts(struct rpc_clnt *);
 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
 			const struct sockaddr *sap);
 void rpc_cleanup_clids(void);
net/sunrpc/clnt.c +10 -0
index a312ea2bc440..399768a443ea 100644
@@ -2994,6 +2994,16 @@ void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
 }
 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
 
+unsigned int rpc_clnt_xprt_switch_num_xprts(struct rpc_clnt *clnt)
+{
+	unsigned int num;
+	rcu_read_lock();
+	num = rcu_dereference(clnt->cl_xpi.xpi_xpswitch)->xps_nxprts;
+	rcu_read_unlock();
+	return num;
+}
+EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_num_xprts);
+
 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
 				   const struct sockaddr *sap)
 {
Signature · 1 line
2.33.1
#2 lore raw

PATCH 3/3 nfs: show dynamic nconnect

In reply to [email protected]

From: Anna Schumaker <[email protected]>

Through sysfs we can change the number of xprts attached to an rpc_client. This lets us change the displayed nconnect= value to reflect the actual number of connected xprts for the given mount.

---
 fs/nfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfs/super.c +1 -1
index 3aced401735c..f6ede7bf18f9 100644
@@ -478,7 +478,7 @@ static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
 		   rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
 	rcu_read_unlock();
 	if (clp->cl_nconnect > 0)
-		seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
+		seq_printf(m, ",nconnect=%u", rpc_clnt_xprt_switch_num_xprts(clp->cl_rpcclient));
 	if (version == 4) {
 		if (clp->cl_max_connect > 1)
 			seq_printf(m, ",max_connect=%u", clp->cl_max_connect);
Signature · 1 line
2.33.1