Skip to content

Commit 36a0fcb

Browse files
mikechristieBrian Maly
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 <[email protected]> Message-Id: <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]> Acked-by: Stefan Hajnoczi <[email protected]> (cherry picked from commit fd47976581333765964f4ce0ea01176fa1e646d5) Orabug: 37980690 Signed-off-by: Mike Christie <[email protected]> Reviewed-by: Dongli Zhang <[email protected]> Signed-off-by: Brian Maly <[email protected]>
1 parent 79cdb13 commit 36a0fcb

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;
@@ -388,7 +388,8 @@ static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
388388
else
389389
put_page(page);
390390
}
391-
kfree(tv_cmd->saved_iter_addr);
391+
kfree(tv_cmd->read_iter);
392+
kfree(tv_cmd->read_iov);
392393
sg_free_table_chained(&tv_cmd->table, vs->inline_sg_cnt);
393394
}
394395
if (tv_cmd->tvc_prot_sgl_count) {
@@ -589,7 +590,7 @@ static void vhost_scsi_evt_work(struct vhost_work *work)
589590

590591
static int vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd *cmd)
591592
{
592-
struct iov_iter *iter = &cmd->saved_iter;
593+
struct iov_iter *iter = cmd->read_iter;
593594
struct scatterlist *sg;
594595
struct page *page;
595596
size_t len;
@@ -637,7 +638,7 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
637638
cmd, se_cmd->residual_count, se_cmd->scsi_status);
638639
memset(&v_rsp, 0, sizeof(v_rsp));
639640

640-
if (cmd->saved_iter_addr && vhost_scsi_copy_sgl_to_iov(cmd)) {
641+
if (cmd->read_iter && vhost_scsi_copy_sgl_to_iov(cmd)) {
641642
v_rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
642643
} else {
643644
v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq,
@@ -827,10 +828,15 @@ vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
827828
int i, ret;
828829

829830
if (data_dir == DMA_FROM_DEVICE) {
830-
cmd->saved_iter_addr = dup_iter(&cmd->saved_iter, iter,
831-
GFP_KERNEL);
832-
if (!cmd->saved_iter_addr)
831+
cmd->read_iter = kzalloc(sizeof(*cmd->read_iter), GFP_KERNEL);
832+
if (!cmd->read_iter)
833833
return -ENOMEM;
834+
835+
cmd->read_iov = dup_iter(cmd->read_iter, iter, GFP_KERNEL);
836+
if (!cmd->read_iov) {
837+
ret = -ENOMEM;
838+
goto free_iter;
839+
}
834840
}
835841

836842
for_each_sgtable_sg(sg_table, sg, i) {
@@ -864,7 +870,9 @@ vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
864870
if (page)
865871
__free_page(page);
866872
}
867-
kfree(cmd->saved_iter_addr);
873+
kfree(cmd->read_iov);
874+
free_iter:
875+
kfree(cmd->read_iter);
868876
return ret;
869877
}
870878

0 commit comments

Comments
 (0)