From patchwork Tue Oct 25 14:34:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10838 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1045834wru; Tue, 25 Oct 2022 07:41:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Y5VYcc+/qXMUUv8Pi44jjzM0BMS8igpEKsr3ywVgHXhpoBcLtmYtoELRZnc6RUkziiUOG X-Received: by 2002:a17:906:eeca:b0:730:6880:c397 with SMTP id wu10-20020a170906eeca00b007306880c397mr32690396ejb.593.1666708869781; Tue, 25 Oct 2022 07:41:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708869; cv=none; d=google.com; s=arc-20160816; b=SRNzUJgyaTbSwAI8xLLgMehPxXhLf87exjEzlAUPsELT96ytWK8CXFJ1TihYG+oJoC sTdb7uybBhgI+6GaRCEQLKULaW/FeTYyZYD5tpS2mrqUVMtZKXL5jYQ9SA/PIbD8R0Fr f/SX3IiwkHr/57l2RvoD2v2+hB7c4jFuU1FG/2+UPzsiHYYsWOJex62/6VYDy+d0I4B5 KY6EGrzsW2yF/emSOal5f/c74iRRj7y53z77TcYAv7x/EnNCKnY6yy06AbOFTOOaCEX4 HJyTJx8SX1s+SHyqcHYmFlp8XJn6GVmj0sFU+YDx5sB9DVOOLYCM68RaEwUP1K8e41o5 nOxw== 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=EPUsq9ry6a/B8ysE7p/Gp0RizcV4HTBL3tocv0bMA9I=; b=0wQEbfSOPkKeJlYU6po4O512K6b08pKxBSHdlER99SxlnT+/w0n/H5JynPrUD6+S0u 2uvSgTcIIisxME6IQpgRu+Z8/rM5IMH4V2ksZxGO+vBLRzNrLqRgcrZoNzTyPkI4wYWh Gq6hemA1KOMRG/ueyWlzCR22tBiMWVghgw9Y+G33zlUB2w880HV5fB+2i91vsdOn+bk+ auM1tbNuGkvAbJgrs58a0pKRG/X7njz4J2BPAYDZn5QERta0OFE/ZjIikIPtEFkUASeL Nm5B2b9G4R/KltvHT8tDy4Frg/qpqOTFdQNoE60evQxHAduFkwUQkhHdgbGMX0Rvv2Fw iVjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fTdB2oj5; 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 b9-20020a50ccc9000000b004585e0f44fesi2732808edj.60.2022.10.25.07.40.39; Tue, 25 Oct 2022 07:41:09 -0700 (PDT) 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=fTdB2oj5; 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 S232433AbiJYOfW (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232179AbiJYOfO (ORCPT ); Tue, 25 Oct 2022 10:35:14 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE50789AF1 for ; Tue, 25 Oct 2022 07:35:13 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id 13so13047803ejn.3 for ; Tue, 25 Oct 2022 07:35:13 -0700 (PDT) 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=EPUsq9ry6a/B8ysE7p/Gp0RizcV4HTBL3tocv0bMA9I=; b=fTdB2oj5zXnht0oEeX2xmQrEskfYbWhGUNlF3H6haxGBao5IRjdLyR3preVA3LQeVZ 29aYtsL68obHnIarkE0OWrORdyGhwVRhNoNqhC3YnSq3HWVaiWq9VPMoEzowIiRd0n4n LY+xBjK2zsEa8RhE2TcTNt5FSnP8AxhJMYqnQ= 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=EPUsq9ry6a/B8ysE7p/Gp0RizcV4HTBL3tocv0bMA9I=; b=CxFQ+y/OXLrLx+VvJv/Ow22pilZ8FMplxPOMjwDvXqDvnzKoHkCe6UJ7VshZ1jkYe3 UnmSENY6XiDzNTl2mnUrfP9m93G35Gfq432R93VzUgLQ5jFV0TDp2mxeyuVSIaur2N+Z +mBxzDEUrt5+hlYKm8rvRL7QZkPvVcrld0qNAQrI/kucL10PvSAiIJUHPen84WR3QGm8 sq9HQzG1Y3z1KbUrr7o08u+Eu05ZqD1NXA1u8oGjt7Y59NfMhAMJfg6mHjLbWn4n0eOf Qgs5fnrnzDsea4+fyyuZ3f3BoS6lKjA8RE/xK8ZnBI/FxynVrcknn89LJnrLK/L0kcjY c9Eg== X-Gm-Message-State: ACrzQf1FWUCUFrxHRnBfnpxuZCNLbOIE9F0U4x0iV/xrkEMhwjRcTm5p mEuGkbBgmibUJwG4A0njuktNxg== X-Received: by 2002:a17:907:7203:b0:7a5:b062:2338 with SMTP id dr3-20020a170907720300b007a5b0622338mr11741623ejc.8.1666708512271; Tue, 25 Oct 2022 07:35:12 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:11 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:22 +0200 Subject: [PATCH v2 1/8] media: uvcvideo: Only create input devs if hw supports it MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-1-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1336; i=ribalda@chromium.org; h=from:subject:message-id; bh=3NadNTBF4BvX9SA2sRowXe1SUqwds9fIYTSwvXBWhlY=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QPx9RjCOkK3+R3LAQ5GMY4FjigtbgykVx6lPrt swaMdAeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0DwAKCRDRN9E+zzrEiJyPD/ 9f5qy1Bc9FRGR0NTl0gc9YOOsfmXgD5GOrksxDkgvooTyJLJwQbGlAy4UbzlXv7hGoE13ZpwVt7knm zSeQzm+to8p0nlRIBvQEyj5LGn6DXxBseUS9XoIUl9Ii/gMt9dnraRVsvjitYSNd2PHgNWmQc3gFV0 5dRMO5JZJ6DOiCdqYzoH3PiiIpZzuAwa+wQIc5uRPAV8Z9RAknxVkqD/bbpmNE7447clD6iGFkpnzT G66Q0s7Si1wtvus0CE7aGgS/WMkwQo7roJT2jQ6FdWc/41eFyYNx8byRLTcP9nD8IuC/mWIZgBLazZ pI2KG7XiJhP/6douXCtcuYKtSxfsHw11K4DtOQyRjw/PsgSLolAovRMA3/0/GuASTMD81MVJPeqtxG O2Cpde/SXeiEYYk0ySF1awU8tivB7lUl/EzQ2lCbtvQr5h7/rEgEfKeDpVJgvKMsIO+WC8dd238WAF QiLYsPHvZgrIBoIV8Gh3BOLN6zRA/W+wNXdlryR4NMcOZpQmoz0xVCoxsgXimdw1imHKop5Up0DidC 1uGuDn+jb9GsrIDaDmykie6PeZn311GDOaY/hb0SGu/ozfm2eTuGPF3bMqDeKGlqmThaKwZpQ+oOT4 gtlSk08fAtJqgGxkvqDdee5WzXnF5pExtS7xlW+xSzrH3tZt0ChSY4lXw3DA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670920194302225?= X-GMAIL-MSGID: =?utf-8?q?1747670920194302225?= Examine the stream headers to figure out if the device has a button and can be used as an input. Reviewed-by: Laurent Pinchart Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index 7518ffce22ed..cb90aff344bc 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -18,11 +18,34 @@ * Input device */ #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV + +static bool uvc_input_has_button(struct uvc_device *dev) +{ + struct uvc_streaming *stream; + + /* + * The device has button events if both bTriggerSupport and + * bTriggerUsage are one. Otherwise the camera button does not + * exist or is handled automatically by the camera without host + * driver or client application intervention. + */ + list_for_each_entry(stream, &dev->streams, list) { + if (stream->header.bTriggerSupport == 1 && + stream->header.bTriggerUsage == 1) + return true; + } + + return false; +} + static int uvc_input_init(struct uvc_device *dev) { struct input_dev *input; int ret; + if (!uvc_input_has_button(dev)) + return 0; + input = input_allocate_device(); if (input == NULL) return -ENOMEM; From patchwork Tue Oct 25 14:34:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1045804wru; Tue, 25 Oct 2022 07:41:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4e7g21JDmUFvYBuCP9R2WgyOCY0IgiAfUBF3dgqfVMdi0mFAUlS5ghj/IcQ/CXNI+HtRRr X-Received: by 2002:a17:907:7e9a:b0:796:7a21:1520 with SMTP id qb26-20020a1709077e9a00b007967a211520mr27059219ejc.236.1666708877386; Tue, 25 Oct 2022 07:41:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708877; cv=none; d=google.com; s=arc-20160816; b=XRdGLfvhBsnuoiWfsy1S3SCVFdjXbHBm2j/5QFDcgRwHp2kU7rkgXGZZCaWzswDX/i LKhFa3b0wIK/7U2hMuvInPD3jD3fsDbl2uMAu+IDQc5uh2QvPffCF1+v8vuz+YX/2YG3 XT8Qv+qP2wbAv+IEVok1SHXNdhrUpaQ6BHsLG6OAprTde8lpNud2pGjXTiAc0NfUVS4n Ovy0LE8gb3w/mMbSjIx6BhJU+vzVUd5ereQ9nQsyGLFLhZGQJ/ZilkI8evFuHo7dPwBY NntE/G/38w7/+vJb6jnrxQ+DWlviN6VCtJhDwc16ww1326p2y1Re7Lfx7K3f182xcSz3 gFNw== 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=fw978dyctQKYz24O/MzUIY5FG9WdD7MbzKHKlFXG3Qw=; b=S80/cc6RAvt1fNExvVR0Kmd1xVj3UMFoYDKfv1MB5vSapdf67vayyNR61U3R5LEzin aRZ+QpAfWWnYjhe+ySdd1WsLIZklqmMRfkxHtLhPzO4FKScUVHXS2WFefbtbAVexhIYb BKBNvUCqMTbztIeyrZoAWLiqaDw/bodTevW0xkZA9G3nOvaI9DAYD94wplIIrTXwjzta OFy/Zqve0P7/J4voQ9Z0X0somiJMxaccUXFATNarVo2Evic8kMhAvNgGkkR8QWQ/PpYh VtOaXJlvCVU0UMWbXWVhDcrpeZYIvvngukZu+x8LTT4Pww4y0iEuLNrAa/iJjzY7jgFy qnrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bmLkeE92; 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 i2-20020a170906250200b00783160b16a1si2527097ejb.397.2022.10.25.07.40.43; Tue, 25 Oct 2022 07:41:17 -0700 (PDT) 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=bmLkeE92; 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 S232330AbiJYOf1 (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232229AbiJYOfR (ORCPT ); Tue, 25 Oct 2022 10:35:17 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3CA38E457 for ; Tue, 25 Oct 2022 07:35:14 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id kt23so7466045ejc.7 for ; Tue, 25 Oct 2022 07:35:14 -0700 (PDT) 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=fw978dyctQKYz24O/MzUIY5FG9WdD7MbzKHKlFXG3Qw=; b=bmLkeE92rI1LV5eRKtnqFtVtKlsxbsg9ecVDft0DT/GIwpvbLDl01gJxig5T0jNIyd +4diSJLScqdvKuCMrCzoJ6SVfJwYuoKr3pk2VSYImmhZg3sPEgPNTlRUslJw0hjj+iVZ Bn/Frf/KpoKM7o1+hOrk1+ZKYfI3YnRw5QZ6A= 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=fw978dyctQKYz24O/MzUIY5FG9WdD7MbzKHKlFXG3Qw=; b=KEa58kaTBlLySDGjUJ1oN3E4sgwgsKmmYcb3gAOaX/HP2AURdOXeTt/QK1Hr6C6biK U13eYAm4CuCtUr9Q/nDEQ8PcGNVby5DZcRNufmRGuDFiudazP4iTkI35fKPR99WtVVDJ swV+MKaRuwfxHhMWA2XpoAbaUmxCsKu+7NTCJosjlWErI7M1gZvri9rGZ+kbXdBRVT5a lFvBiCxnGsyzVdddNoJNDVkRbPcsdbpFXfuglFmXiPxMmrbqwPUnFRAuCWrIgw+GNBQu 7nsVlSxcjcEPR5E1b+OrEoKDxeHgFFSE+lJUEGGHFn8uF67ImyApSKIpqn5eiDsp5x6/ nYQg== X-Gm-Message-State: ACrzQf2Nm9BctH5nJG0QV/x592F8q2EmeW/C+nuG2SdjAngHQyv+D8Iy bUx0d3T8yVUSVe114m8PQGfCCA== X-Received: by 2002:a17:907:c05:b0:73d:6e0a:8d22 with SMTP id ga5-20020a1709070c0500b0073d6e0a8d22mr32994041ejc.646.1666708513448; Tue, 25 Oct 2022 07:35:13 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:12 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:23 +0200 Subject: [PATCH v2 2/8] media: uvcvideo: Refactor streamon/streamoff MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-2-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1977; i=ribalda@chromium.org; h=from:subject:message-id; bh=NB6aEyZdAyQsktG3au/4ny2fc8G0li6jsr2nryh8gpk=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QR6p/N/GSdWGniClNtVwAPcJBAGTXLWJoRKpUt 0WA3K4yJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0EQAKCRDRN9E+zzrEiOeREA CB2VW7wYO9JmilaSY15eLOYmFRdBeDHrKtiMPi7vGit/vrZQEdlX1GkhsmvhtFQOYf5X1bOWzEDFHz 39J8jZqoLIVNkQF7KiyserEfeAtCFaqOYTvNCmGuCmp1tUUTqgdPkTRDlwUOM26FfSuYhaVWcGLdGW QSEZp993O0oDkpfsmCXenx6Vitop2NCT5NOKguJqTb9nCzbp5aSQcWkKvRZmlQvCjsq/gtrViy+161 /gY254FN95KzohXog9BodoFz7nmTGWnscVRdbDsBXA7+WwwkKwMQVec35X+JFHPOywWv9SYOv6ZnXE DUBu1WYi5mqLgpQ05jBw3JPliocxdZ23k4pK05ekZuHOeRlvwlrF5PIfXPmV+5AX/5V3Z+ZD5wthHu fsLk7e+9T7E6pDPpu2DemRO9LS4INVocaafzIeqPYTXors63sWqCr+4Bbkb2CE9qU1ASM+he5ObXfg rAyBA3gQpaU+Ry6/WafZRN6s3wI/xp1XBpW69C/4moCMe9hIUlo5bSFz++ISYR92oDoRscFSH5UKYi QF/Az9KgcfKi0SoR19lfI8dEuYTp+1SfyqYfcx5zZtQsU9DXcaJ+N7IKpwyv+25MIJpsTdk9pdGyJt gWsBDMvV9C7H/vtIemgsOBpTVkq8eI7JnG++g0M7TXp7869qJxxLniTlKzOw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670927911391989?= X-GMAIL-MSGID: =?utf-8?q?1747670927911391989?= Add a new variable to handle the streaming state and handle the streamoff errors, that were not handled before. Suggested-by: Laurent Pinchart Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index f4d4c33b6dfb..1389a87b8ae1 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -840,13 +840,19 @@ static int uvc_ioctl_streamon(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_streaming *stream = handle->stream; - int ret; + int ret = -EBUSY; if (!uvc_has_privileges(handle)) return -EBUSY; mutex_lock(&stream->mutex); + + if (handle->is_streaming) + goto unlock; ret = uvc_queue_streamon(&stream->queue, type); + handle->is_streaming = !ret; + +unlock: mutex_unlock(&stream->mutex); return ret; @@ -857,15 +863,22 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_streaming *stream = handle->stream; + int ret = 0; if (!uvc_has_privileges(handle)) return -EBUSY; mutex_lock(&stream->mutex); - uvc_queue_streamoff(&stream->queue, type); + + if (!handle->is_streaming) + goto unlock; + ret = uvc_queue_streamoff(&stream->queue, type); + handle->is_streaming = !!ret; + +unlock: mutex_unlock(&stream->mutex); - return 0; + return ret; } static int uvc_ioctl_enum_input(struct file *file, void *fh, diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index df93db259312..45310f55475f 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -581,6 +581,7 @@ enum uvc_handle_state { struct uvc_fh { struct v4l2_fh vfh; + bool is_streaming; struct uvc_video_chain *chain; struct uvc_streaming *stream; enum uvc_handle_state state; From patchwork Tue Oct 25 14:34:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1045835wru; Tue, 25 Oct 2022 07:41:21 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7z3j38wBZ2TrXcnnYy4+qgqXOMnYTqH1p27qwcqCmv4McqoaKs1e+C+myFAOhCcIjUMnLa X-Received: by 2002:a17:907:75fc:b0:7aa:e0b3:270 with SMTP id jz28-20020a17090775fc00b007aae0b30270mr6159140ejc.680.1666708880944; Tue, 25 Oct 2022 07:41:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708880; cv=none; d=google.com; s=arc-20160816; b=KDYnNkQTvGBAyWyA40o4r2WvHVNWe3VADf5Cd3RF/DdbKclh5MCtYTs6k8gfU0z8jK po6oUOggLXymPv1HHA+JDYgVoCZ8G7GSdT29JW9mRlLwuPDpXywW8OLPyiYliziveig4 VL986um5U06w8l+zUYn0X/x1Uijlw4rMWLVEfUkR/oT4IuodWueznVFSrgASaHyWgT2L bBfbBGO9q5Bu35KMgZjnqUhKVIJHfcCKoMHX2n44zbszi87jdRbuDzTE2rt/S5JJXk90 t2/w8fCrbwtVXLDYq5qhWWjF3HyVwNCEMTTvZ2sp7xyL96ZqaiHB888KGvGjYz/WB+55 2kTA== 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=Y3aLAqx1UmJhgGtzBHcHb9QyFZFPiOEXRjvNmq6X8fw=; b=ZYQZDhGGl0cIdHMLVYIBRJ7d9Gq3UcifoRh03qVR1MCbwK7nWDCO2G5YJkHhxexUx9 akWAq5d3XMmyysI7YewCrY6g9IC6piwHd4VkaZv3uT8m169hdBqwMqqQ5MLSKjrAXi9e H5hI1rnIjaHzzylo4BdRNITVtvo0Bh6rssXrAjs2hBo447KO4CkWPfr3GdOgrTBo34lg 82LzdU45p9YPT9+QHvKEVuh7d9GDvaFfY/ULlbvEExShyafjpa2F0s5lm+KTzSHTfaHu /dUXmULwF0iCPkEFPR81WzHve6kP9aCQ8FkhIxdwh2+sbAWKiDhvMVTNuuQnDi2r8AtU nfVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ObHKsaoM; 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 qw20-20020a1709066a1400b0078dbec2e632si3256685ejc.620.2022.10.25.07.40.47; Tue, 25 Oct 2022 07:41:20 -0700 (PDT) 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=ObHKsaoM; 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 S232201AbiJYOfe (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232261AbiJYOfS (ORCPT ); Tue, 25 Oct 2022 10:35:18 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC4D2915D7 for ; Tue, 25 Oct 2022 07:35:15 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id b2so13022584eja.6 for ; Tue, 25 Oct 2022 07:35:15 -0700 (PDT) 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=Y3aLAqx1UmJhgGtzBHcHb9QyFZFPiOEXRjvNmq6X8fw=; b=ObHKsaoM0udFhG5HUXUbGulvSvahWlGNY0ellhchoPQK1v4KiCVA6eVPiDLNZ2nPuK CG3b2IS4fmJB7S2ocHGzF0927jbnWMlc9qq66Ge78hdoFZUujctVT3FtpjUO0vWgg/sU yX5mNJ5oRM0b4FyZRoN3jwuFpUoehez/Tb0lA= 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=Y3aLAqx1UmJhgGtzBHcHb9QyFZFPiOEXRjvNmq6X8fw=; b=Ipfs4FiSaIUCKXu3yTud+5retMrobzN8sHlBrRJAVwrK3k3XaJ0EayPT4boofhmwOS w8p9rMyLwMAZtsZRmoHE4g3dvn+fcbOE7Yw+ljtW6uwNx04970HbPnHwXERwj97/LjZv ygoG1Iap1H3xuj/yMye+DPFpMrFU8Hapfp1KdgIveuTqFDMYVFKB/RZIOuFdaeCKZx7A j0vVJGajzURMhDxyqVN9ZGagaVueFykG2b9xsS9nLJ6b03sOjOzGCHOReOflW65aMBvd 3CuHBpza2OsL5JyWIkn/PB/CsB4AYsqQd+3NwE7zDzWglua56fnNeJyZ2odgNoIIhkFr u0fA== X-Gm-Message-State: ACrzQf2rPJkCzMfndoEsFitTu5OFLJg9023eQgVZZG97U2FzmG0GdGwi a5F+N1CUIGwgpvN2L37Z5pBWUgGqg2dubDim X-Received: by 2002:a17:907:1c01:b0:78d:eb6e:3807 with SMTP id nc1-20020a1709071c0100b0078deb6e3807mr32969272ejc.481.1666708514425; Tue, 25 Oct 2022 07:35:14 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:14 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:24 +0200 Subject: [PATCH v2 3/8] media: uvcvideo: Do power management granularly MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-3-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=10240; i=ribalda@chromium.org; h=from:subject:message-id; bh=9JC+xKdhqfQ07Aja2WDiXvrtLcLtkT6rTXo67NjWgLI=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QShKIPLM8A/G8CTvA7j7cOb5vcI1AhvVorXxW2 E4Iq54WJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0EgAKCRDRN9E+zzrEiFitD/ 9FAFJTqD17eH9nS+vWxZBvFp2xBILkvdAgHZdZL58nx5mIH58WWX9BmSjJ+qfeKe8kWmcpIpJVytJ6 dywz46j+exs2yhL7Q+juCQVrSfjbNruuKxUZFxXOaIg6uPJYmr6+5AvwFerodDfH4Iia3nxGpK07Vn 0FWg1P2lLbPMwhPsQtOt2FlB/UDMtX4w4ddrOTabsNDI2uR1NHkJ5I/mLpR1UcQ24oKeRF7At9ONKy wpxxZInWakaf3fOkxIA7/gOJ/Cvmop8HBBcVAyQ2ZUQy1f0XAo/J79WpHaeJCyfSmjYXrr3mvavsfZ 1050FMtCW/uMjgwfc+9lOofYJhUy0rjnFXNZEl+N9GQZzuGoE7wb6zbh6RfshtuyM8oFsNqOiDYHaD XqGVKvLnvDfnAjznrc5UoOQwDiYwoeIfrRKD6KrVqpistTtzlIg0Dikw55Alhuv5JwMoC4cn0zink0 TMi3P92aWdJJrVrnNzvi56PbF7YzAqVMRFHIwZ/z/G9gUVsIlOo1xQqkTUHPSHFDwtc/yslgoJcU3g rOZbw0nUUebpY9q5PAZzpmd9HBJdUTUp7FqXgPkmMNSqo26PouO4QCHvY29GKH/Y45mVpOEb2AGP6e 4Ds5KkuX/LsB8JHti8mW3BZquETsAqJfLJsWRlIsCLeqZvDcCMwVQBJb9dUw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670931811180249?= X-GMAIL-MSGID: =?utf-8?q?1747670931811180249?= Instead of suspending/resume the USB device at open()/close(), do it when the device is actually used. This way we can reduce the power consumption when a service is holding the video device and leaving it in an idle state. Reviewed-by: Tomasz Figa Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 1389a87b8ae1..f5b0f1905962 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -25,6 +25,46 @@ #include "uvcvideo.h" +/* ------------------------------------------------------------------------ + * UVC power management + */ + +static int uvc_pm_get(struct uvc_streaming *stream) +{ + int ret; + + ret = usb_autopm_get_interface(stream->dev->intf); + if (ret) + return ret; + + mutex_lock(&stream->dev->lock); + if (!stream->dev->users) + ret = uvc_status_start(stream->dev, GFP_KERNEL); + if (!ret) + stream->dev->users++; + mutex_unlock(&stream->dev->lock); + + if (ret) + usb_autopm_put_interface(stream->dev->intf); + + return ret; +} + +static void uvc_pm_put(struct uvc_streaming *stream) +{ + mutex_lock(&stream->dev->lock); + if (WARN_ON(!stream->dev->users)) { + mutex_unlock(&stream->dev->lock); + return; + } + stream->dev->users--; + if (!stream->dev->users) + uvc_status_stop(stream->dev); + mutex_unlock(&stream->dev->lock); + + usb_autopm_put_interface(stream->dev->intf); +} + /* ------------------------------------------------------------------------ * UVC ioctls */ @@ -249,6 +289,9 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream, * developers test their webcams with the Linux driver as well as with * the Windows driver). */ + ret = uvc_pm_get(stream); + if (ret) + return ret; mutex_lock(&stream->mutex); if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) probe->dwMaxVideoFrameSize = @@ -257,6 +300,7 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream, /* Probe the device. */ ret = uvc_probe_video(stream, probe); mutex_unlock(&stream->mutex); + uvc_pm_put(stream); if (ret < 0) return ret; @@ -468,7 +512,13 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, } /* Probe the device with the new settings. */ + ret = uvc_pm_get(stream); + if (ret) { + mutex_unlock(&stream->mutex); + return ret; + } ret = uvc_probe_video(stream, &probe); + uvc_pm_put(stream); if (ret < 0) { mutex_unlock(&stream->mutex); return ret; @@ -559,36 +609,29 @@ static int uvc_v4l2_open(struct file *file) { struct uvc_streaming *stream; struct uvc_fh *handle; - int ret = 0; stream = video_drvdata(file); uvc_dbg(stream->dev, CALLS, "%s\n", __func__); - ret = usb_autopm_get_interface(stream->dev->intf); - if (ret < 0) - return ret; - /* Create the device handle. */ handle = kzalloc(sizeof(*handle), GFP_KERNEL); - if (handle == NULL) { - usb_autopm_put_interface(stream->dev->intf); + if (!handle) return -ENOMEM; - } - mutex_lock(&stream->dev->lock); - if (stream->dev->users == 0) { - ret = uvc_status_start(stream->dev, GFP_KERNEL); - if (ret < 0) { - mutex_unlock(&stream->dev->lock); - usb_autopm_put_interface(stream->dev->intf); + /* + * If the uvc evdev exists we cannot suspend when the device + * is idle. Otherwise we will miss button actions. + */ + if (stream->dev->input) { + int ret; + + ret = uvc_pm_get(stream); + if (ret) { kfree(handle); return ret; } } - stream->dev->users++; - mutex_unlock(&stream->dev->lock); - v4l2_fh_init(&handle->vfh, &stream->vdev); v4l2_fh_add(&handle->vfh); handle->chain = stream->chain; @@ -610,6 +653,12 @@ static int uvc_v4l2_release(struct file *file) if (uvc_has_privileges(handle)) uvc_queue_release(&stream->queue); + if (handle->is_streaming) + uvc_pm_put(stream); + + if (stream->dev->input) + uvc_pm_put(stream); + /* Release the file handle. */ uvc_dismiss_privileges(handle); v4l2_fh_del(&handle->vfh); @@ -617,12 +666,6 @@ static int uvc_v4l2_release(struct file *file) kfree(handle); file->private_data = NULL; - mutex_lock(&stream->dev->lock); - if (--stream->dev->users == 0) - uvc_status_stop(stream->dev); - mutex_unlock(&stream->dev->lock); - - usb_autopm_put_interface(stream->dev->intf); return 0; } @@ -849,9 +892,17 @@ static int uvc_ioctl_streamon(struct file *file, void *fh, if (handle->is_streaming) goto unlock; + + ret = uvc_pm_get(stream); + if (ret) + goto unlock; + ret = uvc_queue_streamon(&stream->queue, type); handle->is_streaming = !ret; + if (!handle->is_streaming) + uvc_pm_put(stream); + unlock: mutex_unlock(&stream->mutex); @@ -875,6 +926,9 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh, ret = uvc_queue_streamoff(&stream->queue, type); handle->is_streaming = !!ret; + if (!handle->is_streaming) + uvc_pm_put(stream); + unlock: mutex_unlock(&stream->mutex); @@ -928,6 +982,7 @@ static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input) { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; u8 *buf; int ret; @@ -941,9 +996,16 @@ static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input) if (!buf) return -ENOMEM; + ret = uvc_pm_get(stream); + if (ret) { + kfree(buf); + return ret; + } + ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id, chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, buf, 1); + uvc_pm_put(stream); if (!ret) *input = *buf - 1; @@ -956,6 +1018,7 @@ static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input) { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; u8 *buf; int ret; @@ -977,10 +1040,17 @@ static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input) if (!buf) return -ENOMEM; + ret = uvc_pm_get(stream); + if (ret) { + kfree(buf); + return ret; + } + *buf = input + 1; ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id, chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, buf, 1); + uvc_pm_put(stream); kfree(buf); return ret; @@ -991,8 +1061,15 @@ static int uvc_ioctl_queryctrl(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; + int ret; - return uvc_query_v4l2_ctrl(chain, qc); + ret = uvc_pm_get(stream); + if (ret) + return ret; + ret = uvc_query_v4l2_ctrl(chain, qc); + uvc_pm_put(stream); + return ret; } static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, @@ -1000,10 +1077,15 @@ static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; struct v4l2_queryctrl qc = { qec->id }; int ret; + ret = uvc_pm_get(stream); + if (ret) + return ret; ret = uvc_query_v4l2_ctrl(chain, &qc); + uvc_pm_put(stream); if (ret) return ret; @@ -1049,6 +1131,7 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; struct v4l2_ext_control *ctrl = ctrls->controls; unsigned int i; int ret; @@ -1073,22 +1156,30 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, return 0; } + ret = uvc_pm_get(stream); + if (ret) + return ret; ret = uvc_ctrl_begin(chain); - if (ret < 0) + if (ret < 0) { + uvc_pm_put(stream); return ret; + } for (i = 0; i < ctrls->count; ++ctrl, ++i) { ret = uvc_ctrl_get(chain, ctrl); if (ret < 0) { uvc_ctrl_rollback(handle); ctrls->error_idx = i; - return ret; + goto done; } } ctrls->error_idx = 0; - return uvc_ctrl_rollback(handle); + ret = uvc_ctrl_rollback(handle); +done: + uvc_pm_put(stream); + return ret; } static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, @@ -1097,6 +1188,7 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, { struct v4l2_ext_control *ctrl = ctrls->controls; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; unsigned int i; int ret; @@ -1104,9 +1196,15 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, if (ret < 0) return ret; + ret = uvc_pm_get(stream); + if (ret) + return ret; + ret = uvc_ctrl_begin(chain); - if (ret < 0) + if (ret < 0) { + uvc_pm_put(stream); return ret; + } for (i = 0; i < ctrls->count; ++ctrl, ++i) { ret = uvc_ctrl_set(handle, ctrl); @@ -1114,16 +1212,20 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, uvc_ctrl_rollback(handle); ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ? ctrls->count : i; - return ret; + goto done; } } ctrls->error_idx = 0; if (ioctl == VIDIOC_S_EXT_CTRLS) - return uvc_ctrl_commit(handle, ctrls); + ret = uvc_ctrl_commit(handle, ctrls); else - return uvc_ctrl_rollback(handle); + ret = uvc_ctrl_rollback(handle); + +done: + uvc_pm_put(stream); + return ret; } static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh, @@ -1147,8 +1249,16 @@ static int uvc_ioctl_querymenu(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; + int ret; + + ret = uvc_pm_get(stream); + if (ret) + return ret; + ret = uvc_query_v4l2_menu(chain, qm); + uvc_pm_put(stream); - return uvc_query_v4l2_menu(chain, qm); + return ret; } static int uvc_ioctl_g_selection(struct file *file, void *fh, From patchwork Tue Oct 25 14:34:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10844 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1046304wru; Tue, 25 Oct 2022 07:42:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM52yBgy/51cFaKoOqBzXVpGgwxx3yuTxHzqubfjBxNtSFZXhSgwUMbq+qWQ+EslHV1F1UFy X-Received: by 2002:a17:90b:1e46:b0:20a:f9d8:1ff7 with SMTP id pi6-20020a17090b1e4600b0020af9d81ff7mr82646647pjb.34.1666708936252; Tue, 25 Oct 2022 07:42:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708936; cv=none; d=google.com; s=arc-20160816; b=cp4XPlS/peNG2TZbqh+WCVDU3MBxThduBKyUQTdMOxJ1n9i5+VAcSl/N9SM1p1KKwn e8cpPnM/4a5fMMNa70AXtqlquV4g78cu0eMh9rZm7SjHDi0HbzQ8DfhK4nNMAZf8FL2z adlAW69+biqM+e2k2hYHtqXKDI5mKChbupqv6r4e0VGLCfPFMg/1ZbuuNp9kmloZ2tDM WVY0GSPA4A1+ypmtdoAZBdR6PGK5dEUsyD/LbQqWr6PHwBG3FvMv3LWpYFy1G6rcdbkS ln2eNrDl8X7EsNbIV2VUae2PZWBr1+/dnSVo9Z6aSPDKDIpHhIPAkilnixe9TDcl1wsI MQDA== 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=KvGsjskLZRNKygMGJ72lSJUoo6AJ3rOQ6/7RzoglZg8=; b=oSuZYHPMvVUN2Xp80neoHbtANumJm+IJL6X56ZtkBmSBgcgKnb4wPufVuGcaVdGpFj CHWdiyyTj6h+HV1UkXHcb4uyWxkcXO2rZzUJwFcM1JfMbmNkakpyy3l385UlEWT++Fgw 0gFmYLI1LqPFyZ7tXcklBzswZTI4EbjQ1O5wBqjvDRjmBWNwNJNwA2hF/qSmjIj+RUOJ Bjj6hDAh+2ypLT+kVShPB5Vjk32y5hl9AlhPg0GIdhCrBXfahzb/LBSNInAWV+NFD39B x4owiE+03DHBfjKoICtRoLCjlKG7oeyJoIcN83GBbkk8sLSaY24aEBaY3cX4SulRE5MV KZNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=I4lq41ow; 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 9-20020a630f49000000b00440403ac602si3517973pgp.618.2022.10.25.07.42.01; Tue, 25 Oct 2022 07:42:16 -0700 (PDT) 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=I4lq41ow; 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 S232663AbiJYOfn (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232186AbiJYOfU (ORCPT ); Tue, 25 Oct 2022 10:35:20 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B21292F44 for ; Tue, 25 Oct 2022 07:35:17 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id bj12so12907136ejb.13 for ; Tue, 25 Oct 2022 07:35:17 -0700 (PDT) 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=KvGsjskLZRNKygMGJ72lSJUoo6AJ3rOQ6/7RzoglZg8=; b=I4lq41owIEKs6FfUYkPvTw8hJ5RUkfEADXJCABfFBjTkDUumGSSbh87hiz1PZenvuG o5KrsJCRigJshepcumssIl4Hgqt8TQ6wpenI89tYxdFsJn77X3HQiTGT3FdReqHmoPRO j0tn18aDbDI1Z45O1D6Lgxa2qOl250o9EocGY= 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=KvGsjskLZRNKygMGJ72lSJUoo6AJ3rOQ6/7RzoglZg8=; b=JPL4sDubi30SWMkZTjWyx3+OfXBaHTMvrrzODpWYqWScZzfPUn/llk061M6iFxE+im XO4QIPhjwqmkePQXfwBHi5zM9EKpU7AkUBNhMsgIvlaD1Vb4tKYgEIF8E7oFgKusGfNl z2JH5sHf+q2H0u3XFIdDnTo5dX+PL0PPIZ61DgtDcdZgqPwa3Mp6Q/zENJBvGJp3eKWZ Wr98rjXvyCXnvVFUlrA6AMfingj4ByrJSWGHsUvf4j5rBwHQU9miFlY0Keg/bOxoy3jo WBMOI5z7p3WtsX8syWsE1oackKJfCZMH1vrvhR//UxIfkE3p6cZrNH0CZAvr7/Axes4g 3tGw== X-Gm-Message-State: ACrzQf2PCENoQFIqA1oYebtheCBJvBMfUKGxa1/F3RrlEhFya4y8N/11 o/bC3QwuYY8Gc38CsFurnkMB3Q== X-Received: by 2002:a17:907:b01:b0:78d:ce3d:905d with SMTP id h1-20020a1709070b0100b0078dce3d905dmr32380992ejl.45.1666708515627; Tue, 25 Oct 2022 07:35:15 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:15 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:25 +0200 Subject: [PATCH v2 4/8] media: uvcvideo: Cancel async worker earlier MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-4-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=3850; i=ribalda@chromium.org; h=from:subject:message-id; bh=0l2KdHP21BwHh7O1kFOKoF3d4tIlj1BptCuhhZq8++g=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QVaLHI+mQrPtakacw9hVvyePqZ+MWgIM8uJidM E8/BakCJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0FQAKCRDRN9E+zzrEiECID/ 0RWgzXkrL4a09vGFcPSax2yoszoL7vBaUlZQXK7vmTI6PDTe6GnCloeadCiIS31fThH+juyEWznqvL nwwKjB8lopBC6EjDOkoBPumWZ10CBPQtgOjffooREj6kR6IlflTeJ62XafEdHvyXwfsQlu0UgdHO12 CF8Kb5RwXOpibO/wXCCTiTI9ZoFs4xPsY66Vp6SOUeMiFO3AjiCtwTgFD8NLgebhHdz+2kLsuHvHTx 1LcPxb96dXOHmLH6r/8JlzX8I16gQzej0uP5Kom+JJhUyluB56pWM/JAxrwM3Y1vDnNVP58p4d/Ttz jd7E2p/INm6kkFD8dFWozf71qTDwkq9P9Mmmi+k1x2U5B4fExdBtz7MCneVodXl2jJpIZOK3pjkOkC 2KQU2+KVQpd4vDx9VWQvsPSQIxgLKWmPfodRV9kbF8usACFYhfwFzUEV0j97LIjZWqVx/Aq95da7+O WVOknGfIBTJINPNgfMqx+pCaphuShOKdIK+CpyBINqnsCNbfl7Gc4ub1TUObOcvldp1cFMs2O1hj1l sHCmG0Ocv9tRAuDyEElUURLMgygz5wFfAzeIlOVamZuGVu7GINggogX4di/9fjKRN35ZgCmAdmjfhX 9ghrjilTPhpjnjgpStSxyUeEePvqZ2CsZFclqW9OGHsuKyTfF422+4+4PiMw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670989744034412?= X-GMAIL-MSGID: =?utf-8?q?1747670989744034412?= From: Guenter Roeck So far the asynchronous control worker was canceled only in uvc_ctrl_cleanup_device. This is much later than the call to uvc_disconnect. However, after the call to uvc_disconnect returns, there must be no more USB activity. This can result in all kinds of problems in the USB code. One observed example: URB ffff993e83d0bc00 submitted while active WARNING: CPU: 0 PID: 4046 at drivers/usb/core/urb.c:364 usb_submit_urb+0x4ba/0x55e Modules linked in: <...> CPU: 0 PID: 4046 Comm: kworker/0:35 Not tainted 4.19.139 #18 Hardware name: Google Phaser/Phaser, BIOS Google_Phaser.10952.0.0 08/09/2018 Workqueue: events uvc_ctrl_status_event_work [uvcvideo] RIP: 0010:usb_submit_urb+0x4ba/0x55e Code: <...> RSP: 0018:ffffb08d471ebde8 EFLAGS: 00010246 RAX: a6da85d923ea5d00 RBX: ffff993e71985928 RCX: 0000000000000000 RDX: ffff993f37a1de90 RSI: ffff993f37a153d0 RDI: ffff993f37a153d0 RBP: ffffb08d471ebe28 R08: 000000000000003b R09: 001424bf85822e96 R10: 0000001000000000 R11: ffffffff975a4398 R12: ffff993e83d0b000 R13: ffff993e83d0bc00 R14: 0000000000000000 R15: 00000000fffffff0 FS: 0000000000000000(0000) GS:ffff993f37a00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00000000ec9c0000 CR3: 000000025b160000 CR4: 0000000000340ef0 Call Trace: uvc_ctrl_status_event_work+0xd6/0x107 [uvcvideo] process_one_work+0x19b/0x4c5 worker_thread+0x10d/0x286 kthread+0x138/0x140 ? process_one_work+0x4c5/0x4c5 ? kthread_associate_blkcg+0xc1/0xc1 ret_from_fork+0x1f/0x40 Introduce new function uvc_ctrl_stop_device() to cancel the worker and call it from uvc_unregister_video() to solve the problem. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index c95a2229f4fa..c7af3e08b2c6 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -2597,14 +2597,17 @@ static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, } } -void uvc_ctrl_cleanup_device(struct uvc_device *dev) +void uvc_ctrl_stop_device(struct uvc_device *dev) { - struct uvc_entity *entity; - unsigned int i; - /* Can be uninitialized if we are aborting on probe error. */ if (dev->async_ctrl.work.func) cancel_work_sync(&dev->async_ctrl.work); +} + +void uvc_ctrl_cleanup_device(struct uvc_device *dev) +{ + struct uvc_entity *entity; + unsigned int i; /* Free controls and control mappings for all entities. */ list_for_each_entry(entity, &dev->entities, list) { diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 215fb483efb0..fb58f80f0b37 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1909,6 +1909,7 @@ static void uvc_unregister_video(struct uvc_device *dev) } uvc_status_unregister(dev); + uvc_ctrl_stop_device(dev); if (dev->vdev.dev) v4l2_device_unregister(&dev->vdev); diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 45310f55475f..6b07abac7034 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -739,6 +739,7 @@ int uvc_query_v4l2_menu(struct uvc_video_chain *chain, int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, const struct uvc_control_mapping *mapping); int uvc_ctrl_init_device(struct uvc_device *dev); +void uvc_ctrl_stop_device(struct uvc_device *dev); void uvc_ctrl_cleanup_device(struct uvc_device *dev); int uvc_ctrl_restore_values(struct uvc_device *dev); bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain, From patchwork Tue Oct 25 14:34:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10840 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1045866wru; Tue, 25 Oct 2022 07:41:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5+w0PYsGuABmV+XnyxeEBhueuVl8erkSF6YREJ8KO6xzjA542npi/bRgNa9Qs+kvhsogpL X-Received: by 2002:a05:6402:5024:b0:440:e4ad:f7b6 with SMTP id p36-20020a056402502400b00440e4adf7b6mr36690795eda.358.1666708885099; Tue, 25 Oct 2022 07:41:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708885; cv=none; d=google.com; s=arc-20160816; b=S/RJLlOV0PxshmoZXk/xSFbk0Z/3XqQsI75YUpSaoW34whITMP+xx2UpuT4+8xBGKr toNWpfsAmjdU8M/jNqsR3TK3AY1XE3C4mXaj6/lJdxQbWWpXqDMj5EUO1NNUkh81PcmR vrIWkMk4av85eWoPV5Rl5QJKa9hc5cqlNyF0BjuL9KsWYdB/HSwNZPLl+jDOO8HfZoDh muCQPlRkdWrk74tKmn1rR8jv1FPribAGgUEelKSJ6/5eKKbup8SGVupEoh89nfZhwQxl kmfY52eW47TG3GmSEailj1ZBsGsYA5+g81hNK2LKuY9ipWbwoz8BmSsD3LEcK966fsgU 38/g== 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=C1brNmOeIqvLHZriUymMjmWBOUrEuqumqBXcSDvQDmk=; b=o4gdAbQuJQ8jLVqKy4taoD3FCaJ3OpCsRUwOMfMT0M1bXPSA5SwjX8PoIPWjNZ/PbB AZW614ocNxDvZwCaCfsRyNJaKiD8uLTqzQ5TwMA951z/HMKIhmuIpCf3iiIu+DrpQKOS 67K/HqxWAWGiuuSmhmfiGJZI9hYNLtf6u2CAsWRG0REymDTGYm2lZHbZs3rS6eXpdmVl xCYnTZuGzLHpqB6SuJD7rNCzCV3bPfZFDrgGcX97kzD30ubRcSI9WI65xlxB7mBffa+J Sm+d/Zq4pOURTbG2CkSmZC6VxblGl9GoW9Jafk+EVUTdJqOZEZvwzVmaml0mwZNKLbaw QYCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kk8ztiwJ; 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 a29-20020a50c31d000000b0046207099f6bsi1153227edb.613.2022.10.25.07.40.59; Tue, 25 Oct 2022 07:41:25 -0700 (PDT) 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=kk8ztiwJ; 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 S232442AbiJYOfh (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232242AbiJYOfU (ORCPT ); Tue, 25 Oct 2022 10:35:20 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B3AB89959 for ; Tue, 25 Oct 2022 07:35:18 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id t25so7498664ejb.8 for ; Tue, 25 Oct 2022 07:35:18 -0700 (PDT) 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=C1brNmOeIqvLHZriUymMjmWBOUrEuqumqBXcSDvQDmk=; b=kk8ztiwJk/VrU1CnULvrrSl1rCZFGsUTjAEEuzfqylHAa5owbW3dVON4IAjb8Pdm31 yEreP1X4dIxQmO6rzP5130rr1eLwznp2BBG6IXeq9nkwqsn4QpX8ACnyFcm7MeIoBXFX DbQhtCVHwggkGWFOkZKvqCLgbVPUifol2GQL0= 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=C1brNmOeIqvLHZriUymMjmWBOUrEuqumqBXcSDvQDmk=; b=FBMgPRtA2WB5gIjBLN1aIwGbc9v+rgx3AU6Owa20oluSdSdh+wkJIBNDkDGXhzpEO1 TDpa8uzkhltoCE3hFRCj3qWjuKVGJsj8qbU9dX3ZJHsq++d8Znx6qyVbARYL0EqLHp7G pAAF6XkvFDzMppwN+V+EbH7x2B0j7YPpdsRdtvEX8B+6PtzAwGT7LXyWqlRYksC64Q1C 5slJwAUa1BKSsEJCj/cWdLUg43aDS6VpJgG+6rdwrSBcDhXcQ6bS4K9UmQDRiRjZ+30G dmqjkO7nUo32bJq/rOT8t8ekvPQ7nx2p2vOIBRTTIcR6HfUXYvKsLmqzOVyTw5vImi7q tZJw== X-Gm-Message-State: ACrzQf0y7AKAcx0D0g05XDKXTMmA7J6rjJarSTjrvw0QNi5e7mbQn2fn oWVTZe6gMezOgNcGBNTajfRgPw== X-Received: by 2002:a17:906:7945:b0:73b:e605:f31 with SMTP id l5-20020a170906794500b0073be6050f31mr32990283ejo.129.1666708516872; Tue, 25 Oct 2022 07:35:16 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:16 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:26 +0200 Subject: [PATCH v2 5/8] media: uvcvideo: Lock video streams and queues while unregistering MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-5-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=4775; i=ribalda@chromium.org; h=from:subject:message-id; bh=JccDHJlMGn0ERZrldEIMeE1yCBRex4FL0yGZe55zmQk=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QX1OMDpV++F5pP93hJJb0oIw/vYJZddRC/Z+bx i1JmOr6JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0FwAKCRDRN9E+zzrEiBcVD/ 9tJaK168vSuc8YLN6Gdlp+81+REVjfDUgo3Y9xFQ1FWA39QM8iXTLnrpDs/QvjoWWSAVBu26P1hAZu gHtvLg5PbLy9C9V85/Zltg82NdtKHLPwMqZnun4DpizBarJe3AL3oi85mUaR0fmwoGau2WRpU26JYE yZFcLKLbeMfkoq4lmrJxuWry08d29JrMbPjCljOWZ0nAWgbz1Fi1xQ8i4+eIxAnmqQr6UXx9yvJuLI DpttCA9Sol9gHubHcqiMoqAlIz2M6ednqMzD3dbUITTViQ3W7LtoY+RQLwKEcShdmwUn7EFbK4hbn+ QQewXQiel09f+6llZC4XOkbvjgHQEpfH7DPIIq95He3QDK33tERAYvqib+wGvfEkPdelY9hLqEwcyJ 1kx7hgRK2NhnxfszSGvs4NgMyQT7ekk1bnwJ5axFvLpQsysyFGLK+/fDh8PEC4L4SuLxoOccC7I+rj oXVAC4zfEsjee/T/ljNX3YAjzZmoNxUfjL0+06my4aZGSXyE2r2H0Dn3DuOIpwcxBVIdzaCfv0WB/B fUrdm0/FbmjPYbthwLqljD/5DPnxNgrlV0FyDmT2F/XgYHD0gS875Kxd/AsvHqahSnvYnohLkv1Eh4 v8o4WsVZj3dD6O9Baa6RqBj6iHuOiBnVeSJXEYXDFNHRWzW/0yeafyyzkyUw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670936151694035?= X-GMAIL-MSGID: =?utf-8?q?1747670936151694035?= From: Guenter Roeck The call to uvc_disconnect() is not protected by any mutex. This means it can and will be called while other accesses to the video device are in progress. This can cause all kinds of race conditions, including crashes such as the following. usb 1-4: USB disconnect, device number 3 BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP PTI CPU: 0 PID: 5633 Comm: V4L2CaptureThre Not tainted 4.19.113-08536-g5d29ca36db06 #1 Hardware name: GOOGLE Edgar, BIOS Google_Edgar.7287.167.156 03/25/2019 RIP: 0010:usb_ifnum_to_if+0x29/0x40 Code: <...> RSP: 0018:ffffa46f42a47a80 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 0000000000000000 RCX: ffff904a396c9000 RDX: ffff904a39641320 RSI: 0000000000000001 RDI: 0000000000000000 RBP: ffffa46f42a47a80 R08: 0000000000000002 R09: 0000000000000000 R10: 0000000000009975 R11: 0000000000000009 R12: 0000000000000000 R13: ffff904a396b3800 R14: ffff904a39e88000 R15: 0000000000000000 FS: 00007f396448e700(0000) GS:ffff904a3ba00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000000 CR3: 000000016cb46000 CR4: 00000000001006f0 Call Trace: usb_hcd_alloc_bandwidth+0x1ee/0x30f usb_set_interface+0x1a3/0x2b7 uvc_video_start_transfer+0x29b/0x4b8 [uvcvideo] uvc_video_start_streaming+0x91/0xdd [uvcvideo] uvc_start_streaming+0x28/0x5d [uvcvideo] vb2_start_streaming+0x61/0x143 [videobuf2_common] vb2_core_streamon+0xf7/0x10f [videobuf2_common] uvc_queue_streamon+0x2e/0x41 [uvcvideo] uvc_ioctl_streamon+0x42/0x5c [uvcvideo] __video_do_ioctl+0x33d/0x42a video_usercopy+0x34e/0x5ff ? video_ioctl2+0x16/0x16 v4l2_ioctl+0x46/0x53 do_vfs_ioctl+0x50a/0x76f ksys_ioctl+0x58/0x83 __x64_sys_ioctl+0x1a/0x1e do_syscall_64+0x54/0xde usb_set_interface() should not be called after the USB device has been unregistered. However, in the above case the disconnect happened after v4l2_ioctl() was called, but before the call to usb_ifnum_to_if(). Acquire various mutexes in uvc_unregister_video() to fix the majority (maybe all) of the observed race conditions. The uvc_device lock prevents races against suspend and resume calls and the poll function. The uvc_streaming lock prevents races against stream related functions; for the most part, those are ioctls. This lock also requires other functions using this lock to check if a video device is still registered after acquiring it. For example, it was observed that the video device was already unregistered by the time the stream lock was acquired in uvc_ioctl_streamon(). The uvc_queue lock prevents races against queue functions, Most of those are already protected by the uvc_streaming lock, but some are called directly. This is done as added protection; an actual race was not (yet) observed. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index fb58f80f0b37..14b66019208d 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1898,14 +1898,22 @@ static void uvc_unregister_video(struct uvc_device *dev) { struct uvc_streaming *stream; + mutex_lock(&dev->lock); + list_for_each_entry(stream, &dev->streams, list) { if (!video_is_registered(&stream->vdev)) continue; + mutex_lock(&stream->mutex); + mutex_lock(&stream->queue.mutex); + video_unregister_device(&stream->vdev); video_unregister_device(&stream->meta.vdev); uvc_debugfs_cleanup_stream(stream); + + mutex_unlock(&stream->queue.mutex); + mutex_unlock(&stream->mutex); } uvc_status_unregister(dev); @@ -1917,6 +1925,8 @@ static void uvc_unregister_video(struct uvc_device *dev) if (media_devnode_is_registered(dev->mdev.devnode)) media_device_unregister(&dev->mdev); #endif + + mutex_unlock(&dev->lock); } int uvc_register_video_device(struct uvc_device *dev, diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index f5b0f1905962..c250b628fc4f 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -38,10 +38,18 @@ static int uvc_pm_get(struct uvc_streaming *stream) return ret; mutex_lock(&stream->dev->lock); + + if (!video_is_registered(&stream->vdev)) { + ret = -ENODEV; + goto done; + } + if (!stream->dev->users) ret = uvc_status_start(stream->dev, GFP_KERNEL); if (!ret) stream->dev->users++; + +done: mutex_unlock(&stream->dev->lock); if (ret) From patchwork Tue Oct 25 14:34:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1046161wru; Tue, 25 Oct 2022 07:41:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5K4sKzxGZMhk80vjdOWV4oCYJTXrqTc/ki3CEqE0ptbN11TZrH54HXHPMIfrRMVRbt2dMl X-Received: by 2002:a17:907:6e09:b0:78d:a326:49c6 with SMTP id sd9-20020a1709076e0900b0078da32649c6mr32316806ejc.507.1666708919675; Tue, 25 Oct 2022 07:41:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708919; cv=none; d=google.com; s=arc-20160816; b=vu6iibbaSlYckTKhyeTCO3yFRehBXoYuxnjiDR4kAB660r2kp/vzOqSZTI0GAtnLe9 /yDgJBcGcD55uCFf/s9o8qCkPX5qXEoptWBewappVzNhtPaW2jKIKr5FCMlMIFOXvaQ2 Ur7bKFCUZrNNDOT61BICmAPB8/Hi6YAGwX52TUjOh2Bq2xrsLXlBVQzUPuLuIwjiXBsK Itz/jxtOz2XwE0GUrS+v+mLrF0UXhIcD5a5tkFmYtgZSMk8qfDn7n//o7k+o1ict19Of WgZHboBIRoLxFCXM0pNKZhbtrixoUOIwYfiHZdpFOibJBUc9KYqZ9hJzhPe603UEtcDX Bipw== 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=CBQi3G3Ym31IbBZKsntX/6j7yLQdhJIuZJ9n/O1WbSA=; b=Zji7kGQ/eXwW7dlmpgvfyLGFx8YZ+O5L/q21zpRA97JXqeqBgu/P/hZKz6P0Rkuymf 4LYFSQrwdBUu75IjWiu8jdh4DnzzgRKeldImxBfWW5qXEVeEIeEwino5UTMbn2rPkDXh Tw98egNxAfUamQKY5yXhx7zTbeuRXL/aKmxHRnUtb4zd9Mu4uxUtwkmRMxkU83uSyDFE GIN6PoeI0WiyUBixvyRN8r6s/Wp/I54OTv7fiGtA6HXw2jTXB3ugn9Wl90wNfmWxXZzw O8MGONuxQJFdyO0uoFovL37o2WjgZ7JGmyG7ptnygu8g/FbKGBlfNCJXLrh7BXPRGv3H MUdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Ijtjnait; 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 qo14-20020a170907874e00b0078da83673a3si2955818ejc.138.2022.10.25.07.41.35; Tue, 25 Oct 2022 07:41:59 -0700 (PDT) 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=Ijtjnait; 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 S232922AbiJYOfu (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232307AbiJYOfV (ORCPT ); Tue, 25 Oct 2022 10:35:21 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97BF19623F for ; Tue, 25 Oct 2022 07:35:19 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id b2so13023034eja.6 for ; Tue, 25 Oct 2022 07:35:19 -0700 (PDT) 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=CBQi3G3Ym31IbBZKsntX/6j7yLQdhJIuZJ9n/O1WbSA=; b=IjtjnaitqL4YWVUyckfhshIgXOqDSQJwVkL3vu2mQ4xLMHxL/dvldbBzC935krRQ11 ThoQfe/9ZmVJR9xGdVe49xD+ISZZz6kRFkaVl8VD77hG5C25AaD7EiilpJzTriHn38TK h8Dpip8/8XY32l896EkV72KqhvKco/+1rFN8g= 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=CBQi3G3Ym31IbBZKsntX/6j7yLQdhJIuZJ9n/O1WbSA=; b=WSniylXGlHcrcgjN+ZUF9YrRJKZcfnnRkGNOtV8FOYG0/co3T9b2S3WGcIhI3j3ygO QCp+BVFbF1Crq9y2BDjWgOxodRCnlgE+Qu+Rhae6ayshPfgmILVAI6JEHSD+mzLZgyBE IuDyrnT7861c/TH+M6rOfZ09uLvbz/0svvbhsOX5nFaq9Wp+GFIQuMzI43C8dI9tl9tT XUh+ZxeGCAb8zQdvrPM3vbsUGVsxn2VpIOjpDxGPzy2k3OhiZxqoIC47LGHuIsZOBNAs NebUV6u6h/KH7rZs7kfLfwmFCuRMae4WFw004n7M3AIeMJUyB2Z0v7ryP0+GQj8xTXM6 B5Og== X-Gm-Message-State: ACrzQf2GKdH3cg7vO7esCfRr7BvJCbyAGIOKdQKSx3smzi5p6KjpvF3g T6DBq0YNN3acLP0SdBI4gIk4wA== X-Received: by 2002:a17:907:2672:b0:780:8bb5:25a3 with SMTP id ci18-20020a170907267200b007808bb525a3mr32505579ejc.281.1666708517878; Tue, 25 Oct 2022 07:35:17 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:17 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:27 +0200 Subject: [PATCH v2 6/8] media: uvcvideo: Release stream queue when unregistering video device MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-6-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=3485; i=ribalda@chromium.org; h=from:subject:message-id; bh=lnR79aPbgxGWW1+CW9B6Zk1T+xgYfHfisqEZO2U/Www=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QYDKMQuekATrtARrL80BycwWYzTmtrt+zsj9AD Uhw4JSiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0GAAKCRDRN9E+zzrEiI6QD/ 0YKUZQY1+1WWOIAxk70HDqqWeaHrf/XJIMpizWgNm50/uF6FBK/Fj8hYdn+TwVNqn9lL7mYKzRxPag wlWYgRgL/E2K9QuwwdkFTsC2OLArFbm3KVaH1mCIotDg9O/TLJh2f3o8rHEXFg8GWVhkkMHMoeaQOK +UnpMs8fVCfDv9XOmOqP8T+tsHV7H21K5D5+6dA6ExzDQqFcU2X7fWkBwGgCA98YIOxTrlN4Ud5XWK nCvGc2669t2c+J1u8ZON1MCRrPb3ihRJ3qWIFLCppFhcCV8eseRKkzAUIBIjUB1AtckDWG0mMPW4RH Xd7iLVpcy95Leh1PlxfCDwntWwk4BX44btHUDu+MBaIxQQtQhnesSrLD9IJfQDat8gYfj1mYUaCL3F u2tMmFZtLHfr0XwXa15Ir4oyyPwDDPGeXpIdAAa9PddzaHuio1DgSM/pLugytcvKIKwFcwvDYCHPvm 2lmLqG1fr3duw+kxwbnjsiqMm7B1+0OqgY8CD2NL2k2MonWEsOQFjBYJxPzhQa9IPb/ldhUqERMcwK YYO3w/vvBv3m5NaIBD/6MGPu7BXM/7qqu2ETJ5L2yWawxlxbQ7VdeQUwteDiVf7bByiS7xijIb+TKh jdweYJRY/fQklPWZZBwnEm5a367xU50ftQ6S/BYPFmav6XIg1p6aBMDxJx7Q== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670972183408465?= X-GMAIL-MSGID: =?utf-8?q?1747670972183408465?= From: Guenter Roeck The following traceback was observed when stress testing the uvcvideo driver. config->interface[0] is NULL WARNING: CPU: 0 PID: 2757 at drivers/usb/core/usb.c:285 usb_ifnum_to_if+0x81/0x85 ^^^ added log message and WARN() to prevent crash Modules linked in: <...> CPU: 0 PID: 2757 Comm: inotify_reader Not tainted 4.19.139 #20 Hardware name: Google Phaser/Phaser, BIOS Google_Phaser.10952.0.0 08/09/2018 RIP: 0010:usb_ifnum_to_if+0x81/0x85 Code: <...> RSP: 0018:ffff9ee20141fa58 EFLAGS: 00010246 RAX: 438a0e5a525f1800 RBX: 0000000000000000 RCX: 0000000000000000 RDX: ffff975477a1de90 RSI: ffff975477a153d0 RDI: ffff975477a153d0 RBP: ffff9ee20141fa70 R08: 000000000000002c R09: 002daec189138d78 R10: 0000001000000000 R11: ffffffffa7da42e6 R12: ffff975459594800 R13: 0000000000000001 R14: 0000000000000001 R15: ffff975465376400 FS: 00007ddebffd6700(0000) GS:ffff975477a00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000012c83000 CR3: 00000001bbaf8000 CR4: 0000000000340ef0 Call Trace: usb_set_interface+0x3b/0x2f3 uvc_video_stop_streaming+0x38/0x81 [uvcvideo] uvc_stop_streaming+0x21/0x49 [uvcvideo] __vb2_queue_cancel+0x33/0x249 [videobuf2_common] vb2_core_queue_release+0x1c/0x45 [videobuf2_common] uvc_queue_release+0x26/0x32 [uvcvideo] uvc_v4l2_release+0x41/0xdd [uvcvideo] v4l2_release+0x99/0xed __fput+0xf0/0x1ea task_work_run+0x7f/0xa7 do_exit+0x1d1/0x6eb do_group_exit+0x9d/0xac get_signal+0x135/0x482 do_signal+0x4a/0x63c exit_to_usermode_loop+0x86/0xa5 do_syscall_64+0x171/0x269 ? __do_page_fault+0x272/0x474 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7ddec156dc26 Code: Bad RIP value. RSP: 002b:00007ddebffd5c10 EFLAGS: 00000293 ORIG_RAX: 0000000000000017 RAX: fffffffffffffdfe RBX: 000000000000000a RCX: 00007ddec156dc26 RDX: 0000000000000000 RSI: 00007ddebffd5d28 RDI: 000000000000000a RBP: 00007ddebffd5c50 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000293 R12: 00007ddebffd5d28 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 When this was observed, a USB disconnect was in progress, uvc_disconnect() had returned, but usb_disable_device() was still running. Drivers are not supposed to call any USB functions after the driver disconnect function has been called. The uvcvideo driver does not follow that convention and tries to write to the disconnected USB interface anyway. This results in a variety of race conditions. To solve this specific problem, release the uvc queue from uvc_unregister_video() after the associated video devices have been unregistered. Since the function already holds the uvc queue mutex, bypass uvc_queue_release() and call vb2_queue_release() directly. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 14b66019208d..f15fbdbcd8bb 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1912,6 +1912,8 @@ static void uvc_unregister_video(struct uvc_device *dev) uvc_debugfs_cleanup_stream(stream); + vb2_queue_release(&stream->queue.queue); + mutex_unlock(&stream->queue.mutex); mutex_unlock(&stream->mutex); } From patchwork Tue Oct 25 14:34:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1046183wru; Tue, 25 Oct 2022 07:42:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5+P4AVV3BsYCf6+zcls/pleIt4gtu5bwqxQ9NBqic7w1zR7Mv+MgUPJLeA+PKpxQaConqM X-Received: by 2002:a05:6402:448b:b0:43b:5ec6:8863 with SMTP id er11-20020a056402448b00b0043b5ec68863mr35713412edb.377.1666708922078; Tue, 25 Oct 2022 07:42:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708922; cv=none; d=google.com; s=arc-20160816; b=W1O842d5rLZBYOCKMyxQmIvhwnkfHS6r3Z+lLegxDZGUhya0RTG+X43tZU5rMu6AjT RKNiDKCmnCcUTFtfU45StOtgflo9ox+53NRXBIJcosSuUukvSdhsHfba7rxz756GTheA k23LWBYPA/HXeaBPz3qNTnWE/AHx2WYOzS+Pt0V2x8Yw6gJMG3S39EKf8m6mXtUsTS/m 2c9Bq1nrPYSJ9d6eLEg3b78HoF43lrHj58oNFubNX/i+Edp5dZQhZaKQqOHFZbOZtjQ1 NG6Vupz3JFDbX2cwZrrKPGQi5oYZp2BltBmFKUR3cLy3dUO2gWJUQmrwidCVgDlrKRAV b2lw== 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=mMLlFka1I97WBpNYC6a4inYcni8WZqVzi63jLlkZWi4=; b=VZHcPbL4R1yG24f+pUNnbD2V3yHSEY2dVZkVT/wyIjnDmw0spSz72CJ5GHVgAs2OvF pdK4ezYPlOz5IO9eFm/TVC/E2sYK2Kn4M9nwCdBBBrHqQpyL2FA6TMtVnfvv9DVY3w9l bSM9NqdSPOlpNk9A0HR59H2jfb6t1yribMu5noEsRP0CWuLV5ktGvUGXaLsds48rggVl RDIQSUGvsVspYV3FfnkNIFfTsK1H3JVYC6e4FySMXRE6eA5WBBqmRvd8410/qxHpH9fj vKEOwNpDLBGP88ztrMwJA6u8s70d3ztLlp7zE4E69eHUA+TYIkqva3vbLz9cjM806vl8 xYRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Qfx1Bj1J; 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 gs19-20020a1709072d1300b0078e1216fb07si3114588ejc.558.2022.10.25.07.41.33; Tue, 25 Oct 2022 07:42:02 -0700 (PDT) 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=Qfx1Bj1J; 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 S232798AbiJYOfq (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232440AbiJYOfX (ORCPT ); Tue, 25 Oct 2022 10:35:23 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2933497EED for ; Tue, 25 Oct 2022 07:35:20 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id m15so36488987edb.13 for ; Tue, 25 Oct 2022 07:35:20 -0700 (PDT) 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=mMLlFka1I97WBpNYC6a4inYcni8WZqVzi63jLlkZWi4=; b=Qfx1Bj1JK4e65/6R6MgGPuZBNlmRrurek97IQhmwS7ceqdGvFOmGBXWflS5Vd3NqdV 6iHkNVHzo3C12kEjve4QbVskgamxt0uKBkixiQ3UM/8EOccBLC71OkDBWpxiRqYdOBH5 mx9SSGbDC2WSkZFqEc+VOYvHzPLJ0OrYbE19g= 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=mMLlFka1I97WBpNYC6a4inYcni8WZqVzi63jLlkZWi4=; b=wwzjXCrAngBBbatyBCI8YRHXWw3loeiFUzP1DuCj9nNNGw3yWn2quYxOCi9D3EdMo5 yHWs28wu2WLHCe3+fdLwB0Edk8HdpM8WbyFzeuE0LAbPTY/MMQtqPeLf3snP85ljFgmA dCn9rmK1vz6YXUZoUX1Jjx/DYhVMzGP5w9xTQW770iawZ93WFMSjNIe1AjJA98PyxDrs lWb+W1lRIc1HL0mOo2J8AlyfXUviF+ljxiUK/ZhugHoxLMSc6YZQ3ZENklmvBCvOySQa yL6TydvgX65A//4EDeVoGWXOceoAdjfiI7nAjOj+OdBtmMifnV793cWr0EoJS3IvtUPp WkGA== X-Gm-Message-State: ACrzQf3WM7sKvquGBR62L8A/ZCkyzRbzpMQHLFHyXjJSIO7YJRJOdqir HrcUC7Bzw+/AtQaUPgUd26/pRg== X-Received: by 2002:aa7:c504:0:b0:461:122b:882b with SMTP id o4-20020aa7c504000000b00461122b882bmr25086551edq.14.1666708519387; Tue, 25 Oct 2022 07:35:19 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:18 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:28 +0200 Subject: [PATCH v2 7/8] media: uvcvideo: Protect uvc queue file operations against disconnect MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-7-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=2373; i=ribalda@chromium.org; h=from:subject:message-id; bh=VNSZpEP9LE78JkEZDvuh4x3SXN68AOH2OfrHeEklewU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QaTdNr8W8xCBJiosDRFO8HU1dfVbh3902iej3P m8gzCRmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0GgAKCRDRN9E+zzrEiBnhD/ 9wmBIBsC055I344aXmhM2aHVv1TK7xU8KToLtryYZk8LQNH9y8Ig9+QVK+r4QJg4ospiMPZD1SOawX VUnsDnvOceokhNWnFuKYgUecVqEOlqR8hw7dAbgnyVD1bwj4a5sehLIb3zPikjifZQfMK2dhCNJaYM e5SpvDDj0ovqFzeAwm8Zu93D+SBeW/XQ+smypqV5/Yp2mAf19HpqWLc9BibgpyJvxsMrAEm39EkPg1 BSStVdabcgeVmokFb7QCjTcKgAPCXxICwDljQcdTR3cUt3Vw6N0j1iIenekVpOgh2f/F9N1T6TF6wR GVf+cLl4t6MnSAGEXL8xWpqx/zZZ+CfO3B0ofoLxFvdhiGa62Igbi2uRGnpzlZdoYmg9vINVdXu1EQ neRRkAB0kBEdWswz5RDsfNVsq+OoXGfqfmOFLTVIzZbf+oPDGQPJWmH30b/TEZeruDwMu6LY9/Izhp JTiNsIf7/m4wFbBFXgYs3dy5trr4OpkvQfdgXcbgn/L/c5Uc0GqH4Z1nHko560Uduxpr2z8xXUOWfo G4yT1y48Ct0+i4CXNaN5aBgMg//PdRCdCSnhrvZBly9WPcsGrZZCtRl7skmukpShQbdCewb1KkvYdt h3FS7QlmqZ0RuR8pFcjWvmgk1nIFnfgUm0p6wsRhwxCimvmyV5+2hXDo9ndg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670974531613125?= X-GMAIL-MSGID: =?utf-8?q?1747670974531613125?= From: Guenter Roeck uvc queue file operations have no mutex protection against USB disconnect. This is questionable at least for the poll operation, which has to wait for the uvc queue mutex. By the time that mutex has been acquired, is it possible that the video device has been unregistered. Protect all file operations by using the queue mutex to avoid possible race conditions. After acquiring the mutex, check if the video device is still registered, and bail out if not. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_queue.c b/drivers/media/usb/uvc/uvc_queue.c index 16fa17bbd15e..aed45cbc814e 100644 --- a/drivers/media/usb/uvc/uvc_queue.c +++ b/drivers/media/usb/uvc/uvc_queue.c @@ -354,24 +354,52 @@ int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type) int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) { - return vb2_mmap(&queue->queue, vma); + struct uvc_streaming *stream = uvc_queue_to_stream(queue); + int ret; + + mutex_lock(&queue->mutex); + if (!video_is_registered(&stream->vdev)) { + ret = -ENODEV; + goto unlock; + } + ret = vb2_mmap(&queue->queue, vma); +unlock: + mutex_unlock(&queue->mutex); + return ret; } #ifndef CONFIG_MMU unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, unsigned long pgoff) { - return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); + struct uvc_streaming *stream = uvc_queue_to_stream(queue); + unsigned long ret; + + mutex_lock(&queue->mutex); + if (!video_is_registered(&stream->vdev)) { + ret = -ENODEV; + goto unlock; + } + ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); +unlock: + mutex_unlock(&queue->mutex); + return ret; } #endif __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, poll_table *wait) { + struct uvc_streaming *stream = uvc_queue_to_stream(queue); __poll_t ret; mutex_lock(&queue->mutex); + if (!video_is_registered(&stream->vdev)) { + ret = EPOLLERR; + goto unlock; + } ret = vb2_poll(&queue->queue, file, wait); +unlock: mutex_unlock(&queue->mutex); return ret; From patchwork Tue Oct 25 14:34:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 10843 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1046204wru; Tue, 25 Oct 2022 07:42:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4cMeuZ0RIIJDEsq2NR3wnK/x/JLBQGHmQi5p3L9a2A/k+2VgRL9LKdWcCUQpWVaZ3X9fXA X-Received: by 2002:a17:907:3205:b0:770:8554:c4ee with SMTP id xg5-20020a170907320500b007708554c4eemr33312207ejb.39.1666708924601; Tue, 25 Oct 2022 07:42:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666708924; cv=none; d=google.com; s=arc-20160816; b=I4A8LmjRsN7gXw0PqLfNOsurV2xoJiO22DcwOpA07zv6jK4RM/vMKUaezx/vfqkxkB lRZMEzh6ajwKhgPnzqkTp+Z3kMiyp8LKTe91ylPxw5HSlnzA6i6z4apS8NeMeyYRxS1g wbVOJifq7Hq0SNDtY2Rfqz+kTYz7B9fyXEgkg8oOuUqVTLbsM5Lj334KR3l7JKuh3NXL BD9c29DYFPb7l38dSbRVvQhFIHc1tORuQdNHCcMoSLSVKq7aeGXjyeG/ziekqbhUNb3U Ss4XUYm5cF3/7MylyIYLeDAnHOE+wJ4w8mmeEqZCeEuBk9hO89wn97tPdgNMIc71+1dK hEOQ== 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=cDM4r82Vu25XAn8FTag9RA1fMYhJwpZR6aZfJo/nKnA=; b=BWomTNu01v1b9X7UDsx5gjei+xviBF37ppiyrYCg7K+yW+Xwexe101FBP28fJVbmy8 B2l5nxHJBz1WstWIo7G9N4F2DsfliRbbtFsi/4+Vk7efg7E5t7e7yxrvD79/8g1hPSwJ MKBORobIthwKngIJt3AL7Agdlv6l02LuU1ZnxmhAinNDEMVTuGq07ZlRJMOfrG5NGY3P taVrl7Ib7foxKPdL1Cem/qhWSAulurhuhlYKBiZCbdTT8mIYWwmw8Jxf0DX0Ipteowxc 2+vFhWZfyhrSjxYu2+x6v6C+TnBT5b7lHZt2WDo8PWXTgx2hv2bEUmYTX9xomujjR6BG vSPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nIVyfuuL; 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 mp9-20020a1709071b0900b00770872942d8si3355351ejc.958.2022.10.25.07.41.37; Tue, 25 Oct 2022 07:42:04 -0700 (PDT) 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=nIVyfuuL; 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 S233029AbiJYOfx (ORCPT + 99 others); Tue, 25 Oct 2022 10:35:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232457AbiJYOfX (ORCPT ); Tue, 25 Oct 2022 10:35:23 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8EDC9E69C for ; Tue, 25 Oct 2022 07:35:21 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id b12so36588710edd.6 for ; Tue, 25 Oct 2022 07:35:21 -0700 (PDT) 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=cDM4r82Vu25XAn8FTag9RA1fMYhJwpZR6aZfJo/nKnA=; b=nIVyfuuLI2cY//5mXH5HGb5Gjqd/AcguOXfo5HF8Q8BDI7cOi75JXbGnOG3v+vIuz3 mt1skZQQchxvpTC3zTEBetG8//LJ4N9Xl1oxUA3Tj98dynsanFBRDfN5LYrPR8TOVWal FXN3gLDVO7LtkrPcDknz18vwrl8Qhcxtgll4s= 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=cDM4r82Vu25XAn8FTag9RA1fMYhJwpZR6aZfJo/nKnA=; b=kpCwiS6idde+QIGGqtigJE8nJVSULdGq7u58IoMn8j1kSwVS4eT5tEYtyjDTXl+hzH qQnHm17Hwdz53WwHYVFJtlTqzVIS5QMA0yKHYlmDi3KsX3kjAO5NqF0w9X0nxPqn3ETL 7AL0YnS3uBMwcNw3Cq+Dl43JPpmzTwacUOWrir5lyQH5Mtiz1kNJino0gj44PwaOTk9O WLQWeEiWL6dc5kM4qrZposlmMBXRlZmTsesKrosxq9NmoqP1VoqgPRAvEsl8j1oNW2K9 nD10N2J56tu2k1bSegGeXUKZXSvAfLCthoT3AqC3NO6TgQ2a7zue8pATdh0Rk933wIWo 6d4Q== X-Gm-Message-State: ACrzQf0Ned/ylPAJsMSegVXvO7afDVag3JYu8a8j/qu5TSjbqZMR0O7P i8lrC47t6DR0MDinGoavKOs++LKstp2Dosr4 X-Received: by 2002:a05:6402:496:b0:443:a5f5:d3b with SMTP id k22-20020a056402049600b00443a5f50d3bmr37136318edv.331.1666708520472; Tue, 25 Oct 2022 07:35:20 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:20 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:29 +0200 Subject: [PATCH v2 8/8] media: uvcvideo: Only call status ep if hw supports it MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-8-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=4002; i=ribalda@chromium.org; h=from:subject:message-id; bh=IWi5UBoOQCglgZ8fk/i0G4nYeLqeSgpNrXbJjKKYXhk=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QcG5LK6K0jUjnoGq8KBuuEpJ6sMYJlNi4Z99xs BCAWNeiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0HAAKCRDRN9E+zzrEiH3VEA CIqowtYcGGbBD5Wo2qirQ1FwAwCwchTq489hfSl5qGvsmBQohbYAFQDlUA7h2CRwwQnkU/vt3ZpFab 8M1Rvi+rfRZk9bSrcvodaXlamQIAQvShxFKOBniermTFJ4WJwyTDboodQyCtnhkiVczysx0Tb7RcrQ dNH9Uw/2ttvLSclYZO/msOuA8XYz+64OWIoMVP6wkeimPHw+QXUq+aFmRaWeIwEsEU394owG674Ct7 tvA72i0vlg3Ls3sA1vPavMa/RDwnR+Arx7wXHAhZEYoTHTrq7PBvhBXWaHhye3roTAQZBbokSTmKq6 v8Byx7IpUaBSoURpqsi342uJU+kdU7Or4foGrEXvrM7s5O+O0EHeXGGw8JO4xVZeA/edYDcg5vQ6/u jnE5jYg5sm1mom3gG0sKQnWVxWw770mR+kgAM9eL5nlRqg15VXz/rRLPau8giNSudLk6bmw1bGysgt +eLWLT7vIBwUWfU7I0HPZ8hF8Hxz9I0n26m8VK1xQlP1VxxqHtcvJ85dkIckJ1P++hBOKH9kDumDNe C7NU709b8c9tWMVgdHsaOtB+vgT5t65tpN7+bplq6flFkkt9Id9y6RhD6IbvVzgvo5Zs7c1UsGz8rU 03qsjRjeWNYJueL1GByAPAzYO+yRWATDEkzzZ+velzXiVLlbQCK4VKm1wxmQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.6 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,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747670977443305267?= X-GMAIL-MSGID: =?utf-8?q?1747670977443305267?= Instead of calling uvc_status_* regardless if the hw supports it or not, make all the calls conditional. This simplifies the locking during suspend/resume. Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index f15fbdbcd8bb..d2841f0d9132 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1847,7 +1847,8 @@ static void uvc_delete(struct kref *kref) struct uvc_device *dev = container_of(kref, struct uvc_device, ref); struct list_head *p, *n; - uvc_status_cleanup(dev); + if (dev->int_ep) + uvc_status_cleanup(dev); uvc_ctrl_cleanup_device(dev); usb_put_intf(dev->intf); @@ -1918,7 +1919,8 @@ static void uvc_unregister_video(struct uvc_device *dev) mutex_unlock(&stream->mutex); } - uvc_status_unregister(dev); + if (dev->int_ep) + uvc_status_unregister(dev); uvc_ctrl_stop_device(dev); if (dev->vdev.dev) @@ -2222,7 +2224,9 @@ static int uvc_probe(struct usb_interface *intf, usb_set_intfdata(intf, dev); /* Initialize the interrupt URB. */ - if ((ret = uvc_status_init(dev)) < 0) { + if (dev->int_ep) + ret = uvc_status_init(dev); + if (ret < 0) { dev_info(&dev->udev->dev, "Unable to initialize the status endpoint (%d), status interrupt will not be supported.\n", ret); @@ -2274,6 +2278,8 @@ static int uvc_suspend(struct usb_interface *intf, pm_message_t message) /* Controls are cached on the fly so they don't need to be saved. */ if (intf->cur_altsetting->desc.bInterfaceSubClass == UVC_SC_VIDEOCONTROL) { + if (!dev->int_ep) + return 0; mutex_lock(&dev->lock); if (dev->users) uvc_status_stop(dev); @@ -2308,6 +2314,9 @@ static int __uvc_resume(struct usb_interface *intf, int reset) return ret; } + if (!dev->int_ep) + return ret; + mutex_lock(&dev->lock); if (dev->users) ret = uvc_status_start(dev, GFP_NOIO); diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index cb90aff344bc..627cf11066e7 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -277,7 +277,7 @@ int uvc_status_init(struct uvc_device *dev) unsigned int pipe; int interval; - if (ep == NULL) + if (WARN_ON(!ep)) return 0; uvc_input_init(dev); @@ -312,19 +312,23 @@ int uvc_status_init(struct uvc_device *dev) void uvc_status_unregister(struct uvc_device *dev) { + if (WARN_ON(!dev->int_ep)) + return; usb_kill_urb(dev->int_urb); uvc_input_unregister(dev); } void uvc_status_cleanup(struct uvc_device *dev) { + if (WARN_ON(!dev->int_ep)) + return; usb_free_urb(dev->int_urb); kfree(dev->status); } int uvc_status_start(struct uvc_device *dev, gfp_t flags) { - if (dev->int_urb == NULL) + if (WARN_ON(!dev->int_ep) || !dev->int_urb) return 0; return usb_submit_urb(dev->int_urb, flags); @@ -332,5 +336,8 @@ int uvc_status_start(struct uvc_device *dev, gfp_t flags) void uvc_status_stop(struct uvc_device *dev) { + if (WARN_ON(!dev->int_ep) || !dev->int_urb) + return; + usb_kill_urb(dev->int_urb); } diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index c250b628fc4f..2861ab122beb 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -44,7 +44,7 @@ static int uvc_pm_get(struct uvc_streaming *stream) goto done; } - if (!stream->dev->users) + if (!stream->dev->users && stream->dev->int_ep) ret = uvc_status_start(stream->dev, GFP_KERNEL); if (!ret) stream->dev->users++; @@ -66,7 +66,7 @@ static void uvc_pm_put(struct uvc_streaming *stream) return; } stream->dev->users--; - if (!stream->dev->users) + if (!stream->dev->users && stream->dev->int_ep) uvc_status_stop(stream->dev); mutex_unlock(&stream->dev->lock);