1 // SPDX-License-Identifier: GPL-2.0
6 #ifdef CONFIG_NET_RX_BUSY_POLL
8 /* Timeout for cleanout of stale entries. */
9 #define NAPI_TIMEOUT (60 * SEC_CONVERSION)
11 struct io_napi_entry {
13 struct list_head list;
15 unsigned long timeout;
16 struct hlist_node node;
21 static struct io_napi_entry *io_napi_hash_find(struct hlist_head *hash_list,
24 struct io_napi_entry *e;
26 hlist_for_each_entry_rcu(e, hash_list, node) {
27 if (e->napi_id != napi_id)
35 static inline ktime_t net_to_ktime(unsigned long t)
37 /* napi approximating usecs, reverse busy_loop_current_time */
38 return ns_to_ktime(t << 10);
41 void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock)
43 struct hlist_head *hash_list;
46 struct io_napi_entry *e;
52 napi_id = READ_ONCE(sk->sk_napi_id);
54 /* Non-NAPI IDs can be rejected. */
55 if (napi_id < MIN_NAPI_ID)
58 hash_list = &ctx->napi_ht[hash_min(napi_id, HASH_BITS(ctx->napi_ht))];
61 e = io_napi_hash_find(hash_list, napi_id);
63 e->timeout = jiffies + NAPI_TIMEOUT;
69 e = kmalloc(sizeof(*e), GFP_NOWAIT);
74 e->timeout = jiffies + NAPI_TIMEOUT;
76 spin_lock(&ctx->napi_lock);
77 if (unlikely(io_napi_hash_find(hash_list, napi_id))) {
78 spin_unlock(&ctx->napi_lock);
83 hlist_add_tail_rcu(&e->node, hash_list);
84 list_add_tail(&e->list, &ctx->napi_list);
85 spin_unlock(&ctx->napi_lock);
88 static void __io_napi_remove_stale(struct io_ring_ctx *ctx)
90 struct io_napi_entry *e;
93 spin_lock(&ctx->napi_lock);
94 hash_for_each(ctx->napi_ht, i, e, node) {
95 if (time_after(jiffies, e->timeout)) {
97 hash_del_rcu(&e->node);
101 spin_unlock(&ctx->napi_lock);
104 static inline void io_napi_remove_stale(struct io_ring_ctx *ctx, bool is_stale)
107 __io_napi_remove_stale(ctx);
110 static inline bool io_napi_busy_loop_timeout(ktime_t start_time,
114 ktime_t end_time = ktime_add(start_time, bp);
115 ktime_t now = net_to_ktime(busy_loop_current_time());
117 return ktime_after(now, end_time);
123 static bool io_napi_busy_loop_should_end(void *data,
124 unsigned long start_time)
126 struct io_wait_queue *iowq = data;
128 if (signal_pending(current))
130 if (io_should_wake(iowq) || io_has_work(iowq->ctx))
132 if (io_napi_busy_loop_timeout(net_to_ktime(start_time),
133 iowq->napi_busy_poll_dt))
139 static bool __io_napi_do_busy_loop(struct io_ring_ctx *ctx,
142 struct io_napi_entry *e;
143 bool (*loop_end)(void *, unsigned long) = NULL;
144 bool is_stale = false;
147 loop_end = io_napi_busy_loop_should_end;
149 list_for_each_entry_rcu(e, &ctx->napi_list, list) {
150 napi_busy_loop_rcu(e->napi_id, loop_end, loop_end_arg,
151 ctx->napi_prefer_busy_poll, BUSY_POLL_BUDGET);
153 if (time_after(jiffies, e->timeout))
160 static void io_napi_blocking_busy_loop(struct io_ring_ctx *ctx,
161 struct io_wait_queue *iowq)
163 unsigned long start_time = busy_loop_current_time();
164 void *loop_end_arg = NULL;
165 bool is_stale = false;
167 /* Singular lists use a different napi loop end check function and are
168 * only executed once.
170 if (list_is_singular(&ctx->napi_list))
175 is_stale = __io_napi_do_busy_loop(ctx, loop_end_arg);
176 } while (!io_napi_busy_loop_should_end(iowq, start_time) && !loop_end_arg);
179 io_napi_remove_stale(ctx, is_stale);
183 * io_napi_init() - Init napi settings
184 * @ctx: pointer to io-uring context structure
186 * Init napi settings in the io-uring context.
188 void io_napi_init(struct io_ring_ctx *ctx)
190 u64 sys_dt = READ_ONCE(sysctl_net_busy_poll) * NSEC_PER_USEC;
192 INIT_LIST_HEAD(&ctx->napi_list);
193 spin_lock_init(&ctx->napi_lock);
194 ctx->napi_prefer_busy_poll = false;
195 ctx->napi_busy_poll_dt = ns_to_ktime(sys_dt);
199 * io_napi_free() - Deallocate napi
200 * @ctx: pointer to io-uring context structure
202 * Free the napi list and the hash table in the io-uring context.
204 void io_napi_free(struct io_ring_ctx *ctx)
206 struct io_napi_entry *e;
209 spin_lock(&ctx->napi_lock);
210 hash_for_each(ctx->napi_ht, i, e, node) {
211 hash_del_rcu(&e->node);
214 spin_unlock(&ctx->napi_lock);
218 * io_napi_register() - Register napi with io-uring
219 * @ctx: pointer to io-uring context structure
220 * @arg: pointer to io_uring_napi structure
222 * Register napi in the io-uring context.
224 int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
226 const struct io_uring_napi curr = {
227 .busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
228 .prefer_busy_poll = ctx->napi_prefer_busy_poll
230 struct io_uring_napi napi;
232 if (ctx->flags & IORING_SETUP_IOPOLL)
234 if (copy_from_user(&napi, arg, sizeof(napi)))
236 if (napi.pad[0] || napi.pad[1] || napi.pad[2] || napi.resv)
239 if (copy_to_user(arg, &curr, sizeof(curr)))
242 WRITE_ONCE(ctx->napi_busy_poll_dt, napi.busy_poll_to * NSEC_PER_USEC);
243 WRITE_ONCE(ctx->napi_prefer_busy_poll, !!napi.prefer_busy_poll);
244 WRITE_ONCE(ctx->napi_enabled, true);
249 * io_napi_unregister() - Unregister napi with io-uring
250 * @ctx: pointer to io-uring context structure
251 * @arg: pointer to io_uring_napi structure
253 * Unregister napi. If arg has been specified copy the busy poll timeout and
254 * prefer busy poll setting to the passed in structure.
256 int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
258 const struct io_uring_napi curr = {
259 .busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
260 .prefer_busy_poll = ctx->napi_prefer_busy_poll
263 if (arg && copy_to_user(arg, &curr, sizeof(curr)))
266 WRITE_ONCE(ctx->napi_busy_poll_dt, 0);
267 WRITE_ONCE(ctx->napi_prefer_busy_poll, false);
268 WRITE_ONCE(ctx->napi_enabled, false);
273 * __io_napi_adjust_timeout() - adjust busy loop timeout
274 * @ctx: pointer to io-uring context structure
275 * @iowq: pointer to io wait queue
276 * @ts: pointer to timespec or NULL
278 * Adjust the busy loop timeout according to timespec and busy poll timeout.
279 * If the specified NAPI timeout is bigger than the wait timeout, then adjust
280 * the NAPI timeout accordingly.
282 void __io_napi_adjust_timeout(struct io_ring_ctx *ctx, struct io_wait_queue *iowq,
285 ktime_t poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
288 poll_dt = min(poll_dt, to_wait);
290 iowq->napi_busy_poll_dt = poll_dt;
294 * __io_napi_busy_loop() - execute busy poll loop
295 * @ctx: pointer to io-uring context structure
296 * @iowq: pointer to io wait queue
298 * Execute the busy poll loop and merge the spliced off list.
300 void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
302 iowq->napi_prefer_busy_poll = READ_ONCE(ctx->napi_prefer_busy_poll);
304 if (!(ctx->flags & IORING_SETUP_SQPOLL))
305 io_napi_blocking_busy_loop(ctx, iowq);
309 * io_napi_sqpoll_busy_poll() - busy poll loop for sqpoll
310 * @ctx: pointer to io-uring context structure
312 * Splice of the napi list and execute the napi busy poll loop.
314 int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx)
316 bool is_stale = false;
318 if (!READ_ONCE(ctx->napi_busy_poll_dt))
320 if (list_empty_careful(&ctx->napi_list))
324 is_stale = __io_napi_do_busy_loop(ctx, NULL);
327 io_napi_remove_stale(ctx, is_stale);