All of lore.kernel.org
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Bjorn Andersson <quic_bjorande@quicinc.com>
Cc: Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konrad.dybcio@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Wesley Cheng <quic_wcheng@quicinc.com>,
	Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	Felipe Balbi <balbi@kernel.org>,
	Philipp Zabel <p.zabel@pengutronix.de>,
	linux-arm-msm@vger.kernel.org, linux-usb@vger.kernel.org,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Krishna Kurapati PSSNV <quic_kriskura@quicinc.com>
Subject: Re: [PATCH 04/12] usb: dwc3: Expose core driver as library
Date: Wed, 22 Nov 2023 12:57:37 +0100	[thread overview]
Message-ID: <ZV3ssSP5dTwAs-e3@hovoldconsulting.com> (raw)
In-Reply-To: <20231016-dwc3-refactor-v1-4-ab4a84165470@quicinc.com>

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 <quic_bjorande@quicinc.com>
> ---
>  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

  parent reply	other threads:[~2023-11-22 11:57 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-17  3:11 [PATCH 00/12] usb: dwc3: qcom: Flatten dwc3 structure Bjorn Andersson
2023-10-17  3:11 ` [PATCH 01/12] dt-bindings: usb: qcom,dwc3: Add qcom,sc8180x-dwc3 Bjorn Andersson
2023-10-17  6:03   ` Krzysztof Kozlowski
2023-10-17  3:11 ` [PATCH 02/12] usb: dwc3: qcom: Rename dwc3 platform_device reference Bjorn Andersson
2023-10-17 16:08   ` Konrad Dybcio
2023-11-22  9:58   ` Johan Hovold
2023-10-17  3:11 ` [PATCH 03/12] usb: dwc3: qcom: Merge resources from urs_usb device Bjorn Andersson
2023-10-20  6:02   ` kernel test robot
2023-11-22 10:24   ` Johan Hovold
2024-01-08 16:25     ` Bjorn Andersson
2023-10-17  3:11 ` [PATCH 04/12] usb: dwc3: Expose core driver as library Bjorn Andersson
2023-10-20 22:18   ` Thinh Nguyen
2023-11-22 11:57   ` Johan Hovold [this message]
2024-01-08 16:42     ` Bjorn Andersson
2023-10-17  3:11 ` [PATCH 05/12] usb: dwc3: Override end of dwc3 memory resource Bjorn Andersson
2023-10-17 16:14   ` Konrad Dybcio
2023-10-20 22:07   ` Thinh Nguyen
2023-10-17  3:11 ` [PATCH 06/12] usb: dwc3: qcom: Add dwc3 core reference in driver state Bjorn Andersson
2023-11-22 12:18   ` Johan Hovold
2024-01-08 18:02     ` Bjorn Andersson
2023-10-17  3:11 ` [PATCH 07/12] usb: dwc3: qcom: Instantiate dwc3 core directly Bjorn Andersson
2023-11-22 12:23   ` Johan Hovold
2024-01-10  3:16   ` Krishna Kurapati PSSNV
2023-10-17  3:11 ` [PATCH 08/12] usb: dwc3: qcom: Inline the qscratch constants Bjorn Andersson
2023-10-17 16:18   ` Konrad Dybcio
2023-10-17  3:11 ` [PATCH 09/12] dt-bindings: usb: qcom,dwc3: Rename to "glue" Bjorn Andersson
2023-10-17  6:05   ` Krzysztof Kozlowski
2023-11-22 12:25   ` Johan Hovold
2023-10-17  3:11 ` [PATCH 10/12] dt-bindings: usb: qcom,dwc3: Introduce flattened qcom,dwc3 binding Bjorn Andersson
2023-10-17  6:11   ` Krzysztof Kozlowski
2023-10-17 22:54     ` Bjorn Andersson
2023-10-18  6:00       ` Krzysztof Kozlowski
2023-10-17 18:31   ` Rob Herring
2023-10-17 21:02     ` Bjorn Andersson
2023-11-22 12:40   ` Johan Hovold
2023-10-17  3:11 ` [PATCH 11/12] usb: dwc3: qcom: Flatten the Qualcomm dwc3 binding and implementation Bjorn Andersson
2024-01-10  3:13   ` Krishna Kurapati PSSNV
2024-01-10 19:23     ` Bjorn Andersson
2023-10-17  3:11 ` [PATCH 12/12] arm64: dts: qcom: sc8180x: flatten usb_sec node Bjorn Andersson
2023-10-20 22:04 ` [PATCH 00/12] usb: dwc3: qcom: Flatten dwc3 structure Thinh Nguyen
2023-11-22  9:48 ` Johan Hovold
2024-01-08 16:46   ` Bjorn Andersson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZV3ssSP5dTwAs-e3@hovoldconsulting.com \
    --to=johan@kernel.org \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=andersson@kernel.org \
    --cc=balbi@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=konrad.dybcio@linaro.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=p.zabel@pengutronix.de \
    --cc=quic_bjorande@quicinc.com \
    --cc=quic_kriskura@quicinc.com \
    --cc=quic_wcheng@quicinc.com \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.