// // 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 __CFA_DEBUG_PRINT_IO__ // #define __CFA_DEBUG_PRINT_IO_CORE__ #include "kernel.hfa" #include "bitmanip.hfa" #if !defined(HAVE_LINUX_IO_URING_H) void __kernel_io_startup( cluster &, unsigned, bool ) { // Nothing to do without io_uring } void __kernel_io_finish_start( cluster & ) { // Nothing to do without io_uring } void __kernel_io_prepare_stop( cluster & ) { // Nothing to do without io_uring } void __kernel_io_shutdown( cluster &, bool ) { // Nothing to do without io_uring } #else #define _GNU_SOURCE /* See feature_test_macros(7) */ #include #include #include #include #include extern "C" { #include #include } #include "bits/signal.hfa" #include "kernel_private.hfa" #include "thread.hfa" uint32_t entries_per_cluster() { return 256; } static void * __io_poller_slow( void * arg ); // Weirdly, some systems that do support io_uring don't actually define these #ifdef __alpha__ /* * alpha is the only exception, all other architectures * have common numbers for new system calls. */ #ifndef __NR_io_uring_setup #define __NR_io_uring_setup 535 #endif #ifndef __NR_io_uring_enter #define __NR_io_uring_enter 536 #endif #ifndef __NR_io_uring_register #define __NR_io_uring_register 537 #endif #else /* !__alpha__ */ #ifndef __NR_io_uring_setup #define __NR_io_uring_setup 425 #endif #ifndef __NR_io_uring_enter #define __NR_io_uring_enter 426 #endif #ifndef __NR_io_uring_register #define __NR_io_uring_register 427 #endif #endif // Fast poller user-thread // Not using the "thread" keyword because we want to control // more carefully when to start/stop it struct __io_poller_fast { struct __io_data * ring; $thread thrd; }; void ?{}( __io_poller_fast & this, struct cluster & cltr ) { this.ring = cltr.io; (this.thrd){ "Fast I/O Poller", cltr }; } void ^?{}( __io_poller_fast & mutex this ); void main( __io_poller_fast & this ); static inline $thread * get_thread( __io_poller_fast & this ) { return &this.thrd; } void ^?{}( __io_poller_fast & mutex this ) {} struct __submition_data { // Head and tail of the ring (associated with array) volatile uint32_t * head; volatile uint32_t * tail; volatile uint32_t prev_head; // The actual kernel ring which uses head/tail // indexes into the sqes arrays uint32_t * array; // number of entries and mask to go with it const uint32_t * num; const uint32_t * mask; // Submission flags (Not sure what for) uint32_t * flags; // number of sqes not submitted (whatever that means) uint32_t * dropped; // Like head/tail but not seen by the kernel volatile uint32_t * ready; uint32_t ready_cnt; __spinlock_t lock; // A buffer of sqes (not the actual ring) struct io_uring_sqe * sqes; // The location and size of the mmaped area void * ring_ptr; size_t ring_sz; }; struct __completion_data { // Head and tail of the ring volatile uint32_t * head; volatile uint32_t * tail; // number of entries and mask to go with it const uint32_t * mask; const uint32_t * num; // number of cqes not submitted (whatever that means) uint32_t * overflow; // the kernel ring struct io_uring_cqe * cqes; // The location and size of the mmaped area void * ring_ptr; size_t ring_sz; }; struct __io_data { struct __submition_data submit_q; struct __completion_data completion_q; uint32_t ring_flags; int cltr_flags; int fd; semaphore submit; volatile bool done; struct { struct { __processor_id_t id; void * stack; pthread_t kthrd; volatile bool blocked; } slow; __io_poller_fast fast; __bin_sem_t sem; } poller; }; //============================================================================================= // I/O Startup / Shutdown logic //============================================================================================= void __kernel_io_startup( cluster & this, unsigned io_flags, bool main_cluster ) { if( (io_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS) && (io_flags & CFA_CLUSTER_IO_EAGER_SUBMITS) ) { abort("CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS and CFA_CLUSTER_IO_EAGER_SUBMITS cannot be mixed\n"); } this.io = malloc(); // Step 1 : call to setup struct io_uring_params params; memset(¶ms, 0, sizeof(params)); if( io_flags & CFA_CLUSTER_IO_KERNEL_POLL_SUBMITS ) params.flags |= IORING_SETUP_SQPOLL; if( io_flags & CFA_CLUSTER_IO_KERNEL_POLL_COMPLETES ) params.flags |= IORING_SETUP_IOPOLL; uint32_t nentries = entries_per_cluster(); int fd = syscall(__NR_io_uring_setup, nentries, ¶ms ); if(fd < 0) { abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno)); } // Step 2 : mmap result memset( this.io, 0, sizeof(struct __io_data) ); struct __submition_data & sq = this.io->submit_q; struct __completion_data & cq = this.io->completion_q; // calculate the right ring size sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) ); cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe)); // Requires features #if defined(IORING_FEAT_SINGLE_MMAP) // adjust the size according to the parameters if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { cq->ring_sz = sq->ring_sz = max(cq->ring_sz, sq->ring_sz); } #endif // mmap the Submit Queue into existence sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); if (sq.ring_ptr == (void*)MAP_FAILED) { abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno)); } // Requires features #if defined(IORING_FEAT_SINGLE_MMAP) // mmap the Completion Queue into existence (may or may not be needed) if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { cq->ring_ptr = sq->ring_ptr; } else #endif { // We need multiple call to MMAP cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); if (cq.ring_ptr == (void*)MAP_FAILED) { munmap(sq.ring_ptr, sq.ring_sz); abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno)); } } // mmap the submit queue entries size_t size = params.sq_entries * sizeof(struct io_uring_sqe); sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES); if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) { munmap(sq.ring_ptr, sq.ring_sz); if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz); abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno)); } // Get the pointers from the kernel to fill the structure // submit queue sq.head = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.head); sq.tail = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail); sq.mask = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask); sq.num = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries); sq.flags = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags); sq.dropped = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped); sq.array = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.array); sq.prev_head = *sq.head; { const uint32_t num = *sq.num; for( i; num ) { sq.sqes[i].user_data = 0ul64; } } (sq.lock){}; if( io_flags & ( CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS | CFA_CLUSTER_IO_EAGER_SUBMITS ) ) { /* paranoid */ verify( is_pow2( io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET ) || ((io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET) < 8) ); sq.ready_cnt = max(io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET, 8); sq.ready = alloc_align( 64, sq.ready_cnt ); for(i; sq.ready_cnt) { sq.ready[i] = -1ul32; } } else { sq.ready_cnt = 0; sq.ready = 0p; } // completion queue cq.head = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.head); cq.tail = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail); cq.mask = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask); cq.num = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries); cq.overflow = ( uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow); cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes); // some paranoid checks /* paranoid */ verifyf( (*cq.mask) == ((*cq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*cq.num) - 1ul32, *cq.num, *cq.mask ); /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num ); /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head ); /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail ); /* paranoid */ verifyf( (*sq.mask) == ((*sq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*sq.num) - 1ul32, *sq.num, *sq.mask ); /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num ); /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head ); /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail ); // Update the global ring info this.io->ring_flags = params.flags; this.io->cltr_flags = io_flags; this.io->fd = fd; this.io->done = false; (this.io->submit){ min(*sq.num, *cq.num) }; if(!main_cluster) { __kernel_io_finish_start( this ); } } void __kernel_io_finish_start( cluster & this ) { if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) { __cfadbg_print_safe(io_core, "Kernel I/O : Creating fast poller for cluter %p\n", &this); (this.io->poller.fast){ this }; __thrd_start( this.io->poller.fast, main ); } // Create the poller thread __cfadbg_print_safe(io_core, "Kernel I/O : Creating slow poller for cluter %p\n", &this); this.io->poller.slow.blocked = false; this.io->poller.slow.stack = __create_pthread( &this.io->poller.slow.kthrd, __io_poller_slow, &this ); } void __kernel_io_prepare_stop( cluster & this ) { __cfadbg_print_safe(io_core, "Kernel I/O : Stopping pollers for cluster\n", &this); // Notify the poller thread of the shutdown __atomic_store_n(&this.io->done, true, __ATOMIC_SEQ_CST); // Stop the IO Poller sigval val = { 1 }; pthread_sigqueue( this.io->poller.slow.kthrd, SIGUSR1, val ); post( this.io->poller.sem ); // Wait for the poller thread to finish pthread_join( this.io->poller.slow.kthrd, 0p ); free( this.io->poller.slow.stack ); __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller stopped for cluster\n", &this); if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) { with( this.io->poller.fast ) { /* paranoid */ verify( this.nprocessors == 0 || &this == mainCluster ); /* paranoid */ verify( !ready_mutate_islocked() ); // We need to adjust the clean-up based on where the thread is if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) { ready_schedule_lock( (struct __processor_id_t *)active_processor() ); // This is the tricky case // The thread was preempted and now it is on the ready queue // The thread should be the last on the list /* paranoid */ verify( thrd.link.next != 0p ); // Remove the thread from the ready queue of this cluster __attribute__((unused)) bool removed = remove_head( &this, &thrd ); /* paranoid */ verify( removed ); thrd.link.next = 0p; thrd.link.prev = 0p; __cfaabi_dbg_debug_do( thrd.unpark_stale = true ); // Fixup the thread state thrd.state = Blocked; thrd.ticket = 0; thrd.preempted = __NO_PREEMPTION; ready_schedule_unlock( (struct __processor_id_t *)active_processor() ); // Pretend like the thread was blocked all along } // !!! This is not an else if !!! if( thrd.state == Blocked ) { // This is the "easy case" // The thread is parked and can easily be moved to active cluster verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster ); thrd.curr_cluster = active_cluster(); // unpark the fast io_poller unpark( &thrd __cfaabi_dbg_ctx2 ); } else { // The thread is in a weird state // I don't know what to do here abort("Fast poller thread is in unexpected state, cannot clean-up correctly\n"); } } ^(this.io->poller.fast){}; __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller stopped for cluster\n", &this); } } void __kernel_io_shutdown( cluster & this, bool main_cluster ) { if(!main_cluster) { __kernel_io_prepare_stop( this ); } // Shutdown the io rings struct __submition_data & sq = this.io->submit_q; struct __completion_data & cq = this.io->completion_q; // unmap the submit queue entries munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe)); // unmap the Submit Queue ring munmap(sq.ring_ptr, sq.ring_sz); // unmap the Completion Queue ring, if it is different if (cq.ring_ptr != sq.ring_ptr) { munmap(cq.ring_ptr, cq.ring_sz); } // close the file descriptor close(this.io->fd); free( this.io->submit_q.ready ); // Maybe null, doesn't matter free( this.io ); } //============================================================================================= // I/O Polling //============================================================================================= static unsigned __collect_submitions( struct __io_data & ring ); static uint32_t __release_consumed_submission( struct __io_data & ring ); // Process a single completion message from the io_uring // This is NOT thread-safe static [int, bool] __drain_io( & struct __io_data ring, * sigset_t mask, int waitcnt, bool in_kernel ) { /* paranoid */ verify( !kernelTLS.preemption_state.enabled ); const uint32_t smask = *ring.submit_q.mask; unsigned to_submit = 0; if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) { // If the poller thread also submits, then we need to aggregate the submissions which are ready to_submit = __collect_submitions( ring ); } if (to_submit > 0 || waitcnt > 0) { int ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, waitcnt, IORING_ENTER_GETEVENTS, mask, _NSIG / 8); if( ret < 0 ) { switch((int)errno) { case EAGAIN: case EINTR: return [0, true]; default: abort( "KERNEL ERROR: IO_URING WAIT - %s\n", strerror(errno) ); } } // Release the consumed SQEs __release_consumed_submission( ring ); // update statistics __STATS__( true, if( to_submit > 0 ) { io.submit_q.submit_avg.rdy += to_submit; io.submit_q.submit_avg.csm += ret; io.submit_q.submit_avg.cnt += 1; } ) } // Memory barrier __atomic_thread_fence( __ATOMIC_SEQ_CST ); // Drain the queue unsigned head = *ring.completion_q.head; unsigned tail = *ring.completion_q.tail; const uint32_t mask = *ring.completion_q.mask; // Nothing was new return 0 if (head == tail) { return [0, to_submit > 0]; } uint32_t count = tail - head; /* paranoid */ verify( count != 0 ); for(i; count) { unsigned idx = (head + i) & mask; struct io_uring_cqe & cqe = ring.completion_q.cqes[idx]; /* paranoid */ verify(&cqe); struct __io_user_data_t * data = (struct __io_user_data_t *)(uintptr_t)cqe.user_data; __cfadbg_print_safe( io, "Kernel I/O : Performed reading io cqe %p, result %d for %p\n", data, cqe.res, data->thrd ); data->result = cqe.res; if(!in_kernel) { unpark( data->thrd __cfaabi_dbg_ctx2 ); } else { __unpark( &ring.poller.slow.id, data->thrd __cfaabi_dbg_ctx2 ); } } // Allow new submissions to happen // V(ring.submit, count); // 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_thread_fence( __ATOMIC_SEQ_CST ); __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_RELAXED ); return [count, count > 0 || to_submit > 0]; } static void * __io_poller_slow( void * arg ) { #if !defined( __CFA_NO_STATISTICS__ ) __stats_t local_stats; __init_stats( &local_stats ); kernelTLS.this_stats = &local_stats; #endif cluster * cltr = (cluster *)arg; struct __io_data & ring = *cltr->io; ring.poller.slow.id.id = doregister( &ring.poller.slow.id ); sigset_t mask; sigfillset(&mask); if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) { abort( "KERNEL ERROR: IO_URING - pthread_sigmask" ); } sigdelset( &mask, SIGUSR1 ); verify( (*ring.submit_q.head) == (*ring.submit_q.tail) ); verify( (*ring.completion_q.head) == (*ring.completion_q.tail) ); __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p ready\n", &ring); if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) { while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) { __atomic_store_n( &ring.poller.slow.blocked, true, __ATOMIC_SEQ_CST ); // In the user-thread approach drain and if anything was drained, // batton pass to the user-thread int count; bool again; [count, again] = __drain_io( ring, &mask, 1, true ); __atomic_store_n( &ring.poller.slow.blocked, false, __ATOMIC_SEQ_CST ); // Update statistics __STATS__( true, io.complete_q.completed_avg.val += count; io.complete_q.completed_avg.slow_cnt += 1; ) if(again) { __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to fast poller\n", &ring); __unpark( &ring.poller.slow.id, &ring.poller.fast.thrd __cfaabi_dbg_ctx2 ); wait( ring.poller.sem ); } } } else { while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) { //In the naive approach, just poll the io completion queue directly int count; bool again; [count, again] = __drain_io( ring, &mask, 1, true ); // Update statistics __STATS__( true, io.complete_q.completed_avg.val += count; io.complete_q.completed_avg.slow_cnt += 1; ) } } __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p stopping\n", &ring); unregister( &ring.poller.slow.id ); #if !defined(__CFA_NO_STATISTICS__) __tally_stats(cltr->stats, &local_stats); #endif return 0p; } void main( __io_poller_fast & this ) { verify( this.ring->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ); // Start parked park( __cfaabi_dbg_ctx ); __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p ready\n", &this.ring); int reset = 0; // Then loop until we need to start while(!__atomic_load_n(&this.ring->done, __ATOMIC_SEQ_CST)) { // Drain the io int count; bool again; disable_interrupts(); [count, again] = __drain_io( *this.ring, 0p, 0, false ); if(!again) reset++; // Update statistics __STATS__( true, io.complete_q.completed_avg.val += count; io.complete_q.completed_avg.fast_cnt += 1; ) enable_interrupts( __cfaabi_dbg_ctx ); // If we got something, just yield and check again if(reset < 5) { yield(); } // We didn't get anything baton pass to the slow poller else { __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to slow poller\n", &this.ring); reset = 0; // wake up the slow poller post( this.ring->poller.sem ); // park this thread park( __cfaabi_dbg_ctx ); } } __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p stopping\n", &this.ring); } static inline void __wake_poller( struct __io_data & ring ) __attribute__((artificial)); static inline void __wake_poller( struct __io_data & ring ) { if(!__atomic_load_n( &ring.poller.slow.blocked, __ATOMIC_SEQ_CST)) return; sigval val = { 1 }; pthread_sigqueue( ring.poller.slow.kthrd, SIGUSR1, val ); } //============================================================================================= // 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. // [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ) { /* paranoid */ verify( data != 0 ); // Prepare the data we need __attribute((unused)) int len = 0; __attribute((unused)) int block = 0; uint32_t cnt = *ring.submit_q.num; uint32_t mask = *ring.submit_q.mask; disable_interrupts(); uint32_t off = __tls_rand(); enable_interrupts( __cfaabi_dbg_ctx ); // Loop around looking for an available spot for() { // Look through the list starting at some offset for(i; cnt) { uint64_t expected = 0; uint32_t idx = (i + off) & mask; struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx]; volatile uint64_t * udata = &sqe->user_data; if( *udata == expected && __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) { // update statistics __STATS__( false, io.submit_q.alloc_avg.val += len; io.submit_q.alloc_avg.block += block; io.submit_q.alloc_avg.cnt += 1; ) // Success return the data return [sqe, idx]; } verify(expected != data); len ++; } block++; yield(); } } static inline uint32_t __submit_to_ready_array( struct __io_data & ring, uint32_t idx, const uint32_t mask ) { /* paranoid */ verify( idx <= mask ); /* paranoid */ verify( idx != -1ul32 ); // We need to find a spot in the ready array __attribute((unused)) int len = 0; __attribute((unused)) int block = 0; uint32_t ready_mask = ring.submit_q.ready_cnt - 1; disable_interrupts(); uint32_t off = __tls_rand(); enable_interrupts( __cfaabi_dbg_ctx ); uint32_t picked; LOOKING: for() { for(i; ring.submit_q.ready_cnt) { picked = (i + off) & ready_mask; uint32_t expected = -1ul32; if( __atomic_compare_exchange_n( &ring.submit_q.ready[picked], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) { break LOOKING; } verify(expected != idx); len ++; } block++; if( try_lock(ring.submit_q.lock __cfaabi_dbg_ctx2) ) { __release_consumed_submission( ring ); unlock( ring.submit_q.lock ); } else { yield(); } } // update statistics __STATS__( false, io.submit_q.look_avg.val += len; io.submit_q.look_avg.block += block; io.submit_q.look_avg.cnt += 1; ) return picked; } void __submit( struct __io_data & ring, uint32_t idx ) { // Get now the data we definetely need uint32_t * const tail = ring.submit_q.tail; const uint32_t mask = *ring.submit_q.mask; // There are 2 submission schemes, check which one we are using if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) { // If the poller thread submits, then we just need to add this to the ready array __submit_to_ready_array( ring, idx, mask ); __wake_poller( ring ); __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() ); } else if( ring.cltr_flags & CFA_CLUSTER_IO_EAGER_SUBMITS ) { uint32_t picked = __submit_to_ready_array( ring, idx, mask ); for() { yield(); // If some one else collected our index, we are done #warning ABA problem if( ring.submit_q.ready[picked] != idx ) { __STATS__( false, io.submit_q.helped += 1; ) return; } if( try_lock(ring.submit_q.lock __cfaabi_dbg_ctx2) ) { __STATS__( false, io.submit_q.leader += 1; ) break; } __STATS__( false, io.submit_q.busy += 1; ) } // We got the lock unsigned to_submit = __collect_submitions( ring ); int ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, 0, 0, 0p, _NSIG / 8); if( ret < 0 ) { switch((int)errno) { case EAGAIN: case EINTR: unlock(ring.submit_q.lock); return; default: abort( "KERNEL ERROR: IO_URING WAIT - %s\n", strerror(errno) ); } } /* paranoid */ verify( ret > 0 ); // Release the consumed SQEs __release_consumed_submission( ring ); // update statistics __STATS__( true, io.submit_q.submit_avg.rdy += to_submit; io.submit_q.submit_avg.csm += ret; io.submit_q.submit_avg.cnt += 1; ) unlock(ring.submit_q.lock); } else { // get mutual exclusion lock(ring.submit_q.lock __cfaabi_dbg_ctx2); // Append to the list of ready entries /* paranoid */ verify( idx <= mask ); ring.submit_q.array[ (*tail) & mask ] = idx & mask; __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST); // Submit however, many entries need to be submitted int ret = syscall( __NR_io_uring_enter, ring.fd, 1, 0, 0, 0p, 0); if( ret < 0 ) { switch((int)errno) { default: abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) ); } } // update statistics __STATS__( false, io.submit_q.submit_avg.csm += 1; io.submit_q.submit_avg.cnt += 1; ) // Release the consumed SQEs __release_consumed_submission( ring ); unlock(ring.submit_q.lock); __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret ); } } static unsigned __collect_submitions( struct __io_data & ring ) { /* paranoid */ verify( ring.submit_q.ready != 0p ); /* paranoid */ verify( ring.submit_q.ready_cnt > 0 ); unsigned to_submit = 0; uint32_t tail = *ring.submit_q.tail; const uint32_t mask = *ring.submit_q.mask; // Go through the list of ready submissions for( i; ring.submit_q.ready_cnt ) { // replace any submission with the sentinel, to consume it. uint32_t idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED); // If it was already the sentinel, then we are done if( idx == -1ul32 ) continue; // If we got a real submission, append it to the list ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask; to_submit++; } // Increment the tail based on how many we are ready to submit __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST); return to_submit; } static uint32_t __release_consumed_submission( struct __io_data & ring ) { const uint32_t smask = *ring.submit_q.mask; uint32_t chead = *ring.submit_q.head; uint32_t phead = ring.submit_q.prev_head; ring.submit_q.prev_head = chead; uint32_t count = chead - phead; for( i; count ) { uint32_t idx = ring.submit_q.array[ (phead + i) & smask ]; ring.submit_q.sqes[ idx ].user_data = 0; } return count; } #endif