Skip to contentSkip to conversation

target/rd: reduce code duplication in rd_execute_rw()

PATCH In review Akinobu Mita 3 participants 9 messages Apr 5, 2015 – Apr 9, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 target/rd: reduce code duplication in rd_execute_rw()

Factor out code duplication in rd_execute_rw() into a helper function rd_do_prot_rw(). This change is required to minimize the forthcoming fix in rd_do_prot_rw().

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* v2
- Pass dif_verify() function pointer to helper function instead of is_write, suggested by Sagi Grimberg.

 drivers/target/target_core_rd.c | 66 ++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 34 deletions(-)
drivers/target/target_core_rd.c +32 -34
index 98e83ac..ac5e8d2 100644
@@ -382,6 +382,36 @@ static struct rd_dev_sg_table *rd_get_prot_table(struct rd_dev *rd_dev, u32 page
 	return NULL;
 }
 
+typedef sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned int,
+				     unsigned int, struct scatterlist *, int);
+
+static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
+{
+	struct se_device *se_dev = cmd->se_dev;
+	struct rd_dev *dev = RD_DEV(se_dev);
+	struct rd_dev_sg_table *prot_table;
+	struct scatterlist *prot_sg;
+	u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
+	u32 prot_offset, prot_page;
+	u64 tmp;
+	sense_reason_t rc;
+
+	tmp = cmd->t_task_lba * se_dev->prot_length;
+	prot_offset = do_div(tmp, PAGE_SIZE);
+	prot_page = tmp;
+
+	prot_table = rd_get_prot_table(dev, prot_page);
+	if (!prot_table)
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+	prot_sg = &prot_table->sg_table[prot_page -
+					prot_table->page_start_offset];
+
+	rc = dif_verify(cmd, cmd->t_task_lba, sectors, 0, prot_sg, prot_offset);
+
+	return rc;
+}
+
 static sense_reason_t
 rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 	      enum dma_data_direction data_direction)
@@ -420,23 +450,7 @@ rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 			cmd->t_task_lba, rd_size, rd_page, rd_offset);
 
 	if (cmd->prot_type && data_direction == DMA_TO_DEVICE) {
-		struct rd_dev_sg_table *prot_table;
-		struct scatterlist *prot_sg;
-		u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-		u32 prot_offset, prot_page;
-
-		tmp = cmd->t_task_lba * se_dev->prot_length;
-		prot_offset = do_div(tmp, PAGE_SIZE);
-		prot_page = tmp;
-
-		prot_table = rd_get_prot_table(dev, prot_page);
-		if (!prot_table)
-			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
-
-		prot_sg = &prot_table->sg_table[prot_page - prot_table->page_start_offset];
-
-		rc = sbc_dif_verify_write(cmd, cmd->t_task_lba, sectors, 0,
-					  prot_sg, prot_offset);
+		rc = rd_do_prot_rw(cmd, sbc_dif_verify_write);
 		if (rc)
 			return rc;
 	}
@@ -503,23 +517,7 @@ rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 	sg_miter_stop(&m);
 
 	if (cmd->prot_type && data_direction == DMA_FROM_DEVICE) {
-		struct rd_dev_sg_table *prot_table;
-		struct scatterlist *prot_sg;
-		u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-		u32 prot_offset, prot_page;
-
-		tmp = cmd->t_task_lba * se_dev->prot_length;
-		prot_offset = do_div(tmp, PAGE_SIZE);
-		prot_page = tmp;
-
-		prot_table = rd_get_prot_table(dev, prot_page);
-		if (!prot_table)
-			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
-
-		prot_sg = &prot_table->sg_table[prot_page - prot_table->page_start_offset];
-
-		rc = sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors, 0,
-					 prot_sg, prot_offset);
+		rc = rd_do_prot_rw(cmd, sbc_dif_verify_read);
 		if (rc)
 			return rc;
 	}
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 2/2 target/rd: Don't pass imcomplete scatterlist entries to sbc_dif_verify_*

