HID: nintendo-switch2-usb: Fix potential deadlock on teardown
Subsystems (2)
Vicki Pfau
maintainerPATCH v2 1/3 HID: nintendo-switch2-usb: Fix potential deadlock on teardown
As reported by Sashiko, there is a narrow window in which cancel_work_sync on the message in work can block due to the work waiting on the mutex that the canceling thread holds. By moving the pointer clearing and unlocking the mutex before calling cfg->shutdown we can avoid this deadlock.
--- drivers/hid/hid-nintendo.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/hid/hid-nintendo.c +5 -3
@@ -4292,6 +4292,7 @@ int switch2_controller_attach_cfg(const char *phys, struct switch2_cfg_intf *cfgmutex_lock (&ns2->lock);if (ns2->cfg) { ret = -EBUSY ; +mutex_unlock (&ns2->lock); goto out; } cfg->parent = ns2;
@@ -4301,15 +4302,16 @@ int switch2_controller_attach_cfg(const char *phys, struct switch2_cfg_intf *cfg ret =switch2_init_controller (ns2);if (ret <0 ) { + ns2->cfg =NULL ; +mutex_unlock (&ns2->lock);if (cfg->shutdown) cfg->shutdown (cfg); cfg->parent =NULL ; - ns2->cfg =NULL ; + }else { +mutex_unlock (&ns2->lock); } out: -mutex_unlock (&ns2->lock); -if (ret <0 )kref_put (&ns2->refcount, switch2_kref_put);return ret;
Signature · 1 line
2.54.0
Vicki Pfau
maintainerPATCH v2 2/3 HID: nintendo-switch2-usb: Improve shutdown logic with inbound message work
When detaching the USB interface the driver marks the interface as shutdown to prevent work that may be executing in parallel from attempting to use the URBs before freeing the URBs. There is theoretically a very narrow window in which the work could be scheduled while the shutdown is running in a separate thread, starting to execute before the shutdown cancels the work.
By ensuring the scheduling happens within the locked scope and adding an additional shutdown check, this closes the window entirely.
--- drivers/hid/hid-nintendo-switch2-usb.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
drivers/hid/hid-nintendo-switch2-usb.c +6 -10
@@ -52,12 +52,10 @@ static void switch2_bulk_in(struct urb *urb) {struct switch2_usb *ns2_usb = urb->context;int i; -bool schedule = false;unsigned long flags;switch (urb->status) {case 0 : - schedule = true;break ;case -ECONNRESET :case -ENOENT :
@@ -71,16 +69,14 @@ static void switch2_bulk_in(struct urb *urb) }spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags); -if (ns2_usb->shutdown) - schedule = false; -for (i =0 ; i <NS2_IN_URBS ; i++) {int err;struct switch2_urb *ns2_urb;if (ns2_usb->bulk_in[i].urb == urb) { -if (schedule) { +if (urb->status ==0 && !ns2_usb->shutdown) { ns2_usb->bulk_in[i].state =NS2_URB_IN ; +schedule_work (&ns2_usb->message_in_work);continue ; }else { ns2_usb->bulk_in[i].state =NS2_URB_FREE ;
@@ -115,9 +111,6 @@ static void switch2_bulk_in(struct urb *urb) } }spin_unlock_irqrestore (&ns2_usb->bulk_in_lock, flags); - -if (schedule) -schedule_work (&ns2_usb->message_in_work); }static void switch2_bulk_out (struct urb *urb)
@@ -295,6 +288,9 @@ static void switch2_usb_message_in_work(struct work_struct *work)spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags);for (i =0 ; i <NS2_IN_URBS ; i++) { +if (ns2_usb->shutdown) +break ; + urb = &ns2_usb->bulk_in[i];if (urb->state !=NS2_URB_IN )continue ;
@@ -311,7 +307,7 @@ static void switch2_usb_message_in_work(struct work_struct *work) }spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags); -if (!urb->urb) +if (ns2_usb->shutdown || !urb->urb)/* We're shutting down and this URB was freed */ break ;
Signature · 1 line
2.54.0
Vicki Pfau
maintainerPATCH v2 3/3 HID: nintendo-switch2-usb: Clean up urb in probe error path
In reply to Vicki PfauIn switch2_usb_probe the error path if the initial urb submission fails forgets to unanchor the urb. This patch adds that in.
--- drivers/hid/hid-nintendo-switch2-usb.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/hid/hid-nintendo-switch2-usb.c +2 -0
@@ -427,6 +427,8 @@ static int switch2_usb_probe(struct usb_interface *intf, const struct usb_device ns2_usb->bulk_in[0 ].state =NS2_URB_OUT ;usb_anchor_urb (ns2_usb->bulk_in[0 ].urb, &ns2_usb->bulk_in_anchor); ret =usb_submit_urb (ns2_usb->bulk_in[0 ].urb,GFP_ATOMIC ); +if (ret <0 ) +usb_unanchor_urb (ns2_usb->bulk_in[0 ].urb);spin_unlock_irqrestore (&ns2_usb->bulk_in_lock, flags);if (ret <0 )
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 3/3] HID: nintendo-switch2-usb: Clean up urb in probe error path
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] HID: nintendo-switch2-usb: missing Fixes trailer for switch2_usb_probe() fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] HID: nintendo-switch2-usb: missing Fixes trailer for switch2_usb_probe() fix
--
commit e3ee270bfbbf6fa6ea6a7562052781a9e5556f6b
Author: Vicki Pfau <[email protected]>
Quoted 8 lines
HID: nintendo-switch2-usb: Clean up urb in probe error path
In
switch2_usb_probethe error path if the initial urb submission fails forgets to unanchor the urb. This patch adds that in.
[Severity: Low]
Since this fixes a probe error path bug, should it include a Fixes tag?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Jiri Kosina
maintainerQuoted 9 lines · Vicki Pfau
As reported by Sashiko, there is a narrow window in which
cancel_work_syncon the message in work can block due to the work waiting on the mutex that the canceling thread holds. By moving the pointer clearing and unlocking the mutex before callingcfg->shutdownwe can avoid this deadlock.Reported-by Sashiko <[email protected]>Fixes 9417132df63e ("HID: nintendo: Add preliminary Switch 2 controller driver")Signed-off-by Vicki Pfau <[email protected]>
The series applied to hid.git#for-7.4/upstream-fixes, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs