[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 ) { |
---|
| 78 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 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 ) { |
---|
[f00b26d4] | 136 | ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, 0, flags, 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; |
---|
| 162 | __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", future, cqe.res, data->thrd ); |
---|
[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 ) { |
---|
[e46c753] | 170 | /* paranoid */ verify( !kernelTLS.preemption_state.enabled ); |
---|
| 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 | |
---|
| 300 | disable_interrupts(); |
---|
[4998155] | 301 | __u32 off = __tls_rand(); |
---|
[8ae4165] | 302 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[6f121b8] | 303 | |
---|
| 304 | // Loop around looking for an available spot |
---|
[13c5e19] | 305 | for() { |
---|
[6f121b8] | 306 | // Look through the list starting at some offset |
---|
| 307 | for(i; cnt) { |
---|
[4998155] | 308 | __u64 expected = 0; |
---|
| 309 | __u32 idx = (i + off) & mask; |
---|
[6f121b8] | 310 | struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx]; |
---|
[4998155] | 311 | volatile __u64 * udata = &sqe->user_data; |
---|
[6f121b8] | 312 | |
---|
| 313 | if( *udata == expected && |
---|
| 314 | __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) |
---|
| 315 | { |
---|
| 316 | // update statistics |
---|
[47746a2] | 317 | __STATS__( false, |
---|
| 318 | io.submit_q.alloc_avg.val += len; |
---|
| 319 | io.submit_q.alloc_avg.block += block; |
---|
| 320 | io.submit_q.alloc_avg.cnt += 1; |
---|
| 321 | ) |
---|
[6f121b8] | 322 | |
---|
[13c5e19] | 323 | |
---|
[6f121b8] | 324 | // Success return the data |
---|
| 325 | return [sqe, idx]; |
---|
| 326 | } |
---|
| 327 | verify(expected != data); |
---|
[2489d31] | 328 | |
---|
[6f121b8] | 329 | len ++; |
---|
| 330 | } |
---|
[2489d31] | 331 | |
---|
[6f121b8] | 332 | block++; |
---|
| 333 | yield(); |
---|
| 334 | } |
---|
[2489d31] | 335 | } |
---|
| 336 | |
---|
[4998155] | 337 | static inline __u32 __submit_to_ready_array( struct __io_data & ring, __u32 idx, const __u32 mask ) { |
---|
[df40a56] | 338 | /* paranoid */ verify( idx <= mask ); |
---|
| 339 | /* paranoid */ verify( idx != -1ul32 ); |
---|
| 340 | |
---|
| 341 | // We need to find a spot in the ready array |
---|
| 342 | __attribute((unused)) int len = 0; |
---|
| 343 | __attribute((unused)) int block = 0; |
---|
[4998155] | 344 | __u32 ready_mask = ring.submit_q.ready_cnt - 1; |
---|
[df40a56] | 345 | |
---|
| 346 | disable_interrupts(); |
---|
[4998155] | 347 | __u32 off = __tls_rand(); |
---|
[df40a56] | 348 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 349 | |
---|
[4998155] | 350 | __u32 picked; |
---|
[df40a56] | 351 | LOOKING: for() { |
---|
| 352 | for(i; ring.submit_q.ready_cnt) { |
---|
| 353 | picked = (i + off) & ready_mask; |
---|
[4998155] | 354 | __u32 expected = -1ul32; |
---|
[df40a56] | 355 | if( __atomic_compare_exchange_n( &ring.submit_q.ready[picked], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) { |
---|
| 356 | break LOOKING; |
---|
| 357 | } |
---|
| 358 | verify(expected != idx); |
---|
| 359 | |
---|
| 360 | len ++; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | block++; |
---|
[2fafe7e] | 364 | |
---|
| 365 | __u32 released = __release_consumed_submission( ring ); |
---|
| 366 | if( released == 0 ) { |
---|
[34b61882] | 367 | yield(); |
---|
| 368 | } |
---|
[df40a56] | 369 | } |
---|
| 370 | |
---|
| 371 | // update statistics |
---|
[47746a2] | 372 | __STATS__( false, |
---|
| 373 | io.submit_q.look_avg.val += len; |
---|
| 374 | io.submit_q.look_avg.block += block; |
---|
| 375 | io.submit_q.look_avg.cnt += 1; |
---|
| 376 | ) |
---|
[df40a56] | 377 | |
---|
| 378 | return picked; |
---|
| 379 | } |
---|
| 380 | |
---|
[4998155] | 381 | void __submit( struct io_context * ctx, __u32 idx ) __attribute__((nonnull (1))) { |
---|
[f00b26d4] | 382 | __io_data & ring = *ctx->thrd.ring; |
---|
[5dadc9b] | 383 | // Get now the data we definetely need |
---|
[4998155] | 384 | volatile __u32 * const tail = ring.submit_q.tail; |
---|
| 385 | const __u32 mask = *ring.submit_q.mask; |
---|
[2489d31] | 386 | |
---|
[5dadc9b] | 387 | // There are 2 submission schemes, check which one we are using |
---|
[f00b26d4] | 388 | if( ring.poller_submits ) { |
---|
[5dadc9b] | 389 | // If the poller thread submits, then we just need to add this to the ready array |
---|
[df40a56] | 390 | __submit_to_ready_array( ring, idx, mask ); |
---|
[5dadc9b] | 391 | |
---|
[f00b26d4] | 392 | post( ctx->thrd.sem ); |
---|
[5dadc9b] | 393 | |
---|
[dd4e2d7] | 394 | __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() ); |
---|
[2d8f7b0] | 395 | } |
---|
[f00b26d4] | 396 | else if( ring.eager_submits ) { |
---|
[4998155] | 397 | __u32 picked = __submit_to_ready_array( ring, idx, mask ); |
---|
[e46c753] | 398 | |
---|
[2fafe7e] | 399 | #if defined(LEADER_LOCK) |
---|
| 400 | if( !try_lock(ring.submit_q.submit_lock) ) { |
---|
[47746a2] | 401 | __STATS__( false, |
---|
| 402 | io.submit_q.helped += 1; |
---|
| 403 | ) |
---|
[e46c753] | 404 | return; |
---|
| 405 | } |
---|
[2fafe7e] | 406 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 407 | __STATS__( true, |
---|
| 408 | io.submit_q.leader += 1; |
---|
| 409 | ) |
---|
| 410 | #else |
---|
| 411 | for() { |
---|
| 412 | yield(); |
---|
| 413 | |
---|
| 414 | if( try_lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2) ) { |
---|
| 415 | __STATS__( false, |
---|
| 416 | io.submit_q.leader += 1; |
---|
| 417 | ) |
---|
| 418 | break; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | // If some one else collected our index, we are done |
---|
| 422 | #warning ABA problem |
---|
| 423 | if( ring.submit_q.ready[picked] != idx ) { |
---|
| 424 | __STATS__( false, |
---|
| 425 | io.submit_q.helped += 1; |
---|
| 426 | ) |
---|
| 427 | return; |
---|
| 428 | } |
---|
[e46c753] | 429 | |
---|
[47746a2] | 430 | __STATS__( false, |
---|
[2fafe7e] | 431 | io.submit_q.busy += 1; |
---|
[47746a2] | 432 | ) |
---|
[e46c753] | 433 | } |
---|
[2fafe7e] | 434 | #endif |
---|
[e46c753] | 435 | |
---|
| 436 | // We got the lock |
---|
[be36ec3] | 437 | // Collect the submissions |
---|
[e46c753] | 438 | unsigned to_submit = __collect_submitions( ring ); |
---|
| 439 | |
---|
[be36ec3] | 440 | // Actually submit |
---|
| 441 | int ret = __io_uring_enter( ring, to_submit, false ); |
---|
[309d814] | 442 | |
---|
[2fafe7e] | 443 | #if defined(LEADER_LOCK) |
---|
| 444 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 445 | next(ring.submit_q.submit_lock); |
---|
| 446 | #else |
---|
| 447 | unlock(ring.submit_q.submit_lock); |
---|
| 448 | #endif |
---|
[be36ec3] | 449 | if( ret < 0 ) return; |
---|
[e46c753] | 450 | |
---|
| 451 | // Release the consumed SQEs |
---|
[34b61882] | 452 | __release_consumed_submission( ring ); |
---|
[e46c753] | 453 | |
---|
| 454 | // update statistics |
---|
[be36ec3] | 455 | __STATS__( false, |
---|
[47746a2] | 456 | io.submit_q.submit_avg.rdy += to_submit; |
---|
| 457 | io.submit_q.submit_avg.csm += ret; |
---|
| 458 | io.submit_q.submit_avg.cnt += 1; |
---|
| 459 | ) |
---|
[e46c753] | 460 | } |
---|
[5dadc9b] | 461 | else { |
---|
| 462 | // get mutual exclusion |
---|
[2fafe7e] | 463 | #if defined(LEADER_LOCK) |
---|
| 464 | while(!try_lock(ring.submit_q.submit_lock)); |
---|
| 465 | #else |
---|
| 466 | lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2); |
---|
| 467 | #endif |
---|
[2489d31] | 468 | |
---|
[20ab637] | 469 | /* paranoid */ verifyf( ring.submit_q.sqes[ idx ].user_data != 0, |
---|
| 470 | /* paranoid */ "index %u already reclaimed\n" |
---|
| 471 | /* paranoid */ "head %u, prev %u, tail %u\n" |
---|
| 472 | /* paranoid */ "[-0: %u,-1: %u,-2: %u,-3: %u]\n", |
---|
| 473 | /* paranoid */ idx, |
---|
| 474 | /* paranoid */ *ring.submit_q.head, ring.submit_q.prev_head, *tail |
---|
| 475 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 0) & (*ring.submit_q.mask) ] |
---|
| 476 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 1) & (*ring.submit_q.mask) ] |
---|
| 477 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 2) & (*ring.submit_q.mask) ] |
---|
| 478 | /* paranoid */ ,ring.submit_q.array[ ((*ring.submit_q.head) - 3) & (*ring.submit_q.mask) ] |
---|
| 479 | /* paranoid */ ); |
---|
| 480 | |
---|
[5dadc9b] | 481 | // Append to the list of ready entries |
---|
| 482 | |
---|
| 483 | /* paranoid */ verify( idx <= mask ); |
---|
[20ab637] | 484 | ring.submit_q.array[ (*tail) & mask ] = idx; |
---|
[5dadc9b] | 485 | __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST); |
---|
[d384787] | 486 | |
---|
[5dadc9b] | 487 | // Submit however, many entries need to be submitted |
---|
[f00b26d4] | 488 | int ret = __io_uring_enter( ring, 1, false ); |
---|
[5dadc9b] | 489 | if( ret < 0 ) { |
---|
| 490 | switch((int)errno) { |
---|
| 491 | default: |
---|
| 492 | abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) ); |
---|
| 493 | } |
---|
| 494 | } |
---|
[d384787] | 495 | |
---|
[5dadc9b] | 496 | // update statistics |
---|
[47746a2] | 497 | __STATS__( false, |
---|
| 498 | io.submit_q.submit_avg.csm += 1; |
---|
| 499 | io.submit_q.submit_avg.cnt += 1; |
---|
| 500 | ) |
---|
[5dadc9b] | 501 | |
---|
[34b61882] | 502 | // Release the consumed SQEs |
---|
| 503 | __release_consumed_submission( ring ); |
---|
[7bfc849] | 504 | |
---|
[2fafe7e] | 505 | #if defined(LEADER_LOCK) |
---|
| 506 | next(ring.submit_q.submit_lock); |
---|
| 507 | #else |
---|
| 508 | unlock(ring.submit_q.submit_lock); |
---|
| 509 | #endif |
---|
[dd4e2d7] | 510 | |
---|
| 511 | __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret ); |
---|
[5dadc9b] | 512 | } |
---|
[2489d31] | 513 | } |
---|
[e46c753] | 514 | |
---|
[d2b5d2d] | 515 | // #define PARTIAL_SUBMIT 32 |
---|
[e46c753] | 516 | static unsigned __collect_submitions( struct __io_data & ring ) { |
---|
| 517 | /* paranoid */ verify( ring.submit_q.ready != 0p ); |
---|
| 518 | /* paranoid */ verify( ring.submit_q.ready_cnt > 0 ); |
---|
| 519 | |
---|
| 520 | unsigned to_submit = 0; |
---|
[4998155] | 521 | __u32 tail = *ring.submit_q.tail; |
---|
| 522 | const __u32 mask = *ring.submit_q.mask; |
---|
[1095ccd] | 523 | #if defined(PARTIAL_SUBMIT) |
---|
| 524 | #if defined(LEADER_LOCK) |
---|
| 525 | #error PARTIAL_SUBMIT and LEADER_LOCK cannot co-exist |
---|
| 526 | #endif |
---|
| 527 | const __u32 cnt = ring.submit_q.ready_cnt > PARTIAL_SUBMIT ? PARTIAL_SUBMIT : ring.submit_q.ready_cnt; |
---|
| 528 | const __u32 offset = ring.submit_q.prev_ready; |
---|
| 529 | ring.submit_q.prev_ready += cnt; |
---|
| 530 | #else |
---|
| 531 | const __u32 cnt = ring.submit_q.ready_cnt; |
---|
| 532 | const __u32 offset = 0; |
---|
| 533 | #endif |
---|
[e46c753] | 534 | |
---|
| 535 | // Go through the list of ready submissions |
---|
[1095ccd] | 536 | for( c; cnt ) { |
---|
| 537 | __u32 i = (offset + c) % ring.submit_q.ready_cnt; |
---|
| 538 | |
---|
[e46c753] | 539 | // replace any submission with the sentinel, to consume it. |
---|
[4998155] | 540 | __u32 idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED); |
---|
[e46c753] | 541 | |
---|
| 542 | // If it was already the sentinel, then we are done |
---|
| 543 | if( idx == -1ul32 ) continue; |
---|
| 544 | |
---|
| 545 | // If we got a real submission, append it to the list |
---|
| 546 | ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask; |
---|
| 547 | to_submit++; |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | // Increment the tail based on how many we are ready to submit |
---|
| 551 | __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST); |
---|
| 552 | |
---|
| 553 | return to_submit; |
---|
| 554 | } |
---|
[34b61882] | 555 | |
---|
[4998155] | 556 | static __u32 __release_consumed_submission( struct __io_data & ring ) { |
---|
| 557 | const __u32 smask = *ring.submit_q.mask; |
---|
[732b406] | 558 | |
---|
| 559 | if( !try_lock(ring.submit_q.release_lock __cfaabi_dbg_ctx2) ) return 0; |
---|
[4998155] | 560 | __u32 chead = *ring.submit_q.head; |
---|
| 561 | __u32 phead = ring.submit_q.prev_head; |
---|
[34b61882] | 562 | ring.submit_q.prev_head = chead; |
---|
[732b406] | 563 | unlock(ring.submit_q.release_lock); |
---|
| 564 | |
---|
[4998155] | 565 | __u32 count = chead - phead; |
---|
[34b61882] | 566 | for( i; count ) { |
---|
[4998155] | 567 | __u32 idx = ring.submit_q.array[ (phead + i) & smask ]; |
---|
[34b61882] | 568 | ring.submit_q.sqes[ idx ].user_data = 0; |
---|
| 569 | } |
---|
| 570 | return count; |
---|
| 571 | } |
---|
[47746a2] | 572 | #endif |
---|