From: Vicki Pfau <vi@endrift.com>
To: Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>,
linux-input@vger.kernel.org
Cc: Vicki Pfau <vi@endrift.com>,
Noah Weinberger <noahweinberger2003@proton.me>
Subject: [PATCH v2 2/3] HID: nintendo-switch2-usb: Improve shutdown logic with inbound message work
Date: Tue, 10 Nov 2026 22:49:55 -0800 [thread overview]
Message-ID: <20261111065001.3626071-2-vi@endrift.com> (raw)
In-Reply-To: <20261111065001.3626071-1-vi@endrift.com>
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.
Reported-by: Noah Weinberger <noahweinberger2003@proton.me>
Link: https://lore.kernel.org/linux-input/a2dc00e7-9cab-4315-87a4-795f256c1e03@endrift.com/T/
Fixes: 9417132df63e ("HID: nintendo: Add preliminary Switch 2 controller driver")
Signed-off-by: Vicki Pfau <vi@endrift.com>
---
drivers/hid/hid-nintendo-switch2-usb.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/drivers/hid/hid-nintendo-switch2-usb.c b/drivers/hid/hid-nintendo-switch2-usb.c
index 8b3b6841035d..f53cbf0a3e15 100644
--- a/drivers/hid/hid-nintendo-switch2-usb.c
+++ b/drivers/hid/hid-nintendo-switch2-usb.c
@@ -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;
--
2.54.0
next prev parent reply other threads:[~2026-11-11 6:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 6:49 [PATCH v2 1/3] HID: nintendo-switch2-usb: Fix potential deadlock on teardown Vicki Pfau
2026-11-11 6:49 ` Vicki Pfau [this message]
2026-11-11 6:49 ` [PATCH v2 3/3] HID: nintendo-switch2-usb: Clean up urb in probe error path Vicki Pfau
2026-11-11 7:24 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261111065001.3626071-2-vi@endrift.com \
--to=vi@endrift.com \
--cc=bentiss@kernel.org \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=noahweinberger2003@proton.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox