Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/io_uring/io_uring.h
49156 views
1
#ifndef IOU_CORE_H
2
#define IOU_CORE_H
3
4
#include <linux/errno.h>
5
#include <linux/lockdep.h>
6
#include <linux/resume_user_mode.h>
7
#include <linux/kasan.h>
8
#include <linux/poll.h>
9
#include <linux/io_uring_types.h>
10
#include <uapi/linux/eventpoll.h>
11
#include "alloc_cache.h"
12
#include "io-wq.h"
13
#include "slist.h"
14
#include "opdef.h"
15
16
#ifndef CREATE_TRACE_POINTS
17
#include <trace/events/io_uring.h>
18
#endif
19
20
struct io_rings_layout {
21
/* size of CQ + headers + SQ offset array */
22
size_t rings_size;
23
size_t sq_size;
24
25
size_t sq_array_offset;
26
};
27
28
struct io_ctx_config {
29
struct io_uring_params p;
30
struct io_rings_layout layout;
31
struct io_uring_params __user *uptr;
32
};
33
34
#define IORING_FEAT_FLAGS (IORING_FEAT_SINGLE_MMAP |\
35
IORING_FEAT_NODROP |\
36
IORING_FEAT_SUBMIT_STABLE |\
37
IORING_FEAT_RW_CUR_POS |\
38
IORING_FEAT_CUR_PERSONALITY |\
39
IORING_FEAT_FAST_POLL |\
40
IORING_FEAT_POLL_32BITS |\
41
IORING_FEAT_SQPOLL_NONFIXED |\
42
IORING_FEAT_EXT_ARG |\
43
IORING_FEAT_NATIVE_WORKERS |\
44
IORING_FEAT_RSRC_TAGS |\
45
IORING_FEAT_CQE_SKIP |\
46
IORING_FEAT_LINKED_FILE |\
47
IORING_FEAT_REG_REG_RING |\
48
IORING_FEAT_RECVSEND_BUNDLE |\
49
IORING_FEAT_MIN_TIMEOUT |\
50
IORING_FEAT_RW_ATTR |\
51
IORING_FEAT_NO_IOWAIT)
52
53
#define IORING_SETUP_FLAGS (IORING_SETUP_IOPOLL |\
54
IORING_SETUP_SQPOLL |\
55
IORING_SETUP_SQ_AFF |\
56
IORING_SETUP_CQSIZE |\
57
IORING_SETUP_CLAMP |\
58
IORING_SETUP_ATTACH_WQ |\
59
IORING_SETUP_R_DISABLED |\
60
IORING_SETUP_SUBMIT_ALL |\
61
IORING_SETUP_COOP_TASKRUN |\
62
IORING_SETUP_TASKRUN_FLAG |\
63
IORING_SETUP_SQE128 |\
64
IORING_SETUP_CQE32 |\
65
IORING_SETUP_SINGLE_ISSUER |\
66
IORING_SETUP_DEFER_TASKRUN |\
67
IORING_SETUP_NO_MMAP |\
68
IORING_SETUP_REGISTERED_FD_ONLY |\
69
IORING_SETUP_NO_SQARRAY |\
70
IORING_SETUP_HYBRID_IOPOLL |\
71
IORING_SETUP_CQE_MIXED |\
72
IORING_SETUP_SQE_MIXED)
73
74
#define IORING_ENTER_FLAGS (IORING_ENTER_GETEVENTS |\
75
IORING_ENTER_SQ_WAKEUP |\
76
IORING_ENTER_SQ_WAIT |\
77
IORING_ENTER_EXT_ARG |\
78
IORING_ENTER_REGISTERED_RING |\
79
IORING_ENTER_ABS_TIMER |\
80
IORING_ENTER_EXT_ARG_REG |\
81
IORING_ENTER_NO_IOWAIT)
82
83
84
#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE |\
85
IOSQE_IO_DRAIN |\
86
IOSQE_IO_LINK |\
87
IOSQE_IO_HARDLINK |\
88
IOSQE_ASYNC |\
89
IOSQE_BUFFER_SELECT |\
90
IOSQE_CQE_SKIP_SUCCESS)
91
92
enum {
93
IOU_COMPLETE = 0,
94
95
IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED,
96
97
/*
98
* The request has more work to do and should be retried. io_uring will
99
* attempt to wait on the file for eligible opcodes, but otherwise
100
* it'll be handed to iowq for blocking execution. It works for normal
101
* requests as well as for the multi shot mode.
102
*/
103
IOU_RETRY = -EAGAIN,
104
105
/*
106
* Requeue the task_work to restart operations on this request. The
107
* actual value isn't important, should just be not an otherwise
108
* valid error code, yet less than -MAX_ERRNO and valid internally.
109
*/
110
IOU_REQUEUE = -3072,
111
};
112
113
struct io_defer_entry {
114
struct list_head list;
115
struct io_kiocb *req;
116
};
117
118
struct io_wait_queue {
119
struct wait_queue_entry wq;
120
struct io_ring_ctx *ctx;
121
unsigned cq_tail;
122
unsigned cq_min_tail;
123
unsigned nr_timeouts;
124
int hit_timeout;
125
ktime_t min_timeout;
126
ktime_t timeout;
127
struct hrtimer t;
128
129
#ifdef CONFIG_NET_RX_BUSY_POLL
130
ktime_t napi_busy_poll_dt;
131
bool napi_prefer_busy_poll;
132
#endif
133
};
134
135
static inline bool io_should_wake(struct io_wait_queue *iowq)
136
{
137
struct io_ring_ctx *ctx = iowq->ctx;
138
int dist = READ_ONCE(ctx->rings->cq.tail) - (int) iowq->cq_tail;
139
140
/*
141
* Wake up if we have enough events, or if a timeout occurred since we
142
* started waiting. For timeouts, we always want to return to userspace,
143
* regardless of event count.
144
*/
145
return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
146
}
147
148
#define IORING_MAX_ENTRIES 32768
149
#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
150
151
int io_prepare_config(struct io_ctx_config *config);
152
153
bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32);
154
int io_run_task_work_sig(struct io_ring_ctx *ctx);
155
int io_run_local_work(struct io_ring_ctx *ctx, int min_events, int max_events);
156
void io_req_defer_failed(struct io_kiocb *req, s32 res);
157
bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
158
void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
159
bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags);
160
bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe src_cqe[2]);
161
void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
162
163
unsigned io_linked_nr(struct io_kiocb *req);
164
void io_req_track_inflight(struct io_kiocb *req);
165
struct file *io_file_get_normal(struct io_kiocb *req, int fd);
166
struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
167
unsigned issue_flags);
168
169
void __io_req_task_work_add(struct io_kiocb *req, unsigned flags);
170
void io_req_task_work_add_remote(struct io_kiocb *req, unsigned flags);
171
void io_req_task_queue(struct io_kiocb *req);
172
void io_req_task_complete(struct io_tw_req tw_req, io_tw_token_t tw);
173
void io_req_task_queue_fail(struct io_kiocb *req, int ret);
174
void io_req_task_submit(struct io_tw_req tw_req, io_tw_token_t tw);
175
struct llist_node *io_handle_tw_list(struct llist_node *node, unsigned int *count, unsigned int max_entries);
176
struct llist_node *tctx_task_work_run(struct io_uring_task *tctx, unsigned int max_entries, unsigned int *count);
177
void tctx_task_work(struct callback_head *cb);
178
__cold void io_uring_drop_tctx_refs(struct task_struct *task);
179
180
int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file,
181
int start, int end);
182
void io_req_queue_iowq(struct io_kiocb *req);
183
184
int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw);
185
int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr);
186
int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
187
__cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx);
188
void __io_submit_flush_completions(struct io_ring_ctx *ctx);
189
190
struct io_wq_work *io_wq_free_work(struct io_wq_work *work);
191
void io_wq_submit_work(struct io_wq_work *work);
192
193
void io_free_req(struct io_kiocb *req);
194
void io_queue_next(struct io_kiocb *req);
195
void io_task_refs_refill(struct io_uring_task *tctx);
196
bool __io_alloc_req_refill(struct io_ring_ctx *ctx);
197
198
void io_activate_pollwq(struct io_ring_ctx *ctx);
199
200
static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
201
{
202
#if defined(CONFIG_PROVE_LOCKING)
203
lockdep_assert(in_task());
204
205
if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
206
lockdep_assert_held(&ctx->uring_lock);
207
208
if (ctx->flags & IORING_SETUP_IOPOLL) {
209
lockdep_assert_held(&ctx->uring_lock);
210
} else if (!ctx->task_complete) {
211
lockdep_assert_held(&ctx->completion_lock);
212
} else if (ctx->submitter_task) {
213
/*
214
* ->submitter_task may be NULL and we can still post a CQE,
215
* if the ring has been setup with IORING_SETUP_R_DISABLED.
216
* Not from an SQE, as those cannot be submitted, but via
217
* updating tagged resources.
218
*/
219
if (!percpu_ref_is_dying(&ctx->refs))
220
lockdep_assert(current == ctx->submitter_task);
221
}
222
#endif
223
}
224
225
static inline bool io_is_compat(struct io_ring_ctx *ctx)
226
{
227
return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->compat);
228
}
229
230
static inline void io_req_task_work_add(struct io_kiocb *req)
231
{
232
__io_req_task_work_add(req, 0);
233
}
234
235
static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
236
{
237
if (!wq_list_empty(&ctx->submit_state.compl_reqs) ||
238
ctx->submit_state.cq_flush)
239
__io_submit_flush_completions(ctx);
240
}
241
242
#define io_for_each_link(pos, head) \
243
for (pos = (head); pos; pos = pos->link)
244
245
static inline bool io_get_cqe_overflow(struct io_ring_ctx *ctx,
246
struct io_uring_cqe **ret,
247
bool overflow, bool cqe32)
248
{
249
io_lockdep_assert_cq_locked(ctx);
250
251
if (unlikely(ctx->cqe_sentinel - ctx->cqe_cached < (cqe32 + 1))) {
252
if (unlikely(!io_cqe_cache_refill(ctx, overflow, cqe32)))
253
return false;
254
}
255
*ret = ctx->cqe_cached;
256
ctx->cached_cq_tail++;
257
ctx->cqe_cached++;
258
if (ctx->flags & IORING_SETUP_CQE32) {
259
ctx->cqe_cached++;
260
} else if (cqe32 && ctx->flags & IORING_SETUP_CQE_MIXED) {
261
ctx->cqe_cached++;
262
ctx->cached_cq_tail++;
263
}
264
WARN_ON_ONCE(ctx->cqe_cached > ctx->cqe_sentinel);
265
return true;
266
}
267
268
static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **ret,
269
bool cqe32)
270
{
271
return io_get_cqe_overflow(ctx, ret, false, cqe32);
272
}
273
274
static inline bool io_defer_get_uncommited_cqe(struct io_ring_ctx *ctx,
275
struct io_uring_cqe **cqe_ret)
276
{
277
io_lockdep_assert_cq_locked(ctx);
278
279
ctx->submit_state.cq_flush = true;
280
return io_get_cqe(ctx, cqe_ret, ctx->flags & IORING_SETUP_CQE_MIXED);
281
}
282
283
static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,
284
struct io_kiocb *req)
285
{
286
bool is_cqe32 = req->cqe.flags & IORING_CQE_F_32;
287
struct io_uring_cqe *cqe;
288
289
/*
290
* If we can't get a cq entry, userspace overflowed the submission
291
* (by quite a lot).
292
*/
293
if (unlikely(!io_get_cqe(ctx, &cqe, is_cqe32)))
294
return false;
295
296
memcpy(cqe, &req->cqe, sizeof(*cqe));
297
if (ctx->flags & IORING_SETUP_CQE32 || is_cqe32) {
298
memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe));
299
memset(&req->big_cqe, 0, sizeof(req->big_cqe));
300
}
301
302
if (trace_io_uring_complete_enabled())
303
trace_io_uring_complete(req->ctx, req, cqe);
304
return true;
305
}
306
307
static inline void req_set_fail(struct io_kiocb *req)
308
{
309
req->flags |= REQ_F_FAIL;
310
if (req->flags & REQ_F_CQE_SKIP) {
311
req->flags &= ~REQ_F_CQE_SKIP;
312
req->flags |= REQ_F_SKIP_LINK_CQES;
313
}
314
}
315
316
static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
317
{
318
req->cqe.res = res;
319
req->cqe.flags = cflags;
320
}
321
322
static inline u32 ctx_cqe32_flags(struct io_ring_ctx *ctx)
323
{
324
if (ctx->flags & IORING_SETUP_CQE_MIXED)
325
return IORING_CQE_F_32;
326
return 0;
327
}
328
329
static inline void io_req_set_res32(struct io_kiocb *req, s32 res, u32 cflags,
330
__u64 extra1, __u64 extra2)
331
{
332
req->cqe.res = res;
333
req->cqe.flags = cflags | ctx_cqe32_flags(req->ctx);
334
req->big_cqe.extra1 = extra1;
335
req->big_cqe.extra2 = extra2;
336
}
337
338
static inline void *io_uring_alloc_async_data(struct io_alloc_cache *cache,
339
struct io_kiocb *req)
340
{
341
if (cache) {
342
req->async_data = io_cache_alloc(cache, GFP_KERNEL);
343
} else {
344
const struct io_issue_def *def = &io_issue_defs[req->opcode];
345
346
WARN_ON_ONCE(!def->async_size);
347
req->async_data = kmalloc(def->async_size, GFP_KERNEL);
348
}
349
if (req->async_data)
350
req->flags |= REQ_F_ASYNC_DATA;
351
return req->async_data;
352
}
353
354
static inline bool req_has_async_data(struct io_kiocb *req)
355
{
356
return req->flags & REQ_F_ASYNC_DATA;
357
}
358
359
static inline void io_req_async_data_clear(struct io_kiocb *req,
360
io_req_flags_t extra_flags)
361
{
362
req->flags &= ~(REQ_F_ASYNC_DATA|extra_flags);
363
req->async_data = NULL;
364
}
365
366
static inline void io_req_async_data_free(struct io_kiocb *req)
367
{
368
kfree(req->async_data);
369
io_req_async_data_clear(req, 0);
370
}
371
372
static inline void io_put_file(struct io_kiocb *req)
373
{
374
if (!(req->flags & REQ_F_FIXED_FILE) && req->file)
375
fput(req->file);
376
}
377
378
static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx,
379
unsigned issue_flags)
380
{
381
lockdep_assert_held(&ctx->uring_lock);
382
if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
383
mutex_unlock(&ctx->uring_lock);
384
}
385
386
static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,
387
unsigned issue_flags)
388
{
389
/*
390
* "Normal" inline submissions always hold the uring_lock, since we
391
* grab it from the system call. Same is true for the SQPOLL offload.
392
* The only exception is when we've detached the request and issue it
393
* from an async worker thread, grab the lock for that case.
394
*/
395
if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
396
mutex_lock(&ctx->uring_lock);
397
lockdep_assert_held(&ctx->uring_lock);
398
}
399
400
static inline void io_commit_cqring(struct io_ring_ctx *ctx)
401
{
402
/* order cqe stores with ring update */
403
smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
404
}
405
406
static inline void __io_wq_wake(struct wait_queue_head *wq)
407
{
408
/*
409
*
410
* Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter
411
* set in the mask so that if we recurse back into our own poll
412
* waitqueue handlers, we know we have a dependency between eventfd or
413
* epoll and should terminate multishot poll at that point.
414
*/
415
if (wq_has_sleeper(wq))
416
__wake_up(wq, TASK_NORMAL, 0, poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
417
}
418
419
static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
420
{
421
__io_wq_wake(&ctx->poll_wq);
422
}
423
424
static inline void io_cqring_wake(struct io_ring_ctx *ctx)
425
{
426
/*
427
* Trigger waitqueue handler on all waiters on our waitqueue. This
428
* won't necessarily wake up all the tasks, io_should_wake() will make
429
* that decision.
430
*/
431
432
__io_wq_wake(&ctx->cq_wait);
433
}
434
435
static inline bool io_sqring_full(struct io_ring_ctx *ctx)
436
{
437
struct io_rings *r = ctx->rings;
438
439
/*
440
* SQPOLL must use the actual sqring head, as using the cached_sq_head
441
* is race prone if the SQPOLL thread has grabbed entries but not yet
442
* committed them to the ring. For !SQPOLL, this doesn't matter, but
443
* since this helper is just used for SQPOLL sqring waits (or POLLOUT),
444
* just read the actual sqring head unconditionally.
445
*/
446
return READ_ONCE(r->sq.tail) - READ_ONCE(r->sq.head) == ctx->sq_entries;
447
}
448
449
static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
450
{
451
struct io_rings *rings = ctx->rings;
452
unsigned int entries;
453
454
/* make sure SQ entry isn't read before tail */
455
entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
456
return min(entries, ctx->sq_entries);
457
}
458
459
static inline int io_run_task_work(void)
460
{
461
bool ret = false;
462
463
/*
464
* Always check-and-clear the task_work notification signal. With how
465
* signaling works for task_work, we can find it set with nothing to
466
* run. We need to clear it for that case, like get_signal() does.
467
*/
468
if (test_thread_flag(TIF_NOTIFY_SIGNAL))
469
clear_notify_signal();
470
/*
471
* PF_IO_WORKER never returns to userspace, so check here if we have
472
* notify work that needs processing.
473
*/
474
if (current->flags & PF_IO_WORKER) {
475
if (test_thread_flag(TIF_NOTIFY_RESUME)) {
476
__set_current_state(TASK_RUNNING);
477
resume_user_mode_work(NULL);
478
}
479
if (current->io_uring) {
480
unsigned int count = 0;
481
482
__set_current_state(TASK_RUNNING);
483
tctx_task_work_run(current->io_uring, UINT_MAX, &count);
484
if (count)
485
ret = true;
486
}
487
}
488
if (task_work_pending(current)) {
489
__set_current_state(TASK_RUNNING);
490
task_work_run();
491
ret = true;
492
}
493
494
return ret;
495
}
496
497
static inline bool io_local_work_pending(struct io_ring_ctx *ctx)
498
{
499
return !llist_empty(&ctx->work_llist) || !llist_empty(&ctx->retry_llist);
500
}
501
502
static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
503
{
504
return task_work_pending(current) || io_local_work_pending(ctx);
505
}
506
507
static inline void io_tw_lock(struct io_ring_ctx *ctx, io_tw_token_t tw)
508
{
509
lockdep_assert_held(&ctx->uring_lock);
510
}
511
512
/*
513
* Don't complete immediately but use deferred completion infrastructure.
514
* Protected by ->uring_lock and can only be used either with
515
* IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex.
516
*/
517
static inline void io_req_complete_defer(struct io_kiocb *req)
518
__must_hold(&req->ctx->uring_lock)
519
{
520
struct io_submit_state *state = &req->ctx->submit_state;
521
522
lockdep_assert_held(&req->ctx->uring_lock);
523
524
wq_list_add_tail(&req->comp_list, &state->compl_reqs);
525
}
526
527
static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
528
{
529
if (unlikely(ctx->off_timeout_used ||
530
ctx->has_evfd || ctx->poll_activated))
531
__io_commit_cqring_flush(ctx);
532
}
533
534
static inline void io_get_task_refs(int nr)
535
{
536
struct io_uring_task *tctx = current->io_uring;
537
538
tctx->cached_refs -= nr;
539
if (unlikely(tctx->cached_refs < 0))
540
io_task_refs_refill(tctx);
541
}
542
543
static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
544
{
545
return !ctx->submit_state.free_list.next;
546
}
547
548
extern struct kmem_cache *req_cachep;
549
550
static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx)
551
{
552
struct io_kiocb *req;
553
554
req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list);
555
wq_stack_extract(&ctx->submit_state.free_list);
556
return req;
557
}
558
559
static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req)
560
{
561
if (unlikely(io_req_cache_empty(ctx))) {
562
if (!__io_alloc_req_refill(ctx))
563
return false;
564
}
565
*req = io_extract_req(ctx);
566
return true;
567
}
568
569
static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
570
{
571
return likely(ctx->submitter_task == current);
572
}
573
574
static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
575
{
576
return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
577
ctx->submitter_task == current);
578
}
579
580
static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
581
{
582
io_req_set_res(req, res, 0);
583
req->io_task_work.func = io_req_task_complete;
584
io_req_task_work_add(req);
585
}
586
587
static inline bool io_file_can_poll(struct io_kiocb *req)
588
{
589
if (req->flags & REQ_F_CAN_POLL)
590
return true;
591
if (req->file && file_can_poll(req->file)) {
592
req->flags |= REQ_F_CAN_POLL;
593
return true;
594
}
595
return false;
596
}
597
598
static inline ktime_t io_get_time(struct io_ring_ctx *ctx)
599
{
600
if (ctx->clockid == CLOCK_MONOTONIC)
601
return ktime_get();
602
603
return ktime_get_with_offset(ctx->clock_offset);
604
}
605
606
enum {
607
IO_CHECK_CQ_OVERFLOW_BIT,
608
IO_CHECK_CQ_DROPPED_BIT,
609
};
610
611
static inline bool io_has_work(struct io_ring_ctx *ctx)
612
{
613
return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) ||
614
io_local_work_pending(ctx);
615
}
616
#endif
617
618