From patchwork Mon Aug 17 15:14:54 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: 247853 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp2879429ilo; Mon, 17 Aug 2020 11:52:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyoCzkORf4/8hehBiNWEF2jAjNa60SE0yi3OIzN0WePMKIrY6Zv0JJ/Vm+f4FJGbLsc3SIF X-Received: by 2002:a05:6402:1b1c:: with SMTP id by28mr16804915edb.89.1597690344296; Mon, 17 Aug 2020 11:52:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597690344; cv=none; d=google.com; s=arc-20160816; b=be3FNq5OGjpMa0RPCfjkK2/F5ftwTLFDxEG4vZ97/iE23IzRNY9puSd/G1j0nkJewT O81EHiVwi9xGvDASnMWpU2reNZpX6xXtpRvjZ8/GHxZpRQ+VQxtS68Qf+74vCdAJrvYZ 5YPhKV1Nlr6ls1ZBdTH6LydTO05m8VwQCH3JW/Alqym88NSp93VTVU452vAGl0Ktof1c q+xn8xbMmaT0FsjUqtsJSMcI3Z+X5SHekNo2i/EaG5JuIV0FVStEYgfl9QPjSqTC05QD DEhaA8tIgnQd/eXpWl0U1rtzPbWLvOZg6FQMO2viv8c5adtDDlxSMgIX7gV7A9zybp1E zkPQ== 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=uCIRWj+Snyq+snNsm7jpNOvxynTsaJ1LgfFvdacWU7g=; b=lNQqUGxc9+YSoDr+F2FXSAOciwq0QnBlkxhDqpoFuZ8USX2vjmf9CVkKA7FXmqtw/R amk1SLb6xvjr1lHMCkMuJBx0yFAL+vaz3awS1Ant6xj4EA16GYoWEmUaotpvz796+Bxf wNJfZEHmCnC6LWT8HmC3W03IQkphOjp9ByJANei3zVxu+ej2JODzuzsORg2aFvC89F/H zuU7xd5UuoW7/EvzdwbSq8emtAXm8oPAVCF7Wmam9d7GlnLgwzKnXoNe8lqXfTjzDkpR 1RCBVc7FR56EnyXMj1SFvjyQJwx6bUDEtD8NhBoa8P8q+VifUNnsxUujPXSs2aNUHwYF U2fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BHvxDVZL; 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 rs7si12267875ejb.195.2020.08.17.11.52.24; Mon, 17 Aug 2020 11:52:24 -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=BHvxDVZL; 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 S2403910AbgHQSwU (ORCPT + 15 others); Mon, 17 Aug 2020 14:52:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:37666 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387462AbgHQPwL (ORCPT ); Mon, 17 Aug 2020 11:52:11 -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 740D5208B3; Mon, 17 Aug 2020 15:52:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597679531; bh=g2OJoc78cXZVdQzYawWrSL+0Rb7WpVND5XaW11Ri9e4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BHvxDVZLzBI1yx3pkAU2ZX0Mzv2k5bBxp1AJs+iSpPPe6jZvD2KgoSDSxfLExCN1l ro2A9nn7D7UpykTD1eOW1IBqgnyrZkmCrcalxlYgdNJLf3W4C2A4ANO9s6s0NrS+Oj gD7bWNyl/n1gQOFIk9ZNW3WJwAnmlNvPq0JLuZTs= 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.7 244/393] usb: dwc2: Fix error path in gadget registration Date: Mon, 17 Aug 2020 17:14:54 +0200 Message-Id: <20200817143831.455577031@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143819.579311991@linuxfoundation.org> References: <20200817143819.579311991@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 797afa99ef3b4..4ad85fa2c9328 100644 --- a/drivers/usb/dwc2/platform.c +++ b/drivers/usb/dwc2/platform.c @@ -543,6 +543,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; } @@ -554,7 +555,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; }