nbd: revalidate startup state after queue updates
Subsystems (3)
cover nbd: revalidate startup state after queue updates
From: Jackie Liu <[email protected]>
Hi,
Commit 8b428f42f3ed ("nbd: fix lockdep deadlock warning") drops config_lock around blk_mq_update_nr_hw_queues(). The initial checks for a running device and NBD_FLAG_CAN_MULTI_CONN can become stale while the lock is dropped.
Two NBD_DO_IT callers can both pass the PID check. After the first caller starts the device, the loser overwrites nbd->pid before failing to create the existing sysfs pid attribute. Concurrent NBD_SET_SOCK or NBD_SET_FLAGS can also leave multiple connections without NBD_FLAG_CAN_MULTI_CONN, which the queue-count retry does not validate.
Recheck both conditions after reacquiring config_lock. Reject an already started device with EBUSY, and reject unsupported multiple connections with EINVAL before retrying the queue update or starting receive workers.
Each fix is followed by its regression test. The tests prime the hardware queue count before each attempt and use socket pairs, so no external NBD server is needed. They require CAP_SYS_ADMIN, at least two online CPUs and unused NBD devices. To build and run them:
modprobe nbd nbds_max=4 max_part=0
make -C tools/testing/selftests TARGETS=nbd make -C tools/testing/selftests TARGETS=nbd run_tests
For longer runs with a reproducible random seed:
tools/testing/selftests/nbd/nbd_pid_race --rounds 100 --seed 123 tools/testing/selftests/nbd/nbd_multi_conn --rounds 100 --seed 123
These tests use scheduling stress rather than kernel instrumentation. A passing run means that no regression was observed during that run; it does not guarantee that every interleaving was exercised. The flags test excludes changes observed after PID publication.
Testing on the v7.3 base below:
- ARM64 kernel Image and modules built with GCC.
- Both selftests built with GCC and Clang; kselftest installation passed.
- In QEMU TCG with two CPUs, all three cases passed 100 rounds with seed 123, with no reported regressions or test errors.
- Both tests passed through the standard kselftest runner.
- Removing both fixes made all three cases detect their regression. With only the PID fix, the PID case passed and both multi-connection cases still detected their regression.
- The fixed-kernel run had no WARN/BUG/Oops reports and remained untainted. Missing-device skips emitted one TAP plan per test.
- Fault injection checked PID-read error propagation, EINTR retry and attribute disappearance handling in the test helper.
- In-tree checkpatch --strict reported no errors or checks. The generic new-file MAINTAINERS warning on patch 4 was reviewed: patch 2 already registers the entire NBD selftest directory.
Both driver fixes carry the upstream Fixes tag and a stable Cc for v6.17 and later. The selftest patches do not request stable backports.
Thanks,
Jackie
Jackie Liu (4):
nbd: reject concurrent device starts
selftests: nbd: test concurrent device starts
nbd: recheck multi-connection support on startup
selftests: nbd: test multi-connection startup races
MAINTAINERS | 1 + drivers/block/nbd.c | 7 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/nbd/.gitignore | 2 + tools/testing/selftests/nbd/Makefile | 8 + tools/testing/selftests/nbd/config | 7 + tools/testing/selftests/nbd/nbd_common.c | 465 +++++++++++++++++++ tools/testing/selftests/nbd/nbd_common.h | 53 +++ tools/testing/selftests/nbd/nbd_multi_conn.c | 147 ++++++ tools/testing/selftests/nbd/nbd_pid_race.c | 86 ++++ 10 files changed, 777 insertions(+) create mode 100644 tools/testing/selftests/nbd/.gitignore create mode 100644 tools/testing/selftests/nbd/Makefile create mode 100644 tools/testing/selftests/nbd/config create mode 100644 tools/testing/selftests/nbd/nbd_common.c create mode 100644 tools/testing/selftests/nbd/nbd_common.h create mode 100644 tools/testing/selftests/nbd/nbd_multi_conn.c create mode 100644 tools/testing/selftests/nbd/nbd_pid_race.c
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
PATCH 1/4 nbd: reject concurrent device starts
From: Jackie Liu <[email protected]>
nbd_start_device() drops config_lock while updating the hardware queue count. Two NBD_DO_IT callers can pass the initial nbd->pid check before either caller publishes its PID.
After the first caller starts the device, the second caller overwrites nbd->pid and fails to create the existing sysfs pid attribute with -EEXIST. The running device then reports the losing caller's PID.
Recheck nbd->pid after reacquiring config_lock and return -EBUSY if another caller has already started the device. Do this before retrying the queue update or changing any startup state.
--- drivers/block/nbd.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/block/nbd.c +4 -0
@@ -1530,6 +1530,10 @@ static int nbd_start_device(struct nbd_device *nbd)blk_mq_update_nr_hw_queues (&nbd->tag_set, num_connections);mutex_lock (&nbd->config_lock); +/* The device may have been started or reconfigured while unlocked. */ +if (nbd->pid) +return -EBUSY ; +/* if another code path updated nr_hw_queues, retry until succeed */ if (num_connections != config->num_connections) { num_connections = config->num_connections;
PATCH 2/4 selftests: nbd: test concurrent device starts
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Add an NBD selftest that races two NBD_DO_IT callers and checks that the loser returns EBUSY while the sysfs pid attribute retains the successful caller's PID. Prime the device with a different connection count before each attempt so that the hardware queue update cannot take its no-op path.
Use socket pairs and an exclusively opened unused device, with bounded waits and cleanup of the test's own configuration. Skip the test when CAP_SYS_ADMIN, at least two CPUs, or an unused NBD device is unavailable. Register the new nbd target with kselftest.
The test uses scheduling stress rather than kernel instrumentation, so a passing run only means that no regression was observed during that run. The number of attempts, delays and random seed can be set on the command line. Load nbd with nbds_max=4 max_part=0 before running the test.
--- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/nbd/.gitignore | 1 + tools/testing/selftests/nbd/Makefile | 8 + tools/testing/selftests/nbd/config | 7 + tools/testing/selftests/nbd/nbd_common.c | 465 +++++++++++++++++++++ tools/testing/selftests/nbd/nbd_common.h | 53 +++ tools/testing/selftests/nbd/nbd_pid_race.c | 86 ++++ 8 files changed, 622 insertions(+) create mode 100644 tools/testing/selftests/nbd/.gitignore create mode 100644 tools/testing/selftests/nbd/Makefile create mode 100644 tools/testing/selftests/nbd/config create mode 100644 tools/testing/selftests/nbd/nbd_common.c create mode 100644 tools/testing/selftests/nbd/nbd_common.h create mode 100644 tools/testing/selftests/nbd/nbd_pid_race.c
MAINTAINERS +1 -0
@@ -18884,6 +18884,7 @@ F: Documentation/admin-guide/blockdev/nbd.rst F: drivers/block/nbd.c F: include/trace/events/nbd.h F: include/uapi/linux/nbd.h +F: tools/testing/selftests/nbd/ NETWORK DROP MONITOR M: Neil Horman <[email protected]>
tools/testing/selftests/Makefile +1 -0
@@ -82,6 +82,7 @@ TARGETS += mount_setattr TARGETS += move_mount_set_group TARGETS += mqueue TARGETS += mseal_system_mappings +TARGETS += nbd TARGETS += nci TARGETS += net TARGETS += net/af_unix
tools/testing/selftests/nbd/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/nbd_pid_race
tools/testing/selftests/nbd/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +CFLAGS += -O2 -Wall -Wextra $(KHDR_INCLUDES) +TEST_GEN_PROGS := nbd_pid_race +LOCAL_HDRS += nbd_common.h + +include ../lib.mk + +$(TEST_GEN_PROGS): nbd_common.c
tools/testing/selftests/nbd/config new file +7 -0
@@ -0,0 +1,7 @@ +CONFIG_MODULES=y +CONFIG_BLOCK=y +CONFIG_BLK_DEV_NBD=m +CONFIG_NET=y +CONFIG_UNIX=y +CONFIG_SMP=y +CONFIG_SYSFS=y
tools/testing/selftests/nbd/nbd_common.c new file +465 -0
465 lines not on this page · raw message
tools/testing/selftests/nbd/nbd_common.h new file +53 -0
53 lines not on this page · raw message
tools/testing/selftests/nbd/nbd_pid_race.c new file +86 -0
86 lines not on this page · raw message
PATCH 3/4 nbd: recheck multi-connection support on startup
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
nbd_start_device() checks NBD_FLAG_CAN_MULTI_CONN before dropping config_lock to update the hardware queue count. A concurrent NBD_SET_SOCK can add a connection during that window, and NBD_SET_FLAGS can clear the flag without changing the connection count.
The queue-count retry only notices changes to num_connections and does not repeat the flag check. Startup can therefore create multiple receive workers even though the server no longer advertises multi-connection support.
Recheck the current connection count and flags after reacquiring config_lock, before retrying the queue update or starting the device. Return -EINVAL if multiple connections are no longer supported.
--- drivers/block/nbd.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/block/nbd.c +3 -0
@@ -1533,6 +1533,9 @@ static int nbd_start_device(struct nbd_device *nbd)/* The device may have been started or reconfigured while unlocked. */ if (nbd->pid)return -EBUSY ; +if (config->num_connections >1 && + !(config->flags &NBD_FLAG_CAN_MULTI_CONN )) +return -EINVAL ;/* if another code path updated nr_hw_queues, retry until succeed */ if (num_connections != config->num_connections) {
PATCH 4/4 selftests: nbd: test multi-connection startup races
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Extend the NBD startup selftests to race NBD_DO_IT against adding a second socket without NBD_FLAG_CAN_MULTI_CONN, and against clearing the flag on a device that already has two sockets. Prime the queue count before each attempt to exercise the unlocked hardware queue update.
Check for EINVAL or a valid single-connection start. Detect an invalid multi-connection start by checking whether a receive worker consumes a byte from the second socket. Keep the reply incomplete so that the check does not inject a malformed reply into request completion.
Exclude flag changes observed after PID publication, since changing flags on an already running device is outside the startup race being tested. As with the concurrent-start test, this is scheduling stress and a passing run does not prove that every race window was exercised.
--- tools/testing/selftests/nbd/.gitignore | 1 + tools/testing/selftests/nbd/Makefile | 2 +- tools/testing/selftests/nbd/nbd_multi_conn.c | 147 +++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/nbd/nbd_multi_conn.c
tools/testing/selftests/nbd/.gitignore +1 -0
@@ -1 +1,2 @@ /nbd_pid_race +/nbd_multi_conn
tools/testing/selftests/nbd/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only CFLAGS += -O2 -Wall -Wextra $(KHDR_INCLUDES) -TEST_GEN_PROGS := nbd_pid_race +TEST_GEN_PROGS := nbd_pid_race nbd_multi_conn LOCAL_HDRS += nbd_common.h include ../lib.mk
tools/testing/selftests/nbd/nbd_multi_conn.c new file +147 -0
147 lines not on this page · raw message
Hi Jackie,
Quoted 11 lines · Jackie Liu
Commit
8b428f42f3ed("nbd: fix lockdep deadlock warning") dropsconfig_lockaroundblk_mq_update_nr_hw_queues(). The initial checks for a running device andNBD_FLAG_CAN_MULTI_CONNcan become stale while the lock is dropped.Two
NBD_DO_ITcallers can both pass the PID check. After the first caller starts the device, the loser overwritesnbd->pidbefore failing to create the existing sysfs pid attribute. ConcurrentNBD_SET_SOCKorNBD_SET_FLAGScan also leave multiple connections withoutNBD_FLAG_CAN_MULTI_CONN, which the queue-count retry does not validate.
Quoted 8 lines
Recheck both conditions after reacquiring
config_lock. Reject an already started device with EBUSY, and reject unsupported multiple connections with EINVAL before retrying the queue update or starting receive workers.Each fix is followed by its regression test. The tests prime the hardware queue count before each attempt and use socket pairs, so no external NBD server is needed. They require
CAP_SYS_ADMIN, at least two online CPUs and unused NBD devices.
Tested this series on top of next-20261019 (x86_64 KVM guest, 8 vCPUs), building and running the new selftests in-guest with nbd loaded as a module (nbds_max=4 max_part=0).
On the unpatched kernel both bugs reproduce at round 1: the losing NBD_DO_IT caller exits with errno=17 (EEXIST) and /sys/block/nbdX/pid reports the loser's pid, and nbd_multi_conn flags both the socket-added and flags-changed variants ("invalid second receiver").
With the series applied, nbd_pid_race and nbd_multi_conn pass 100 rounds per case with seeds 123 and 456 - every round rejected with EBUSY/EINVAL, no regressions, dmesg clean.
Thanks,
Tao
2026年10月20日 21:03, "Tao Cui" <[email protected] mailto:[email protected]?to=%22Tao%20Cui%22%20%3Ccui.tao%40linux.dev%3E > 写到:
Quoted 35 lines
Hi Jackie,
Quoted 12 lines · Jackie Liu
Commit
8b428f42f3ed("nbd: fix lockdep deadlock warning") drops
config_lockaroundblk_mq_update_nr_hw_queues(). The initial checks for a running device andNBD_FLAG_CAN_MULTI_CONNcan become stale while the lock is dropped.Two
NBD_DO_ITcallers can both pass the PID check. After the first caller starts the device, the loser overwritesnbd->pidbefore failing to create the existing sysfs pid attribute. ConcurrentNBD_SET_SOCKorNBD_SET_FLAGScan also leave multiple connections withoutNBD_FLAG_CAN_MULTI_CONN, which the queue-count retry does not validate.Quoted 9 lines
Recheck both conditions after reacquiring
config_lock. Reject an already started device with EBUSY, and reject unsupported multiple connections with EINVAL before retrying the queue update or starting receive workers.Each fix is followed by its regression test. The tests prime the hardware queue count before each attempt and use socket pairs, so no external NBD server is needed. They require
CAP_SYS_ADMIN, at least two online CPUs and unused NBD devices.Tested this series on top of next-20261019 (
x86_64KVM guest, 8 vCPUs), building and running the new selftests in-guest with nbd loaded as a module (nbds_max=4 max_part=0).On the unpatched kernel both bugs reproduce at round 1: the losing
NBD_DO_ITcaller exits with errno=17 (EEXIST) and/sys/block/nbdX/pidreports the loser's pid, andnbd_multi_connflags both the socket-added and flags-changed variants ("invalid second receiver").
Quoted 5 lines
With the series applied,
nbd_pid_raceandnbd_multi_connpass 100 rounds per case with seeds 123 and 456 - every round rejected with EBUSY/EINVAL, no regressions, dmesg clean.Tested-by Tao Cui <[email protected]>
Thanks for test.
Signature · 1 line
Jackie Liu
Quoted 4 lines
Thanks,
Tao