Linux block layer
 help / color / mirror / Atom feed
From: Jackie Liu <liu.yun@linux.dev>
To: josef@toxicpanda.com
Cc: axboe@kernel.dk, shuah@kernel.org, tom.leiming@gmail.com,
	yukuai@fygo.io, linux-block@vger.kernel.org,
	nbd@other.debian.org, linux-kselftest@vger.kernel.org
Subject: [PATCH 0/4] nbd: revalidate startup state after queue updates
Date: Mon, 19 Oct 2026 16:42:23 +0800	[thread overview]
Message-ID: <cover.1792399225.git.liuyun01@kylinos.cn> (raw)

From: Jackie Liu <liuyun01@kylinos.cn>

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

             reply	other threads:[~2026-10-19  8:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19  8:42 Jackie Liu [this message]
2026-10-19  8:42 ` [PATCH 1/4] nbd: reject concurrent device starts Jackie Liu
2026-10-19  8:42 ` [PATCH 2/4] selftests: nbd: test " Jackie Liu
2026-10-19  8:42 ` [PATCH 3/4] nbd: recheck multi-connection support on startup Jackie Liu
2026-10-19  8:42 ` [PATCH 4/4] selftests: nbd: test multi-connection startup races Jackie Liu
2026-10-20 13:03 ` [PATCH 0/4] nbd: revalidate startup state after queue updates Tao Cui

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=cover.1792399225.git.liuyun01@kylinos.cn \
    --to=liu.yun@linux.dev \
    --cc=axboe@kernel.dk \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=nbd@other.debian.org \
    --cc=shuah@kernel.org \
    --cc=tom.leiming@gmail.com \
    --cc=yukuai@fygo.io \
    /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