Skip to content

usb: gadget: Refactor NCM net_device lifecycle

Kuen-Han Tsai linux-tegra linux-usb lkml 1 version In review

Get series with b4
b4 am [email protected] change-id 20251230-ncm-refactor-988f9a6d4591

Versions newest first

  1. v1 Latest3 patches 5 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3usb: gadget: u_ether: add gether_opts for config cachingdrivers/usb/gadget/function/u_ether.c drivers/usb/gadget/function/u_ether.h drivers/usb/gadget/function/u_ether_configfs.h+234 -00 replies
2/3usb: gadget: u_ether: Add auto-cleanup helper for freeing net_devicedrivers/usb/gadget/function/u_ether.c drivers/usb/gadget/function/u_ether.h+17 -00 replies
3/3usb: gadget: f_ncm: align net_device lifecycle with bind/unbinddrivers/usb/gadget/function/f_ncm.c drivers/usb/gadget/function/u_ncm.h+66 -665 replies

Reviews trailers per patch

None of the 3 patches has a Reviewed-by or Acked-by.

PatchErnest Van Hoecke
1/3
2/3
3/3T-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/3] usb: gadget: Refactor NCM net_device lifecycle

Refactor the network device lifecycle management within the USB gadget NCM function and the underlying u_ether library.

The primary goal is to align the net_device lifetime with the USB function's bind/unbind events rather than the function instance allocation/deallocation. This resolves issues with dangling sysfs symbolic links and potential use-after-free scenarios when the gadget is disconnected, as the net_device could previously outlive its parent gadget device.

Other gadget function drivers using u_ether (e.g., ECM, EEM, RNDIS) may have similar net_device lifecycle mismatches. While not addressed in this series, they could potentially benefit from a similar refactoring in the future.

The series is structured as follows:

1. u_ether: add gether_opts for config caching: Introduce a new struct gether_opts to cache network settings (MAC addresses, ifname, etc.) from configfs independently of the net_device. This allows deferring net_device creation to the bind phase without losing pre-configuration capabilities.

2. u_ether: Add auto-cleanup helper for freeing net_device: Introduce gether_unregister_free_netdev() and DEFINE_FREE to support auto-cleanup for net_device within the bind path, preparing for net_device allocation changes in f_ncm.

3. f_ncm: align net_device lifecycle with bind/unbind: The main patch that moves net_device allocation/registration into ncm_bind() and unregistration/freeing into ncm_unbind(). It utilizes the gether_opts for applying cached settings and the new cleanup helper.

---
Kuen-Han Tsai (3):
usb: gadget: u_ether: add gether_opts for config caching
usb: gadget: u_ether: Add auto-cleanup helper for freeing net_device
usb: gadget: f_ncm: align net_device lifecycle with bind/unbind

 drivers/usb/gadget/function/f_ncm.c            | 128 +++++++++---------
 drivers/usb/gadget/function/u_ether.c          |  45 +++++++
 drivers/usb/gadget/function/u_ether.h          |  30 +++++
 drivers/usb/gadget/function/u_ether_configfs.h | 176 +++++++++++++++++++++++++
 drivers/usb/gadget/function/u_ncm.h            |   4 +-
 5 files changed, 317 insertions(+), 66 deletions(-)

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251230-ncm-refactor-988f9a6d4591

Best regards,

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