The scatterlist for protection information which is passed to sbc_dif_verify_read() or sbc_dif_verify_write() requires that neighboring scatterlist entries are contiguous or chained so that they can be iterated by sg_next().

However, the protection information for RD-MCP backends could be located in the multiple scatterlist arrays when the ramdisk space is too large. So if the read/write request straddles this boundary, sbc_dif_verify_read() or sbc_dif_verify_write() can't iterate all scatterlist entries.

This fixes it by allocating temporary scatterlist if it is needed.

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* No change from v1

 drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +35 -4
index ac5e8d2..6e25eaa 100644
@@ -390,11 +390,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
 	struct se_device *se_dev = cmd->se_dev;
 	struct rd_dev *dev = RD_DEV(se_dev);
 	struct rd_dev_sg_table *prot_table;
+	bool need_to_release = false;
 	struct scatterlist *prot_sg;
 	u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-	u32 prot_offset, prot_page;
+	u32 prot_offset, prot_page, prot_npages;
 	u64 tmp;
-	sense_reason_t rc;
+	sense_reason_t rc = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
 	tmp = cmd->t_task_lba * se_dev->prot_length;
 	prot_offset = do_div(tmp, PAGE_SIZE);
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
 	if (!prot_table)
 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-	prot_sg = &prot_table->sg_table[prot_page -
-					prot_table->page_start_offset];
+	prot_npages = DIV_ROUND_UP(prot_offset + sectors * se_dev->prot_length,
+				   PAGE_SIZE);
+
+	/* prot pages straddles multiple scatterlist tables */
+	if (prot_table->page_end_offset < prot_page + prot_npages - 1) {
+		int i;
+
+		prot_sg = kcalloc(prot_npages, sizeof(*prot_sg), GFP_KERNEL);
+		if (!prot_sg)
+			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+		need_to_release = true;
+		sg_init_table(prot_sg, prot_npages);
+
+		for (i = 0; i < prot_npages; i++) {
+			if (prot_page + i > prot_table->page_end_offset) {
+				prot_table = rd_get_prot_table(dev,
+								prot_page + i);
+				if (!prot_table)
+					goto out;
+				sg_unmark_end(&prot_sg[i - 1]);
+			}
+			prot_sg[i] = prot_table->sg_table[prot_page + i -
+						prot_table->page_start_offset];
+		}
+	} else {
+		prot_sg = &prot_table->sg_table[prot_page -
+						prot_table->page_start_offset];
+	}
 
 	rc = dif_verify(cmd, cmd->t_task_lba, sectors, 0, prot_sg, prot_offset);
+out:
+	if (need_to_release)
+		kfree(prot_sg);
 
 	return rc;
 }
Signature · 1 line
1.9.1
Acked-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] target/rd: Don't pass imcomplete scatterlist entries to sbc_dif_verify_*

Quoted 79 lines · Akinobu Mita

The scatterlist for protection information which is passed to sbc_dif_verify_read() or sbc_dif_verify_write() requires that neighboring scatterlist entries are contiguous or chained so that they can be iterated by sg_next().

However, the protection information for RD-MCP backends could be located in the multiple scatterlist arrays when the ramdisk space is too large. So if the read/write request straddles this boundary, sbc_dif_verify_read() or sbc_dif_verify_write() can't iterate all scatterlist entries.

This fixes it by allocating temporary scatterlist if it is needed.

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* No change from v1

  drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
  1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +30 -4
index ac5e8d2..6e25eaa 100644
@@ -390,11 +390,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
  	struct se_device *se_dev = cmd->se_dev;
  	struct rd_dev *dev = RD_DEV(se_dev);
  	struct rd_dev_sg_table *prot_table;
+	bool need_to_release = false;
  	struct scatterlist *prot_sg;
  	u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-	u32 prot_offset, prot_page;
+	u32 prot_offset, prot_page, prot_npages;
  	u64 tmp;
-	sense_reason_t rc;
+	sense_reason_t rc = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
  	tmp = cmd->t_task_lba * se_dev->prot_length;
  	prot_offset = do_div(tmp, PAGE_SIZE);
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
  	if (!prot_table)
  		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-	prot_sg = &prot_table->sg_table[prot_page -
-					prot_table->page_start_offset];
+	prot_npages = DIV_ROUND_UP(prot_offset + sectors * se_dev->prot_length,
+				   PAGE_SIZE);
+
+	/* prot pages straddles multiple scatterlist tables */
+	if (prot_table->page_end_offset < prot_page + prot_npages - 1) {
+		int i;
+
+		prot_sg = kcalloc(prot_npages, sizeof(*prot_sg), GFP_KERNEL);
+		if (!prot_sg)
+			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+		need_to_release = true;
+		sg_init_table(prot_sg, prot_npages);
+
+		for (i = 0; i < prot_npages; i++) {
+			if (prot_page + i > prot_table->page_end_offset) {
+				prot_table = rd_get_prot_table(dev,
+								prot_page + i);
+				if (!prot_table)
+					goto out;
+				sg_unmark_end(&prot_sg[i - 1]);
+			}
+			prot_sg[i] = prot_table->sg_table[prot_page + i -
+						prot_table->page_start_offset];
+		}
+	} else {
+		prot_sg = &prot_table->sg_table[prot_page -

drivers/target/target_core_rd.c static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify) · line 444

Quoted 10 lines
drivers/target/target_core_rd.c +5 -0
+						prot_table->page_start_offset];
+	}
 
  	rc = dif_verify(cmd, cmd->t_task_lba, sectors, 0, prot_sg, prot_offset);
+out:
+	if (need_to_release)
+		kfree(prot_sg);
 
  	return rc;
  }
#3 lore raw

Re: [PATCH v2 2/2] target/rd: Don't pass imcomplete scatterlist entries to sbc_dif_verify_*

In reply to Akinobu Mita
Quoted 71 lines · Akinobu Mita

The scatterlist for protection information which is passed to sbc_dif_verify_read() or sbc_dif_verify_write() requires that neighboring scatterlist entries are contiguous or chained so that they can be iterated by sg_next().

However, the protection information for RD-MCP backends could be located in the multiple scatterlist arrays when the ramdisk space is too large. So if the read/write request straddles this boundary, sbc_dif_verify_read() or sbc_dif_verify_write() can't iterate all scatterlist entries.

This fixes it by allocating temporary scatterlist if it is needed.

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* No change from v1

 drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +23 -4
index ac5e8d2..6e25eaa 100644
@@ -390,11 +390,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
 	struct se_device *se_dev = cmd->se_dev;
 	struct rd_dev *dev = RD_DEV(se_dev);
 	struct rd_dev_sg_table *prot_table;
+	bool need_to_release = false;
 	struct scatterlist *prot_sg;
 	u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-	u32 prot_offset, prot_page;
+	u32 prot_offset, prot_page, prot_npages;
 	u64 tmp;
