Signed-off-by: Ricardo Ribalda ribalda@chromium.org --- Changes in v4: Thanks Laurent. - Remove refcounted cleaup to support devres. - Link to v3: https://lore.kernel.org/r/20241105-uvc-crashrmmod-v3-1-c0959c8906d3@chromium...
Changes in v3: Thanks Sakari. - Rename variable to initialized. - Other CodeStyle. - Link to v2: https://lore.kernel.org/r/20241105-uvc-crashrmmod-v2-1-547ce6a6962e@chromium...
Changes in v2: Thanks to Laurent. - The main structure is not allocated with devres so there is a small period of time where we can get an irq with the structure free. Do not use devres for the IRQ. - Link to v1: https://lore.kernel.org/r/20241031-uvc-crashrmmod-v1-1-059fe593b1e6@chromium...
--- Ricardo Ribalda (2): media: uvcvideo: Remove refcounted cleanup media: uvcvideo: Fix crash during unbind if gpio unit is in use
drivers/media/usb/uvc/uvc_driver.c | 30 ++++++++---------------------- drivers/media/usb/uvc/uvcvideo.h | 1 - 2 files changed, 8 insertions(+), 23 deletions(-) --- base-commit: c7ccf3683ac9746b263b0502255f5ce47f64fe0a change-id: 20241031-uvc-crashrmmod-666de3fc9141
Best regards,
After commit: c9ec6f173636 ("media: uvcvideo: Stop stream during unregister") we have some guarantee that userspace will not be able to access any of our internal structures after disconnect().
This means that we can do the cleanup at the end of disconnect and we will be able to use devres functions without fear of races.
Cc: stable@vger.kernel.org Fixes: 2886477ff987 ("media: uvcvideo: Implement UVC_EXT_GPIO_UNIT") Signed-off-by: Ricardo Ribalda ribalda@chromium.org --- drivers/media/usb/uvc/uvc_driver.c | 24 +++++------------------- drivers/media/usb/uvc/uvcvideo.h | 1 - 2 files changed, 5 insertions(+), 20 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index a96f6ca0889f..2735fccdf454 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1868,16 +1868,12 @@ static int uvc_scan_device(struct uvc_device *dev) /* * Delete the UVC device. * - * Called by the kernel when the last reference to the uvc_device structure - * is released. - * - * As this function is called after or during disconnect(), all URBs have + * As this function is called during disconnect(), all URBs have * already been cancelled by the USB core. There is no need to kill the * interrupt URB manually. */ -static void uvc_delete(struct kref *kref) +static void uvc_delete(struct uvc_device *dev) { - struct uvc_device *dev = container_of(kref, struct uvc_device, ref); struct list_head *p, *n;
uvc_status_cleanup(dev); @@ -1919,14 +1915,6 @@ static void uvc_delete(struct kref *kref) kfree(dev); }
-static void uvc_release(struct video_device *vdev) -{ - struct uvc_streaming *stream = video_get_drvdata(vdev); - struct uvc_device *dev = stream->dev; - - kref_put(&dev->ref, uvc_delete); -} - /* * Unregister the video devices. */ @@ -2009,7 +1997,7 @@ int uvc_register_video_device(struct uvc_device *dev, vdev->v4l2_dev = &dev->vdev; vdev->fops = fops; vdev->ioctl_ops = ioctl_ops; - vdev->release = uvc_release; + vdev->release = video_device_release_empty; vdev->prio = &stream->chain->prio; if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT) vdev->vfl_dir = VFL_DIR_TX; @@ -2045,7 +2033,6 @@ int uvc_register_video_device(struct uvc_device *dev, return ret; }
- kref_get(&dev->ref); return 0; }
@@ -2160,7 +2147,6 @@ static int uvc_probe(struct usb_interface *intf, INIT_LIST_HEAD(&dev->entities); INIT_LIST_HEAD(&dev->chains); INIT_LIST_HEAD(&dev->streams); - kref_init(&dev->ref); atomic_set(&dev->nmappings, 0);
dev->udev = usb_get_dev(udev); @@ -2300,7 +2286,7 @@ static int uvc_probe(struct usb_interface *intf,
error: uvc_unregister_video(dev); - kref_put(&dev->ref, uvc_delete); + uvc_delete(dev); return -ENODEV; }
@@ -2319,7 +2305,7 @@ static void uvc_disconnect(struct usb_interface *intf) return;
uvc_unregister_video(dev); - kref_put(&dev->ref, uvc_delete); + uvc_delete(dev); }
static int uvc_suspend(struct usb_interface *intf, pm_message_t message) diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 07f9921d83f2..feb8de640a26 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -578,7 +578,6 @@ struct uvc_device {
/* Video Streaming interfaces */ struct list_head streams; - struct kref ref;
/* Status Interrupt Endpoint */ struct usb_host_endpoint *int_ep;
We used the wrong device for the device managed functions. We used the usb device, when we should be using the interface device.
If we unbind the driver from the usb interface, the cleanup functions are never called. In our case, the IRQ is never disabled.
If an IRQ is triggered, it will try to access memory sections that are already free, causing an OOPS.
Luckily this bug has small impact, as it is only affected by devices with gpio units and the user has to unbind the device, a disconnect will not trigger this error.
Cc: stable@vger.kernel.org Fixes: 2886477ff987 ("media: uvcvideo: Implement UVC_EXT_GPIO_UNIT") Reviewed-by: Sergey Senozhatsky senozhatsky@chromium.org Signed-off-by: Ricardo Ribalda ribalda@chromium.org --- drivers/media/usb/uvc/uvc_driver.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 2735fccdf454..c1b2fb7f1428 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1295,14 +1295,14 @@ static int uvc_gpio_parse(struct uvc_device *dev) struct gpio_desc *gpio_privacy; int irq;
- gpio_privacy = devm_gpiod_get_optional(&dev->udev->dev, "privacy", + gpio_privacy = devm_gpiod_get_optional(&dev->intf->dev, "privacy", GPIOD_IN); if (IS_ERR_OR_NULL(gpio_privacy)) return PTR_ERR_OR_ZERO(gpio_privacy);
irq = gpiod_to_irq(gpio_privacy); if (irq < 0) - return dev_err_probe(&dev->udev->dev, irq, + return dev_err_probe(&dev->intf->dev, irq, "No IRQ for privacy GPIO\n");
unit = uvc_alloc_new_entity(dev, UVC_EXT_GPIO_UNIT, @@ -1333,7 +1333,7 @@ static int uvc_gpio_init_irq(struct uvc_device *dev) if (!unit || unit->gpio.irq < 0) return 0;
- return devm_request_threaded_irq(&dev->udev->dev, unit->gpio.irq, NULL, + return devm_request_threaded_irq(&dev->intf->dev, unit->gpio.irq, NULL, uvc_gpio_irq, IRQF_ONESHOT | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Hi
Please ignore this version. I screwed it up when I prepared the set.
Sorry about that
On Tue, 5 Nov 2024 at 15:06, Ricardo Ribalda ribalda@chromium.org wrote:
Signed-off-by: Ricardo Ribalda ribalda@chromium.org
Changes in v4: Thanks Laurent.
- Remove refcounted cleaup to support devres.
- Link to v3: https://lore.kernel.org/r/20241105-uvc-crashrmmod-v3-1-c0959c8906d3@chromium...
Changes in v3: Thanks Sakari.
- Rename variable to initialized.
- Other CodeStyle.
- Link to v2: https://lore.kernel.org/r/20241105-uvc-crashrmmod-v2-1-547ce6a6962e@chromium...
Changes in v2: Thanks to Laurent.
- The main structure is not allocated with devres so there is a small period of time where we can get an irq with the structure free. Do not use devres for the IRQ.
- Link to v1: https://lore.kernel.org/r/20241031-uvc-crashrmmod-v1-1-059fe593b1e6@chromium...
Ricardo Ribalda (2): media: uvcvideo: Remove refcounted cleanup media: uvcvideo: Fix crash during unbind if gpio unit is in use
drivers/media/usb/uvc/uvc_driver.c | 30 ++++++++---------------------- drivers/media/usb/uvc/uvcvideo.h | 1 - 2 files changed, 8 insertions(+), 23 deletions(-)
base-commit: c7ccf3683ac9746b263b0502255f5ce47f64fe0a change-id: 20241031-uvc-crashrmmod-666de3fc9141
Best regards,
Ricardo Ribalda ribalda@chromium.org
linux-stable-mirror@lists.linaro.org