[ecf6b46] | 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.cfa -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Thu Apr 23 17:31:00 2020 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | |
---|
[3e2b9c9] | 16 | #define __cforall_thread__ |
---|
| 17 | |
---|
[20ab637] | 18 | #if defined(__CFA_DEBUG__) |
---|
[d60d30e] | 19 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
| 20 | // #define __CFA_DEBUG_PRINT_IO_CORE__ |
---|
[20ab637] | 21 | #endif |
---|
[4069faad] | 22 | |
---|
[f6660520] | 23 | |
---|
[3e2b9c9] | 24 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[31bb2e1] | 25 | #define _GNU_SOURCE /* See feature_test_macros(7) */ |
---|
| 26 | #include <errno.h> |
---|
[3e2b9c9] | 27 | #include <signal.h> |
---|
[31bb2e1] | 28 | #include <stdint.h> |
---|
| 29 | #include <string.h> |
---|
| 30 | #include <unistd.h> |
---|
| 31 | |
---|
[92976d9] | 32 | extern "C" { |
---|
| 33 | #include <sys/syscall.h> |
---|
[dddb3dd0] | 34 | #include <sys/eventfd.h> |
---|
[92976d9] | 35 | |
---|
| 36 | #include <linux/io_uring.h> |
---|
| 37 | } |
---|
| 38 | |
---|
[3e2b9c9] | 39 | #include "stats.hfa" |
---|
| 40 | #include "kernel.hfa" |
---|
| 41 | #include "kernel/fwd.hfa" |
---|
| 42 | #include "io/types.hfa" |
---|
[185efe6] | 43 | |
---|
[2fab24e3] | 44 | __attribute__((unused)) static const char * opcodes[] = { |
---|
[426f60c] | 45 | "OP_NOP", |
---|
| 46 | "OP_READV", |
---|
| 47 | "OP_WRITEV", |
---|
| 48 | "OP_FSYNC", |
---|
| 49 | "OP_READ_FIXED", |
---|
| 50 | "OP_WRITE_FIXED", |
---|
| 51 | "OP_POLL_ADD", |
---|
| 52 | "OP_POLL_REMOVE", |
---|
| 53 | "OP_SYNC_FILE_RANGE", |
---|
| 54 | "OP_SENDMSG", |
---|
| 55 | "OP_RECVMSG", |
---|
| 56 | "OP_TIMEOUT", |
---|
| 57 | "OP_TIMEOUT_REMOVE", |
---|
| 58 | "OP_ACCEPT", |
---|
| 59 | "OP_ASYNC_CANCEL", |
---|
| 60 | "OP_LINK_TIMEOUT", |
---|
| 61 | "OP_CONNECT", |
---|
| 62 | "OP_FALLOCATE", |
---|
| 63 | "OP_OPENAT", |
---|
| 64 | "OP_CLOSE", |
---|
| 65 | "OP_FILES_UPDATE", |
---|
| 66 | "OP_STATX", |
---|
| 67 | "OP_READ", |
---|
| 68 | "OP_WRITE", |
---|
| 69 | "OP_FADVISE", |
---|
| 70 | "OP_MADVISE", |
---|
| 71 | "OP_SEND", |
---|
| 72 | "OP_RECV", |
---|
| 73 | "OP_OPENAT2", |
---|
| 74 | "OP_EPOLL_CTL", |
---|
| 75 | "OP_SPLICE", |
---|
| 76 | "OP_PROVIDE_BUFFERS", |
---|
| 77 | "OP_REMOVE_BUFFERS", |
---|
| 78 | "OP_TEE", |
---|
| 79 | "INVALID_OP" |
---|
| 80 | }; |
---|
| 81 | |
---|
[11054eb] | 82 | static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ); |
---|
| 83 | static void __ioarbiter_submit( $io_context * , __u32 idxs[], __u32 have, bool lazy ); |
---|
| 84 | static void __ioarbiter_flush ( $io_context & ); |
---|
[dddb3dd0] | 85 | static inline void __ioarbiter_notify( $io_context & ctx ); |
---|
[92976d9] | 86 | //============================================================================================= |
---|
| 87 | // I/O Polling |
---|
| 88 | //============================================================================================= |
---|
[78da4ab] | 89 | static inline unsigned __flush( struct $io_context & ); |
---|
| 90 | static inline __u32 __release_sqes( struct $io_context & ); |
---|
[1d5e4711] | 91 | |
---|
[c1c95b1] | 92 | bool __cfa_io_drain( processor * proc ) { |
---|
[dddb3dd0] | 93 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 94 | /* paranoid */ verify( proc ); |
---|
| 95 | /* paranoid */ verify( proc->io.ctx ); |
---|
[6f121b8] | 96 | |
---|
[d384787] | 97 | // Drain the queue |
---|
[dddb3dd0] | 98 | $io_context * ctx = proc->io.ctx; |
---|
| 99 | unsigned head = *ctx->cq.head; |
---|
| 100 | unsigned tail = *ctx->cq.tail; |
---|
| 101 | const __u32 mask = *ctx->cq.mask; |
---|
[92976d9] | 102 | |
---|
[4998155] | 103 | __u32 count = tail - head; |
---|
[dddb3dd0] | 104 | __STATS__( false, io.calls.drain++; io.calls.completed += count; ) |
---|
[d60d30e] | 105 | |
---|
[c1c95b1] | 106 | if(count == 0) return false; |
---|
| 107 | |
---|
[d384787] | 108 | for(i; count) { |
---|
[6f121b8] | 109 | unsigned idx = (head + i) & mask; |
---|
[dddb3dd0] | 110 | volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx]; |
---|
[92976d9] | 111 | |
---|
[d384787] | 112 | /* paranoid */ verify(&cqe); |
---|
[92976d9] | 113 | |
---|
[78da4ab] | 114 | struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data; |
---|
| 115 | __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future ); |
---|
| 116 | |
---|
| 117 | fulfil( *future, cqe.res ); |
---|
| 118 | } |
---|
| 119 | |
---|
[dddb3dd0] | 120 | __cfadbg_print_safe(io, "Kernel I/O : %u completed\n", count); |
---|
[2d8f7b0] | 121 | |
---|
[92976d9] | 122 | // Mark to the kernel that the cqe has been seen |
---|
| 123 | // Ensure that the kernel only sees the new value of the head index after the CQEs have been read. |
---|
[dddb3dd0] | 124 | __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST ); |
---|
[92976d9] | 125 | |
---|
[dddb3dd0] | 126 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 127 | |
---|
[c1c95b1] | 128 | return true; |
---|
[92976d9] | 129 | } |
---|
| 130 | |
---|
[dddb3dd0] | 131 | void __cfa_io_flush( processor * proc ) { |
---|
| 132 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 133 | /* paranoid */ verify( proc ); |
---|
| 134 | /* paranoid */ verify( proc->io.ctx ); |
---|
[1539bbd] | 135 | |
---|
[dddb3dd0] | 136 | $io_context & ctx = *proc->io.ctx; |
---|
[78da4ab] | 137 | |
---|
[11054eb] | 138 | __ioarbiter_flush( ctx ); |
---|
[3c039b0] | 139 | |
---|
[dddb3dd0] | 140 | __STATS__( true, io.calls.flush++; ) |
---|
| 141 | int ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, 0, 0, (sigset_t *)0p, _NSIG / 8); |
---|
| 142 | if( ret < 0 ) { |
---|
| 143 | switch((int)errno) { |
---|
| 144 | case EAGAIN: |
---|
| 145 | case EINTR: |
---|
| 146 | case EBUSY: |
---|
| 147 | // Update statistics |
---|
| 148 | __STATS__( false, io.calls.errors.busy ++; ) |
---|
| 149 | return; |
---|
| 150 | default: |
---|
| 151 | abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) ); |
---|
[61dd73d] | 152 | } |
---|
[dddb3dd0] | 153 | } |
---|
[ece0e80] | 154 | |
---|
[dddb3dd0] | 155 | __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd); |
---|
| 156 | __STATS__( true, io.calls.submitted += ret; ) |
---|
| 157 | /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num ); |
---|
| 158 | /* paranoid */ verify( ctx.sq.to_submit >= ret ); |
---|
| 159 | |
---|
| 160 | ctx.sq.to_submit -= ret; |
---|
[ece0e80] | 161 | |
---|
[dddb3dd0] | 162 | /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num ); |
---|
[5dadc9b] | 163 | |
---|
[dddb3dd0] | 164 | // Release the consumed SQEs |
---|
| 165 | __release_sqes( ctx ); |
---|
[ece0e80] | 166 | |
---|
[dddb3dd0] | 167 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[61dd73d] | 168 | |
---|
[dddb3dd0] | 169 | ctx.proc->io.pending = false; |
---|
[61dd73d] | 170 | } |
---|
[f6660520] | 171 | |
---|
[92976d9] | 172 | //============================================================================================= |
---|
| 173 | // I/O Submissions |
---|
| 174 | //============================================================================================= |
---|
| 175 | |
---|
[2d8f7b0] | 176 | // Submition steps : |
---|
[e46c753] | 177 | // 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones |
---|
[2d8f7b0] | 178 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not |
---|
| 179 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we |
---|
| 180 | // need to write an allocator that allows allocating concurrently. |
---|
| 181 | // |
---|
[e46c753] | 182 | // 2 - Actually fill the submit entry, this is the only simple and straightforward step. |
---|
[2d8f7b0] | 183 | // |
---|
[e46c753] | 184 | // 3 - Append the entry index to the array and adjust the tail accordingly. This operation |
---|
[2d8f7b0] | 185 | // needs to arrive to two concensus at the same time: |
---|
| 186 | // A - The order in which entries are listed in the array: no two threads must pick the |
---|
| 187 | // same index for their entries |
---|
| 188 | // B - When can the tail be update for the kernel. EVERY entries in the array between |
---|
| 189 | // head and tail must be fully filled and shouldn't ever be touched again. |
---|
| 190 | // |
---|
[78da4ab] | 191 | //============================================================================================= |
---|
| 192 | // Allocation |
---|
| 193 | // for user's convenience fill the sqes from the indexes |
---|
| 194 | static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx) { |
---|
| 195 | struct io_uring_sqe * sqes = ctx->sq.sqes; |
---|
| 196 | for(i; want) { |
---|
[dddb3dd0] | 197 | __cfadbg_print_safe(io, "Kernel I/O : filling loop\n"); |
---|
[78da4ab] | 198 | out_sqes[i] = &sqes[idxs[i]]; |
---|
| 199 | } |
---|
| 200 | } |
---|
[2489d31] | 201 | |
---|
[78da4ab] | 202 | // Try to directly allocate from the a given context |
---|
| 203 | // Not thread-safe |
---|
| 204 | static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) { |
---|
| 205 | __sub_ring_t & sq = ctx->sq; |
---|
| 206 | const __u32 mask = *sq.mask; |
---|
| 207 | __u32 fhead = sq.free_ring.head; // get the current head of the queue |
---|
| 208 | __u32 ftail = sq.free_ring.tail; // get the current tail of the queue |
---|
[2489d31] | 209 | |
---|
[78da4ab] | 210 | // If we don't have enough sqes, fail |
---|
| 211 | if((ftail - fhead) < want) { return false; } |
---|
[426f60c] | 212 | |
---|
[78da4ab] | 213 | // copy all the indexes we want from the available list |
---|
| 214 | for(i; want) { |
---|
[dddb3dd0] | 215 | __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n"); |
---|
[78da4ab] | 216 | idxs[i] = sq.free_ring.array[(fhead + i) & mask]; |
---|
[6f121b8] | 217 | } |
---|
[2489d31] | 218 | |
---|
[78da4ab] | 219 | // Advance the head to mark the indexes as consumed |
---|
| 220 | __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE); |
---|
[df40a56] | 221 | |
---|
[78da4ab] | 222 | // return success |
---|
| 223 | return true; |
---|
| 224 | } |
---|
[df40a56] | 225 | |
---|
[78da4ab] | 226 | // Allocate an submit queue entry. |
---|
| 227 | // The kernel cannot see these entries until they are submitted, but other threads must be |
---|
| 228 | // able to see which entries can be used and which are already un used by an other thread |
---|
| 229 | // for convenience, return both the index and the pointer to the sqe |
---|
| 230 | // sqe == &sqes[idx] |
---|
| 231 | struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) { |
---|
| 232 | __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want); |
---|
[df40a56] | 233 | |
---|
[78da4ab] | 234 | disable_interrupts(); |
---|
| 235 | processor * proc = __cfaabi_tls.this_processor; |
---|
[dddb3dd0] | 236 | $io_context * ctx = proc->io.ctx; |
---|
[78da4ab] | 237 | /* paranoid */ verify( __cfaabi_tls.this_processor ); |
---|
[dddb3dd0] | 238 | /* paranoid */ verify( ctx ); |
---|
[78da4ab] | 239 | |
---|
[dddb3dd0] | 240 | __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n"); |
---|
[78da4ab] | 241 | |
---|
[dddb3dd0] | 242 | // We can proceed to the fast path |
---|
| 243 | if( __alloc(ctx, idxs, want) ) { |
---|
| 244 | // Allocation was successful |
---|
| 245 | __STATS__( true, io.alloc.fast += 1; ) |
---|
[a3821fa] | 246 | enable_interrupts(); |
---|
[df40a56] | 247 | |
---|
[dddb3dd0] | 248 | __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd); |
---|
[2fafe7e] | 249 | |
---|
[dddb3dd0] | 250 | __fill( sqes, want, idxs, ctx ); |
---|
| 251 | return ctx; |
---|
[df40a56] | 252 | } |
---|
[dddb3dd0] | 253 | // The fast path failed, fallback |
---|
| 254 | __STATS__( true, io.alloc.fail += 1; ) |
---|
[df40a56] | 255 | |
---|
[78da4ab] | 256 | // Fast path failed, fallback on arbitration |
---|
[d60d30e] | 257 | __STATS__( true, io.alloc.slow += 1; ) |
---|
[a3821fa] | 258 | enable_interrupts(); |
---|
[78da4ab] | 259 | |
---|
[dddb3dd0] | 260 | $io_arbiter * ioarb = proc->cltr->io.arbiter; |
---|
| 261 | /* paranoid */ verify( ioarb ); |
---|
| 262 | |
---|
[78da4ab] | 263 | __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n"); |
---|
| 264 | |
---|
[11054eb] | 265 | struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want); |
---|
[78da4ab] | 266 | |
---|
[dddb3dd0] | 267 | __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd); |
---|
[df40a56] | 268 | |
---|
[78da4ab] | 269 | __fill( sqes, want, idxs,ret ); |
---|
| 270 | return ret; |
---|
[df40a56] | 271 | } |
---|
| 272 | |
---|
[426f60c] | 273 | |
---|
[78da4ab] | 274 | //============================================================================================= |
---|
| 275 | // submission |
---|
[dddb3dd0] | 276 | static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) { |
---|
[78da4ab] | 277 | // We can proceed to the fast path |
---|
| 278 | // Get the right objects |
---|
| 279 | __sub_ring_t & sq = ctx->sq; |
---|
| 280 | const __u32 mask = *sq.mask; |
---|
[dddb3dd0] | 281 | __u32 tail = *sq.kring.tail; |
---|
[78da4ab] | 282 | |
---|
| 283 | // Add the sqes to the array |
---|
| 284 | for( i; have ) { |
---|
[dddb3dd0] | 285 | __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n"); |
---|
[78da4ab] | 286 | sq.kring.array[ (tail + i) & mask ] = idxs[i]; |
---|
[426f60c] | 287 | } |
---|
| 288 | |
---|
[78da4ab] | 289 | // Make the sqes visible to the submitter |
---|
[dddb3dd0] | 290 | __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE); |
---|
| 291 | sq.to_submit++; |
---|
[426f60c] | 292 | |
---|
[dddb3dd0] | 293 | ctx->proc->io.pending = true; |
---|
| 294 | ctx->proc->io.dirty = true; |
---|
| 295 | if(sq.to_submit > 30 || !lazy) { |
---|
| 296 | __cfa_io_flush( ctx->proc ); |
---|
| 297 | } |
---|
[78da4ab] | 298 | } |
---|
[2489d31] | 299 | |
---|
[dddb3dd0] | 300 | void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) { |
---|
| 301 | __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager"); |
---|
[5dadc9b] | 302 | |
---|
[78da4ab] | 303 | disable_interrupts(); |
---|
| 304 | processor * proc = __cfaabi_tls.this_processor; |
---|
| 305 | $io_context * ctx = proc->io.ctx; |
---|
[dddb3dd0] | 306 | /* paranoid */ verify( __cfaabi_tls.this_processor ); |
---|
| 307 | /* paranoid */ verify( ctx ); |
---|
[e46c753] | 308 | |
---|
[78da4ab] | 309 | // Can we proceed to the fast path |
---|
[dddb3dd0] | 310 | if( ctx == inctx ) // We have the right instance? |
---|
[78da4ab] | 311 | { |
---|
[dddb3dd0] | 312 | __submit(ctx, idxs, have, lazy); |
---|
[e46c753] | 313 | |
---|
[78da4ab] | 314 | // Mark the instance as no longer in-use, re-enable interrupts and return |
---|
[d60d30e] | 315 | __STATS__( true, io.submit.fast += 1; ) |
---|
[a3821fa] | 316 | enable_interrupts(); |
---|
[ece0e80] | 317 | |
---|
[78da4ab] | 318 | __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n"); |
---|
| 319 | return; |
---|
[e46c753] | 320 | } |
---|
[d384787] | 321 | |
---|
[78da4ab] | 322 | // Fast path failed, fallback on arbitration |
---|
[d60d30e] | 323 | __STATS__( true, io.submit.slow += 1; ) |
---|
[a3821fa] | 324 | enable_interrupts(); |
---|
[5dadc9b] | 325 | |
---|
[78da4ab] | 326 | __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n"); |
---|
[426f60c] | 327 | |
---|
[11054eb] | 328 | __ioarbiter_submit(inctx, idxs, have, lazy); |
---|
[78da4ab] | 329 | } |
---|
[2fab24e3] | 330 | |
---|
[78da4ab] | 331 | //============================================================================================= |
---|
| 332 | // Flushing |
---|
[426f60c] | 333 | // Go through the ring's submit queue and release everything that has already been consumed |
---|
| 334 | // by io_uring |
---|
[78da4ab] | 335 | // This cannot be done by multiple threads |
---|
| 336 | static __u32 __release_sqes( struct $io_context & ctx ) { |
---|
| 337 | const __u32 mask = *ctx.sq.mask; |
---|
[732b406] | 338 | |
---|
[426f60c] | 339 | __attribute__((unused)) |
---|
[78da4ab] | 340 | __u32 ctail = *ctx.sq.kring.tail; // get the current tail of the queue |
---|
| 341 | __u32 chead = *ctx.sq.kring.head; // get the current head of the queue |
---|
| 342 | __u32 phead = ctx.sq.kring.released; // get the head the last time we were here |
---|
| 343 | |
---|
| 344 | __u32 ftail = ctx.sq.free_ring.tail; // get the current tail of the queue |
---|
[732b406] | 345 | |
---|
[426f60c] | 346 | // the 3 fields are organized like this diagram |
---|
| 347 | // except it's are ring |
---|
| 348 | // ---+--------+--------+---- |
---|
| 349 | // ---+--------+--------+---- |
---|
| 350 | // ^ ^ ^ |
---|
| 351 | // phead chead ctail |
---|
| 352 | |
---|
| 353 | // make sure ctail doesn't wrap around and reach phead |
---|
| 354 | /* paranoid */ verify( |
---|
| 355 | (ctail >= chead && chead >= phead) |
---|
| 356 | || (chead >= phead && phead >= ctail) |
---|
| 357 | || (phead >= ctail && ctail >= chead) |
---|
| 358 | ); |
---|
| 359 | |
---|
| 360 | // find the range we need to clear |
---|
[4998155] | 361 | __u32 count = chead - phead; |
---|
[426f60c] | 362 | |
---|
[78da4ab] | 363 | if(count == 0) { |
---|
| 364 | return 0; |
---|
| 365 | } |
---|
| 366 | |
---|
[426f60c] | 367 | // We acquired an previous-head/current-head range |
---|
| 368 | // go through the range and release the sqes |
---|
[34b61882] | 369 | for( i; count ) { |
---|
[dddb3dd0] | 370 | __cfadbg_print_safe(io, "Kernel I/O : release loop\n"); |
---|
[78da4ab] | 371 | __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ]; |
---|
| 372 | ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx; |
---|
[34b61882] | 373 | } |
---|
[78da4ab] | 374 | |
---|
| 375 | ctx.sq.kring.released = chead; // note up to were we processed |
---|
| 376 | __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST); |
---|
| 377 | |
---|
| 378 | __ioarbiter_notify(ctx); |
---|
| 379 | |
---|
[34b61882] | 380 | return count; |
---|
| 381 | } |
---|
[35285fd] | 382 | |
---|
[78da4ab] | 383 | //============================================================================================= |
---|
| 384 | // I/O Arbiter |
---|
| 385 | //============================================================================================= |
---|
[11054eb] | 386 | static inline void block(__outstanding_io_queue & queue, __outstanding_io & item) { |
---|
| 387 | // Lock the list, it's not thread safe |
---|
| 388 | lock( queue.lock __cfaabi_dbg_ctx2 ); |
---|
| 389 | { |
---|
| 390 | // Add our request to the list |
---|
| 391 | add( queue.queue, item ); |
---|
| 392 | |
---|
| 393 | // Mark as pending |
---|
| 394 | __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST ); |
---|
| 395 | } |
---|
| 396 | unlock( queue.lock ); |
---|
| 397 | |
---|
| 398 | wait( item.sem ); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | static inline bool empty(__outstanding_io_queue & queue ) { |
---|
| 402 | return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) { |
---|
[78da4ab] | 406 | __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n"); |
---|
| 407 | |
---|
[d60d30e] | 408 | __STATS__( false, io.alloc.block += 1; ) |
---|
| 409 | |
---|
[78da4ab] | 410 | // No one has any resources left, wait for something to finish |
---|
[11054eb] | 411 | // We need to add ourself to a list of pending allocs and wait for an answer |
---|
| 412 | __pending_alloc pa; |
---|
| 413 | pa.idxs = idxs; |
---|
| 414 | pa.want = want; |
---|
[78da4ab] | 415 | |
---|
[11054eb] | 416 | block(this.pending, (__outstanding_io&)pa); |
---|
[78da4ab] | 417 | |
---|
[11054eb] | 418 | return pa.ctx; |
---|
[dddb3dd0] | 419 | |
---|
[78da4ab] | 420 | } |
---|
| 421 | |
---|
[11054eb] | 422 | static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) { |
---|
| 423 | /* paranoid */ verify( !empty(this.pending.queue) ); |
---|
[78da4ab] | 424 | |
---|
[11054eb] | 425 | lock( this.pending.lock __cfaabi_dbg_ctx2 ); |
---|
| 426 | { |
---|
| 427 | while( !empty(this.pending.queue) ) { |
---|
| 428 | __cfadbg_print_safe(io, "Kernel I/O : notifying\n"); |
---|
| 429 | __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head; |
---|
| 430 | __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue ); |
---|
[78da4ab] | 431 | |
---|
[11054eb] | 432 | if( have > pa.want ) goto DONE; |
---|
| 433 | drop( this.pending.queue ); |
---|
[78da4ab] | 434 | |
---|
[11054eb] | 435 | /* paranoid */__attribute__((unused)) bool ret = |
---|
[78da4ab] | 436 | |
---|
[11054eb] | 437 | __alloc(ctx, pa.idxs, pa.want); |
---|
| 438 | |
---|
| 439 | /* paranoid */ verify( ret ); |
---|
| 440 | |
---|
| 441 | pa.ctx = ctx; |
---|
| 442 | |
---|
| 443 | post( pa.sem ); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | this.pending.empty = true; |
---|
| 447 | DONE:; |
---|
| 448 | } |
---|
| 449 | unlock( this.pending.lock ); |
---|
[78da4ab] | 450 | } |
---|
| 451 | |
---|
| 452 | static void __ioarbiter_notify( $io_context & ctx ) { |
---|
[11054eb] | 453 | if(!empty( ctx.arbiter->pending )) { |
---|
[78da4ab] | 454 | __ioarbiter_notify( *ctx.arbiter, &ctx ); |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | // Simply append to the pending |
---|
[11054eb] | 459 | static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) { |
---|
[78da4ab] | 460 | __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd); |
---|
| 461 | |
---|
| 462 | __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have); |
---|
| 463 | |
---|
[11054eb] | 464 | __external_io ei; |
---|
| 465 | ei.idxs = idxs; |
---|
| 466 | ei.have = have; |
---|
| 467 | ei.lazy = lazy; |
---|
[78da4ab] | 468 | |
---|
[11054eb] | 469 | block(ctx->ext_sq, (__outstanding_io&)ei); |
---|
[78da4ab] | 470 | |
---|
| 471 | __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have); |
---|
| 472 | } |
---|
| 473 | |
---|
[11054eb] | 474 | static void __ioarbiter_flush( $io_context & ctx ) { |
---|
| 475 | if(!empty( ctx.ext_sq )) { |
---|
| 476 | __STATS__( false, io.flush.external += 1; ) |
---|
[78da4ab] | 477 | |
---|
[11054eb] | 478 | __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n"); |
---|
[d60d30e] | 479 | |
---|
[11054eb] | 480 | lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 ); |
---|
| 481 | { |
---|
| 482 | while( !empty(ctx.ext_sq.queue) ) { |
---|
| 483 | __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue ); |
---|
[78da4ab] | 484 | |
---|
[11054eb] | 485 | __submit(&ctx, ei.idxs, ei.have, ei.lazy); |
---|
[78da4ab] | 486 | |
---|
[11054eb] | 487 | post( ei.sem ); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | ctx.ext_sq.empty = true; |
---|
| 491 | } |
---|
| 492 | unlock(ctx.ext_sq.lock ); |
---|
| 493 | } |
---|
[78da4ab] | 494 | } |
---|
[47746a2] | 495 | #endif |
---|