Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Revert "usb: gadget: u_ether: add gether_opts for config caching"

This reverts commit e065c6a7e46c2ee9c677fdbf50035323d2de1215.

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: 56a512a9b410 ("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>

authored by

Kuen-Han Tsai and committed by
Greg Kroah-Hartman
3131c1af 3549d0ae

-234
-30
drivers/usb/gadget/function/u_ether.c
··· 1040 1040 } 1041 1041 EXPORT_SYMBOL_GPL(gether_set_ifname); 1042 1042 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 - 1073 1043 void gether_suspend(struct gether *link) 1074 1044 { 1075 1045 struct eth_dev *dev = link->ioport;
-28
drivers/usb/gadget/function/u_ether.h
··· 38 38 39 39 struct eth_dev; 40 40 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 - 66 41 /* 67 42 * This represents the USB side of an "ethernet" link, managed by a USB 68 43 * function which provides control and (maybe) framing. Two functions ··· 258 283 int gether_set_ifname(struct net_device *net, const char *name, int len); 259 284 260 285 void gether_cleanup(struct eth_dev *dev); 261 - 262 - void gether_setup_opts_default(struct gether_opts *opts, const char *name); 263 - void gether_apply_opts(struct net_device *net, struct gether_opts *opts); 264 286 265 287 void gether_suspend(struct gether *link); 266 288 void gether_resume(struct gether *link);
-176
drivers/usb/gadget/function/u_ether_configfs.h
··· 13 13 #ifndef __U_ETHER_CONFIGFS_H 14 14 #define __U_ETHER_CONFIGFS_H 15 15 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 - 22 16 #define USB_ETHERNET_CONFIGFS_ITEM(_f_) \ 23 17 static void _f_##_attr_release(struct config_item *item) \ 24 18 { \ ··· 196 202 } \ 197 203 \ 198 204 CONFIGFS_ATTR(_f_##_opts_, _n_) 199 - 200 - #define USB_ETHER_OPTS_ITEM(_f_) \ 201 - static void _f_##_attr_release(struct config_item *item) \ 202 - { \ 203 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 204 - \ 205 - usb_put_function_instance(&opts->func_inst); \ 206 - } \ 207 - \ 208 - static struct configfs_item_operations _f_##_item_ops = { \ 209 - .release = _f_##_attr_release, \ 210 - } 211 - 212 - #define USB_ETHER_OPTS_ATTR_DEV_ADDR(_f_) \ 213 - static ssize_t _f_##_opts_dev_addr_show(struct config_item *item, \ 214 - char *page) \ 215 - { \ 216 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 217 - \ 218 - guard(mutex)(&opts->lock); \ 219 - return sysfs_emit(page, "%pM\n", opts->net_opts.dev_mac); \ 220 - } \ 221 - \ 222 - static ssize_t _f_##_opts_dev_addr_store(struct config_item *item, \ 223 - const char *page, size_t len) \ 224 - { \ 225 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 226 - u8 new_addr[ETH_ALEN]; \ 227 - const char *p = page; \ 228 - \ 229 - guard(mutex)(&opts->lock); \ 230 - if (opts->refcnt) \ 231 - return -EBUSY; \ 232 - \ 233 - for (int i = 0; i < ETH_ALEN; i++) { \ 234 - unsigned char num; \ 235 - if ((*p == '.') || (*p == ':')) \ 236 - p++; \ 237 - num = hex_to_bin(*p++) << 4; \ 238 - num |= hex_to_bin(*p++); \ 239 - new_addr[i] = num; \ 240 - } \ 241 - if (!is_valid_ether_addr(new_addr)) \ 242 - return -EINVAL; \ 243 - memcpy(opts->net_opts.dev_mac, new_addr, ETH_ALEN); \ 244 - opts->net_opts.addr_assign_type = NET_ADDR_SET; \ 245 - return len; \ 246 - } \ 247 - \ 248 - CONFIGFS_ATTR(_f_##_opts_, dev_addr) 249 - 250 - #define USB_ETHER_OPTS_ATTR_HOST_ADDR(_f_) \ 251 - static ssize_t _f_##_opts_host_addr_show(struct config_item *item, \ 252 - char *page) \ 253 - { \ 254 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 255 - \ 256 - guard(mutex)(&opts->lock); \ 257 - return sysfs_emit(page, "%pM\n", opts->net_opts.host_mac); \ 258 - } \ 259 - \ 260 - static ssize_t _f_##_opts_host_addr_store(struct config_item *item, \ 261 - const char *page, size_t len) \ 262 - { \ 263 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 264 - u8 new_addr[ETH_ALEN]; \ 265 - const char *p = page; \ 266 - \ 267 - guard(mutex)(&opts->lock); \ 268 - if (opts->refcnt) \ 269 - return -EBUSY; \ 270 - \ 271 - for (int i = 0; i < ETH_ALEN; i++) { \ 272 - unsigned char num; \ 273 - if ((*p == '.') || (*p == ':')) \ 274 - p++; \ 275 - num = hex_to_bin(*p++) << 4; \ 276 - num |= hex_to_bin(*p++); \ 277 - new_addr[i] = num; \ 278 - } \ 279 - if (!is_valid_ether_addr(new_addr)) \ 280 - return -EINVAL; \ 281 - memcpy(opts->net_opts.host_mac, new_addr, ETH_ALEN); \ 282 - return len; \ 283 - } \ 284 - \ 285 - CONFIGFS_ATTR(_f_##_opts_, host_addr) 286 - 287 - #define USB_ETHER_OPTS_ATTR_QMULT(_f_) \ 288 - static ssize_t _f_##_opts_qmult_show(struct config_item *item, \ 289 - char *page) \ 290 - { \ 291 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 292 - \ 293 - guard(mutex)(&opts->lock); \ 294 - return sysfs_emit(page, "%u\n", opts->net_opts.qmult); \ 295 - } \ 296 - \ 297 - static ssize_t _f_##_opts_qmult_store(struct config_item *item, \ 298 - const char *page, size_t len) \ 299 - { \ 300 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 301 - u32 val; \ 302 - int ret; \ 303 - \ 304 - guard(mutex)(&opts->lock); \ 305 - if (opts->refcnt) \ 306 - return -EBUSY; \ 307 - \ 308 - ret = kstrtou32(page, 0, &val); \ 309 - if (ret) \ 310 - return ret; \ 311 - \ 312 - opts->net_opts.qmult = val; \ 313 - return len; \ 314 - } \ 315 - \ 316 - CONFIGFS_ATTR(_f_##_opts_, qmult) 317 - 318 - #define USB_ETHER_OPTS_ATTR_IFNAME(_f_) \ 319 - static ssize_t _f_##_opts_ifname_show(struct config_item *item, \ 320 - char *page) \ 321 - { \ 322 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 323 - const char *name; \ 324 - \ 325 - guard(mutex)(&opts->lock); \ 326 - rtnl_lock(); \ 327 - if (opts->net_opts.ifname_set) \ 328 - name = opts->net_opts.name; \ 329 - else if (opts->net) \ 330 - name = netdev_name(opts->net); \ 331 - else \ 332 - name = "(inactive net_device)"; \ 333 - rtnl_unlock(); \ 334 - return sysfs_emit(page, "%s\n", name); \ 335 - } \ 336 - \ 337 - static ssize_t _f_##_opts_ifname_store(struct config_item *item, \ 338 - const char *page, size_t len) \ 339 - { \ 340 - struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ 341 - char tmp[IFNAMSIZ]; \ 342 - const char *p; \ 343 - size_t c_len = len; \ 344 - \ 345 - if (c_len > 0 && page[c_len - 1] == '\n') \ 346 - c_len--; \ 347 - \ 348 - if (c_len >= sizeof(tmp)) \ 349 - return -E2BIG; \ 350 - \ 351 - strscpy(tmp, page, c_len + 1); \ 352 - if (!dev_valid_name(tmp)) \ 353 - return -EINVAL; \ 354 - \ 355 - /* Require exactly one %d */ \ 356 - p = strchr(tmp, '%'); \ 357 - if (!p || p[1] != 'd' || strchr(p + 2, '%')) \ 358 - return -EINVAL; \ 359 - \ 360 - guard(mutex)(&opts->lock); \ 361 - if (opts->refcnt) \ 362 - return -EBUSY; \ 363 - strscpy(opts->net_opts.name, tmp, sizeof(opts->net_opts.name)); \ 364 - opts->net_opts.ifname_set = true; \ 365 - return len; \ 366 - } \ 367 - \ 368 - CONFIGFS_ATTR(_f_##_opts_, ifname) 369 205 370 206 #endif /* __U_ETHER_CONFIGFS_H */