Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1029135rdf; Wed, 22 Nov 2023 03:57:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqjLzzGZHFhqwOzhlm70qrJqL/9MrZB0ToOJcTApjAD+dtCG4o7by0q5df+T8lwh+oFdv8 X-Received: by 2002:a05:6a20:8f25:b0:14d:e075:fc5d with SMTP id b37-20020a056a208f2500b0014de075fc5dmr2191192pzk.40.1700654260882; Wed, 22 Nov 2023 03:57:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700654260; cv=none; d=google.com; s=arc-20160816; b=JGpmFbLFp5dQYiHirVyl764zcJH0YHchhiKtXakcUVzF1q0tu1rFo0bwXZazWv5MKE 4mls2zFhhR0wI+LyMdsBLVOIGvMad+lB4oX1R01qj1HkFc0gZY0VnsVoCBNR24M+1L1x GVgwrTED4ft7GXkkPoGHhnFZA6FuzxcJBlO4FK0E/rhH+4Ld6x5ADeM/R34OHzGeiVht kAEXF+mKf98d3GaRXHaAu5t+4MqdBO4EYdOXYPJ1yOdA5arpmGWfvJ8ZQdbwnehWDPvX Uhx0p9xtw997qrtcdon/oXmVZDI4Iq+/aS5iY/yU3UjQ2bc2Vt18vHUlxFPwExohXZLs tmZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=2cUg2qk2Y51TaW9h5kMCtTYKmp0V6gOVNeiPzFEjXCQ=; fh=1ndqQu9TN2NeubMezyPfztUO1hBQ/5jm/0fERnoIcQA=; b=XYhPQ2PD/86AD905MfbCSEGZHN/0c3CIO+R2ynwK9mi4b+Ei8Lbln7cBAdwkuZSoy2 0cgX6N0m7V05bX3Tdmi2oa/AlCz47cTBh7YD/JxZSMUT3bS/k/MaRW2mVC8+9G1sbktC RlIh4Ono6w/kAZGDn0hCCBRjWWx6BN5PjKVQNw+M2dSyuL95ZIrteaCE9x3ljbCgvwzW IFbxo1koCdWGnLs4cWVj2VHMxF92nvcTDKeOT4gDy8cDIvjs0AHANLNPFSlwpMQyemrP yALN1oQOyYnfCrA71RgLHRppaRa4LLi994luyGAQVxeaARhWn4WuTqbKb9+Work2Lb6K /wDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=D0oGwxCj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id fa1-20020a056a002d0100b006c2d5919b46si12614751pfb.117.2023.11.22.03.57.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 03:57:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=D0oGwxCj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 08DE6819DFE6; Wed, 22 Nov 2023 03:57:30 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343577AbjKVL53 (ORCPT + 99 others); Wed, 22 Nov 2023 06:57:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233111AbjKVL51 (ORCPT ); Wed, 22 Nov 2023 06:57:27 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF5A4197 for ; Wed, 22 Nov 2023 03:57:23 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E5C1C433C8; Wed, 22 Nov 2023 11:57:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700654243; bh=dhMFJjDMdHJmWFuL/Wz6Hy55a2bliAN+jn2DVLtt1l4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=D0oGwxCjLnODyhXiJsAd0pJ2Q3TpcD6IBQBoAwHKPP5g+eJZ09t6EC0LqE77xOwAf +64jj7gRMSYoTpdX0U0u3VFzdxFNYCifKB7ndtf9EBCHabveyDebo/9ITRbI3sXr/J VLV+Lq6LDBMOezWNmYXqVdZz3f4wfoY3d+GWLPOHWIWYNmXuOLFvn92pafASWgLWP2 S8leQr7Mz7N3pFu8qI22CGtN1ODlhndDfPy2Trb3sFccW6FCjdV0BBae2ykfGQh0kX wPn1W/zXlvWfUAs5hMGSJWHPjlfNtfZid3T2NsvAiC9z6F33jtseZOX+YwD4gDBPUI I4MMpAxs/0aRg== Received: from johan by xi.lan with local (Exim 4.96.2) (envelope-from ) id 1r5lrN-00005t-0A; Wed, 22 Nov 2023 12:57:37 +0100 Date: Wed, 22 Nov 2023 12:57:37 +0100 From: Johan Hovold To: Bjorn Andersson Cc: Bjorn Andersson , Konrad Dybcio , Greg Kroah-Hartman , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Wesley Cheng , Thinh Nguyen , Felipe Balbi , Philipp Zabel , linux-arm-msm@vger.kernel.org, linux-usb@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Krishna Kurapati PSSNV Subject: Re: [PATCH 04/12] usb: dwc3: Expose core driver as library Message-ID: References: <20231016-dwc3-refactor-v1-0-ab4a84165470@quicinc.com> <20231016-dwc3-refactor-v1-4-ab4a84165470@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231016-dwc3-refactor-v1-4-ab4a84165470@quicinc.com> X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 22 Nov 2023 03:57:30 -0800 (PST) On Mon, Oct 16, 2023 at 08:11:12PM -0700, Bjorn Andersson wrote: > The DWC3 IP block is handled by three distinct device drivers: XHCI, > DWC3 core and a platform specific (optional) DWC3 glue driver. > > This has resulted in, at least in the case of the Qualcomm glue, the > presence of a number of layering violations, where the glue code either > can't handle, or has to work around, the fact that core might not probe > deterministically. > > An example of this is that the suspend path should operate slightly > different depending on the device operating in host or peripheral mode, > and the only way to determine the operating state is to peek into the > core's drvdata. > > The Qualcomm glue driver is expected to make updates in the qscratch > register region (the "glue" region) during role switch events, but with > the glue and core split using the driver model, there is no reasonable > way to introduce listeners for mode changes. > > Split the dwc3 core platfrom_driver callbacks and their implementation > and export the implementation, to make it possible to deterministically > instantiate the dwc3 core as part of the dwc3 glue drivers and to > allow flattening of the DeviceTree representation. > > Signed-off-by: Bjorn Andersson > --- > drivers/usb/dwc3/core.c | 134 ++++++++++++++++++++++++++++++++---------------- > drivers/usb/dwc3/core.h | 10 ++++ > 2 files changed, 100 insertions(+), 44 deletions(-) > > diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c > index d25490965b27..71e376bebb16 100644 > --- a/drivers/usb/dwc3/core.c > +++ b/drivers/usb/dwc3/core.c > @@ -1876,7 +1876,7 @@ static int dwc3_get_clocks(struct dwc3 *dwc) > return 0; > } > > -static int dwc3_probe(struct platform_device *pdev) > +struct dwc3 *dwc3_probe(struct platform_device *pdev) Perhaps you should move allocation of struct dwc3 to the caller as well as you are going to need some way to pass in callback to core which need to be set before you register the xhci platform device. There could be other ways, like passing in a struct of callbacks, which can be added incrementally but it may be good think this through from the start. > { > struct device *dev = &pdev->dev; > struct resource *res, dwc_res; > @@ -1886,14 +1886,14 @@ static int dwc3_probe(struct platform_device *pdev) > > dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL); > if (!dwc) > - return -ENOMEM; > + return ERR_PTR(-ENOMEM); > > dwc->dev = dev; > > res = platform_get_resource(pdev, IORESOURCE_MEM, 0); > if (!res) { > dev_err(dev, "missing memory resource\n"); > - return -ENODEV; > + return ERR_PTR(-ENODEV); > } > > dwc->xhci_resources[0].start = res->start; > @@ -1922,7 +1922,7 @@ static int dwc3_probe(struct platform_device *pdev) > > regs = devm_ioremap_resource(dev, &dwc_res); > if (IS_ERR(regs)) > - return PTR_ERR(regs); > + return ERR_CAST(regs); > > dwc->regs = regs; > dwc->regs_size = resource_size(&dwc_res); > @@ -1953,7 +1953,6 @@ static int dwc3_probe(struct platform_device *pdev) > goto err_disable_clks; > } > > - platform_set_drvdata(pdev, dwc); This is broken however as the pm ops access the data driver data and can be called as soon as you call pm_runtime_put() below. > dwc3_cache_hwparams(dwc); > > if (!dwc->sysdev_is_parent && > @@ -2006,7 +2005,7 @@ static int dwc3_probe(struct platform_device *pdev) > > pm_runtime_put(dev); That is here. > - return 0; > + return dwc; > -static void dwc3_remove(struct platform_device *pdev) > +static int dwc3_plat_probe(struct platform_device *pdev) > { > - struct dwc3 *dwc = platform_get_drvdata(pdev); > + struct dwc3 *dwc; > + > + dwc = dwc3_probe(pdev); > + if (IS_ERR(dwc)) > + return PTR_ERR(dwc); And that leaves a window, for example, here where you can hit a NULL pointer dereference. > + platform_set_drvdata(pdev, dwc); > + > + return 0; > +} Johan