// // 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/setup.cfa -- // // Author : Thierry Delisle // Created On : Fri Jul 31 16:25:51 2020 // Last Modified By : // Last Modified On : // Update Count : // #define __cforall_thread__ #define _GNU_SOURCE /* See feature_test_macros(7) */ #if defined(__CFA_DEBUG__) // #define __CFA_DEBUG_PRINT_IO__ // #define __CFA_DEBUG_PRINT_IO_CORE__ #endif #include "io/types.hfa" #include "kernel.hfa" #if !defined(CFA_HAVE_LINUX_IO_URING_H) void __kernel_io_startup() { // Nothing to do without io_uring } void __kernel_io_shutdown() { // Nothing to do without io_uring } void ?{}(io_context_params & this) {} void ?{}(io_context & this, struct cluster & cl) {} void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {} void ^?{}(io_context & this) {} void ^?{}(io_context & this, bool cluster_context) {} #else #include #include #include #include #include extern "C" { #include #include #include #include #include #include } #include "bitmanip.hfa" #include "kernel_private.hfa" #include "thread.hfa" void ?{}(io_context_params & this) { this.num_entries = 256; this.num_ready = 256; this.submit_aff = -1; this.eager_submits = false; this.poller_submits = false; this.poll_submit = false; this.poll_complete = false; } 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 //============================================================================================= // I/O Startup / Shutdown logic + Master Poller //============================================================================================= // IO Master poller loop forward static void * iopoll_loop( __attribute__((unused)) void * args ); static struct { pthread_t thrd; // pthread handle to io poller thread void * stack; // pthread stack for io poller thread int epollfd; // file descriptor to the epoll instance volatile bool run; // Whether or not to continue } iopoll; void __kernel_io_startup(void) { __cfadbg_print_safe(io_core, "Kernel : Creating EPOLL instance\n" ); iopoll.epollfd = epoll_create1(0); if (iopoll.epollfd == -1) { abort( "internal error, epoll_create1\n"); } __cfadbg_print_safe(io_core, "Kernel : Starting io poller thread\n" ); iopoll.run = true; iopoll.stack = __create_pthread( &iopoll.thrd, iopoll_loop, 0p ); } void __kernel_io_shutdown(void) { // Notify the io poller thread of the shutdown iopoll.run = false; sigval val = { 1 }; pthread_sigqueue( iopoll.thrd, SIGUSR1, val ); // Wait for the io poller thread to finish __destroy_pthread( iopoll.thrd, iopoll.stack, 0p ); int ret = close(iopoll.epollfd); if (ret == -1) { abort( "internal error, close epoll\n"); } // Io polling is now fully stopped __cfadbg_print_safe(io_core, "Kernel : IO poller stopped\n" ); } static void * iopoll_loop( __attribute__((unused)) void * args ) { __processor_id_t id; id.full_proc = false; id.id = doregister(&id); __cfaabi_tls.this_proc_id = &id; __cfadbg_print_safe(io_core, "Kernel : IO poller thread starting\n" ); // Block signals to control when they arrive sigset_t mask; sigfillset(&mask); if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) { abort( "internal error, pthread_sigmask" ); } sigdelset( &mask, SIGUSR1 ); // Create sufficient events struct epoll_event events[10]; // Main loop while( iopoll.run ) { __cfadbg_print_safe(io_core, "Kernel I/O - epoll : waiting on io_uring contexts\n"); // Wait for events int nfds = epoll_pwait( iopoll.epollfd, events, 10, -1, &mask ); __cfadbg_print_safe(io_core, "Kernel I/O - epoll : %d io contexts events, waking up\n", nfds); // Check if an error occured if (nfds == -1) { if( errno == EINTR ) continue; abort( "internal error, pthread_sigmask" ); } for(i; nfds) { $io_ctx_thread * io_ctx = ($io_ctx_thread *)(uintptr_t)events[i].data.u64; /* paranoid */ verify( io_ctx ); __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Unparking io poller %d (%p)\n", io_ctx->ring->fd, io_ctx); #if !defined( __CFA_NO_STATISTICS__ ) __cfaabi_tls.this_stats = io_ctx->self.curr_cluster->stats; #endif eventfd_t v; eventfd_read(io_ctx->ring->efd, &v); post( io_ctx->sem ); } } __cfadbg_print_safe(io_core, "Kernel : IO poller thread stopping\n" ); unregister(&id); return 0p; } //============================================================================================= // I/O Context Constrution/Destruction //============================================================================================= void ?{}($io_ctx_thread & this, struct cluster & cl) { (this.self){ "IO Poller", cl }; } void main( $io_ctx_thread & this ); static inline $thread * get_thread( $io_ctx_thread & this ) { return &this.self; } void ^?{}( $io_ctx_thread & mutex this ) {} static void __io_create ( __io_data & this, const io_context_params & params_in ); static void __io_destroy( __io_data & this ); void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) { (this.thrd){ cl }; this.thrd.ring = malloc(); __cfadbg_print_safe(io_core, "Kernel I/O : Creating ring for io_context %p\n", &this); __io_create( *this.thrd.ring, params ); __cfadbg_print_safe(io_core, "Kernel I/O : Starting poller thread for io_context %p\n", &this); this.thrd.done = false; __thrd_start( this.thrd, main ); __cfadbg_print_safe(io_core, "Kernel I/O : io_context %p ready\n", &this); } void ?{}(io_context & this, struct cluster & cl) { io_context_params params; (this){ cl, params }; } void ^?{}(io_context & this, bool cluster_context) { __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %p\n", &this); // Notify the thread of the shutdown __atomic_store_n(&this.thrd.done, true, __ATOMIC_SEQ_CST); // If this is an io_context within a cluster, things get trickier $thread & thrd = this.thrd.self; if( cluster_context ) { // We are about to do weird things with the threads // we don't need interrupts to complicate everything disable_interrupts(); // Get cluster info cluster & cltr = *thrd.curr_cluster; /* paranoid */ verify( cltr.idles.total == 0 || &cltr == 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 ) { // This is the tricky case // The thread was preempted or ready to run and now it is on the ready queue // but the cluster is shutting down, so there aren't any processors to run the ready queue // the solution is to steal the thread from the ready-queue and pretend it was blocked all along ready_schedule_lock(); // The thread should on the list /* paranoid */ verify( thrd.link.next != 0p ); // Remove the thread from the ready queue of this cluster // The thread should be the last on the list __attribute__((unused)) bool removed = remove_head( &cltr, &thrd ); /* paranoid */ verify( removed ); thrd.link.next = 0p; thrd.link.prev = 0p; // Fixup the thread state thrd.state = Blocked; thrd.ticket = TICKET_BLOCKED; thrd.preempted = __NO_PREEMPTION; ready_schedule_unlock(); // Pretend like the thread was blocked all along } // !!! This is not an else if !!! // Ok, now the thread is blocked (whether we cheated to get here or not) 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 ); } else { // The thread is in a weird state // I don't know what to do here abort("io_context poller thread is in unexpected state, cannot clean-up correctly\n"); } // The weird thread kidnapping stuff is over, restore interrupts. enable_interrupts( __cfaabi_dbg_ctx ); } else { post( this.thrd.sem ); } ^(this.thrd){}; __cfadbg_print_safe(io_core, "Kernel I/O : Stopped poller thread for io_context %p\n", &this); __io_destroy( *this.thrd.ring ); __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %p\n", &this); free(this.thrd.ring); } void ^?{}(io_context & this) { ^(this){ false }; } static void __io_create( __io_data & this, const io_context_params & params_in ) { // Step 1 : call to setup struct io_uring_params params; memset(¶ms, 0, sizeof(params)); if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL; if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL; __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256; if( !is_pow2(nentries) ) { abort("ERROR: I/O setup 'num_entries' must be a power of 2\n"); } if( params_in.poller_submits && params_in.eager_submits ) { abort("ERROR: I/O setup 'poller_submits' and 'eager_submits' cannot be used together\n"); } 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, 0, sizeof(struct __io_data) ); struct __submition_data & sq = this.submit_q; struct __completion_data & cq = this.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)); } // Step 3 : Initialize the data structure // Get the pointers from the kernel to fill the structure // submit queue sq.head = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head); sq.tail = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail); sq.mask = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask); sq.num = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries); sq.flags = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags); sq.dropped = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped); sq.array = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array); sq.prev_head = *sq.head; { const __u32 num = *sq.num; for( i; num ) { sq.sqes[i].opcode = IORING_OP_LAST; sq.sqes[i].user_data = 3ul64; } } (sq.submit_lock){}; (sq.release_lock){}; if( params_in.poller_submits || params_in.eager_submits ) { /* paranoid */ verify( is_pow2( params_in.num_ready ) || (params_in.num_ready < 8) ); sq.ready_cnt = max( params_in.num_ready, 8 ); sq.ready = alloc( sq.ready_cnt, 64`align ); for(i; sq.ready_cnt) { sq.ready[i] = -1ul32; } sq.prev_ready = 0; } else { sq.ready_cnt = 0; sq.ready = 0p; sq.prev_ready = 0; } // completion queue cq.head = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.head); cq.tail = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail); cq.mask = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask); cq.num = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries); cq.overflow = ( __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow); cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes); // Step 4 : eventfd int efd; for() { efd = eventfd(0, 0); if (efd < 0) { if (errno == EINTR) continue; abort("KERNEL ERROR: IO_URING EVENTFD - %s\n", strerror(errno)); } break; } int ret; for() { ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &efd, 1); if (ret < 0) { if (errno == EINTR) continue; abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno)); } break; } // 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.ring_flags = params.flags; this.fd = fd; this.efd = efd; this.eager_submits = params_in.eager_submits; this.poller_submits = params_in.poller_submits; } static void __io_destroy( __io_data & this ) { // Shutdown the io rings struct __submition_data & sq = this.submit_q; struct __completion_data & cq = this.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.fd); close(this.efd); free( this.submit_q.ready ); // Maybe null, doesn't matter } //============================================================================================= // I/O Context Sleep //============================================================================================= #define IOEVENTS EPOLLIN | EPOLLONESHOT static inline void __ioctx_epoll_ctl($io_ctx_thread & ctx, int op, const char * error) { struct epoll_event ev; ev.events = IOEVENTS; ev.data.u64 = (__u64)&ctx; int ret = epoll_ctl(iopoll.epollfd, op, ctx.ring->efd, &ev); if (ret < 0) { abort( "KERNEL ERROR: EPOLL %s - (%d) %s\n", error, (int)errno, strerror(errno) ); } } void __ioctx_register($io_ctx_thread & ctx) { __ioctx_epoll_ctl(ctx, EPOLL_CTL_ADD, "ADD"); } void __ioctx_prepare_block($io_ctx_thread & ctx) { __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Re-arming io poller %d (%p)\n", ctx.ring->fd, &ctx); __ioctx_epoll_ctl(ctx, EPOLL_CTL_MOD, "REARM"); } //============================================================================================= // I/O Context Misc Setup //============================================================================================= void register_fixed_files( io_context & ctx, int * files, unsigned count ) { int ret = syscall( __NR_io_uring_register, ctx.thrd.ring->fd, IORING_REGISTER_FILES, files, count ); if( ret < 0 ) { abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) ); } __cfadbg_print_safe( io_core, "Kernel I/O : Performed io_register for %p, returned %d\n", active_thread(), ret ); } void register_fixed_files( cluster & cltr, int * files, unsigned count ) { for(i; cltr.io.cnt) { register_fixed_files( cltr.io.ctxs[i], files, count ); } } #endif