[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__) |
---|
| 19 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
[e660761] | 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" { |
---|
[f00b26d4] | 33 | #include <sys/epoll.h> |
---|
[92976d9] | 34 | #include <sys/syscall.h> |
---|
| 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 | |
---|
[2fafe7e] | 44 | // returns true of acquired as leader or second leader |
---|
| 45 | static inline bool try_lock( __leaderlock_t & this ) { |
---|
| 46 | const uintptr_t thrd = 1z | (uintptr_t)active_thread(); |
---|
| 47 | bool block; |
---|
| 48 | disable_interrupts(); |
---|
| 49 | for() { |
---|
| 50 | struct $thread * expected = this.value; |
---|
| 51 | if( 1p != expected && 0p != expected ) { |
---|
| 52 | /* paranoid */ verify( thrd != (uintptr_t)expected ); // We better not already be the next leader |
---|
| 53 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 54 | return false; |
---|
| 55 | } |
---|
| 56 | struct $thread * desired; |
---|
| 57 | if( 0p == expected ) { |
---|
| 58 | // If the lock isn't locked acquire it, no need to block |
---|
| 59 | desired = 1p; |
---|
| 60 | block = false; |
---|
| 61 | } |
---|
| 62 | else { |
---|
| 63 | // If the lock is already locked try becomming the next leader |
---|
| 64 | desired = (struct $thread *)thrd; |
---|
| 65 | block = true; |
---|
| 66 | } |
---|
| 67 | if( __atomic_compare_exchange_n(&this.value, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) break; |
---|
| 68 | } |
---|
| 69 | if( block ) { |
---|
| 70 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[e235429] | 71 | park(); |
---|
[2fafe7e] | 72 | disable_interrupts(); |
---|
| 73 | } |
---|
| 74 | return true; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static inline bool next( __leaderlock_t & this ) { |
---|
[8fc652e0] | 78 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[2fafe7e] | 79 | struct $thread * nextt; |
---|
| 80 | for() { |
---|
| 81 | struct $thread * expected = this.value; |
---|
| 82 | /* paranoid */ verify( (1 & (uintptr_t)expected) == 1 ); // The lock better be locked |
---|
| 83 | |
---|
| 84 | struct $thread * desired; |
---|
| 85 | if( 1p == expected ) { |
---|
| 86 | // No next leader, just unlock |
---|
| 87 | desired = 0p; |
---|
| 88 | nextt = 0p; |
---|
| 89 | } |
---|
| 90 | else { |
---|
| 91 | // There is a next leader, remove but keep locked |
---|
| 92 | desired = 1p; |
---|
| 93 | nextt = (struct $thread *)(~1z & (uintptr_t)expected); |
---|
| 94 | } |
---|
| 95 | if( __atomic_compare_exchange_n(&this.value, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) break; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | if(nextt) { |
---|
[b4b63e8] | 99 | unpark( nextt ); |
---|
[2fafe7e] | 100 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 101 | return true; |
---|
| 102 | } |
---|
| 103 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 104 | return false; |
---|
| 105 | } |
---|
| 106 | |
---|
[92976d9] | 107 | //============================================================================================= |
---|
[3e2b9c9] | 108 | // I/O Syscall |
---|
[92976d9] | 109 | //============================================================================================= |
---|
[3e2b9c9] | 110 | static int __io_uring_enter( struct __io_data & ring, unsigned to_submit, bool get ) { |
---|
[20ab637] | 111 | bool need_sys_to_submit = false; |
---|
| 112 | bool need_sys_to_complete = false; |
---|
| 113 | unsigned flags = 0; |
---|
| 114 | |
---|
| 115 | TO_SUBMIT: |
---|
| 116 | if( to_submit > 0 ) { |
---|
| 117 | if( !(ring.ring_flags & IORING_SETUP_SQPOLL) ) { |
---|
| 118 | need_sys_to_submit = true; |
---|
| 119 | break TO_SUBMIT; |
---|
| 120 | } |
---|
| 121 | if( (*ring.submit_q.flags) & IORING_SQ_NEED_WAKEUP ) { |
---|
| 122 | need_sys_to_submit = true; |
---|
| 123 | flags |= IORING_ENTER_SQ_WAKEUP; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if( get && !(ring.ring_flags & IORING_SETUP_SQPOLL) ) { |
---|
| 128 | flags |= IORING_ENTER_GETEVENTS; |
---|
| 129 | if( (ring.ring_flags & IORING_SETUP_IOPOLL) ) { |
---|
| 130 | need_sys_to_complete = true; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | int ret = 0; |
---|
| 135 | if( need_sys_to_submit || need_sys_to_complete ) { |
---|
[b982fb2] | 136 | ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, 0, flags, (sigset_t *)0p, _NSIG / 8); |
---|
[20ab637] | 137 | if( ret < 0 ) { |
---|
| 138 | switch((int)errno) { |
---|
| 139 | case EAGAIN: |
---|
| 140 | case EINTR: |
---|
| 141 | ret = -1; |
---|
| 142 | break; |
---|
| 143 | default: |
---|
| 144 | abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | // Memory barrier |
---|
| 150 | __atomic_thread_fence( __ATOMIC_SEQ_CST ); |
---|
| 151 | return ret; |
---|
| 152 | } |
---|
| 153 | |
---|
[92976d9] | 154 | //============================================================================================= |
---|
| 155 | // I/O Polling |
---|
| 156 | //============================================================================================= |
---|
[1d5e4711] | 157 | static unsigned __collect_submitions( struct __io_data & ring ); |
---|
[4998155] | 158 | static __u32 __release_consumed_submission( struct __io_data & ring ); |
---|
[1d5e4711] | 159 | |
---|
[f00b26d4] | 160 | static inline void process(struct io_uring_cqe & cqe ) { |
---|
[c402739f] | 161 | struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data; |
---|
[fe9468e2] | 162 | __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future ); |
---|
[5751a56] | 163 | |
---|
[c402739f] | 164 | fulfil( *future, cqe.res ); |
---|
[5751a56] | 165 | } |
---|
| 166 | |
---|
[92976d9] | 167 | // Process a single completion message from the io_uring |
---|
| 168 | // This is NOT thread-safe |
---|
[f00b26d4] | 169 | static [int, bool] __drain_io( & struct __io_data ring ) { |
---|
[8fc652e0] | 170 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[e46c753] | 171 | |
---|
[5dadc9b] | 172 | unsigned to_submit = 0; |
---|
[f00b26d4] | 173 | if( ring.poller_submits ) { |
---|
[5dadc9b] | 174 | // If the poller thread also submits, then we need to aggregate the submissions which are ready |
---|
[e46c753] | 175 | to_submit = __collect_submitions( ring ); |
---|
[5dadc9b] | 176 | } |
---|
| 177 | |
---|
[f00b26d4] | 178 | int ret = __io_uring_enter(ring, to_submit, true); |
---|
[20ab637] | 179 | if( ret < 0 ) { |
---|
| 180 | return [0, true]; |
---|
| 181 | } |
---|
[1d5e4711] | 182 | |
---|
[20ab637] | 183 | // update statistics |
---|
| 184 | if (to_submit > 0) { |
---|
[1d5e4711] | 185 | __STATS__( true, |
---|
| 186 | if( to_submit > 0 ) { |
---|
| 187 | io.submit_q.submit_avg.rdy += to_submit; |
---|
| 188 | io.submit_q.submit_avg.csm += ret; |
---|
| 189 | io.submit_q.submit_avg.cnt += 1; |
---|
| 190 | } |
---|
| 191 | ) |
---|
[6f121b8] | 192 | } |
---|
| 193 | |
---|
[20ab637] | 194 | // Release the consumed SQEs |
---|
| 195 | __release_consumed_submission( ring ); |
---|
[6f121b8] | 196 | |
---|
[d384787] | 197 | // Drain the queue |
---|
[92976d9] | 198 | unsigned head = *ring.completion_q.head; |
---|
[6f121b8] | 199 | unsigned tail = *ring.completion_q.tail; |
---|
[4998155] | 200 | const __u32 mask = *ring.completion_q.mask; |
---|
[6f121b8] | 201 | |
---|
[d384787] | 202 | // Nothing was new return 0 |
---|
| 203 | if (head == tail) { |
---|
[e46c753] | 204 | return [0, to_submit > 0]; |
---|
[d384787] | 205 | } |
---|
[92976d9] | 206 | |
---|
[4998155] | 207 | __u32 count = tail - head; |
---|
[1d5e4711] | 208 | /* paranoid */ verify( count != 0 ); |
---|
[d384787] | 209 | for(i; count) { |
---|
[6f121b8] | 210 | unsigned idx = (head + i) & mask; |
---|
[d384787] | 211 | struct io_uring_cqe & cqe = ring.completion_q.cqes[idx]; |
---|
[92976d9] | 212 | |
---|
[d384787] | 213 | /* paranoid */ verify(&cqe); |
---|
[92976d9] | 214 | |
---|
[f00b26d4] | 215 | process( cqe ); |
---|
[d384787] | 216 | } |
---|
[2d8f7b0] | 217 | |
---|
[92976d9] | 218 | // Mark to the kernel that the cqe has been seen |
---|
| 219 | // Ensure that the kernel only sees the new value of the head index after the CQEs have been read. |
---|
[6f121b8] | 220 | __atomic_thread_fence( __ATOMIC_SEQ_CST ); |
---|
[d384787] | 221 | __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_RELAXED ); |
---|
[92976d9] | 222 | |
---|
[5dadc9b] | 223 | return [count, count > 0 || to_submit > 0]; |
---|
[92976d9] | 224 | } |
---|
| 225 | |
---|
[f00b26d4] | 226 | void main( $io_ctx_thread & this ) { |
---|
| 227 | epoll_event ev; |
---|
[3e2b9c9] | 228 | __ioctx_register( this, ev ); |
---|
[1539bbd] | 229 | |
---|
[f00b26d4] | 230 | __cfadbg_print_safe(io_core, "Kernel I/O : IO poller %p for ring %p ready\n", &this, &this.ring); |
---|
[1539bbd] | 231 | |
---|
[4e74466] | 232 | int reset = 0; |
---|
[61dd73d] | 233 | // Then loop until we need to start |
---|
[f00b26d4] | 234 | while(!__atomic_load_n(&this.done, __ATOMIC_SEQ_CST)) { |
---|
[61dd73d] | 235 | // Drain the io |
---|
[5dadc9b] | 236 | int count; |
---|
| 237 | bool again; |
---|
[13c5e19] | 238 | disable_interrupts(); |
---|
[f00b26d4] | 239 | [count, again] = __drain_io( *this.ring ); |
---|
[5dadc9b] | 240 | |
---|
[13c5e19] | 241 | if(!again) reset++; |
---|
[3c039b0] | 242 | |
---|
[13c5e19] | 243 | // Update statistics |
---|
[47746a2] | 244 | __STATS__( true, |
---|
| 245 | io.complete_q.completed_avg.val += count; |
---|
[dcb5f8d] | 246 | io.complete_q.completed_avg.cnt += 1; |
---|
[47746a2] | 247 | ) |
---|
[13c5e19] | 248 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[3c039b0] | 249 | |
---|
[5dadc9b] | 250 | // If we got something, just yield and check again |
---|
[4e74466] | 251 | if(reset < 5) { |
---|
[61dd73d] | 252 | yield(); |
---|
| 253 | } |
---|
[5dadc9b] | 254 | // We didn't get anything baton pass to the slow poller |
---|
[61dd73d] | 255 | else { |
---|
[93526ef] | 256 | __STATS__( false, |
---|
[dcb5f8d] | 257 | io.complete_q.blocks += 1; |
---|
| 258 | ) |
---|
[f00b26d4] | 259 | __cfadbg_print_safe(io_core, "Kernel I/O : Parking io poller %p\n", &this.self); |
---|
[5dadc9b] | 260 | reset = 0; |
---|
| 261 | |
---|
[3e2b9c9] | 262 | // block this thread |
---|
| 263 | __ioctx_prepare_block( this, ev ); |
---|
[f00b26d4] | 264 | wait( this.sem ); |
---|
[f6660520] | 265 | } |
---|
| 266 | } |
---|
[61dd73d] | 267 | |
---|
| 268 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p stopping\n", &this.ring); |
---|
| 269 | } |
---|
[f6660520] | 270 | |
---|
[92976d9] | 271 | //============================================================================================= |
---|
| 272 | // I/O Submissions |
---|
| 273 | //============================================================================================= |
---|
| 274 | |
---|
[2d8f7b0] | 275 | // Submition steps : |
---|
[e46c753] | 276 | // 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones |
---|
[2d8f7b0] | 277 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not |
---|
| 278 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we |
---|
| 279 | // need to write an allocator that allows allocating concurrently. |
---|
| 280 | // |
---|
[e46c753] | 281 | // 2 - Actually fill the submit entry, this is the only simple and straightforward step. |
---|
[2d8f7b0] | 282 | // |
---|
[e46c753] | 283 | // 3 - Append the entry index to the array and adjust the tail accordingly. This operation |
---|
[2d8f7b0] | 284 | // needs to arrive to two concensus at the same time: |
---|
| 285 | // A - The order in which entries are listed in the array: no two threads must pick the |
---|
| 286 | // same index for their entries |
---|
| 287 | // B - When can the tail be update for the kernel. EVERY entries in the array between |
---|
| 288 | // head and tail must be fully filled and shouldn't ever be touched again. |
---|
| 289 | // |
---|
| 290 | |
---|
[4998155] | 291 | [* struct io_uring_sqe, __u32] __submit_alloc( struct __io_data & ring, __u64 data ) { |
---|
[e46c753] | 292 | /* paranoid */ verify( data != 0 ); |
---|
[13c5e19] | 293 | |
---|
[6f121b8] | 294 | // Prepare the data we need |
---|
| 295 | __attribute((unused)) int len = 0; |
---|
| 296 | __attribute((unused)) int block = 0; |
---|
[4998155] | 297 | __u32 cnt = *ring.submit_q.num; |
---|
| 298 | __u32 mask = *ring.submit_q.mask; |
---|
[8ae4165] | 299 | |
---|
[fe9468e2] | 300 | __u32 off = thread_rand(); |
---|
[6f121b8] | 301 | |
---|
| 302 | // Loop around looking for an available spot |
---|
[13c5e19] | 303 | for() { |
---|
[6f121b8] | 304 | // Look through the list starting at some offset |
---|
| 305 | for(i; cnt) { |
---|
[4998155] | 306 | __u64 expected = 0; |
---|
| 307 | __u32 idx = (i + off) & mask; |
---|
[6f121b8] | 308 | struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx]; |
---|
[4998155] | 309 | volatile __u64 * udata = &sqe->user_data; |
---|
[6f121b8] | 310 | |
---|
| 311 | if( *udata == expected && |
---|
| 312 | __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) |
---|
| 313 | { |
---|
| 314 | // update statistics |
---|
[47746a2] | 315 | __STATS__( false, |
---|
| 316 | io.submit_q.alloc_avg.val += len; |
---|
| 317 | io.submit_q.alloc_avg.block += block; |
---|
| 318 | io.submit_q.alloc_avg.cnt += 1; |
---|
| 319 | ) |
---|
[6f121b8] | 320 | |
---|
[13c5e19] | 321 | |
---|
[6f121b8] | 322 | // Success return the data |
---|
| 323 | return [sqe, idx]; |
---|
| 324 | } |
---|
| 325 | verify(expected != data); |
---|
[2489d31] | 326 | |
---|
[6f121b8] | 327 | len ++; |
---|
| 328 | } |
---|
[2489d31] | 329 | |
---|
[6f121b8] | 330 | block++; |
---|
| 331 | yield(); |
---|
| 332 | } |
---|
[2489d31] | 333 | } |
---|
| 334 | |
---|
[4998155] | 335 | static inline __u32 __submit_to_ready_array( struct __io_data & ring, __u32 idx, const __u32 mask ) { |
---|
[df40a56] | 336 | /* paranoid */ verify( idx <= mask ); |
---|
| 337 | /* paranoid */ verify( idx != -1ul32 ); |
---|
| 338 | |
---|
| 339 | // We need to find a spot in the ready array |
---|
| 340 | __attribute((unused)) int len = 0; |
---|
| 341 | __attribute((unused)) int block = 0; |
---|
[4998155] | 342 | __u32 ready_mask = ring.submit_q.ready_cnt - 1; |
---|
[df40a56] | 343 | |
---|
[fe9468e2] | 344 | __u32 off = thread_rand(); |
---|
[df40a56] | 345 | |
---|
[4998155] | 346 | __u32 picked; |
---|
[df40a56] | 347 | LOOKING: for() { |
---|
| 348 | for(i; ring.submit_q.ready_cnt) { |
---|
| 349 | picked = (i + off) & ready_mask; |
---|
[4998155] | 350 | __u32 expected = -1ul32; |
---|
[df40a56] | 351 | if( __atomic_compare_exchange_n( &ring.submit_q.ready[picked], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) { |
---|
| 352 | break LOOKING; |
---|
| 353 | } |
---|
| 354 | verify(expected != idx); |
---|
| 355 | |
---|
| 356 | len ++; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | block++; |
---|
[2fafe7e] | 360 | |
---|
| 361 | __u32 released = __release_consumed_submission( ring ); |
---|
| 362 | if( released == 0 ) { |
---|
[34b61882] | 363 | yield(); |
---|
| 364 | } |
---|
[df40a56] | 365 | } |
---|
| 366 | |
---|
| 367 | // update statistics |
---|
[47746a2] | 368 | __STATS__( false, |
---|
| 369 | io.submit_q.look_avg.val += len; |
---|
| 370 | io.submit_q.look_avg.block += block; |
---|
| 371 | io.submit_q.look_avg.cnt += 1; |
---|
| 372 | ) |
---|
[df40a56] | 373 | |
---|
| 374 | return picked; |
---|
| 375 | } |
---|
| 376 | |
---|
[4998155] | 377 | void __submit( struct io_context * ctx, __u32 idx ) __attribute__((nonnull (1))) { |
---|
[f00b26d4] | 378 | __io_data & ring = *ctx->thrd.ring; |
---|
[5dadc9b] | 379 | // Get now the data we definetely need |
---|
[4998155] | 380 | volatile __u32 * const tail = ring.submit_q.tail; |
---|
| 381 | const __u32 mask = *ring.submit_q.mask; |
---|
[2489d31] | 382 | |
---|
[5dadc9b] | 383 | // There are 2 submission schemes, check which one we are using |
---|
[f00b26d4] | 384 | if( ring.poller_submits ) { |
---|
[5dadc9b] | 385 | // If the poller thread submits, then we just need to add this to the ready array |
---|
[df40a56] | 386 | __submit_to_ready_array( ring, idx, mask ); |
---|
[5dadc9b] | 387 | |
---|
[f00b26d4] | 388 | post( ctx->thrd.sem ); |
---|
[5dadc9b] | 389 | |
---|
[dd4e2d7] | 390 | __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() ); |
---|
[2d8f7b0] | 391 | } |
---|
[f00b26d4] | 392 | else if( ring.eager_submits ) { |
---|
[4998155] | 393 | __u32 picked = __submit_to_ready_array( ring, idx, mask ); |
---|
[e46c753] | 394 | |
---|
[2fafe7e] | 395 | #if defined(LEADER_LOCK) |
---|
| 396 | if( !try_lock(ring.submit_q.submit_lock) ) { |
---|
[47746a2] | 397 | __STATS__( false, |
---|
| 398 | io.submit_q.helped += 1; |
---|
| 399 | ) |
---|
[e46c753] | 400 | return; |
---|
| 401 | } |
---|
[8fc652e0] | 402 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[2fafe7e] | 403 | __STATS__( true, |
---|
| 404 | io.submit_q.leader += 1; |
---|
| 405 | ) |
---|
| 406 | #else |
---|
| 407 | for() { |
---|
| 408 | yield(); |
---|
| 409 | |
---|
| 410 | if( try_lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2) ) { |
---|
| 411 | __STATS__( false, |
---|
| 412 | io.submit_q.leader += 1; |
---|
| 413 | ) |
---|
| 414 | break; |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | // If some one else collected our index, we are done |
---|
| 418 | #warning ABA problem |
---|
| 419 | if( ring.submit_q.ready[picked] != idx ) { |
---|
| 420 | __STATS__( false, |
---|
| 421 | io.submit_q.helped += 1; |
---|
| 422 | ) |
---|
| 423 | return; |
---|
| 424 | } |
---|
[e46c753] | 425 | |
---|
[47746a2] | 426 | __STATS__( false, |
---|
[2fafe7e] | 427 | io.submit_q.busy += 1; |
---|
[47746a2] | 428 | ) |
---|
[e46c753] | 429 | } |
---|
[2fafe7e] | 430 | #endif |
---|
[e46c753] | 431 | |
---|
| 432 | // We got the lock |
---|
[be36ec3] | 433 | // Collect the submissions |
---|
[e46c753] | 434 | unsigned to_submit = __collect_submitions( ring ); |
---|
| 435 | |
---|
[be36ec3] | 436 | // Actually submit |
---|
| 437 | int ret = __io_uring_enter( ring, to_submit, false ); |
---|
[309d814] | 438 | |
---|
[2fafe7e] | 439 | #if defined(LEADER_LOCK) |
---|
[8fc652e0] | 440 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[2fafe7e] | 441 | next(ring.submit_q.submit_lock); |
---|
| 442 | #else |
---|
| 443 | unlock(ring.submit_q.submit_lock); |
---|
| 444 | #endif |
---|
[be36ec3] | 445 | if( ret < 0 ) return; |
---|
[e46c753] | 446 | |
---|
| 447 | // Release the consumed SQEs |
---|
[34b61882] | 448 | __release_consumed_submission( ring ); |
---|
[e46c753] | 449 | |
---|
| 450 | // update statistics |
---|
[be36ec3] | 451 | __STATS__( false, |
---|
[47746a2] | 452 | io.submit_q.submit_avg.rdy += to_submit; |
---|
| 453 | io.submit_q.submit_avg.csm += ret; |
---|
| 454 | io.submit_q.submit_avg.cnt += 1; |
---|
| 455 | ) |
---|
[e46c753] | 456 | } |
---|
[5dadc9b] | 457 | else { |
---|
| 458 | // get mutual exclusion |
---|
[2fafe7e] | 459 | #if defined(LEADER_LOCK) |
---|
| 460 | while(!try_lock(ring.submit_q.submit_lock)); |
---|
| 461 | #else |
---|
| 462 | lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2); |
---|
| 463 | #endif |
---|
[2489d31] | 464 | |
---|
[20ab637] | 465 | /* paranoid */ verifyf( ring.submit_q.sqes[ idx ].user_data != 0, |
---|
| 466 | /* paranoid */ "index %u already reclaimed\n" |
---|
| 467 | /* paranoid */ "head %u, prev %u, tail %u\n" |
---|
| 468 | /* paranoid */ "[-0: %u,-1: %u,-2: %u,-3: %u]\n", |
---|
| 469 | /* paranoid */ idx, |
---|
| 470 | /* paranoid */ *ring.submit_q.head, ring.submit_q.prev_head, *tail |
---|
| 471 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 0) & (*ring.submit_q.mask) ] |
---|
| 472 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 1) & (*ring.submit_q.mask) ] |
---|
| 473 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 2) & (*ring.submit_q.mask) ] |
---|
| 474 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 3) & (*ring.submit_q.mask) ] |
---|
| 475 | /* paranoid */ ); |
---|
| 476 | |
---|
[5dadc9b] | 477 | // Append to the list of ready entries |
---|
| 478 | |
---|
| 479 | /* paranoid */ verify( idx <= mask ); |
---|
[20ab637] | 480 | ring.submit_q.array[ (*tail) & mask ] = idx; |
---|
[5dadc9b] | 481 | __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST); |
---|
[d384787] | 482 | |
---|
[5dadc9b] | 483 | // Submit however, many entries need to be submitted |
---|
[f00b26d4] | 484 | int ret = __io_uring_enter( ring, 1, false ); |
---|
[5dadc9b] | 485 | if( ret < 0 ) { |
---|
| 486 | switch((int)errno) { |
---|
| 487 | default: |
---|
| 488 | abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) ); |
---|
| 489 | } |
---|
| 490 | } |
---|
[d384787] | 491 | |
---|
[5dadc9b] | 492 | // update statistics |
---|
[47746a2] | 493 | __STATS__( false, |
---|
| 494 | io.submit_q.submit_avg.csm += 1; |
---|
| 495 | io.submit_q.submit_avg.cnt += 1; |
---|
| 496 | ) |
---|
[5dadc9b] | 497 | |
---|
[34b61882] | 498 | // Release the consumed SQEs |
---|
| 499 | __release_consumed_submission( ring ); |
---|
[7bfc849] | 500 | |
---|
[2fafe7e] | 501 | #if defined(LEADER_LOCK) |
---|
| 502 | next(ring.submit_q.submit_lock); |
---|
| 503 | #else |
---|
| 504 | unlock(ring.submit_q.submit_lock); |
---|
| 505 | #endif |
---|
[dd4e2d7] | 506 | |
---|
| 507 | __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret ); |
---|
[5dadc9b] | 508 | } |
---|
[2489d31] | 509 | } |
---|
[e46c753] | 510 | |
---|
[d2b5d2d] | 511 | // #define PARTIAL_SUBMIT 32 |
---|
[e46c753] | 512 | static unsigned __collect_submitions( struct __io_data & ring ) { |
---|
| 513 | /* paranoid */ verify( ring.submit_q.ready != 0p ); |
---|
| 514 | /* paranoid */ verify( ring.submit_q.ready_cnt > 0 ); |
---|
| 515 | |
---|
| 516 | unsigned to_submit = 0; |
---|
[4998155] | 517 | __u32 tail = *ring.submit_q.tail; |
---|
| 518 | const __u32 mask = *ring.submit_q.mask; |
---|
[1095ccd] | 519 | #if defined(PARTIAL_SUBMIT) |
---|
| 520 | #if defined(LEADER_LOCK) |
---|
| 521 | #error PARTIAL_SUBMIT and LEADER_LOCK cannot co-exist |
---|
| 522 | #endif |
---|
| 523 | const __u32 cnt = ring.submit_q.ready_cnt > PARTIAL_SUBMIT ? PARTIAL_SUBMIT : ring.submit_q.ready_cnt; |
---|
| 524 | const __u32 offset = ring.submit_q.prev_ready; |
---|
| 525 | ring.submit_q.prev_ready += cnt; |
---|
| 526 | #else |
---|
| 527 | const __u32 cnt = ring.submit_q.ready_cnt; |
---|
| 528 | const __u32 offset = 0; |
---|
| 529 | #endif |
---|
[e46c753] | 530 | |
---|
| 531 | // Go through the list of ready submissions |
---|
[1095ccd] | 532 | for( c; cnt ) { |
---|
| 533 | __u32 i = (offset + c) % ring.submit_q.ready_cnt; |
---|
| 534 | |
---|
[e46c753] | 535 | // replace any submission with the sentinel, to consume it. |
---|
[4998155] | 536 | __u32 idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED); |
---|
[e46c753] | 537 | |
---|
| 538 | // If it was already the sentinel, then we are done |
---|
| 539 | if( idx == -1ul32 ) continue; |
---|
| 540 | |
---|
| 541 | // If we got a real submission, append it to the list |
---|
| 542 | ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask; |
---|
| 543 | to_submit++; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | // Increment the tail based on how many we are ready to submit |
---|
| 547 | __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST); |
---|
| 548 | |
---|
| 549 | return to_submit; |
---|
| 550 | } |
---|
[34b61882] | 551 | |
---|
[4998155] | 552 | static __u32 __release_consumed_submission( struct __io_data & ring ) { |
---|
| 553 | const __u32 smask = *ring.submit_q.mask; |
---|
[732b406] | 554 | |
---|
| 555 | if( !try_lock(ring.submit_q.release_lock __cfaabi_dbg_ctx2) ) return 0; |
---|
[4998155] | 556 | __u32 chead = *ring.submit_q.head; |
---|
| 557 | __u32 phead = ring.submit_q.prev_head; |
---|
[34b61882] | 558 | ring.submit_q.prev_head = chead; |
---|
[732b406] | 559 | unlock(ring.submit_q.release_lock); |
---|
| 560 | |
---|
[4998155] | 561 | __u32 count = chead - phead; |
---|
[34b61882] | 562 | for( i; count ) { |
---|
[4998155] | 563 | __u32 idx = ring.submit_q.array[ (phead + i) & smask ]; |
---|
[34b61882] | 564 | ring.submit_q.sqes[ idx ].user_data = 0; |
---|
| 565 | } |
---|
| 566 | return count; |
---|
| 567 | } |
---|
[47746a2] | 568 | #endif |
---|