* [PATCH V2] selftests: ublk: cap nthreads to kernel's actual nr_hw_queues
@ 2026-05-13 10:19 Ming Lei
2026-05-13 13:56 ` Jens Axboe
0 siblings, 1 reply; 2+ messages in thread
From: Ming Lei @ 2026-05-13 10:19 UTC (permalink / raw)
To: Jens Axboe, linux-block; +Cc: Caleb Sander Mateos, Uday Shankar, Ming Lei
dev->nthreads is derived from the user-requested queue count before the
ADD command, but the kernel may reduce nr_hw_queues (capped to
nr_cpu_ids). When the VM has fewer CPUs than requested queues, the
daemon creates more handler threads than there are kernel queues.
In non-batch mode, the extra threads access uninitialized queues
(q_depth=0), submit zero io_uring SQEs, and block forever in
io_cqring_wait. In batch mode, the extra threads cause similar hangs
during device removal.
In both cases, the stuck threads prevent the daemon from closing the
char device, holding the last ublk_device reference and causing
ublk_ctrl_del_dev() to hang in wait_event_interruptible().
Fix by capping dev->nthreads to the kernel-returned nr_hw_queues after
the ADD command completes. per_io_tasks mode is excluded because threads
interleave across all queues, so nthreads > nr_hw_queues is valid.
Fixes: abe54c160346 ("selftests: ublk: kublk: decouple ublk_queues from ublk server threads")
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
V2:
- fix email address & signed-off-by
tools/testing/selftests/ublk/kublk.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests/ublk/kublk.c
index fbd9b1e7342a..0b23c09daea5 100644
--- a/tools/testing/selftests/ublk/kublk.c
+++ b/tools/testing/selftests/ublk/kublk.c
@@ -1735,6 +1735,17 @@ static int __cmd_dev_add(const struct dev_ctx *ctx)
goto fail;
}
+ /*
+ * The kernel may reduce nr_hw_queues (e.g. capped to nr_cpu_ids).
+ * Cap nthreads to the actual queue count to avoid creating extra
+ * handler threads that will hang during device removal.
+ *
+ * per_io_tasks mode is excluded: threads interleave across all
+ * queues so nthreads > nr_hw_queues is valid and intentional.
+ */
+ if (!ctx->per_io_tasks && dev->nthreads > info->nr_hw_queues)
+ dev->nthreads = info->nr_hw_queues;
+
ret = ublk_start_daemon(ctx, dev);
ublk_dbg(UBLK_DBG_DEV, "%s: daemon exit %d\n", __func__, ret);
if (ret < 0)
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH V2] selftests: ublk: cap nthreads to kernel's actual nr_hw_queues
2026-05-13 10:19 [PATCH V2] selftests: ublk: cap nthreads to kernel's actual nr_hw_queues Ming Lei
@ 2026-05-13 13:56 ` Jens Axboe
0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2026-05-13 13:56 UTC (permalink / raw)
To: linux-block, Ming Lei; +Cc: Caleb Sander Mateos, Uday Shankar
On Wed, 13 May 2026 18:19:40 +0800, Ming Lei wrote:
> dev->nthreads is derived from the user-requested queue count before the
> ADD command, but the kernel may reduce nr_hw_queues (capped to
> nr_cpu_ids). When the VM has fewer CPUs than requested queues, the
> daemon creates more handler threads than there are kernel queues.
>
> In non-batch mode, the extra threads access uninitialized queues
> (q_depth=0), submit zero io_uring SQEs, and block forever in
> io_cqring_wait. In batch mode, the extra threads cause similar hangs
> during device removal.
>
> [...]
Applied, thanks!
[1/1] selftests: ublk: cap nthreads to kernel's actual nr_hw_queues
commit: 87d0740b7c4cc847be1b6f307ab6d8547cb1a726
Best regards,
--
Jens Axboe
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-13 13:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-13 10:19 [PATCH V2] selftests: ublk: cap nthreads to kernel's actual nr_hw_queues Ming Lei
2026-05-13 13:56 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox