ublk: don't get ublk device reference in ublk_abort_queue()
[linux-block.git] / io_uring / cancel.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/namei.h>
9 #include <linux/nospec.h>
10 #include <linux/io_uring.h>
11
12 #include <uapi/linux/io_uring.h>
13
14 #include "io_uring.h"
15 #include "tctx.h"
16 #include "poll.h"
17 #include "timeout.h"
18 #include "waitid.h"
19 #include "cancel.h"
20
21 struct io_cancel {
22         struct file                     *file;
23         u64                             addr;
24         u32                             flags;
25         s32                             fd;
26         u8                              opcode;
27 };
28
29 #define CANCEL_FLAGS    (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_FD | \
30                          IORING_ASYNC_CANCEL_ANY | IORING_ASYNC_CANCEL_FD_FIXED | \
31                          IORING_ASYNC_CANCEL_USERDATA | IORING_ASYNC_CANCEL_OP)
32
33 /*
34  * Returns true if the request matches the criteria outlined by 'cd'.
35  */
36 bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd)
37 {
38         bool match_user_data = cd->flags & IORING_ASYNC_CANCEL_USERDATA;
39
40         if (req->ctx != cd->ctx)
41                 return false;
42
43         if (!(cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP)))
44                 match_user_data = true;
45
46         if (cd->flags & IORING_ASYNC_CANCEL_ANY)
47                 goto check_seq;
48         if (cd->flags & IORING_ASYNC_CANCEL_FD) {
49                 if (req->file != cd->file)
50                         return false;
51         }
52         if (cd->flags & IORING_ASYNC_CANCEL_OP) {
53                 if (req->opcode != cd->opcode)
54                         return false;
55         }
56         if (match_user_data && req->cqe.user_data != cd->data)
57                 return false;
58         if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
59 check_seq:
60                 if (cd->seq == req->work.cancel_seq)
61                         return false;
62                 req->work.cancel_seq = cd->seq;
63         }
64
65         return true;
66 }
67
68 static bool io_cancel_cb(struct io_wq_work *work, void *data)
69 {
70         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
71         struct io_cancel_data *cd = data;
72
73         return io_cancel_req_match(req, cd);
74 }
75
76 static int io_async_cancel_one(struct io_uring_task *tctx,
77                                struct io_cancel_data *cd)
78 {
79         enum io_wq_cancel cancel_ret;
80         int ret = 0;
81         bool all;
82
83         if (!tctx || !tctx->io_wq)
84                 return -ENOENT;
85
86         all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
87         cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all);
88         switch (cancel_ret) {
89         case IO_WQ_CANCEL_OK:
90                 ret = 0;
91                 break;
92         case IO_WQ_CANCEL_RUNNING:
93                 ret = -EALREADY;
94                 break;
95         case IO_WQ_CANCEL_NOTFOUND:
96                 ret = -ENOENT;
97                 break;
98         }
99
100         return ret;
101 }
102
103 int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd,
104                   unsigned issue_flags)
105 {
106         struct io_ring_ctx *ctx = cd->ctx;
107         int ret;
108
109         WARN_ON_ONCE(!io_wq_current_is_worker() && tctx != current->io_uring);
110
111         ret = io_async_cancel_one(tctx, cd);
112         /*
113          * Fall-through even for -EALREADY, as we may have poll armed
114          * that need unarming.
115          */
116         if (!ret)
117                 return 0;
118
119         ret = io_poll_cancel(ctx, cd, issue_flags);
120         if (ret != -ENOENT)
121                 return ret;
122
123         ret = io_waitid_cancel(ctx, cd, issue_flags);
124         if (ret != -ENOENT)
125                 return ret;
126
127         spin_lock(&ctx->completion_lock);
128         if (!(cd->flags & IORING_ASYNC_CANCEL_FD))
129                 ret = io_timeout_cancel(ctx, cd);
130         spin_unlock(&ctx->completion_lock);
131         return ret;
132 }
133
134 int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
135 {
136         struct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel);
137
138         if (unlikely(req->flags & REQ_F_BUFFER_SELECT))
139                 return -EINVAL;
140         if (sqe->off || sqe->splice_fd_in)
141                 return -EINVAL;
142
143         cancel->addr = READ_ONCE(sqe->addr);
144         cancel->flags = READ_ONCE(sqe->cancel_flags);
145         if (cancel->flags & ~CANCEL_FLAGS)
146                 return -EINVAL;
147         if (cancel->flags & IORING_ASYNC_CANCEL_FD) {
148                 if (cancel->flags & IORING_ASYNC_CANCEL_ANY)
149                         return -EINVAL;
150                 cancel->fd = READ_ONCE(sqe->fd);
151         }
152         if (cancel->flags & IORING_ASYNC_CANCEL_OP) {
153                 if (cancel->flags & IORING_ASYNC_CANCEL_ANY)
154                         return -EINVAL;
155                 cancel->opcode = READ_ONCE(sqe->len);
156         }
157
158         return 0;
159 }
160
161 static int __io_async_cancel(struct io_cancel_data *cd,
162                              struct io_uring_task *tctx,
163                              unsigned int issue_flags)
164 {
165         bool all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
166         struct io_ring_ctx *ctx = cd->ctx;
167         struct io_tctx_node *node;
168         int ret, nr = 0;
169
170         do {
171                 ret = io_try_cancel(tctx, cd, issue_flags);
172                 if (ret == -ENOENT)
173                         break;
174                 if (!all)
175                         return ret;
176                 nr++;
177         } while (1);
178
179         /* slow path, try all io-wq's */
180         io_ring_submit_lock(ctx, issue_flags);
181         ret = -ENOENT;
182         list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
183                 struct io_uring_task *tctx = node->task->io_uring;
184
185                 ret = io_async_cancel_one(tctx, cd);
186                 if (ret != -ENOENT) {
187                         if (!all)
188                                 break;
189                         nr++;
190                 }
191         }
192         io_ring_submit_unlock(ctx, issue_flags);
193         return all ? nr : ret;
194 }
195
196 int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
197 {
198         struct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel);
199         struct io_cancel_data cd = {
200                 .ctx    = req->ctx,
201                 .data   = cancel->addr,
202                 .flags  = cancel->flags,
203                 .opcode = cancel->opcode,
204                 .seq    = atomic_inc_return(&req->ctx->cancel_seq),
205         };
206         struct io_uring_task *tctx = req->task->io_uring;
207         int ret;
208
209         if (cd.flags & IORING_ASYNC_CANCEL_FD) {
210                 if (req->flags & REQ_F_FIXED_FILE ||
211                     cd.flags & IORING_ASYNC_CANCEL_FD_FIXED) {
212                         req->flags |= REQ_F_FIXED_FILE;
213                         req->file = io_file_get_fixed(req, cancel->fd,
214                                                         issue_flags);
215                 } else {
216                         req->file = io_file_get_normal(req, cancel->fd);
217                 }
218                 if (!req->file) {
219                         ret = -EBADF;
220                         goto done;
221                 }
222                 cd.file = req->file;
223         }
224
225         ret = __io_async_cancel(&cd, tctx, issue_flags);
226 done:
227         if (ret < 0)
228                 req_set_fail(req);
229         io_req_set_res(req, ret, 0);
230         return IOU_OK;
231 }
232
233 void init_hash_table(struct io_hash_table *table, unsigned size)
234 {
235         unsigned int i;
236
237         for (i = 0; i < size; i++) {
238                 spin_lock_init(&table->hbs[i].lock);
239                 INIT_HLIST_HEAD(&table->hbs[i].list);
240         }
241 }
242
243 static int __io_sync_cancel(struct io_uring_task *tctx,
244                             struct io_cancel_data *cd, int fd)
245 {
246         struct io_ring_ctx *ctx = cd->ctx;
247
248         /* fixed must be grabbed every time since we drop the uring_lock */
249         if ((cd->flags & IORING_ASYNC_CANCEL_FD) &&
250             (cd->flags & IORING_ASYNC_CANCEL_FD_FIXED)) {
251                 if (unlikely(fd >= ctx->nr_user_files))
252                         return -EBADF;
253                 fd = array_index_nospec(fd, ctx->nr_user_files);
254                 cd->file = io_file_from_index(&ctx->file_table, fd);
255                 if (!cd->file)
256                         return -EBADF;
257         }
258
259         return __io_async_cancel(cd, tctx, 0);
260 }
261
262 int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)
263         __must_hold(&ctx->uring_lock)
264 {
265         struct io_cancel_data cd = {
266                 .ctx    = ctx,
267                 .seq    = atomic_inc_return(&ctx->cancel_seq),
268         };
269         ktime_t timeout = KTIME_MAX;
270         struct io_uring_sync_cancel_reg sc;
271         struct fd f = { };
272         DEFINE_WAIT(wait);
273         int ret, i;
274
275         if (copy_from_user(&sc, arg, sizeof(sc)))
276                 return -EFAULT;
277         if (sc.flags & ~CANCEL_FLAGS)
278                 return -EINVAL;
279         for (i = 0; i < ARRAY_SIZE(sc.pad); i++)
280                 if (sc.pad[i])
281                         return -EINVAL;
282         for (i = 0; i < ARRAY_SIZE(sc.pad2); i++)
283                 if (sc.pad2[i])
284                         return -EINVAL;
285
286         cd.data = sc.addr;
287         cd.flags = sc.flags;
288         cd.opcode = sc.opcode;
289
290         /* we can grab a normal file descriptor upfront */
291         if ((cd.flags & IORING_ASYNC_CANCEL_FD) &&
292            !(cd.flags & IORING_ASYNC_CANCEL_FD_FIXED)) {
293                 f = fdget(sc.fd);
294                 if (!f.file)
295                         return -EBADF;
296                 cd.file = f.file;
297         }
298
299         ret = __io_sync_cancel(current->io_uring, &cd, sc.fd);
300
301         /* found something, done! */
302         if (ret != -EALREADY)
303                 goto out;
304
305         if (sc.timeout.tv_sec != -1UL || sc.timeout.tv_nsec != -1UL) {
306                 struct timespec64 ts = {
307                         .tv_sec         = sc.timeout.tv_sec,
308                         .tv_nsec        = sc.timeout.tv_nsec
309                 };
310
311                 timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
312         }
313
314         /*
315          * Keep looking until we get -ENOENT. we'll get woken everytime
316          * every time a request completes and will retry the cancelation.
317          */
318         do {
319                 cd.seq = atomic_inc_return(&ctx->cancel_seq);
320
321                 prepare_to_wait(&ctx->cq_wait, &wait, TASK_INTERRUPTIBLE);
322
323                 ret = __io_sync_cancel(current->io_uring, &cd, sc.fd);
324
325                 mutex_unlock(&ctx->uring_lock);
326                 if (ret != -EALREADY)
327                         break;
328
329                 ret = io_run_task_work_sig(ctx);
330                 if (ret < 0)
331                         break;
332                 ret = schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS);
333                 if (!ret) {
334                         ret = -ETIME;
335                         break;
336                 }
337                 mutex_lock(&ctx->uring_lock);
338         } while (1);
339
340         finish_wait(&ctx->cq_wait, &wait);
341         mutex_lock(&ctx->uring_lock);
342
343         if (ret == -ENOENT || ret > 0)
344                 ret = 0;
345 out:
346         fdput(f);
347         return ret;
348 }