Skip to contentSkip to conversation

vhost: NULL callback checks

PATCH Applied Kevin Traynor 2 participants 4 messages Jan 24, 2025 – Feb 7, 2025

lore
Share & tools
lore
#0 lore raw

cover vhost: NULL callback checks

Not a normal case, but noticed that setting some callbacks to NULL in rte_vhost_driver_callback_register() causes a seg fault e.g. [0].

Added missing checks before calling. Alternative fix is to make some callbacks mandatory and check during register - is that an API change? maybe, so took simpler approach.

A bit hard to find fixed commit due to the time and amount of development in vhost code.

[0]

3302│         if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3303├>                if (dev->notify_ops->new_device(dev->vid) == 0)
3304│                         dev->flags |= VIRTIO_DEV_RUNNING;
3305│         }

(gdb) frame 1
at ../lib/vhost/vhost_user.c:3303
(gdb) p dev->notify_ops->new_device
$3 = (int (*)(int)) 0x0

Kevin Traynor (1):
vhost: add NULL callback checks

 lib/vhost/vduse.c      | 3 ++-
 lib/vhost/vhost.c      | 5 +++--
 lib/vhost/vhost_user.c | 3 ++-
 3 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/1 vhost: add NULL callback checks

rte_vhost_driver_callback_register() does not specify any mandatory callbacks in struct rte_vhost_device_ops.

Add some missing NULL checks before calling rte_vhost_device_ops callbacks.

Fixes 4796ad63ba1f ("examples/vhost: import userspace vhost application")
---
 lib/vhost/vduse.c      | 3 ++-
 lib/vhost/vhost.c      | 5 +++--
 lib/vhost/vhost_user.c | 3 ++-
 3 files changed, 7 insertions(+), 4 deletions(-)
lib/vhost/vduse.c +2 -1
index 8ba58555f9..be548f051e 100644
@@ -319,5 +319,6 @@ vduse_device_start(struct virtio_net *dev, bool reconnect)
 	dev->flags |= VIRTIO_DEV_READY;
 
-	if (dev->notify_ops->new_device(dev->vid) == 0)
+	if (!dev->notify_ops->new_device ||
+		dev->notify_ops->new_device(dev->vid) == 0)
 		dev->flags |= VIRTIO_DEV_RUNNING;
 
lib/vhost/vhost.c +3 -2
index 5a50a06f8d..c41c29ca57 100644
@@ -752,8 +752,9 @@ vhost_destroy_device_notify(struct virtio_net *dev)
 	if (dev->flags & VIRTIO_DEV_RUNNING) {
 		vdpa_dev = dev->vdpa_dev;
-		if (vdpa_dev)
+		if (vdpa_dev && vdpa_dev->ops->dev_close)
 			vdpa_dev->ops->dev_close(dev->vid);
 		dev->flags &= ~VIRTIO_DEV_RUNNING;
-		dev->notify_ops->destroy_device(dev->vid);
+		if (dev->notify_ops->destroy_device)
+			dev->notify_ops->destroy_device(dev->vid);
 	}
 }
lib/vhost/vhost_user.c +2 -1
index 52d8078d7c..26dc0bde97 100644
@@ -3302,5 +3302,6 @@ vhost_user_msg_handler(int vid, int fd)
 
 	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
-		if (dev->notify_ops->new_device(dev->vid) == 0)
+		if (!dev->notify_ops->new_device ||
+			dev->notify_ops->new_device(dev->vid) == 0)
 			dev->flags |= VIRTIO_DEV_RUNNING;
 	}
Signature · 1 line
2.48.1
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH 1/1] vhost: add NULL callback checks

Quoted 49 lines · Kevin Traynor

rte_vhost_driver_callback_register() does not specify any mandatory callbacks in struct rte_vhost_device_ops.

Add some missing NULL checks before calling rte_vhost_device_ops callbacks.

Fixes 4796ad63ba1f ("examples/vhost: import userspace vhost application")
---
  lib/vhost/vduse.c      | 3 ++-
  lib/vhost/vhost.c      | 5 +++--
  lib/vhost/vhost_user.c | 3 ++-
  3 files changed, 7 insertions(+), 4 deletions(-)
lib/vhost/vduse.c +2 -1
index 8ba58555f9..be548f051e 100644
@@ -319,5 +319,6 @@ vduse_device_start(struct virtio_net *dev, bool reconnect)
  	dev->flags |= VIRTIO_DEV_READY;
 
-	if (dev->notify_ops->new_device(dev->vid) == 0)
+	if (!dev->notify_ops->new_device ||
+		dev->notify_ops->new_device(dev->vid) == 0)
  		dev->flags |= VIRTIO_DEV_RUNNING;
 
lib/vhost/vhost.c +3 -2
index 5a50a06f8d..c41c29ca57 100644
@@ -752,8 +752,9 @@ vhost_destroy_device_notify(struct virtio_net *dev)
  	if (dev->flags & VIRTIO_DEV_RUNNING) {
  		vdpa_dev = dev->vdpa_dev;
-		if (vdpa_dev)
+		if (vdpa_dev && vdpa_dev->ops->dev_close)
  			vdpa_dev->ops->dev_close(dev->vid);
  		dev->flags &= ~VIRTIO_DEV_RUNNING;
-		dev->notify_ops->destroy_device(dev->vid);
+		if (dev->notify_ops->destroy_device)
+			dev->notify_ops->destroy_device(dev->vid);
  	}
  }
lib/vhost/vhost_user.c +0 -0
index 52d8078d7c..26dc0bde97 100644

lib/vhost/vhost_user.c vhost_user_msg_handler(int vid, int fd) · line 3307

Quoted 7 lines
lib/vhost/vhost_user.c +2 -1
@@ -3302,5 +3302,6 @@ vhost_user_msg_handler(int vid, int fd)
 
  	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
-		if (dev->notify_ops->new_device(dev->vid) == 0)
+		if (!dev->notify_ops->new_device ||
+			dev->notify_ops->new_device(dev->vid) == 0)
  			dev->flags |= VIRTIO_DEV_RUNNING;
  	}

Thanks,
Maxime

#3 lore raw

Re: [PATCH 1/1] vhost: add NULL callback checks

In reply to Kevin Traynor
Quoted 9 lines · Kevin Traynor

rte_vhost_driver_callback_register() does not specify any mandatory callbacks in struct rte_vhost_device_ops.

Add some missing NULL checks before calling rte_vhost_device_ops callbacks.

Fixes 4796ad63ba1f ("examples/vhost: import userspace vhost application")
Quoted 6 lines
---
  lib/vhost/vduse.c      | 3 ++-
  lib/vhost/vhost.c      | 5 +++--
  lib/vhost/vhost_user.c | 3 ++-
  3 files changed, 7 insertions(+), 4 deletions(-

Applied to next-virtio/for-next-net.

Thanks,
Maxime