Skip to content

Commit 3131c1a

Browse files
Kuen-Han Tsaigregkh
authored andcommitted
Revert "usb: gadget: u_ether: add gether_opts for config caching"
This reverts commit e065c6a. This commit is being reverted as part of a series-wide revert. By deferring the net_device allocation to the bind() phase, a single function instance will spawn multiple network devices if it is symlinked to multiple USB configurations. This causes regressions for userspace tools (like the postmarketOS DHCP daemon) that rely on reading the interface name (e.g., "usb0") from configfs. Currently, configfs returns the template "usb%d", causing the userspace network setup to fail. Crucially, because this patch breaks the 1:1 mapping between the function instance and the network device, this naming issue cannot simply be patched. Configfs only exposes a single 'ifname' attribute per instance, making it impossible to accurately report the actual interface name when multiple underlying network devices can exist for that single instance. All configurations tied to the same function instance are meant to share a single network device. Revert this change to restore the 1:1 mapping by allocating the network device at the instance level (alloc_inst). Reported-by: David Heidelberg <david@ixit.cz> Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c951131249af@ixit.cz/ Fixes: 56a512a ("usb: gadget: f_ncm: align net_device lifecycle with bind/unbind") Cc: stable <stable@kernel.org> Signed-off-by: Kuen-Han Tsai <khtsai@google.com> Link: https://patch.msgid.link/20260309-f-ncm-revert-v2-6-ea2afbc7d9b2@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3549d0a commit 3131c1a

3 files changed

Lines changed: 0 additions & 234 deletions

File tree

drivers/usb/gadget/function/u_ether.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,36 +1040,6 @@ int gether_set_ifname(struct net_device *net, const char *name, int len)
10401040
}
10411041
EXPORT_SYMBOL_GPL(gether_set_ifname);
10421042

