Skip to content

Commit 06be302

Browse files
committed
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Pull virtio fixes from Michael Tsirkin: "Some last minute fixes that took a while to get ready. Not regressions, but they look safe and seem to be worth to have" * tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost: tools/virtio: handle fallout from folio work tools/virtio: fix virtio_test execution vhost: remove avail_event arg from vhost_update_avail_event() virtio: drop default for virtio-mem vdpa: fix use-after-free on vp_vdpa_remove virtio-blk: Remove BUG_ON() in virtio_queue_rq() virtio-blk: Don't use MAX_DISCARD_SEGMENTS if max_discard_seg is zero vhost: fix hung thread due to erroneous iotlb entries vduse: Fix returning wrong type in vduse_domain_alloc_iova() vdpa/mlx5: add validation for VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET command vdpa/mlx5: should verify CTRL_VQ feature exists for MQ vdpa: factor out vdpa_set_features_unlocked for vdpa internal use virtio_console: break out of buf poll on remove virtio: document virtio_reset_device virtio: acknowledge all features before access virtio: unexport virtio_finalize_features
2 parents aa6f8dc + 3dd7d13 commit 06be302

File tree

17 files changed

+127
-47
lines changed

17 files changed

+127
-47
lines changed

drivers/block/virtio_blk.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ struct virtio_blk {
7676
*/
7777
refcount_t refs;
7878

79-
/* What host tells us, plus 2 for header & tailer. */
80-
unsigned int sg_elems;
81-
8279
/* Ida index - used to track minor number allocations. */
8380
int index;
8481

@@ -322,8 +319,6 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
322319
blk_status_t status;
323320
int err;
324321

325-
BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
326-
327322
status = virtblk_setup_cmd(vblk->vdev, req, vbr);
328323
if (unlikely(status))
329324
return status;
@@ -783,8 +778,6 @@ static int virtblk_probe(struct virtio_device *vdev)
783778
/* Prevent integer overflows and honor max vq size */
784779
sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
785780

786-
/* We need extra sg elements at head and tail. */
787-
sg_elems += 2;
788781
vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
789782
if (!vblk) {
790783
err = -ENOMEM;
@@ -796,7 +789,6 @@ static int virtblk_probe(struct virtio_device *vdev)
796789
mutex_init(&vblk->vdev_mutex);
797790

798791
vblk->vdev = vdev;
799-
vblk->sg_elems = sg_elems;
800792

801793
INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
802794

@@ -853,7 +845,7 @@ static int virtblk_probe(struct virtio_device *vdev)
853845
set_disk_ro(vblk->disk, 1);
854846

855847
/* We can handle whatever the host told us to handle. */
856-
blk_queue_max_segments(q, vblk->sg_elems-2);
848+
blk_queue_max_segments(q, sg_elems);
857849

858850
/* No real sector limit. */
859851
blk_queue_max_hw_sectors(q, -1U);
@@ -925,9 +917,15 @@ static int virtblk_probe(struct virtio_device *vdev)
925917

926918
virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
927919
&v);
920+
921+
/*
922+
* max_discard_seg == 0 is out of spec but we always
923+
* handled it.
924+
*/
925+
if (!v)
926+
v = sg_elems;
928927
blk_queue_max_discard_segments(q,
929-
min_not_zero(v,
930-
MAX_DISCARD_SEGMENTS));
928+
min(v, MAX_DISCARD_SEGMENTS));
931929

932930
blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
933931
}

drivers/char/virtio_console.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,13 @@ static void virtcons_remove(struct virtio_device *vdev)
19571957
list_del(&portdev->list);
19581958
spin_unlock_irq(&pdrvdata_lock);
19591959

1960+
/* Device is going away, exit any polling for buffers */
1961+
virtio_break_device(vdev);
1962+
if (use_multiport(portdev))
1963+
flush_work(&portdev->control_work);
1964+
else
1965+
flush_work(&portdev->config_work);
1966+
19601967
/* Disable interrupts for vqs */
19611968
virtio_reset_device(vdev);
19621969
/* Finish up work that's lined up */

drivers/vdpa/mlx5/net/mlx5_vnet.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,11 +1563,27 @@ static virtio_net_ctrl_ack handle_ctrl_mq(struct mlx5_vdpa_dev *mvdev, u8 cmd)
15631563

15641564
switch (cmd) {
15651565
case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET:
1566+
/* This mq feature check aligns with pre-existing userspace
1567+
* implementation.
1568+
*
1569+
* Without it, an untrusted driver could fake a multiqueue config
1570+
* request down to a non-mq device that may cause kernel to
1571+
* panic due to uninitialized resources for extra vqs. Even with
1572+
* a well behaving guest driver, it is not expected to allow
1573+
* changing the number of vqs on a non-mq device.
1574+
*/
1575+
if (!MLX5_FEATURE(mvdev, VIRTIO_NET_F_MQ))
1576+
break;
1577+
15661578
read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)&mq, sizeof(mq));
15671579
if (read != sizeof(mq))
15681580
break;
15691581

15701582
newqps = mlx5vdpa16_to_cpu(mvdev, mq.virtqueue_pairs);
1583+
if (newqps < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1584+
newqps > mlx5_vdpa_max_qps(mvdev->max_vqs))
1585+
break;
1586+
15711587
if (ndev->cur_num_vqs == 2 * newqps) {
15721588
status = VIRTIO_NET_OK;
15731589
break;
@@ -1897,11 +1913,25 @@ static u64 mlx5_vdpa_get_device_features(struct vdpa_device *vdev)
18971913
return ndev->mvdev.mlx_features;
18981914
}
18991915

