xref: /aosp_15_r20/external/liburing/test/ce593a6c480a.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Test 5.7 regression with task_work not being run while a task is
4  * waiting on another event in the kernel.
5  */
6 #include <errno.h>
7 #include <poll.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/eventfd.h>
11 #include <unistd.h>
12 #include <pthread.h>
13 #include "liburing.h"
14 #include "helpers.h"
15 
16 static int use_sqpoll = 0;
17 
notify_fd(int fd)18 void notify_fd(int fd)
19 {
20 	char buf[8] = {0, 0, 0, 0, 0, 0, 1};
21 	int ret;
22 
23 	ret = write(fd, &buf, 8);
24 	if (ret < 0)
25 		perror("write");
26 }
27 
delay_set_fd_from_thread(void * data)28 void *delay_set_fd_from_thread(void *data)
29 {
30 	int fd = (intptr_t) data;
31 
32 	sleep(1);
33 	notify_fd(fd);
34 	return NULL;
35 }
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39 	struct io_uring_params p = {};
40 	struct io_uring ring;
41 	int loop_fd, other_fd;
42 	struct io_uring_sqe *sqe;
43 	struct io_uring_cqe *cqe = NULL;
44 	int ret, use_fd;
45 	char buf[8] = {0, 0, 0, 0, 0, 0, 1};
46 	pthread_t tid;
47 
48 	if (argc > 1)
49 		return 0;
50 
51 	/* Create an eventfd to be registered with the loop to be
52 	 * notified of events being ready
53 	 */
54 	loop_fd = eventfd(0, EFD_CLOEXEC);
55 	if (loop_fd == -1) {
56 		fprintf(stderr, "eventfd errno=%d\n", errno);
57 		return 1;
58 	}
59 
60 	/* Create an eventfd that can create events */
61 	use_fd = other_fd = eventfd(0, EFD_CLOEXEC);
62 	if (other_fd == -1) {
63 		fprintf(stderr, "eventfd errno=%d\n", errno);
64 		return 1;
65 	}
66 
67 	if (use_sqpoll)
68 		p.flags = IORING_SETUP_SQPOLL;
69 
70 	/* Setup the ring with a registered event fd to be notified on events */
71 	ret = t_create_ring_params(8, &ring, &p);
72 	if (ret == T_SETUP_SKIP)
73 		return 0;
74 	else if (ret < 0)
75 		return ret;
76 
77 	ret = io_uring_register_eventfd(&ring, loop_fd);
78 	if (ret < 0) {
79 		fprintf(stderr, "register_eventfd=%d\n", ret);
80 		return 1;
81 	}
82 
83 	if (use_sqpoll) {
84 		ret = io_uring_register_files(&ring, &other_fd, 1);
85 		if (ret < 0) {
86 			fprintf(stderr, "register_files=%d\n", ret);
87 			return 1;
88 		}
89 		use_fd = 0;
90 	}
91 
92 	/* Submit a poll operation to wait on an event in other_fd */
93 	sqe = io_uring_get_sqe(&ring);
94 	io_uring_prep_poll_add(sqe, use_fd, POLLIN);
95 	sqe->user_data = 1;
96 	if (use_sqpoll)
97 		sqe->flags |= IOSQE_FIXED_FILE;
98 	ret = io_uring_submit(&ring);
99 	if (ret != 1) {
100 		fprintf(stderr, "submit=%d\n", ret);
101 		return 1;
102 	}
103 
104 	/*
105 	 * CASE 3: Hangs forever in Linux 5.7.5; Works in Linux 5.6.0 When this
106 	 * code is uncommented, we don't se a notification on other_fd until
107 	 * _after_ we have started the read on loop_fd. In that case, the read() on
108 	 * loop_fd seems to hang forever.
109 	*/
110     	pthread_create(&tid, NULL, delay_set_fd_from_thread,
111 			(void*) (intptr_t) other_fd);
112 
113 	/* Wait on the event fd for an event to be ready */
114 	ret = read(loop_fd, buf, 8);
115 	if (ret < 0) {
116 		perror("read");
117 		return 1;
118 	} else if (ret != 8) {
119 		fprintf(stderr, "Odd-sized eventfd read: %d\n", ret);
120 		return 1;
121 	}
122 
123 
124 	ret = io_uring_wait_cqe(&ring, &cqe);
125 	if (ret) {
126 		fprintf(stderr, "wait_cqe=%d\n", ret);
127 		return ret;
128 	}
129 	if (cqe->res < 0) {
130 		fprintf(stderr, "cqe->res=%d\n", cqe->res);
131 		return 1;
132 	}
133 
134 	io_uring_cqe_seen(&ring, cqe);
135 	return 0;
136 }
137