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/setup.cfa -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Fri Jul 31 16:25:51 2020 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | #define _GNU_SOURCE |
---|
18 | |
---|
19 | #if defined(__CFA_DEBUG__) |
---|
20 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
21 | // #define __CFA_DEBUG_PRINT_IO_CORE__ |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "io/types.hfa" |
---|
25 | #include "kernel.hfa" |
---|
26 | |
---|
27 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
28 | void ?{}(io_context_params & this) {} |
---|
29 | |
---|
30 | void ?{}($io_context & this, struct cluster & cl) {} |
---|
31 | void ^?{}($io_context & this) {} |
---|
32 | |
---|
33 | void __cfa_io_start( processor * proc ) {} |
---|
34 | bool __cfa_io_flush( processor * proc, bool ) {} |
---|
35 | void __cfa_io_stop ( processor * proc ) {} |
---|
36 | |
---|
37 | $io_arbiter * create(void) { return 0p; } |
---|
38 | void destroy($io_arbiter *) {} |
---|
39 | |
---|
40 | #else |
---|
41 | #include <errno.h> |
---|
42 | #include <stdint.h> |
---|
43 | #include <string.h> |
---|
44 | #include <signal.h> |
---|
45 | #include <unistd.h> |
---|
46 | |
---|
47 | extern "C" { |
---|
48 | #include <pthread.h> |
---|
49 | #include <sys/epoll.h> |
---|
50 | #include <sys/eventfd.h> |
---|
51 | #include <sys/mman.h> |
---|
52 | #include <sys/syscall.h> |
---|
53 | |
---|
54 | #include <linux/io_uring.h> |
---|
55 | } |
---|
56 | |
---|
57 | #include "bitmanip.hfa" |
---|
58 | #include "kernel_private.hfa" |
---|
59 | #include "thread.hfa" |
---|
60 | |
---|
61 | void ?{}(io_context_params & this) { |
---|
62 | this.num_entries = 256; |
---|
63 | } |
---|
64 | |
---|
65 | static void * __io_poller_slow( void * arg ); |
---|
66 | |
---|
67 | // Weirdly, some systems that do support io_uring don't actually define these |
---|
68 | #ifdef __alpha__ |
---|
69 | /* |
---|
70 | * alpha is the only exception, all other architectures |
---|
71 | * have common numbers for new system calls. |
---|
72 | */ |
---|
73 | #ifndef __NR_io_uring_setup |
---|
74 | #define __NR_io_uring_setup 535 |
---|
75 | #endif |
---|
76 | #ifndef __NR_io_uring_enter |
---|
77 | #define __NR_io_uring_enter 536 |
---|
78 | #endif |
---|
79 | #ifndef __NR_io_uring_register |
---|
80 | #define __NR_io_uring_register 537 |
---|
81 | #endif |
---|
82 | #else /* !__alpha__ */ |
---|
83 | #ifndef __NR_io_uring_setup |
---|
84 | #define __NR_io_uring_setup 425 |
---|
85 | #endif |
---|
86 | #ifndef __NR_io_uring_enter |
---|
87 | #define __NR_io_uring_enter 426 |
---|
88 | #endif |
---|
89 | #ifndef __NR_io_uring_register |
---|
90 | #define __NR_io_uring_register 427 |
---|
91 | #endif |
---|
92 | #endif |
---|
93 | |
---|
94 | //============================================================================================= |
---|
95 | // I/O Context Constrution/Destruction |
---|
96 | //============================================================================================= |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | static void __io_uring_setup ( $io_context & this, const io_context_params & params_in, int procfd ); |
---|
101 | static void __io_uring_teardown( $io_context & this ); |
---|
102 | static void __epoll_register($io_context & ctx); |
---|
103 | static void __epoll_unregister($io_context & ctx); |
---|
104 | void __ioarbiter_register( $io_arbiter & mutex, $io_context & ctx ); |
---|
105 | void __ioarbiter_unregister( $io_arbiter & mutex, $io_context & ctx ); |
---|
106 | |
---|
107 | void ?{}($io_context & this, processor * proc, struct cluster & cl) { |
---|
108 | /* paranoid */ verify( cl.io.arbiter ); |
---|
109 | this.proc = proc; |
---|
110 | this.arbiter = cl.io.arbiter; |
---|
111 | this.ext_sq.empty = true; |
---|
112 | (this.ext_sq.queue){}; |
---|
113 | __io_uring_setup( this, cl.io.params, proc->idle_fd ); |
---|
114 | __cfadbg_print_safe(io_core, "Kernel I/O : Created ring for io_context %u (%p)\n", this.fd, &this); |
---|
115 | } |
---|
116 | |
---|
117 | void ^?{}($io_context & this) { |
---|
118 | __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %u\n", this.fd); |
---|
119 | |
---|
120 | __io_uring_teardown( this ); |
---|
121 | __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %u\n", this.fd); |
---|
122 | } |
---|
123 | |
---|
124 | extern void __disable_interrupts_hard(); |
---|
125 | extern void __enable_interrupts_hard(); |
---|
126 | |
---|
127 | static void __io_uring_setup( $io_context & this, const io_context_params & params_in, int procfd ) { |
---|
128 | // Step 1 : call to setup |
---|
129 | struct io_uring_params params; |
---|
130 | memset(¶ms, 0, sizeof(params)); |
---|
131 | // if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL; |
---|
132 | // if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL; |
---|
133 | |
---|
134 | __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256; |
---|
135 | if( !is_pow2(nentries) ) { |
---|
136 | abort("ERROR: I/O setup 'num_entries' must be a power of 2\n"); |
---|
137 | } |
---|
138 | |
---|
139 | int fd = syscall(__NR_io_uring_setup, nentries, ¶ms ); |
---|
140 | if(fd < 0) { |
---|
141 | abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno)); |
---|
142 | } |
---|
143 | |
---|
144 | // Step 2 : mmap result |
---|
145 | struct __sub_ring_t & sq = this.sq; |
---|
146 | struct __cmp_ring_t & cq = this.cq; |
---|
147 | |
---|
148 | // calculate the right ring size |
---|
149 | sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) ); |
---|
150 | cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe)); |
---|
151 | |
---|
152 | // Requires features |
---|
153 | #if defined(IORING_FEAT_SINGLE_MMAP) |
---|
154 | // adjust the size according to the parameters |
---|
155 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { |
---|
156 | cq.ring_sz = sq.ring_sz = max(cq.ring_sz, sq.ring_sz); |
---|
157 | } |
---|
158 | #endif |
---|
159 | |
---|
160 | // mmap the Submit Queue into existence |
---|
161 | sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); |
---|
162 | if (sq.ring_ptr == (void*)MAP_FAILED) { |
---|
163 | abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno)); |
---|
164 | } |
---|
165 | |
---|
166 | // Requires features |
---|
167 | #if defined(IORING_FEAT_SINGLE_MMAP) |
---|
168 | // mmap the Completion Queue into existence (may or may not be needed) |
---|
169 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { |
---|
170 | cq.ring_ptr = sq.ring_ptr; |
---|
171 | } |
---|
172 | else |
---|
173 | #endif |
---|
174 | { |
---|
175 | // We need multiple call to MMAP |
---|
176 | cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); |
---|
177 | if (cq.ring_ptr == (void*)MAP_FAILED) { |
---|
178 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
179 | abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno)); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | // mmap the submit queue entries |
---|
184 | size_t size = params.sq_entries * sizeof(struct io_uring_sqe); |
---|
185 | sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES); |
---|
186 | if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) { |
---|
187 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
188 | if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz); |
---|
189 | abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno)); |
---|
190 | } |
---|
191 | |
---|
192 | // Step 3 : Initialize the data structure |
---|
193 | // Get the pointers from the kernel to fill the structure |
---|
194 | // submit queue |
---|
195 | sq.kring.head = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head); |
---|
196 | sq.kring.tail = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail); |
---|
197 | sq.kring.array = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array); |
---|
198 | sq.mask = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask); |
---|
199 | sq.num = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries); |
---|
200 | sq.flags = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags); |
---|
201 | sq.dropped = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped); |
---|
202 | |
---|
203 | sq.kring.released = 0; |
---|
204 | |
---|
205 | sq.free_ring.head = 0; |
---|
206 | sq.free_ring.tail = *sq.num; |
---|
207 | sq.free_ring.array = alloc( *sq.num, 128`align ); |
---|
208 | for(i; (__u32)*sq.num) { |
---|
209 | sq.free_ring.array[i] = i; |
---|
210 | } |
---|
211 | |
---|
212 | sq.to_submit = 0; |
---|
213 | |
---|
214 | // completion queue |
---|
215 | cq.head = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.head); |
---|
216 | cq.tail = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail); |
---|
217 | cq.mask = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask); |
---|
218 | cq.num = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries); |
---|
219 | cq.overflow = ( __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow); |
---|
220 | cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes); |
---|
221 | |
---|
222 | #if !defined(IO_URING_IDLE) |
---|
223 | // Step 4 : eventfd |
---|
224 | // io_uring_register is so f*cking slow on some machine that it |
---|
225 | // will never succeed if preemption isn't hard blocked |
---|
226 | __cfadbg_print_safe(io_core, "Kernel I/O : registering %d for completion with ring %d\n", procfd, fd); |
---|
227 | |
---|
228 | __disable_interrupts_hard(); |
---|
229 | |
---|
230 | int ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &procfd, 1); |
---|
231 | if (ret < 0) { |
---|
232 | abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno)); |
---|
233 | } |
---|
234 | |
---|
235 | __enable_interrupts_hard(); |
---|
236 | |
---|
237 | __cfadbg_print_safe(io_core, "Kernel I/O : registered %d for completion with ring %d\n", procfd, fd); |
---|
238 | #endif |
---|
239 | |
---|
240 | // some paranoid checks |
---|
241 | /* paranoid */ verifyf( (*cq.mask) == ((*cq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*cq.num) - 1ul32, *cq.num, *cq.mask ); |
---|
242 | /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num ); |
---|
243 | /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head ); |
---|
244 | /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail ); |
---|
245 | |
---|
246 | /* paranoid */ verifyf( (*sq.mask) == ((*sq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*sq.num) - 1ul32, *sq.num, *sq.mask ); |
---|
247 | /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num ); |
---|
248 | /* paranoid */ verifyf( (*sq.kring.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.kring.head ); |
---|
249 | /* paranoid */ verifyf( (*sq.kring.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.kring.tail ); |
---|
250 | |
---|
251 | // Update the global ring info |
---|
252 | this.ring_flags = 0; |
---|
253 | this.fd = fd; |
---|
254 | } |
---|
255 | |
---|
256 | static void __io_uring_teardown( $io_context & this ) { |
---|
257 | // Shutdown the io rings |
---|
258 | struct __sub_ring_t & sq = this.sq; |
---|
259 | struct __cmp_ring_t & cq = this.cq; |
---|
260 | |
---|
261 | // unmap the submit queue entries |
---|
262 | munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe)); |
---|
263 | |
---|
264 | // unmap the Submit Queue ring |
---|
265 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
266 | |
---|
267 | // unmap the Completion Queue ring, if it is different |
---|
268 | if (cq.ring_ptr != sq.ring_ptr) { |
---|
269 | munmap(cq.ring_ptr, cq.ring_sz); |
---|
270 | } |
---|
271 | |
---|
272 | // close the file descriptor |
---|
273 | close(this.fd); |
---|
274 | |
---|
275 | free( this.sq.free_ring.array ); // Maybe null, doesn't matter |
---|
276 | } |
---|
277 | |
---|
278 | void __cfa_io_start( processor * proc ) { |
---|
279 | proc->io.ctx = alloc(); |
---|
280 | (*proc->io.ctx){proc, *proc->cltr}; |
---|
281 | } |
---|
282 | void __cfa_io_stop ( processor * proc ) { |
---|
283 | ^(*proc->io.ctx){}; |
---|
284 | free(proc->io.ctx); |
---|
285 | } |
---|
286 | |
---|
287 | //============================================================================================= |
---|
288 | // I/O Context Sleep |
---|
289 | //============================================================================================= |
---|
290 | // static inline void __epoll_ctl($io_context & ctx, int op, const char * error) { |
---|
291 | // struct epoll_event ev; |
---|
292 | // ev.events = EPOLLIN | EPOLLONESHOT; |
---|
293 | // ev.data.u64 = (__u64)&ctx; |
---|
294 | // int ret = epoll_ctl(iopoll.epollfd, op, ctx.efd, &ev); |
---|
295 | // if (ret < 0) { |
---|
296 | // abort( "KERNEL ERROR: EPOLL %s - (%d) %s\n", error, (int)errno, strerror(errno) ); |
---|
297 | // } |
---|
298 | // } |
---|
299 | |
---|
300 | // static void __epoll_register($io_context & ctx) { |
---|
301 | // __epoll_ctl(ctx, EPOLL_CTL_ADD, "ADD"); |
---|
302 | // } |
---|
303 | |
---|
304 | // static void __epoll_unregister($io_context & ctx) { |
---|
305 | // // Read the current epoch so we know when to stop |
---|
306 | // size_t curr = __atomic_load_n(&iopoll.epoch, __ATOMIC_SEQ_CST); |
---|
307 | |
---|
308 | // // Remove the fd from the iopoller |
---|
309 | // __epoll_ctl(ctx, EPOLL_CTL_DEL, "REMOVE"); |
---|
310 | |
---|
311 | // // Notify the io poller thread of the shutdown |
---|
312 | // iopoll.run = false; |
---|
313 | // sigval val = { 1 }; |
---|
314 | // pthread_sigqueue( iopoll.thrd, SIGUSR1, val ); |
---|
315 | |
---|
316 | // // Make sure all this is done |
---|
317 | // __atomic_thread_fence(__ATOMIC_SEQ_CST); |
---|
318 | |
---|
319 | // // Wait for the next epoch |
---|
320 | // while(curr == iopoll.epoch && !iopoll.stopped) Pause(); |
---|
321 | // } |
---|
322 | |
---|
323 | // void __ioctx_prepare_block($io_context & ctx) { |
---|
324 | // __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Re-arming io poller %d (%p)\n", ctx.fd, &ctx); |
---|
325 | // __epoll_ctl(ctx, EPOLL_CTL_MOD, "REARM"); |
---|
326 | // } |
---|
327 | |
---|
328 | |
---|
329 | //============================================================================================= |
---|
330 | // I/O Context Misc Setup |
---|
331 | //============================================================================================= |
---|
332 | void ?{}( $io_arbiter & this ) { |
---|
333 | this.pending.empty = true; |
---|
334 | } |
---|
335 | |
---|
336 | void ^?{}( $io_arbiter & this ) {} |
---|
337 | |
---|
338 | $io_arbiter * create(void) { |
---|
339 | return new(); |
---|
340 | } |
---|
341 | void destroy($io_arbiter * arbiter) { |
---|
342 | delete(arbiter); |
---|
343 | } |
---|
344 | |
---|
345 | //============================================================================================= |
---|
346 | // I/O Context Misc Setup |
---|
347 | //============================================================================================= |
---|
348 | |
---|
349 | #endif |
---|