Skip to contentSkip to conversation

nvmet-tcp: Enforce update ordering between queue->cmd and rcv_state

PATCH In review Meir Elisha 2 participants 2 messages Feb 19, 2025 – Feb 20, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 nvmet-tcp: Enforce update ordering between queue->cmd and rcv_state

The order in which queue->cmd and rcv_state are updated is crucial. If these assignments are reordered by the compiler, the worker might not get queued in nvmet_tcp_queue_response(), hanging the IO. to enforce the the correct reordering, set rcv_state using smp_store_release().

---
Changes from v2:
- Read queue_state and queue_cmd locally and use load barrier
- Remove unnecessary barrier in nvmet_tcp_handle_h2c_data_pdu

Changes from v1:
- Change comments to c-style

 drivers/nvme/target/tcp.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/nvme/target/tcp.c +11 -4
index 7c51c2a8c109..20fc1a27b720 100644
@@ -572,9 +572,15 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
 		container_of(req, struct nvmet_tcp_cmd, req);
 	struct nvmet_tcp_queue	*queue = cmd->queue;
 	struct nvme_sgl_desc *sgl;
+	enum nvmet_tcp_queue_state queue_state = READ_ONCE(queue->state);
+	/*
+	 * Use an acquire load to ensure that any updates to queue->state are visible
+	 * before loading queue->cmd.
+	 */
+	struct nvmet_tcp_cmd *queue_cmd = smp_load_acquire(&queue->cmd);
 	u32 len;
 
-	if (unlikely(cmd == queue->cmd)) {
+	if (unlikely(cmd == queue_cmd)) {
 		sgl = &cmd->req.cmd->common.dptr.sgl;
 		len = le32_to_cpu(sgl->length);
 
@@ -583,7 +589,7 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
 		 * Avoid using helpers, this might happen before
 		 * nvmet_req_init is completed.
 		 */
-		if (queue->rcv_state == NVMET_TCP_RECV_PDU &&
+		if (queue_state == NVMET_TCP_RECV_PDU &&
 		    len && len <= cmd->req.port->inline_data_size &&
 		    nvme_is_write(cmd->req.cmd))
 			return;
@@ -847,8 +853,9 @@ static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue)
 {
 	queue->offset = 0;
 	queue->left = sizeof(struct nvme_tcp_hdr);
-	queue->cmd = NULL;
-	queue->rcv_state = NVMET_TCP_RECV_PDU;
+	WRITE_ONCE(queue->cmd, NULL);
+	/* Ensure rcv_state is visible only after queue->cmd is set */
+	smp_store_release(&queue->rcv_state, NVMET_TCP_RECV_PDU);
 }
 
 static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue)
Signature · 40 lines
2.34.1

This ordering is critical on weakly ordered architectures (such as ARM)
so that any observer which sees the new rcv_state is guaranteed to also
see the updated cmd. Without this guarantee (i.e if the two stores were
reordered), a parallel context might see the new state while queue->cmd
still holds a stale value. This could cause the inline-data check to
return early and ultimately hang the IO.
Additionally, I reviewed the assembly code for ARM and confirmed that
the instructions were reordered(unlike x86), reinforcing the need for
this change.

This scenario was encountered during fio testing, which involved
running 2 min of 4K random writes using an ARM-based machine as the
target. We observed hanging I/O typically after 10-20 iterations.

fio config used:
[global]
ioengine=libaio
max_latency=45s
end_fsync=1
create_serialize=0
size=3200m
directory=/mnt/volumez/vol0
ramp_time=30
lat_percentiles=1
direct=1
filename_format=fiodata.$jobnum
verify_dump=1
numjobs=16
fallocate=native
stonewall=1
group_reporting=1
file_service_type=random
iodepth=16
runtime=5m
time_based=1
[random_0_100_4k]
bs=4k
rw=randwrite
maintainer
#1 lore raw
Quoted 6 lines · Meir Elisha

The order in which queue->cmd and rcv_state are updated is crucial. If these assignments are reordered by the compiler, the worker might not get queued in nvmet_tcp_queue_response(), hanging the IO. to enforce the the correct reordering, set rcv_state using smp_store_release().

I think we need a fixes tag, and also rephrase the patch title to describe it is fixing a bug rather than what it is doing.

Something like:
"nvmet-tcp: Fix a possible sporadic response drops in weakly ordered architectures"

Quoted 16 lines

---
Changes from v2:
- Read queue_state and queue_cmd locally and use load barrier
- Remove unnecessary barrier in nvmet_tcp_handle_h2c_data_pdu

Changes from v1:
- Change comments to c-style

  drivers/nvme/target/tcp.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
drivers/nvme/target/tcp.c +0 -0
index 7c51c2a8c109..20fc1a27b720 100644

drivers/nvme/target/tcp.c static void nvmet_tcp_queue_response(struct nvmet_req *req) · line 580

Quoted 9 lines
drivers/nvme/target/tcp.c +6 -0
@@ -572,9 +572,15 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
  		container_of(req, struct nvmet_tcp_cmd, req);
  	struct nvmet_tcp_queue	*queue = cmd->queue;
  	struct nvme_sgl_desc *sgl;
+	enum nvmet_tcp_queue_state queue_state = READ_ONCE(queue->state);
+	/*
+	 * Use an acquire load to ensure that any updates to queue->state are visible
+	 * before loading queue->cmd.
+	 */
+	struct nvmet_tcp_cmd *queue_cmd = smp_load_acquire(&queue->cmd);

Nit, lets move this above the sgl.

Quoted 28 lines
drivers/nvme/target/tcp.c +5 -4
  	u32 len;
 
-	if (unlikely(cmd == queue->cmd)) {
+	if (unlikely(cmd == queue_cmd)) {
  		sgl = &cmd->req.cmd->common.dptr.sgl;
  		len = le32_to_cpu(sgl->length);
 
@@ -583,7 +589,7 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
  		 * Avoid using helpers, this might happen before
  		 * nvmet_req_init is completed.
  		 */
-		if (queue->rcv_state == NVMET_TCP_RECV_PDU &&
+		if (queue_state == NVMET_TCP_RECV_PDU &&
  		    len && len <= cmd->req.port->inline_data_size &&
  		    nvme_is_write(cmd->req.cmd))
  			return;
@@ -847,8 +853,9 @@ static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue)
  {
  	queue->offset = 0;
  	queue->left = sizeof(struct nvme_tcp_hdr);
-	queue->cmd = NULL;
-	queue->rcv_state = NVMET_TCP_RECV_PDU;
+	WRITE_ONCE(queue->cmd, NULL);
+	/* Ensure rcv_state is visible only after queue->cmd is set */
+	smp_store_release(&queue->rcv_state, NVMET_TCP_RECV_PDU);
  }
 
  static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue)