[PATCH v4] nvmet-tcp: fix data digest calculation for multiple H2CData PDUs

Varun Prakash varun at chelsio.com
Tue Aug 15 04:42:00 PDT 2023


On receiving H2CData PDU current code calculates data digest for the entire
transfer length, it works if host sends only one H2CData PDU per cmd, if
host sends multiple H2CData PDU per cmd then target reports data digest
error.

This issue was fixed in commit fda871c0ba5d ("nvmet-tcp: fix receive data
digest calculation for multiple h2cdata PDUs").

commit bac04454ef9f ("nvmet-tcp: fix kmap leak when data digest in use")
unmaps sg page before calculating data digest and
commit 69b85e1f1d1d ("nvmet-tcp: add an helper to free the cmd buffers")
sets cmd->nr_mapped = 0 after unmapping iovec, both of these commits
caused regression in data digest calculation.

To fix this regression commit ed0691cf5514 ("nvmet-tcp: fix regression in
data_digest calculation") changed the logic to use sg directly instead of
iovec, this commit calculates data digest for the entire transfer length
so it caused another regression for multiple H2CData PDU per cmd case.

commit 5bfaba275ae6 ("nvmet-tcp: don't map pages which can't come from
HIGHMEM") removed sg page mapping code and added bio_vec so now nvmet-tcp
driver can use bio_vec for calculating data digest.

Fixes: ed0691cf5514 ("nvmet-tcp: fix regression in data_digest calculation")
Signed-off-by: Rakshana Sridhar <rakshanas at chelsio.com>
Signed-off-by: Varun Prakash <varun at chelsio.com>
---
 drivers/nvme/target/tcp.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c
index cd92d7ddf5ed..49cc96ace4a7 100644
--- a/drivers/nvme/target/tcp.c
+++ b/drivers/nvme/target/tcp.c
@@ -415,7 +415,7 @@ static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
 	return NVME_SC_INTERNAL;
 }
 
-static void nvmet_tcp_calc_ddgst(struct ahash_request *hash,
+static void nvmet_tcp_calc_send_ddgst(struct ahash_request *hash,
 		struct nvmet_tcp_cmd *cmd)
 {
 	ahash_request_set_crypt(hash, cmd->req.sg,
@@ -447,7 +447,7 @@ static void nvmet_setup_c2h_data_pdu(struct nvmet_tcp_cmd *cmd)
 
 	if (queue->data_digest) {
 		pdu->hdr.flags |= NVME_TCP_F_DDGST;
-		nvmet_tcp_calc_ddgst(queue->snd_hash, cmd);
+		nvmet_tcp_calc_send_ddgst(queue->snd_hash, cmd);
 	}
 
 	if (cmd->queue->hdr_digest) {
@@ -1152,11 +1152,34 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
 	return nvmet_tcp_done_recv_pdu(queue);
 }
 
+static void nvmet_tcp_calc_recv_ddgst(struct ahash_request *hash,
+		struct nvmet_tcp_cmd *cmd)
+{
+	struct bio_vec *iov = cmd->iov;
+	struct scatterlist sg;
+	u32 data_length = cmd->pdu_len;
+
+	sg_init_table(&sg, 1);
+	crypto_ahash_init(hash);
+
+	while (data_length) {
+		sg_set_page(&sg, iov->bv_page, iov->bv_len, iov->bv_offset);
+		ahash_request_set_crypt(hash, &sg, NULL, iov->bv_len);
+		crypto_ahash_update(hash);
+
+		data_length -= iov->bv_len;
+		iov++;
+	}
+
+	ahash_request_set_crypt(hash, NULL, (void *)&cmd->exp_ddgst, 0);
+	crypto_ahash_final(hash);
+}
+
 static void nvmet_tcp_prep_recv_ddgst(struct nvmet_tcp_cmd *cmd)
 {
 	struct nvmet_tcp_queue *queue = cmd->queue;
 
-	nvmet_tcp_calc_ddgst(queue->rcv_hash, cmd);
+	nvmet_tcp_calc_recv_ddgst(queue->rcv_hash, cmd);
 	queue->offset = 0;
 	queue->left = NVME_TCP_DIGEST_LENGTH;
 	queue->rcv_state = NVMET_TCP_RECV_DDGST;
-- 
2.27.0




More information about the Linux-nvme mailing list