Skip to content

Commit 1a48db3

Browse files
committed
sunrpc: Fix potential race conditions in rpc_sysfs_xprt_state_change()
We need to use test_and_set_bit() when changing xprt state flags to avoid potentially getting xps->xps_nactive out of sync. Signed-off-by: Anna Schumaker <[email protected]>
1 parent 776d794 commit 1a48db3

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

net/sunrpc/sysfs.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,28 @@ static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
309309
goto release_tasks;
310310
}
311311
if (offline) {
312-
set_bit(XPRT_OFFLINE, &xprt->state);
313-
spin_lock(&xps->xps_lock);
314-
xps->xps_nactive--;
315-
spin_unlock(&xps->xps_lock);
312+
if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
313+
spin_lock(&xps->xps_lock);
314+
xps->xps_nactive--;
315+
spin_unlock(&xps->xps_lock);
316+
}
316317
} else if (online) {
317-
clear_bit(XPRT_OFFLINE, &xprt->state);
318-
spin_lock(&xps->xps_lock);
319-
xps->xps_nactive++;
320-
spin_unlock(&xps->xps_lock);
318+
if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
319+
spin_lock(&xps->xps_lock);
320+
xps->xps_nactive++;
321+
spin_unlock(&xps->xps_lock);
322+
}
321323
} else if (remove) {
322324
if (test_bit(XPRT_OFFLINE, &xprt->state)) {
323-
set_bit(XPRT_REMOVE, &xprt->state);
324-
xprt_force_disconnect(xprt);
325-
if (test_bit(XPRT_CONNECTED, &xprt->state)) {
326-
if (!xprt->sending.qlen &&
327-
!xprt->pending.qlen &&
328-
!xprt->backlog.qlen &&
329-
!atomic_long_read(&xprt->queuelen))
330-
rpc_xprt_switch_remove_xprt(xps, xprt);
325+
if (!test_and_set_bit(XPRT_REMOVE, &xprt->state)) {
326+
xprt_force_disconnect(xprt);
327+
if (test_bit(XPRT_CONNECTED, &xprt->state)) {
328+
if (!xprt->sending.qlen &&
329+
!xprt->pending.qlen &&
330+
!xprt->backlog.qlen &&
331+
!atomic_long_read(&xprt->queuelen))
332+
rpc_xprt_switch_remove_xprt(xps, xprt);
333+
}
331334
}
332335
} else {
333336
count = -EINVAL;

0 commit comments

Comments
 (0)