Linux USB
 help / color / mirror / Atom feed
* [PATCH net v2] net: usb: cdc_ncm: send the NTB once another datagram would not fit
@ 2026-11-27 11:05 William A. Kennington III
  2026-11-27 11:10 ` sashiko-bot
  2026-11-28 11:07 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: William A. Kennington III @ 2026-11-27 11:05 UTC (permalink / raw)
  To: Oliver Neukum, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni
  Cc: linux-usb, netdev, linux-kernel, William A. Kennington III

Observed on BMCs in real use communicating with a USB NCM gadget over an
EHCI host controller, where bulk TCP traffic caused steady tx_dropped
growth and retransmits. Tracing the link with kprobes and the kfree_skb
tracepoint during iperf3 TCP runs confirmed that every drop came from
cdc_ncm_txpath_bh() calling usbnet_start_xmit(NULL, ...) into a stopped
TX queue.

cdc_ncm_fill_tx_frame() only learns that an NTB is full when it is given
a datagram that no longer fits. It then hands the NTB to usbnet and
parks that datagram in tx_rem_skb for the next NTB. If the stack sends
nothing more, only the TX timer is left to send it.

With 16 KiB NTBs usbnet allows 5 TX URBs in flight at high speed. When
the NTB that was just handed over is the fifth, usbnet stops the TX
queue. The timer then builds an NTB for the parked datagram and calls
usbnet_start_xmit(NULL, ...) directly, bypassing the qdisc. Since commit
ad70411a978d ("usbnet: fix kernel crash after disconnect")
usbnet_start_xmit() frees what it is given while the queue is stopped,
so the datagram is lost and counted in tx_dropped. A bulk TCP sender
hits this several times per second.

Send the NTB as soon as another datagram of the interface MTU, with a
VLAN tag, would not fit, provided no datagram is still parked in
tx_rem_skb. Nothing is parked on that path, so cdc_ncm holds no
datagram when usbnet stops the queue for a full TX queue, and the timer
has nothing to flush into it. An NTB also no longer waits for the next
datagram to arrive before it is sent.

tx_rem_skb is still used when a datagram needs a new NDP that does not
fit, as with several MBIM sessions.

The cost is that an NTB is closed while it could still take a datagram
smaller than the MTU. With 16 KiB NTBs and a 1500 byte MTU this does
not change the 10 full size frames per NTB.

Tested on an NPCM730 BMC (EHCI host, Linux 7.3.6 with BQL and fq_codel)
running 30 s iperf3 IPv6 TCP streams to an NCM gadget, same board
before and after:

                        tx_dropped  Mbit/s (before -> after)
  1 stream,  fwd        106 -> 0    119.8 -> 141.0
  4 streams, fwd        128 -> 0    164.3 -> 172.4
  1 stream,  bidir       51 -> 0     39.5/118.3 -> 73.0/107.7
  4 streams, bidir       62 -> 0     36.7/150.3 -> 78.0/130.2
  4 streams, MSS 512     81 -> 0    129.2 -> 152.8

Fixes: ad70411a978d ("usbnet: fix kernel crash after disconnect")
Signed-off-by: William A. Kennington III <william@wkennington.com>
---
v2:
  - Replace the timer re-arm polling from v1 with closing the NTB as
    soon as another full-MTU (plus VLAN) datagram would not fit, so no
    datagram is parked in tx_rem_skb when usbnet stops the TX queue.
  - Add real-hardware discovery/trace validation and same-board Linux
    7.3.6 (with BQL and fq_codel) test results. Mainline usbnet BQL
    still hits these drops because DQL's limit exceeds the 5-URB
    (80 KiB) TX ring cap.
v1: https://lore.kernel.org/r/20261118034122.315979-1-william@wkennington.com

 drivers/net/usb/cdc_ncm.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
index e92dc7f011ea..7fbce4846a04 100644
--- a/drivers/net/usb/cdc_ncm.c
+++ b/drivers/net/usb/cdc_ncm.c
@@ -43,6 +43,7 @@
 #include <linux/ctype.h>
 #include <linux/etherdevice.h>
 #include <linux/ethtool.h>
+#include <linux/if_vlan.h>
 #include <linux/kstrtox.h>
 #include <linux/workqueue.h>
 #include <linux/mii.h>
@@ -1369,6 +1370,22 @@ cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
 			ctx->tx_reason_ndp_full++;	/* count reason for transmitting */
 			break;
 		}
+
+		/* Send now if another full size datagram would not fit.
+		 * Accepting one more would park it in tx_rem_skb after
+		 * this NTB has been handed to usbnet. If that NTB fills
+		 * the TX queue, usbnet stops the queue and drops the NTB
+		 * the timer builds for the parked datagram.
+		 */
+		if (!ctx->tx_rem_skb && n + 1 < ctx->tx_max_datagrams &&
+		    skb_out->len + ctx->tx_modulus + ctx->tx_remainder +
+		    READ_ONCE(dev->net->mtu) + cdc_ncm_eth_hlen(dev) +
+		    VLAN_HLEN + delayed_ndp_size > ctx->tx_curr_size) {
+			ready2send = 1;
+			ctx->tx_reason_ntb_full++;	/* count reason for transmitting */
+			n++;
+			break;
+		}
 	}
 
 	/* free up any dangling skb */
-- 
2.56.1.839.g22a8290db9-goog


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

* Re: [PATCH net v2] net: usb: cdc_ncm: send the NTB once another datagram would not fit
  2026-11-27 11:05 [PATCH net v2] net: usb: cdc_ncm: send the NTB once another datagram would not fit William A. Kennington III
@ 2026-11-27 11:10 ` sashiko-bot
  2026-11-28 11:07 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-27 11:10 UTC (permalink / raw)
  To: William A. Kennington III; +Cc: linux-usb

> Observed on BMCs in real use communicating with a USB NCM gadget over an
> EHCI host controller, where bulk TCP traffic caused steady tx_dropped
> growth and retransmits. Tracing the link with kprobes and the kfree_skb
> tracepoint during iperf3 TCP runs confirmed that every drop came from
> cdc_ncm_txpath_bh() calling usbnet_start_xmit(NULL, ...) into a stopped
> TX queue.
> 
> cdc_ncm_fill_tx_frame() only learns that an NTB is full when it is given
> a datagram that no longer fits. It then hands the NTB to usbnet and
> parks that datagram in tx_rem_skb for the next NTB. If the stack sends
> nothing more, only the TX timer is left to send it.
> 
> With 16 KiB NTBs usbnet allows 5 TX URBs in flight at high speed. When
> the NTB that was just handed over is the fifth, usbnet stops the TX
> queue. The timer then builds an NTB for the parked datagram and calls
> [ ... ]
>   4 streams, bidir       62 -> 0     36.7/150.3 -> 78.0/130.2
>   4 streams, MSS 512     81 -> 0    129.2 -> 152.8
> 
> Fixes: ad70411a978d ("usbnet: fix kernel crash after disconnect")
> Signed-off-by: William A. Kennington III <william@wkennington.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127110521.1235409-1-william@wkennington.com?part=1

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

* Re: [PATCH net v2] net: usb: cdc_ncm: send the NTB once another datagram would not fit
  2026-11-27 11:05 [PATCH net v2] net: usb: cdc_ncm: send the NTB once another datagram would not fit William A. Kennington III
  2026-11-27 11:10 ` sashiko-bot
@ 2026-11-28 11:07 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 11:07 UTC (permalink / raw)
  To: william
  Cc: oliver, andrew+netdev, davem, edumazet, kuba, pabeni, linux-usb,
	netdev, linux-kernel

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

Critical: 0 · High: 1 · Medium: 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).

- [High] The patch only narrows one way that a datagram ends up parked in
  ctx->tx_rem_skb.
- [Medium] Other usbnet queue stops cause the same drop, and a heuristic
  based on NTB size cannot prevent them.
- [Low] The new early-close check in cdc_ncm_fill_tx_frame()
  (drivers/net/usb/cdc_ncm.c:1382-1383) adds VLAN_HLEN to every reserved…

--

net: usb: cdc_ncm: send the NTB once another datagram would not fit

With this patch, cdc_ncm_fill_tx_frame() sends the current NTB as soon
as another MTU-sized datagram (with Ethernet and VLAN headers) would no
longer fit, as long as nothing is parked in tx_rem_skb. The goal is
that no datagram is held in tx_rem_skb when usbnet stops the TX queue,
so the timer flush in cdc_ncm_txpath_bh() no longer drops it into a
stopped queue.

> diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
> index e92dc7f011ea7..7fbce4846a044 100644
> --- a/drivers/net/usb/cdc_ncm.c
> +++ b/drivers/net/usb/cdc_ncm.c

[ ... ]

> @@ -1369,6 +1370,22 @@ cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
>  			ctx->tx_reason_ndp_full++;	/* count reason for transmitting */
>  			break;
>  		}
> +
> +		/* Send now if another full size datagram would not fit.
> +		 * Accepting one more would park it in tx_rem_skb after
> +		 * this NTB has been handed to usbnet. If that NTB fills
> +		 * the TX queue, usbnet stops the queue and drops the NTB
> +		 * the timer builds for the parked datagram.
> +		 */
> +		if (!ctx->tx_rem_skb && n + 1 < ctx->tx_max_datagrams &&
> +		    skb_out->len + ctx->tx_modulus + ctx->tx_remainder +
> +		    READ_ONCE(dev->net->mtu) + cdc_ncm_eth_hlen(dev) +
> +		    VLAN_HLEN + delayed_ndp_size > ctx->tx_curr_size) {

[Severity: High]
Does this check cover every way a datagram can still be parked in
ctx->tx_rem_skb? It only reserves room for a same-signature datagram of
up to mtu + eth_hlen + VLAN_HLEN. The "no room for skb - store for
later" branch in cdc_ncm_fill_tx_frame() still looks reachable:

	ctx->tx_rem_skb = skb;
	ctx->tx_rem_sign = sign;
	skb = NULL;
	ready2send = 1;

Here are a few cases where that seems to happen.

MBIM with several IPS/DSS sessions, without NDP_TO_END. The first
datagram of a new session needs NDP alignment plus max_ndp_size, which
cdc_ncm_ndp16() and cdc_ncm_ndp32() check:

	if (skb->len + reserve + ctx->max_ndp_size > ctx->tx_curr_size)
		return NULL;

The new check doesn't reserve that space, so an MTU-sized datagram for
a new session can still be parked.

Devices with CDC_NCM_FLAG_NDP_TO_END, such as cdc_mbim_info_ndp_to_end
or any device with the ndp_to_end sysfs flag set. cdc_ncm_ndp16() and
cdc_ncm_ndp32() return NULL for any second signature, whatever the free
space:

	else if (ctx->delayed_ndp16->dwSignature)
		return NULL;

The comment above cdc_mbim_info_ndp_to_end says multiplexed sessions
cannot share an NTB. So each session switch parks a datagram.

Frames longer than mtu + ETH_HLEN + VLAN_HLEN on plain cdc_ncm. cdc_ncm
doesn't set NETIF_F_HW_VLAN_CTAG_TX. Stacked 802.1ad/802.1Q devices
inherit the real device's MTU, so they send inline frames of mtu + 22
bytes. Frames queued before an MTU decrease also qualify, because
READ_ONCE(dev->net->mtu) returns the new, smaller value. These pass the
new check and then fail the real fit check, which uses skb->len.

In each case, the NTB returned with ready2send = 1 can be the one that
brings txq.qlen up to TX_QLEN in usbnet_start_xmit():

	if (dev->txq.qlen >= TX_QLEN (dev))
		netif_stop_queue (net);

The hrtimer then fires, and the following runs:

cdc_ncm_txpath_bh()
  usbnet_start_xmit(NULL, dev->net)
    info->tx_fixup()    /* builds an NTB from the parked datagram */
    netif_queue_stopped(net) -> goto drop

Isn't this the same tx_dropped path that the commit message and the
Fixes tag describe? cdc_mbim and huawei_cdc_ncm share this code.

The commit message also says:

  tx_rem_skb is still used when a datagram needs a new NDP that does not
  fit, as with several MBIM sessions.

For NDP_TO_END devices the datagram is parked whether or not it fits.
Is "does not fit" accurate there?

Would it be more robust to fix this in the flush path? One option is to
check netif_queue_stopped() in cdc_ncm_txpath_bh() under netif_tx_lock
and re-arm the timer instead of flushing. Another is to keep usbnet
from consuming tx_fixup(NULL) data while the queue is stopped.

[Severity: Low]
Should VLAN_HLEN be added for MBIM? cdc_ncm_eth_hlen() returns 0 for
MBIM because the datagrams are raw L3. cdc_mbim sets
NETIF_F_HW_VLAN_CTAG_TX. Also, cdc_mbim_tx_fixup() strips the L2 header
before calling cdc_ncm_fill_tx_frame():

	if (vlan_get_tag(skb, &tci) < 0 && skb->len > VLAN_ETH_HLEN &&
	    __vlan_get_tag(skb, &tci) == 0) {
		...
		skb_pull(skb, VLAN_ETH_HLEN);
	} else {
		...
		skb_pull(skb, ETH_HLEN);
	}

So the largest MBIM datagram is mtu, but this check reserves mtu + 4.
Inside a 4-byte window, an MBIM NTB gets closed even though another
full-size datagram would still fit.

Would adding VLAN_HLEN only when cdc_ncm_eth_hlen(dev) is non-zero
match the header model that the helper encodes?

> +			ready2send = 1;
> +			ctx->tx_reason_ntb_full++;	/* count reason for transmitting */
> +			n++;
> +			break;
> +		}
>  	}

[Severity: Medium]
Can other kinds of queue stop still lose datagrams through the same
flush path? For -EPROTO, -ETIME and -EILSEQ, tx_complete() stops the
queue and arms dev->delay for THROTTLE_JIFFIES (HZ/8). EHCI hosts can
report these errors:

	case -EPROTO:
	case -ETIME:
	case -EILSEQ:
		...
		netif_stop_queue(dev->net);
		break;

usbnet_start_xmit() also stops the queue on -EPIPE and for
EVENT_DEV_ASLEEP.

After this patch, cdc_ncm_fill_tx_frame() still holds partially filled
NTBs in ctx->tx_curr_skb with the hrtimer armed. The flush interval is
about 400 us plus a few re-arms through tx_timer_pending. That is much
shorter than the 125 ms throttle window, so
cdc_ncm_txpath_bh()->usbnet_start_xmit(NULL) would build an NTB from
every held datagram and drop it at the netif_queue_stopped() check.

The commit message says "every drop came from cdc_ncm_txpath_bh()
calling usbnet_start_xmit(NULL, ...) into a stopped TX queue". That
diagnosis and the Fixes tag cover these stops too, since ad70411a978d
added the stopped-queue drop for every stop reason. This patch only
addresses stops caused by a full TX queue.

Would a flush-path fix that re-arms the timer instead of flushing into
a stopped queue cover these cases as well?

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

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

end of thread, other threads:[~2026-11-28 11:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 11:05 [PATCH net v2] net: usb: cdc_ncm: send the NTB once another datagram would not fit William A. Kennington III
2026-11-27 11:10 ` sashiko-bot
2026-11-28 11:07 ` netdev-bot+sashiko

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