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