From patchwork Mon Aug 17 15:13:59 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: 247799 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp2734248ilo; Mon, 17 Aug 2020 08:33:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxm0Ne8s2N09rgMO+541XfbU0cBhHcFM/Km61Kwd1GyKEYRDaebWzlJVTdpgInPJDzD23jL X-Received: by 2002:a17:906:95d4:: with SMTP id n20mr16523972ejy.485.1597678386109; Mon, 17 Aug 2020 08:33:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597678386; cv=none; d=google.com; s=arc-20160816; b=vZrOohIkI5Q5XoWIiM3ZiQUzXE9RmAxnAAzQGAdGT73krcaoRmSg+dnjjJs5B1WZkh rY1vmXARJVvPTaAsxEHt7a0nO1rp+pXmNu/nRlxu6qUDGQusFvxts32EnI92WBlyiUJl RW3QL8R9yXYc21XDaY9rW4qP0mJ5aiS87zfPgG9y0r9C9GyxRtG33EHgL7coD3GpXPzc ze3StQaYnJtMwl0jNhkiL/3Tkw5lMY5b+IzYZGX/eZ0FslNRbqItIkQf1A8Vg2ywmaHP YEqLA23heRNM5hVLKB/2evc/o1Ao2KlqMFZoZNLQYY9gPLebeYdhAsYrnr7zfbBhXQk3 z+fQ== 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=lF45s/Uo20bW6n0NkEX7NR3wRv5c6Ui4+JMIxdMEgGw=; b=P52PDVA2X4os+xGL9JZIJYA5kk3vY8Cwb4vXCn2dMCVI7FH1Dlq9MKcYW7EhPVb6TT MDYgYAKsP8upVGuh4IDA+7HZCvlI+Y+U9PXgXED9pFpH1Oa89pF8eAPTQelvXPLfMI0D AX6glX3VE6yb7jbnld7jcup99YY3uWVOa8uFBkkSaWvbjK9td79XEBXseut75LtzfpBX KSRJY+zF6LldgjQhFxYH/w6Z4lJZgf6x0EJBhitom061RxjJLKjmmNBjNh5AAAgTlt0G 3SDm0deIKfJCbUHbn73/9XK9Ow/fpImAu7avGqIK1NyfOsyl8q0A3HGaXZSav1lYA2GY Oyaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mdrmiSpm; 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 gh13si11412414ejb.3.2020.08.17.08.33.05; Mon, 17 Aug 2020 08:33:06 -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=mdrmiSpm; 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 S1730120AbgHQPdA (ORCPT + 15 others); Mon, 17 Aug 2020 11:33:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:33856 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730344AbgHQPc6 (ORCPT ); Mon, 17 Aug 2020 11:32:58 -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 AB78E22B40; Mon, 17 Aug 2020 15:32:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597678378; bh=OtSyzvPyV67jAsPHZaD/hbRlFPM/3oUHi/6wTrtPyrE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mdrmiSpmtEicmS9ga9W3f1IJ5UsrG2g4B30jX6JRw16n0fnZd0BwxcS9wrlP84kn3 XHoxflV0cvmLPJjEtFf39BiR3DQ1U2i+svCAXjsvr6Lm9Ic82GwzYX6sqyTjHNH8Xt eg5w6TeFJY25LF7hKg7zPa0xywFT6YU9LBZ6cCXs= 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 5.8 286/464] usb: dwc2: Fix error path in gadget registration Date: Mon, 17 Aug 2020 17:13:59 +0200 Message-Id: <20200817143847.455714575@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143833.737102804@linuxfoundation.org> References: <20200817143833.737102804@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 cb8ddbd537187..db9fd4bd1a38c 100644 --- a/drivers/usb/dwc2/platform.c +++ b/drivers/usb/dwc2/platform.c @@ -582,6 +582,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_init; } @@ -593,7 +594,8 @@ static int dwc2_driver_probe(struct platform_device *dev) if (hsotg->params.activate_stm_id_vb_detection) regulator_disable(hsotg->usb33d); error: - dwc2_lowlevel_hw_disable(hsotg); + if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) + dwc2_lowlevel_hw_disable(hsotg); return retval; }