diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 143f380baa1c..5b151605aaf8 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -15,6 +15,9 @@
 #include <linux/spinlock.h>
 #include <xen/xen.h>
 
+static bool force_used_validation = false;
+module_param(force_used_validation, bool, 0444);
+
 #ifdef DEBUG
 /* For development, we want to crash whenever the ring is screwed. */
 #define BAD_RING(_vq, fmt, args...)				\
@@ -105,6 +108,9 @@ struct vring_virtqueue_split {
 	struct vring_desc_state_split *desc_state;
 	struct vring_desc_extra *desc_extra;
 
+	/* Maximum in buffer length, NULL means no used validation */
+	u32 *buflen;
+
 	/* DMA address and size information */
 	dma_addr_t queue_dma_addr;
 	size_t queue_size_in_bytes;
@@ -145,6 +151,9 @@ struct vring_virtqueue_packed {
 	struct vring_desc_state_packed *desc_state;
 	struct vring_desc_extra *desc_extra;
 
+	/* Maximum in buffer length, NULL means no used validation */
+	u32 *buflen;
+
 	/* DMA address and size information */
 	dma_addr_t ring_dma_addr;
 	dma_addr_t driver_event_dma_addr;
@@ -552,6 +561,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	unsigned int i, n, avail, descs_used, prev, err_idx;
 	int head;
 	bool indirect;
+	u32 buflen = 0;
 
 	START_USE(vq);
 
@@ -635,6 +645,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 						     VRING_DESC_F_NEXT |
 						     VRING_DESC_F_WRITE,
 						     indirect);
+			buflen += sg->length;
 		}
 	}
 	/* Last one doesn't continue. */
@@ -675,6 +686,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	else
 		vq->split.desc_state[head].indir_desc = ctx;
 
+	/* Store in buffer length if necessary */
+	if (vq->split.buflen)
+		vq->split.buflen[head] = buflen;
+
 	/* Put entry in available array (but don't update avail->idx until they
 	 * do sync). */
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
@@ -861,6 +876,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
 		BAD_RING(vq, "id %u is not a head!\n", i);
 		return NULL;
 	}
+	if (vq->split.buflen && unlikely(*len > vq->split.buflen[i])) {
+		BAD_RING(vq, "used len %d is larger than max in buffer len %u\n",
+			*len, vq->split.buflen[i]);
+		return NULL;
+	}
 
 	/* detach_buf_split clears data, so grab it now. */
 	ret = vq->split.desc_state[i].data;
@@ -1085,10 +1105,25 @@ static void vring_free_split(struct vring_virtqueue_split *vring_split,
 			 vring_split->queue_dma_addr,
 			 dma_dev);
 
+	kfree(vring_split->buflen);
 	kfree(vring_split->desc_state);
 	kfree(vring_split->desc_extra);
 }
 
+static bool vring_needs_used_validation(const struct virtio_device *vdev)
+{
+	/*
+	 * Several legacy devices are known to produce buggy used
+	 * length. In order to let driver work, we won't validate used
+	 * buffer length in this case.
+	 */
+	if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
+		return false;
+	if (force_used_validation)
+		return true;
+	return false;
+}
+
 static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
 				   struct virtio_device *vdev,
 				   u32 num,
@@ -1137,7 +1172,19 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
 	vring_split->vring_align = vring_align;
 	vring_split->may_reduce_num = may_reduce_num;
 
+	if (vring_needs_used_validation(vdev)) {
+		vring_split->buflen =
+			kmalloc_array(num, sizeof(*vring_split->buflen),
+				      GFP_KERNEL);
+		if (!vring_split->buflen)
+			goto err_buflen;
+	}
+
 	return 0;
+
+err_buflen:
+	vring_free_split(vring_split, vdev, dma_dev);
+	return -ENOMEM;
 }
 
 static struct virtqueue *vring_create_virtqueue_split(
@@ -1297,6 +1344,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	unsigned int i, n, err_idx;
 	u16 head, id;
 	dma_addr_t addr;
+	u32 buflen = 0;
 
 	head = vq->packed.next_avail_idx;
 	desc = alloc_indirect_packed(total_sg, gfp);
@@ -1325,6 +1373,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 			desc[i].addr = cpu_to_le64(addr);
 			desc[i].len = cpu_to_le32(sg->length);
 			i++;
+			if (n >= out_sgs)
+				buflen += sg->length;
 		}
 	}
 
@@ -1379,6 +1429,10 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	vq->packed.desc_state[id].last = id;
 	vq->packed.desc_state[id].premapped = premapped;
 
+	/* Store in buffer length if necessary */
+	if (vq->packed.buflen)
+		vq->packed.buflen[id] = buflen;
+
 	vq->num_added += 1;
 
 	pr_debug("Added buffer head %i to %p\n", head, vq);
@@ -1416,6 +1470,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
 	__le16 head_flags, flags;
 	u16 head, id, prev, curr, avail_used_flags;
 	int err;
+	u32 buflen = 0;
 
 	START_USE(vq);
 
@@ -1498,6 +1553,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
 					1 << VRING_PACKED_DESC_F_AVAIL |
 					1 << VRING_PACKED_DESC_F_USED;
 			}
+			if (n >= out_sgs)
+				buflen += sg->length;
 		}
 	}
 
@@ -1518,6 +1575,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
 	vq->packed.desc_state[id].last = prev;
 	vq->packed.desc_state[id].premapped = premapped;
 
+	/* Store in buffer length if necessary */
+	if (vq->packed.buflen)
+		vq->packed.buflen[id] = buflen;
+
 	/*
 	 * A driver MUST NOT make the first descriptor in the list
 	 * available before all subsequent descriptors comprising
@@ -1718,6 +1779,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
 		BAD_RING(vq, "id %u is not a head!\n", id);
 		return NULL;
 	}
+	if (vq->packed.buflen && unlikely(*len > vq->packed.buflen[id])) {
+		BAD_RING(vq, "used len %d is larger than max in buffer len %u\n",
+			*len, vq->packed.buflen[id]);
+		return NULL;
+	}
 
 	/* detach_buf_packed clears data, so grab it now. */
 	ret = vq->packed.desc_state[id].data;
@@ -1937,6 +2003,7 @@ static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
 				 vring_packed->device_event_dma_addr,
 				 dma_dev);
 
+	kfree(vring_packed->buflen);
 	kfree(vring_packed->desc_state);
 	kfree(vring_packed->desc_extra);
 }
@@ -1988,6 +2055,14 @@ static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
 
 	vring_packed->vring.num = num;
 
+	if (vring_needs_used_validation(vdev)) {
+		vring_packed->buflen =
+			kmalloc_array(num, sizeof(*vring_packed->buflen),
+				      GFP_KERNEL);
+		if (!vring_packed->buflen)
+			goto err;
+	}
+
 	return 0;
 
 err:
