scsi: esp: use sg helper to iterate over scatterlist [Linux 5.3]

This Linux kernel change "scsi: esp: use sg helper to iterate over scatterlist" is included in the Linux 5.3 release. This change is authored by Ming Lei <ming.lei [at] redhat.com> on Thu Jun 6 16:34:10 2019 +0800. The commit for this change in Linux stable tree is ee5a1db (patch).

scsi: esp: use sg helper to iterate over scatterlist

Unlike the legacy I/O path, scsi-mq preallocates a large array to hold
the scatterlist for each request. This static allocation can consume
substantial amounts of memory on modern controllers which support a
large number of concurrently outstanding requests.

To facilitate a switch to a smaller static allocation combined with a
dynamic allocation for requests that need it, we need to make sure all
SCSI drivers handle chained scatterlists correctly.

Convert remaining drivers that directly dereference the scatterlist
array to using the iterator functions.

[mkp: clarified commit message]

Cc: Christoph Hellwig <hch@lst.de>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Ewan D. Milne <emilne@redhat.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Finn Thain <fthain@telegraphics.com.au>
Cc: Guenter Roeck <linux@roeck-us.net>
Reported-by: Guenter Roeck <linux@roeck-us.net>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Finn Thain <fthain@telegraphics.com.au>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

There are 22 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/scsi/esp_scsi.c | 20 +++++++++++++-------
 drivers/scsi/esp_scsi.h |  2 ++
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/esp_scsi.c b/drivers/scsi/esp_scsi.c
index 76fd02c..1e3ea9d 100644
--- a/drivers/scsi/esp_scsi.c
+++ b/drivers/scsi/esp_scsi.c
@@ -370,6 +370,7 @@ static void esp_map_dma(struct esp *esp, struct scsi_cmnd *cmd)
    struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
    struct scatterlist *sg = scsi_sglist(cmd);
    int total = 0, i;
+   struct scatterlist *s;

    if (cmd->sc_data_direction == DMA_NONE)
        return;
@@ -380,16 +381,18 @@ static void esp_map_dma(struct esp *esp, struct scsi_cmnd *cmd)
         * a dma address, so perform an identity mapping.
         */
        spriv->num_sg = scsi_sg_count(cmd);
-       for (i = 0; i < spriv->num_sg; i++) {
-           sg[i].dma_address = (uintptr_t)sg_virt(&sg[i]);
-           total += sg_dma_len(&sg[i]);
+
+       scsi_for_each_sg(cmd, s, spriv->num_sg, i) {
+           s->dma_address = (uintptr_t)sg_virt(s);
+           total += sg_dma_len(s);
        }
    } else {
        spriv->num_sg = scsi_dma_map(cmd);
-       for (i = 0; i < spriv->num_sg; i++)
-           total += sg_dma_len(&sg[i]);
+       scsi_for_each_sg(cmd, s, spriv->num_sg, i)
+           total += sg_dma_len(s);
    }
    spriv->cur_residue = sg_dma_len(sg);
+   spriv->prv_sg = NULL;
    spriv->cur_sg = sg;
    spriv->tot_residue = total;
 }
@@ -443,7 +446,8 @@ static void esp_advance_dma(struct esp *esp, struct esp_cmd_entry *ent,
        p->tot_residue = 0;
    }
    if (!p->cur_residue && p->tot_residue) {
-       p->cur_sg++;
+       p->prv_sg = p->cur_sg;
+       p->cur_sg = sg_next(p->cur_sg);
        p->cur_residue = sg_dma_len(p->cur_sg);
    }
 }
@@ -464,6 +468,7 @@ static void esp_save_pointers(struct esp *esp, struct esp_cmd_entry *ent)
        return;
    }
    ent->saved_cur_residue = spriv->cur_residue;
+   ent->saved_prv_sg = spriv->prv_sg;
    ent->saved_cur_sg = spriv->cur_sg;
    ent->saved_tot_residue = spriv->tot_residue;
 }
@@ -478,6 +483,7 @@ static void esp_restore_pointers(struct esp *esp, struct esp_cmd_entry *ent)
        return;
    }
    spriv->cur_residue = ent->saved_cur_residue;
+   spriv->prv_sg = ent->saved_prv_sg;
    spriv->cur_sg = ent->saved_cur_sg;
    spriv->tot_residue = ent->saved_tot_residue;
 }
@@ -1646,7 +1652,7 @@ static int esp_msgin_process(struct esp *esp)
        spriv = ESP_CMD_PRIV(ent->cmd);

        if (spriv->cur_residue == sg_dma_len(spriv->cur_sg)) {
-           spriv->cur_sg--;
+           spriv->cur_sg = spriv->prv_sg;
            spriv->cur_residue = 1;
        } else
            spriv->cur_residue++;
diff --git a/drivers/scsi/esp_scsi.h b/drivers/scsi/esp_scsi.h
index aa87a6b..91b32f2 100644
--- a/drivers/scsi/esp_scsi.h
+++ b/drivers/scsi/esp_scsi.h
@@ -251,6 +251,7 @@
 struct esp_cmd_priv {
    int         num_sg;
    int         cur_residue;
+   struct scatterlist  *prv_sg;
    struct scatterlist  *cur_sg;
    int         tot_residue;
 };
@@ -273,6 +274,7 @@ struct esp_cmd_entry {
    struct scsi_cmnd    *cmd;

    unsigned int        saved_cur_residue;
+   struct scatterlist  *saved_prv_sg;
    struct scatterlist  *saved_cur_sg;
    unsigned int        saved_tot_residue;

Leave a Reply

Your email address will not be published. Required fields are marked *