1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // io.cfa -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Thu Apr 23 17:31:00 2020 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | #define _GNU_SOURCE |
---|
18 | |
---|
19 | #if defined(__CFA_DEBUG__) |
---|
20 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
21 | // #define __CFA_DEBUG_PRINT_IO_CORE__ |
---|
22 | #endif |
---|
23 | |
---|
24 | |
---|
25 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
26 | #include <errno.h> |
---|
27 | #include <signal.h> |
---|
28 | #include <stdint.h> |
---|
29 | #include <string.h> |
---|
30 | #include <unistd.h> |
---|
31 | |
---|
32 | extern "C" { |
---|
33 | #include <sys/syscall.h> |
---|
34 | #include <sys/eventfd.h> |
---|
35 | #include <sys/uio.h> |
---|
36 | |
---|
37 | #include <linux/io_uring.h> |
---|
38 | } |
---|
39 | |
---|
40 | #include "stats.hfa" |
---|
41 | #include "kernel.hfa" |
---|
42 | #include "kernel/fwd.hfa" |
---|
43 | #include "kernel/private.hfa" |
---|
44 | #include "kernel/cluster.hfa" |
---|
45 | #include "io/types.hfa" |
---|
46 | |
---|
47 | __attribute__((unused)) static const char * opcodes[] = { |
---|
48 | "OP_NOP", |
---|
49 | "OP_READV", |
---|
50 | "OP_WRITEV", |
---|
51 | "OP_FSYNC", |
---|
52 | "OP_READ_FIXED", |
---|
53 | "OP_WRITE_FIXED", |
---|
54 | "OP_POLL_ADD", |
---|
55 | "OP_POLL_REMOVE", |
---|
56 | "OP_SYNC_FILE_RANGE", |
---|
57 | "OP_SENDMSG", |
---|
58 | "OP_RECVMSG", |
---|
59 | "OP_TIMEOUT", |
---|
60 | "OP_TIMEOUT_REMOVE", |
---|
61 | "OP_ACCEPT", |
---|
62 | "OP_ASYNC_CANCEL", |
---|
63 | "OP_LINK_TIMEOUT", |
---|
64 | "OP_CONNECT", |
---|
65 | "OP_FALLOCATE", |
---|
66 | "OP_OPENAT", |
---|
67 | "OP_CLOSE", |
---|
68 | "OP_FILES_UPDATE", |
---|
69 | "OP_STATX", |
---|
70 | "OP_READ", |
---|
71 | "OP_WRITE", |
---|
72 | "OP_FADVISE", |
---|
73 | "OP_MADVISE", |
---|
74 | "OP_SEND", |
---|
75 | "OP_RECV", |
---|
76 | "OP_OPENAT2", |
---|
77 | "OP_EPOLL_CTL", |
---|
78 | "OP_SPLICE", |
---|
79 | "OP_PROVIDE_BUFFERS", |
---|
80 | "OP_REMOVE_BUFFERS", |
---|
81 | "OP_TEE", |
---|
82 | "INVALID_OP" |
---|
83 | }; |
---|
84 | |
---|
85 | static io_context$ * __ioarbiter_allocate( io_arbiter$ & this, __u32 idxs[], __u32 want ); |
---|
86 | static void __ioarbiter_submit( io_context$ * , __u32 idxs[], __u32 have, bool lazy ); |
---|
87 | static void __ioarbiter_flush ( io_context$ & ); |
---|
88 | static inline void __ioarbiter_notify( io_context$ & ctx ); |
---|
89 | //============================================================================================= |
---|
90 | // I/O Polling |
---|
91 | //============================================================================================= |
---|
92 | static inline unsigned __flush( struct io_context$ & ); |
---|
93 | static inline __u32 __release_sqes( struct io_context$ & ); |
---|
94 | extern void __kernel_unpark( thread$ * thrd, unpark_hint ); |
---|
95 | |
---|
96 | static void ioring_syscsll( struct io_context$ & ctx, unsigned int min_comp, unsigned int flags ) { |
---|
97 | __STATS__( true, io.calls.flush++; ) |
---|
98 | int ret; |
---|
99 | for() { |
---|
100 | ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, min_comp, flags, (sigset_t *)0p, _NSIG / 8); |
---|
101 | if( ret < 0 ) { |
---|
102 | switch((int)errno) { |
---|
103 | case EINTR: |
---|
104 | continue; |
---|
105 | case EAGAIN: |
---|
106 | case EBUSY: |
---|
107 | // Update statistics |
---|
108 | __STATS__( false, io.calls.errors.busy ++; ) |
---|
109 | return false; |
---|
110 | default: |
---|
111 | abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) ); |
---|
112 | } |
---|
113 | } |
---|
114 | break; |
---|
115 | } |
---|
116 | |
---|
117 | __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd); |
---|
118 | __STATS__( true, io.calls.submitted += ret; ) |
---|
119 | /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num ); |
---|
120 | /* paranoid */ verify( ctx.sq.to_submit >= ret ); |
---|
121 | |
---|
122 | ctx.sq.to_submit -= ret; |
---|
123 | |
---|
124 | /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num ); |
---|
125 | |
---|
126 | // Release the consumed SQEs |
---|
127 | __release_sqes( ctx ); |
---|
128 | |
---|
129 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
130 | |
---|
131 | __atomic_store_n(&ctx.proc->io.pending, false, __ATOMIC_RELAXED); |
---|
132 | } |
---|
133 | |
---|
134 | static bool try_acquire( io_context$ * ctx ) __attribute__((nonnull(1))) { |
---|
135 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
136 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
137 | |
---|
138 | |
---|
139 | { |
---|
140 | const __u32 head = *ctx->cq.head; |
---|
141 | const __u32 tail = *ctx->cq.tail; |
---|
142 | |
---|
143 | if(head == tail) return false; |
---|
144 | } |
---|
145 | |
---|
146 | // Drain the queue |
---|
147 | if(!__atomic_try_acquire(&ctx->cq.lock)) { |
---|
148 | __STATS__( false, io.calls.locked++; ) |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | return true; |
---|
153 | } |
---|
154 | |
---|
155 | static bool __cfa_do_drain( io_context$ * ctx, cluster * cltr ) __attribute__((nonnull(1, 2))) { |
---|
156 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
157 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
158 | /* paranoid */ verify( ctx->cq.lock == true ); |
---|
159 | |
---|
160 | const __u32 mask = *ctx->cq.mask; |
---|
161 | const __u32 num = *ctx->cq.num; |
---|
162 | unsigned long long ts_prev = ctx->cq.ts; |
---|
163 | unsigned long long ts_next; |
---|
164 | |
---|
165 | // We might need to do this multiple times if more events completed than can fit in the queue. |
---|
166 | for() { |
---|
167 | // re-read the head and tail in case it already changed. |
---|
168 | const __u32 head = *ctx->cq.head; |
---|
169 | const __u32 tail = *ctx->cq.tail; |
---|
170 | const __u32 count = tail - head; |
---|
171 | __STATS__( false, io.calls.drain++; io.calls.completed += count; ) |
---|
172 | |
---|
173 | for(i; count) { |
---|
174 | unsigned idx = (head + i) & mask; |
---|
175 | volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx]; |
---|
176 | |
---|
177 | /* paranoid */ verify(&cqe); |
---|
178 | |
---|
179 | struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data; |
---|
180 | // __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future ); |
---|
181 | |
---|
182 | __kernel_unpark( fulfil( *future, cqe.res, false ), UNPARK_LOCAL ); |
---|
183 | } |
---|
184 | |
---|
185 | ts_next = ctx->cq.ts = rdtscl(); |
---|
186 | |
---|
187 | // Mark to the kernel that the cqe has been seen |
---|
188 | // Ensure that the kernel only sees the new value of the head index after the CQEs have been read. |
---|
189 | __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST ); |
---|
190 | ctx->proc->idle_wctx.drain_time = ts_next; |
---|
191 | |
---|
192 | if(likely(count < num)) break; |
---|
193 | |
---|
194 | ioring_syscsll( *ctx, 0, IORING_ENTER_GETEVENTS); |
---|
195 | } |
---|
196 | |
---|
197 | __cfadbg_print_safe(io, "Kernel I/O : %u completed age %llu\n", count, ts_next); |
---|
198 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
199 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
200 | |
---|
201 | __atomic_unlock(&ctx->cq.lock); |
---|
202 | |
---|
203 | touch_tsc( cltr->sched.io.tscs, ctx->cq.id, ts_prev, ts_next ); |
---|
204 | |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | bool __cfa_io_drain( processor * proc ) { |
---|
209 | bool local = false; |
---|
210 | bool remote = false; |
---|
211 | |
---|
212 | ready_schedule_lock(); |
---|
213 | |
---|
214 | cluster * const cltr = proc->cltr; |
---|
215 | io_context$ * const ctx = proc->io.ctx; |
---|
216 | /* paranoid */ verify( cltr ); |
---|
217 | /* paranoid */ verify( ctx ); |
---|
218 | |
---|
219 | with(cltr->sched) { |
---|
220 | const size_t ctxs_count = io.count; |
---|
221 | |
---|
222 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
223 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
224 | /* paranoid */ verify( active_processor() == proc ); |
---|
225 | /* paranoid */ verify( __shard_factor.io > 0 ); |
---|
226 | /* paranoid */ verify( ctxs_count > 0 ); |
---|
227 | /* paranoid */ verify( ctx->cq.id < ctxs_count ); |
---|
228 | |
---|
229 | const unsigned this_cache = cache_id(cltr, ctx->cq.id / __shard_factor.io); |
---|
230 | const unsigned long long ctsc = rdtscl(); |
---|
231 | |
---|
232 | if(proc->io.target == UINT_MAX) { |
---|
233 | uint64_t chaos = __tls_rand(); |
---|
234 | unsigned ext = chaos & 0xff; |
---|
235 | unsigned other = (chaos >> 8) % (ctxs_count); |
---|
236 | |
---|
237 | if(ext < 3 || __atomic_load_n(&caches[other / __shard_factor.io].id, __ATOMIC_RELAXED) == this_cache) { |
---|
238 | proc->io.target = other; |
---|
239 | } |
---|
240 | } |
---|
241 | else { |
---|
242 | const unsigned target = proc->io.target; |
---|
243 | /* paranoid */ verify( io.tscs[target].t.tv != ULLONG_MAX ); |
---|
244 | HELP: if(target < ctxs_count) { |
---|
245 | const unsigned long long cutoff = calc_cutoff(ctsc, ctx->cq.id, ctxs_count, io.data, io.tscs, __shard_factor.io); |
---|
246 | const unsigned long long age = moving_average(ctsc, io.tscs[target].t.tv, io.tscs[target].t.ma); |
---|
247 | __cfadbg_print_safe(io, "Kernel I/O: Help attempt on %u from %u, age %'llu vs cutoff %'llu, %s\n", target, ctx->cq.id, age, cutoff, age > cutoff ? "yes" : "no"); |
---|
248 | if(age <= cutoff) break HELP; |
---|
249 | |
---|
250 | if(!try_acquire(io.data[target])) break HELP; |
---|
251 | |
---|
252 | if(!__cfa_do_drain( io.data[target], cltr )) break HELP; |
---|
253 | |
---|
254 | remote = true; |
---|
255 | __STATS__( true, io.calls.helped++; ) |
---|
256 | } |
---|
257 | proc->io.target = UINT_MAX; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | // Drain the local queue |
---|
263 | if(try_acquire( proc->io.ctx )) { |
---|
264 | local = __cfa_do_drain( proc->io.ctx, cltr ); |
---|
265 | } |
---|
266 | |
---|
267 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
268 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
269 | /* paranoid */ verify( active_processor() == proc ); |
---|
270 | |
---|
271 | ready_schedule_unlock(); |
---|
272 | return local || remote; |
---|
273 | } |
---|
274 | |
---|
275 | bool __cfa_io_flush( processor * proc ) { |
---|
276 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
277 | /* paranoid */ verify( proc ); |
---|
278 | /* paranoid */ verify( proc->io.ctx ); |
---|
279 | |
---|
280 | io_context$ & ctx = *proc->io.ctx; |
---|
281 | |
---|
282 | __ioarbiter_flush( ctx ); |
---|
283 | |
---|
284 | if(ctx.sq.to_submit != 0) { |
---|
285 | ioring_syscsll(ctx, 0, 0); |
---|
286 | |
---|
287 | } |
---|
288 | |
---|
289 | return __cfa_io_drain( proc ); |
---|
290 | } |
---|
291 | |
---|
292 | //============================================================================================= |
---|
293 | // I/O Submissions |
---|
294 | //============================================================================================= |
---|
295 | |
---|
296 | // Submition steps : |
---|
297 | // 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones |
---|
298 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not |
---|
299 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we |
---|
300 | // need to write an allocator that allows allocating concurrently. |
---|
301 | // |
---|
302 | // 2 - Actually fill the submit entry, this is the only simple and straightforward step. |
---|
303 | // |
---|
304 | // 3 - Append the entry index to the array and adjust the tail accordingly. This operation |
---|
305 | // needs to arrive to two concensus at the same time: |
---|
306 | // A - The order in which entries are listed in the array: no two threads must pick the |
---|
307 | // same index for their entries |
---|
308 | // B - When can the tail be update for the kernel. EVERY entries in the array between |
---|
309 | // head and tail must be fully filled and shouldn't ever be touched again. |
---|
310 | // |
---|
311 | //============================================================================================= |
---|
312 | // Allocation |
---|
313 | // for user's convenience fill the sqes from the indexes |
---|
314 | static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct io_context$ * ctx) { |
---|
315 | struct io_uring_sqe * sqes = ctx->sq.sqes; |
---|
316 | for(i; want) { |
---|
317 | // __cfadbg_print_safe(io, "Kernel I/O : filling loop\n"); |
---|
318 | out_sqes[i] = &sqes[idxs[i]]; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | // Try to directly allocate from the a given context |
---|
323 | // Not thread-safe |
---|
324 | static inline bool __alloc(struct io_context$ * ctx, __u32 idxs[], __u32 want) { |
---|
325 | __sub_ring_t & sq = ctx->sq; |
---|
326 | const __u32 mask = *sq.mask; |
---|
327 | __u32 fhead = sq.free_ring.head; // get the current head of the queue |
---|
328 | __u32 ftail = sq.free_ring.tail; // get the current tail of the queue |
---|
329 | |
---|
330 | // If we don't have enough sqes, fail |
---|
331 | if((ftail - fhead) < want) { return false; } |
---|
332 | |
---|
333 | // copy all the indexes we want from the available list |
---|
334 | for(i; want) { |
---|
335 | // __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n"); |
---|
336 | idxs[i] = sq.free_ring.array[(fhead + i) & mask]; |
---|
337 | } |
---|
338 | |
---|
339 | // Advance the head to mark the indexes as consumed |
---|
340 | __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE); |
---|
341 | |
---|
342 | // return success |
---|
343 | return true; |
---|
344 | } |
---|
345 | |
---|
346 | // Allocate an submit queue entry. |
---|
347 | // The kernel cannot see these entries until they are submitted, but other threads must be |
---|
348 | // able to see which entries can be used and which are already un used by an other thread |
---|
349 | // for convenience, return both the index and the pointer to the sqe |
---|
350 | // sqe == &sqes[idx] |
---|
351 | struct io_context$ * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) libcfa_public { |
---|
352 | // __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want); |
---|
353 | |
---|
354 | disable_interrupts(); |
---|
355 | processor * proc = __cfaabi_tls.this_processor; |
---|
356 | io_context$ * ctx = proc->io.ctx; |
---|
357 | /* paranoid */ verify( __cfaabi_tls.this_processor ); |
---|
358 | /* paranoid */ verify( ctx ); |
---|
359 | |
---|
360 | // __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n"); |
---|
361 | |
---|
362 | // We can proceed to the fast path |
---|
363 | if( __alloc(ctx, idxs, want) ) { |
---|
364 | // Allocation was successful |
---|
365 | __STATS__( true, io.alloc.fast += 1; ) |
---|
366 | enable_interrupts(); |
---|
367 | |
---|
368 | // __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd); |
---|
369 | |
---|
370 | __fill( sqes, want, idxs, ctx ); |
---|
371 | return ctx; |
---|
372 | } |
---|
373 | // The fast path failed, fallback |
---|
374 | __STATS__( true, io.alloc.fail += 1; ) |
---|
375 | |
---|
376 | // Fast path failed, fallback on arbitration |
---|
377 | __STATS__( true, io.alloc.slow += 1; ) |
---|
378 | enable_interrupts(); |
---|
379 | |
---|
380 | io_arbiter$ * ioarb = proc->cltr->io.arbiter; |
---|
381 | /* paranoid */ verify( ioarb ); |
---|
382 | |
---|
383 | // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n"); |
---|
384 | |
---|
385 | struct io_context$ * ret = __ioarbiter_allocate(*ioarb, idxs, want); |
---|
386 | |
---|
387 | // __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd); |
---|
388 | |
---|
389 | __fill( sqes, want, idxs,ret ); |
---|
390 | return ret; |
---|
391 | } |
---|
392 | |
---|
393 | //============================================================================================= |
---|
394 | // submission |
---|
395 | static inline void __submit_only( struct io_context$ * ctx, __u32 idxs[], __u32 have) { |
---|
396 | // We can proceed to the fast path |
---|
397 | // Get the right objects |
---|
398 | __sub_ring_t & sq = ctx->sq; |
---|
399 | const __u32 mask = *sq.mask; |
---|
400 | __u32 tail = *sq.kring.tail; |
---|
401 | |
---|
402 | // Add the sqes to the array |
---|
403 | for( i; have ) { |
---|
404 | // __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n"); |
---|
405 | sq.kring.array[ (tail + i) & mask ] = idxs[i]; |
---|
406 | } |
---|
407 | |
---|
408 | // Make the sqes visible to the submitter |
---|
409 | __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE); |
---|
410 | sq.to_submit += have; |
---|
411 | |
---|
412 | __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_RELAXED); |
---|
413 | __atomic_store_n(&ctx->proc->io.dirty , true, __ATOMIC_RELAXED); |
---|
414 | } |
---|
415 | |
---|
416 | static inline void __submit( struct io_context$ * ctx, __u32 idxs[], __u32 have, bool lazy) { |
---|
417 | __sub_ring_t & sq = ctx->sq; |
---|
418 | __submit_only(ctx, idxs, have); |
---|
419 | |
---|
420 | if(sq.to_submit > 30) { |
---|
421 | __tls_stats()->io.flush.full++; |
---|
422 | __cfa_io_flush( ctx->proc ); |
---|
423 | } |
---|
424 | if(!lazy) { |
---|
425 | __tls_stats()->io.flush.eager++; |
---|
426 | __cfa_io_flush( ctx->proc ); |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | void cfa_io_submit( struct io_context$ * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) libcfa_public { |
---|
431 | // __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager"); |
---|
432 | |
---|
433 | disable_interrupts(); |
---|
434 | __STATS__( true, if(!lazy) io.submit.eagr += 1; ) |
---|
435 | processor * proc = __cfaabi_tls.this_processor; |
---|
436 | io_context$ * ctx = proc->io.ctx; |
---|
437 | /* paranoid */ verify( __cfaabi_tls.this_processor ); |
---|
438 | /* paranoid */ verify( ctx ); |
---|
439 | |
---|
440 | // Can we proceed to the fast path |
---|
441 | if( ctx == inctx ) // We have the right instance? |
---|
442 | { |
---|
443 | __submit(ctx, idxs, have, lazy); |
---|
444 | |
---|
445 | // Mark the instance as no longer in-use, re-enable interrupts and return |
---|
446 | __STATS__( true, io.submit.fast += 1; ) |
---|
447 | enable_interrupts(); |
---|
448 | |
---|
449 | // __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n"); |
---|
450 | return; |
---|
451 | } |
---|
452 | |
---|
453 | // Fast path failed, fallback on arbitration |
---|
454 | __STATS__( true, io.submit.slow += 1; ) |
---|
455 | enable_interrupts(); |
---|
456 | |
---|
457 | // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n"); |
---|
458 | |
---|
459 | __ioarbiter_submit(inctx, idxs, have, lazy); |
---|
460 | } |
---|
461 | |
---|
462 | //============================================================================================= |
---|
463 | // Flushing |
---|
464 | // Go through the ring's submit queue and release everything that has already been consumed |
---|
465 | // by io_uring |
---|
466 | // This cannot be done by multiple threads |
---|
467 | static __u32 __release_sqes( struct io_context$ & ctx ) { |
---|
468 | const __u32 mask = *ctx.sq.mask; |
---|
469 | |
---|
470 | __attribute__((unused)) |
---|
471 | __u32 ctail = *ctx.sq.kring.tail; // get the current tail of the queue |
---|
472 | __u32 chead = *ctx.sq.kring.head; // get the current head of the queue |
---|
473 | __u32 phead = ctx.sq.kring.released; // get the head the last time we were here |
---|
474 | |
---|
475 | __u32 ftail = ctx.sq.free_ring.tail; // get the current tail of the queue |
---|
476 | |
---|
477 | // the 3 fields are organized like this diagram |
---|
478 | // except it's are ring |
---|
479 | // ---+--------+--------+---- |
---|
480 | // ---+--------+--------+---- |
---|
481 | // ^ ^ ^ |
---|
482 | // phead chead ctail |
---|
483 | |
---|
484 | // make sure ctail doesn't wrap around and reach phead |
---|
485 | /* paranoid */ verify( |
---|
486 | (ctail >= chead && chead >= phead) |
---|
487 | || (chead >= phead && phead >= ctail) |
---|
488 | || (phead >= ctail && ctail >= chead) |
---|
489 | ); |
---|
490 | |
---|
491 | // find the range we need to clear |
---|
492 | __u32 count = chead - phead; |
---|
493 | |
---|
494 | if(count == 0) { |
---|
495 | return 0; |
---|
496 | } |
---|
497 | |
---|
498 | // We acquired an previous-head/current-head range |
---|
499 | // go through the range and release the sqes |
---|
500 | for( i; count ) { |
---|
501 | // __cfadbg_print_safe(io, "Kernel I/O : release loop\n"); |
---|
502 | __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ]; |
---|
503 | ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx; |
---|
504 | } |
---|
505 | |
---|
506 | ctx.sq.kring.released = chead; // note up to were we processed |
---|
507 | __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST); |
---|
508 | |
---|
509 | __ioarbiter_notify(ctx); |
---|
510 | |
---|
511 | return count; |
---|
512 | } |
---|
513 | |
---|
514 | //============================================================================================= |
---|
515 | // I/O Arbiter |
---|
516 | //============================================================================================= |
---|
517 | static inline bool enqueue(__outstanding_io_queue & queue, __outstanding_io & item) { |
---|
518 | bool was_empty; |
---|
519 | |
---|
520 | // Lock the list, it's not thread safe |
---|
521 | lock( queue.lock __cfaabi_dbg_ctx2 ); |
---|
522 | { |
---|
523 | was_empty = empty(queue.queue); |
---|
524 | |
---|
525 | // Add our request to the list |
---|
526 | add( queue.queue, item ); |
---|
527 | |
---|
528 | // Mark as pending |
---|
529 | __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST ); |
---|
530 | } |
---|
531 | unlock( queue.lock ); |
---|
532 | |
---|
533 | return was_empty; |
---|
534 | } |
---|
535 | |
---|
536 | static inline bool empty(__outstanding_io_queue & queue ) { |
---|
537 | return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST); |
---|
538 | } |
---|
539 | |
---|
540 | static io_context$ * __ioarbiter_allocate( io_arbiter$ & this, __u32 idxs[], __u32 want ) { |
---|
541 | // __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n"); |
---|
542 | |
---|
543 | __STATS__( false, io.alloc.block += 1; ) |
---|
544 | |
---|
545 | // No one has any resources left, wait for something to finish |
---|
546 | // We need to add ourself to a list of pending allocs and wait for an answer |
---|
547 | __pending_alloc pa; |
---|
548 | pa.idxs = idxs; |
---|
549 | pa.want = want; |
---|
550 | |
---|
551 | enqueue(this.pending, (__outstanding_io&)pa); |
---|
552 | |
---|
553 | wait( pa.sem ); |
---|
554 | |
---|
555 | return pa.ctx; |
---|
556 | |
---|
557 | } |
---|
558 | |
---|
559 | static void __ioarbiter_notify( io_arbiter$ & this, io_context$ * ctx ) { |
---|
560 | /* paranoid */ verify( !empty(this.pending.queue) ); |
---|
561 | |
---|
562 | lock( this.pending.lock __cfaabi_dbg_ctx2 ); |
---|
563 | { |
---|
564 | while( !empty(this.pending.queue) ) { |
---|
565 | __cfadbg_print_safe(io, "Kernel I/O : notifying\n"); |
---|
566 | __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head; |
---|
567 | __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue ); |
---|
568 | |
---|
569 | if( have > pa.want ) goto DONE; |
---|
570 | drop( this.pending.queue ); |
---|
571 | |
---|
572 | /* paranoid */__attribute__((unused)) bool ret = |
---|
573 | |
---|
574 | __alloc(ctx, pa.idxs, pa.want); |
---|
575 | |
---|
576 | /* paranoid */ verify( ret ); |
---|
577 | |
---|
578 | pa.ctx = ctx; |
---|
579 | |
---|
580 | post( pa.sem ); |
---|
581 | } |
---|
582 | |
---|
583 | this.pending.empty = true; |
---|
584 | DONE:; |
---|
585 | } |
---|
586 | unlock( this.pending.lock ); |
---|
587 | } |
---|
588 | |
---|
589 | static void __ioarbiter_notify( io_context$ & ctx ) { |
---|
590 | if(!empty( ctx.arbiter->pending )) { |
---|
591 | __ioarbiter_notify( *ctx.arbiter, &ctx ); |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | // Simply append to the pending |
---|
596 | static void __ioarbiter_submit( io_context$ * ctx, __u32 idxs[], __u32 have, bool lazy ) { |
---|
597 | __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd); |
---|
598 | |
---|
599 | __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have); |
---|
600 | |
---|
601 | __external_io ei; |
---|
602 | ei.idxs = idxs; |
---|
603 | ei.have = have; |
---|
604 | ei.lazy = lazy; |
---|
605 | |
---|
606 | bool we = enqueue(ctx->ext_sq, (__outstanding_io&)ei); |
---|
607 | |
---|
608 | __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_SEQ_CST); |
---|
609 | |
---|
610 | if( we ) { |
---|
611 | sigval_t value = { PREEMPT_IO }; |
---|
612 | pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value); |
---|
613 | } |
---|
614 | |
---|
615 | wait( ei.sem ); |
---|
616 | |
---|
617 | __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have); |
---|
618 | } |
---|
619 | |
---|
620 | static void __ioarbiter_flush( io_context$ & ctx ) { |
---|
621 | if(!empty( ctx.ext_sq )) { |
---|
622 | __STATS__( false, io.flush.external += 1; ) |
---|
623 | |
---|
624 | __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n"); |
---|
625 | |
---|
626 | lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 ); |
---|
627 | { |
---|
628 | while( !empty(ctx.ext_sq.queue) ) { |
---|
629 | __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue ); |
---|
630 | |
---|
631 | __submit_only(&ctx, ei.idxs, ei.have); |
---|
632 | |
---|
633 | post( ei.sem ); |
---|
634 | } |
---|
635 | |
---|
636 | ctx.ext_sq.empty = true; |
---|
637 | } |
---|
638 | unlock(ctx.ext_sq.lock ); |
---|
639 | } |
---|
640 | } |
---|
641 | |
---|
642 | #if defined(CFA_WITH_IO_URING_IDLE) |
---|
643 | bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) { |
---|
644 | io_context$ * ctx = proc->io.ctx; |
---|
645 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
646 | /* paranoid */ verify( proc == __cfaabi_tls.this_processor ); |
---|
647 | /* paranoid */ verify( ctx ); |
---|
648 | |
---|
649 | __u32 idx; |
---|
650 | struct io_uring_sqe * sqe; |
---|
651 | |
---|
652 | // We can proceed to the fast path |
---|
653 | if( !__alloc(ctx, &idx, 1) ) { |
---|
654 | /* paranoid */ verify( false ); // for now check if this happens, next time just abort the sleep. |
---|
655 | return false; |
---|
656 | } |
---|
657 | |
---|
658 | // Allocation was successful |
---|
659 | __fill( &sqe, 1, &idx, ctx ); |
---|
660 | |
---|
661 | sqe->user_data = (uintptr_t)&future; |
---|
662 | sqe->flags = 0; |
---|
663 | sqe->fd = fd; |
---|
664 | sqe->off = 0; |
---|
665 | sqe->ioprio = 0; |
---|
666 | sqe->fsync_flags = 0; |
---|
667 | sqe->__pad2[0] = 0; |
---|
668 | sqe->__pad2[1] = 0; |
---|
669 | sqe->__pad2[2] = 0; |
---|
670 | |
---|
671 | #if defined(CFA_HAVE_IORING_OP_READ) |
---|
672 | sqe->opcode = IORING_OP_READ; |
---|
673 | sqe->addr = (uint64_t)iov.iov_base; |
---|
674 | sqe->len = iov.iov_len; |
---|
675 | #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV) |
---|
676 | sqe->opcode = IORING_OP_READV; |
---|
677 | sqe->addr = (uintptr_t)&iov; |
---|
678 | sqe->len = 1; |
---|
679 | #else |
---|
680 | #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined |
---|
681 | #endif |
---|
682 | |
---|
683 | asm volatile("": : :"memory"); |
---|
684 | |
---|
685 | /* paranoid */ verify( sqe->user_data == (uintptr_t)&future ); |
---|
686 | __submit_only( ctx, &idx, 1 ); |
---|
687 | |
---|
688 | /* paranoid */ verify( proc == __cfaabi_tls.this_processor ); |
---|
689 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
690 | |
---|
691 | return true; |
---|
692 | } |
---|
693 | |
---|
694 | void __cfa_io_idle( processor * proc ) { |
---|
695 | iovec iov; |
---|
696 | __atomic_acquire( &proc->io.ctx->cq.lock ); |
---|
697 | |
---|
698 | __attribute__((used)) volatile bool was_reset = false; |
---|
699 | |
---|
700 | with( proc->idle_wctx) { |
---|
701 | |
---|
702 | // Do we already have a pending read |
---|
703 | if(available(*ftr)) { |
---|
704 | // There is no pending read, we need to add one |
---|
705 | reset(*ftr); |
---|
706 | |
---|
707 | iov.iov_base = rdbuf; |
---|
708 | iov.iov_len = sizeof(eventfd_t); |
---|
709 | __kernel_read(proc, *ftr, iov, evfd ); |
---|
710 | ftr->result = 0xDEADDEAD; |
---|
711 | *((eventfd_t *)rdbuf) = 0xDEADDEADDEADDEAD; |
---|
712 | was_reset = true; |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | if( !__atomic_load_n( &proc->do_terminate, __ATOMIC_SEQ_CST ) ) { |
---|
717 | __ioarbiter_flush( *proc->io.ctx ); |
---|
718 | proc->idle_wctx.sleep_time = rdtscl(); |
---|
719 | ioring_syscsll( *proc->io.ctx, 1, IORING_ENTER_GETEVENTS); |
---|
720 | } |
---|
721 | |
---|
722 | ready_schedule_lock(); |
---|
723 | __cfa_do_drain( proc->io.ctx, proc->cltr ); |
---|
724 | ready_schedule_unlock(); |
---|
725 | |
---|
726 | asm volatile ("" :: "m" (was_reset)); |
---|
727 | } |
---|
728 | #endif |
---|
729 | #endif |
---|