octeon: disable edgerouter image
[openwrt/staging/chunkeey.git] / target / linux / generic / pending-4.19 / 690-net-add-support-for-threaded-NAPI-polling.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sun, 26 Jul 2020 14:03:21 +0200
3 Subject: [PATCH] net: add support for threaded NAPI polling
4
5 For some drivers (especially 802.11 drivers), doing a lot of work in the NAPI
6 poll function does not perform well. Since NAPI poll is bound to the CPU it
7 was scheduled from, we can easily end up with a few very busy CPUs spending
8 most of their time in softirq/ksoftirqd and some idle ones.
9
10 Introduce threaded NAPI for such drivers based on a workqueue. The API is the
11 same except for using netif_threaded_napi_add instead of netif_napi_add.
12
13 In my tests with mt76 on MT7621 using threaded NAPI + a thread for tx scheduling
14 improves LAN->WLAN bridging throughput by 10-50%. Throughput without threaded
15 NAPI is wildly inconsistent, depending on the CPU that runs the tx scheduling
16 thread.
17
18 With threaded NAPI it seems stable and consistent (and higher than the best
19 results I got without it).
20
21 Based on a patch by Hillf Danton
22
23 Cc: Hillf Danton <hdanton@sina.com>
24 Signed-off-by: Felix Fietkau <nbd@nbd.name>
25 ---
26
27 --- a/include/linux/netdevice.h
28 +++ b/include/linux/netdevice.h
29 @@ -339,6 +339,7 @@ struct napi_struct {
30 struct list_head dev_list;
31 struct hlist_node napi_hash_node;
32 unsigned int napi_id;
33 + struct work_struct work;
34 };
35
36 enum {
37 @@ -349,6 +350,7 @@ enum {
38 NAPI_STATE_HASHED, /* In NAPI hash (busy polling possible) */
39 NAPI_STATE_NO_BUSY_POLL,/* Do not add in napi_hash, no busy polling */
40 NAPI_STATE_IN_BUSY_POLL,/* sk_busy_loop() owns this NAPI */
41 + NAPI_STATE_THREADED, /* Use threaded NAPI */
42 };
43
44 enum {
45 @@ -359,6 +361,7 @@ enum {
46 NAPIF_STATE_HASHED = BIT(NAPI_STATE_HASHED),
47 NAPIF_STATE_NO_BUSY_POLL = BIT(NAPI_STATE_NO_BUSY_POLL),
48 NAPIF_STATE_IN_BUSY_POLL = BIT(NAPI_STATE_IN_BUSY_POLL),
49 + NAPIF_STATE_THREADED = BIT(NAPI_STATE_THREADED),
50 };
51
52 enum gro_result {
53 @@ -2230,6 +2233,26 @@ void netif_napi_add(struct net_device *d
54 int (*poll)(struct napi_struct *, int), int weight);
55
56 /**
57 + * netif_threaded_napi_add - initialize a NAPI context
58 + * @dev: network device
59 + * @napi: NAPI context
60 + * @poll: polling function
61 + * @weight: default weight
62 + *
63 + * This variant of netif_napi_add() should be used from drivers using NAPI
64 + * with CPU intensive poll functions.
65 + * This will schedule polling from a high priority workqueue
66 + */
67 +static inline void netif_threaded_napi_add(struct net_device *dev,
68 + struct napi_struct *napi,
69 + int (*poll)(struct napi_struct *, int),
70 + int weight)
71 +{
72 + set_bit(NAPI_STATE_THREADED, &napi->state);
73 + netif_napi_add(dev, napi, poll, weight);
74 +}
75 +
76 +/**
77 * netif_tx_napi_add - initialize a NAPI context
78 * @dev: network device
79 * @napi: NAPI context
80 --- a/net/core/dev.c
81 +++ b/net/core/dev.c
82 @@ -160,6 +160,7 @@ static DEFINE_SPINLOCK(offload_lock);
83 struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
84 struct list_head ptype_all __read_mostly; /* Taps */
85 static struct list_head offload_base __read_mostly;
86 +static struct workqueue_struct *napi_workq __read_mostly;
87
88 static int netif_rx_internal(struct sk_buff *skb);
89 static int call_netdevice_notifiers_info(unsigned long val,
90 @@ -5891,6 +5892,11 @@ void __napi_schedule(struct napi_struct
91 {
92 unsigned long flags;
93
94 + if (test_bit(NAPI_STATE_THREADED, &n->state)) {
95 + queue_work(napi_workq, &n->work);
96 + return;
97 + }
98 +
99 local_irq_save(flags);
100 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
101 local_irq_restore(flags);
102 @@ -5938,6 +5944,11 @@ EXPORT_SYMBOL(napi_schedule_prep);
103 */
104 void __napi_schedule_irqoff(struct napi_struct *n)
105 {
106 + if (test_bit(NAPI_STATE_THREADED, &n->state)) {
107 + queue_work(napi_workq, &n->work);
108 + return;
109 + }
110 +
111 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
112 }
113 EXPORT_SYMBOL(__napi_schedule_irqoff);
114 @@ -6186,6 +6197,82 @@ static void init_gro_hash(struct napi_st
115 napi->gro_bitmask = 0;
116 }
117
118 +static int __napi_poll(struct napi_struct *n, bool *repoll)
119 +{
120 + int work, weight;
121 +
122 + weight = n->weight;
123 +
124 + /* This NAPI_STATE_SCHED test is for avoiding a race
125 + * with netpoll's poll_napi(). Only the entity which
126 + * obtains the lock and sees NAPI_STATE_SCHED set will
127 + * actually make the ->poll() call. Therefore we avoid
128 + * accidentally calling ->poll() when NAPI is not scheduled.
129 + */
130 + work = 0;
131 + if (test_bit(NAPI_STATE_SCHED, &n->state)) {
132 + work = n->poll(n, weight);
133 + trace_napi_poll(n, work, weight);
134 + }
135 +
136 + WARN_ON_ONCE(work > weight);
137 +
138 + if (likely(work < weight))
139 + return work;
140 +
141 + /* Drivers must not modify the NAPI state if they
142 + * consume the entire weight. In such cases this code
143 + * still "owns" the NAPI instance and therefore can
144 + * move the instance around on the list at-will.
145 + */
146 + if (unlikely(napi_disable_pending(n))) {
147 + napi_complete(n);
148 + return work;
149 + }
150 +
151 + if (n->gro_bitmask) {
152 + /* flush too old packets
153 + * If HZ < 1000, flush all packets.
154 + */
155 + napi_gro_flush(n, HZ >= 1000);
156 + }
157 +
158 + *repoll = true;
159 +
160 + return work;
161 +}
162 +
163 +static void napi_workfn(struct work_struct *work)
164 +{
165 + struct napi_struct *n = container_of(work, struct napi_struct, work);
166 + void *have;
167 +
168 + for (;;) {
169 + bool repoll = false;
170 +
171 + local_bh_disable();
172 +
173 + have = netpoll_poll_lock(n);
174 + __napi_poll(n, &repoll);
175 + netpoll_poll_unlock(have);
176 +
177 + local_bh_enable();
178 +
179 + if (!repoll)
180 + return;
181 +
182 + if (!need_resched())
183 + continue;
184 +
185 + /*
186 + * have to pay for the latency of task switch even if
187 + * napi is scheduled
188 + */
189 + queue_work(napi_workq, work);
190 + return;
191 + }
192 +}
193 +
194 void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
195 int (*poll)(struct napi_struct *, int), int weight)
196 {
197 @@ -6204,6 +6291,7 @@ void netif_napi_add(struct net_device *d
198 #ifdef CONFIG_NETPOLL
199 napi->poll_owner = -1;
200 #endif
201 + INIT_WORK(&napi->work, napi_workfn);
202 set_bit(NAPI_STATE_SCHED, &napi->state);
203 napi_hash_add(napi);
204 }
205 @@ -6242,6 +6330,7 @@ static void flush_gro_hash(struct napi_s
206 void netif_napi_del(struct napi_struct *napi)
207 {
208 might_sleep();
209 + cancel_work_sync(&napi->work);
210 if (napi_hash_del(napi))
211 synchronize_net();
212 list_del_init(&napi->dev_list);
213 @@ -6254,48 +6343,18 @@ EXPORT_SYMBOL(netif_napi_del);
214
215 static int napi_poll(struct napi_struct *n, struct list_head *repoll)
216 {
217 + bool do_repoll = false;
218 void *have;
219 - int work, weight;
220 + int work;
221
222 list_del_init(&n->poll_list);
223
224 have = netpoll_poll_lock(n);
225
226 - weight = n->weight;
227 -
228 - /* This NAPI_STATE_SCHED test is for avoiding a race
229 - * with netpoll's poll_napi(). Only the entity which
230 - * obtains the lock and sees NAPI_STATE_SCHED set will
231 - * actually make the ->poll() call. Therefore we avoid
232 - * accidentally calling ->poll() when NAPI is not scheduled.
233 - */
234 - work = 0;
235 - if (test_bit(NAPI_STATE_SCHED, &n->state)) {
236 - work = n->poll(n, weight);
237 - trace_napi_poll(n, work, weight);
238 - }
239 -
240 - WARN_ON_ONCE(work > weight);
241 -
242 - if (likely(work < weight))
243 - goto out_unlock;
244 + work = __napi_poll(n, &do_repoll);
245
246 - /* Drivers must not modify the NAPI state if they
247 - * consume the entire weight. In such cases this code
248 - * still "owns" the NAPI instance and therefore can
249 - * move the instance around on the list at-will.
250 - */
251 - if (unlikely(napi_disable_pending(n))) {
252 - napi_complete(n);
253 + if (!do_repoll)
254 goto out_unlock;
255 - }
256 -
257 - if (n->gro_bitmask) {
258 - /* flush too old packets
259 - * If HZ < 1000, flush all packets.
260 - */
261 - napi_gro_flush(n, HZ >= 1000);
262 - }
263
264 /* Some drivers may have called napi_schedule
265 * prior to exhausting their budget.
266 @@ -9895,6 +9954,10 @@ static int __init net_dev_init(void)
267 sd->backlog.weight = weight_p;
268 }
269
270 + napi_workq = alloc_workqueue("napi_workq", WQ_UNBOUND | WQ_HIGHPRI,
271 + WQ_UNBOUND_MAX_ACTIVE | WQ_SYSFS);
272 + BUG_ON(!napi_workq);
273 +
274 dev_boot_phase = 0;
275
276 /* The loopback device is special if any other network devices
277 --- a/net/core/net-sysfs.c
278 +++ b/net/core/net-sysfs.c
279 @@ -447,6 +447,52 @@ static ssize_t proto_down_store(struct d
280 }
281 NETDEVICE_SHOW_RW(proto_down, fmt_dec);
282
283 +static int change_napi_threaded(struct net_device *dev, unsigned long val)
284 +{
285 + struct napi_struct *napi;
286 +
287 + if (list_empty(&dev->napi_list))
288 + return -EOPNOTSUPP;
289 +
290 + list_for_each_entry(napi, &dev->napi_list, dev_list) {
291 + if (val)
292 + set_bit(NAPI_STATE_THREADED, &napi->state);
293 + else
294 + clear_bit(NAPI_STATE_THREADED, &napi->state);
295 + }
296 +
297 + return 0;
298 +}
299 +
300 +static ssize_t napi_threaded_store(struct device *dev,
301 + struct device_attribute *attr,
302 + const char *buf, size_t len)
303 +{
304 + return netdev_store(dev, attr, buf, len, change_napi_threaded);
305 +}
306 +
307 +static ssize_t napi_threaded_show(struct device *dev,
308 + struct device_attribute *attr,
309 + char *buf)
310 +{
311 + struct net_device *netdev = to_net_dev(dev);
312 + struct napi_struct *napi;
313 + bool enabled = false;
314 +
315 + if (!rtnl_trylock())
316 + return restart_syscall();
317 +
318 + list_for_each_entry(napi, &netdev->napi_list, dev_list) {
319 + if (test_bit(NAPI_STATE_THREADED, &napi->state))
320 + enabled = true;
321 + }
322 +
323 + rtnl_unlock();
324 +
325 + return sprintf(buf, fmt_dec, enabled);
326 +}
327 +static DEVICE_ATTR_RW(napi_threaded);
328 +
329 static ssize_t phys_port_id_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331 {
332 @@ -542,6 +588,7 @@ static struct attribute *net_class_attrs
333 &dev_attr_flags.attr,
334 &dev_attr_tx_queue_len.attr,
335 &dev_attr_gro_flush_timeout.attr,
336 + &dev_attr_napi_threaded.attr,
337 &dev_attr_phys_port_id.attr,
338 &dev_attr_phys_port_name.attr,
339 &dev_attr_phys_switch_id.attr,