Re: [PATCH 1/2] usb: dwc3: core: Call cpu_relax() in registers polling busy loops
From: Thinh Nguyen
Date: Tue Aug 20 2024 - 18:06:18 EST
On Tue, Aug 20, 2024, Zhongqiu Han wrote:
> Busy loops that poll on a register should call cpu_relax(). On some
> architectures, it can lower CPU power consumption or yield to a
> hyperthreaded twin processor. It also serves as a compiler barrier,
> see Documentation/process/volatile-considered-harmful.rst. In addition,
> if something goes wrong in the busy loop at least it can prevent things
> from getting worse.
>
> Signed-off-by: Zhongqiu Han <quic_zhonhan@xxxxxxxxxxx>
> ---
> drivers/usb/dwc3/core.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 734de2a8bd21..498f08dbbdb5 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -2050,6 +2050,8 @@ static int dwc3_get_num_ports(struct dwc3 *dwc)
> if (!offset)
> break;
>
> + cpu_relax();
> +
> val = readl(base + offset);
> major_revision = XHCI_EXT_PORT_MAJOR(val);
>
> --
> 2.25.1
>
We're not polling on a register here. We're just traversing and reading
the next port capability. The loop in dwc3_get_num_ports() should not be
more than DWC3_USB2_MAX_PORTS + DWC3_USB3_MAX_PORTS.
What's really causing this busy loop you found?
If polling for a register is really a problem, then we would have that
problem everywhere else in dwc3. But why here?
Thanks,
Thinh