690-net-add-support-for-threaded-NAPI-polling.patch 9.3 KB

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