* [PATCH 1/8] nbd: Wake up config->conn_wait when setting NBD_RT_DISCONNECTED
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 2/8] nbd: Requeue requests directly in nbd_send_cmd() Bart Van Assche
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche
Wake up config->conn_wait in sock_shutdown() after setting
NBD_RT_DISCONNECTED and marking all sockets dead, and also in
nbd_mark_nsock_dead() when it sets NBD_RT_DISCONNECTED because the last
live connection died after a disconnect request. Remove the wake_up()
call from nbd_disconnect_and_put() since it is no longer necessary.
wait_for_reconnect() waits on config->conn_wait for NBD_RT_DISCONNECTED
to be set or for a live connection to become available. However, neither
sock_shutdown() nor nbd_mark_nsock_dead() woke up config->conn_wait after
setting NBD_RT_DISCONNECTED. Hence, callers of wait_for_reconnect() kept
waiting until dead_conn_timeout expired unless the flag was set by
sock_shutdown() called from nbd_disconnect_and_put(). Waking up waiters
wherever NBD_RT_DISCONNECTED is set also guarantees that a wakeup happens
if sock_shutdown() returns early because nbd_mark_nsock_dead() already
set NBD_RT_DISCONNECTED.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index ffce519bf008..864746624ff5 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -322,6 +322,7 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
&nbd->config->runtime_flags)) {
set_bit(NBD_RT_DISCONNECTED,
&nbd->config->runtime_flags);
+ wake_up(&nbd->config->conn_wait);
dev_info(nbd_to_dev(nbd),
"Disconnected due to user request.\n");
}
@@ -420,6 +421,7 @@ static void sock_shutdown(struct nbd_device *nbd)
nbd_mark_nsock_dead(nbd, nsock, 0);
mutex_unlock(&nsock->tx_lock);
}
+ wake_up(&config->conn_wait);
dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
}
@@ -2327,7 +2329,6 @@ static void nbd_disconnect_and_put(struct nbd_device *nbd)
mutex_lock(&nbd->config_lock);
nbd_disconnect(nbd);
sock_shutdown(nbd);
- wake_up(&nbd->config->conn_wait);
/*
* Make sure recv thread has finished, we can safely call nbd_clear_que()
* to cancel the inflight I/Os.
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 2/8] nbd: Requeue requests directly in nbd_send_cmd()
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
2026-11-16 21:48 ` [PATCH 1/8] nbd: Wake up config->conn_wait when setting NBD_RT_DISCONNECTED Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 3/8] nbd: Stop taking a config_refs reference per request Bart Van Assche
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche
Call nbd_requeue_cmd(cmd) and return BLK_STS_OK when sock_xmit() is
interrupted before any bytes have been sent in nbd_send_cmd(), instead of
setting NBD_CMD_REQUEUED and returning BLK_STS_RESOURCE.
nbd_handle_cmd() already calls blk_mq_start_request(req) before invoking
nbd_send_cmd(). Calling nbd_requeue_cmd(cmd) transitions the started
request back to MQ_RQ_IDLE via blk_mq_requeue_request() and ensures
nbd_send_cmd() only returns BLK_STS_OK or BLK_STS_IOERR, which simplifies
deferring request transmission to a workqueue.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 864746624ff5..64f2265fb5d9 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -650,8 +650,7 @@ static void nbd_sched_pending_work(struct nbd_device *nbd,
}
/*
- * Returns BLK_STS_RESOURCE if the caller should retry after a delay.
- * Returns BLK_STS_IOERR if sending failed.
+ * Returns BLK_STS_IOERR if sending failed and cannot be retried.
*/
static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
int index)
@@ -728,7 +727,8 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
trace_nbd_header_sent(req, handle);
if (result < 0) {
if (was_interrupted(result)) {
- /* If we haven't sent anything we can just return BUSY,
+ /*
+ * If we haven't sent anything we can just requeue,
* however if we have sent something we need to make
* sure we only allow this req to be sent until we are
* completely done.
@@ -737,8 +737,8 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
nbd_sched_pending_work(nbd, nsock, cmd, sent);
return BLK_STS_OK;
}
- set_bit(NBD_CMD_REQUEUED, &cmd->flags);
- return BLK_STS_RESOURCE;
+ nbd_requeue_cmd(cmd);
+ return BLK_STS_OK;
}
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Send control failed (result %d)\n", result);
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 3/8] nbd: Stop taking a config_refs reference per request
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
2026-11-16 21:48 ` [PATCH 1/8] nbd: Wake up config->conn_wait when setting NBD_RT_DISCONNECTED Bart Van Assche
2026-11-16 21:48 ` [PATCH 2/8] nbd: Requeue requests directly in nbd_send_cmd() Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 4/8] nbd: Defer request transmission to a workqueue Bart Van Assche
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche
Check refcount_read(&nbd->config_refs) instead of calling
nbd_get_config_unlocked() and nbd_config_put() for every request in
nbd_handle_cmd(), and use refcount_inc_not_zero() in
nbd_sched_pending_work().
nbd_config_put() calls nbd_clear_sock() -> nbd_clear_que(), which
quiesces the request queue before freeing config->socks and nbd->config.
Any nbd_queue_rq() call that observes config_refs > 0 completes before
blk_mq_quiesce_queue() returns, and any nbd_queue_rq() call after
blk_mq_unquiesce_queue() observes either config_refs == 0 or a newly
allocated configuration. The only other function that modifies
config->socks is nbd_add_socket(). It only runs before
nbd_start_device(), while the capacity is zero and the write cache is
disabled, so no request reaches the driver concurrently. Avoiding
nbd_config_put() in the request submission path also avoids acquiring
nbd->config_lock from the send workqueue when request transmission is
deferred to a workqueue.
If nbd_sched_pending_work() fails because config_refs dropped to zero
after part of a request has been sent, mark the socket dead so that no
other request is sent after the truncated request.
Use smp_wmb() instead of smp_mb__before_atomic() in
nbd_alloc_and_init_config() because refcount_set() is not a
read-modify-write operation, and document both readers that pair with
this barrier.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 67 ++++++++++++++++++++++++++++++++-------------
1 file changed, 48 insertions(+), 19 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 64f2265fb5d9..6373363cc4c4 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -629,11 +629,16 @@ static inline int was_interrupted(int result)
* We've already sent header or part of data payload, have no choice but
* to set pending and schedule it in work.
*
- * And we have to return BLK_STS_OK to block core, otherwise this same
- * request may be re-dispatched with different tag, but our header has
- * been sent out with old tag, and this way does confuse reply handling.
+ * The request must not be requeued, otherwise this same request may be
+ * re-dispatched with different tag, but our header has been sent out with
+ * old tag, and this way does confuse reply handling.
+ *
+ * Returns true if nsock->work has been scheduled. Returns false if no
+ * config_refs reference could be obtained because the device is being torn
+ * down. In that case the caller must mark @nsock dead, since a truncated
+ * request has been sent, and fail the request.
*/
-static void nbd_sched_pending_work(struct nbd_device *nbd,
+static bool nbd_sched_pending_work(struct nbd_device *nbd,
struct nbd_sock *nsock,
struct nbd_cmd *cmd, int sent)
{
@@ -642,11 +647,14 @@ static void nbd_sched_pending_work(struct nbd_device *nbd,
/* pending work should be scheduled only once */
WARN_ON_ONCE(test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags));
+ if (!refcount_inc_not_zero(&nbd->config_refs))
+ return false;
+
nsock->pending = req;
nsock->sent = sent;
set_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags);
- refcount_inc(&nbd->config_refs);
schedule_work(&nsock->work);
+ return true;
}
/*
@@ -734,8 +742,16 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
* completely done.
*/
if (sent) {
- nbd_sched_pending_work(nbd, nsock, cmd, sent);
- return BLK_STS_OK;
+ if (nbd_sched_pending_work(nbd, nsock, cmd,
+ sent))
+ return BLK_STS_OK;
+ /*
+ * Don't send anything after a truncated request.
+ * The device is being torn down, so don't notify
+ * user space that the link is dead.
+ */
+ nbd_mark_nsock_dead(nbd, nsock, 0);
+ return BLK_STS_IOERR;
}
nbd_requeue_cmd(cmd);
return BLK_STS_OK;
@@ -772,8 +788,11 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
result = sock_xmit(nbd, index, 1, &from, flags, &sent);
if (result < 0) {
if (was_interrupted(result)) {
- nbd_sched_pending_work(nbd, nsock, cmd, sent);
- return BLK_STS_OK;
+ if (nbd_sched_pending_work(nbd, nsock,
+ cmd, sent))
+ return BLK_STS_OK;
+ nbd_mark_nsock_dead(nbd, nsock, 0);
+ return BLK_STS_IOERR;
}
dev_err(disk_to_dev(nbd->disk),
"Send data failed (result %d)\n",
@@ -1126,17 +1145,28 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
lockdep_assert_held(&cmd->lock);
- config = nbd_get_config_unlocked(nbd);
- if (!config) {
+ /*
+ * No config_refs reference is held here. nbd->config and
+ * config->socks remain valid because nbd_config_put() calls
+ * nbd_clear_que(), which quiesces the request queue, before freeing
+ * them. The only other place that modifies config->socks is
+ * nbd_add_socket(). It only runs before nbd_start_device(), while the
+ * capacity is zero and the write cache is disabled, so no request
+ * reaches this driver concurrently (see also commit 285908f554fe
+ * ("nbd: remove queue freeze in nbd_add_socket")).
+ */
+ if (!refcount_read(&nbd->config_refs)) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Socks array is empty\n");
return BLK_STS_IOERR;
}
+ /* Pairs with smp_wmb() in nbd_alloc_and_init_config(). */
+ smp_rmb();
+ config = READ_ONCE(nbd->config);
if (index >= config->num_connections) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Attempted send on invalid socket\n");
- nbd_config_put(nbd);
return BLK_STS_IOERR;
}
cmd->status = BLK_STS_OK;
@@ -1159,7 +1189,6 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
* and instead just error out.
*/
sock_shutdown(nbd);
- nbd_config_put(nbd);
return BLK_STS_IOERR;
}
goto again;
@@ -1179,7 +1208,6 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
ret = nbd_send_cmd(nbd, cmd, index);
out:
mutex_unlock(&nsock->tx_lock);
- nbd_config_put(nbd);
return ret;
}
@@ -1734,12 +1762,13 @@ static int nbd_alloc_and_init_config(struct nbd_device *nbd)
nbd->config = config;
/*
- * Order refcount_set(&nbd->config_refs, 1) and nbd->config assignment,
- * its pair is the barrier in nbd_get_config_unlocked().
- * So nbd_get_config_unlocked() won't see nbd->config as null after
- * refcount_inc_not_zero() succeed.
+ * Order the nbd->config assignment before refcount_set(), such that
+ * readers that observe a non-zero config_refs also observe the new
+ * nbd->config value. Pairs with the barrier after
+ * refcount_inc_not_zero() in nbd_get_config_unlocked() and with the
+ * smp_rmb() in nbd_handle_cmd().
*/
- smp_mb__before_atomic();
+ smp_wmb(); /* Pairs with smp_rmb() and smp_mb__after_atomic(). */
refcount_set(&nbd->config_refs, 1);
return 0;
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 4/8] nbd: Defer request transmission to a workqueue
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
` (2 preceding siblings ...)
2026-11-16 21:48 ` [PATCH 3/8] nbd: Stop taking a config_refs reference per request Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 5/8] nbd: Call sk_set_memalloc() outside nsock->tx_lock Bart Van Assche
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche, syzbot+a761911cb2560f3ca2f7,
syzbot+d7d72f3d467710442af0, syzbot+e0faf21748da6475085c
Transmit requests from a per-device unbound workqueue using a per-socket
command list and work item instead of transmitting directly from
nbd_queue_rq(), similar to the loop driver.
Transmitting over a socket directly from nbd_queue_rq() acquires
cmd->lock, nsock->tx_lock, and sk_lock while blk-mq holds
q->q_usage_counter, q->elevator_lock, and set->srcu. Because NBD sockets
are created and owned by user space, other operations on those sockets
or sockets sharing the same lockdep class acquire sk_lock before memory
reclaim (fs_reclaim), mm->mmap_lock, or other subsystem locks, causing
circular lockdep dependency reports.
Break this dependency chain by queueing commands onto a per-socket
cmd_list protected by a spinlock in nbd_queue_rq() and performing socket
transmission from a workqueue under memalloc_noio_save().
Keep BLK_MQ_F_BLOCKING although nbd_queue_rq() no longer sleeps. With
that flag, blk_mq_quiesce_queue() waits for an SRCU grace period of the
tag set instead of a system-wide RCU grace period. nbd_clear_que() is
called for every NBD_CLEAR_SOCK ioctl and every configuration teardown,
and dropping the flag makes blktests nbd/003 about 20 times slower. Since
nbd_queue_rq() only acquires the nsock->cmd_lock spinlock while holding
set->srcu, keeping the flag does not reintroduce the lockdep dependency
chains described above.
Allocate the send workqueue with the same flags as the receive
workqueue. WQ_MEM_RECLAIM is required because the send workqueue is in
the writeback path, e.g. for swap over NBD, and because nbd_clear_que()
flushes it from recv_work() and from the block layer timeout handler,
both of which may run on WQ_MEM_RECLAIM workqueues. Do not use
WQ_FREEZABLE since request transmission was not frozen during system
suspend before this change.
Since request transmission now happens from a kernel worker thread, no
signal can be pending while sending and the partial-send handling in
nbd_send_cmd() is only kept for robustness. Destroy the send workqueue
before freeing the tag set in nbd_dev_remove().
Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")
Reported-by: syzbot+a761911cb2560f3ca2f7@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/6aec646f.aca85a03.3b899.0000.GAE@google.com/
Reported-by: syzbot+d7d72f3d467710442af0@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/6aec66fc.059f5055.2712e4.0001.GAE@google.com/
Reported-by: syzbot+e0faf21748da6475085c@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/6aec699e.73adde43.12e2c1.0001.GAE@google.com/
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 158 +++++++++++++++++++++++++++++++-------------
1 file changed, 112 insertions(+), 46 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 6373363cc4c4..4d362e3cc395 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -63,6 +63,10 @@ struct nbd_sock {
int fallback_index;
int cookie;
struct work_struct work;
+ spinlock_t cmd_lock;
+ struct list_head cmd_list;
+ struct work_struct tx_work;
+ int index;
};
struct recv_thread_args {
@@ -123,6 +127,7 @@ struct nbd_device {
struct mutex config_lock;
struct gendisk *disk;
struct workqueue_struct *recv_workq;
+ struct workqueue_struct *send_workq;
struct work_struct remove_work;
struct list_head list;
@@ -136,7 +141,7 @@ struct nbd_device {
#define NBD_CMD_REQUEUED 1
/*
- * This flag will be set if nbd_queue_rq() succeed, and will be checked and
+ * This flag will be set if nbd_send_cmd() succeeded, and will be checked and
* cleared in completion. Both setting and clearing of the flag are protected
* by cmd->lock.
*/
@@ -148,6 +153,7 @@ struct nbd_device {
struct nbd_cmd {
struct nbd_device *nbd;
struct mutex lock;
+ struct list_head list_entry;
int index;
int cookie;
int retries;
@@ -267,6 +273,7 @@ static void nbd_dev_remove(struct nbd_device *nbd)
struct gendisk *disk = nbd->disk;
del_gendisk(disk);
+ destroy_workqueue(nbd->send_workq);
blk_mq_free_tag_set(&nbd->tag_set);
/*
@@ -1080,6 +1087,7 @@ static bool nbd_clear_req(struct request *req, void *data)
static void nbd_clear_que(struct nbd_device *nbd)
{
blk_mq_quiesce_queue(nbd->disk->queue);
+ flush_workqueue(nbd->send_workq);
blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
blk_mq_unquiesce_queue(nbd->disk->queue);
dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
@@ -1139,36 +1147,12 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
{
struct request *req = blk_mq_rq_from_pdu(cmd);
struct nbd_device *nbd = cmd->nbd;
- struct nbd_config *config;
+ struct nbd_config *config = nbd->config;
struct nbd_sock *nsock;
blk_status_t ret;
lockdep_assert_held(&cmd->lock);
- /*
- * No config_refs reference is held here. nbd->config and
- * config->socks remain valid because nbd_config_put() calls
- * nbd_clear_que(), which quiesces the request queue, before freeing
- * them. The only other place that modifies config->socks is
- * nbd_add_socket(). It only runs before nbd_start_device(), while the
- * capacity is zero and the write cache is disabled, so no request
- * reaches this driver concurrently (see also commit 285908f554fe
- * ("nbd: remove queue freeze in nbd_add_socket")).
- */
- if (!refcount_read(&nbd->config_refs)) {
- dev_err_ratelimited(disk_to_dev(nbd->disk),
- "Socks array is empty\n");
- return BLK_STS_IOERR;
- }
- /* Pairs with smp_wmb() in nbd_alloc_and_init_config(). */
- smp_rmb();
- config = READ_ONCE(nbd->config);
-
- if (index >= config->num_connections) {
- dev_err_ratelimited(disk_to_dev(nbd->disk),
- "Attempted send on invalid socket\n");
- return BLK_STS_IOERR;
- }
cmd->status = BLK_STS_OK;
again:
nsock = config->socks[index];
@@ -1189,6 +1173,7 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
* and instead just error out.
*/
sock_shutdown(nbd);
+ blk_mq_start_request(req);
return BLK_STS_IOERR;
}
goto again;
@@ -1211,33 +1196,98 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
return ret;
}
+static void nbd_tx_work(struct work_struct *work)
+{
+ struct nbd_sock *nsock = container_of(work, struct nbd_sock, tx_work);
+ struct nbd_cmd *cmd;
+ unsigned int memflags;
+
+ /* See nbd_queue_rq() for why nsock and nbd->config remain valid. */
+ memflags = memalloc_noio_save();
+
+ spin_lock(&nsock->cmd_lock);
+ while (!list_empty(&nsock->cmd_list)) {
+ struct request *req;
+ blk_status_t ret;
+
+ cmd = list_first_entry(&nsock->cmd_list, struct nbd_cmd,
+ list_entry);
+ list_del_init(&cmd->list_entry);
+ spin_unlock(&nsock->cmd_lock);
+
+ req = blk_mq_rq_from_pdu(cmd);
+ /*
+ * Since we look at the bio's to send the request over the
+ * network we need to make sure the completion work doesn't
+ * mark this request done before we are done doing our send.
+ * This keeps us from dereferencing freed data if we have
+ * particularly fast completions (ie we get the completion
+ * before we exit sock_xmit on the last bvec) or in the case
+ * that the server is misbehaving (or there was an error)
+ * before we're done sending everything over the wire.
+ */
+ mutex_lock(&cmd->lock);
+ clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
+ ret = nbd_handle_cmd(cmd, nsock->index);
+ if (ret != BLK_STS_OK)
+ cmd->status = ret;
+ mutex_unlock(&cmd->lock);
+
+ if (ret != BLK_STS_OK)
+ blk_mq_complete_request(req);
+
+ cond_resched();
+
+ spin_lock(&nsock->cmd_lock);
+ }
+ spin_unlock(&nsock->cmd_lock);
+
+ memalloc_noio_restore(memflags);
+}
+
static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
const struct blk_mq_queue_data *bd)
{
- struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
- blk_status_t ret;
+ struct request *req = bd->rq;
+ struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
+ struct nbd_device *nbd = cmd->nbd;
+ struct nbd_config *config;
+ struct nbd_sock *nsock;
+ unsigned int index = hctx->queue_num;
/*
- * Since we look at the bio's to send the request over the network we
- * need to make sure the completion work doesn't mark this request done
- * before we are done doing our send. This keeps us from dereferencing
- * freed data if we have particularly fast completions (ie we get the
- * completion before we exit sock_xmit on the last bvec) or in the case
- * that the server is misbehaving (or there was an error) before we're
- * done sending everything over the wire.
+ * No config_refs reference is held here or in nbd_tx_work().
+ * nbd->config and config->socks remain valid because
+ * nbd_config_put() calls nbd_clear_que(), which quiesces the request
+ * queue and flushes nbd->send_workq, before freeing them. The only
+ * other place that modifies config->socks is nbd_add_socket(). It only
+ * runs before nbd_start_device(), while the capacity is zero and the
+ * write cache is disabled, so no request reaches this driver
+ * concurrently (see also commit 285908f554fe ("nbd: remove queue freeze
+ * in nbd_add_socket")).
*/
- mutex_lock(&cmd->lock);
- clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
+ if (!refcount_read(&nbd->config_refs)) {
+ dev_err_ratelimited(disk_to_dev(nbd->disk),
+ "Socks array is empty\n");
+ return BLK_STS_IOERR;
+ }
+ /* Pairs with smp_wmb() in nbd_alloc_and_init_config(). */
+ smp_rmb();
+ config = READ_ONCE(nbd->config);
- /* We can be called directly from the user space process, which means we
- * could possibly have signals pending so our sendmsg will fail. In
- * this case we need to return that we are busy, otherwise error out as
- * appropriate.
- */
- ret = nbd_handle_cmd(cmd, hctx->queue_num);
- mutex_unlock(&cmd->lock);
+ if (index >= config->num_connections) {
+ dev_err_ratelimited(disk_to_dev(nbd->disk),
+ "Attempted send on invalid socket\n");
+ return BLK_STS_IOERR;
+ }
- return ret;
+ nsock = config->socks[index];
+ spin_lock(&nsock->cmd_lock);
+ list_add_tail(&cmd->list_entry, &nsock->cmd_list);
+ queue_work(nbd->send_workq, &nsock->tx_work);
+ spin_unlock(&nsock->cmd_lock);
+
+ return BLK_STS_OK;
}
static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
@@ -1364,6 +1414,10 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
nsock->pending = NULL;
nsock->sent = 0;
nsock->cookie = 0;
+ nsock->index = config->num_connections;
+ spin_lock_init(&nsock->cmd_lock);
+ INIT_LIST_HEAD(&nsock->cmd_list);
+ INIT_WORK(&nsock->tx_work, nbd_tx_work);
INIT_WORK(&nsock->work, nbd_pending_cmd_work);
socks[config->num_connections++] = nsock;
atomic_inc(&config->live_connections);
@@ -1766,7 +1820,7 @@ static int nbd_alloc_and_init_config(struct nbd_device *nbd)
* readers that observe a non-zero config_refs also observe the new
* nbd->config value. Pairs with the barrier after
* refcount_inc_not_zero() in nbd_get_config_unlocked() and with the
- * smp_rmb() in nbd_handle_cmd().
+ * smp_rmb() in nbd_queue_rq().
*/
smp_wmb(); /* Pairs with smp_rmb() and smp_mb__after_atomic(). */
refcount_set(&nbd->config_refs, 1);
@@ -1965,6 +2019,7 @@ static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
cmd->nbd = set->driver_data;
cmd->flags = 0;
+ INIT_LIST_HEAD(&cmd->list_entry);
mutex_init(&cmd->lock);
return 0;
}
@@ -2040,6 +2095,15 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs,
goto out_err_disk;
}
+ nbd->send_workq = alloc_workqueue("nbd%d-send",
+ WQ_MEM_RECLAIM | WQ_HIGHPRI |
+ WQ_UNBOUND, 0, nbd->index);
+ if (!nbd->send_workq) {
+ dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd send work queue.\n");
+ err = -ENOMEM;
+ goto out_free_recv_work;
+ }
+
mutex_init(&nbd->config_lock);
refcount_set(&nbd->config_refs, 0);
/*
@@ -2066,6 +2130,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs,
return nbd;
out_free_work:
+ destroy_workqueue(nbd->send_workq);
+out_free_recv_work:
destroy_workqueue(nbd->recv_workq);
out_err_disk:
put_disk(disk);
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 5/8] nbd: Call sk_set_memalloc() outside nsock->tx_lock
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
` (3 preceding siblings ...)
2026-11-16 21:48 ` [PATCH 4/8] nbd: Defer request transmission to a workqueue Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 6/8] nbd: Remove socket lockdep reclassification Bart Van Assche
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche, syzbot+72fceb8393e04ce876d9
Call sk_set_memalloc() before acquiring nsock->tx_lock in
nbd_genl_reconnect_sock_cb() instead of while holding nsock->tx_lock.
sk_set_memalloc() increments the memalloc_socks_key static branch, which
acquires cpu_hotplug_lock and jump_label_mutex. Calling it under
nsock->tx_lock creates a nsock->tx_lock -> cpu_hotplug_lock lockdep
dependency chain. Because nbd_genl_reconnect_sock_cb() is called with
nbd->config_lock held and is the only place that transitions nsock->dead
from true to false, nsock->dead cannot become false after the initial
check, so sk_set_memalloc() can safely be called before acquiring
nsock->tx_lock. Since other contexts may set nsock->dead concurrently,
read it with READ_ONCE() and annotate the stores in
nbd_mark_nsock_dead() and nbd_genl_reconnect_sock_cb() with
WRITE_ONCE().
Fixes: b7aa3d39385d ("nbd: add a reconfigure netlink command")
Reported-by: syzbot+72fceb8393e04ce876d9@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/6aece79e.382bd2ce.33e041.0001.GAE@google.com/
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 4d362e3cc395..fd0789849612 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -335,7 +335,7 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
}
}
}
- nsock->dead = true;
+ WRITE_ONCE(nsock->dead, true);
nsock->pending = NULL;
nsock->sent = 0;
}
@@ -1447,26 +1447,31 @@ static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)
return -ENOMEM;
}
+ lockdep_assert_held(&nbd->config_lock);
+
for (i = 0; i < config->num_connections; i++) {
struct nbd_sock *nsock = config->socks[i];
- if (!nsock->dead)
+ /*
+ * Only this function, with config_lock held, clears
+ * nsock->dead. Other contexts may set it concurrently; if that
+ * happens after this check, the socket is reconnected on a
+ * later reconfigure request.
+ */
+ if (!READ_ONCE(nsock->dead))
continue;
- mutex_lock(&nsock->tx_lock);
- if (!nsock->dead) {
- mutex_unlock(&nsock->tx_lock);
- continue;
- }
sk_set_memalloc(sock->sk);
if (nbd->tag_set.timeout)
sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
atomic_inc(&config->recv_threads);
refcount_inc(&nbd->config_refs);
+
+ mutex_lock(&nsock->tx_lock);
old = nsock->sock;
nsock->fallback_index = -1;
nsock->sock = sock;
- nsock->dead = false;
+ WRITE_ONCE(nsock->dead, false);
INIT_WORK(&args->work, recv_work);
args->index = i;
args->nbd = nbd;
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 6/8] nbd: Remove socket lockdep reclassification
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
` (4 preceding siblings ...)
2026-11-16 21:48 ` [PATCH 5/8] nbd: Call sk_set_memalloc() outside nsock->tx_lock Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 7/8] nbd: Remove the partial-send handling Bart Van Assche
2026-11-16 21:48 ` [PATCH 8/8] nbd: Remove the sent argument of sock_xmit() Bart Van Assche
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche, syzbot+b29ed72382b0e218323a
Remove nbd_reclassify_socket() and its lockdep class keys, reverting
commit d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular
dependency") and commit 9280e6edf656 ("nbd: don't warn when reclassifying
a busy socket lock").
Unlike kernel-internal sockets created via sock_create_kern(), NBD sockets
are passed from user space as already-connected file descriptors and
remain accessible to user-space system calls, timers, and softirqs.
Calling sock_lock_init_class_and_name() on a live user-space socket:
- Races lockdep_init_map_type() against concurrent lock_acquire() calls
in bh_lock_sock() and lock_sock_nested(), corrupting lockdep state and
causing spinlock/lockdep splats.
- Calls sk_owner_set() repeatedly when the same socket fd is passed to
NBD_SET_SOCK multiple times, leaking module reference counts because
sk_prot_free() only calls sk_owner_put() once.
- Does not prevent circular lockdep reports when user space performs
sendmsg() on the retained socket fd and faults in user pages under
sk_lock-AF_INET-NBD.
The reclassification suppressed a false positive involving the chain
fs_reclaim -> q->q_usage_counter -> ... -> sk_lock, where the last step
was socket transmission from nbd_queue_rq(). Now that request
transmission is deferred from nbd_queue_rq() to a workqueue, sk_lock is
no longer acquired while blk-mq holds q->q_usage_counter, and
transmission happens under memalloc_noio_save(), so no fs_reclaim ->
sk_lock dependency is recorded for NBD sockets. Hence socket lockdep
reclassification is no longer needed.
Fixes: d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular dependency")
Fixes: 9280e6edf656 ("nbd: don't warn when reclassifying a busy socket lock")
Reported-by: syzbot+b29ed72382b0e218323a@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6aec63fd.5f5735a5.290c1c.0003.GAE@google.com/
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 37 -------------------------------------
1 file changed, 37 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index fd0789849612..f713779b5a5d 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1318,42 +1318,6 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
return sock;
}
-#ifdef CONFIG_DEBUG_LOCK_ALLOC
-static struct lock_class_key nbd_key[3];
-static struct lock_class_key nbd_slock_key[3];
-
-static void nbd_reclassify_socket(struct socket *sock)
-{
- struct sock *sk = sock->sk;
-
- if (!sock_allow_reclassification(sk))
- return;
-
- switch (sk->sk_family) {
- case AF_INET:
- sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
- &nbd_slock_key[0],
- "sk_lock-AF_INET-NBD",
- &nbd_key[0]);
- break;
- case AF_INET6:
- sock_lock_init_class_and_name(sk, "slock-AF_INET6-NBD",
- &nbd_slock_key[1],
- "sk_lock-AF_INET6-NBD",
- &nbd_key[1]);
- break;
- case AF_UNIX:
- sock_lock_init_class_and_name(sk, "slock-AF_UNIX-NBD",
- &nbd_slock_key[2],
- "sk_lock-AF_UNIX-NBD",
- &nbd_key[2]);
- break;
- }
-}
-#else
-static inline void nbd_reclassify_socket(struct socket *sock) {}
-#endif
-
static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
bool netlink)
{
@@ -1376,7 +1340,6 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
- nbd_reclassify_socket(sock);
if (!netlink && !nbd->task_setup &&
!test_bit(NBD_RT_BOUND, &config->runtime_flags))
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 7/8] nbd: Remove the partial-send handling
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
` (5 preceding siblings ...)
2026-11-16 21:48 ` [PATCH 6/8] nbd: Remove socket lockdep reclassification Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
2026-11-16 21:48 ` [PATCH 8/8] nbd: Remove the sent argument of sock_xmit() Bart Van Assche
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche
Remove was_interrupted(), nbd_sched_pending_work(),
nbd_pending_cmd_work(), NBD_CMD_PARTIAL_SEND and the nsock->pending,
nsock->sent and nsock->work members. Treat a failure of sock_xmit() in
nbd_send_cmd() the same way regardless of the error code: mark the socket
dead and requeue the request.
The partial-send handling was introduced for the case where sendmsg()
fails with -ERESTARTSYS or -EINTR because a signal is pending in the task
that called nbd_queue_rq(). Since requests are now transmitted from
nbd_tx_work(), which runs on a kernel worker thread, no signal can be
pending while sending and that code is unreachable. Other send errors,
including a send timeout after part of a request has been sent, already
mark the socket dead and requeue the request, so a truncated request is
never followed by another request on the same socket.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 178 +-------------------------------------------
1 file changed, 3 insertions(+), 175 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index f713779b5a5d..3d834e4e1349 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -57,12 +57,9 @@ static int nbd_total_devices = 0;
struct nbd_sock {
struct socket *sock;
struct mutex tx_lock;
- struct request *pending;
- int sent;
bool dead;
int fallback_index;
int cookie;
- struct work_struct work;
spinlock_t cmd_lock;
struct list_head cmd_list;
struct work_struct tx_work;
@@ -147,9 +144,6 @@ struct nbd_device {
*/
#define NBD_CMD_INFLIGHT 2
-/* Just part of request header or data payload is sent successfully */
-#define NBD_CMD_PARTIAL_SEND 3
-
struct nbd_cmd {
struct nbd_device *nbd;
struct mutex lock;
@@ -336,8 +330,6 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
}
}
WRITE_ONCE(nsock->dead, true);
- nsock->pending = NULL;
- nsock->sent = 0;
}
static void nbd_apply_limits(struct queue_limits *lim, u32 flags)
@@ -475,12 +467,6 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
if (!mutex_trylock(&cmd->lock))
return BLK_EH_RESET_TIMER;
- /* partial send is handled in nbd_sock's work function */
- if (test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags)) {
- mutex_unlock(&cmd->lock);
- return BLK_EH_RESET_TIMER;
- }
-
if (!test_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
mutex_unlock(&cmd->lock);
return BLK_EH_DONE;
@@ -623,47 +609,6 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send,
return __sock_xmit(nbd, sock, send, iter, msg_flags, sent);
}
-/*
- * Different settings for sk->sk_sndtimeo can result in different return values
- * if there is a signal pending when we enter sendmsg, because reasons?
- */
-static inline int was_interrupted(int result)
-{
- return result == -ERESTARTSYS || result == -EINTR;
-}
-
-/*
- * We've already sent header or part of data payload, have no choice but
- * to set pending and schedule it in work.
- *
- * The request must not be requeued, otherwise this same request may be
- * re-dispatched with different tag, but our header has been sent out with
- * old tag, and this way does confuse reply handling.
- *
- * Returns true if nsock->work has been scheduled. Returns false if no
- * config_refs reference could be obtained because the device is being torn
- * down. In that case the caller must mark @nsock dead, since a truncated
- * request has been sent, and fail the request.
- */
-static bool nbd_sched_pending_work(struct nbd_device *nbd,
- struct nbd_sock *nsock,
- struct nbd_cmd *cmd, int sent)
-{
- struct request *req = blk_mq_rq_from_pdu(cmd);
-
- /* pending work should be scheduled only once */
- WARN_ON_ONCE(test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags));
-
- if (!refcount_inc_not_zero(&nbd->config_refs))
- return false;
-
- nsock->pending = req;
- nsock->sent = sent;
- set_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags);
- schedule_work(&nsock->work);
- return true;
-}
-
/*
* Returns BLK_STS_IOERR if sending failed and cannot be retried.
*/
@@ -681,7 +626,6 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
u64 handle;
u32 type;
u32 nbd_cmd_flags = 0;
- int sent = nsock->sent, skip = 0;
lockdep_assert_held(&cmd->lock);
lockdep_assert_held(&nsock->tx_lock);
@@ -704,23 +648,7 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
if ((req->cmd_flags & REQ_NOUNMAP) && (type == NBD_CMD_WRITE_ZEROES))
nbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE;
- /* We did a partial send previously, and we at least sent the whole
- * request struct, so just go and send the rest of the pages in the
- * request.
- */
- if (sent) {
- if (sent >= sizeof(request)) {
- skip = sent - sizeof(request);
-
- /* initialize handle for tracing purposes */
- handle = nbd_cmd_handle(cmd);
-
- goto send_pages;
- }
- iov_iter_advance(&from, sent);
- } else {
- cmd->cmd_cookie++;
- }
+ cmd->cmd_cookie++;
cmd->index = index;
cmd->cookie = nsock->cookie;
cmd->retries = 0;
@@ -738,36 +666,13 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
req, nbdcmd_to_ascii(type),
(unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
result = sock_xmit(nbd, index, 1, &from,
- (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
+ (type == NBD_CMD_WRITE) ? MSG_MORE : 0, NULL);
trace_nbd_header_sent(req, handle);
if (result < 0) {
- if (was_interrupted(result)) {
- /*
- * If we haven't sent anything we can just requeue,
- * however if we have sent something we need to make
- * sure we only allow this req to be sent until we are
- * completely done.
- */
- if (sent) {
- if (nbd_sched_pending_work(nbd, nsock, cmd,
- sent))
- return BLK_STS_OK;
- /*
- * Don't send anything after a truncated request.
- * The device is being torn down, so don't notify
- * user space that the link is dead.
- */
- nbd_mark_nsock_dead(nbd, nsock, 0);
- return BLK_STS_IOERR;
- }
- nbd_requeue_cmd(cmd);
- return BLK_STS_OK;
- }
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Send control failed (result %d)\n", result);
goto requeue;
}
-send_pages:
if (type != NBD_CMD_WRITE)
goto out;
@@ -784,23 +689,8 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
req, bvec.bv_len);
iov_iter_bvec(&from, ITER_SOURCE, &bvec, 1, bvec.bv_len);
- if (skip) {
- if (skip >= iov_iter_count(&from)) {
- skip -= iov_iter_count(&from);
- continue;
- }
- iov_iter_advance(&from, skip);
- skip = 0;
- }
- result = sock_xmit(nbd, index, 1, &from, flags, &sent);
+ result = sock_xmit(nbd, index, 1, &from, flags, NULL);
if (result < 0) {
- if (was_interrupted(result)) {
- if (nbd_sched_pending_work(nbd, nsock,
- cmd, sent))
- return BLK_STS_OK;
- nbd_mark_nsock_dead(nbd, nsock, 0);
- return BLK_STS_IOERR;
- }
dev_err(disk_to_dev(nbd->disk),
"Send data failed (result %d)\n",
result);
@@ -819,20 +709,10 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
}
out:
trace_nbd_payload_sent(req, handle);
- nsock->pending = NULL;
- nsock->sent = 0;
__set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
return BLK_STS_OK;
requeue:
- /*
- * Can't requeue in case we are dealing with partial send
- *
- * We must run from pending work function.
- * */
- if (test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags))
- return BLK_STS_OK;
-
/* retry on a different socket */
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Request send failed, requeueing\n");
@@ -841,44 +721,6 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
return BLK_STS_OK;
}
-/* handle partial sending */
-static void nbd_pending_cmd_work(struct work_struct *work)
-{
- struct nbd_sock *nsock = container_of(work, struct nbd_sock, work);
- struct request *req = nsock->pending;
- struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
- struct nbd_device *nbd = cmd->nbd;
- unsigned long deadline = READ_ONCE(req->deadline);
- unsigned int wait_ms = 2;
-
- mutex_lock(&cmd->lock);
-
- WARN_ON_ONCE(test_bit(NBD_CMD_REQUEUED, &cmd->flags));
- if (WARN_ON_ONCE(!test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags)))
- goto out;
-
- mutex_lock(&nsock->tx_lock);
- while (true) {
- nbd_send_cmd(nbd, cmd, cmd->index);
- if (!nsock->pending)
- break;
-
- /* don't bother timeout handler for partial sending */
- if (READ_ONCE(jiffies) + msecs_to_jiffies(wait_ms) >= deadline) {
- cmd->status = BLK_STS_IOERR;
- blk_mq_complete_request(req);
- break;
- }
- msleep(wait_ms);
- wait_ms *= 2;
- }
- mutex_unlock(&nsock->tx_lock);
- clear_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags);
-out:
- mutex_unlock(&cmd->lock);
- nbd_config_put(nbd);
-}
-
static int nbd_read_reply(struct nbd_device *nbd, struct socket *sock,
struct nbd_reply *reply)
{
@@ -1179,19 +1021,8 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
goto again;
}
- /* Handle the case that we have a pending request that was partially
- * transmitted that _has_ to be serviced first. We need to call requeue
- * here so that it gets put _after_ the request that is already on the
- * dispatch list.
- */
blk_mq_start_request(req);
- if (unlikely(nsock->pending && nsock->pending != req)) {
- nbd_requeue_cmd(cmd);
- ret = BLK_STS_OK;
- goto out;
- }
ret = nbd_send_cmd(nbd, cmd, index);
-out:
mutex_unlock(&nsock->tx_lock);
return ret;
}
@@ -1374,14 +1205,11 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
nsock->dead = false;
mutex_init(&nsock->tx_lock);
nsock->sock = sock;
- nsock->pending = NULL;
- nsock->sent = 0;
nsock->cookie = 0;
nsock->index = config->num_connections;
spin_lock_init(&nsock->cmd_lock);
INIT_LIST_HEAD(&nsock->cmd_list);
INIT_WORK(&nsock->tx_work, nbd_tx_work);
- INIT_WORK(&nsock->work, nbd_pending_cmd_work);
socks[config->num_connections++] = nsock;
atomic_inc(&config->live_connections);
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 8/8] nbd: Remove the sent argument of sock_xmit()
2026-11-16 21:48 [PATCH 0/8] nbd: Defer request transmission to a workqueue Bart Van Assche
` (6 preceding siblings ...)
2026-11-16 21:48 ` [PATCH 7/8] nbd: Remove the partial-send handling Bart Van Assche
@ 2026-11-16 21:48 ` Bart Van Assche
7 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2026-11-16 21:48 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Josef Bacik, Eric Dumazet, Aibing Chen,
Bart Van Assche
Remove the sent argument of sock_xmit() and __sock_xmit(). All callers
pass NULL since the partial-send handling has been removed.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/block/nbd.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 3d834e4e1349..73fc546447a6 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -553,7 +553,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
}
static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
- struct iov_iter *iter, int msg_flags, int *sent)
+ struct iov_iter *iter, int msg_flags)
{
int result;
struct msghdr msg = {} ;
@@ -586,8 +586,6 @@ static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
result = -EPIPE; /* short read */
break;
}
- if (sent)
- *sent += result;
} while (msg_data_left(&msg));
}
@@ -601,12 +599,12 @@ static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
* negtive value on failure, and never return 0.
*/
static int sock_xmit(struct nbd_device *nbd, int index, int send,
- struct iov_iter *iter, int msg_flags, int *sent)
+ struct iov_iter *iter, int msg_flags)
{
struct nbd_config *config = nbd->config;
struct socket *sock = config->socks[index]->sock;
- return __sock_xmit(nbd, sock, send, iter, msg_flags, sent);
+ return __sock_xmit(nbd, sock, send, iter, msg_flags);
}
/*
@@ -666,7 +664,7 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
req, nbdcmd_to_ascii(type),
(unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
result = sock_xmit(nbd, index, 1, &from,
- (type == NBD_CMD_WRITE) ? MSG_MORE : 0, NULL);
+ (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
trace_nbd_header_sent(req, handle);
if (result < 0) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
@@ -689,7 +687,7 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
req, bvec.bv_len);
iov_iter_bvec(&from, ITER_SOURCE, &bvec, 1, bvec.bv_len);
- result = sock_xmit(nbd, index, 1, &from, flags, NULL);
+ result = sock_xmit(nbd, index, 1, &from, flags);
if (result < 0) {
dev_err(disk_to_dev(nbd->disk),
"Send data failed (result %d)\n",
@@ -730,7 +728,7 @@ static int nbd_read_reply(struct nbd_device *nbd, struct socket *sock,
reply->magic = 0;
iov_iter_kvec(&to, ITER_DEST, &iov, 1, sizeof(*reply));
- result = __sock_xmit(nbd, sock, 0, &to, MSG_WAITALL, NULL);
+ result = __sock_xmit(nbd, sock, 0, &to, MSG_WAITALL);
if (result < 0) {
if (!nbd_disconnected(nbd->config))
dev_err(disk_to_dev(nbd->disk),
@@ -819,7 +817,7 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index,
rq_for_each_segment(bvec, req, iter) {
iov_iter_bvec(&to, ITER_DEST, &bvec, 1, bvec.bv_len);
- result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
+ result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
if (result < 0) {
dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
result);
@@ -1319,7 +1317,7 @@ static void send_disconnects(struct nbd_device *nbd)
iov_iter_kvec(&from, ITER_SOURCE, &iov, 1, sizeof(request));
mutex_lock(&nsock->tx_lock);
- ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
+ ret = sock_xmit(nbd, i, 1, &from, 0);
if (ret < 0)
dev_err(disk_to_dev(nbd->disk),
"Send disconnect failed %d\n", ret);
^ permalink raw reply related [flat|nested] 9+ messages in thread