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