Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp672169lqs; Fri, 14 Jun 2024 02:02:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhxDxImWnX3+4DW+Abfn2tjMYQCWXD/t7QH56bD22TKxwLeo0b6hnTKtNsgIZgQ1OEakeo+IkvNIQ52dnZGXri4dQ1iz8+fpnspTRiHA== X-Google-Smtp-Source: AGHT+IESYKivvf8YkFEIxcCia194yYA0gAOvmbHOjVZNBds5llFhIAMfo/SXmHKl6ss5qj49qDVK X-Received: by 2002:a05:620a:4409:b0:795:59ec:4377 with SMTP id af79cd13be357-798d259ccf0mr253944685a.47.1718355747558; Fri, 14 Jun 2024 02:02:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718355747; cv=pass; d=google.com; s=arc-20160816; b=EXX9yejH8U9zvkee/M/yzhXSwcvmzFJ5UX5/OjbvJjwfyGsf11dAbVJcckhKCZpCqb sZT78SuhPaHRVm+uIkoHddXZqIrVvC2/nQwe38lu/4tkhflMmY+DL20unNI1cgaNR3kE AtcYCSq/rh/a/YxTCMZL+HkM9wWPPn8zl4J1tLqYW8MMy5YI6GGHzrfkSnO99mxF5AVI 9ZcH8qNamo40ye2ADgYAP+MuOugx77ttUuVHyKHU1XMhRggltHQwpVfMgZzPts5XQtDw pRTXjwCjuxMZN4j77x4T/u6PvwmXSF3jwM0vyFC1a899SZFTGwJILWNW3oU9Hj6mGX1L EhGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=sXOQ7em11Xl1FygGGYkoYQmzZOed4F2VOKf8oGjSeoI=; fh=OCDdRnAgiOtRn0l9DPygyBep7ljzpeKOZcmRxNw/BXk=; b=GTAuMkX3AInt4mMALgFmO+yI3K7/jjwd16rh2C0lEMtkdK5kJipKHc4sJi5BDEpa9T nSvZNGJjN4svicJ9ThHeOkBr3PPpegcEuHjWWYAvETemSqMBfQvqvcNiTo/xIaG/VVlz 9mlm/kPIpp6IZkCDPLXENUIe/hZKJ+tKcCKwq71oXaF0zw4J+vp1/NHRzngY8m5a/KZW 0JrLnzr+3Sa0CsyyvWZqacIf7BBJoUVOPTlm+Y/E0WLsQz65hDfhz+BBkufpR+DWVCij j8bTe0AAo2PEc8R6Is5P9mRMJOotNRnjyRKuiZcmVvDADF4v1/NVV8qa9rXjHK/+Enu8 XRYw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-214595-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214595-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-798abd16be7si315466185a.409.2024.06.14.02.02.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 02:02:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-214595-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-214595-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214595-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 46E401C210CF for ; Fri, 14 Jun 2024 09:02:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 51FEC1922EA; Fri, 14 Jun 2024 09:02:13 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9E2B146A7A; Fri, 14 Jun 2024 09:02:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718355732; cv=none; b=nM14kHWFiLp1ijfYm+ApDYNdBjSEYWtAnswHmVxXR/pGa+yjTsBkoRRV8alLFN+LYmXBY+mhNPvEWQt71v/YXsBjwQnxtOk5i5g5eGs15xHHZlj/WL8Pyh4Z+1VlIDW8y3WBWEgpm5pM2RslyjTFprlwVYwToDcqBwOQ0yt++2A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718355732; c=relaxed/simple; bh=xSwd9RT7wcgXqiFIolq3usCVmZfuWKhwVGxFbsWPf5k=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=qWAzzEdlVMovsSXjlSHylDnIsquI/g72dvfBwuBHhI9sT2/ojz1LlPxslVwaY93n+COafB7tixzgnsGcc66svTEvOFubgmpVc8OhwUolK4Nz2unD8F2XTD8MHIqnmk4ILG4iH6OerlWXecFuS7iuwm7iqtvZ9eFPxN5mKJSYHrA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E23DC32786; Fri, 14 Jun 2024 09:02:10 +0000 (UTC) Message-ID: <08900a94-cbba-4c76-8b47-e467f747cd99@xs4all.nl> Date: Fri, 14 Jun 2024 11:02:08 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 1/4] media: uvcvideo: stop stream during unregister To: Ricardo Ribalda , Mauro Carvalho Chehab Cc: Guenter Roeck , Tomasz Figa , Laurent Pinchart , Alan Stern , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Sakari Ailus References: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> <20240611-guenter-mini-v5-1-047b6fe5d08b@chromium.org> Content-Language: en-US, nl From: Hans Verkuil In-Reply-To: <20240611-guenter-mini-v5-1-047b6fe5d08b@chromium.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/06/2024 10:12, Ricardo Ribalda wrote: > 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 | 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. */ streamming -> streaming > + uvc_queue_streamoff(&stream->queue, stream->type); This should call uvc_queue_release (just a wrapper around vb2_queue_release()) instead. Then it is identical in behavior to vb2_video_unregister_device. Note that uvc_v4l2_release() also calls uvc_queue_release(): it's safe to call uvc_queue_release() twice. With that change: Reviewed-by: Hans Verkuil Regards, Hans > + > + 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); > } >