| 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/setup.cfa --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Thierry Delisle
|
|---|
| 10 | // Created On : Fri Jul 31 16:25:51 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 | #include "io/types.hfa"
|
|---|
| 25 | #include "kernel.hfa"
|
|---|
| 26 |
|
|---|
| 27 | #if !defined(CFA_HAVE_LINUX_IO_URING_H)
|
|---|
| 28 | void ?{}(io_context_params & this) libcfa_public {}
|
|---|
| 29 |
|
|---|
| 30 | void ?{}(io_context$ & this, struct cluster & cl) {}
|
|---|
| 31 | void ^?{}(io_context$ & this) {}
|
|---|
| 32 |
|
|---|
| 33 | void __cfa_io_start( processor * proc ) {}
|
|---|
| 34 | bool __cfa_io_flush( processor * proc ) { return false; }
|
|---|
| 35 | bool __cfa_io_drain( processor * proc ) __attribute__((nonnull (1))) { return false; }
|
|---|
| 36 | void __cfa_io_stop ( processor * proc ) {}
|
|---|
| 37 |
|
|---|
| 38 | io_arbiter$ * create(void) { return 0p; }
|
|---|
| 39 | void destroy(io_arbiter$ *) {}
|
|---|
| 40 |
|
|---|
| 41 | #else
|
|---|
| 42 | #pragma GCC diagnostic push
|
|---|
| 43 | #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <stdint.h>
|
|---|
| 46 | #include <string.h>
|
|---|
| 47 | #include <signal.h>
|
|---|
| 48 | #include <unistd.h>
|
|---|
| 49 |
|
|---|
| 50 | extern "C" {
|
|---|
| 51 | #include <pthread.h>
|
|---|
| 52 | #include <sys/epoll.h>
|
|---|
| 53 | #include <sys/eventfd.h>
|
|---|
| 54 | #include <sys/mman.h>
|
|---|
| 55 | #include <sys/syscall.h>
|
|---|
| 56 |
|
|---|
| 57 | #include <linux/io_uring.h>
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | #include "bitmanip.hfa"
|
|---|
| 61 | #include "fstream.hfa"
|
|---|
| 62 | #include "kernel/private.hfa"
|
|---|
| 63 | #include "limits.hfa"
|
|---|
| 64 | #include "thread.hfa"
|
|---|
| 65 | #pragma GCC diagnostic pop
|
|---|
| 66 |
|
|---|
| 67 | void ?{}(io_context_params & this) libcfa_public {
|
|---|
| 68 | this.num_entries = 256;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static void * __io_poller_slow( void * arg );
|
|---|
| 72 |
|
|---|
| 73 | // Weirdly, some systems that do support io_uring don't actually define these
|
|---|
| 74 | #ifdef __alpha__
|
|---|
| 75 | /*
|
|---|
| 76 | * alpha is the only exception, all other architectures
|
|---|
| 77 | * have common numbers for new system calls.
|
|---|
| 78 | */
|
|---|
| 79 | #ifndef __NR_io_uring_setup
|
|---|
| 80 | #define __NR_io_uring_setup 535
|
|---|
| 81 | #endif
|
|---|
| 82 | #ifndef __NR_io_uring_enter
|
|---|
| 83 | #define __NR_io_uring_enter 536
|
|---|
| 84 | #endif
|
|---|
| 85 | #ifndef __NR_io_uring_register
|
|---|
| 86 | #define __NR_io_uring_register 537
|
|---|
| 87 | #endif
|
|---|
| 88 | #else /* !__alpha__ */
|
|---|
| 89 | #ifndef __NR_io_uring_setup
|
|---|
| 90 | #define __NR_io_uring_setup 425
|
|---|
| 91 | #endif
|
|---|
| 92 | #ifndef __NR_io_uring_enter
|
|---|
| 93 | #define __NR_io_uring_enter 426
|
|---|
| 94 | #endif
|
|---|
| 95 | #ifndef __NR_io_uring_register
|
|---|
| 96 | #define __NR_io_uring_register 427
|
|---|
| 97 | #endif
|
|---|
| 98 | #endif
|
|---|
| 99 |
|
|---|
| 100 | //=============================================================================================
|
|---|
| 101 | // I/O Context Constrution/Destruction
|
|---|
| 102 | //=============================================================================================
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | static void __io_uring_setup ( io_context$ & this, const io_context_params & params_in, int procfd );
|
|---|
| 107 | static void __io_uring_teardown( io_context$ & this );
|
|---|
| 108 | static void __epoll_register(io_context$ & ctx);
|
|---|
| 109 | static void __epoll_unregister(io_context$ & ctx);
|
|---|
| 110 | void __ioarbiter_register( io_arbiter$ & mutex, io_context$ & ctx );
|
|---|
| 111 | void __ioarbiter_unregister( io_arbiter$ & mutex, io_context$ & ctx );
|
|---|
| 112 |
|
|---|
| 113 | void ?{}(io_context$ & this, processor * proc, struct cluster & cl) {
|
|---|
| 114 | /* paranoid */ verify( cl.io.arbiter );
|
|---|
| 115 | this.proc = proc;
|
|---|
| 116 | this.arbiter = cl.io.arbiter;
|
|---|
| 117 | this.ext_sq.empty = true;
|
|---|
| 118 | (this.ext_sq.queue){};
|
|---|
| 119 | __io_uring_setup( this, cl.io.params, proc->idle_wctx.evfd );
|
|---|
| 120 | __cfadbg_print_safe(io_core, "Kernel I/O : Created ring for io_context %u (%p)\n", this.fd, &this);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void ^?{}(io_context$ & this) {
|
|---|
| 124 | __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %u\n", this.fd);
|
|---|
| 125 |
|
|---|
| 126 | __io_uring_teardown( this );
|
|---|
| 127 | __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %u\n", this.fd);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static void __io_uring_setup( io_context$ & this, const io_context_params & params_in, int procfd ) {
|
|---|
| 131 | // Step 1 : call to setup
|
|---|
| 132 | struct io_uring_params params;
|
|---|
| 133 | memset(¶ms, 0, sizeof(params));
|
|---|
| 134 | // if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL;
|
|---|
| 135 | // if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL;
|
|---|
| 136 |
|
|---|
| 137 | __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256;
|
|---|
| 138 | if( !is_pow2(nentries) ) {
|
|---|
| 139 | abort("ERROR: I/O setup 'num_entries' must be a power of 2, was %u\n", nentries);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | int fd = syscall(__NR_io_uring_setup, nentries, ¶ms );
|
|---|
| 143 | if(fd < 0) {
|
|---|
| 144 | abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | // Step 2 : mmap result
|
|---|
| 148 | struct __sub_ring_t & sq = this.sq;
|
|---|
| 149 | struct __cmp_ring_t & cq = this.cq;
|
|---|
| 150 |
|
|---|
| 151 | // calculate the right ring size
|
|---|
| 152 | sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) );
|
|---|
| 153 | cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe));
|
|---|
| 154 |
|
|---|
| 155 | // Requires features
|
|---|
| 156 | #if defined(IORING_FEAT_SINGLE_MMAP)
|
|---|
| 157 | // adjust the size according to the parameters
|
|---|
| 158 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
|
|---|
| 159 | cq.ring_sz = sq.ring_sz = max(cq.ring_sz, sq.ring_sz);
|
|---|
| 160 | }
|
|---|
| 161 | #endif
|
|---|
| 162 |
|
|---|
| 163 | // mmap the Submit Queue into existence
|
|---|
| 164 | sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
|
|---|
| 165 | if (sq.ring_ptr == (void*)MAP_FAILED) {
|
|---|
| 166 | abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | // Requires features
|
|---|
| 170 | #if defined(IORING_FEAT_SINGLE_MMAP)
|
|---|
| 171 | // mmap the Completion Queue into existence (may or may not be needed)
|
|---|
| 172 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
|
|---|
| 173 | cq.ring_ptr = sq.ring_ptr;
|
|---|
| 174 | }
|
|---|
| 175 | else
|
|---|
| 176 | #endif
|
|---|
| 177 | {
|
|---|
| 178 | // We need multiple call to MMAP
|
|---|
| 179 | cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
|
|---|
| 180 | if (cq.ring_ptr == (void*)MAP_FAILED) {
|
|---|
| 181 | munmap(sq.ring_ptr, sq.ring_sz);
|
|---|
| 182 | abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | // mmap the submit queue entries
|
|---|
| 187 | size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
|
|---|
| 188 | sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
|
|---|
| 189 | if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) {
|
|---|
| 190 | munmap(sq.ring_ptr, sq.ring_sz);
|
|---|
| 191 | if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz);
|
|---|
| 192 | abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno));
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | // Step 3 : Initialize the data structure
|
|---|
| 196 | // Get the pointers from the kernel to fill the structure
|
|---|
| 197 | // submit queue
|
|---|
| 198 | sq.kring.head = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
|
|---|
| 199 | sq.kring.tail = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
|
|---|
| 200 | sq.kring.array = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
|
|---|
| 201 | sq.mask = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
|
|---|
| 202 | sq.num = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
|
|---|
| 203 | sq.flags = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
|
|---|
| 204 | sq.dropped = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
|
|---|
| 205 |
|
|---|
| 206 | sq.kring.released = 0;
|
|---|
| 207 |
|
|---|
| 208 | sq.free_ring.head = 0;
|
|---|
| 209 | sq.free_ring.tail = *sq.num;
|
|---|
| 210 | sq.free_ring.array = alloc( *sq.num, 128`align );
|
|---|
| 211 | for(i; (__u32)*sq.num) {
|
|---|
| 212 | sq.free_ring.array[i] = i;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | sq.to_submit = 0;
|
|---|
| 216 |
|
|---|
| 217 | // completion queue
|
|---|
| 218 | cq.lock = false;
|
|---|
| 219 | cq.id = MAX;
|
|---|
| 220 | cq.ts = rdtscl();
|
|---|
| 221 | cq.head = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.head);
|
|---|
| 222 | cq.tail = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail);
|
|---|
| 223 | cq.mask = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask);
|
|---|
| 224 | cq.num = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries);
|
|---|
| 225 | cq.overflow = ( __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow);
|
|---|
| 226 | cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes);
|
|---|
| 227 |
|
|---|
| 228 | #if !defined(CFA_WITH_IO_URING_IDLE)
|
|---|
| 229 | {
|
|---|
| 230 | // Step 4 : eventfd
|
|---|
| 231 | __cfadbg_print_safe(io_core, "Kernel I/O : registering %d for completion with ring %d\n", procfd, fd);
|
|---|
| 232 |
|
|---|
| 233 | int ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &procfd, 1);
|
|---|
| 234 | if (ret < 0) {
|
|---|
| 235 | abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno));
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | __cfadbg_print_safe(io_core, "Kernel I/O : registered %d for completion with ring %d\n", procfd, fd);
|
|---|
| 239 | }
|
|---|
| 240 | #endif
|
|---|
| 241 |
|
|---|
| 242 | // TODO: implement a proper version of this.
|
|---|
| 243 | // I have not found a better maximum that works in general but users should be able to configure it
|
|---|
| 244 | // the same way they configure other I/O options
|
|---|
| 245 | // #if defined(CFA_HAVE_IORING_REGISTER_IOWQ_MAX_WORKERS)
|
|---|
| 246 | // {
|
|---|
| 247 | // // Step 5 : max worker count
|
|---|
| 248 | // __cfadbg_print_safe(io_core, "Kernel I/O : lmiting max workers for ring %d\n", fd);
|
|---|
| 249 |
|
|---|
| 250 | // unsigned int maxes[2];
|
|---|
| 251 | // maxes[0] = 64; // max number of bounded workers (Regular files / block)
|
|---|
| 252 | // maxes[1] = 64; // max number of unbounded workers (IOSQE_ASYNC)
|
|---|
| 253 | // int ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_IOWQ_MAX_WORKERS, maxes, 2);
|
|---|
| 254 | // if (ret < 0) {
|
|---|
| 255 | // abort("KERNEL ERROR: IO_URING MAX WORKER REGISTER - %s\n", strerror(errno));
|
|---|
| 256 | // }
|
|---|
| 257 |
|
|---|
| 258 | // __cfadbg_print_safe(io_core, "Kernel I/O : lmited max workers for ring %d\n", fd);
|
|---|
| 259 | // }
|
|---|
| 260 | // #endif
|
|---|
| 261 |
|
|---|
| 262 | // some paranoid checks
|
|---|
| 263 | /* 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 );
|
|---|
| 264 | /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num );
|
|---|
| 265 | /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head );
|
|---|
| 266 | /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail );
|
|---|
| 267 |
|
|---|
| 268 | /* 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 );
|
|---|
| 269 | /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
|
|---|
| 270 | /* paranoid */ verifyf( (*sq.kring.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.kring.head );
|
|---|
| 271 | /* paranoid */ verifyf( (*sq.kring.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.kring.tail );
|
|---|
| 272 |
|
|---|
| 273 | // Update the global ring info
|
|---|
| 274 | this.ring_flags = 0;
|
|---|
| 275 | this.fd = fd;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | static void __io_uring_teardown( io_context$ & this ) {
|
|---|
| 279 | // Shutdown the io rings
|
|---|
| 280 | struct __sub_ring_t & sq = this.sq;
|
|---|
| 281 | struct __cmp_ring_t & cq = this.cq;
|
|---|
| 282 | {
|
|---|
| 283 | __u32 fhead = sq.free_ring.head;
|
|---|
| 284 | __u32 ftail = sq.free_ring.tail;
|
|---|
| 285 |
|
|---|
| 286 | __u32 total = *sq.num;
|
|---|
| 287 | __u32 avail = ftail - fhead;
|
|---|
| 288 |
|
|---|
| 289 | if(avail != total) abort | "Processor (" | (void*)this.proc | ") tearing down ring with" | (total - avail) | "entries allocated but not submitted, out of" | total;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | // unmap the submit queue entries
|
|---|
| 293 | munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
|
|---|
| 294 |
|
|---|
| 295 | // unmap the Submit Queue ring
|
|---|
| 296 | munmap(sq.ring_ptr, sq.ring_sz);
|
|---|
| 297 |
|
|---|
| 298 | // unmap the Completion Queue ring, if it is different
|
|---|
| 299 | if (cq.ring_ptr != sq.ring_ptr) {
|
|---|
| 300 | munmap(cq.ring_ptr, cq.ring_sz);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | // close the file descriptor
|
|---|
| 304 | close(this.fd);
|
|---|
| 305 |
|
|---|
| 306 | free( this.sq.free_ring.array ); // Maybe null, doesn't matter
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | void __cfa_io_start( processor * proc ) {
|
|---|
| 310 | proc->io.ctx = alloc();
|
|---|
| 311 | (*proc->io.ctx){proc, *proc->cltr};
|
|---|
| 312 | }
|
|---|
| 313 | void __cfa_io_stop ( processor * proc ) {
|
|---|
| 314 | ^(*proc->io.ctx){};
|
|---|
| 315 | free(proc->io.ctx);
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 | //=============================================================================================
|
|---|
| 320 | // I/O Context Misc Setup
|
|---|
| 321 | //=============================================================================================
|
|---|
| 322 | void ?{}( io_arbiter$ & this ) {
|
|---|
| 323 | this.pending.empty = true;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | void ^?{}( io_arbiter$ & mutex this ) {}
|
|---|
| 327 |
|
|---|
| 328 | io_arbiter$ * create(void) {
|
|---|
| 329 | return new();
|
|---|
| 330 | }
|
|---|
| 331 | void destroy(io_arbiter$ * arbiter) {
|
|---|
| 332 | delete(arbiter);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | //=============================================================================================
|
|---|
| 336 | // I/O Context Misc Setup
|
|---|
| 337 | //=============================================================================================
|
|---|
| 338 |
|
|---|
| 339 | #endif
|
|---|