Skip to contentSkip to conversation

vhost_user: protect active rings from async ring changes

PATCH Superseded Victor Kaplansky 4 participants 10 messages Dec 6, 2017 – Dec 12, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost_user: protect active rings from async ring changes

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece are out of critical section accessing the vring data.

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

---
 lib/librte_vhost/vhost.h      |  1 +
 lib/librte_vhost/vhost_user.c | 44 +++++++++++++++++++++++++++++++++++++++++++
 lib/librte_vhost/virtio_net.c |  8 ++++++++
 3 files changed, 53 insertions(+)
lib/librte_vhost/vhost.h +1 -0
index 1cc81c17..812aeccd 100644
@@ -137,6 +137,7 @@ struct vhost_virtqueue {
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
 	int				iotlb_cache_nr;
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
+        rte_spinlock_t active_lock;
 } __rte_cache_aligned;
 
 /* Old kernels have no such macros defined */
lib/librte_vhost/vhost_user.c +44 -0

62 lines not on this page · raw message

lib/librte_vhost/virtio_net.c +8 -0
index 6fee16e5..de7e38bb 100644
@@ -44,6 +44,7 @@
 #include <rte_udp.h>
 #include <rte_sctp.h>
 #include <rte_arp.h>
+#include <rte_spinlock.h>
 
 #include "iotlb.h"
 #include "vhost.h"
@@ -1180,9 +1181,12 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 	}
 
 	vq = dev->virtqueue[queue_id];
+
 	if (unlikely(vq->enabled == 0))
 		return 0;
 
+	rte_spinlock_lock(&vq->active_lock);
+
 	vq->batch_copy_nb_elems = 0;
 
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
@@ -1240,6 +1244,7 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 		if (rarp_mbuf == NULL) {
 			RTE_LOG(ERR, VHOST_DATA,
 				"Failed to allocate memory for mbuf.\n");
+			rte_spinlock_unlock(&vq->active_lock);
 			return 0;
 		}
 
@@ -1356,6 +1361,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_unlock(vq);
 
+        rte_spinlock_unlock(&vq->active_lock);        
+	
 	if (unlikely(rarp_mbuf != NULL)) {
 		/*
 		 * Inject it to the head of "pkts" array, so that switch's mac
@@ -1366,5 +1373,6 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 		i += 1;
 	}
 
+
 	return i;
 }
Signature · 1 line
Victor
#1 lore raw
Quoted 9 lines · Victor Kaplansky

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

Quoted 8 lines

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece are out of critical section accessing the vring data.

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Hi,

Thanks for the patch! Firstly, I didn't see your SoB.

I'm also more interested to know do you see any performance penalty?

Quoted 11 lines
---
 lib/librte_vhost/vhost.h      |  1 +
 lib/librte_vhost/vhost_user.c | 44 +++++++++++++++++++++++++++++++++++++++++++
 lib/librte_vhost/virtio_net.c |  8 ++++++++
 3 files changed, 53 insertions(+)
lib/librte_vhost/vhost.h +0 -0
index 1cc81c17..812aeccd 100644

lib/librte_vhost/vhost.h struct vhost_virtqueue { · line 140

Quoted 4 lines
lib/librte_vhost/vhost.h +1 -0
@@ -137,6 +137,7 @@ struct vhost_virtqueue {
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
 	int				iotlb_cache_nr;
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
+        rte_spinlock_t active_lock;

The indentation is broken.

lib/librte_vhost/vhost.h rte_vhost_dequeue_burst(int vid, uint16_t queue_id, · line 1364

Quoted 6 lines
lib/librte_vhost/vhost.h +1 -0
 } __rte_cache_aligned;
@@ -1356,6 +1361,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_unlock(vq);
 
+        rte_spinlock_unlock(&vq->active_lock);        

Ditto.

	--yliu
#2 lore raw
In reply to Victor Kaplansky
Quoted 10 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 5 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 5 lines

are out of critical section accessing the vring data.

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for example) after call destroy_device() callback so that every PMD thread has enough time to skip out the criterial area?

Quoted 102 lines
---
 lib/librte_vhost/vhost.h      |  1 +
 lib/librte_vhost/vhost_user.c | 44
+++++++++++++++++++++++++++++++++++++++++++
 lib/librte_vhost/virtio_net.c |  8 ++++++++
 3 files changed, 53 insertions(+)
lib/librte_vhost/vhost.h +1 -0
index 1cc81c17..812aeccd 100644
@@ -137,6 +137,7 @@ struct vhost_virtqueue {
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
 	int				iotlb_cache_nr;
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
+        rte_spinlock_t active_lock;
 } __rte_cache_aligned;
 
 /* Old kernels have no such macros defined */
lib/librte_vhost/vhost_user.c +40 -0
index f4c7ce46..02a3a7d3 100644
@@ -1190,6 +1190,46 @@ vhost_user_check_and_alloc_queue_pair(struct
 virtio_net *dev, VhostUserMsg *msg)
 	return alloc_vring_queue(dev, vring_idx);
 }
 
+static void
+vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
+{
+	unsigned int i = 0;
+	unsigned int vq_num = 0;
+
+	while (vq_num < dev->nr_vring) {
+		struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+		if (!vq) {
+			i++;
+			continue;
+		}
+
+		rte_spinlock_lock(&vq->active_lock);
+		vq_num++;
+		i++;
+	}
+}
+
+static void
+vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
+{
+	unsigned int i = 0;
+	unsigned int vq_num = 0;
+
+	while (vq_num < dev->nr_vring) {
+		struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+		if (!vq) {
+			i++;
+			continue;
+		}
+
+		rte_spinlock_unlock(&vq->active_lock);
+		vq_num++;
+		i++;
+	}
+}
+
 int
 vhost_user_msg_handler(int vid, int fd)

{

lib/librte_vhost/vhost_user.c +4 -0
@@ -1241,6 +1281,8 @@ vhost_user_msg_handler(int vid, int fd)
 		return -1;
 	}
 
+	vhost_user_lock_all_queue_pairs(dev);
+
 	switch (msg.request.master) {
 	case VHOST_USER_GET_FEATURES:
 		msg.payload.u64 = vhost_user_get_features(dev);
@@ -1342,6 +1384,8 @@ vhost_user_msg_handler(int vid, int fd)
 
 	}
 
