From patchwork Thu Nov 9 00:41:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avichal Rakesh X-Patchwork-Id: 163180 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp137488vqs; Wed, 8 Nov 2023 16:41:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IEu5rAgtnpXRjcPZHUWAQz5aljPGdiFkwpjVkRQglSC7U9s9pVhZtAippCuG08MwdJQVkFw X-Received: by 2002:a17:90a:aa81:b0:280:b062:202 with SMTP id l1-20020a17090aaa8100b00280b0620202mr209282pjq.33.1699490476141; Wed, 08 Nov 2023 16:41:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699490476; cv=none; d=google.com; s=arc-20160816; b=XpeCkRLSKB4Szz5KaGk5mMpR4Papa6bJpEHjIQVX5s5SKinsttN5tqrBRJqBdeXD7L BUMjh4u1T+rhqbsQWlBpSjOhzK/oPlbiP5NxsHEK9dzH/91eIJKKp1cpWg65ZkcBa8Wf YoiVIUnKfEuIwTv2P4wmkVwkxh+VCnl4+0XkncUU9U+45LY7qCMgCpmtWBSDmLTBNjeK Bn5aR9wH8Yl4lewXH5LsKz2lMqYKsTwUo29jnQjlJveLcPzju5fbRQbkD1OAtB8y1xiC vg2tZyNVEUnpgyTHQUVCiZii+H0NauTzBVwlPyvYIAxp221XiZitwimJwwYSBKfFQMs0 dA/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=H3EiwN1qoUJxzkJq7np8tfIJfl4CuGSTuhpyOd2tJ0s=; fh=812M7VuOpiTijUQuamc/m0WVPtcBYikel9HAhgg9kY8=; b=nXq038OqLJYmaAsHJrYg/8ngCV0qhNUXoDA0F6mbTj/G+PdRpVdwqBBchCpg1NDBvB WT2IbfnNdc2P4Bfah2uMq+oSx9doBS06C/pAE9YO6Yz9uJsGs32vjCv8tCtbFJVVrJ10 savM4RNAeoFCVlJjU6CmaX3wPrmkrA00GKUAbK1s9bxiARvAoRnJ/8Imv4PIiCLV9GGr ZvFtSCgccEPCZzl36EUlLwv1zes5Bk99bbXmy0jM5e2WXK3/w4HUytvOumSBCK/ykn3t fI5sbWdsY8/ERAdzihbc3rR+/ltQi/twOnTPNuVPTiNGtNSuMnCXodi2GAsZcoXPk/Zv 3o/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4kFlzVHv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id fz3-20020a17090b024300b0027d032092d7si319943pjb.184.2023.11.08.16.41.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 16:41:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4kFlzVHv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 51AA181CEB2C; Wed, 8 Nov 2023 16:41:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230451AbjKIAlM (ORCPT + 32 others); Wed, 8 Nov 2023 19:41:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjKIAlL (ORCPT ); Wed, 8 Nov 2023 19:41:11 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B72A2693 for ; Wed, 8 Nov 2023 16:41:09 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5b02ed0f886so4469067b3.0 for ; Wed, 08 Nov 2023 16:41:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699490468; x=1700095268; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=H3EiwN1qoUJxzkJq7np8tfIJfl4CuGSTuhpyOd2tJ0s=; b=4kFlzVHvsQH9kXbIcIS917dp+b+1apoiiGwUo4dY2KCLiruw0L35IjaBsdnI5tj0bT VbcQeqqOVuPMbQJ4fKRH0ckABnfqC5FhsRpFM3oTkD66SsTA1HOwYaavsrw3AbJT1DHR lrfctQ0NLRjGESZ0m1K4tHQKIQEe3w91Zm3LxO0Mg1LZoC6vUWJQg7EfkxZ3AMWuWZJf X2gL1EH8hyfhlOOVHgiNvDlu2899RM56PpeAxzjDXDcKybe4gFT4o0ZPl06g2cBzwLdT 1TCY3A52S3P/pxlyHRAqpakk/S++rDIm9NmpPvorkgTF5tq6aExRLYXlGhgodP3SVD2c LYyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699490468; x=1700095268; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=H3EiwN1qoUJxzkJq7np8tfIJfl4CuGSTuhpyOd2tJ0s=; b=sHAs4/sUhHSvuydO3QQ86DAMUluBj5oJRzFr/1AEgRIwJNwGc90jcnwhGhglC07gJE wrdqXGnmIvqpDwhgMpmoWSVvGQoZ1HArjrtvVnBYYyEeHpWqFJFTJgYuZq6x9LcclPP1 eVXWM0PRBUynz//rwREymLN2BGCoNeI9YIgaiDvVqx9woX1xtV/khg3OzMKnEEkdM3le Vv8coa2ww99o29VMcNZImerBMiUR+QQEKfpWAvolYzLnhh10DTX70KO8iW4BR5t5ypRQ 1RguPpP4sOvH8PU0eIKlr3XTNvHzps4h0CCGk423HrQDl5/CokVZZtDEgLMyk/jKMQpZ NRKw== X-Gm-Message-State: AOJu0YwPp7Y8C35CvEeVtkwvgSVbjuXYIFu2We8RI7KQrM9mHUxKYrk/ iK4D4+rOj9938FUIhREM4RqkaR+E7lQx X-Received: from hi-h2o-specialist.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3cef]) (user=arakesh job=sendgmr) by 2002:a0d:d752:0:b0:59b:e97e:f7df with SMTP id z79-20020a0dd752000000b0059be97ef7dfmr100355ywd.2.1699490468293; Wed, 08 Nov 2023 16:41:08 -0800 (PST) Date: Wed, 8 Nov 2023 16:41:01 -0800 In-Reply-To: <73309396-3856-43a2-9a6f-81a40ed594db@google.com> Mime-Version: 1.0 References: <73309396-3856-43a2-9a6f-81a40ed594db@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231109004104.3467968-1-arakesh@google.com> Subject: [PATCH v12 1/4] usb: gadget: uvc: prevent use of disabled endpoint From: Avichal Rakesh To: arakesh@google.com, dan.scally@ideasonboard.com, gregkh@linuxfoundation.org Cc: etalvala@google.com, jchowdhary@google.com, laurent.pinchart@ideasonboard.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, m.grzeschik@pengutronix.de 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 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 16:41:15 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780941353830649923 X-GMAIL-MSGID: 1782044925686232078 Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint. This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only. Link: https://lore.kernel.org/20230615171558.GK741@pendragon.ideasonboard.com/ Link: https://lore.kernel.org/20230531085544.253363-1-dan.scally@ideasonboard.com/ Reviewed-by: Daniel Scally Reviewed-by: Michael Grzeschik Tested-by: Michael Grzeschik Signed-off-by: Avichal Rakesh --- v1 -> v2 : Rebased to ToT and reworded commit message. v2 -> v3 : Fix email threading goof-up v3 -> v4 : Address review comments & re-rebase to ToT v4 -> v5 : Add Reviewed-by & Tested-by v5 -> v6 : No change v6 -> v7 : No change v7 -> v8 : No change. Getting back in review queue v8 -> v9 : Fix typo. No functional change. v9 -> v10 : Rebase to ToT (usb-next) v10 -> v11 : No change v11 -> v12 : Rebase to ToT (usb-next) drivers/usb/gadget/function/f_uvc.c | 11 +++++------ drivers/usb/gadget/function/f_uvc.h | 2 +- drivers/usb/gadget/function/uvc.h | 2 +- drivers/usb/gadget/function/uvc_v4l2.c | 20 +++++++++++++++++--- drivers/usb/gadget/function/uvc_video.c | 3 ++- 5 files changed, 26 insertions(+), 12 deletions(-) -- 2.42.0.869.gea05f2083d-goog diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c index 786379f1b7b7..77999ed53d33 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) return 0; } -void uvc_function_setup_continue(struct uvc_device *uvc) +void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep) { struct usb_composite_dev *cdev = uvc->func.config->cdev; + if (disable_ep && uvc->video.ep) + usb_ep_disable(uvc->video.ep); + usb_composite_setup_continue(cdev); } @@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) if (uvc->state != UVC_STATE_STREAMING) return 0; - if (uvc->video.ep) - usb_ep_disable(uvc->video.ep); - memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_STREAMOFF; v4l2_event_queue(&uvc->vdev, &v4l2_event); - uvc->state = UVC_STATE_CONNECTED; - return 0; + return USB_GADGET_DELAYED_STATUS; case 1: if (uvc->state != UVC_STATE_CONNECTED) diff --git a/drivers/usb/gadget/function/f_uvc.h b/drivers/usb/gadget/function/f_uvc.h index 1db972d4beeb..083aef0c65c6 100644 --- a/drivers/usb/gadget/function/f_uvc.h +++ b/drivers/usb/gadget/function/f_uvc.h @@ -11,7 +11,7 @@ struct uvc_device; -void uvc_function_setup_continue(struct uvc_device *uvc); +void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep); void uvc_function_connect(struct uvc_device *uvc); diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h index 6751de8b63ad..989bc6b4e93d 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -177,7 +177,7 @@ struct uvc_file_handle { * Functions */ -extern void uvc_function_setup_continue(struct uvc_device *uvc); +extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep); extern void uvc_function_connect(struct uvc_device *uvc); extern void uvc_function_disconnect(struct uvc_device *uvc); diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c index 3f0a9795c0d4..7cb8d027ff0c 100644 --- a/drivers/usb/gadget/function/uvc_v4l2.c +++ b/drivers/usb/gadget/function/uvc_v4l2.c @@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) * Complete the alternate setting selection setup phase now that * userspace is ready to provide video frames. */ - uvc_function_setup_continue(uvc); + uvc_function_setup_continue(uvc, 0); uvc->state = UVC_STATE_STREAMING; return 0; @@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) struct video_device *vdev = video_devdata(file); struct uvc_device *uvc = video_get_drvdata(vdev); struct uvc_video *video = &uvc->video; + int ret = 0; if (type != video->queue.queue.type) return -EINVAL; - return uvcg_video_enable(video, 0); + uvc->state = UVC_STATE_CONNECTED; + ret = uvcg_video_enable(video, 0); + if (ret < 0) + return ret; + + uvc_function_setup_continue(uvc, 1); + return 0; } static int @@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh, static void uvc_v4l2_disable(struct uvc_device *uvc) { uvc_function_disconnect(uvc); + /* + * Drop uvc->state to CONNECTED if it was streaming before. + * This ensures that the usb_requests are no longer queued + * to the controller. + */ + if (uvc->state == UVC_STATE_STREAMING) + uvc->state = UVC_STATE_CONNECTED; + uvcg_video_enable(&uvc->video, 0); uvcg_free_buffers(&uvc->video.queue); uvc->func_connected = false; @@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = { .get_unmapped_area = uvcg_v4l2_get_unmapped_area, #endif }; - diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index 91af3b1ef0d4..c334802ac0a4 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work) struct uvc_video_queue *queue = &video->queue; /* video->max_payload_size is only set when using bulk transfer */ bool is_bulk = video->max_payload_size; + struct uvc_device *uvc = video->uvc; struct usb_request *req = NULL; struct uvc_buffer *buf; unsigned long flags; bool buf_done; int ret; - while (video->ep->enabled) { + while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) { /* * Retrieve the first available USB request, protected by the * request lock.