From patchwork Thu Aug 20 09:21:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 248032 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:522:0:0:0:0 with SMTP id h2csp1274679ils; Thu, 20 Aug 2020 04:48:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQUQce7eBHwvLcsq8NJLZ4PJxVvxOdwOS5N56T/eG58OqdryhlrzlwDzqzjA6doODq6jBS X-Received: by 2002:a50:9358:: with SMTP id n24mr1013822eda.7.1597924122648; Thu, 20 Aug 2020 04:48:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597924122; cv=none; d=google.com; s=arc-20160816; b=VTM/j4ktc8Dah++F6+VHG+M1dSpI41/Y5gv8BsfubWMEDNmRg9zXsZ7EbgdVdaQ0Yh WMYDYvFyX5px7aOv01TdWqV6lcG5jB0z4WiYa/lHVLS2ArpaGwZxg9yV5A5AO85VqqGZ JdkQv5SzMkGd8++8N44LOUQpZCJQvyDYKy2Lv5Y2wSHFb1/nmxxo6HvDsPm/UYdKi0WH vM03/5IrNQMWYv0RpwDHJTLyssM13HnOdxnPAdtW3ponfAYZlTra6FQdqsx2SYhsUwNJ GnIPAqTw7gpW2rmsleoqfW9Xf4qB7J3T5vpWuv/JoIF9ezjS4ZpHFh/xTX4UieFnmvdJ 9JZw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LQuwJLffXGx/HGGcWYFXm9SQDKZNVmW3DBpk8x/Upn4=; b=wbYB/dcFx8BVmEb8/O2CuIJnQBiOrYd600gmdYslX1IJe5Z00mDm337Xy2U34yyMZe AthicdpyxAuFBOMZlQNnCvKVGBZdg7gAq22yHmoZQjKBdiiR5Jaiw5nAYX74c8VbxVwC lbDUr+dqr2hcZs19MyLDzulTk9gl0UEupU3qiJkMf58U+5dMY9nfYdO/BwCYlfIOoEBs lKMs3Y0wsT6fmdKzuQ/V/zpH4GHCYzCpjnWJ4vxDQxNb3fZnjpQVigxr5KtEgGjZKmhb k74BhU7E5ANQzpBStpNOvpiS9BWXDeMf4jNg2/8ZtIXaoCFsYxZG9IMqWzKh/1dilew7 d3JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uqMO02Jd; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si1180762eja.208.2020.08.20.04.48.42; Thu, 20 Aug 2020 04:48:42 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uqMO02Jd; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730733AbgHTLsj (ORCPT + 15 others); Thu, 20 Aug 2020 07:48:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:55426 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729784AbgHTKDO (ORCPT ); Thu, 20 Aug 2020 06:03:14 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D02A520855; Thu, 20 Aug 2020 10:03:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597917794; bh=Et7ls60VZjoaoIrfzW+kXQbNaDg2LPboBoHyqEgWOYg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uqMO02Jd0QuvH/l5owDO1Pd+y3r0hiN8RtBndAoaDrZs5E6A953fUgId90Vl6/qW2 JU2G7+5EvYF80DtjzXY4jwbTjyDF1gZxPlTpOC7DdYsT2QMzJ/dZQtjdkdPqiMd+uD G8X06zkzXC1oS12sMNGNUAp9X1LRLJD+mDTPTBRw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Minas Harutyunyan , Marek Szyprowski , Felipe Balbi , Sasha Levin Subject: [PATCH 4.9 133/212] usb: dwc2: Fix error path in gadget registration Date: Thu, 20 Aug 2020 11:21:46 +0200 Message-Id: <20200820091609.066997808@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200820091602.251285210@linuxfoundation.org> References: <20200820091602.251285210@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Marek Szyprowski [ Upstream commit 33a06f1300a79cfd461cea0268f05e969d4f34ec ] When gadget registration fails, one should not call usb_del_gadget_udc(). Ensure this by setting gadget->udc to NULL. Also in case of a failure there is no need to disable low-level hardware, so return immiedetly instead of jumping to error_init label. This fixes the following kernel NULL ptr dereference on gadget failure (can be easily triggered with g_mass_storage without any module parameters): dwc2 12480000.hsotg: dwc2_check_params: Invalid parameter besl=1 dwc2 12480000.hsotg: dwc2_check_params: Invalid parameter g_np_tx_fifo_size=1024 dwc2 12480000.hsotg: EPs: 16, dedicated fifos, 7808 entries in SPRAM Mass Storage Function, version: 2009/09/11 LUN: removable file: (no medium) no file given for LUN0 g_mass_storage 12480000.hsotg: failed to start g_mass_storage: -22 8<--- cut here --- Unable to handle kernel NULL pointer dereference at virtual address 00000104 pgd = (ptrval) [00000104] *pgd=00000000 Internal error: Oops: 805 [#1] PREEMPT SMP ARM Modules linked in: CPU: 0 PID: 12 Comm: kworker/0:1 Not tainted 5.8.0-rc5 #3133 Hardware name: Samsung Exynos (Flattened Device Tree) Workqueue: events deferred_probe_work_func PC is at usb_del_gadget_udc+0x38/0xc4 LR is at __mutex_lock+0x31c/0xb18 ... Process kworker/0:1 (pid: 12, stack limit = 0x(ptrval)) Stack: (0xef121db0 to 0xef122000) ... [] (usb_del_gadget_udc) from [] (dwc2_hsotg_remove+0x10/0x20) [] (dwc2_hsotg_remove) from [] (dwc2_driver_probe+0x57c/0x69c) [] (dwc2_driver_probe) from [] (platform_drv_probe+0x6c/0xa4) [] (platform_drv_probe) from [] (really_probe+0x200/0x48c) [] (really_probe) from [] (driver_probe_device+0x78/0x1fc) [] (driver_probe_device) from [] (bus_for_each_drv+0x74/0xb8) [] (bus_for_each_drv) from [] (__device_attach+0xd4/0x16c) [] (__device_attach) from [] (bus_probe_device+0x88/0x90) [] (bus_probe_device) from [] (deferred_probe_work_func+0x3c/0xd0) [] (deferred_probe_work_func) from [] (process_one_work+0x234/0x7dc) [] (process_one_work) from [] (worker_thread+0x44/0x51c) [] (worker_thread) from [] (kthread+0x158/0x1a0) [] (kthread) from [] (ret_from_fork+0x14/0x20) Exception stack(0xef121fb0 to 0xef121ff8) ... ---[ end trace 9724c2fc7cc9c982 ]--- While fixing this also fix the double call to dwc2_lowlevel_hw_disable() if dr_mode is set to USB_DR_MODE_PERIPHERAL. In such case low-level hardware is already disabled before calling usb_add_gadget_udc(). That function correctly preserves low-level hardware state, there is no need for the second unconditional dwc2_lowlevel_hw_disable() call. Fixes: 207324a321a8 ("usb: dwc2: Postponed gadget registration to the udc class driver") Acked-by: Minas Harutyunyan Signed-off-by: Marek Szyprowski Signed-off-by: Felipe Balbi Signed-off-by: Sasha Levin --- drivers/usb/dwc2/platform.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) -- 2.25.1 diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c index 38926495c751d..f985315ebd3bd 100644 --- a/drivers/usb/dwc2/platform.c +++ b/drivers/usb/dwc2/platform.c @@ -668,6 +668,7 @@ static int dwc2_driver_probe(struct platform_device *dev) if (hsotg->gadget_enabled) { retval = usb_add_gadget_udc(hsotg->dev, &hsotg->gadget); if (retval) { + hsotg->gadget.udc = NULL; dwc2_hsotg_remove(hsotg); goto error; } @@ -676,7 +677,8 @@ static int dwc2_driver_probe(struct platform_device *dev) return 0; error: - dwc2_lowlevel_hw_disable(hsotg); + if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) + dwc2_lowlevel_hw_disable(hsotg); return retval; }