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