Skip to content

Commit b182687

Browse files
Dongli Zhangmstsirkin
authored andcommitted
vhost-scsi: Fix vhost_scsi_send_bad_target()
Although the support of VIRTIO_F_ANY_LAYOUT + VIRTIO_F_VERSION_1 was signaled by the commit 664ed90 ("vhost/scsi: Set VIRTIO_F_ANY_LAYOUT + VIRTIO_F_VERSION_1 feature bits"), vhost_scsi_send_bad_target() still assumes the response in a single descriptor. In addition, although vhost_scsi_send_bad_target() is used by both I/O queue and control queue, the response header is always virtio_scsi_cmd_resp. It is required to use virtio_scsi_ctrl_tmf_resp or virtio_scsi_ctrl_an_resp for control queue. Fixes: 664ed90 ("vhost/scsi: Set VIRTIO_F_ANY_LAYOUT + VIRTIO_F_VERSION_1 feature bits") Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com> Acked-by: Jason Wang <jasowang@redhat.com> Reviewed-by: Mike Christie <michael.christie@oracle.com> Message-Id: <20250403063028.16045-3-dongli.zhang@oracle.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent f591cf9 commit b182687

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

drivers/vhost/scsi.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,23 +1015,46 @@ vhost_scsi_send_status(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
10151015
pr_err("Faulted on virtio_scsi_cmd_resp\n");
10161016
}
10171017

1018+
#define TYPE_IO_CMD 0
1019+
#define TYPE_CTRL_TMF 1
1020+
#define TYPE_CTRL_AN 2
1021+
10181022
static void
10191023
vhost_scsi_send_bad_target(struct vhost_scsi *vs,
10201024
struct vhost_virtqueue *vq,
1021-
int head, unsigned out)
1025+
struct vhost_scsi_ctx *vc, int type)
10221026
{
1023-
struct virtio_scsi_cmd_resp __user *resp;
1024-
struct virtio_scsi_cmd_resp rsp;
1027+
union {
1028+
struct virtio_scsi_cmd_resp cmd;
1029+
struct virtio_scsi_ctrl_tmf_resp tmf;
1030+
struct virtio_scsi_ctrl_an_resp an;
1031+
} rsp;
1032+
struct iov_iter iov_iter;
1033+
size_t rsp_size;
10251034
int ret;
10261035

10271036
memset(&rsp, 0, sizeof(rsp));
1028-
rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
1029-
resp = vq->iov[out].iov_base;
1030-
ret = __copy_to_user(resp, &rsp, sizeof(rsp));
1031-
if (!ret)
1032-
vhost_add_used_and_signal(&vs->dev, vq, head, 0);
1037+
1038+
if (type == TYPE_IO_CMD) {
1039+
rsp_size = sizeof(struct virtio_scsi_cmd_resp);
1040+
rsp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
1041+
} else if (type == TYPE_CTRL_TMF) {
1042+
rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
1043+
rsp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
1044+
} else {
1045+
rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
1046+
rsp.an.response = VIRTIO_SCSI_S_BAD_TARGET;
1047+
}
1048+
1049+
iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
1050+
rsp_size);
1051+
1052+
ret = copy_to_iter(&rsp, rsp_size, &iov_iter);
1053+
1054+
if (likely(ret == rsp_size))
1055+
vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
10331056
else
1034-
pr_err("Faulted on virtio_scsi_cmd_resp\n");
1057+
pr_err("Faulted on virtio scsi type=%d\n", type);
10351058
}
10361059

10371060
static int
@@ -1395,7 +1418,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
13951418
if (ret == -ENXIO)
13961419
break;
13971420
else if (ret == -EIO)
1398-
vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out);
1421+
vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD);
13991422
else if (ret == -ENOMEM)
14001423
vhost_scsi_send_status(vs, vq, vc.head, vc.out,
14011424
SAM_STAT_TASK_SET_FULL);
@@ -1631,7 +1654,10 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
16311654
if (ret == -ENXIO)
16321655
break;
16331656
else if (ret == -EIO)
1634-
vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out);
1657+
vhost_scsi_send_bad_target(vs, vq, &vc,
1658+
v_req.type == VIRTIO_SCSI_T_TMF ?
1659+
TYPE_CTRL_TMF :
1660+
TYPE_CTRL_AN);
16351661
} while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
16361662
out:
16371663
mutex_unlock(&vq->mutex);

0 commit comments

Comments
 (0)