From patchwork Tue Jun 11 08:12:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 803764 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (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 18F93173321 for ; Tue, 11 Jun 2024 08:12:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093533; cv=none; b=a2MYWGeYmaruZaHj1MAWdxqBK6gaEcdmn4Gm59+kSK5B5DivUPMCQZcy7Fcd8SuqWATeHSh1hIC7Ek8RpNl1mKvqze0z+fU6Fmx8qLwCW1q1uUIDqEr98PO9zGmtRX1RjM9P2RfooDqlnJjMemwKDVUdAbFgAKkkUe/DFf1HYn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093533; c=relaxed/simple; bh=VBjl8MShjb4jhR/wD8VA+h2otUV+ZOfH5etejEqA764=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FNuH6D4zNcSZQg9mZoK6Zu1U13vn+T+l5INutcKbpOqaHQ8VmEiJFNLrs7vZzeitPLUdPnC9HREVyM6P01sU4wwf9z0Vt1GbKrBsatETZiR8GaQMRbG5jamqUAbH7Bqhx4FtlieMYlzd5MTccnyIRjTishfIDdyOLjK2T8LmJTw= 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=dQ3GW1cG; arc=none smtp.client-ip=209.85.219.52 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="dQ3GW1cG" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6aedd5167d1so4299996d6.1 for ; Tue, 11 Jun 2024 01:12:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718093530; x=1718698330; 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=i8v2HQzZnsAarKFZFIMw3d0cEESgGzGkmVZsX2rItjw=; b=dQ3GW1cGwHS6WY7hLP8xLKQ/QFBn0kB0bn+iMZlaGl6loEJnPjk1UT9x5PEmRw1zRP uN8LSed9YT4v2soVzdfBsZfEYLylW69olvevfUFEuB5EiQ5bsAVAobeB9f6I0Vh/mwT2 4PdxyDRNz23oUwD/QsAJaniqMIpSARxGWRs0o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718093530; x=1718698330; 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=i8v2HQzZnsAarKFZFIMw3d0cEESgGzGkmVZsX2rItjw=; b=qY2ECYiftMD0mEfhC8tqdCXzNm7v06qO+nWy1s9tS7sv6/vtL4eMoK7YKdfxDIt+2R UkdM7WckcKdvXkOx4eMsTqH5ZhUFpRv7nbcALm1WGAZtwjC5teKtmQw/ObqP5I3Rg3gQ q+/LJePt+PgBL0/4X4wkTg1KvwDCj25WRdgKVCQXdzCElRCJNC0L5vEIRkkommmFg07g R4HtJ9MokRJCnAqI7llVz9gWxmAJlxK+pqh4TtrYBkMNJ6akiuNmaSXCxh+OcSuoCCZ9 gBbLXjObruYdpt0utRYRwcyirSrRelzHte+iVjMvEHuxo92hrgSQFJsrz1TXNxxPcrsd FgAQ== X-Forwarded-Encrypted: i=1; AJvYcCUCa6+sOdsJAUOgucxK3JQkhfOdQJFQwlsc/bjpZxlQkZFrKRWcVgseVJyh1Y02QRrV8pBKf8T15xCtvMRoxTkDIIKxkRKXI64AK+U= X-Gm-Message-State: AOJu0YwwUBZ/x1B5gB0po7JwS2k83QR8hn/ErWLfLhWzxDVqbz3dXApY j5D6RIB0kur8EXcXG4yEH7lpaypBPQnOyywBvgO6hAPLTjPBDgSkf/ro1cqynA== X-Google-Smtp-Source: AGHT+IHJrD3aaT0KJbL4s5UWU+yxBKPow1+9VOCVV8mz/2gwC8+wamLfq6YRjGQFX/uJQr7Tf9Tp0g== X-Received: by 2002:a05:6214:3186:b0:6b0:8b2d:b6e with SMTP id 6a1803df08f44-6b08b2d3a43mr14553876d6.63.1718093529989; Tue, 11 Jun 2024 01:12:09 -0700 (PDT) Received: from denia.c.googlers.com (123.178.145.34.bc.googleusercontent.com. [34.145.178.123]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b09039c2b9sm1548886d6.28.2024.06.11.01.12.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 01:12:08 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 11 Jun 2024 08:12:06 +0000 Subject: [PATCH v5 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: <20240611-guenter-mini-v5-1-047b6fe5d08b@chromium.org> References: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> In-Reply-To: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , 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.13.0 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 Reviewed-by: Hans Verkuil --- drivers/media/usb/uvc/uvc_driver.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index bbd90123a4e7..f1df6384e738 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1908,11 +1908,41 @@ static void uvc_unregister_video(struct uvc_device *dev) struct uvc_streaming *stream; list_for_each_entry(stream, &dev->streams, list) { + /* Nothing to do here, continue. */ if (!video_is_registered(&stream->vdev)) continue; + /* + * For stream->vdev we follow the same logic as: + * vb2_video_unregister_device(). + */ + + /* 1. Take a reference to vdev */ + get_device(&stream->vdev.dev); + + /* 2. Ensure that no new ioctls can be called. */ video_unregister_device(&stream->vdev); - video_unregister_device(&stream->meta.vdev); + + /* 3. Wait for old ioctls to finish. */ + mutex_lock(&stream->mutex); + + /* 4. Stop streamming. */ + uvc_queue_streamoff(&stream->queue, stream->type); + + mutex_unlock(&stream->mutex); + + put_device(&stream->vdev.dev); + + /* + * For stream->meta.vdev we can directly call: + * vb2_video_unregister_device(). + */ + vb2_video_unregister_device(&stream->meta.vdev); + + /* + * Now both vdevs are not streaming and all the ioctls will + * return -ENODEV. + */ uvc_debugfs_cleanup_stream(stream); } From patchwork Tue Jun 11 08:12:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 803374 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (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 E4050173345 for ; Tue, 11 Jun 2024 08:12:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093534; cv=none; b=BDeFms2N15PCGev+IkDdv7jDLAngH00G/k046xBaN/fg5b+VS0+q217zkvlKdXxdMU9YPiaV9Oo4F9k9zBxN/6WjaR0yziYj7JdOEFlX+HxE/MttSnODd9rQME23ymaog7ImFScWJYTbrSLKcsTFNVjjcy/9mwSv7hy6mg2+qco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093534; c=relaxed/simple; bh=64alHD74cnDTIfHQOlIz+XywS99iYHgMLqGlSmRdKn0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TaW3fjR+a1v0K+45Et9HCoU5YT/43f/GKg6KDZ43smPI62FQ3hlKu7KASzEM5dtCu92VVghr8osPjrfrvbOUeBIMczj39fPj3p5ul+iCu7olK56Ga3ELiXMbY48nMlgPad8SLCkX7c1ZmO4Qeds5CXheMvewldnuaWgo+54Rmu4= 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=EYrlbsNF; arc=none smtp.client-ip=209.85.222.178 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="EYrlbsNF" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-795482e114cso46419785a.1 for ; Tue, 11 Jun 2024 01:12:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718093532; x=1718698332; 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=EOMaXZDQx8tFTSRH/17UvPzHD6txAvg9gMmc9mkFLc0=; b=EYrlbsNFlqU7UlOXwXFRLpxZs1ELmSEIgSwC/VOo31hy6JHrCCFdUJ81uUN77Weqjd neBxiic6vit+o3v6OUwsdGrxGDx1JmuvKmUZb63E2EQLJriGXPXiPYqWTfxIPx38596p qvivmwk5PGc7yZK+XvOED5pxKiabflCrhnM6Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718093532; x=1718698332; 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=EOMaXZDQx8tFTSRH/17UvPzHD6txAvg9gMmc9mkFLc0=; b=A/8tnnp0SnjRI5nnJRWp/X7Wx6q7Un3VdvX3SUBlaOV0RmPDyPvnU7fnV5mQT0YuxZ jKDMuE/OWeRyl20/iWRCmvWE0/L5mr10sDIZdAY3zeV1GOKR5TWPV2FvSPHw7LRGQvo/ 3bouNVDqxMo7+98xFRKI/jPr5s0aCNa/3REK7KrvdMsXwK4O3VOipcx8hfT2rCBMj7CZ Vo5yt0dd5gTszV6SQxLhmJZGosDU2Cwvf0bfsjCIh2CInZa0h9NtwG9Pc6twyAmn30hR w4XqaIJoVmsqoSJdzjzIG4cwogo9sFnGwQbSHYXsNkQqOOHQsdyEsaqAZCYmym0vBMMQ BUbw== X-Forwarded-Encrypted: i=1; AJvYcCUBKZleJo3o/3Pc5N60XmHP2dknqHnfQX911CSSr6vPSLnvkqZ1JAKU/9bcQobAfefh7w3ttKdyIeuL3VHdmt7VhGTPWzDQ47M03kI= X-Gm-Message-State: AOJu0YxYwMZCoXieYLzmWrHMn0sZEj2lIl1pKOLgJRwto9Zx61tbQEKz H+tFpcayb5Ve2Wpq2SY2Z9uznaE0fSfNEMlhc9LkLhtThAgkZmGZJRgf9HiyAA== X-Google-Smtp-Source: AGHT+IGJ+4NUXCKFlk1lZknsxWrx0lQOFKK6eC9/TccyOHyB219dw2R65Gykyu0l1FHZsE4vR0WQQw== X-Received: by 2002:a05:6214:4486:b0:6a3:294b:5622 with SMTP id 6a1803df08f44-6b059b712camr137350906d6.17.1718093531754; Tue, 11 Jun 2024 01:12:11 -0700 (PDT) Received: from denia.c.googlers.com (123.178.145.34.bc.googleusercontent.com. [34.145.178.123]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b09039c2b9sm1548886d6.28.2024.06.11.01.12.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 01:12:11 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 11 Jun 2024 08:12:07 +0000 Subject: [PATCH v5 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: <20240611-guenter-mini-v5-2-047b6fe5d08b@chromium.org> References: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> In-Reply-To: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , 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.13.0 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 f1df6384e738..078b8fb05b54 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2135,7 +2135,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); @@ -2301,10 +2300,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; } @@ -2335,12 +2331,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 a78a88c710e2..375a95dd3011 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 f4988f03640a..97c5407f6603 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 6fb0a78b1b00..00b600eb058c 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 Tue Jun 11 08:12:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 803763 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 37F3C174EEB for ; Tue, 11 Jun 2024 08:12:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093535; cv=none; b=QJdC2YkSx/2tzFrqA26iipZQpzIXRUkkuvfP/KJuG4FJZAhgm5sB4uJsjo4c2LdYFh3glKT/pM5CwrJWU7DaQkOcWbuAnTaKpDKAVwmnOVofCQekW28lg06gD4EDGW89jkVYnyf/7Gph7JZSUBICbwCXtQTVlKzQKqvACiepLsc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093535; c=relaxed/simple; bh=hVLGmVR1HMkUx7+t5Nvw+6vPF4SbFF3ImKNPy0MAlEU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GpI/cggzCgM6Jas2D6xvr/9MJxxtghH5LVqiG7XUdSJSHQU3a+kvPBRtm3uX2jtTBpiWAtUrKdYMIU+yiEVfsli4i4n8KpRCDCgXbgK9bLO3fXfrebkY5U8gt3cnVek6rCNuw++d6EYLve7t2g4Qb4PMOuBN9miX1BSw48EK+8g= 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=Uc5Zv05L; arc=none smtp.client-ip=209.85.219.42 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="Uc5Zv05L" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6b05e504bceso30191096d6.2 for ; Tue, 11 Jun 2024 01:12:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718093533; x=1718698333; 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=1qcNpYr9SkPtRLJMkT2pD1WoLD/0+PdytNXdst5BZm8=; b=Uc5Zv05L3zx0B+t6IHfmS25NKwjWRsoWfUBKmZgHSW0INbbFCiQLjODl8S/s7aAhAo FWN5QbQzDDAZd3eut+Ei3LP47sQwcdMLIuhkK5BvAdSf+n5mjYzQ5XHdrqMvpnYRTWKr twlnQ9LiWmKCBx3g4589rrXy4rL/OxM+87O8A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718093533; x=1718698333; 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=1qcNpYr9SkPtRLJMkT2pD1WoLD/0+PdytNXdst5BZm8=; b=XSFXay2mpmyHPxlfg3/9BezXMuZU6y+4AXualMHNY+es3fUJ2mUIAQ1bFAmHsNvbO7 O3lFl3GkT3A5hjA9O9y1Wy/P24v2x6KLVYt9m+B9iHPr9DGYlYPIaxSxSrDLAIs7sEZ4 6hYkS8HBPU+LFm4UBsJ5o1YhY8Bg0wkXWVSQx2EEScapg2gqVMdeSvR2KhIDkcRZtjRz VMWrgug8URPeei3UKagzzGcN6F3eIXJxrr6RqawwtCv74M5iF+0ee/JRczcTi2eeYJpi Yg12LSHV2XJzFbWNx8JGA2bz6DB6HBw5mYgcbJqPprisUxLc6rmMkVbVIv7Kz7W0IVe6 oiEg== X-Forwarded-Encrypted: i=1; AJvYcCXt7Ca2uKZuLEXH0l1uUjggmILH4rYFFB2xVbFYnuCPXLYNAd8TsKmSyDkpGa3DM67eBkfObmtJEF826gHsZtZl0IUFwpFE2Y6jzWw= X-Gm-Message-State: AOJu0YxhBWPPeSmwFYAnfl+GyvtV6VWe04lJzsqYbafFiLK/HjEoodV1 f100UM5Ly2XuhK3f9IwO0+PMmR0bMoZOUH7ORjkQ6yry5JqJZ68wSWztNn0EDTKJ2gwRFg20y2d YPrXN X-Google-Smtp-Source: AGHT+IH48KT7q9z49yEVjuF/xwKgCzBz70RWMzOb4ndYUOF7z0Kudt/BHMFgmwtck1NHVeX9Opq37Q== X-Received: by 2002:a05:6214:3912:b0:6b0:7ba0:ef67 with SMTP id 6a1803df08f44-6b07ba0f05cmr47438746d6.31.1718093533266; Tue, 11 Jun 2024 01:12:13 -0700 (PDT) Received: from denia.c.googlers.com (123.178.145.34.bc.googleusercontent.com. [34.145.178.123]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b09039c2b9sm1548886d6.28.2024.06.11.01.12.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 01:12:12 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 11 Jun 2024 08:12:08 +0000 Subject: [PATCH v5 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: <20240611-guenter-mini-v5-3-047b6fe5d08b@chromium.org> References: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> In-Reply-To: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , 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.13.0 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 375a95dd3011..8fd8250110e2 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 Tue Jun 11 08:12:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 803373 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (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 84C08174EDF for ; Tue, 11 Jun 2024 08:12:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093537; cv=none; b=XVlYxSjBsm9FguJAfv9OXPwYm51z9w/4uJB/VKcX0I/R7ZmzAdhW6+HOH7TC2kT5R9bPOUJ1/6xxQA1wdH9gN0yM/Ipuj4AeLUarP/9GJW9LoZzdgR9Xhd8//lv5do8FRf/uzRalwFQKv24qFpdSBOoVXyRlVm3EzMgu8Vkm6MQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093537; c=relaxed/simple; bh=lM/AUoiIFm5wy7ofM3hcaSkgrO99OY8cJt9TzA0Mu0s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=f4YY9OLqK/89nppk5OY78hGQK8Oqgaf5n8s6+xiM36/QKdhb9w21fHrdNK6rOSbjjltSh3WYGXdWns94Xm7MpyxARd23c6z5S8SWc5RSBYqob/FxKF0u0yM9BkU+5qzd9b4wCMtctPflRPrhy2AZvB+WphvblpEvCsBLgwk0cec= 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=fMel/r97; arc=none smtp.client-ip=209.85.219.49 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="fMel/r97" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-6b06f1f9a06so12869296d6.3 for ; Tue, 11 Jun 2024 01:12:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718093534; x=1718698334; 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=8IVYq4GaETsYJZvHz4dUOl+klY8inCplH9StLoFX6IE=; b=fMel/r97AwRXfzRf/t4roSGkTmuESgiOdit34GoxNAuN39Iqs+8ZmdSMO2QVIq66FS N3oSErUN4z01RjKNLsRa0mdifTtG3dENFyaH+DLxyPwjsXw/nImIptMgNgu6LUcBHN3G asyqWR1916/mIbC8EYVLAnEtGKepQ1krjQYQY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718093534; x=1718698334; 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=8IVYq4GaETsYJZvHz4dUOl+klY8inCplH9StLoFX6IE=; b=oR1D7R0v657yTODDLFGEsMEitMqN9EubLFDj2FeoTW8FJtyb+v46tVYP5iKa0RAZmA KGYBz9jojD3redZ/HrNg3SRAjbQBoCziBx+y8As02W7k01SDjqAkXk/F9dc+L2sbXbF9 4CeIXhB3L1v5s5NVE8DkgA9FdGri0Kab85PUavF8osSy40Cf2bdFWzr54VuVHiN63fFY Jf0efM3NxjRYEGvX2yxj7vs+/f/OZ/tdoy65y6lxJpNLzoxlZd3pqgddk51er8D3RsGB jtmP4Ar2ehopTyAq6zTfPEMTgM2wA5uGX8c1QQf2tPJxHggxyW8GpLEo5mdSAdOxUQ4Y EHBQ== X-Forwarded-Encrypted: i=1; AJvYcCUkcGA/0lkkmwQwEif5UbiZdoXSrv+9UkjB4f2hwOHrZHIrhHu2c68vFRJ92MPsC/1Y3zcqOTPl8gB00ZRki6zoB5CMrekCWCZ7U/s= X-Gm-Message-State: AOJu0YwK4Z0AscYAqg/b2eTCfvap1Xqiymt02Fayh9a68rIzZsyQKC3m pVmAP3N2r701JD5stRE+F9jZo20guIyD2iOkJ1osxG4Te2JSUcxmQ0e7vl5jWQ== X-Google-Smtp-Source: AGHT+IEjrTQrxUo5zhixs+9k+Fjq2LfdHnJzlQOexh9ApNRRB0qiNKvKRrFodyQbd3JFr46awJRgPA== X-Received: by 2002:a05:6214:319b:b0:6b0:8e1d:f71b with SMTP id 6a1803df08f44-6b08e1dfafcmr6921546d6.49.1718093534519; Tue, 11 Jun 2024 01:12:14 -0700 (PDT) Received: from denia.c.googlers.com (123.178.145.34.bc.googleusercontent.com. [34.145.178.123]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b09039c2b9sm1548886d6.28.2024.06.11.01.12.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 01:12:13 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 11 Jun 2024 08:12:09 +0000 Subject: [PATCH v5 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: <20240611-guenter-mini-v5-4-047b6fe5d08b@chromium.org> References: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> In-Reply-To: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , 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.13.0 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 8fd8250110e2..9108522beea6 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