Skip to contentSkip to conversation

nvmet-tcp: fix data digest calculation for multiple H2CData PDUs

PATCH Superseded Varun Prakash 2 participants 6 messages Aug 3, 2023 – Aug 13, 2023

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvmet-tcp: fix data digest calculation for multiple H2CData PDUs

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 iovec 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, it calculates data digest for the entire transfer length this caused another regression for multiple H2CData PDU per cmd case.

Fixes ed0691cf5514 ("nvmet-tcp: fix regression in data_digest calculation")

---

v2:
- used sg instead of iovec for data digest calculation
- updated commit message

 drivers/nvme/target/tcp.c | 42 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
drivers/nvme/target/tcp.c +39 -3
index cd92d7ddf5ed..4d72b4b2b205 100644
@@ -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_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_send_ddgst(queue->snd_hash, cmd);
 	}
 
 	if (cmd->queue->hdr_digest) {
@@ -1152,11 +1152,47 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
 	return nvmet_tcp_done_recv_pdu(queue);
 }
 
+static void nvmet_tcp_recv_ddgst(struct ahash_request *hash,
+		struct nvmet_tcp_cmd *cmd)
+{
+	struct scatterlist *sg = &cmd->req.sg[cmd->sg_idx];
+	struct bio_vec *iov = cmd->iov;
+	u32 data_length = cmd->pdu_len;
+
+	crypto_ahash_init(hash);
+
+	if (iov->bv_offset) {
+		struct scatterlist first_sg;
+
+		sg_init_table(&first_sg, 1);
+		sg_set_page(&first_sg, sg_page(sg), iov->bv_len,
+				iov->bv_offset);
+
+		data_length -= iov->bv_len;
+
+		if (data_length) {
+			ahash_request_set_crypt(hash, &first_sg, NULL,
+						iov->bv_len);
+			crypto_ahash_update(hash);
+			sg = sg_next(sg);
+		} else {
+			ahash_request_set_crypt(hash, &first_sg,
+					(void *)&cmd->exp_ddgst, iov->bv_len);
+			crypto_ahash_finup(hash);
+			return;
+		}
+	}
+
+	ahash_request_set_crypt(hash, sg, (void *)&cmd->exp_ddgst,
+				data_length);
+	crypto_ahash_finup(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_recv_ddgst(queue->rcv_hash, cmd);
 	queue->offset = 0;
 	queue->left = NVME_TCP_DIGEST_LENGTH;
 	queue->rcv_state = NVMET_TCP_RECV_DDGST;
Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 36 lines · Varun Prakash

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 iovec 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, it calculates data digest for the entire transfer length this caused another regression for multiple H2CData PDU per cmd case.

Fixes ed0691cf5514 ("nvmet-tcp: fix regression in data_digest calculation")

---

v2:
- used sg instead of iovec for data digest calculation
- updated commit message

  drivers/nvme/target/tcp.c | 42 ++++++++++++++++++++++++++++++++++++---
  1 file changed, 39 insertions(+), 3 deletions(-)
drivers/nvme/target/tcp.c +0 -0
index cd92d7ddf5ed..4d72b4b2b205 100644

drivers/nvme/target/tcp.c static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd) · line 419

Quoted 6 lines
drivers/nvme/target/tcp.c +1 -1
@@ -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_send_ddgst(struct ahash_request *hash,
  		struct nvmet_tcp_cmd *cmd)

We should call it nvmet_tcp_calc_send_ddgst

Quoted 12 lines
drivers/nvme/target/tcp.c +1 -1
  {
  	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_send_ddgst(queue->snd_hash, cmd);
  	}
 
  	if (cmd->queue->hdr_digest) {

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) · line 1156

Quoted 5 lines
drivers/nvme/target/tcp.c +2 -0
@@ -1152,11 +1152,47 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
  	return nvmet_tcp_done_recv_pdu(queue);
  }
 
+static void nvmet_tcp_recv_ddgst(struct ahash_request *hash,
+		struct nvmet_tcp_cmd *cmd)

We should call it nvmet_tcp_calc_recv_ddgst

