xref: /aosp_15_r20/external/liburing/test/io_uring_setup.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * io_uring_setup.c
4  *
5  * Description: Unit tests for the io_uring_setup system call.
6  *
7  * Copyright 2019, Red Hat, Inc.
8  * Author: Jeff Moyer <[email protected]>
9  */
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/sysinfo.h>
17 #include "liburing.h"
18 
19 #include "../syscall.h"
20 
features_string(struct io_uring_params * p)21 char *features_string(struct io_uring_params *p)
22 {
23 	static char flagstr[64];
24 
25 	if (!p || !p->features)
26 		return "none";
27 
28 	if (p->features & ~IORING_FEAT_SINGLE_MMAP) {
29 		snprintf(flagstr, 64, "0x%.8x", p->features);
30 		return flagstr;
31 	}
32 
33 	if (p->features & IORING_FEAT_SINGLE_MMAP)
34 		strncat(flagstr, "IORING_FEAT_SINGLE_MMAP", 64 - strlen(flagstr));
35 
36 	return flagstr;
37 }
38 
39 /*
40  * Attempt the call with the given args.  Return 0 when expect matches
41  * the return value of the system call, 1 otherwise.
42  */
43 char *
flags_string(struct io_uring_params * p)44 flags_string(struct io_uring_params *p)
45 {
46 	static char flagstr[64];
47 	int add_pipe = 0;
48 
49 	memset(flagstr, 0, sizeof(flagstr));
50 
51 	if (!p || p->flags == 0)
52 		return "none";
53 
54 	/*
55 	 * If unsupported flags are present, just print the bitmask.
56 	 */
57 	if (p->flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
58 			 IORING_SETUP_SQ_AFF)) {
59 		snprintf(flagstr, 64, "0x%.8x", p->flags);
60 		return flagstr;
61 	}
62 
63 	if (p->flags & IORING_SETUP_IOPOLL) {
64 		strncat(flagstr, "IORING_SETUP_IOPOLL", 64 - strlen(flagstr));
65 		add_pipe = 1;
66 	}
67 	if (p->flags & IORING_SETUP_SQPOLL) {
68 		if (add_pipe)
69 			strncat(flagstr, "|", 64 - strlen(flagstr));
70 		else
71 			add_pipe = 1;
72 		strncat(flagstr, "IORING_SETUP_SQPOLL", 64 - strlen(flagstr));
73 	}
74 	if (p->flags & IORING_SETUP_SQ_AFF) {
75 		if (add_pipe)
76 			strncat(flagstr, "|", 64 - strlen(flagstr));
77 		strncat(flagstr, "IORING_SETUP_SQ_AFF", 64 - strlen(flagstr));
78 	}
79 
80 	return flagstr;
81 }
82 
83 char *
dump_resv(struct io_uring_params * p)84 dump_resv(struct io_uring_params *p)
85 {
86 	static char resvstr[4096];
87 
88 	if (!p)
89 		return "";
90 
91 	sprintf(resvstr, "0x%.8x 0x%.8x 0x%.8x", p->resv[0],
92 		p->resv[1], p->resv[2]);
93 
94 	return resvstr;
95 }
96 
97 /* bogus: setup returns a valid fd on success... expect can't predict the
98    fd we'll get, so this really only takes 1 parameter: error */
99 int
try_io_uring_setup(unsigned entries,struct io_uring_params * p,int expect,int error)100 try_io_uring_setup(unsigned entries, struct io_uring_params *p, int expect, int error)
101 {
102 	int ret, err;
103 
104 	ret = __sys_io_uring_setup(entries, p);
105 	if (ret != expect) {
106 		fprintf(stderr, "expected %d, got %d\n", expect, ret);
107 		/* if we got a valid uring, close it */
108 		if (ret > 0)
109 			close(ret);
110 		return 1;
111 	}
112 	err = errno;
113 	if (expect == -1 && error != err) {
114 		if (err == EPERM && geteuid() != 0) {
115 			printf("Needs root, not flagging as an error\n");
116 			return 0;
117 		}
118 		fprintf(stderr, "expected errno %d, got %d\n", error, err);
119 		return 1;
120 	}
121 
122 	return 0;
123 }
124 
125 int
main(int argc,char ** argv)126 main(int argc, char **argv)
127 {
128 	int fd;
129 	unsigned int status = 0;
130 	struct io_uring_params p;
131 
132 	if (argc > 1)
133 		return 0;
134 
135 	memset(&p, 0, sizeof(p));
136 	status |= try_io_uring_setup(0, &p, -1, EINVAL);
137 	status |= try_io_uring_setup(1, NULL, -1, EFAULT);
138 
139 	/* resv array is non-zero */
140 	memset(&p, 0, sizeof(p));
141 	p.resv[0] = p.resv[1] = p.resv[2] = 1;
142 	status |= try_io_uring_setup(1, &p, -1, EINVAL);
143 
144 	/* invalid flags */
145 	memset(&p, 0, sizeof(p));
146 	p.flags = ~0U;
147 	status |= try_io_uring_setup(1, &p, -1, EINVAL);
148 
149 	/* IORING_SETUP_SQ_AFF set but not IORING_SETUP_SQPOLL */
150 	memset(&p, 0, sizeof(p));
151 	p.flags = IORING_SETUP_SQ_AFF;
152 	status |= try_io_uring_setup(1, &p, -1, EINVAL);
153 
154 	/* attempt to bind to invalid cpu */
155 	memset(&p, 0, sizeof(p));
156 	p.flags = IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
157 	p.sq_thread_cpu = get_nprocs_conf();
158 	status |= try_io_uring_setup(1, &p, -1, EINVAL);
159 
160 	/* I think we can limit a process to a set of cpus.  I assume
161 	 * we shouldn't be able to setup a kernel thread outside of that.
162 	 * try to do that. (task->cpus_allowed) */
163 
164 	/* read/write on io_uring_fd */
165 	memset(&p, 0, sizeof(p));
166 	fd = __sys_io_uring_setup(1, &p);
167 	if (fd < 0) {
168 		fprintf(stderr, "io_uring_setup failed with %d, expected success\n",
169 		       errno);
170 		status = 1;
171 	} else {
172 		char buf[4096];
173 		int ret;
174 		ret = read(fd, buf, 4096);
175 		if (ret >= 0) {
176 			fprintf(stderr, "read from io_uring fd succeeded.  expected fail\n");
177 			status = 1;
178 		}
179 	}
180 
181 	if (!status)
182 		return 0;
183 
184 	fprintf(stderr, "FAIL\n");
185 	return -1;
186 }
187