Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] nvme-tcp: limit the direct send path to poll queues
@ 2026-11-26  6:32 Seiji Nishikawa
  2026-11-26  8:53 ` Christoph Hellwig
  2026-11-26  9:53 ` Hannes Reinecke
  0 siblings, 2 replies; 3+ messages in thread
From: Seiji Nishikawa @ 2026-11-26  6:32 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: Hannes Reinecke, Chris Leech, Seiji Nishikawa, linux-nvme,
	linux-kernel

nvme_tcp_queue_request() sends the request directly from the submitting
context when it runs on the io_cpu of the queue, the queue is empty and
it can take send_mutex. Commit db5ad6b7f8cd ("nvme-tcp: try to send
request in queue_rq context") added this so that an idle queue can send
the request without waiting for io_work to run.

Since then this path has needed a series of fixes for races and
priority inversions between it and io_work. These are
commit 5c11f7d9f843 ("nvme-tcp: Fix possible race of io_work and direct
send"), commit a0fdd1418007 ("nvme-tcp: rerun io_work if req_list is
not empty"), commit 70f437fb4395 ("nvme-tcp: fix io_work priority
inversion"), commit 08acb54b063a ("nvme-tcp: defer TLS inline send to
io_work") and commit 5db358f3a7f9 ("nvme-tcp: fix io_work priority
inversion when send_mutex is held").

On a non-poll queue, the direct send path makes almost no difference to
the latency. fio with libaio and 4k random I/O at queue depth 1 over a
1Gb network, with and without the direct send path (average of 3 runs
of 60 seconds, latency in usec):

                      IOPS   slat   clat    lat
  randread   with     4153    7.1  232.3  239.3
             without  4154    4.7  234.7  239.4
  randwrite  with     2472   24.5  375.6  400.1
             without  2460   14.3  387.6  402.0

lat is slat plus clat. It changes by 0.1 usec for randread and by 1.9
usec for randwrite.

On a poll queue, the latency becomes worse without the direct send path.
The submitting task polls for the completion without sleeping. When it
runs on io_cpu, where io_work also runs, the kworker that runs io_work
can send the request only after the task is switched out. This happens
once per request. fio with io_uring and --hipri at queue depth 1 over
the same network, on the io_cpu of a poll queue (ctx is the number of
context switches of fio in one run):

                      IOPS   slat    clat     lat      ctx
  randread   with     5017   4.72  193.95  198.67      553
             without  4179   9.35  229.70  239.05  251,318
  randwrite  with     3619   5.60  269.94  275.54      542
             without  3621  10.62  264.53  275.16  217,806

For randread, lat goes up by 40 usec (20%) without the direct send
path.

So remove the direct send path, except for poll queues. The other
queues always send from io_work, as TLS queues already do.

Add a queue flag, NVME_TCP_Q_POLLED, for this. nvme_tcp_alloc_queue()
sets the flag when the queue is a poll queue, and
nvme_tcp_queue_request() checks it.

Suggested-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
---
Changes since RFC:
- Keep the direct send path for poll queues. Sashiko pointed out the
  cost on poll queues, and the numbers in the RFC thread confirm it.
- Drop the patch that removed send_mutex. Poll queues still have 2
  senders, so send_mutex is still needed.

RFC: https://lore.kernel.org/linux-nvme/20261122011826.1959697-1-snishika@redhat.com/

 drivers/nvme/host/tcp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b..3725cac 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -124,6 +124,7 @@ enum nvme_tcp_queue_flags {
 	NVME_TCP_Q_POLLING	= 2,
 	NVME_TCP_Q_IO_CPU_SET	= 3,
 	NVME_TCP_Q_TLS_RECORD	= 4,
+	NVME_TCP_Q_POLLED	= 5,
 };
 
 #define NVME_TCP_OPT_NR_QUEUES(opts) ((opts)->nr_io_queues + \
@@ -452,15 +453,18 @@ static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
 		list_empty(&queue->send_list) && !queue->request;
 
 	/*
-	 * if we're the first on the send_list and we can try to send
-	 * directly, otherwise queue io_work. Also, only do that if we
-	 * are on the same cpu, so we don't introduce contention.
+	 * On a poll queue, the submitting task keeps polling on its cpu
+	 * instead of sleeping. If that cpu is io_cpu, sending from io_work
+	 * would cause a context switch to the kworker for every request.
+	 * So send the request directly here if the queue is empty and we
+	 * are on io_cpu. Otherwise send from io_work.
 	 *
 	 * TLS kTLS send takes ctx->tx_lock while blk_mq holds set->srcu.
 	 * lockdep reports circular locking via elevator_lock.  Defer TLS
 	 * sends to the io workqueue instead of inline from this path.
 	 */
-	if (queue->io_cpu == raw_smp_processor_id() &&
+	if (test_bit(NVME_TCP_Q_POLLED, &queue->flags) &&
+	    queue->io_cpu == raw_smp_processor_id() &&
 	    !nvme_tcp_queue_tls(queue) &&
 	    empty && mutex_trylock(&queue->send_mutex)) {
 		nvme_tcp_send_all(queue);
@@ -2190,6 +2194,8 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
 	queue->sock->sk->sk_allocation = GFP_ATOMIC;
 	queue->sock->sk->sk_use_task_frag = false;
 	queue->io_cpu = WORK_CPU_UNBOUND;
+	assign_bit(NVME_TCP_Q_POLLED, &queue->flags,
+		   nvme_tcp_poll_queue(queue));
 	queue->request = NULL;
 	queue->data_remaining = 0;
 	queue->ddgst_remaining = 0;
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] nvme-tcp: limit the direct send path to poll queues
  2026-11-26  6:32 [PATCH v2] nvme-tcp: limit the direct send path to poll queues Seiji Nishikawa
@ 2026-11-26  8:53 ` Christoph Hellwig
  2026-11-26  9:53 ` Hannes Reinecke
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2026-11-26  8:53 UTC (permalink / raw)
  To: Seiji Nishikawa
  Cc: Keith Busch, Jens Axboe, Sagi Grimberg, Hannes Reinecke,
	Chris Leech, linux-nvme, linux-kernel

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] nvme-tcp: limit the direct send path to poll queues
  2026-11-26  6:32 [PATCH v2] nvme-tcp: limit the direct send path to poll queues Seiji Nishikawa
  2026-11-26  8:53 ` Christoph Hellwig
@ 2026-11-26  9:53 ` Hannes Reinecke
  1 sibling, 0 replies; 3+ messages in thread
From: Hannes Reinecke @ 2026-11-26  9:53 UTC (permalink / raw)
  To: Seiji Nishikawa, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg
  Cc: Chris Leech, linux-nvme, linux-kernel

On 11/26/26 7:32 AM, Seiji Nishikawa wrote:
> nvme_tcp_queue_request() sends the request directly from the submitting
> context when it runs on the io_cpu of the queue, the queue is empty and
> it can take send_mutex. Commit db5ad6b7f8cd ("nvme-tcp: try to send
> request in queue_rq context") added this so that an idle queue can send
> the request without waiting for io_work to run.
> 
> Since then this path has needed a series of fixes for races and
> priority inversions between it and io_work. These are
> commit 5c11f7d9f843 ("nvme-tcp: Fix possible race of io_work and direct
> send"), commit a0fdd1418007 ("nvme-tcp: rerun io_work if req_list is
> not empty"), commit 70f437fb4395 ("nvme-tcp: fix io_work priority
> inversion"), commit 08acb54b063a ("nvme-tcp: defer TLS inline send to
> io_work") and commit 5db358f3a7f9 ("nvme-tcp: fix io_work priority
> inversion when send_mutex is held").
> 
> On a non-poll queue, the direct send path makes almost no difference to
> the latency. fio with libaio and 4k random I/O at queue depth 1 over a
> 1Gb network, with and without the direct send path (average of 3 runs
> of 60 seconds, latency in usec):
> 
>                        IOPS   slat   clat    lat
>    randread   with     4153    7.1  232.3  239.3
>               without  4154    4.7  234.7  239.4
>    randwrite  with     2472   24.5  375.6  400.1
>               without  2460   14.3  387.6  402.0
> 
> lat is slat plus clat. It changes by 0.1 usec for randread and by 1.9
> usec for randwrite.
> 
> On a poll queue, the latency becomes worse without the direct send path.
> The submitting task polls for the completion without sleeping. When it
> runs on io_cpu, where io_work also runs, the kworker that runs io_work
> can send the request only after the task is switched out. This happens
> once per request. fio with io_uring and --hipri at queue depth 1 over
> the same network, on the io_cpu of a poll queue (ctx is the number of
> context switches of fio in one run):
> 
>                        IOPS   slat    clat     lat      ctx
>    randread   with     5017   4.72  193.95  198.67      553
>               without  4179   9.35  229.70  239.05  251,318
>    randwrite  with     3619   5.60  269.94  275.54      542
>               without  3621  10.62  264.53  275.16  217,806
> 
> For randread, lat goes up by 40 usec (20%) without the direct send
> path.
> 
> So remove the direct send path, except for poll queues. The other
> queues always send from io_work, as TLS queues already do.
> 
> Add a queue flag, NVME_TCP_Q_POLLED, for this. nvme_tcp_alloc_queue()
> sets the flag when the queue is a poll queue, and
> nvme_tcp_queue_request() checks it.
> 
> Suggested-by: Hannes Reinecke <hare@suse.de>
> Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
> ---
> Changes since RFC:
> - Keep the direct send path for poll queues. Sashiko pointed out the
>    cost on poll queues, and the numbers in the RFC thread confirm it.
> - Drop the patch that removed send_mutex. Poll queues still have 2
>    senders, so send_mutex is still needed.
> 
> RFC: https://lore.kernel.org/linux-nvme/20261122011826.1959697-1-snishika@redhat.com/
> 
>   drivers/nvme/host/tcp.c | 14 ++++++++++----
>   1 file changed, 10 insertions(+), 4 deletions(-)
> 
Reviewed-by: Hannes Reinecke <hare@kernel.org>

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                  Kernel Storage Architect
hare@suse.de                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-26  9:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  6:32 [PATCH v2] nvme-tcp: limit the direct send path to poll queues Seiji Nishikawa
2026-11-26  8:53 ` Christoph Hellwig
2026-11-26  9:53 ` Hannes Reinecke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox