• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test SQPOLL with IORING_SETUP_ATTACH_WQ and closing of
4  * the original ring descriptor.
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/types.h>
13 #include <sys/poll.h>
14 #include <sys/eventfd.h>
15 #include <sys/resource.h>
16 
17 #include "helpers.h"
18 #include "liburing.h"
19 
20 #define FILE_SIZE	(128 * 1024 * 1024)
21 #define BS		4096
22 #define BUFFERS		64
23 
24 #define NR_RINGS	4
25 
26 static struct iovec *vecs;
27 static struct io_uring rings[NR_RINGS];
28 
wait_io(struct io_uring * ring,int nr_ios)29 static int wait_io(struct io_uring *ring, int nr_ios)
30 {
31 	struct io_uring_cqe *cqe;
32 
33 	while (nr_ios) {
34 		io_uring_wait_cqe(ring, &cqe);
35 		if (cqe->res != BS) {
36 			fprintf(stderr, "Unexpected ret %d\n", cqe->res);
37 			return 1;
38 		}
39 		io_uring_cqe_seen(ring, cqe);
40 		nr_ios--;
41 	}
42 
43 	return 0;
44 }
45 
queue_io(struct io_uring * ring,int fd,int nr_ios)46 static int queue_io(struct io_uring *ring, int fd, int nr_ios)
47 {
48 	unsigned long off;
49 	int i;
50 
51 	i = 0;
52 	off = 0;
53 	while (nr_ios) {
54 		struct io_uring_sqe *sqe;
55 
56 		sqe = io_uring_get_sqe(ring);
57 		if (!sqe)
58 			break;
59 		io_uring_prep_read(sqe, fd, vecs[i].iov_base, vecs[i].iov_len, off);
60 		nr_ios--;
61 		i++;
62 		off += BS;
63 	}
64 
65 	io_uring_submit(ring);
66 	return i;
67 }
68 
do_io(int fd,int ring_start,int ring_end)69 static int do_io(int fd, int ring_start, int ring_end)
70 {
71 	int i, rets[NR_RINGS];
72 	unsigned ios = 0;
73 
74 	while (ios < 32) {
75 		for (i = ring_start; i < ring_end; i++) {
76 			int ret = queue_io(&rings[i], fd, BUFFERS);
77 			if (ret < 0)
78 				goto err;
79 			rets[i] = ret;
80 		}
81 		for (i = ring_start; i < ring_end; i++) {
82 			if (wait_io(&rings[i], rets[i]))
83 				goto err;
84 		}
85 		ios += BUFFERS;
86 	}
87 
88 	return 0;
89 err:
90 	return 1;
91 }
92 
test(int fd,int do_dup_and_close,int close_ring)93 static int test(int fd, int do_dup_and_close, int close_ring)
94 {
95 	int i, ret, ring_fd;
96 
97 	for (i = 0; i < NR_RINGS; i++) {
98 		struct io_uring_params p = { };
99 
100 		p.flags = IORING_SETUP_SQPOLL;
101 		p.sq_thread_idle = 100;
102 		if (i) {
103 			p.wq_fd = rings[0].ring_fd;
104 			p.flags |= IORING_SETUP_ATTACH_WQ;
105 		}
106 		ret = io_uring_queue_init_params(BUFFERS, &rings[i], &p);
107 		if (ret) {
108 			fprintf(stderr, "queue_init: %d/%d\n", ret, i);
109 			goto err;
110 		}
111 		/* no sharing for non-fixed either */
112 		if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
113 			fprintf(stdout, "No SQPOLL sharing, skipping\n");
114 			return 0;
115 		}
116 	}
117 
118 	/* test all rings */
119 	if (do_io(fd, 0, NR_RINGS))
120 		goto err;
121 
122 	/* dup and close original ring fd */
123 	ring_fd = dup(rings[0].ring_fd);
124 	if (close_ring)
125 		close(rings[0].ring_fd);
126 	rings[0].ring_fd = ring_fd;
127 	if (do_dup_and_close)
128 		goto done;
129 
130 	/* test all but closed one */
131 	if (do_io(fd, 1, NR_RINGS))
132 		goto err;
133 
134 	/* test closed one */
135 	if (do_io(fd, 0, 1))
136 		goto err;
137 
138 	/* make sure thread is idle so we enter the kernel */
139 	usleep(200000);
140 
141 	/* test closed one */
142 	if (do_io(fd, 0, 1))
143 		goto err;
144 
145 
146 done:
147 	for (i = 0; i < NR_RINGS; i++)
148 		io_uring_queue_exit(&rings[i]);
149 
150 	return 0;
151 err:
152 	return 1;
153 }
154 
main(int argc,char * argv[])155 int main(int argc, char *argv[])
156 {
157 	char *fname;
158 	int ret, fd;
159 
160 	if (argc > 1) {
161 		fname = argv[1];
162 	} else {
163 		fname = ".basic-rw";
164 		t_create_file(fname, FILE_SIZE);
165 	}
166 
167 	vecs = t_create_buffers(BUFFERS, BS);
168 
169 	fd = open(fname, O_RDONLY | O_DIRECT);
170 	if (fd < 0) {
171 		perror("open");
172 		return -1;
173 	}
174 
175 	ret = test(fd, 0, 0);
176 	if (ret) {
177 		fprintf(stderr, "test 0 0 failed\n");
178 		goto err;
179 	}
180 
181 	ret = test(fd, 0, 1);
182 	if (ret) {
183 		fprintf(stderr, "test 0 1 failed\n");
184 		goto err;
185 	}
186 
187 
188 	ret = test(fd, 1, 0);
189 	if (ret) {
190 		fprintf(stderr, "test 1 0 failed\n");
191 		goto err;
192 	}
193 
194 	if (fname != argv[1])
195 		unlink(fname);
196 	return 0;
197 err:
198 	if (fname != argv[1])
199 		unlink(fname);
200 	return 1;
201 }
202