-	sense_reason_t rc;
+	sense_reason_t rc = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
 	tmp = cmd->t_task_lba * se_dev->prot_length;
 	prot_offset = do_div(tmp, PAGE_SIZE);
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
 	if (!prot_table)
 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-	prot_sg = &prot_table->sg_table[prot_page -
-					prot_table->page_start_offset];
+	prot_npages = DIV_ROUND_UP(prot_offset + sectors * se_dev->prot_length,
+				   PAGE_SIZE);
+
+	/* prot pages straddles multiple scatterlist tables */
+	if (prot_table->page_end_offset < prot_page + prot_npages - 1) {
+		int i;
+
+		prot_sg = kcalloc(prot_npages, sizeof(*prot_sg), GFP_KERNEL);
+		if (!prot_sg)
+			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+		need_to_release = true;
+		sg_init_table(prot_sg, prot_npages);
+
+		for (i = 0; i < prot_npages; i++) {
+			if (prot_page + i > prot_table->page_end_offset) {
+				prot_table = rd_get_prot_table(dev,
+								prot_page + i);
+				if (!prot_table)
+					goto out;

drivers/target/target_core_rd.c static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify) · line 437

Quoted 10 lines
drivers/target/target_core_rd.c +9 -0
+				sg_unmark_end(&prot_sg[i - 1]);
+			}
+			prot_sg[i] = prot_table->sg_table[prot_page + i -
+						prot_table->page_start_offset];
+		}
+	} else {
+		prot_sg = &prot_table->sg_table[prot_page -
+						prot_table->page_start_offset];
+	}
 

Mmm, how about just explicitly doing sg_link() at prot_table scatterlist creation time instead..?

That would save the extra allocation above, and AFAICT make for simpler code.

--nab

#4 lore raw

Re: [PATCH v2 2/2] target/rd: Don't pass imcomplete scatterlist entries to sbc_dif_verify_*

2015-04-08 14:13 GMT+09:00 Nicholas A. Bellinger <[email protected]>:

Quoted 77 lines · Akinobu Mita
Quoted 76 lines

The scatterlist for protection information which is passed to sbc_dif_verify_read() or sbc_dif_verify_write() requires that neighboring scatterlist entries are contiguous or chained so that they can be iterated by sg_next().

However, the protection information for RD-MCP backends could be located in the multiple scatterlist arrays when the ramdisk space is too large. So if the read/write request straddles this boundary, sbc_dif_verify_read() or sbc_dif_verify_write() can't iterate all scatterlist entries.

This fixes it by allocating temporary scatterlist if it is needed.

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* No change from v1

 drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +28 -4
index ac5e8d2..6e25eaa 100644
@@ -390,11 +390,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
      struct se_device *se_dev = cmd->se_dev;
      struct rd_dev *dev = RD_DEV(se_dev);
      struct rd_dev_sg_table *prot_table;
+     bool need_to_release = false;
      struct scatterlist *prot_sg;
      u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-     u32 prot_offset, prot_page;
+     u32 prot_offset, prot_page, prot_npages;
      u64 tmp;
-     sense_reason_t rc;
+     sense_reason_t rc = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
      tmp = cmd->t_task_lba * se_dev->prot_length;
      prot_offset = do_div(tmp, PAGE_SIZE);
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
      if (!prot_table)
              return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-     prot_sg = &prot_table->sg_table[prot_page -
-                                     prot_table->page_start_offset];
+     prot_npages = DIV_ROUND_UP(prot_offset + sectors * se_dev->prot_length,
+                                PAGE_SIZE);
+
+     /* prot pages straddles multiple scatterlist tables */
+     if (prot_table->page_end_offset < prot_page + prot_npages - 1) {
+             int i;
+
+             prot_sg = kcalloc(prot_npages, sizeof(*prot_sg), GFP_KERNEL);
+             if (!prot_sg)
+                     return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+             need_to_release = true;
+             sg_init_table(prot_sg, prot_npages);
+
+             for (i = 0; i < prot_npages; i++) {
+                     if (prot_page + i > prot_table->page_end_offset) {
+                             prot_table = rd_get_prot_table(dev,
+                                                             prot_page + i);
+                             if (!prot_table)
+                                     goto out;
+                             sg_unmark_end(&prot_sg[i - 1]);
+                     }
+                     prot_sg[i] = prot_table->sg_table[prot_page + i -
+                                             prot_table->page_start_offset];
+             }
Quoted 8 lines

drivers/target/target_core_rd.c static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify) · line 437

Quoted 5 lines
drivers/target/target_core_rd.c +4 -0
+     } else {
+             prot_sg = &prot_table->sg_table[prot_page -
+                                             prot_table->page_start_offset];
+     }
 

