Skip to content

usb: gadget: Fix net_device lifecycles and configfs races

Kuen-Han Tsai linux-usb lkml stable 1 version New

Get series with b4
b4 am [email protected] change-id 20260320-usb-net-lifecycle-cd5911d7e013

Versions newest first

  1. v1 Latest7 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7usb: gadget: f_subset: Fix unbalanced refcnt in geth_freedrivers/usb/gadget/function/f_subset.c+6 -00 replies
2/7usb: gadget: f_rndis: Protect RNDIS options with mutexdrivers/usb/gadget/function/f_rndis.c+6 -30 replies
3/7usb: gadget: u_ncm: Add kernel-doc comments for struct f_ncm_optsdrivers/usb/gadget/function/u_ncm.h+15 -60 replies
4/7usb: gadget: f_ecm: Fix net_device lifecycle with device_movedrivers/usb/gadget/function/f_ecm.c drivers/usb/gadget/function/u_ecm.h+39 -190 replies
5/7usb: gadget: f_eem: Fix net_device lifecycle with device_movedrivers/usb/gadget/function/f_eem.c drivers/usb/gadget/function/u_eem.h+46 -340 replies
6/7usb: gadget: f_subset: Fix net_device lifecycle with device_movedrivers/usb/gadget/function/f_subset.c drivers/usb/gadget/function/u_gether.h+44 -350 replies
7/7usb: gadget: f_rndis: Fix net_device lifecycle with device_movedrivers/usb/gadget/function/f_rndis.c drivers/usb/gadget/function/u_rndis.h+48 -250 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/7] usb: gadget: Fix net_device lifecycles and configfs races

Hi all,

This patch series addresses dangling sysfs symlink issues across the remaining USB gadget network functions (f_ecm, f_eem, f_subset, and f_rndis). It also includes configfs concurrency and reference-counting bug fixes identified during the investigation.

Commit ec35c1969650 ("usb: gadget: f_ncm: Fix net_device lifecycle with device_move") introduced an architectural fix for f_ncm to resolve an issue where unbinding the function resulted in the net_device surviving the destruction of its gadget parent device. This lifecycle mismatch leaves dangling sysfs symlinks:

console:/ # ls -l /sys/class/net/usb0
lrwxrwxrwx ... /sys/class/net/usb0 -> /sys/devices/platform/.../gadget.0/net/usb0 console:/ # ls -l /sys/devices/platform/.../gadget.0/net/usb0
ls: .../gadget.0/net/usb0: No such file or directory

This series applies the same device_move() pattern to the rest of the USB network functions. Temporarily moving the net_device to /sys/devices/virtual during unbind, and re-parenting it back to the new gadget device during bind, restores proper sysfs topology without leaking references or breaking legacy composite drivers (like multi.c).

To ensure safe teardown and avoid regressing legacy composite drivers that manually pre-register net_devices, the bound and borrowed_net flags now strictly indicate if the device was shared and pre-registered during the legacy driver's bind phase.

Patch Summary:
- Patches 1-2: Fix an unbalanced reference count in f_subset and add a missing configfs mutex lock for RNDIS options.
- Patch 3: Add comprehensive kernel-doc descriptions for struct f_ncm_opts to match the standard applied to the other functions.
- Patches 4-7: Implement the device_move() lifecycle fix consistently across f_ecm, f_eem, f_subset, and f_rndis.

---
Kuen-Han Tsai (7):
usb: gadget: f_subset: Fix unbalanced refcnt in geth_free
usb: gadget: f_rndis: Protect RNDIS options with mutex
usb: gadget: u_ncm: Add kernel-doc comments for struct f_ncm_opts
usb: gadget: f_ecm: Fix net_device lifecycle with device_move
usb: gadget: f_eem: Fix net_device lifecycle with device_move
usb: gadget: f_subset: Fix net_device lifecycle with device_move
usb: gadget: f_rndis: Fix net_device lifecycle with device_move

 drivers/usb/gadget/function/f_ecm.c    | 37 +++++++++++++-------
 drivers/usb/gadget/function/f_eem.c    | 59 ++++++++++++++++---------------
 drivers/usb/gadget/function/f_rndis.c  | 51 ++++++++++++++++-----------
 drivers/usb/gadget/function/f_subset.c | 63 +++++++++++++++++++---------------
 drivers/usb/gadget/function/u_ecm.h    | 21 ++++++++----
 drivers/usb/gadget/function/u_eem.h    | 21 ++++++++----
 drivers/usb/gadget/function/u_gether.h | 22 ++++++++----
 drivers/usb/gadget/function/u_ncm.h    | 21 ++++++++----
 drivers/usb/gadget/function/u_rndis.h  | 31 ++++++++++++-----
 9 files changed, 204 insertions(+), 122 deletions(-)

---
base-commit: f50200dd44125e445a6164e88c217472fa79cdbc
change-id: 20260320-usb-net-lifecycle-cd5911d7e013

Best regards,

Signature · 1 line
Kuen-Han Tsai <[email protected]>