1043-
void gether_setup_opts_default(struct gether_opts *opts, const char *name)
1044-
{
1045-
opts->qmult = QMULT_DEFAULT;
1046-
snprintf(opts->name, sizeof(opts->name), "%s%%d", name);
1047-
eth_random_addr(opts->dev_mac);
1048-
opts->addr_assign_type = NET_ADDR_RANDOM;
1049-
eth_random_addr(opts->host_mac);
1050-
}
1051-
EXPORT_SYMBOL_GPL(gether_setup_opts_default);
1052-
1053-
void gether_apply_opts(struct net_device *net, struct gether_opts *opts)
1054-
{
1055-
struct eth_dev *dev = netdev_priv(net);
1056-
1057-
dev->qmult = opts->qmult;
1058-
1059-
if (opts->ifname_set) {
1060-
strscpy(net->name, opts->name, sizeof(net->name));
1061-
dev->ifname_set = true;
1062-
}
1063-
1064-
memcpy(dev->host_mac, opts->host_mac, sizeof(dev->host_mac));
1065-
1066-
if (opts->addr_assign_type == NET_ADDR_SET) {
1067-
memcpy(dev->dev_mac, opts->dev_mac, sizeof(dev->dev_mac));
1068-
net->addr_assign_type = opts->addr_assign_type;
1069-
}
1070-
}
1071-
EXPORT_SYMBOL_GPL(gether_apply_opts);
1072-
10731043
void gether_suspend(struct gether *link)
10741044
{
10751045
struct eth_dev *dev = link->ioport;

drivers/usb/gadget/function/u_ether.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,6 @@
3838

3939
struct eth_dev;
4040

41-
/**
42-
* struct gether_opts - Options for Ethernet gadget function instances
43-
* @name: Pattern for the network interface name (e.g., "usb%d").
44-
* Used to generate the net device name.
45-
* @qmult: Queue length multiplier for high/super speed.
46-
* @host_mac: The MAC address to be used by the host side.
47-
* @dev_mac: The MAC address to be used by the device side.
48-
* @ifname_set: True if the interface name pattern has been set by userspace.
49-
* @addr_assign_type: The method used for assigning the device MAC address
50-
* (e.g., NET_ADDR_RANDOM, NET_ADDR_SET).
51-
*
52-
* This structure caches network-related settings provided through configfs
53-
* before the net_device is fully instantiated. This allows for early
54-
* configuration while deferring net_device allocation until the function
55-
* is bound.
56-
*/
57-
struct gether_opts {
58-
char name[IFNAMSIZ];
59-
unsigned int qmult;
60-
u8 host_mac[ETH_ALEN];
61-
u8 dev_mac[ETH_ALEN];
62-
bool ifname_set;
63-
unsigned char addr_assign_type;
64-
};
65-
6641
/*
6742
* This represents the USB side of an "ethernet" link, managed by a USB
6843
* function which provides control and (maybe) framing. Two functions
@@ -284,9 +259,6 @@ int gether_set_ifname(struct net_device *net, const char *name, int len);
284259

285260
void gether_cleanup(struct eth_dev *dev);
286261

287-
void gether_setup_opts_default(struct gether_opts *opts, const char *name);
288-
void gether_apply_opts(struct net_device *net, struct gether_opts *opts);
289-
290262
void gether_suspend(struct gether *link);
291263
void gether_resume(struct gether *link);
292264

drivers/usb/gadget/function/u_ether_configfs.h

Lines changed: 0 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
#ifndef __U_ETHER_CONFIGFS_H
1414
#define __U_ETHER_CONFIGFS_H
1515

16-
#include <linux/cleanup.h>
17-
#include <linux/if_ether.h>
18-
#include <linux/mutex.h>
19-
#include <linux/netdevice.h>
20-
#include <linux/rtnetlink.h>
21-
2216
#define USB_ETHERNET_CONFIGFS_ITEM(_f_) \
2317
static void _f_##_attr_release(struct config_item *item) \
2418
{ \
@@ -203,174 +197,4 @@ out: \
203197
\
204198
CONFIGFS_ATTR(_f_##_opts_, _n_)
205199

206-
#define USB_ETHER_OPTS_ITEM(_f_) \
207-
static void _f_##_attr_release(struct config_item *item) \
208-
{ \
209-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
210-
\
211-
usb_put_function_instance(&opts->func_inst); \
212-
} \
213-
\
214-
static struct configfs_item_operations _f_##_item_ops = { \
215-
.release = _f_##_attr_release, \
216-
}
217-
218-
#define USB_ETHER_OPTS_ATTR_DEV_ADDR(_f_) \
219-
static ssize_t _f_##_opts_dev_addr_show(struct config_item *item, \
220-
char *page) \
221-
{ \
222-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
223-
\
224-
guard(mutex)(&opts->lock); \
225-
return sysfs_emit(page, "%pM\n", opts->net_opts.dev_mac); \
226-
} \
227-
\
228-
static ssize_t _f_##_opts_dev_addr_store(struct config_item *item, \
229-
const char *page, size_t len) \
230-
{ \
231-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
232-
u8 new_addr[ETH_ALEN]; \
233-
const char *p = page; \
234-
\
235-
guard(mutex)(&opts->lock); \
236-
if (opts->refcnt) \
237-
return -EBUSY; \
238-
\
239-
for (int i = 0; i < ETH_ALEN; i++) { \
240-
unsigned char num; \
241-
if ((*p == '.') || (*p == ':')) \
242-
p++; \
243-
num = hex_to_bin(*p++) << 4; \
244-
num |= hex_to_bin(*p++); \
245-
new_addr[i] = num; \
246-
} \
247-
if (!is_valid_ether_addr(new_addr)) \
248-
return -EINVAL; \
249-
memcpy(opts->net_opts.dev_mac, new_addr, ETH_ALEN); \
250-
opts->net_opts.addr_assign_type = NET_ADDR_SET; \
251-
return len; \
252-
} \
253-
\
254-
CONFIGFS_ATTR(_f_##_opts_, dev_addr)
255-
256-
#define USB_ETHER_OPTS_ATTR_HOST_ADDR(_f_) \
257-
static ssize_t _f_##_opts_host_addr_show(struct config_item *item, \
258-
char *page) \
259-
{ \
260-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
261-
\
262-
guard(mutex)(&opts->lock); \
263-
return sysfs_emit(page, "%pM\n", opts->net_opts.host_mac); \
264-
} \
265-
\
266-
static ssize_t _f_##_opts_host_addr_store(struct config_item *item, \
267-
const char *page, size_t len) \
268-
{ \
269-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
270-
u8 new_addr[ETH_ALEN]; \
271-
const char *p = page; \
272-
\
273-
guard(mutex)(&opts->lock); \
274-
if (opts->refcnt) \
275-
return -EBUSY; \
276-
\
277-
for (int i = 0; i < ETH_ALEN; i++) { \
278-
unsigned char num; \
279-
if ((*p == '.') || (*p == ':')) \
280-
p++; \
281-
num = hex_to_bin(*p++) << 4; \
282-
num |= hex_to_bin(*p++); \
283-
new_addr[i] = num; \
284-
} \
285-
if (!is_valid_ether_addr(new_addr)) \
286-
return -EINVAL; \
287-
memcpy(opts->net_opts.host_mac, new_addr, ETH_ALEN); \
288-
return len; \
289-
} \
290-
\
291-
CONFIGFS_ATTR(_f_##_opts_, host_addr)
292-
293-
#define USB_ETHER_OPTS_ATTR_QMULT(_f_) \
294-
static ssize_t _f_##_opts_qmult_show(struct config_item *item, \
295-
char *page) \
296-
{ \
297-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
298-
\
299-
guard(mutex)(&opts->lock); \
300-
return sysfs_emit(page, "%u\n", opts->net_opts.qmult); \
301-
} \
302-
\
303-
static ssize_t _f_##_opts_qmult_store(struct config_item *item, \
304-
const char *page, size_t len) \
305-
{ \
306-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
307-
u32 val; \
308-
int ret; \
309-
\
310-
guard(mutex)(&opts->lock); \
311-
if (opts->refcnt) \
312-
return -EBUSY; \
313-
\
314-
ret = kstrtou32(page, 0, &val); \
315-
if (ret) \
316-
return ret; \
317-
\
318-
opts->net_opts.qmult = val; \
319-
return len; \
320-
} \
321-
\
322-
CONFIGFS_ATTR(_f_##_opts_, qmult)
323-
324-
#define USB_ETHER_OPTS_ATTR_IFNAME(_f_) \
325-
static ssize_t _f_##_opts_ifname_show(struct config_item *item, \
326-
char *page) \
327-
{ \
328-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
329-
const char *name; \
330-
\
331-
guard(mutex)(&opts->lock); \
332-
rtnl_lock(); \
333-
if (opts->net_opts.ifname_set) \
334-
name = opts->net_opts.name; \
335-
else if (opts->net) \
336-
name = netdev_name(opts->net); \
337-
else \
338-
name = "(inactive net_device)"; \
339-
rtnl_unlock(); \
340-
return sysfs_emit(page, "%s\n", name); \
341-
} \
342-
\
343-
static ssize_t _f_##_opts_ifname_store(struct config_item *item, \
344-
const char *page, size_t len) \
345-
{ \
346-
struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
347-
char tmp[IFNAMSIZ]; \
348-
const char *p; \
349-
size_t c_len = len; \
350-
\
351-
if (c_len > 0 && page[c_len - 1] == '\n') \
352-
c_len--; \
353-
\
354-
if (c_len >= sizeof(tmp)) \
355-
return -E2BIG; \
356-
\
357-
strscpy(tmp, page, c_len + 1); \
358-
if (!dev_valid_name(tmp)) \
359-
return -EINVAL; \
360-
\
361-
/* Require exactly one %d */ \
362-
p = strchr(tmp, '%'); \
363-
if (!p || p[1] != 'd' || strchr(p + 2, '%')) \
364-
return -EINVAL; \
365-
\
366-
guard(mutex)(&opts->lock); \
367-
if (opts->refcnt) \
368-
return -EBUSY; \
369-
strscpy(opts->net_opts.name, tmp, sizeof(opts->net_opts.name)); \
370-
opts->net_opts.ifname_set = true; \
371-
return len; \
372-
} \
373-
\
374-
CONFIGFS_ATTR(_f_##_opts_, ifname)
375-
376200
#endif /* __U_ETHER_CONFIGFS_H */

0 commit comments

Comments
 (0)