Mmm, how about just explicitly doing sg_link() at prot_table scatterlist creation time instead..?

Yeap, that would be an optimal solution. But some architectures don't support sg chaining (i.e. !CONFIG_ARCH_HAS_SG_CHAIN).

Quoted 2 lines

That would save the extra allocation above, and AFAICT make for simpler code.

Do you prefer fixing it conditionally with using #ifdef ? (i.e. sg chaining at creating time if CONFIG_ARCH_HAS_SG_CHAIN is defined, otherwise do the extra allocation above) If so, I'll resubmit the patch with such changes.

#5 lore raw

Re: [PATCH v2 2/2] target/rd: Don't pass imcomplete scatterlist entries to sbc_dif_verify_*

Quoted 90 lines · Akinobu Mita

2015-04-08 14:13 GMT+09:00 Nicholas A. Bellinger <[email protected]>:

Quoted 77 lines · Akinobu Mita
Quoted 76 lines

The scatterlist for protection information which is passed to sbc_dif_verify_read() or sbc_dif_verify_write() requires that neighboring scatterlist entries are contiguous or chained so that they can be iterated by sg_next().

However, the protection information for RD-MCP backends could be located in the multiple scatterlist arrays when the ramdisk space is too large. So if the read/write request straddles this boundary, sbc_dif_verify_read() or sbc_dif_verify_write() can't iterate all scatterlist entries.

This fixes it by allocating temporary scatterlist if it is needed.

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* No change from v1

 drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +28 -4
index ac5e8d2..6e25eaa 100644
@@ -390,11 +390,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
      struct se_device *se_dev = cmd->se_dev;
      struct rd_dev *dev = RD_DEV(se_dev);
      struct rd_dev_sg_table *prot_table;
+     bool need_to_release = false;
      struct scatterlist *prot_sg;
      u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-     u32 prot_offset, prot_page;
+     u32 prot_offset, prot_page, prot_npages;
      u64 tmp;
-     sense_reason_t rc;
+     sense_reason_t rc = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
      tmp = cmd->t_task_lba * se_dev->prot_length;
      prot_offset = do_div(tmp, PAGE_SIZE);
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
      if (!prot_table)
              return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-     prot_sg = &prot_table->sg_table[prot_page -
-                                     prot_table->page_start_offset];
+     prot_npages = DIV_ROUND_UP(prot_offset + sectors * se_dev->prot_length,
+                                PAGE_SIZE);
+
+     /* prot pages straddles multiple scatterlist tables */
+     if (prot_table->page_end_offset < prot_page + prot_npages - 1) {
+             int i;
+
+             prot_sg = kcalloc(prot_npages, sizeof(*prot_sg), GFP_KERNEL);
+             if (!prot_sg)
+                     return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+             need_to_release = true;
+             sg_init_table(prot_sg, prot_npages);
+
+             for (i = 0; i < prot_npages; i++) {
+                     if (prot_page + i > prot_table->page_end_offset) {
+                             prot_table = rd_get_prot_table(dev,
+                                                             prot_page + i);
+                             if (!prot_table)
+                                     goto out;
+                             sg_unmark_end(&prot_sg[i - 1]);
+                     }
+                     prot_sg[i] = prot_table->sg_table[prot_page + i -
+                                             prot_table->page_start_offset];
+             }
Quoted 8 lines

drivers/target/target_core_rd.c static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify) · line 437

Quoted 5 lines
drivers/target/target_core_rd.c +4 -0
+     } else {
+             prot_sg = &prot_table->sg_table[prot_page -
+                                             prot_table->page_start_offset];
+     }
 

Mmm, how about just explicitly doing sg_link() at prot_table scatterlist creation time instead..?

Yeap, that would be an optimal solution. But some architectures don't support sg chaining (i.e. !CONFIG_ARCH_HAS_SG_CHAIN).

