xref: /linux/io_uring/napi.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "io_uring.h"
4 #include "napi.h"
5 
6 #ifdef CONFIG_NET_RX_BUSY_POLL
7 
8 /* Timeout for cleanout of stale entries. */
9 #define NAPI_TIMEOUT		(60 * HZ)
10 
11 struct io_napi_entry {
12 	unsigned int		napi_id;
13 	struct list_head	list;
14 
15 	unsigned long		timeout;
16 	struct hlist_node	node;
17 
18 	struct rcu_head		rcu;
19 };
20 
21 static struct io_napi_entry *io_napi_hash_find(struct hlist_head *hash_list,
22 					       unsigned int napi_id)
23 {
24 	struct io_napi_entry *e;
25 
26 	hlist_for_each_entry_rcu(e, hash_list, node) {
27 		if (e->napi_id != napi_id)
28 			continue;
29 		return e;
30 	}
31 
32 	return NULL;
33 }
34 
35 static inline ktime_t net_to_ktime(unsigned long t)
36 {
37 	/* napi approximating usecs, reverse busy_loop_current_time */
38 	return ns_to_ktime(t << 10);
39 }
40 
41 int __io_napi_add_id(struct io_ring_ctx *ctx, unsigned int napi_id,
42 		     unsigned int mode)
43 {
44 	struct hlist_head *hash_list;
45 	struct io_napi_entry *e;
46 
47 	/* Non-NAPI IDs can be rejected. */
48 	if (!napi_id_valid(napi_id))
49 		return -EINVAL;
50 
51 	hash_list = &ctx->napi_ht[hash_min(napi_id, HASH_BITS(ctx->napi_ht))];
52 
53 	scoped_guard(rcu) {
54 		e = io_napi_hash_find(hash_list, napi_id);
55 		if (e) {
56 			WRITE_ONCE(e->timeout, jiffies + NAPI_TIMEOUT);
57 			return -EEXIST;
58 		}
59 	}
60 
61 	e = kmalloc_obj(*e, GFP_NOWAIT);
62 	if (!e)
63 		return -ENOMEM;
64 
65 	e->napi_id = napi_id;
66 	e->timeout = jiffies + NAPI_TIMEOUT;
67 
68 	/*
69 	 * guard(spinlock) is not used to manually unlock it before calling
70 	 * kfree()
71 	 */
72 	spin_lock(&ctx->napi_lock);
73 	if (unlikely(READ_ONCE(ctx->napi_track_mode) != mode)) {
74 		spin_unlock(&ctx->napi_lock);
75 		kfree(e);
76 		return -EINVAL;
77 	}
78 	if (unlikely(io_napi_hash_find(hash_list, napi_id))) {
79 		spin_unlock(&ctx->napi_lock);
80 		kfree(e);
81 		return -EEXIST;
82 	}
83 
84 	hlist_add_tail_rcu(&e->node, hash_list);
85 	list_add_tail_rcu(&e->list, &ctx->napi_list);
86 	spin_unlock(&ctx->napi_lock);
87 	return 0;
88 }
89 
90 static int __io_napi_del_id(struct io_ring_ctx *ctx, unsigned int napi_id)
91 {
92 	struct hlist_head *hash_list;
93 	struct io_napi_entry *e;
94 
95 	/* Non-NAPI IDs can be rejected. */
96 	if (!napi_id_valid(napi_id))
97 		return -EINVAL;
98 
99 	hash_list = &ctx->napi_ht[hash_min(napi_id, HASH_BITS(ctx->napi_ht))];
100 	guard(spinlock)(&ctx->napi_lock);
101 	e = io_napi_hash_find(hash_list, napi_id);
102 	if (!e)
103 		return -ENOENT;
104 
105 	list_del_rcu(&e->list);
106 	hash_del_rcu(&e->node);
107 	kfree_rcu(e, rcu);
108 	return 0;
109 }
110 
111 static void __io_napi_remove_stale(struct io_ring_ctx *ctx)
112 {
113 	struct io_napi_entry *e, *next;
114 
115 	guard(spinlock)(&ctx->napi_lock);
116 	list_for_each_entry_safe(e, next, &ctx->napi_list, list) {
117 		if (time_after(jiffies, READ_ONCE(e->timeout))) {
118 			list_del_rcu(&e->list);
119 			hash_del_rcu(&e->node);
120 			kfree_rcu(e, rcu);
121 		}
122 	}
123 }
124 
125 static inline void io_napi_remove_stale(struct io_ring_ctx *ctx, bool is_stale)
126 {
127 	if (is_stale)
128 		__io_napi_remove_stale(ctx);
129 }
130 
131 static inline bool io_napi_busy_loop_timeout(struct io_wait_queue *iowq)
132 {
133 	if (!iowq->napi_busy_poll_dt)
134 		return true;
135 	return ktime_after(net_to_ktime(busy_loop_current_time()),
136 			   iowq->napi_busy_poll_end);
137 }
138 
139 /* Uses an absolute deadline, so the napi loop can be re-entered */
140 static bool io_napi_busy_loop_should_end(void *data,
141 					 unsigned long start_time)
142 {
143 	struct io_wait_queue *iowq = data;
144 
145 	if (signal_pending(current))
146 		return true;
147 	if (io_should_wake(iowq) || io_has_work(iowq->ctx))
148 		return true;
149 	if (io_napi_busy_loop_timeout(iowq))
150 		return true;
151 
152 	return false;
153 }
154 
155 /*
156  * never report stale entries
157  */
158 static bool static_tracking_do_busy_loop(struct io_ring_ctx *ctx,
159 					 bool (*loop_end)(void *, unsigned long),
160 					 void *loop_end_arg)
161 {
162 	struct io_napi_entry *e;
163 
164 	list_for_each_entry_rcu(e, &ctx->napi_list, list)
165 		napi_busy_loop_rcu(e->napi_id, loop_end, loop_end_arg,
166 				   ctx->napi_prefer_busy_poll, BUSY_POLL_BUDGET);
167 	return false;
168 }
169 
170 static bool
171 dynamic_tracking_do_busy_loop(struct io_ring_ctx *ctx,
172 			      bool (*loop_end)(void *, unsigned long),
173 			      void *loop_end_arg)
174 {
175 	struct io_napi_entry *e;
176 	bool is_stale = false;
177 
178 	list_for_each_entry_rcu(e, &ctx->napi_list, list) {
179 		napi_busy_loop_rcu(e->napi_id, loop_end, loop_end_arg,
180 				   ctx->napi_prefer_busy_poll, BUSY_POLL_BUDGET);
181 
182 		if (time_after(jiffies, READ_ONCE(e->timeout)))
183 			is_stale = true;
184 	}
185 
186 	return is_stale;
187 }
188 
189 static inline bool
190 __io_napi_do_busy_loop(struct io_ring_ctx *ctx,
191 		       bool (*loop_end)(void *, unsigned long),
192 		       void *loop_end_arg)
193 {
194 	switch (READ_ONCE(ctx->napi_track_mode)) {
195 	case IO_URING_NAPI_TRACKING_STATIC:
196 		return static_tracking_do_busy_loop(ctx, loop_end, loop_end_arg);
197 	case IO_URING_NAPI_TRACKING_DYNAMIC:
198 		return dynamic_tracking_do_busy_loop(ctx, loop_end, loop_end_arg);
199 	default:
200 		return false;
201 	}
202 }
203 
204 static void io_napi_blocking_busy_loop(struct io_ring_ctx *ctx,
205 				       struct io_wait_queue *iowq)
206 {
207 	ktime_t now = net_to_ktime(busy_loop_current_time());
208 	bool (*loop_end)(void *, unsigned long) = NULL;
209 	void *loop_end_arg = NULL;
210 	bool is_stale = false;
211 
212 	iowq->napi_busy_poll_end = ktime_add(now, iowq->napi_busy_poll_dt);
213 
214 	/*
215 	 * A single NAPI is held by __napi_busy_loop() for the whole spin. It
216 	 * may return early on need_resched(), loop until our own deadline.
217 	 */
218 	if (list_is_singular(&ctx->napi_list)) {
219 		loop_end = io_napi_busy_loop_should_end;
220 		loop_end_arg = iowq;
221 	}
222 
223 	scoped_guard(rcu) {
224 		do {
225 			is_stale = __io_napi_do_busy_loop(ctx, loop_end,
226 							  loop_end_arg);
227 		} while (!io_napi_busy_loop_should_end(iowq, 0));
228 	}
229 
230 	io_napi_remove_stale(ctx, is_stale);
231 }
232 
233 /*
234  * io_napi_init() - Init napi settings
235  * @ctx: pointer to io-uring context structure
236  *
237  * Init napi settings in the io-uring context.
238  */
239 void io_napi_init(struct io_ring_ctx *ctx)
240 {
241 	u64 sys_dt = READ_ONCE(sysctl_net_busy_poll) * NSEC_PER_USEC;
242 
243 	INIT_LIST_HEAD(&ctx->napi_list);
244 	spin_lock_init(&ctx->napi_lock);
245 	ctx->napi_prefer_busy_poll = false;
246 	ctx->napi_busy_poll_dt = ns_to_ktime(sys_dt);
247 	ctx->napi_track_mode = IO_URING_NAPI_TRACKING_INACTIVE;
248 }
249 
250 /*
251  * io_napi_free() - Deallocate napi
252  * @ctx: pointer to io-uring context structure
253  *
254  * Free the napi list and the hash table in the io-uring context.
255  */
256 void io_napi_free(struct io_ring_ctx *ctx)
257 {
258 	struct io_napi_entry *e, *next;
259 
260 	guard(spinlock)(&ctx->napi_lock);
261 	list_for_each_entry_safe(e, next, &ctx->napi_list, list) {
262 		hash_del_rcu(&e->node);
263 		kfree_rcu(e, rcu);
264 	}
265 	INIT_LIST_HEAD_RCU(&ctx->napi_list);
266 }
267 
268 static int io_napi_register_napi(struct io_ring_ctx *ctx,
269 				 struct io_uring_napi *napi)
270 {
271 	switch (napi->op_param) {
272 	case IO_URING_NAPI_TRACKING_DYNAMIC:
273 	case IO_URING_NAPI_TRACKING_STATIC:
274 		break;
275 	default:
276 		return -EINVAL;
277 	}
278 	WRITE_ONCE(ctx->napi_track_mode, IO_URING_NAPI_TRACKING_INACTIVE);
279 	io_napi_free(ctx);
280 	/* cap NAPI at 10 msec of spin time */
281 	napi->busy_poll_to = min(10000, napi->busy_poll_to);
282 	WRITE_ONCE(ctx->napi_busy_poll_dt, napi->busy_poll_to * NSEC_PER_USEC);
283 	WRITE_ONCE(ctx->napi_prefer_busy_poll, !!napi->prefer_busy_poll);
284 	WRITE_ONCE(ctx->napi_track_mode, napi->op_param);
285 	return 0;
286 }
287 
288 /*
289  * io_napi_register() - Register napi with io-uring
290  * @ctx: pointer to io-uring context structure
291  * @arg: pointer to io_uring_napi structure
292  *
293  * Register napi in the io-uring context.
294  */
295 int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
296 {
297 	const struct io_uring_napi curr = {
298 		.busy_poll_to 	  = ktime_to_us(ctx->napi_busy_poll_dt),
299 		.prefer_busy_poll = ctx->napi_prefer_busy_poll,
300 		.op_param	  = ctx->napi_track_mode
301 	};
302 	struct io_uring_napi napi;
303 
304 	if (ctx->flags & IORING_SETUP_IOPOLL)
305 		return -EINVAL;
306 	if (copy_from_user(&napi, arg, sizeof(napi)))
307 		return -EFAULT;
308 	if (napi.pad[0] || napi.pad[1] || napi.resv)
309 		return -EINVAL;
310 
311 	if (copy_to_user(arg, &curr, sizeof(curr)))
312 		return -EFAULT;
313 
314 	switch (napi.opcode) {
315 	case IO_URING_NAPI_REGISTER_OP:
316 		return io_napi_register_napi(ctx, &napi);
317 	case IO_URING_NAPI_STATIC_ADD_ID:
318 		if (curr.op_param != IO_URING_NAPI_TRACKING_STATIC)
319 			return -EINVAL;
320 		return __io_napi_add_id(ctx, napi.op_param,
321 					IO_URING_NAPI_TRACKING_STATIC);
322 	case IO_URING_NAPI_STATIC_DEL_ID:
323 		if (curr.op_param != IO_URING_NAPI_TRACKING_STATIC)
324 			return -EINVAL;
325 		return __io_napi_del_id(ctx, napi.op_param);
326 	default:
327 		return -EINVAL;
328 	}
329 }
330 
331 /*
332  * io_napi_unregister() - Unregister napi with io-uring
333  * @ctx: pointer to io-uring context structure
334  * @arg: pointer to io_uring_napi structure
335  *
336  * Unregister napi. If arg has been specified copy the busy poll timeout and
337  * prefer busy poll setting to the passed in structure.
338  */
339 int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
340 {
341 	const struct io_uring_napi curr = {
342 		.busy_poll_to 	  = ktime_to_us(ctx->napi_busy_poll_dt),
343 		.prefer_busy_poll = ctx->napi_prefer_busy_poll
344 	};
345 
346 	if (arg && copy_to_user(arg, &curr, sizeof(curr)))
347 		return -EFAULT;
348 
349 	WRITE_ONCE(ctx->napi_track_mode, IO_URING_NAPI_TRACKING_INACTIVE);
350 	WRITE_ONCE(ctx->napi_busy_poll_dt, 0);
351 	WRITE_ONCE(ctx->napi_prefer_busy_poll, false);
352 	io_napi_free(ctx);
353 	return 0;
354 }
355 
356 /*
357  * __io_napi_busy_loop() - execute busy poll loop
358  * @ctx: pointer to io-uring context structure
359  * @iowq: pointer to io wait queue
360  *
361  * Execute the busy poll loop and merge the spliced off list.
362  */
363 void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
364 {
365 	if (ctx->flags & IORING_SETUP_SQPOLL)
366 		return;
367 
368 	iowq->napi_busy_poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
369 	if (iowq->timeout != KTIME_MAX) {
370 		ktime_t dt = ktime_sub(iowq->timeout, io_get_time(ctx));
371 
372 		/* an absolute timeout may have passed already */
373 		dt = max_t(ktime_t, dt, 0);
374 		iowq->napi_busy_poll_dt = min_t(u64, iowq->napi_busy_poll_dt, dt);
375 	}
376 
377 	iowq->napi_prefer_busy_poll = READ_ONCE(ctx->napi_prefer_busy_poll);
378 	io_napi_blocking_busy_loop(ctx, iowq);
379 }
380 
381 /*
382  * io_napi_sqpoll_busy_poll() - busy poll loop for sqpoll
383  * @ctx: pointer to io-uring context structure
384  *
385  * Splice of the napi list and execute the napi busy poll loop.
386  */
387 int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx)
388 {
389 	bool is_stale = false;
390 
391 	if (!READ_ONCE(ctx->napi_busy_poll_dt))
392 		return 0;
393 	if (list_empty_careful(&ctx->napi_list))
394 		return 0;
395 
396 	scoped_guard(rcu) {
397 		is_stale = __io_napi_do_busy_loop(ctx, NULL, NULL);
398 	}
399 
400 	io_napi_remove_stale(ctx, is_stale);
401 	return 1;
402 }
403 
404 #endif
405