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