History

io_uring: add support for vectored futex waits

This adds support for IORING_OP_FUTEX_WAITV, which allows registering a notification for a number of futexes at once. If one of the futexes are woken, then the request will complete with the index of the futex that got woken as the result. This is identical to what the normal vectored futex waitv operation does. Use like IORING_OP_FUTEX_WAIT, except sqe->addr must now contain a pointer to a struct futex_waitv array, and sqe->off must now contain the number of elements in that array. As flags are passed in the futex_vector array, and likewise for the value and futex address(es), sqe->addr2 and sqe->addr3 are also reserved for IORING_OP_FUTEX_WAITV. For cancelations, FUTEX_WAITV does not rely on the futex_unqueue() return value as we're dealing with multiple futexes. Instead, a separate per io_uring request atomic is used to claim ownership of the request. Waiting on N futexes could be done with IORING_OP_FUTEX_WAIT as well, but that punts a lot of the work to the application: 1) Application would need to submit N IORING_OP_FUTEX_WAIT requests, rather than just a single IORING_OP_FUTEX_WAITV. 2) When one futex is woken, application would need to cancel the remaining N-1 requests that didn't trigger. While this is of course doable, having a single vectored futex wait makes for much simpler application code.

Jens Axboe committed 3 years ago

Commit 8f350191 parent e9a56c94 files · +174 −9Browse files

Unified · Split

4 files
  1. include/uapi/linux/io_uring.h +1 −0
  2. io_uring/futex.c +160 −9
  3. io_uring/futex.h +2 −0
  4. io_uring/opdef.c +11 −0
