Skip to contentSkip to conversation

vhost: fix unsafe vrings addresses modifications

PATCH Applied Maxime Coquelin 3 participants 4 messages Jan 27, 2022 – Feb 17, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: fix unsafe vrings addresses modifications

This patch adds missing protection around vring_invalidate and translate_ring_addresses calls in vhost_user_iotlb_msg.

Fixes eefac9536a90 ("vhost: postpone device creation until rings are mapped")
---
 lib/vhost/vhost_user.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/vhost/vhost_user.c +8 -2
index 5eb1dd6812..ae8513c465 100644
@@ -2566,8 +2566,11 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
 					len, imsg->perm);
 
-			if (is_vring_iotlb(dev, vq, imsg))
+			if (is_vring_iotlb(dev, vq, imsg)) {
+				rte_spinlock_lock(&vq->access_lock);
 				*pdev = dev = translate_ring_addresses(dev, i);
+				rte_spinlock_unlock(&vq->access_lock);
+			}
 		}
 		break;
 	case VHOST_IOTLB_INVALIDATE:
@@ -2580,8 +2583,11 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_cache_remove(vq, imsg->iova,
 					imsg->size);
 
-			if (is_vring_iotlb(dev, vq, imsg))
+			if (is_vring_iotlb(dev, vq, imsg)) {
+				rte_spinlock_lock(&vq->access_lock);
 				vring_invalidate(dev, vq);
+				rte_spinlock_unlock(&vq->access_lock);
+			}
 		}
 		break;
 	default:
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 50 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Thursday, January 27, 2022 7:10 PM
To: [email protected]; Xia, Chenbo <[email protected]>; Wang, YuanX <[email protected]>; [email protected]
Cc: [email protected]; Maxime Coquelin <[email protected]>
Subject: [PATCH] vhost: fix unsafe vrings addresses modifications

This patch adds missing protection around vring_invalidate and translate_ring_addresses calls in vhost_user_iotlb_msg.

Fixes eefac9536a90 ("vhost: postpone device creation until rings are mapped")
---
 lib/vhost/vhost_user.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/vhost/vhost_user.c +4 -1
index 5eb1dd6812..ae8513c465 100644
@@ -2566,8 +2566,11 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct
 VhostUserMsg *msg,
 			vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
 					len, imsg->perm);
 
-			if (is_vring_iotlb(dev, vq, imsg))
+			if (is_vring_iotlb(dev, vq, imsg)) {
+				rte_spinlock_lock(&vq->access_lock);
 				*pdev = dev = translate_ring_addresses(dev, i);
+				rte_spinlock_unlock(&vq->access_lock);
+			}
 		}
 		break;
lib/vhost/vhost_user.c +4 -1
 	case VHOST_IOTLB_INVALIDATE:
@@ -2580,8 +2583,11 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct
 VhostUserMsg *msg,
 			vhost_user_iotlb_cache_remove(vq, imsg->iova,
 					imsg->size);
 
-			if (is_vring_iotlb(dev, vq, imsg))
+			if (is_vring_iotlb(dev, vq, imsg)) {
+				rte_spinlock_lock(&vq->access_lock);
 				vring_invalidate(dev, vq);
+				rte_spinlock_unlock(&vq->access_lock);
+			}
 		}
 		break;
Quoted 3 lines

default:
--
2.34.1

author Applied
#3 lore raw
In reply to Maxime Coquelin
Quoted 31 lines · Maxime Coquelin

This patch adds missing protection around vring_invalidate and translate_ring_addresses calls in vhost_user_iotlb_msg.

Fixes eefac9536a90 ("vhost: postpone device creation until rings are mapped")
---
  lib/vhost/vhost_user.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
lib/vhost/vhost_user.c +4 -1
index 5eb1dd6812..ae8513c465 100644
@@ -2566,8 +2566,11 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
  			vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
  					len, imsg->perm);
 
-			if (is_vring_iotlb(dev, vq, imsg))
+			if (is_vring_iotlb(dev, vq, imsg)) {
+				rte_spinlock_lock(&vq->access_lock);
  				*pdev = dev = translate_ring_addresses(dev, i);
+				rte_spinlock_unlock(&vq->access_lock);
+			}
  		}
  		break;
  	case VHOST_IOTLB_INVALIDATE:
@@ -2580,8 +2583,11 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
  			vhost_user_iotlb_cache_remove(vq, imsg->iova,
  					imsg->size);

lib/vhost/vhost_user.c vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg, · line 2593

Quoted 10 lines
lib/vhost/vhost_user.c +4 -1
 
-			if (is_vring_iotlb(dev, vq, imsg))
+			if (is_vring_iotlb(dev, vq, imsg)) {
+				rte_spinlock_lock(&vq->access_lock);
  				vring_invalidate(dev, vq);
+				rte_spinlock_unlock(&vq->access_lock);
+			}
  		}
  		break;
  	default:

Applied to dpdk-next-virtio/main.

Thanks,
Maxime