From nobody Sun Nov 24 11:34:14 2024 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 991BF1D8DE1 for ; Tue, 5 Nov 2024 14:06:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730815573; cv=none; b=ZyuBCeF6zq8lnfGI5rkJtbh3tT+6vbqgKRdp0sTv/j6JNe/vONdncYy12Xp0sRiBdrdzq1C/7IgwlpTUD7iRCW+D4gHfk8tvAMvdgSqCJs0SWdY7rExuVJLxtDcJFHnMLcSJAttBPq11Vwo/oSrRHKXexVayDTt1y+8DOKth8BE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730815573; c=relaxed/simple; bh=llHpLGlkUHZowYuOkDk6k/JCIAhD9hLmX4RX3PNhlsQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=APL88VFTblcPeGOMS3C5CPWLjqp00eh39f70jP8dyADZChv6MOIvaX5FaYc1539IdkQ5tklU3aNbN38q7zlmNS1Pb02Um4pR07bwtgpcrU0stebrhzyP4P3fkHfn8vO3ABDdd0GXJrqf+cp9I5GSTogZ4aCd8ADpid3YWthMZVI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=PKaGrq1n; arc=none smtp.client-ip=209.85.222.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="PKaGrq1n" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-7b158542592so389877285a.2 for ; Tue, 05 Nov 2024 06:06:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1730815570; x=1731420370; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Gio7v/Jh4+pISjPYX2FMMZSUjUbp0P+7Vz75sUpvXsU=; b=PKaGrq1nJyNdSxwiQg6orjFw8thaOymlBjKIRG/HhxpJ3POB7+KMv16ZmbmmAGU8NA 0uyq8V6qpJ4AuqMzz/pbBku6jcPokLfkmXzdcMnbZcNzyyFACmFCtcFTyGs0C910yEH/ FFB+XuiOgSzlqVAcGOkGgZ/9c5AmwEOCbpHg0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730815570; x=1731420370; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gio7v/Jh4+pISjPYX2FMMZSUjUbp0P+7Vz75sUpvXsU=; b=bEG8AsqFcmxiA3nuN6dFLdsylLtGyjPLfWC/9vi7hcL+Qg0PB6p7UEgCsCliLky0a7 esMNtGjb38KEJV4hvApp5vajTuTFbI0DHpf9qCV5xRaRDsc0l2+OP6QtQCQQSzSQSr6c b36eLn3jLJ5POwe44bE+aYVzUY/LaF9M9qJwFr0XPCtqPk3ct0C7Br5hlYYjFNWTCM8V DfypaLXXRCMSaFWmw4KgYwwELhhNpBoCv0F7ycEz6zaotvvI05G04R3S72Y+Fydf3XEq Z+4S6qsYmiNC2TlqUfyZFvKJ0deNV0CM1QFFFyhi8PCFflrLFCBOibysBX6NAxpU/y7y brQQ== X-Forwarded-Encrypted: i=1; AJvYcCUukfPKJweB6ViXlKLckc1itIN4VV6ApFTPX8jL31Yu/uwXaZO6qFqYbZZ1xE6+e2gkdVjzeIKMveQckkQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yxnw7HcHOtzbUjBbrDco7OQQe6ppuvH1EKAR9wjwB5qUn0ct5tC dlQKmZpBwa1VZ8uxXozy7MEoXvScVJdHI9fX542/OrjWt++qrDiCzbvmGC8rqQ== X-Google-Smtp-Source: AGHT+IHJSp6KoCO0JCiw9Js2LtOlX7ZsvBREP0xvGv2MuJzWFhwN8LTZLpMVZXngGMQU2mZFB23TSQ== X-Received: by 2002:a05:620a:2a12:b0:7b1:4caf:3750 with SMTP id af79cd13be357-7b2fb9beb07mr2585903885a.53.1730815570498; Tue, 05 Nov 2024 06:06:10 -0800 (PST) Received: from denia.c.googlers.com (189.216.85.34.bc.googleusercontent.com. [34.85.216.189]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b2f3a816afsm520422185a.101.2024.11.05.06.06.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Nov 2024 06:06:10 -0800 (PST) From: Ricardo Ribalda Date: Tue, 05 Nov 2024 14:06:06 +0000 Subject: [PATCH v4 1/2] media: uvcvideo: Remove refcounted cleanup Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241105-uvc-crashrmmod-v4-1-410e548f097a@chromium.org> References: <20241105-uvc-crashrmmod-v4-0-410e548f097a@chromium.org> In-Reply-To: <20241105-uvc-crashrmmod-v4-0-410e548f097a@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Ricardo Ribalda , Sakari Ailus , stable@vger.kernel.org X-Mailer: b4 0.13.0 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 --- 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 =3D container_of(kref, struct uvc_device, ref); struct list_head *p, *n; =20 uvc_status_cleanup(dev); @@ -1919,14 +1915,6 @@ static void uvc_delete(struct kref *kref) kfree(dev); } =20 -static void uvc_release(struct video_device *vdev) -{ - struct uvc_streaming *stream =3D video_get_drvdata(vdev); - struct uvc_device *dev =3D 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 =3D &dev->vdev; vdev->fops =3D fops; vdev->ioctl_ops =3D ioctl_ops; - vdev->release =3D uvc_release; + vdev->release =3D video_device_release_empty; vdev->prio =3D &stream->chain->prio; if (type =3D=3D V4L2_BUF_TYPE_VIDEO_OUTPUT) vdev->vfl_dir =3D VFL_DIR_TX; @@ -2045,7 +2033,6 @@ int uvc_register_video_device(struct uvc_device *dev, return ret; } =20 - kref_get(&dev->ref); return 0; } =20 @@ -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); =20 dev->udev =3D usb_get_dev(udev); @@ -2300,7 +2286,7 @@ static int uvc_probe(struct usb_interface *intf, =20 error: uvc_unregister_video(dev); - kref_put(&dev->ref, uvc_delete); + uvc_delete(dev); return -ENODEV; } =20 @@ -2319,7 +2305,7 @@ static void uvc_disconnect(struct usb_interface *intf) return; =20 uvc_unregister_video(dev); - kref_put(&dev->ref, uvc_delete); + uvc_delete(dev); } =20 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/uvcvi= deo.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 { =20 /* Video Streaming interfaces */ struct list_head streams; - struct kref ref; =20 /* Status Interrupt Endpoint */ struct usb_host_endpoint *int_ep; --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:34:14 2024 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 665ED1D935A for ; Tue, 5 Nov 2024 14:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730815574; cv=none; b=Lg7Qaxw64EYQndPS7hMNNAbRhD0PHaRt5X8tGNrxGBzVNGQ0h7jLK7p6MStTu3LNOfPJXeVkM+g3jgeBnDqBPDSrVXRW6a+2J1MWj8mR9BCA8oehYhduoV2etyw+vpqdK4cdv38sZqBCb4QlVFR3186CZ2s3N9TPKDf4DLiBA4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730815574; c=relaxed/simple; bh=QVb1o0qgKlSv/qhNBm0mM8Ma1splu8Yi7c/RKdK6kZM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=X/ecOCigW3NC1L6bTM+aioMKU+fWuf+0OMRrzeVni5puf3dRi3jkrqzphKy+TNFshOHDbmILQX3QXT4j7MR9CAjrG8kCf92tkIFcUNMkVsbb5q5H02zNeARB/duGRgSIkPwEWolVP6hrOyCk7EAzzZYF1nqkhIXfKHcfxIbvXCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Q7YTCTM+; arc=none smtp.client-ip=209.85.222.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Q7YTCTM+" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7b157c9ad12so344781785a.1 for ; Tue, 05 Nov 2024 06:06:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1730815571; x=1731420371; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=yf0PBF4kMneT4CHz7Zgh8clF/HAKHaFSwjgqCM9cvPA=; b=Q7YTCTM+IjYnIZSWoNvmO0a3vBgwu4FvTww1mEK2vigGkojBKJgzml/v3ATwzhvZ1G 1kGhNdlrA7UokNW9legHVLOozF1foDLSdEcU1ucNotwVtdB8gyaVA1WPLjytPihe6SqX nEYO7SO3mPZDTP2ZiedajikwPGVkc/VOn9JwA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730815571; x=1731420371; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yf0PBF4kMneT4CHz7Zgh8clF/HAKHaFSwjgqCM9cvPA=; b=MRx5lXbeLr6GEQMUkcsxMWPc3D5keszByqUxFxp6KNZ+RNoNDpiDmvu2YKBMS/SHgE jZaMxeuyXdm3Un6LbNlvC8UA2xOiDCuBtfbimXXFHDQCOWvaa9nTFTnKoBBNfHRu2s8a tCfY6XvwHOVKb60IHzAWagNuYgak2gT555Alvqu6isVMZU6+EOMyCKOkhjMv9kyZK+Ez FgXAj/bV3oYrrEGqUrZItS5a6uNFejSO6j6B092egaKbXX06yWjxcirbXUiZnH+v4Uig SV2LJ6oG9hAJI8vs1cK3Nhz6sWu12fqflLcQS5Y3d7LA75W5ge2AqGIps0sKKI8fOPaT Y4zQ== X-Forwarded-Encrypted: i=1; AJvYcCXInFRZw8yiIVWMJyn0gHfcKh2lhQgoJTZA4maM8mzzKZQAdYMY7gzt5gohkHWcon3ueOvNI7NZpyK8Zhk=@vger.kernel.org X-Gm-Message-State: AOJu0YwMnhX6/Btzoj0yaIIBh3vDzTj3vvFA6eterVM282qZ+pkWSxXz tLHG9F/0wxkN+FnK7ZjQzdwxV4sS11skXehUO/Qwtk7aDYIEBpOXfvZThoK81Q== X-Google-Smtp-Source: AGHT+IEWVNYihrENoX/DowKpgEnulMjOF544+crUJdNi7nlfrQ1ImMrZyVSzPhhVwnMGSZIZ0F8ulQ== X-Received: by 2002:a05:620a:45a4:b0:7b1:3754:7da2 with SMTP id af79cd13be357-7b193ed8335mr5096381185a.3.1730815571112; Tue, 05 Nov 2024 06:06:11 -0800 (PST) Received: from denia.c.googlers.com (189.216.85.34.bc.googleusercontent.com. [34.85.216.189]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b2f3a816afsm520422185a.101.2024.11.05.06.06.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Nov 2024 06:06:10 -0800 (PST) From: Ricardo Ribalda Date: Tue, 05 Nov 2024 14:06:07 +0000 Subject: [PATCH v4 2/2] media: uvcvideo: Fix crash during unbind if gpio unit is in use Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241105-uvc-crashrmmod-v4-2-410e548f097a@chromium.org> References: <20241105-uvc-crashrmmod-v4-0-410e548f097a@chromium.org> In-Reply-To: <20241105-uvc-crashrmmod-v4-0-410e548f097a@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Ricardo Ribalda , Sakari Ailus , stable@vger.kernel.org, Sergey Senozhatsky X-Mailer: b4 0.13.0 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 Signed-off-by: Ricardo Ribalda --- 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; =20 - gpio_privacy =3D devm_gpiod_get_optional(&dev->udev->dev, "privacy", + gpio_privacy =3D devm_gpiod_get_optional(&dev->intf->dev, "privacy", GPIOD_IN); if (IS_ERR_OR_NULL(gpio_privacy)) return PTR_ERR_OR_ZERO(gpio_privacy); =20 irq =3D 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"); =20 unit =3D 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; =20 - 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, --=20 2.47.0.199.ga7371fff76-goog