Skip to contentSkip to conversation

net/vhost-net: do not assert on null pointer return from tap_get_vhost_net()

PATCH In review Ani Sinha 4 participants 6 messages Jun 28, 2023

lore
Share & tools
lore

Ani Sinha

author
#0 lore raw

PATCH 1/1 net/vhost-net: do not assert on null pointer return from tap_get_vhost_net()

When 'vhost=off' or no vhost specific options at all are passed for the tap net-device backend, tap_get_vhost_net() can return NULL. The function net_init_tap_one() does not call vhost_net_init() on such cases and therefore vhost_net pointer within the tap device state structure remains NULL. Hence, assertion here on a NULL pointer return from tap_get_vhost_net() would not be correct. Remove it and fix the crash generated by qemu upon initialization in the following call chain :

qdev_realize() -> pci_qdev_realize() -> virtio_device_realize() -> virtio_bus_device_plugged() -> virtio_net_get_features() -> get_vhost_net()

Fixes 0e994668d00c9c ("vhost_net: add an assertion for TAP client backends")
---
 hw/net/vhost_net.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/vhost_net.c +6 -1
index 6db23ca323..6b958d6363 100644
@@ -507,7 +507,12 @@ VHostNetState *get_vhost_net(NetClientState *nc)
     switch (nc->info->type) {
     case NET_CLIENT_DRIVER_TAP:
         vhost_net = tap_get_vhost_net(nc);
-        assert(vhost_net);
+        /*
+         * tap_get_vhost_net() can return NULL if a tap net-device backend is
+         * created with 'vhost=off' option, 'vhostforce=off' or no vhost or
+         * vhostforce or vhostfd options at all. Please see net_init_tap_one().
+         * Hence, we omit the assertion here.
+         */
         break;
 #ifdef CONFIG_VHOST_NET_USER
     case NET_CLIENT_DRIVER_VHOST_USER:
Signature · 1 line
2.39.1
Reviewed-by Cédric Le Goater Tested-by Cédric Le Goater on v1 1/1
#1 lore raw
Quoted 8 lines · Ani Sinha

When 'vhost=off' or no vhost specific options at all are passed for the tap net-device backend, tap_get_vhost_net() can return NULL. The function net_init_tap_one() does not call vhost_net_init() on such cases and therefore vhost_net pointer within the tap device state structure remains NULL. Hence, assertion here on a NULL pointer return from tap_get_vhost_net() would not be correct. Remove it and fix the crash generated by qemu upon initialization in the following call chain :

Quoted 5 lines

qdev_realize() -> pci_qdev_realize() -> virtio_device_realize() -> virtio_bus_device_plugged() -> virtio_net_get_features() -> get_vhost_net()

Fixes 0e994668d00c9c ("vhost_net: add an assertion for TAP client backends")

Thanks !

C.

Quoted 22 lines
---
  hw/net/vhost_net.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/vhost_net.c +6 -1
index 6db23ca323..6b958d6363 100644
@@ -507,7 +507,12 @@ VHostNetState *get_vhost_net(NetClientState *nc)
      switch (nc->info->type) {
      case NET_CLIENT_DRIVER_TAP:
          vhost_net = tap_get_vhost_net(nc);
-        assert(vhost_net);
+        /*
+         * tap_get_vhost_net() can return NULL if a tap net-device backend is
+         * created with 'vhost=off' option, 'vhostforce=off' or no vhost or
+         * vhostforce or vhostfd options at all. Please see net_init_tap_one().
+         * Hence, we omit the assertion here.
+         */
          break;
  #ifdef CONFIG_VHOST_NET_USER
      case NET_CLIENT_DRIVER_VHOST_USER:
#2 lore raw
In reply to Ani Sinha
Quoted 8 lines · Ani Sinha

When 'vhost=off' or no vhost specific options at all are passed for the tap net-device backend, tap_get_vhost_net() can return NULL. The function net_init_tap_one() does not call vhost_net_init() on such cases and therefore vhost_net pointer within the tap device state structure remains NULL. Hence, assertion here on a NULL pointer return from tap_get_vhost_net() would not be correct. Remove it and fix the crash generated by qemu upon initialization in the following call chain :

Quoted 5 lines

qdev_realize() -> pci_qdev_realize() -> virtio_device_realize() -> virtio_bus_device_plugged() -> virtio_net_get_features() -> get_vhost_net()

Fixes 0e994668d00c9c ("vhost_net: add an assertion for TAP client backends")

I added a bunch of tags and sent it upstream. Take a look at the pull request so you can do it yourself going forward, pls.

Quoted 24 lines
---
 hw/net/vhost_net.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/vhost_net.c +6 -1
index 6db23ca323..6b958d6363 100644
@@ -507,7 +507,12 @@ VHostNetState *get_vhost_net(NetClientState *nc)
     switch (nc->info->type) {
     case NET_CLIENT_DRIVER_TAP:
         vhost_net = tap_get_vhost_net(nc);
-        assert(vhost_net);
+        /*
+         * tap_get_vhost_net() can return NULL if a tap net-device backend is
+         * created with 'vhost=off' option, 'vhostforce=off' or no vhost or
+         * vhostforce or vhostfd options at all. Please see net_init_tap_one().
+         * Hence, we omit the assertion here.
+         */
         break;
 #ifdef CONFIG_VHOST_NET_USER
     case NET_CLIENT_DRIVER_VHOST_USER:
Signature · 1 line
2.39.1
author
#3 lore raw
Quoted 16 lines · Michael S. Tsirkin
Quoted 11 lines · Ani Sinha

When 'vhost=off' or no vhost specific options at all are passed for the tap net-device backend, tap_get_vhost_net() can return NULL. The function net_init_tap_one() does not call vhost_net_init() on such cases and therefore vhost_net pointer within the tap device state structure remains NULL. Hence, assertion here on a NULL pointer return from tap_get_vhost_net() would not be correct. Remove it and fix the crash generated by qemu upon initialization in the following call chain :

qdev_realize() -> pci_qdev_realize() -> virtio_device_realize() -> virtio_bus_device_plugged() -> virtio_net_get_features() -> get_vhost_net()

Quoted 6 lines
Quoted 2 lines
Fixes 0e994668d00c9c ("vhost_net: add an assertion for TAP client backends")

I added a bunch of tags and sent it upstream. Take a look at the pull request so you can do it yourself going forward, pls.

I thought only maintainers sends PR? Do you have any handy scripts?

Quoted 26 lines
Quoted 25 lines

---
hw/net/vhost_net.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/vhost_net.c +6 -1
index 6db23ca323..6b958d6363 100644
@@ -507,7 +507,12 @@ VHostNetState *get_vhost_net(NetClientState *nc)
    switch (nc->info->type) {
    case NET_CLIENT_DRIVER_TAP:
        vhost_net = tap_get_vhost_net(nc);
-        assert(vhost_net);
+        /*
+         * tap_get_vhost_net() can return NULL if a tap net-device backend is
+         * created with 'vhost=off' option, 'vhostforce=off' or no vhost or
+         * vhostforce or vhostfd options at all. Please see net_init_tap_one().
+         * Hence, we omit the assertion here.
+         */
        break;
 #ifdef CONFIG_VHOST_NET_USER
    case NET_CLIENT_DRIVER_VHOST_USER:
Signature · 1 line
2.39.1
#4 lore raw
Quoted 16 lines · Ani Sinha
Quoted 16 lines · Michael S. Tsirkin
Quoted 11 lines · Ani Sinha

When 'vhost=off' or no vhost specific options at all are passed for the tap net-device backend, tap_get_vhost_net() can return NULL. The function net_init_tap_one() does not call vhost_net_init() on such cases and therefore vhost_net pointer within the tap device state structure remains NULL. Hence, assertion here on a NULL pointer return from tap_get_vhost_net() would not be correct. Remove it and fix the crash generated by qemu upon initialization in the following call chain :

qdev_realize() -> pci_qdev_realize() -> virtio_device_realize() -> virtio_bus_device_plugged() -> virtio_net_get_features() -> get_vhost_net()

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Fixes 0e994668d00c9c ("vhost_net: add an assertion for TAP client backends")

I added a bunch of tags and sent it upstream. Take a look at the pull request so you can do it yourself going forward, pls.

I thought only maintainers sends PR? Do you have any handy scripts?

https://github.com/stefanha/git-publish/blob/master/git-publish.pod#sending-pull-requests

author
#5 lore raw
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Ani Sinha
Quoted 17 lines · Michael S. Tsirkin
Quoted 13 lines · Ani Sinha

When 'vhost=off' or no vhost specific options at all are passed for the tap net-device backend, tap_get_vhost_net() can return NULL. The function net_init_tap_one() does not call vhost_net_init() on such cases and therefore vhost_net pointer within the tap device state structure remains NULL. Hence, assertion here on a NULL pointer return from tap_get_vhost_net() would not be correct. Remove it and fix the crash generated by qemu upon initialization in the following call chain :

qdev_realize() -> pci_qdev_realize() -> virtio_device_realize() -> virtio_bus_device_plugged() -> virtio_net_get_features() -> get_vhost_net()

Fixes 0e994668d00c9c ("vhost_net: add an assertion for TAP client backends")
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

I added a bunch of tags and sent it upstream. Take a look at the pull request so you can do it yourself going forward, pls.

I thought only maintainers sends PR? Do you have any handy scripts?

https://github.com/stefanha/git-publish/blob/master/git-publish.pod#sending-pull-requests

Cool! Thanks Phil! Will check this and talk to Stefan.