Skip to contentSkip to conversation

target/rd: reduce code duplication in rd_execute_rw()

PATCH Superseded Akinobu Mita 2 participants 6 messages Apr 4, 2015 – Apr 5, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 Asias He <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
---
 drivers/target/target_core_rd.c | 66 ++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 34 deletions(-)
drivers/target/target_core_rd.c +32 -34
index 98e83ac..4d614c9 100644
@@ -382,6 +382,36 @@ static struct rd_dev_sg_table *rd_get_prot_table(struct rd_dev *rd_dev, u32 page
 	return NULL;
 }
 
+static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
+{
+	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;
+	sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned int,
+			unsigned int, struct scatterlist *, int) =
+		is_write ?  sbc_dif_verify_write : sbc_dif_verify_read;
+
+	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, true);
 		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, false);
 		if (rc)
 			return rc;
 	}
Signature · 1 line
1.9.1
#1 lore raw

PATCH 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 Asias He <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
---
 drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +35 -4
index 4d614c9..19c893d 100644
@@ -387,11 +387,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
 	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;
 	sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned int,
 			unsigned int, struct scatterlist *, int) =
 		is_write ?  sbc_dif_verify_write : sbc_dif_verify_read;
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
 	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
#4 lore raw
In reply to Akinobu Mita
Quoted 27 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 Asias He <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
---
  drivers/target/target_core_rd.c | 66 ++++++++++++++++++++---------------------
  1 file changed, 32 insertions(+), 34 deletions(-)
drivers/target/target_core_rd.c +3 -0
index 98e83ac..4d614c9 100644
@@ -382,6 +382,36 @@ static struct rd_dev_sg_table *rd_get_prot_table(struct rd_dev *rd_dev, u32 page
  	return NULL;
  }
 
+static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
+{
+	struct se_device *se_dev = cmd->se_dev;

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

Quoted 10 lines
drivers/target/target_core_rd.c +10 -0
+	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;
+	sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned int,
+			unsigned int, struct scatterlist *, int) =
+		is_write ?  sbc_dif_verify_write : sbc_dif_verify_read;

Hi Akinobu,

I think it would make more sense to pass the dif_verify() function pointer instead of is_write (only used for that anyway).

#2 lore raw

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

In reply to Akinobu Mita
Quoted 73 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 Asias He <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
---
  drivers/target/target_core_rd.c | 39 +++++++++++++++++++++++++++++++++++----
  1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +27 -4
index 4d614c9..19c893d 100644
@@ -387,11 +387,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
  	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;
  	sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned int,
  			unsigned int, struct scatterlist *, int) =
  		is_write ?  sbc_dif_verify_write : sbc_dif_verify_read;
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
  	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];

drivers/target/target_core_rd.c static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write) · line 441

Quoted 10 lines
drivers/target/target_core_rd.c +8 -0
+		}
+	} 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);

I think it is safe to free prot_sg if you just make sure to initialize it to NULL at declaration time, no need for 'need_to_release'.

#5 lore raw
In reply to Sagi Grimberg

2015-04-05 19:01 GMT+09:00 Sagi Grimberg <[email protected]>:

Quoted 47 lines · Akinobu Mita
Quoted 32 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 Asias He <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
---
  drivers/target/target_core_rd.c | 66
++++++++++++++++++++---------------------
  1 file changed, 32 insertions(+), 34 deletions(-)
drivers/target/target_core_rd.c +4 -0
index 98e83ac..4d614c9 100644
@@ -382,6 +382,36 @@ static struct rd_dev_sg_table
 *rd_get_prot_table(struct rd_dev *rd_dev, u32 page
        return NULL;
  }
 
+static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write)
+{
+       struct se_device *se_dev = cmd->se_dev;
+       struct rd_dev *dev = RD_DEV(se_dev);

drivers/target/target_core_rd.c static struct rd_dev_sg_table · line 399

Quoted 10 lines
drivers/target/target_core_rd.c +9 -0
+       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;
+       sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned
 int,
+                       unsigned int, struct scatterlist *, int) =
+               is_write ?  sbc_dif_verify_write : sbc_dif_verify_read;

Hi Akinobu,

Quoted 2 lines

I think it would make more sense to pass the dif_verify() function pointer instead of is_write (only used for that anyway).

Make sense. I'll update this series with that change.

#3 lore raw

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

In reply to Sagi Grimberg

2015-04-05 19:10 GMT+09:00 Sagi Grimberg <[email protected]>:

Quoted 92 lines · Akinobu Mita
Quoted 91 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 Asias He <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
---
  drivers/target/target_core_rd.c | 39
+++++++++++++++++++++++++++++++++++----
  1 file changed, 35 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +3 -2
index 4d614c9..19c893d 100644
@@ -387,11 +387,12 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd
 *cmd, bool is_write)
        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;
        sense_reason_t (*dif_verify)(struct se_cmd *, sector_t, unsigned
 int,

unsigned int, struct scatterlist *, int) =
is_write ? sbc_dif_verify_write : sbc_dif_verify_read;

drivers/target/target_core_rd.c +30 -2
@@ -404,10 +405,40 @@ static sense_reason_t rd_do_prot_rw(struct se_cmd
 *cmd, bool is_write)
        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];
+       }
Quoted 9 lines

drivers/target/target_core_rd.c static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_write) · line 441

Quoted 5 lines
        rc = dif_verify(cmd, cmd->t_task_lba, sectors, 0, prot_sg,

prot_offset);

drivers/target/target_core_rd.c +3 -0
+out:
+       if (need_to_release)
+               kfree(prot_sg);

I think it is safe to free prot_sg if you just make sure to initialize it to NULL at declaration time, no need for 'need_to_release'.

'need_to_release' is required to avoid kfree when 'prot_sg' is initialized to a pointer in the real prot space (i.e. "else" case above).