dfe8b68d4a4aad6809ef69deba280e20889fd5ee
[openwrt/staging/xback.git] /
1 From: Wei Wang <weiwan@google.com>
2 Date: Mon, 8 Feb 2021 11:34:10 -0800
3 Subject: [PATCH] net: add sysfs attribute to control napi threaded mode
4
5 This patch adds a new sysfs attribute to the network device class.
6 Said attribute provides a per-device control to enable/disable the
7 threaded mode for all the napi instances of the given network device,
8 without the need for a device up/down.
9 User sets it to 1 or 0 to enable or disable threaded mode.
10 Note: when switching between threaded and the current softirq based mode
11 for a napi instance, it will not immediately take effect if the napi is
12 currently being polled. The mode switch will happen for the next time
13 napi_schedule() is called.
14
15 Co-developed-by: Paolo Abeni <pabeni@redhat.com>
16 Signed-off-by: Paolo Abeni <pabeni@redhat.com>
17 Co-developed-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
18 Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
19 Co-developed-by: Felix Fietkau <nbd@nbd.name>
20 Signed-off-by: Felix Fietkau <nbd@nbd.name>
21 Signed-off-by: Wei Wang <weiwan@google.com>
22 Reviewed-by: Alexander Duyck <alexanderduyck@fb.com>
23 Signed-off-by: David S. Miller <davem@davemloft.net>
24 ---
25
26 --- a/Documentation/ABI/testing/sysfs-class-net
27 +++ b/Documentation/ABI/testing/sysfs-class-net
28 @@ -337,3 +337,18 @@ Contact: netdev@vger.kernel.org
29 Description:
30 32-bit unsigned integer counting the number of times the link has
31 been down
32 +
33 +What: /sys/class/net/<iface>/threaded
34 +Date: Jan 2021
35 +KernelVersion: 5.12
36 +Contact: netdev@vger.kernel.org
37 +Description:
38 + Boolean value to control the threaded mode per device. User could
39 + set this value to enable/disable threaded mode for all napi
40 + belonging to this device, without the need to do device up/down.
41 +
42 + Possible values:
43 + == ==================================
44 + 0 threaded mode disabled for this dev
45 + 1 threaded mode enabled for this dev
46 + == ==================================
47 --- a/include/linux/netdevice.h
48 +++ b/include/linux/netdevice.h
49 @@ -491,6 +491,8 @@ static inline bool napi_complete(struct
50 return napi_complete_done(n, 0);
51 }
52
53 +int dev_set_threaded(struct net_device *dev, bool threaded);
54 +
55 /**
56 * napi_disable - prevent NAPI from scheduling
57 * @n: NAPI context
58 --- a/net/core/dev.c
59 +++ b/net/core/dev.c
60 @@ -4287,8 +4287,9 @@ static inline void ____napi_schedule(str
61
62 if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
63 /* Paired with smp_mb__before_atomic() in
64 - * napi_enable(). Use READ_ONCE() to guarantee
65 - * a complete read on napi->thread. Only call
66 + * napi_enable()/dev_set_threaded().
67 + * Use READ_ONCE() to guarantee a complete
68 + * read on napi->thread. Only call
69 * wake_up_process() when it's not NULL.
70 */
71 thread = READ_ONCE(napi->thread);
72 @@ -6762,6 +6763,49 @@ static void init_gro_hash(struct napi_st
73 napi->gro_bitmask = 0;
74 }
75
76 +int dev_set_threaded(struct net_device *dev, bool threaded)
77 +{
78 + struct napi_struct *napi;
79 + int err = 0;
80 +
81 + if (dev->threaded == threaded)
82 + return 0;
83 +
84 + if (threaded) {
85 + list_for_each_entry(napi, &dev->napi_list, dev_list) {
86 + if (!napi->thread) {
87 + err = napi_kthread_create(napi);
88 + if (err) {
89 + threaded = false;
90 + break;
91 + }
92 + }
93 + }
94 + }
95 +
96 + dev->threaded = threaded;
97 +
98 + /* Make sure kthread is created before THREADED bit
99 + * is set.
100 + */
101 + smp_mb__before_atomic();
102 +
103 + /* Setting/unsetting threaded mode on a napi might not immediately
104 + * take effect, if the current napi instance is actively being
105 + * polled. In this case, the switch between threaded mode and
106 + * softirq mode will happen in the next round of napi_schedule().
107 + * This should not cause hiccups/stalls to the live traffic.
108 + */
109 + list_for_each_entry(napi, &dev->napi_list, dev_list) {
110 + if (threaded)
111 + set_bit(NAPI_STATE_THREADED, &napi->state);
112 + else
113 + clear_bit(NAPI_STATE_THREADED, &napi->state);
114 + }
115 +
116 + return err;
117 +}
118 +
119 void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
120 int (*poll)(struct napi_struct *, int), int weight)
121 {
122 --- a/net/core/net-sysfs.c
123 +++ b/net/core/net-sysfs.c
124 @@ -538,6 +538,45 @@ static ssize_t phys_switch_id_show(struc
125 }
126 static DEVICE_ATTR_RO(phys_switch_id);
127
128 +static ssize_t threaded_show(struct device *dev,
129 + struct device_attribute *attr, char *buf)
130 +{
131 + struct net_device *netdev = to_net_dev(dev);
132 + ssize_t ret = -EINVAL;
133 +
134 + if (!rtnl_trylock())
135 + return restart_syscall();
136 +
137 + if (dev_isalive(netdev))
138 + ret = sprintf(buf, fmt_dec, netdev->threaded);
139 +
140 + rtnl_unlock();
141 + return ret;
142 +}
143 +
144 +static int modify_napi_threaded(struct net_device *dev, unsigned long val)
145 +{
146 + int ret;
147 +
148 + if (list_empty(&dev->napi_list))
149 + return -EOPNOTSUPP;
150 +
151 + if (val != 0 && val != 1)
152 + return -EOPNOTSUPP;
153 +
154 + ret = dev_set_threaded(dev, val);
155 +
156 + return ret;
157 +}
158 +
159 +static ssize_t threaded_store(struct device *dev,
160 + struct device_attribute *attr,
161 + const char *buf, size_t len)
162 +{
163 + return netdev_store(dev, attr, buf, len, modify_napi_threaded);
164 +}
165 +static DEVICE_ATTR_RW(threaded);
166 +
167 static struct attribute *net_class_attrs[] __ro_after_init = {
168 &dev_attr_netdev_group.attr,
169 &dev_attr_type.attr,
170 @@ -570,6 +609,7 @@ static struct attribute *net_class_attrs
171 &dev_attr_proto_down.attr,
172 &dev_attr_carrier_up_count.attr,
173 &dev_attr_carrier_down_count.attr,
174 + &dev_attr_threaded.attr,
175 NULL,
176 };
177 ATTRIBUTE_GROUPS(net_class);