ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_iouring.c
Revision: 1.23
Committed: Sun Jul 26 11:10:45 2020 UTC (3 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +70 -50 lines
Log Message:
§

File Contents

# User Rev Content
1 root 1.1 /*
2     * libev linux io_uring fd activity backend
3     *
4 root 1.21 * Copyright (c) 2019-2020 Marc Alexander Lehmann <libev@schmorp.de>
5 root 1.1 * 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 root 1.18 * e) why two mmaps instead of one? one would be more space-efficient,
50     * and I can't see what benefit two would have (other than being
51 root 1.1 * somehow resizable/relocatable, but that's apparently not possible).
52 root 1.18 * f) hmm, it's practically undebuggable (gdb can't access the memory, and
53 root 1.7 * the bizarre way structure offsets are communicated makes it hard to
54 root 1.1 * just print the ring buffer heads, even *iff* the memory were visible
55     * in gdb. but then, that's also ok, really.
56     * g) well, you cannot specify a timeout when waiting for events. no,
57     * seriously, the interface doesn't support a timeout. never seen _that_
58     * before. sure, you can use a timerfd, but that's another syscall
59     * you could have avoided. overall, this bizarre omission smells
60     * like a µ-optimisation by the io_uring author for his personal
61     * applications, to the detriment of everybody else who just wants
62     * an event loop. but, umm, ok, if that's all, it could be worse.
63 root 1.19 * (from what I gather from the author Jens Axboe, it simply didn't
64 root 1.22 * occur to him, and he made good on it by adding an unlimited number
65 root 1.19 * of timeouts later :).
66 root 1.18 * h) initially there was a hardcoded limit of 4096 outstanding events.
67 root 1.19 * later versions not only bump this to 32k, but also can handle
68 root 1.18 * an unlimited amount of events, so this only affects the batch size.
69 root 1.1 * i) unlike linux aio, you *can* register more then the limit
70 root 1.18 * of fd events. while early verisons of io_uring signalled an overflow
71     * and you ended up getting wet. 5.5+ does not do this anymore.
72     * j) but, oh my! it had exactly the same bugs as the linux aio backend,
73     * where some undocumented poll combinations just fail. fortunately,
74     * after finally reaching the author, he was more than willing to fix
75     * this probably in 5.6+.
76 root 1.1 * k) overall, the *API* itself is, I dare to say, not a total trainwreck.
77 root 1.18 * once the bugs ae fixed (probably in 5.6+), it will be without
78     * competition.
79 root 1.1 */
80    
81 root 1.9 /* TODO: use internal TIMEOUT */
82     /* TODO: take advantage of single mmap, NODROP etc. */
83     /* TODO: resize cq/sq size independently */
84    
85 root 1.1 #include <sys/timerfd.h>
86     #include <sys/mman.h>
87     #include <poll.h>
88 root 1.12 #include <stdint.h>
89 root 1.1
90     #define IOURING_INIT_ENTRIES 32
91    
92     /*****************************************************************************/
93     /* syscall wrapdadoop - this section has the raw api/abi definitions */
94    
95     #include <linux/fs.h>
96     #include <linux/types.h>
97    
98     /* mostly directly taken from the kernel or documentation */
99    
100     struct io_uring_sqe
101     {
102     __u8 opcode;
103     __u8 flags;
104     __u16 ioprio;
105     __s32 fd;
106 root 1.8 union {
107     __u64 off;
108     __u64 addr2;
109     };
110 root 1.1 __u64 addr;
111     __u32 len;
112     union {
113     __kernel_rwf_t rw_flags;
114     __u32 fsync_flags;
115     __u16 poll_events;
116     __u32 sync_range_flags;
117     __u32 msg_flags;
118 root 1.8 __u32 timeout_flags;
119     __u32 accept_flags;
120     __u32 cancel_flags;
121     __u32 open_flags;
122     __u32 statx_flags;
123 root 1.23 __u32 fadvise_advice;
124 root 1.1 };
125     __u64 user_data;
126     union {
127     __u16 buf_index;
128 root 1.23 __u16 personality;
129 root 1.1 __u64 __pad2[3];
130     };
131     };
132    
133     struct io_uring_cqe
134     {
135     __u64 user_data;
136     __s32 res;
137     __u32 flags;
138     };
139    
140     struct io_sqring_offsets
141     {
142     __u32 head;
143     __u32 tail;
144     __u32 ring_mask;
145     __u32 ring_entries;
146     __u32 flags;
147     __u32 dropped;
148     __u32 array;
149     __u32 resv1;
150     __u64 resv2;
151     };
152    
153     struct io_cqring_offsets
154     {
155     __u32 head;
156     __u32 tail;
157     __u32 ring_mask;
158     __u32 ring_entries;
159     __u32 overflow;
160     __u32 cqes;
161     __u64 resv[2];
162     };
163    
164     struct io_uring_params
165     {
166     __u32 sq_entries;
167     __u32 cq_entries;
168     __u32 flags;
169     __u32 sq_thread_cpu;
170     __u32 sq_thread_idle;
171 root 1.8 __u32 features;
172     __u32 resv[4];
173 root 1.1 struct io_sqring_offsets sq_off;
174     struct io_cqring_offsets cq_off;
175     };
176    
177 root 1.23 #define IORING_FEAT_SINGLE_MMAP 0x00000001
178     #define IORING_FEAT_NODROP 0x00000002
179     #define IORING_FEAT_SUBMIT_STABLE 0x00000004
180    
181 root 1.12 #define IORING_SETUP_CQSIZE 0x00000008
182 root 1.23 #define IORING_SETUP_CLAMP 0x00000010
183 root 1.12
184     #define IORING_OP_POLL_ADD 6
185     #define IORING_OP_POLL_REMOVE 7
186     #define IORING_OP_TIMEOUT 11
187     #define IORING_OP_TIMEOUT_REMOVE 12
188    
189 root 1.23 #define IORING_REGISTER_EVENTFD 4
190     #define IORING_REGISTER_EVENTFD_ASYNC 7
191     #define IORING_REGISTER_PROBE 8
192    
193     #define IO_URING_OP_SUPPORTED 1
194    
195     struct io_uring_probe_op {
196     __u8 op;
197     __u8 resv;
198     __u16 flags;
199     __u32 resv2;
200     };
201    
202     struct io_uring_probe
203     {
204     __u8 last_op;
205     __u8 ops_len;
206     __u16 resv;
207     __u32 resv2[3];
208     struct io_uring_probe_op ops[0];
209     };
210    
211 root 1.12 /* relative or absolute, reference clock is CLOCK_MONOTONIC */
212     struct iouring_kernel_timespec
213     {
214     int64_t tv_sec;
215     long long tv_nsec;
216     };
217    
218     #define IORING_TIMEOUT_ABS 0x00000001
219 root 1.1
220     #define IORING_ENTER_GETEVENTS 0x01
221    
222     #define IORING_OFF_SQ_RING 0x00000000ULL
223     #define IORING_OFF_SQES 0x10000000ULL
224    
225 root 1.12 #define IORING_FEAT_SINGLE_MMAP 0x00000001
226     #define IORING_FEAT_NODROP 0x00000002
227     #define IORING_FEAT_SUBMIT_STABLE 0x00000004
228 root 1.8
229 root 1.1 inline_size
230     int
231     evsys_io_uring_setup (unsigned entries, struct io_uring_params *params)
232     {
233     return ev_syscall2 (SYS_io_uring_setup, entries, params);
234     }
235    
236     inline_size
237     int
238     evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz)
239     {
240     return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz);
241     }
242    
243 root 1.23 inline_size
244     int
245     evsys_io_uring_register (unsigned int fd, unsigned int opcode, void *arg, unsigned int nr_args)
246     {
247     return ev_syscall4 (SYS_io_uring_register, fd, opcode, arg, nr_args);
248     }
249    
250 root 1.1 /*****************************************************************************/
251 root 1.23 /* actual backend implementation */
252 root 1.1
253     /* we hope that volatile will make the compiler access this variables only once */
254 root 1.23 #define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_sq_ ## name)
255     #define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_cq_ ## name)
256 root 1.1
257     /* the index array */
258 root 1.23 #define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array))
259 root 1.1
260     /* the submit/completion queue entries */
261     #define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
262 root 1.23 #define EV_CQES ((struct io_uring_cqe *)((char *)iouring_ring + iouring_cq_cqes))
263 root 1.1
264 root 1.16 inline_speed
265     int
266 root 1.14 iouring_enter (EV_P_ ev_tstamp timeout)
267     {
268     int res;
269    
270     EV_RELEASE_CB;
271    
272     res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
273     timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
274    
275     assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
276    
277     iouring_to_submit = 0;
278    
279     EV_ACQUIRE_CB;
280    
281     return res;
282     }
283    
284 root 1.16 /* TODO: can we move things around so we don't need this forward-reference? */
285     static void
286     iouring_poll (EV_P_ ev_tstamp timeout);
287    
288 root 1.1 static
289     struct io_uring_sqe *
290     iouring_sqe_get (EV_P)
291     {
292 root 1.16 unsigned tail;
293    
294     for (;;)
295     {
296     tail = EV_SQ_VAR (tail);
297 root 1.1
298 root 1.16 if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
299     break; /* whats the problem, we have free sqes */
300    
301     /* queue full, need to flush and possibly handle some events */
302 root 1.14
303 root 1.16 #if EV_FEATURE_CODE
304     /* first we ask the kernel nicely, most often this frees up some sqes */
305 root 1.14 int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
306    
307 root 1.16 ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
308    
309     if (res >= 0)
310     continue; /* yes, it worked, try again */
311     #endif
312    
313     /* some problem, possibly EBUSY - do the full poll and let it handle any issues */
314 root 1.14
315 root 1.16 iouring_poll (EV_A_ EV_TS_CONST (0.));
316     /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
317 root 1.1 }
318    
319 root 1.14 /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
320 root 1.1
321     return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
322     }
323    
324     inline_size
325     struct io_uring_sqe *
326     iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe)
327     {
328     unsigned idx = sqe - EV_SQES;
329    
330 root 1.23 printf ("submit idx %d, op %d, fd %d, us5r %p, poll %d\n", idx, sqe->opcode, sqe->fd, sqe->user_data, sqe->poll_events);
331    
332 root 1.1 EV_SQ_ARRAY [idx] = idx;
333     ECB_MEMORY_FENCE_RELEASE;
334     ++EV_SQ_VAR (tail);
335     /*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */
336     ++iouring_to_submit;
337     }
338    
339     /*****************************************************************************/
340    
341     /* when the timerfd expires we simply note the fact,
342     * as the purpose of the timerfd is to wake us up, nothing else.
343     * the next iteration should re-set it.
344     */
345     static void
346     iouring_tfd_cb (EV_P_ struct ev_io *w, int revents)
347     {
348     iouring_tfd_to = EV_TSTAMP_HUGE;
349     }
350    
351     /* called for full and partial cleanup */
352     ecb_cold
353     static int
354     iouring_internal_destroy (EV_P)
355     {
356     close (iouring_tfd);
357     close (iouring_fd);
358    
359 root 1.23 if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size);
360     if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size);
361 root 1.1
362 root 1.8 if (ev_is_active (&iouring_tfd_w))
363     {
364     ev_ref (EV_A);
365     ev_io_stop (EV_A_ &iouring_tfd_w);
366     }
367 root 1.1 }
368    
369     ecb_cold
370     static int
371     iouring_internal_init (EV_P)
372     {
373     struct io_uring_params params = { 0 };
374 root 1.23 uint32_t sq_size, cq_size;
375    
376     params.flags = IORING_SETUP_CLAMP;
377 root 1.1
378     iouring_to_submit = 0;
379    
380 root 1.23 iouring_tfd = -1;
381     iouring_ring = MAP_FAILED;
382     iouring_sqes = MAP_FAILED;
383 root 1.1
384 root 1.12 if (!have_monotonic) /* cannot really happen, but what if11 */
385     return -1;
386    
387 root 1.23 iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
388    
389     if (iouring_fd < 0)
390     return -1;
391 root 1.1
392 root 1.23 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
393     return -1; /* we require the above features */
394 root 1.1
395 root 1.23 /* TODO: remember somehow whether our queue size has been clamped */
396 root 1.1
397 root 1.23 sq_size = params.sq_off.array + params.sq_entries * sizeof (unsigned);
398     cq_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe);
399 root 1.11
400 root 1.23 iouring_ring_size = sq_size > cq_size ? sq_size : cq_size;
401     iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);
402 root 1.1
403 root 1.23 iouring_ring = mmap (0, iouring_ring_size, PROT_READ | PROT_WRITE,
404     MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING);
405     iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE,
406     MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES);
407 root 1.1
408 root 1.23 if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
409 root 1.1 return -1;
410    
411     iouring_sq_head = params.sq_off.head;
412     iouring_sq_tail = params.sq_off.tail;
413     iouring_sq_ring_mask = params.sq_off.ring_mask;
414     iouring_sq_ring_entries = params.sq_off.ring_entries;
415     iouring_sq_flags = params.sq_off.flags;
416     iouring_sq_dropped = params.sq_off.dropped;
417     iouring_sq_array = params.sq_off.array;
418    
419     iouring_cq_head = params.cq_off.head;
420     iouring_cq_tail = params.cq_off.tail;
421     iouring_cq_ring_mask = params.cq_off.ring_mask;
422     iouring_cq_ring_entries = params.cq_off.ring_entries;
423     iouring_cq_overflow = params.cq_off.overflow;
424     iouring_cq_cqes = params.cq_off.cqes;
425    
426 root 1.23 iouring_tfd_to = EV_TSTAMP_HUGE;
427    
428 root 1.1 iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC);
429    
430     if (iouring_tfd < 0)
431 root 1.23 return -1;
432 root 1.1
433     return 0;
434     }
435    
436     ecb_cold
437     static void
438     iouring_fork (EV_P)
439     {
440     iouring_internal_destroy (EV_A);
441    
442     while (iouring_internal_init (EV_A) < 0)
443     ev_syserr ("(libev) io_uring_setup");
444    
445 root 1.8 fd_rearm_all (EV_A);
446 root 1.1
447     ev_io_stop (EV_A_ &iouring_tfd_w);
448     ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ);
449     ev_io_start (EV_A_ &iouring_tfd_w);
450     }
451    
452     /*****************************************************************************/
453    
454     static void
455     iouring_modify (EV_P_ int fd, int oev, int nev)
456     {
457     if (oev)
458     {
459     /* we assume the sqe's are all "properly" initialised */
460     struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
461     sqe->opcode = IORING_OP_POLL_REMOVE;
462     sqe->fd = fd;
463 root 1.13 /* Jens Axboe notified me that user_data is not what is documented, but is
464     * some kind of unique ID that has to match, otherwise the request cannot
465     * be removed. Since we don't *really* have that, we pass in the old
466     * generation counter - if that fails, too bad, it will hopefully be removed
467     * at close time and then be ignored. */
468 root 1.15 sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
469     sqe->user_data = (uint64_t)-1;
470 root 1.1 iouring_sqe_submit (EV_A_ sqe);
471 root 1.2
472     /* increment generation counter to avoid handling old events */
473     ++anfds [fd].egen;
474 root 1.1 }
475    
476     if (nev)
477     {
478     struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
479     sqe->opcode = IORING_OP_POLL_ADD;
480     sqe->fd = fd;
481 root 1.20 sqe->addr = 0;
482 root 1.2 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
483 root 1.1 sqe->poll_events =
484     (nev & EV_READ ? POLLIN : 0)
485     | (nev & EV_WRITE ? POLLOUT : 0);
486     iouring_sqe_submit (EV_A_ sqe);
487     }
488     }
489    
490     inline_size
491     void
492     iouring_tfd_update (EV_P_ ev_tstamp timeout)
493     {
494     ev_tstamp tfd_to = mn_now + timeout;
495    
496     /* we assume there will be many iterations per timer change, so
497     * we only re-set the timerfd when we have to because its expiry
498     * is too late.
499     */
500     if (ecb_expect_false (tfd_to < iouring_tfd_to))
501     {
502     struct itimerspec its;
503    
504     iouring_tfd_to = tfd_to;
505     EV_TS_SET (its.it_interval, 0.);
506     EV_TS_SET (its.it_value, tfd_to);
507    
508     if (timerfd_settime (iouring_tfd, TFD_TIMER_ABSTIME, &its, 0) < 0)
509     assert (("libev: iouring timerfd_settime failed", 0));
510     }
511     }
512    
513     inline_size
514     void
515     iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe)
516     {
517     int fd = cqe->user_data & 0xffffffffU;
518     uint32_t gen = cqe->user_data >> 32;
519     int res = cqe->res;
520    
521 root 1.15 /* user_data -1 is a remove that we are not atm. interested in */
522     if (cqe->user_data == (uint64_t)-1)
523     return;
524    
525 root 1.1 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
526    
527     /* documentation lies, of course. the result value is NOT like
528     * normal syscalls, but like linux raw syscalls, i.e. negative
529     * error numbers. fortunate, as otherwise there would be no way
530     * to get error codes at all. still, why not document this?
531     */
532    
533     /* ignore event if generation doesn't match */
534 root 1.13 /* other than skipping removal events, */
535 root 1.1 /* this should actually be very rare */
536 root 1.2 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
537 root 1.1 return;
538    
539     if (ecb_expect_false (res < 0))
540     {
541 root 1.11 /*TODO: EINVAL handling (was something failed with this fd)*/
542 root 1.1
543 root 1.8 if (res == -EBADF)
544 root 1.1 {
545     assert (("libev: event loop rejected bad fd", res != -EBADF));
546     fd_kill (EV_A_ fd);
547     }
548     else
549     {
550     errno = -res;
551     ev_syserr ("(libev) IORING_OP_POLL_ADD");
552     }
553    
554     return;
555     }
556    
557     /* feed events, we do not expect or handle POLLNVAL */
558     fd_event (
559     EV_A_
560     fd,
561     (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
562     | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
563     );
564    
565     /* io_uring is oneshot, so we need to re-arm the fd next iteration */
566     /* this also means we usually have to do at least one syscall per iteration */
567     anfds [fd].events = 0;
568     fd_change (EV_A_ fd, EV_ANFD_REIFY);
569     }
570    
571     /* called when the event queue overflows */
572     ecb_cold
573     static void
574     iouring_overflow (EV_P)
575     {
576     /* we have two options, resize the queue (by tearing down
577     * everything and recreating it, or living with it
578     * and polling.
579 root 1.10 * we implement this by resizing the queue, and, if that fails,
580 root 1.1 * we just recreate the state on every failure, which
581     * kind of is a very inefficient poll.
582     * one danger is, due to the bios toward lower fds,
583     * we will only really get events for those, so
584     * maybe we need a poll() fallback, after all.
585     */
586     /*EV_CQ_VAR (overflow) = 0;*/ /* need to do this if we keep the state and poll manually */
587    
588     fd_rearm_all (EV_A);
589    
590     /* we double the size until we hit the hard-to-probe maximum */
591     if (!iouring_max_entries)
592     {
593     iouring_entries <<= 1;
594     iouring_fork (EV_A);
595     }
596     else
597     {
598     /* we hit the kernel limit, we should fall back to something else.
599     * we can either poll() a few times and hope for the best,
600     * poll always, or switch to epoll.
601 root 1.10 * TODO: is this necessary with newer kernels?
602 root 1.1 */
603    
604     iouring_internal_destroy (EV_A);
605    
606 root 1.10 /* this should make it so that on return, we don't call any uring functions */
607 root 1.1 iouring_to_submit = 0;
608    
609     for (;;)
610     {
611     backend = epoll_init (EV_A_ 0);
612    
613     if (backend)
614     break;
615    
616     ev_syserr ("(libev) iouring switch to epoll");
617     }
618     }
619     }
620    
621     /* handle any events in the completion queue, return true if there were any */
622     static int
623     iouring_handle_cq (EV_P)
624     {
625     unsigned head, tail, mask;
626    
627     head = EV_CQ_VAR (head);
628     ECB_MEMORY_FENCE_ACQUIRE;
629     tail = EV_CQ_VAR (tail);
630    
631     if (head == tail)
632     return 0;
633    
634     /* it can only overflow if we have events, yes, yes? */
635     if (ecb_expect_false (EV_CQ_VAR (overflow)))
636     {
637     iouring_overflow (EV_A);
638     return 1;
639     }
640    
641     mask = EV_CQ_VAR (ring_mask);
642    
643     do
644     iouring_process_cqe (EV_A_ &EV_CQES [head++ & mask]);
645     while (head != tail);
646    
647     EV_CQ_VAR (head) = head;
648     ECB_MEMORY_FENCE_RELEASE;
649    
650     return 1;
651     }
652    
653     static void
654     iouring_poll (EV_P_ ev_tstamp timeout)
655     {
656     /* if we have events, no need for extra syscalls, but we might have to queue events */
657 root 1.16 /* we also clar the timeout if there are outstanding fdchanges */
658     /* the latter should only happen if both the sq and cq are full, most likely */
659     /* because we have a lot of event sources that immediately complete */
660     /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
661     if (iouring_handle_cq (EV_A) || fdchangecnt)
662 root 1.3 timeout = EV_TS_CONST (0.);
663 root 1.1 else
664     /* no events, so maybe wait for some */
665     iouring_tfd_update (EV_A_ timeout);
666    
667 root 1.6 /* only enter the kernel if we have something to submit, or we need to wait */
668 root 1.1 if (timeout || iouring_to_submit)
669     {
670 root 1.14 int res = iouring_enter (EV_A_ timeout);
671 root 1.1
672     if (ecb_expect_false (res < 0))
673     if (errno == EINTR)
674     /* ignore */;
675 root 1.14 else if (errno == EBUSY)
676     /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
677 root 1.1 else
678     ev_syserr ("(libev) iouring setup");
679     else
680     iouring_handle_cq (EV_A);
681     }
682     }
683    
684     inline_size
685     int
686     iouring_init (EV_P_ int flags)
687     {
688     iouring_entries = IOURING_INIT_ENTRIES;
689     iouring_max_entries = 0;
690    
691     if (iouring_internal_init (EV_A) < 0)
692     {
693     iouring_internal_destroy (EV_A);
694     return 0;
695     }
696    
697 root 1.6 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ);
698 root 1.8 ev_set_priority (&iouring_tfd_w, EV_MINPRI);
699 root 1.1 ev_io_start (EV_A_ &iouring_tfd_w);
700     ev_unref (EV_A); /* watcher should not keep loop alive */
701    
702     backend_modify = iouring_modify;
703     backend_poll = iouring_poll;
704    
705     return EVBACKEND_IOURING;
706     }
707    
708     inline_size
709     void
710     iouring_destroy (EV_P)
711     {
712     iouring_internal_destroy (EV_A);
713     }
714