Quoted 7 lines
Quoted 2 lines

That would save the extra allocation above, and AFAICT make for simpler code.

Do you prefer fixing it conditionally with using #ifdef ? (i.e. sg chaining at creating time if CONFIG_ARCH_HAS_SG_CHAIN is defined, otherwise do the extra allocation above) If so, I'll resubmit the patch with such changes.

Yes please.

It would be nice to use sg_link() for the typical case when available, as long as the #idefs aren't too ugly and limited to target_core_rd.c code.

--nab

#6 lore raw
In reply to Akinobu Mita
Quoted 46 lines · Akinobu Mita

Factor out code duplication in rd_execute_rw() into a helper function rd_do_prot_rw(). This change is required to minimize the forthcoming fix in rd_do_prot_rw().

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* v2
- Pass dif_verify() function pointer to helper function instead of is_write, suggested by Sagi Grimberg.

  drivers/target/target_core_rd.c | 66 ++++++++++++++++++++---------------------
  1 file changed, 32 insertions(+), 34 deletions(-)
drivers/target/target_core_rd.c +18 -0
index 98e83ac..ac5e8d2 100644
@@ -382,6 +382,36 @@ static struct rd_dev_sg_table *rd_get_prot_table(struct rd_dev *rd_dev, u32 page
  	return NULL;
  }
 
+typedef sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned int,
+				     unsigned int, struct scatterlist *, int);
+
+static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify dif_verify)
+{
+	struct se_device *se_dev = cmd->se_dev;
+	struct rd_dev *dev = RD_DEV(se_dev);
+	struct rd_dev_sg_table *prot_table;
+	struct scatterlist *prot_sg;
+	u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
+	u32 prot_offset, prot_page;
+	u64 tmp;
+	sense_reason_t rc;
+
+	tmp = cmd->t_task_lba * se_dev->prot_length;
+	prot_offset = do_div(tmp, PAGE_SIZE);
+	prot_page = tmp;
+

drivers/target/target_core_rd.c static struct rd_dev_sg_table *rd_get_prot_table(struct rd_dev *rd_dev, u32 page · line 412

Quoted 10 lines
drivers/target/target_core_rd.c +10 -0
+	prot_table = rd_get_prot_table(dev, prot_page);
+	if (!prot_table)
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+	prot_sg = &prot_table->sg_table[prot_page -
+					prot_table->page_start_offset];
+
+	rc = dif_verify(cmd, cmd->t_task_lba, sectors, 0, prot_sg, prot_offset);
+
+	return rc;

Nit, Given there is no action on the returned rc, this can be reduced to:

	return dif_verify();
Quoted 56 lines
drivers/target/target_core_rd.c +4 -34
+}
+
  static sense_reason_t
  rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  	      enum dma_data_direction data_direction)
@@ -420,23 +450,7 @@ rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  			cmd->t_task_lba, rd_size, rd_page, rd_offset);
 
  	if (cmd->prot_type && data_direction == DMA_TO_DEVICE) {
-		struct rd_dev_sg_table *prot_table;
-		struct scatterlist *prot_sg;
-		u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-		u32 prot_offset, prot_page;
-
-		tmp = cmd->t_task_lba * se_dev->prot_length;
-		prot_offset = do_div(tmp, PAGE_SIZE);
-		prot_page = tmp;
-
-		prot_table = rd_get_prot_table(dev, prot_page);
-		if (!prot_table)
-			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
-
-		prot_sg = &prot_table->sg_table[prot_page - prot_table->page_start_offset];
-
-		rc = sbc_dif_verify_write(cmd, cmd->t_task_lba, sectors, 0,
-					  prot_sg, prot_offset);
+		rc = rd_do_prot_rw(cmd, sbc_dif_verify_write);
  		if (rc)
  			return rc;
  	}
