Skip to contentSkip to conversation

drm/virtio: fix ring free check

PATCH Superseded Gerd Hoffmann 2 participants 3 messages Feb 6, 2020 – Feb 7, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gerd Hoffmann

author maintainer
#0 lore raw

PATCH 1/1 drm/virtio: fix ring free check

If the virtio device supports indirect ring descriptors we need only one ring entry for the whole command. Take that into account when checking whenever the virtqueue has enough free entries for our command.

---
 drivers/gpu/drm/virtio/virtgpu_vq.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/virtio/virtgpu_vq.c +6 -3
index 41e475fbd67b..a2ec09dba530 100644
@@ -328,7 +328,8 @@ static void virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev,
 {
 	struct virtqueue *vq = vgdev->ctrlq.vq;
 	bool notify = false;
-	int ret;
+	bool indirect;
+	int vqcnt, ret;
 
 again:
 	spin_lock(&vgdev->ctrlq.qlock);
@@ -341,9 +342,11 @@ static void virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev,
 		return;
 	}
 
-	if (vq->num_free < elemcnt) {
+	indirect = virtio_has_feature(vgdev->vdev, VIRTIO_RING_F_INDIRECT_DESC);
+	vqcnt = indirect ? 1 : elemcnt;
+	if (vq->num_free < vqcnt) {
 		spin_unlock(&vgdev->ctrlq.qlock);
-		wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= elemcnt);
+		wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= vqcnt);
 		goto again;
 	}
 
Signature · 1 line
2.18.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Gerd Hoffmann

If the virtio device supports indirect ring descriptors we need only one ring entry for the whole command. Take that into account when checking whenever the virtqueue has enough free entries for our command.

---
 drivers/gpu/drm/virtio/virtgpu_vq.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/virtio/virtgpu_vq.c +2 -1
index 41e475fbd67b..a2ec09dba530 100644
@@ -328,7 +328,8 @@ static void virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev,
 {
        struct virtqueue *vq = vgdev->ctrlq.vq;
        bool notify = false;
-       int ret;
+       bool indirect;
+       int vqcnt, ret;
 
 again:
        spin_lock(&vgdev->ctrlq.qlock);

drivers/gpu/drm/virtio/virtgpu_vq.c static void virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev, · line 346

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +2 -1
@@ -341,9 +342,11 @@ static void virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev,
                return;
        }
 
-       if (vq->num_free < elemcnt) {
+       indirect = virtio_has_feature(vgdev->vdev, VIRTIO_RING_F_INDIRECT_DESC);
+       vqcnt = indirect ? 1 : elemcnt;

Is the feature dynamic and require the lock held? If not, the result can be cached and the fixup can happen before grabbing the lock

  if (vgdev->has_indirect_desc)
    elemcnt = 1;

Either way, patch is

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +2 -2
+       if (vq->num_free < vqcnt) {
                spin_unlock(&vgdev->ctrlq.qlock);
-               wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= elemcnt);
+               wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= vqcnt);
                goto again;
        }
 
--

2.18.1

author maintainer
#2 lore raw

Hi,

Quoted 4 lines

drivers/gpu/drm/virtio/virtgpu_vq.c static void virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev, · line 346

Quoted 2 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +2 -0
+       indirect = virtio_has_feature(vgdev->vdev, VIRTIO_RING_F_INDIRECT_DESC);
+       vqcnt = indirect ? 1 : elemcnt;

Is the feature dynamic and require the lock held? If not, the result can be cached and the fixup can happen before grabbing the lock

Not dynamic, so yes, caching makes sense.

cheers,
Gerd