xref: /linux/net/core/net-sysfs.c (revision 4e3b56eb2ed879353de392acec7b485b123840a4)
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 		/* Do not accept too large tables. */
1114 		if (mask > (INT_MAX / sizeof(*table) - 1))
1115 			return -EINVAL;
1116 
1117 		sz = max_t(size_t, sizeof(*table) * (mask + 1),
1118 			   PAGE_SIZE);
1119 		if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) ||
1120 		    is_power_of_2(sizeof(*table)))
1121 			table = kvmalloc(sz, GFP_KERNEL);
1122 		else
1123 			table = vmalloc(sz);
1124 		if (!table)
1125 			return -ENOMEM;
1126 		tag_ptr = (rps_tag_ptr)table;
1127 		if (rps_tag_to_log(tag_ptr)) {
1128 			pr_err_once("store_rps_dev_flow_table_cnt() got a non page aligned allocation.\n");
1129 			kvfree(table);
1130 			return -ENOMEM;
1131 		}
1132 		tag_ptr |= (ilog2(mask) + 1);
1133 		for (count = 0; count <= mask; count++) {
1134 			table[count].cpu = RPS_NO_CPU;
1135 			table[count].filter = RPS_NO_FILTER;
1136 		}
1137 	}
1138 
1139 	otag = xchg(&queue->rps_flow_table, tag_ptr);
1140 	if (otag)
1141 		kvfree_rcu_mightsleep(rps_tag_to_table(otag));
1142 
1143 	return len;
1144 }
1145 
1146 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
1147 	= __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
1148 
1149 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
1150 	= __ATTR(rps_flow_cnt, 0644,
1151 		 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
1152 #endif /* CONFIG_RPS */
1153 
1154 static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
1155 #ifdef CONFIG_RPS
1156 	&rps_cpus_attribute.attr,
1157 	&rps_dev_flow_table_cnt_attribute.attr,
1158 #endif
1159 	NULL
1160 };
1161 ATTRIBUTE_GROUPS(rx_queue_default);
1162 
1163 static void rx_queue_release(struct kobject *kobj)
1164 {
1165 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
1166 #ifdef CONFIG_RPS
1167 	rps_tag_ptr tag_ptr;
1168 	struct rps_map *map;
1169 
1170 	map = rcu_dereference_protected(queue->rps_map, 1);
1171 	if (map) {
1172 		RCU_INIT_POINTER(queue->rps_map, NULL);
1173 		kfree_rcu(map, rcu);
1174 	}
1175 
1176 	tag_ptr = xchg(&queue->rps_flow_table, 0UL);
1177 	if (tag_ptr)
1178 		kvfree_rcu_mightsleep(rps_tag_to_table(tag_ptr));
1179 #endif
1180 
1181 	netdev_tracker_free(queue->dev, &queue->dev_tracker);
1182 	/* Pairs with the smp_mb() in rx_queue_add_kobject(). */
1183 	smp_mb();
1184 	memset(kobj, 0, sizeof(*kobj));
1185 	__dev_put(queue->dev);
1186 }
1187 
1188 static const struct ns_common *rx_queue_namespace(const struct kobject *kobj)
1189 {
1190 	struct netdev_rx_queue *queue = to_rx_queue(kobj);
1191 	struct device *dev = &queue->dev->dev;
1192 
1193 	if (dev->class && dev->class->namespace)
1194 		return dev->class->namespace(dev);
1195 
1196 	return NULL;
1197 }
1198 
1199 static void rx_queue_get_ownership(const struct kobject *kobj,
1200 				   kuid_t *uid, kgid_t *gid)
1201 {
1202 	const struct ns_common *ns = rx_queue_namespace(kobj);
1203 
1204 	net_ns_get_ownership(ns ? container_of(ns, struct net, ns) : NULL,
1205 			     uid, gid);
1206 }
1207 
1208 static const struct kobj_type rx_queue_ktype = {
1209 	.sysfs_ops = &rx_queue_sysfs_ops,
1210 	.release = rx_queue_release,
1211 	.namespace = rx_queue_namespace,
1212 	.get_ownership = rx_queue_get_ownership,
1213 };
1214 
1215 static int rx_queue_default_mask(struct net_device *dev,
1216 				 struct netdev_rx_queue *queue)
1217 {
1218 #if IS_ENABLED(CONFIG_RPS) && IS_ENABLED(CONFIG_SYSCTL)
1219 	struct cpumask *rps_default_mask;
1220 	int res = 0;
1221 
1222 	mutex_lock(&rps_default_mask_mutex);
1223 
1224 	rps_default_mask = dev_net(dev)->core.rps_default_mask;
1225 	if (rps_default_mask && !cpumask_empty(rps_default_mask))
1226 		res = netdev_rx_queue_set_rps_mask(queue, rps_default_mask);
1227 
1228 	mutex_unlock(&rps_default_mask_mutex);
1229 
1230 	return res;
1231 #else
1232 	return 0;
1233 #endif
1234 }
1235 
1236 static int rx_queue_add_kobject(struct net_device *dev, int index)
1237 {
1238 	struct netdev_rx_queue *queue = dev->_rx + index;
1239 	struct kobject *kobj = &queue->kobj;
1240 	int error = 0;
1241 
1242 	/* Rx queues are cleared in rx_queue_release to allow later
1243 	 * re-registration. This is triggered when their kobj refcount is
1244 	 * dropped.
1245 	 *
1246 	 * If a queue is removed while both a read (or write) operation and a
1247 	 * the re-addition of the same queue are pending (waiting on rntl_lock)
1248 	 * it might happen that the re-addition will execute before the read,
1249 	 * making the initial removal to never happen (queue's kobj refcount
1250 	 * won't drop enough because of the pending read). In such rare case,
1251 	 * return to allow the removal operation to complete.
1252 	 */
1253 	if (unlikely(kobj->state_initialized)) {
1254 		netdev_warn_once(dev, "Cannot re-add rx queues before their removal completed");
1255 		return -EAGAIN;
1256 	}
1257 
1258 	/* Pairs with the smp_mb() in rx_queue_release(). */
1259 	smp_mb();
1260 
1261 	/* Kobject_put later will trigger rx_queue_release call which
1262 	 * decreases dev refcount: Take that reference here
1263 	 */
1264 	netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
1265 
1266 	kobj->kset = dev->queues_kset;
1267 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1268 				     "rx-%u", index);
1269 	if (error)
1270 		goto err;
1271 
1272 	queue->groups = rx_queue_default_groups;
1273 	error = sysfs_create_groups(kobj, queue->groups);
1274 	if (error)
1275 		goto err;
1276 
1277 	if (dev->sysfs_rx_queue_group) {
1278 		error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1279 		if (error)
1280 			goto err_default_groups;
1281 	}
1282 
1283 	error = rx_queue_default_mask(dev, queue);
1284 	if (error)
1285 		goto err_default_groups;
1286 
1287 	kobject_uevent(kobj, KOBJ_ADD);
1288 
1289 	return error;
1290 
1291 err_default_groups:
1292 	sysfs_remove_groups(kobj, queue->groups);
1293 err:
1294 	kobject_put(kobj);
1295 	return error;
1296 }
1297 
1298 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
1299 				 kgid_t kgid)
1300 {
1301 	struct netdev_rx_queue *queue = dev->_rx + index;
1302 	struct kobject *kobj = &queue->kobj;
1303 	int error;
1304 
1305 	error = sysfs_change_owner(kobj, kuid, kgid);
1306 	if (error)
1307 		return error;
1308 
1309 	if (dev->sysfs_rx_queue_group)
1310 		error = sysfs_group_change_owner(
1311 			kobj, dev->sysfs_rx_queue_group, kuid, kgid);
1312 
1313 	return error;
1314 }
1315 #endif /* CONFIG_SYSFS */
1316 
1317 int
1318 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1319 {
1320 #ifdef CONFIG_SYSFS
1321 	int i;
1322 	int error = 0;
1323 
1324 #ifndef CONFIG_RPS
1325 	if (!dev->sysfs_rx_queue_group)
1326 		return 0;
1327 #endif
1328 	for (i = old_num; i < new_num; i++) {
1329 		error = rx_queue_add_kobject(dev, i);
1330 		if (error) {
1331 			new_num = old_num;
1332 			break;
1333 		}
1334 	}
1335 
1336 	while (--i >= new_num) {
1337 		struct netdev_rx_queue *queue = &dev->_rx[i];
1338 		struct kobject *kobj = &queue->kobj;
1339 
1340 		if (!check_net(dev_net(dev)))
1341 			kobj->uevent_suppress = 1;
1342 		if (dev->sysfs_rx_queue_group)
1343 			sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1344 		sysfs_remove_groups(kobj, queue->groups);
1345 		kobject_put(kobj);
1346 	}
1347 
1348 	return error;
1349 #else
1350 	return 0;
1351 #endif
1352 }
1353 
1354 static int net_rx_queue_change_owner(struct net_device *dev, int num,
1355 				     kuid_t kuid, kgid_t kgid)
1356 {
1357 #ifdef CONFIG_SYSFS
1358 	int error = 0;
1359 	int i;
1360 
1361 #ifndef CONFIG_RPS
1362 	if (!dev->sysfs_rx_queue_group)
1363 		return 0;
1364 #endif
1365 	for (i = 0; i < num; i++) {
1366 		error = rx_queue_change_owner(dev, i, kuid, kgid);
1367 		if (error)
1368 			break;
1369 	}
1370 
1371 	return error;
1372 #else
1373 	return 0;
1374 #endif
1375 }
1376 
1377 #ifdef CONFIG_SYSFS
1378 /*
1379  * netdev_queue sysfs structures and functions.
1380  */
1381 struct netdev_queue_attribute {
1382 	struct attribute attr;
1383 	ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
1384 			struct netdev_queue *queue, char *buf);
1385 	ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
1386 			 struct netdev_queue *queue, const char *buf,
1387 			 size_t len);
1388 };
1389 #define to_netdev_queue_attr(_attr) \
1390 	container_of(_attr, struct netdev_queue_attribute, attr)
1391 
1392 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1393 
1394 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1395 				      struct attribute *attr, char *buf)
1396 {
1397 	const struct netdev_queue_attribute *attribute
1398 		= to_netdev_queue_attr(attr);
1399 	struct netdev_queue *queue = to_netdev_queue(kobj);
1400 
1401 	if (!attribute->show)
1402 		return -EIO;
1403 
1404 	return attribute->show(kobj, attr, queue, buf);
1405 }
1406 
1407 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1408 				       struct attribute *attr,
1409 				       const char *buf, size_t count)
1410 {
1411 	const struct netdev_queue_attribute *attribute
1412 		= to_netdev_queue_attr(attr);
1413 	struct netdev_queue *queue = to_netdev_queue(kobj);
1414 
1415 	if (!attribute->store)
1416 		return -EIO;
1417 
1418 	return attribute->store(kobj, attr, queue, buf, count);
1419 }
1420 
1421 static const struct sysfs_ops netdev_queue_sysfs_ops = {
1422 	.show = netdev_queue_attr_show,
1423 	.store = netdev_queue_attr_store,
1424 };
1425 
1426 static ssize_t tx_timeout_show(struct kobject *kobj, struct attribute *attr,
1427 			       struct netdev_queue *queue, char *buf)
1428 {
1429 	unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout);
1430 
1431 	return sysfs_emit(buf, fmt_ulong, trans_timeout);
1432 }
1433 
1434 static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1435 {
1436 	struct net_device *dev = queue->dev;
1437 	unsigned int i;
1438 
1439 	i = queue - dev->_tx;
1440 	BUG_ON(i >= dev->num_tx_queues);
1441 
1442 	return i;
1443 }
1444 
1445 static ssize_t traffic_class_show(struct kobject *kobj, struct attribute *attr,
1446 				  struct netdev_queue *queue, char *buf)
1447 {
1448 	struct net_device *dev = queue->dev;
1449 	int num_tc, tc, index, ret;
1450 
1451 	if (!netif_is_multiqueue(dev))
1452 		return -ENOENT;
1453 
1454 	ret = sysfs_rtnl_lock(kobj, attr, queue->dev);
1455 	if (ret)
1456 		return ret;
1457 
1458 	index = get_netdev_queue_index(queue);
1459 
1460 	/* If queue belongs to subordinate dev use its TC mapping */
1461 	dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1462 
1463 	num_tc = READ_ONCE(dev->num_tc);
1464 	tc = netdev_txq_to_tc(dev, index);
1465 
1466 	rtnl_unlock();
1467 
1468 	if (tc < 0)
1469 		return -EINVAL;
1470 
1471 	/* We can report the traffic class one of two ways:
1472 	 * Subordinate device traffic classes are reported with the traffic
1473 	 * class first, and then the subordinate class so for example TC0 on
1474 	 * subordinate device 2 will be reported as "0-2". If the queue
1475 	 * belongs to the root device it will be reported with just the
1476 	 * traffic class, so just "0" for TC 0 for example.
1477 	 */
1478 	return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) :
1479 			    sysfs_emit(buf, "%d\n", tc);
1480 }
1481 
1482 #ifdef CONFIG_XPS
1483 static ssize_t tx_maxrate_show(struct kobject *kobj, struct attribute *attr,
1484 			       struct netdev_queue *queue, char *buf)
1485 {
1486 	return sysfs_emit(buf, "%lu\n", queue->tx_maxrate);
1487 }
1488 
1489 static ssize_t tx_maxrate_store(struct kobject *kobj, struct attribute *attr,
1490 				struct netdev_queue *queue, const char *buf,
1491 				size_t len)
1492 {
1493 	int err, index = get_netdev_queue_index(queue);
1494 	struct net_device *dev = queue->dev;
1495 	u32 rate = 0;
1496 
1497 	if (!capable(CAP_NET_ADMIN))
1498 		return -EPERM;
1499 
1500 	/* The check is also done later; this helps returning early without
1501 	 * hitting the locking section below.
1502 	 */
1503 	if (!dev->netdev_ops->ndo_set_tx_maxrate)
1504 		return -EOPNOTSUPP;
1505 
1506 	err = kstrtou32(buf, 10, &rate);
1507 	if (err < 0)
1508 		return err;
1509 
1510 	err = sysfs_rtnl_lock(kobj, attr, dev);
1511 	if (err)
1512 		return err;
1513 
1514 	err = -EOPNOTSUPP;
1515 	netdev_lock_ops(dev);
1516 	if (dev->netdev_ops->ndo_set_tx_maxrate)
1517 		err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1518 	netdev_unlock_ops(dev);
1519 
1520 	if (!err) {
1521 		queue->tx_maxrate = rate;
1522 		rtnl_unlock();
1523 		return len;
1524 	}
1525 
1526 	rtnl_unlock();
1527 	return err;
1528 }
1529 
1530 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1531 	= __ATTR_RW(tx_maxrate);
1532 #endif
1533 
1534 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1535 	= __ATTR_RO(tx_timeout);
1536 
1537 static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1538 	= __ATTR_RO(traffic_class);
1539 
1540 #ifdef CONFIG_BQL
1541 /*
1542  * Byte queue limits sysfs structures and functions.
1543  */
1544 static ssize_t bql_show(char *buf, unsigned int value)
1545 {
1546 	return sysfs_emit(buf, "%u\n", value);
1547 }
1548 
1549 static ssize_t bql_set(const char *buf, const size_t count,
1550 		       unsigned int *pvalue)
1551 {
1552 	unsigned int value;
1553 	int err;
1554 
1555 	if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1556 		value = DQL_MAX_LIMIT;
1557 	} else {
1558 		err = kstrtouint(buf, 10, &value);
1559 		if (err < 0)
1560 			return err;
1561 		if (value > DQL_MAX_LIMIT)
1562 			return -EINVAL;
1563 	}
1564 
1565 	*pvalue = value;
1566 
1567 	return count;
1568 }
1569 
1570 static ssize_t bql_show_hold_time(struct kobject *kobj, struct attribute *attr,
1571 				  struct netdev_queue *queue, char *buf)
1572 {
1573 	struct dql *dql = &queue->dql;
1574 
1575 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1576 }
1577 
1578 static ssize_t bql_set_hold_time(struct kobject *kobj, struct attribute *attr,
1579 				 struct netdev_queue *queue, const char *buf,
1580 				 size_t len)
1581 {
1582 	struct dql *dql = &queue->dql;
1583 	unsigned int value;
1584 	int err;
1585 
1586 	err = kstrtouint(buf, 10, &value);
1587 	if (err < 0)
1588 		return err;
1589 
1590 	dql->slack_hold_time = msecs_to_jiffies(value);
1591 
1592 	return len;
1593 }
1594 
1595 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1596 	= __ATTR(hold_time, 0644,
1597 		 bql_show_hold_time, bql_set_hold_time);
1598 
1599 static ssize_t bql_show_stall_thrs(struct kobject *kobj, struct attribute *attr,
1600 				   struct netdev_queue *queue, char *buf)
1601 {
1602 	struct dql *dql = &queue->dql;
1603 
1604 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->stall_thrs));
1605 }
1606 
1607 static ssize_t bql_set_stall_thrs(struct kobject *kobj, struct attribute *attr,
1608 				  struct netdev_queue *queue, const char *buf,
1609 				  size_t len)
1610 {
1611 	struct dql *dql = &queue->dql;
1612 	unsigned int value;
1613 	int err;
1614 
1615 	err = kstrtouint(buf, 10, &value);
1616 	if (err < 0)
1617 		return err;
1618 
1619 	value = msecs_to_jiffies(value);
1620 	if (value && (value < 4 || value > 4 / 2 * BITS_PER_LONG))
1621 		return -ERANGE;
1622 
1623 	if (!dql->stall_thrs && value)
1624 		dql->last_reap = jiffies;
1625 	/* Force last_reap to be live */
1626 	smp_wmb();
1627 	dql->stall_thrs = value;
1628 
1629 	return len;
1630 }
1631 
1632 static struct netdev_queue_attribute bql_stall_thrs_attribute __ro_after_init =
1633 	__ATTR(stall_thrs, 0644, bql_show_stall_thrs, bql_set_stall_thrs);
1634 
1635 static ssize_t bql_show_stall_max(struct kobject *kobj, struct attribute *attr,
1636 				  struct netdev_queue *queue, char *buf)
1637 {
1638 	return sysfs_emit(buf, "%u\n", READ_ONCE(queue->dql.stall_max));
1639 }
1640 
1641 static ssize_t bql_set_stall_max(struct kobject *kobj, struct attribute *attr,
1642 				 struct netdev_queue *queue, const char *buf,
1643 				 size_t len)
1644 {
1645 	WRITE_ONCE(queue->dql.stall_max, 0);
1646 	return len;
1647 }
1648 
1649 static struct netdev_queue_attribute bql_stall_max_attribute __ro_after_init =
1650 	__ATTR(stall_max, 0644, bql_show_stall_max, bql_set_stall_max);
1651 
1652 static ssize_t bql_show_stall_cnt(struct kobject *kobj, struct attribute *attr,
1653 				  struct netdev_queue *queue, char *buf)
1654 {
1655 	struct dql *dql = &queue->dql;
1656 
1657 	return sysfs_emit(buf, "%lu\n", dql->stall_cnt);
1658 }
1659 
1660 static struct netdev_queue_attribute bql_stall_cnt_attribute __ro_after_init =
1661 	__ATTR(stall_cnt, 0444, bql_show_stall_cnt, NULL);
1662 
1663 static ssize_t bql_show_inflight(struct kobject *kobj, struct attribute *attr,
1664 				 struct netdev_queue *queue, char *buf)
1665 {
1666 	struct dql *dql = &queue->dql;
1667 
1668 	return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed);
1669 }
1670 
1671 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1672 	__ATTR(inflight, 0444, bql_show_inflight, NULL);
1673 
1674 #define BQL_ATTR(NAME, FIELD)						\
1675 static ssize_t bql_show_ ## NAME(struct kobject *kobj,			\
1676 				 struct attribute *attr,		\
1677 				 struct netdev_queue *queue, char *buf)	\
1678 {									\
1679 	return bql_show(buf, queue->dql.FIELD);				\
1680 }									\
1681 									\
1682 static ssize_t bql_set_ ## NAME(struct kobject *kobj,			\
1683 				struct attribute *attr,			\
1684 				struct netdev_queue *queue,		\
1685 				const char *buf, size_t len)		\
1686 {									\
1687 	return bql_set(buf, len, &queue->dql.FIELD);			\
1688 }									\
1689 									\
1690 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1691 	= __ATTR(NAME, 0644,				\
1692 		 bql_show_ ## NAME, bql_set_ ## NAME)
1693 
1694 BQL_ATTR(limit, limit);
1695 BQL_ATTR(limit_max, max_limit);
1696 BQL_ATTR(limit_min, min_limit);
1697 
1698 static struct attribute *dql_attrs[] __ro_after_init = {
1699 	&bql_limit_attribute.attr,
1700 	&bql_limit_max_attribute.attr,
1701 	&bql_limit_min_attribute.attr,
1702 	&bql_hold_time_attribute.attr,
1703 	&bql_inflight_attribute.attr,
1704 	&bql_stall_thrs_attribute.attr,
1705 	&bql_stall_cnt_attribute.attr,
1706 	&bql_stall_max_attribute.attr,
1707 	NULL
1708 };
1709 
1710 static const struct attribute_group dql_group = {
1711 	.name  = "byte_queue_limits",
1712 	.attrs  = dql_attrs,
1713 };
1714 #else
1715 /* Fake declaration, all the code using it should be dead */
1716 static const struct attribute_group dql_group = {};
1717 #endif /* CONFIG_BQL */
1718 
1719 #ifdef CONFIG_XPS
1720 static ssize_t xps_queue_show(struct net_device *dev, unsigned int index,
1721 			      int tc, char *buf, enum xps_map_type type)
1722 {
1723 	struct xps_dev_maps *dev_maps;
1724 	unsigned long *mask;
1725 	unsigned int nr_ids;
1726 	int j, len;
1727 
1728 	rcu_read_lock();
1729 	dev_maps = rcu_dereference(dev->xps_maps[type]);
1730 
1731 	/* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0
1732 	 * when dev_maps hasn't been allocated yet, to be backward compatible.
1733 	 */
1734 	nr_ids = dev_maps ? dev_maps->nr_ids :
1735 		 (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues);
1736 
1737 	mask = bitmap_zalloc(nr_ids, GFP_NOWAIT);
1738 	if (!mask) {
1739 		rcu_read_unlock();
1740 		return -ENOMEM;
1741 	}
1742 
1743 	if (!dev_maps || tc >= dev_maps->num_tc)
1744 		goto out_no_maps;
1745 
1746 	for (j = 0; j < nr_ids; j++) {
1747 		int i, tci = j * dev_maps->num_tc + tc;
1748 		struct xps_map *map;
1749 
1750 		map = rcu_dereference(dev_maps->attr_map[tci]);
1751 		if (!map)
1752 			continue;
1753 
1754 		for (i = map->len; i--;) {
1755 			if (map->queues[i] == index) {
1756 				__set_bit(j, mask);
1757 				break;
1758 			}
1759 		}
1760 	}
1761 out_no_maps:
1762 	rcu_read_unlock();
1763 
1764 	len = sysfs_emit(buf, "%*pb\n", nr_ids, mask);
1765 	bitmap_free(mask);
1766 
1767 	return len < PAGE_SIZE ? len : -EINVAL;
1768 }
1769 
1770 static ssize_t xps_cpus_show(struct kobject *kobj, struct attribute *attr,
1771 			     struct netdev_queue *queue, char *buf)
1772 {
1773 	struct net_device *dev = queue->dev;
1774 	unsigned int index;
1775 	int len, tc, ret;
1776 
1777 	if (!netif_is_multiqueue(dev))
1778 		return -ENOENT;
1779 
1780 	index = get_netdev_queue_index(queue);
1781 
1782 	ret = sysfs_rtnl_lock(kobj, attr, queue->dev);
1783 	if (ret)
1784 		return ret;
1785 
1786 	/* If queue belongs to subordinate dev use its map */
1787 	dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1788 
1789 	tc = netdev_txq_to_tc(dev, index);
1790 	if (tc < 0) {
1791 		rtnl_unlock();
1792 		return -EINVAL;
1793 	}
1794 
1795 	/* Increase the net device refcnt to make sure it won't be freed while
1796 	 * xps_queue_show is running.
1797 	 */
1798 	dev_hold(dev);
1799 	rtnl_unlock();
1800 
1801 	len = xps_queue_show(dev, index, tc, buf, XPS_CPUS);
1802 
1803 	dev_put(dev);
1804 	return len;
1805 }
1806 
1807 static ssize_t xps_cpus_store(struct kobject *kobj, struct attribute *attr,
1808 			      struct netdev_queue *queue, const char *buf,
1809 			      size_t len)
1810 {
1811 	struct net_device *dev = queue->dev;
1812 	unsigned int index;
1813 	cpumask_var_t mask;
1814 	int err;
1815 
1816 	if (!netif_is_multiqueue(dev))
1817 		return -ENOENT;
1818 
1819 	if (!capable(CAP_NET_ADMIN))
1820 		return -EPERM;
1821 
1822 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1823 		return -ENOMEM;
1824 
1825 	index = get_netdev_queue_index(queue);
1826 
1827 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1828 	if (err) {
1829 		free_cpumask_var(mask);
1830 		return err;
1831 	}
1832 
1833 	err = sysfs_rtnl_lock(kobj, attr, dev);
1834 	if (err) {
1835 		free_cpumask_var(mask);
1836 		return err;
1837 	}
1838 
1839 	err = netif_set_xps_queue(dev, mask, index);
1840 	rtnl_unlock();
1841 
1842 	free_cpumask_var(mask);
1843 
1844 	return err ? : len;
1845 }
1846 
1847 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1848 	= __ATTR_RW(xps_cpus);
1849 
1850 static ssize_t xps_rxqs_show(struct kobject *kobj, struct attribute *attr,
1851 			     struct netdev_queue *queue, char *buf)
1852 {
1853 	struct net_device *dev = queue->dev;
1854 	unsigned int index;
1855 	int tc, ret;
1856 
1857 	index = get_netdev_queue_index(queue);
1858 
1859 	ret = sysfs_rtnl_lock(kobj, attr, dev);
1860 	if (ret)
1861 		return ret;
1862 
1863 	tc = netdev_txq_to_tc(dev, index);
1864 
1865 	/* Increase the net device refcnt to make sure it won't be freed while
1866 	 * xps_queue_show is running.
1867 	 */
1868 	dev_hold(dev);
1869 	rtnl_unlock();
1870 
1871 	ret = tc >= 0 ? xps_queue_show(dev, index, tc, buf, XPS_RXQS) : -EINVAL;
1872 	dev_put(dev);
1873 	return ret;
1874 }
1875 
1876 static ssize_t xps_rxqs_store(struct kobject *kobj, struct attribute *attr,
1877 			      struct netdev_queue *queue, const char *buf,
1878 			      size_t len)
1879 {
1880 	struct net_device *dev = queue->dev;
1881 	struct net *net = dev_net(dev);
1882 	unsigned long *mask;
1883 	unsigned int index;
1884 	int err;
1885 
1886 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1887 		return -EPERM;
1888 
1889 	mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1890 	if (!mask)
1891 		return -ENOMEM;
1892 
1893 	index = get_netdev_queue_index(queue);
1894 
1895 	err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1896 	if (err) {
1897 		bitmap_free(mask);
1898 		return err;
1899 	}
1900 
1901 	err = sysfs_rtnl_lock(kobj, attr, dev);
1902 	if (err) {
1903 		bitmap_free(mask);
1904 		return err;
1905 	}
1906 
1907 	cpus_read_lock();
1908 	err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS);
1909 	cpus_read_unlock();
1910 
1911 	rtnl_unlock();
1912 
1913 	bitmap_free(mask);
1914 	return err ? : len;
1915 }
1916 
1917 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1918 	= __ATTR_RW(xps_rxqs);
1919 #endif /* CONFIG_XPS */
1920 
1921 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1922 	&queue_trans_timeout.attr,
1923 	&queue_traffic_class.attr,
1924 #ifdef CONFIG_XPS
1925 	&xps_cpus_attribute.attr,
1926 	&xps_rxqs_attribute.attr,
1927 	&queue_tx_maxrate.attr,
1928 #endif
1929 	NULL
1930 };
1931 ATTRIBUTE_GROUPS(netdev_queue_default);
1932 
1933 static void netdev_queue_release(struct kobject *kobj)
1934 {
1935 	struct netdev_queue *queue = to_netdev_queue(kobj);
1936 
1937 	netdev_tracker_free(queue->dev, &queue->dev_tracker);
1938 	/* Pairs with the smp_mb() in netdev_queue_add_kobject(). */
1939 	smp_mb();
1940 	memset(kobj, 0, sizeof(*kobj));
1941 	__dev_put(queue->dev);
1942 }
1943 
1944 static const struct ns_common *netdev_queue_namespace(const struct kobject *kobj)
1945 {
1946 	struct netdev_queue *queue = to_netdev_queue(kobj);
1947 	struct device *dev = &queue->dev->dev;
1948 
1949 	if (dev->class && dev->class->namespace)
1950 		return dev->class->namespace(dev);
1951 
1952 	return NULL;
1953 }
1954 
1955 static void netdev_queue_get_ownership(const struct kobject *kobj,
1956 				       kuid_t *uid, kgid_t *gid)
1957 {
1958 	const struct ns_common *ns = netdev_queue_namespace(kobj);
1959 
1960 	net_ns_get_ownership(ns ? container_of(ns, struct net, ns) : NULL,
1961 			     uid, gid);
1962 }
1963 
1964 static const struct kobj_type netdev_queue_ktype = {
1965 	.sysfs_ops = &netdev_queue_sysfs_ops,
1966 	.release = netdev_queue_release,
1967 	.namespace = netdev_queue_namespace,
1968 	.get_ownership = netdev_queue_get_ownership,
1969 };
1970 
1971 static bool netdev_uses_bql(const struct net_device *dev)
1972 {
1973 	if (dev->lltx || (dev->priv_flags & IFF_NO_QUEUE))
1974 		return false;
1975 
1976 	return IS_ENABLED(CONFIG_BQL);
1977 }
1978 
1979 static int netdev_queue_add_kobject(struct net_device *dev, int index)
1980 {
1981 	struct netdev_queue *queue = dev->_tx + index;
1982 	struct kobject *kobj = &queue->kobj;
1983 	int error = 0;
1984 
1985 	/* Tx queues are cleared in netdev_queue_release to allow later
1986 	 * re-registration. This is triggered when their kobj refcount is
1987 	 * dropped.
1988 	 *
1989 	 * If a queue is removed while both a read (or write) operation and a
1990 	 * the re-addition of the same queue are pending (waiting on rntl_lock)
1991 	 * it might happen that the re-addition will execute before the read,
1992 	 * making the initial removal to never happen (queue's kobj refcount
1993 	 * won't drop enough because of the pending read). In such rare case,
1994 	 * return to allow the removal operation to complete.
1995 	 */
1996 	if (unlikely(kobj->state_initialized)) {
1997 		netdev_warn_once(dev, "Cannot re-add tx queues before their removal completed");
1998 		return -EAGAIN;
1999 	}
2000 
2001 	/* Pairs with the smp_mb() in netdev_queue_release(). */
2002 	smp_mb();
2003 
2004 	/* Kobject_put later will trigger netdev_queue_release call
2005 	 * which decreases dev refcount: Take that reference here
2006 	 */
2007 	netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
2008 
2009 	kobj->kset = dev->queues_kset;
2010 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
2011 				     "tx-%u", index);
2012 	if (error)
2013 		goto err;
2014 
2015 	queue->groups = netdev_queue_default_groups;
2016 	error = sysfs_create_groups(kobj, queue->groups);
2017 	if (error)
2018 		goto err;
2019 
2020 	if (netdev_uses_bql(dev)) {
2021 		error = sysfs_create_group(kobj, &dql_group);
2022 		if (error)
2023 			goto err_default_groups;
2024 	}
2025 
2026 	kobject_uevent(kobj, KOBJ_ADD);
2027 	return 0;
2028 
2029 err_default_groups:
2030 	sysfs_remove_groups(kobj, queue->groups);
2031 err:
2032 	kobject_put(kobj);
2033 	return error;
2034 }
2035 
2036 static int tx_queue_change_owner(struct net_device *ndev, int index,
2037 				 kuid_t kuid, kgid_t kgid)
2038 {
2039 	struct netdev_queue *queue = ndev->_tx + index;
2040 	struct kobject *kobj = &queue->kobj;
2041 	int error;
2042 
2043 	error = sysfs_change_owner(kobj, kuid, kgid);
2044 	if (error)
2045 		return error;
2046 
2047 	if (netdev_uses_bql(ndev))
2048 		error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
2049 
2050 	return error;
2051 }
2052 #endif /* CONFIG_SYSFS */
2053 
2054 int
2055 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
2056 {
2057 #ifdef CONFIG_SYSFS
2058 	int i;
2059 	int error = 0;
2060 
2061 	/* Tx queue kobjects are allowed to be updated when a device is being
2062 	 * unregistered, but solely to remove queues from qdiscs. Any path
2063 	 * adding queues should be fixed.
2064 	 */
2065 	WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
2066 	     "New queues can't be registered after device unregistration.");
2067 
2068 	for (i = old_num; i < new_num; i++) {
2069 		error = netdev_queue_add_kobject(dev, i);
2070 		if (error) {
2071 			new_num = old_num;
2072 			break;
2073 		}
2074 	}
2075 
2076 	while (--i >= new_num) {
2077 		struct netdev_queue *queue = dev->_tx + i;
2078 
2079 		if (!check_net(dev_net(dev)))
2080 			queue->kobj.uevent_suppress = 1;
2081 
2082 		if (netdev_uses_bql(dev))
2083 			sysfs_remove_group(&queue->kobj, &dql_group);
2084 
2085 		sysfs_remove_groups(&queue->kobj, queue->groups);
2086 		kobject_put(&queue->kobj);
2087 	}
2088 
2089 	return error;
2090 #else
2091 	return 0;
2092 #endif /* CONFIG_SYSFS */
2093 }
2094 
2095 static int net_tx_queue_change_owner(struct net_device *dev, int num,
2096 				     kuid_t kuid, kgid_t kgid)
2097 {
2098 #ifdef CONFIG_SYSFS
2099 	int error = 0;
2100 	int i;
2101 
2102 	for (i = 0; i < num; i++) {
2103 		error = tx_queue_change_owner(dev, i, kuid, kgid);
2104 		if (error)
2105 			break;
2106 	}
2107 
2108 	return error;
2109 #else
2110 	return 0;
2111 #endif /* CONFIG_SYSFS */
2112 }
2113 
2114 static int register_queue_kobjects(struct net_device *dev)
2115 {
2116 	int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
2117 
2118 #ifdef CONFIG_SYSFS
2119 	dev->queues_kset = kset_create_and_add("queues",
2120 					       NULL, &dev->dev.kobj);
2121 	if (!dev->queues_kset)
2122 		return -ENOMEM;
2123 	real_rx = dev->real_num_rx_queues;
2124 #endif
2125 	real_tx = dev->real_num_tx_queues;
2126 
2127 	error = net_rx_queue_update_kobjects(dev, 0, real_rx);
2128 	if (error)
2129 		goto error;
2130 	rxq = real_rx;
2131 
2132 	error = netdev_queue_update_kobjects(dev, 0, real_tx);
2133 	if (error)
2134 		goto error;
2135 	txq = real_tx;
2136 
2137 	return 0;
2138 
2139 error:
2140 	netdev_queue_update_kobjects(dev, txq, 0);
2141 	net_rx_queue_update_kobjects(dev, rxq, 0);
2142 #ifdef CONFIG_SYSFS
2143 	kset_unregister(dev->queues_kset);
2144 #endif
2145 	return error;
2146 }
2147 
2148 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
2149 {
2150 	int error = 0, real_rx = 0, real_tx = 0;
2151 
2152 #ifdef CONFIG_SYSFS
2153 	if (ndev->queues_kset) {
2154 		error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
2155 		if (error)
2156 			return error;
2157 	}
2158 	real_rx = ndev->real_num_rx_queues;
2159 #endif
2160 	real_tx = ndev->real_num_tx_queues;
2161 
2162 	error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
2163 	if (error)
2164 		return error;
2165 
2166 	error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
2167 	if (error)
2168 		return error;
2169 
2170 	return 0;
2171 }
2172 
2173 static void remove_queue_kobjects(struct net_device *dev)
2174 {
2175 	int real_rx = 0, real_tx = 0;
2176 
2177 #ifdef CONFIG_SYSFS
2178 	real_rx = dev->real_num_rx_queues;
2179 #endif
2180 	real_tx = dev->real_num_tx_queues;
2181 
2182 	net_rx_queue_update_kobjects(dev, real_rx, 0);
2183 	netdev_queue_update_kobjects(dev, real_tx, 0);
2184 
2185 	netdev_lock_ops(dev);
2186 	dev->real_num_rx_queues = 0;
2187 	dev->real_num_tx_queues = 0;
2188 	netdev_unlock_ops(dev);
2189 #ifdef CONFIG_SYSFS
2190 	kset_unregister(dev->queues_kset);
2191 #endif
2192 }
2193 
2194 static bool net_current_may_mount(void)
2195 {
2196 	struct net *net = current->nsproxy->net_ns;
2197 
2198 	return ns_capable(net->user_ns, CAP_SYS_ADMIN);
2199 }
2200 
2201 static struct ns_common *net_grab_current_ns(void)
2202 {
2203 	struct net *net = current->nsproxy->net_ns;
2204 #ifdef CONFIG_NET_NS
2205 	if (net)
2206 		refcount_inc(&net->passive);
2207 #endif
2208 	return net ? to_ns_common(net) : NULL;
2209 }
2210 
2211 static const struct ns_common *net_initial_ns(void)
2212 {
2213 	return to_ns_common(&init_net);
2214 }
2215 
2216 static const struct ns_common *net_netlink_ns(struct sock *sk)
2217 {
2218 	return to_ns_common(sock_net(sk));
2219 }
2220 
2221 const struct kobj_ns_type_operations net_ns_type_operations = {
2222 	.type = KOBJ_NS_TYPE_NET,
2223 	.current_may_mount = net_current_may_mount,
2224 	.grab_current_ns = net_grab_current_ns,
2225 	.netlink_ns = net_netlink_ns,
2226 	.initial_ns = net_initial_ns,
2227 	.drop_ns = net_drop_ns,
2228 };
2229 EXPORT_SYMBOL_GPL(net_ns_type_operations);
2230 
2231 static int netdev_uevent(const struct device *d, struct kobj_uevent_env *env)
2232 {
2233 	const struct net_device *dev = to_net_dev(d);
2234 	int retval;
2235 
2236 	/* pass interface to uevent. */
2237 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
2238 	if (retval)
2239 		goto exit;
2240 
2241 	/* pass ifindex to uevent.
2242 	 * ifindex is useful as it won't change (interface name may change)
2243 	 * and is what RtNetlink uses natively.
2244 	 */
2245 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
2246 
2247 exit:
2248 	return retval;
2249 }
2250 
2251 /*
2252  *	netdev_release -- destroy and free a dead device.
2253  *	Called when last reference to device kobject is gone.
2254  */
2255 static void netdev_release(struct device *d)
2256 {
2257 	struct net_device *dev = to_net_dev(d);
2258 
2259 	BUG_ON(dev->reg_state != NETREG_RELEASED);
2260 
2261 	/* no need to wait for rcu grace period:
2262 	 * device is dead and about to be freed.
2263 	 */
2264 	kfree(rcu_access_pointer(dev->ifalias));
2265 	kvfree(dev);
2266 }
2267 
2268 static const struct ns_common *net_namespace(const struct device *d)
2269 {
2270 	const struct net_device *dev = to_net_dev(d);
2271 
2272 	return to_ns_common(dev_net(dev));
2273 }
2274 
2275 static void net_get_ownership(const struct device *d, kuid_t *uid, kgid_t *gid)
2276 {
2277 	const struct net_device *dev = to_net_dev(d);
2278 	const struct net *net = dev_net(dev);
2279 
2280 	net_ns_get_ownership(net, uid, gid);
2281 }
2282 
2283 static const struct class net_class = {
2284 	.name = "net",
2285 	.dev_release = netdev_release,
2286 	.dev_groups = net_class_groups,
2287 	.dev_uevent = netdev_uevent,
2288 	.ns_type = &net_ns_type_operations,
2289 	.namespace = net_namespace,
2290 	.get_ownership = net_get_ownership,
2291 };
2292 
2293 #ifdef CONFIG_OF
2294 static int of_dev_node_match(struct device *dev, const void *data)
2295 {
2296 	for (; dev; dev = dev->parent) {
2297 		if (dev->of_node == data)
2298 			return 1;
2299 	}
2300 
2301 	return 0;
2302 }
2303 
2304 /*
2305  * of_find_net_device_by_node - lookup the net device for the device node
2306  * @np: OF device node
2307  *
2308  * Looks up the net_device structure corresponding with the device node.
2309  * If successful, returns a pointer to the net_device with the embedded
2310  * struct device refcount incremented by one, or NULL on failure. The
2311  * refcount must be dropped when done with the net_device.
2312  */
2313 struct net_device *of_find_net_device_by_node(struct device_node *np)
2314 {
2315 	struct device *dev;
2316 
2317 	dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
2318 	if (!dev)
2319 		return NULL;
2320 
2321 	return to_net_dev(dev);
2322 }
2323 EXPORT_SYMBOL(of_find_net_device_by_node);
2324 #endif
2325 
2326 /* Delete sysfs entries but hold kobject reference until after all
2327  * netdev references are gone.
2328  */
2329 void netdev_unregister_kobject(struct net_device *ndev)
2330 {
2331 	struct device *dev = &ndev->dev;
2332 
2333 	if (!check_net(dev_net(ndev)))
2334 		dev_set_uevent_suppress(dev, 1);
2335 
2336 	kobject_get(&dev->kobj);
2337 
2338 	remove_queue_kobjects(ndev);
2339 
2340 	pm_runtime_set_memalloc_noio(dev, false);
2341 
2342 	device_del(dev);
2343 }
2344 
2345 /* Create sysfs entries for network device. */
2346 int netdev_register_kobject(struct net_device *ndev)
2347 {
2348 	struct device *dev = &ndev->dev;
2349 	const struct attribute_group **groups = ndev->sysfs_groups;
2350 	int error = 0;
2351 
2352 	device_initialize(dev);
2353 	dev->class = &net_class;
2354 	dev->platform_data = ndev;
2355 	dev->groups = groups;
2356 
2357 	dev_set_name(dev, "%s", ndev->name);
2358 
2359 #ifdef CONFIG_SYSFS
2360 	/* Allow for a device specific group */
2361 	if (*groups)
2362 		groups++;
2363 
2364 	*groups++ = &netstat_group;
2365 	*groups++ = &netdev_phys_group;
2366 
2367 	if (wireless_group_needed(ndev))
2368 		*groups++ = &wireless_group;
2369 #endif /* CONFIG_SYSFS */
2370 
2371 	/* Hold back the KOBJ_ADD uevent until the device is listed. */
2372 	dev_set_uevent_suppress(dev, 1);
2373 
2374 	error = device_add(dev);
2375 	if (error)
2376 		return error;
2377 
2378 	error = register_queue_kobjects(ndev);
2379 	if (error) {
2380 		device_del(dev);
2381 		return error;
2382 	}
2383 
2384 	pm_runtime_set_memalloc_noio(dev, true);
2385 
2386 	return error;
2387 }
2388 
2389 /* Announce a fully registered device to userspace. This pairs with the uevent
2390  * suppression from netdev_register_kobject().
2391  */
2392 void netdev_uevent_add(struct net_device *ndev)
2393 {
2394 	struct device *dev = &ndev->dev;
2395 
2396 	dev_set_uevent_suppress(dev, 0);
2397 	kobject_uevent(&dev->kobj, KOBJ_ADD);
2398 }
2399 
2400 /* Change owner for sysfs entries when moving network devices across network
2401  * namespaces owned by different user namespaces.
2402  */
2403 int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
2404 			const struct net *net_new)
2405 {
2406 	kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
2407 	kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
2408 	struct device *dev = &ndev->dev;
2409 	int error;
2410 
2411 	net_ns_get_ownership(net_old, &old_uid, &old_gid);
2412 	net_ns_get_ownership(net_new, &new_uid, &new_gid);
2413 
2414 	/* The network namespace was changed but the owning user namespace is
2415 	 * identical so there's no need to change the owner of sysfs entries.
2416 	 */
2417 	if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
2418 		return 0;
2419 
2420 	error = device_change_owner(dev, new_uid, new_gid);
2421 	if (error)
2422 		return error;
2423 
2424 	error = queue_change_owner(ndev, new_uid, new_gid);
2425 	if (error)
2426 		return error;
2427 
2428 	return 0;
2429 }
2430 
2431 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
2432 				const struct ns_common *ns)
2433 {
2434 	return class_create_file_ns(&net_class, class_attr, ns);
2435 }
2436 EXPORT_SYMBOL(netdev_class_create_file_ns);
2437 
2438 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
2439 				 const struct ns_common *ns)
2440 {
2441 	class_remove_file_ns(&net_class, class_attr, ns);
2442 }
2443 EXPORT_SYMBOL(netdev_class_remove_file_ns);
2444 
2445 int __init netdev_kobject_init(void)
2446 {
2447 	kobj_ns_type_register(&net_ns_type_operations);
2448 	return class_register(&net_class);
2449 }
2450