Quoted 17 lines
drivers/nvme/target/tcp.c +17 -0
+{
+	struct scatterlist *sg = &cmd->req.sg[cmd->sg_idx];
+	struct bio_vec *iov = cmd->iov;
+	u32 data_length = cmd->pdu_len;
+
+	crypto_ahash_init(hash);
+
+	if (iov->bv_offset) {
+		struct scatterlist first_sg;
+
+		sg_init_table(&first_sg, 1);
+		sg_set_page(&first_sg, sg_page(sg), iov->bv_len,
+				iov->bv_offset);
+
+		data_length -= iov->bv_len;
+
+		if (data_length) {

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) · line 1183

Quoted 10 lines
drivers/nvme/target/tcp.c +10 -0
+			ahash_request_set_crypt(hash, &first_sg, NULL,
+						iov->bv_len);
+			crypto_ahash_update(hash);
+			sg = sg_next(sg);
+		} else {
+			ahash_request_set_crypt(hash, &first_sg,
+					(void *)&cmd->exp_ddgst, iov->bv_len);
+			crypto_ahash_finup(hash);
+			return;
+		}

What is the case for data_length=0 ? I'm not entirely clear how this can happen.

Quoted 16 lines
drivers/nvme/target/tcp.c +8 -1
+	}
+
+	ahash_request_set_crypt(hash, sg, (void *)&cmd->exp_ddgst,
+				data_length);
+	crypto_ahash_finup(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_recv_ddgst(queue->rcv_hash, cmd);
  	queue->offset = 0;
  	queue->left = NVME_TCP_DIGEST_LENGTH;
  	queue->rcv_state = NVMET_TCP_RECV_DDGST;
#2 lore raw
Quoted 23 lines
Quoted 23 lines
drivers/nvme/target/tcp.c +22 -0
+{
+	struct scatterlist *sg = &cmd->req.sg[cmd->sg_idx];
+	struct bio_vec *iov = cmd->iov;
+	u32 data_length = cmd->pdu_len;
+
+	crypto_ahash_init(hash);
+
+	if (iov->bv_offset) {
+		struct scatterlist first_sg;
+
+		sg_init_table(&first_sg, 1);
+		sg_set_page(&first_sg, sg_page(sg), iov->bv_len,
+				iov->bv_offset);
+
+		data_length -= iov->bv_len;
+
+		if (data_length) {
+			ahash_request_set_crypt(hash, &first_sg, NULL,
+						iov->bv_len);
+			crypto_ahash_update(hash);
+			sg = sg_next(sg);
+		} else {
Quoted 8 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) · line 1183

Quoted 5 lines
drivers/nvme/target/tcp.c +5 -0
+			ahash_request_set_crypt(hash, &first_sg,
+					(void *)&cmd->exp_ddgst, iov->bv_len);
+			crypto_ahash_finup(hash);
+			return;
+		}

What is the case for data_length=0 ? I'm not entirely clear how this can happen.

This can happen if H2CData PDU data length is not a multiple of target page size.

For example if transfer length is 32K, page size is 4K and H2CData PDU data length is 6K, host will send 6 H2CData PDUs, for the last PDU iov->bv_offset = 2K, iov->bv_len = 2K and data_length = 2K.

maintainer
#3 lore raw
Quoted 32 lines · Varun Prakash
Quoted 23 lines
Quoted 23 lines
drivers/nvme/target/tcp.c +22 -0
+{
+	struct scatterlist *sg = &cmd->req.sg[cmd->sg_idx];
+	struct bio_vec *iov = cmd->iov;
+	u32 data_length = cmd->pdu_len;
+
+	crypto_ahash_init(hash);
+
+	if (iov->bv_offset) {
+		struct scatterlist first_sg;
+
+		sg_init_table(&first_sg, 1);
+		sg_set_page(&first_sg, sg_page(sg), iov->bv_len,
+				iov->bv_offset);
+
+		data_length -= iov->bv_len;
+
+		if (data_length) {
+			ahash_request_set_crypt(hash, &first_sg, NULL,
+						iov->bv_len);
+			crypto_ahash_update(hash);
+			sg = sg_next(sg);
+		} else {
Quoted 8 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) · line 1183

Quoted 5 lines
drivers/nvme/target/tcp.c +5 -0
+			ahash_request_set_crypt(hash, &first_sg,
+					(void *)&cmd->exp_ddgst, iov->bv_len);
+			crypto_ahash_finup(hash);
+			return;
+		}

What is the case for data_length=0 ? I'm not entirely clear how this can happen.

Quoted 5 lines

This can happen if H2CData PDU data length is not a multiple of target page size.

For example if transfer length is 32K, page size is 4K and H2CData PDU data length is 6K, host will send 6 H2CData PDUs, for the last PDU iov->bv_offset = 2K, iov->bv_len = 2K and data_length = 2K.

I understand.

Looking into this, I'm almost thinking we should just give up and iterate. at this point it unnecessarily complicates the code...

#4 lore raw
Quoted 34 lines
Quoted 34 lines
Quoted 23 lines
Quoted 23 lines
drivers/nvme/target/tcp.c +22 -0
+{
+	struct scatterlist *sg = &cmd->req.sg[cmd->sg_idx];
+	struct bio_vec *iov = cmd->iov;
+	u32 data_length = cmd->pdu_len;
+
+	crypto_ahash_init(hash);
+
+	if (iov->bv_offset) {
+		struct scatterlist first_sg;
+
+		sg_init_table(&first_sg, 1);
+		sg_set_page(&first_sg, sg_page(sg), iov->bv_len,
+				iov->bv_offset);
+
+		data_length -= iov->bv_len;
+
+		if (data_length) {
+			ahash_request_set_crypt(hash, &first_sg, NULL,
+						iov->bv_len);
+			crypto_ahash_update(hash);
+			sg = sg_next(sg);
+		} else {
Quoted 8 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) · line 1183

Quoted 5 lines
drivers/nvme/target/tcp.c +5 -0
+			ahash_request_set_crypt(hash, &first_sg,
+					(void *)&cmd->exp_ddgst, iov->bv_len);
+			crypto_ahash_finup(hash);
+			return;
+		}

What is the case for data_length=0 ? I'm not entirely clear how this can happen.

This can happen if H2CData PDU data length is not a multiple of target page size.

Quoted 8 lines
Quoted 3 lines

For example if transfer length is 32K, page size is 4K and H2CData PDU data length is 6K, host will send 6 H2CData PDUs, for the last PDU iov->bv_offset = 2K, iov->bv_len = 2K and data_length = 2K.

I understand.

Looking into this, I'm almost thinking we should just give up and iterate. at this point it unnecessarily complicates the code...

Should I resend v1?

maintainer
#5 lore raw
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

What is the case for data_length=0 ? I'm not entirely clear how this can happen.

This can happen if H2CData PDU data length is not a multiple of target page size.

For example if transfer length is 32K, page size is 4K and H2CData PDU data length is 6K, host will send 6 H2CData PDUs, for the last PDU iov->bv_offset = 2K, iov->bv_len = 2K and data_length = 2K.

Quoted 6 lines
Quoted 4 lines

I understand.

Looking into this, I'm almost thinking we should just give up and iterate. at this point it unnecessarily complicates the code...

Should I resend v1?

Yes please.