napi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "io_uring.h"
  3. #include "napi.h"
  4. #ifdef CONFIG_NET_RX_BUSY_POLL
  5. /* Timeout for cleanout of stale entries. */
  6. #define NAPI_TIMEOUT (60 * SEC_CONVERSION)
  7. struct io_napi_entry {
  8. unsigned int napi_id;
  9. struct list_head list;
  10. unsigned long timeout;
  11. struct hlist_node node;
  12. struct rcu_head rcu;
  13. };
  14. static struct io_napi_entry *io_napi_hash_find(struct hlist_head *hash_list,
  15. unsigned int napi_id)
  16. {
  17. struct io_napi_entry *e;
  18. hlist_for_each_entry_rcu(e, hash_list, node) {
  19. if (e->napi_id != napi_id)
  20. continue;
  21. return e;
  22. }
  23. return NULL;
  24. }
  25. static inline ktime_t net_to_ktime(unsigned long t)
  26. {
  27. /* napi approximating usecs, reverse busy_loop_current_time */
  28. return ns_to_ktime(t << 10);
  29. }
  30. void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock)
  31. {
  32. struct hlist_head *hash_list;
  33. unsigned int napi_id;
  34. struct sock *sk;
  35. struct io_napi_entry *e;
  36. sk = sock->sk;
  37. if (!sk)
  38. return;
  39. napi_id = READ_ONCE(sk->sk_napi_id);
  40. /* Non-NAPI IDs can be rejected. */
  41. if (napi_id < MIN_NAPI_ID)
  42. return;
  43. hash_list = &ctx->napi_ht[hash_min(napi_id, HASH_BITS(ctx->napi_ht))];
  44. rcu_read_lock();
  45. e = io_napi_hash_find(hash_list, napi_id);
  46. if (e) {
  47. e->timeout = jiffies + NAPI_TIMEOUT;
  48. rcu_read_unlock();
  49. return;
  50. }
  51. rcu_read_unlock();
  52. e = kmalloc(sizeof(*e), GFP_NOWAIT);
  53. if (!e)
  54. return;
  55. e->napi_id = napi_id;
  56. e->timeout = jiffies + NAPI_TIMEOUT;
  57. spin_lock(&ctx->napi_lock);
  58. if (unlikely(io_napi_hash_find(hash_list, napi_id))) {
  59. spin_unlock(&ctx->napi_lock);
  60. kfree(e);
  61. return;
  62. }
  63. hlist_add_tail_rcu(&e->node, hash_list);
  64. list_add_tail(&e->list, &ctx->napi_list);
  65. spin_unlock(&ctx->napi_lock);
  66. }
  67. static void __io_napi_remove_stale(struct io_ring_ctx *ctx)
  68. {
  69. struct io_napi_entry *e;
  70. unsigned int i;
  71. spin_lock(&ctx->napi_lock);
  72. hash_for_each(ctx->napi_ht, i, e, node) {
  73. if (time_after(jiffies, e->timeout)) {
  74. list_del(&e->list);
  75. hash_del_rcu(&e->node);
  76. kfree_rcu(e, rcu);
  77. }
  78. }
  79. spin_unlock(&ctx->napi_lock);
  80. }
  81. static inline void io_napi_remove_stale(struct io_ring_ctx *ctx, bool is_stale)
  82. {
  83. if (is_stale)
  84. __io_napi_remove_stale(ctx);
  85. }
  86. static inline bool io_napi_busy_loop_timeout(ktime_t start_time,
  87. ktime_t bp)
  88. {
  89. if (bp) {
  90. ktime_t end_time = ktime_add(start_time, bp);
  91. ktime_t now = net_to_ktime(busy_loop_current_time());
  92. return ktime_after(now, end_time);
  93. }
  94. return true;
  95. }
  96. static bool io_napi_busy_loop_should_end(void *data,
  97. unsigned long start_time)
  98. {
  99. struct io_wait_queue *iowq = data;
  100. if (signal_pending(current))
  101. return true;
  102. if (io_should_wake(iowq) || io_has_work(iowq->ctx))
  103. return true;
  104. if (io_napi_busy_loop_timeout(net_to_ktime(start_time),
  105. iowq->napi_busy_poll_dt))
  106. return true;
  107. return false;
  108. }
  109. static bool __io_napi_do_busy_loop(struct io_ring_ctx *ctx,
  110. void *loop_end_arg)
  111. {
  112. struct io_napi_entry *e;
  113. bool (*loop_end)(void *, unsigned long) = NULL;
  114. bool is_stale = false;
  115. if (loop_end_arg)
  116. loop_end = io_napi_busy_loop_should_end;
  117. list_for_each_entry_rcu(e, &ctx->napi_list, list) {
  118. napi_busy_loop_rcu(e->napi_id, loop_end, loop_end_arg,
  119. ctx->napi_prefer_busy_poll, BUSY_POLL_BUDGET);
  120. if (time_after(jiffies, e->timeout))
  121. is_stale = true;
  122. }
  123. return is_stale;
  124. }
  125. static void io_napi_blocking_busy_loop(struct io_ring_ctx *ctx,
  126. struct io_wait_queue *iowq)
  127. {
  128. unsigned long start_time = busy_loop_current_time();
  129. void *loop_end_arg = NULL;
  130. bool is_stale = false;
  131. /* Singular lists use a different napi loop end check function and are
  132. * only executed once.
  133. */
  134. if (list_is_singular(&ctx->napi_list))
  135. loop_end_arg = iowq;
  136. rcu_read_lock();
  137. do {
  138. is_stale = __io_napi_do_busy_loop(ctx, loop_end_arg);
  139. } while (!io_napi_busy_loop_should_end(iowq, start_time) && !loop_end_arg);
  140. rcu_read_unlock();
  141. io_napi_remove_stale(ctx, is_stale);
  142. }
  143. /*
  144. * io_napi_init() - Init napi settings
  145. * @ctx: pointer to io-uring context structure
  146. *
  147. * Init napi settings in the io-uring context.
  148. */
  149. void io_napi_init(struct io_ring_ctx *ctx)
  150. {
  151. u64 sys_dt = READ_ONCE(sysctl_net_busy_poll) * NSEC_PER_USEC;
  152. INIT_LIST_HEAD(&ctx->napi_list);
  153. spin_lock_init(&ctx->napi_lock);
  154. ctx->napi_prefer_busy_poll = false;
  155. ctx->napi_busy_poll_dt = ns_to_ktime(sys_dt);
  156. }
  157. /*
  158. * io_napi_free() - Deallocate napi
  159. * @ctx: pointer to io-uring context structure
  160. *
  161. * Free the napi list and the hash table in the io-uring context.
  162. */
  163. void io_napi_free(struct io_ring_ctx *ctx)
  164. {
  165. struct io_napi_entry *e;
  166. unsigned int i;
  167. spin_lock(&ctx->napi_lock);
  168. hash_for_each(ctx->napi_ht, i, e, node) {
  169. hash_del_rcu(&e->node);
  170. kfree_rcu(e, rcu);
  171. }
  172. spin_unlock(&ctx->napi_lock);
  173. }
  174. /*
  175. * io_napi_register() - Register napi with io-uring
  176. * @ctx: pointer to io-uring context structure
  177. * @arg: pointer to io_uring_napi structure
  178. *
  179. * Register napi in the io-uring context.
  180. */
  181. int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
  182. {
  183. const struct io_uring_napi curr = {
  184. .busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
  185. .prefer_busy_poll = ctx->napi_prefer_busy_poll
  186. };
  187. struct io_uring_napi napi;
  188. if (ctx->flags & IORING_SETUP_IOPOLL)
  189. return -EINVAL;
  190. if (copy_from_user(&napi, arg, sizeof(napi)))
  191. return -EFAULT;
  192. if (napi.pad[0] || napi.pad[1] || napi.pad[2] || napi.resv)
  193. return -EINVAL;
  194. if (copy_to_user(arg, &curr, sizeof(curr)))
  195. return -EFAULT;
  196. WRITE_ONCE(ctx->napi_busy_poll_dt, napi.busy_poll_to * NSEC_PER_USEC);
  197. WRITE_ONCE(ctx->napi_prefer_busy_poll, !!napi.prefer_busy_poll);
  198. WRITE_ONCE(ctx->napi_enabled, true);
  199. return 0;
  200. }
  201. /*
  202. * io_napi_unregister() - Unregister napi with io-uring
  203. * @ctx: pointer to io-uring context structure
  204. * @arg: pointer to io_uring_napi structure
  205. *
  206. * Unregister napi. If arg has been specified copy the busy poll timeout and
  207. * prefer busy poll setting to the passed in structure.
  208. */
  209. int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
  210. {
  211. const struct io_uring_napi curr = {
  212. .busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
  213. .prefer_busy_poll = ctx->napi_prefer_busy_poll
  214. };
  215. if (arg && copy_to_user(arg, &curr, sizeof(curr)))
  216. return -EFAULT;
  217. WRITE_ONCE(ctx->napi_busy_poll_dt, 0);
  218. WRITE_ONCE(ctx->napi_prefer_busy_poll, false);
  219. WRITE_ONCE(ctx->napi_enabled, false);
  220. return 0;
  221. }
  222. /*
  223. * __io_napi_busy_loop() - execute busy poll loop
  224. * @ctx: pointer to io-uring context structure
  225. * @iowq: pointer to io wait queue
  226. *
  227. * Execute the busy poll loop and merge the spliced off list.
  228. */
  229. void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
  230. {
  231. if (ctx->flags & IORING_SETUP_SQPOLL)
  232. return;
  233. iowq->napi_busy_poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
  234. if (iowq->timeout != KTIME_MAX) {
  235. ktime_t dt = ktime_sub(iowq->timeout, io_get_time(ctx));
  236. iowq->napi_busy_poll_dt = min_t(u64, iowq->napi_busy_poll_dt, dt);
  237. }
  238. iowq->napi_prefer_busy_poll = READ_ONCE(ctx->napi_prefer_busy_poll);
  239. io_napi_blocking_busy_loop(ctx, iowq);
  240. }
  241. /*
  242. * io_napi_sqpoll_busy_poll() - busy poll loop for sqpoll
  243. * @ctx: pointer to io-uring context structure
  244. *
  245. * Splice of the napi list and execute the napi busy poll loop.
  246. */
  247. int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx)
  248. {
  249. bool is_stale = false;
  250. if (!READ_ONCE(ctx->napi_busy_poll_dt))
  251. return 0;
  252. if (list_empty_careful(&ctx->napi_list))
  253. return 0;
  254. rcu_read_lock();
  255. is_stale = __io_napi_do_busy_loop(ctx, NULL);
  256. rcu_read_unlock();
  257. io_napi_remove_stale(ctx, is_stale);
  258. return 1;
  259. }
  260. #endif