From 1373eaf5fd76e7c05b4c833089f14f18fdc101f1 Mon Sep 17 00:00:00 2001 From: Chris Gray Date: Wed, 11 Nov 2026 12:20:02 -0600 Subject: [PATCH 1/2] usb: gadget: f_ncm: pause aggregation retries during suspend Retry a timer flush when the USB request pool returns NETDEV_TX_BUSY. Track bus and function suspend through the function callbacks so a busy flush cannot rearm every 300 us while the host sleeps. Resume schedules one flush so an aggregation tail stopped by suspend can make progress. Assisted-by: LLM Signed-off-by: Chris Gray --- drivers/usb/gadget/function/f_ncm.c | 35 +++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c index 500c9c19c7..f683b3dc10 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -76,6 +76,7 @@ struct f_ncm { struct sk_buff *skb_tx_ndp; u16 ndp_dgram_count; struct hrtimer task_timer; + bool suspended; }; static inline struct f_ncm *func_to_ncm(struct usb_function *f) @@ -919,6 +920,7 @@ static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) net = gether_connect(&ncm->port); if (IS_ERR(net)) return PTR_ERR(net); + WRITE_ONCE(ncm->suspended, false); ncm->netdev = net; } @@ -1149,7 +1151,8 @@ static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data) struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer); struct net_device *netdev = READ_ONCE(ncm->netdev); - if (netdev) { + if (netdev && !READ_ONCE(ncm->suspended) && + !READ_ONCE(ncm->port.is_suspend)) { /* XXX This allowance of a NULL skb argument to ndo_start_xmit * XXX is not sane. The gadget layer should be redesigned so * XXX that the dev->wrap() invocations to build SKBs is transparent @@ -1158,11 +1161,37 @@ static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data) * * This will call directly into u_ether's eth_start_xmit() */ - netdev->netdev_ops->ndo_start_xmit(NULL, netdev); + /* Retry the buffered NTB when a USB request becomes available. */ + if (netdev->netdev_ops->ndo_start_xmit(NULL, netdev) == + NETDEV_TX_BUSY && !READ_ONCE(ncm->suspended) && + !READ_ONCE(ncm->port.is_suspend)) { + hrtimer_forward_now(data, TX_TIMEOUT_NSECS); + return HRTIMER_RESTART; + } } return HRTIMER_NORESTART; } +/* Stop aggregation retries for both bus and USB3 function suspend. */ +static void ncm_suspend(struct usb_function *f) +{ + struct f_ncm *ncm = func_to_ncm(f); + + WRITE_ONCE(ncm->suspended, true); + hrtimer_try_to_cancel(&ncm->task_timer); +} + +static void ncm_resume(struct usb_function *f) +{ + struct f_ncm *ncm = func_to_ncm(f); + + WRITE_ONCE(ncm->suspended, false); + /* A timer stopped by suspend may still have a buffered tail. */ + if (READ_ONCE(ncm->netdev)) + hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS, + HRTIMER_MODE_REL_SOFT); +} + static int ncm_unwrap_ntb(struct gether *port, struct sk_buff *skb, struct sk_buff_head *list) @@ -1798,6 +1827,8 @@ static struct usb_function *ncm_alloc(struct usb_function_instance *fi) ncm->port.func.get_alt = ncm_get_alt; ncm->port.func.setup = ncm_setup; ncm->port.func.disable = ncm_disable; + ncm->port.func.suspend = ncm_suspend; + ncm->port.func.resume = ncm_resume; ncm->port.func.free_func = ncm_free; ncm->port.wrap = ncm_wrap_ntb; From 5291303165f3c9c881caf1c29bbe5aa629b6579c Mon Sep 17 00:00:00 2001 From: Chris Gray Date: Wed, 11 Nov 2026 12:20:02 -0600 Subject: [PATCH 2/2] selftests: usb: exercise NCM aggregation timeout and suspend Extract the production callbacks into a host fixture to exercise success, repeated BUSY, suspend, suspend during transmit, resume and disconnect. Skip an installed selftest tree without sources unless KERNEL_SRC is set. The attended SSH transfer profile belongs in omarchy-mobile tooling. Assisted-by: LLM Signed-off-by: Chris Gray --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/usb_ncm/Makefile | 3 + .../testing/selftests/usb_ncm/ncm_timeout.py | 109 ++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/usb_ncm/Makefile create mode 100755 tools/testing/selftests/usb_ncm/ncm_timeout.py diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 273853937c..c955dd30f2 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -137,6 +137,7 @@ TARGETS += tpm2 TARGETS += tty TARGETS += ublk TARGETS += uevent +TARGETS += usb_ncm TARGETS += user_events TARGETS += vDSO TARGETS += mm diff --git a/tools/testing/selftests/usb_ncm/Makefile b/tools/testing/selftests/usb_ncm/Makefile new file mode 100644 index 0000000000..8ce41128c3 --- /dev/null +++ b/tools/testing/selftests/usb_ncm/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_PROGS := ncm_timeout.py +include ../lib.mk diff --git a/tools/testing/selftests/usb_ncm/ncm_timeout.py b/tools/testing/selftests/usb_ncm/ncm_timeout.py new file mode 100755 index 0000000000..2e961aacce --- /dev/null +++ b/tools/testing/selftests/usb_ncm/ncm_timeout.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +"""Compile the actual NCM callbacks with host stubs; no USB device needed.""" +from pathlib import Path +import os +import shutil +import subprocess +import tempfile + +root = Path(os.environ.get('KERNEL_SRC', Path(__file__).resolve().parents[4])) +source_file = root / 'drivers/usb/gadget/function/f_ncm.c' +if not source_file.is_file(): + print('SKIP: kernel sources unavailable; set KERNEL_SRC to the source tree') + raise SystemExit(4) +source = source_file.read_text() +cc = os.environ.get('HOSTCC', 'cc') +if shutil.which(cc) is None: + print('SKIP: host C compiler required') + raise SystemExit(4) + + +def function(signature): + return signature + source.split(signature, 1)[1].split('\n}', 1)[0] + '\n}\n' + + +stubs = r''' +#include +#include +#include +#include +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define container_of(p, t, m) ((t *)((char *)(p) - offsetof(t, m))) +#define NETDEV_TX_BUSY 16 +#define TX_TIMEOUT_NSECS 300000 +#define HRTIMER_MODE_REL_SOFT 0 +enum hrtimer_restart { HRTIMER_NORESTART, HRTIMER_RESTART }; +struct hrtimer { long forwarded; int armed; }; +struct net_device; +struct net_device_ops { int (*ndo_start_xmit)(void *, struct net_device *); }; +struct net_device { const struct net_device_ops *netdev_ops; }; +struct usb_function { int unused; }; +struct f_ncm { + struct { struct usb_function func; bool is_suspend; } port; + struct hrtimer task_timer; + struct net_device *netdev; + bool suspended; +}; +#define func_to_ncm(f) container_of(f, struct f_ncm, port.func) +static struct f_ncm *current; +static int result, attempts, suspend_in_xmit; +static int transmit(void *skb, struct net_device *dev) +{ + (void)skb; (void)dev; attempts++; + if (suspend_in_xmit) current->suspended = true; + return result; +} +static void hrtimer_forward_now(struct hrtimer *t, long ns) { t->forwarded += ns; } +static int hrtimer_try_to_cancel(struct hrtimer *t) { t->armed = 0; return 0; } +static void hrtimer_start(struct hrtimer *t, long ns, int mode) +{ (void)ns; (void)mode; t->armed = 1; } +''' +main = r''' +int main(void) +{ + const struct net_device_ops ops = { .ndo_start_xmit = transmit }; + struct net_device dev = { .netdev_ops = &ops }; + struct f_ncm ncm = { .netdev = &dev }; + current = &ncm; + result = 0; + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART); + assert(attempts == 1 && ncm.task_timer.forwarded == 0); + result = NETDEV_TX_BUSY; attempts = 0; + for (int i = 0; i < 10; i++) { + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_RESTART); + assert(attempts == i + 1); + assert(ncm.task_timer.forwarded == (i + 1) * TX_TIMEOUT_NSECS); + } + result = 0; + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART); + ncm_suspend(&ncm.port.func); attempts = 0; + assert(ncm.suspended && !ncm.task_timer.armed); + for (int i = 0; i < 100; i++) + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART); + assert(attempts == 0); + ncm_resume(&ncm.port.func); + assert(!ncm.suspended && ncm.task_timer.armed); + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART && attempts == 1); + ncm.port.is_suspend = true; attempts = 0; + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART && attempts == 0); + ncm.port.is_suspend = false; suspend_in_xmit = 1; result = NETDEV_TX_BUSY; + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART && attempts == 1); + suspend_in_xmit = 0; ncm.suspended = false; ncm.netdev = NULL; attempts = 0; + assert(ncm_tx_timeout(&ncm.task_timer) == HRTIMER_NORESTART && attempts == 0); + ncm.task_timer.armed = 0; ncm_resume(&ncm.port.func); + assert(!ncm.task_timer.armed); + puts("ok - busy retries, success, suspend, resume and disconnect"); + return 0; +} +''' +callbacks = ''.join(function(s) for s in ( + 'static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)', + 'static void ncm_suspend(struct usb_function *f)', + 'static void ncm_resume(struct usb_function *f)')) +with tempfile.TemporaryDirectory() as tmp: + p = Path(tmp) + (p / 'test.c').write_text(stubs + callbacks + main) + subprocess.run([cc, '-Wall', '-Wextra', '-Werror', str(p / 'test.c'), '-o', str(p / 'test')], check=True) + subprocess.run([str(p / 'test')], check=True)