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

Kuen-Han Tsai posted 3 patches 1 month, 1 week ago
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(-)
[PATCH 0/3] usb: gadget: Refactor NCM net_device lifecycle
Posted by Kuen-Han Tsai 1 month, 1 week ago
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.

Signed-off-by: Kuen-Han Tsai <khtsai@google.com>
---
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,
-- 
Kuen-Han Tsai <khtsai@google.com>