nvme-fc: improve memory usage in nvme_fc_rcv_ls_req()
authorChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Sun, 2 Oct 2022 09:59:45 +0000 (11:59 +0200)
committerChristoph Hellwig <hch@lst.de>
Tue, 15 Nov 2022 09:50:30 +0000 (10:50 +0100)
sizeof( struct nvmefc_ls_rcv_op ) = 64
sizeof( union nvmefc_ls_requests ) = 1024
sizeof( union nvmefc_ls_responses ) = 128

So, in nvme_fc_rcv_ls_req(), 1216 bytes of memory are requested when
kzalloc() is called.

Because of the way memory allocations are performed, 2048 bytes are
allocated. So about 800 bytes are wasted for each request.

Switch to 3 distinct memory allocations, in order to:
   - save these 800 bytes
   - avoid zeroing this extra memory
   - make sure that memory is properly aligned in case of DMA access
    ("fc_dma_map_single(lsop->rspbuf)" just a few lines below)

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Reviewed-by: James Smart <jsmart2021@gmail.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/host/fc.c

index 5d57a04..2d3c548 100644 (file)
@@ -1475,6 +1475,8 @@ nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
        fc_dma_unmap_single(lport->dev, lsop->rspdma,
                        sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
 
+       kfree(lsop->rspbuf);
+       kfree(lsop->rqstbuf);
        kfree(lsop);
 
        nvme_fc_rport_put(rport);
@@ -1751,20 +1753,17 @@ nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
                goto out_put;
        }
 
-       lsop = kzalloc(sizeof(*lsop) +
-                       sizeof(union nvmefc_ls_requests) +
-                       sizeof(union nvmefc_ls_responses),
-                       GFP_KERNEL);
-       if (!lsop) {
+       lsop = kzalloc(sizeof(*lsop), GFP_KERNEL);
+       lsop->rqstbuf = kzalloc(sizeof(*lsop->rqstbuf), GFP_KERNEL);
+       lsop->rspbuf = kzalloc(sizeof(*lsop->rspbuf), GFP_KERNEL);
+       if (!lsop || !lsop->rqstbuf || !lsop->rspbuf) {
                dev_info(lport->dev,
                        "RCV %s LS failed: No memory\n",
                        (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
                                nvmefc_ls_names[w0->ls_cmd] : "");
                ret = -ENOMEM;
-               goto out_put;
+               goto out_free;
        }
-       lsop->rqstbuf = (union nvmefc_ls_requests *)&lsop[1];
-       lsop->rspbuf = (union nvmefc_ls_responses *)&lsop->rqstbuf[1];
 
        lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
                                        sizeof(*lsop->rspbuf),
@@ -1801,6 +1800,8 @@ out_unmap:
        fc_dma_unmap_single(lport->dev, lsop->rspdma,
                        sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
 out_free:
+       kfree(lsop->rspbuf);
+       kfree(lsop->rqstbuf);
        kfree(lsop);
 out_put:
        nvme_fc_rport_put(rport);