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