Skip to contentSkip to conversation

vhost: add device op to offload the interrupt kick

PATCH Superseded Eelco Chaudron 3 participants 9 messages Mar 27, 2023 – Apr 3, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: add device op to offload the interrupt kick

This patch adds an operation callback which gets called every time the library wants to call eventfd_write(). This eventfd_write() call could result in a system call, which could potentially block the PMD thread.

The callback function can decide whether it's ok to handle the eventfd_write() now or have the newly introduced function, rte_vhost_notify_guest(), called at a later time.

This can be used by 3rd party applications, like OVS, to avoid system calls being called as part of the PMD threads.

---
 lib/vhost/rte_vhost.h |   10 +++++++++-
 lib/vhost/vhost.c     |   21 +++++++++++++++++++++
 lib/vhost/vhost.h     |   43 ++++++++++++++++++++++++++++---------------
 3 files changed, 58 insertions(+), 16 deletions(-)
lib/vhost/rte_vhost.h +9 -1
index 58a5d4be92..af7a394d0f 100644
@@ -298,7 +298,13 @@ struct rte_vhost_device_ops {
 	 */
 	void (*guest_notified)(int vid);
 
-	void *reserved[1]; /**< Reserved for future extension */
+	/**
+	 * If this callback is registered, notification to the guest can
+	 * be handled by the front-end calling rte_vhost_notify_guest().
+	 * If it's not handled, 'false' should be returned. This can be used
+	 * to remove the "slow" eventfd_write() syscall from the datapath.
+	 */
+	bool (*guest_notify)(int vid, uint16_t queue_id);
 };
 
 /**
@@ -433,6 +439,8 @@ void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
 
 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
 
+void rte_vhost_notify_guest(int vid, uint16_t queue_id);
+
 /**
  * Register vhost driver. path could be different for multiple
  * instance support.
lib/vhost/vhost.c +21 -0
index ef37943817..ee090d78ef 100644
@@ -1467,6 +1467,27 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
 	return ret;
 }
 
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id)
+{
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+
 void
 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
 {
lib/vhost/vhost.h +28 -15

66 lines not on this page · raw message

#1 lore raw

Hi Eelco,

Quoted 10 lines · Eelco Chaudron

This patch adds an operation callback which gets called every time the library wants to call eventfd_write(). This eventfd_write() call could result in a system call, which could potentially block the PMD thread.

The callback function can decide whether it's ok to handle the eventfd_write() now or have the newly introduced function, rte_vhost_notify_guest(), called at a later time.

This can be used by 3rd party applications, like OVS, to avoid system calls being called as part of the PMD threads.

That's a good idea, please find some comments inline:

Quoted 27 lines
---
  lib/vhost/rte_vhost.h |   10 +++++++++-
  lib/vhost/vhost.c     |   21 +++++++++++++++++++++
  lib/vhost/vhost.h     |   43 ++++++++++++++++++++++++++++---------------
  3 files changed, 58 insertions(+), 16 deletions(-)
lib/vhost/rte_vhost.h +7 -1
index 58a5d4be92..af7a394d0f 100644
@@ -298,7 +298,13 @@ struct rte_vhost_device_ops {
  	 */
  	void (*guest_notified)(int vid);
 
-	void *reserved[1]; /**< Reserved for future extension */
+	/**
+	 * If this callback is registered, notification to the guest can
+	 * be handled by the front-end calling rte_vhost_notify_guest().
+	 * If it's not handled, 'false' should be returned. This can be used
+	 * to remove the "slow" eventfd_write() syscall from the datapath.
+	 */
+	bool (*guest_notify)(int vid, uint16_t queue_id);
  };
 
  /**

lib/vhost/rte_vhost.h void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, · line 442

Quoted 4 lines
lib/vhost/rte_vhost.h +1 -0
@@ -433,6 +439,8 @@ void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
 
  int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
 
+void rte_vhost_notify_guest(int vid, uint16_t queue_id);

The new API needs to be tagged as experimental, and also documented. (I see rte_vhost_enable_guest_notification is not properly documented, so not a good example!)

Quoted 19 lines
lib/vhost/rte_vhost.h +1 -0
+
  /**
   * Register vhost driver. path could be different for multiple
   * instance support.
lib/vhost/vhost.c +7 -0
index ef37943817..ee090d78ef 100644
@@ -1467,6 +1467,27 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
  	return ret;
  }
 
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id)
+{
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1486

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);

Maybe we should return an error of callfd is invalid or eventfd_write() failed.

Quoted 18 lines
lib/vhost/vhost.c +4 -0
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+
  void
  rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
  {
lib/vhost/vhost.h +3 -0
index 8fdab13c70..39ad8260a1 100644
@@ -883,6 +883,30 @@ vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
  	return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
  }
 
+static __rte_always_inline void
+vhost_vring_kick_guest(struct virtio_net *dev, struct vhost_virtqueue *vq)
+{

lib/vhost/vhost.h vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) · line 898

Quoted 10 lines
lib/vhost/vhost.h +10 -0
+	if (dev->notify_ops->guest_notify) {
+		uint16_t qid;
+		for (qid = 0;  qid < dev->nr_vring; qid++) {
+			if (dev->virtqueue[qid] == vq) {
+				if (dev->notify_ops->guest_notify(dev->vid,
+								  qid))
+					goto done;
+				break;
+			}
+		}

Since v22.11, you no more need to iterate through the port's virtqueues, David introduced index field in vhost_virtuqueue in v22.11 (57e414e3ec29 ("vhost: keep a reference to virtqueue index")).

lib/vhost/vhost.h vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) · line 907

Quoted 9 lines
lib/vhost/vhost.h +9 -0
+	}
+	eventfd_write(vq->callfd, (eventfd_t) 1);
+
+done:
+	if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
+		vq->stats.guest_notifications++;
+	if (dev->notify_ops->guest_notified)
+		dev->notify_ops->guest_notified(dev->vid);
+}

FYI, I have done almost the same refactoring in the VDUSE series I will soon send:

https://gitlab.com/mcoquelin/dpdk-next-virtio/-/commit/7089a8a6d1e89b9db90547eb5b4fef886f24fd5b

My version also introduces a new counter in case the notification failed. Maybe we could also have a counter in case the notification has been "offloaded" to the application?

Quoted 44 lines
lib/vhost/vhost.h +6 -15
+
+
  static __rte_always_inline void
  vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
  {
@@ -905,21 +929,13 @@ vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
  		if ((vhost_need_event(vhost_used_event(vq), new, old) &&
  					(vq->callfd >= 0)) ||
  				unlikely(!signalled_used_valid)) {
-			eventfd_write(vq->callfd, (eventfd_t) 1);
-			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
-				vq->stats.guest_notifications++;
-			if (dev->notify_ops->guest_notified)
-				dev->notify_ops->guest_notified(dev->vid);
+			vhost_vring_kick_guest(dev, vq);
  		}
  	} else {
  		/* Kick the guest if necessary. */
  		if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
  				&& (vq->callfd >= 0)) {
-			eventfd_write(vq->callfd, (eventfd_t)1);
-			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
-				vq->stats.guest_notifications++;
-			if (dev->notify_ops->guest_notified)
-				dev->notify_ops->guest_notified(dev->vid);
+			vhost_vring_kick_guest(dev, vq);
  		}
  	}
  }
@@ -971,11 +987,8 @@ vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
  	if (vhost_need_event(off, new, old))
  		kick = true;
  kick:
-	if (kick) {
-		eventfd_write(vq->callfd, (eventfd_t)1);
-		if (dev->notify_ops->guest_notified)
-			dev->notify_ops->guest_notified(dev->vid);
-	}
+	if (kick)
+		vhost_vring_kick_guest(dev, vq);
  }
 
  static __rte_always_inline void
#2 lore raw
Quoted 7 lines · Maxime Coquelin

Hi Eelco,

Quoted 4 lines · Eelco Chaudron

This patch adds an operation callback which gets called every time the library wants to call eventfd_write(). This eventfd_write() call could result in a system call, which could potentially block the PMD thread.

Quoted 8 lines
Quoted 6 lines

The callback function can decide whether it's ok to handle the eventfd_write() now or have the newly introduced function, rte_vhost_notify_guest(), called at a later time.

This can be used by 3rd party applications, like OVS, to avoid system calls being called as part of the PMD threads.

That's a good idea, please find some comments inline:

Thanks for the review, see inline.

Quoted 24 lines
Quoted 24 lines
---
  lib/vhost/rte_vhost.h |   10 +++++++++-
  lib/vhost/vhost.c     |   21 +++++++++++++++++++++
  lib/vhost/vhost.h     |   43 ++++++++++++++++++++++++++++---------------
  3 files changed, 58 insertions(+), 16 deletions(-)
lib/vhost/rte_vhost.h +7 -0
index 58a5d4be92..af7a394d0f 100644
@@ -298,7 +298,13 @@ struct rte_vhost_device_ops {
  	 */
  	void (*guest_notified)(int vid);
 -	void *reserved[1]; /**< Reserved for future extension */
+	/**
+	 * If this callback is registered, notification to the guest can
+	 * be handled by the front-end calling rte_vhost_notify_guest().
+	 * If it's not handled, 'false' should be returned. This can be used
+	 * to remove the "slow" eventfd_write() syscall from the datapath.
+	 */
+	bool (*guest_notify)(int vid, uint16_t queue_id);
  };
Quoted 8 lines

lib/vhost/rte_vhost.h void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, · line 440

Quoted 4 lines
lib/vhost/rte_vhost.h +0 -0
   /**
@@ -433,6 +439,8 @@ void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
   int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
 +void rte_vhost_notify_guest(int vid, uint16_t queue_id);

The new API needs to be tagged as experimental, and also documented. (I see rte_vhost_enable_guest_notification is not properly documented, so not a good example!)

I used exactly that as an example, so thought it should be good ;) Will add this in the next revision...

Quoted 23 lines
Quoted 23 lines
lib/vhost/rte_vhost.h +1 -0
+
  /**
   * Register vhost driver. path could be different for multiple
   * instance support.
lib/vhost/vhost.c +11 -0
index ef37943817..ee090d78ef 100644
@@ -1467,6 +1467,27 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
  	return ret;
  }
 +void
+rte_vhost_notify_guest(int vid, uint16_t queue_id)
+{
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
Quoted 8 lines

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1486

Quoted 5 lines
lib/vhost/vhost.c +5 -0
+
+	rte_spinlock_lock(&vq->access_lock);
+
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);

Maybe we should return an error of callfd is invalid or eventfd_write() failed.

See below

Quoted 23 lines
Quoted 23 lines
lib/vhost/vhost.c +4 -0
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+
  void
  rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
  {
lib/vhost/vhost.h +8 -0
index 8fdab13c70..39ad8260a1 100644
@@ -883,6 +883,30 @@ vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
  	return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
  }
 +static __rte_always_inline void
+vhost_vring_kick_guest(struct virtio_net *dev, struct vhost_virtqueue *vq)
+{
+	if (dev->notify_ops->guest_notify) {
+		uint16_t qid;
+		for (qid = 0;  qid < dev->nr_vring; qid++) {
+			if (dev->virtqueue[qid] == vq) {
+				if (dev->notify_ops->guest_notify(dev->vid,
+								  qid))
Quoted 8 lines

lib/vhost/vhost.h vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) · line 898

Quoted 4 lines
lib/vhost/vhost.h +4 -0
+					goto done;
+				break;
+			}
+		}

Since v22.11, you no more need to iterate through the port's virtqueues, David introduced index field in vhost_virtuqueue in v22.11 (57e414e3ec29 ("vhost: keep a reference to virtqueue index")).

Thanks will change this, I did most of this a while back and did not re-check.

Quoted 13 lines

lib/vhost/vhost.h vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) · line 907

Quoted 9 lines
lib/vhost/vhost.h +9 -0
+	}
+	eventfd_write(vq->callfd, (eventfd_t) 1);
+
+done:
+	if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
+		vq->stats.guest_notifications++;
+	if (dev->notify_ops->guest_notified)
+		dev->notify_ops->guest_notified(dev->vid);
+}

FYI, I have done almost the same refactoring in the VDUSE series I will soon send:

Quoted 5 lines

https://gitlab.com/mcoquelin/dpdk-next-virtio/-/commit/7089a8a6d1e89b9db90547eb5b4fef886f24fd5b

My version also introduces a new counter in case the notification failed. Maybe we could also have a counter in case the notification has been "offloaded" to the application?

What would be the best approach!? I can wait to send a new rev until your patch is out.

Quoted 43 lines
Quoted 43 lines
lib/vhost/vhost.h +6 -15
+
+
  static __rte_always_inline void
  vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
  {
@@ -905,21 +929,13 @@ vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
  		if ((vhost_need_event(vhost_used_event(vq), new, old) &&
  					(vq->callfd >= 0)) ||
  				unlikely(!signalled_used_valid)) {
-			eventfd_write(vq->callfd, (eventfd_t) 1);
-			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
-				vq->stats.guest_notifications++;
-			if (dev->notify_ops->guest_notified)
-				dev->notify_ops->guest_notified(dev->vid);
+			vhost_vring_kick_guest(dev, vq);
  		}
  	} else {
  		/* Kick the guest if necessary. */
  		if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
  				&& (vq->callfd >= 0)) {
-			eventfd_write(vq->callfd, (eventfd_t)1);
-			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
-				vq->stats.guest_notifications++;
-			if (dev->notify_ops->guest_notified)
-				dev->notify_ops->guest_notified(dev->vid);
+			vhost_vring_kick_guest(dev, vq);
  		}
  	}
  }
@@ -971,11 +987,8 @@ vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
  	if (vhost_need_event(off, new, old))
  		kick = true;
  kick:
-	if (kick) {
-		eventfd_write(vq->callfd, (eventfd_t)1);
-		if (dev->notify_ops->guest_notified)
-			dev->notify_ops->guest_notified(dev->vid);
-	}
+	if (kick)
+		vhost_vring_kick_guest(dev, vq);
  }
   static __rte_always_inline void
 
#3 lore raw
In reply to Eelco Chaudron

Hi Eelco,

Quoted 4 lines
lib/vhost/vhost.c +4 -0
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1484

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+

Is spin lock needed here before system call ?

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1490

Quoted 6 lines
lib/vhost/vhost.c +6 -0
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+

Thanks.

#4 lore raw
Quoted 18 lines · Gowrishankar Muthukrishnan

Hi Eelco,

Quoted 4 lines
lib/vhost/vhost.c +4 -0
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1484

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+

Is spin lock needed here before system call ?

I assumed access_lock is protecting all the following fields in this structure, so I need the lock to read the vq->callfd, however, I can/should move the eventfd_write outside of the lock.

Quoted 8 lines

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1490

Quoted 6 lines
lib/vhost/vhost.c +6 -0
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+

Thanks.

#5 lore raw
Quoted 23 lines · Eelco Chaudron
Quoted 18 lines · Gowrishankar Muthukrishnan

Hi Eelco,

Quoted 4 lines
lib/vhost/vhost.c +4 -0
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1484

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+

Is spin lock needed here before system call ?

Quoted 1 line

I assumed access_lock is protecting all the following fields in this structure, so I need the lock to read the vq->callfd, however, I can/should move the eventfd_write outside of the lock.

The FD might be closed between the check and the call to eventfd_write though, but I agree this is not optimal to call the eventfd_write under the spinlock in your case, as you will block the pmd thread if it tries to enqueue/dequeue packets on this queue, defeating the purpose of this patch.

Maybe the solution is to change to read-write locks for the access_lock spinlock. The datapath (rte_vhost_enqueue_burst/rte_vhost_dequeue_burst) and this API would use the read version, meaning they won't lock each other, and the control path (lib/vhost/vhost_user.c) will use the write version.

Does that make sense?

Maxime

Quoted 10 lines
Quoted 9 lines

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1490

Quoted 7 lines
lib/vhost/vhost.c +6 -0
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+

Thanks.

#6 lore raw
Quoted 32 lines · Maxime Coquelin
Quoted 23 lines · Eelco Chaudron
Quoted 18 lines · Gowrishankar Muthukrishnan

Hi Eelco,

Quoted 4 lines
lib/vhost/vhost.c +4 -0
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1484

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+

Is spin lock needed here before system call ?

Quoted 1 line

I assumed access_lock is protecting all the following fields in this structure, so I need the lock to read the vq->callfd, however, I can/should move the eventfd_write outside of the lock.

The FD might be closed between the check and the call to eventfd_write though, but I agree this is not optimal to call the eventfd_write under the spinlock in your case, as you will block the pmd thread if it tries to enqueue/dequeue packets on this queue, defeating the purpose of this patch.

Quoted 7 lines

Maybe the solution is to change to read-write locks for the access_lock spinlock. The datapath (rte_vhost_enqueue_burst/rte_vhost_dequeue_burst) and this API would use the read version, meaning they won't lock each other, and the control path (lib/vhost/vhost_user.c) will use the write version.

Does that make sense?

Yes, this makes sense, let me investigate this and get back.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1490

Quoted 6 lines
lib/vhost/vhost.c +6 -0
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+

Thanks.

#7 lore raw
In reply to Maxime Coquelin
Quoted 32 lines · Maxime Coquelin
Quoted 23 lines · Eelco Chaudron
Quoted 18 lines · Gowrishankar Muthukrishnan

Hi Eelco,

Quoted 4 lines
lib/vhost/vhost.c +4 -0
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1484

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+

Is spin lock needed here before system call ?

Quoted 1 line

I assumed access_lock is protecting all the following fields in this structure, so I need the lock to read the vq->callfd, however, I can/should move the eventfd_write outside of the lock.

The FD might be closed between the check and the call to eventfd_write though, but I agree this is not optimal to call the eventfd_write under the spinlock in your case, as you will block the pmd thread if it tries to enqueue/dequeue packets on this queue, defeating the purpose of this patch.

Quoted 7 lines

Maybe the solution is to change to read-write locks for the access_lock spinlock. The datapath (rte_vhost_enqueue_burst/rte_vhost_dequeue_burst) and this API would use the read version, meaning they won't lock each other, and the control path (lib/vhost/vhost_user.c) will use the write version.

Does that make sense?

Hi Maxime, I prepped a patch, but not the read/write part yet, https://github.com/chaudron/dpdk/commit/d51c93b4ff08b43daed33e3c0fee193a6d039c25#.

I was thinking that even a read/write lock does not work (or maybe we need a combination of taking the read and write lock). As we need to update statistics, which need protection. For example here you see the split (with just two locks, but the sys call could be called in the read lock):

https://github.com/chaudron/dpdk/blob/d51c93b4ff08b43daed33e3c0fee193a6d039c25/lib/vhost/vhost.c#L1493

The best would be to not have a lock when calling the system call, but that does not seem safe. I do not see a clear solution, guess also as I have some trouble understanding the lock rules around vq’s.

Do you have some more insights to share? I can ping you offline and discuss this.

Cheers,

Eelco

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1490

Quoted 7 lines
lib/vhost/vhost.c +6 -0
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+

Thanks.

#8 lore raw

Hi Eelco,

Quoted 44 lines · Eelco Chaudron
Quoted 32 lines · Maxime Coquelin
Quoted 23 lines · Eelco Chaudron
Quoted 18 lines · Gowrishankar Muthukrishnan

Hi Eelco,

Quoted 4 lines
lib/vhost/vhost.c +4 -0
+void
+rte_vhost_notify_guest(int vid, uint16_t queue_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1484

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+
+	if (!dev ||  queue_id >= VHOST_MAX_VRING)
+		return;
+
+	vq = dev->virtqueue[queue_id];
+	if (!vq)
+		return;
+
+	rte_spinlock_lock(&vq->access_lock);
+

Is spin lock needed here before system call ?

Quoted 1 line

I assumed access_lock is protecting all the following fields in this structure, so I need the lock to read the vq->callfd, however, I can/should move the eventfd_write outside of the lock.

The FD might be closed between the check and the call to eventfd_write though, but I agree this is not optimal to call the eventfd_write under the spinlock in your case, as you will block the pmd thread if it tries to enqueue/dequeue packets on this queue, defeating the purpose of this patch.

Quoted 7 lines

Maybe the solution is to change to read-write locks for the access_lock spinlock. The datapath (rte_vhost_enqueue_burst/rte_vhost_dequeue_burst) and this API would use the read version, meaning they won't lock each other, and the control path (lib/vhost/vhost_user.c) will use the write version.

Does that make sense?

Quoted 8 lines

Hi Maxime, I prepped a patch, but not the read/write part yet, https://github.com/chaudron/dpdk/commit/d51c93b4ff08b43daed33e3c0fee193a6d039c25#.

I was thinking that even a read/write lock does not work (or maybe we need a combination of taking the read and write lock). As we need to update statistics, which need protection. For example here you see the split (with just two locks, but the sys call could be called in the read lock):

https://github.com/chaudron/dpdk/blob/d51c93b4ff08b43daed33e3c0fee193a6d039c25/lib/vhost/vhost.c#L1493

The best would be to not have a lock when calling the system call, but that does not seem safe. I do not see a clear solution, guess also as I have some trouble understanding the lock rules around vq’s.

The lock is indeed required. Maybe we can use read-lock, and use atomic operations for counters that could be accessed by several threads?

Quoted 2 lines

Do you have some more insights to share? I can ping you offline and discuss this.

Sure, I'll be happy to discuss more about this.

Thanks,
Maxime

Quoted 15 lines

Cheers,

Eelco

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

lib/vhost/vhost.c rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) · line 1490

Quoted 7 lines
lib/vhost/vhost.c +6 -0
+	if (vq->callfd >= 0)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+
+	rte_spinlock_unlock(&vq->access_lock);
+}
+

Thanks.