xref: /linux/net/core/net-sysfs.c (revision de5ca699bc3f7fe9f90ba927d8a6e7783cd7311d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net-sysfs.c - network device class and attributes
4  *
5  * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
6  */
7 
8 #include <linux/capability.h>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sched/isolation.h>
15 #include <linux/nsproxy.h>
16 #include <net/sock.h>
17 #include <net/net_namespace.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/vmalloc.h>
20 #include <linux/export.h>
21 #include <linux/jiffies.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/of.h>
24 #include <linux/of_net.h>
25 #include <linux/cpu.h>
26 #include <net/netdev_lock.h>
27 #include <net/netdev_rx_queue.h>
28 #include <net/rps.h>
29 
30 #include "dev.h"
31 #include "net-sysfs.h"
32 
33 #ifdef CONFIG_SYSFS
34 static const char fmt_hex[] = "%#x\n";
35 static const char fmt_dec[] = "%d\n";
36 static const char fmt_uint[] = "%u\n";
37 static const char fmt_ulong[] = "%lu\n";
38 static const char fmt_u64[] = "%llu\n";
39 
40 /* Caller holds RTNL, netdev->lock or RCU */
41 static inline int dev_isalive(const struct net_device *dev)
42 {
43 	return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED;
44 }
45 
46 /* There is a possible ABBA deadlock between rtnl_lock and kernfs_node->active,
47  * when unregistering a net device and accessing associated sysfs files. The
48  * potential deadlock is as follow:
49  *
50  *         CPU 0                                         CPU 1
51  *
52  *    rtnl_lock                                   vfs_read
53  *    unregister_netdevice_many                   kernfs_seq_start
54  *    device_del / kobject_put                      kernfs_get_active (kn->active++)
55  *    kernfs_drain                                sysfs_kf_seq_show
56  *    wait_event(                                 rtnl_lock
57  *       kn->active == KN_DEACTIVATED_BIAS)       -> waits on CPU 0 to release
58  *    -> waits on CPU 1 to decrease kn->active       the rtnl lock.
59  *
60  * The historical fix was to use rtnl_trylock with restart_syscall to bail out
61  * of sysfs operations when the lock couldn't be taken. This fixed the above
62  * issue as it allowed CPU 1 to bail out of the ABBA situation.
63  *
64  * But it came with performances issues, as syscalls are being restarted in
65  * loops when there was contention on the rtnl lock, with huge slow downs in
66  * specific scenarios (e.g. lots of virtual interfaces created and userspace
67  * daemons querying their attributes).
68  *
69  * The idea below is to bail out of the active kernfs_node protection
70  * (kn->active) while trying to take the rtnl lock.
71  *
72  * This replaces rtnl_lock() and still has to be used with rtnl_unlock(). The
73  * net device is guaranteed to be alive if this returns successfully.
74  */
75 static int sysfs_rtnl_lock(struct kobject *kobj, struct attribute *attr,
76 			   struct net_device *ndev)
77 {
78 	struct kernfs_node *kn;
79 	int ret = 0;
80 
81 	/* First, we hold a reference to the net device as the unregistration
82 	 * path might run in parallel. This will ensure the net device and the
83 	 * associated sysfs objects won't be freed while we try to take the rtnl
84 	 * lock.
85 	 */
86 	dev_hold(ndev);
87 	/* sysfs_break_active_protection was introduced to allow self-removal of
88 	 * devices and their associated sysfs files by bailing out of the
89 	 * sysfs/kernfs protection. We do this here to allow the unregistration
90 	 * path to complete in parallel. The following takes a reference on the
91 	 * kobject and the kernfs_node being accessed.
92 	 *
93 	 * This works because we hold a reference onto the net device and the
94 	 * unregistration path will wait for us eventually in netdev_run_todo
95 	 * (outside an rtnl lock section).
96 	 */
97 	kn = sysfs_break_active_protection(kobj, attr);
98 	/* We can now try to take the rtnl lock. This can't deadlock us as the
99 	 * unregistration path is able to drain sysfs files (kernfs_node) thanks
100 	 * to the above dance.
101 	 */
102 	if (rtnl_lock_interruptible()) {
103 		ret = -ERESTARTSYS;
104 		goto unbreak;
105 	}
106 	/* Check dismantle on the device hasn't started, otherwise deny the
107 	 * operation.
108 	 */
109 	if (!dev_isalive(ndev)) {
110 		rtnl_unlock();
111 		ret = -ENODEV;
112 		goto unbreak;
113 	}
114 	/* We are now sure the device dismantle hasn't started nor that it can
115 	 * start before we exit the locking section as we hold the rtnl lock.
116 	 * There's no need to keep unbreaking the sysfs protection nor to hold
117 	 * a net device reference from that point; that was only needed to take
118 	 * the rtnl lock.
119 	 */
120 unbreak:
121 	sysfs_unbreak_active_protection(kn);
122 	dev_put(ndev);
123 
124 	return ret;
125 }
126 
127 /* use same locking rules as GIF* ioctl's */
128 static ssize_t netdev_show(const struct device *dev,
129 			   struct device_attribute *attr, char *buf,
130 			   ssize_t (*format)(const struct net_device *, char *))
131 {
132 	struct net_device *ndev = to_net_dev(dev);
133 	ssize_t ret = -EINVAL;
134 
135 	rcu_read_lock();
136 	if (dev_isalive(ndev))
137 		ret = (*format)(ndev, buf);
138 	rcu_read_unlock();
139 
140 	return ret;
141 }
142 
143 /* generate a show function for simple field */
144 #define NETDEVICE_SHOW(field, format_string)				\
145 static ssize_t format_##field(const struct net_device *dev, char *buf)	\
146 {									\
147 	return sysfs_emit(buf, format_string, READ_ONCE(dev->field));		\
148 }									\
149 static ssize_t field##_show(struct device *dev,				\
150 			    struct device_attribute *attr, char *buf)	\
151 {									\
152 	return netdev_show(dev, attr, buf, format_##field);		\
153 }									\
154 
155 #define NETDEVICE_SHOW_RO(field, format_string)				\
156 NETDEVICE_SHOW(field, format_string);					\
157 static DEVICE_ATTR_RO(field)
158 
159 #define NETDEVICE_SHOW_RW(field, format_string)				\
160 NETDEVICE_SHOW(field, format_string);					\
161 static DEVICE_ATTR_RW(field)
162 
163 /* use same locking and permission rules as SIF* ioctl's */
164 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
165 			    const char *buf, size_t len,
166 			    int (*set)(struct net_device *, unsigned long))
167 {
168 	struct net_device *netdev = to_net_dev(dev);
169 	struct net *net = dev_net(netdev);
170 	unsigned long new;
171 	int ret;
172 
173 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
174 		return -EPERM;
175 
176 	ret = kstrtoul(buf, 0, &new);
177 	if (ret)
178 		goto err;
179 
180 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
181 	if (ret)
182 		goto err;
183 
184 	ret = (*set)(netdev, new);
185 	if (ret == 0)
186 		ret = len;
187 
188 	rtnl_unlock();
189  err:
190 	return ret;
191 }
192 
193 /* Same as netdev_store() but takes netdev_lock() instead of rtnl_lock() */
194 static ssize_t
195 netdev_lock_store(struct device *dev, struct device_attribute *attr,
196 		  const char *buf, size_t len,
197 		  int (*set)(struct net_device *, unsigned long))
198 {
199 	struct net_device *netdev = to_net_dev(dev);
200 	struct net *net = dev_net(netdev);
201 	unsigned long new;
202 	int ret;
203 
204 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
205 		return -EPERM;
206 
207 	ret = kstrtoul(buf, 0, &new);
208 	if (ret)
209 		return ret;
210 
211 	netdev_lock(netdev);
212 
213 	if (dev_isalive(netdev)) {
214 		ret = (*set)(netdev, new);
215 		if (ret == 0)
216 			ret = len;
217 	}
218 	netdev_unlock(netdev);
219 
220 	return ret;
221 }
222 
223 NETDEVICE_SHOW_RO(dev_id, fmt_hex);
224 NETDEVICE_SHOW_RO(dev_port, fmt_dec);
225 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
226 NETDEVICE_SHOW_RO(addr_len, fmt_dec);
227 NETDEVICE_SHOW_RO(ifindex, fmt_dec);
228 NETDEVICE_SHOW_RO(type, fmt_dec);
229 NETDEVICE_SHOW_RO(link_mode, fmt_dec);
230 
231 static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
232 			   char *buf)
233 {
234 	struct net_device *ndev = to_net_dev(dev);
235 
236 	return sysfs_emit(buf, fmt_dec, dev_get_iflink(ndev));
237 }
238 static DEVICE_ATTR_RO(iflink);
239 
240 static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
241 {
242 	return sysfs_emit(buf, fmt_dec, READ_ONCE(dev->name_assign_type));
243 }
244 
245 static ssize_t name_assign_type_show(struct device *dev,
246 				     struct device_attribute *attr,
247 				     char *buf)
248 {
249 	struct net_device *ndev = to_net_dev(dev);
250 	ssize_t ret = -EINVAL;
251 
252 	if (READ_ONCE(ndev->name_assign_type) != NET_NAME_UNKNOWN)
253 		ret = netdev_show(dev, attr, buf, format_name_assign_type);
254 
255 	return ret;
256 }
257 static DEVICE_ATTR_RO(name_assign_type);
258 
259 /* use same locking rules as GIFHWADDR ioctl's (dev_get_mac_address()) */
260 static ssize_t address_show(struct device *dev, struct device_attribute *attr,
261 			    char *buf)
262 {
263 	struct net_device *ndev = to_net_dev(dev);
264 	ssize_t ret = -EINVAL;
265 
266 	netdev_lock(ndev);
267 	if (dev_isalive(ndev))
268 		ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
269 	netdev_unlock(ndev);
270 
271 	return ret;
272 }
273 static DEVICE_ATTR_RO(address);
274 
275 static ssize_t broadcast_show(struct device *dev,
276 			      struct device_attribute *attr, char *buf)
277 {
278 	struct net_device *ndev = to_net_dev(dev);
279 	int ret = -EINVAL;
280 
281 	rcu_read_lock();
282 	if (dev_isalive(ndev))
283 		ret = sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
284 	rcu_read_unlock();
285 	return ret;
286 }
287 static DEVICE_ATTR_RO(broadcast);
288 
289 static int change_carrier(struct net_device *dev, unsigned long new_carrier)
290 {
291 	if (!netif_running(dev))
292 		return -EINVAL;
293 	return dev_change_carrier(dev, (bool)new_carrier);
294 }
295 
296 static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
297 			     const char *buf, size_t len)
298 {
299 	struct net_device *netdev = to_net_dev(dev);
300 
301 	/* The check is also done in change_carrier; this helps returning early
302 	 * without hitting the locking section in netdev_store.
303 	 */
304 	if (!netdev->netdev_ops->ndo_change_carrier)
305 		return -EOPNOTSUPP;
306 
307 	return netdev_store(dev, attr, buf, len, change_carrier);
308 }
309 
310 static ssize_t carrier_show(struct device *dev,
311 			    struct device_attribute *attr, char *buf)
312 {
313 	struct net_device *netdev = to_net_dev(dev);
314 	int ret;
315 
316 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
317 	if (ret)
318 		return ret;
319 
320 	ret = -EINVAL;
321 	if (netif_running(netdev)) {
322 		/* Synchronize carrier state with link watch,
323 		 * see also rtnl_getlink().
324 		 */
325 		linkwatch_sync_dev(netdev);
326 
327 		ret = sysfs_emit(buf, fmt_dec, !!netif_carrier_ok(netdev));
328 	}
329 
330 	rtnl_unlock();
331 	return ret;
332 }
333 static DEVICE_ATTR_RW(carrier);
334 
335 static ssize_t speed_show(struct device *dev,
336 			  struct device_attribute *attr, char *buf)
337 {
338 	struct net_device *netdev = to_net_dev(dev);
339 	int ret = -EINVAL;
340 
341 	/* The check is also done in __ethtool_get_link_ksettings; this helps
342 	 * returning early without hitting the locking section below.
343 	 */
344 	if (!netdev->ethtool_ops->get_link_ksettings)
345 		return ret;
346 
347 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
348 	if (ret)
349 		return ret;
350 
351 	ret = -EINVAL;
352 	if (netif_running(netdev)) {
353 		struct ethtool_link_ksettings cmd;
354 
355 		if (!__ethtool_get_link_ksettings(netdev, &cmd))
356 			ret = sysfs_emit(buf, fmt_dec, cmd.base.speed);
357 	}
358 	rtnl_unlock();
359 	return ret;
360 }
361 static DEVICE_ATTR_RO(speed);
362 
363 static ssize_t duplex_show(struct device *dev,
364 			   struct device_attribute *attr, char *buf)
365 {
366 	struct net_device *netdev = to_net_dev(dev);
367 	int ret = -EINVAL;
368 
369 	/* The check is also done in __ethtool_get_link_ksettings; this helps
370 	 * returning early without hitting the locking section below.
371 	 */
372 	if (!netdev->ethtool_ops->get_link_ksettings)
373 		return ret;
374 
375 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
376 	if (ret)
377 		return ret;
378 
379 	ret = -EINVAL;
380 	if (netif_running(netdev)) {
381 		struct ethtool_link_ksettings cmd;
382 
383 		if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
384 			const char *duplex;
385 
386 			switch (cmd.base.duplex) {
387 			case DUPLEX_HALF:
388 				duplex = "half";
389 				break;
390 			case DUPLEX_FULL:
391 				duplex = "full";
392 				break;
393 			default:
394 				duplex = "unknown";
395 				break;
396 			}
397 			ret = sysfs_emit(buf, "%s\n", duplex);
398 		}
399 	}
400 	rtnl_unlock();
401 	return ret;
402 }
403 static DEVICE_ATTR_RO(duplex);
404 
405 static ssize_t testing_show(struct device *dev,
406 			    struct device_attribute *attr, char *buf)
407 {
408 	struct net_device *netdev = to_net_dev(dev);
409 
410 	if (netif_running(netdev))
411 		return sysfs_emit(buf, fmt_dec, !!netif_testing(netdev));
412 
413 	return -EINVAL;
414 }
415 static DEVICE_ATTR_RO(testing);
416 
417 static ssize_t dormant_show(struct device *dev,
418 			    struct device_attribute *attr, char *buf)
419 {
420 	struct net_device *netdev = to_net_dev(dev);
421 
422 	if (netif_running(netdev))
423 		return sysfs_emit(buf, fmt_dec, !!netif_dormant(netdev));
424 
425 	return -EINVAL;
426 }
427 static DEVICE_ATTR_RO(dormant);
428 
429 static const char *const operstates[] = {
430 	"unknown",
431 	"notpresent", /* currently unused */
432 	"down",
433 	"lowerlayerdown",
434 	"testing",
435 	"dormant",
436 	"up"
437 };
438 
439 static ssize_t operstate_show(struct device *dev,
440 			      struct device_attribute *attr, char *buf)
441 {
442 	const struct net_device *netdev = to_net_dev(dev);
443 	unsigned char operstate;
444 
445 	operstate = READ_ONCE(netdev->operstate);
446 	if (!netif_running(netdev))
447 		operstate = IF_OPER_DOWN;
448 
449 	if (operstate >= ARRAY_SIZE(operstates))
450 		return -EINVAL; /* should not happen */
451 
452 	return sysfs_emit(buf, "%s\n", operstates[operstate]);
453 }
454 static DEVICE_ATTR_RO(operstate);
455 
456 static ssize_t carrier_changes_show(struct device *dev,
457 				    struct device_attribute *attr,
458 				    char *buf)
459 {
460 	struct net_device *netdev = to_net_dev(dev);
461 
462 	return sysfs_emit(buf, fmt_dec,
463 			  atomic_read(&netdev->carrier_up_count) +
464 			  atomic_read(&netdev->carrier_down_count));
465 }
466 static DEVICE_ATTR_RO(carrier_changes);
467 
468 static ssize_t carrier_up_count_show(struct device *dev,
469 				     struct device_attribute *attr,
470 				     char *buf)
471 {
472 	struct net_device *netdev = to_net_dev(dev);
473 
474 	return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
475 }
476 static DEVICE_ATTR_RO(carrier_up_count);
477 
478 static ssize_t carrier_down_count_show(struct device *dev,
479 				       struct device_attribute *attr,
480 				       char *buf)
481 {
482 	struct net_device *netdev = to_net_dev(dev);
483 
484 	return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
485 }
486 static DEVICE_ATTR_RO(carrier_down_count);
487 
488 /* read-write attributes */
489 
490 static int change_mtu(struct net_device *dev, unsigned long new_mtu)
491 {
492 	return dev_set_mtu(dev, (int)new_mtu);
493 }
494 
495 static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
496 			 const char *buf, size_t len)
497 {
498 	return netdev_store(dev, attr, buf, len, change_mtu);
499 }
500 NETDEVICE_SHOW_RW(mtu, fmt_dec);
501 
502 static int change_flags(struct net_device *dev, unsigned long new_flags)
503 {
504 	return dev_change_flags(dev, (unsigned int)new_flags, NULL);
505 }
506 
507 static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
508 			   const char *buf, size_t len)
509 {
510 	return netdev_store(dev, attr, buf, len, change_flags);
511 }
512 NETDEVICE_SHOW_RW(flags, fmt_hex);
513 
514 static ssize_t tx_queue_len_store(struct device *dev,
515 				  struct device_attribute *attr,
516 				  const char *buf, size_t len)
517 {
518 	if (!capable(CAP_NET_ADMIN))
519 		return -EPERM;
520 
521 	return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
522 }
523 NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
524 
525 static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
526 {
527 	netdev_set_gro_flush_timeout(dev, val);
528 	return 0;
529 }
530 
531 static ssize_t gro_flush_timeout_store(struct device *dev,
532 				       struct device_attribute *attr,
533 				       const char *buf, size_t len)
534 {
535 	if (!capable(CAP_NET_ADMIN))
536 		return -EPERM;
537 
538 	return netdev_lock_store(dev, attr, buf, len, change_gro_flush_timeout);
539 }
540 NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
541 
542 static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val)
543 {
544 	if (val > S32_MAX)
545 		return -ERANGE;
546 
547 	netdev_set_defer_hard_irqs(dev, (u32)val);
548 	return 0;
549 }
550 
551 static ssize_t napi_defer_hard_irqs_store(struct device *dev,
552 					  struct device_attribute *attr,
553 					  const char *buf, size_t len)
554 {
555 	if (!capable(CAP_NET_ADMIN))
556 		return -EPERM;
557 
558 	return netdev_lock_store(dev, attr, buf, len,
559 				 change_napi_defer_hard_irqs);
560 }
561 NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_uint);
562 
563 static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
564 			     const char *buf, size_t len)
565 {
566 	struct net_device *netdev = to_net_dev(dev);
567 	struct net *net = dev_net(netdev);
568 	size_t count = len;
569 	ssize_t ret;
570 
571 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
572 		return -EPERM;
573 
574 	/* ignore trailing newline */
575 	if (len >  0 && buf[len - 1] == '\n')
576 		--count;
577 
578 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
579 	if (ret)
580 		return ret;
581 
582 	ret = dev_set_alias(netdev, buf, count);
583 	if (ret < 0)
584 		goto err;
585 	ret = len;
586 	netdev_state_change(netdev);
587 err:
588 	rtnl_unlock();
589 
590 	return ret;
591 }
592 
593 static ssize_t ifalias_show(struct device *dev,
594 			    struct device_attribute *attr, char *buf)
595 {
596 	const struct net_device *netdev = to_net_dev(dev);
597 	char tmp[IFALIASZ];
598 	ssize_t ret;
599 
600 	ret = dev_get_alias(netdev, tmp, sizeof(tmp));
601 	if (ret > 0)
602 		ret = sysfs_emit(buf, "%s\n", tmp);
603 	return ret;
604 }
605 static DEVICE_ATTR_RW(ifalias);
606 
607 static int change_group(struct net_device *dev, unsigned long new_group)
608 {
609 	dev_set_group(dev, (int)new_group);
610 	return 0;
611 }
612 
613 static ssize_t group_store(struct device *dev, struct device_attribute *attr,
614 			   const char *buf, size_t len)
615 {
616 	return netdev_store(dev, attr, buf, len, change_group);
617 }
618 NETDEVICE_SHOW(group, fmt_dec);
619 static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
620 
621 static int change_proto_down(struct net_device *dev, unsigned long proto_down)
622 {
623 	return dev_change_proto_down(dev, (bool)proto_down);
624 }
625 
626 static ssize_t proto_down_store(struct device *dev,
627 				struct device_attribute *attr,
628 				const char *buf, size_t len)
629 {
630 	return netdev_store(dev, attr, buf, len, change_proto_down);
631 }
632 NETDEVICE_SHOW_RW(proto_down, fmt_dec);
633 
634 static ssize_t phys_port_id_show(struct device *dev,
635 				 struct device_attribute *attr, char *buf)
636 {
637 	struct net_device *netdev = to_net_dev(dev);
638 	struct netdev_phys_item_id ppid;
639 	ssize_t ret;
640 
641 	/* The check is also done in dev_get_phys_port_id; this helps returning
642 	 * early without hitting the locking section below.
643 	 */
644 	if (!netdev->netdev_ops->ndo_get_phys_port_id)
645 		return -EOPNOTSUPP;
646 
647 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
648 	if (ret)
649 		return ret;
650 
651 	ret = dev_get_phys_port_id(netdev, &ppid);
652 	if (!ret)
653 		ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id);
654 
655 	rtnl_unlock();
656 
657 	return ret;
658 }
659 static DEVICE_ATTR_RO(phys_port_id);
660 
661 static ssize_t phys_port_name_show(struct device *dev,
662 				   struct device_attribute *attr, char *buf)
663 {
664 	struct net_device *netdev = to_net_dev(dev);
665 	char name[IFNAMSIZ];
666 	ssize_t ret;
667 
668 	/* The checks are also done in dev_get_phys_port_name; this helps
669 	 * returning early without hitting the locking section below.
670 	 */
671 	if (!netdev->netdev_ops->ndo_get_phys_port_name &&
672 	    !netdev->devlink_port)
673 		return -EOPNOTSUPP;
674 
675 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
676 	if (ret)
677 		return ret;
678 
679 	ret = dev_get_phys_port_name(netdev, name, sizeof(name));
680 	if (!ret)
681 		ret = sysfs_emit(buf, "%s\n", name);
682 
683 	rtnl_unlock();
684 
685 	return ret;
686 }
687 static DEVICE_ATTR_RO(phys_port_name);
688 
689 static ssize_t phys_switch_id_show(struct device *dev,
690 				   struct device_attribute *attr, char *buf)
691 {
692 	struct net_device *netdev = to_net_dev(dev);
693 	struct netdev_phys_item_id ppid = { };
694 	ssize_t ret;
695 
696 	/* The checks are also done in dev_get_phys_port_name; this helps
697 	 * returning early without hitting the locking section below. This works
698 	 * because recurse is false when calling dev_get_port_parent_id.
699 	 */
700 	if (!netdev->netdev_ops->ndo_get_port_parent_id &&
701 	    !netdev->devlink_port)
702 		return -EOPNOTSUPP;
703 
704 	ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
705 	if (ret)
706 		return ret;
707 
708 	ret = dev_get_port_parent_id(netdev, &ppid, false);
709 	if (!ret)
710 		ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id);
711 
712 	rtnl_unlock();
713 
714 	return ret;
715 }
716 static DEVICE_ATTR_RO(phys_switch_id);
717 
718 static ssize_t threaded_show(struct device *dev,
719 			     struct device_attribute *attr, char *buf)
720 {
721 	struct net_device *netdev = to_net_dev(dev);
722 	ssize_t ret = -EINVAL;
723 
724 	rcu_read_lock();
725 
726 	if (dev_isalive(netdev))
727 		ret = sysfs_emit(buf, fmt_dec, READ_ONCE(netdev->threaded));
728 
729 	rcu_read_unlock();
730 
731 	return ret;
732 }
733 
734 static int modify_napi_threaded(struct net_device *dev, unsigned long val)
735 {
736 	int ret;
737 
738 	if (list_empty(&dev->napi_list))
739 		return -EOPNOTSUPP;
740 
741 	if (val != 0 && val != 1)
742 		return -EOPNOTSUPP;
743 
744 	ret = dev_set_threaded(dev, val);
745 
746 	return ret;
747 }
748 
749 static ssize_t threaded_store(struct device *dev,
750 			      struct device_attribute *attr,
751 			      const char *buf, size_t len)
752 {
753 	return netdev_lock_store(dev, attr, buf, len, modify_napi_threaded);
754 }
755 static DEVICE_ATTR_RW(threaded);
756 
757 static struct attribute *net_class_attrs[] __ro_after_init = {
758 	&dev_attr_netdev_group.attr,
759 	&dev_attr_type.attr,
760 	&dev_attr_dev_id.attr,
761 	&dev_attr_dev_port.attr,
762 	&dev_attr_iflink.attr,
763 	&dev_attr_ifindex.attr,
764 	&dev_attr_name_assign_type.attr,
765 	&dev_attr_addr_assign_type.attr,
766 	&dev_attr_addr_len.attr,
767 	&dev_attr_link_mode.attr,
768 	&dev_attr_address.attr,
769 	&dev_attr_broadcast.attr,
770 	&dev_attr_speed.attr,
771 	&dev_attr_duplex.attr,
772 	&dev_attr_dormant.attr,
773 	&dev_attr_testing.attr,
774 	&dev_attr_operstate.attr,
775 	&dev_attr_carrier_changes.attr,
776 	&dev_attr_ifalias.attr,
777 	&dev_attr_carrier.attr,
778 	&dev_attr_mtu.attr,
779 	&dev_attr_flags.attr,
780 	&dev_attr_tx_queue_len.attr,
781 	&dev_attr_gro_flush_timeout.attr,
782 	&dev_attr_napi_defer_hard_irqs.attr,
783 	&dev_attr_phys_port_id.attr,
784 	&dev_attr_phys_port_name.attr,
785 	&dev_attr_phys_switch_id.attr,
786 	&dev_attr_proto_down.attr,
787 	&dev_attr_carrier_up_count.attr,
788 	&dev_attr_carrier_down_count.attr,
789 	&dev_attr_threaded.attr,
790 	NULL,
791 };
792 ATTRIBUTE_GROUPS(net_class);
793 
794 /* Show a given an attribute in the statistics group */
795 static ssize_t netstat_show(const struct device *d,
796 			    struct device_attribute *attr, char *buf,
797 			    unsigned long offset)
798 {
799 	struct net_device *dev = to_net_dev(d);
800 	ssize_t ret = -EINVAL;
801 
802 	WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
803 		offset % sizeof(u64) != 0);
804 
805 	rcu_read_lock();
806 	if (dev_isalive(dev)) {
807 		struct rtnl_link_stats64 temp;
808 		const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
809 
810 		ret = sysfs_emit(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
811 	}
812 	rcu_read_unlock();
813 	return ret;
814 }
815 
816 /* generate a read-only statistics attribute */
817 #define NETSTAT_ENTRY(name)						\
818 static ssize_t name##_show(struct device *d,				\
819 			   struct device_attribute *attr, char *buf)	\
820 {									\
821 	return netstat_show(d, attr, buf,				\
822 			    offsetof(struct rtnl_link_stats64, name));	\
823 }									\
824 static DEVICE_ATTR_RO(name)
825 
826 NETSTAT_ENTRY(rx_packets);
827 NETSTAT_ENTRY(tx_packets);
828 NETSTAT_ENTRY(rx_bytes);
829 NETSTAT_ENTRY(tx_bytes);
830 NETSTAT_ENTRY(rx_errors);
831 NETSTAT_ENTRY(tx_errors);
832 NETSTAT_ENTRY(rx_dropped);
833 NETSTAT_ENTRY(tx_dropped);
834 NETSTAT_ENTRY(multicast);
835 NETSTAT_ENTRY(collisions);
836 NETSTAT_ENTRY(rx_length_errors);
837 NETSTAT_ENTRY(rx_over_errors);
838 NETSTAT_ENTRY(rx_crc_errors);
839 NETSTAT_ENTRY(rx_frame_errors);
840 NETSTAT_ENTRY(rx_fifo_errors);
841 NETSTAT_ENTRY(rx_missed_errors);
842 NETSTAT_ENTRY(tx_aborted_errors);
843 NETSTAT_ENTRY(tx_carrier_errors);
844 NETSTAT_ENTRY(tx_fifo_errors);
845 NETSTAT_ENTRY(tx_heartbeat_errors);
846 NETSTAT_ENTRY(tx_window_errors);
847 NETSTAT_ENTRY(rx_compressed);
848 NETSTAT_ENTRY(tx_compressed);
849 NETSTAT_ENTRY(rx_nohandler);
850 
851 static struct attribute *netstat_attrs[] __ro_after_init = {
852 	&dev_attr_rx_packets.attr,
853 	&dev_attr_tx_packets.attr,
854 	&dev_attr_rx_bytes.attr,
855 	&dev_attr_tx_bytes.attr,
856 	&dev_attr_rx_errors.attr,
857 	&dev_attr_tx_errors.attr,
858 	&dev_attr_rx_dropped.attr,
859 	&dev_attr_tx_dropped.attr,
860 	&dev_attr_multicast.attr,
861 	&dev_attr_collisions.attr,
862 	&dev_attr_rx_length_errors.attr,
863 	&dev_attr_rx_over_errors.attr,
864 	&dev_attr_rx_crc_errors.attr,
865 	&dev_attr_rx_frame_errors.attr,
866 	&dev_attr_rx_fifo_errors.attr,
867 	&dev_attr_rx_missed_errors.attr,
868 	&dev_attr_tx_aborted_errors.attr,
869 	&dev_attr_tx_carrier_errors.attr,
870 	&dev_attr_tx_fifo_errors.attr,
871 	&dev_attr_tx_heartbeat_errors.attr,
872 	&dev_attr_tx_window_errors.attr,
873 	&dev_attr_rx_compressed.attr,
874 	&dev_attr_tx_compressed.attr,
875 	&dev_attr_rx_nohandler.attr,
876 	NULL
877 };
878 
879 static const struct attribute_group netstat_group = {
880 	.name  = "statistics",
881 	.attrs  = netstat_attrs,
882 };
883 
884 static struct attribute *wireless_attrs[] = {
885 	NULL
886 };
887 
888 static const struct attribute_group wireless_group = {
889 	.name = "wireless",
890 	.attrs = wireless_attrs,
891 };
892 
893 static bool wireless_group_needed(struct net_device *ndev)
894 {
895 #if IS_ENABLED(CONFIG_CFG80211)
896 	if (ndev->ieee80211_ptr)
897 		return true;
898 #endif
899 #if IS_ENABLED(CONFIG_WIRELESS_EXT)
900 	if (ndev->wireless_handlers)
901 		return true;
902 #endif
903 	return false;
904 }
905 
906 #else /* CONFIG_SYSFS */
907 #define net_class_groups	NULL
908 #endif /* CONFIG_SYSFS */
909 
910 #ifdef CONFIG_SYSFS
911 #define to_rx_queue_attr(_attr) \
912 	container_of(_attr, struct rx_queue_attribute, attr)
913 
914 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
915 
916 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
917 				  char *buf)
918 {
919 	const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
920 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
921 
922 	if (!attribute->show)
923 		return -EIO;
924 
925 	return attribute->show(queue, buf);
926 }
927 
928 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
929 				   const char *buf, size_t count)
930 {
931 	const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
932 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
933 
934 	if (!attribute->store)
935 		return -EIO;
936 
937 	return attribute->store(queue, buf, count);
938 }
939 
940 static const struct sysfs_ops rx_queue_sysfs_ops = {
941 	.show = rx_queue_attr_show,
942 	.store = rx_queue_attr_store,
943 };
944 
945 #ifdef CONFIG_RPS
946 static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
947 {
948 	struct rps_map *map;
949 	cpumask_var_t mask;
950 	int i, len;
951 
952 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
953 		return -ENOMEM;
954 
955 	rcu_read_lock();
956 	map = rcu_dereference(queue->rps_map);
957 	if (map)
958 		for (i = 0; i < map->len; i++)
959 			cpumask_set_cpu(map->cpus[i], mask);
960 
961 	len = sysfs_emit(buf, "%*pb\n", cpumask_pr_args(mask));
962 	rcu_read_unlock();
963 	free_cpumask_var(mask);
964 
965 	return len < PAGE_SIZE ? len : -EINVAL;
966 }
967 
968 static int netdev_rx_queue_set_rps_mask(struct netdev_rx_queue *queue,
969 					cpumask_var_t mask)
970 {
971 	static DEFINE_MUTEX(rps_map_mutex);
972 	struct rps_map *old_map, *map;
973 	int cpu, i;
974 
975 	map = kzalloc(max_t(unsigned int,
976 			    RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
977 		      GFP_KERNEL);
978 	if (!map)
979 		return -ENOMEM;
980 
981 	i = 0;
982 	for_each_cpu_and(cpu, mask, cpu_online_mask)
983 		map->cpus[i++] = cpu;
984 
985 	if (i) {
986 		map->len = i;
987 	} else {
988 		kfree(map);
989 		map = NULL;
990 	}
991 
992 	mutex_lock(&rps_map_mutex);
993 	old_map = rcu_dereference_protected(queue->rps_map,
994 					    mutex_is_locked(&rps_map_mutex));
995 	rcu_assign_pointer(queue->rps_map, map);
996 
997 	if (map)
998 		static_branch_inc(&rps_needed);
999 	if (old_map)
1000 		static_branch_dec(&rps_needed);
1001 
1002 	mutex_unlock(&rps_map_mutex);
1003 
1004 	if (old_map)
1005 		kfree_rcu(old_map, rcu);
1006 	return 0;
1007 }
1008 
1009 int rps_cpumask_housekeeping(struct cpumask *mask)
1010 {
1011 	if (!cpumask_empty(mask)) {
1012 		cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_DOMAIN));
1013 		cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_WQ));
1014 		if (cpumask_empty(mask))
1015 			return -EINVAL;
1016 	}
1017 	return 0;
1018 }
1019 
1020 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
1021 			     const char *buf, size_t len)
1022 {
1023 	cpumask_var_t mask;
1024 	int err;
1025 
1026 	if (!capable(CAP_NET_ADMIN))
1027 		return -EPERM;
1028 
1029 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1030 		return -ENOMEM;
1031 
1032 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1033 	if (err)
1034 		goto out;
1035 
1036 	err = rps_cpumask_housekeeping(mask);
1037 	if (err)
1038 		goto out;
1039 
1040 	err = netdev_rx_queue_set_rps_mask(queue, mask);
1041 
1042 out:
1043 	free_cpumask_var(mask);
1044 	return err ? : len;
1045 }
1046 
1047 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
1048 					   char *buf)
1049 {
1050 	struct rps_dev_flow_table *flow_table;
1051 	unsigned long val = 0;
1052 
1053 	rcu_read_lock();
1054 	flow_table = rcu_dereference(queue->rps_flow_table);
1055 	if (flow_table)
1056 		val = (unsigned long)flow_table->mask + 1;
1057 	rcu_read_unlock();
1058 
1059 	return sysfs_emit(buf, "%lu\n", val);
1060 }
1061 
1062 static void rps_dev_flow_table_release(struct rcu_head *rcu)
1063 {
1064 	struct rps_dev_flow_table *table = container_of(rcu,
1065 	    struct rps_dev_flow_table, rcu);
1066 	vfree(table);
1067 }
1068 
1069 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
1070 					    const char *buf, size_t len)
1071 {
1072 	unsigned long mask, count;
1073 	struct rps_dev_flow_table *table, *old_table;
1074 	static DEFINE_SPINLOCK(rps_dev_flow_lock);
1075 	int rc;
1076 
1077 	if (!capable(CAP_NET_ADMIN))
1078 		return -EPERM;
1079 
1080 	rc = kstrtoul(buf, 0, &count);
1081 	if (rc < 0)
1082 		return rc;
1083 
1084 	if (count) {
1085 		mask = count - 1;
1086 		/* mask = roundup_pow_of_two(count) - 1;
1087 		 * without overflows...
1088 		 */
1089 		while ((mask | (mask >> 1)) != mask)
1090 			mask |= (mask >> 1);
1091 		/* On 64 bit arches, must check mask fits in table->mask (u32),
1092 		 * and on 32bit arches, must check
1093 		 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
1094 		 */
1095 #if BITS_PER_LONG > 32
1096 		if (mask > (unsigned long)(u32)mask)
1097 			return -EINVAL;
1098 #else
1099 		if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
1100 				/ sizeof(struct rps_dev_flow)) {
1101 			/* Enforce a limit to prevent overflow */
1102 			return -EINVAL;
1103 		}
1104 #endif
1105 		table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
1106 		if (!table)
1107 			return -ENOMEM;
1108 
1109 		table->mask = mask;
1110 		for (count = 0; count <= mask; count++)
1111 			table->flows[count].cpu = RPS_NO_CPU;
1112 	} else {
1113 		table = NULL;
1114 	}
1115 
1116 	spin_lock(&rps_dev_flow_lock);
1117 	old_table = rcu_dereference_protected(queue->rps_flow_table,
1118 					      lockdep_is_held(&rps_dev_flow_lock));
1119 	rcu_assign_pointer(queue->rps_flow_table, table);
1120 	spin_unlock(&rps_dev_flow_lock);
1121 
1122 	if (old_table)
1123 		call_rcu(&old_table->rcu, rps_dev_flow_table_release);
1124 
1125 	return len;
1126 }
1127 
1128 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
1129 	= __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
1130 
1131 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
1132 	= __ATTR(rps_flow_cnt, 0644,
1133 		 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
1134 #endif /* CONFIG_RPS */
1135 
1136 static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
1137 #ifdef CONFIG_RPS
1138 	&rps_cpus_attribute.attr,
1139 	&rps_dev_flow_table_cnt_attribute.attr,
1140 #endif
1141 	NULL
1142 };
1143 ATTRIBUTE_GROUPS(rx_queue_default);
1144 
1145 static void rx_queue_release(struct kobject *kobj)
1146 {
1147 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
1148 #ifdef CONFIG_RPS
1149 	struct rps_map *map;
1150 	struct rps_dev_flow_table *flow_table;
1151 
1152 	map = rcu_dereference_protected(queue->rps_map, 1);
1153 	if (map) {
1154 		RCU_INIT_POINTER(queue->rps_map, NULL);
1155 		kfree_rcu(map, rcu);
1156 	}
1157 
1158 	flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
1159 	if (flow_table) {
1160 		RCU_INIT_POINTER(queue->rps_flow_table, NULL);
1161 		call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
1162 	}
1163 #endif
1164 
1165 	memset(kobj, 0, sizeof(*kobj));
1166 	netdev_put(queue->dev, &queue->dev_tracker);
1167 }
1168 
1169 static const void *rx_queue_namespace(const struct kobject *kobj)
1170 {
1171 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
1172 	struct device *dev = &queue->dev->dev;
1173 	const void *ns = NULL;
1174 
1175 	if (dev->class && dev->class->namespace)
1176 		ns = dev->class->namespace(dev);
1177 
1178 	return ns;
1179 }
1180 
1181 static void rx_queue_get_ownership(const struct kobject *kobj,
1182 				   kuid_t *uid, kgid_t *gid)
1183 {
1184 	const struct net *net = rx_queue_namespace(kobj);
1185 
1186 	net_ns_get_ownership(net, uid, gid);
1187 }
1188 
1189 static const struct kobj_type rx_queue_ktype = {
1190 	.sysfs_ops = &rx_queue_sysfs_ops,
1191 	.release = rx_queue_release,
1192 	.namespace = rx_queue_namespace,
1193 	.get_ownership = rx_queue_get_ownership,
1194 };
1195 
1196 static int rx_queue_default_mask(struct net_device *dev,
1197 				 struct netdev_rx_queue *queue)
1198 {
1199 #if IS_ENABLED(CONFIG_RPS) && IS_ENABLED(CONFIG_SYSCTL)
1200 	struct cpumask *rps_default_mask = READ_ONCE(dev_net(dev)->core.rps_default_mask);
1201 
1202 	if (rps_default_mask && !cpumask_empty(rps_default_mask))
1203 		return netdev_rx_queue_set_rps_mask(queue, rps_default_mask);
1204 #endif
1205 	return 0;
1206 }
1207 
1208 static int rx_queue_add_kobject(struct net_device *dev, int index)
1209 {
1210 	struct netdev_rx_queue *queue = dev->_rx + index;
1211 	struct kobject *kobj = &queue->kobj;
1212 	int error = 0;
1213 
1214 	/* Rx queues are cleared in rx_queue_release to allow later
1215 	 * re-registration. This is triggered when their kobj refcount is
1216 	 * dropped.
1217 	 *
1218 	 * If a queue is removed while both a read (or write) operation and a
1219 	 * the re-addition of the same queue are pending (waiting on rntl_lock)
1220 	 * it might happen that the re-addition will execute before the read,
1221 	 * making the initial removal to never happen (queue's kobj refcount
1222 	 * won't drop enough because of the pending read). In such rare case,
1223 	 * return to allow the removal operation to complete.
1224 	 */
1225 	if (unlikely(kobj->state_initialized)) {
1226 		netdev_warn_once(dev, "Cannot re-add rx queues before their removal completed");
1227 		return -EAGAIN;
1228 	}
1229 
1230 	/* Kobject_put later will trigger rx_queue_release call which
1231 	 * decreases dev refcount: Take that reference here
1232 	 */
1233 	netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1234 
1235 	kobj->kset = dev->queues_kset;
1236 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1237 				     "rx-%u", index);
1238 	if (error)
1239 		goto err;
1240 
1241 	queue->groups = rx_queue_default_groups;
1242 	error = sysfs_create_groups(kobj, queue->groups);
1243 	if (error)
1244 		goto err;
1245 
1246 	if (dev->sysfs_rx_queue_group) {
1247 		error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1248 		if (error)
1249 			goto err_default_groups;
1250 	}
1251 
1252 	error = rx_queue_default_mask(dev, queue);
1253 	if (error)
1254 		goto err_default_groups;
1255 
1256 	kobject_uevent(kobj, KOBJ_ADD);
1257 
1258 	return error;
1259 
1260 err_default_groups:
1261 	sysfs_remove_groups(kobj, queue->groups);
1262 err:
1263 	kobject_put(kobj);
1264 	return error;
1265 }
1266 
1267 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
1268 				 kgid_t kgid)
1269 {
1270 	struct netdev_rx_queue *queue = dev->_rx + index;
1271 	struct kobject *kobj = &queue->kobj;
1272 	int error;
1273 
1274 	error = sysfs_change_owner(kobj, kuid, kgid);
1275 	if (error)
1276 		return error;
1277 
1278 	if (dev->sysfs_rx_queue_group)
1279 		error = sysfs_group_change_owner(
1280 			kobj, dev->sysfs_rx_queue_group, kuid, kgid);
1281 
1282 	return error;
1283 }
1284 #endif /* CONFIG_SYSFS */
1285 
1286 int
1287 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1288 {
1289 #ifdef CONFIG_SYSFS
1290 	int i;
1291 	int error = 0;
1292 
1293 #ifndef CONFIG_RPS
1294 	if (!dev->sysfs_rx_queue_group)
1295 		return 0;
1296 #endif
1297 	for (i = old_num; i < new_num; i++) {
1298 		error = rx_queue_add_kobject(dev, i);
1299 		if (error) {
1300 			new_num = old_num;
1301 			break;
1302 		}
1303 	}
1304 
1305 	while (--i >= new_num) {
1306 		struct netdev_rx_queue *queue = &dev->_rx[i];
1307 		struct kobject *kobj = &queue->kobj;
1308 
1309 		if (!refcount_read(&dev_net(dev)->ns.count))
1310 			kobj->uevent_suppress = 1;
1311 		if (dev->sysfs_rx_queue_group)
1312 			sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1313 		sysfs_remove_groups(kobj, queue->groups);
1314 		kobject_put(kobj);
1315 	}
1316 
1317 	return error;
1318 #else
1319 	return 0;
1320 #endif
1321 }
1322 
1323 static int net_rx_queue_change_owner(struct net_device *dev, int num,
1324 				     kuid_t kuid, kgid_t kgid)
1325 {
1326 #ifdef CONFIG_SYSFS
1327 	int error = 0;
1328 	int i;
1329 
1330 #ifndef CONFIG_RPS
1331 	if (!dev->sysfs_rx_queue_group)
1332 		return 0;
1333 #endif
1334 	for (i = 0; i < num; i++) {
1335 		error = rx_queue_change_owner(dev, i, kuid, kgid);
1336 		if (error)
1337 			break;
1338 	}
1339 
1340 	return error;
1341 #else
1342 	return 0;
1343 #endif
1344 }
1345 
1346 #ifdef CONFIG_SYSFS
1347 /*
1348  * netdev_queue sysfs structures and functions.
1349  */
1350 struct netdev_queue_attribute {
1351 	struct attribute attr;
1352 	ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
1353 			struct netdev_queue *queue, char *buf);
1354 	ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
1355 			 struct netdev_queue *queue, const char *buf,
1356 			 size_t len);
1357 };
1358 #define to_netdev_queue_attr(_attr) \
1359 	container_of(_attr, struct netdev_queue_attribute, attr)
1360 
1361 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1362 
1363 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1364 				      struct attribute *attr, char *buf)
1365 {
1366 	const struct netdev_queue_attribute *attribute
1367 		= to_netdev_queue_attr(attr);
1368 	struct netdev_queue *queue = to_netdev_queue(kobj);
1369 
1370 	if (!attribute->show)
1371 		return -EIO;
1372 
1373 	return attribute->show(kobj, attr, queue, buf);
1374 }
1375 
1376 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1377 				       struct attribute *attr,
1378 				       const char *buf, size_t count)
1379 {
1380 	const struct netdev_queue_attribute *attribute
1381 		= to_netdev_queue_attr(attr);
1382 	struct netdev_queue *queue = to_netdev_queue(kobj);
1383 
1384 	if (!attribute->store)
1385 		return -EIO;
1386 
1387 	return attribute->store(kobj, attr, queue, buf, count);
1388 }
1389 
1390 static const struct sysfs_ops netdev_queue_sysfs_ops = {
1391 	.show = netdev_queue_attr_show,
1392 	.store = netdev_queue_attr_store,
1393 };
1394 
1395 static ssize_t tx_timeout_show(struct kobject *kobj, struct attribute *attr,
1396 			       struct netdev_queue *queue, char *buf)
1397 {
1398 	unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout);
1399 
1400 	return sysfs_emit(buf, fmt_ulong, trans_timeout);
1401 }
1402 
1403 static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1404 {
1405 	struct net_device *dev = queue->dev;
1406 	unsigned int i;
1407 
1408 	i = queue - dev->_tx;
1409 	BUG_ON(i >= dev->num_tx_queues);
1410 
1411 	return i;
1412 }
1413 
1414 static ssize_t traffic_class_show(struct kobject *kobj, struct attribute *attr,
1415 				  struct netdev_queue *queue, char *buf)
1416 {
1417 	struct net_device *dev = queue->dev;
1418 	int num_tc, tc, index, ret;
1419 
1420 	if (!netif_is_multiqueue(dev))
1421 		return -ENOENT;
1422 
1423 	ret = sysfs_rtnl_lock(kobj, attr, queue->dev);
1424 	if (ret)
1425 		return ret;
1426 
1427 	index = get_netdev_queue_index(queue);
1428 
1429 	/* If queue belongs to subordinate dev use its TC mapping */
1430 	dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1431 
1432 	num_tc = dev->num_tc;
1433 	tc = netdev_txq_to_tc(dev, index);
1434 
1435 	rtnl_unlock();
1436 
1437 	if (tc < 0)
1438 		return -EINVAL;
1439 
1440 	/* We can report the traffic class one of two ways:
1441 	 * Subordinate device traffic classes are reported with the traffic
1442 	 * class first, and then the subordinate class so for example TC0 on
1443 	 * subordinate device 2 will be reported as "0-2". If the queue
1444 	 * belongs to the root device it will be reported with just the
1445 	 * traffic class, so just "0" for TC 0 for example.
1446 	 */
1447 	return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) :
1448 			    sysfs_emit(buf, "%d\n", tc);
1449 }
1450 
1451 #ifdef CONFIG_XPS
1452 static ssize_t tx_maxrate_show(struct kobject *kobj, struct attribute *attr,
1453 			       struct netdev_queue *queue, char *buf)
1454 {
1455 	return sysfs_emit(buf, "%lu\n", queue->tx_maxrate);
1456 }
1457 
1458 static ssize_t tx_maxrate_store(struct kobject *kobj, struct attribute *attr,
1459 				struct netdev_queue *queue, const char *buf,
1460 				size_t len)
1461 {
1462 	int err, index = get_netdev_queue_index(queue);
1463 	struct net_device *dev = queue->dev;
1464 	u32 rate = 0;
1465 
1466 	if (!capable(CAP_NET_ADMIN))
1467 		return -EPERM;
1468 
1469 	/* The check is also done later; this helps returning early without
1470 	 * hitting the locking section below.
1471 	 */
1472 	if (!dev->netdev_ops->ndo_set_tx_maxrate)
1473 		return -EOPNOTSUPP;
1474 
1475 	err = kstrtou32(buf, 10, &rate);
1476 	if (err < 0)
1477 		return err;
1478 
1479 	err = sysfs_rtnl_lock(kobj, attr, dev);
1480 	if (err)
1481 		return err;
1482 
1483 	err = -EOPNOTSUPP;
1484 	netdev_lock_ops(dev);
1485 	if (dev->netdev_ops->ndo_set_tx_maxrate)
1486 		err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1487 	netdev_unlock_ops(dev);
1488 
1489 	if (!err) {
1490 		queue->tx_maxrate = rate;
1491 		rtnl_unlock();
1492 		return len;
1493 	}
1494 
1495 	rtnl_unlock();
1496 	return err;
1497 }
1498 
1499 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1500 	= __ATTR_RW(tx_maxrate);
1501 #endif
1502 
1503 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1504 	= __ATTR_RO(tx_timeout);
1505 
1506 static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1507 	= __ATTR_RO(traffic_class);
1508 
1509 #ifdef CONFIG_BQL
1510 /*
1511  * Byte queue limits sysfs structures and functions.
1512  */
1513 static ssize_t bql_show(char *buf, unsigned int value)
1514 {
1515 	return sysfs_emit(buf, "%u\n", value);
1516 }
1517 
1518 static ssize_t bql_set(const char *buf, const size_t count,
1519 		       unsigned int *pvalue)
1520 {
1521 	unsigned int value;
1522 	int err;
1523 
1524 	if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1525 		value = DQL_MAX_LIMIT;
1526 	} else {
1527 		err = kstrtouint(buf, 10, &value);
1528 		if (err < 0)
1529 			return err;
1530 		if (value > DQL_MAX_LIMIT)
1531 			return -EINVAL;
1532 	}
1533 
1534 	*pvalue = value;
1535 
1536 	return count;
1537 }
1538 
1539 static ssize_t bql_show_hold_time(struct kobject *kobj, struct attribute *attr,
1540 				  struct netdev_queue *queue, char *buf)
1541 {
1542 	struct dql *dql = &queue->dql;
1543 
1544 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1545 }
1546 
1547 static ssize_t bql_set_hold_time(struct kobject *kobj, struct attribute *attr,
1548 				 struct netdev_queue *queue, const char *buf,
1549 				 size_t len)
1550 {
1551 	struct dql *dql = &queue->dql;
1552 	unsigned int value;
1553 	int err;
1554 
1555 	err = kstrtouint(buf, 10, &value);
1556 	if (err < 0)
1557 		return err;
1558 
1559 	dql->slack_hold_time = msecs_to_jiffies(value);
1560 
1561 	return len;
1562 }
1563 
1564 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1565 	= __ATTR(hold_time, 0644,
1566 		 bql_show_hold_time, bql_set_hold_time);
1567 
1568 static ssize_t bql_show_stall_thrs(struct kobject *kobj, struct attribute *attr,
1569 				   struct netdev_queue *queue, char *buf)
1570 {
1571 	struct dql *dql = &queue->dql;
1572 
1573 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->stall_thrs));
1574 }
1575 
1576 static ssize_t bql_set_stall_thrs(struct kobject *kobj, struct attribute *attr,
1577 				  struct netdev_queue *queue, const char *buf,
1578 				  size_t len)
1579 {
1580 	struct dql *dql = &queue->dql;
1581 	unsigned int value;
1582 	int err;
1583 
1584 	err = kstrtouint(buf, 10, &value);
1585 	if (err < 0)
1586 		return err;
1587 
1588 	value = msecs_to_jiffies(value);
1589 	if (value && (value < 4 || value > 4 / 2 * BITS_PER_LONG))
1590 		return -ERANGE;
1591 
1592 	if (!dql->stall_thrs && value)
1593 		dql->last_reap = jiffies;
1594 	/* Force last_reap to be live */
1595 	smp_wmb();
1596 	dql->stall_thrs = value;
1597 
1598 	return len;
1599 }
1600 
1601 static struct netdev_queue_attribute bql_stall_thrs_attribute __ro_after_init =
1602 	__ATTR(stall_thrs, 0644, bql_show_stall_thrs, bql_set_stall_thrs);
1603 
1604 static ssize_t bql_show_stall_max(struct kobject *kobj, struct attribute *attr,
1605 				  struct netdev_queue *queue, char *buf)
1606 {
1607 	return sysfs_emit(buf, "%u\n", READ_ONCE(queue->dql.stall_max));
1608 }
1609 
1610 static ssize_t bql_set_stall_max(struct kobject *kobj, struct attribute *attr,
1611 				 struct netdev_queue *queue, const char *buf,
1612 				 size_t len)
1613 {
1614 	WRITE_ONCE(queue->dql.stall_max, 0);
1615 	return len;
1616 }
1617 
1618 static struct netdev_queue_attribute bql_stall_max_attribute __ro_after_init =
1619 	__ATTR(stall_max, 0644, bql_show_stall_max, bql_set_stall_max);
1620 
1621 static ssize_t bql_show_stall_cnt(struct kobject *kobj, struct attribute *attr,
1622 				  struct netdev_queue *queue, char *buf)
1623 {
1624 	struct dql *dql = &queue->dql;
1625 
1626 	return sysfs_emit(buf, "%lu\n", dql->stall_cnt);
1627 }
1628 
1629 static struct netdev_queue_attribute bql_stall_cnt_attribute __ro_after_init =
1630 	__ATTR(stall_cnt, 0444, bql_show_stall_cnt, NULL);
1631 
1632 static ssize_t bql_show_inflight(struct kobject *kobj, struct attribute *attr,
1633 				 struct netdev_queue *queue, char *buf)
1634 {
1635 	struct dql *dql = &queue->dql;
1636 
1637 	return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed);
1638 }
1639 
1640 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1641 	__ATTR(inflight, 0444, bql_show_inflight, NULL);
1642 
1643 #define BQL_ATTR(NAME, FIELD)						\
1644 static ssize_t bql_show_ ## NAME(struct kobject *kobj,			\
1645 				 struct attribute *attr,		\
1646 				 struct netdev_queue *queue, char *buf)	\
1647 {									\
1648 	return bql_show(buf, queue->dql.FIELD);				\
1649 }									\
1650 									\
1651 static ssize_t bql_set_ ## NAME(struct kobject *kobj,			\
1652 				struct attribute *attr,			\
1653 				struct netdev_queue *queue,		\
1654 				const char *buf, size_t len)		\
1655 {									\
1656 	return bql_set(buf, len, &queue->dql.FIELD);			\
1657 }									\
1658 									\
1659 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1660 	= __ATTR(NAME, 0644,				\
1661 		 bql_show_ ## NAME, bql_set_ ## NAME)
1662 
1663 BQL_ATTR(limit, limit);
1664 BQL_ATTR(limit_max, max_limit);
1665 BQL_ATTR(limit_min, min_limit);
1666 
1667 static struct attribute *dql_attrs[] __ro_after_init = {
1668 	&bql_limit_attribute.attr,
1669 	&bql_limit_max_attribute.attr,
1670 	&bql_limit_min_attribute.attr,
1671 	&bql_hold_time_attribute.attr,
1672 	&bql_inflight_attribute.attr,
1673 	&bql_stall_thrs_attribute.attr,
1674 	&bql_stall_cnt_attribute.attr,
1675 	&bql_stall_max_attribute.attr,
1676 	NULL
1677 };
1678 
1679 static const struct attribute_group dql_group = {
1680 	.name  = "byte_queue_limits",
1681 	.attrs  = dql_attrs,
1682 };
1683 #else
1684 /* Fake declaration, all the code using it should be dead */
1685 static const struct attribute_group dql_group = {};
1686 #endif /* CONFIG_BQL */
1687 
1688 #ifdef CONFIG_XPS
1689 static ssize_t xps_queue_show(struct net_device *dev, unsigned int index,
1690 			      int tc, char *buf, enum xps_map_type type)
1691 {
1692 	struct xps_dev_maps *dev_maps;
1693 	unsigned long *mask;
1694 	unsigned int nr_ids;
1695 	int j, len;
1696 
1697 	rcu_read_lock();
1698 	dev_maps = rcu_dereference(dev->xps_maps[type]);
1699 
1700 	/* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0
1701 	 * when dev_maps hasn't been allocated yet, to be backward compatible.
1702 	 */
1703 	nr_ids = dev_maps ? dev_maps->nr_ids :
1704 		 (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues);
1705 
1706 	mask = bitmap_zalloc(nr_ids, GFP_NOWAIT);
1707 	if (!mask) {
1708 		rcu_read_unlock();
1709 		return -ENOMEM;
1710 	}
1711 
1712 	if (!dev_maps || tc >= dev_maps->num_tc)
1713 		goto out_no_maps;
1714 
1715 	for (j = 0; j < nr_ids; j++) {
1716 		int i, tci = j * dev_maps->num_tc + tc;
1717 		struct xps_map *map;
1718 
1719 		map = rcu_dereference(dev_maps->attr_map[tci]);
1720 		if (!map)
1721 			continue;
1722 
1723 		for (i = map->len; i--;) {
1724 			if (map->queues[i] == index) {
1725 				__set_bit(j, mask);
1726 				break;
1727 			}
1728 		}
1729 	}
1730 out_no_maps:
1731 	rcu_read_unlock();
1732 
1733 	len = bitmap_print_to_pagebuf(false, buf, mask, nr_ids);
1734 	bitmap_free(mask);
1735 
1736 	return len < PAGE_SIZE ? len : -EINVAL;
1737 }
1738 
1739 static ssize_t xps_cpus_show(struct kobject *kobj, struct attribute *attr,
1740 			     struct netdev_queue *queue, char *buf)
1741 {
1742 	struct net_device *dev = queue->dev;
1743 	unsigned int index;
1744 	int len, tc, ret;
1745 
1746 	if (!netif_is_multiqueue(dev))
1747 		return -ENOENT;
1748 
1749 	index = get_netdev_queue_index(queue);
1750 
1751 	ret = sysfs_rtnl_lock(kobj, attr, queue->dev);
1752 	if (ret)
1753 		return ret;
1754 
1755 	/* If queue belongs to subordinate dev use its map */
1756 	dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1757 
1758 	tc = netdev_txq_to_tc(dev, index);
1759 	if (tc < 0) {
1760 		rtnl_unlock();
1761 		return -EINVAL;
1762 	}
1763 
1764 	/* Increase the net device refcnt to make sure it won't be freed while
1765 	 * xps_queue_show is running.
1766 	 */
1767 	dev_hold(dev);
1768 	rtnl_unlock();
1769 
1770 	len = xps_queue_show(dev, index, tc, buf, XPS_CPUS);
1771 
1772 	dev_put(dev);
1773 	return len;
1774 }
1775 
1776 static ssize_t xps_cpus_store(struct kobject *kobj, struct attribute *attr,
1777 			      struct netdev_queue *queue, const char *buf,
1778 			      size_t len)
1779 {
1780 	struct net_device *dev = queue->dev;
1781 	unsigned int index;
1782 	cpumask_var_t mask;
1783 	int err;
1784 
1785 	if (!netif_is_multiqueue(dev))
1786 		return -ENOENT;
1787 
1788 	if (!capable(CAP_NET_ADMIN))
1789 		return -EPERM;
1790 
1791 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1792 		return -ENOMEM;
1793 
1794 	index = get_netdev_queue_index(queue);
1795 
1796 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1797 	if (err) {
1798 		free_cpumask_var(mask);
1799 		return err;
1800 	}
1801 
1802 	err = sysfs_rtnl_lock(kobj, attr, dev);
1803 	if (err) {
1804 		free_cpumask_var(mask);
1805 		return err;
1806 	}
1807 
1808 	err = netif_set_xps_queue(dev, mask, index);
1809 	rtnl_unlock();
1810 
1811 	free_cpumask_var(mask);
1812 
1813 	return err ? : len;
1814 }
1815 
1816 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1817 	= __ATTR_RW(xps_cpus);
1818 
1819 static ssize_t xps_rxqs_show(struct kobject *kobj, struct attribute *attr,
1820 			     struct netdev_queue *queue, char *buf)
1821 {
1822 	struct net_device *dev = queue->dev;
1823 	unsigned int index;
1824 	int tc, ret;
1825 
1826 	index = get_netdev_queue_index(queue);
1827 
1828 	ret = sysfs_rtnl_lock(kobj, attr, dev);
1829 	if (ret)
1830 		return ret;
1831 
1832 	tc = netdev_txq_to_tc(dev, index);
1833 
1834 	/* Increase the net device refcnt to make sure it won't be freed while
1835 	 * xps_queue_show is running.
1836 	 */
1837 	dev_hold(dev);
1838 	rtnl_unlock();
1839 
1840 	ret = tc >= 0 ? xps_queue_show(dev, index, tc, buf, XPS_RXQS) : -EINVAL;
1841 	dev_put(dev);
1842 	return ret;
1843 }
1844 
1845 static ssize_t xps_rxqs_store(struct kobject *kobj, struct attribute *attr,
1846 			      struct netdev_queue *queue, const char *buf,
1847 			      size_t len)
1848 {
1849 	struct net_device *dev = queue->dev;
1850 	struct net *net = dev_net(dev);
1851 	unsigned long *mask;
1852 	unsigned int index;
1853 	int err;
1854 
1855 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1856 		return -EPERM;
1857 
1858 	mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1859 	if (!mask)
1860 		return -ENOMEM;
1861 
1862 	index = get_netdev_queue_index(queue);
1863 
1864 	err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1865 	if (err) {
1866 		bitmap_free(mask);
1867 		return err;
1868 	}
1869 
1870 	err = sysfs_rtnl_lock(kobj, attr, dev);
1871 	if (err) {
1872 		bitmap_free(mask);
1873 		return err;
1874 	}
1875 
1876 	cpus_read_lock();
1877 	err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS);
1878 	cpus_read_unlock();
1879 
1880 	rtnl_unlock();
1881 
1882 	bitmap_free(mask);
1883 	return err ? : len;
1884 }
1885 
1886 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1887 	= __ATTR_RW(xps_rxqs);
1888 #endif /* CONFIG_XPS */
1889 
1890 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1891 	&queue_trans_timeout.attr,
1892 	&queue_traffic_class.attr,
1893 #ifdef CONFIG_XPS
1894 	&xps_cpus_attribute.attr,
1895 	&xps_rxqs_attribute.attr,
1896 	&queue_tx_maxrate.attr,
1897 #endif
1898 	NULL
1899 };
1900 ATTRIBUTE_GROUPS(netdev_queue_default);
1901 
1902 static void netdev_queue_release(struct kobject *kobj)
1903 {
1904 	struct netdev_queue *queue = to_netdev_queue(kobj);
1905 
1906 	memset(kobj, 0, sizeof(*kobj));
1907 	netdev_put(queue->dev, &queue->dev_tracker);
1908 }
1909 
1910 static const void *netdev_queue_namespace(const struct kobject *kobj)
1911 {
1912 	struct netdev_queue *queue = to_netdev_queue(kobj);
1913 	struct device *dev = &queue->dev->dev;
1914 	const void *ns = NULL;
1915 
1916 	if (dev->class && dev->class->namespace)
1917 		ns = dev->class->namespace(dev);
1918 
1919 	return ns;
1920 }
1921 
1922 static void netdev_queue_get_ownership(const struct kobject *kobj,
1923 				       kuid_t *uid, kgid_t *gid)
1924 {
1925 	const struct net *net = netdev_queue_namespace(kobj);
1926 
1927 	net_ns_get_ownership(net, uid, gid);
1928 }
1929 
1930 static const struct kobj_type netdev_queue_ktype = {
1931 	.sysfs_ops = &netdev_queue_sysfs_ops,
1932 	.release = netdev_queue_release,
1933 	.namespace = netdev_queue_namespace,
1934 	.get_ownership = netdev_queue_get_ownership,
1935 };
1936 
1937 static bool netdev_uses_bql(const struct net_device *dev)
1938 {
1939 	if (dev->lltx || (dev->priv_flags & IFF_NO_QUEUE))
1940 		return false;
1941 
1942 	return IS_ENABLED(CONFIG_BQL);
1943 }
1944 
1945 static int netdev_queue_add_kobject(struct net_device *dev, int index)
1946 {
1947 	struct netdev_queue *queue = dev->_tx + index;
1948 	struct kobject *kobj = &queue->kobj;
1949 	int error = 0;
1950 
1951 	/* Tx queues are cleared in netdev_queue_release to allow later
1952 	 * re-registration. This is triggered when their kobj refcount is
1953 	 * dropped.
1954 	 *
1955 	 * If a queue is removed while both a read (or write) operation and a
1956 	 * the re-addition of the same queue are pending (waiting on rntl_lock)
1957 	 * it might happen that the re-addition will execute before the read,
1958 	 * making the initial removal to never happen (queue's kobj refcount
1959 	 * won't drop enough because of the pending read). In such rare case,
1960 	 * return to allow the removal operation to complete.
1961 	 */
1962 	if (unlikely(kobj->state_initialized)) {
1963 		netdev_warn_once(dev, "Cannot re-add tx queues before their removal completed");
1964 		return -EAGAIN;
1965 	}
1966 
1967 	/* Kobject_put later will trigger netdev_queue_release call
1968 	 * which decreases dev refcount: Take that reference here
1969 	 */
1970 	netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1971 
1972 	kobj->kset = dev->queues_kset;
1973 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1974 				     "tx-%u", index);
1975 	if (error)
1976 		goto err;
1977 
1978 	queue->groups = netdev_queue_default_groups;
1979 	error = sysfs_create_groups(kobj, queue->groups);
1980 	if (error)
1981 		goto err;
1982 
1983 	if (netdev_uses_bql(dev)) {
1984 		error = sysfs_create_group(kobj, &dql_group);
1985 		if (error)
1986 			goto err_default_groups;
1987 	}
1988 
1989 	kobject_uevent(kobj, KOBJ_ADD);
1990 	return 0;
1991 
1992 err_default_groups:
1993 	sysfs_remove_groups(kobj, queue->groups);
1994 err:
1995 	kobject_put(kobj);
1996 	return error;
1997 }
1998 
1999 static int tx_queue_change_owner(struct net_device *ndev, int index,
2000 				 kuid_t kuid, kgid_t kgid)
2001 {
2002 	struct netdev_queue *queue = ndev->_tx + index;
2003 	struct kobject *kobj = &queue->kobj;
2004 	int error;
2005 
2006 	error = sysfs_change_owner(kobj, kuid, kgid);
2007 	if (error)
2008 		return error;
2009 
2010 	if (netdev_uses_bql(ndev))
2011 		error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
2012 
2013 	return error;
2014 }
2015 #endif /* CONFIG_SYSFS */
2016 
2017 int
2018 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
2019 {
2020 #ifdef CONFIG_SYSFS
2021 	int i;
2022 	int error = 0;
2023 
2024 	/* Tx queue kobjects are allowed to be updated when a device is being
2025 	 * unregistered, but solely to remove queues from qdiscs. Any path
2026 	 * adding queues should be fixed.
2027 	 */
2028 	WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
2029 	     "New queues can't be registered after device unregistration.");
2030 
2031 	for (i = old_num; i < new_num; i++) {
2032 		error = netdev_queue_add_kobject(dev, i);
2033 		if (error) {
2034 			new_num = old_num;
2035 			break;
2036 		}
2037 	}
2038 
2039 	while (--i >= new_num) {
2040 		struct netdev_queue *queue = dev->_tx + i;
2041 
2042 		if (!refcount_read(&dev_net(dev)->ns.count))
2043 			queue->kobj.uevent_suppress = 1;
2044 
2045 		if (netdev_uses_bql(dev))
2046 			sysfs_remove_group(&queue->kobj, &dql_group);
2047 
2048 		sysfs_remove_groups(&queue->kobj, queue->groups);
2049 		kobject_put(&queue->kobj);
2050 	}
2051 
2052 	return error;
2053 #else
2054 	return 0;
2055 #endif /* CONFIG_SYSFS */
2056 }
2057 
2058 static int net_tx_queue_change_owner(struct net_device *dev, int num,
2059 				     kuid_t kuid, kgid_t kgid)
2060 {
2061 #ifdef CONFIG_SYSFS
2062 	int error = 0;
2063 	int i;
2064 
2065 	for (i = 0; i < num; i++) {
2066 		error = tx_queue_change_owner(dev, i, kuid, kgid);
2067 		if (error)
2068 			break;
2069 	}
2070 
2071 	return error;
2072 #else
2073 	return 0;
2074 #endif /* CONFIG_SYSFS */
2075 }
2076 
2077 static int register_queue_kobjects(struct net_device *dev)
2078 {
2079 	int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
2080 
2081 #ifdef CONFIG_SYSFS
2082 	dev->queues_kset = kset_create_and_add("queues",
2083 					       NULL, &dev->dev.kobj);
2084 	if (!dev->queues_kset)
2085 		return -ENOMEM;
2086 	real_rx = dev->real_num_rx_queues;
2087 #endif
2088 	real_tx = dev->real_num_tx_queues;
2089 
2090 	error = net_rx_queue_update_kobjects(dev, 0, real_rx);
2091 	if (error)
2092 		goto error;
2093 	rxq = real_rx;
2094 
2095 	error = netdev_queue_update_kobjects(dev, 0, real_tx);
2096 	if (error)
2097 		goto error;
2098 	txq = real_tx;
2099 
2100 	return 0;
2101 
2102 error:
2103 	netdev_queue_update_kobjects(dev, txq, 0);
2104 	net_rx_queue_update_kobjects(dev, rxq, 0);
2105 #ifdef CONFIG_SYSFS
2106 	kset_unregister(dev->queues_kset);
2107 #endif
2108 	return error;
2109 }
2110 
2111 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
2112 {
2113 	int error = 0, real_rx = 0, real_tx = 0;
2114 
2115 #ifdef CONFIG_SYSFS
2116 	if (ndev->queues_kset) {
2117 		error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
2118 		if (error)
2119 			return error;
2120 	}
2121 	real_rx = ndev->real_num_rx_queues;
2122 #endif
2123 	real_tx = ndev->real_num_tx_queues;
2124 
2125 	error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
2126 	if (error)
2127 		return error;
2128 
2129 	error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
2130 	if (error)
2131 		return error;
2132 
2133 	return 0;
2134 }
2135 
2136 static void remove_queue_kobjects(struct net_device *dev)
2137 {
2138 	int real_rx = 0, real_tx = 0;
2139 
2140 #ifdef CONFIG_SYSFS
2141 	real_rx = dev->real_num_rx_queues;
2142 #endif
2143 	real_tx = dev->real_num_tx_queues;
2144 
2145 	net_rx_queue_update_kobjects(dev, real_rx, 0);
2146 	netdev_queue_update_kobjects(dev, real_tx, 0);
2147 
2148 	dev->real_num_rx_queues = 0;
2149 	dev->real_num_tx_queues = 0;
2150 #ifdef CONFIG_SYSFS
2151 	kset_unregister(dev->queues_kset);
2152 #endif
2153 }
2154 
2155 static bool net_current_may_mount(void)
2156 {
2157 	struct net *net = current->nsproxy->net_ns;
2158 
2159 	return ns_capable(net->user_ns, CAP_SYS_ADMIN);
2160 }
2161 
2162 static void *net_grab_current_ns(void)
2163 {
2164 	struct net *ns = current->nsproxy->net_ns;
2165 #ifdef CONFIG_NET_NS
2166 	if (ns)
2167 		refcount_inc(&ns->passive);
2168 #endif
2169 	return ns;
2170 }
2171 
2172 static const void *net_initial_ns(void)
2173 {
2174 	return &init_net;
2175 }
2176 
2177 static const void *net_netlink_ns(struct sock *sk)
2178 {
2179 	return sock_net(sk);
2180 }
2181 
2182 const struct kobj_ns_type_operations net_ns_type_operations = {
2183 	.type = KOBJ_NS_TYPE_NET,
2184 	.current_may_mount = net_current_may_mount,
2185 	.grab_current_ns = net_grab_current_ns,
2186 	.netlink_ns = net_netlink_ns,
2187 	.initial_ns = net_initial_ns,
2188 	.drop_ns = net_drop_ns,
2189 };
2190 EXPORT_SYMBOL_GPL(net_ns_type_operations);
2191 
2192 static int netdev_uevent(const struct device *d, struct kobj_uevent_env *env)
2193 {
2194 	const struct net_device *dev = to_net_dev(d);
2195 	int retval;
2196 
2197 	/* pass interface to uevent. */
2198 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
2199 	if (retval)
2200 		goto exit;
2201 
2202 	/* pass ifindex to uevent.
2203 	 * ifindex is useful as it won't change (interface name may change)
2204 	 * and is what RtNetlink uses natively.
2205 	 */
2206 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
2207 
2208 exit:
2209 	return retval;
2210 }
2211 
2212 /*
2213  *	netdev_release -- destroy and free a dead device.
2214  *	Called when last reference to device kobject is gone.
2215  */
2216 static void netdev_release(struct device *d)
2217 {
2218 	struct net_device *dev = to_net_dev(d);
2219 
2220 	BUG_ON(dev->reg_state != NETREG_RELEASED);
2221 
2222 	/* no need to wait for rcu grace period:
2223 	 * device is dead and about to be freed.
2224 	 */
2225 	kfree(rcu_access_pointer(dev->ifalias));
2226 	kvfree(dev);
2227 }
2228 
2229 static const void *net_namespace(const struct device *d)
2230 {
2231 	const struct net_device *dev = to_net_dev(d);
2232 
2233 	return dev_net(dev);
2234 }
2235 
2236 static void net_get_ownership(const struct device *d, kuid_t *uid, kgid_t *gid)
2237 {
2238 	const struct net_device *dev = to_net_dev(d);
2239 	const struct net *net = dev_net(dev);
2240 
2241 	net_ns_get_ownership(net, uid, gid);
2242 }
2243 
2244 static const struct class net_class = {
2245 	.name = "net",
2246 	.dev_release = netdev_release,
2247 	.dev_groups = net_class_groups,
2248 	.dev_uevent = netdev_uevent,
2249 	.ns_type = &net_ns_type_operations,
2250 	.namespace = net_namespace,
2251 	.get_ownership = net_get_ownership,
2252 };
2253 
2254 #ifdef CONFIG_OF
2255 static int of_dev_node_match(struct device *dev, const void *data)
2256 {
2257 	for (; dev; dev = dev->parent) {
2258 		if (dev->of_node == data)
2259 			return 1;
2260 	}
2261 
2262 	return 0;
2263 }
2264 
2265 /*
2266  * of_find_net_device_by_node - lookup the net device for the device node
2267  * @np: OF device node
2268  *
2269  * Looks up the net_device structure corresponding with the device node.
2270  * If successful, returns a pointer to the net_device with the embedded
2271  * struct device refcount incremented by one, or NULL on failure. The
2272  * refcount must be dropped when done with the net_device.
2273  */
2274 struct net_device *of_find_net_device_by_node(struct device_node *np)
2275 {
2276 	struct device *dev;
2277 
2278 	dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
2279 	if (!dev)
2280 		return NULL;
2281 
2282 	return to_net_dev(dev);
2283 }
2284 EXPORT_SYMBOL(of_find_net_device_by_node);
2285 #endif
2286 
2287 /* Delete sysfs entries but hold kobject reference until after all
2288  * netdev references are gone.
2289  */
2290 void netdev_unregister_kobject(struct net_device *ndev)
2291 {
2292 	struct device *dev = &ndev->dev;
2293 
2294 	if (!refcount_read(&dev_net(ndev)->ns.count))
2295 		dev_set_uevent_suppress(dev, 1);
2296 
2297 	kobject_get(&dev->kobj);
2298 
2299 	remove_queue_kobjects(ndev);
2300 
2301 	pm_runtime_set_memalloc_noio(dev, false);
2302 
2303 	device_del(dev);
2304 }
2305 
2306 /* Create sysfs entries for network device. */
2307 int netdev_register_kobject(struct net_device *ndev)
2308 {
2309 	struct device *dev = &ndev->dev;
2310 	const struct attribute_group **groups = ndev->sysfs_groups;
2311 	int error = 0;
2312 
2313 	device_initialize(dev);
2314 	dev->class = &net_class;
2315 	dev->platform_data = ndev;
2316 	dev->groups = groups;
2317 
2318 	dev_set_name(dev, "%s", ndev->name);
2319 
2320 #ifdef CONFIG_SYSFS
2321 	/* Allow for a device specific group */
2322 	if (*groups)
2323 		groups++;
2324 
2325 	*groups++ = &netstat_group;
2326 
2327 	if (wireless_group_needed(ndev))
2328 		*groups++ = &wireless_group;
2329 #endif /* CONFIG_SYSFS */
2330 
2331 	error = device_add(dev);
2332 	if (error)
2333 		return error;
2334 
2335 	error = register_queue_kobjects(ndev);
2336 	if (error) {
2337 		device_del(dev);
2338 		return error;
2339 	}
2340 
2341 	pm_runtime_set_memalloc_noio(dev, true);
2342 
2343 	return error;
2344 }
2345 
2346 /* Change owner for sysfs entries when moving network devices across network
2347  * namespaces owned by different user namespaces.
2348  */
2349 int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
2350 			const struct net *net_new)
2351 {
2352 	kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
2353 	kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
2354 	struct device *dev = &ndev->dev;
2355 	int error;
2356 
2357 	net_ns_get_ownership(net_old, &old_uid, &old_gid);
2358 	net_ns_get_ownership(net_new, &new_uid, &new_gid);
2359 
2360 	/* The network namespace was changed but the owning user namespace is
2361 	 * identical so there's no need to change the owner of sysfs entries.
2362 	 */
2363 	if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
2364 		return 0;
2365 
2366 	error = device_change_owner(dev, new_uid, new_gid);
2367 	if (error)
2368 		return error;
2369 
2370 	error = queue_change_owner(ndev, new_uid, new_gid);
2371 	if (error)
2372 		return error;
2373 
2374 	return 0;
2375 }
2376 
2377 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
2378 				const void *ns)
2379 {
2380 	return class_create_file_ns(&net_class, class_attr, ns);
2381 }
2382 EXPORT_SYMBOL(netdev_class_create_file_ns);
2383 
2384 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
2385 				 const void *ns)
2386 {
2387 	class_remove_file_ns(&net_class, class_attr, ns);
2388 }
2389 EXPORT_SYMBOL(netdev_class_remove_file_ns);
2390 
2391 int __init netdev_kobject_init(void)
2392 {
2393 	kobj_ns_type_register(&net_ns_type_operations);
2394 	return class_register(&net_class);
2395 }
2396