Skip to content

Commit 8ad8b8d

Browse files
Kuen-Han TsaiSasha Levin
authored andcommitted
usb: gadget: u_ether: Add auto-cleanup helper for freeing net_device
[ Upstream commit 0c09811 ] The net_device in the u_ether framework currently requires explicit calls to unregister and free the device. Introduce gether_unregister_free_netdev() and the corresponding auto-cleanup macro. This ensures that if a net_device is registered, it is properly unregistered and the associated work queue is flushed before the memory is freed. This is a preparatory patch to simplify error handling paths in gadget drivers by removing the need for explicit goto labels for net_device cleanup. Signed-off-by: Kuen-Han Tsai <khtsai@google.com> Link: https://patch.msgid.link/20251230-ncm-refactor-v1-2-793e347bc7a7@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Stable-dep-of: 56a512a ("usb: gadget: f_ncm: align net_device lifecycle with bind/unbind") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 9a6dc80 commit 8ad8b8d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

drivers/usb/gadget/function/u_ether.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,21 @@ void gether_cleanup(struct eth_dev *dev)
11251125
}
11261126
EXPORT_SYMBOL_GPL(gether_cleanup);
11271127

1128+
void gether_unregister_free_netdev(struct net_device *net)
1129+
{
1130+
if (!net)
1131+
return;
1132+
1133+
struct eth_dev *dev = netdev_priv(net);
1134+
1135+
if (net->reg_state == NETREG_REGISTERED) {
1136+
unregister_netdev(net);
1137+
flush_work(&dev->work);
1138+
}
1139+
free_netdev(net);
1140+
}
1141+
EXPORT_SYMBOL_GPL(gether_unregister_free_netdev);
1142+
11281143
/**
11291144
* gether_connect - notify network layer that USB link is active
11301145
* @link: the USB link, set up with endpoints, descriptors matching

drivers/usb/gadget/function/u_ether.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ int gether_get_ifname(struct net_device *net, char *name, int len);
283283
int gether_set_ifname(struct net_device *net, const char *name, int len);
284284

285285
void gether_cleanup(struct eth_dev *dev);
286+
void gether_unregister_free_netdev(struct net_device *net);
287+
DEFINE_FREE(free_gether_netdev, struct net_device *, gether_unregister_free_netdev(_T));
286288

287289
void gether_setup_opts_default(struct gether_opts *opts, const char *name);
288290
void gether_apply_opts(struct net_device *net, struct gether_opts *opts);

0 commit comments

Comments
 (0)