ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_iouring.c
(Generate patch)

Comparing libev/ev_iouring.c (file contents):
Revision 1.7 by root, Sun Dec 22 15:05:32 2019 UTC vs.
Revision 1.17 by root, Sat Dec 28 07:39:18 2019 UTC

80 * k) overall, the *API* itself is, I dare to say, not a total trainwreck. 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 81 * the big isuess with it are the bugs requiring epoll, which might
82 * or might not get fixed (do I hold my breath?). 82 * or might not get fixed (do I hold my breath?).
83 */ 83 */
84 84
85/* TODO: use internal TIMEOUT */
86/* TODO: take advantage of single mmap, NODROP etc. */
87/* TODO: resize cq/sq size independently */
88
85#include <sys/timerfd.h> 89#include <sys/timerfd.h>
86#include <sys/mman.h> 90#include <sys/mman.h>
87#include <poll.h> 91#include <poll.h>
92#include <stdint.h>
88 93
89#define IOURING_INIT_ENTRIES 32 94#define IOURING_INIT_ENTRIES 32
90 95
91/*****************************************************************************/ 96/*****************************************************************************/
92/* syscall wrapdadoop - this section has the raw api/abi definitions */ 97/* syscall wrapdadoop - this section has the raw api/abi definitions */
100{ 105{
101 __u8 opcode; 106 __u8 opcode;
102 __u8 flags; 107 __u8 flags;
103 __u16 ioprio; 108 __u16 ioprio;
104 __s32 fd; 109 __s32 fd;
110 union {
105 __u64 off; 111 __u64 off;
112 __u64 addr2;
113 };
106 __u64 addr; 114 __u64 addr;
107 __u32 len; 115 __u32 len;
108 union { 116 union {
109 __kernel_rwf_t rw_flags; 117 __kernel_rwf_t rw_flags;
110 __u32 fsync_flags; 118 __u32 fsync_flags;
111 __u16 poll_events; 119 __u16 poll_events;
112 __u32 sync_range_flags; 120 __u32 sync_range_flags;
113 __u32 msg_flags; 121 __u32 msg_flags;
122 __u32 timeout_flags;
123 __u32 accept_flags;
124 __u32 cancel_flags;
125 __u32 open_flags;
126 __u32 statx_flags;
114 }; 127 };
115 __u64 user_data; 128 __u64 user_data;
116 union { 129 union {
117 __u16 buf_index; 130 __u16 buf_index;
118 __u64 __pad2[3]; 131 __u64 __pad2[3];
155 __u32 sq_entries; 168 __u32 sq_entries;
156 __u32 cq_entries; 169 __u32 cq_entries;
157 __u32 flags; 170 __u32 flags;
158 __u32 sq_thread_cpu; 171 __u32 sq_thread_cpu;
159 __u32 sq_thread_idle; 172 __u32 sq_thread_idle;
173 __u32 features;
160 __u32 resv[5]; 174 __u32 resv[4];
161 struct io_sqring_offsets sq_off; 175 struct io_sqring_offsets sq_off;
162 struct io_cqring_offsets cq_off; 176 struct io_cqring_offsets cq_off;
163}; 177};
164 178
179#define IORING_SETUP_CQSIZE 0x00000008
180
165#define IORING_OP_POLL_ADD 6 181#define IORING_OP_POLL_ADD 6
166#define IORING_OP_POLL_REMOVE 7 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 */
187struct iouring_kernel_timespec
188{
189 int64_t tv_sec;
190 long long tv_nsec;
191};
192
193#define IORING_TIMEOUT_ABS 0x00000001
167 194
168#define IORING_ENTER_GETEVENTS 0x01 195#define IORING_ENTER_GETEVENTS 0x01
169 196
170#define IORING_OFF_SQ_RING 0x00000000ULL 197#define IORING_OFF_SQ_RING 0x00000000ULL
171#define IORING_OFF_CQ_RING 0x08000000ULL 198#define IORING_OFF_CQ_RING 0x08000000ULL
172#define IORING_OFF_SQES 0x10000000ULL 199#define IORING_OFF_SQES 0x10000000ULL
173 200
201#define IORING_FEAT_SINGLE_MMAP 0x00000001
202#define IORING_FEAT_NODROP 0x00000002
203#define IORING_FEAT_SUBMIT_STABLE 0x00000004
204
174inline_size 205inline_size
175int 206int
176evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) 207evsys_io_uring_setup (unsigned entries, struct io_uring_params *params)
177{ 208{
178 return ev_syscall2 (SYS_io_uring_setup, entries, params); 209 return ev_syscall2 (SYS_io_uring_setup, entries, params);
197 228
198/* the submit/completion queue entries */ 229/* the submit/completion queue entries */
199#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) 230#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
200#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) 231#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes))
201 232
233inline_speed
234int
235iouring_enter (EV_P_ ev_tstamp timeout)
236{
237 int res;
238
239 EV_RELEASE_CB;
240
241 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
242 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
243
244 assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
245
246 iouring_to_submit = 0;
247
248 EV_ACQUIRE_CB;
249
250 return res;
251}
252
253/* TODO: can we move things around so we don't need this forward-reference? */
254static void
255iouring_poll (EV_P_ ev_tstamp timeout);
256
202static 257static
203struct io_uring_sqe * 258struct io_uring_sqe *
204iouring_sqe_get (EV_P) 259iouring_sqe_get (EV_P)
205{ 260{
261 unsigned tail;
262
263 for (;;)
264 {
206 unsigned tail = EV_SQ_VAR (tail); 265 tail = EV_SQ_VAR (tail);
207 266
208 if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) 267 if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
268 break; /* whats the problem, we have free sqes */
269
270 /* queue full, need to flush and possibly handle some events */
271
272#if EV_FEATURE_CODE
273 /* first we ask the kernel nicely, most often this frees up some sqes */
274 int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
275
276 ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
277
278 if (res >= 0)
279 continue; /* yes, it worked, try again */
280#endif
281
282 /* some problem, possibly EBUSY - do the full poll and let it handle any issues */
283
284 iouring_poll (EV_A_ EV_TS_CONST (0.));
285 /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
209 { 286 }
210 /* queue full, flush */
211 evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0);
212 iouring_to_submit = 0;
213 }
214 287
215 assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); 288 /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
216 289
217 return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); 290 return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
218} 291}
219 292
220inline_size 293inline_size
240iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) 313iouring_tfd_cb (EV_P_ struct ev_io *w, int revents)
241{ 314{
242 iouring_tfd_to = EV_TSTAMP_HUGE; 315 iouring_tfd_to = EV_TSTAMP_HUGE;
243} 316}
244 317
245static void
246iouring_epoll_cb (EV_P_ struct ev_io *w, int revents)
247{
248 epoll_poll (EV_A_ 0);
249}
250
251/* called for full and partial cleanup */ 318/* called for full and partial cleanup */
252ecb_cold 319ecb_cold
253static int 320static int
254iouring_internal_destroy (EV_P) 321iouring_internal_destroy (EV_P)
255{ 322{
258 325
259 if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); 326 if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size);
260 if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size); 327 if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size);
261 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); 328 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size );
262 329
263 if (ev_is_active (&iouring_epoll_w)) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_epoll_w); 330 if (ev_is_active (&iouring_tfd_w))
264 if (ev_is_active (&iouring_tfd_w )) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_tfd_w ); 331 {
332 ev_ref (EV_A);
333 ev_io_stop (EV_A_ &iouring_tfd_w);
334 }
265} 335}
266 336
267ecb_cold 337ecb_cold
268static int 338static int
269iouring_internal_init (EV_P) 339iouring_internal_init (EV_P)
275 iouring_tfd = -1; 345 iouring_tfd = -1;
276 iouring_sq_ring = MAP_FAILED; 346 iouring_sq_ring = MAP_FAILED;
277 iouring_cq_ring = MAP_FAILED; 347 iouring_cq_ring = MAP_FAILED;
278 iouring_sqes = MAP_FAILED; 348 iouring_sqes = MAP_FAILED;
279 349
350 if (!have_monotonic) /* cannot really happen, but what if11 */
351 return -1;
352
280 for (;;) 353 for (;;)
281 { 354 {
282 iouring_fd = evsys_io_uring_setup (iouring_entries, &params); 355 iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
283 356
284 if (iouring_fd >= 0) 357 if (iouring_fd >= 0)
285 break; /* yippie */ 358 break; /* yippie */
286 359
287 if (errno != EINVAL) 360 if (errno != EINVAL)
288 return -1; /* we failed */ 361 return -1; /* we failed */
362
363#if TODO
364 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
365 return -1; /* we require the above features */
366#endif
289 367
290 /* EINVAL: lots of possible reasons, but maybe 368 /* EINVAL: lots of possible reasons, but maybe
291 * it is because we hit the unqueryable hardcoded size limit 369 * it is because we hit the unqueryable hardcoded size limit
292 */ 370 */
293 371
346 iouring_internal_destroy (EV_A); 424 iouring_internal_destroy (EV_A);
347 425
348 while (iouring_internal_init (EV_A) < 0) 426 while (iouring_internal_init (EV_A) < 0)
349 ev_syserr ("(libev) io_uring_setup"); 427 ev_syserr ("(libev) io_uring_setup");
350 428
351 /* forking epoll should also effectively unregister all fds from the backend */
352 epoll_fork (EV_A);
353 /* epoll_fork already did this. hopefully */
354 /*fd_rearm_all (EV_A);*/ 429 fd_rearm_all (EV_A);
355
356 ev_io_stop (EV_A_ &iouring_epoll_w);
357 ev_io_set (EV_A_ &iouring_epoll_w, backend_fd, EV_READ);
358 ev_io_start (EV_A_ &iouring_epoll_w);
359 430
360 ev_io_stop (EV_A_ &iouring_tfd_w); 431 ev_io_stop (EV_A_ &iouring_tfd_w);
361 ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); 432 ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ);
362 ev_io_start (EV_A_ &iouring_tfd_w); 433 ev_io_start (EV_A_ &iouring_tfd_w);
363} 434}
365/*****************************************************************************/ 436/*****************************************************************************/
366 437
367static void 438static void
368iouring_modify (EV_P_ int fd, int oev, int nev) 439iouring_modify (EV_P_ int fd, int oev, int nev)
369{ 440{
370 if (ecb_expect_false (anfds [fd].eflags))
371 {
372 /* we handed this fd over to epoll, so undo this first */
373 /* we do it manually because the optimisations on epoll_modify won't do us any good */
374 epoll_ctl (iouring_fd, EPOLL_CTL_DEL, fd, 0);
375 anfds [fd].eflags = 0;
376 oev = 0;
377 }
378
379 if (oev) 441 if (oev)
380 { 442 {
381 /* we assume the sqe's are all "properly" initialised */ 443 /* we assume the sqe's are all "properly" initialised */
382 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 444 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
383 sqe->opcode = IORING_OP_POLL_REMOVE; 445 sqe->opcode = IORING_OP_POLL_REMOVE;
384 sqe->fd = fd; 446 sqe->fd = fd;
447 /* Jens Axboe notified me that user_data is not what is documented, but is
448 * some kind of unique ID that has to match, otherwise the request cannot
449 * be removed. Since we don't *really* have that, we pass in the old
450 * generation counter - if that fails, too bad, it will hopefully be removed
451 * at close time and then be ignored. */
452 sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
385 sqe->user_data = -1; 453 sqe->user_data = (uint64_t)-1;
386 iouring_sqe_submit (EV_A_ sqe); 454 iouring_sqe_submit (EV_A_ sqe);
387 455
388 /* increment generation counter to avoid handling old events */ 456 /* increment generation counter to avoid handling old events */
389 ++anfds [fd].egen; 457 ++anfds [fd].egen;
390 } 458 }
431{ 499{
432 int fd = cqe->user_data & 0xffffffffU; 500 int fd = cqe->user_data & 0xffffffffU;
433 uint32_t gen = cqe->user_data >> 32; 501 uint32_t gen = cqe->user_data >> 32;
434 int res = cqe->res; 502 int res = cqe->res;
435 503
436 /* ignore fd removal events, if there are any. TODO: verify */ 504 /* user_data -1 is a remove that we are not atm. interested in */
437 if (cqe->user_data == (__u64)-1) 505 if (cqe->user_data == (uint64_t)-1)
438 abort ();//D 506 return;
439 507
440 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); 508 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
441 509
442 /* documentation lies, of course. the result value is NOT like 510 /* documentation lies, of course. the result value is NOT like
443 * normal syscalls, but like linux raw syscalls, i.e. negative 511 * normal syscalls, but like linux raw syscalls, i.e. negative
444 * error numbers. fortunate, as otherwise there would be no way 512 * error numbers. fortunate, as otherwise there would be no way
445 * to get error codes at all. still, why not document this? 513 * to get error codes at all. still, why not document this?
446 */ 514 */
447 515
448 /* ignore event if generation doesn't match */ 516 /* ignore event if generation doesn't match */
517 /* other than skipping removal events, */
449 /* this should actually be very rare */ 518 /* this should actually be very rare */
450 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) 519 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
451 return; 520 return;
452 521
453 if (ecb_expect_false (res < 0)) 522 if (ecb_expect_false (res < 0))
454 { 523 {
455 if (res == -EINVAL) 524 /*TODO: EINVAL handling (was something failed with this fd)*/
456 { 525 /*TODO: EBUSY happens when?*/
457 /* we assume this error code means the fd/poll combination is buggy
458 * and fall back to epoll.
459 * this error code might also indicate a bug, but the kernel doesn't
460 * distinguish between those two conditions, so... sigh...
461 */
462 526
463 epoll_modify (EV_A_ fd, 0, anfds [fd].events);
464 }
465 else if (res == -EBADF) 527 if (res == -EBADF)
466 { 528 {
467 assert (("libev: event loop rejected bad fd", res != -EBADF)); 529 assert (("libev: event loop rejected bad fd", res != -EBADF));
468 fd_kill (EV_A_ fd); 530 fd_kill (EV_A_ fd);
469 } 531 }
470 else 532 else
496iouring_overflow (EV_P) 558iouring_overflow (EV_P)
497{ 559{
498 /* we have two options, resize the queue (by tearing down 560 /* we have two options, resize the queue (by tearing down
499 * everything and recreating it, or living with it 561 * everything and recreating it, or living with it
500 * and polling. 562 * and polling.
501 * we implement this by resizing tghe queue, and, if that fails, 563 * we implement this by resizing the queue, and, if that fails,
502 * we just recreate the state on every failure, which 564 * we just recreate the state on every failure, which
503 * kind of is a very inefficient poll. 565 * kind of is a very inefficient poll.
504 * one danger is, due to the bios toward lower fds, 566 * one danger is, due to the bios toward lower fds,
505 * we will only really get events for those, so 567 * we will only really get events for those, so
506 * maybe we need a poll() fallback, after all. 568 * maybe we need a poll() fallback, after all.
518 else 580 else
519 { 581 {
520 /* we hit the kernel limit, we should fall back to something else. 582 /* we hit the kernel limit, we should fall back to something else.
521 * we can either poll() a few times and hope for the best, 583 * we can either poll() a few times and hope for the best,
522 * poll always, or switch to epoll. 584 * poll always, or switch to epoll.
523 * since we use epoll anyways, go epoll. 585 * TODO: is this necessary with newer kernels?
524 */ 586 */
525 587
526 iouring_internal_destroy (EV_A); 588 iouring_internal_destroy (EV_A);
527 589
528 /* this should make it so that on return, we don'T call any uring functions */ 590 /* this should make it so that on return, we don't call any uring functions */
529 iouring_to_submit = 0; 591 iouring_to_submit = 0;
530 592
531 for (;;) 593 for (;;)
532 { 594 {
533 backend = epoll_init (EV_A_ 0); 595 backend = epoll_init (EV_A_ 0);
574 636
575static void 637static void
576iouring_poll (EV_P_ ev_tstamp timeout) 638iouring_poll (EV_P_ ev_tstamp timeout)
577{ 639{
578 /* if we have events, no need for extra syscalls, but we might have to queue events */ 640 /* if we have events, no need for extra syscalls, but we might have to queue events */
641 /* we also clar the timeout if there are outstanding fdchanges */
642 /* the latter should only happen if both the sq and cq are full, most likely */
643 /* because we have a lot of event sources that immediately complete */
644 /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
579 if (iouring_handle_cq (EV_A)) 645 if (iouring_handle_cq (EV_A) || fdchangecnt)
580 timeout = EV_TS_CONST (0.); 646 timeout = EV_TS_CONST (0.);
581 else 647 else
582 /* no events, so maybe wait for some */ 648 /* no events, so maybe wait for some */
583 iouring_tfd_update (EV_A_ timeout); 649 iouring_tfd_update (EV_A_ timeout);
584 650
585 /* only enter the kernel if we have something to submit, or we need to wait */ 651 /* only enter the kernel if we have something to submit, or we need to wait */
586 if (timeout || iouring_to_submit) 652 if (timeout || iouring_to_submit)
587 { 653 {
588 int res; 654 int res = iouring_enter (EV_A_ timeout);
589
590 EV_RELEASE_CB;
591
592 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
593 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
594 iouring_to_submit = 0;
595
596 EV_ACQUIRE_CB;
597 655
598 if (ecb_expect_false (res < 0)) 656 if (ecb_expect_false (res < 0))
599 if (errno == EINTR) 657 if (errno == EINTR)
600 /* ignore */; 658 /* ignore */;
659 else if (errno == EBUSY)
660 /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
601 else 661 else
602 ev_syserr ("(libev) iouring setup"); 662 ev_syserr ("(libev) iouring setup");
603 else 663 else
604 iouring_handle_cq (EV_A); 664 iouring_handle_cq (EV_A);
605 } 665 }
607 667
608inline_size 668inline_size
609int 669int
610iouring_init (EV_P_ int flags) 670iouring_init (EV_P_ int flags)
611{ 671{
612 if (!epoll_init (EV_A_ 0))
613 return 0;
614
615 iouring_entries = IOURING_INIT_ENTRIES; 672 iouring_entries = IOURING_INIT_ENTRIES;
616 iouring_max_entries = 0; 673 iouring_max_entries = 0;
617 674
618 if (iouring_internal_init (EV_A) < 0) 675 if (iouring_internal_init (EV_A) < 0)
619 { 676 {
620 iouring_internal_destroy (EV_A); 677 iouring_internal_destroy (EV_A);
621 return 0; 678 return 0;
622 } 679 }
623 680
624 ev_io_init (&iouring_epoll_w, iouring_epoll_cb, backend_fd, EV_READ);
625 ev_set_priority (&iouring_epoll_w, EV_MAXPRI);
626
627 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); 681 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ);
628 ev_set_priority (&iouring_tfd_w, EV_MAXPRI); 682 ev_set_priority (&iouring_tfd_w, EV_MINPRI);
629
630 ev_io_start (EV_A_ &iouring_epoll_w);
631 ev_unref (EV_A); /* watcher should not keep loop alive */
632
633 ev_io_start (EV_A_ &iouring_tfd_w); 683 ev_io_start (EV_A_ &iouring_tfd_w);
634 ev_unref (EV_A); /* watcher should not keep loop alive */ 684 ev_unref (EV_A); /* watcher should not keep loop alive */
635 685
636 backend_modify = iouring_modify; 686 backend_modify = iouring_modify;
637 backend_poll = iouring_poll; 687 backend_poll = iouring_poll;
642inline_size 692inline_size
643void 693void
644iouring_destroy (EV_P) 694iouring_destroy (EV_P)
645{ 695{
646 iouring_internal_destroy (EV_A); 696 iouring_internal_destroy (EV_A);
647 epoll_destroy (EV_A);
648} 697}
649 698

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines