]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
usb: roles: set switch registered flag early on
authorElson Roy Serrao <quic_eserrao@quicinc.com>
Thu, 6 Feb 2025 19:39:50 +0000 (11:39 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 13 Mar 2025 11:50:37 +0000 (12:50 +0100)
commit6a902573f2ca7e40fc8714724cf6f0aff0fbb57f
tree748ffeaed96e27da3092964675566e8e1e39cb58
parentf4ca0cf536b13f34b5c581ef85266879b30bf8ec
usb: roles: set switch registered flag early on

commit 634775a752a86784511018a108f3b530cc3399a7 upstream.

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 <stable@kernel.org>
Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Link: https://lore.kernel.org/r/20250206193950.22421-1-quic_eserrao@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/roles/class.c