xref: /aosp_15_r20/external/liburing/test/rw_merge_test.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Regression test for incorrect async_list io_should_merge() logic
4  * Bug was fixed in 5.5 by (commit: 561fb04 io_uring: replace workqueue usage with io-wq")
5  * Affects 5.4 lts branch, at least 5.4.106 is affected.
6  */
7 #include <stdio.h>
8 #include <errno.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <assert.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 
15 #include "liburing.h"
16 #include "helpers.h"
17 
main(int argc,char * argv[])18 int main(int argc, char *argv[])
19 {
20 	struct io_uring_sqe *sqe;
21 	struct io_uring_cqe *cqe;
22 	struct io_uring ring;
23 	int ret, fd, pipe1[2];
24 	char buf[4096];
25 	struct iovec vec = {
26 		.iov_base = buf,
27 		.iov_len = sizeof(buf)
28 	};
29 	struct __kernel_timespec ts = {.tv_sec = 3, .tv_nsec = 0};
30 
31 	if (argc > 1)
32 		return 0;
33 
34 	ret = pipe(pipe1);
35 	assert(!ret);
36 
37 	fd = open("testfile", O_RDWR | O_CREAT, 0644);
38 	assert(fd >= 0);
39 	unlink("testfile");
40 	ret = ftruncate(fd, 4096);
41 	assert(!ret);
42 
43 	ret = t_create_ring(4, &ring, 0);
44 	if (ret == T_SETUP_SKIP)
45 		return 0;
46 	else if (ret < 0)
47 		return 1;
48 
49 	/* REQ1 */
50 	sqe = io_uring_get_sqe(&ring);
51 	io_uring_prep_readv(sqe, pipe1[0], &vec, 1, 0);
52 	sqe->user_data = 1;
53 
54 	/* REQ2 */
55 	sqe = io_uring_get_sqe(&ring);
56 	io_uring_prep_readv(sqe, fd, &vec, 1, 4096);
57 	sqe->user_data = 2;
58 
59 	ret = io_uring_submit(&ring);
60 	assert(ret == 2);
61 
62 	ret = io_uring_wait_cqe(&ring, &cqe);
63 	assert(!ret);
64 	assert(cqe->res == 0);
65 	assert(cqe->user_data == 2);
66 	io_uring_cqe_seen(&ring, cqe);
67 
68 	/*
69 	 * REQ3
70 	 * Prepare request adjacent to previous one, so merge logic may want to
71 	 * link it to previous request, but because of a bug in merge logic
72 	 * it may be merged with <REQ1> request
73 	 */
74 	sqe = io_uring_get_sqe(&ring);
75 	io_uring_prep_readv(sqe, fd, &vec, 1, 2048);
76 	sqe->user_data = 3;
77 
78 	ret = io_uring_submit(&ring);
79 	assert(ret == 1);
80 
81 	/*
82 	 * Read may stuck because of bug there request was be incorrecly
83 	 * merged with <REQ1> request
84 	 */
85 	ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
86 	if (ret == -ETIME) {
87 		printf("TEST_FAIL: readv req3 stuck\n");
88 		return 1;
89 	}
90 	assert(!ret);
91 
92 	assert(cqe->res == 2048);
93 	assert(cqe->user_data == 3);
94 
95 	io_uring_cqe_seen(&ring, cqe);
96 	io_uring_queue_exit(&ring);
97 	return 0;
98 }
99