mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] wifi: rt2x00: prevent txstatus_timer rearm during USB uninitialize
@ 2026-11-26  6:51 Bin Luo (University of Electronic Science and Technology of China)
  2026-12-01 16:39 ` Stanislaw Gruszka
  2026-12-02  6:06 ` Bin Luo (University of Electronic Science and Technology of China)
  0 siblings, 2 replies; 3+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-26  6:51 UTC (permalink / raw)
  To: stf_xl, johannes; +Cc: linux-wireless, linux-kernel

rt2x00usb_uninitialize() cancels rt2x00dev->txstatus_timer before
calling cancel_work_sync(&rt2x00dev->txdone_work). Because
rt2800usb_work_txdone() calls rt2800usb_async_read_tx_status() and
rearms txstatus_timer, the timer can remain armed after
rt2x00usb_uninitialize() frees queue entries via
rt2x00usb_free_entries(), allowing rt2800usb_tx_sta_fifo_timeout() to
submit new async register-read URBs and queue txdone_work on torn-down
queues.

Because rt2x00lib_uninitialize() clears DEVICE_STATE_INITIALIZED before
calling rt2x00usb_uninitialize(), guard txstatus_timer rearming and
timer-driven async register reads with DEVICE_STATE_INITIALIZED and
cancel txstatus_timer after cancel_work_sync(&rt2x00dev->txdone_work).

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>
---
Found by code inspection. Compile-tested on x86_64 with W=1 and KASAN (drivers/net/wireless/ralink/rt2x00/rt2800usb.o and rt2x00usb.o); not runtime-tested on Ralink USB hardware.

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

diff --git a/drivers/net/wireless/ralink/rt2x00/rt2800usb.c b/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
index 15de3db..57e04c6 100644
--- a/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
+++ b/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
@@ -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,
@@ -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..bfd5a81 100644
--- a/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
+++ b/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
@@ -746,9 +746,9 @@ void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
 	struct data_queue *queue;
 
 	usb_kill_anchored_urbs(rt2x00dev->anchor);
-	hrtimer_cancel(&rt2x00dev->txstatus_timer);
 	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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] wifi: rt2x00: prevent txstatus_timer rearm during USB uninitialize
  2026-11-26  6:51 [PATCH] wifi: rt2x00: prevent txstatus_timer rearm during USB uninitialize Bin Luo (University of Electronic Science and Technology of China)
@ 2026-12-01 16:39 ` Stanislaw Gruszka
  2026-12-02  6:06 ` Bin Luo (University of Electronic Science and Technology of China)
  1 sibling, 0 replies; 3+ messages in thread
From: Stanislaw Gruszka @ 2026-12-01 16:39 UTC (permalink / raw)
  To: Bin Luo (University of Electronic Science and Technology of China)
  Cc: johannes, linux-wireless, linux-kernel

Hi,

On Thu, Nov 26, 2026 at 02:51:32PM +0800, Bin Luo (University of Electronic Science and Technology of China) wrote:
> rt2x00usb_uninitialize() cancels rt2x00dev->txstatus_timer before
> calling cancel_work_sync(&rt2x00dev->txdone_work). Because
> rt2800usb_work_txdone() calls rt2800usb_async_read_tx_status() and

This is under condition:

  if (rt2800_txstatus_pending(rt2x00dev))

which should be false at this point, or I missed something?

Regards
Stanislaw

> rearms txstatus_timer, the timer can remain armed after
> rt2x00usb_uninitialize() frees queue entries via
> rt2x00usb_free_entries(), allowing rt2800usb_tx_sta_fifo_timeout() to
> submit new async register-read URBs and queue txdone_work on torn-down
> queues.
> 
> Because rt2x00lib_uninitialize() clears DEVICE_STATE_INITIALIZED before
> calling rt2x00usb_uninitialize(), guard txstatus_timer rearming and
> timer-driven async register reads with DEVICE_STATE_INITIALIZED and
> cancel txstatus_timer after cancel_work_sync(&rt2x00dev->txdone_work).
> 
> 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>
> ---
> Found by code inspection. Compile-tested on x86_64 with W=1 and KASAN (drivers/net/wireless/ralink/rt2x00/rt2800usb.o and rt2x00usb.o); not runtime-tested on Ralink USB hardware.
> 
>  drivers/net/wireless/ralink/rt2x00/rt2800usb.c | 10 +++++++++-
>  drivers/net/wireless/ralink/rt2x00/rt2x00usb.c |  2 +-
>  2 files changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/wireless/ralink/rt2x00/rt2800usb.c b/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
> index 15de3db..57e04c6 100644
> --- a/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
> +++ b/drivers/net/wireless/ralink/rt2x00/rt2800usb.c
> @@ -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,
> @@ -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..bfd5a81 100644
> --- a/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
> +++ b/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
> @@ -746,9 +746,9 @@ void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
>  	struct data_queue *queue;
>  
>  	usb_kill_anchored_urbs(rt2x00dev->anchor);
> -	hrtimer_cancel(&rt2x00dev->txstatus_timer);
>  	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
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] wifi: rt2x00: prevent txstatus_timer rearm during USB uninitialize
  2026-11-26  6:51 [PATCH] wifi: rt2x00: prevent txstatus_timer rearm during USB uninitialize Bin Luo (University of Electronic Science and Technology of China)
  2026-12-01 16:39 ` Stanislaw Gruszka
@ 2026-12-02  6:06 ` Bin Luo (University of Electronic Science and Technology of China)
  1 sibling, 0 replies; 3+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-12-02  6:06 UTC (permalink / raw)
  To: stf_xl, johannes; +Cc: linux-wireless, linux-kernel

(Resending in plain text so it reaches the mailing lists.)

Hi Stanislaw,

On Wed, Dec 2, 2026 at 12:39 AM, Stanislaw Gruszka wrote:
> On Thu, Nov 26, 2026 at 02:51:32PM +0800, Bin Luo (University of Electronic Science and Technology of China) wrote:
> > rt2x00usb_uninitialize() cancels rt2x00dev->txstatus_timer before
> > calling cancel_work_sync(&rt2x00dev->txdone_work). Because
> > rt2800usb_work_txdone() calls rt2800usb_async_read_tx_status() and
>
> This is under condition:
>
>   if (rt2800_txstatus_pending(rt2x00dev))
>
> which should be false at this point, or I missed something?

Thanks for the review!

I tested this on an unmodified v7.4-rc4 kernel in QEMU using dummy_hcd +
raw_gadget emulating an RT2872 USB device (with kprobe tracing), and
rt2800_txstatus_pending(rt2x00dev) does indeed remain true when
rt2x00usb_uninitialize() runs.

Here is why it stays true on both sysfs unbind and physical USB disconnect:

1. rt2x00lib_remove_dev() clears DEVICE_STATE_PRESENT and calls
   rt2x00lib_disable_radio() -> rt2x00queue_flush_queues(rt2x00dev, true) ->
   rt2x00usb_flush_queue(queue, true).
2. Unlike rt2x00mac_flush(), rt2x00lib_disable_radio() does not set
   DEVICE_STATE_FLUSHING (and rt2x00mac_flush() returns early once
   DEVICE_STATE_PRESENT is cleared).
3. Without DEVICE_STATE_FLUSHING, rt2800_entry_txstatus_timeout() uses the
   2000 ms timeout, whereas rt2x00usb_flush_queue() only loops
   10 * msleep(50) = 500 ms before returning with "Queue 0 failed to flush".
4. Any TX entry whose bulk-OUT URB already completed within ~1.5 s without
   a TX_STA_FIFO report (or even a bulk-OUT URB cancelled by
   rt2x00usb_flush_entry() with ENTRY_DATA_IO_FAILED set, since
   rt2800_txstatus_timeout() does not check ENTRY_DATA_IO_FAILED) is not
   drained during the 500 ms flush, leaving Q_INDEX_DMA_DONE != Q_INDEX_DONE
   (rt2800_txstatus_pending() == true) when rt2x00usb_uninitialize() runs.

Here is the observed trace on unmodified v7.4-rc4 during sysfs unbind right
after a TX bulk-OUT URB completes without a TX_STA_FIFO entry:

  [   31.778597] ieee80211 phy0: rt2x00queue_flush_queue: Warning - Queue 0 failed to flush
  [   31.780458] ieee80211 phy0: rt2800usb_tx_sta_fifo_read_completed: Warning - TX status read failed -2

  rt2800usb_repro-84 [003] ... 31.778300: rt_flush_out:     (rt2x00queue_flush_queue <- rt2x00usb_flush_queue)
         pr/ttyS0-42 [000] ... 31.779332: rt_timer_cb:      (rt2800usb_tx_sta_fifo_timeout)
         pr/ttyS0-42 [000] ... 31.779341: rt_read_async:    (rt2x00usb_register_read_async)
  rt2800usb_repro-84 [003] ... 31.779845: rt_uninit_in:     (rt2x00usb_uninitialize)
  rt2800usb_repro-84 [003] ... 31.779913: rt_kill_urbs:     (usb_kill_anchored_urbs)
  rt2800usb_repro-85 [000] ... 31.780198: rt_fifo_cb_in:    (rt2800usb_tx_sta_fifo_read_completed) urb_status=-2
  rt2800usb_repro-85 [000] ... 31.780531: rt_pending_ret:   (rt2800usb_tx_sta_fifo_read_completed+0x171 <- rt2800_txstatus_pending) ret=0x1
  rt2800usb_repro-85 [000] ... 31.780567: rt_fifo_cb_out:   (rt2x00usb_register_read_async_cb <- rt2800usb_tx_sta_fifo_read_completed) ret=0x1
  rt2800usb_repro-84 [003] ... 31.781440: rt_kill_urbs_out: (rt2x00usb_uninitialize <- usb_kill_anchored_urbs)

Runtime testing also highlighted two additional details worth addressing in v2:
- At stop_reading: in rt2800usb_tx_sta_fifo_read_completed(), when
  usb_kill_anchored_urbs() cancels the URB with -ENOENT, stop_reading: sees
  rt2800_txstatus_pending() == true, re-sets TX_STATUS_READING, and returns
  true to resubmit the killed URB (which fails with -EPERM and leaves
  TX_STATUS_READING stuck set, e.g. across suspend/resume).
- In rt2x00usb_uninitialize(), usb_kill_anchored_urbs() runs before
  hrtimer_cancel(&rt2x00dev->txstatus_timer), so a timer firing in between
  can submit a fresh async TX_STA_FIFO URB after usb_kill_anchored_urbs()
  has returned.

Would you prefer that I send a v2 that also guards stop_reading: with
DEVICE_STATE_INITIALIZED and kills anchored URBs after stopping
txdone_work/txstatus_timer, or would you also like rt2x00lib_disable_radio() /
rt2800_txstatus_timeout() adjusted to flush pending TX entries faster?

Best regards,
Bin Luo

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02  6:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  6:51 [PATCH] wifi: rt2x00: prevent txstatus_timer rearm during USB uninitialize Bin Luo (University of Electronic Science and Technology of China)
2026-12-01 16:39 ` Stanislaw Gruszka
2026-12-02  6:06 ` Bin Luo (University of Electronic Science and Technology of China)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®