Skip to contentSkip to conversation

vhost: fix vhost interrupt support

PATCH Applied Tiwei Bie 3 participants 7 messages Sep 4, 2018 – Sep 14, 2018

lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

PATCH 1/1 vhost: fix vhost interrupt support

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
---
 lib/librte_vhost/Makefile |  1 +
 lib/librte_vhost/vhost.c  | 18 ++++++++++++------
 lib/librte_vhost/vhost.h  |  2 ++
 3 files changed, 15 insertions(+), 6 deletions(-)
lib/librte_vhost/Makefile +1 -0
index de431fbb7..531cf4832 100644
@@ -13,6 +13,7 @@ LIBABIVER := 4
 CFLAGS += -DALLOW_EXPERIMENTAL_API
 CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -D_FILE_OFFSET_BITS=64
 CFLAGS += -I vhost_user
+CFLAGS += -fno-strict-aliasing
 LDLIBS += -lpthread
 
 ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
lib/librte_vhost/vhost.c +12 -6
index 3c9be10a0..88b1781d5 100644
@@ -646,12 +646,18 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
 }
 
 static inline void
-vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
+vhost_enable_notify_split(struct virtio_net *dev,
+		struct vhost_virtqueue *vq, int enable)
 {
-	if (enable)
-		vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
-	else
-		vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
+		if (enable)
+			vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
+		else
+			vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	} else {
+		if (enable)
+			vhost_avail_event(vq) = vq->last_avail_idx;
+	}
 }
 
 static inline void
@@ -689,7 +695,7 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
 	if (vq_is_packed(dev))
 		vhost_enable_notify_packed(dev, vq, enable);
 	else
-		vhost_enable_notify_split(vq, enable);
+		vhost_enable_notify_split(dev, vq, enable);
 
 	return 0;
 }
lib/librte_vhost/vhost.h +2 -0
index 760a09c0d..25ffd7614 100644
@@ -648,6 +648,8 @@ vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
 }
 
+#define vhost_avail_event(vr) \
+	(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
 #define vhost_used_event(vr) \
 	(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
 
Signature · 1 line
2.18.0
#1 lore raw

Hi Tiwei,

Quoted 18 lines · Tiwei Bie

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
---
  lib/librte_vhost/Makefile |  1 +
  lib/librte_vhost/vhost.c  | 18 ++++++++++++------
  lib/librte_vhost/vhost.h  |  2 ++
  3 files changed, 15 insertions(+), 6 deletions(-)
lib/librte_vhost/Makefile +0 -0
index de431fbb7..531cf4832 100644

lib/librte_vhost/Makefile LIBABIVER := 4 · line 16

Quoted 4 lines
lib/librte_vhost/Makefile +1 -0
@@ -13,6 +13,7 @@ LIBABIVER := 4
  CFLAGS += -DALLOW_EXPERIMENTAL_API
  CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -D_FILE_OFFSET_BITS=64
  CFLAGS += -I vhost_user
+CFLAGS += -fno-strict-aliasing

I'm not clear why this is needed looking at the code below, could you please explain?

Quoted 54 lines
lib/librte_vhost/Makefile +0 -0
  LDLIBS += -lpthread
 
  ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
lib/librte_vhost/vhost.c +12 -6
index 3c9be10a0..88b1781d5 100644
@@ -646,12 +646,18 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
  }
 
  static inline void
-vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
+vhost_enable_notify_split(struct virtio_net *dev,
+		struct vhost_virtqueue *vq, int enable)
  {
-	if (enable)
-		vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
-	else
-		vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
+		if (enable)
+			vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
+		else
+			vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	} else {
+		if (enable)
+			vhost_avail_event(vq) = vq->last_avail_idx;
+	}
  }
 
  static inline void
@@ -689,7 +695,7 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
  	if (vq_is_packed(dev))
  		vhost_enable_notify_packed(dev, vq, enable);
  	else
-		vhost_enable_notify_split(vq, enable);
+		vhost_enable_notify_split(dev, vq, enable);
 
  	return 0;
  }
lib/librte_vhost/vhost.h +2 -0
index 760a09c0d..25ffd7614 100644
@@ -648,6 +648,8 @@ vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
  	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
  }
 
