• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(ret >= 0);
39 	ret = ftruncate(fd, 4096);
40 	assert(!ret);
41 
42 	ret = t_create_ring(4, &ring, 0);
43 	if (ret == T_SETUP_SKIP)
44 		return 0;
45 	else if (ret < 0)
46 		return 1;
47 
48 	/* REQ1 */
49 	sqe = io_uring_get_sqe(&ring);
50 	io_uring_prep_readv(sqe, pipe1[0], &vec, 1, 0);
51 	sqe->user_data = 1;
52 
53 	/* REQ2 */
54 	sqe = io_uring_get_sqe(&ring);
55 	io_uring_prep_readv(sqe, fd, &vec, 1, 4096);
56 	sqe->user_data = 2;
57 
58 	ret = io_uring_submit(&ring);
59 	assert(ret == 2);
60 
61 	ret = io_uring_wait_cqe(&ring, &cqe);
62 	assert(!ret);
63 	assert(cqe->res == 0);
64 	assert(cqe->user_data == 2);
65 	io_uring_cqe_seen(&ring, cqe);
66 
67 	/*
68 	 * REQ3
69 	 * Prepare request adjacent to previous one, so merge logic may want to
70 	 * link it to previous request, but because of a bug in merge logic
71 	 * it may be merged with <REQ1> request
72 	 */
73 	sqe = io_uring_get_sqe(&ring);
74 	io_uring_prep_readv(sqe, fd, &vec, 1, 2048);
75 	sqe->user_data = 3;
76 
77 	ret = io_uring_submit(&ring);
78 	assert(ret == 1);
79 
80 	/*
81 	 * Read may stuck because of bug there request was be incorrecly
82 	 * merged with <REQ1> request
83 	 */
84 	ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
85 	if (ret == -ETIME) {
86 		printf("TEST_FAIL: readv req3 stuck\n");
87 		return 1;
88 	}
89 	assert(!ret);
90 
91 	assert(cqe->res == 2048);
92 	assert(cqe->user_data == 3);
93 
94 	io_uring_cqe_seen(&ring, cqe);
95 	io_uring_queue_exit(&ring);
96 	return 0;
97 }
98