Linux wireless drivers development
 help / color / mirror / Atom feed
From: Bin Luo via B4 Relay <devnull+luob87709.gmail.com@kernel.org>
To: Stanislaw Gruszka <stf_xl@wp.pl>
Cc: Johannes Berg <johannes@sipsolutions.net>,
	 linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH wireless v3 2/2] wifi: rt2x00: prevent TX_STA_FIFO URB resubmission on error and teardown
Date: Fri, 04 Dec 2026 14:06:49 +0800	[thread overview]
Message-ID: <20261204060647.476-3-luob87709@gmail.com> (raw)
In-Reply-To: <20261204060647.476-1-luob87709@gmail.com>

From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

Two TX_STA_FIFO polling issues remain on URB failure or if a TX queue
fails to drain before rt2x00usb_uninitialize():

1. In rt2800usb_tx_sta_fifo_read_completed(), a non-zero urb_status (such
   as -ENOENT from usb_kill_anchored_urbs() or a bus error on disconnect)
   jumps to stop_reading: and runs the rt2800_txstatus_pending() race
   recheck meant for normal completion.  When TX status is still pending,
   it sets TX_STATUS_READING again and returns true to resubmit the
   failed URB; if usb_submit_urb() then fails (e.g. -EPERM while killing
   anchored URBs), TX_STATUS_READING stays stuck set.

2. In rt2x00usb_uninitialize(), usb_kill_anchored_urbs() runs before
   hrtimer_cancel(&rt2x00dev->txstatus_timer) and cancel_work_sync() on
   txdone_work, allowing txstatus_timer to fire after anchored URBs were
   killed and submit a fresh async TX_STA_FIFO URB.

Clear TX_STATUS_READING and return false directly when urb_status is
non-zero, guard TX_STA_FIFO timer/URB rearming with
DEVICE_STATE_INITIALIZED, and cancel txstatus_timer before
usb_kill_anchored_urbs() as well as after draining txdone_work.

Fixes: f421111b5e69 ("rt2x00: rt2800usb: rework txstatus code")
Fixes: 6232c17438ed ("rt2x00: avoid introducing a USB dependency in the rt2x00lib module")
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
Changes in v3:
- Resend cleanly via b4 web relay (v2 was line-wrapped by Gmail);
  no code changes from v2.

Changes in v2:
- Split into a separate patch with its own description per Stanislaw
  Gruszka's request.
- Clear TX_STATUS_READING and return false directly when urb_status != 0
  in rt2800usb_tx_sta_fifo_read_completed() instead of jumping into the
  stop_reading: race-window recheck.

Validation:
- scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks
- W=1 KASAN+LOCKDEP build of rt2800usb.o and rt2x00usb.o:
  0 warnings, 0 errors