+#define vhost_avail_event(vr) \
+	(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
  #define vhost_used_event(vr) \
  	(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
 
author
#2 lore raw

Hi Maxime,

Quoted 16 lines · Maxime Coquelin

Hi Tiwei,

Quoted 13 lines · Tiwei Bie

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
---
  lib/librte_vhost/Makefile |  1 +
  lib/librte_vhost/vhost.c  | 18 ++++++++++++------
  lib/librte_vhost/vhost.h  |  2 ++
  3 files changed, 15 insertions(+), 6 deletions(-)
Quoted 12 lines

lib/librte_vhost/Makefile LIBABIVER := 4 · line 16

Quoted 9 lines
lib/librte_vhost/Makefile +1 -0
index de431fbb7..531cf4832 100644
@@ -13,6 +13,7 @@ LIBABIVER := 4
  CFLAGS += -DALLOW_EXPERIMENTAL_API
  CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -D_FILE_OFFSET_BITS=64
  CFLAGS += -I vhost_user
+CFLAGS += -fno-strict-aliasing

I'm not clear why this is needed looking at the code below, could you please explain?

Without this, we will get below build error:

lib/librte_vhost/vhost.c: In function ‘vhost_enable_notify_split’:
lib/librte_vhost/vhost.h:656:4: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/librte_vhost/vhost.c:659:4: note: in expansion of macro ‘vhost_avail_event’
    vhost_avail_event(vq) = vq->last_avail_idx;
    ^~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

Quoted 49 lines
Quoted 49 lines
lib/librte_vhost/Makefile +0 -0
  LDLIBS += -lpthread
  ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
lib/librte_vhost/vhost.c +12 -6
index 3c9be10a0..88b1781d5 100644
@@ -646,12 +646,18 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
  }
  static inline void
-vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
+vhost_enable_notify_split(struct virtio_net *dev,
+		struct vhost_virtqueue *vq, int enable)
  {
-	if (enable)
-		vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
-	else
-		vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
+		if (enable)
+			vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
+		else
+			vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	} else {
+		if (enable)
+			vhost_avail_event(vq) = vq->last_avail_idx;
+	}
  }
  static inline void
@@ -689,7 +695,7 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
  	if (vq_is_packed(dev))
  		vhost_enable_notify_packed(dev, vq, enable);
  	else
-		vhost_enable_notify_split(vq, enable);
+		vhost_enable_notify_split(dev, vq, enable);
  	return 0;
  }
lib/librte_vhost/vhost.h +2 -0
index 760a09c0d..25ffd7614 100644
@@ -648,6 +648,8 @@ vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
  	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
  }
+#define vhost_avail_event(vr) \
+	(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
  #define vhost_used_event(vr) \
  	(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
 
Reviewed-by
#3 lore raw
Quoted 34 lines · Tiwei Bie

Hi Maxime,

Quoted 16 lines · Maxime Coquelin

Hi Tiwei,

Quoted 13 lines · Tiwei Bie

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
---
   lib/librte_vhost/Makefile |  1 +
   lib/librte_vhost/vhost.c  | 18 ++++++++++++------
   lib/librte_vhost/vhost.h  |  2 ++
   3 files changed, 15 insertions(+), 6 deletions(-)
Quoted 12 lines

lib/librte_vhost/Makefile LIBABIVER := 4 · line 16

Quoted 9 lines
lib/librte_vhost/Makefile +1 -0
index de431fbb7..531cf4832 100644
@@ -13,6 +13,7 @@ LIBABIVER := 4
   CFLAGS += -DALLOW_EXPERIMENTAL_API
   CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -D_FILE_OFFSET_BITS=64
   CFLAGS += -I vhost_user
+CFLAGS += -fno-strict-aliasing

I'm not clear why this is needed looking at the code below, could you please explain?

Without this, we will get below build error:

Quoted 8 lines

lib/librte_vhost/vhost.c: In function ‘vhost_enable_notify_split’:
lib/librte_vhost/vhost.h:656:4: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])

     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/librte_vhost/vhost.c:659:4: note: in expansion of macro ‘vhost_avail_event’
     vhost_avail_event(vq) = vq->last_avail_idx;
     ^~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

OK, thanks for the info.

Maxime

#4 lore raw
In reply to Tiwei Bie
Quoted 34 lines · Tiwei Bie

Hi Maxime,

Quoted 16 lines · Maxime Coquelin

Hi Tiwei,

Quoted 13 lines · Tiwei Bie

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
---
  lib/librte_vhost/Makefile |  1 +
  lib/librte_vhost/vhost.c  | 18 ++++++++++++------
  lib/librte_vhost/vhost.h  |  2 ++
  3 files changed, 15 insertions(+), 6 deletions(-)
Quoted 12 lines

lib/librte_vhost/Makefile LIBABIVER := 4 · line 16

Quoted 9 lines
lib/librte_vhost/Makefile +1 -0
index de431fbb7..531cf4832 100644
@@ -13,6 +13,7 @@ LIBABIVER := 4
  CFLAGS += -DALLOW_EXPERIMENTAL_API
  CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -D_FILE_OFFSET_BITS=64
  CFLAGS += -I vhost_user
+CFLAGS += -fno-strict-aliasing

I'm not clear why this is needed looking at the code below, could you please explain?

Without this, we will get below build error:

Quoted 9 lines

lib/librte_vhost/vhost.c: In function ‘vhost_enable_notify_split’:
lib/librte_vhost/vhost.h:656:4: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/librte_vhost/vhost.c:659:4: note: in expansion of macro ‘vhost_avail_event’
    vhost_avail_event(vq) = vq->last_avail_idx;
    ^~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

Getting this error with meson build system, since "-fno-strict-aliasing" is not added to meson build file. BUT instead of disabling compiler warning,

first element of the "struct vring_used_elem ring[0]" is "uint32_t id;", so it should be accessed as uint32_t. What about following:

#define vhost_avail_event(vr) \
- (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
+ (*(volatile uint32_t*)&(vr)->used->ring[(vr)->size])

And for "vhost_avail_event(vq) = vq->last_avail_idx;", "last_avail_idx" is uint16_t, so assigning from uint16_t to uint32_t should be ok too.

If above works for you please send a patch for update also removing "-fno-strict-aliasing", this can be rebased on existing patch.

If not works, please at least send a patch to add flag to meson build file

Thanks,
ferruh

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
lib/librte_vhost/Makefile +0 -0
  LDLIBS += -lpthread
  ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
lib/librte_vhost/vhost.c +12 -6
index 3c9be10a0..88b1781d5 100644
@@ -646,12 +646,18 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
  }
  static inline void
-vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
+vhost_enable_notify_split(struct virtio_net *dev,
+		struct vhost_virtqueue *vq, int enable)
  {
-	if (enable)
-		vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
-	else
-		vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
+		if (enable)
+			vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
+		else
+			vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	} else {
+		if (enable)
+			vhost_avail_event(vq) = vq->last_avail_idx;
+	}
  }
  static inline void
@@ -689,7 +695,7 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
  	if (vq_is_packed(dev))
  		vhost_enable_notify_packed(dev, vq, enable);
  	else
-		vhost_enable_notify_split(vq, enable);
+		vhost_enable_notify_split(dev, vq, enable);
  	return 0;
  }
lib/librte_vhost/vhost.h +2 -0
index 760a09c0d..25ffd7614 100644
@@ -648,6 +648,8 @@ vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
  	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
  }
