Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1140727rdf; Wed, 22 Nov 2023 06:43:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdU2YaWfC90WREsFUbLlT4XMgJDEwEKVobfrCdwA6cXAmNwK8ilxxmwdPc+k7y9hFX027j X-Received: by 2002:a17:902:a9cb:b0:1cc:3829:8355 with SMTP id b11-20020a170902a9cb00b001cc38298355mr2075669plr.12.1700664186762; Wed, 22 Nov 2023 06:43:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700664186; cv=pass; d=google.com; s=arc-20160816; b=KzZ22g7vxwbRrX2173o/to9UC/jbL2NOTryUE1GkBmjvRzs5UQwwiS9JSpXyAU6p5S +tQdAgOoiR5H54cI5qd1C87Qx73IlxNTau4EjWd9e6xusTh/ATBakzvvjj9wypQF/n2A 2sWr1V40pWm85qD/BNTofuV7G7Z/rS4aFYsqeuhI0rrs+muO8TWDPcNTC4AmorcAGAAG VwcIjSFeg8OSwpJYezrkf0umXmiE4Ai500UmhPBEATDEGhJih+UDJnkX7J8DEt2z38XU 6qHuZBBcJkOw1AxEwbnIeRXLEq02Du82RvHz/xOTHD0zXhOpMyusB9kc0sIHpeqKHmCU 0pbg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jloNRvYuaaF2FKShhuxL+B4kWjy4TZNLsSyG0DAi95o=; fh=a6VsggPG9tEKuhkmtzjCvdOafQ4NDBx+WcKcqMQUYrM=; b=oLMSX1RkSdMQyl37RDgJxakggBoAtnm5YmpEQWDjmezoRsNi6k3l37Qnls/ecZTVmB KlQxLWJX06TEkTEIUvTylUWWrx57Q0k5lsQcZRLsUi6aFQlFD279c98+oUhJ0r+JnumI NvIMPaAxm+1yyfYoiOyW6ldyoRB3evZLSR0TDNsZEZLy5fSolckptXHNBXedSgyIFT0c 8sD0Dw/vnEuqsXXg3KFGzCNeHL+AfbHUzSazf6BY+3j+dsEPosx7AITyjZqy+gJ5zGfV Vbge+aJxoOfbY6ng9ZaLQ72XibgL6LapBocfQtTMaCNsbgR6QQ+L1uOu3Z88tXtQcG7l sDFw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@iki.fi header.s=lahtoruutu header.b=KmYYMrVe; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id n17-20020a170902e55100b001cf5572d972si9472283plf.210.2023.11.22.06.43.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 06:43:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@iki.fi header.s=lahtoruutu header.b=KmYYMrVe; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 476F4809B9C4; Wed, 22 Nov 2023 02:25:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343748AbjKVKZ3 (ORCPT + 99 others); Wed, 22 Nov 2023 05:25:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231174AbjKVKZ2 (ORCPT ); Wed, 22 Nov 2023 05:25:28 -0500 Received: from lahtoruutu.iki.fi (lahtoruutu.iki.fi [IPv6:2a0b:5c81:1c1::37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30BF7D4F; Wed, 22 Nov 2023 02:25:24 -0800 (PST) Received: from hillosipuli.retiisi.eu (185-9-10-242.cust.suomicom.net [185.9.10.242]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sailus) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4SZy762YBCz49Q5M; Wed, 22 Nov 2023 12:25:18 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1700648722; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jloNRvYuaaF2FKShhuxL+B4kWjy4TZNLsSyG0DAi95o=; b=KmYYMrVeVcPWgXg9W+/diTJe8ILYaWohSX/YrCLzUFm5NAsmh7V0qOWxfRN36zC9h3hNaz agq9Ez8eTL/7QmmP3pO2DCTymPHB6WCHmoi/w8uDyJLBy2LRhDoHZ9bQdE6ARo+fPR1FGc omfj2RRi8MC46kRDpEuPDZQ+FcyNwZ/HEU21xIUVqJBNeifD29KSZ5hhigLWwuaueiGyCU 5Myk+8NuhpXK7QJkv9MdahI/RV2HbVzwOddIiVDipNvGtePXpwjGduH4nwcsQb7O3ebbC8 ZYx2SOksrWP4iiaAds0nEx1bWMH4fWrAkIxzhd1cnWgObltPjN9hBWGbsyBCfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1700648722; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jloNRvYuaaF2FKShhuxL+B4kWjy4TZNLsSyG0DAi95o=; b=qYSm/k/mYGB687/E/ZMumXgcDXnas7DohVSOYhsDnzZFdREMCqoqWWCBhq8INVWgbaQ3Ci JmHKfShctamhJ8xruW5fAh9XzGd6nBvxGOEwa6o7KPUDceVMJZW0ec7x1p+fKNZc55mb0p ApUJoH5Fi8dY8iXWBZWybwDSXlu2RR1lsP7b9pJ4PiwH12L5cvv6hWMd9VJHKaKmjny6+/ EYIhgVIvX4jDF2ugXZqFfhToyhuwaLnQoFndKQeCco4u9nMDofUE/+BeLrEizEfcn1eFjc fdKZvNl/n1rMuz/MFwgTa+5dnOddvHfxWZ8qLhW3R/ear4e2zIdKJhoDD6g5aQ== ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1700648722; a=rsa-sha256; cv=none; b=iGdaTXLqCo9CVVvaw9/N+0RDCVC/qjOTahXeRSRjZEqYO6EerCRylv6RMMv6Wi9lXJuW7i J7cz3/rM5tBJyXtL40Rrm/wrCXvj9TjbFe0IPjI8TX1992B0oAjar4f2JiJ2ExaY4TJAdd ugC5qZE/7Y19zpbVa2nyIqZ55EyxLQ/+nWXziUoqwEb34BIWNG4hq86Y9E7af5klU/OucN cXamDPTtUzOsTdBV28DjOl4W5LtXVQcfELxqNa7Su+hSFH486JCJzPNdWjaa6Qi1ztDzd3 sVmPXsSDFCXA7j4xn4aQ2+FCW9NVNfcuRMuGnObgXprb3kzqqOXmc0/VBsQ8qA== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=sailus smtp.mailfrom=sakari.ailus@iki.fi Received: from valkosipuli.retiisi.eu (valkosipuli.localdomain [192.168.4.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by hillosipuli.retiisi.eu (Postfix) with ESMTPS id 6F143634C93; Wed, 22 Nov 2023 12:25:18 +0200 (EET) Date: Wed, 22 Nov 2023 10:25:18 +0000 From: Sakari Ailus To: Ricardo Ribalda Cc: Mauro Carvalho Chehab , Guenter Roeck , Tomasz Figa , Laurent Pinchart , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Sakari Ailus Subject: Re: [PATCH v3 2/3] media: uvcvideo: Do not halt the device after disconnect Message-ID: References: <20231121-guenter-mini-v3-0-d8a5eae2312b@chromium.org> <20231121-guenter-mini-v3-2-d8a5eae2312b@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231121-guenter-mini-v3-2-d8a5eae2312b@chromium.org> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 22 Nov 2023 02:25:56 -0800 (PST) Hi Ricardo, Thank you for the patch. On Tue, Nov 21, 2023 at 07:53:49PM +0000, Ricardo Ribalda wrote: > usb drivers should not call to any usb_() function after the > .disconnect() callback has been triggered. > > If the camera is streaming, the uvc driver will call usb_set_interface or > usb_clear_halt once the device is being released. Let's fix this issue. > > This is probably not the only driver affected with this kind of bug, but > until there is a better way to do it in the core this is the way to > solve this issue. > > When/if a different mechanism is implemented in the core to solve the > lifetime of devices we will adopt it in uvc. > > Trace: > [ 1065.389723] drivers/media/usb/uvc/uvc_driver.c:2248 uvc_disconnect enter > [ 1065.390160] drivers/media/usb/uvc/uvc_driver.c:2264 uvc_disconnect exit > [ 1065.433956] drivers/media/usb/uvc/uvc_v4l2.c:659 uvc_v4l2_release enter > [ 1065.433973] drivers/media/usb/uvc/uvc_video.c:2274 uvc_video_stop_streaming enter > [ 1065.434560] drivers/media/usb/uvc/uvc_video.c:2285 uvc_video_stop_streaming exit > [ 1065.435154] drivers/media/usb/uvc/uvc_v4l2.c:680 uvc_v4l2_release exit > [ 1065.435188] drivers/media/usb/uvc/uvc_driver.c:2248 uvc_disconnect enter > > Signed-off-by: Ricardo Ribalda > --- > drivers/media/usb/uvc/uvc_driver.c | 2 ++ > drivers/media/usb/uvc/uvc_video.c | 45 ++++++++++++++++++++++++-------------- > drivers/media/usb/uvc/uvcvideo.h | 2 ++ > 3 files changed, 32 insertions(+), 17 deletions(-) > > diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c > index 08fcd2ffa727..413c32867617 100644 > --- a/drivers/media/usb/uvc/uvc_driver.c > +++ b/drivers/media/usb/uvc/uvc_driver.c > @@ -2257,6 +2257,8 @@ static void uvc_disconnect(struct usb_interface *intf) > return; > > uvc_unregister_video(dev); > + /* Barrier needed to synchronize with uvc_video_stop_streaming(). */ > + smp_store_release(&dev->disconnected, true); > kref_put(&dev->ref, uvc_delete); > } > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c > index 28dde08ec6c5..032b44e45b22 100644 > --- a/drivers/media/usb/uvc/uvc_video.c > +++ b/drivers/media/usb/uvc/uvc_video.c > @@ -2243,28 +2243,39 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) > return ret; > } > > -void uvc_video_stop_streaming(struct uvc_streaming *stream) > +static void uvc_video_halt(struct uvc_streaming *stream) > { > - uvc_video_stop_transfer(stream, 1); > + unsigned int epnum; > + unsigned int pipe; > + unsigned int dir; > > if (stream->intf->num_altsetting > 1) { Doesn't this imply the device is using isochronous mode? > usb_set_interface(stream->dev->udev, stream->intfnum, 0); > - } else { > - /* > - * UVC doesn't specify how to inform a bulk-based device > - * when the video stream is stopped. Windows sends a > - * CLEAR_FEATURE(HALT) request to the video streaming > - * bulk endpoint, mimic the same behaviour. > - */ > - unsigned int epnum = stream->header.bEndpointAddress > - & USB_ENDPOINT_NUMBER_MASK; > - unsigned int dir = stream->header.bEndpointAddress > - & USB_ENDPOINT_DIR_MASK; > - unsigned int pipe; > - > - pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir; > - usb_clear_halt(stream->dev->udev, pipe); > + return; > } > > + /* > + * UVC doesn't specify how to inform a bulk-based device Then this comment doesn't look right. What about the code? This isn't mentioned in the commit message either. > + * when the video stream is stopped. Windows sends a > + * CLEAR_FEATURE(HALT) request to the video streaming > + * bulk endpoint, mimic the same behaviour. > + */ > + epnum = stream->header.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; > + dir = stream->header.bEndpointAddress & USB_ENDPOINT_DIR_MASK; > + pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir; > + usb_clear_halt(stream->dev->udev, pipe); > +} > + > +void uvc_video_stop_streaming(struct uvc_streaming *stream) > +{ > + uvc_video_stop_transfer(stream, 1); > + > + /* > + * Barrier needed to synchronize with uvc_disconnect(). > + * We cannot call usb_* functions on a disconnected USB device. > + */ > + if (!smp_load_acquire(&stream->dev->disconnected)) > + uvc_video_halt(stream); > + > uvc_video_clock_cleanup(stream); > } > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h > index 6fb0a78b1b00..4318ce8e31db 100644 > --- a/drivers/media/usb/uvc/uvcvideo.h > +++ b/drivers/media/usb/uvc/uvcvideo.h > @@ -559,6 +559,8 @@ struct uvc_device { > unsigned int users; > atomic_t nmappings; > > + bool disconnected; > + > /* Video control interface */ > #ifdef CONFIG_MEDIA_CONTROLLER > struct media_device mdev; > -- Kind regards, Sakari Ailus