- QEMU KVM (v7.4-rc4, KASAN+LOCKDEP) tested both standalone (when Queue 0
  fails to flush) and together with patch 1/2 across sysfs unbind and
  physical USB disconnect: 0 killed-URB resubmissions, 0 warnings,
  0 KASAN, 0 Lockdep.

 drivers/net/wireless/ralink/rt2x00/rt2800usb.c | 16 ++++++++++++----
 drivers/net/wireless/ralink/rt2x00/rt2x00usb.c |  8 +++++++-
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/drivers/net/wireless/ralink/rt2x00/rt2800usb.c b/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
index 15de3db..0327785 100644
--- a/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
+++ b/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
@@ -99,8 +99,8 @@ static bool rt2800usb_tx_sta_fifo_read_completed(struct rt2x00_dev *rt2x00dev,
 	if (urb_status) {
 		rt2x00_warn(rt2x00dev, "TX status read failed %d\n",
 			    urb_status);
-
-		goto stop_reading;
+		clear_bit(TX_STATUS_READING, &rt2x00dev->flags);
+		return false;
 	}
 
 	valid = rt2x00_get_field32(tx_status, TX_STA_FIFO_VALID);
@@ -118,7 +118,8 @@ static bool rt2800usb_tx_sta_fifo_read_completed(struct rt2x00_dev *rt2x00dev,
 	if (rt2800_txstatus_timeout(rt2x00dev))
 		queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
 
-	if (rt2800_txstatus_pending(rt2x00dev)) {
+	if (rt2800_txstatus_pending(rt2x00dev) &&
+	    test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags)) {
 		/* Read register after 1 ms */
 		hrtimer_start(&rt2x00dev->txstatus_timer,
 			      TXSTATUS_READ_INTERVAL,
@@ -126,7 +127,6 @@ static bool rt2800usb_tx_sta_fifo_read_completed(struct rt2x00_dev *rt2x00dev,
 		return false;
 	}
 
-stop_reading:
 	clear_bit(TX_STATUS_READING, &rt2x00dev->flags);
 	/*
 	 * There is small race window above, between txstatus pending check and
@@ -134,6 +134,7 @@ stop_reading:
 	 * here again if status reading is needed.
 	 */
 	if (rt2800_txstatus_pending(rt2x00dev) &&
+	    test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags) &&
 	    !test_and_set_bit(TX_STATUS_READING, &rt2x00dev->flags))
 		return true;
 	else
@@ -142,6 +143,8 @@ stop_reading:
 
 static void rt2800usb_async_read_tx_status(struct rt2x00_dev *rt2x00dev)
 {
+	if (!test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
+		return;
 
 	if (test_and_set_bit(TX_STATUS_READING, &rt2x00dev->flags))
 		return;
@@ -164,6 +167,11 @@ static enum hrtimer_restart rt2800usb_tx_sta_fifo_timeout(struct hrtimer *timer)
 	struct rt2x00_dev *rt2x00dev =
 	    container_of(timer, struct rt2x00_dev, txstatus_timer);
 
+	if (!test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags)) {
+		clear_bit(TX_STATUS_READING, &rt2x00dev->flags);
+		return HRTIMER_NORESTART;
+	}
+
 	rt2x00usb_register_read_async(rt2x00dev, TX_STA_FIFO,
 				      rt2800usb_tx_sta_fifo_read_completed);
 
diff --git a/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c b/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
index c9fbc37..8d77cd5 100644
--- a/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
+++ b/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
@@ -745,10 +745,16 @@ void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
 {
 	struct data_queue *queue;
 
-	usb_kill_anchored_urbs(rt2x00dev->anchor);
+	/*
+	 * Cancel txstatus_timer before killing anchored TX_STA_FIFO URBs so
+	 * the timer cannot submit new URBs afterwards, then cancel again
+	 * after draining txdone_work in case an earlier callback rearmed it.
+	 */
 	hrtimer_cancel(&rt2x00dev->txstatus_timer);
+	usb_kill_anchored_urbs(rt2x00dev->anchor);
 	cancel_work_sync(&rt2x00dev->rxdone_work);
 	cancel_work_sync(&rt2x00dev->txdone_work);
+	hrtimer_cancel(&rt2x00dev->txstatus_timer);
 
 	queue_for_each(rt2x00dev, queue)
 		rt2x00usb_free_entries(queue);
-- 
2.43.0




      parent reply	other threads:[~2026-12-04  6:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  6:06 [PATCH wireless v3 0/2] wifi: rt2x00: fix TX queue flush and TX_STA_FIFO teardown Bin Luo via B4 Relay
2026-12-04  6:06 ` [PATCH wireless v3 1/2] wifi: rt2x00: set DEVICE_STATE_FLUSHING in rt2x00queue_flush_queues() Bin Luo via B4 Relay
2026-12-04  6:06 ` Bin Luo via B4 Relay [this message]

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=20261204060647.476-3-luob87709@gmail.com \
    --to=devnull+luob87709.gmail.com@kernel.org \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=luob87709@gmail.com \
    --cc=stf_xl@wp.pl \
    /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