[3e2b9c9] | 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__ |
---|
[43784ac] | 17 | #define _GNU_SOURCE |
---|
[3e2b9c9] | 18 | |
---|
[80444bb] | 19 | #if defined(__CFA_DEBUG__) |
---|
| 20 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
| 21 | // #define __CFA_DEBUG_PRINT_IO_CORE__ |
---|
| 22 | #endif |
---|
| 23 | |
---|
[3e2b9c9] | 24 | #include "io/types.hfa" |
---|
[c44d652] | 25 | #include "kernel.hfa" |
---|
[3e2b9c9] | 26 | |
---|
| 27 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[108345a] | 28 | void ?{}(io_context_params & this) libcfa_public {} |
---|
[f277633e] | 29 | |
---|
[8bee858] | 30 | void ?{}(io_context$ & this, struct cluster & cl) {} |
---|
| 31 | void ^?{}(io_context$ & this) {} |
---|
[3e2b9c9] | 32 | |
---|
[f815c46] | 33 | void __cfa_io_start( processor * proc ) {} |
---|
[18f7858] | 34 | bool __cfa_io_flush( processor * proc ) { return false; } |
---|
[7425720] | 35 | bool __cfa_io_drain( processor * proc ) __attribute__((nonnull (1))) { return false; } |
---|
[f815c46] | 36 | void __cfa_io_stop ( processor * proc ) {} |
---|
| 37 | |
---|
[8bee858] | 38 | io_arbiter$ * create(void) { return 0p; } |
---|
| 39 | void destroy(io_arbiter$ *) {} |
---|
[b7664a03] | 40 | |
---|
[3e2b9c9] | 41 | #else |
---|
[bfb9bf5] | 42 | #pragma GCC diagnostic push |
---|
| 43 | #pragma GCC diagnostic ignored "-Waddress-of-packed-member" |
---|
[3e2b9c9] | 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> |
---|
[426f60c] | 53 | #include <sys/eventfd.h> |
---|
[3e2b9c9] | 54 | #include <sys/mman.h> |
---|
| 55 | #include <sys/syscall.h> |
---|
| 56 | |
---|
| 57 | #include <linux/io_uring.h> |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | #include "bitmanip.hfa" |
---|
[b0d0285] | 61 | #include "fstream.hfa" |
---|
[708ae38] | 62 | #include "kernel/private.hfa" |
---|
[78a580d] | 63 | #include "limits.hfa" |
---|
[3e2b9c9] | 64 | #include "thread.hfa" |
---|
[bfb9bf5] | 65 | #pragma GCC diagnostic pop |
---|
[3e2b9c9] | 66 | |
---|
[108345a] | 67 | void ?{}(io_context_params & this) libcfa_public { |
---|
[3e2b9c9] | 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 | //============================================================================================= |
---|
[dddb3dd0] | 101 | // I/O Context Constrution/Destruction |
---|
[3e2b9c9] | 102 | //============================================================================================= |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
[8bee858] | 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 ); |
---|
[3e2b9c9] | 112 | |
---|
[8bee858] | 113 | void ?{}(io_context$ & this, processor * proc, struct cluster & cl) { |
---|
[dddb3dd0] | 114 | /* paranoid */ verify( cl.io.arbiter ); |
---|
| 115 | this.proc = proc; |
---|
| 116 | this.arbiter = cl.io.arbiter; |
---|
[78da4ab] | 117 | this.ext_sq.empty = true; |
---|
[11054eb] | 118 | (this.ext_sq.queue){}; |
---|
[22226e4] | 119 | __io_uring_setup( this, cl.io.params, proc->idle_wctx.evfd ); |
---|
[78da4ab] | 120 | __cfadbg_print_safe(io_core, "Kernel I/O : Created ring for io_context %u (%p)\n", this.fd, &this); |
---|
[3e2b9c9] | 121 | } |
---|
| 122 | |
---|
[8bee858] | 123 | void ^?{}(io_context$ & this) { |
---|
[78da4ab] | 124 | __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %u\n", this.fd); |
---|
[3e2b9c9] | 125 | |
---|
[78da4ab] | 126 | __io_uring_teardown( this ); |
---|
| 127 | __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %u\n", this.fd); |
---|
| 128 | } |
---|
[3e2b9c9] | 129 | |
---|
[8bee858] | 130 | static void __io_uring_setup( io_context$ & this, const io_context_params & params_in, int procfd ) { |
---|
[3e2b9c9] | 131 | // Step 1 : call to setup |
---|
| 132 | struct io_uring_params params; |
---|
| 133 | memset(¶ms, 0, sizeof(params)); |
---|
[78da4ab] | 134 | // if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL; |
---|
| 135 | // if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL; |
---|
[3e2b9c9] | 136 | |
---|
[4998155] | 137 | __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256; |
---|
[63fe427c] | 138 | if( !is_pow2(nentries) ) { |
---|
[bf0263c] | 139 | abort("ERROR: I/O setup 'num_entries' must be a power of 2, was %u\n", nentries); |
---|
[63fe427c] | 140 | } |
---|
[3e2b9c9] | 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 |
---|
[78da4ab] | 148 | struct __sub_ring_t & sq = this.sq; |
---|
| 149 | struct __cmp_ring_t & cq = this.cq; |
---|
[3e2b9c9] | 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 | |
---|
[426f60c] | 195 | // Step 3 : Initialize the data structure |
---|
[3e2b9c9] | 196 | // Get the pointers from the kernel to fill the structure |
---|
| 197 | // submit queue |
---|
[78da4ab] | 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; |
---|
[3e2b9c9] | 213 | } |
---|
| 214 | |
---|
[78da4ab] | 215 | sq.to_submit = 0; |
---|
[3e2b9c9] | 216 | |
---|
| 217 | // completion queue |
---|
[18f7858] | 218 | cq.lock = false; |
---|
[78a580d] | 219 | cq.id = MAX; |
---|
| 220 | cq.ts = rdtscl(); |
---|
[4998155] | 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); |
---|
[3e2b9c9] | 227 | |
---|
[d3605f8] | 228 | #if !defined(CFA_WITH_IO_URING_IDLE) |
---|
[19cb0cb] | 229 | { |
---|
[7ef162b2] | 230 | // Step 4 : eventfd |
---|
| 231 | __cfadbg_print_safe(io_core, "Kernel I/O : registering %d for completion with ring %d\n", procfd, fd); |
---|
[bb58825] | 232 | |
---|
[7ef162b2] | 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 | } |
---|
[426f60c] | 237 | |
---|
[7ef162b2] | 238 | __cfadbg_print_safe(io_core, "Kernel I/O : registered %d for completion with ring %d\n", procfd, fd); |
---|
[19cb0cb] | 239 | } |
---|
[7ef162b2] | 240 | #endif |
---|
[dddb3dd0] | 241 | |
---|
[19cb0cb] | 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 |
---|
[7ce8873] | 245 | // #if defined(CFA_HAVE_IORING_REGISTER_IOWQ_MAX_WORKERS) |
---|
[19cb0cb] | 246 | // { |
---|
[7ce8873] | 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); |
---|
[19cb0cb] | 259 | // } |
---|
[7ce8873] | 260 | // #endif |
---|
| 261 | |
---|
[3e2b9c9] | 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 ); |
---|
[78da4ab] | 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 ); |
---|
[3e2b9c9] | 272 | |
---|
| 273 | // Update the global ring info |
---|
[78da4ab] | 274 | this.ring_flags = 0; |
---|
[3e2b9c9] | 275 | this.fd = fd; |
---|
| 276 | } |
---|
| 277 | |
---|
[8bee858] | 278 | static void __io_uring_teardown( io_context$ & this ) { |
---|
[3e2b9c9] | 279 | // Shutdown the io rings |
---|
[78da4ab] | 280 | struct __sub_ring_t & sq = this.sq; |
---|
| 281 | struct __cmp_ring_t & cq = this.cq; |
---|
[b0d0285] | 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 | } |
---|
[3e2b9c9] | 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 | |
---|
[78da4ab] | 306 | free( this.sq.free_ring.array ); // Maybe null, doesn't matter |
---|
[3e2b9c9] | 307 | } |
---|
| 308 | |
---|
[dddb3dd0] | 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 | |
---|
[78da4ab] | 318 | |
---|
[3e2b9c9] | 319 | //============================================================================================= |
---|
| 320 | // I/O Context Misc Setup |
---|
| 321 | //============================================================================================= |
---|
[8bee858] | 322 | void ?{}( io_arbiter$ & this ) { |
---|
[11054eb] | 323 | this.pending.empty = true; |
---|
[78da4ab] | 324 | } |
---|
[3e2b9c9] | 325 | |
---|
[8bee858] | 326 | void ^?{}( io_arbiter$ & mutex this ) {} |
---|
[3e2b9c9] | 327 | |
---|
[8bee858] | 328 | io_arbiter$ * create(void) { |
---|
[78da4ab] | 329 | return new(); |
---|
[3e2b9c9] | 330 | } |
---|
[8bee858] | 331 | void destroy(io_arbiter$ * arbiter) { |
---|
[78da4ab] | 332 | delete(arbiter); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | //============================================================================================= |
---|
| 336 | // I/O Context Misc Setup |
---|
| 337 | //============================================================================================= |
---|
| 338 | |
---|
[c44d652] | 339 | #endif |
---|