mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: william@wkennington.com
Cc: oliver@neukum.org, andrew+netdev@lunn.ch, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	Jan.Kloetzke@preh.de, linux-usb@vger.kernel.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net] net: usb: cdc_ncm: don't flush the TX timer into a stopped queue
Date: Sat, 21 Nov 2026 23:02:49 +0000	[thread overview]
Message-ID: <179530216937.3135.2477795214247865968@kernel.org> (raw)
In-Reply-To: <20261118034122.315979-1-william@wkennington.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 0 (1) · Medium: 3 (1) · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The new retry branch in cdc_ncm_txpath_bh()
  (drivers/net/usb/cdc_ncm.c:1519-1528) can re-arm the TX hrtimer with…
- [Medium] The new 'the queue will be woken again' test in
  cdc_ncm_txpath_bh() looks only at dev->txq and dev->done.
- [Medium] cdc_ncm_txpath_bh() decides between keeping the frames and
  flushing them from lockless reads of netif_queue_stopped(),…
- [Low] The new comment in cdc_ncm_txpath_bh() (cdc_ncm.c:1515-1518) says
  the retry is based on TX URBs that are in flight or 'completed but not…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
  cdc_ncm_unbind() has a teardown race that can leave the TX hrtimer…
- [Medium] This problem wasn't introduced by this patch, but two other
  cdc_ncm paths still flush buffered TX frames with…

--

The six regression comments are below, written as an inline LKML reply to the patch. Four are about the new code. Two are marked pre-existing: an unbind/hrtimer use-after-free race (High), and two other flush paths with the same drop (Medium). Each comment is placed next to the part of the diff it refers to.

net: usb: cdc_ncm: don't flush the TX timer into a stopped queue

This commit changes cdc_ncm_txpath_bh() to re-arm the TX timer instead of
flushing buffered frames into usbnet_start_xmit() in one case: usbnet has
stopped the TX queue and TX URBs are still on dev->txq or dev->done. The
existing flush path is kept when nothing is queued, the interface is down,
or the timer is disabled.

> diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
> index e92dc7f011ea7..e5d659dd74369 100644
> --- a/drivers/net/usb/cdc_ncm.c
> +++ b/drivers/net/usb/cdc_ncm.c
> @@ -1509,9 +1509,26 @@ static void cdc_ncm_txpath_bh(struct tasklet_struct *t)
>  		cdc_ncm_tx_timeout_start(ctx);
>  		spin_unlock(&ctx->mtx);

[Severity: High]
This isn't a bug introduced by this patch, but is there a teardown race
between this re-arm in the tx_timer_pending branch and cdc_ncm_unbind()?

cdc_ncm_unbind() does:

	atomic_set(&ctx->stop, 1);

	hrtimer_cancel(&ctx->tx_timer);

	tasklet_kill(&ctx->bh);
	...
	cdc_ncm_free(ctx);

cdc_ncm_tx_timeout_start() checks the flag and arms the timer, and nothing
serializes that against unbind:

	if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
		hrtimer_start(&ctx->tx_timer,

A possible sequence:

CPU1 (tasklet, tx_timer_pending branch)
cdc_ncm_tx_timeout_start()
  reads stop == 0, timer inactive

CPU2
cdc_ncm_unbind()
  atomic_set(&ctx->stop, 1)
  hrtimer_cancel()    <- returns at once, timer is inactive
  tasklet_kill()      <- waits for CPU1

CPU1
  hrtimer_start(&ctx->tx_timer, ...)

CPU2
  cdc_ncm_free(ctx)   <- ctx freed with tx_timer still enqueued

Can cdc_ncm_tx_timer_cb() then run on freed memory? cdc_ncm_free() does
not cancel the timer again.

The new branch added by this patch can't take part in this.
usbnet_disconnect() calls unregister_netdev() before
driver_info->unbind(), and the new branch requires netif_running().

The race needs the pre-existing tx_timer_pending chain to still be running
at unbind. That is more likely with a large tx_timer_usecs.

Would taking ctx->mtx around atomic_set(&ctx->stop, 1), or cancelling the
timer again after tasklet_kill(), close this?

>  	} else if (dev->net != NULL) {
> -		ctx->tx_reason_timeout++;	/* count reason for transmitting */
>  		spin_unlock(&ctx->mtx);
>  		netif_tx_lock_bh(dev->net);
> +		spin_lock(&ctx->mtx);
> +		/* usbnet drops the whole NTB if its queue is stopped. While
> +		 * TX URBs are in flight, or completed but not yet reaped by
> +		 * usbnet_bh(), the queue will be woken again, so keep any
> +		 * buffered frames and retry a timer interval later.
> +		 */
> +		if (ctx->timer_interval &&
> +		    (ctx->tx_curr_skb || ctx->tx_rem_skb) &&
> +		    netif_running(dev->net) && netif_queue_stopped(dev->net) &&
> +		    (skb_queue_len_lockless(&dev->txq) ||
> +		     skb_queue_len_lockless(&dev->done))) {

[Severity: Low]
The comment above says the retry is based on TX URBs "completed but not
yet reaped by usbnet_bh()". However, dev->done is not TX-only. RX
completions go there too:

rx_complete()
  defer_bh(dev, skb, &dev->rxq, state)
    __skb_queue_tail(&dev->done, skb)

So a non-empty dev->done doesn't mean a TX completion is pending.

usbnet_bh() drains done quickly, so in practice this probably only adds a
few re-arms before the old flush. Should the condition, or the comment, be
changed so they agree, for example by checking only dev->txq?

[Severity: Medium]
Does this miss the runtime PM case? cdc_ncm_driver sets
.supports_autosuspend = 1. When EVENT_DEV_ASLEEP is set,
usbnet_start_xmit() does:

	usb_anchor_urb(urb, &dev->deferred);
	/* no use to process more packets */
	netif_stop_queue(net);

The URB goes on dev->deferred, not dev->txq. usbnet_suspend() has already
emptied txq and done through usbnet_terminate_urbs().

Suppose a TX burst to a suspended device fills one NTB and leaves a frame
in tx_rem_skb. The queue is then stopped with txq and done both at zero.

The cdc_ncm timer uses up its tx_timer_pending re-arms long before the USB
resume completes. At that point this condition is false, and the code
falls through to usbnet_start_xmit(NULL).

The NTB is then dropped at the stopped-queue check, which comes before the
EVENT_DEV_ASLEEP deferral:

	if (netif_queue_stopped(net)) {
		usb_autopm_put_interface_async(dev->intf);
		spin_unlock_irqrestore(&dev->txq.lock, flags);
		goto drop;
	}

usbnet_resume() later submits the deferred URBs and calls
netif_tx_wake_all_queues(), so this is also a state where the queue will
be woken.

Should !usb_anchor_empty(&dev->deferred) or
test_bit(EVENT_DEV_ASLEEP, &dev->flags) also count here? The EVENT_TX_HALT
recovery after a synchronous -EPIPE has a similar, rarer gap.

[Severity: Medium]
Can these lockless reads see txq and done both at zero while the queue is
stopped and about to be woken? If so, the code falls through and
usbnet_start_xmit() drops the NTB, which is the failure this patch is
meant to fix.

In defer_bh(), the last TX skb is unlinked from txq before it is added to
done:

	__skb_unlink(skb, list);
	...
	__skb_queue_tail(&dev->done, skb);

A reader on another CPU can see both lengths as zero in between.

In usbnet_bh(), done is drained first:

	while ((skb = skb_dequeue (&dev->done))) {

and the queue is only woken at the end:

	if (dev->txq.qlen < TX_QLEN (dev))
		netif_wake_queue(dev->net);

If every in-flight TX URB completed before the drain, the queue stays
stopped with txq and done both empty for that whole interval.

On -EPROTO, -ETIME or -EILSEQ, tx_complete() calls netif_stop_queue() and
arms dev->delay. usbnet_bh() won't wake the queue while
timer_pending(&dev->delay) is true.

Once the remaining URBs are reaped, txq and done are both zero, but the
queue stays stopped until the throttle timer fires. A cdc_ncm timer flush
in that window still drops the NTB.

Would it be more robust to make this decision under dev->txq.lock? Another
option is for usbnet to keep the NTB, rather than drop it, when the queue
is only flow-controlled.

> +			cdc_ncm_tx_timeout_start(ctx);

[Severity: Medium]
Is this re-arm bounded? The commit message says:

  Keep the existing flush path when nothing is queued to wake the device,
  the interface is down, or the timer is disabled (tx_timer_usecs = 0), so
  the timer cannot rearm indefinitely.

While the interface is up, the only exit is dev->txq or dev->done
draining.

Take a device that stops servicing bulk-OUT (it NAKs and the HCD keeps
retrying) and then sends NETWORK_CONNECTION=0:

cdc_ncm_status()
  usbnet_link_change(dev, 0, 0)
    netif_carrier_off()

After that, nothing appears to unlink the TX URBs. __handle_link_change()
only does:

	unlink_urbs(dev, &dev->rxq);

TX cleanup is left to usbnet_tx_timeout(), but dev_watchdog() only calls
ndo_tx_timeout when:

	if (netif_device_present(dev) &&
	    netif_running(dev) &&
	    netif_carrier_ok(dev)) {

usbnet_bh() also won't wake the queue while the carrier is off. So
timer_interval stays non-zero, the netdev stays running, the queue stays
stopped and txq stays non-empty.

The (ctx->tx_curr_skb || ctx->tx_rem_skb) test doesn't end this either,
because cdc_ncm_fill_tx_frame() can leave an NTB with zero datagrams in
tx_curr_skb:

	if (n == 0) {
		/* wait for more frames */
		/* push variables */
		ctx->tx_curr_skb = skb_out;
		goto exit_no_skb;

The tasklet would re-arm every timer_interval (400 us by default, 5 us at
the minimum) and take netif_tx_lock_bh() each time. That continues until
the device completes the URBs, the interface goes down, or the device is
unplugged.

Before this patch, the chain ended after at most
CDC_NCM_TIMER_PENDING_CNT + 1 firings. Even with the carrier on, the same
polling runs for up to the watchdog timeout before usbnet_tx_timeout()
unlinks txq.

Should there be a retry limit, or a netif_carrier_ok() check, here?

> +			spin_unlock(&ctx->mtx);
> +			netif_tx_unlock_bh(dev->net);
> +			return;
> +		}
> +		ctx->tx_reason_timeout++;
> +		spin_unlock(&ctx->mtx);
>  		usbnet_start_xmit(NULL, dev->net);

[Severity: Medium]
This isn't a bug introduced by this patch, but ndp_to_end_store() and
cdc_ncm_update_rxtx_max() flush the same way, with no stopped-queue check:

ndp_to_end_store()
	netif_tx_lock_bh(dev->net);
	usbnet_start_xmit(NULL, dev->net);

cdc_ncm_update_rxtx_max()
	if (netif_running(dev->net) && val > ctx->tx_max) {
		netif_tx_lock_bh(dev->net);
		usbnet_start_xmit(NULL, dev->net);

Suppose a sysfs write to ndp_to_end or tx_max, or an ethtool coalesce
change, happens while usbnet has the queue stopped. Wouldn't the flushed
NTB also be dropped at the netif_queue_stopped() check in
usbnet_start_xmit(), with tx_dropped incremented?

>  		netif_tx_unlock_bh(dev->net);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118034122.315979-1-william%40wkennington.com

      reply	other threads:[~2026-11-21 23:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  3:41 William A. Kennington III
2026-11-21 23:02 ` netdev-bot+sashiko [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=179530216937.3135.2477795214247865968@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=Jan.Kloetzke@preh.de \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=oliver@neukum.org \
    --cc=pabeni@redhat.com \
    --cc=william@wkennington.com \
    /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

all inboxes | Powered by JetHome®