+#define vhost_avail_event(vr) \
+	(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
  #define vhost_used_event(vr) \
  	(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
 
author
#5 lore raw
Quoted 45 lines · Ferruh Yigit
Quoted 34 lines · Tiwei Bie

Hi Maxime,

Quoted 16 lines · Maxime Coquelin

Hi Tiwei,

Quoted 13 lines · Tiwei Bie

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
---
  lib/librte_vhost/Makefile |  1 +
  lib/librte_vhost/vhost.c  | 18 ++++++++++++------
  lib/librte_vhost/vhost.h  |  2 ++
  3 files changed, 15 insertions(+), 6 deletions(-)
Quoted 12 lines

lib/librte_vhost/Makefile LIBABIVER := 4 · line 16

Quoted 9 lines
lib/librte_vhost/Makefile +1 -0
index de431fbb7..531cf4832 100644
@@ -13,6 +13,7 @@ LIBABIVER := 4
  CFLAGS += -DALLOW_EXPERIMENTAL_API
  CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -D_FILE_OFFSET_BITS=64
  CFLAGS += -I vhost_user
+CFLAGS += -fno-strict-aliasing

I'm not clear why this is needed looking at the code below, could you please explain?

Without this, we will get below build error:

Quoted 9 lines

lib/librte_vhost/vhost.c: In function ‘vhost_enable_notify_split’:
lib/librte_vhost/vhost.h:656:4: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/librte_vhost/vhost.c:659:4: note: in expansion of macro ‘vhost_avail_event’
    vhost_avail_event(vq) = vq->last_avail_idx;
    ^~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

Quoted 2 lines

Getting this error with meson build system, since "-fno-strict-aliasing" is not added to meson build file.

Thanks! I didn't notice this. I just tried the meson build manually. The warning isn't treated as errors on my side. So it was not caught by the build script..

Quoted 11 lines

BUT instead of disabling compiler warning,

first element of the "struct vring_used_elem ring[0]" is "uint32_t id;", so it should be accessed as uint32_t. What about following:

#define vhost_avail_event(vr) \
- (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
+ (*(volatile uint32_t*)&(vr)->used->ring[(vr)->size])

And for "vhost_avail_event(vq) = vq->last_avail_idx;", "last_avail_idx" is uint16_t, so assigning from uint16_t to uint32_t should be ok too.

At the end of used ring, there is a 16bits event_idx. It can't be interpreted as a vring_used_elem and it's also not part of a vring_used_elem. This is the traditional way how we access it:

https://github.com/torvalds/linux/blob/a06b0c82a049/drivers/vhost/vhost.c#L50
https://github.com/freebsd/freebsd/blob/209b94df8701/sys/dev/virtio/virtio_ring.h#L133

Quoted 5 lines

If above works for you please send a patch for update also removing "-fno-strict-aliasing", this can be rebased on existing patch.

If not works, please at least send a patch to add flag to meson build file

I'll send a patch to add flag to meson build file.

Thanks!

Quoted 54 lines

Thanks,
ferruh

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
lib/librte_vhost/Makefile +0 -0
  LDLIBS += -lpthread
  ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
lib/librte_vhost/vhost.c +12 -6
index 3c9be10a0..88b1781d5 100644
@@ -646,12 +646,18 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
  }
  static inline void
-vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
+vhost_enable_notify_split(struct virtio_net *dev,
+		struct vhost_virtqueue *vq, int enable)
  {
-	if (enable)
-		vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
-	else
-		vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
+		if (enable)
+			vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
+		else
+			vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+	} else {
+		if (enable)
+			vhost_avail_event(vq) = vq->last_avail_idx;
+	}
  }
  static inline void
@@ -689,7 +695,7 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
  	if (vq_is_packed(dev))
  		vhost_enable_notify_packed(dev, vq, enable);
  	else
-		vhost_enable_notify_split(vq, enable);
+		vhost_enable_notify_split(dev, vq, enable);
  	return 0;
  }
lib/librte_vhost/vhost.h +2 -0
index 760a09c0d..25ffd7614 100644
@@ -648,6 +648,8 @@ vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
  	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
  }
+#define vhost_avail_event(vr) \
+	(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
  #define vhost_used_event(vr) \
  	(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
 
#6 lore raw
In reply to Tiwei Bie
Quoted 6 lines · Tiwei Bie

When VIRTIO_RING_F_EVENT_IDX is negotiated, we need to update the avail event to enable the notification.

Fixes 3f8ff12821e4 ("vhost: support interrupt mode")
Quoted 6 lines
---
  lib/librte_vhost/Makefile |  1 +
  lib/librte_vhost/vhost.c  | 18 ++++++++++++------
  lib/librte_vhost/vhost.h  |  2 ++
  3 files changed, 15 insertions(+), 6 deletions(-)

Applied to dpdk-next-virtio/master.

Thanks!
Maxime