io_uring: split provided buffers handling into its own file
[linux-2.6-microblaze.git] / io_uring / timeout.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/file.h>
5 #include <linux/io_uring.h>
6
7 #include <trace/events/io_uring.h>
8
9 #include <uapi/linux/io_uring.h>
10
11 #include "io_uring_types.h"
12 #include "io_uring.h"
13 #include "refs.h"
14 #include "cancel.h"
15 #include "timeout.h"
16
17 struct io_timeout {
18         struct file                     *file;
19         u32                             off;
20         u32                             target_seq;
21         struct list_head                list;
22         /* head of the link, used by linked timeouts only */
23         struct io_kiocb                 *head;
24         /* for linked completions */
25         struct io_kiocb                 *prev;
26 };
27
28 struct io_timeout_rem {
29         struct file                     *file;
30         u64                             addr;
31
32         /* timeout update */
33         struct timespec64               ts;
34         u32                             flags;
35         bool                            ltimeout;
36 };
37
38 static inline bool io_is_timeout_noseq(struct io_kiocb *req)
39 {
40         struct io_timeout *timeout = io_kiocb_to_cmd(req);
41
42         return !timeout->off;
43 }
44
45 static inline void io_put_req(struct io_kiocb *req)
46 {
47         if (req_ref_put_and_test(req)) {
48                 io_queue_next(req);
49                 io_free_req(req);
50         }
51 }
52
53 static void io_kill_timeout(struct io_kiocb *req, int status)
54         __must_hold(&req->ctx->completion_lock)
55         __must_hold(&req->ctx->timeout_lock)
56 {
57         struct io_timeout_data *io = req->async_data;
58
59         if (hrtimer_try_to_cancel(&io->timer) != -1) {
60                 struct io_timeout *timeout = io_kiocb_to_cmd(req);
61
62                 if (status)
63                         req_set_fail(req);
64                 atomic_set(&req->ctx->cq_timeouts,
65                         atomic_read(&req->ctx->cq_timeouts) + 1);
66                 list_del_init(&timeout->list);
67                 io_req_tw_post_queue(req, status, 0);
68         }
69 }
70
71 __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
72         __must_hold(&ctx->completion_lock)
73 {
74         u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
75         struct io_timeout *timeout, *tmp;
76
77         spin_lock_irq(&ctx->timeout_lock);
78         list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
79                 struct io_kiocb *req = cmd_to_io_kiocb(timeout);
80                 u32 events_needed, events_got;
81
82                 if (io_is_timeout_noseq(req))
83                         break;
84
85                 /*
86                  * Since seq can easily wrap around over time, subtract
87                  * the last seq at which timeouts were flushed before comparing.
88                  * Assuming not more than 2^31-1 events have happened since,
89                  * these subtractions won't have wrapped, so we can check if
90                  * target is in [last_seq, current_seq] by comparing the two.
91                  */
92                 events_needed = timeout->target_seq - ctx->cq_last_tm_flush;
93                 events_got = seq - ctx->cq_last_tm_flush;
94                 if (events_got < events_needed)
95                         break;
96
97                 io_kill_timeout(req, 0);
98         }
99         ctx->cq_last_tm_flush = seq;
100         spin_unlock_irq(&ctx->timeout_lock);
101 }
102
103 static void io_fail_links(struct io_kiocb *req)
104         __must_hold(&req->ctx->completion_lock)
105 {
106         struct io_kiocb *nxt, *link = req->link;
107         bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
108
109         req->link = NULL;
110         while (link) {
111                 long res = -ECANCELED;
112
113                 if (link->flags & REQ_F_FAIL)
114                         res = link->cqe.res;
115
116                 nxt = link->link;
117                 link->link = NULL;
118
119                 trace_io_uring_fail_link(req->ctx, req, req->cqe.user_data,
120                                         req->opcode, link);
121
122                 if (ignore_cqes)
123                         link->flags |= REQ_F_CQE_SKIP;
124                 else
125                         link->flags &= ~REQ_F_CQE_SKIP;
126                 io_req_set_res(link, res, 0);
127                 __io_req_complete_post(link);
128                 link = nxt;
129         }
130 }
131
132 static inline void io_remove_next_linked(struct io_kiocb *req)
133 {
134         struct io_kiocb *nxt = req->link;
135
136         req->link = nxt->link;
137         nxt->link = NULL;
138 }
139
140 bool io_disarm_next(struct io_kiocb *req)
141         __must_hold(&req->ctx->completion_lock)
142 {
143         struct io_kiocb *link = NULL;
144         bool posted = false;
145
146         if (req->flags & REQ_F_ARM_LTIMEOUT) {
147                 link = req->link;
148                 req->flags &= ~REQ_F_ARM_LTIMEOUT;
149                 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
150                         io_remove_next_linked(req);
151                         io_req_tw_post_queue(link, -ECANCELED, 0);
152                         posted = true;
153                 }
154         } else if (req->flags & REQ_F_LINK_TIMEOUT) {
155                 struct io_ring_ctx *ctx = req->ctx;
156
157                 spin_lock_irq(&ctx->timeout_lock);
158                 link = io_disarm_linked_timeout(req);
159                 spin_unlock_irq(&ctx->timeout_lock);
160                 if (link) {
161                         posted = true;
162                         io_req_tw_post_queue(link, -ECANCELED, 0);
163                 }
164         }
165         if (unlikely((req->flags & REQ_F_FAIL) &&
166                      !(req->flags & REQ_F_HARDLINK))) {
167                 posted |= (req->link != NULL);
168                 io_fail_links(req);
169         }
170         return posted;
171 }
172
173 struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,
174                                             struct io_kiocb *link)
175         __must_hold(&req->ctx->completion_lock)
176         __must_hold(&req->ctx->timeout_lock)
177 {
178         struct io_timeout_data *io = link->async_data;
179         struct io_timeout *timeout = io_kiocb_to_cmd(link);
180
181         io_remove_next_linked(req);
182         timeout->head = NULL;
183         if (hrtimer_try_to_cancel(&io->timer) != -1) {
184                 list_del(&timeout->list);
185                 return link;
186         }
187
188         return NULL;
189 }
190
191 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
192 {
193         struct io_timeout_data *data = container_of(timer,
194                                                 struct io_timeout_data, timer);
195         struct io_kiocb *req = data->req;
196         struct io_timeout *timeout = io_kiocb_to_cmd(req);
197         struct io_ring_ctx *ctx = req->ctx;
198         unsigned long flags;
199
200         spin_lock_irqsave(&ctx->timeout_lock, flags);
201         list_del_init(&timeout->list);
202         atomic_set(&req->ctx->cq_timeouts,
203                 atomic_read(&req->ctx->cq_timeouts) + 1);
204         spin_unlock_irqrestore(&ctx->timeout_lock, flags);
205
206         if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
207                 req_set_fail(req);
208
209         io_req_set_res(req, -ETIME, 0);
210         req->io_task_work.func = io_req_task_complete;
211         io_req_task_work_add(req);
212         return HRTIMER_NORESTART;
213 }
214
215 static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
216                                            struct io_cancel_data *cd)
217         __must_hold(&ctx->timeout_lock)
218 {
219         struct io_timeout *timeout;
220         struct io_timeout_data *io;
221         struct io_kiocb *req = NULL;
222
223         list_for_each_entry(timeout, &ctx->timeout_list, list) {
224                 struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
225
226                 if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
227                     cd->data != tmp->cqe.user_data)
228                         continue;
229                 if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) {
230                         if (cd->seq == tmp->work.cancel_seq)
231                                 continue;
232                         tmp->work.cancel_seq = cd->seq;
233                 }
234                 req = tmp;
235                 break;
236         }
237         if (!req)
238                 return ERR_PTR(-ENOENT);
239
240         io = req->async_data;
241         if (hrtimer_try_to_cancel(&io->timer) == -1)
242                 return ERR_PTR(-EALREADY);
243         timeout = io_kiocb_to_cmd(req);
244         list_del_init(&timeout->list);
245         return req;
246 }
247
248 int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
249         __must_hold(&ctx->completion_lock)
250 {
251         struct io_kiocb *req;
252
253         spin_lock_irq(&ctx->timeout_lock);
254         req = io_timeout_extract(ctx, cd);
255         spin_unlock_irq(&ctx->timeout_lock);
256
257         if (IS_ERR(req))
258                 return PTR_ERR(req);
259         io_req_task_queue_fail(req, -ECANCELED);
260         return 0;
261 }
262
263 static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
264 {
265         struct io_timeout *timeout = io_kiocb_to_cmd(req);
266         struct io_kiocb *prev = timeout->prev;
267         int ret = -ENOENT;
268
269         if (prev) {
270                 if (!(req->task->flags & PF_EXITING)) {
271                         struct io_cancel_data cd = {
272                                 .ctx            = req->ctx,
273                                 .data           = prev->cqe.user_data,
274                         };
275
276                         ret = io_try_cancel(req, &cd);
277                 }
278                 io_req_set_res(req, ret ?: -ETIME, 0);
279                 io_req_complete_post(req);
280                 io_put_req(prev);
281         } else {
282                 io_req_set_res(req, -ETIME, 0);
283                 io_req_complete_post(req);
284         }
285 }
286
287 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
288 {
289         struct io_timeout_data *data = container_of(timer,
290                                                 struct io_timeout_data, timer);
291         struct io_kiocb *prev, *req = data->req;
292         struct io_timeout *timeout = io_kiocb_to_cmd(req);
293         struct io_ring_ctx *ctx = req->ctx;
294         unsigned long flags;
295
296         spin_lock_irqsave(&ctx->timeout_lock, flags);
297         prev = timeout->head;
298         timeout->head = NULL;
299
300         /*
301          * We don't expect the list to be empty, that will only happen if we
302          * race with the completion of the linked work.
303          */
304         if (prev) {
305                 io_remove_next_linked(prev);
306                 if (!req_ref_inc_not_zero(prev))
307                         prev = NULL;
308         }
309         list_del(&timeout->list);
310         timeout->prev = prev;
311         spin_unlock_irqrestore(&ctx->timeout_lock, flags);
312
313         req->io_task_work.func = io_req_task_link_timeout;
314         io_req_task_work_add(req);
315         return HRTIMER_NORESTART;
316 }
317
318 static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
319 {
320         switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
321         case IORING_TIMEOUT_BOOTTIME:
322                 return CLOCK_BOOTTIME;
323         case IORING_TIMEOUT_REALTIME:
324                 return CLOCK_REALTIME;
325         default:
326                 /* can't happen, vetted at prep time */
327                 WARN_ON_ONCE(1);
328                 fallthrough;
329         case 0:
330                 return CLOCK_MONOTONIC;
331         }
332 }
333
334 static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
335                                     struct timespec64 *ts, enum hrtimer_mode mode)
336         __must_hold(&ctx->timeout_lock)
337 {
338         struct io_timeout_data *io;
339         struct io_timeout *timeout;
340         struct io_kiocb *req = NULL;
341
342         list_for_each_entry(timeout, &ctx->ltimeout_list, list) {
343                 struct io_kiocb *tmp = cmd_to_io_kiocb(timeout);
344
345                 if (user_data == tmp->cqe.user_data) {
346                         req = tmp;
347                         break;
348                 }
349         }
350         if (!req)
351                 return -ENOENT;
352
353         io = req->async_data;
354         if (hrtimer_try_to_cancel(&io->timer) == -1)
355                 return -EALREADY;
356         hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
357         io->timer.function = io_link_timeout_fn;
358         hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
359         return 0;
360 }
361
362 static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
363                              struct timespec64 *ts, enum hrtimer_mode mode)
364         __must_hold(&ctx->timeout_lock)
365 {
366         struct io_cancel_data cd = { .data = user_data, };
367         struct io_kiocb *req = io_timeout_extract(ctx, &cd);
368         struct io_timeout *timeout = io_kiocb_to_cmd(req);
369         struct io_timeout_data *data;
370
371         if (IS_ERR(req))
372                 return PTR_ERR(req);
373
374         timeout->off = 0; /* noseq */
375         data = req->async_data;
376         list_add_tail(&timeout->list, &ctx->timeout_list);
377         hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
378         data->timer.function = io_timeout_fn;
379         hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
380         return 0;
381 }
382
383 int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
384 {
385         struct io_timeout_rem *tr = io_kiocb_to_cmd(req);
386
387         if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
388                 return -EINVAL;
389         if (sqe->buf_index || sqe->len || sqe->splice_fd_in)
390                 return -EINVAL;
391
392         tr->ltimeout = false;
393         tr->addr = READ_ONCE(sqe->addr);
394         tr->flags = READ_ONCE(sqe->timeout_flags);
395         if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
396                 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
397                         return -EINVAL;
398                 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
399                         tr->ltimeout = true;
400                 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
401                         return -EINVAL;
402                 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
403                         return -EFAULT;
404                 if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0)
405                         return -EINVAL;
406         } else if (tr->flags) {
407                 /* timeout removal doesn't support flags */
408                 return -EINVAL;
409         }
410
411         return 0;
412 }
413
414 static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
415 {
416         return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
417                                             : HRTIMER_MODE_REL;
418 }
419
420 /*
421  * Remove or update an existing timeout command
422  */
423 int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
424 {
425         struct io_timeout_rem *tr = io_kiocb_to_cmd(req);
426         struct io_ring_ctx *ctx = req->ctx;
427         int ret;
428
429         if (!(tr->flags & IORING_TIMEOUT_UPDATE)) {
430                 struct io_cancel_data cd = { .data = tr->addr, };
431
432                 spin_lock(&ctx->completion_lock);
433                 ret = io_timeout_cancel(ctx, &cd);
434                 spin_unlock(&ctx->completion_lock);
435         } else {
436                 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
437
438                 spin_lock_irq(&ctx->timeout_lock);
439                 if (tr->ltimeout)
440                         ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
441                 else
442                         ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
443                 spin_unlock_irq(&ctx->timeout_lock);
444         }
445
446         if (ret < 0)
447                 req_set_fail(req);
448         io_req_set_res(req, ret, 0);
449         return IOU_OK;
450 }
451
452 static int __io_timeout_prep(struct io_kiocb *req,
453                              const struct io_uring_sqe *sqe,
454                              bool is_timeout_link)
455 {
456         struct io_timeout *timeout = io_kiocb_to_cmd(req);
457         struct io_timeout_data *data;
458         unsigned flags;
459         u32 off = READ_ONCE(sqe->off);
460
461         if (sqe->buf_index || sqe->len != 1 || sqe->splice_fd_in)
462                 return -EINVAL;
463         if (off && is_timeout_link)
464                 return -EINVAL;
465         flags = READ_ONCE(sqe->timeout_flags);
466         if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
467                       IORING_TIMEOUT_ETIME_SUCCESS))
468                 return -EINVAL;
469         /* more than one clock specified is invalid, obviously */
470         if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
471                 return -EINVAL;
472
473         INIT_LIST_HEAD(&timeout->list);
474         timeout->off = off;
475         if (unlikely(off && !req->ctx->off_timeout_used))
476                 req->ctx->off_timeout_used = true;
477
478         if (WARN_ON_ONCE(req_has_async_data(req)))
479                 return -EFAULT;
480         if (io_alloc_async_data(req))
481                 return -ENOMEM;
482
483         data = req->async_data;
484         data->req = req;
485         data->flags = flags;
486
487         if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
488                 return -EFAULT;
489
490         if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0)
491                 return -EINVAL;
492
493         INIT_LIST_HEAD(&timeout->list);
494         data->mode = io_translate_timeout_mode(flags);
495         hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
496
497         if (is_timeout_link) {
498                 struct io_submit_link *link = &req->ctx->submit_state.link;
499
500                 if (!link->head)
501                         return -EINVAL;
502                 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
503                         return -EINVAL;
504                 timeout->head = link->last;
505                 link->last->flags |= REQ_F_ARM_LTIMEOUT;
506         }
507         return 0;
508 }
509
510 int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
511 {
512         return __io_timeout_prep(req, sqe, false);
513 }
514
515 int io_link_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
516 {
517         return __io_timeout_prep(req, sqe, true);
518 }
519
520 int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
521 {
522         struct io_timeout *timeout = io_kiocb_to_cmd(req);
523         struct io_ring_ctx *ctx = req->ctx;
524         struct io_timeout_data *data = req->async_data;
525         struct list_head *entry;
526         u32 tail, off = timeout->off;
527
528         spin_lock_irq(&ctx->timeout_lock);
529
530         /*
531          * sqe->off holds how many events that need to occur for this
532          * timeout event to be satisfied. If it isn't set, then this is
533          * a pure timeout request, sequence isn't used.
534          */
535         if (io_is_timeout_noseq(req)) {
536                 entry = ctx->timeout_list.prev;
537                 goto add;
538         }
539
540         tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
541         timeout->target_seq = tail + off;
542
543         /* Update the last seq here in case io_flush_timeouts() hasn't.
544          * This is safe because ->completion_lock is held, and submissions
545          * and completions are never mixed in the same ->completion_lock section.
546          */
547         ctx->cq_last_tm_flush = tail;
548
549         /*
550          * Insertion sort, ensuring the first entry in the list is always
551          * the one we need first.
552          */
553         list_for_each_prev(entry, &ctx->timeout_list) {
554                 struct io_timeout *nextt = list_entry(entry, struct io_timeout, list);
555                 struct io_kiocb *nxt = cmd_to_io_kiocb(nextt);
556
557                 if (io_is_timeout_noseq(nxt))
558                         continue;
559                 /* nxt.seq is behind @tail, otherwise would've been completed */
560                 if (off >= nextt->target_seq - tail)
561                         break;
562         }
563 add:
564         list_add(&timeout->list, entry);
565         data->timer.function = io_timeout_fn;
566         hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
567         spin_unlock_irq(&ctx->timeout_lock);
568         return IOU_ISSUE_SKIP_COMPLETE;
569 }
570
571 void io_queue_linked_timeout(struct io_kiocb *req)
572 {
573         struct io_timeout *timeout = io_kiocb_to_cmd(req);
574         struct io_ring_ctx *ctx = req->ctx;
575
576         spin_lock_irq(&ctx->timeout_lock);
577         /*
578          * If the back reference is NULL, then our linked request finished
579          * before we got a chance to setup the timer
580          */
581         if (timeout->head) {
582                 struct io_timeout_data *data = req->async_data;
583
584                 data->timer.function = io_link_timeout_fn;
585                 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
586                                 data->mode);
587                 list_add_tail(&timeout->list, &ctx->ltimeout_list);
588         }
589         spin_unlock_irq(&ctx->timeout_lock);
590         /* drop submission reference */
591         io_put_req(req);
592 }
593
594 static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
595                           bool cancel_all)
596         __must_hold(&req->ctx->timeout_lock)
597 {
598         struct io_kiocb *req;
599
600         if (task && head->task != task)
601                 return false;
602         if (cancel_all)
603                 return true;
604
605         io_for_each_link(req, head) {
606                 if (req->flags & REQ_F_INFLIGHT)
607                         return true;
608         }
609         return false;
610 }
611
612 /* Returns true if we found and killed one or more timeouts */
613 __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
614                              bool cancel_all)
615 {
616         struct io_timeout *timeout, *tmp;
617         int canceled = 0;
618
619         spin_lock(&ctx->completion_lock);
620         spin_lock_irq(&ctx->timeout_lock);
621         list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
622                 struct io_kiocb *req = cmd_to_io_kiocb(timeout);
623
624                 if (io_match_task(req, tsk, cancel_all)) {
625                         io_kill_timeout(req, -ECANCELED);
626                         canceled++;
627                 }
628         }
629         spin_unlock_irq(&ctx->timeout_lock);
630         io_commit_cqring(ctx);
631         spin_unlock(&ctx->completion_lock);
632         if (canceled != 0)
633                 io_cqring_ev_posted(ctx);
634         return canceled != 0;
635 }