summaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/cancel-fd-userdata.c
blob: 955b09af27c3e0cad1610b3bd87abef8ca34d3fd (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: Test IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_USERDATA
 *
 * Tests that combining CANCEL_FD and CANCEL_USERDATA correctly matches
 * requests by both file descriptor and user_data. These share a union
 * in the kernel (io_cancel_data.data / io_cancel_data.file), so this
 * exercises that the union is handled correctly.
 */
#include <stdio.h>
#include <unistd.h>
#include <poll.h>

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

static int cancel_all(struct io_uring *ring, int fd, int fixed, int nr)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int i, ret;

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
	sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
	if (fixed)
		sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD_FIXED;
	sqe->fd = fd;
	sqe->user_data = 200;

	ret = io_uring_submit(ring);
	if (ret != 1)
		return 1;

	for (i = 0; i < nr + 1; i++) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret)
			return 1;
		io_uring_cqe_seen(ring, cqe);
	}
	return 0;
}

/*
 * Submit multiple polls on the same fd with different user_data values.
 * Cancel with CANCEL_FD | CANCEL_USERDATA targeting a specific user_data.
 * Only the request matching both fd AND user_data should be canceled.
 */
static int test_cancel_fd_userdata(struct io_uring *ring, int *fd, int fixed)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret, i, __fd = fd[0];
	int target_ud = 2;
	int canceled = 0;

	if (fixed) {
		ret = io_uring_register_files(ring, fd, 2);
		if (ret) {
			fprintf(stderr, "file register: %d\n", ret);
			return T_EXIT_FAIL;
		}
		__fd = 0;
	}

	/* Submit 4 polls on the same fd, with user_data 1..4 */
	for (i = 0; i < 4; i++) {
		sqe = io_uring_get_sqe(ring);
		if (!sqe) {
			fprintf(stderr, "get sqe failed\n");
			return T_EXIT_FAIL;
		}
		io_uring_prep_poll_add(sqe, __fd, POLLIN);
		sqe->user_data = i + 1;
		if (fixed)
			sqe->flags |= IOSQE_FIXED_FILE;
	}

	ret = io_uring_submit(ring);
	if (ret != 4) {
		fprintf(stderr, "submit: %d\n", ret);
		return T_EXIT_FAIL;
	}

	/* Cancel only the request with user_data == target_ud on this fd */
	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "get sqe failed\n");
		return T_EXIT_FAIL;
	}

	io_uring_prep_cancel(sqe, 0, 0);
	sqe->cancel_flags = IORING_ASYNC_CANCEL_FD |
			    IORING_ASYNC_CANCEL_USERDATA;
	if (fixed)
		sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD_FIXED;
	sqe->fd = __fd;
	sqe->addr = target_ud;
	sqe->user_data = 100;

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "cancel submit: %d\n", ret);
		return T_EXIT_FAIL;
	}

	/*
	 * Get the cancel CQE first. If the kernel doesn't support these
	 * cancel flags, it will return -EINVAL and we skip the test.
	 */
	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stderr, "wait: %d\n", ret);
		return T_EXIT_FAIL;
	}

	/*
	 * The cancel CQE may arrive before or after the canceled poll CQE.
	 * If we got the cancel result first, check it. If we got a poll
	 * completion first, consume it and get the cancel result next.
	 */
	if (cqe->user_data == 100) {
		if (cqe->res == -EINVAL) {
			io_uring_cqe_seen(ring, cqe);
			cancel_all(ring, __fd, fixed, 4);
			if (fixed)
				io_uring_unregister_files(ring);
			return T_EXIT_SKIP;
		}
		if (cqe->res < 0) {
			fprintf(stderr, "cancel failed: %d\n", cqe->res);
			io_uring_cqe_seen(ring, cqe);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);

		/* Now get the canceled poll CQE */
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait: %d\n", ret);
			return T_EXIT_FAIL;
		}
		if (cqe->user_data != target_ud || cqe->res != -ECANCELED) {
			fprintf(stderr, "unexpected ud=%lu res=%d\n",
				(unsigned long)cqe->user_data, cqe->res);
			io_uring_cqe_seen(ring, cqe);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);
		canceled++;
	} else if (cqe->user_data == target_ud) {
		if (cqe->res != -ECANCELED) {
			fprintf(stderr, "poll ud=%d res=%d\n",
				target_ud, cqe->res);
			io_uring_cqe_seen(ring, cqe);
			return T_EXIT_FAIL;
		}
		canceled++;
		io_uring_cqe_seen(ring, cqe);

		/* Now get the cancel result CQE */
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait: %d\n", ret);
			return T_EXIT_FAIL;
		}
		if (cqe->user_data != 100 || cqe->res < 0) {
			fprintf(stderr, "cancel ud=%lu res=%d\n",
				(unsigned long)cqe->user_data, cqe->res);
			io_uring_cqe_seen(ring, cqe);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);
	} else {
		fprintf(stderr, "unexpected user_data %lu res %d\n",
			(unsigned long)cqe->user_data, cqe->res);
		io_uring_cqe_seen(ring, cqe);
		return T_EXIT_FAIL;
	}

	if (canceled != 1) {
		fprintf(stderr, "expected 1 canceled, got %d\n", canceled);
		return T_EXIT_FAIL;
	}

	/* The other 3 polls should still be pending - cancel them */
	if (cancel_all(ring, __fd, fixed, 3)) {
		fprintf(stderr, "cleanup failed\n");
		return T_EXIT_FAIL;
	}

	if (fixed)
		io_uring_unregister_files(ring);

	return T_EXIT_PASS;
}

