source: libcfa/src/concurrency/io.cfa @ 95b3a9c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 95b3a9c was 2fab24e3, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fixed some warnings and printing.

  • Property mode set to 100644
File size: 21.4 KB
RevLine 
[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" {
33                #include <sys/syscall.h>
34
35                #include <linux/io_uring.h>
36        }
37
[3e2b9c9]38        #include "stats.hfa"
39        #include "kernel.hfa"
40        #include "kernel/fwd.hfa"
41        #include "io/types.hfa"
[185efe6]42
[2fab24e3]43        __attribute__((unused)) static const char * opcodes[] = {
[426f60c]44                "OP_NOP",
45                "OP_READV",
46                "OP_WRITEV",
47                "OP_FSYNC",
48                "OP_READ_FIXED",
49                "OP_WRITE_FIXED",
50                "OP_POLL_ADD",
51                "OP_POLL_REMOVE",
52                "OP_SYNC_FILE_RANGE",
53                "OP_SENDMSG",
54                "OP_RECVMSG",
55                "OP_TIMEOUT",
56                "OP_TIMEOUT_REMOVE",
57                "OP_ACCEPT",
58                "OP_ASYNC_CANCEL",
59                "OP_LINK_TIMEOUT",
60                "OP_CONNECT",
61                "OP_FALLOCATE",
62                "OP_OPENAT",
63                "OP_CLOSE",
64                "OP_FILES_UPDATE",
65                "OP_STATX",
66                "OP_READ",
67                "OP_WRITE",
68                "OP_FADVISE",
69                "OP_MADVISE",
70                "OP_SEND",
71                "OP_RECV",
72                "OP_OPENAT2",
73                "OP_EPOLL_CTL",
74                "OP_SPLICE",
75                "OP_PROVIDE_BUFFERS",
76                "OP_REMOVE_BUFFERS",
77                "OP_TEE",
78                "INVALID_OP"
79        };
80
[2fafe7e]81        // returns true of acquired as leader or second leader
82        static inline bool try_lock( __leaderlock_t & this ) {
83                const uintptr_t thrd = 1z | (uintptr_t)active_thread();
84                bool block;
85                disable_interrupts();
86                for() {
87                        struct $thread * expected = this.value;
88                        if( 1p != expected && 0p != expected ) {
89                                /* paranoid */ verify( thrd != (uintptr_t)expected ); // We better not already be the next leader
90                                enable_interrupts( __cfaabi_dbg_ctx );
91                                return false;
92                        }
93                        struct $thread * desired;
94                        if( 0p == expected ) {
95                                // If the lock isn't locked acquire it, no need to block
96                                desired = 1p;
97                                block = false;
98                        }
99                        else {
100                                // If the lock is already locked try becomming the next leader
101                                desired = (struct $thread *)thrd;
102                                block = true;
103                        }
104                        if( __atomic_compare_exchange_n(&this.value, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) break;
105                }
106                if( block ) {
107                        enable_interrupts( __cfaabi_dbg_ctx );
[e235429]108                        park();
[2fafe7e]109                        disable_interrupts();
110                }
111                return true;
112        }
113
114        static inline bool next( __leaderlock_t & this ) {
[8fc652e0]115                /* paranoid */ verify( ! __preemption_enabled() );
[2fafe7e]116                struct $thread * nextt;
117                for() {
118                        struct $thread * expected = this.value;
119                        /* paranoid */ verify( (1 & (uintptr_t)expected) == 1 ); // The lock better be locked
120
121                        struct $thread * desired;
122                        if( 1p == expected ) {
123                                // No next leader, just unlock
124                                desired = 0p;
125                                nextt   = 0p;
126                        }
127                        else {
128                                // There is a next leader, remove but keep locked
129                                desired = 1p;
130                                nextt   = (struct $thread *)(~1z & (uintptr_t)expected);
131                        }
132                        if( __atomic_compare_exchange_n(&this.value, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) break;
133                }
134
135                if(nextt) {
[b4b63e8]136                        unpark( nextt );
[2fafe7e]137                        enable_interrupts( __cfaabi_dbg_ctx );
138                        return true;
139                }
140                enable_interrupts( __cfaabi_dbg_ctx );
141                return false;
142        }
143
[92976d9]144//=============================================================================================
[3e2b9c9]145// I/O Syscall
[92976d9]146//=============================================================================================
[3e2b9c9]147        static int __io_uring_enter( struct __io_data & ring, unsigned to_submit, bool get ) {
[20ab637]148                bool need_sys_to_submit = false;
149                bool need_sys_to_complete = false;
150                unsigned flags = 0;
151
152                TO_SUBMIT:
153                if( to_submit > 0 ) {
154                        if( !(ring.ring_flags & IORING_SETUP_SQPOLL) ) {
155                                need_sys_to_submit = true;
156                                break TO_SUBMIT;
157                        }
158                        if( (*ring.submit_q.flags) & IORING_SQ_NEED_WAKEUP ) {
159                                need_sys_to_submit = true;
160                                flags |= IORING_ENTER_SQ_WAKEUP;
161                        }
162                }
163
164                if( get && !(ring.ring_flags & IORING_SETUP_SQPOLL) ) {
165                        flags |= IORING_ENTER_GETEVENTS;
166                        if( (ring.ring_flags & IORING_SETUP_IOPOLL) ) {
167                                need_sys_to_complete = true;
168                        }
169                }
170
171                int ret = 0;
172                if( need_sys_to_submit || need_sys_to_complete ) {
[ece0e80]173                        __cfadbg_print_safe(io_core, "Kernel I/O : IO_URING enter %d %u %u\n", ring.fd, to_submit, flags);
[b982fb2]174                        ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, 0, flags, (sigset_t *)0p, _NSIG / 8);
[2fab24e3]175                        __cfadbg_print_safe(io_core, "Kernel I/O : IO_URING %d returned %d\n", ring.fd, ret);
176
[20ab637]177                        if( ret < 0 ) {
178                                switch((int)errno) {
179                                case EAGAIN:
180                                case EINTR:
[2fab24e3]181                                case EBUSY:
[20ab637]182                                        ret = -1;
183                                        break;
184                                default:
185                                        abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
186                                }
187                        }
188                }
189
190                // Memory barrier
191                __atomic_thread_fence( __ATOMIC_SEQ_CST );
192                return ret;
193        }
194
[92976d9]195//=============================================================================================
196// I/O Polling
197//=============================================================================================
[1d5e4711]198        static unsigned __collect_submitions( struct __io_data & ring );
[4998155]199        static __u32 __release_consumed_submission( struct __io_data & ring );
[35285fd]200        static inline void __clean( volatile struct io_uring_sqe * sqe );
[1d5e4711]201
[426f60c]202        // Process a single completion message from the io_uring
203        // This is NOT thread-safe
204        static inline void process( volatile struct io_uring_cqe & cqe ) {
[c402739f]205                struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
[fe9468e2]206                __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
[5751a56]207
[c402739f]208                fulfil( *future, cqe.res );
[5751a56]209        }
210
[f00b26d4]211        static [int, bool] __drain_io( & struct __io_data ring ) {
[8fc652e0]212                /* paranoid */ verify( ! __preemption_enabled() );
[e46c753]213
[5dadc9b]214                unsigned to_submit = 0;
[f00b26d4]215                if( ring.poller_submits ) {
[5dadc9b]216                        // If the poller thread also submits, then we need to aggregate the submissions which are ready
[e46c753]217                        to_submit = __collect_submitions( ring );
[5dadc9b]218                }
219
[f00b26d4]220                int ret = __io_uring_enter(ring, to_submit, true);
[20ab637]221                if( ret < 0 ) {
222                        return [0, true];
223                }
[1d5e4711]224
[20ab637]225                // update statistics
226                if (to_submit > 0) {
[1d5e4711]227                        __STATS__( true,
228                                if( to_submit > 0 ) {
229                                        io.submit_q.submit_avg.rdy += to_submit;
230                                        io.submit_q.submit_avg.csm += ret;
231                                        io.submit_q.submit_avg.cnt += 1;
232                                }
233                        )
[6f121b8]234                }
235
[426f60c]236                __atomic_thread_fence( __ATOMIC_SEQ_CST );
237
[20ab637]238                // Release the consumed SQEs
239                __release_consumed_submission( ring );
[6f121b8]240
[d384787]241                // Drain the queue
[92976d9]242                unsigned head = *ring.completion_q.head;
[6f121b8]243                unsigned tail = *ring.completion_q.tail;
[4998155]244                const __u32 mask = *ring.completion_q.mask;
[6f121b8]245
[d384787]246                // Nothing was new return 0
247                if (head == tail) {
[e46c753]248                        return [0, to_submit > 0];
[d384787]249                }
[92976d9]250
[4998155]251                __u32 count = tail - head;
[1d5e4711]252                /* paranoid */ verify( count != 0 );
[d384787]253                for(i; count) {
[6f121b8]254                        unsigned idx = (head + i) & mask;
[426f60c]255                        volatile struct io_uring_cqe & cqe = ring.completion_q.cqes[idx];
[92976d9]256
[d384787]257                        /* paranoid */ verify(&cqe);
[92976d9]258
[f00b26d4]259                        process( cqe );
[d384787]260                }
[2d8f7b0]261
[92976d9]262                // Mark to the kernel that the cqe has been seen
263                // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
[426f60c]264                __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_SEQ_CST );
[92976d9]265
[5dadc9b]266                return [count, count > 0 || to_submit > 0];
[92976d9]267        }
268
[f00b26d4]269        void main( $io_ctx_thread & this ) {
[d48b174]270                __ioctx_register( this );
[1539bbd]271
[426f60c]272                __cfadbg_print_safe(io_core, "Kernel I/O : IO poller %d (%p) ready\n", this.ring->fd, &this);
[1539bbd]273
[ece0e80]274                const int reset_cnt = 5;
275                int reset = reset_cnt;
[61dd73d]276                // Then loop until we need to start
[ece0e80]277                LOOP:
[f00b26d4]278                while(!__atomic_load_n(&this.done, __ATOMIC_SEQ_CST)) {
[61dd73d]279                        // Drain the io
[5dadc9b]280                        int count;
281                        bool again;
[13c5e19]282                        disable_interrupts();
[f00b26d4]283                                [count, again] = __drain_io( *this.ring );
[5dadc9b]284
[ece0e80]285                                if(!again) reset--;
[3c039b0]286
[13c5e19]287                                // Update statistics
[47746a2]288                                __STATS__( true,
289                                        io.complete_q.completed_avg.val += count;
[dcb5f8d]290                                        io.complete_q.completed_avg.cnt += 1;
[47746a2]291                                )
[13c5e19]292                        enable_interrupts( __cfaabi_dbg_ctx );
[3c039b0]293
[5dadc9b]294                        // If we got something, just yield and check again
[ece0e80]295                        if(reset > 1) {
[61dd73d]296                                yield();
[ece0e80]297                                continue LOOP;
[61dd73d]298                        }
[ece0e80]299
[426f60c]300                        // We alread failed to find completed entries a few time.
[ece0e80]301                        if(reset == 1) {
302                                // Rearm the context so it can block
303                                // but don't block right away
304                                // we need to retry one last time in case
305                                // something completed *just now*
[d48b174]306                                __ioctx_prepare_block( this );
[ece0e80]307                                continue LOOP;
308                        }
309
[93526ef]310                                __STATS__( false,
[dcb5f8d]311                                        io.complete_q.blocks += 1;
312                                )
[426f60c]313                                __cfadbg_print_safe(io_core, "Kernel I/O : Parking io poller %d (%p)\n", this.ring->fd, &this);
[5dadc9b]314
[3e2b9c9]315                                // block this thread
[f00b26d4]316                                wait( this.sem );
[ece0e80]317
318                        // restore counter
319                        reset = reset_cnt;
[f6660520]320                }
[61dd73d]321
[426f60c]322                __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller %d (%p) stopping\n", this.ring->fd, &this);
[d611995]323
324                __ioctx_unregister( this );
[61dd73d]325        }
[f6660520]326
[92976d9]327//=============================================================================================
328// I/O Submissions
329//=============================================================================================
330
[2d8f7b0]331// Submition steps :
[e46c753]332// 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
[2d8f7b0]333//     listed in sq.array are visible by the kernel. For those not listed, the kernel does not
334//     offer any assurance that an entry is not being filled by multiple flags. Therefore, we
335//     need to write an allocator that allows allocating concurrently.
336//
[e46c753]337// 2 - Actually fill the submit entry, this is the only simple and straightforward step.
[2d8f7b0]338//
[e46c753]339// 3 - Append the entry index to the array and adjust the tail accordingly. This operation
[2d8f7b0]340//     needs to arrive to two concensus at the same time:
341//     A - The order in which entries are listed in the array: no two threads must pick the
342//         same index for their entries
343//     B - When can the tail be update for the kernel. EVERY entries in the array between
344//         head and tail must be fully filled and shouldn't ever be touched again.
345//
346
[426f60c]347        // Allocate an submit queue entry.
348        // The kernel cannot see these entries until they are submitted, but other threads must be
349        // able to see which entries can be used and which are already un used by an other thread
350        // for convenience, return both the index and the pointer to the sqe
351        // sqe == &sqes[idx]
352        [* volatile struct io_uring_sqe, __u32] __submit_alloc( struct __io_data & ring, __u64 data ) {
[e46c753]353                /* paranoid */ verify( data != 0 );
[13c5e19]354
[6f121b8]355                // Prepare the data we need
356                __attribute((unused)) int len   = 0;
357                __attribute((unused)) int block = 0;
[4998155]358                __u32 cnt = *ring.submit_q.num;
359                __u32 mask = *ring.submit_q.mask;
[8ae4165]360
[fe9468e2]361                __u32 off = thread_rand();
[6f121b8]362
363                // Loop around looking for an available spot
[13c5e19]364                for() {
[6f121b8]365                        // Look through the list starting at some offset
366                        for(i; cnt) {
[426f60c]367                                __u64 expected = 3;
368                                __u32 idx = (i + off) & mask; // Get an index from a random
369                                volatile struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx];
[4998155]370                                volatile __u64 * udata = &sqe->user_data;
[6f121b8]371
[426f60c]372                                // Allocate the entry by CASing the user_data field from 0 to the future address
[6f121b8]373                                if( *udata == expected &&
374                                        __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) )
375                                {
376                                        // update statistics
[47746a2]377                                        __STATS__( false,
378                                                io.submit_q.alloc_avg.val   += len;
379                                                io.submit_q.alloc_avg.block += block;
380                                                io.submit_q.alloc_avg.cnt   += 1;
381                                        )
[6f121b8]382
[426f60c]383                                        // debug log
[ece0e80]384                                        __cfadbg_print_safe( io, "Kernel I/O : allocated [%p, %u] for %p (%p)\n", sqe, idx, active_thread(), (void*)data );
[13c5e19]385
[6f121b8]386                                        // Success return the data
387                                        return [sqe, idx];
388                                }
389                                verify(expected != data);
[2489d31]390
[426f60c]391                                // This one was used
[6f121b8]392                                len ++;
393                        }
[2489d31]394
[6f121b8]395                        block++;
[426f60c]396
[6f121b8]397                        yield();
398                }
[2489d31]399        }
400
[4998155]401        static inline __u32 __submit_to_ready_array( struct __io_data & ring, __u32 idx, const __u32 mask ) {
[df40a56]402                /* paranoid */ verify( idx <= mask   );
403                /* paranoid */ verify( idx != -1ul32 );
404
405                // We need to find a spot in the ready array
406                __attribute((unused)) int len   = 0;
407                __attribute((unused)) int block = 0;
[4998155]408                __u32 ready_mask = ring.submit_q.ready_cnt - 1;
[df40a56]409
[fe9468e2]410                __u32 off = thread_rand();
[df40a56]411
[4998155]412                __u32 picked;
[df40a56]413                LOOKING: for() {
414                        for(i; ring.submit_q.ready_cnt) {
415                                picked = (i + off) & ready_mask;
[4998155]416                                __u32 expected = -1ul32;
[df40a56]417                                if( __atomic_compare_exchange_n( &ring.submit_q.ready[picked], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) {
418                                        break LOOKING;
419                                }
420                                verify(expected != idx);
421
422                                len ++;
423                        }
424
425                        block++;
[2fafe7e]426
427                        __u32 released = __release_consumed_submission( ring );
428                        if( released == 0 ) {
[34b61882]429                                yield();
430                        }
[df40a56]431                }
432
433                // update statistics
[47746a2]434                __STATS__( false,
435                        io.submit_q.look_avg.val   += len;
436                        io.submit_q.look_avg.block += block;
437                        io.submit_q.look_avg.cnt   += 1;
438                )
[df40a56]439
440                return picked;
441        }
442
[4998155]443        void __submit( struct io_context * ctx, __u32 idx ) __attribute__((nonnull (1))) {
[f00b26d4]444                __io_data & ring = *ctx->thrd.ring;
[426f60c]445
446                {
447                        __attribute__((unused)) volatile struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx];
448                        __cfadbg_print_safe( io,
449                                "Kernel I/O : submitting %u (%p) for %p\n"
450                                "    data: %p\n"
451                                "    opcode: %s\n"
452                                "    fd: %d\n"
453                                "    flags: %d\n"
454                                "    prio: %d\n"
455                                "    off: %p\n"
456                                "    addr: %p\n"
457                                "    len: %d\n"
458                                "    other flags: %d\n"
459                                "    splice fd: %d\n"
460                                "    pad[0]: %llu\n"
461                                "    pad[1]: %llu\n"
462                                "    pad[2]: %llu\n",
463                                idx, sqe,
464                                active_thread(),
465                                (void*)sqe->user_data,
466                                opcodes[sqe->opcode],
467                                sqe->fd,
468                                sqe->flags,
469                                sqe->ioprio,
[2fab24e3]470                                (void*)sqe->off,
471                                (void*)sqe->addr,
[426f60c]472                                sqe->len,
473                                sqe->accept_flags,
474                                sqe->splice_fd_in,
475                                sqe->__pad2[0],
476                                sqe->__pad2[1],
477                                sqe->__pad2[2]
478                        );
479                }
480
481
[5dadc9b]482                // Get now the data we definetely need
[4998155]483                volatile __u32 * const tail = ring.submit_q.tail;
484                const __u32 mask  = *ring.submit_q.mask;
[2489d31]485
[5dadc9b]486                // There are 2 submission schemes, check which one we are using
[f00b26d4]487                if( ring.poller_submits ) {
[5dadc9b]488                        // If the poller thread submits, then we just need to add this to the ready array
[df40a56]489                        __submit_to_ready_array( ring, idx, mask );
[5dadc9b]490
[f00b26d4]491                        post( ctx->thrd.sem );
[5dadc9b]492
[dd4e2d7]493                        __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() );
[2d8f7b0]494                }
[f00b26d4]495                else if( ring.eager_submits ) {
[2fab24e3]496                        __attribute__((unused)) __u32 picked = __submit_to_ready_array( ring, idx, mask );
[e46c753]497
[2fafe7e]498                        #if defined(LEADER_LOCK)
499                                if( !try_lock(ring.submit_q.submit_lock) ) {
[47746a2]500                                        __STATS__( false,
501                                                io.submit_q.helped += 1;
502                                        )
[e46c753]503                                        return;
504                                }
[8fc652e0]505                                /* paranoid */ verify( ! __preemption_enabled() );
[2fafe7e]506                                __STATS__( true,
507                                        io.submit_q.leader += 1;
508                                )
509                        #else
510                                for() {
511                                        yield();
512
513                                        if( try_lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2) ) {
514                                                __STATS__( false,
515                                                        io.submit_q.leader += 1;
516                                                )
517                                                break;
518                                        }
519
520                                        // If some one else collected our index, we are done
521                                        #warning ABA problem
522                                        if( ring.submit_q.ready[picked] != idx ) {
523                                                __STATS__( false,
524                                                        io.submit_q.helped += 1;
525                                                )
526                                                return;
527                                        }
[e46c753]528
[47746a2]529                                        __STATS__( false,
[2fafe7e]530                                                io.submit_q.busy += 1;
[47746a2]531                                        )
[e46c753]532                                }
[2fafe7e]533                        #endif
[e46c753]534
535                        // We got the lock
[be36ec3]536                        // Collect the submissions
[e46c753]537                        unsigned to_submit = __collect_submitions( ring );
538
[be36ec3]539                        // Actually submit
540                        int ret = __io_uring_enter( ring, to_submit, false );
[309d814]541
[2fafe7e]542                        #if defined(LEADER_LOCK)
[8fc652e0]543                                /* paranoid */ verify( ! __preemption_enabled() );
[2fafe7e]544                                next(ring.submit_q.submit_lock);
545                        #else
546                                unlock(ring.submit_q.submit_lock);
547                        #endif
[ece0e80]548                        if( ret < 0 ) {
549                                return;
550                        }
[e46c753]551
552                        // Release the consumed SQEs
[34b61882]553                        __release_consumed_submission( ring );
[e46c753]554
555                        // update statistics
[be36ec3]556                        __STATS__( false,
[47746a2]557                                io.submit_q.submit_avg.rdy += to_submit;
558                                io.submit_q.submit_avg.csm += ret;
559                                io.submit_q.submit_avg.cnt += 1;
560                        )
[ece0e80]561
562                        __cfadbg_print_safe( io, "Kernel I/O : submitted %u (among %u) for %p\n", idx, ret, active_thread() );
[e46c753]563                }
[426f60c]564                else
565                {
[5dadc9b]566                        // get mutual exclusion
[2fafe7e]567                        #if defined(LEADER_LOCK)
568                                while(!try_lock(ring.submit_q.submit_lock));
569                        #else
570                                lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2);
571                        #endif
[2489d31]572
[426f60c]573                        /* paranoid */ verifyf( ring.submit_q.sqes[ idx ].user_data != 3ul64,
[20ab637]574                        /* paranoid */  "index %u already reclaimed\n"
575                        /* paranoid */  "head %u, prev %u, tail %u\n"
576                        /* paranoid */  "[-0: %u,-1: %u,-2: %u,-3: %u]\n",
577                        /* paranoid */  idx,
578                        /* paranoid */  *ring.submit_q.head, ring.submit_q.prev_head, *tail
579                        /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 0) & (*ring.submit_q.mask) ]
580                        /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 1) & (*ring.submit_q.mask) ]
581                        /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 2) & (*ring.submit_q.mask) ]
582                        /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 3) & (*ring.submit_q.mask) ]
583                        /* paranoid */ );
584
[5dadc9b]585                        // Append to the list of ready entries
586
587                        /* paranoid */ verify( idx <= mask );
[20ab637]588                        ring.submit_q.array[ (*tail) & mask ] = idx;
[5dadc9b]589                        __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST);
[d384787]590
[5dadc9b]591                        // Submit however, many entries need to be submitted
[f00b26d4]592                        int ret = __io_uring_enter( ring, 1, false );
[5dadc9b]593                        if( ret < 0 ) {
594                                switch((int)errno) {
595                                default:
596                                        abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) );
597                                }
598                        }
[d384787]599
[426f60c]600                        /* paranoid */ verify(ret == 1);
601
[5dadc9b]602                        // update statistics
[47746a2]603                        __STATS__( false,
604                                io.submit_q.submit_avg.csm += 1;
605                                io.submit_q.submit_avg.cnt += 1;
606                        )
[5dadc9b]607
[426f60c]608                        {
609                                __attribute__((unused)) volatile __u32 * const head = ring.submit_q.head;
610                                __attribute__((unused)) __u32 last_idx = ring.submit_q.array[ ((*head) - 1) & mask ];
611                                __attribute__((unused)) volatile struct io_uring_sqe * sqe = &ring.submit_q.sqes[last_idx];
612
613                                __cfadbg_print_safe( io,
614                                        "Kernel I/O : last submitted is %u (%p)\n"
615                                        "    data: %p\n"
616                                        "    opcode: %s\n"
617                                        "    fd: %d\n"
618                                        "    flags: %d\n"
619                                        "    prio: %d\n"
620                                        "    off: %p\n"
621                                        "    addr: %p\n"
622                                        "    len: %d\n"
623                                        "    other flags: %d\n"
624                                        "    splice fd: %d\n"
625                                        "    pad[0]: %llu\n"
626                                        "    pad[1]: %llu\n"
627                                        "    pad[2]: %llu\n",
628                                        last_idx, sqe,
629                                        (void*)sqe->user_data,
630                                        opcodes[sqe->opcode],
631                                        sqe->fd,
632                                        sqe->flags,
633                                        sqe->ioprio,
[2fab24e3]634                                        (void*)sqe->off,
635                                        (void*)sqe->addr,
[426f60c]636                                        sqe->len,
637                                        sqe->accept_flags,
638                                        sqe->splice_fd_in,
639                                        sqe->__pad2[0],
640                                        sqe->__pad2[1],
641                                        sqe->__pad2[2]
642                                );
643                        }
644
645                        __atomic_thread_fence( __ATOMIC_SEQ_CST );
[34b61882]646                        // Release the consumed SQEs
[2fab24e3]647
[34b61882]648                        __release_consumed_submission( ring );
[426f60c]649                        // ring.submit_q.sqes[idx].user_data = 3ul64;
[7bfc849]650
[2fafe7e]651                        #if defined(LEADER_LOCK)
652                                next(ring.submit_q.submit_lock);
653                        #else
654                                unlock(ring.submit_q.submit_lock);
655                        #endif
[dd4e2d7]656
[426f60c]657                        __cfadbg_print_safe( io, "Kernel I/O : submitted %u for %p\n", idx, active_thread() );
[5dadc9b]658                }
[2489d31]659        }
[e46c753]660
[d2b5d2d]661        // #define PARTIAL_SUBMIT 32
[426f60c]662
663        // go through the list of submissions in the ready array and moved them into
664        // the ring's submit queue
[e46c753]665        static unsigned __collect_submitions( struct __io_data & ring ) {
666                /* paranoid */ verify( ring.submit_q.ready != 0p );
667                /* paranoid */ verify( ring.submit_q.ready_cnt > 0 );
668
669                unsigned to_submit = 0;
[4998155]670                __u32 tail = *ring.submit_q.tail;
671                const __u32 mask = *ring.submit_q.mask;
[1095ccd]672                #if defined(PARTIAL_SUBMIT)
673                        #if defined(LEADER_LOCK)
674                                #error PARTIAL_SUBMIT and LEADER_LOCK cannot co-exist
675                        #endif
676                        const __u32 cnt = ring.submit_q.ready_cnt > PARTIAL_SUBMIT ? PARTIAL_SUBMIT : ring.submit_q.ready_cnt;
677                        const __u32 offset = ring.submit_q.prev_ready;
678                        ring.submit_q.prev_ready += cnt;
679                #else
680                        const __u32 cnt = ring.submit_q.ready_cnt;
681                        const __u32 offset = 0;
682                #endif
[e46c753]683
684                // Go through the list of ready submissions
[1095ccd]685                for( c; cnt ) {
686                        __u32 i = (offset + c) % ring.submit_q.ready_cnt;
687
[e46c753]688                        // replace any submission with the sentinel, to consume it.
[4998155]689                        __u32 idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED);
[e46c753]690
691                        // If it was already the sentinel, then we are done
692                        if( idx == -1ul32 ) continue;
693
694                        // If we got a real submission, append it to the list
695                        ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask;
696                        to_submit++;
697                }
698
699                // Increment the tail based on how many we are ready to submit
700                __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST);
701
702                return to_submit;
703        }
[34b61882]704
[426f60c]705        // Go through the ring's submit queue and release everything that has already been consumed
706        // by io_uring
[4998155]707        static __u32 __release_consumed_submission( struct __io_data & ring ) {
708                const __u32 smask = *ring.submit_q.mask;
[732b406]709
[426f60c]710                // We need to get the lock to copy the old head and new head
[732b406]711                if( !try_lock(ring.submit_q.release_lock __cfaabi_dbg_ctx2) ) return 0;
[426f60c]712                __attribute__((unused))
713                __u32 ctail = *ring.submit_q.tail;        // get the current tail of the queue
714                __u32 chead = *ring.submit_q.head;              // get the current head of the queue
715                __u32 phead = ring.submit_q.prev_head;  // get the head the last time we were here
716                ring.submit_q.prev_head = chead;                // note up to were we processed
[732b406]717                unlock(ring.submit_q.release_lock);
718
[426f60c]719                // the 3 fields are organized like this diagram
720                // except it's are ring
721                // ---+--------+--------+----
722                // ---+--------+--------+----
723                //    ^        ^        ^
724                // phead    chead    ctail
725
726                // make sure ctail doesn't wrap around and reach phead
727                /* paranoid */ verify(
728                           (ctail >= chead && chead >= phead)
729                        || (chead >= phead && phead >= ctail)
730                        || (phead >= ctail && ctail >= chead)
731                );
732
733                // find the range we need to clear
[4998155]734                __u32 count = chead - phead;
[426f60c]735
736                // We acquired an previous-head/current-head range
737                // go through the range and release the sqes
[34b61882]738                for( i; count ) {
[4998155]739                        __u32 idx = ring.submit_q.array[ (phead + i) & smask ];
[426f60c]740
741                        /* paranoid */ verify( 0 != ring.submit_q.sqes[ idx ].user_data );
[35285fd]742                        __clean( &ring.submit_q.sqes[ idx ] );
[34b61882]743                }
744                return count;
745        }
[35285fd]746
747        void __sqe_clean( volatile struct io_uring_sqe * sqe ) {
748                __clean( sqe );
749        }
750
751        static inline void __clean( volatile struct io_uring_sqe * sqe ) {
752                // If we are in debug mode, thrash the fields to make sure we catch reclamation errors
753                __cfaabi_dbg_debug_do(
754                        memset(sqe, 0xde, sizeof(*sqe));
[ec19b21]755                        sqe->opcode = (sizeof(opcodes) / sizeof(const char *)) - 1;
[35285fd]756                );
757
758                // Mark the entry as unused
759                __atomic_store_n(&sqe->user_data, 3ul64, __ATOMIC_SEQ_CST);
760        }
[47746a2]761#endif
Note: See TracBrowser for help on using the repository browser.