1900-
static int verify_min_features(struct mlx5_vdpa_dev *mvdev, u64 features)
1916+
static int verify_driver_features(struct mlx5_vdpa_dev *mvdev, u64 features)
19011917
{
1918+
/* Minimum features to expect */
19021919
if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)))
19031920
return -EOPNOTSUPP;
19041921

1922+
/* Double check features combination sent down by the driver.
1923+
* Fail invalid features due to absence of the depended feature.
1924+
*
1925+
* Per VIRTIO v1.1 specification, section 5.1.3.1 Feature bit
1926+
* requirements: "VIRTIO_NET_F_MQ Requires VIRTIO_NET_F_CTRL_VQ".
1927+
* By failing the invalid features sent down by untrusted drivers,
1928+
* we're assured the assumption made upon is_index_valid() and
1929+
* is_ctrl_vq_idx() will not be compromised.
1930+
*/
1931+
if ((features & (BIT_ULL(VIRTIO_NET_F_MQ) | BIT_ULL(VIRTIO_NET_F_CTRL_VQ))) ==
1932+
BIT_ULL(VIRTIO_NET_F_MQ))
1933+
return -EINVAL;
1934+
19051935
return 0;
19061936
}
19071937

@@ -1977,7 +2007,7 @@ static int mlx5_vdpa_set_driver_features(struct vdpa_device *vdev, u64 features)
19772007

19782008
print_features(mvdev, features, true);
19792009

1980-
err = verify_min_features(mvdev, features);
2010+
err = verify_driver_features(mvdev, features);
19812011
if (err)
19822012
return err;
19832013

drivers/vdpa/vdpa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static void vdpa_get_config_unlocked(struct vdpa_device *vdev,
393393
* If it does happen we assume a legacy guest.
394394
*/
395395
if (!vdev->features_valid)
396-
vdpa_set_features(vdev, 0, true);
396+
vdpa_set_features_unlocked(vdev, 0);
397397
ops->get_config(vdev, offset, buf, len);
398398
}
399399

drivers/vdpa/vdpa_user/iova_domain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ vduse_domain_alloc_iova(struct iova_domain *iovad,
294294

295295
iova_pfn = alloc_iova_fast(iovad, iova_len, limit >> shift, true);
296296

297-
return iova_pfn << shift;
297+
return (dma_addr_t)iova_pfn << shift;
298298
}
299299

300300
static void vduse_domain_free_iova(struct iova_domain *iovad,

drivers/vdpa/virtio_pci/vp_vdpa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ static void vp_vdpa_remove(struct pci_dev *pdev)
533533
{
534534
struct vp_vdpa *vp_vdpa = pci_get_drvdata(pdev);
535535

536-
vdpa_unregister_device(&vp_vdpa->vdpa);
537536
vp_modern_remove(&vp_vdpa->mdev);
537+
vdpa_unregister_device(&vp_vdpa->vdpa);
538538
}
539539

540540
static struct pci_driver vp_vdpa_driver = {

drivers/vhost/iotlb.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ int vhost_iotlb_add_range_ctx(struct vhost_iotlb *iotlb,
5757
if (last < start)
5858
return -EFAULT;
5959

60+
/* If the range being mapped is [0, ULONG_MAX], split it into two entries
61+
* otherwise its size would overflow u64.
62+
*/
63+
if (start == 0 && last == ULONG_MAX) {
64+
u64 mid = last / 2;
65+
66+
vhost_iotlb_add_range_ctx(iotlb, start, mid, addr, perm, opaque);
67+
addr += mid + 1;
68+
start = mid + 1;
69+
}
70+
6071
if (iotlb->limit &&
6172
iotlb->nmaps == iotlb->limit &&
6273
iotlb->flags & VHOST_IOTLB_FLAG_RETIRE) {

drivers/vhost/vdpa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
286286
if (copy_from_user(&features, featurep, sizeof(features)))
287287
return -EFAULT;
288288

289-
if (vdpa_set_features(vdpa, features, false))
289+
if (vdpa_set_features(vdpa, features))
290290
return -EINVAL;
291291

292292
return 0;

drivers/vhost/vhost.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,11 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
11701170
goto done;
11711171
}
11721172

1173+
if (msg.size == 0) {
1174+
ret = -EINVAL;
1175+
goto done;
1176+
}
1177+
11731178
if (dev->msg_handler)
11741179
ret = dev->msg_handler(dev, &msg);
11751180
else
@@ -1981,7 +1986,7 @@ static int vhost_update_used_flags(struct vhost_virtqueue *vq)
19811986
return 0;
19821987
}
19831988

1984-
static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1989+
static int vhost_update_avail_event(struct vhost_virtqueue *vq)
19851990
{
19861991
if (vhost_put_avail_event(vq))
19871992
return -EFAULT;
@@ -2527,7 +2532,7 @@ bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
25272532
return false;
25282533
}
25292534
} else {
2530-
r = vhost_update_avail_event(vq, vq->avail_idx);
2535+
r = vhost_update_avail_event(vq);
25312536
if (r) {
25322537
vq_err(vq, "Failed to update avail event index at %p: %d\n",
25332538
vhost_avail_event(vq), r);

drivers/virtio/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ config VIRTIO_BALLOON
105105

106106
config VIRTIO_MEM
107107
tristate "Virtio mem driver"
108-
default m
109108
depends on X86_64
110109
depends on VIRTIO
111110
depends on MEMORY_HOTPLUG

0 commit comments

Comments
 (0)