/*
 * Submit polls on two different fds with the same user_data.
 * Cancel with CANCEL_FD | CANCEL_USERDATA should only match the
 * request on the right fd with the right user_data.
 */
static int test_cancel_fd_userdata_two_fds(struct io_uring *ring, int *fd1,
					   int *fd2)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret, i;
	int canceled = 0;

	/* Poll on fd1 with user_data=1 */
	sqe = io_uring_get_sqe(ring);
	io_uring_prep_poll_add(sqe, fd1[0], POLLIN);
	sqe->user_data = 1;

	/* Poll on fd2 with user_data=1 (same user_data, different fd) */
	sqe = io_uring_get_sqe(ring);
	io_uring_prep_poll_add(sqe, fd2[0], POLLIN);
	sqe->user_data = 1;

	/* Poll on fd1 with user_data=2 (same fd, different user_data) */
	sqe = io_uring_get_sqe(ring);
	io_uring_prep_poll_add(sqe, fd1[0], POLLIN);
	sqe->user_data = 2;

	ret = io_uring_submit(ring);
	if (ret != 3) {
		fprintf(stderr, "submit: %d\n", ret);
		return T_EXIT_FAIL;
	}

	/*
	 * Cancel: fd=fd1[0], user_data=1. Should only cancel the first
	 * poll (fd1, ud=1), not the second (fd2, ud=1) or third (fd1, ud=2).
	 */
	sqe = io_uring_get_sqe(ring);
	io_uring_prep_cancel(sqe, 0, 0);
	sqe->cancel_flags = IORING_ASYNC_CANCEL_FD |
			    IORING_ASYNC_CANCEL_USERDATA;
	sqe->fd = fd1[0];
	sqe->addr = 1;
	sqe->user_data = 100;

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "cancel submit: %d\n", ret);
		return T_EXIT_FAIL;
	}

	for (i = 0; i < 2; i++) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait: %d\n", ret);
			return T_EXIT_FAIL;
		}
		if (cqe->user_data == 100) {
			if (cqe->res < 0) {
				fprintf(stderr, "cancel res: %d\n", cqe->res);
				io_uring_cqe_seen(ring, cqe);
				return T_EXIT_FAIL;
			}
		} else if (cqe->user_data == 1) {
			if (cqe->res != -ECANCELED) {
				fprintf(stderr, "poll res: %d\n", cqe->res);
				io_uring_cqe_seen(ring, cqe);
				return T_EXIT_FAIL;
			}
			canceled++;
		} else {
			fprintf(stderr, "unexpected ud=%lu res=%d\n",
				(unsigned long)cqe->user_data, cqe->res);
			io_uring_cqe_seen(ring, cqe);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);
	}

	if (canceled != 1) {
		fprintf(stderr, "expected 1 cancel, got %d\n", canceled);
		return T_EXIT_FAIL;
	}

	/* Clean up: cancel all remaining */
	sqe = io_uring_get_sqe(ring);
	io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ANY |
				     IORING_ASYNC_CANCEL_ALL);
	sqe->user_data = 200;

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "cleanup submit: %d\n", ret);
		return T_EXIT_FAIL;
	}

	for (i = 0; i < 3; i++) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret) {
			fprintf(stderr, "cleanup wait: %d\n", ret);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);
	}

	return T_EXIT_PASS;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	int ret, fd[2], fd2[2];

	if (argc > 1)
		return T_EXIT_SKIP;

	if (pipe(fd) < 0) {
		perror("pipe");
		return T_EXIT_FAIL;
	}

	ret = io_uring_queue_init(8, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup: %d\n", ret);
		return T_EXIT_FAIL;
	}

	ret = test_cancel_fd_userdata(&ring, fd, 0);
	if (ret == T_EXIT_SKIP)
		return T_EXIT_SKIP;
	if (ret) {
		fprintf(stderr, "test normal fd failed\n");
		return T_EXIT_FAIL;
	}

	ret = test_cancel_fd_userdata(&ring, fd, 1);
	if (ret == T_EXIT_SKIP)
		return T_EXIT_SKIP;
	if (ret) {
		fprintf(stderr, "test fixed fd failed\n");
		return T_EXIT_FAIL;
	}

	if (pipe(fd2) < 0) {
		perror("pipe");
		return T_EXIT_FAIL;
	}

	ret = test_cancel_fd_userdata_two_fds(&ring, fd, fd2);
	if (ret) {
		fprintf(stderr, "test two fds failed\n");
		return ret;
	}

	close(fd2[0]);
	close(fd2[1]);
	close(fd[0]);
	close(fd[1]);
	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
}