From patchwork Mon Aug 17 15:16:04 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: 247833 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp2851661ilo; Mon, 17 Aug 2020 11:10:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtoka0npPYq1B9sUX5IYjMjcB49Udi1Fu8IPgYpJgfTtVqyuKox8B3yq+bKBiTnsynHZQR X-Received: by 2002:aa7:cd76:: with SMTP id ca22mr15810581edb.42.1597687851422; Mon, 17 Aug 2020 11:10:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597687851; cv=none; d=google.com; s=arc-20160816; b=WKbAnlUKAmGS/KKNLbqI4LXZPgmAZ14Qta2TSWheW2vRD9C+lVECxVEOGqKE+9Bpo2 q2ZFzcuWk7Br6HNtL6FuXKR6RMhGiKfUZDKN37NFQeqw2PPzhR6AbvaAHlQdHpbeaNX9 8FDSS3EM2Z5vu0qG6P9Kg2Bg0TLaVyr4yOQyLf4C0cOIwMeOS9NbxclxJHEmBhYzgcff ghC7AKO4Qk6s5jBRwn2pj2q3YpE7sVv4K2wVgL2GBMMgfgyZ++bYr2Uxx5z3X2Uo3r/8 19mLGI5LO7bQEXy3VzpDBrNf7naOkv4XPpTO1N/CdrSyIEB3Ss2vXnz6hxK3dDFaIGrr r/bQ== 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=poIpBHedq62ThMyPD1UR/CGCLrkpNKpeNazwueD1cpE=; b=wRyiv5Oy08QJp7ns2cqbv1cJo4ulf83/MoQXgOUxYbNTh213znYkoKwU2OxinyOCey gH/6uE2xYwkwhLhlOlxQHyyplXZX7VcdGo2Nb7fU5fBBPVNJDfLBC93sP5um7LRb4h6e YK/P+Ix8+nodqkuexBY76k4GB+2Hf4lwUEUWyrdrJbHhe+7bnkzqEkqO7ZTnOxzjiuyj dqZBuVIiPFqiWVreKjPchMKdpiEhzPulhIVfAH2/k5EONFnwT7byNYPpjy0C3bXkX5aW kRU90m28X0B0aS7W297cICe5uKjoDfI0CloBpBE18f7p4Qe+KcWAmspYMWwc+trYkvSd zRbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Nnxn1VSc; 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 lc20si11579278ejb.468.2020.08.17.11.10.51; Mon, 17 Aug 2020 11:10:51 -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=Nnxn1VSc; 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 S2390185AbgHQSKp (ORCPT + 15 others); Mon, 17 Aug 2020 14:10:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:55846 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388418AbgHQQHg (ORCPT ); Mon, 17 Aug 2020 12:07:36 -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 F2F3420772; Mon, 17 Aug 2020 16:07:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597680455; bh=O2kPUHFXsZUE9fM/qtFPfvCkivRIdDWRqncxrwEOQLU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nnxn1VSclDl0r3Wjb2hPZd6oC22guN/MfeVnNXc9I0sdhz7JziKYPgQEA65IwteqK w38Ct+5DaA30rUR8dmf8JUi48r4oqcdmtnPB7j9y8KWzYG9D7tfUQx4f5h1nEXaKxB PK9aeWMsX8p5izdj208aUh1GK4RhFiWBDbL4iako= 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.4 163/270] usb: dwc2: Fix error path in gadget registration Date: Mon, 17 Aug 2020 17:16:04 +0200 Message-Id: <20200817143803.957885748@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143755.807583758@linuxfoundation.org> References: <20200817143755.807583758@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 4e14c4f7fed7a..34bb6124f1e2f 100644 --- a/drivers/usb/dwc2/platform.c +++ b/drivers/usb/dwc2/platform.c @@ -514,6 +514,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; } @@ -522,7 +523,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; }