// // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // io.cfa -- // // Author : Thierry Delisle // Created On : Thu Apr 23 17:31:00 2020 // Last Modified By : // Last Modified On : // Update Count : // #define __cforall_thread__ #define _GNU_SOURCE #if defined(__CFA_DEBUG__) // #define __CFA_DEBUG_PRINT_IO__ // #define __CFA_DEBUG_PRINT_IO_CORE__ #endif #if defined(CFA_HAVE_LINUX_IO_URING_H) #include #include #include #include #include extern "C" { #include #include #include #include } #include "stats.hfa" #include "kernel.hfa" #include "kernel/fwd.hfa" #include "kernel/private.hfa" #include "kernel/cluster.hfa" #include "io/types.hfa" __attribute__((unused)) static const char * opcodes[] = { "OP_NOP", "OP_READV", "OP_WRITEV", "OP_FSYNC", "OP_READ_FIXED", "OP_WRITE_FIXED", "OP_POLL_ADD", "OP_POLL_REMOVE", "OP_SYNC_FILE_RANGE", "OP_SENDMSG", "OP_RECVMSG", "OP_TIMEOUT", "OP_TIMEOUT_REMOVE", "OP_ACCEPT", "OP_ASYNC_CANCEL", "OP_LINK_TIMEOUT", "OP_CONNECT", "OP_FALLOCATE", "OP_OPENAT", "OP_CLOSE", "OP_FILES_UPDATE", "OP_STATX", "OP_READ", "OP_WRITE", "OP_FADVISE", "OP_MADVISE", "OP_SEND", "OP_RECV", "OP_OPENAT2", "OP_EPOLL_CTL", "OP_SPLICE", "OP_PROVIDE_BUFFERS", "OP_REMOVE_BUFFERS", "OP_TEE", "INVALID_OP" }; static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ); static void __ioarbiter_submit( $io_context * , __u32 idxs[], __u32 have, bool lazy ); static void __ioarbiter_flush ( $io_context & ); static inline void __ioarbiter_notify( $io_context & ctx ); //============================================================================================= // I/O Polling //============================================================================================= static inline unsigned __flush( struct $io_context & ); static inline __u32 __release_sqes( struct $io_context & ); extern void __kernel_unpark( thread$ * thrd, unpark_hint ); static void ioring_syscsll( struct $io_context & ctx, unsigned int min_comp, unsigned int flags ) { __STATS__( true, io.calls.flush++; ) int ret; for() { ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, min_comp, flags, (sigset_t *)0p, _NSIG / 8); if( ret < 0 ) { switch((int)errno) { case EINTR: continue; case EAGAIN: case EBUSY: // Update statistics __STATS__( false, io.calls.errors.busy ++; ) return false; default: abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) ); } } break; } __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd); __STATS__( true, io.calls.submitted += ret; ) /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num ); /* paranoid */ verify( ctx.sq.to_submit >= ret ); ctx.sq.to_submit -= ret; /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num ); // Release the consumed SQEs __release_sqes( ctx ); /* paranoid */ verify( ! __preemption_enabled() ); __atomic_store_n(&ctx.proc->io.pending, false, __ATOMIC_RELAXED); } static bool try_acquire( $io_context * ctx ) __attribute__((nonnull(1))) { /* paranoid */ verify( ! __preemption_enabled() ); /* paranoid */ verify( ready_schedule_islocked() ); { const __u32 head = *ctx->cq.head; const __u32 tail = *ctx->cq.tail; if(head == tail) return false; } // Drain the queue if(!__atomic_try_acquire(&ctx->cq.lock)) { __STATS__( false, io.calls.locked++; ) return false; } return true; } static bool __cfa_do_drain( $io_context * ctx, cluster * cltr ) __attribute__((nonnull(1, 2))) { /* paranoid */ verify( ! __preemption_enabled() ); /* paranoid */ verify( ready_schedule_islocked() ); /* paranoid */ verify( ctx->cq.lock == true ); const __u32 mask = *ctx->cq.mask; const __u32 num = *ctx->cq.num; unsigned long long ts_prev = ctx->cq.ts; unsigned long long ts_next; // We might need to do this multiple times if more events completed than can fit in the queue. for() { // re-read the head and tail in case it already changed. const __u32 head = *ctx->cq.head; const __u32 tail = *ctx->cq.tail; const __u32 count = tail - head; __STATS__( false, io.calls.drain++; io.calls.completed += count; ) for(i; count) { unsigned idx = (head + i) & mask; volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx]; /* paranoid */ verify(&cqe); struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data; // __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future ); __kernel_unpark( fulfil( *future, cqe.res, false ), UNPARK_LOCAL ); } ts_next = ctx->cq.ts = rdtscl(); // Mark to the kernel that the cqe has been seen // Ensure that the kernel only sees the new value of the head index after the CQEs have been read. __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST ); ctx->proc->idle_wctx.drain_time = ts_next; if(likely(count < num)) break; ioring_syscsll( *ctx, 0, IORING_ENTER_GETEVENTS); } __cfadbg_print_safe(io, "Kernel I/O : %u completed age %llu\n", count, ts_next); /* paranoid */ verify( ready_schedule_islocked() ); /* paranoid */ verify( ! __preemption_enabled() ); __atomic_unlock(&ctx->cq.lock); touch_tsc( cltr->sched.io.tscs, ctx->cq.id, ts_prev, ts_next ); return true; } bool __cfa_io_drain( processor * proc ) { bool local = false; bool remote = false; ready_schedule_lock(); cluster * const cltr = proc->cltr; $io_context * const ctx = proc->io.ctx; /* paranoid */ verify( cltr ); /* paranoid */ verify( ctx ); with(cltr->sched) { const size_t ctxs_count = io.count; /* paranoid */ verify( ready_schedule_islocked() ); /* paranoid */ verify( ! __preemption_enabled() ); /* paranoid */ verify( active_processor() == proc ); /* paranoid */ verify( __shard_factor.io > 0 ); /* paranoid */ verify( ctxs_count > 0 ); /* paranoid */ verify( ctx->cq.id < ctxs_count ); const unsigned this_cache = cache_id(cltr, ctx->cq.id / __shard_factor.io); const unsigned long long ctsc = rdtscl(); if(proc->io.target == UINT_MAX) { uint64_t chaos = __tls_rand(); unsigned ext = chaos & 0xff; unsigned other = (chaos >> 8) % (ctxs_count); if(ext < 3 || __atomic_load_n(&caches[other / __shard_factor.io].id, __ATOMIC_RELAXED) == this_cache) { proc->io.target = other; } } else { const unsigned target = proc->io.target; /* paranoid */ verify( io.tscs[target].t.tv != ULLONG_MAX ); HELP: if(target < ctxs_count) { const unsigned long long cutoff = calc_cutoff(ctsc, ctx->cq.id, ctxs_count, io.data, io.tscs, __shard_factor.io); const unsigned long long age = moving_average(ctsc, io.tscs[target].t.tv, io.tscs[target].t.ma); __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"); if(age <= cutoff) break HELP; if(!try_acquire(io.data[target])) break HELP; if(!__cfa_do_drain( io.data[target], cltr )) break HELP; remote = true; __STATS__( true, io.calls.helped++; ) } proc->io.target = UINT_MAX; } } // Drain the local queue if(try_acquire( proc->io.ctx )) { local = __cfa_do_drain( proc->io.ctx, cltr ); } /* paranoid */ verify( ready_schedule_islocked() ); /* paranoid */ verify( ! __preemption_enabled() ); /* paranoid */ verify( active_processor() == proc ); ready_schedule_unlock(); return local || remote; } bool __cfa_io_flush( processor * proc ) { /* paranoid */ verify( ! __preemption_enabled() ); /* paranoid */ verify( proc ); /* paranoid */ verify( proc->io.ctx ); $io_context & ctx = *proc->io.ctx; __ioarbiter_flush( ctx ); if(ctx.sq.to_submit != 0) { ioring_syscsll(ctx, 0, 0); } return __cfa_io_drain( proc ); } //============================================================================================= // I/O Submissions //============================================================================================= // Submition steps : // 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones // listed in sq.array are visible by the kernel. For those not listed, the kernel does not // offer any assurance that an entry is not being filled by multiple flags. Therefore, we // need to write an allocator that allows allocating concurrently. // // 2 - Actually fill the submit entry, this is the only simple and straightforward step. // // 3 - Append the entry index to the array and adjust the tail accordingly. This operation // needs to arrive to two concensus at the same time: // A - The order in which entries are listed in the array: no two threads must pick the // same index for their entries // B - When can the tail be update for the kernel. EVERY entries in the array between // head and tail must be fully filled and shouldn't ever be touched again. // //============================================================================================= // Allocation // for user's convenience fill the sqes from the indexes static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx) { struct io_uring_sqe * sqes = ctx->sq.sqes; for(i; want) { // __cfadbg_print_safe(io, "Kernel I/O : filling loop\n"); out_sqes[i] = &sqes[idxs[i]]; } } // Try to directly allocate from the a given context // Not thread-safe static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) { __sub_ring_t & sq = ctx->sq; const __u32 mask = *sq.mask; __u32 fhead = sq.free_ring.head; // get the current head of the queue __u32 ftail = sq.free_ring.tail; // get the current tail of the queue // If we don't have enough sqes, fail if((ftail - fhead) < want) { return false; } // copy all the indexes we want from the available list for(i; want) { // __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n"); idxs[i] = sq.free_ring.array[(fhead + i) & mask]; } // Advance the head to mark the indexes as consumed __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE); // return success return true; } // Allocate an submit queue entry. // The kernel cannot see these entries until they are submitted, but other threads must be // able to see which entries can be used and which are already un used by an other thread // for convenience, return both the index and the pointer to the sqe // sqe == &sqes[idx] struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) libcfa_public { // __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want); disable_interrupts(); processor * proc = __cfaabi_tls.this_processor; $io_context * ctx = proc->io.ctx; /* paranoid */ verify( __cfaabi_tls.this_processor ); /* paranoid */ verify( ctx ); // __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n"); // We can proceed to the fast path if( __alloc(ctx, idxs, want) ) { // Allocation was successful __STATS__( true, io.alloc.fast += 1; ) enable_interrupts(); // __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd); __fill( sqes, want, idxs, ctx ); return ctx; } // The fast path failed, fallback __STATS__( true, io.alloc.fail += 1; ) // Fast path failed, fallback on arbitration __STATS__( true, io.alloc.slow += 1; ) enable_interrupts(); $io_arbiter * ioarb = proc->cltr->io.arbiter; /* paranoid */ verify( ioarb ); // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n"); struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want); // __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd); __fill( sqes, want, idxs,ret ); return ret; } //============================================================================================= // submission static inline void __submit_only( struct $io_context * ctx, __u32 idxs[], __u32 have) { // We can proceed to the fast path // Get the right objects __sub_ring_t & sq = ctx->sq; const __u32 mask = *sq.mask; __u32 tail = *sq.kring.tail; // Add the sqes to the array for( i; have ) { // __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n"); sq.kring.array[ (tail + i) & mask ] = idxs[i]; } // Make the sqes visible to the submitter __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE); sq.to_submit += have; __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_RELAXED); __atomic_store_n(&ctx->proc->io.dirty , true, __ATOMIC_RELAXED); } static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) { __sub_ring_t & sq = ctx->sq; __submit_only(ctx, idxs, have); if(sq.to_submit > 30) { __tls_stats()->io.flush.full++; __cfa_io_flush( ctx->proc ); } if(!lazy) { __tls_stats()->io.flush.eager++; __cfa_io_flush( ctx->proc ); } } void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) libcfa_public { // __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager"); disable_interrupts(); processor * proc = __cfaabi_tls.this_processor; $io_context * ctx = proc->io.ctx; /* paranoid */ verify( __cfaabi_tls.this_processor ); /* paranoid */ verify( ctx ); // Can we proceed to the fast path if( ctx == inctx ) // We have the right instance? { __submit(ctx, idxs, have, lazy); // Mark the instance as no longer in-use, re-enable interrupts and return __STATS__( true, io.submit.fast += 1; ) enable_interrupts(); // __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n"); return; } // Fast path failed, fallback on arbitration __STATS__( true, io.submit.slow += 1; ) enable_interrupts(); // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n"); __ioarbiter_submit(inctx, idxs, have, lazy); } //============================================================================================= // Flushing // Go through the ring's submit queue and release everything that has already been consumed // by io_uring // This cannot be done by multiple threads static __u32 __release_sqes( struct $io_context & ctx ) { const __u32 mask = *ctx.sq.mask; __attribute__((unused)) __u32 ctail = *ctx.sq.kring.tail; // get the current tail of the queue __u32 chead = *ctx.sq.kring.head; // get the current head of the queue __u32 phead = ctx.sq.kring.released; // get the head the last time we were here __u32 ftail = ctx.sq.free_ring.tail; // get the current tail of the queue // the 3 fields are organized like this diagram // except it's are ring // ---+--------+--------+---- // ---+--------+--------+---- // ^ ^ ^ // phead chead ctail // make sure ctail doesn't wrap around and reach phead /* paranoid */ verify( (ctail >= chead && chead >= phead) || (chead >= phead && phead >= ctail) || (phead >= ctail && ctail >= chead) ); // find the range we need to clear __u32 count = chead - phead; if(count == 0) { return 0; } // We acquired an previous-head/current-head range // go through the range and release the sqes for( i; count ) { // __cfadbg_print_safe(io, "Kernel I/O : release loop\n"); __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ]; ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx; } ctx.sq.kring.released = chead; // note up to were we processed __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST); __ioarbiter_notify(ctx); return count; } //============================================================================================= // I/O Arbiter //============================================================================================= static inline bool enqueue(__outstanding_io_queue & queue, __outstanding_io & item) { bool was_empty; // Lock the list, it's not thread safe lock( queue.lock __cfaabi_dbg_ctx2 ); { was_empty = empty(queue.queue); // Add our request to the list add( queue.queue, item ); // Mark as pending __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST ); } unlock( queue.lock ); return was_empty; } static inline bool empty(__outstanding_io_queue & queue ) { return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST); } static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) { // __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n"); __STATS__( false, io.alloc.block += 1; ) // No one has any resources left, wait for something to finish // We need to add ourself to a list of pending allocs and wait for an answer __pending_alloc pa; pa.idxs = idxs; pa.want = want; enqueue(this.pending, (__outstanding_io&)pa); wait( pa.sem ); return pa.ctx; } static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) { /* paranoid */ verify( !empty(this.pending.queue) ); lock( this.pending.lock __cfaabi_dbg_ctx2 ); { while( !empty(this.pending.queue) ) { __cfadbg_print_safe(io, "Kernel I/O : notifying\n"); __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head; __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue ); if( have > pa.want ) goto DONE; drop( this.pending.queue ); /* paranoid */__attribute__((unused)) bool ret = __alloc(ctx, pa.idxs, pa.want); /* paranoid */ verify( ret ); pa.ctx = ctx; post( pa.sem ); } this.pending.empty = true; DONE:; } unlock( this.pending.lock ); } static void __ioarbiter_notify( $io_context & ctx ) { if(!empty( ctx.arbiter->pending )) { __ioarbiter_notify( *ctx.arbiter, &ctx ); } } // Simply append to the pending static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) { __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd); __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have); __external_io ei; ei.idxs = idxs; ei.have = have; ei.lazy = lazy; bool we = enqueue(ctx->ext_sq, (__outstanding_io&)ei); __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_SEQ_CST); if( we ) { sigval_t value = { PREEMPT_IO }; pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value); } wait( ei.sem ); __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have); } static void __ioarbiter_flush( $io_context & ctx ) { if(!empty( ctx.ext_sq )) { __STATS__( false, io.flush.external += 1; ) __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n"); lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 ); { while( !empty(ctx.ext_sq.queue) ) { __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue ); __submit_only(&ctx, ei.idxs, ei.have); post( ei.sem ); } ctx.ext_sq.empty = true; } unlock(ctx.ext_sq.lock ); } } #if defined(CFA_WITH_IO_URING_IDLE) bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) { $io_context * ctx = proc->io.ctx; /* paranoid */ verify( ! __preemption_enabled() ); /* paranoid */ verify( proc == __cfaabi_tls.this_processor ); /* paranoid */ verify( ctx ); __u32 idx; struct io_uring_sqe * sqe; // We can proceed to the fast path if( !__alloc(ctx, &idx, 1) ) { /* paranoid */ verify( false ); // for now check if this happens, next time just abort the sleep. return false; } // Allocation was successful __fill( &sqe, 1, &idx, ctx ); sqe->user_data = (uintptr_t)&future; sqe->flags = 0; sqe->fd = fd; sqe->off = 0; sqe->ioprio = 0; sqe->fsync_flags = 0; sqe->__pad2[0] = 0; sqe->__pad2[1] = 0; sqe->__pad2[2] = 0; #if defined(CFA_HAVE_IORING_OP_READ) sqe->opcode = IORING_OP_READ; sqe->addr = (uint64_t)iov.iov_base; sqe->len = iov.iov_len; #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV) sqe->opcode = IORING_OP_READV; sqe->addr = (uintptr_t)&iov; sqe->len = 1; #else #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined #endif asm volatile("": : :"memory"); /* paranoid */ verify( sqe->user_data == (uintptr_t)&future ); __submit_only( ctx, &idx, 1 ); /* paranoid */ verify( proc == __cfaabi_tls.this_processor ); /* paranoid */ verify( ! __preemption_enabled() ); return true; } void __cfa_io_idle( processor * proc ) { iovec iov; __atomic_acquire( &proc->io.ctx->cq.lock ); __attribute__((used)) volatile bool was_reset = false; with( proc->idle_wctx) { // Do we already have a pending read if(available(*ftr)) { // There is no pending read, we need to add one reset(*ftr); iov.iov_base = rdbuf; iov.iov_len = sizeof(eventfd_t); __kernel_read(proc, *ftr, iov, evfd ); ftr->result = 0xDEADDEAD; *((eventfd_t *)rdbuf) = 0xDEADDEADDEADDEAD; was_reset = true; } } if( !__atomic_load_n( &proc->do_terminate, __ATOMIC_SEQ_CST ) ) { __ioarbiter_flush( *proc->io.ctx ); proc->idle_wctx.sleep_time = rdtscl(); ioring_syscsll( *proc->io.ctx, 1, IORING_ENTER_GETEVENTS); } ready_schedule_lock(); __cfa_do_drain( proc->io.ctx, proc->cltr ); ready_schedule_unlock(); asm volatile ("" :: "m" (was_reset)); } #endif #endif