aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/poll-mshot-overflow.c
blob: 94a82bd64de5f9043bc1718b93a7e14691f1b1b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "../config-host.h"
// SPDX-License-Identifier: MIT

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <poll.h>
#include <sys/wait.h>

#include "liburing.h"
#include "helpers.h"

static int check_final_cqe(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	int count = 0;
	bool signalled_no_more = false;

	while (!io_uring_peek_cqe(ring, &cqe)) {
		if (cqe->user_data == 1) {
			count++;
			if (signalled_no_more) {
				fprintf(stderr, "signalled no more!\n");
				return T_EXIT_FAIL;
			}
			if (!(cqe->flags & IORING_CQE_F_MORE))
				signalled_no_more = true;
		} else if (cqe->user_data != 3) {
			fprintf(stderr, "%d: got unexpected %d\n", count, (int)cqe->user_data);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);
	}

	if (!count) {
		fprintf(stderr, "no cqe\n");
		return T_EXIT_FAIL;
	}

	return T_EXIT_PASS;
}

static int test(bool defer_taskrun)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	int pipe1[2];
	int ret, i;

	if (pipe(pipe1) != 0) {
		perror("pipe");
		return T_EXIT_FAIL;
	}

	struct io_uring_params params = {
		/* cheat using SINGLE_ISSUER existence to know if this behaviour
		 * is updated
		 */
		.flags = IORING_SETUP_CQSIZE | IORING_SETUP_SINGLE_ISSUER,
		.cq_entries = 2
	};

	if (defer_taskrun)
		params.flags |= IORING_SETUP_SINGLE_ISSUER |
				IORING_SETUP_DEFER_TASKRUN;

	ret = io_uring_queue_init_params(2, &ring, &params);
	if (ret)
		return T_EXIT_SKIP;

	sqe = io_uring_get_sqe(&ring);
	if (!sqe) {
		fprintf(stderr, "get sqe failed\n");
		return T_EXIT_FAIL;
	}
	io_uring_prep_poll_multishot(sqe, pipe1[0], POLLIN);
	io_uring_sqe_set_data64(sqe, 1);

	if (io_uring_cq_ready(&ring)) {
		fprintf(stderr, "unexpected cqe\n");
		return T_EXIT_FAIL;
	}

	for (i = 0; i < 2; i++) {
		sqe = io_uring_get_sqe(&ring);
		io_uring_prep_nop(sqe);
		io_uring_sqe_set_data64(sqe, 2);
		io_uring_submit(&ring);
	}

	do {
		errno = 0;
		ret = write(pipe1[1], "foo", 3);
	} while (ret == -1 && errno == EINTR);

	if (ret <= 0) {
		fprintf(stderr, "write failed: %d\n", errno);
		return T_EXIT_FAIL;
	}

	/* should have 2 cqe + 1 overflow now, so take out two cqes */
	for (i = 0; i < 2; i++) {
		if (io_uring_peek_cqe(&ring, &cqe)) {
			fprintf(stderr, "unexpectedly no cqe\n");
			return T_EXIT_FAIL;
		}
		if (cqe->user_data != 2) {
			fprintf(stderr, "unexpected user_data\n");
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(&ring, cqe);
	}

	/* make sure everything is processed */
	io_uring_get_events(&ring);

	/* now remove the poll */
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_poll_remove(sqe, 1);
	io_uring_sqe_set_data64(sqe, 3);
	ret = io_uring_submit(&ring);

	if (ret != 1) {
		fprintf(stderr, "bad poll remove\n");
		return T_EXIT_FAIL;
	}

	ret = check_final_cqe(&ring);

	close(pipe1[0]);
	close(pipe1[1]);
	io_uring_queue_exit(&ring);

	return ret;
}

static int test_downgrade(bool support_defer)
{
	struct io_uring_cqe cqes[128];
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	int fds[2];
	int ret, i, cqe_count, tmp = 0, more_cqe_count;

	if (pipe(fds) != 0) {
		perror("pipe");
		return -1;
	}

	struct io_uring_params params = {
		.flags = IORING_SETUP_CQSIZE,
		.cq_entries = 2
	};

	ret = io_uring_queue_init_params(2, &ring, &params);
	if (ret) {
		fprintf(stderr, "queue init: %d\n", ret);
		return -1;
	}

	sqe = io_uring_get_sqe(&ring);
	if (!sqe) {
		fprintf(stderr, "get sqe failed\n");
		return -1;
	}
	io_uring_prep_poll_multishot(sqe, fds[0], POLLIN);
	io_uring_sqe_set_data64(sqe, 1);
	io_uring_submit(&ring);

	for (i = 0; i < 8; i++) {
		ret = write(fds[1], &tmp, sizeof(tmp));
		if (ret != sizeof(tmp)) {
			perror("write");
			return -1;
		}
		ret = read(fds[0], &tmp, sizeof(tmp));
		if (ret != sizeof(tmp)) {
			perror("read");
			return -1;
		}
	}

	cqe_count = 0;
	while (!io_uring_peek_cqe(&ring, &cqe)) {
		cqes[cqe_count++] = *cqe;
		io_uring_cqe_seen(&ring, cqe);
	}

	/* Some kernels might allow overflows to poll,
	 * but if they didn't it should stop the MORE flag
	 */
	if (cqe_count < 3) {
		fprintf(stderr, "too few cqes: %d\n", cqe_count);
		return -1;
	} else if (cqe_count == 8) {
		more_cqe_count = cqe_count;
		/* downgrade only available since support_defer */
		if (support_defer) {
			fprintf(stderr, "did not downgrade on overflow\n");
			return -1;
		}
	} else {
		more_cqe_count = cqe_count - 1;
		cqe = &cqes[cqe_count - 1];
		if (cqe->flags & IORING_CQE_F_MORE) {
			fprintf(stderr, "incorrect MORE flag %x\n", cqe->flags);
			return -1;
		}
	}

	for (i = 0; i < more_cqe_count; i++) {
		cqe = &cqes[i];
		if (!(cqe->flags & IORING_CQE_F_MORE)) {
			fprintf(stderr, "missing MORE flag\n");
			return -1;
		}
		if (cqe->res < 0) {
			fprintf(stderr, "bad res: %d\n", cqe->res);
			return -1;
		}
	}

	close(fds[0]);
	close(fds[1]);
	io_uring_queue_exit(&ring);
	return 0;
}

int main(int argc, char *argv[])
{
	int ret;
	bool support_defer;

	if (argc > 1)
		return T_EXIT_SKIP;

	support_defer = t_probe_defer_taskrun();
	ret = test_downgrade(support_defer);
	if (ret) {
		fprintf(stderr, "%s: test_downgrade(%d) failed\n", argv[0], support_defer);
		return T_EXIT_FAIL;
	}

	ret = test(false);
	if (ret == T_EXIT_SKIP)
		return ret;
	if (ret != T_EXIT_PASS) {
		fprintf(stderr, "%s: test(false) failed\n", argv[0]);
		return ret;
	}

	if (support_defer) {
		ret = test(true);
		if (ret != T_EXIT_PASS) {
			fprintf(stderr, "%s: test(true) failed\n", argv[0]);
			return ret;
		}
	}

	return ret;
}