From patchwork Tue Sep 12 04:19:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avichal Rakesh X-Patchwork-Id: 137972 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp200473vqx; Mon, 11 Sep 2023 23:04:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEhPlQENVvVXL/1ERgBbol96vin7LWGm8EjNnFqzCeb+zUAZiMnndt/IOtUHIsQaGTB6yE X-Received: by 2002:a05:6a00:801b:b0:68f:c865:5ba8 with SMTP id eg27-20020a056a00801b00b0068fc8655ba8mr4494436pfb.18.1694498656885; Mon, 11 Sep 2023 23:04:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694498656; cv=none; d=google.com; s=arc-20160816; b=Syme3kRZu39w7qYxzdAWMOFnwf/uhSYiwvoRgk9TN8vXW0VkTQtR9qHpuPnYM7U29A TEhu78TL6lWEBapjzGzMQVgAo277Oy1jwJ9o4vL/Jug+vB39PYfj195WVD+vby+dwFFF 1LY/rJ/Q5A2zRiRGakIz1dcm29npl0RbESG2g4qFzBt9uODXnucG9mogHJqV/+x8otG3 EWR3kDevw/NtiyLgmDHqqFTOSM8mkO/SzvF3a0OKrXzq09M/e1/UOjyHHVTS0ivlSOr6 fxXMn9Rt8crNmTu5abYjSqluzBhpebvnnMSxaDkrMPFIh1PjKaFyKNPkbsEn1D31sM3c Ryvw== 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=3FoTtHZn5McnPgtWNsfVZSyVXUY1RZpX9ounYkXsDLo=; fh=QvX9MTmnaAE6jfh4r/Rtm06n4Sfej0NUOiMyXoms1KM=; b=zvuNobV7y9xxBTLb5I6E4cWN+BQ3I7nYn4KkwjYs34W11pBA1+Cveyuo1vHKxeEACY w0+Im9XRMNSRlkkWafQi4go4i+hqRzpIeyld22s2SsJW3xqEk/0MPbO1hBqPTrfzDdrl RIcZUogKXj2LZkB+8fNcSERhAtF3LqNb1YVp2eRN3jeeeRjtNxgw9AV8KLQABV1zVoeL 0SGjFYyQ5ccYf1OF3gM+qbjHmiR1LT5SCwEMKRMbFWxrNB2egz89Qn8bGwglLv+12PvT ZSN3OKVZmNdoHkc8Ryc5BE9xa+nZxdjgVUe8AzY9dv/OtRZxvILMuswF0V/F3iutMP5m dFyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m01Afecu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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. [23.128.96.37]) by mx.google.com with ESMTPS id f16-20020a056a00229000b0068e45c9c986si7848093pfe.136.2023.09.11.23.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 23:04:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m01Afecu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 0277B81A7AE8; Mon, 11 Sep 2023 21:50:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232394AbjILETw (ORCPT + 40 others); Tue, 12 Sep 2023 00:19:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232146AbjILETp (ORCPT ); Tue, 12 Sep 2023 00:19:45 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C7C7E64 for ; Mon, 11 Sep 2023 21:19:38 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1bf60f85d78so73910595ad.3 for ; Mon, 11 Sep 2023 21:19:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694492377; x=1695097177; 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=3FoTtHZn5McnPgtWNsfVZSyVXUY1RZpX9ounYkXsDLo=; b=m01Afecuhy9AK3GxxTyUuPvu74pfp2YF5BpLxhxSTCguBgYu9/ZbTxh3cRWhTBfUbb d481CrU/dV0gARnOvAYUDAN/h3YN6vfizBcQv194JXZdOP2H3gee98/hLKPom8RsHfE0 e9fCzHuAbchc62yUbTiOJ1mwklZMWsF9aEhl7qUQS55CQrKCs4w5MEOokhSWhKZFRVs8 RyssCBzXxrJd2Z2bw0Yr4JL9z8kxaFKDU/6XeiYH9LufPuwBwCm3WSMlIrWsObnhXSsN byR79q4iSenoZHcQw3nZmgD313yu6HeZ49DV3zVdCBamw3jZw2um1w5nlsSi/cGPtZrY ngPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694492377; x=1695097177; 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=3FoTtHZn5McnPgtWNsfVZSyVXUY1RZpX9ounYkXsDLo=; b=Vw+bH/8+swmOP1As6lFVp0p+QFBd6dHGIMzMZuBxCYBWTQhX6b70otS2D+r5GZJ67H ckaEBKDviBS47jxQcqp2vx14VcQBauXTfPjAKm47EEPG/mxh/FGrC7aZ573cZn4+mZqj THkTBw9ejDUaaDtWazSw1CcWoasK4Q2O2lI+W2shcuprWe0Yl/YU+NfLBZ/9w5OqIxET ui8LkYl1Lr8rvP3QsjC0zIRQ1OmeDIyTczavIw6ZvofHgRcfw8xyqiqx357wvN9qmPUV hbj3LCQx1PpWHaB7vvB8RYi6YC/jI4BT6fSULaBef5UsF4K9d54P0lQ/XWF89tsSjUuR PUgA== X-Gm-Message-State: AOJu0YwOjPODUy40bWqpTfYtBpyy10SLPcP4X9zMV9CdsySHJX9RCIaB ilgPDFeHNUdWWF91i/KzpVKT40i5MybE X-Received: from hi-h2o-specialist.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3cef]) (user=arakesh job=sendgmr) by 2002:a17:902:e808:b0:1bb:de7f:a4b7 with SMTP id u8-20020a170902e80800b001bbde7fa4b7mr4796225plg.10.1694492377612; Mon, 11 Sep 2023 21:19:37 -0700 (PDT) Date: Mon, 11 Sep 2023 21:19:10 -0700 In-Reply-To: <20230912041910.726442-1-arakesh@google.com> Mime-Version: 1.0 References: <20230912041910.726442-1-arakesh@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912041910.726442-3-arakesh@google.com> Subject: [PATCH v1 2/2] usb: gadget: uvc: prevent de-allocating inflight usb_requests From: Avichal Rakesh To: Laurent Pinchart , Daniel Scally , Greg Kroah-Hartman , Michael Grzeschik Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Avichal Rakesh 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]); Mon, 11 Sep 2023 21:50:50 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776810623544480477 X-GMAIL-MSGID: 1776810623544480477 Currently, when stopping the stream, uvcg_video_enable immediately deallocates the usb_requests after calling usb_ep_dequeue. However, usb_ep_dequeue is asynchronous and it is possible that it deallocates an inflight request. The gadget drivers should wait until the complete callbacks before assuming ownership of the request. This patch adds a simple request counting mechanism to track how many requests are currently owned by the driver. Now when stopping the stream, uvcg_video_enable waits for all the complete callbacks to come through before deallocating the usb_requests. Signed-off-by: Avichal Rakesh --- drivers/usb/gadget/function/uvc.h | 3 +++ drivers/usb/gadget/function/uvc_video.c | 31 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) -- 2.42.0.283.g2d96d420d3-goog diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h index 989bc6b4e93d..e40e702a7074 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -104,8 +104,11 @@ struct uvc_video { unsigned int req_size; struct uvc_request *ureq; struct list_head req_free; + unsigned int req_free_count; /* number of requests in req_free */ spinlock_t req_lock; + wait_queue_head_t req_free_queue; + unsigned int req_int_count; void (*encode) (struct usb_request *req, struct uvc_video *video, diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index 70ff88854539..3ea7d52df80d 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -284,10 +284,18 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req) spin_lock_irqsave(&video->req_lock, flags); list_add_tail(&req->list, &video->req_free); + video->req_free_count++; spin_unlock_irqrestore(&video->req_lock, flags); - if (uvc->state == UVC_STATE_STREAMING) + if (uvc->state == UVC_STATE_STREAMING) { queue_work(video->async_wq, &video->pump); + } else if (video->req_free_count == video->req_size) { + /* + * Wake up thread waiting for all requests to be returned to + * the gadget driver. + */ + wake_up_interruptible(&video->req_free_queue); + } } static int @@ -316,6 +324,7 @@ uvc_video_free_requests(struct uvc_video *video) INIT_LIST_HEAD(&video->req_free); video->req_size = 0; + video->req_free_count = 0; return 0; } @@ -360,6 +369,7 @@ uvc_video_alloc_requests(struct uvc_video *video) } video->req_size = req_size; + video->req_free_count = req_size; /* all requests are currently free */ return 0; @@ -404,6 +414,7 @@ static void uvcg_video_pump(struct work_struct *work) req = list_first_entry(&video->req_free, struct usb_request, list); list_del(&req->list); + video->req_free_count--; spin_unlock_irqrestore(&video->req_lock, flags); /* @@ -480,6 +491,7 @@ static void uvcg_video_pump(struct work_struct *work) spin_lock_irqsave(&video->req_lock, flags); list_add_tail(&req->list, &video->req_free); + video->req_free_count++; spin_unlock_irqrestore(&video->req_lock, flags); return; } @@ -506,6 +518,22 @@ int uvcg_video_enable(struct uvc_video *video, int enable) if (video->ureq && video->ureq[i].req) usb_ep_dequeue(video->ep, video->ureq[i].req); + /* + * Wait 500ms for the usb_requests to be given back to the + * gadget driver. This ensures that we don't accidentally + * reference de-allocated usb_requests in the complete callback. + */ + if (video->req_free_count != video->req_size) { + uvcg_info(&video->uvc->func, + "Waiting 500ms for usb_request complete callbacks.\n"); + ret = wait_event_interruptible_timeout( + video->req_free_queue, + video->req_free_count == video->req_size, + msecs_to_jiffies(500)); + uvcg_info(&video->uvc->func, + "Done waiting for complete callbacks: %d\n", ret); + } + uvc_video_free_requests(video); uvcg_queue_enable(&video->queue, 0); return 0; @@ -538,6 +566,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc) { INIT_LIST_HEAD(&video->req_free); spin_lock_init(&video->req_lock); + init_waitqueue_head(&video->req_free_queue); INIT_WORK(&video->pump, uvcg_video_pump); /* Allocate a work queue for asynchronous video pump handler. */