Skip to contentSkip to conversation

nvme-tcp: fix connect failure on receiving partial ICResp PDU

PATCH Superseded Caleb Sander Mateos 2 participants 3 messages Jan 24, 2025 NVMe

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 nvme-tcp: fix connect failure on receiving partial ICResp PDU

nvme_tcp_init_connection() attempts to receive an ICResp PDU but only checks that the return value from recvmsg() is non-negative. If the sender closes the TCP connection or sends fewer than 128 bytes, this check will pass even though the full PDU wasn't received.

Ensure the full ICResp PDU is received by checking that recvmsg() returns the expected 128 bytes.

Additionally set the MSG_WAITALL flag for recvmsg(), as a sender could split the ICResp over multiple TCP frames. Without MSG_WAITALL, recvmsg() could return prematurely with only part of the PDU.

Fixes 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver")

---
v3: fix return value to indicate error
v2: add Fixes tag

 drivers/nvme/host/tcp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/host/tcp.c +3 -1
index dc5bbca58c6d..8cd1735772c6 100644
@@ -1447,15 +1447,17 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
 	iov.iov_len = sizeof(*icresp);
 	if (nvme_tcp_queue_tls(queue)) {
 		msg.msg_control = cbuf;
 		msg.msg_controllen = sizeof(cbuf);
 	}
+	msg.msg_flags = MSG_WAITALL;
 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
 			iov.iov_len, msg.msg_flags);
-	if (ret < 0) {
+	if (ret < sizeof(*icresp)) {
 		pr_warn("queue %d: failed to receive icresp, error %d\n",
 			nvme_tcp_queue_id(queue), ret);
+		ret = -ECONNRESET;
 		goto free_icresp;
 	}
 	ret = -ENOTCONN;
 	if (nvme_tcp_queue_tls(queue)) {
 		ctype = tls_get_record_type(queue->sock->sk,
Signature · 1 line
2.45.2
maintainer
#1 lore raw

drivers/nvme/host/tcp.c static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) · line 1458

Quoted 8 lines · Caleb Sander Mateos
drivers/nvme/host/tcp.c +3 -1
+	msg.msg_flags = MSG_WAITALL;
 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
 			iov.iov_len, msg.msg_flags);
-	if (ret < 0) {
+	if (ret < sizeof(*icresp)) {
 		pr_warn("queue %d: failed to receive icresp, error %d\n",
 			nvme_tcp_queue_id(queue), ret);
+		ret = -ECONNRESET;

If kernel_recvmsg() returns an error, don't we want to preserve and return that instead of unconditionally overriding to -ECONNRESET? I think the suggestion was to set ret to that only if ret >= 0.

#2 lore raw

Indeed. Time for v4...

Quoted 14 lines · Keith Busch

drivers/nvme/host/tcp.c static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) · line 1458

Quoted 8 lines · Caleb Sander Mateos
drivers/nvme/host/tcp.c +3 -1
+     msg.msg_flags = MSG_WAITALL;
      ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
                      iov.iov_len, msg.msg_flags);
-     if (ret < 0) {
+     if (ret < sizeof(*icresp)) {
              pr_warn("queue %d: failed to receive icresp, error %d\n",
                      nvme_tcp_queue_id(queue), ret);
+             ret = -ECONNRESET;

If kernel_recvmsg() returns an error, don't we want to preserve and return that instead of unconditionally overriding to -ECONNRESET? I think the suggestion was to set ret to that only if ret >= 0.