Commit | Line | Data |
---|---|---|
329061d3 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include <linux/kernel.h> | |
3 | #include <linux/errno.h> | |
4 | #include <linux/fs.h> | |
5 | #include <linux/file.h> | |
6 | #include <linux/mm.h> | |
7 | #include <linux/slab.h> | |
8 | #include <linux/poll.h> | |
9 | #include <linux/hashtable.h> | |
10 | #include <linux/io_uring.h> | |
11 | ||
12 | #include <trace/events/io_uring.h> | |
13 | ||
14 | #include <uapi/linux/io_uring.h> | |
15 | ||
16 | #include "io_uring_types.h" | |
17 | #include "io_uring.h" | |
18 | #include "refs.h" | |
19 | #include "opdef.h" | |
3b77495a | 20 | #include "kbuf.h" |
329061d3 | 21 | #include "poll.h" |
38513c46 | 22 | #include "cancel.h" |
329061d3 JA |
23 | |
24 | struct io_poll_update { | |
25 | struct file *file; | |
26 | u64 old_user_data; | |
27 | u64 new_user_data; | |
28 | __poll_t events; | |
29 | bool update_events; | |
30 | bool update_user_data; | |
31 | }; | |
32 | ||
33 | struct io_poll_table { | |
34 | struct poll_table_struct pt; | |
35 | struct io_kiocb *req; | |
36 | int nr_entries; | |
37 | int error; | |
38 | }; | |
39 | ||
40 | #define IO_POLL_CANCEL_FLAG BIT(31) | |
41 | #define IO_POLL_REF_MASK GENMASK(30, 0) | |
42 | ||
43 | /* | |
44 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
45 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
46 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
47 | * arming poll and wakeups. | |
48 | */ | |
49 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
50 | { | |
51 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
52 | } | |
53 | ||
54 | static void io_poll_mark_cancelled(struct io_kiocb *req) | |
55 | { | |
56 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); | |
57 | } | |
58 | ||
59 | static struct io_poll *io_poll_get_double(struct io_kiocb *req) | |
60 | { | |
61 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ | |
62 | if (req->opcode == IORING_OP_POLL_ADD) | |
63 | return req->async_data; | |
64 | return req->apoll->double_poll; | |
65 | } | |
66 | ||
67 | static struct io_poll *io_poll_get_single(struct io_kiocb *req) | |
68 | { | |
69 | if (req->opcode == IORING_OP_POLL_ADD) | |
70 | return io_kiocb_to_cmd(req); | |
71 | return &req->apoll->poll; | |
72 | } | |
73 | ||
74 | static void io_poll_req_insert(struct io_kiocb *req) | |
75 | { | |
e6f89be6 PB |
76 | struct io_hash_table *table = &req->ctx->cancel_table; |
77 | u32 index = hash_long(req->cqe.user_data, table->hash_bits); | |
78 | struct io_hash_bucket *hb = &table->hbs[index]; | |
329061d3 | 79 | |
38513c46 HX |
80 | spin_lock(&hb->lock); |
81 | hlist_add_head(&req->hash_node, &hb->list); | |
82 | spin_unlock(&hb->lock); | |
83 | } | |
84 | ||
85 | static void io_poll_req_delete(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
86 | { | |
e6f89be6 PB |
87 | struct io_hash_table *table = &req->ctx->cancel_table; |
88 | u32 index = hash_long(req->cqe.user_data, table->hash_bits); | |
89 | spinlock_t *lock = &table->hbs[index].lock; | |
38513c46 HX |
90 | |
91 | spin_lock(lock); | |
92 | hash_del(&req->hash_node); | |
93 | spin_unlock(lock); | |
329061d3 JA |
94 | } |
95 | ||
9ca9fb24 PB |
96 | static void io_poll_req_insert_locked(struct io_kiocb *req) |
97 | { | |
98 | struct io_hash_table *table = &req->ctx->cancel_table_locked; | |
99 | u32 index = hash_long(req->cqe.user_data, table->hash_bits); | |
100 | ||
101 | hlist_add_head(&req->hash_node, &table->hbs[index].list); | |
102 | } | |
103 | ||
104 | static void io_poll_tw_hash_eject(struct io_kiocb *req, bool *locked) | |
105 | { | |
106 | struct io_ring_ctx *ctx = req->ctx; | |
107 | ||
108 | if (req->flags & REQ_F_HASH_LOCKED) { | |
109 | /* | |
110 | * ->cancel_table_locked is protected by ->uring_lock in | |
111 | * contrast to per bucket spinlocks. Likely, tctx_task_work() | |
112 | * already grabbed the mutex for us, but there is a chance it | |
113 | * failed. | |
114 | */ | |
115 | io_tw_lock(ctx, locked); | |
116 | hash_del(&req->hash_node); | |
117 | } else { | |
118 | io_poll_req_delete(req, ctx); | |
119 | } | |
120 | } | |
121 | ||
329061d3 JA |
122 | static void io_init_poll_iocb(struct io_poll *poll, __poll_t events, |
123 | wait_queue_func_t wake_func) | |
124 | { | |
125 | poll->head = NULL; | |
126 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) | |
127 | /* mask in events that we always want/need */ | |
128 | poll->events = events | IO_POLL_UNMASK; | |
129 | INIT_LIST_HEAD(&poll->wait.entry); | |
130 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
131 | } | |
132 | ||
133 | static inline void io_poll_remove_entry(struct io_poll *poll) | |
134 | { | |
135 | struct wait_queue_head *head = smp_load_acquire(&poll->head); | |
136 | ||
137 | if (head) { | |
138 | spin_lock_irq(&head->lock); | |
139 | list_del_init(&poll->wait.entry); | |
140 | poll->head = NULL; | |
141 | spin_unlock_irq(&head->lock); | |
142 | } | |
143 | } | |
144 | ||
145 | static void io_poll_remove_entries(struct io_kiocb *req) | |
146 | { | |
147 | /* | |
148 | * Nothing to do if neither of those flags are set. Avoid dipping | |
149 | * into the poll/apoll/double cachelines if we can. | |
150 | */ | |
151 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
152 | return; | |
153 | ||
154 | /* | |
155 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
156 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
157 | * lock in the first place can race with the waitqueue being freed. | |
158 | * | |
159 | * We solve this as eventpoll does: by taking advantage of the fact that | |
160 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
161 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
162 | * non-NULL, we can then lock it without the memory being freed out from | |
163 | * under us. | |
164 | * | |
165 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
166 | * case the caller deletes the entry from the queue, leaving it empty. | |
167 | * In that case, only RCU prevents the queue memory from being freed. | |
168 | */ | |
169 | rcu_read_lock(); | |
170 | if (req->flags & REQ_F_SINGLE_POLL) | |
171 | io_poll_remove_entry(io_poll_get_single(req)); | |
172 | if (req->flags & REQ_F_DOUBLE_POLL) | |
173 | io_poll_remove_entry(io_poll_get_double(req)); | |
174 | rcu_read_unlock(); | |
175 | } | |
176 | ||
177 | /* | |
178 | * All poll tw should go through this. Checks for poll events, manages | |
179 | * references, does rewait, etc. | |
180 | * | |
181 | * Returns a negative error on failure. >0 when no action require, which is | |
182 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
183 | * the request, then the mask is stored in req->cqe.res. | |
184 | */ | |
185 | static int io_poll_check_events(struct io_kiocb *req, bool *locked) | |
186 | { | |
187 | struct io_ring_ctx *ctx = req->ctx; | |
188 | int v, ret; | |
189 | ||
190 | /* req->task == current here, checking PF_EXITING is safe */ | |
191 | if (unlikely(req->task->flags & PF_EXITING)) | |
192 | return -ECANCELED; | |
193 | ||
194 | do { | |
195 | v = atomic_read(&req->poll_refs); | |
196 | ||
197 | /* tw handler should be the owner, and so have some references */ | |
198 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
199 | return 0; | |
200 | if (v & IO_POLL_CANCEL_FLAG) | |
201 | return -ECANCELED; | |
202 | ||
203 | if (!req->cqe.res) { | |
204 | struct poll_table_struct pt = { ._key = req->apoll_events }; | |
205 | req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events; | |
206 | } | |
207 | ||
208 | if ((unlikely(!req->cqe.res))) | |
209 | continue; | |
210 | if (req->apoll_events & EPOLLONESHOT) | |
211 | return 0; | |
212 | ||
213 | /* multishot, just fill a CQE and proceed */ | |
214 | if (!(req->flags & REQ_F_APOLL_MULTISHOT)) { | |
215 | __poll_t mask = mangle_poll(req->cqe.res & | |
216 | req->apoll_events); | |
217 | bool filled; | |
218 | ||
219 | spin_lock(&ctx->completion_lock); | |
220 | filled = io_fill_cqe_aux(ctx, req->cqe.user_data, | |
221 | mask, IORING_CQE_F_MORE); | |
222 | io_commit_cqring(ctx); | |
223 | spin_unlock(&ctx->completion_lock); | |
224 | if (filled) { | |
225 | io_cqring_ev_posted(ctx); | |
226 | continue; | |
227 | } | |
228 | return -ECANCELED; | |
229 | } | |
230 | ||
231 | ret = io_poll_issue(req, locked); | |
232 | if (ret) | |
233 | return ret; | |
234 | ||
235 | /* | |
236 | * Release all references, retry if someone tried to restart | |
237 | * task_work while we were executing it. | |
238 | */ | |
239 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
240 | ||
241 | return 1; | |
242 | } | |
243 | ||
244 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) | |
245 | { | |
329061d3 JA |
246 | int ret; |
247 | ||
248 | ret = io_poll_check_events(req, locked); | |
249 | if (ret > 0) | |
250 | return; | |
251 | ||
252 | if (!ret) { | |
253 | struct io_poll *poll = io_kiocb_to_cmd(req); | |
254 | ||
255 | req->cqe.res = mangle_poll(req->cqe.res & poll->events); | |
256 | } else { | |
257 | req->cqe.res = ret; | |
258 | req_set_fail(req); | |
259 | } | |
260 | ||
261 | io_poll_remove_entries(req); | |
9ca9fb24 PB |
262 | io_poll_tw_hash_eject(req, locked); |
263 | ||
0ec6dca2 PB |
264 | io_req_set_res(req, req->cqe.res, 0); |
265 | io_req_task_complete(req, locked); | |
329061d3 JA |
266 | } |
267 | ||
268 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) | |
269 | { | |
329061d3 JA |
270 | int ret; |
271 | ||
272 | ret = io_poll_check_events(req, locked); | |
273 | if (ret > 0) | |
274 | return; | |
275 | ||
276 | io_poll_remove_entries(req); | |
9ca9fb24 | 277 | io_poll_tw_hash_eject(req, locked); |
329061d3 JA |
278 | |
279 | if (!ret) | |
280 | io_req_task_submit(req, locked); | |
281 | else | |
282 | io_req_complete_failed(req, ret); | |
283 | } | |
284 | ||
285 | static void __io_poll_execute(struct io_kiocb *req, int mask, | |
286 | __poll_t __maybe_unused events) | |
287 | { | |
288 | io_req_set_res(req, mask, 0); | |
289 | /* | |
290 | * This is useful for poll that is armed on behalf of another | |
291 | * request, and where the wakeup path could be on a different | |
292 | * CPU. We want to avoid pulling in req->apoll->events for that | |
293 | * case. | |
294 | */ | |
295 | if (req->opcode == IORING_OP_POLL_ADD) | |
296 | req->io_task_work.func = io_poll_task_func; | |
297 | else | |
298 | req->io_task_work.func = io_apoll_task_func; | |
299 | ||
300 | trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask); | |
301 | io_req_task_work_add(req); | |
302 | } | |
303 | ||
304 | static inline void io_poll_execute(struct io_kiocb *req, int res, | |
305 | __poll_t events) | |
306 | { | |
307 | if (io_poll_get_ownership(req)) | |
308 | __io_poll_execute(req, res, events); | |
309 | } | |
310 | ||
311 | static void io_poll_cancel_req(struct io_kiocb *req) | |
312 | { | |
313 | io_poll_mark_cancelled(req); | |
314 | /* kick tw, which should complete the request */ | |
315 | io_poll_execute(req, 0, 0); | |
316 | } | |
317 | ||
318 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) | |
319 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
320 | #define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI) | |
321 | ||
322 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
323 | void *key) | |
324 | { | |
325 | struct io_kiocb *req = wqe_to_req(wait); | |
326 | struct io_poll *poll = container_of(wait, struct io_poll, wait); | |
327 | __poll_t mask = key_to_poll(key); | |
328 | ||
329 | if (unlikely(mask & POLLFREE)) { | |
330 | io_poll_mark_cancelled(req); | |
331 | /* we have to kick tw in case it's not already */ | |
332 | io_poll_execute(req, 0, poll->events); | |
333 | ||
334 | /* | |
335 | * If the waitqueue is being freed early but someone is already | |
336 | * holds ownership over it, we have to tear down the request as | |
337 | * best we can. That means immediately removing the request from | |
338 | * its waitqueue and preventing all further accesses to the | |
339 | * waitqueue via the request. | |
340 | */ | |
341 | list_del_init(&poll->wait.entry); | |
342 | ||
343 | /* | |
344 | * Careful: this *must* be the last step, since as soon | |
345 | * as req->head is NULL'ed out, the request can be | |
346 | * completed and freed, since aio_poll_complete_work() | |
347 | * will no longer need to take the waitqueue lock. | |
348 | */ | |
349 | smp_store_release(&poll->head, NULL); | |
350 | return 1; | |
351 | } | |
352 | ||
353 | /* for instances that support it check for an event match first */ | |
354 | if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON))) | |
355 | return 0; | |
356 | ||
357 | if (io_poll_get_ownership(req)) { | |
358 | /* optional, saves extra locking for removal in tw handler */ | |
359 | if (mask && poll->events & EPOLLONESHOT) { | |
360 | list_del_init(&poll->wait.entry); | |
361 | poll->head = NULL; | |
362 | if (wqe_is_double(wait)) | |
363 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
364 | else | |
365 | req->flags &= ~REQ_F_SINGLE_POLL; | |
366 | } | |
367 | __io_poll_execute(req, mask, poll->events); | |
368 | } | |
369 | return 1; | |
370 | } | |
371 | ||
372 | static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt, | |
373 | struct wait_queue_head *head, | |
374 | struct io_poll **poll_ptr) | |
375 | { | |
376 | struct io_kiocb *req = pt->req; | |
377 | unsigned long wqe_private = (unsigned long) req; | |
378 | ||
379 | /* | |
380 | * The file being polled uses multiple waitqueues for poll handling | |
381 | * (e.g. one for read, one for write). Setup a separate io_poll | |
382 | * if this happens. | |
383 | */ | |
384 | if (unlikely(pt->nr_entries)) { | |
385 | struct io_poll *first = poll; | |
386 | ||
387 | /* double add on the same waitqueue head, ignore */ | |
388 | if (first->head == head) | |
389 | return; | |
390 | /* already have a 2nd entry, fail a third attempt */ | |
391 | if (*poll_ptr) { | |
392 | if ((*poll_ptr)->head == head) | |
393 | return; | |
394 | pt->error = -EINVAL; | |
395 | return; | |
396 | } | |
397 | ||
398 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
399 | if (!poll) { | |
400 | pt->error = -ENOMEM; | |
401 | return; | |
402 | } | |
403 | /* mark as double wq entry */ | |
404 | wqe_private |= 1; | |
405 | req->flags |= REQ_F_DOUBLE_POLL; | |
406 | io_init_poll_iocb(poll, first->events, first->wait.func); | |
407 | *poll_ptr = poll; | |
408 | if (req->opcode == IORING_OP_POLL_ADD) | |
409 | req->flags |= REQ_F_ASYNC_DATA; | |
410 | } | |
411 | ||
412 | req->flags |= REQ_F_SINGLE_POLL; | |
413 | pt->nr_entries++; | |
414 | poll->head = head; | |
415 | poll->wait.private = (void *) wqe_private; | |
416 | ||
417 | if (poll->events & EPOLLEXCLUSIVE) | |
418 | add_wait_queue_exclusive(head, &poll->wait); | |
419 | else | |
420 | add_wait_queue(head, &poll->wait); | |
421 | } | |
422 | ||
423 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, | |
424 | struct poll_table_struct *p) | |
425 | { | |
426 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
427 | struct io_poll *poll = io_kiocb_to_cmd(pt->req); | |
428 | ||
429 | __io_queue_proc(poll, pt, head, | |
430 | (struct io_poll **) &pt->req->async_data); | |
431 | } | |
432 | ||
433 | static int __io_arm_poll_handler(struct io_kiocb *req, | |
434 | struct io_poll *poll, | |
435 | struct io_poll_table *ipt, __poll_t mask) | |
436 | { | |
437 | struct io_ring_ctx *ctx = req->ctx; | |
438 | int v; | |
439 | ||
440 | INIT_HLIST_NODE(&req->hash_node); | |
441 | req->work.cancel_seq = atomic_read(&ctx->cancel_seq); | |
442 | io_init_poll_iocb(poll, mask, io_poll_wake); | |
443 | poll->file = req->file; | |
444 | ||
445 | req->apoll_events = poll->events; | |
446 | ||
447 | ipt->pt._key = mask; | |
448 | ipt->req = req; | |
449 | ipt->error = 0; | |
450 | ipt->nr_entries = 0; | |
451 | ||
452 | /* | |
453 | * Take the ownership to delay any tw execution up until we're done | |
454 | * with poll arming. see io_poll_get_ownership(). | |
455 | */ | |
456 | atomic_set(&req->poll_refs, 1); | |
457 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; | |
458 | ||
b9ba8a44 JA |
459 | if (mask && |
460 | ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) { | |
329061d3 JA |
461 | io_poll_remove_entries(req); |
462 | /* no one else has access to the req, forget about the ref */ | |
463 | return mask; | |
464 | } | |
b9ba8a44 | 465 | |
329061d3 JA |
466 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { |
467 | io_poll_remove_entries(req); | |
468 | if (!ipt->error) | |
469 | ipt->error = -EINVAL; | |
470 | return 0; | |
471 | } | |
472 | ||
9ca9fb24 PB |
473 | if (req->flags & REQ_F_HASH_LOCKED) |
474 | io_poll_req_insert_locked(req); | |
475 | else | |
476 | io_poll_req_insert(req); | |
329061d3 | 477 | |
b9ba8a44 | 478 | if (mask && (poll->events & EPOLLET)) { |
329061d3 JA |
479 | /* can't multishot if failed, just queue the event we've got */ |
480 | if (unlikely(ipt->error || !ipt->nr_entries)) { | |
481 | poll->events |= EPOLLONESHOT; | |
482 | req->apoll_events |= EPOLLONESHOT; | |
483 | ipt->error = 0; | |
484 | } | |
485 | __io_poll_execute(req, mask, poll->events); | |
486 | return 0; | |
487 | } | |
488 | ||
489 | /* | |
490 | * Release ownership. If someone tried to queue a tw while it was | |
491 | * locked, kick it off for them. | |
492 | */ | |
493 | v = atomic_dec_return(&req->poll_refs); | |
494 | if (unlikely(v & IO_POLL_REF_MASK)) | |
495 | __io_poll_execute(req, 0, poll->events); | |
496 | return 0; | |
497 | } | |
498 | ||
499 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
500 | struct poll_table_struct *p) | |
501 | { | |
502 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
503 | struct async_poll *apoll = pt->req->apoll; | |
504 | ||
505 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
506 | } | |
507 | ||
508 | int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) | |
509 | { | |
510 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
511 | struct io_ring_ctx *ctx = req->ctx; | |
512 | struct async_poll *apoll; | |
513 | struct io_poll_table ipt; | |
b9ba8a44 | 514 | __poll_t mask = POLLPRI | POLLERR | EPOLLET; |
329061d3 JA |
515 | int ret; |
516 | ||
9ca9fb24 PB |
517 | /* |
518 | * apoll requests already grab the mutex to complete in the tw handler, | |
519 | * so removal from the mutex-backed hash is free, use it by default. | |
520 | */ | |
521 | if (issue_flags & IO_URING_F_UNLOCKED) | |
522 | req->flags &= ~REQ_F_HASH_LOCKED; | |
523 | else | |
524 | req->flags |= REQ_F_HASH_LOCKED; | |
525 | ||
329061d3 JA |
526 | if (!def->pollin && !def->pollout) |
527 | return IO_APOLL_ABORTED; | |
528 | if (!file_can_poll(req->file)) | |
529 | return IO_APOLL_ABORTED; | |
530 | if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED) | |
531 | return IO_APOLL_ABORTED; | |
532 | if (!(req->flags & REQ_F_APOLL_MULTISHOT)) | |
533 | mask |= EPOLLONESHOT; | |
534 | ||
535 | if (def->pollin) { | |
536 | mask |= EPOLLIN | EPOLLRDNORM; | |
537 | ||
538 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
539 | if (req->flags & REQ_F_CLEAR_POLLIN) | |
540 | mask &= ~EPOLLIN; | |
541 | } else { | |
542 | mask |= EPOLLOUT | EPOLLWRNORM; | |
543 | } | |
544 | if (def->poll_exclusive) | |
545 | mask |= EPOLLEXCLUSIVE; | |
546 | if (req->flags & REQ_F_POLLED) { | |
547 | apoll = req->apoll; | |
548 | kfree(apoll->double_poll); | |
549 | } else if (!(issue_flags & IO_URING_F_UNLOCKED) && | |
550 | !list_empty(&ctx->apoll_cache)) { | |
551 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
552 | poll.wait.entry); | |
553 | list_del_init(&apoll->poll.wait.entry); | |
554 | } else { | |
555 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
556 | if (unlikely(!apoll)) | |
557 | return IO_APOLL_ABORTED; | |
558 | } | |
559 | apoll->double_poll = NULL; | |
560 | req->apoll = apoll; | |
561 | req->flags |= REQ_F_POLLED; | |
562 | ipt.pt._qproc = io_async_queue_proc; | |
563 | ||
564 | io_kbuf_recycle(req, issue_flags); | |
565 | ||
566 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); | |
567 | if (ret || ipt.error) | |
568 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
569 | ||
570 | trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode, | |
571 | mask, apoll->poll.events); | |
572 | return IO_APOLL_OK; | |
573 | } | |
574 | ||
9ca9fb24 PB |
575 | static __cold bool io_poll_remove_all_table(struct task_struct *tsk, |
576 | struct io_hash_table *table, | |
577 | bool cancel_all) | |
329061d3 | 578 | { |
e6f89be6 | 579 | unsigned nr_buckets = 1U << table->hash_bits; |
329061d3 JA |
580 | struct hlist_node *tmp; |
581 | struct io_kiocb *req; | |
582 | bool found = false; | |
583 | int i; | |
584 | ||
e6f89be6 PB |
585 | for (i = 0; i < nr_buckets; i++) { |
586 | struct io_hash_bucket *hb = &table->hbs[i]; | |
329061d3 | 587 | |
38513c46 HX |
588 | spin_lock(&hb->lock); |
589 | hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) { | |
329061d3 JA |
590 | if (io_match_task_safe(req, tsk, cancel_all)) { |
591 | hlist_del_init(&req->hash_node); | |
592 | io_poll_cancel_req(req); | |
593 | found = true; | |
594 | } | |
595 | } | |
38513c46 | 596 | spin_unlock(&hb->lock); |
329061d3 | 597 | } |
329061d3 JA |
598 | return found; |
599 | } | |
600 | ||
9ca9fb24 PB |
601 | /* |
602 | * Returns true if we found and killed one or more poll requests | |
603 | */ | |
604 | __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
605 | bool cancel_all) | |
606 | __must_hold(&ctx->uring_lock) | |
607 | { | |
608 | return io_poll_remove_all_table(tsk, &ctx->cancel_table, cancel_all) | | |
609 | io_poll_remove_all_table(tsk, &ctx->cancel_table_locked, cancel_all); | |
610 | } | |
611 | ||
329061d3 | 612 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only, |
1ab1edb0 | 613 | struct io_cancel_data *cd, |
e6f89be6 | 614 | struct io_hash_table *table, |
1ab1edb0 | 615 | struct io_hash_bucket **out_bucket) |
329061d3 | 616 | { |
329061d3 | 617 | struct io_kiocb *req; |
e6f89be6 PB |
618 | u32 index = hash_long(cd->data, table->hash_bits); |
619 | struct io_hash_bucket *hb = &table->hbs[index]; | |
329061d3 | 620 | |
1ab1edb0 PB |
621 | *out_bucket = NULL; |
622 | ||
38513c46 HX |
623 | spin_lock(&hb->lock); |
624 | hlist_for_each_entry(req, &hb->list, hash_node) { | |
329061d3 JA |
625 | if (cd->data != req->cqe.user_data) |
626 | continue; | |
627 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) | |
628 | continue; | |
629 | if (cd->flags & IORING_ASYNC_CANCEL_ALL) { | |
630 | if (cd->seq == req->work.cancel_seq) | |
631 | continue; | |
632 | req->work.cancel_seq = cd->seq; | |
633 | } | |
1ab1edb0 | 634 | *out_bucket = hb; |
329061d3 JA |
635 | return req; |
636 | } | |
38513c46 | 637 | spin_unlock(&hb->lock); |
329061d3 JA |
638 | return NULL; |
639 | } | |
640 | ||
641 | static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx, | |
1ab1edb0 | 642 | struct io_cancel_data *cd, |
e6f89be6 | 643 | struct io_hash_table *table, |
1ab1edb0 | 644 | struct io_hash_bucket **out_bucket) |
329061d3 | 645 | { |
e6f89be6 | 646 | unsigned nr_buckets = 1U << table->hash_bits; |
329061d3 JA |
647 | struct io_kiocb *req; |
648 | int i; | |
649 | ||
1ab1edb0 PB |
650 | *out_bucket = NULL; |
651 | ||
e6f89be6 PB |
652 | for (i = 0; i < nr_buckets; i++) { |
653 | struct io_hash_bucket *hb = &table->hbs[i]; | |
329061d3 | 654 | |
38513c46 HX |
655 | spin_lock(&hb->lock); |
656 | hlist_for_each_entry(req, &hb->list, hash_node) { | |
329061d3 JA |
657 | if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) && |
658 | req->file != cd->file) | |
659 | continue; | |
660 | if (cd->seq == req->work.cancel_seq) | |
661 | continue; | |
662 | req->work.cancel_seq = cd->seq; | |
1ab1edb0 | 663 | *out_bucket = hb; |
329061d3 JA |
664 | return req; |
665 | } | |
38513c46 | 666 | spin_unlock(&hb->lock); |
329061d3 JA |
667 | } |
668 | return NULL; | |
669 | } | |
670 | ||
9ca9fb24 | 671 | static int io_poll_disarm(struct io_kiocb *req) |
329061d3 | 672 | { |
9ca9fb24 PB |
673 | if (!req) |
674 | return -ENOENT; | |
329061d3 | 675 | if (!io_poll_get_ownership(req)) |
9ca9fb24 | 676 | return -EALREADY; |
329061d3 JA |
677 | io_poll_remove_entries(req); |
678 | hash_del(&req->hash_node); | |
9ca9fb24 | 679 | return 0; |
329061d3 JA |
680 | } |
681 | ||
a2cdd519 | 682 | static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, |
e6f89be6 | 683 | struct io_hash_table *table) |
329061d3 | 684 | { |
1ab1edb0 | 685 | struct io_hash_bucket *bucket; |
329061d3 JA |
686 | struct io_kiocb *req; |
687 | ||
688 | if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY)) | |
e6f89be6 | 689 | req = io_poll_file_find(ctx, cd, table, &bucket); |
329061d3 | 690 | else |
e6f89be6 | 691 | req = io_poll_find(ctx, false, cd, table, &bucket); |
1ab1edb0 PB |
692 | |
693 | if (req) | |
694 | io_poll_cancel_req(req); | |
695 | if (bucket) | |
696 | spin_unlock(&bucket->lock); | |
697 | return req ? 0 : -ENOENT; | |
329061d3 JA |
698 | } |
699 | ||
5d7943d9 PB |
700 | int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, |
701 | unsigned issue_flags) | |
a2cdd519 | 702 | { |
9ca9fb24 PB |
703 | int ret; |
704 | ||
705 | ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table); | |
706 | if (ret != -ENOENT) | |
707 | return ret; | |
708 | ||
709 | io_ring_submit_lock(ctx, issue_flags); | |
710 | ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table_locked); | |
711 | io_ring_submit_unlock(ctx, issue_flags); | |
712 | return ret; | |
a2cdd519 PB |
713 | } |
714 | ||
329061d3 JA |
715 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
716 | unsigned int flags) | |
717 | { | |
718 | u32 events; | |
719 | ||
720 | events = READ_ONCE(sqe->poll32_events); | |
721 | #ifdef __BIG_ENDIAN | |
722 | events = swahw32(events); | |
723 | #endif | |
724 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
725 | events |= EPOLLONESHOT; | |
b9ba8a44 JA |
726 | if (!(flags & IORING_POLL_ADD_LEVEL)) |
727 | events |= EPOLLET; | |
728 | return demangle_poll(events) | | |
729 | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET)); | |
329061d3 JA |
730 | } |
731 | ||
732 | int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
733 | { | |
734 | struct io_poll_update *upd = io_kiocb_to_cmd(req); | |
735 | u32 flags; | |
736 | ||
737 | if (sqe->buf_index || sqe->splice_fd_in) | |
738 | return -EINVAL; | |
739 | flags = READ_ONCE(sqe->len); | |
740 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
741 | IORING_POLL_ADD_MULTI)) | |
742 | return -EINVAL; | |
743 | /* meaningless without update */ | |
744 | if (flags == IORING_POLL_ADD_MULTI) | |
745 | return -EINVAL; | |
746 | ||
747 | upd->old_user_data = READ_ONCE(sqe->addr); | |
748 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
749 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
750 | ||
751 | upd->new_user_data = READ_ONCE(sqe->off); | |
752 | if (!upd->update_user_data && upd->new_user_data) | |
753 | return -EINVAL; | |
754 | if (upd->update_events) | |
755 | upd->events = io_poll_parse_events(sqe, flags); | |
756 | else if (sqe->poll32_events) | |
757 | return -EINVAL; | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
762 | int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
763 | { | |
764 | struct io_poll *poll = io_kiocb_to_cmd(req); | |
765 | u32 flags; | |
766 | ||
767 | if (sqe->buf_index || sqe->off || sqe->addr) | |
768 | return -EINVAL; | |
769 | flags = READ_ONCE(sqe->len); | |
b9ba8a44 | 770 | if (flags & ~(IORING_POLL_ADD_MULTI|IORING_POLL_ADD_LEVEL)) |
329061d3 JA |
771 | return -EINVAL; |
772 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) | |
773 | return -EINVAL; | |
774 | ||
329061d3 JA |
775 | poll->events = io_poll_parse_events(sqe, flags); |
776 | return 0; | |
777 | } | |
778 | ||
779 | int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) | |
780 | { | |
781 | struct io_poll *poll = io_kiocb_to_cmd(req); | |
782 | struct io_poll_table ipt; | |
783 | int ret; | |
784 | ||
785 | ipt.pt._qproc = io_poll_queue_proc; | |
786 | ||
9ca9fb24 PB |
787 | /* |
788 | * If sqpoll or single issuer, there is no contention for ->uring_lock | |
789 | * and we'll end up holding it in tw handlers anyway. | |
790 | */ | |
791 | if (!(issue_flags & IO_URING_F_UNLOCKED) && | |
792 | (req->ctx->flags & (IORING_SETUP_SQPOLL | IORING_SETUP_SINGLE_ISSUER))) | |
793 | req->flags |= REQ_F_HASH_LOCKED; | |
794 | else | |
795 | req->flags &= ~REQ_F_HASH_LOCKED; | |
796 | ||
329061d3 JA |
797 | ret = __io_arm_poll_handler(req, poll, &ipt, poll->events); |
798 | if (ret) { | |
799 | io_req_set_res(req, ret, 0); | |
800 | return IOU_OK; | |
801 | } | |
802 | if (ipt.error) { | |
803 | req_set_fail(req); | |
804 | return ipt.error; | |
805 | } | |
806 | ||
807 | return IOU_ISSUE_SKIP_COMPLETE; | |
808 | } | |
809 | ||
810 | int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) | |
811 | { | |
812 | struct io_poll_update *poll_update = io_kiocb_to_cmd(req); | |
813 | struct io_cancel_data cd = { .data = poll_update->old_user_data, }; | |
814 | struct io_ring_ctx *ctx = req->ctx; | |
1ab1edb0 | 815 | struct io_hash_bucket *bucket; |
329061d3 JA |
816 | struct io_kiocb *preq; |
817 | int ret2, ret = 0; | |
818 | bool locked; | |
819 | ||
e6f89be6 | 820 | preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table, &bucket); |
9ca9fb24 | 821 | ret2 = io_poll_disarm(preq); |
1ab1edb0 PB |
822 | if (bucket) |
823 | spin_unlock(&bucket->lock); | |
9ca9fb24 PB |
824 | if (!ret2) |
825 | goto found; | |
826 | if (ret2 != -ENOENT) { | |
827 | ret = ret2; | |
38513c46 HX |
828 | goto out; |
829 | } | |
9ca9fb24 PB |
830 | |
831 | io_ring_submit_lock(ctx, issue_flags); | |
832 | preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table_locked, &bucket); | |
833 | ret2 = io_poll_disarm(preq); | |
834 | if (bucket) | |
835 | spin_unlock(&bucket->lock); | |
836 | io_ring_submit_unlock(ctx, issue_flags); | |
837 | if (ret2) { | |
838 | ret = ret2; | |
329061d3 JA |
839 | goto out; |
840 | } | |
329061d3 | 841 | |
9ca9fb24 | 842 | found: |
329061d3 JA |
843 | if (poll_update->update_events || poll_update->update_user_data) { |
844 | /* only mask one event flags, keep behavior flags */ | |
845 | if (poll_update->update_events) { | |
846 | struct io_poll *poll = io_kiocb_to_cmd(preq); | |
847 | ||
848 | poll->events &= ~0xffff; | |
849 | poll->events |= poll_update->events & 0xffff; | |
850 | poll->events |= IO_POLL_UNMASK; | |
851 | } | |
852 | if (poll_update->update_user_data) | |
853 | preq->cqe.user_data = poll_update->new_user_data; | |
854 | ||
855 | ret2 = io_poll_add(preq, issue_flags); | |
856 | /* successfully updated, don't complete poll request */ | |
857 | if (!ret2 || ret2 == -EIOCBQUEUED) | |
858 | goto out; | |
859 | } | |
860 | ||
861 | req_set_fail(preq); | |
862 | io_req_set_res(preq, -ECANCELED, 0); | |
863 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
864 | io_req_task_complete(preq, &locked); | |
865 | out: | |
866 | if (ret < 0) { | |
867 | req_set_fail(req); | |
868 | return ret; | |
869 | } | |
870 | /* complete update request, we're done with it */ | |
871 | io_req_set_res(req, ret, 0); | |
872 | return IOU_OK; | |
873 | } |