* [PATCH v2] usb: roles: set switch registered flag early on
@ 2025-02-06 19:39 Elson Roy Serrao
2025-02-13 11:25 ` Heikki Krogerus
0 siblings, 1 reply; 2+ messages in thread
From: Elson Roy Serrao @ 2025-02-06 19:39 UTC (permalink / raw)
To: gregkh, heikki.krogerus, xu.yang_2
Cc: linux-usb, linux-kernel, Elson Roy Serrao, stable
The role switch registration and set_role() can happen in parallel as they
are invoked independent of each other. There is a possibility that a driver
might spend significant amount of time in usb_role_switch_register() API
due to the presence of time intensive operations like component_add()
which operate under common mutex. This leads to a time window after
allocating the switch and before setting the registered flag where the set
role notifications are dropped. Below timeline summarizes this behavior
Thread1 | Thread2
usb_role_switch_register() |
| |
---> allocate switch |
| |
---> component_add() | usb_role_switch_set_role()
| | |
| | --> Drop role notifications
| | since sw->registered
| | flag is not set.
| |
--->Set registered flag.|
To avoid this, set the registered flag early on in the switch register
API.
Fixes: b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered")
cc: stable@vger.kernel.org
Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
---
Changes in v2:
- Set the switch registered flag from the get-go as suggested by
Heikki.
- Modified subject line and commit next as per the new logic.
- Link to v1: https://lore.kernel.org/all/20250127230715.6142-1-quic_eserrao@quicinc.com/
drivers/usb/roles/class.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
index c58a12c147f4..30482d4cf826 100644
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -387,8 +387,11 @@ usb_role_switch_register(struct device *parent,
dev_set_name(&sw->dev, "%s-role-switch",
desc->name ? desc->name : dev_name(parent));
+ sw->registered = true;
+
ret = device_register(&sw->dev);
if (ret) {
+ sw->registered = false;
put_device(&sw->dev);
return ERR_PTR(ret);
}
@@ -399,8 +402,6 @@ usb_role_switch_register(struct device *parent,
dev_warn(&sw->dev, "failed to add component\n");
}
- sw->registered = true;
-
/* TODO: Symlinks for the host port and the device controller. */
return sw;
--
2.17.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] usb: roles: set switch registered flag early on
2025-02-06 19:39 [PATCH v2] usb: roles: set switch registered flag early on Elson Roy Serrao
@ 2025-02-13 11:25 ` Heikki Krogerus
0 siblings, 0 replies; 2+ messages in thread
From: Heikki Krogerus @ 2025-02-13 11:25 UTC (permalink / raw)
To: Elson Roy Serrao; +Cc: gregkh, xu.yang_2, linux-usb, linux-kernel, stable
On Thu, Feb 06, 2025 at 11:39:50AM -0800, Elson Roy Serrao wrote:
> The role switch registration and set_role() can happen in parallel as they
> are invoked independent of each other. There is a possibility that a driver
> might spend significant amount of time in usb_role_switch_register() API
> due to the presence of time intensive operations like component_add()
> which operate under common mutex. This leads to a time window after
> allocating the switch and before setting the registered flag where the set
> role notifications are dropped. Below timeline summarizes this behavior
>
> Thread1 | Thread2
> usb_role_switch_register() |
> | |
> ---> allocate switch |
> | |
> ---> component_add() | usb_role_switch_set_role()
> | | |
> | | --> Drop role notifications
> | | since sw->registered
> | | flag is not set.
> | |
> --->Set registered flag.|
>
> To avoid this, set the registered flag early on in the switch register
> API.
>
> Fixes: b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered")
> cc: stable@vger.kernel.org
> Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
> Changes in v2:
> - Set the switch registered flag from the get-go as suggested by
> Heikki.
> - Modified subject line and commit next as per the new logic.
> - Link to v1: https://lore.kernel.org/all/20250127230715.6142-1-quic_eserrao@quicinc.com/
>
> drivers/usb/roles/class.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
> index c58a12c147f4..30482d4cf826 100644
> --- a/drivers/usb/roles/class.c
> +++ b/drivers/usb/roles/class.c
> @@ -387,8 +387,11 @@ usb_role_switch_register(struct device *parent,
> dev_set_name(&sw->dev, "%s-role-switch",
> desc->name ? desc->name : dev_name(parent));
>
> + sw->registered = true;
> +
> ret = device_register(&sw->dev);
> if (ret) {
> + sw->registered = false;
> put_device(&sw->dev);
> return ERR_PTR(ret);
> }
> @@ -399,8 +402,6 @@ usb_role_switch_register(struct device *parent,
> dev_warn(&sw->dev, "failed to add component\n");
> }
>
> - sw->registered = true;
> -
> /* TODO: Symlinks for the host port and the device controller. */
>
> return sw;
> --
> 2.17.1
--
heikki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-02-13 11:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-06 19:39 [PATCH v2] usb: roles: set switch registered flag early on Elson Roy Serrao
2025-02-13 11:25 ` Heikki Krogerus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox