Skip to contentSkip to conversation

nvmet-tcp: Fix a possible sporadic response drops in weakly ordered arch

PATCH Superseded Meir Elisha 3 participants 5 messages Feb 20, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet-tcp: Fix a possible sporadic response drops in weakly ordered arch

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

Fixes bdaf13279192 ("nvmet-tcp: fix a segmentation fault during io parsing error")
---
 drivers/nvme/target/tcp.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/nvme/target/tcp.c +11 -4
index 7c51c2a8c109..4021468c8857 100644
@@ -571,10 +571,16 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
 	struct nvmet_tcp_cmd *cmd =
 		container_of(req, struct nvmet_tcp_cmd, req);
 	struct nvmet_tcp_queue	*queue = cmd->queue;
+	enum nvmet_tcp_recv_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);
 	struct nvme_sgl_desc *sgl;
 	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
#2 lore raw
In reply to Meir Elisha
Quoted 17 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().

Fixes bdaf13279192 ("nvmet-tcp: fix a segmentation fault during io parsing error")
---
 drivers/nvme/target/tcp.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/nvme/target/tcp.c +0 -0
index 7c51c2a8c109..4021468c8857 100644

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

Quoted 4 lines
drivers/nvme/target/tcp.c +1 -0
@@ -571,10 +571,16 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
        struct nvmet_tcp_cmd *cmd =
                container_of(req, struct nvmet_tcp_cmd, req);
        struct nvmet_tcp_queue  *queue = cmd->queue;
+       enum nvmet_tcp_recv_state queue_state = READ_ONCE(queue->state);

Why did this change from queue->rcv_state to queue->state? Doesn't look like enum nvmet_tcp_recv_state is the correct type for queue->state either.

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

Quoted 5 lines
drivers/nvme/target/tcp.c +5 -0
+       /*
+        * 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);

Acquire ordering prevents memory operations that come *after* from being reordered *before*. It does not prevent earlier operations (such as the load of queue->state) from being reordered after the acquire load. Additionally, an acquire must pair with a release store *on the same value* to have any effect. But the release store is to queue->rcv_state, not queue->cmd.

Correct uses of release-acquire ordering generally look something like this:
Thread 1:
Non-atomic store to A
Release-ordering store to B

Thread 2:
Acquire-ordering load from B
Non-atomic load from A

This ensures that if thread 2 observes the new value thread 1 stored in B, it will also observe the new value in A.

Quoted 18 lines
drivers/nvme/target/tcp.c +2 -2
        struct nvme_sgl_desc *sgl;
        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;

drivers/nvme/target/tcp.c static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue) · line 858

Quoted 8 lines
drivers/nvme/target/tcp.c +3 -2
@@ -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);

Is this also needed in the other places updating queue->rcv_state and queue->cmd, e.g. nvmet_tcp_handle_h2c_data_pdu()?

Best,
Caleb

Quoted 45 lines
drivers/nvme/target/tcp.c +0 -1
 }
 
 static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue)
--

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

#3 lore raw

Hi Caleb

Thanks for the review. I'll resend patch after testing.

Quoted 13 lines · Caleb Sander Mateos
Quoted 12 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().

Fixes bdaf13279192 ("nvmet-tcp: fix a segmentation fault during io parsing error")
---
 drivers/nvme/target/tcp.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Quoted 13 lines

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

Quoted 9 lines
drivers/nvme/target/tcp.c +1 -0
index 7c51c2a8c109..4021468c8857 100644
@@ -571,10 +571,16 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
        struct nvmet_tcp_cmd *cmd =
                container_of(req, struct nvmet_tcp_cmd, req);
        struct nvmet_tcp_queue  *queue = cmd->queue;
+       enum nvmet_tcp_recv_state queue_state = READ_ONCE(queue->state);

Why did this change from queue->rcv_state to queue->state? Doesn't look like enum nvmet_tcp_recv_state is the correct type for queue->state either.

it was by mistake. should be queue->rcv_state.

Quoted 19 lines

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

Quoted 6 lines
drivers/nvme/target/tcp.c +5 -0
+       /*
+        * 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);

Acquire ordering prevents memory operations that come *after* from being reordered *before*. It does not prevent earlier operations (such as the load of queue->state) from being reordered after the acquire load. Additionally, an acquire must pair with a release store *on the same value* to have any effect. But the release store is to queue->rcv_state, not queue->cmd.

Correct uses of release-acquire ordering generally look something like this:
Thread 1:
Non-atomic store to A
Release-ordering store to B

Quoted 6 lines

Thread 2:
Acquire-ordering load from B
Non-atomic load from A

This ensures that if thread 2 observes the new value thread 1 stored in B, it will also observe the new value in A.

Thanks for noticed that.

Quoted 22 lines
Quoted 22 lines
drivers/nvme/target/tcp.c +2 -2
        struct nvme_sgl_desc *sgl;
        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);
Quoted 8 lines

drivers/nvme/target/tcp.c static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue) · line 858

Quoted 5 lines
drivers/nvme/target/tcp.c +3 -2
-       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);

Is this also needed in the other places updating queue->rcv_state and queue->cmd, e.g. nvmet_tcp_handle_h2c_data_pdu()?

nvmet_tcp_handle_h2c_data_pdu() doesn't set rcv_state to NVMET_TCP_RECV_PDU. the other context wont exit early in nvmet_tcp_queue_response() so I don't think we need it.

Quoted 49 lines

Best,
Caleb

Quoted 45 lines
drivers/nvme/target/tcp.c +0 -1
 }
 
 static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue)
--

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

#4 lore raw
Quoted 90 lines · Meir Elisha

Hi Caleb

Thanks for the review. I'll resend patch after testing.

Quoted 13 lines · Caleb Sander Mateos
Quoted 12 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().

Fixes bdaf13279192 ("nvmet-tcp: fix a segmentation fault during io parsing error")
---
 drivers/nvme/target/tcp.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Quoted 13 lines

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

Quoted 9 lines
drivers/nvme/target/tcp.c +1 -0
index 7c51c2a8c109..4021468c8857 100644
@@ -571,10 +571,16 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
        struct nvmet_tcp_cmd *cmd =
                container_of(req, struct nvmet_tcp_cmd, req);
        struct nvmet_tcp_queue  *queue = cmd->queue;
+       enum nvmet_tcp_recv_state queue_state = READ_ONCE(queue->state);

Why did this change from queue->rcv_state to queue->state? Doesn't look like enum nvmet_tcp_recv_state is the correct type for queue->state either.

it was by mistake. should be queue->rcv_state.

Quoted 19 lines

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

Quoted 6 lines
drivers/nvme/target/tcp.c +5 -0
+       /*
+        * 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);

Acquire ordering prevents memory operations that come *after* from being reordered *before*. It does not prevent earlier operations (such as the load of queue->state) from being reordered after the acquire load. Additionally, an acquire must pair with a release store *on the same value* to have any effect. But the release store is to queue->rcv_state, not queue->cmd.

Correct uses of release-acquire ordering generally look something like this:
Thread 1:
Non-atomic store to A
Release-ordering store to B

Quoted 6 lines

Thread 2:
Acquire-ordering load from B
Non-atomic load from A

This ensures that if thread 2 observes the new value thread 1 stored in B, it will also observe the new value in A.

Thanks for noticed that.

Quoted 22 lines
Quoted 22 lines
drivers/nvme/target/tcp.c +2 -2
        struct nvme_sgl_desc *sgl;
        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);
Quoted 8 lines

drivers/nvme/target/tcp.c static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue) · line 858

Quoted 5 lines
drivers/nvme/target/tcp.c +3 -2
-       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);

Is this also needed in the other places updating queue->rcv_state and queue->cmd, e.g. nvmet_tcp_handle_h2c_data_pdu()?

Quoted 3 lines

nvmet_tcp_handle_h2c_data_pdu() doesn't set rcv_state to NVMET_TCP_RECV_PDU. the other context wont exit early in nvmet_tcp_queue_response() so I don't think we need it.

Okay. Even if the memory ordering is not required, all stores to queue->cmd and queue->rcv_state should probably at least be using WRITE_ONCE() since those values may be concurrently read by nvmet_tcp_queue_response() on another thread. But that's a pre-existing issue, so no need to fix it in this patch.

Best,
Caleb