target/file: allocate the bvec array as part of struct target_core_file_cmd
authorChristoph Hellwig <hch@lst.de>
Sat, 9 Jan 2021 16:03:00 +0000 (16:03 +0000)
committerJens Axboe <axboe@kernel.dk>
Mon, 25 Jan 2021 15:58:24 +0000 (08:58 -0700)
This saves one memory allocation, and ensures the bvecs aren't freed
before the AIO completion.  This will allow the lower level code to be
optimized so that it can avoid allocating another bvec array.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/target/target_core_file.c

index b0cb5b9..cce4559 100644 (file)
@@ -241,6 +241,7 @@ struct target_core_file_cmd {
        unsigned long   len;
        struct se_cmd   *cmd;
        struct kiocb    iocb;
+       struct bio_vec  bvecs[];
 };
 
 static void cmd_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
@@ -268,29 +269,22 @@ fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
        struct target_core_file_cmd *aio_cmd;
        struct iov_iter iter = {};
        struct scatterlist *sg;
-       struct bio_vec *bvec;
        ssize_t len = 0;
        int ret = 0, i;
 
-       aio_cmd = kmalloc(sizeof(struct target_core_file_cmd), GFP_KERNEL);
+       aio_cmd = kmalloc(struct_size(aio_cmd, bvecs, sgl_nents), GFP_KERNEL);
        if (!aio_cmd)
                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-       bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
-       if (!bvec) {
-               kfree(aio_cmd);
-               return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
-       }
-
        for_each_sg(sgl, sg, sgl_nents, i) {
-               bvec[i].bv_page = sg_page(sg);
-               bvec[i].bv_len = sg->length;
-               bvec[i].bv_offset = sg->offset;
+               aio_cmd->bvecs[i].bv_page = sg_page(sg);
+               aio_cmd->bvecs[i].bv_len = sg->length;
+               aio_cmd->bvecs[i].bv_offset = sg->offset;
 
                len += sg->length;
        }
 
-       iov_iter_bvec(&iter, is_write, bvec, sgl_nents, len);
+       iov_iter_bvec(&iter, is_write, aio_cmd->bvecs, sgl_nents, len);
 
        aio_cmd->cmd = cmd;
        aio_cmd->len = len;
@@ -307,8 +301,6 @@ fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
        else
                ret = call_read_iter(file, &aio_cmd->iocb, &iter);
 
-       kfree(bvec);
-
        if (ret != -EIOCBQUEUED)
                cmd_rw_aio_complete(&aio_cmd->iocb, ret, 0);