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