include/uapi/linux/io_uring.h+1−0
@@ -246,6 +246,7 @@ enum io_uring_op { · 245 unmodified lines
246 IORING_OP_WAITID,246 IORING_OP_WAITID,
247 IORING_OP_FUTEX_WAIT,247 IORING_OP_FUTEX_WAIT,
248 IORING_OP_FUTEX_WAKE,248 IORING_OP_FUTEX_WAKE,
249 IORING_OP_FUTEX_WAITV,
249 250
250 /* this goes last, obviously */251 /* this goes last, obviously */
251 IORING_OP_LAST,252 IORING_OP_LAST,
io_uring/futex.c+160−9
@@ -14,10 +14,16 @@ · 13 unmodified lines
14 14
15struct io_futex {15struct io_futex {
16 struct file *file;16 struct file *file;
17 u32 __user *uaddr;17 union {
18 u32 __user *uaddr;
19 struct futex_waitv __user *uwaitv;
20 };
18 unsigned long futex_val;21 unsigned long futex_val;
19 unsigned long futex_mask;22 unsigned long futex_mask;
23 unsigned long futexv_owned;
20 u32 futex_flags;24 u32 futex_flags;
25 unsigned int futex_nr;
26 bool futexv_unqueued;
21};27};
22 28
23struct io_futex_data {29struct io_futex_data {
@@ -44,6 +50,13 @@ void io_futex_cache_free(struct io_ring_ctx *ctx) · 20 unmodified lines
44 io_alloc_cache_free(&ctx->futex_cache, io_futex_cache_entry_free);50 io_alloc_cache_free(&ctx->futex_cache, io_futex_cache_entry_free);
45}51}
46 52
53static void __io_futex_complete(struct io_kiocb *req, struct io_tw_state *ts)
54{
55 req->async_data = NULL;
56 hlist_del_init(&req->hash_node);
57 io_req_task_complete(req, ts);
58}
59
47static void io_futex_complete(struct io_kiocb *req, struct io_tw_state *ts)60static void io_futex_complete(struct io_kiocb *req, struct io_tw_state *ts)
48{61{
49 struct io_futex_data *ifd = req->async_data;62 struct io_futex_data *ifd = req->async_data;
@@ -52,22 +65,56 @@ static void io_futex_complete(struct io_kiocb *req, struct io_tw_state *ts) · 2 unmodified lines
52 io_tw_lock(ctx, ts);65 io_tw_lock(ctx, ts);
53 if (!io_alloc_cache_put(&ctx->futex_cache, &ifd->cache))66 if (!io_alloc_cache_put(&ctx->futex_cache, &ifd->cache))
54 kfree(ifd);67 kfree(ifd);
55 req->async_data = NULL;68 __io_futex_complete(req, ts);
56 hlist_del_init(&req->hash_node);
57 io_req_task_complete(req, ts);
58}69}
59 70
60static bool __io_futex_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)71static void io_futexv_complete(struct io_kiocb *req, struct io_tw_state *ts)
61{72{
62 struct io_futex_data *ifd = req->async_data;73 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
74 struct futex_vector *futexv = req->async_data;
63 75
64 /* futex wake already done or in progress */76 io_tw_lock(req->ctx, ts);
65 if (!futex_unqueue(&ifd->q))77
78 if (!iof->futexv_unqueued) {
79 int res;
80
81 res = futex_unqueue_multiple(futexv, iof->futex_nr);
82 if (res != -1)
83 io_req_set_res(req, res, 0);
84 }
85
86 kfree(req->async_data);
87 req->flags &= ~REQ_F_ASYNC_DATA;
88 __io_futex_complete(req, ts);
89}
90
91static bool io_futexv_claim(struct io_futex *iof)
92{
93 if (test_bit(0, &iof->futexv_owned) ||
94 test_and_set_bit_lock(0, &iof->futexv_owned))
66 return false;95 return false;
96 return true;
97}
98
99static bool __io_futex_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
100{
101 /* futex wake already done or in progress */
102 if (req->opcode == IORING_OP_FUTEX_WAIT) {
103 struct io_futex_data *ifd = req->async_data;
104
105 if (!futex_unqueue(&ifd->q))
106 return false;
107 req->io_task_work.func = io_futex_complete;
108 } else {
109 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
110
111 if (!io_futexv_claim(iof))
112 return false;
113 req->io_task_work.func = io_futexv_complete;
114 }
67 115
68 hlist_del_init(&req->hash_node);116 hlist_del_init(&req->hash_node);
69 io_req_set_res(req, -ECANCELED, 0);117 io_req_set_res(req, -ECANCELED, 0);
70 req->io_task_work.func = io_futex_complete;
71 io_req_task_work_add(req);118 io_req_task_work_add(req);
72 return true;119 return true;
73}120}
@@ -147,6 +194,55 @@ int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 73 unmodified lines
147 return 0;194 return 0;
148}195}
149 196
197static void io_futex_wakev_fn(struct wake_q_head *wake_q, struct futex_q *q)
198{
199 struct io_kiocb *req = q->wake_data;
200 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
201
202 if (!io_futexv_claim(iof))
203 return;
204 if (unlikely(!__futex_wake_mark(q)))
205 return;
206
207 io_req_set_res(req, 0, 0);
208 req->io_task_work.func = io_futexv_complete;
209 io_req_task_work_add(req);
210}
211
212int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
213{
214 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
215 struct futex_vector *futexv;
216 int ret;
217
218 /* No flags or mask supported for waitv */
219 if (unlikely(sqe->fd || sqe->buf_index || sqe->file_index ||
220 sqe->addr2 || sqe->futex_flags || sqe->addr3))
221 return -EINVAL;
222
223 iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
224 iof->futex_nr = READ_ONCE(sqe->len);
225 if (!iof->futex_nr || iof->futex_nr > FUTEX_WAITV_MAX)
226 return -EINVAL;
227
228 futexv = kcalloc(iof->futex_nr, sizeof(*futexv), GFP_KERNEL);
229 if (!futexv)
230 return -ENOMEM;
231
232 ret = futex_parse_waitv(futexv, iof->uwaitv, iof->futex_nr,
233 io_futex_wakev_fn, req);
234 if (ret) {
235 kfree(futexv);
236 return ret;
237 }
238
239 iof->futexv_owned = 0;
240 iof->futexv_unqueued = 0;
241 req->flags |= REQ_F_ASYNC_DATA;
242 req->async_data = futexv;
243 return 0;
244}
245
150static void io_futex_wake_fn(struct wake_q_head *wake_q, struct futex_q *q)246static void io_futex_wake_fn(struct wake_q_head *wake_q, struct futex_q *q)
151{247{
152 struct io_futex_data *ifd = container_of(q, struct io_futex_data, q);248 struct io_futex_data *ifd = container_of(q, struct io_futex_data, q);
@@ -171,6 +267,61 @@ static struct io_futex_data *io_alloc_ifd(struct io_ring_ctx *ctx) · 18 unmodified lines
171 return kmalloc(sizeof(struct io_futex_data), GFP_NOWAIT);267 return kmalloc(sizeof(struct io_futex_data), GFP_NOWAIT);
172}268}
173 269
270int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)
271{
272 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
273 struct futex_vector *futexv = req->async_data;
274 struct io_ring_ctx *ctx = req->ctx;
275 int ret, woken = -1;
276
277 io_ring_submit_lock(ctx, issue_flags);
278
279 ret = futex_wait_multiple_setup(futexv, iof->futex_nr, &woken);
280
281 /*
282 * Error case, ret is < 0. Mark the request as failed.
283 */
284 if (unlikely(ret < 0)) {
285 io_ring_submit_unlock(ctx, issue_flags);
286 req_set_fail(req);
287 io_req_set_res(req, ret, 0);
288 kfree(futexv);
289 req->async_data = NULL;
290 req->flags &= ~REQ_F_ASYNC_DATA;
291 return IOU_OK;
292 }
293
294 /*
295 * 0 return means that we successfully setup the waiters, and that
296 * nobody triggered a wakeup while we were doing so. If the wakeup
297 * happened post setup, the task_work will be run post this issue and
298 * under the submission lock. 1 means We got woken while setting up,
299 * let that side do the completion. Note that
300 * futex_wait_multiple_setup() will have unqueued all the futexes in
301 * this case. Mark us as having done that already, since this is
302 * different from normal wakeup.
303 */
304 if (!ret) {
305 /*
306 * If futex_wait_multiple_setup() returns 0 for a
307 * successful setup, then the task state will not be
308 * runnable. This is fine for the sync syscall, as
309 * it'll be blocking unless we already got one of the
310 * futexes woken, but it obviously won't work for an
311 * async invocation. Mark us runnable again.
312 */
313 __set_current_state(TASK_RUNNING);
314 hlist_add_head(&req->hash_node, &ctx->futex_list);
315 } else {
316 iof->futexv_unqueued = 1;
317 if (woken != -1)
318 io_req_set_res(req, woken, 0);
319 }
320
321 io_ring_submit_unlock(ctx, issue_flags);
322 return IOU_ISSUE_SKIP_COMPLETE;
323}
324
174int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)325int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
175{326{
176 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);327 struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
io_uring/futex.h+2−0
@@ -3,7 +3,9 @@ · 2 unmodified lines
3#include "cancel.h"3#include "cancel.h"
4 4
5int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);5int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
6int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
6int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags);7int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags);
8int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags);
7int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags);9int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags);
8 10
9#if defined(CONFIG_FUTEX)11#if defined(CONFIG_FUTEX)
io_uring/opdef.c+11−0
@@ -459,6 +459,14 @@ const struct io_issue_def io_issue_defs[] = { · 458 unmodified lines
459 .issue = io_futex_wake,459 .issue = io_futex_wake,
460#else460#else
461 .prep = io_eopnotsupp_prep,461 .prep = io_eopnotsupp_prep,
462#endif
463 },
464 [IORING_OP_FUTEX_WAITV] = {
465#if defined(CONFIG_FUTEX)
466 .prep = io_futexv_prep,
467 .issue = io_futexv_wait,
468#else
469 .prep = io_eopnotsupp_prep,
462#endif470#endif
463 },471 },
464};472};
@@ -693,6 +701,9 @@ const struct io_cold_def io_cold_defs[] = { · 228 unmodified lines
693 [IORING_OP_FUTEX_WAKE] = {701 [IORING_OP_FUTEX_WAKE] = {
694 .name = "FUTEX_WAKE",702 .name = "FUTEX_WAKE",
695 },703 },
704 [IORING_OP_FUTEX_WAITV] = {
705 .name = "FUTEX_WAITV",
706 },
696};707};
697 708
698const char *io_uring_get_opcode(u8 opcode)709const char *io_uring_get_opcode(u8 opcode)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t