Skip to content

Commit fd47976

Browse files
mikechristiemstsirkin
authored andcommitted
vhost-scsi: Allocate iov_iter used for unaligned copies when needed
It's extremely rare that we get unaligned requests that need to drop down to the data copy code path. However, the iov_iter is almost 5% of the mem used for the vhost_scsi_cmd. This patch has us allocate the iov_iter only when needed since it's not a perf path that uses the struct. This along with the patches that removed the duplicated fields on the vhost_scsd_cmd allow us to reduce mem use by 1 MB in mid size setups where we have 16 virtqueues and are doing 1024 cmds per queue. Signed-off-by: Mike Christie <michael.christie@oracle.com> Message-Id: <20241203191705.19431-8-michael.christie@oracle.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
1 parent ddc5b5f commit fd47976

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

drivers/vhost/scsi.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ struct vhost_scsi_cmd {
112112
u32 tvc_sgl_count;
113113
u32 tvc_prot_sgl_count;
114114
u32 copied_iov:1;
115-
const void *saved_iter_addr;
116-
struct iov_iter saved_iter;
115+
const void *read_iov;
116+
struct iov_iter *read_iter;
117117
struct scatterlist *sgl;
118118
struct sg_table table;
119119
struct scatterlist *prot_sgl;
@@ -378,7 +378,8 @@ static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
378378
else
379379
put_page(page);
380380
}
381-
kfree(tv_cmd->saved_iter_addr);
381+
kfree(tv_cmd->read_iter);
382+
kfree(tv_cmd->read_iov);
382383
sg_free_table_chained(&tv_cmd->table, vs->inline_sg_cnt);
383384
}
384385
if (tv_cmd->tvc_prot_sgl_count) {
@@ -576,7 +577,7 @@ static void vhost_scsi_evt_work(struct vhost_work *work)
576577

577578
static int vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd *cmd)
578579
{
579-
struct iov_iter *iter = &cmd->saved_iter;
580+
struct iov_iter *iter = cmd->read_iter;
580581
struct scatterlist *sg;
581582
struct page *page;
582583
size_t len;
@@ -624,7 +625,7 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
624625
cmd, se_cmd->residual_count, se_cmd->scsi_status);
625626
memset(&v_rsp, 0, sizeof(v_rsp));
626627

627-
if (cmd->saved_iter_addr && vhost_scsi_copy_sgl_to_iov(cmd)) {
628+
if (cmd->read_iter && vhost_scsi_copy_sgl_to_iov(cmd)) {
628629
v_rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
629630
} else {
630631
v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq,
@@ -808,10 +809,15 @@ vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
808809
int i, ret;
809810

810811
if (data_dir == DMA_FROM_DEVICE) {
811-
cmd->saved_iter_addr = dup_iter(&cmd->saved_iter, iter,
812-
GFP_KERNEL);
813-
if (!cmd->saved_iter_addr)
812+
cmd->read_iter = kzalloc(sizeof(*cmd->read_iter), GFP_KERNEL);
813+
if (!cmd->read_iter)
814814
return -ENOMEM;
815+
816+
cmd->read_iov = dup_iter(cmd->read_iter, iter, GFP_KERNEL);
817+
if (!cmd->read_iov) {
818+
ret = -ENOMEM;
819+
goto free_iter;
820+
}
815821
}
816822

817823
for_each_sgtable_sg(sg_table, sg, i) {
@@ -845,7 +851,9 @@ vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
845851
if (page)
846852
__free_page(page);
847853
}
848-
kfree(cmd->saved_iter_addr);
854+
kfree(cmd->read_iov);
855+
free_iter:
856+
kfree(cmd->read_iter);
849857
return ret;
850858
}
851859

0 commit comments

Comments
 (0)