|
| 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/io_uring.h> |
| 7 | + |
| 8 | +#include <uapi/linux/io_uring.h> |
| 9 | + |
| 10 | +#include "../kernel/futex/futex.h" |
| 11 | +#include "io_uring.h" |
| 12 | +#include "rsrc.h" |
| 13 | +#include "futex.h" |
| 14 | + |
| 15 | +struct io_futex { |
| 16 | + struct file *file; |
| 17 | + u32 __user *uaddr; |
| 18 | + unsigned long futex_val; |
| 19 | + unsigned long futex_mask; |
| 20 | + u32 futex_flags; |
| 21 | +}; |
| 22 | + |
| 23 | +struct io_futex_data { |
| 24 | + union { |
| 25 | + struct futex_q q; |
| 26 | + struct io_cache_entry cache; |
| 27 | + }; |
| 28 | + struct io_kiocb *req; |
| 29 | +}; |
| 30 | + |
| 31 | +void io_futex_cache_init(struct io_ring_ctx *ctx) |
| 32 | +{ |
| 33 | + io_alloc_cache_init(&ctx->futex_cache, IO_NODE_ALLOC_CACHE_MAX, |
| 34 | + sizeof(struct io_futex_data)); |
| 35 | +} |
| 36 | + |
| 37 | +static void io_futex_cache_entry_free(struct io_cache_entry *entry) |
| 38 | +{ |
| 39 | + kfree(container_of(entry, struct io_futex_data, cache)); |
| 40 | +} |
| 41 | + |
| 42 | +void io_futex_cache_free(struct io_ring_ctx *ctx) |
| 43 | +{ |
| 44 | + io_alloc_cache_free(&ctx->futex_cache, io_futex_cache_entry_free); |
| 45 | +} |
| 46 | + |
| 47 | +static void io_futex_complete(struct io_kiocb *req, struct io_tw_state *ts) |
| 48 | +{ |
| 49 | + struct io_futex_data *ifd = req->async_data; |
| 50 | + struct io_ring_ctx *ctx = req->ctx; |
| 51 | + |
| 52 | + io_tw_lock(ctx, ts); |
| 53 | + if (!io_alloc_cache_put(&ctx->futex_cache, &ifd->cache)) |
| 54 | + kfree(ifd); |
| 55 | + req->async_data = NULL; |
| 56 | + hlist_del_init(&req->hash_node); |
| 57 | + io_req_task_complete(req, ts); |
| 58 | +} |
| 59 | + |
| 60 | +static bool __io_futex_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req) |
| 61 | +{ |
| 62 | + struct io_futex_data *ifd = req->async_data; |
| 63 | + |
| 64 | + /* futex wake already done or in progress */ |
| 65 | + if (!futex_unqueue(&ifd->q)) |
| 66 | + return false; |
| 67 | + |
| 68 | + hlist_del_init(&req->hash_node); |
| 69 | + io_req_set_res(req, -ECANCELED, 0); |
| 70 | + req->io_task_work.func = io_futex_complete; |
| 71 | + io_req_task_work_add(req); |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +int io_futex_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, |
| 76 | + unsigned int issue_flags) |
| 77 | +{ |
| 78 | + struct hlist_node *tmp; |
| 79 | + struct io_kiocb *req; |
| 80 | + int nr = 0; |
| 81 | + |
| 82 | + if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_FD_FIXED)) |
| 83 | + return -ENOENT; |
| 84 | + |
| 85 | + io_ring_submit_lock(ctx, issue_flags); |
| 86 | + hlist_for_each_entry_safe(req, tmp, &ctx->futex_list, hash_node) { |
| 87 | + if (req->cqe.user_data != cd->data && |
| 88 | + !(cd->flags & IORING_ASYNC_CANCEL_ANY)) |
| 89 | + continue; |
| 90 | + if (__io_futex_cancel(ctx, req)) |
| 91 | + nr++; |
| 92 | + if (!(cd->flags & IORING_ASYNC_CANCEL_ALL)) |
| 93 | + break; |
| 94 | + } |
| 95 | + io_ring_submit_unlock(ctx, issue_flags); |
| 96 | + |
| 97 | + if (nr) |
| 98 | + return nr; |
| 99 | + |
| 100 | + return -ENOENT; |
| 101 | +} |
| 102 | + |
| 103 | +bool io_futex_remove_all(struct io_ring_ctx *ctx, struct task_struct *task, |
| 104 | + bool cancel_all) |
| 105 | +{ |
| 106 | + struct hlist_node *tmp; |
| 107 | + struct io_kiocb *req; |
| 108 | + bool found = false; |
| 109 | + |
| 110 | + lockdep_assert_held(&ctx->uring_lock); |
| 111 | + |
| 112 | + hlist_for_each_entry_safe(req, tmp, &ctx->futex_list, hash_node) { |
| 113 | + if (!io_match_task_safe(req, task, cancel_all)) |
| 114 | + continue; |
| 115 | + __io_futex_cancel(ctx, req); |
| 116 | + found = true; |
| 117 | + } |
| 118 | + |
| 119 | + return found; |
| 120 | +} |
| 121 | + |
| 122 | +int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 123 | +{ |
| 124 | + struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex); |
| 125 | + u32 flags; |
| 126 | + |
| 127 | + if (unlikely(sqe->len || sqe->futex_flags || sqe->buf_index || |
| 128 | + sqe->file_index)) |
| 129 | + return -EINVAL; |
| 130 | + |
| 131 | + iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 132 | + iof->futex_val = READ_ONCE(sqe->addr2); |
| 133 | + iof->futex_mask = READ_ONCE(sqe->addr3); |
| 134 | + flags = READ_ONCE(sqe->fd); |
| 135 | + |
| 136 | + if (flags & ~FUTEX2_VALID_MASK) |
| 137 | + return -EINVAL; |
| 138 | + |
| 139 | + iof->futex_flags = futex2_to_flags(flags); |
| 140 | + if (!futex_flags_valid(iof->futex_flags)) |
| 141 | + return -EINVAL; |
| 142 | + |
| 143 | + if (!futex_validate_input(iof->futex_flags, iof->futex_val) || |
| 144 | + !futex_validate_input(iof->futex_flags, iof->futex_mask)) |
| 145 | + return -EINVAL; |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +static void io_futex_wake_fn(struct wake_q_head *wake_q, struct futex_q *q) |
| 151 | +{ |
| 152 | + struct io_futex_data *ifd = container_of(q, struct io_futex_data, q); |
| 153 | + struct io_kiocb *req = ifd->req; |
| 154 | + |
| 155 | + if (unlikely(!__futex_wake_mark(q))) |
| 156 | + return; |
| 157 | + |
| 158 | + io_req_set_res(req, 0, 0); |
| 159 | + req->io_task_work.func = io_futex_complete; |
| 160 | + io_req_task_work_add(req); |
| 161 | +} |
| 162 | + |
| 163 | +static struct io_futex_data *io_alloc_ifd(struct io_ring_ctx *ctx) |
| 164 | +{ |
| 165 | + struct io_cache_entry *entry; |
| 166 | + |
| 167 | + entry = io_alloc_cache_get(&ctx->futex_cache); |
| 168 | + if (entry) |
| 169 | + return container_of(entry, struct io_futex_data, cache); |
| 170 | + |
| 171 | + return kmalloc(sizeof(struct io_futex_data), GFP_NOWAIT); |
| 172 | +} |
| 173 | + |
| 174 | +int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags) |
| 175 | +{ |
| 176 | + struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex); |
| 177 | + struct io_ring_ctx *ctx = req->ctx; |
| 178 | + struct io_futex_data *ifd = NULL; |
| 179 | + struct futex_hash_bucket *hb; |
| 180 | + int ret; |
| 181 | + |
| 182 | + if (!iof->futex_mask) { |
| 183 | + ret = -EINVAL; |
| 184 | + goto done; |
| 185 | + } |
| 186 | + |
| 187 | + io_ring_submit_lock(ctx, issue_flags); |
| 188 | + ifd = io_alloc_ifd(ctx); |
| 189 | + if (!ifd) { |
| 190 | + ret = -ENOMEM; |
| 191 | + goto done_unlock; |
| 192 | + } |
| 193 | + |
| 194 | + req->async_data = ifd; |
| 195 | + ifd->q = futex_q_init; |
| 196 | + ifd->q.bitset = iof->futex_mask; |
| 197 | + ifd->q.wake = io_futex_wake_fn; |
| 198 | + ifd->req = req; |
| 199 | + |
| 200 | + ret = futex_wait_setup(iof->uaddr, iof->futex_val, iof->futex_flags, |
| 201 | + &ifd->q, &hb); |
| 202 | + if (!ret) { |
| 203 | + hlist_add_head(&req->hash_node, &ctx->futex_list); |
| 204 | + io_ring_submit_unlock(ctx, issue_flags); |
| 205 | + |
| 206 | + futex_queue(&ifd->q, hb); |
| 207 | + return IOU_ISSUE_SKIP_COMPLETE; |
| 208 | + } |
| 209 | + |
| 210 | +done_unlock: |
| 211 | + io_ring_submit_unlock(ctx, issue_flags); |
| 212 | +done: |
| 213 | + if (ret < 0) |
| 214 | + req_set_fail(req); |
| 215 | + io_req_set_res(req, ret, 0); |
| 216 | + kfree(ifd); |
| 217 | + return IOU_OK; |
| 218 | +} |
| 219 | + |
| 220 | +int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags) |
| 221 | +{ |
| 222 | + struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex); |
| 223 | + int ret; |
| 224 | + |
| 225 | + /* |
| 226 | + * Strict flags - ensure that waking 0 futexes yields a 0 result. |
| 227 | + * See commit 43adf8449510 ("futex: FLAGS_STRICT") for details. |
| 228 | + */ |
| 229 | + ret = futex_wake(iof->uaddr, FLAGS_STRICT | iof->futex_flags, |
| 230 | + iof->futex_val, iof->futex_mask); |
| 231 | + if (ret < 0) |
| 232 | + req_set_fail(req); |
| 233 | + io_req_set_res(req, ret, 0); |
| 234 | + return IOU_OK; |
| 235 | +} |
0 commit comments