Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp410515imu; Tue, 8 Jan 2019 23:12:43 -0800 (PST) X-Google-Smtp-Source: ALg8bN5HTMKiHCmm/l/TQTXufPIvMX9lGnqmOKJwn0W4eXcYtM4ahrrf5rnqvjI7ER0Bbm8nLER2 X-Received: by 2002:a17:902:5982:: with SMTP id p2mr4877535pli.39.1547017963351; Tue, 08 Jan 2019 23:12:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547017963; cv=none; d=google.com; s=arc-20160816; b=Yxcu/MZnJbmMXnDoObob7d8/jBy49fg7kSFLIpmoYkDnxPC957Oy5Xr6/1mw8IQ5iM h6Zh4eRNyjN80b4CDculO0ljO4QWMSATgHfeLjcGtqaKj0ZxI2q+CnVtGK/jRxyWTuA6 kNxPDx9myO5oDCkodt4BXpiI8GUpUrWpnhCOu6THo71PZZhuKw+Iav7nQFD/jXNBZxp0 SBV4expfAHPi9EUqxuykWp8rOh9eZHgfjN3WIB8Y51qxGVfPZZCKdDWdzMjDfEL0oHTR 3ORlTk0tRsXfCAddeviQ/g9L4tCU549EuaMFPrvbfkGJv3Az4jLbJXyOUcQRUu+CfHiA Pe1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Sg//iON9xrNUnWcZLCxew6jODQHev0jPth+F7mB7Cpk=; b=Nn3dehk8RmsQLmXekGxaFyncyJJjtwCrq9Ut6H+03syuncHyYpHH902WYAFS5ufCoK GIKf3nZejNSbytayaQgpVJe4hEsFEb3LAoR9G7K170+6bhj/ct6JPARIPvlkV9yVHpPK A6XtUEWcpS45gCfZ7rMDfCJn+auc6YmasUUmnf7hsx0O6eLNyCr590dp8YMSTtZAg9R/ RO4R1fztl3zGA9SEbaGWOg8sZ5KZ/oM22aejH3RvZ95+NxU6in/T+uBUHWi1BkfSesI1 oA/buThA98ZQMGUBjzZWQkpPz3IgWq6AtDIf0mql0t7iKOIEYN8qxjPs4QC2W0cuCvaq Rfzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b="ciSpj/5F"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a185si2552153pge.404.2019.01.08.23.12.28; Tue, 08 Jan 2019 23:12:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b="ciSpj/5F"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729983AbfAIHLA (ORCPT + 99 others); Wed, 9 Jan 2019 02:11:00 -0500 Received: from perceval.ideasonboard.com ([213.167.242.64]:50448 "EHLO perceval.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727947AbfAIHLA (ORCPT ); Wed, 9 Jan 2019 02:11:00 -0500 Received: from localhost.localdomain (unknown [96.44.9.117]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C7B7256D; Wed, 9 Jan 2019 08:10:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1547017858; bh=Y6P+KmtmRz/cEyCru4/X4VKj7dXj9sPjC8dYl9DqxA8=; h=From:To:Cc:Subject:Date:From; b=ciSpj/5FV+YfkN1a8T83nsiOutXaCaPrH9072GihxtzeXUZkKCddgfXw/aUrh4EiW KfbHCH41zk0O06I318dpZ0ZruGoZT2C6t5c1ev0gAWDbOkUv4LMWUKAPTtTnR3xMZc /DqxlrvyKq6H6EaZ3hbCaSDdQK1hS3fcCiAOkHKg= From: Paul Elder To: laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com Cc: Paul Elder , rogerq@ti.com, balbi@kernel.org, gregkh@linuxfoundation.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 0/4] usb: gadget: uvc: fix racing between uvc_function_set_alt and streamon/off Date: Wed, 9 Jan 2019 02:10:35 -0500 Message-Id: <20190109071039.27702-1-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 can happen in a few ways, such as if the userspace uvc gadget application simply doesn't issue VIDIOC_STREAMOFF. Another way is if uvc_function_set_alt with alt 0 is called after it is called with 1 but before VIDIOC_STREAMON is called; in this case, UVC_EVENT_STREAMOFF will not be queued to userspace, and therefore userspace will never call VIDIOC_STREAMOFF. To fix this, add two more uvc states: starting and stopping. The starting state is entered when uvc_function_set_alt 1 is called, and is exited in uvc_v4l2_streamon, when the state is changed to streaming. The stopping state is entered when uvc_function_set_alt 0 is called, and is exited in uvc_v4l2_streamoff, when the state is changed to connected. The status phase of the SET_INTERFACE request doesn't need to be delayed by the uvc gadget driver, so that is removed. Finally, there is another way to trigger the aforementioned BUG: start streaming and (physically) disconnect usb. To fix this, call uvcg_video_enable 0 in uvc_function_disable. Changes in v3: - add state guard to uvc_function_set_alt 1 - add documentation for newly added uvc states - reorder uvc states to more or less follow the flow diagram - add more state guards to ioctl handlers for streamon and streamoff - added interrupt-safe uvcg_video_cancel and used instead of the non-interrupt-save uvcg_video_enable 0 in uvc_function_disable Changes in v2: 1. Remove delay usb status phase Paul Elder (4): usb: gadget: uvc: synchronize streamon/off with uvc_function_set_alt usb: gadget: uvc: don't delay the status phase of non-zero SET_INTERFACE requests usb: gadget: uvc: disable stream when disconnected usb: gadget: uvc: remove unused/duplicate function prototypes from uvc.h drivers/usb/gadget/function/f_uvc.c | 23 ++++++++---- drivers/usb/gadget/function/uvc.h | 47 +++++++++++++++++++------ drivers/usb/gadget/function/uvc_v4l2.c | 28 +++++++++++---- drivers/usb/gadget/function/uvc_video.c | 13 +++++++ drivers/usb/gadget/function/uvc_video.h | 2 ++ 5 files changed, 91 insertions(+), 22 deletions(-) -- 2.20.1