@@ -503,23 +517,7 @@ rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  	sg_miter_stop(&m);
 
  	if (cmd->prot_type && data_direction == DMA_FROM_DEVICE) {
-		struct rd_dev_sg_table *prot_table;
-		struct scatterlist *prot_sg;
-		u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
-		u32 prot_offset, prot_page;
-
-		tmp = cmd->t_task_lba * se_dev->prot_length;
-		prot_offset = do_div(tmp, PAGE_SIZE);
-		prot_page = tmp;
-
-		prot_table = rd_get_prot_table(dev, prot_page);
-		if (!prot_table)
-			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
-
-		prot_sg = &prot_table->sg_table[prot_page - prot_table->page_start_offset];
-
-		rc = sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors, 0,
-					 prot_sg, prot_offset);
+		rc = rd_do_prot_rw(cmd, sbc_dif_verify_read);
  		if (rc)
  			return rc;
  	}
#7 lore raw

2015-04-06 16:43 GMT+09:00 Sagi Grimberg <[email protected]>:

Quoted 69 lines · Akinobu Mita
Quoted 63 lines

Factor out code duplication in rd_execute_rw() into a helper function rd_do_prot_rw(). This change is required to minimize the forthcoming fix in rd_do_prot_rw().

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>

---
* v2
- Pass dif_verify() function pointer to helper function instead of is_write,
suggested by Sagi Grimberg.

  drivers/target/target_core_rd.c | 66
++++++++++++++++++++---------------------
  1 file changed, 32 insertions(+), 34 deletions(-)
drivers/target/target_core_rd.c +4 -0
index 98e83ac..ac5e8d2 100644
@@ -382,6 +382,36 @@ static struct rd_dev_sg_table
 *rd_get_prot_table(struct rd_dev *rd_dev, u32 page
        return NULL;
  }
 
+typedef sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned
 int,
+                                    unsigned int, struct scatterlist *,
 int);
+
+static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, dif_verify

dif_verify)

drivers/target/target_core_rd.c +22 -0
+{
+       struct se_device *se_dev = cmd->se_dev;
+       struct rd_dev *dev = RD_DEV(se_dev);
+       struct rd_dev_sg_table *prot_table;
+       struct scatterlist *prot_sg;
+       u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
+       u32 prot_offset, prot_page;
+       u64 tmp;
+       sense_reason_t rc;
+
+       tmp = cmd->t_task_lba * se_dev->prot_length;
+       prot_offset = do_div(tmp, PAGE_SIZE);
+       prot_page = tmp;
+
+       prot_table = rd_get_prot_table(dev, prot_page);
+       if (!prot_table)
+               return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+       prot_sg = &prot_table->sg_table[prot_page -
+                                       prot_table->page_start_offset];
+
+       rc = dif_verify(cmd, cmd->t_task_lba, sectors, 0, prot_sg,

prot_offset);

drivers/target/target_core_rd.c

Quoted 2 lines
drivers/target/target_core_rd.c +2 -0
+
+       return rc;

Nit, Given there is no action on the returned rc, this can be reduced to:

Quoted 1 line
        return dif_verify();

You are right, but the patch 2/2 inserts the lines to release temporary prot_sg before return statement, so assignment and return statements will be both required in the end.

#8 lore raw
In reply to Akinobu Mita
Quoted 12 lines · Akinobu Mita

Factor out code duplication in rd_execute_rw() into a helper function rd_do_prot_rw(). This change is required to minimize the forthcoming fix in rd_do_prot_rw().

Cc Nicholas Bellinger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Quoted 7 lines

---
* v2
- Pass dif_verify() function pointer to helper function instead of is_write, suggested by Sagi Grimberg.

 drivers/target/target_core_rd.c | 66 ++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 34 deletions(-)

Nice patch. Applied to for-next with a bit of fuzz due to the WRITE_STRIP + READ_INSERT related changes already queued up..

Thanks Akinobu!

--nab