source: libcfa/src/concurrency/io.cfa@ 9e2341b4

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9e2341b4 was e9c0b4c, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

I/O drain now keeps the schedlock for the duration of the call.
(Rather than locking/unlocking for each thread).

  • Property mode set to 100644
File size: 15.1 KB
RevLine 
[ecf6b46]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
[3e2b9c9]16#define __cforall_thread__
17
[20ab637]18#if defined(__CFA_DEBUG__)
[d60d30e]19 // #define __CFA_DEBUG_PRINT_IO__
20 // #define __CFA_DEBUG_PRINT_IO_CORE__
[20ab637]21#endif
[4069faad]22
[f6660520]23
[3e2b9c9]24#if defined(CFA_HAVE_LINUX_IO_URING_H)
[31bb2e1]25 #define _GNU_SOURCE /* See feature_test_macros(7) */
26 #include <errno.h>
[3e2b9c9]27 #include <signal.h>
[31bb2e1]28 #include <stdint.h>
29 #include <string.h>
30 #include <unistd.h>
31
[92976d9]32 extern "C" {
33 #include <sys/syscall.h>
[dddb3dd0]34 #include <sys/eventfd.h>
[92976d9]35
36 #include <linux/io_uring.h>
37 }
38
[3e2b9c9]39 #include "stats.hfa"
40 #include "kernel.hfa"
41 #include "kernel/fwd.hfa"
[e9c0b4c]42 #include "kernel_private.hfa"
[3e2b9c9]43 #include "io/types.hfa"
[185efe6]44
[2fab24e3]45 __attribute__((unused)) static const char * opcodes[] = {
[426f60c]46 "OP_NOP",
47 "OP_READV",
48 "OP_WRITEV",
49 "OP_FSYNC",
50 "OP_READ_FIXED",
51 "OP_WRITE_FIXED",
52 "OP_POLL_ADD",
53 "OP_POLL_REMOVE",
54 "OP_SYNC_FILE_RANGE",
55 "OP_SENDMSG",
56 "OP_RECVMSG",
57 "OP_TIMEOUT",
58 "OP_TIMEOUT_REMOVE",
59 "OP_ACCEPT",
60 "OP_ASYNC_CANCEL",
61 "OP_LINK_TIMEOUT",
62 "OP_CONNECT",
63 "OP_FALLOCATE",
64 "OP_OPENAT",
65 "OP_CLOSE",
66 "OP_FILES_UPDATE",
67 "OP_STATX",
68 "OP_READ",
69 "OP_WRITE",
70 "OP_FADVISE",
71 "OP_MADVISE",
72 "OP_SEND",
73 "OP_RECV",
74 "OP_OPENAT2",
75 "OP_EPOLL_CTL",
76 "OP_SPLICE",
77 "OP_PROVIDE_BUFFERS",
78 "OP_REMOVE_BUFFERS",
79 "OP_TEE",
80 "INVALID_OP"
81 };
82
[11054eb]83 static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want );
84 static void __ioarbiter_submit( $io_context * , __u32 idxs[], __u32 have, bool lazy );
85 static void __ioarbiter_flush ( $io_context & );
[dddb3dd0]86 static inline void __ioarbiter_notify( $io_context & ctx );
[92976d9]87//=============================================================================================
88// I/O Polling
89//=============================================================================================
[78da4ab]90 static inline unsigned __flush( struct $io_context & );
91 static inline __u32 __release_sqes( struct $io_context & );
[e9c0b4c]92 extern void __kernel_unpark( $thread * thrd );
[1d5e4711]93
[c1c95b1]94 bool __cfa_io_drain( processor * proc ) {
[dddb3dd0]95 /* paranoid */ verify( ! __preemption_enabled() );
[e9c0b4c]96 /* paranoid */ verify( ready_schedule_islocked() );
[dddb3dd0]97 /* paranoid */ verify( proc );
98 /* paranoid */ verify( proc->io.ctx );
[6f121b8]99
[d384787]100 // Drain the queue
[dddb3dd0]101 $io_context * ctx = proc->io.ctx;
102 unsigned head = *ctx->cq.head;
103 unsigned tail = *ctx->cq.tail;
104 const __u32 mask = *ctx->cq.mask;
[92976d9]105
[4998155]106 __u32 count = tail - head;
[dddb3dd0]107 __STATS__( false, io.calls.drain++; io.calls.completed += count; )
[d60d30e]108
[c1c95b1]109 if(count == 0) return false;
110
[d384787]111 for(i; count) {
[6f121b8]112 unsigned idx = (head + i) & mask;
[dddb3dd0]113 volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx];
[92976d9]114
[d384787]115 /* paranoid */ verify(&cqe);
[92976d9]116
[78da4ab]117 struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
118 __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
119
[e9c0b4c]120 __kernel_unpark( fulfil( *future, cqe.res, false ) );
[78da4ab]121 }
122
[dddb3dd0]123 __cfadbg_print_safe(io, "Kernel I/O : %u completed\n", count);
[2d8f7b0]124
[92976d9]125 // Mark to the kernel that the cqe has been seen
126 // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
[dddb3dd0]127 __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST );
[92976d9]128
[e9c0b4c]129 /* paranoid */ verify( ready_schedule_islocked() );
[dddb3dd0]130 /* paranoid */ verify( ! __preemption_enabled() );
131
[c1c95b1]132 return true;
[92976d9]133 }
134
[dddb3dd0]135 void __cfa_io_flush( processor * proc ) {
136 /* paranoid */ verify( ! __preemption_enabled() );
137 /* paranoid */ verify( proc );
138 /* paranoid */ verify( proc->io.ctx );
[1539bbd]139
[dddb3dd0]140 $io_context & ctx = *proc->io.ctx;
[78da4ab]141
[11054eb]142 __ioarbiter_flush( ctx );
[3c039b0]143
[dddb3dd0]144 __STATS__( true, io.calls.flush++; )
145 int ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, 0, 0, (sigset_t *)0p, _NSIG / 8);
146 if( ret < 0 ) {
147 switch((int)errno) {
148 case EAGAIN:
149 case EINTR:
150 case EBUSY:
151 // Update statistics
152 __STATS__( false, io.calls.errors.busy ++; )
153 return;
154 default:
155 abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
[61dd73d]156 }
[dddb3dd0]157 }
[ece0e80]158
[dddb3dd0]159 __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd);
160 __STATS__( true, io.calls.submitted += ret; )
161 /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
162 /* paranoid */ verify( ctx.sq.to_submit >= ret );
163
164 ctx.sq.to_submit -= ret;
[ece0e80]165
[dddb3dd0]166 /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
[5dadc9b7]167
[dddb3dd0]168 // Release the consumed SQEs
169 __release_sqes( ctx );
[ece0e80]170
[dddb3dd0]171 /* paranoid */ verify( ! __preemption_enabled() );
[61dd73d]172
[dddb3dd0]173 ctx.proc->io.pending = false;
[61dd73d]174 }
[f6660520]175
[92976d9]176//=============================================================================================
177// I/O Submissions
178//=============================================================================================
179
[2d8f7b0]180// Submition steps :
[e46c753]181// 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
[2d8f7b0]182// listed in sq.array are visible by the kernel. For those not listed, the kernel does not
183// offer any assurance that an entry is not being filled by multiple flags. Therefore, we
184// need to write an allocator that allows allocating concurrently.
185//
[e46c753]186// 2 - Actually fill the submit entry, this is the only simple and straightforward step.
[2d8f7b0]187//
[e46c753]188// 3 - Append the entry index to the array and adjust the tail accordingly. This operation
[2d8f7b0]189// needs to arrive to two concensus at the same time:
190// A - The order in which entries are listed in the array: no two threads must pick the
191// same index for their entries
192// B - When can the tail be update for the kernel. EVERY entries in the array between
193// head and tail must be fully filled and shouldn't ever be touched again.
194//
[78da4ab]195 //=============================================================================================
196 // Allocation
197 // for user's convenience fill the sqes from the indexes
198 static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx) {
199 struct io_uring_sqe * sqes = ctx->sq.sqes;
200 for(i; want) {
[dddb3dd0]201 __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
[78da4ab]202 out_sqes[i] = &sqes[idxs[i]];
203 }
204 }
[2489d31]205
[78da4ab]206 // Try to directly allocate from the a given context
207 // Not thread-safe
208 static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) {
209 __sub_ring_t & sq = ctx->sq;
210 const __u32 mask = *sq.mask;
211 __u32 fhead = sq.free_ring.head; // get the current head of the queue
212 __u32 ftail = sq.free_ring.tail; // get the current tail of the queue
[2489d31]213
[78da4ab]214 // If we don't have enough sqes, fail
215 if((ftail - fhead) < want) { return false; }
[426f60c]216
[78da4ab]217 // copy all the indexes we want from the available list
218 for(i; want) {
[dddb3dd0]219 __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
[78da4ab]220 idxs[i] = sq.free_ring.array[(fhead + i) & mask];
[6f121b8]221 }
[2489d31]222
[78da4ab]223 // Advance the head to mark the indexes as consumed
224 __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
[df40a56]225
[78da4ab]226 // return success
227 return true;
228 }
[df40a56]229
[78da4ab]230 // Allocate an submit queue entry.
231 // The kernel cannot see these entries until they are submitted, but other threads must be
232 // able to see which entries can be used and which are already un used by an other thread
233 // for convenience, return both the index and the pointer to the sqe
234 // sqe == &sqes[idx]
235 struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) {
236 __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
[df40a56]237
[78da4ab]238 disable_interrupts();
239 processor * proc = __cfaabi_tls.this_processor;
[dddb3dd0]240 $io_context * ctx = proc->io.ctx;
[78da4ab]241 /* paranoid */ verify( __cfaabi_tls.this_processor );
[dddb3dd0]242 /* paranoid */ verify( ctx );
[78da4ab]243
[dddb3dd0]244 __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
[78da4ab]245
[dddb3dd0]246 // We can proceed to the fast path
247 if( __alloc(ctx, idxs, want) ) {
248 // Allocation was successful
249 __STATS__( true, io.alloc.fast += 1; )
[a3821fa]250 enable_interrupts();
[df40a56]251
[dddb3dd0]252 __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
[2fafe7e]253
[dddb3dd0]254 __fill( sqes, want, idxs, ctx );
255 return ctx;
[df40a56]256 }
[dddb3dd0]257 // The fast path failed, fallback
258 __STATS__( true, io.alloc.fail += 1; )
[df40a56]259
[78da4ab]260 // Fast path failed, fallback on arbitration
[d60d30e]261 __STATS__( true, io.alloc.slow += 1; )
[a3821fa]262 enable_interrupts();
[78da4ab]263
[dddb3dd0]264 $io_arbiter * ioarb = proc->cltr->io.arbiter;
265 /* paranoid */ verify( ioarb );
266
[78da4ab]267 __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
268
[11054eb]269 struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want);
[78da4ab]270
[dddb3dd0]271 __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
[df40a56]272
[78da4ab]273 __fill( sqes, want, idxs,ret );
274 return ret;
[df40a56]275 }
276
[426f60c]277
[78da4ab]278 //=============================================================================================
279 // submission
[dddb3dd0]280 static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) {
[78da4ab]281 // We can proceed to the fast path
282 // Get the right objects
283 __sub_ring_t & sq = ctx->sq;
284 const __u32 mask = *sq.mask;
[dddb3dd0]285 __u32 tail = *sq.kring.tail;
[78da4ab]286
287 // Add the sqes to the array
288 for( i; have ) {
[dddb3dd0]289 __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
[78da4ab]290 sq.kring.array[ (tail + i) & mask ] = idxs[i];
[426f60c]291 }
292
[78da4ab]293 // Make the sqes visible to the submitter
[dddb3dd0]294 __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
295 sq.to_submit++;
[426f60c]296
[dddb3dd0]297 ctx->proc->io.pending = true;
298 ctx->proc->io.dirty = true;
299 if(sq.to_submit > 30 || !lazy) {
300 __cfa_io_flush( ctx->proc );
301 }
[78da4ab]302 }
[2489d31]303
[dddb3dd0]304 void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
305 __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
[5dadc9b7]306
[78da4ab]307 disable_interrupts();
308 processor * proc = __cfaabi_tls.this_processor;
309 $io_context * ctx = proc->io.ctx;
[dddb3dd0]310 /* paranoid */ verify( __cfaabi_tls.this_processor );
311 /* paranoid */ verify( ctx );
[e46c753]312
[78da4ab]313 // Can we proceed to the fast path
[dddb3dd0]314 if( ctx == inctx ) // We have the right instance?
[78da4ab]315 {
[dddb3dd0]316 __submit(ctx, idxs, have, lazy);
[e46c753]317
[78da4ab]318 // Mark the instance as no longer in-use, re-enable interrupts and return
[d60d30e]319 __STATS__( true, io.submit.fast += 1; )
[a3821fa]320 enable_interrupts();
[ece0e80]321
[78da4ab]322 __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
323 return;
[e46c753]324 }
[d384787]325
[78da4ab]326 // Fast path failed, fallback on arbitration
[d60d30e]327 __STATS__( true, io.submit.slow += 1; )
[a3821fa]328 enable_interrupts();
[5dadc9b7]329
[78da4ab]330 __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
[426f60c]331
[11054eb]332 __ioarbiter_submit(inctx, idxs, have, lazy);
[78da4ab]333 }
[2fab24e3]334
[78da4ab]335 //=============================================================================================
336 // Flushing
[426f60c]337 // Go through the ring's submit queue and release everything that has already been consumed
338 // by io_uring
[78da4ab]339 // This cannot be done by multiple threads
340 static __u32 __release_sqes( struct $io_context & ctx ) {
341 const __u32 mask = *ctx.sq.mask;
[732b406]342
[426f60c]343 __attribute__((unused))
[78da4ab]344 __u32 ctail = *ctx.sq.kring.tail; // get the current tail of the queue
345 __u32 chead = *ctx.sq.kring.head; // get the current head of the queue
346 __u32 phead = ctx.sq.kring.released; // get the head the last time we were here
347
348 __u32 ftail = ctx.sq.free_ring.tail; // get the current tail of the queue
[732b406]349
[426f60c]350 // the 3 fields are organized like this diagram
351 // except it's are ring
352 // ---+--------+--------+----
353 // ---+--------+--------+----
354 // ^ ^ ^
355 // phead chead ctail
356
357 // make sure ctail doesn't wrap around and reach phead
358 /* paranoid */ verify(
359 (ctail >= chead && chead >= phead)
360 || (chead >= phead && phead >= ctail)
361 || (phead >= ctail && ctail >= chead)
362 );
363
364 // find the range we need to clear
[4998155]365 __u32 count = chead - phead;
[426f60c]366
[78da4ab]367 if(count == 0) {
368 return 0;
369 }
370
[426f60c]371 // We acquired an previous-head/current-head range
372 // go through the range and release the sqes
[34b61882]373 for( i; count ) {
[dddb3dd0]374 __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
[78da4ab]375 __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
376 ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
[34b61882]377 }
[78da4ab]378
379 ctx.sq.kring.released = chead; // note up to were we processed
380 __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST);
381
382 __ioarbiter_notify(ctx);
383
[34b61882]384 return count;
385 }
[35285fd]386
[78da4ab]387//=============================================================================================
388// I/O Arbiter
389//=============================================================================================
[11054eb]390 static inline void block(__outstanding_io_queue & queue, __outstanding_io & item) {
391 // Lock the list, it's not thread safe
392 lock( queue.lock __cfaabi_dbg_ctx2 );
393 {
394 // Add our request to the list
395 add( queue.queue, item );
396
397 // Mark as pending
398 __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST );
399 }
400 unlock( queue.lock );
401
402 wait( item.sem );
403 }
404
405 static inline bool empty(__outstanding_io_queue & queue ) {
406 return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST);
407 }
408
409 static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) {
[78da4ab]410 __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
411
[d60d30e]412 __STATS__( false, io.alloc.block += 1; )
413
[78da4ab]414 // No one has any resources left, wait for something to finish
[11054eb]415 // We need to add ourself to a list of pending allocs and wait for an answer
416 __pending_alloc pa;
417 pa.idxs = idxs;
418 pa.want = want;
[78da4ab]419
[11054eb]420 block(this.pending, (__outstanding_io&)pa);
[78da4ab]421
[11054eb]422 return pa.ctx;
[dddb3dd0]423
[78da4ab]424 }
425
[11054eb]426 static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) {
427 /* paranoid */ verify( !empty(this.pending.queue) );
[78da4ab]428
[11054eb]429 lock( this.pending.lock __cfaabi_dbg_ctx2 );
430 {
431 while( !empty(this.pending.queue) ) {
432 __cfadbg_print_safe(io, "Kernel I/O : notifying\n");
433 __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head;
434 __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue );
[78da4ab]435
[11054eb]436 if( have > pa.want ) goto DONE;
437 drop( this.pending.queue );
[78da4ab]438
[11054eb]439 /* paranoid */__attribute__((unused)) bool ret =
[78da4ab]440
[11054eb]441 __alloc(ctx, pa.idxs, pa.want);
442
443 /* paranoid */ verify( ret );
444
445 pa.ctx = ctx;
446
447 post( pa.sem );
448 }
449
450 this.pending.empty = true;
451 DONE:;
452 }
453 unlock( this.pending.lock );
[78da4ab]454 }
455
456 static void __ioarbiter_notify( $io_context & ctx ) {
[11054eb]457 if(!empty( ctx.arbiter->pending )) {
[78da4ab]458 __ioarbiter_notify( *ctx.arbiter, &ctx );
459 }
460 }
461
462 // Simply append to the pending
[11054eb]463 static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
[78da4ab]464 __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd);
465
466 __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have);
467
[11054eb]468 __external_io ei;
469 ei.idxs = idxs;
470 ei.have = have;
471 ei.lazy = lazy;
[78da4ab]472
[11054eb]473 block(ctx->ext_sq, (__outstanding_io&)ei);
[78da4ab]474
475 __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
476 }
477
[11054eb]478 static void __ioarbiter_flush( $io_context & ctx ) {
479 if(!empty( ctx.ext_sq )) {
480 __STATS__( false, io.flush.external += 1; )
[78da4ab]481
[11054eb]482 __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
[d60d30e]483
[11054eb]484 lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 );
485 {
486 while( !empty(ctx.ext_sq.queue) ) {
487 __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue );
[78da4ab]488
[11054eb]489 __submit(&ctx, ei.idxs, ei.have, ei.lazy);
[78da4ab]490
[11054eb]491 post( ei.sem );
492 }
493
494 ctx.ext_sq.empty = true;
495 }
496 unlock(ctx.ext_sq.lock );
497 }
[78da4ab]498 }
[47746a2]499#endif
Note: See TracBrowser for help on using the repository browser.