From patchwork Tue Jul 14 08:48:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 235510 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp3544998ilg; Tue, 14 Jul 2020 01:48:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweBNCtngBoG0yJxaSQplZQjFNCyStHsw+vjuh/uj0pkNTIYClEQ/rxKIkf9Uo2pW4xA8KK X-Received: by 2002:a17:906:35ce:: with SMTP id p14mr3500907ejb.514.1594716494671; Tue, 14 Jul 2020 01:48:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594716494; cv=none; d=google.com; s=arc-20160816; b=IfM9tG1HxNjbjH8iGj7eVTS4iHvPjXvNovhwo0UYVtCsDshj8pDeuvL5FzBHAd3WLN KSspbtF2sfJ+DsQtw68romPT2dT7wKcffloCnCpvpvdSO1WLtsddZOjMrMtjvV+A0Oio /jdN54G+/bKQsGTA1aaGFagP4HyDRyUA0wx0Coqh2OEqMCcfulPnLE0qXEoKhEXXYj1p vlEpHE7bG3oYHEtNljJ9Ppg9tNMiUQoaeRRFrgLUZUMCNu8qabQq9wGefWBgAcRfWShP CcoTiDi4tW5LrdYdNqzYuJDszN/Z2A8BQe5OXUmhCV4GGUwNDfHSre66OHZUX2uhhq37 /Y/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=nf9+rmH806iB74WPwNFe4oqbior1dSEZhoJ7GlacjDk=; b=yiBeQfzlb7o9B1wksm5UFKqzYR6sXgxMVWR97s7LqFVXkBd5CHNwPc5uNPA9ZqQTBB UhmWgan85QIBUmg1woyezmZ/AYZlr6nibb937hzUSIL01qVyE2Mfyo8QLgRyVg47RKwB r5EiS5B4r7qlnuvYZhJS30fCuMPSVRHCgZYRZguubCL3iKNAiyrP32DCMOlO1uCEwOVT ctv2F+JuNomMD/MRlJvK700XYDKbvWilHYX+LcVHfMBEfntfTIcMfMw+9Da37uaqi3Q1 icGCS3UdGASOb9lNvfBHKENNav4Bkhck9GsQvDPXqdcL3C+mR/YhC3h1UVjId5Er8XnL K4pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=fQNQBl07; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y24si11502993edv.573.2020.07.14.01.48.14; Tue, 14 Jul 2020 01:48:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-usb-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=@samsung.com header.s=mail20170921 header.b=fQNQBl07; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725981AbgGNIsL (ORCPT + 5 others); Tue, 14 Jul 2020 04:48:11 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:51516 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725820AbgGNIsK (ORCPT ); Tue, 14 Jul 2020 04:48:10 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200714084808euoutp02796d5a6a96c8bfa645aefccc33f0a44f~hkjJp0v4S0683906839euoutp02P for ; Tue, 14 Jul 2020 08:48:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200714084808euoutp02796d5a6a96c8bfa645aefccc33f0a44f~hkjJp0v4S0683906839euoutp02P DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1594716488; bh=nf9+rmH806iB74WPwNFe4oqbior1dSEZhoJ7GlacjDk=; h=From:To:Cc:Subject:Date:References:From; b=fQNQBl071BT9nZGPGW5MqxTwd6FJBOO50PMkuGhb1BfM1KWFCnRQePG2aZGTa94w6 GU26+9s9H2U0Jt8dyHtH6VOPxJ+Y3NMhGYhzovsdULRDmqPibkO9N6aXT4Cx9OHkOy YraeUZFUVK/gQGE3hKV6VTGTB5Lg1VosyUYRS6Jw= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200714084808eucas1p19a340f9b3d245baa125565eafbc89992~hkjJeJzbP0985509855eucas1p1F; Tue, 14 Jul 2020 08:48:08 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id 44.FA.06456.7417D0F5; Tue, 14 Jul 2020 09:48:07 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200714084807eucas1p1f6e680d162573c6a0660037172a65805~hkjJGzWkA3158931589eucas1p1e; Tue, 14 Jul 2020 08:48:07 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20200714084807eusmtrp19327fdb45397ddfab5ad9af6edb2ba27~hkjJGJcdw0886508865eusmtrp1T; Tue, 14 Jul 2020 08:48:07 +0000 (GMT) X-AuditID: cbfec7f2-7efff70000001938-a2-5f0d7147c03d Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id CE.49.06017.7417D0F5; Tue, 14 Jul 2020 09:48:07 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200714084807eusmtip16580d3ed95cbdcd055ffd5bf2b5ca7df~hkjIq9KwM1330213302eusmtip1Y; Tue, 14 Jul 2020 08:48:07 +0000 (GMT) From: Marek Szyprowski To: linux-usb@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Marek Szyprowski , Minas Harutyunyan , Felipe Balbi , Bartlomiej Zolnierkiewicz , Greg Kroah-Hartman Subject: [PATCH] usb: dwc2: Fix error path in gadget registration Date: Tue, 14 Jul 2020 10:48:00 +0200 Message-Id: <20200714084800.11340-1-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrLIsWRmVeSWpSXmKPExsWy7djP87ruhbzxBq9v2FhsnLGe1eJY2xN2 i+bF69ksGn/tZbe4vGsOm8WiZa3MFmuP3GV3YPfYtKqTzWP/3DXsHn1bVjF6bNn/mdHj8ya5 ANYoLpuU1JzMstQifbsEroymvrtsBQ8lKw788WxgXCfWxcjJISFgInH94hPGLkYuDiGBFYwS e47cYANJCAl8YZRouWcKkfjMKNHQ9YMZpqPrbSM7RGI5o8TLhd+hHKCOuf+2soBUsQkYAlV1 gY0SEZCVOHzlNzNIEbNAC5PE4vMHwEYJCzhK7D77B2g5BweLgKrE2/NVIGFeAVuJV+cnM0Js k5dYveEA1ObrbBLN3zIhbBeJey2tLBC2sMSr41vYIWwZidOTe1hAdkkINDNKPDy3lh3C6WGU uNw0A2qqtcSdc7/YQBYzC2hKrN+lDxF2lOh/8g3sHgkBPokbbwVBwsxA5qRt05khwrwSHW1C ENVqErOOr4Nbe/DCJagzPSQOXT8ODcVYib3P2hgnMMrNQti1gJFxFaN4amlxbnpqsWFearle cWJucWleul5yfu4mRmAiOP3v+KcdjF8vJR1iFOBgVOLhlfDniRdiTSwrrsw9xCjBwaw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2iqxFGckGmoxFxUnAgDdMmdb VwIAAA== X-CMS-MailID: 20200714084807eucas1p1f6e680d162573c6a0660037172a65805 X-Msg-Generator: CA X-RootMTR: 20200714084807eucas1p1f6e680d162573c6a0660037172a65805 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200714084807eucas1p1f6e680d162573c6a0660037172a65805 References: Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 ]--- Fixes: 207324a321a8 ("usb: dwc2: Postponed gadget registration to the udc class driver") Signed-off-by: Marek Szyprowski --- drivers/usb/dwc2/platform.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c index c347d93eae64..a6022dc4766d 100644 --- a/drivers/usb/dwc2/platform.c +++ b/drivers/usb/dwc2/platform.c @@ -581,8 +581,9 @@ 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; + return retval; } } #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */