Message ID | 20221212-uvc-race-v2-1-54496cc3b8ab@chromium.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp158313wrn; Tue, 13 Dec 2022 06:38:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ZHvB99+hk0yrEpbkd12Ogy5zZ1hvkiiDgJFkr/wiEBNY/ZRfODooM/wuGCqZOvG2+qFD6 X-Received: by 2002:a17:90a:bd17:b0:218:77c1:5557 with SMTP id y23-20020a17090abd1700b0021877c15557mr21003594pjr.41.1670942295454; Tue, 13 Dec 2022 06:38:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670942295; cv=none; d=google.com; s=arc-20160816; b=Kqc3RL2E6FmBO9yLBtQ3aNpT8PaL6bom55mZu9+zrwu713fM/oMYJlRMvb0GqUonaR x3qwOGytLgE0gY3Nl9PxYLJ+ztoEgDqbhS8iYPrSc/4CYyW8dlyodF60hRmeOzGMjPiD CcOhD1jezWasByg/Lyp3Tqv+TKk8Bp4IjGIVGfuzus6R9zSkcmvR6H13XltAEZzNLtQO Ecq1RHnS+P3YIpG26l0K9saZQUU/Qf2qAGAxNxvxBjUYN1OdCxtYL6eg8wZZgYVbTBXW b/3lsxbSRvfjvuFWEdzNBmLiz9+0MilBHjfv/W/FNwS4jDAJRGO8eYwCumlNUhdOIpF5 2W1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=1FvB7MtetQWVy4gpQjKQUoehBeWEkd2f6NLqciuJ/UA=; b=Faq3A0CX1t9rPFtw4C/bonucAirAoQyup33irrpGcumankOVGx7DSRyTo+1/1DrhdQ rbWF37wg5wMJM2/xFcU2xDpaitjX405tDULuJdQh2mq+aEXFdx4/jyuKnty6wnMiKTLg UEau5sEGdbi8Q0JziELtCXpxYTHSp51q5SDJlAnQ2pFYswdjPI/G6m0ALYuu0uygFmpz tL5cLKbQQIrn+gEXtORACmTJMxxAUzcjt9kZFEvQL38b515MS4qt0usM5ZUemZVvAHwn NydYvYG3jT2G9phUvQuJiqk/za3yVNMe4+S1jLpEIhXwKMBKaVdHQFl2DlcJt1/ca8/j Qnpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=a07UG2F1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lx13-20020a17090b4b0d00b002194ca43255si13153579pjb.50.2022.12.13.06.38.01; Tue, 13 Dec 2022 06:38:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=a07UG2F1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235686AbiLMOgP (ORCPT <rfc822;jeantsuru.cumc.mandola@gmail.com> + 99 others); Tue, 13 Dec 2022 09:36:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235586AbiLMOgI (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 13 Dec 2022 09:36:08 -0500 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B07313CE5 for <linux-kernel@vger.kernel.org>; Tue, 13 Dec 2022 06:36:04 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id s5so17967396edc.12 for <linux-kernel@vger.kernel.org>; Tue, 13 Dec 2022 06:36:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=1FvB7MtetQWVy4gpQjKQUoehBeWEkd2f6NLqciuJ/UA=; b=a07UG2F1mEaOU+rcH4QvgYNzKNzFe9RCVowEPRQ2GEaa6UpV37hXVOvsNsv0WRkxTr QSml1QV9DD3ArrZKWjULnA+kU2iKaeh6VkQ5E/9/djOD3PtiBxzXlb1Cpkx6fV058FkX pW6bk/Z03oO9ZBAzBLvf48kGNqIP6VbvOCL9s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=1FvB7MtetQWVy4gpQjKQUoehBeWEkd2f6NLqciuJ/UA=; b=P0Yc/5ZR9IeAnI5YxSHYo4iNya3iC+HM03fl78DFiXeAFugRXCpM8g62tCOalkjQZB D5K84DQfd63Hh64B8CsKSmdIzviuIdPz5KJ2MuoY1Ucn1FOevcflYkTitz9qIL4LDR08 hD8V/KvM08xzRWTJwTCnYDWPKe+JwjNeJziUh5w69CBkH5KP2rblR2r4GReC08EonLsE 0MJFwwkFe3RLOAwmgY4yCKDi8BFtdxTJePa0l2OtSs/04eNcM5lDjzGYm3YFs6DNpVde ySjDYhIcCAfQAoJRvnMD0RN2bquuVO6mF7v+BTWcFyTQCopEcG6GN+uP6s45+KttK/Rx ES0Q== X-Gm-Message-State: ANoB5pkTaD4QaApqHezryH7qaDbgPJNjGmVSd8pdyliX0J6q+6u/m/8D j2hpD1lXN+ncyf3QlbhZ9yECSQ== X-Received: by 2002:a05:6402:1ccc:b0:461:8a1a:271f with SMTP id ds12-20020a0564021ccc00b004618a1a271fmr19408522edb.11.1670942162928; Tue, 13 Dec 2022 06:36:02 -0800 (PST) Received: from alco.roam.corp.google.com ([100.104.168.209]) by smtp.gmail.com with ESMTPSA id kv17-20020a17090778d100b00781dbdb292asm4613960ejc.155.2022.12.13.06.36.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Dec 2022 06:36:02 -0800 (PST) From: Ricardo Ribalda <ribalda@chromium.org> Date: Tue, 13 Dec 2022 15:35:30 +0100 Subject: [PATCH v2 1/2] media: uvcvideo: Fix race condition with usb_kill_urb MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20221212-uvc-race-v2-1-54496cc3b8ab@chromium.org> References: <20221212-uvc-race-v2-0-54496cc3b8ab@chromium.org> In-Reply-To: <20221212-uvc-race-v2-0-54496cc3b8ab@chromium.org> To: Max Staudt <mstaudt@google.com>, Sergey Senozhatsky <senozhatsky@chromium.org>, Yunke Cao <yunkec@chromium.org>, Laurent Pinchart <laurent.pinchart@ideasonboard.com>, Mauro Carvalho Chehab <mchehab@kernel.org> Cc: linux-media@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org, Ricardo Ribalda <ribalda@chromium.org> X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3069; i=ribalda@chromium.org; h=from:subject:message-id; bh=6kvmCn7eEeTM0+R88QPjF6t0UZo7MMe7Xr8f2IbHo6E=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjmI3Nfh3FShn9Zm/bK1NkD4oeuhLal1kJLCxQ/VKk dw1x0UmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY5iNzQAKCRDRN9E+zzrEiDu6EA CblzFoF5m/MDm9R7vQsjAvJXV5xk0xBsUMXBY4L77c7L+gZundJHeVCKQf2g1O7gb4SbxpCHJ8bZQM g/OeEe9GEMoCrhNcwIywTZ/k6DkydcYpQZ1+vA/jdtLBMk3pwis7KwcNmFOdgN7/iGzq8dMREPYzsg sm+d1w3zbifczm48+Au0fPtzA6uIk2YcyTHfOW6jVM/Rq43nA1uqrxICMqYgr2cb3vTKGcPRx9DBPS chF3wg22j2krjbGG47I7U/XCt+42Q9zFiIOqHHXW+vCDz92cbwX2k3jRVLA4izf45LN1TTJUX/7zb1 ftXXqaXZlMsUxzIvhHMwFTYGUwiiidScIjZM3XlS6sKQ2H66oTwpKIrQMkCxm8F2pQmfvWSqZZNHy8 FRSxoEgoe2BDFkAw2KUV0NCvMhSu3PjpN4TklgcfOB9euq/SCMl/sr8QuauZ/43E0w/gkPr7c/z2Ei Bk67/0f2aAIjJQtKVGjHlX8Fe+wlfhEnq2qvBdPyxfiK4cHwGSiFWlSDOM5s+LtKUXDG6FVHhgA7vT DiAvIPTYTB5T5mm4dQPK5vXh2pWiIFaRYjsqe9kT0PSbblryx/IgoMz+ZHgXlNWfpThqpshOtGU3JT qH6Vf7t3g9lCUCwllEDB+UzFXJVmrccQGTcEjDw7IMpERhMdeknOd92oUYZQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752109988374414285?= X-GMAIL-MSGID: =?utf-8?q?1752109988374414285?= |
Series |
uvc: Fix race condition on uvc
|
|
Commit Message
Ricardo Ribalda
Dec. 13, 2022, 2:35 p.m. UTC
usb_kill_urb warranties that all the handlers are finished when it
returns, but does not protect against threads that might be handling
asynchronously the urb.
For UVC, the function uvc_ctrl_status_event_async() takes care of
control changes asynchronously.
If the code is executed in the following order:
CPU 0 CPU 1
===== =====
uvc_status_complete()
uvc_status_stop()
uvc_ctrl_status_event_work()
uvc_status_start() -> FAIL
Then uvc_status_start will keep failing and this error will be shown:
<4>[ 5.540139] URB 0000000000000000 submitted while active
drivers/usb/core/urb.c:378 usb_submit_urb+0x4c3/0x528
Let's improve the current situation, by not re-submiting the urb if
we are stopping the status event. Also process the queued work
(if any) during stop.
CPU 0 CPU 1
===== =====
uvc_status_complete()
uvc_status_stop()
uvc_status_start()
uvc_ctrl_status_event_work() -> FAIL
Hopefully, with the usb layer protection this should be enough to cover
all the cases.
Cc: stable@vger.kernel.org
Fixes: e5225c820c05 ("media: uvcvideo: Send a control event when a Control Change interrupt arrives")
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_ctrl.c | 3 +++
drivers/media/usb/uvc/uvc_status.c | 6 ++++++
drivers/media/usb/uvc/uvcvideo.h | 1 +
3 files changed, 10 insertions(+)
Comments
On Tue, Dec 13, 2022 at 11:36 PM Ricardo Ribalda <ribalda@chromium.org> wrote: > > usb_kill_urb warranties that all the handlers are finished when it > returns, but does not protect against threads that might be handling > asynchronously the urb. > > For UVC, the function uvc_ctrl_status_event_async() takes care of > control changes asynchronously. > > If the code is executed in the following order: > > CPU 0 CPU 1 > ===== ===== > uvc_status_complete() > uvc_status_stop() > uvc_ctrl_status_event_work() > uvc_status_start() -> FAIL > > Then uvc_status_start will keep failing and this error will be shown: > > <4>[ 5.540139] URB 0000000000000000 submitted while active > drivers/usb/core/urb.c:378 usb_submit_urb+0x4c3/0x528 > > Let's improve the current situation, by not re-submiting the urb if > we are stopping the status event. Also process the queued work > (if any) during stop. > > CPU 0 CPU 1 > ===== ===== > uvc_status_complete() > uvc_status_stop() > uvc_status_start() > uvc_ctrl_status_event_work() -> FAIL > > Hopefully, with the usb layer protection this should be enough to cover > all the cases. > > Cc: stable@vger.kernel.org > Fixes: e5225c820c05 ("media: uvcvideo: Send a control event when a Control Change interrupt arrives") > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> > --- > drivers/media/usb/uvc/uvc_ctrl.c | 3 +++ > drivers/media/usb/uvc/uvc_status.c | 6 ++++++ > drivers/media/usb/uvc/uvcvideo.h | 1 + > 3 files changed, 10 insertions(+) > > diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c > index c95a2229f4fa..5160facc8e20 100644 > --- a/drivers/media/usb/uvc/uvc_ctrl.c > +++ b/drivers/media/usb/uvc/uvc_ctrl.c > @@ -1442,6 +1442,9 @@ static void uvc_ctrl_status_event_work(struct work_struct *work) > > uvc_ctrl_status_event(w->chain, w->ctrl, w->data); > > + if (dev->flush_status) > + return; > + > /* Resubmit the URB. */ > w->urb->interval = dev->int_ep->desc.bInterval; > ret = usb_submit_urb(w->urb, GFP_KERNEL); > diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c > index 7518ffce22ed..09a5802dc974 100644 > --- a/drivers/media/usb/uvc/uvc_status.c > +++ b/drivers/media/usb/uvc/uvc_status.c > @@ -304,10 +304,16 @@ int uvc_status_start(struct uvc_device *dev, gfp_t flags) > if (dev->int_urb == NULL) > return 0; > > + dev->flush_status = false; > return usb_submit_urb(dev->int_urb, flags); > } > > void uvc_status_stop(struct uvc_device *dev) > { > + struct uvc_ctrl_work *w = &dev->async_ctrl; > + > + dev->flush_status = true; > usb_kill_urb(dev->int_urb); > + if (cancel_work_sync(&w->work)) > + uvc_ctrl_status_event(w->chain, w->ctrl, w->data); > } > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h > index df93db259312..6a9b72d6789e 100644 > --- a/drivers/media/usb/uvc/uvcvideo.h > +++ b/drivers/media/usb/uvc/uvcvideo.h > @@ -560,6 +560,7 @@ struct uvc_device { > struct usb_host_endpoint *int_ep; > struct urb *int_urb; > u8 *status; > + bool flush_status; > struct input_dev *input; > char input_phys[64]; > > > -- > 2.39.0.rc1.256.g54fd8350bd-goog-b4-0.11.0-dev-696ae Reviewed-by: Yunke Cao <yunkec@chromium.org>
diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index c95a2229f4fa..5160facc8e20 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -1442,6 +1442,9 @@ static void uvc_ctrl_status_event_work(struct work_struct *work) uvc_ctrl_status_event(w->chain, w->ctrl, w->data); + if (dev->flush_status) + return; + /* Resubmit the URB. */ w->urb->interval = dev->int_ep->desc.bInterval; ret = usb_submit_urb(w->urb, GFP_KERNEL); diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index 7518ffce22ed..09a5802dc974 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -304,10 +304,16 @@ int uvc_status_start(struct uvc_device *dev, gfp_t flags) if (dev->int_urb == NULL) return 0; + dev->flush_status = false; return usb_submit_urb(dev->int_urb, flags); } void uvc_status_stop(struct uvc_device *dev) { + struct uvc_ctrl_work *w = &dev->async_ctrl; + + dev->flush_status = true; usb_kill_urb(dev->int_urb); + if (cancel_work_sync(&w->work)) + uvc_ctrl_status_event(w->chain, w->ctrl, w->data); } diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index df93db259312..6a9b72d6789e 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -560,6 +560,7 @@ struct uvc_device { struct usb_host_endpoint *int_ep; struct urb *int_urb; u8 *status; + bool flush_status; struct input_dev *input; char input_phys[64];