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