Skip to contentSkip to conversation

vhost: avoid iotlb mempool allocation while IOMMU disabled

PATCH Superseded Wan Junjie 3 participants 5 messages Jan 29, 2021 – Feb 3, 2021

lore
Share & tools
lore

Wan Junjie

author
#0 lore raw

PATCH 1/1 vhost: avoid iotlb mempool allocation while IOMMU disabled

If vhost device's IOMMU feature is disabled, iotlb mempool allocation is unnecessary.

---
 lib/librte_vhost/vhost.c      | 6 ++++--
 lib/librte_vhost/vhost_user.c | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
lib/librte_vhost/vhost.c +4 -2
index efb136edd..00c5040e2 100644
@@ -352,7 +352,8 @@ free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
 		vhost_free_async_mem(vq);
 	}
 	rte_free(vq->batch_copy_elems);
-	rte_mempool_free(vq->iotlb_pool);
+	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+		rte_mempool_free(vq->iotlb_pool);
 	rte_free(vq);
 }
 
@@ -556,7 +557,8 @@ init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 	vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
 
-	vhost_user_iotlb_init(dev, vring_idx);
+	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+		vhost_user_iotlb_init(dev, vring_idx);
 	/* Backends are set to -1 indicating an inactive device. */
 	vq->backend = -1;
 }
lib/librte_vhost/vhost_user.c +1 -1
index a60bb945a..d415607d7 100644
@@ -575,7 +575,7 @@ numa_realloc(struct virtio_net *dev, int index)
 	dev->virtqueue[index] = vq;
 	vhost_devices[dev->vid] = dev;
 
-	if (old_vq != vq)
+	if (old_vq != vq && (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
 		vhost_user_iotlb_init(dev, index);
 
 	return dev;
Signature · 1 line
2.29.2
#1 lore raw

Hi Junjie,

Quoted 24 lines

-----Original Message-----
From: Wan Junjie <[email protected]>
Sent: Saturday, January 30, 2021 12:39 AM
To: [email protected]; Xia, Chenbo <[email protected]>
Cc: [email protected]; Wan Junjie <[email protected]>; Peng He <[email protected]>; Zhihong Wang <[email protected]>
Subject: [PATCH] vhost: avoid iotlb mempool allocation while IOMMU disabled

If vhost device's IOMMU feature is disabled, iotlb mempool allocation is unnecessary.

---
 lib/librte_vhost/vhost.c      | 6 ++++--
 lib/librte_vhost/vhost_user.c | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
lib/librte_vhost/vhost.c +0 -0
index efb136edd..00c5040e2 100644

lib/librte_vhost/vhost.c free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq) · line 356

Quoted 6 lines
lib/librte_vhost/vhost.c +2 -1
@@ -352,7 +352,8 @@ free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
 		vhost_free_async_mem(vq);
 	}
 	rte_free(vq->batch_copy_elems);
-	rte_mempool_free(vq->iotlb_pool);
+	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+		rte_mempool_free(vq->iotlb_pool);

We could make it simpler, check vq->iotlb_pool is not NULL and then free the mempool, which ignores the problem that features may not be set yet (See below comment). Although we could also keep the original because rte_mempool_free will do the check, let's do it outside to avoid useless func call.

lib/librte_vhost/vhost.c init_vring_queue(struct virtio_net *dev, uint32_t · line 562

Quoted 11 lines
lib/librte_vhost/vhost.c +2 -1
 	rte_free(vq);
 }
 
@@ -556,7 +557,8 @@ init_vring_queue(struct virtio_net *dev, uint32_t
 vring_idx)
 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 	vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
 
-	vhost_user_iotlb_init(dev, vring_idx);
+	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+		vhost_user_iotlb_init(dev, vring_idx);

Note that when this func is called, dev->features are not set. So it's meaningless to do this. So, we should do it after the features are set.

Thanks,
Chenbo

Quoted 18 lines
lib/librte_vhost/vhost.c +0 -0
 	/* Backends are set to -1 indicating an inactive device. */
 	vq->backend = -1;

}

lib/librte_vhost/vhost_user.c +1 -2
index a60bb945a..d415607d7 100644
@@ -575,7 +575,7 @@ numa_realloc(struct virtio_net *dev, int index)
 	dev->virtqueue[index] = vq;
 	vhost_devices[dev->vid] = dev;
 
