[1/2] usb: misc: onboard_hub: Invert driver registration order

Message ID 20230105230119.1.I75494ebee7027a50235ce4b1e930fa73a578fbe2@changeid
State New
Headers
Series [1/2] usb: misc: onboard_hub: Invert driver registration order |

Commit Message

Matthias Kaehlcke Jan. 5, 2023, 11:03 p.m. UTC
  The onboard_hub 'driver' consists of two drivers, a platform
driver and a USB driver. Currently when the onboard hub driver
is initialized it first registers the platform driver, then the
USB driver. This results in a race condition when the 'attach'
work is executed, which is scheduled when the platform device
is probed. The purpose of fhe 'attach' work is to bind elegible
USB hub devices to the onboard_hub USB driver. This fails if
the work runs before the USB driver has been registered.

Register the USB driver first, then the platform driver. This
increases the chances that the onboard_hub USB devices are probed
before their corresponding platform device, which the USB driver
tries to locate in _probe(). The driver already handles this
situation and defers probing if the onboard hub platform device
doesn't exist yet.

Cc: stable@vger.kernel.org
Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver")
Link: https://lore.kernel.org/lkml/Y6W00vQm3jfLflUJ@hovoldconsulting.com/T/#m0d64295f017942fd988f7c53425db302d61952b4
Reported-by: Alexander Stein <alexander.stein@ew.tq-group.com>
Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---

 drivers/usb/misc/onboard_usb_hub.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Greg KH Jan. 6, 2023, 3:32 p.m. UTC | #1
On Thu, Jan 05, 2023 at 11:03:28PM +0000, Matthias Kaehlcke wrote:
> The onboard_hub 'driver' consists of two drivers, a platform
> driver and a USB driver. Currently when the onboard hub driver
> is initialized it first registers the platform driver, then the
> USB driver. This results in a race condition when the 'attach'
> work is executed, which is scheduled when the platform device
> is probed. The purpose of fhe 'attach' work is to bind elegible
> USB hub devices to the onboard_hub USB driver. This fails if
> the work runs before the USB driver has been registered.
> 
> Register the USB driver first, then the platform driver. This
> increases the chances that the onboard_hub USB devices are probed
> before their corresponding platform device, which the USB driver
> tries to locate in _probe(). The driver already handles this
> situation and defers probing if the onboard hub platform device
> doesn't exist yet.
> 
> Cc: stable@vger.kernel.org
> Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver")
> Link: https://lore.kernel.org/lkml/Y6W00vQm3jfLflUJ@hovoldconsulting.com/T/#m0d64295f017942fd988f7c53425db302d61952b4
> Reported-by: Alexander Stein <alexander.stein@ew.tq-group.com>
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
> 
>  drivers/usb/misc/onboard_usb_hub.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

Does this superseed this thread:
	Link: https://lore.kernel.org/r/20221222022605.v2.1.If5e7ec83b1782e4dffa6ea759416a27326c8231d@changeid

or is that also needed?

confused,

greg k-h
  
Matthias Kaehlcke Jan. 6, 2023, 4:13 p.m. UTC | #2
On Fri, Jan 06, 2023 at 04:32:52PM +0100, Greg Kroah-Hartman wrote:
> On Thu, Jan 05, 2023 at 11:03:28PM +0000, Matthias Kaehlcke wrote:
> > The onboard_hub 'driver' consists of two drivers, a platform
> > driver and a USB driver. Currently when the onboard hub driver
> > is initialized it first registers the platform driver, then the
> > USB driver. This results in a race condition when the 'attach'
> > work is executed, which is scheduled when the platform device
> > is probed. The purpose of fhe 'attach' work is to bind elegible
> > USB hub devices to the onboard_hub USB driver. This fails if
> > the work runs before the USB driver has been registered.
> > 
> > Register the USB driver first, then the platform driver. This
> > increases the chances that the onboard_hub USB devices are probed
> > before their corresponding platform device, which the USB driver
> > tries to locate in _probe(). The driver already handles this
> > situation and defers probing if the onboard hub platform device
> > doesn't exist yet.
> > 
> > Cc: stable@vger.kernel.org
> > Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver")
> > Link: https://lore.kernel.org/lkml/Y6W00vQm3jfLflUJ@hovoldconsulting.com/T/#m0d64295f017942fd988f7c53425db302d61952b4
> > Reported-by: Alexander Stein <alexander.stein@ew.tq-group.com>
> > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > ---
> > 
> >  drivers/usb/misc/onboard_usb_hub.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> Does this superseed this thread:
> 	Link: https://lore.kernel.org/r/20221222022605.v2.1.If5e7ec83b1782e4dffa6ea759416a27326c8231d@changeid

This series ("usb: misc: onboard_hub: Invert driver registration order"
et al) fixes the race condition mentioned in the commit message
of the other series ("usb: misc: onboard_usb_hub: Don't create platform
devices for DT nodes without 'vdd-supply'" et al), plus another race
that was reported later (this patch).

> or is that also needed?

This series is (mostly) independent from the other one, it should
fix the issue that was reported for the RPi 3 B+. It can be landed
even if the other one is abandonded.

With this series the other one doesn't fix or mitigate any actual
issue (AFAIK), it would only be an optimization (don't instantiate
the onboard_hub drivers if they'd do nothing).

> confused

Sorry, hope this clarifies things a bit.
  

Patch

diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c
index 94e7966e199d..db0844b30bbd 100644
--- a/drivers/usb/misc/onboard_usb_hub.c
+++ b/drivers/usb/misc/onboard_usb_hub.c
@@ -433,13 +433,13 @@  static int __init onboard_hub_init(void)
 {
 	int ret;
 
-	ret = platform_driver_register(&onboard_hub_driver);
+	ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
 	if (ret)
 		return ret;
 
-	ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
+	ret = platform_driver_register(&onboard_hub_driver);
 	if (ret)
-		platform_driver_unregister(&onboard_hub_driver);
+		usb_deregister_device_driver(&onboard_hub_usbdev_driver);
 
 	return ret;
 }