+	vhost_user_unlock_all_queue_pairs(dev);
+
 	if (msg.flags & VHOST_USER_NEED_REPLY) {
 		msg.payload.u64 = !!ret;
 		msg.size = sizeof(msg.payload.u64);
lib/librte_vhost/virtio_net.c +1 -0
index 6fee16e5..de7e38bb 100644
@@ -44,6 +44,7 @@
 #include <rte_udp.h>
 #include <rte_sctp.h>
 #include <rte_arp.h>
+#include <rte_spinlock.h>
 
 #include "iotlb.h"
 #include "vhost.h"

lib/librte_vhost/virtio_net.c rte_vhost_dequeue_burst(int vid, uint16_t · line 1185

Quoted 5 lines
lib/librte_vhost/virtio_net.c +1 -0
@@ -1180,9 +1181,12 @@ rte_vhost_dequeue_burst(int vid, uint16_t
 queue_id,
 	}
 
 	vq = dev->virtqueue[queue_id];
+

Remove above blank line.

Quoted 28 lines
lib/librte_vhost/virtio_net.c +2 -0
 	if (unlikely(vq->enabled == 0))
 		return 0;
 
+	rte_spinlock_lock(&vq->active_lock);
+
 	vq->batch_copy_nb_elems = 0;
 
lib/librte_vhost/virtio_net.c +3 -0
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
@@ -1240,6 +1244,7 @@ rte_vhost_dequeue_burst(int vid, uint16_t
 queue_id,
 		if (rarp_mbuf == NULL) {
 			RTE_LOG(ERR, VHOST_DATA,
 				"Failed to allocate memory for mbuf.\n");
+			rte_spinlock_unlock(&vq->active_lock);
 			return 0;
 		}
@@ -1356,6 +1361,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t
 queue_id,
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_unlock(vq);
 
+        rte_spinlock_unlock(&vq->active_lock);
+
 	if (unlikely(rarp_mbuf != NULL)) {
 		/*
lib/librte_vhost/virtio_net.c +0 -0
 		 * Inject it to the head of "pkts" array, so that switch's mac

lib/librte_vhost/virtio_net.c rte_vhost_dequeue_burst(int vid, uint16_t · line 1377

Quoted 5 lines
lib/librte_vhost/virtio_net.c +1 -0
@@ -1366,5 +1373,6 @@ rte_vhost_dequeue_burst(int vid, uint16_t
 queue_id,
 		i += 1;
 	}
 
+

Remove above blank line.

Quoted 4 lines
lib/librte_vhost/virtio_net.c +0 -0
 	return i;

}
--
Victor

#3 lore raw
Quoted 30 lines · Jianfeng
Quoted 10 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 5 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 2 lines

are out of critical section accessing the vring data.

Quoted 7 lines
Quoted 3 lines

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for example) after call destroy_device() callback so that every PMD thread has enough time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

The reason we need the lock is to protect PMD threads from the handling of some vhost-user protocol requests.
For example SET_MEM_TABLE in the case of memory hotplug, or SET_LOG_BASE in case of multiqueue, which is sent for every queue pair and results in unmapping/remapping the logging area.

Maxime

#4 lore raw
Quoted 51 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, December 7, 2017 6:02 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected]; Bie, Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 30 lines · Jianfeng
Quoted 10 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 5 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 2 lines

are out of critical section accessing the vring data.

Quoted 7 lines
Quoted 3 lines

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for example)

Quoted 8 lines

after call destroy_device() callback so that every PMD thread has enough time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

OK, I did not put it right way as there are multiple cases above: migration and memory hot plug. Let me try again:

Whenever a vhost thread handles a message affecting PMD threads, (like SET_MEM_TABLE, GET_VRING_BASE, etc) we can remove the dev flag - VIRTIO_DEV_RUNNING, and wait for a while so that PMD threads skip out of those criterial area. After message handling, reset the flag - VIRTIO_DEV_RUNNING.

I suppose it can work, basing on an assumption that PMD threads work in polling mode and can skip criterial area quickly and inevitably.

Quoted 7 lines

The reason we need the lock is to protect PMD threads from the handling of some vhost-user protocol requests.
For example SET_MEM_TABLE in the case of memory hotplug, or SET_LOG_BASE
in case of multiqueue, which is sent for every queue pair and results in unmapping/remapping the logging area.

Yes, I understand how it fails.

Thanks,
Jianfeng

Quoted 2 lines

Maxime

#5 lore raw
Quoted 60 lines · Jianfeng
Quoted 51 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, December 7, 2017 6:02 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected]; Bie, Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 30 lines · Jianfeng
Quoted 10 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 5 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 2 lines

are out of critical section accessing the vring data.

Quoted 7 lines
Quoted 3 lines

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for example)

Quoted 8 lines

after call destroy_device() callback so that every PMD thread has enough time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

Quoted 3 lines

OK, I did not put it right way as there are multiple cases above: migration and memory hot plug. Let me try again:

Whenever a vhost thread handles a message affecting PMD threads, (like SET_MEM_TABLE, GET_VRING_BASE, etc) we can remove the dev flag - VIRTIO_DEV_RUNNING, and wait for a while so that PMD threads skip out of those criterial area. After message handling, reset the flag - VIRTIO_DEV_RUNNING.

I think you mean clearing vq's enabled flag, because PMD threads never check the VIRTIO_DEV_RUNNING flag.

Quoted 1 line

I suppose it can work, basing on an assumption that PMD threads work in polling mode and can skip criterial area quickly and inevitably.

That sounds very fragile, because if the CPU aren't perfectly isolated, your PMD thread can be preempted for interrupt handling for example.

Or what if for some reason the PMD thread CPU stalls for a short while?

The later is unlikely, but if it happens, it will be hard to debug.

Let's see first the performance impact of using the spinlock. It might not be that important because 99.9999% of the times, it will not even spin.

Thanks,
Maxime

Quoted 15 lines
Quoted 7 lines

The reason we need the lock is to protect PMD threads from the handling of some vhost-user protocol requests.
For example SET_MEM_TABLE in the case of memory hotplug, or SET_LOG_BASE
in case of multiqueue, which is sent for every queue pair and results in unmapping/remapping the logging area.

Yes, I understand how it fails.

Thanks,
Jianfeng

Quoted 2 lines

Maxime

#6 lore raw
Quoted 80 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, December 8, 2017 4:36 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected]; Bie, Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 6 lines · Jianfeng
Quoted 6 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, December 7, 2017 6:02 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected];

Bie,

Quoted 10 lines
Quoted 10 lines

Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 2 lines · Jianfeng
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring

changes

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 2 lines

are out of critical section accessing the vring data.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for

example)

Quoted 10 lines
Quoted 8 lines

after call destroy_device() callback so that every PMD thread has enough time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

OK, I did not put it right way as there are multiple cases above: migration

and memory hot plug. Let me try again:

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Whenever a vhost thread handles a message affecting PMD threads, (like

SET_MEM_TABLE, GET_VRING_BASE, etc) we can remove the dev flag - VIRTIO_DEV_RUNNING, and wait for a while so that PMD threads skip out of those criterial area. After message handling, reset the flag - VIRTIO_DEV_RUNNING.

I think you mean clearing vq's enabled flag, because PMD threads never check the VIRTIO_DEV_RUNNING flag.

Ah, yes.

Quoted 7 lines
Quoted 2 lines

I suppose it can work, basing on an assumption that PMD threads work in

polling mode and can skip criterial area quickly and inevitably.

That sounds very fragile, because if the CPU aren't perfectly isolated, your PMD thread can be preempted for interrupt handling for example.

Quoted 7 lines

Or what if for some reason the PMD thread CPU stalls for a short while?

The later is unlikely, but if it happens, it will be hard to debug.

Let's see first the performance impact of using the spinlock. It might not be that important because 99.9999% of the times, it will not even spin.

Fair enough.

Thanks,
Jianfeng

Quoted 19 lines

Thanks,
Maxime

Quoted 15 lines
Quoted 7 lines

The reason we need the lock is to protect PMD threads from the handling of some vhost-user protocol requests.
For example SET_MEM_TABLE in the case of memory hotplug, or SET_LOG_BASE
in case of multiqueue, which is sent for every queue pair and results in unmapping/remapping the logging area.

Yes, I understand how it fails.

Thanks,
Jianfeng

Quoted 2 lines

Maxime

#7 lore raw

Hi Jianfeng,

Quoted 100 lines · Jianfeng
Quoted 80 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, December 8, 2017 4:36 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected]; Bie, Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 6 lines · Jianfeng
Quoted 6 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, December 7, 2017 6:02 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected];

Bie,

Quoted 10 lines
Quoted 10 lines

Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 2 lines · Jianfeng
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring

changes

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 2 lines

are out of critical section accessing the vring data.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for

example)

Quoted 10 lines
Quoted 8 lines

after call destroy_device() callback so that every PMD thread has enough time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

OK, I did not put it right way as there are multiple cases above: migration

and memory hot plug. Let me try again:

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Whenever a vhost thread handles a message affecting PMD threads, (like

SET_MEM_TABLE, GET_VRING_BASE, etc) we can remove the dev flag - VIRTIO_DEV_RUNNING, and wait for a while so that PMD threads skip out of those criterial area. After message handling, reset the flag - VIRTIO_DEV_RUNNING.

I think you mean clearing vq's enabled flag, because PMD threads never check the VIRTIO_DEV_RUNNING flag.

Ah, yes.

Quoted 9 lines
Quoted 2 lines

I suppose it can work, basing on an assumption that PMD threads work in

polling mode and can skip criterial area quickly and inevitably.

That sounds very fragile, because if the CPU aren't perfectly isolated, your PMD thread can be preempted for interrupt handling for example.

Or what if for some reason the PMD thread CPU stalls for a short while?

Quoted 7 lines
Quoted 5 lines

The later is unlikely, but if it happens, it will be hard to debug.

Let's see first the performance impact of using the spinlock. It might not be that important because 99.9999% of the times, it will not even spin.

Fair enough.

I did some benchmarks on my Broadwell test bench (see patch below), and it seems that depending on the benchmark, perfs are on par, or better with the spinlock! I guess it explains because with the spinlock there is a better batching and less concurrent accesses on the rings, but I'm not sure.

Please find my results below (CPU E5-2667 v4 @ 3.20GHz):

      Bench         v17.11     v17.11 + spinlock
 ---------------- ----------- -------------------
  PVP Bidir run1   19.29Mpps   19.26Mpps
  PVP Bidir run2   19.26Mpps   19.28Mpps
  TxOnly           18.47Mpps   18.83Mpps
  RxOnly           13.83Mpps   13.83Mpps
  IO Loopback      7.94Mpps    7.97Mpps

Patch:
---------------------------------------------------------

From 7e18cefce682235558fed66a1fb87ab937ec9297 Mon Sep 17 00:00:00 2001
From: Maxime Coquelin <[email protected]>
Date: Fri, 8 Dec 2017 04:21:25 -0500
Subject: [PATCH] vhost: spinlock benchmarking

---
 lib/librte_vhost/vhost.c      |  2 ++
 lib/librte_vhost/vhost.h      |  2 ++
 lib/librte_vhost/virtio_net.c | 12 ++++++++++++
 3 files changed, 16 insertions(+)
lib/librte_vhost/vhost.c +2 -0
index 4f8b73a..d9752cf 100644
@@ -219,6 +219,8 @@ struct virtio_net *
 	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
+	rte_spinlock_init(&vq->access_lock);
+
 	vhost_user_iotlb_init(dev, vring_idx);
 	/* Backends are set to -1 indicating an inactive device. */
 	vq->backend = -1;
lib/librte_vhost/vhost.h +2 -0
index 1cc81c1..56242a8 100644
@@ -131,6 +131,8 @@ struct vhost_virtqueue {
 	struct batch_copy_elem	*batch_copy_elems;
 	uint16_t		batch_copy_nb_elems;
 
+	rte_spinlock_t access_lock;
+
 	rte_rwlock_t	iotlb_lock;
 	rte_rwlock_t	iotlb_pending_lock;
 	struct rte_mempool *iotlb_pool;
lib/librte_vhost/virtio_net.c +12 -0
index 6fee16e..50cc3de 100644
@@ -329,6 +329,8 @@
 	if (unlikely(vq->enabled == 0))
 		return 0;
 
+	rte_spinlock_lock(&vq->access_lock);
+
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_lock(vq);
 
@@ -419,6 +421,8 @@
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_unlock(vq);
 
+	rte_spinlock_unlock(&vq->access_lock);
+
 	return count;
 }
 
@@ -654,6 +658,8 @@
 	if (unlikely(vq->enabled == 0))
 		return 0;
 
+	rte_spinlock_lock(&vq->access_lock);
+
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_lock(vq);
 
@@ -715,6 +721,8 @@
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_unlock(vq);
 
+	rte_spinlock_unlock(&vq->access_lock);
+
 	return pkt_idx;
 }
 
@@ -1183,6 +1191,8 @@
 	if (unlikely(vq->enabled == 0))
 		return 0;
 
+	rte_spinlock_lock(&vq->access_lock);
+
 	vq->batch_copy_nb_elems = 0;
 
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
@@ -1356,6 +1366,8 @@
 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 		vhost_user_iotlb_rd_unlock(vq);
 
+	rte_spinlock_unlock(&vq->access_lock);
+
 	if (unlikely(rarp_mbuf != NULL)) {
 		/*
 		 * Inject it to the head of "pkts" array, so that switch's mac
Signature · 1 line
1.8.3.1
#8 lore raw
Quoted 136 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, December 8, 2017 6:12 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected]; Bie, Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Hi Jianfeng,

Quoted 6 lines · Jianfeng
Quoted 6 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, December 8, 2017 4:36 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected];

Bie,

Quoted 7 lines
Quoted 7 lines

Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 7 lines
Quoted 7 lines · Jianfeng
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, December 7, 2017 6:02 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected];

[email protected];

Quoted 44 lines
Quoted 44 lines

Bie,

Quoted 10 lines
Quoted 10 lines

Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 2 lines · Jianfeng
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring

changes

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 6 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 5 lines

are out of critical section accessing the vring data.

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for

example)

Quoted 1 line
Quoted 1 line

after call destroy_device() callback so that every PMD thread has

enough

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

OK, I did not put it right way as there are multiple cases above: migration

and memory hot plug. Let me try again:

Quoted 2 lines

Whenever a vhost thread handles a message affecting PMD threads,

(like

Quoted 2 lines
Quoted 2 lines

SET_MEM_TABLE, GET_VRING_BASE, etc) we can remove the dev flag - VIRTIO_DEV_RUNNING, and wait for a while so that PMD threads skip out

of

Quoted 10 lines
Quoted 5 lines

those criterial area. After message handling, reset the flag - VIRTIO_DEV_RUNNING.

I think you mean clearing vq's enabled flag, because PMD threads never check the VIRTIO_DEV_RUNNING flag.

Ah, yes.

Quoted 2 lines
Quoted 2 lines

I suppose it can work, basing on an assumption that PMD threads work

in

Quoted 7 lines
Quoted 7 lines

polling mode and can skip criterial area quickly and inevitably.

That sounds very fragile, because if the CPU aren't perfectly isolated, your PMD thread can be preempted for interrupt handling for example.

Or what if for some reason the PMD thread CPU stalls for a short while?

Quoted 7 lines
Quoted 5 lines

The later is unlikely, but if it happens, it will be hard to debug.

Let's see first the performance impact of using the spinlock. It might not be that important because 99.9999% of the times, it will not even spin.

Fair enough.

I did some benchmarks on my Broadwell test bench (see patch below), and it seems that depending on the benchmark, perfs are on par, or better with the spinlock! I guess it explains because with the spinlock there is a better batching and less concurrent accesses on the rings, but I'm not sure.

Please find my results below (CPU E5-2667 v4 @ 3.20GHz):

Quoted 8 lines
       Bench         v17.11     v17.11 + spinlock
  ---------------- ----------- -------------------
   PVP Bidir run1   19.29Mpps   19.26Mpps
   PVP Bidir run2   19.26Mpps   19.28Mpps
   TxOnly           18.47Mpps   18.83Mpps
   RxOnly           13.83Mpps   13.83Mpps
   IO Loopback      7.94Mpps    7.97Mpps

This number seems really good for throughput.

FYI, we are recently doing a test to measure how long it takes for a noop (means there is no packets on the virtqueue) vhost dequeue operation. It's like:

    start_tsc = rte_rdtsc();
    for (j = 0; j < 10000000; ++j) 
            nb_rx = rte_eth_rx_burst(port_id, queue_id, pkts_burst, nb_pkt_per_burst);
    end_tsc = rte_rdtsc();
    printf("%"PRIu64"\n", (end_tsc - start_tsc) / 10000000);

Turns out that this patch will make it from 50 cycles -> 80 cycles for each noop rte_eth_rx_burst() operation on vhost port. My server is Haswell 2.3GHz.

Note that I'm not against this patch. Just that if such operation takes so many cycles, we might consider to introduce interrupt mode for vhost ports.

Thanks,
Jianfeng

Quoted 104 lines

Patch:
---------------------------------------------------------

From 7e18cefce682235558fed66a1fb87ab937ec9297 Mon Sep 17 00:00:00
2001
From: Maxime Coquelin <[email protected]>
Date: Fri, 8 Dec 2017 04:21:25 -0500
Subject: [PATCH] vhost: spinlock benchmarking

---
  lib/librte_vhost/vhost.c      |  2 ++
  lib/librte_vhost/vhost.h      |  2 ++
  lib/librte_vhost/virtio_net.c | 12 ++++++++++++
  3 files changed, 16 insertions(+)
lib/librte_vhost/vhost.c +2 -0
index 4f8b73a..d9752cf 100644
@@ -219,6 +219,8 @@ struct virtio_net *
  	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
  	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
+	rte_spinlock_init(&vq->access_lock);
+
  	vhost_user_iotlb_init(dev, vring_idx);
  	/* Backends are set to -1 indicating an inactive device. */
  	vq->backend = -1;
lib/librte_vhost/vhost.h +2 -0
index 1cc81c1..56242a8 100644
@@ -131,6 +131,8 @@ struct vhost_virtqueue {
  	struct batch_copy_elem	*batch_copy_elems;
  	uint16_t		batch_copy_nb_elems;
 
+	rte_spinlock_t access_lock;
+
  	rte_rwlock_t	iotlb_lock;
  	rte_rwlock_t	iotlb_pending_lock;
  	struct rte_mempool *iotlb_pool;
lib/librte_vhost/virtio_net.c +12 -1
index 6fee16e..50cc3de 100644
@@ -329,6 +329,8 @@
  	if (unlikely(vq->enabled == 0))
  		return 0;
 
+	rte_spinlock_lock(&vq->access_lock);
+
  	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
  		vhost_user_iotlb_rd_lock(vq);
 
@@ -419,6 +421,8 @@
  	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
  		vhost_user_iotlb_rd_unlock(vq);
 
+	rte_spinlock_unlock(&vq->access_lock);
+
  	return count;
  }
 
@@ -654,6 +658,8 @@
  	if (unlikely(vq->enabled == 0))
  		return 0;
 
+	rte_spinlock_lock(&vq->access_lock);
+
  	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
  		vhost_user_iotlb_rd_lock(vq);
 
@@ -715,6 +721,8 @@
  	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
  		vhost_user_iotlb_rd_unlock(vq);
 
+	rte_spinlock_unlock(&vq->access_lock);
+
  	return pkt_idx;
  }
 
@@ -1183,6 +1191,8 @@
  	if (unlikely(vq->enabled == 0))
  		return 0;
 
+	rte_spinlock_lock(&vq->access_lock);
+
  	vq->batch_copy_nb_elems = 0;
 
  	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
@@ -1356,6 +1366,8 @@
  	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
  		vhost_user_iotlb_rd_unlock(vq);
 
+	rte_spinlock_unlock(&vq->access_lock);
+
  	if (unlikely(rarp_mbuf != NULL)) {
  		/*
  		 * Inject it to the head of "pkts" array, so that switch's mac
--

1.8.3.1

#9 lore raw
Quoted 147 lines · Jianfeng
Quoted 136 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, December 8, 2017 6:12 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected]; Bie, Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Hi Jianfeng,

Quoted 6 lines · Jianfeng
Quoted 6 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, December 8, 2017 4:36 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected]; [email protected];

Bie,

Quoted 7 lines
Quoted 7 lines

Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 7 lines
Quoted 7 lines · Jianfeng
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, December 7, 2017 6:02 PM
To: Tan, Jianfeng; Victor Kaplansky; [email protected];

[email protected];

Quoted 44 lines
Quoted 44 lines

Bie,

Quoted 10 lines
Quoted 10 lines

Tiwei
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost_user: protect active rings from async ring changes

Quoted 2 lines · Jianfeng
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Victor Kaplansky [mailto:[email protected]]
Sent: Wednesday, December 6, 2017 9:56 PM
To: [email protected]; [email protected]; Bie, Tiwei; Tan, Jianfeng; [email protected]
Cc: [email protected]; [email protected]; Maxime Coquelin
Subject: [PATCH] vhost_user: protect active rings from async ring

changes

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 6 lines

When performing live migration or memory hot-plugging, the changes to the device and vrings made by message handler done independently from vring usage by PMD threads.

This causes for example segfauls during live-migration

segfauls ->segfaults?

Quoted 7 lines

with MQ enable, but in general virtually any request sent by qemu changing the state of device can cause problems.

These patches fixes all above issues by adding a spinlock to every vring and requiring message handler to start operation only after ensuring that all PMD threads related to the divece

Another typo: divece.

Quoted 5 lines

are out of critical section accessing the vring data.

Each vring has its own lock in order to not create contention between PMD threads of different vrings and to prevent performance degradation by scaling queue pair number.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Also wonder how much overhead it brings.

Instead of locking each vring, can we just, waiting a while (10us for

example)

Quoted 1 line
Quoted 1 line

after call destroy_device() callback so that every PMD thread has

enough

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

time to skip out the criterial area?

No, because we are not destroying the device when it is needed. Actually, once destroy_device() is called, it is likely that the application has taken care the ring aren't being processed anymore before returning from the callback (This is at least the case with Vhost PMD).

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

OK, I did not put it right way as there are multiple cases above: migration

and memory hot plug. Let me try again:

Quoted 2 lines

Whenever a vhost thread handles a message affecting PMD threads,

(like

Quoted 2 lines
Quoted 2 lines

SET_MEM_TABLE, GET_VRING_BASE, etc) we can remove the dev flag - VIRTIO_DEV_RUNNING, and wait for a while so that PMD threads skip out

of

Quoted 10 lines
Quoted 5 lines

those criterial area. After message handling, reset the flag - VIRTIO_DEV_RUNNING.

I think you mean clearing vq's enabled flag, because PMD threads never check the VIRTIO_DEV_RUNNING flag.

Ah, yes.

Quoted 2 lines
Quoted 2 lines

I suppose it can work, basing on an assumption that PMD threads work

in

Quoted 7 lines
Quoted 7 lines

polling mode and can skip criterial area quickly and inevitably.

That sounds very fragile, because if the CPU aren't perfectly isolated, your PMD thread can be preempted for interrupt handling for example.

Or what if for some reason the PMD thread CPU stalls for a short while?

Quoted 7 lines
Quoted 5 lines

The later is unlikely, but if it happens, it will be hard to debug.

Let's see first the performance impact of using the spinlock. It might not be that important because 99.9999% of the times, it will not even spin.

Fair enough.

I did some benchmarks on my Broadwell test bench (see patch below), and it seems that depending on the benchmark, perfs are on par, or better with the spinlock! I guess it explains because with the spinlock there is a better batching and less concurrent accesses on the rings, but I'm not sure.

Please find my results below (CPU E5-2667 v4 @ 3.20GHz):

Quoted 8 lines
        Bench         v17.11     v17.11 + spinlock
   ---------------- ----------- -------------------
    PVP Bidir run1   19.29Mpps   19.26Mpps
    PVP Bidir run2   19.26Mpps   19.28Mpps
    TxOnly           18.47Mpps   18.83Mpps
    RxOnly           13.83Mpps   13.83Mpps
    IO Loopback      7.94Mpps    7.97Mpps

This number seems really good for throughput.

Quoted 7 lines

FYI, we are recently doing a test to measure how long it takes for a noop (means there is no packets on the virtqueue) vhost dequeue operation. It's like:

     start_tsc = rte_rdtsc();
     for (j = 0; j < 10000000; ++j)
             nb_rx = rte_eth_rx_burst(port_id, queue_id, pkts_burst, nb_pkt_per_burst);
     end_tsc = rte_rdtsc();
     printf("%"PRIu64"\n", (end_tsc - start_tsc) / 10000000);

Thanks for sharing, it's an interesting benchmark.
Any chance it could land somewhere in DPDK upstream repo as a test application or something else

Quoted 1 line

Turns out that this patch will make it from 50 cycles -> 80 cycles for each noop rte_eth_rx_burst() operation on vhost port. My server is Haswell 2.3GHz.

I declared the spinlock at the end of the struct, so it may not be in the same cache line as enabled flag. Maybe putting them on the same cache line could have a positive impact
in the vring empty case?

Quoted 1 line

Note that I'm not against this patch. Just that if such operation takes so many cycles, we might consider to introduce interrupt mode for vhost ports.

Indeed, that could make sense in case of lot of vhost ports processed on a single CPU.

Thanks,
Maxime

Quoted 3 lines

Thanks,
Jianfeng