1 |
root |
1.1 |
/* |
2 |
|
|
* libev linux io_uring fd activity backend |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2019 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without modifica- |
8 |
|
|
* tion, are permitted provided that the following conditions are met: |
9 |
|
|
* |
10 |
|
|
* 1. Redistributions of source code must retain the above copyright notice, |
11 |
|
|
* this list of conditions and the following disclaimer. |
12 |
|
|
* |
13 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
|
* documentation and/or other materials provided with the distribution. |
16 |
|
|
* |
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 |
|
|
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 |
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 |
|
|
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
25 |
|
|
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 |
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
|
|
* |
28 |
|
|
* Alternatively, the contents of this file may be used under the terms of |
29 |
|
|
* the GNU General Public License ("GPL") version 2 or any later version, |
30 |
|
|
* in which case the provisions of the GPL are applicable instead of |
31 |
|
|
* the above. If you wish to allow the use of your version of this file |
32 |
|
|
* only under the terms of the GPL and not to allow others to use your |
33 |
|
|
* version of this file under the BSD license, indicate your decision |
34 |
|
|
* by deleting the provisions above and replace them with the notice |
35 |
|
|
* and other provisions required by the GPL. If you do not delete the |
36 |
|
|
* provisions above, a recipient may use your version of this file under |
37 |
|
|
* either the BSD or the GPL. |
38 |
|
|
*/ |
39 |
|
|
|
40 |
|
|
/* |
41 |
|
|
* general notes about linux io_uring: |
42 |
|
|
* |
43 |
|
|
* a) it's the best interface I have seen so far. on linux. |
44 |
|
|
* b) best is not necessarily very good. |
45 |
|
|
* c) it's better than the aio mess, doesn't suffer from the fork problems |
46 |
|
|
* of linux aio or epoll and so on and so on. and you could do event stuff |
47 |
|
|
* without any syscalls. what's not to like? |
48 |
|
|
* d) ok, it's vastly more complex, but that's ok, really. |
49 |
|
|
* e) why 3 mmaps instead of one? one would be more space-efficient, |
50 |
|
|
* and I can't see what benefit three would have (other than being |
51 |
|
|
* somehow resizable/relocatable, but that's apparently not possible). |
52 |
root |
1.7 |
* (FIXME: newer kernels can use 2 mmaps only, need to look into this). |
53 |
root |
1.1 |
* f) hmm, it's practiclaly undebuggable (gdb can't access the memory, and |
54 |
root |
1.7 |
* the bizarre way structure offsets are communicated makes it hard to |
55 |
root |
1.1 |
* just print the ring buffer heads, even *iff* the memory were visible |
56 |
|
|
* in gdb. but then, that's also ok, really. |
57 |
|
|
* g) well, you cannot specify a timeout when waiting for events. no, |
58 |
|
|
* seriously, the interface doesn't support a timeout. never seen _that_ |
59 |
|
|
* before. sure, you can use a timerfd, but that's another syscall |
60 |
|
|
* you could have avoided. overall, this bizarre omission smells |
61 |
|
|
* like a ยต-optimisation by the io_uring author for his personal |
62 |
|
|
* applications, to the detriment of everybody else who just wants |
63 |
|
|
* an event loop. but, umm, ok, if that's all, it could be worse. |
64 |
root |
1.7 |
* (FIXME: jens mentioned timeout commands, need to investigate) |
65 |
root |
1.1 |
* h) there is a hardcoded limit of 4096 outstanding events. okay, |
66 |
|
|
* at least there is no arbitrary low system-wide limit... |
67 |
root |
1.7 |
* (FIXME: apparently, this was increased to 32768 in later kernels( |
68 |
root |
1.1 |
* i) unlike linux aio, you *can* register more then the limit |
69 |
|
|
* of fd events, and the kernel will "gracefully" signal an |
70 |
|
|
* overflow, after which you could destroy and recreate the kernel |
71 |
|
|
* state, a bit bigger, or fall back to e.g. poll. thats not |
72 |
|
|
* totally insane, but kind of questions the point a high |
73 |
|
|
* performance I/O framework when it doesn't really work |
74 |
|
|
* under stress. |
75 |
root |
1.7 |
* (FIXME: iouring should no longer drop events, need to investigate) |
76 |
root |
1.1 |
* j) but, oh my! is has exactly the same bugs as the linux aio backend, |
77 |
|
|
* where some undocumented poll combinations just fail. |
78 |
|
|
* so we need epoll AGAIN as a fallback. AGAIN! epoll!! and of course, |
79 |
|
|
* this is completely undocumented, have I mantioned this already? |
80 |
|
|
* k) overall, the *API* itself is, I dare to say, not a total trainwreck. |
81 |
|
|
* the big isuess with it are the bugs requiring epoll, which might |
82 |
|
|
* or might not get fixed (do I hold my breath?). |
83 |
|
|
*/ |
84 |
|
|
|
85 |
root |
1.9 |
/* TODO: use internal TIMEOUT */ |
86 |
|
|
/* TODO: take advantage of single mmap, NODROP etc. */ |
87 |
|
|
/* TODO: resize cq/sq size independently */ |
88 |
|
|
|
89 |
root |
1.1 |
#include <sys/timerfd.h> |
90 |
|
|
#include <sys/mman.h> |
91 |
|
|
#include <poll.h> |
92 |
root |
1.12 |
#include <stdint.h> |
93 |
root |
1.1 |
|
94 |
|
|
#define IOURING_INIT_ENTRIES 32 |
95 |
|
|
|
96 |
|
|
/*****************************************************************************/ |
97 |
|
|
/* syscall wrapdadoop - this section has the raw api/abi definitions */ |
98 |
|
|
|
99 |
|
|
#include <linux/fs.h> |
100 |
|
|
#include <linux/types.h> |
101 |
|
|
|
102 |
|
|
/* mostly directly taken from the kernel or documentation */ |
103 |
|
|
|
104 |
|
|
struct io_uring_sqe |
105 |
|
|
{ |
106 |
|
|
__u8 opcode; |
107 |
|
|
__u8 flags; |
108 |
|
|
__u16 ioprio; |
109 |
|
|
__s32 fd; |
110 |
root |
1.8 |
union { |
111 |
|
|
__u64 off; |
112 |
|
|
__u64 addr2; |
113 |
|
|
}; |
114 |
root |
1.1 |
__u64 addr; |
115 |
|
|
__u32 len; |
116 |
|
|
union { |
117 |
|
|
__kernel_rwf_t rw_flags; |
118 |
|
|
__u32 fsync_flags; |
119 |
|
|
__u16 poll_events; |
120 |
|
|
__u32 sync_range_flags; |
121 |
|
|
__u32 msg_flags; |
122 |
root |
1.8 |
__u32 timeout_flags; |
123 |
|
|
__u32 accept_flags; |
124 |
|
|
__u32 cancel_flags; |
125 |
|
|
__u32 open_flags; |
126 |
|
|
__u32 statx_flags; |
127 |
root |
1.1 |
}; |
128 |
|
|
__u64 user_data; |
129 |
|
|
union { |
130 |
|
|
__u16 buf_index; |
131 |
|
|
__u64 __pad2[3]; |
132 |
|
|
}; |
133 |
|
|
}; |
134 |
|
|
|
135 |
|
|
struct io_uring_cqe |
136 |
|
|
{ |
137 |
|
|
__u64 user_data; |
138 |
|
|
__s32 res; |
139 |
|
|
__u32 flags; |
140 |
|
|
}; |
141 |
|
|
|
142 |
|
|
struct io_sqring_offsets |
143 |
|
|
{ |
144 |
|
|
__u32 head; |
145 |
|
|
__u32 tail; |
146 |
|
|
__u32 ring_mask; |
147 |
|
|
__u32 ring_entries; |
148 |
|
|
__u32 flags; |
149 |
|
|
__u32 dropped; |
150 |
|
|
__u32 array; |
151 |
|
|
__u32 resv1; |
152 |
|
|
__u64 resv2; |
153 |
|
|
}; |
154 |
|
|
|
155 |
|
|
struct io_cqring_offsets |
156 |
|
|
{ |
157 |
|
|
__u32 head; |
158 |
|
|
__u32 tail; |
159 |
|
|
__u32 ring_mask; |
160 |
|
|
__u32 ring_entries; |
161 |
|
|
__u32 overflow; |
162 |
|
|
__u32 cqes; |
163 |
|
|
__u64 resv[2]; |
164 |
|
|
}; |
165 |
|
|
|
166 |
|
|
struct io_uring_params |
167 |
|
|
{ |
168 |
|
|
__u32 sq_entries; |
169 |
|
|
__u32 cq_entries; |
170 |
|
|
__u32 flags; |
171 |
|
|
__u32 sq_thread_cpu; |
172 |
|
|
__u32 sq_thread_idle; |
173 |
root |
1.8 |
__u32 features; |
174 |
|
|
__u32 resv[4]; |
175 |
root |
1.1 |
struct io_sqring_offsets sq_off; |
176 |
|
|
struct io_cqring_offsets cq_off; |
177 |
|
|
}; |
178 |
|
|
|
179 |
root |
1.12 |
#define IORING_SETUP_CQSIZE 0x00000008 |
180 |
|
|
|
181 |
|
|
#define IORING_OP_POLL_ADD 6 |
182 |
|
|
#define IORING_OP_POLL_REMOVE 7 |
183 |
|
|
#define IORING_OP_TIMEOUT 11 |
184 |
|
|
#define IORING_OP_TIMEOUT_REMOVE 12 |
185 |
|
|
|
186 |
|
|
/* relative or absolute, reference clock is CLOCK_MONOTONIC */ |
187 |
|
|
struct iouring_kernel_timespec |
188 |
|
|
{ |
189 |
|
|
int64_t tv_sec; |
190 |
|
|
long long tv_nsec; |
191 |
|
|
}; |
192 |
|
|
|
193 |
|
|
#define IORING_TIMEOUT_ABS 0x00000001 |
194 |
root |
1.1 |
|
195 |
|
|
#define IORING_ENTER_GETEVENTS 0x01 |
196 |
|
|
|
197 |
|
|
#define IORING_OFF_SQ_RING 0x00000000ULL |
198 |
|
|
#define IORING_OFF_CQ_RING 0x08000000ULL |
199 |
|
|
#define IORING_OFF_SQES 0x10000000ULL |
200 |
|
|
|
201 |
root |
1.12 |
#define IORING_FEAT_SINGLE_MMAP 0x00000001 |
202 |
|
|
#define IORING_FEAT_NODROP 0x00000002 |
203 |
|
|
#define IORING_FEAT_SUBMIT_STABLE 0x00000004 |
204 |
root |
1.8 |
|
205 |
root |
1.1 |
inline_size |
206 |
|
|
int |
207 |
|
|
evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) |
208 |
|
|
{ |
209 |
|
|
return ev_syscall2 (SYS_io_uring_setup, entries, params); |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
inline_size |
213 |
|
|
int |
214 |
|
|
evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz) |
215 |
|
|
{ |
216 |
|
|
return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
/*****************************************************************************/ |
220 |
|
|
/* actual backed implementation */ |
221 |
|
|
|
222 |
|
|
/* we hope that volatile will make the compiler access this variables only once */ |
223 |
|
|
#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_sq_ring + iouring_sq_ ## name) |
224 |
|
|
#define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_cq_ring + iouring_cq_ ## name) |
225 |
|
|
|
226 |
|
|
/* the index array */ |
227 |
|
|
#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_sq_ring + iouring_sq_array)) |
228 |
|
|
|
229 |
|
|
/* the submit/completion queue entries */ |
230 |
|
|
#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
231 |
|
|
#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) |
232 |
|
|
|
233 |
|
|
static |
234 |
|
|
struct io_uring_sqe * |
235 |
|
|
iouring_sqe_get (EV_P) |
236 |
|
|
{ |
237 |
|
|
unsigned tail = EV_SQ_VAR (tail); |
238 |
|
|
|
239 |
|
|
if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) |
240 |
|
|
{ |
241 |
|
|
/* queue full, flush */ |
242 |
|
|
evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0); |
243 |
|
|
iouring_to_submit = 0; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); |
247 |
|
|
|
248 |
|
|
return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
inline_size |
252 |
|
|
struct io_uring_sqe * |
253 |
|
|
iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe) |
254 |
|
|
{ |
255 |
|
|
unsigned idx = sqe - EV_SQES; |
256 |
|
|
|
257 |
|
|
EV_SQ_ARRAY [idx] = idx; |
258 |
|
|
ECB_MEMORY_FENCE_RELEASE; |
259 |
|
|
++EV_SQ_VAR (tail); |
260 |
|
|
/*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */ |
261 |
|
|
++iouring_to_submit; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
/*****************************************************************************/ |
265 |
|
|
|
266 |
|
|
/* when the timerfd expires we simply note the fact, |
267 |
|
|
* as the purpose of the timerfd is to wake us up, nothing else. |
268 |
|
|
* the next iteration should re-set it. |
269 |
|
|
*/ |
270 |
|
|
static void |
271 |
|
|
iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) |
272 |
|
|
{ |
273 |
|
|
iouring_tfd_to = EV_TSTAMP_HUGE; |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
/* called for full and partial cleanup */ |
277 |
|
|
ecb_cold |
278 |
|
|
static int |
279 |
|
|
iouring_internal_destroy (EV_P) |
280 |
|
|
{ |
281 |
|
|
close (iouring_tfd); |
282 |
|
|
close (iouring_fd); |
283 |
|
|
|
284 |
|
|
if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); |
285 |
|
|
if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size); |
286 |
|
|
if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); |
287 |
|
|
|
288 |
root |
1.8 |
if (ev_is_active (&iouring_tfd_w)) |
289 |
|
|
{ |
290 |
|
|
ev_ref (EV_A); |
291 |
|
|
ev_io_stop (EV_A_ &iouring_tfd_w); |
292 |
|
|
} |
293 |
root |
1.1 |
} |
294 |
|
|
|
295 |
|
|
ecb_cold |
296 |
|
|
static int |
297 |
|
|
iouring_internal_init (EV_P) |
298 |
|
|
{ |
299 |
|
|
struct io_uring_params params = { 0 }; |
300 |
|
|
|
301 |
|
|
iouring_to_submit = 0; |
302 |
|
|
|
303 |
|
|
iouring_tfd = -1; |
304 |
|
|
iouring_sq_ring = MAP_FAILED; |
305 |
|
|
iouring_cq_ring = MAP_FAILED; |
306 |
|
|
iouring_sqes = MAP_FAILED; |
307 |
|
|
|
308 |
root |
1.12 |
if (!have_monotonic) /* cannot really happen, but what if11 */ |
309 |
|
|
return -1; |
310 |
|
|
|
311 |
root |
1.1 |
for (;;) |
312 |
|
|
{ |
313 |
|
|
iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
314 |
|
|
|
315 |
|
|
if (iouring_fd >= 0) |
316 |
|
|
break; /* yippie */ |
317 |
|
|
|
318 |
|
|
if (errno != EINVAL) |
319 |
|
|
return -1; /* we failed */ |
320 |
|
|
|
321 |
root |
1.11 |
#if TODO |
322 |
|
|
if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP)) |
323 |
|
|
return -1; /* we require the above features */ |
324 |
|
|
#endif |
325 |
|
|
|
326 |
root |
1.1 |
/* EINVAL: lots of possible reasons, but maybe |
327 |
|
|
* it is because we hit the unqueryable hardcoded size limit |
328 |
|
|
*/ |
329 |
|
|
|
330 |
|
|
/* we hit the limit already, give up */ |
331 |
|
|
if (iouring_max_entries) |
332 |
|
|
return -1; |
333 |
|
|
|
334 |
|
|
/* first time we hit EINVAL? assume we hit the limit, so go back and retry */ |
335 |
|
|
iouring_entries >>= 1; |
336 |
|
|
iouring_max_entries = iouring_entries; |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
iouring_sq_ring_size = params.sq_off.array + params.sq_entries * sizeof (unsigned); |
340 |
|
|
iouring_cq_ring_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe); |
341 |
|
|
iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe); |
342 |
|
|
|
343 |
|
|
iouring_sq_ring = mmap (0, iouring_sq_ring_size, PROT_READ | PROT_WRITE, |
344 |
|
|
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING); |
345 |
|
|
iouring_cq_ring = mmap (0, iouring_cq_ring_size, PROT_READ | PROT_WRITE, |
346 |
|
|
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_CQ_RING); |
347 |
|
|
iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE, |
348 |
|
|
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES); |
349 |
|
|
|
350 |
|
|
if (iouring_sq_ring == MAP_FAILED || iouring_cq_ring == MAP_FAILED || iouring_sqes == MAP_FAILED) |
351 |
|
|
return -1; |
352 |
|
|
|
353 |
|
|
iouring_sq_head = params.sq_off.head; |
354 |
|
|
iouring_sq_tail = params.sq_off.tail; |
355 |
|
|
iouring_sq_ring_mask = params.sq_off.ring_mask; |
356 |
|
|
iouring_sq_ring_entries = params.sq_off.ring_entries; |
357 |
|
|
iouring_sq_flags = params.sq_off.flags; |
358 |
|
|
iouring_sq_dropped = params.sq_off.dropped; |
359 |
|
|
iouring_sq_array = params.sq_off.array; |
360 |
|
|
|
361 |
|
|
iouring_cq_head = params.cq_off.head; |
362 |
|
|
iouring_cq_tail = params.cq_off.tail; |
363 |
|
|
iouring_cq_ring_mask = params.cq_off.ring_mask; |
364 |
|
|
iouring_cq_ring_entries = params.cq_off.ring_entries; |
365 |
|
|
iouring_cq_overflow = params.cq_off.overflow; |
366 |
|
|
iouring_cq_cqes = params.cq_off.cqes; |
367 |
|
|
|
368 |
|
|
iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC); |
369 |
|
|
|
370 |
|
|
if (iouring_tfd < 0) |
371 |
|
|
return iouring_tfd; |
372 |
|
|
|
373 |
|
|
iouring_tfd_to = EV_TSTAMP_HUGE; |
374 |
|
|
|
375 |
|
|
return 0; |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
ecb_cold |
379 |
|
|
static void |
380 |
|
|
iouring_fork (EV_P) |
381 |
|
|
{ |
382 |
|
|
iouring_internal_destroy (EV_A); |
383 |
|
|
|
384 |
|
|
while (iouring_internal_init (EV_A) < 0) |
385 |
|
|
ev_syserr ("(libev) io_uring_setup"); |
386 |
|
|
|
387 |
root |
1.8 |
fd_rearm_all (EV_A); |
388 |
root |
1.1 |
|
389 |
|
|
ev_io_stop (EV_A_ &iouring_tfd_w); |
390 |
|
|
ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); |
391 |
|
|
ev_io_start (EV_A_ &iouring_tfd_w); |
392 |
|
|
} |
393 |
|
|
|
394 |
|
|
/*****************************************************************************/ |
395 |
|
|
|
396 |
|
|
static void |
397 |
|
|
iouring_modify (EV_P_ int fd, int oev, int nev) |
398 |
|
|
{ |
399 |
|
|
if (oev) |
400 |
|
|
{ |
401 |
|
|
/* we assume the sqe's are all "properly" initialised */ |
402 |
|
|
struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
403 |
|
|
sqe->opcode = IORING_OP_POLL_REMOVE; |
404 |
|
|
sqe->fd = fd; |
405 |
root |
1.13 |
/* Jens Axboe notified me that user_data is not what is documented, but is |
406 |
|
|
* some kind of unique ID that has to match, otherwise the request cannot |
407 |
|
|
* be removed. Since we don't *really* have that, we pass in the old |
408 |
|
|
* generation counter - if that fails, too bad, it will hopefully be removed |
409 |
|
|
* at close time and then be ignored. */ |
410 |
|
|
sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
411 |
root |
1.1 |
iouring_sqe_submit (EV_A_ sqe); |
412 |
root |
1.2 |
|
413 |
|
|
/* increment generation counter to avoid handling old events */ |
414 |
|
|
++anfds [fd].egen; |
415 |
root |
1.1 |
} |
416 |
|
|
|
417 |
|
|
if (nev) |
418 |
|
|
{ |
419 |
|
|
struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
420 |
|
|
sqe->opcode = IORING_OP_POLL_ADD; |
421 |
|
|
sqe->fd = fd; |
422 |
root |
1.2 |
sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
423 |
root |
1.1 |
sqe->poll_events = |
424 |
|
|
(nev & EV_READ ? POLLIN : 0) |
425 |
|
|
| (nev & EV_WRITE ? POLLOUT : 0); |
426 |
|
|
iouring_sqe_submit (EV_A_ sqe); |
427 |
|
|
} |
428 |
|
|
} |
429 |
|
|
|
430 |
|
|
inline_size |
431 |
|
|
void |
432 |
|
|
iouring_tfd_update (EV_P_ ev_tstamp timeout) |
433 |
|
|
{ |
434 |
|
|
ev_tstamp tfd_to = mn_now + timeout; |
435 |
|
|
|
436 |
|
|
/* we assume there will be many iterations per timer change, so |
437 |
|
|
* we only re-set the timerfd when we have to because its expiry |
438 |
|
|
* is too late. |
439 |
|
|
*/ |
440 |
|
|
if (ecb_expect_false (tfd_to < iouring_tfd_to)) |
441 |
|
|
{ |
442 |
|
|
struct itimerspec its; |
443 |
|
|
|
444 |
|
|
iouring_tfd_to = tfd_to; |
445 |
|
|
EV_TS_SET (its.it_interval, 0.); |
446 |
|
|
EV_TS_SET (its.it_value, tfd_to); |
447 |
|
|
|
448 |
|
|
if (timerfd_settime (iouring_tfd, TFD_TIMER_ABSTIME, &its, 0) < 0) |
449 |
|
|
assert (("libev: iouring timerfd_settime failed", 0)); |
450 |
|
|
} |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
inline_size |
454 |
|
|
void |
455 |
|
|
iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe) |
456 |
|
|
{ |
457 |
|
|
int fd = cqe->user_data & 0xffffffffU; |
458 |
|
|
uint32_t gen = cqe->user_data >> 32; |
459 |
|
|
int res = cqe->res; |
460 |
|
|
|
461 |
|
|
assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
462 |
|
|
|
463 |
|
|
/* documentation lies, of course. the result value is NOT like |
464 |
|
|
* normal syscalls, but like linux raw syscalls, i.e. negative |
465 |
|
|
* error numbers. fortunate, as otherwise there would be no way |
466 |
|
|
* to get error codes at all. still, why not document this? |
467 |
|
|
*/ |
468 |
|
|
|
469 |
|
|
/* ignore event if generation doesn't match */ |
470 |
root |
1.13 |
/* other than skipping removal events, */ |
471 |
root |
1.1 |
/* this should actually be very rare */ |
472 |
root |
1.2 |
if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
473 |
root |
1.1 |
return; |
474 |
|
|
|
475 |
|
|
if (ecb_expect_false (res < 0)) |
476 |
|
|
{ |
477 |
root |
1.11 |
/*TODO: EINVAL handling (was something failed with this fd)*/ |
478 |
|
|
/*TODO: EBUSY happens when?*/ |
479 |
root |
1.1 |
|
480 |
root |
1.8 |
if (res == -EBADF) |
481 |
root |
1.1 |
{ |
482 |
|
|
assert (("libev: event loop rejected bad fd", res != -EBADF)); |
483 |
|
|
fd_kill (EV_A_ fd); |
484 |
|
|
} |
485 |
|
|
else |
486 |
|
|
{ |
487 |
|
|
errno = -res; |
488 |
|
|
ev_syserr ("(libev) IORING_OP_POLL_ADD"); |
489 |
|
|
} |
490 |
|
|
|
491 |
|
|
return; |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
/* feed events, we do not expect or handle POLLNVAL */ |
495 |
|
|
fd_event ( |
496 |
|
|
EV_A_ |
497 |
|
|
fd, |
498 |
|
|
(res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
499 |
|
|
| (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
500 |
|
|
); |
501 |
|
|
|
502 |
|
|
/* io_uring is oneshot, so we need to re-arm the fd next iteration */ |
503 |
|
|
/* this also means we usually have to do at least one syscall per iteration */ |
504 |
|
|
anfds [fd].events = 0; |
505 |
|
|
fd_change (EV_A_ fd, EV_ANFD_REIFY); |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
/* called when the event queue overflows */ |
509 |
|
|
ecb_cold |
510 |
|
|
static void |
511 |
|
|
iouring_overflow (EV_P) |
512 |
|
|
{ |
513 |
|
|
/* we have two options, resize the queue (by tearing down |
514 |
|
|
* everything and recreating it, or living with it |
515 |
|
|
* and polling. |
516 |
root |
1.10 |
* we implement this by resizing the queue, and, if that fails, |
517 |
root |
1.1 |
* we just recreate the state on every failure, which |
518 |
|
|
* kind of is a very inefficient poll. |
519 |
|
|
* one danger is, due to the bios toward lower fds, |
520 |
|
|
* we will only really get events for those, so |
521 |
|
|
* maybe we need a poll() fallback, after all. |
522 |
|
|
*/ |
523 |
|
|
/*EV_CQ_VAR (overflow) = 0;*/ /* need to do this if we keep the state and poll manually */ |
524 |
|
|
|
525 |
|
|
fd_rearm_all (EV_A); |
526 |
|
|
|
527 |
|
|
/* we double the size until we hit the hard-to-probe maximum */ |
528 |
|
|
if (!iouring_max_entries) |
529 |
|
|
{ |
530 |
|
|
iouring_entries <<= 1; |
531 |
|
|
iouring_fork (EV_A); |
532 |
|
|
} |
533 |
|
|
else |
534 |
|
|
{ |
535 |
|
|
/* we hit the kernel limit, we should fall back to something else. |
536 |
|
|
* we can either poll() a few times and hope for the best, |
537 |
|
|
* poll always, or switch to epoll. |
538 |
root |
1.10 |
* TODO: is this necessary with newer kernels? |
539 |
root |
1.1 |
*/ |
540 |
|
|
|
541 |
|
|
iouring_internal_destroy (EV_A); |
542 |
|
|
|
543 |
root |
1.10 |
/* this should make it so that on return, we don't call any uring functions */ |
544 |
root |
1.1 |
iouring_to_submit = 0; |
545 |
|
|
|
546 |
|
|
for (;;) |
547 |
|
|
{ |
548 |
|
|
backend = epoll_init (EV_A_ 0); |
549 |
|
|
|
550 |
|
|
if (backend) |
551 |
|
|
break; |
552 |
|
|
|
553 |
|
|
ev_syserr ("(libev) iouring switch to epoll"); |
554 |
|
|
} |
555 |
|
|
} |
556 |
|
|
} |
557 |
|
|
|
558 |
|
|
/* handle any events in the completion queue, return true if there were any */ |
559 |
|
|
static int |
560 |
|
|
iouring_handle_cq (EV_P) |
561 |
|
|
{ |
562 |
|
|
unsigned head, tail, mask; |
563 |
|
|
|
564 |
|
|
head = EV_CQ_VAR (head); |
565 |
|
|
ECB_MEMORY_FENCE_ACQUIRE; |
566 |
|
|
tail = EV_CQ_VAR (tail); |
567 |
|
|
|
568 |
|
|
if (head == tail) |
569 |
|
|
return 0; |
570 |
|
|
|
571 |
|
|
/* it can only overflow if we have events, yes, yes? */ |
572 |
|
|
if (ecb_expect_false (EV_CQ_VAR (overflow))) |
573 |
|
|
{ |
574 |
|
|
iouring_overflow (EV_A); |
575 |
|
|
return 1; |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
mask = EV_CQ_VAR (ring_mask); |
579 |
|
|
|
580 |
|
|
do |
581 |
|
|
iouring_process_cqe (EV_A_ &EV_CQES [head++ & mask]); |
582 |
|
|
while (head != tail); |
583 |
|
|
|
584 |
|
|
EV_CQ_VAR (head) = head; |
585 |
|
|
ECB_MEMORY_FENCE_RELEASE; |
586 |
|
|
|
587 |
|
|
return 1; |
588 |
|
|
} |
589 |
|
|
|
590 |
|
|
static void |
591 |
|
|
iouring_poll (EV_P_ ev_tstamp timeout) |
592 |
|
|
{ |
593 |
|
|
/* if we have events, no need for extra syscalls, but we might have to queue events */ |
594 |
|
|
if (iouring_handle_cq (EV_A)) |
595 |
root |
1.3 |
timeout = EV_TS_CONST (0.); |
596 |
root |
1.1 |
else |
597 |
|
|
/* no events, so maybe wait for some */ |
598 |
|
|
iouring_tfd_update (EV_A_ timeout); |
599 |
|
|
|
600 |
root |
1.6 |
/* only enter the kernel if we have something to submit, or we need to wait */ |
601 |
root |
1.1 |
if (timeout || iouring_to_submit) |
602 |
|
|
{ |
603 |
|
|
int res; |
604 |
|
|
|
605 |
|
|
EV_RELEASE_CB; |
606 |
|
|
|
607 |
|
|
res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1, |
608 |
root |
1.3 |
timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0); |
609 |
root |
1.1 |
iouring_to_submit = 0; |
610 |
|
|
|
611 |
|
|
EV_ACQUIRE_CB; |
612 |
|
|
|
613 |
|
|
if (ecb_expect_false (res < 0)) |
614 |
|
|
if (errno == EINTR) |
615 |
|
|
/* ignore */; |
616 |
|
|
else |
617 |
|
|
ev_syserr ("(libev) iouring setup"); |
618 |
|
|
else |
619 |
|
|
iouring_handle_cq (EV_A); |
620 |
|
|
} |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
inline_size |
624 |
|
|
int |
625 |
|
|
iouring_init (EV_P_ int flags) |
626 |
|
|
{ |
627 |
|
|
iouring_entries = IOURING_INIT_ENTRIES; |
628 |
|
|
iouring_max_entries = 0; |
629 |
|
|
|
630 |
|
|
if (iouring_internal_init (EV_A) < 0) |
631 |
|
|
{ |
632 |
|
|
iouring_internal_destroy (EV_A); |
633 |
|
|
return 0; |
634 |
|
|
} |
635 |
|
|
|
636 |
root |
1.6 |
ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); |
637 |
root |
1.8 |
ev_set_priority (&iouring_tfd_w, EV_MINPRI); |
638 |
root |
1.1 |
ev_io_start (EV_A_ &iouring_tfd_w); |
639 |
|
|
ev_unref (EV_A); /* watcher should not keep loop alive */ |
640 |
|
|
|
641 |
|
|
backend_modify = iouring_modify; |
642 |
|
|
backend_poll = iouring_poll; |
643 |
|
|
|
644 |
|
|
return EVBACKEND_IOURING; |
645 |
|
|
} |
646 |
|
|
|
647 |
|
|
inline_size |
648 |
|
|
void |
649 |
|
|
iouring_destroy (EV_P) |
650 |
|
|
{ |
651 |
|
|
iouring_internal_destroy (EV_A); |
652 |
|
|
} |
653 |
|
|
|