From patchwork Thu Jul 16 12:09:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 235642 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp733378ilg; Thu, 16 Jul 2020 05:10:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYK+S+nMZwT/aAJU6wt6/1g9rvCmJCtKRBH1+5CIAnmIlAOMLSmp0zKxnjp6BiQj3/vKi1 X-Received: by 2002:aa7:d341:: with SMTP id m1mr4066551edr.50.1594901403252; Thu, 16 Jul 2020 05:10:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594901403; cv=none; d=google.com; s=arc-20160816; b=vf9FpR1PEcwzbweKbbpKhrpRd6Kbkxe7JhCPTTMODvX2TuE32FKRI+J+CFnnoAdAkB 2ZbRuOTzfWSX1jqklu3KLwa274Y0DLApMr7irXz38qz4iZ4I3abZRFuPQFPgad5HPLF1 lOvSiZl66xJUoyo39W/VwAxuI60qVDTBEYoBh+xt8UB92kR4YXL6h5b+xPTbYsy5yqky YBisf8lv3OzHnHjFr0UmJKY5vLMC65/fbWFgg70FLRzFqe26HHK26N1f87CkMDygwOK3 8e9ifeO9UKHoKlrnea7nmnrn9rvq3t2sWY+k2ohFYif+6YXc3Bzn5W9ARuBSrRB8mJVD 6g9w== 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=qV/2zLgNvC0a3UfT7Zky5MCOQpz0Ag3pF7I6DZ5fm1s=; b=mfv8228/wtYWiS1BFMOrsJFzhaVNmrwKjAdyaK8ahlFtlcMvWQbPyCcGnIVIYD29Tr ZbA+1JVuOHGV3sjGHOjmyFcU1nnJnuuhaRJkNDz3H8L0+WpY0+FWeic7fzQIq4CT0Ruz cFbAiMYgmMm9XJhVVDzmuEc3LbDTXnL9k09QcLHRlxH2q5vFEQesVxH59aXhBQBt3Hg6 chlHeSBUrPEFJXw6irL0E4OL3gs/+cs/BTcUl9n7DuCdp+CrFmoVko1zqkJa54jC5eJ5 sGYKhvhLRnhpduCA0phIy32cjJWaaPV6OEBu5BSB6DmKLna8uuA7C+lDj+GWAepV5/ze JuWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=GMNbWsWa; 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 f16si3245909edq.307.2020.07.16.05.10.03; Thu, 16 Jul 2020 05:10:03 -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=GMNbWsWa; 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 S1728096AbgGPMJ7 (ORCPT + 4 others); Thu, 16 Jul 2020 08:09:59 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:60634 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726515AbgGPMJ6 (ORCPT ); Thu, 16 Jul 2020 08:09:58 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200716120957euoutp02d61e6993fcce85e19e9e6be3722a8dd2~iOl7bLqjv2359423594euoutp02u for ; Thu, 16 Jul 2020 12:09:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200716120957euoutp02d61e6993fcce85e19e9e6be3722a8dd2~iOl7bLqjv2359423594euoutp02u DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1594901397; bh=qV/2zLgNvC0a3UfT7Zky5MCOQpz0Ag3pF7I6DZ5fm1s=; h=From:To:Cc:Subject:Date:References:From; b=GMNbWsWaqZ9GuLmZU3sVeZE3Lq1sE+7Lo/LhrnDVp8htNyf7lZ0pes+u7sm17AwL8 R0xZkMnBI33E7I5VKdWOTdYf+uaqYChZkE/+0NN4c4VYB7lZ2JTjFFcyjomQTwMORP NI2BXmEU2NlDOtUcHz+G5kxd+SNwmOHm2K6mQqTg= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200716120956eucas1p182b19f804e68f5e7359a54077682e848~iOl7KjAvP1344213442eucas1p1V; Thu, 16 Jul 2020 12:09:56 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id FF.5E.06318.493401F5; Thu, 16 Jul 2020 13:09:56 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200716120956eucas1p1c5fd8e24c16e6616041467f618034b44~iOl60-zXW1000010000eucas1p1C; Thu, 16 Jul 2020 12:09:56 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200716120956eusmtrp28afd21a2b3556b05592dd73f0f68640a~iOl60a8Kt1849418494eusmtrp2U; Thu, 16 Jul 2020 12:09:56 +0000 (GMT) X-AuditID: cbfec7f5-371ff700000018ae-0e-5f1043948671 Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 49.16.06314.493401F5; Thu, 16 Jul 2020 13:09:56 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20200716120956eusmtip2c7234882333b6b2ce6f3b01c7cd3e0dc~iOl6Z26RE0100101001eusmtip2h; Thu, 16 Jul 2020 12:09:55 +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 v2] usb: dwc2: Fix error path in gadget registration Date: Thu, 16 Jul 2020 14:09:48 +0200 Message-Id: <20200716120948.28180-1-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrPIsWRmVeSWpSXmKPExsWy7djPc7pTnAXiDQ4/1rPYOGM9q8Wxtifs Fs2L17NZNP7ay25xedccNotFy1qZLdYeucvuwO6xaVUnm8f+uWvYPfq2rGL02LL/M6PH501y AaxRXDYpqTmZZalF+nYJXBlrT05iKVguVzFzmmID42KpLkZODgkBE4m3866xdDFycQgJrGCU WPf2ECOE84VR4vKRb1CZz4wSUzf1s8C0LH7SDFW1nFHi7/dNzCAJsJa700xAbDYBQ4mut11s ILaIgKzE4Su/mUEamAVamCQWnz8A1iAs4CIxaW4XE4jNIqAq8WJpD1icV8BWYub/z1Db5CVW bzgA1iwhcJ1NYvLfi6wQCReJrasOskHYwhKvjm9hh7BlJP7vnM8E0dDMKPHw3Fp2CKcH6KOm GYwQVdYSd879AurmALpJU2L9Ln2IsKPEjheLmUDCEgJ8EjfeCoKEmYHMSdumM0OEeSU62oQg qtUkZh1fB7f24IVLzBC2h0RD50YmSKDESpzY+Id1AqPcLIRdCxgZVzGKp5YW56anFhvnpZbr FSfmFpfmpesl5+duYgQmg9P/jn/dwbjvT9IhRgEORiUe3orffPFCrIllxZW5hxglOJiVRHid zp6OE+JNSaysSi3Kjy8qzUktPsQozcGiJM5rvOhlrJBAemJJanZqakFqEUyWiYNTqoExR6wn Tcde6ssqrsrvXtOdhW7ps4TyPk+fddhn5s815Z7Fs9YXNBY2vTJTyvuzy/J22oS+/c4Cz05a vWtQE4z/VZ/pebbz2NlFZlmsHKu+rHVlctikUBG2N8RL7Hum188bLCvn7QhL3pnaciti98eD d171y7e83H5COPpNmKXCs+hdJYUTmhSUWIozEg21mIuKEwGE4vyFAgMAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrCLMWRmVeSWpSXmKPExsVy+t/xe7pTnAXiDSbOErfYOGM9q8Wxtifs Fs2L17NZNP7ay25xedccNotFy1qZLdYeucvuwO6xaVUnm8f+uWvYPfq2rGL02LL/M6PH501y AaxRejZF+aUlqQoZ+cUltkrRhhZGeoaWFnpGJpZ6hsbmsVZGpkr6djYpqTmZZalF+nYJehlr T05iKVguVzFzmmID42KpLkZODgkBE4nFT5oZuxi5OIQEljJKHDj4kwkiISNxcloDK4QtLPHn WhcbiC0k8IlRYsYSSRCbTcBQoustRFxEQFbi8JXfzCCDmAU6mCR6e34ygiSEBVwkJs3tAhvK IqAq8WJpDzOIzStgKzHz/2cWiAXyEqs3HGCewMizgJFhFaNIamlxbnpusaFecWJucWleul5y fu4mRmAIbjv2c/MOxksbgw8xCnAwKvHwKvzkixdiTSwrrsw9xCjBwawkwut09nScEG9KYmVV alF+fFFpTmrxIUZToOUTmaVEk/OB8ZFXEm9oamhuYWlobmxubGahJM7bIXAwRkggPbEkNTs1 tSC1CKaPiYNTqoExo9XhsGrrpyKlXdn8mTlJRjnvzt86veaXBb9G9OJDa/mvpX1eeuGSbOtc Wb/XKfaiycfu/75zV8DKa8P7/5M4ZYX1+RMYjN4knDcvsvkU6D67Re9NiIPHv3ojSQ4evmcz fFjfaq3/EFwZX/+c/5i0+72v+/9IVwU99DE+yurIN9UpiO+7xWwlluKMREMt5qLiRAANp9JC VwIAAA== X-CMS-MailID: 20200716120956eucas1p1c5fd8e24c16e6616041467f618034b44 X-Msg-Generator: CA X-RootMTR: 20200716120956eucas1p1c5fd8e24c16e6616041467f618034b44 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200716120956eucas1p1c5fd8e24c16e6616041467f618034b44 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 ]--- 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") Signed-off-by: Marek Szyprowski --- drivers/usb/dwc2/platform.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) -- 2.17.1 Acked-by: Minas Harutyunyan diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c index cb8ddbd53718..db9fd4bd1a38 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; }