From patchwork Wed Mar 27 08:24:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 783858 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) (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 8013228DD7 for ; Wed, 27 Mar 2024 08:24:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527892; cv=none; b=rUQyQ31IDBtamS+AklSIVUK9m+6b6JhQM4YXZhmLH6r8t8gmdfGPgtW1hGKdZvknSu/JkKVEaYFBJz9/wi6W193BXygkBdPr2tu4598rsQxg9lysyQHOuKwT/5tC/9pYPnK8qELzdzL5uyIVkBzCfWAuSFOAkh9+qVzxXwt9ofA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527892; c=relaxed/simple; bh=IbGWx43paY/eY3BoKH9LIehCJLKILNpJ3miMe4Ay6xU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZNzW/aDBv9n12Hc/3DSvS46WzEh8nyOO1AYdxXlHfDYiqfPMWxK45SuWauejz3v9sRnJjdEChA7+KJu9ueilA/RH/A74wAWH+flHvdFSOwozU+gY5EuPtbA6eoIahHBt+QNmfno5GZX/fOeOFdb0BHk93Mo0C1RKEPGoZMjTB4I= 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=j195btc+; arc=none smtp.client-ip=209.85.160.177 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="j195btc+" Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-430c63d4da9so45520671cf.0 for ; Wed, 27 Mar 2024 01:24:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711527889; x=1712132689; 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=yu6CRq784zZk8JmoJH+VcllViInTfNkEV44O9xYOrqY=; b=j195btc+VTQAeWheL6eIlXjFDfcR87V3HGPtAkf5AM9y4VKX4MVYtflg7RPLiupkbh SITfTx+3alq059iL6R0ASdTwA6Qr5OePwsrFfDlQGIbT3G4xiaOBOlGLgxVWKgfhUXtD EHgPnYd5nTS4EnKfnRwVR1UmzWHd2ZD72QTOQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711527889; x=1712132689; 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=yu6CRq784zZk8JmoJH+VcllViInTfNkEV44O9xYOrqY=; b=FRTPEqN1U8SwU8Ji+QfNgvYA0UhzAm1rHX2Az6KbDB0AxlIhl++XXJobXRTK76GR2e V6F7ln671glRl7MAKf2+8gq2dIfzaVfB+UZi670jUbROnZpJNPkI0IFWjilH6vZnx0lq tmvCOQ6f0YaUR13wZgTMv/yroRL/qeN7t9QNTL64ZJGp94J1kDJMQyCHGa0BSlYc5/e0 ZOKAJkt8RM6Z8yoRUzw5S5Qrg1W2KWqdIQYge9sxoR1QgFxQP9yA46IwGeWWoCNc6xrU J2CpVnohBOXzdEj91/WWzfoUxRGoWCdWt3or5udeiNWYhCBmH3NhWDRlwsMruUBgjXVq 4TpA== X-Forwarded-Encrypted: i=1; AJvYcCWGduT4vOyXlxv9doMt1VDcvqvyyo7rtI1eHheNA3mrXKdxw5s85sO6T/iL4BYMrACTiPm9F4Wfkxr68qTFujSPb4AtHIlORy5yT/E= X-Gm-Message-State: AOJu0YyiuqxpM2C1hlVlO/de5JGRqYyK6yJsI5WdeQlx8oywvz0RhzIl pNw025tISq2sP71EnTr6rajrIrM0iZe2BleDTkpkaZZo90RqgsICH+KpR2F6WA== X-Google-Smtp-Source: AGHT+IGgVDltHpo85/iezWoRecn1u3UhxgTUnv8za/M4lLvLgH586s4zoUdEZcsJsYWBFRbX5QyQxg== X-Received: by 2002:a05:622a:2a07:b0:431:80c9:ab17 with SMTP id hc7-20020a05622a2a0700b0043180c9ab17mr683009qtb.29.1711527889256; Wed, 27 Mar 2024 01:24:49 -0700 (PDT) Received: from denia.c.googlers.com (131.65.194.35.bc.googleusercontent.com. [35.194.65.131]) by smtp.gmail.com with ESMTPSA id ca9-20020a05622a1f0900b00430ea220b32sm4581691qtb.71.2024.03.27.01.24.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 01:24:48 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 27 Mar 2024 08:24:44 +0000 Subject: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240327-guenter-mini-v4-1-49955c198eae@chromium.org> References: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> In-Reply-To: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , Max Staudt , Tomasz Figa , Laurent Pinchart , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Ricardo Ribalda , Sakari Ailus X-Mailer: b4 0.12.4 uvc_unregister_video() can be called asynchronously from uvc_disconnect(). If the device is still streaming when that happens, a plethora of race conditions can happen. Make sure that the device has stopped streaming before exiting this function. If the user still holds handles to the driver's file descriptors, any ioctl will return -ENODEV from the v4l2 core. This change make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers. Suggested-by: Hans Verkuil Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index bbd90123a4e76..17fc945c8deb6 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev) if (!video_is_registered(&stream->vdev)) continue; + /* + * Serialize other access to the stream. + */ + mutex_lock(&stream->mutex); + uvc_queue_streamoff(&stream->queue, stream->type); video_unregister_device(&stream->vdev); video_unregister_device(&stream->meta.vdev); + mutex_unlock(&stream->mutex); + + /* + * Now the vdev is not streaming and all the ioctls will + * return -ENODEV + */ uvc_debugfs_cleanup_stream(stream); } From patchwork Wed Mar 27 08:24:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 783451 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) (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 00A5E2E646 for ; Wed, 27 Mar 2024 08:24:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527893; cv=none; b=XPu7ywXoz1wdY6LWCIFy2fGJj3IKnHlpY5BIQjS1dSd6O+YgwHc9j/Tgoq3AuoKJF9zFvmnRwhfNy11K/4HXiHRwDoGfdTy0zdGoq8QXUgNAAOHEdtjl6/HIdcIep9TjwfloYY0Z5a78Vt84rt7dqjwvZtAYzEDGhnsjYKG1rzQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527893; c=relaxed/simple; bh=9RLuwP/ogAKSLndvjalwDyIeMRGXBo7XEO//Re2gJyk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fhnrGXvbBGLRf/cK9wOlsICrHW5RRDxLZ+KgVolqy9wnQ8/tHJ1gMPrQ2TqXmxuyO0SJT4TEueNq/oBYQDxPmzhEGv+ivbc/ZNe8N9Nv0b9Yt1Wom1OyOS/xxwMnZ9oaBImPpABY+fxga+Yuh2dUHPR4HFV7dRNWJFJsp6HwMaM= 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=KN2VspRS; arc=none smtp.client-ip=209.85.160.175 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="KN2VspRS" Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-430c63d4da9so45520871cf.0 for ; Wed, 27 Mar 2024 01:24:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711527891; x=1712132691; 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=OmWRpBECwxe7ijkwLIPczozPsnS1Cw5Xsz7/RS0qQRY=; b=KN2VspRSefxtVCrZwxhPyUR3lyvvXDry6xKhdC9et//LgUKh5H5xwzegX7Yfn9PJ8b TWWIlCZJa62htCAp6yvw2FYJvQE2vOOedJig99A3mOIsYmK6U+aTAofQc8iabmis2v2a EoTlH6WjhMJFREThjwvC8b2SDwcCnDsElQhEY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711527891; x=1712132691; 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=OmWRpBECwxe7ijkwLIPczozPsnS1Cw5Xsz7/RS0qQRY=; b=KzjBGdnoFrvCZ7zFFf5QC+oQCWS43Y1vgmmODTqoTZJx3z4pxI/kpaoeYQReJSlmfA tRz3OgvAcgNypJGDZrvmXXb4Qqx+DCFhoqGiNzFATAgvgSwmeWYh4wXst53ZURedTvp3 Fe3NhtXvAKFNfOJkdpCwnRqHfu6287ux5FXOFgyrmVvUQHK3S4HL2Dd3TJOUKun//Ov3 5UCDFPdHO4xkcNz+BYFYg7gdEgkH0CqveMvEGiVxlbmvZYJnkRdug852e9+m18uU81uB 7xRP3GouPBlItaZDVUvFeAHGh697tECl6vHj85x+nnEmmBv172+eaaWuWQR7VT5/1Q5o 0B2A== X-Forwarded-Encrypted: i=1; AJvYcCUVWRu65zg8rEq6ppWIMRNUUMKr9FbTFz0Fj3uPQUB3HFxdU9GZq6fJN3vQsT28dJ1NY3k3XaPwbMHNR0J5/SXFbi9IhfqGEu4Ijkc= X-Gm-Message-State: AOJu0Yyyd2pVRGZuaCYuWO+DMDawm469/wsBXSNyeD4ApVrT5OqW8MJy 4n1fe0rGMHjKw1PmI6gFX9wu6aZaT8tEpHFuEZBLIDBZAJ2nOMSA6lMqBU6p8w== X-Google-Smtp-Source: AGHT+IHcrYLlePvtS2L+3EOSxz98tF2AwNVGbSteXEp13d492FBjYS/lfiS4OitGu1Suvpn3kOVSXw== X-Received: by 2002:a05:622a:253:b0:431:50a0:9154 with SMTP id c19-20020a05622a025300b0043150a09154mr4232946qtx.18.1711527890552; Wed, 27 Mar 2024 01:24:50 -0700 (PDT) Received: from denia.c.googlers.com (131.65.194.35.bc.googleusercontent.com. [35.194.65.131]) by smtp.gmail.com with ESMTPSA id ca9-20020a05622a1f0900b00430ea220b32sm4581691qtb.71.2024.03.27.01.24.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 01:24:49 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 27 Mar 2024 08:24:45 +0000 Subject: [PATCH v4 2/4] media: uvcvideo: Refactor the status irq API Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240327-guenter-mini-v4-2-49955c198eae@chromium.org> References: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> In-Reply-To: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , Max Staudt , Tomasz Figa , Laurent Pinchart , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Ricardo Ribalda , Sakari Ailus X-Mailer: b4 0.12.4 There are two different use-cases of uvc_status(): - adding/removing a user when the camera is open/closed - stopping/starting when the camera is suspended/resumed Make the API reflect these two use-cases and move all the refcounting and locking logic to the uvc_status.c file. Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_driver.c | 13 ++------- drivers/media/usb/uvc/uvc_status.c | 55 ++++++++++++++++++++++++++++++++++++-- drivers/media/usb/uvc/uvc_v4l2.c | 22 +++++---------- drivers/media/usb/uvc/uvcvideo.h | 10 ++++--- 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 17fc945c8deb6..b579644ac0745 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2116,7 +2116,6 @@ static int uvc_probe(struct usb_interface *intf, INIT_LIST_HEAD(&dev->streams); kref_init(&dev->ref); atomic_set(&dev->nmappings, 0); - mutex_init(&dev->lock); dev->udev = usb_get_dev(udev); dev->intf = usb_get_intf(intf); @@ -2282,10 +2281,7 @@ static int uvc_suspend(struct usb_interface *intf, pm_message_t message) /* Controls are cached on the fly so they don't need to be saved. */ if (intf->cur_altsetting->desc.bInterfaceSubClass == UVC_SC_VIDEOCONTROL) { - mutex_lock(&dev->lock); - if (dev->users) - uvc_status_stop(dev); - mutex_unlock(&dev->lock); + uvc_status_suspend(dev); return 0; } @@ -2316,12 +2312,7 @@ static int __uvc_resume(struct usb_interface *intf, int reset) return ret; } - mutex_lock(&dev->lock); - if (dev->users) - ret = uvc_status_start(dev, GFP_NOIO); - mutex_unlock(&dev->lock); - - return ret; + return uvc_status_resume(dev); } list_for_each_entry(stream, &dev->streams, list) { diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index a78a88c710e24..375a95dd30110 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -257,6 +257,8 @@ int uvc_status_init(struct uvc_device *dev) unsigned int pipe; int interval; + mutex_init(&dev->status_lock); + if (ep == NULL) return 0; @@ -302,18 +304,22 @@ void uvc_status_cleanup(struct uvc_device *dev) kfree(dev->status); } -int uvc_status_start(struct uvc_device *dev, gfp_t flags) +static int __uvc_status_start(struct uvc_device *dev, gfp_t flags) { + lockdep_assert_held(&dev->status_lock); + if (dev->int_urb == NULL) return 0; return usb_submit_urb(dev->int_urb, flags); } -void uvc_status_stop(struct uvc_device *dev) +static void __uvc_status_stop(struct uvc_device *dev) { struct uvc_ctrl_work *w = &dev->async_ctrl; + lockdep_assert_held(&dev->status_lock); + /* * Prevent the asynchronous control handler from requeing the URB. The * barrier is needed so the flush_status change is visible to other @@ -350,3 +356,48 @@ void uvc_status_stop(struct uvc_device *dev) */ smp_store_release(&dev->flush_status, false); } + +int uvc_status_resume(struct uvc_device *dev) +{ + int ret = 0; + + mutex_lock(&dev->status_lock); + if (dev->status_users) + ret = __uvc_status_start(dev, GFP_NOIO); + mutex_unlock(&dev->status_lock); + + return ret; +} + +void uvc_status_suspend(struct uvc_device *dev) +{ + mutex_lock(&dev->status_lock); + if (dev->status_users) + __uvc_status_stop(dev); + mutex_unlock(&dev->status_lock); +} + +int uvc_status_get(struct uvc_device *dev) +{ + int ret = 0; + + mutex_lock(&dev->status_lock); + if (!dev->status_users) + ret = __uvc_status_start(dev, GFP_KERNEL); + if (!ret) + dev->status_users++; + mutex_unlock(&dev->status_lock); + + return ret; +} + +void uvc_status_put(struct uvc_device *dev) +{ + mutex_lock(&dev->status_lock); + if (dev->status_users == 1) + __uvc_status_stop(dev); + WARN_ON(!dev->status_users); + if (dev->status_users) + dev->status_users--; + mutex_unlock(&dev->status_lock); +} diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index f4988f03640ae..97c5407f66032 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -628,20 +628,13 @@ static int uvc_v4l2_open(struct file *file) return -ENOMEM; } - mutex_lock(&stream->dev->lock); - if (stream->dev->users == 0) { - ret = uvc_status_start(stream->dev, GFP_KERNEL); - if (ret < 0) { - mutex_unlock(&stream->dev->lock); - usb_autopm_put_interface(stream->dev->intf); - kfree(handle); - return ret; - } + ret = uvc_status_get(stream->dev); + if (ret) { + usb_autopm_put_interface(stream->dev->intf); + kfree(handle); + return ret; } - stream->dev->users++; - mutex_unlock(&stream->dev->lock); - v4l2_fh_init(&handle->vfh, &stream->vdev); v4l2_fh_add(&handle->vfh); handle->chain = stream->chain; @@ -670,10 +663,7 @@ static int uvc_v4l2_release(struct file *file) kfree(handle); file->private_data = NULL; - mutex_lock(&stream->dev->lock); - if (--stream->dev->users == 0) - uvc_status_stop(stream->dev); - mutex_unlock(&stream->dev->lock); + uvc_status_put(stream->dev); usb_autopm_put_interface(stream->dev->intf); return 0; diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 6fb0a78b1b009..00b600eb058cc 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -555,8 +555,6 @@ struct uvc_device { const struct uvc_device_info *info; - struct mutex lock; /* Protects users */ - unsigned int users; atomic_t nmappings; /* Video control interface */ @@ -578,6 +576,8 @@ struct uvc_device { struct usb_host_endpoint *int_ep; struct urb *int_urb; struct uvc_status *status; + struct mutex status_lock; /* Protects status_users */ + unsigned int status_users; bool flush_status; struct input_dev *input; @@ -744,8 +744,10 @@ int uvc_register_video_device(struct uvc_device *dev, int uvc_status_init(struct uvc_device *dev); void uvc_status_unregister(struct uvc_device *dev); void uvc_status_cleanup(struct uvc_device *dev); -int uvc_status_start(struct uvc_device *dev, gfp_t flags); -void uvc_status_stop(struct uvc_device *dev); +int uvc_status_resume(struct uvc_device *dev); +void uvc_status_suspend(struct uvc_device *dev); +int uvc_status_get(struct uvc_device *dev); +void uvc_status_put(struct uvc_device *dev); /* Controls */ extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited; From patchwork Wed Mar 27 08:24:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 783857 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) (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 3EAF12E652 for ; Wed, 27 Mar 2024 08:24:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527893; cv=none; b=Fs2TO/qqvq4OpZUYA0hPB5fFoVft1YL8pbAdrHyk2Ts/OU5HrDnmCBKgiFI3gHpm2TNtQC38T2bV6pmLc9S7ZSnFCokEs8z/Erz6F31g/3GcQs7eQzU6b+EWx33eUohqHGhV1WqJd7MQoku7Ex46tj5SehUna12GzgLNrftZ1zk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527893; c=relaxed/simple; bh=nahiOHPE8IkoaUj7w2mRwbmhYqYPACwR6bZojjKSo5U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uIy1NWpYazBm3y/iVttAYHk9tmx56A/z+fRIu8eZz7tojONUxAspMfkYVA7tvGqqtrwfmoKCs91pOQ9O8jUAvCiopi/F1s/+HDiFlnY2gYhO3uRjuvEYSluat51uQ5UQFQCzXGPlSee9+SGKibBUwNgbCZOTEL8nNAyce0xgfC0= 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=ehGslJHm; arc=none smtp.client-ip=209.85.160.175 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="ehGslJHm" Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-42e323a2e39so5189051cf.1 for ; Wed, 27 Mar 2024 01:24:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711527891; x=1712132691; 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=KkLI9A62H1Ggh1TCkrNUBd+w+3o29QTz44nWNnN5YQA=; b=ehGslJHm5IXl9yQVuCJylonirPcnS3eQBD4CvC4/vwO8y2nejVQkF3w6ZimSMZ0oMO +gf0y63rkdhVn3FEwPusmdNhaLsd40Lba/bm14gwviaV0lHD1NtairyLJHIyCzzdhsyh yjN5cj2/3W1lWN3J09vjJvfLG4K051G/4DD8I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711527891; x=1712132691; 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=KkLI9A62H1Ggh1TCkrNUBd+w+3o29QTz44nWNnN5YQA=; b=TrQ+yiSohpbMwvFTh2f5ZZpghWEtGFCX/yeazdWab2DuSiSs1u7+GQLXiO/Dcouowl QoOco9VT3lwk6g6I642k+4gGV5f3rYJL0o2oklb2S+8sUvpxtygbGPvTj3oLkKG3akIz NiBm33MBUySWwL4Y2LLJgnLUnd8IHTbtxcVhnFOJ8Wzmu1JgEHBpOytwLe7Cleprpj1N TbBlFWVlUJ0V1cBjg5cSlWxrrSLCk5TIT4b6slojRgGh/otKTErdCAQE766etJjVqtcE U3UTCj6wXUL6Z8eZU4bOLKwuXfx54Wb6WpyhYiYXyJWrxNZBjUxMHrD/1YkmQZzOUnzN Dp0Q== X-Forwarded-Encrypted: i=1; AJvYcCUglviW2d6zpyOqxbvKoC7PdfU0mBzrQ9yX0x8ijJpE/8uz81XWIqsXErWfiT2INpw7jGooh/GbmZRXGHeBqm8b+SO/tzAcmNwhd2g= X-Gm-Message-State: AOJu0Yx1E8Qa3WFSmTc3yKutDrl5ASQmReIDCqzgb3LupDgRU5MQ7rML SlafABGuQjJFwijoXSHe77+jNLGiz39/8QN3s1/5XiLRcL6ci7gzrsut0iWnxZnTGPwx3RBt1rB UZ53f X-Google-Smtp-Source: AGHT+IGNUblwo/JlwehDtdXHQDYBlalI2e9kD1Y2cVhoODvGCnn0vyY+nubC8YlGQFiQ/qJcUAgU9w== X-Received: by 2002:a05:622a:40a:b0:431:503e:4139 with SMTP id n10-20020a05622a040a00b00431503e4139mr6285588qtx.14.1711527891164; Wed, 27 Mar 2024 01:24:51 -0700 (PDT) Received: from denia.c.googlers.com (131.65.194.35.bc.googleusercontent.com. [35.194.65.131]) by smtp.gmail.com with ESMTPSA id ca9-20020a05622a1f0900b00430ea220b32sm4581691qtb.71.2024.03.27.01.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 01:24:50 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 27 Mar 2024 08:24:46 +0000 Subject: [PATCH v4 3/4] media: uvcvideo: Avoid race condition during unregister Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240327-guenter-mini-v4-3-49955c198eae@chromium.org> References: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> In-Reply-To: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , Max Staudt , Tomasz Figa , Laurent Pinchart , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Ricardo Ribalda , Sakari Ailus X-Mailer: b4 0.12.4 The control events are handled asynchronously by the driver. Once the control event are handled, the urb is re-submitted. If we simply kill the urb, there is a chance that a control event is waiting to be processed, which will re-submit the urb after the device is disconnected. uvc_status_suspend() flushes the async controls and stops the urb in a correct manner. Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_status.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index 375a95dd30110..8fd8250110e2f 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -294,7 +294,7 @@ int uvc_status_init(struct uvc_device *dev) void uvc_status_unregister(struct uvc_device *dev) { - usb_kill_urb(dev->int_urb); + uvc_status_suspend(dev); uvc_input_unregister(dev); } From patchwork Wed Mar 27 08:24:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 783450 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) (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 42B46339A8 for ; Wed, 27 Mar 2024 08:24:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527894; cv=none; b=P/MENBlFj9wiaPPeJ5VBiaT9SUV2QCEfW+7Zo1jWxMHA+RqTSR8ePZMy0LAM2hzQ8XQq67EQ+JyTtp5CO+vm0d4zJ+Oc5rwLOKsoT+/OX3cxBopAw+iHP2CsjUf8rT1GlHT3XXxYMkTPH2qgqcl3NIp6INtib7SWrG0n4GTXGXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711527894; c=relaxed/simple; bh=bRmG+R4WpzyKvUI1uUWRSJ67hIlvwT6pr/FQGV/U1XQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bcQfxWUbELhB1cGbV/ZzfZY3AZ6i3uwYOHY/hILnvOTqIS2/ugBL69117Ov1wMX4QhHA4loclat/X4mOX9eoNAmzRG8S2xVX7MLd36vlAMSIk/6LaCXobmQ9PD3MW3ZCi7lh6QLT8bBtKNs+Ah2rVJiwLOEq1AWRiPeFUPUVoc8= 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=jkP+KvMh; arc=none smtp.client-ip=209.85.160.169 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="jkP+KvMh" Received: by mail-qt1-f169.google.com with SMTP id d75a77b69052e-429f53f0b0bso42773601cf.2 for ; Wed, 27 Mar 2024 01:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711527892; x=1712132692; 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=q91fMluof7ru1htcNbZ7jN0/AlY71gRdn67Zo4X/cIc=; b=jkP+KvMhRa+iIrcyrjHIyHuqg1kadBEvLfW7cBoys16ubuivXLDfWkCqhkC2RdhIp1 DfvHqqqKr9LQdEAs7M+w8khg8z4E/V1zFFw+LWiDntjTsNm3yt1U2vRBoUvu2LVPM2Bh 3o/RZAdqWaNZslHNOb/nhBduu+614DoxNh0eQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711527892; x=1712132692; 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=q91fMluof7ru1htcNbZ7jN0/AlY71gRdn67Zo4X/cIc=; b=wB81p48OzMUYCD00I7sB7Z0/48cybRDykoBZJ9/Pb9NsZNiB51oBXQFArk08UGkiIZ civRAXJkNFB8AX9AYtqSaNUg+pFX50ygp9FWNTG8Z5ZH+nnVfs3aLKDE9rMtJltJwPoq SvGeeXWk2pTv2RtFJ93mohguYnwrWvtCNmo2WCoAQjo6dMieUAajRO+9eU5A3xvV7YQM WaaFeV9lIX2joJ0ECSH5tvTnXioUjriT2qfttDCip4EZDsajTzQ9W9AurAEkkqgu5ObG JnG3eFHASt1MjTNg5E38OlvE0SM8NNg2MIb+jaoduRGfr3+fZw+BXgkMIiJtXBF/C32J lPYA== X-Forwarded-Encrypted: i=1; AJvYcCXDNwFMqHRhk56xNWeIIE8zA9PBQ9r0mSeWtkjTxMe33aqLG9nKLgOYUjzzpmFO2B24qh7gotk1HLvftPRf/fZhrV0fF8+dHUH0bJU= X-Gm-Message-State: AOJu0YwYyOPiubYsdJ9cMWkW7qDYc4OHs4B2+CBzKMw3edorApM0gCKX fCF586WITiHar6pl+Wx6pJ2vWl9tFQZLu8VvWPWLHWTocGchwfrCWybs0uUdaQ== X-Google-Smtp-Source: AGHT+IFeyiFHPVw9YcXiAwhqjUitPd4tkGsUFvfkwVfV8A2FB+olpwurffx057UQmDPwxHVVXdKZRQ== X-Received: by 2002:ac8:5b93:0:b0:431:5e50:ac0b with SMTP id a19-20020ac85b93000000b004315e50ac0bmr3849716qta.3.1711527892288; Wed, 27 Mar 2024 01:24:52 -0700 (PDT) Received: from denia.c.googlers.com (131.65.194.35.bc.googleusercontent.com. [35.194.65.131]) by smtp.gmail.com with ESMTPSA id ca9-20020a05622a1f0900b00430ea220b32sm4581691qtb.71.2024.03.27.01.24.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 01:24:51 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 27 Mar 2024 08:24:47 +0000 Subject: [PATCH v4 4/4] media: uvcvideo: Exit early if there is not int_urb Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240327-guenter-mini-v4-4-49955c198eae@chromium.org> References: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> In-Reply-To: <20240327-guenter-mini-v4-0-49955c198eae@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , Max Staudt , Tomasz Figa , Laurent Pinchart , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Ricardo Ribalda , Sakari Ailus X-Mailer: b4 0.12.4 If there is no int_urb there is no need to do a clean stop. Also we avoid calling usb_kill_urb(NULL). It is properly handled by the usb framework, but it is not polite. Now that we are at it, fix the code style in uvc_status_start() for consistency. Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_status.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index 8fd8250110e2f..9108522beea6a 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -308,7 +308,7 @@ static int __uvc_status_start(struct uvc_device *dev, gfp_t flags) { lockdep_assert_held(&dev->status_lock); - if (dev->int_urb == NULL) + if (!dev->int_urb) return 0; return usb_submit_urb(dev->int_urb, flags); @@ -320,6 +320,9 @@ static void __uvc_status_stop(struct uvc_device *dev) lockdep_assert_held(&dev->status_lock); + if (!dev->int_urb) + return; + /* * Prevent the asynchronous control handler from requeing the URB. The * barrier is needed so the flush_status change is visible to other