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 <jmoyer@redhat.com>
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, __errno;
103
104 printf("io_uring_setup(%u, %p), flags: %s, feat: %s, resv: %s, sq_thread_cpu: %u\n",
105 entries, p, flags_string(p), features_string(p), dump_resv(p),
106 p ? p->sq_thread_cpu : 0);
107
108 ret = __sys_io_uring_setup(entries, p);
109 if (ret != expect) {
110 printf("expected %d, got %d\n", expect, ret);
111 /* if we got a valid uring, close it */
112 if (ret > 0)
113 close(ret);
114 return 1;
115 }
116 __errno = errno;
117 if (expect == -1 && error != __errno) {
118 if (__errno == EPERM && geteuid() != 0) {
119 printf("Needs root, not flagging as an error\n");
120 return 0;
121 }
122 printf("expected errno %d, got %d\n", error, __errno);
123 return 1;
124 }
125
126 return 0;
127 }
128
129 int
main(int argc,char ** argv)130 main(int argc, char **argv)
131 {
132 int fd;
133 unsigned int status = 0;
134 struct io_uring_params p;
135
136 if (argc > 1)
137 return 0;
138
139 memset(&p, 0, sizeof(p));
140 status |= try_io_uring_setup(0, &p, -1, EINVAL);
141 status |= try_io_uring_setup(1, NULL, -1, EFAULT);
142
143 /* resv array is non-zero */
144 memset(&p, 0, sizeof(p));
145 p.resv[0] = p.resv[1] = p.resv[2] = 1;
146 status |= try_io_uring_setup(1, &p, -1, EINVAL);
147
148 /* invalid flags */
149 memset(&p, 0, sizeof(p));
150 p.flags = ~0U;
151 status |= try_io_uring_setup(1, &p, -1, EINVAL);
152
153 /* IORING_SETUP_SQ_AFF set but not IORING_SETUP_SQPOLL */
154 memset(&p, 0, sizeof(p));
155 p.flags = IORING_SETUP_SQ_AFF;
156 status |= try_io_uring_setup(1, &p, -1, EINVAL);
157
158 /* attempt to bind to invalid cpu */
159 memset(&p, 0, sizeof(p));
160 p.flags = IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
161 p.sq_thread_cpu = get_nprocs_conf();
162 status |= try_io_uring_setup(1, &p, -1, EINVAL);
163
164 /* I think we can limit a process to a set of cpus. I assume
165 * we shouldn't be able to setup a kernel thread outside of that.
166 * try to do that. (task->cpus_allowed) */
167
168 /* read/write on io_uring_fd */
169 memset(&p, 0, sizeof(p));
170 fd = __sys_io_uring_setup(1, &p);
171 if (fd < 0) {
172 printf("io_uring_setup failed with %d, expected success\n",
173 errno);
174 status = 1;
175 } else {
176 char buf[4096];
177 int ret;
178 ret = read(fd, buf, 4096);
179 if (ret >= 0) {
180 printf("read from io_uring fd succeeded. expected fail\n");
181 status = 1;
182 }
183 }
184
185 if (!status) {
186 printf("PASS\n");
187 return 0;
188 }
189
190 printf("FAIL\n");
191 return -1;
192 }
193