From patchwork Wed Apr 18 03:18:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 10347315 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 38F1360216 for ; Wed, 18 Apr 2018 03:19:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 31ECA28469 for ; Wed, 18 Apr 2018 03:19:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 239A828470; Wed, 18 Apr 2018 03:19:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5BC6428469 for ; Wed, 18 Apr 2018 03:19:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753526AbeDRDT3 (ORCPT ); Tue, 17 Apr 2018 23:19:29 -0400 Received: from mail-cys01nam02on0081.outbound.protection.outlook.com ([104.47.37.81]:45536 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752899AbeDRDT2 (ORCPT ); Tue, 17 Apr 2018 23:19:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pitt.onmicrosoft.com; s=selector1-pitt-edu; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Phg0I//dfLUrRmaDNvTMvAgsNgZVymCYrH8/NzEDXks=; b=A41ES8epS0l4Nt3mH//Xy0MNhqo+iywpBxYf55NkSpdw+sknx9t928SdXpDNmpp+j+UmdZlF7Ao//8jB4wBqNvRvgtAZMIJeUk2jtWmp6wBnrrJ89V5nK3NpK2Hub6dN+L3OWe6wCTyqB2RJnmtWhADsuT41Iwi/0b1s98jlvdY= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=pitt.edu; Received: from garnet.hsd1.pa.comcast.net (2601:547:500:1222::543d) by CY1PR04MB1926.namprd04.prod.outlook.com (2a01:111:e400:50f1::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.675.9; Wed, 18 Apr 2018 03:19:26 +0000 From: Paul Elder To: linux-usb@vger.kernel.org Cc: Paul Elder , laurent.pinchart@ideasonboard.com Subject: [PATCH 1/4] usb: gadget: uvc: synchronize streamon/off with uvc_function_set_alt Date: Tue, 17 Apr 2018 23:18:13 -0400 Message-Id: X-Mailer: git-send-email 2.17.0 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [2601:547:500:1222::543d] X-ClientProxiedBy: BN3PR03CA0105.namprd03.prod.outlook.com (2603:10b6:400:4::23) To CY1PR04MB1926.namprd04.prod.outlook.com (2a01:111:e400:50f1::22) X-MS-PublicTrafficType: Email X-Microsoft-Antispam: UriScan:(55037057525728); BCL:0; PCL:0; RULEID:(7020095)(4652020)(8989080)(5600026)(4534165)(4627221)(201703031133081)(201702281549075)(8990040)(2017052603328)(7153060)(7193020); SRVR:CY1PR04MB1926; X-Microsoft-Exchange-Diagnostics: 1; CY1PR04MB1926; 3:QhrmPNkoA5GqpsWX9LgKdMK0WOk4xy3yDXX9eh+1Ylg6k+q/sDjzcQTRS3khZ77W6UpZkgaEIVsgUKia9gapPwRz+dgcq856o4ziFSGGm8lI2tmsoZXpnirBw1bYyssOQe8l8I4ZCAigWLxSt3lTEk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GHYQlypZtA5rKpUR4BkjQKAPymgNuTvfUQOOk/BQC/S1uJa1GPlCs1QRVeOptEzg/3uHj/I/8VOABZyi9s7y0CA5isJO5LJNm4h/X5dU6vgUOt0uzc1hVyumiHmtIPXI0pYIFcqfXTf+rtp+BPpeAiUWBaGS88ywpJzyJHU0SJVZKoUwlXsx6C/FfDq5eLmGhS9khhGwHHT0Jp4UwN1hJ1WXzHuS+FLl/PLar5vjK4Z5RHErkUk; 4:yDSyrJn2W4KgIaAi9UF0Mgk9b8YRekJcg8J3LpZGrh54n9BO1VFH036u34a01wQ/Yf8Hi29siNSsYXy0+EcfPjEPqsF+6VKZhlBLRoZf5QYDOvCrCnSiBnj6phWiVfc0c59BVo4nvfNdBz9ZmN3uemMI2ZmyvQ9ljAey+EC88IR3kvSLEMKhOEFrIJxpQDwwkGu6I1pDp5AXNtWAn1tR7vNMaMuM7MA9BZoCpWyK2XBf91H4ugzQwyAsCWqYG9KEaLvrDHU0rZbn8WQJoBifsdAVd0zlP0u+SADcL9oFYg/aUVCvQCLPg6H9SMj1/Nso X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(55037057525728); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(3002001)(93006095)(93001095)(3231232)(944501327)(52105095)(10201501046)(6041310)(201703131423095)(201702281529075)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123560045)(20161123564045)(20161123558120)(6072148)(201708071742011); SRVR:CY1PR04MB1926; BCL:0; PCL:0; RULEID:; SRVR:CY1PR04MB1926; X-Forefront-PRVS: 06469BCC91 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(376002)(39380400002)(396003)(366004)(346002)(39860400002)(52396003)(16586007)(7736002)(6486002)(476003)(75432002)(786003)(52116002)(2361001)(316002)(6116002)(6506007)(59450400001)(46003)(2616005)(305945005)(478600001)(446003)(11346002)(186003)(16526019)(86362001)(76176011)(25786009)(2906002)(88552002)(48376002)(50226002)(118296001)(4326008)(8676002)(8936002)(50466002)(81166006)(5660300001)(44832011)(36756003)(6916009)(2351001)(6512007)(6666003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR04MB1926; H:garnet.hsd1.pa.comcast.net; FPR:; SPF:None; LANG:en; MLV:sfv; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY1PR04MB1926; 23:wiY3KsY5GAeGV4sOWR1iVpl2lagNPip8ttoosAdba?= =?us-ascii?Q?1y57BnBoPtZjoJdNN1UlNC/T11IN9+lsQDAYsEoCZG74E2IiWfvyk7nABMfh?= =?us-ascii?Q?jL/UM/vNR6dPSSuX44PF1X+yRN04AXhQttEUbDXT90ZjooZdDmjwUjnvUG9G?= =?us-ascii?Q?TsMboZtGsWjqXESxUxBBU72ARMmxoYm+izP5mDo7Wf0mvb5d43V1PhYyQYgz?= =?us-ascii?Q?7ar9MhOGbvThl9SuEtNz/RGsPnRxaumpOXegS9Wf/NOWDqKCvWGX/8lp0eJ5?= =?us-ascii?Q?HQZgQk5QdXYel+otx9oKN5msSzRtRPug8Cgjj9EzKo4I0p2A1KdIPVn0Q/8e?= =?us-ascii?Q?GWgtqR9ERdL26HZjPR4kqBhQ6snBSjVJxYbu/a7ljnKkdce/Vj2ZQflAGVQo?= =?us-ascii?Q?gJHwH6EDFqAdyryD0plwHFy6BV/Fkz0nGk9pIAKBbAEzphRTCmkl3hTeq/Mt?= =?us-ascii?Q?88OUTdk/f2vKdUpF0ubyZw3lMhAD6mf2bJiAwpN131xYaNdi7BhS7j7wyHgv?= =?us-ascii?Q?9pqVzKBx9Ofv0EslHTY2L2hvm84Yhgc4GbX8o1P8C6CESOmHV3XClvF6qvAP?= =?us-ascii?Q?gMDsxjU7GH9v/tNi6a9/NQiwMD6lSe6vbyCgTlTOfNSGfyLIwYae8B+B1Syj?= =?us-ascii?Q?S68CcJg4aW/m4Mzpl9CJ77ktQL+AapWWi4y0pdxZPO7Ec9IEPKFjauiz5e58?= =?us-ascii?Q?2/lAEh4bk2QlmZT17eZvamPggZXob7HSS+LT0FsU3+CY922mnHnezuHmPkgq?= =?us-ascii?Q?ewODDk0i8MoXHlBQrTBuUSTEjfoJQAfBT6xYRUCb2I7HUcd+iMzrmThhW2xw?= =?us-ascii?Q?Ko5Nmq/GxXd41lyPavDPuEsq+8snJfeZ6xCcBgdLw2MDwyS3VBOAX5SUupU3?= =?us-ascii?Q?3rgjQttCwvDIjnkLlRJaTqs+LgFBAkah7JFQ99a44xZ8O5xsZpNLNGT4bjXc?= =?us-ascii?Q?Id0K5hs/0qprxFY7eePuAWAo231Pjh2qywvXCRTmdiVyFdr6RkWVanc+sKk+?= =?us-ascii?Q?9pJgSoB/9bsHrD7cZx2mLQbk60mHA/tKQiHJhfpqLmnAiRLuR2AUU/QFb30m?= =?us-ascii?Q?G+utY/bTEQ093EEb/AQGEVg6egz?= X-Microsoft-Antispam-Message-Info: Xz3Z5RjLEFRd2dNRHZMjw4jZQNGXKCtwq/LAenmSW0vBgnU1m977IhSp+jubYai3Exq2cC5Qt9fWIPbQNrYtkbaoN3gDQnMsUFUq0UL4xCXXmy1dMc3/4x89VxG4Luud39G0CcVg+Jddnz0K62QngX73Y5Sdm1CrkMyd4+M8tVkKP39orLMtG6HrWRpVvwwF X-Microsoft-Exchange-Diagnostics: 1; CY1PR04MB1926; 6:54qJusE9thRnTOoJnqzLYE8vkO2Prto1m+xgjYfzinNT2uDr5IFZFLA4f3YfJDSYwQa7Y0YGNWU/s18NCDFpHBEbCHYFUgibHVcmpVnq0PSM2JeuL1/zkgz5lGqvzxzl/7uER224grtwrT+EKDh9gYMQS3fvb55j2Wdw7WPqNOlcpVAw4Y3bmtA6X9R9CUgjL/CwXmXapX48fLGnU7IIsSMRopjd/nK5TWah61HLHn+i3/yCFXToZCmW0FV2tTrwIjIU2m9LeaIY7bUSN25FeuNmNVJ4gLhDMfThtwdtfgvWb3GSjgtkjie/w959ywz3cI9yxvM0Q1h+c/ehyMIrUGHr8Yc9k0XvTWqaAAeyd8RAEPEYF5RYIO4ygciHK3GmLCcVpgN4WwbEP9GjcJ0mwoBUvELvEQVjs9ig3ia3dt9vscljNv9pXuhV/KhA/oiusjiE6c7JS52Hy8Bo94aWww==; 5:oQke2/QHX0d5eP8hUJIwSkX7in0pZobUM3gQZcnl146+MA+f+TbKt3hu/vHzxkXrb37/ebXGdx9syRogXfmB4RTL9nfELqz/RAYUaqbEVu+4oWc/8t6HzTTYGi8rs2dUX3l0jD09ofzu0HeTABxOaSISZ1LB/bBsZ18mrPg/ErE=; 24:bOrN+IX6s3rGReIS7YI8EvWj7/DlCWbN4EGtaTFy2UBdl/An2GUnsj+VQWtkllXfFJHSLKR3pZGr6yYhQKIK0F5sfKPgzRw8zo6P3B6M9mk= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY1PR04MB1926; 7:tQ4Fl8SLUW9Dj4f8O9SA7RLEBbSXKua7ZqWspQ/ctJMGJby8yiy2MmFH2mWRh3DvQwTbmuAvIOsABQXwxI85j5edQ07ztUgRgfnrGJaX+ZpFQP3ag9VyQljwkytO5P3M1Y6Yf4QtFOsH9QIXP0IzwQvng5d4azhGBugv/HaGuSNeH5pAUSnsW8o8VGXt6TVhTtFCeCj8mKi1kCN87vuWFASf6OuYu4m9I2Y8VtVsOnBZ/FavbgtvgrO3bqb3Snk1 X-MS-Office365-Filtering-Correlation-Id: cbea37b9-cd6b-4d5d-0ff0-08d5a4db311c X-OriginatorOrg: pitt.edu X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2018 03:19:26.7915 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cbea37b9-cd6b-4d5d-0ff0-08d5a4db311c X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 9ef9f489-e0a0-4eeb-87cc-3a526112fd0d X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR04MB1926 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Down the call stack from the ioctl handler for VIDIOC_STREAMON, uvc_video_alloc_requests contains a BUG_ON, which in the high level, triggers when VIDIOC_STREAMON ioctl is issued without VIDIOC_STREAMOFF being issued previously. This could be triggered by uvc_function_set_alt 0 racing and winning against uvc_v4l2_streamon, or by userspace neglecting to issue the VIDIOC_STREAMOFF ioctl. To fix this, add two more uvc states: starting and stopping. Use these to prevent the racing, and to detect when VIDIOC_STREAMON is issued without previously issuing VIDIOC_STREAMOFF. Signed-off-by: Paul Elder --- drivers/usb/gadget/function/f_uvc.c | 8 ++++++-- drivers/usb/gadget/function/uvc.h | 2 ++ drivers/usb/gadget/function/uvc_v4l2.c | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c index 439eba660e95..9b63b28a1ee3 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -325,17 +325,19 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) switch (alt) { case 0: - if (uvc->state != UVC_STATE_STREAMING) + if (uvc->state != UVC_STATE_STREAMING && + uvc->state != UVC_STATE_STARTING) return 0; if (uvc->video.ep) usb_ep_disable(uvc->video.ep); + uvc->state = UVC_STATE_STOPPING; + 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; case 1: @@ -354,6 +356,8 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) return ret; usb_ep_enable(uvc->video.ep); + uvc->state = UVC_STATE_STARTING; + memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_STREAMON; v4l2_event_queue(&uvc->vdev, &v4l2_event); diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h index a64e07e61f8c..afb2eac1f337 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -131,6 +131,8 @@ enum uvc_state { UVC_STATE_DISCONNECTED, UVC_STATE_CONNECTED, UVC_STATE_STREAMING, + UVC_STATE_STARTING, + UVC_STATE_STOPPING, }; struct uvc_device { diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c index 9a9019625496..fdf02b6987c0 100644 --- a/drivers/usb/gadget/function/uvc_v4l2.c +++ b/drivers/usb/gadget/function/uvc_v4l2.c @@ -193,6 +193,9 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) struct uvc_video *video = &uvc->video; int ret; + if (uvc->state != UVC_STATE_STARTING) + return 0; + if (type != video->queue.queue.type) return -EINVAL; @@ -201,12 +204,13 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) if (ret < 0) return ret; + uvc->state = UVC_STATE_STREAMING; + /* * Complete the alternate setting selection setup phase now that * userspace is ready to provide video frames. */ uvc_function_setup_continue(uvc); - uvc->state = UVC_STATE_STREAMING; return 0; } @@ -217,11 +221,22 @@ 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; + + if (uvc->state != UVC_STATE_STOPPING) + return 0; if (type != video->queue.queue.type) return -EINVAL; - return uvcg_video_enable(video, 0); + ret = uvcg_video_enable(video, 0); + if (ret < 0) + return ret; + + uvc->state = UVC_STATE_CONNECTED; + + return 0; + } static int