source: libcfa/src/concurrency/io/setup.cfa@ 5baa33c

ADT ast-experimental enum pthread-emulation qualifiedEnum
Last change on this file since 5baa33c was b0d0285, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

kernel now aborts if a ring is closed with unsubmitted entries.

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