1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: test if issuing IO from thread and immediately exiting will
4 * proceed correctly.
5 *
6 * Original test case from: https://github.com/axboe/liburing/issues/582
7 */
8 #include <unistd.h>
9 #include <pthread.h>
10 #include <sys/timerfd.h>
11 #include <string.h>
12 #include <stdio.h>
13
14 #include "liburing.h"
15 #include "helpers.h"
16
17 static int no_iopoll;
18
19 struct data {
20 struct io_uring *ring;
21 int timer_fd1;
22 int timer_fd2;
23 uint64_t buf1;
24 uint64_t buf2;
25 };
26
submit(void * data)27 static void *submit(void *data)
28 {
29 struct io_uring_sqe *sqe;
30 struct data *d = data;
31 int ret;
32
33 sqe = io_uring_get_sqe(d->ring);
34 io_uring_prep_read(sqe, d->timer_fd1, &d->buf1, sizeof(d->buf1), 0);
35
36 sqe = io_uring_get_sqe(d->ring);
37 io_uring_prep_read(sqe, d->timer_fd2, &d->buf2, sizeof(d->buf2), 0);
38
39 ret = io_uring_submit(d->ring);
40 if (ret != 2) {
41 struct io_uring_cqe *cqe;
42
43 /*
44 * Kernels without submit-all-on-error behavior will
45 * fail submitting all, check if that's the case and
46 * don't error
47 */
48 ret = io_uring_peek_cqe(d->ring, &cqe);
49 if (!ret && cqe->res == -EOPNOTSUPP) {
50 no_iopoll = 1;
51 return NULL;
52 }
53 return (void *) (uintptr_t) 1;
54 }
55
56 /* Exit suddenly. */
57 return NULL;
58 }
59
test(int flags)60 static int test(int flags)
61 {
62 struct io_uring_params params = { .flags = flags, };
63 struct io_uring ring;
64 struct data d = { .ring = &ring, };
65 pthread_t thread;
66 void *res;
67 int ret;
68
69 ret = t_create_ring_params(8, &ring, ¶ms);
70 if (ret == T_SETUP_SKIP)
71 return 0;
72 else if (ret != T_SETUP_OK)
73 return 1;
74
75 d.timer_fd1 = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
76 if (d.timer_fd1 < 0) {
77 perror("timerfd_create");
78 return 1;
79 }
80 d.timer_fd2 = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
81 if (d.timer_fd2 < 0) {
82 perror("timerfd_create");
83 return 1;
84 }
85
86 pthread_create(&thread, NULL, submit, &d);
87 pthread_join(thread, &res);
88
89 /** Wait for completions and do stuff ... **/
90
91 io_uring_queue_exit(&ring);
92
93 close(d.timer_fd1);
94 close(d.timer_fd2);
95 return !!res;
96 }
97
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100 int ret, i;
101
102 for (i = 0; i < 1000; i++) {
103 ret = test(0);
104 if (ret) {
105 fprintf(stderr, "Test failed\n");
106 return ret;
107 }
108 }
109
110 for (i = 0; i < 1000; i++) {
111 ret = test(IORING_SETUP_IOPOLL);
112 if (ret) {
113 fprintf(stderr, "Test IOPOLL failed loop %d\n", ret);
114 return ret;
115 }
116 if (no_iopoll)
117 break;
118 }
119
120 for (i = 0; i < 100; i++) {
121 ret = test(IORING_SETUP_SQPOLL);
122 if (ret) {
123 fprintf(stderr, "Test SQPOLL failed\n");
124 return ret;
125 }
126 }
127
128 return 0;
129 }
130