nvme-tcp: fix possible req->offset corruption
authorVarun Prakash <varun@chelsio.com>
Tue, 26 Oct 2021 13:31:55 +0000 (19:01 +0530)
committerChristoph Hellwig <hch@lst.de>
Wed, 27 Oct 2021 05:58:26 +0000 (07:58 +0200)
With commit db5ad6b7f8cd ("nvme-tcp: try to send request in queue_rq
context") r2t and response PDU can get processed while send function
is executing.

Current data digest send code uses req->offset after kernel_sendmsg(),
this creates a race condition where req->offset gets reset before it
is used in send function.

This can happen in two cases -
1. Target sends r2t PDU which resets req->offset.
2. Target send response PDU which completes the req and then req is
   used for a new command, nvme_tcp_setup_cmd_pdu() resets req->offset.

Fix this by storing req->offset in a local variable and using
this local variable after kernel_sendmsg().

Fixes: db5ad6b7f8cd ("nvme-tcp: try to send request in queue_rq context")
Signed-off-by: Varun Prakash <varun@chelsio.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/host/tcp.c

index 0626d14e6d4c017936d8eba6a7704e4f4919d7e9..1a209f0d7181ddc1c82275214d4af26b213ffacf 100644 (file)
@@ -1050,6 +1050,7 @@ static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
 {
        struct nvme_tcp_queue *queue = req->queue;
+       size_t offset = req->offset;
        int ret;
        struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
        struct kvec iov = {
@@ -1066,7 +1067,7 @@ static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
        if (unlikely(ret <= 0))
                return ret;
 
-       if (req->offset + ret == NVME_TCP_DIGEST_LENGTH) {
+       if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
                nvme_tcp_done_send_req(queue);
                return 1;
        }