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