Skip to content

Commit bb617b9

Browse files
committed
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Pull virtio/vhost fixes and cleanups from Michael Tsirkin: "Fixes and cleanups all over the place" * tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost: vhost/scsi: Use copy_to_iter() to send control queue response vhost: return EINVAL if iovecs size does not match the message size virtio-balloon: tweak config_changed implementation virtio: don't allocate vqs when names[i] = NULL virtio_pci: use queue idx instead of array idx to set up the vq virtio: document virtio_config_ops restrictions virtio: fix virtio_config_ops description
2 parents 1be969f + 8e5dadf commit bb617b9

File tree

9 files changed

+126
-60
lines changed

9 files changed

+126
-60
lines changed

drivers/misc/mic/vop/vop_main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,21 @@ static int vop_find_vqs(struct virtio_device *dev, unsigned nvqs,
394394
struct _vop_vdev *vdev = to_vopvdev(dev);
395395
struct vop_device *vpdev = vdev->vpdev;
396396
struct mic_device_ctrl __iomem *dc = vdev->dc;
397-
int i, err, retry;
397+
int i, err, retry, queue_idx = 0;
398398

399399
/* We must have this many virtqueues. */
400400
if (nvqs > ioread8(&vdev->desc->num_vq))
401401
return -ENOENT;
402402

403403
for (i = 0; i < nvqs; ++i) {
404+
if (!names[i]) {
405+
vqs[i] = NULL;
406+
continue;
407+
}
408+
404409
dev_dbg(_vop_dev(vdev), "%s: %d: %s\n",
405410
__func__, i, names[i]);
406-
vqs[i] = vop_find_vq(dev, i, callbacks[i], names[i],
411+
vqs[i] = vop_find_vq(dev, queue_idx++, callbacks[i], names[i],
407412
ctx ? ctx[i] : false);
408413
if (IS_ERR(vqs[i])) {
409414
err = PTR_ERR(vqs[i]);

drivers/remoteproc/remoteproc_virtio.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,15 @@ static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
153153
const bool * ctx,
154154
struct irq_affinity *desc)
155155
{
156-
int i, ret;
156+
int i, ret, queue_idx = 0;
157157

158158
for (i = 0; i < nvqs; ++i) {
159-
vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i],
159+
if (!names[i]) {
160+
vqs[i] = NULL;
161+
continue;
162+
}
163+
164+
vqs[i] = rp_find_vq(vdev, queue_idx++, callbacks[i], names[i],
160165
ctx ? ctx[i] : false);
161166
if (IS_ERR(vqs[i])) {
162167
ret = PTR_ERR(vqs[i]);

drivers/s390/virtio/virtio_ccw.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,16 +635,22 @@ static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
635635
{
636636
struct virtio_ccw_device *vcdev = to_vc_device(vdev);
637637
unsigned long *indicatorp = NULL;
638-
int ret, i;
638+
int ret, i, queue_idx = 0;
639639
struct ccw1 *ccw;
640640

641641
ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
642642
if (!ccw)
643643
return -ENOMEM;
644644

645645
for (i = 0; i < nvqs; ++i) {
646-
vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
647-
ctx ? ctx[i] : false, ccw);
646+
if (!names[i]) {
647+
vqs[i] = NULL;
648+
continue;
649+
}
650+
651+
vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
652+
names[i], ctx ? ctx[i] : false,
653+
ccw);
648654
if (IS_ERR(vqs[i])) {
649655
ret = PTR_ERR(vqs[i]);
650656
vqs[i] = NULL;

drivers/vhost/scsi.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,16 +1127,18 @@ vhost_scsi_send_tmf_reject(struct vhost_scsi *vs,
11271127
struct vhost_virtqueue *vq,
11281128
struct vhost_scsi_ctx *vc)
11291129
{
1130-
struct virtio_scsi_ctrl_tmf_resp __user *resp;
11311130
struct virtio_scsi_ctrl_tmf_resp rsp;
1131+
struct iov_iter iov_iter;
11321132
int ret;
11331133

11341134
pr_debug("%s\n", __func__);
11351135
memset(&rsp, 0, sizeof(rsp));
11361136
rsp.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
1137-
resp = vq->iov[vc->out].iov_base;
1138-
ret = __copy_to_user(resp, &rsp, sizeof(rsp));
1139-
if (!ret)
1137+
1138+
iov_iter_init(&iov_iter, READ, &vq->iov[vc->out], vc->in, sizeof(rsp));
1139+
1140+
ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1141+
if (likely(ret == sizeof(rsp)))
11401142
vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
11411143
else
11421144
pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n");
@@ -1147,16 +1149,18 @@ vhost_scsi_send_an_resp(struct vhost_scsi *vs,
11471149
struct vhost_virtqueue *vq,
11481150
struct vhost_scsi_ctx *vc)
11491151
{
1150-
struct virtio_scsi_ctrl_an_resp __user *resp;
11511152
struct virtio_scsi_ctrl_an_resp rsp;
1153+
struct iov_iter iov_iter;
11521154
int ret;
11531155

11541156
pr_debug("%s\n", __func__);
11551157
memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */
11561158
rsp.response = VIRTIO_SCSI_S_OK;
1157-
resp = vq->iov[vc->out].iov_base;
1158-
ret = __copy_to_user(resp, &rsp, sizeof(rsp));
1159-
if (!ret)
1159+
1160+
iov_iter_init(&iov_iter, READ, &vq->iov[vc->out], vc->in, sizeof(rsp));
1161+
1162+
ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1163+
if (likely(ret == sizeof(rsp)))
11601164
vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
11611165
else
11621166
pr_err("Faulted on virtio_scsi_ctrl_an_resp\n");

drivers/vhost/vhost.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,10 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
10341034
int type, ret;
10351035

10361036
ret = copy_from_iter(&type, sizeof(type), from);
1037-
if (ret != sizeof(type))
1037+
if (ret != sizeof(type)) {
1038+
ret = -EINVAL;
10381039
goto done;
1040+
}
10391041

10401042
switch (type) {
10411043
case VHOST_IOTLB_MSG:
@@ -1054,8 +1056,10 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
10541056

10551057
iov_iter_advance(from, offset);
10561058
ret = copy_from_iter(&msg, sizeof(msg), from);
1057-
if (ret != sizeof(msg))
1059+
if (ret != sizeof(msg)) {
1060+
ret = -EINVAL;
10581061
goto done;
1062+
}
10591063
if (vhost_process_iotlb_msg(dev, &msg)) {
10601064
ret = -EFAULT;
10611065
goto done;

drivers/virtio/virtio_balloon.c

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ enum virtio_balloon_vq {
6161
VIRTIO_BALLOON_VQ_MAX
6262
};
6363

64+
enum virtio_balloon_config_read {
65+
VIRTIO_BALLOON_CONFIG_READ_CMD_ID = 0,
66+
};
67+
6468
struct virtio_balloon {
6569
struct virtio_device *vdev;
6670
struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq;
@@ -77,14 +81,20 @@ struct virtio_balloon {
7781
/* Prevent updating balloon when it is being canceled. */
7882
spinlock_t stop_update_lock;
7983
bool stop_update;
84+
/* Bitmap to indicate if reading the related config fields are needed */
85+
unsigned long config_read_bitmap;
8086

8187
/* The list of allocated free pages, waiting to be given back to mm */
8288
struct list_head free_page_list;
8389
spinlock_t free_page_list_lock;
8490
/* The number of free page blocks on the above list */
8591
unsigned long num_free_page_blocks;
86-
/* The cmd id received from host */
87-
u32 cmd_id_received;
92+
/*
93+
* The cmd id received from host.
94+
* Read it via virtio_balloon_cmd_id_received to get the latest value
95+
* sent from host.
96+
*/
97+
u32 cmd_id_received_cache;
8898
/* The cmd id that is actively in use */
8999
__virtio32 cmd_id_active;
90100
/* Buffer to store the stop sign */
@@ -390,37 +400,31 @@ static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
390400
return num_returned;
391401
}
392402

403+
static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
404+
{
405+
if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
406+
return;
407+
408+
/* No need to queue the work if the bit was already set. */
409+
if (test_and_set_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
410+
&vb->config_read_bitmap))
411+
return;
412+
413+
queue_work(vb->balloon_wq, &vb->report_free_page_work);
414+
}
415+
393416
static void virtballoon_changed(struct virtio_device *vdev)
394417
{
395418
struct virtio_balloon *vb = vdev->priv;
396419
unsigned long flags;
397-
s64 diff = towards_target(vb);
398-
399-
if (diff) {
400-
spin_lock_irqsave(&vb->stop_update_lock, flags);
401-
if (!vb->stop_update)
402-
queue_work(system_freezable_wq,
403-
&vb->update_balloon_size_work);
404-
spin_unlock_irqrestore(&vb->stop_update_lock, flags);
405-
}
406420

407-
if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
408-
virtio_cread(vdev, struct virtio_balloon_config,
409-
free_page_report_cmd_id, &vb->cmd_id_received);
410-
if (vb->cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) {
411-
/* Pass ULONG_MAX to give back all the free pages */
412-
return_free_pages_to_mm(vb, ULONG_MAX);
413-
} else if (vb->cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP &&
414-
vb->cmd_id_received !=
415-
virtio32_to_cpu(vdev, vb->cmd_id_active)) {
416-
spin_lock_irqsave(&vb->stop_update_lock, flags);
417-
if (!vb->stop_update) {
418-
queue_work(vb->balloon_wq,
419-
&vb->report_free_page_work);
420-
}
421-
spin_unlock_irqrestore(&vb->stop_update_lock, flags);
422-
}
421+
spin_lock_irqsave(&vb->stop_update_lock, flags);
422+
if (!vb->stop_update) {
423+
queue_work(system_freezable_wq,
424+
&vb->update_balloon_size_work);
425+
virtio_balloon_queue_free_page_work(vb);
423426
}
427+
spin_unlock_irqrestore(&vb->stop_update_lock, flags);
424428
}
425429

426430
static void update_balloon_size(struct virtio_balloon *vb)
@@ -527,6 +531,17 @@ static int init_vqs(struct virtio_balloon *vb)
527531
return 0;
528532
}
529533

534+
static u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb)
535+
{
536+
if (test_and_clear_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
537+
&vb->config_read_bitmap))
538+
virtio_cread(vb->vdev, struct virtio_balloon_config,
539+
free_page_report_cmd_id,
540+
&vb->cmd_id_received_cache);
541+
542+
return vb->cmd_id_received_cache;
543+
}
544+
530545
static int send_cmd_id_start(struct virtio_balloon *vb)
531546
{
532547
struct scatterlist sg;
@@ -537,7 +552,8 @@ static int send_cmd_id_start(struct virtio_balloon *vb)
537552
while (virtqueue_get_buf(vq, &unused))
538553
;
539554

540-
vb->cmd_id_active = cpu_to_virtio32(vb->vdev, vb->cmd_id_received);
555+
vb->cmd_id_active = virtio32_to_cpu(vb->vdev,
556+
virtio_balloon_cmd_id_received(vb));
541557
sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active));
542558
err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL);
543559
if (!err)
@@ -620,7 +636,8 @@ static int send_free_pages(struct virtio_balloon *vb)
620636
* stop the reporting.
621637
*/
622638
cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active);
623-
if (cmd_id_active != vb->cmd_id_received)
639+
if (unlikely(cmd_id_active !=
640+
virtio_balloon_cmd_id_received(vb)))
624641
break;
625642

626643
/*
@@ -637,11 +654,9 @@ static int send_free_pages(struct virtio_balloon *vb)
637654
return 0;
638655
}
639656

640-
static void report_free_page_func(struct work_struct *work)
657+
static void virtio_balloon_report_free_page(struct virtio_balloon *vb)
641658
{
642659
int err;
643-
struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
644-
report_free_page_work);
645660
struct device *dev = &vb->vdev->dev;
646661

647662
/* Start by sending the received cmd id to host with an outbuf. */
@@ -659,6 +674,23 @@ static void report_free_page_func(struct work_struct *work)
659674
dev_err(dev, "Failed to send a stop id, err = %d\n", err);
660675
}
661676

677+
static void report_free_page_func(struct work_struct *work)
678+
{
679+
struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
680+
report_free_page_work);
681+
u32 cmd_id_received;
682+
683+
cmd_id_received = virtio_balloon_cmd_id_received(vb);
684+
if (cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) {
685+
/* Pass ULONG_MAX to give back all the free pages */
686+
return_free_pages_to_mm(vb, ULONG_MAX);
687+
} else if (cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP &&
688+
cmd_id_received !=
689+
virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) {
690+
virtio_balloon_report_free_page(vb);
691+
}
692+
}
693+
662694
#ifdef CONFIG_BALLOON_COMPACTION
663695
/*
664696
* virtballoon_migratepage - perform the balloon page migration on behalf of
@@ -885,7 +917,7 @@ static int virtballoon_probe(struct virtio_device *vdev)
885917
goto out_del_vqs;
886918
}
887919
INIT_WORK(&vb->report_free_page_work, report_free_page_func);
888-
vb->cmd_id_received = VIRTIO_BALLOON_CMD_ID_STOP;
920+
vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP;
889921
vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
890922
VIRTIO_BALLOON_CMD_ID_STOP);
891923
vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,

drivers/virtio/virtio_mmio.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,20 @@ static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
468468
{
469469
struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
470470
unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
471-
int i, err;
471+
int i, err, queue_idx = 0;
472472

473473
err = request_irq(irq, vm_interrupt, IRQF_SHARED,
474474
dev_name(&vdev->dev), vm_dev);
475475
if (err)
476476
return err;
477477

478478
for (i = 0; i < nvqs; ++i) {
479-
vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i],
479+
if (!names[i]) {
480+
vqs[i] = NULL;
481+
continue;
482+
}
483+
484+
vqs[i] = vm_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
480485
ctx ? ctx[i] : false);
481486
if (IS_ERR(vqs[i])) {
482487
vm_del_vqs(vdev);

drivers/virtio/virtio_pci_common.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
285285
{
286286
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
287287
u16 msix_vec;
288-
int i, err, nvectors, allocated_vectors;
288+
int i, err, nvectors, allocated_vectors, queue_idx = 0;
289289

290290
vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
291291
if (!vp_dev->vqs)
@@ -321,7 +321,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
321321
msix_vec = allocated_vectors++;
322322
else
323323
msix_vec = VP_MSIX_VQ_VECTOR;
324-
vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i],
324+
vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
325325
ctx ? ctx[i] : false,
326326
msix_vec);
327327
if (IS_ERR(vqs[i])) {
@@ -356,7 +356,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
356356
const char * const names[], const bool *ctx)
357357
{
358358
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
359-
int i, err;
359+
int i, err, queue_idx = 0;
360360

361361
vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
362362
if (!vp_dev->vqs)
@@ -374,7 +374,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
374374
vqs[i] = NULL;
375375
continue;
376376
}
377-
vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i],
377+
vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
378378
ctx ? ctx[i] : false,
379379
VIRTIO_MSI_NO_VECTOR);
380380
if (IS_ERR(vqs[i])) {

0 commit comments

Comments
 (0)