The .remove() callback of a driver is normally not mandatory, and the
increase of devm_ functions availability makes it even less so. For a
virtio_driver it's not marked explicitly as optional, but nothing
enforces its presence, so check for its existence before calling it.

The NULL dereference that led to this fix was seen in the wild for the
spi-virtio driver, which is currently the only in-tree virtio driver
without a remove callback.

Fixes: 74b2553f1d13 ("virtio: fix module/device unloading")
Signed-off-by: Francesco Valla <[email protected]>
---
The behavior of the spi-virtio driver is not conformant to the virtio
framework guidelines, but that will be tackled in a different series.
---
 drivers/virtio/virtio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index a8588d7ad109..067d32c67b44 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -374,7 +374,8 @@ static void virtio_dev_remove(struct device *_d)
 
        virtio_config_core_disable(dev);
 
-       drv->remove(dev);
+       if (drv->remove)
+               drv->remove(dev);
 
        /* Driver should have reset device. */
        WARN_ON_ONCE(dev->config->get_status(dev));

---
base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd
change-id: 20261108-virtio_drv_remove_fix-5ba8e3123d7e

Best regards,
--  
Francesco Valla <[email protected]>


Reply via email to