-	if (old_vq != vq)
+	if (old_vq != vq && (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
 		vhost_user_iotlb_init(dev, index);
 
 	return dev;
--

2.29.2

author
#2 lore raw

PATCH v2 1/1 vhost: avoid iotlb mempool allocation while IOMMU disabled

In reply to Wan Junjie

If vhost device's IOMMU feature is disabled, iotlb mempool allocation is unnecessary.

---
 lib/librte_vhost/vhost.c      | 4 ++--
 lib/librte_vhost/vhost_user.c | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)
lib/librte_vhost/vhost.c +2 -2
index b83cf639e..e5341ab98 100644
@@ -352,7 +352,8 @@ free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
 		vhost_free_async_mem(vq);
 	}
 	rte_free(vq->batch_copy_elems);
-	rte_mempool_free(vq->iotlb_pool);
+	if (vq->iotlb_pool)
+		rte_mempool_free(vq->iotlb_pool);
 	rte_free(vq);
 }
 
@@ -556,7 +557,6 @@ init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 	vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
 
-	vhost_user_iotlb_init(dev, vring_idx);
 	/* Backends are set to -1 indicating an inactive device. */
 	vq->backend = -1;
 }
lib/librte_vhost/vhost_user.c +5 -1
index 45c8ac09d..81cbb9797 100644
@@ -470,6 +470,10 @@ vhost_user_set_vring_num(struct virtio_net **pdev,
 		return RTE_VHOST_MSG_RESULT_ERR;
 	}
 
+	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
+		if (vhost_user_iotlb_init(dev, msg->payload.state.index))
+			return RTE_VHOST_MSG_RESULT_ERR;
+	}
 	return RTE_VHOST_MSG_RESULT_OK;
 }
 
@@ -575,7 +579,7 @@ numa_realloc(struct virtio_net *dev, int index)
 	dev->virtqueue[index] = vq;
 	vhost_devices[dev->vid] = dev;
 
-	if (old_vq != vq)
+	if (old_vq != vq && (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
 		vhost_user_iotlb_init(dev, index);
 
 	return dev;
Signature · 1 line
2.11.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 68 lines

-----Original Message-----
From: Wan Junjie <[email protected]>
Sent: Tuesday, February 2, 2021 4:39 PM
To: Xia, Chenbo <[email protected]>; Maxime Coquelin <[email protected]>
Cc: [email protected]; Wan Junjie <[email protected]>; Peng He <[email protected]>; Zhihong Wang <[email protected]>
Subject: [PATCH v2] vhost: avoid iotlb mempool allocation while IOMMU disabled

If vhost device's IOMMU feature is disabled, iotlb mempool allocation is unnecessary.

---
 lib/librte_vhost/vhost.c      | 4 ++--
 lib/librte_vhost/vhost_user.c | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)
lib/librte_vhost/vhost.c +2 -2
index b83cf639e..e5341ab98 100644
@@ -352,7 +352,8 @@ free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
 		vhost_free_async_mem(vq);
 	}
 	rte_free(vq->batch_copy_elems);
-	rte_mempool_free(vq->iotlb_pool);
+	if (vq->iotlb_pool)
+		rte_mempool_free(vq->iotlb_pool);
 	rte_free(vq);
 }
 
@@ -556,7 +557,6 @@ init_vring_queue(struct virtio_net *dev, uint32_t
 vring_idx)
 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 	vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
 
-	vhost_user_iotlb_init(dev, vring_idx);
 	/* Backends are set to -1 indicating an inactive device. */
 	vq->backend = -1;

}

lib/librte_vhost/vhost_user.c +5 -2
index 45c8ac09d..81cbb9797 100644
@@ -470,6 +470,10 @@ vhost_user_set_vring_num(struct virtio_net **pdev,
 		return RTE_VHOST_MSG_RESULT_ERR;
 	}
 
+	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
+		if (vhost_user_iotlb_init(dev, msg->payload.state.index))
+			return RTE_VHOST_MSG_RESULT_ERR;
+	}
 	return RTE_VHOST_MSG_RESULT_OK;
 }
 
@@ -575,7 +579,7 @@ numa_realloc(struct virtio_net *dev, int index)
 	dev->virtqueue[index] = vq;
 	vhost_devices[dev->vid] = dev;
 
-	if (old_vq != vq)
+	if (old_vq != vq && (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
 		vhost_user_iotlb_init(dev, index);
 
 	return dev;
--
Quoted 1 line

2.11.0

Reviewed-by on v2 1/1
#4 lore raw
In reply to Wan Junjie

Hi,

Quoted 5 lines · Wan Junjie

If vhost device's IOMMU feature is disabled, iotlb mempool allocation is unnecessary.

Welcome to the community, looking forward for more contributions!

Quoted 1 line

And welcome back Zhihong! :)

Quoted 5 lines
---
 lib/librte_vhost/vhost.c      | 4 ++--
 lib/librte_vhost/vhost_user.c | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)

The patch looks good to me, however this is more an optimization than a fix. As we are late in the release cycle, I will postpone it to next release.

Thanks,
Maxime