Skip to contentSkip to conversation

nvme-tcp: limit the direct send path to poll queues

PATCH In review Seiji Nishikawa 3 participants 3 messages Nov 26 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvme-tcp: limit the direct send path to poll queues

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.

---
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/[email protected]/

 drivers/nvme/host/tcp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/nvme/host/tcp.c +10 -4
index 62a594b..3725cac 100644
@@ -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;
Signature · 1 line
2.56.1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Seiji Nishikawa
Quoted 62 lines · Seiji Nishikawa

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.

---
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.

Quoted 5 lines

RFC: https://lore.kernel.org/linux-nvme/[email protected]/

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

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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