[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 |
|
---|
[21a5bfb7] | 146 | if(ctx.sq.to_submit != 0 || min_comp > 0) {
|
---|
| 147 |
|
---|
| 148 | __STATS__( true, io.calls.flush++; )
|
---|
| 149 | 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);
|
---|
| 150 | if( ret < 0 ) {
|
---|
| 151 | switch((int)errno) {
|
---|
| 152 | case EAGAIN:
|
---|
| 153 | case EINTR:
|
---|
| 154 | case EBUSY:
|
---|
| 155 | // Update statistics
|
---|
| 156 | __STATS__( false, io.calls.errors.busy ++; )
|
---|
| 157 | return false;
|
---|
| 158 | default:
|
---|
| 159 | abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
|
---|
| 160 | }
|
---|
[61dd73d] | 161 | }
|
---|
[ece0e80] | 162 |
|
---|
[21a5bfb7] | 163 | __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd);
|
---|
| 164 | __STATS__( true, io.calls.submitted += ret; )
|
---|
| 165 | /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
|
---|
| 166 | /* paranoid */ verify( ctx.sq.to_submit >= ret );
|
---|
[dddb3dd0] | 167 |
|
---|
[21a5bfb7] | 168 | ctx.sq.to_submit -= ret;
|
---|
[ece0e80] | 169 |
|
---|
[21a5bfb7] | 170 | /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
|
---|
[5dadc9b7] | 171 |
|
---|
[21a5bfb7] | 172 | // Release the consumed SQEs
|
---|
| 173 | __release_sqes( ctx );
|
---|
[ece0e80] | 174 |
|
---|
[21a5bfb7] | 175 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 176 |
|
---|
| 177 | ctx.proc->io.pending = false;
|
---|
| 178 | }
|
---|
[61dd73d] | 179 |
|
---|
[7ef162b2] | 180 | ready_schedule_lock();
|
---|
| 181 | bool ret = __cfa_io_drain( proc );
|
---|
| 182 | ready_schedule_unlock();
|
---|
| 183 | return ret;
|
---|
[61dd73d] | 184 | }
|
---|
[f6660520] | 185 |
|
---|
[92976d9] | 186 | //=============================================================================================
|
---|
| 187 | // I/O Submissions
|
---|
| 188 | //=============================================================================================
|
---|
| 189 |
|
---|
[2d8f7b0] | 190 | // Submition steps :
|
---|
[e46c753] | 191 | // 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
|
---|
[2d8f7b0] | 192 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not
|
---|
| 193 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we
|
---|
| 194 | // need to write an allocator that allows allocating concurrently.
|
---|
| 195 | //
|
---|
[e46c753] | 196 | // 2 - Actually fill the submit entry, this is the only simple and straightforward step.
|
---|
[2d8f7b0] | 197 | //
|
---|
[e46c753] | 198 | // 3 - Append the entry index to the array and adjust the tail accordingly. This operation
|
---|
[2d8f7b0] | 199 | // needs to arrive to two concensus at the same time:
|
---|
| 200 | // A - The order in which entries are listed in the array: no two threads must pick the
|
---|
| 201 | // same index for their entries
|
---|
| 202 | // B - When can the tail be update for the kernel. EVERY entries in the array between
|
---|
| 203 | // head and tail must be fully filled and shouldn't ever be touched again.
|
---|
| 204 | //
|
---|
[78da4ab] | 205 | //=============================================================================================
|
---|
| 206 | // Allocation
|
---|
| 207 | // for user's convenience fill the sqes from the indexes
|
---|
| 208 | static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx) {
|
---|
| 209 | struct io_uring_sqe * sqes = ctx->sq.sqes;
|
---|
| 210 | for(i; want) {
|
---|
[dddb3dd0] | 211 | __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
|
---|
[78da4ab] | 212 | out_sqes[i] = &sqes[idxs[i]];
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
[2489d31] | 215 |
|
---|
[78da4ab] | 216 | // Try to directly allocate from the a given context
|
---|
| 217 | // Not thread-safe
|
---|
| 218 | static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) {
|
---|
| 219 | __sub_ring_t & sq = ctx->sq;
|
---|
| 220 | const __u32 mask = *sq.mask;
|
---|
| 221 | __u32 fhead = sq.free_ring.head; // get the current head of the queue
|
---|
| 222 | __u32 ftail = sq.free_ring.tail; // get the current tail of the queue
|
---|
[2489d31] | 223 |
|
---|
[78da4ab] | 224 | // If we don't have enough sqes, fail
|
---|
| 225 | if((ftail - fhead) < want) { return false; }
|
---|
[426f60c] | 226 |
|
---|
[78da4ab] | 227 | // copy all the indexes we want from the available list
|
---|
| 228 | for(i; want) {
|
---|
[dddb3dd0] | 229 | __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
|
---|
[78da4ab] | 230 | idxs[i] = sq.free_ring.array[(fhead + i) & mask];
|
---|
[6f121b8] | 231 | }
|
---|
[2489d31] | 232 |
|
---|
[78da4ab] | 233 | // Advance the head to mark the indexes as consumed
|
---|
| 234 | __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
|
---|
[df40a56] | 235 |
|
---|
[78da4ab] | 236 | // return success
|
---|
| 237 | return true;
|
---|
| 238 | }
|
---|
[df40a56] | 239 |
|
---|
[78da4ab] | 240 | // Allocate an submit queue entry.
|
---|
| 241 | // The kernel cannot see these entries until they are submitted, but other threads must be
|
---|
| 242 | // able to see which entries can be used and which are already un used by an other thread
|
---|
| 243 | // for convenience, return both the index and the pointer to the sqe
|
---|
| 244 | // sqe == &sqes[idx]
|
---|
| 245 | struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) {
|
---|
| 246 | __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
|
---|
[df40a56] | 247 |
|
---|
[78da4ab] | 248 | disable_interrupts();
|
---|
| 249 | processor * proc = __cfaabi_tls.this_processor;
|
---|
[dddb3dd0] | 250 | $io_context * ctx = proc->io.ctx;
|
---|
[78da4ab] | 251 | /* paranoid */ verify( __cfaabi_tls.this_processor );
|
---|
[dddb3dd0] | 252 | /* paranoid */ verify( ctx );
|
---|
[78da4ab] | 253 |
|
---|
[dddb3dd0] | 254 | __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
|
---|
[78da4ab] | 255 |
|
---|
[dddb3dd0] | 256 | // We can proceed to the fast path
|
---|
| 257 | if( __alloc(ctx, idxs, want) ) {
|
---|
| 258 | // Allocation was successful
|
---|
| 259 | __STATS__( true, io.alloc.fast += 1; )
|
---|
[a3821fa] | 260 | enable_interrupts();
|
---|
[df40a56] | 261 |
|
---|
[dddb3dd0] | 262 | __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
|
---|
[2fafe7e] | 263 |
|
---|
[dddb3dd0] | 264 | __fill( sqes, want, idxs, ctx );
|
---|
| 265 | return ctx;
|
---|
[df40a56] | 266 | }
|
---|
[dddb3dd0] | 267 | // The fast path failed, fallback
|
---|
| 268 | __STATS__( true, io.alloc.fail += 1; )
|
---|
[df40a56] | 269 |
|
---|
[78da4ab] | 270 | // Fast path failed, fallback on arbitration
|
---|
[d60d30e] | 271 | __STATS__( true, io.alloc.slow += 1; )
|
---|
[a3821fa] | 272 | enable_interrupts();
|
---|
[78da4ab] | 273 |
|
---|
[dddb3dd0] | 274 | $io_arbiter * ioarb = proc->cltr->io.arbiter;
|
---|
| 275 | /* paranoid */ verify( ioarb );
|
---|
| 276 |
|
---|
[78da4ab] | 277 | __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
|
---|
| 278 |
|
---|
[11054eb] | 279 | struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want);
|
---|
[78da4ab] | 280 |
|
---|
[dddb3dd0] | 281 | __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
|
---|
[df40a56] | 282 |
|
---|
[78da4ab] | 283 | __fill( sqes, want, idxs,ret );
|
---|
| 284 | return ret;
|
---|
[df40a56] | 285 | }
|
---|
| 286 |
|
---|
[78da4ab] | 287 | //=============================================================================================
|
---|
| 288 | // submission
|
---|
[dddb3dd0] | 289 | static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) {
|
---|
[78da4ab] | 290 | // We can proceed to the fast path
|
---|
| 291 | // Get the right objects
|
---|
| 292 | __sub_ring_t & sq = ctx->sq;
|
---|
| 293 | const __u32 mask = *sq.mask;
|
---|
[dddb3dd0] | 294 | __u32 tail = *sq.kring.tail;
|
---|
[78da4ab] | 295 |
|
---|
| 296 | // Add the sqes to the array
|
---|
| 297 | for( i; have ) {
|
---|
[dddb3dd0] | 298 | __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
|
---|
[78da4ab] | 299 | sq.kring.array[ (tail + i) & mask ] = idxs[i];
|
---|
[426f60c] | 300 | }
|
---|
| 301 |
|
---|
[78da4ab] | 302 | // Make the sqes visible to the submitter
|
---|
[dddb3dd0] | 303 | __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
|
---|
[e8ac228] | 304 | sq.to_submit += have;
|
---|
[426f60c] | 305 |
|
---|
[dddb3dd0] | 306 | ctx->proc->io.pending = true;
|
---|
| 307 | ctx->proc->io.dirty = true;
|
---|
[70b4aeb9] | 308 | if(sq.to_submit > 30) {
|
---|
| 309 | __tls_stats()->io.flush.full++;
|
---|
| 310 | __cfa_io_flush( ctx->proc, 0 );
|
---|
| 311 | }
|
---|
| 312 | if(!lazy) {
|
---|
| 313 | __tls_stats()->io.flush.eager++;
|
---|
[c7b2215] | 314 | __cfa_io_flush( ctx->proc, 0 );
|
---|
[dddb3dd0] | 315 | }
|
---|
[78da4ab] | 316 | }
|
---|
[2489d31] | 317 |
|
---|
[dddb3dd0] | 318 | void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
|
---|
| 319 | __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
|
---|
[5dadc9b7] | 320 |
|
---|
[78da4ab] | 321 | disable_interrupts();
|
---|
| 322 | processor * proc = __cfaabi_tls.this_processor;
|
---|
| 323 | $io_context * ctx = proc->io.ctx;
|
---|
[dddb3dd0] | 324 | /* paranoid */ verify( __cfaabi_tls.this_processor );
|
---|
| 325 | /* paranoid */ verify( ctx );
|
---|
[e46c753] | 326 |
|
---|
[78da4ab] | 327 | // Can we proceed to the fast path
|
---|
[dddb3dd0] | 328 | if( ctx == inctx ) // We have the right instance?
|
---|
[78da4ab] | 329 | {
|
---|
[dddb3dd0] | 330 | __submit(ctx, idxs, have, lazy);
|
---|
[e46c753] | 331 |
|
---|
[78da4ab] | 332 | // Mark the instance as no longer in-use, re-enable interrupts and return
|
---|
[d60d30e] | 333 | __STATS__( true, io.submit.fast += 1; )
|
---|
[a3821fa] | 334 | enable_interrupts();
|
---|
[ece0e80] | 335 |
|
---|
[78da4ab] | 336 | __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
|
---|
| 337 | return;
|
---|
[e46c753] | 338 | }
|
---|
[d384787] | 339 |
|
---|
[78da4ab] | 340 | // Fast path failed, fallback on arbitration
|
---|
[d60d30e] | 341 | __STATS__( true, io.submit.slow += 1; )
|
---|
[a3821fa] | 342 | enable_interrupts();
|
---|
[5dadc9b7] | 343 |
|
---|
[78da4ab] | 344 | __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
|
---|
[426f60c] | 345 |
|
---|
[11054eb] | 346 | __ioarbiter_submit(inctx, idxs, have, lazy);
|
---|
[78da4ab] | 347 | }
|
---|
[2fab24e3] | 348 |
|
---|
[78da4ab] | 349 | //=============================================================================================
|
---|
| 350 | // Flushing
|
---|
[426f60c] | 351 | // Go through the ring's submit queue and release everything that has already been consumed
|
---|
| 352 | // by io_uring
|
---|
[78da4ab] | 353 | // This cannot be done by multiple threads
|
---|
| 354 | static __u32 __release_sqes( struct $io_context & ctx ) {
|
---|
| 355 | const __u32 mask = *ctx.sq.mask;
|
---|
[732b406] | 356 |
|
---|
[426f60c] | 357 | __attribute__((unused))
|
---|
[78da4ab] | 358 | __u32 ctail = *ctx.sq.kring.tail; // get the current tail of the queue
|
---|
| 359 | __u32 chead = *ctx.sq.kring.head; // get the current head of the queue
|
---|
| 360 | __u32 phead = ctx.sq.kring.released; // get the head the last time we were here
|
---|
| 361 |
|
---|
| 362 | __u32 ftail = ctx.sq.free_ring.tail; // get the current tail of the queue
|
---|
[732b406] | 363 |
|
---|
[426f60c] | 364 | // the 3 fields are organized like this diagram
|
---|
| 365 | // except it's are ring
|
---|
| 366 | // ---+--------+--------+----
|
---|
| 367 | // ---+--------+--------+----
|
---|
| 368 | // ^ ^ ^
|
---|
| 369 | // phead chead ctail
|
---|
| 370 |
|
---|
| 371 | // make sure ctail doesn't wrap around and reach phead
|
---|
| 372 | /* paranoid */ verify(
|
---|
| 373 | (ctail >= chead && chead >= phead)
|
---|
| 374 | || (chead >= phead && phead >= ctail)
|
---|
| 375 | || (phead >= ctail && ctail >= chead)
|
---|
| 376 | );
|
---|
| 377 |
|
---|
| 378 | // find the range we need to clear
|
---|
[4998155] | 379 | __u32 count = chead - phead;
|
---|
[426f60c] | 380 |
|
---|
[78da4ab] | 381 | if(count == 0) {
|
---|
| 382 | return 0;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
[426f60c] | 385 | // We acquired an previous-head/current-head range
|
---|
| 386 | // go through the range and release the sqes
|
---|
[34b61882] | 387 | for( i; count ) {
|
---|
[dddb3dd0] | 388 | __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
|
---|
[78da4ab] | 389 | __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
|
---|
| 390 | ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
|
---|
[34b61882] | 391 | }
|
---|
[78da4ab] | 392 |
|
---|
| 393 | ctx.sq.kring.released = chead; // note up to were we processed
|
---|
| 394 | __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST);
|
---|
| 395 |
|
---|
| 396 | __ioarbiter_notify(ctx);
|
---|
| 397 |
|
---|
[34b61882] | 398 | return count;
|
---|
| 399 | }
|
---|
[35285fd] | 400 |
|
---|
[78da4ab] | 401 | //=============================================================================================
|
---|
| 402 | // I/O Arbiter
|
---|
| 403 | //=============================================================================================
|
---|
[11054eb] | 404 | static inline void block(__outstanding_io_queue & queue, __outstanding_io & item) {
|
---|
| 405 | // Lock the list, it's not thread safe
|
---|
| 406 | lock( queue.lock __cfaabi_dbg_ctx2 );
|
---|
| 407 | {
|
---|
| 408 | // Add our request to the list
|
---|
| 409 | add( queue.queue, item );
|
---|
| 410 |
|
---|
| 411 | // Mark as pending
|
---|
| 412 | __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST );
|
---|
| 413 | }
|
---|
| 414 | unlock( queue.lock );
|
---|
| 415 |
|
---|
| 416 | wait( item.sem );
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | static inline bool empty(__outstanding_io_queue & queue ) {
|
---|
| 420 | return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) {
|
---|
[78da4ab] | 424 | __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
|
---|
| 425 |
|
---|
[d60d30e] | 426 | __STATS__( false, io.alloc.block += 1; )
|
---|
| 427 |
|
---|
[78da4ab] | 428 | // No one has any resources left, wait for something to finish
|
---|
[11054eb] | 429 | // We need to add ourself to a list of pending allocs and wait for an answer
|
---|
| 430 | __pending_alloc pa;
|
---|
| 431 | pa.idxs = idxs;
|
---|
| 432 | pa.want = want;
|
---|
[78da4ab] | 433 |
|
---|
[11054eb] | 434 | block(this.pending, (__outstanding_io&)pa);
|
---|
[78da4ab] | 435 |
|
---|
[11054eb] | 436 | return pa.ctx;
|
---|
[dddb3dd0] | 437 |
|
---|
[78da4ab] | 438 | }
|
---|
| 439 |
|
---|
[11054eb] | 440 | static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) {
|
---|
| 441 | /* paranoid */ verify( !empty(this.pending.queue) );
|
---|
[78da4ab] | 442 |
|
---|
[11054eb] | 443 | lock( this.pending.lock __cfaabi_dbg_ctx2 );
|
---|
| 444 | {
|
---|
| 445 | while( !empty(this.pending.queue) ) {
|
---|
| 446 | __cfadbg_print_safe(io, "Kernel I/O : notifying\n");
|
---|
| 447 | __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head;
|
---|
| 448 | __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue );
|
---|
[78da4ab] | 449 |
|
---|
[11054eb] | 450 | if( have > pa.want ) goto DONE;
|
---|
| 451 | drop( this.pending.queue );
|
---|
[78da4ab] | 452 |
|
---|
[11054eb] | 453 | /* paranoid */__attribute__((unused)) bool ret =
|
---|
[78da4ab] | 454 |
|
---|
[11054eb] | 455 | __alloc(ctx, pa.idxs, pa.want);
|
---|
| 456 |
|
---|
| 457 | /* paranoid */ verify( ret );
|
---|
| 458 |
|
---|
| 459 | pa.ctx = ctx;
|
---|
| 460 |
|
---|
| 461 | post( pa.sem );
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | this.pending.empty = true;
|
---|
| 465 | DONE:;
|
---|
| 466 | }
|
---|
| 467 | unlock( this.pending.lock );
|
---|
[78da4ab] | 468 | }
|
---|
| 469 |
|
---|
| 470 | static void __ioarbiter_notify( $io_context & ctx ) {
|
---|
[11054eb] | 471 | if(!empty( ctx.arbiter->pending )) {
|
---|
[78da4ab] | 472 | __ioarbiter_notify( *ctx.arbiter, &ctx );
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | // Simply append to the pending
|
---|
[11054eb] | 477 | static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
|
---|
[78da4ab] | 478 | __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd);
|
---|
| 479 |
|
---|
| 480 | __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have);
|
---|
| 481 |
|
---|
[11054eb] | 482 | __external_io ei;
|
---|
| 483 | ei.idxs = idxs;
|
---|
| 484 | ei.have = have;
|
---|
| 485 | ei.lazy = lazy;
|
---|
[78da4ab] | 486 |
|
---|
[11054eb] | 487 | block(ctx->ext_sq, (__outstanding_io&)ei);
|
---|
[78da4ab] | 488 |
|
---|
| 489 | __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[11054eb] | 492 | static void __ioarbiter_flush( $io_context & ctx ) {
|
---|
| 493 | if(!empty( ctx.ext_sq )) {
|
---|
| 494 | __STATS__( false, io.flush.external += 1; )
|
---|
[78da4ab] | 495 |
|
---|
[11054eb] | 496 | __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
|
---|
[d60d30e] | 497 |
|
---|
[11054eb] | 498 | lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 );
|
---|
| 499 | {
|
---|
| 500 | while( !empty(ctx.ext_sq.queue) ) {
|
---|
| 501 | __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue );
|
---|
[78da4ab] | 502 |
|
---|
[11054eb] | 503 | __submit(&ctx, ei.idxs, ei.have, ei.lazy);
|
---|
[78da4ab] | 504 |
|
---|
[11054eb] | 505 | post( ei.sem );
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | ctx.ext_sq.empty = true;
|
---|
| 509 | }
|
---|
| 510 | unlock(ctx.ext_sq.lock );
|
---|
| 511 | }
|
---|
[78da4ab] | 512 | }
|
---|
[7ef162b2] | 513 |
|
---|
[d3605f8] | 514 | #if defined(CFA_WITH_IO_URING_IDLE)
|
---|
| 515 | bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) {
|
---|
[6ddef36] | 516 | $io_context * ctx = proc->io.ctx;
|
---|
| 517 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 518 | /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
|
---|
| 519 | /* paranoid */ verify( ctx );
|
---|
[7ef162b2] | 520 |
|
---|
[6ddef36] | 521 | __u32 idx;
|
---|
| 522 | struct io_uring_sqe * sqe;
|
---|
[7ef162b2] | 523 |
|
---|
[6ddef36] | 524 | // We can proceed to the fast path
|
---|
| 525 | if( !__alloc(ctx, &idx, 1) ) return false;
|
---|
| 526 |
|
---|
| 527 | // Allocation was successful
|
---|
| 528 | __fill( &sqe, 1, &idx, ctx );
|
---|
| 529 |
|
---|
| 530 | sqe->user_data = (uintptr_t)&future;
|
---|
| 531 | sqe->flags = 0;
|
---|
[a1f3d93] | 532 | sqe->fd = fd;
|
---|
[6ddef36] | 533 | sqe->off = 0;
|
---|
[d3605f8] | 534 | sqe->ioprio = 0;
|
---|
[6ddef36] | 535 | sqe->fsync_flags = 0;
|
---|
| 536 | sqe->__pad2[0] = 0;
|
---|
| 537 | sqe->__pad2[1] = 0;
|
---|
| 538 | sqe->__pad2[2] = 0;
|
---|
[d3605f8] | 539 |
|
---|
| 540 | #if defined(CFA_HAVE_IORING_OP_READ)
|
---|
| 541 | sqe->opcode = IORING_OP_READ;
|
---|
| 542 | sqe->addr = (uint64_t)iov.iov_base;
|
---|
| 543 | sqe->len = iov.iov_len;
|
---|
| 544 | #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV)
|
---|
| 545 | sqe->opcode = IORING_OP_READV;
|
---|
| 546 | sqe->addr = (uintptr_t)&iov;
|
---|
| 547 | sqe->len = 1;
|
---|
| 548 | #else
|
---|
| 549 | #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined
|
---|
| 550 | #endif
|
---|
[6ddef36] | 551 |
|
---|
| 552 | asm volatile("": : :"memory");
|
---|
| 553 |
|
---|
| 554 | /* paranoid */ verify( sqe->user_data == (uintptr_t)&future );
|
---|
| 555 | __submit( ctx, &idx, 1, true );
|
---|
| 556 |
|
---|
| 557 | /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
|
---|
| 558 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[078fb05] | 559 |
|
---|
| 560 | return true;
|
---|
[6ddef36] | 561 | }
|
---|
| 562 | #endif
|
---|
[47746a2] | 563 | #endif
|
---|