scsi: esp: use sg helper to iterate over scatterlist
authorMing Lei <ming.lei@redhat.com>
Thu, 6 Jun 2019 08:34:10 +0000 (16:34 +0800)
committerMartin K. Petersen <martin.petersen@oracle.com>
Thu, 20 Jun 2019 19:21:33 +0000 (15:21 -0400)
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>
drivers/scsi/esp_scsi.c
drivers/scsi/esp_scsi.h

index 76fd02ccbf49138fe78b0526b06e7ecd6331da87..1e3ea9dc74d2d29fdded547b9869873473def3a4 100644 (file)
@@ -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++;
index aa87a6b72dccdd4d26f28e827716dc5b84af2f9c..91b32f2a1a1b62148c8edfeb3865ebcecdadd92b 100644 (file)
 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;