Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions drivers/usb/gadget/function/f_ncm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ TARGETS += tpm2
TARGETS += tty
TARGETS += ublk
TARGETS += uevent
TARGETS += usb_ncm
TARGETS += user_events
TARGETS += vDSO
TARGETS += mm
Expand Down
3 changes: 3 additions & 0 deletions tools/testing/selftests/usb_ncm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0
TEST_PROGS := ncm_timeout.py
include ../lib.mk
109 changes: 109 additions & 0 deletions tools/testing/selftests/usb_ncm/ncm_timeout.py
Original file line number Diff line number Diff line change
@@ -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 <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#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)