760-net-core-add-optional-threading-for-backlog-processi.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. From: Felix Fietkau <[email protected]>
  2. Date: Thu, 16 Feb 2023 18:39:04 +0100
  3. Subject: [PATCH] net/core: add optional threading for backlog processing
  4. When dealing with few flows or an imbalance on CPU utilization, static RPS
  5. CPU assignment can be too inflexible. Add support for enabling threaded NAPI
  6. for backlog processing in order to allow the scheduler to better balance
  7. processing. This helps better spread the load across idle CPUs.
  8. Signed-off-by: Felix Fietkau <[email protected]>
  9. ---
  10. --- a/include/linux/netdevice.h
  11. +++ b/include/linux/netdevice.h
  12. @@ -502,6 +502,7 @@ static inline bool napi_complete(struct
  13. }
  14. int dev_set_threaded(struct net_device *dev, bool threaded);
  15. +int backlog_set_threaded(bool threaded);
  16. /**
  17. * napi_disable - prevent NAPI from scheduling
  18. @@ -3368,6 +3369,7 @@ struct softnet_data {
  19. unsigned int processed;
  20. unsigned int time_squeeze;
  21. unsigned int received_rps;
  22. + unsigned int process_queue_empty;
  23. #ifdef CONFIG_RPS
  24. struct softnet_data *rps_ipi_list;
  25. #endif
  26. --- a/net/core/dev.c
  27. +++ b/net/core/dev.c
  28. @@ -4578,7 +4578,7 @@ static int rps_ipi_queued(struct softnet
  29. #ifdef CONFIG_RPS
  30. struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
  31. - if (sd != mysd) {
  32. + if (sd != mysd && !test_bit(NAPI_STATE_THREADED, &sd->backlog.state)) {
  33. sd->rps_ipi_next = mysd->rps_ipi_list;
  34. mysd->rps_ipi_list = sd;
  35. @@ -5759,6 +5759,8 @@ static DEFINE_PER_CPU(struct work_struct
  36. /* Network device is going away, flush any packets still pending */
  37. static void flush_backlog(struct work_struct *work)
  38. {
  39. + unsigned int process_queue_empty;
  40. + bool threaded, flush_processq;
  41. struct sk_buff *skb, *tmp;
  42. struct softnet_data *sd;
  43. @@ -5774,9 +5776,18 @@ static void flush_backlog(struct work_st
  44. input_queue_head_incr(sd);
  45. }
  46. }
  47. +
  48. + threaded = test_bit(NAPI_STATE_THREADED, &sd->backlog.state);
  49. + flush_processq = threaded &&
  50. + !skb_queue_empty_lockless(&sd->process_queue);
  51. + if (flush_processq)
  52. + process_queue_empty = sd->process_queue_empty;
  53. rps_unlock(sd);
  54. local_irq_enable();
  55. + if (threaded)
  56. + goto out;
  57. +
  58. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  59. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  60. __skb_unlink(skb, &sd->process_queue);
  61. @@ -5784,7 +5795,18 @@ static void flush_backlog(struct work_st
  62. input_queue_head_incr(sd);
  63. }
  64. }
  65. +
  66. +out:
  67. local_bh_enable();
  68. +
  69. + while (flush_processq) {
  70. + msleep(1);
  71. + local_irq_disable();
  72. + rps_lock(sd);
  73. + flush_processq = process_queue_empty == sd->process_queue_empty;
  74. + rps_unlock(sd);
  75. + local_irq_enable();
  76. + }
  77. }
  78. static bool flush_required(int cpu)
  79. @@ -6467,6 +6489,7 @@ static int process_backlog(struct napi_s
  80. local_irq_disable();
  81. rps_lock(sd);
  82. + sd->process_queue_empty++;
  83. if (skb_queue_empty(&sd->input_pkt_queue)) {
  84. /*
  85. * Inline a custom version of __napi_complete().
  86. @@ -6476,7 +6499,8 @@ static int process_backlog(struct napi_s
  87. * We can use a plain write instead of clear_bit(),
  88. * and we dont need an smp_mb() memory barrier.
  89. */
  90. - napi->state = 0;
  91. + napi->state &= ~(NAPIF_STATE_SCHED |
  92. + NAPIF_STATE_SCHED_THREADED);
  93. again = false;
  94. } else {
  95. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  96. @@ -6893,6 +6917,57 @@ int dev_set_threaded(struct net_device *
  97. }
  98. EXPORT_SYMBOL(dev_set_threaded);
  99. +int backlog_set_threaded(bool threaded)
  100. +{
  101. + static bool backlog_threaded;
  102. + int err = 0;
  103. + int i;
  104. +
  105. + if (backlog_threaded == threaded)
  106. + return 0;
  107. +
  108. + for_each_possible_cpu(i) {
  109. + struct softnet_data *sd = &per_cpu(softnet_data, i);
  110. + struct napi_struct *n = &sd->backlog;
  111. +
  112. + if (n->thread)
  113. + continue;
  114. + n->thread = kthread_run(napi_threaded_poll, n, "napi/backlog-%d", i);
  115. + if (IS_ERR(n->thread)) {
  116. + err = PTR_ERR(n->thread);
  117. + pr_err("kthread_run failed with err %d\n", err);
  118. + n->thread = NULL;
  119. + threaded = false;
  120. + break;
  121. + }
  122. +
  123. + }
  124. +
  125. + backlog_threaded = threaded;
  126. +
  127. + /* Make sure kthread is created before THREADED bit
  128. + * is set.
  129. + */
  130. + smp_mb__before_atomic();
  131. +
  132. + for_each_possible_cpu(i) {
  133. + struct softnet_data *sd = &per_cpu(softnet_data, i);
  134. + struct napi_struct *n = &sd->backlog;
  135. + unsigned long flags;
  136. +
  137. + local_irq_save(flags);
  138. + rps_lock(sd);
  139. + if (threaded)
  140. + n->state |= NAPIF_STATE_THREADED;
  141. + else
  142. + n->state &= ~NAPIF_STATE_THREADED;
  143. + rps_unlock(sd);
  144. + local_irq_restore(flags);
  145. + }
  146. +
  147. + return err;
  148. +}
  149. +
  150. void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
  151. int (*poll)(struct napi_struct *, int), int weight)
  152. {
  153. @@ -11371,6 +11446,9 @@ static int dev_cpu_dead(unsigned int old
  154. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  155. local_irq_enable();
  156. + if (test_bit(NAPI_STATE_THREADED, &oldsd->backlog.state))
  157. + return 0;
  158. +
  159. #ifdef CONFIG_RPS
  160. remsd = oldsd->rps_ipi_list;
  161. oldsd->rps_ipi_list = NULL;
  162. @@ -11710,6 +11788,7 @@ static int __init net_dev_init(void)
  163. sd->cpu = i;
  164. #endif
  165. + INIT_LIST_HEAD(&sd->backlog.poll_list);
  166. init_gro_hash(&sd->backlog);
  167. sd->backlog.poll = process_backlog;
  168. sd->backlog.weight = weight_p;
  169. --- a/net/core/sysctl_net_core.c
  170. +++ b/net/core/sysctl_net_core.c
  171. @@ -28,6 +28,7 @@ static int int_3600 = 3600;
  172. static int min_sndbuf = SOCK_MIN_SNDBUF;
  173. static int min_rcvbuf = SOCK_MIN_RCVBUF;
  174. static int max_skb_frags = MAX_SKB_FRAGS;
  175. +static int backlog_threaded;
  176. static long long_one __maybe_unused = 1;
  177. static long long_max __maybe_unused = LONG_MAX;
  178. @@ -114,6 +115,23 @@ static int rps_sock_flow_sysctl(struct c
  179. }
  180. #endif /* CONFIG_RPS */
  181. +static int backlog_threaded_sysctl(struct ctl_table *table, int write,
  182. + void *buffer, size_t *lenp, loff_t *ppos)
  183. +{
  184. + static DEFINE_MUTEX(backlog_threaded_mutex);
  185. + int ret;
  186. +
  187. + mutex_lock(&backlog_threaded_mutex);
  188. +
  189. + ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  190. + if (write && !ret)
  191. + ret = backlog_set_threaded(backlog_threaded);
  192. +
  193. + mutex_unlock(&backlog_threaded_mutex);
  194. +
  195. + return ret;
  196. +}
  197. +
  198. #ifdef CONFIG_NET_FLOW_LIMIT
  199. static DEFINE_MUTEX(flow_limit_update_mutex);
  200. @@ -470,6 +488,15 @@ static struct ctl_table net_core_table[]
  201. .proc_handler = rps_sock_flow_sysctl
  202. },
  203. #endif
  204. + {
  205. + .procname = "backlog_threaded",
  206. + .data = &backlog_threaded,
  207. + .maxlen = sizeof(unsigned int),
  208. + .mode = 0644,
  209. + .proc_handler = backlog_threaded_sysctl,
  210. + .extra1 = SYSCTL_ZERO,
  211. + .extra2 = SYSCTL_ONE
  212. + },
  213. #ifdef CONFIG_NET_FLOW_LIMIT
  214. {
  215. .procname = "flow_limit_cpu_bitmap",