polyphony 0.85 → 0.86

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile.lock +1 -1
  4. data/ext/polyphony/io_extensions.c +2 -3
  5. data/lib/polyphony/version.rb +1 -1
  6. data/polyphony.gemspec +1 -1
  7. data/test/test_backend.rb +1 -1
  8. data/test/test_signal.rb +3 -3
  9. data/vendor/liburing/.github/pull_request_template.md +86 -0
  10. data/vendor/liburing/.github/workflows/build.yml +85 -0
  11. data/vendor/liburing/.github/workflows/shellcheck.yml +20 -0
  12. data/vendor/liburing/.gitignore +149 -0
  13. data/vendor/liburing/COPYING +502 -0
  14. data/vendor/liburing/COPYING.GPL +339 -0
  15. data/vendor/liburing/LICENSE +7 -0
  16. data/vendor/liburing/Makefile +82 -0
  17. data/vendor/liburing/Makefile.common +5 -0
  18. data/vendor/liburing/Makefile.quiet +11 -0
  19. data/vendor/liburing/README +46 -0
  20. data/vendor/liburing/configure +486 -0
  21. data/vendor/liburing/debian/README.Debian +7 -0
  22. data/vendor/liburing/debian/changelog +27 -0
  23. data/vendor/liburing/debian/compat +1 -0
  24. data/vendor/liburing/debian/control +48 -0
  25. data/vendor/liburing/debian/copyright +49 -0
  26. data/vendor/liburing/debian/liburing-dev.install +4 -0
  27. data/vendor/liburing/debian/liburing-dev.manpages +6 -0
  28. data/vendor/liburing/debian/liburing1-udeb.install +1 -0
  29. data/vendor/liburing/debian/liburing1.install +1 -0
  30. data/vendor/liburing/debian/liburing1.symbols +32 -0
  31. data/vendor/liburing/debian/patches/series +1 -0
  32. data/vendor/liburing/debian/rules +81 -0
  33. data/vendor/liburing/debian/source/format +1 -0
  34. data/vendor/liburing/debian/source/local-options +2 -0
  35. data/vendor/liburing/debian/source/options +1 -0
  36. data/vendor/liburing/debian/watch +3 -0
  37. data/vendor/liburing/examples/Makefile +38 -0
  38. data/vendor/liburing/examples/io_uring-cp.c +282 -0
  39. data/vendor/liburing/examples/io_uring-test.c +112 -0
  40. data/vendor/liburing/examples/link-cp.c +193 -0
  41. data/vendor/liburing/examples/ucontext-cp.c +273 -0
  42. data/vendor/liburing/liburing.pc.in +12 -0
  43. data/vendor/liburing/liburing.spec +66 -0
  44. data/vendor/liburing/make-debs.sh +53 -0
  45. data/vendor/liburing/man/io_uring.7 +754 -0
  46. data/vendor/liburing/man/io_uring_cq_advance.3 +35 -0
  47. data/vendor/liburing/man/io_uring_cq_ready.3 +25 -0
  48. data/vendor/liburing/man/io_uring_cqe_get_data.3 +34 -0
  49. data/vendor/liburing/man/io_uring_cqe_seen.3 +32 -0
  50. data/vendor/liburing/man/io_uring_enter.2 +1483 -0
  51. data/vendor/liburing/man/io_uring_free_probe.3 +24 -0
  52. data/vendor/liburing/man/io_uring_get_probe.3 +29 -0
  53. data/vendor/liburing/man/io_uring_get_sqe.3 +38 -0
  54. data/vendor/liburing/man/io_uring_opcode_supported.3 +29 -0
  55. data/vendor/liburing/man/io_uring_prep_msg_ring.3 +58 -0
  56. data/vendor/liburing/man/io_uring_prep_read.3 +50 -0
  57. data/vendor/liburing/man/io_uring_prep_read_fixed.3 +54 -0
  58. data/vendor/liburing/man/io_uring_prep_readv.3 +51 -0
  59. data/vendor/liburing/man/io_uring_prep_readv2.3 +79 -0
  60. data/vendor/liburing/man/io_uring_prep_write.3 +50 -0
  61. data/vendor/liburing/man/io_uring_prep_write_fixed.3 +54 -0
  62. data/vendor/liburing/man/io_uring_prep_writev.3 +51 -0
  63. data/vendor/liburing/man/io_uring_prep_writev2.3 +78 -0
  64. data/vendor/liburing/man/io_uring_queue_exit.3 +27 -0
  65. data/vendor/liburing/man/io_uring_queue_init.3 +44 -0
  66. data/vendor/liburing/man/io_uring_register.2 +688 -0
  67. data/vendor/liburing/man/io_uring_register_buffers.3 +41 -0
  68. data/vendor/liburing/man/io_uring_register_files.3 +35 -0
  69. data/vendor/liburing/man/io_uring_setup.2 +534 -0
  70. data/vendor/liburing/man/io_uring_sq_ready.3 +25 -0
  71. data/vendor/liburing/man/io_uring_sq_space_left.3 +25 -0
  72. data/vendor/liburing/man/io_uring_sqe_set_data.3 +30 -0
  73. data/vendor/liburing/man/io_uring_sqe_set_flags.3 +60 -0
  74. data/vendor/liburing/man/io_uring_sqring_wait.3 +30 -0
  75. data/vendor/liburing/man/io_uring_submit.3 +29 -0
  76. data/vendor/liburing/man/io_uring_submit_and_wait.3 +34 -0
  77. data/vendor/liburing/man/io_uring_submit_and_wait_timeout.3 +49 -0
  78. data/vendor/liburing/man/io_uring_unregister_buffers.3 +26 -0
  79. data/vendor/liburing/man/io_uring_unregister_files.3 +26 -0
  80. data/vendor/liburing/man/io_uring_wait_cqe.3 +33 -0
  81. data/vendor/liburing/man/io_uring_wait_cqe_nr.3 +36 -0
  82. data/vendor/liburing/man/io_uring_wait_cqe_timeout.3 +39 -0
  83. data/vendor/liburing/man/io_uring_wait_cqes.3 +46 -0
  84. data/vendor/liburing/src/Makefile +89 -0
  85. data/vendor/liburing/src/arch/aarch64/syscall.h +95 -0
  86. data/vendor/liburing/src/arch/generic/lib.h +21 -0
  87. data/vendor/liburing/src/arch/generic/syscall.h +87 -0
  88. data/vendor/liburing/src/arch/syscall-defs.h +67 -0
  89. data/vendor/liburing/src/arch/x86/lib.h +32 -0
  90. data/vendor/liburing/src/arch/x86/syscall.h +160 -0
  91. data/vendor/liburing/src/include/liburing/barrier.h +81 -0
  92. data/vendor/liburing/src/include/liburing/io_uring.h +442 -0
  93. data/vendor/liburing/src/include/liburing.h +921 -0
  94. data/vendor/liburing/src/int_flags.h +8 -0
  95. data/vendor/liburing/src/lib.h +57 -0
  96. data/vendor/liburing/src/liburing.map +53 -0
  97. data/vendor/liburing/src/nolibc.c +48 -0
  98. data/vendor/liburing/src/queue.c +403 -0
  99. data/vendor/liburing/src/register.c +293 -0
  100. data/vendor/liburing/src/setup.c +332 -0
  101. data/vendor/liburing/src/syscall.c +47 -0
  102. data/vendor/liburing/src/syscall.h +103 -0
  103. data/vendor/liburing/test/232c93d07b74-test.c +306 -0
  104. data/vendor/liburing/test/35fa71a030ca-test.c +329 -0
  105. data/vendor/liburing/test/500f9fbadef8-test.c +89 -0
  106. data/vendor/liburing/test/7ad0e4b2f83c-test.c +93 -0
  107. data/vendor/liburing/test/8a9973408177-test.c +106 -0
  108. data/vendor/liburing/test/917257daa0fe-test.c +53 -0
  109. data/vendor/liburing/test/Makefile +244 -0
  110. data/vendor/liburing/test/a0908ae19763-test.c +58 -0
  111. data/vendor/liburing/test/a4c0b3decb33-test.c +180 -0
  112. data/vendor/liburing/test/accept-link.c +254 -0
  113. data/vendor/liburing/test/accept-reuse.c +164 -0
  114. data/vendor/liburing/test/accept-test.c +79 -0
  115. data/vendor/liburing/test/accept.c +477 -0
  116. data/vendor/liburing/test/across-fork.c +283 -0
  117. data/vendor/liburing/test/b19062a56726-test.c +53 -0
  118. data/vendor/liburing/test/b5837bd5311d-test.c +77 -0
  119. data/vendor/liburing/test/ce593a6c480a-test.c +136 -0
  120. data/vendor/liburing/test/close-opath.c +122 -0
  121. data/vendor/liburing/test/config +10 -0
  122. data/vendor/liburing/test/connect.c +398 -0
  123. data/vendor/liburing/test/cq-full.c +96 -0
  124. data/vendor/liburing/test/cq-overflow.c +294 -0
  125. data/vendor/liburing/test/cq-peek-batch.c +102 -0
  126. data/vendor/liburing/test/cq-ready.c +94 -0
  127. data/vendor/liburing/test/cq-size.c +64 -0
  128. data/vendor/liburing/test/d4ae271dfaae-test.c +96 -0
  129. data/vendor/liburing/test/d77a67ed5f27-test.c +65 -0
  130. data/vendor/liburing/test/defer.c +307 -0
  131. data/vendor/liburing/test/double-poll-crash.c +185 -0
  132. data/vendor/liburing/test/drop-submit.c +92 -0
  133. data/vendor/liburing/test/eeed8b54e0df-test.c +114 -0
  134. data/vendor/liburing/test/empty-eownerdead.c +45 -0
  135. data/vendor/liburing/test/eventfd-disable.c +151 -0
  136. data/vendor/liburing/test/eventfd-reg.c +76 -0
  137. data/vendor/liburing/test/eventfd-ring.c +97 -0
  138. data/vendor/liburing/test/eventfd.c +112 -0
  139. data/vendor/liburing/test/exec-target.c +6 -0
  140. data/vendor/liburing/test/exit-no-cleanup.c +117 -0
  141. data/vendor/liburing/test/fadvise.c +202 -0
  142. data/vendor/liburing/test/fallocate.c +249 -0
  143. data/vendor/liburing/test/fc2a85cb02ef-test.c +131 -0
  144. data/vendor/liburing/test/file-register.c +858 -0
  145. data/vendor/liburing/test/file-update.c +173 -0
  146. data/vendor/liburing/test/file-verify.c +629 -0
  147. data/vendor/liburing/test/files-exit-hang-poll.c +128 -0
  148. data/vendor/liburing/test/files-exit-hang-timeout.c +134 -0
  149. data/vendor/liburing/test/fixed-link.c +90 -0
  150. data/vendor/liburing/test/fpos.c +252 -0
  151. data/vendor/liburing/test/fsync.c +224 -0
  152. data/vendor/liburing/test/hardlink.c +136 -0
  153. data/vendor/liburing/test/helpers.c +135 -0
  154. data/vendor/liburing/test/helpers.h +67 -0
  155. data/vendor/liburing/test/io-cancel.c +550 -0
  156. data/vendor/liburing/test/io_uring_enter.c +296 -0
  157. data/vendor/liburing/test/io_uring_register.c +676 -0
  158. data/vendor/liburing/test/io_uring_setup.c +192 -0
  159. data/vendor/liburing/test/iopoll.c +372 -0
  160. data/vendor/liburing/test/lfs-openat-write.c +119 -0
  161. data/vendor/liburing/test/lfs-openat.c +275 -0
  162. data/vendor/liburing/test/link-timeout.c +1107 -0
  163. data/vendor/liburing/test/link.c +496 -0
  164. data/vendor/liburing/test/link_drain.c +229 -0
  165. data/vendor/liburing/test/madvise.c +195 -0
  166. data/vendor/liburing/test/mkdir.c +108 -0
  167. data/vendor/liburing/test/msg-ring.c +234 -0
  168. data/vendor/liburing/test/multicqes_drain.c +387 -0
  169. data/vendor/liburing/test/nop-all-sizes.c +99 -0
  170. data/vendor/liburing/test/nop.c +115 -0
  171. data/vendor/liburing/test/open-close.c +261 -0
  172. data/vendor/liburing/test/openat2.c +308 -0
  173. data/vendor/liburing/test/personality.c +204 -0
  174. data/vendor/liburing/test/pipe-eof.c +83 -0
  175. data/vendor/liburing/test/pipe-reuse.c +105 -0
  176. data/vendor/liburing/test/poll-cancel-ton.c +135 -0
  177. data/vendor/liburing/test/poll-cancel.c +228 -0
  178. data/vendor/liburing/test/poll-link.c +230 -0
  179. data/vendor/liburing/test/poll-many.c +208 -0
  180. data/vendor/liburing/test/poll-mshot-update.c +273 -0
  181. data/vendor/liburing/test/poll-ring.c +48 -0
  182. data/vendor/liburing/test/poll-v-poll.c +353 -0
  183. data/vendor/liburing/test/poll.c +109 -0
  184. data/vendor/liburing/test/pollfree.c +426 -0
  185. data/vendor/liburing/test/probe.c +135 -0
  186. data/vendor/liburing/test/read-write.c +876 -0
  187. data/vendor/liburing/test/register-restrictions.c +633 -0
  188. data/vendor/liburing/test/rename.c +135 -0
  189. data/vendor/liburing/test/ring-leak.c +173 -0
  190. data/vendor/liburing/test/ring-leak2.c +249 -0
  191. data/vendor/liburing/test/rsrc_tags.c +449 -0
  192. data/vendor/liburing/test/runtests-loop.sh +16 -0
  193. data/vendor/liburing/test/runtests.sh +170 -0
  194. data/vendor/liburing/test/rw_merge_test.c +97 -0
  195. data/vendor/liburing/test/self.c +91 -0
  196. data/vendor/liburing/test/send_recv.c +286 -0
  197. data/vendor/liburing/test/send_recvmsg.c +345 -0
  198. data/vendor/liburing/test/sendmsg_fs_cve.c +200 -0
  199. data/vendor/liburing/test/shared-wq.c +84 -0
  200. data/vendor/liburing/test/short-read.c +75 -0
  201. data/vendor/liburing/test/shutdown.c +165 -0
  202. data/vendor/liburing/test/sigfd-deadlock.c +74 -0
  203. data/vendor/liburing/test/skip-cqe.c +429 -0
  204. data/vendor/liburing/test/socket-rw-eagain.c +158 -0
  205. data/vendor/liburing/test/socket-rw-offset.c +157 -0
  206. data/vendor/liburing/test/socket-rw.c +145 -0
  207. data/vendor/liburing/test/splice.c +512 -0
  208. data/vendor/liburing/test/sq-full-cpp.cc +45 -0
  209. data/vendor/liburing/test/sq-full.c +45 -0
  210. data/vendor/liburing/test/sq-poll-dup.c +204 -0
  211. data/vendor/liburing/test/sq-poll-kthread.c +169 -0
  212. data/vendor/liburing/test/sq-poll-share.c +137 -0
  213. data/vendor/liburing/test/sq-space_left.c +159 -0
  214. data/vendor/liburing/test/sqpoll-cancel-hang.c +157 -0
  215. data/vendor/liburing/test/sqpoll-disable-exit.c +196 -0
  216. data/vendor/liburing/test/sqpoll-exit-hang.c +78 -0
  217. data/vendor/liburing/test/sqpoll-sleep.c +69 -0
  218. data/vendor/liburing/test/statx.c +172 -0
  219. data/vendor/liburing/test/stdout.c +232 -0
  220. data/vendor/liburing/test/submit-link-fail.c +154 -0
  221. data/vendor/liburing/test/submit-reuse.c +239 -0
  222. data/vendor/liburing/test/symlink.c +116 -0
  223. data/vendor/liburing/test/teardowns.c +58 -0
  224. data/vendor/liburing/test/thread-exit.c +143 -0
  225. data/vendor/liburing/test/timeout-new.c +252 -0
  226. data/vendor/liburing/test/timeout-overflow.c +204 -0
  227. data/vendor/liburing/test/timeout.c +1523 -0
  228. data/vendor/liburing/test/unlink.c +112 -0
  229. data/vendor/liburing/test/wakeup-hang.c +162 -0
  230. metadata +223 -2
@@ -0,0 +1,676 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * io_uring_register.c
4
+ *
5
+ * Description: Unit tests for the io_uring_register 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 <poll.h>
18
+ #include <assert.h>
19
+ #include <sys/uio.h>
20
+ #include <sys/mman.h>
21
+ #include <linux/mman.h>
22
+ #include <sys/time.h>
23
+ #include <sys/resource.h>
24
+ #include <limits.h>
25
+
26
+ #include "helpers.h"
27
+ #include "liburing.h"
28
+ #include "../src/syscall.h"
29
+
30
+ static int pagesize;
31
+ static rlim_t mlock_limit;
32
+ static int devnull;
33
+
34
+ #if !defined(CONFIG_HAVE_MEMFD_CREATE)
35
+ #include <sys/syscall.h>
36
+ #include <linux/memfd.h>
37
+
38
+ static int memfd_create(const char *name, unsigned int flags)
39
+ {
40
+ return (int)syscall(SYS_memfd_create, name, flags);
41
+ }
42
+ #endif
43
+
44
+
45
+ int
46
+ expect_fail(int fd, unsigned int opcode, void *arg,
47
+ unsigned int nr_args, int error)
48
+ {
49
+ int ret;
50
+
51
+ printf("io_uring_register(%d, %u, %p, %u)\n",
52
+ fd, opcode, arg, nr_args);
53
+ ret = __sys_io_uring_register(fd, opcode, arg, nr_args);
54
+ if (ret != -1) {
55
+ int ret2 = 0;
56
+
57
+ printf("expected %s, but call succeeded\n", strerror(error));
58
+ if (opcode == IORING_REGISTER_BUFFERS) {
59
+ ret2 = __sys_io_uring_register(fd,
60
+ IORING_UNREGISTER_BUFFERS, 0, 0);
61
+ } else if (opcode == IORING_REGISTER_FILES) {
62
+ ret2 = __sys_io_uring_register(fd,
63
+ IORING_UNREGISTER_FILES, 0, 0);
64
+ }
65
+ if (ret2) {
66
+ printf("internal error: failed to unregister\n");
67
+ exit(1);
68
+ }
69
+ return 1;
70
+ }
71
+
72
+ if (errno != error) {
73
+ printf("expected %d, got %d\n", error, errno);
74
+ return 1;
75
+ }
76
+ return 0;
77
+ }
78
+
79
+ int
80
+ new_io_uring(int entries, struct io_uring_params *p)
81
+ {
82
+ int fd;
83
+
84
+ fd = __sys_io_uring_setup(entries, p);
85
+ if (fd < 0) {
86
+ perror("io_uring_setup");
87
+ exit(1);
88
+ }
89
+ return fd;
90
+ }
91
+
92
+ #define MAXFDS (UINT_MAX * sizeof(int))
93
+
94
+ void *
95
+ map_filebacked(size_t size)
96
+ {
97
+ int fd, ret;
98
+ void *addr;
99
+ char template[32] = "io_uring_register-test-XXXXXXXX";
100
+
101
+ fd = mkstemp(template);
102
+ if (fd < 0) {
103
+ perror("mkstemp");
104
+ return NULL;
105
+ }
106
+ unlink(template);
107
+
108
+ ret = ftruncate(fd, size);
109
+ if (ret < 0) {
110
+ perror("ftruncate");
111
+ close(fd);
112
+ return NULL;
113
+ }
114
+
115
+ addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
116
+ if (addr == MAP_FAILED) {
117
+ perror("mmap");
118
+ close(fd);
119
+ return NULL;
120
+ }
121
+
122
+ close(fd);
123
+ return addr;
124
+ }
125
+
126
+ /*
127
+ * NOTE: this is now limited by SCM_MAX_FD (253). Keep the code for now,
128
+ * but probably should augment it to test 253 and 254, specifically.
129
+ */
130
+ int
131
+ test_max_fds(int uring_fd)
132
+ {
133
+ int status = 1;
134
+ int ret;
135
+ void *fd_as; /* file descriptor address space */
136
+ int fdtable_fd; /* fd for the file that will be mapped over and over */
137
+ int io_fd; /* the valid fd for I/O -- /dev/null */
138
+ int *fds; /* used to map the file into the address space */
139
+ char template[32] = "io_uring_register-test-XXXXXXXX";
140
+ unsigned long long i, nr_maps, nr_fds;
141
+
142
+ /*
143
+ * First, mmap anonymous the full size. That will guarantee the
144
+ * mapping will fit in the memory area selected by mmap. Then,
145
+ * over-write that mapping using a file-backed mapping, 128MiB at
146
+ * a time using MAP_FIXED.
147
+ */
148
+ fd_as = mmap(NULL, UINT_MAX * sizeof(int), PROT_READ|PROT_WRITE,
149
+ MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
150
+ if (fd_as == MAP_FAILED) {
151
+ if (errno == ENOMEM) {
152
+ printf("Not enough memory for this test, skipping\n");
153
+ return 0;
154
+ }
155
+ perror("mmap fd_as");
156
+ exit(1);
157
+ }
158
+ printf("allocated %zu bytes of address space\n", UINT_MAX * sizeof(int));
159
+
160
+ fdtable_fd = mkstemp(template);
161
+ if (fdtable_fd < 0) {
162
+ perror("mkstemp");
163
+ exit(1);
164
+ }
165
+ unlink(template);
166
+ ret = ftruncate(fdtable_fd, 128*1024*1024);
167
+ if (ret < 0) {
168
+ perror("ftruncate");
169
+ exit(1);
170
+ }
171
+
172
+ io_fd = open("/dev/null", O_RDWR);
173
+ if (io_fd < 0) {
174
+ perror("open /dev/null");
175
+ exit(1);
176
+ }
177
+ fds = mmap(fd_as, 128*1024*1024, PROT_READ|PROT_WRITE,
178
+ MAP_SHARED|MAP_FIXED, fdtable_fd, 0);
179
+ if (fds == MAP_FAILED) {
180
+ perror("mmap fdtable");
181
+ exit(1);
182
+ }
183
+
184
+ /* fill the fd table */
185
+ nr_fds = 128*1024*1024 / sizeof(int);
186
+ for (i = 0; i < nr_fds; i++)
187
+ fds[i] = io_fd;
188
+
189
+ /* map the file through the rest of the address space */
190
+ nr_maps = (UINT_MAX * sizeof(int)) / (128*1024*1024);
191
+ for (i = 0; i < nr_maps; i++) {
192
+ fds = &fds[nr_fds]; /* advance fds by 128MiB */
193
+ fds = mmap(fds, 128*1024*1024, PROT_READ|PROT_WRITE,
194
+ MAP_SHARED|MAP_FIXED, fdtable_fd, 0);
195
+ if (fds == MAP_FAILED) {
196
+ printf("mmap failed at offset %lu\n",
197
+ (unsigned long)((char *)fd_as - (char *)fds));
198
+ exit(1);
199
+ }
200
+ }
201
+
202
+ /* Now fd_as points to the file descriptor array. */
203
+ /*
204
+ * We may not be able to map all of these files. Let's back off
205
+ * until success.
206
+ */
207
+ nr_fds = UINT_MAX;
208
+ while (nr_fds) {
209
+ ret = __sys_io_uring_register(uring_fd, IORING_REGISTER_FILES,
210
+ fd_as, nr_fds);
211
+ if (ret != 0) {
212
+ nr_fds /= 2;
213
+ continue;
214
+ }
215
+ printf("io_uring_register(%d, IORING_REGISTER_FILES, %p, %llu)"
216
+ "...succeeded\n", uring_fd, fd_as, nr_fds);
217
+ status = 0;
218
+ printf("io_uring_register(%d, IORING_UNREGISTER_FILES, 0, 0)...",
219
+ uring_fd);
220
+ ret = __sys_io_uring_register(uring_fd, IORING_UNREGISTER_FILES,
221
+ 0, 0);
222
+ if (ret < 0) {
223
+ ret = errno;
224
+ printf("failed\n");
225
+ errno = ret;
226
+ perror("io_uring_register UNREGISTER_FILES");
227
+ exit(1);
228
+ }
229
+ printf("succeeded\n");
230
+ break;
231
+ }
232
+
233
+ close(io_fd);
234
+ close(fdtable_fd);
235
+ ret = munmap(fd_as, UINT_MAX * sizeof(int));
236
+ if (ret != 0) {
237
+ printf("munmap(%zu) failed\n", UINT_MAX * sizeof(int));
238
+ exit(1);
239
+ }
240
+
241
+ return status;
242
+ }
243
+
244
+ int
245
+ test_memlock_exceeded(int fd)
246
+ {
247
+ int ret;
248
+ void *buf;
249
+ struct iovec iov;
250
+
251
+ /* if limit is larger than 2gb, just skip this test */
252
+ if (mlock_limit >= 2 * 1024 * 1024 * 1024ULL)
253
+ return 0;
254
+
255
+ iov.iov_len = mlock_limit * 2;
256
+ buf = t_malloc(iov.iov_len);
257
+ iov.iov_base = buf;
258
+
259
+ while (iov.iov_len) {
260
+ ret = __sys_io_uring_register(fd, IORING_REGISTER_BUFFERS, &iov, 1);
261
+ if (ret < 0) {
262
+ if (errno == ENOMEM) {
263
+ printf("io_uring_register of %zu bytes failed "
264
+ "with ENOMEM (expected).\n", iov.iov_len);
265
+ iov.iov_len /= 2;
266
+ continue;
267
+ }
268
+ if (errno == EFAULT) {
269
+ free(buf);
270
+ return 0;
271
+ }
272
+ printf("expected success or EFAULT, got %d\n", errno);
273
+ free(buf);
274
+ return 1;
275
+ }
276
+ printf("successfully registered %zu bytes (%d).\n",
277
+ iov.iov_len, ret);
278
+ ret = __sys_io_uring_register(fd, IORING_UNREGISTER_BUFFERS,
279
+ NULL, 0);
280
+ if (ret != 0) {
281
+ printf("error: unregister failed with %d\n", errno);
282
+ free(buf);
283
+ return 1;
284
+ }
285
+ break;
286
+ }
287
+ if (!iov.iov_len)
288
+ printf("Unable to register buffers. Check memlock rlimit.\n");
289
+
290
+ free(buf);
291
+ return 0;
292
+ }
293
+
294
+ int
295
+ test_iovec_nr(int fd)
296
+ {
297
+ int i, ret, status = 0;
298
+ unsigned int nr = 1000000;
299
+ struct iovec *iovs;
300
+ void *buf;
301
+
302
+ iovs = malloc(nr * sizeof(struct iovec));
303
+ if (!iovs) {
304
+ fprintf(stdout, "can't allocate iovecs, skip\n");
305
+ return 0;
306
+ }
307
+ buf = t_malloc(pagesize);
308
+
309
+ for (i = 0; i < nr; i++) {
310
+ iovs[i].iov_base = buf;
311
+ iovs[i].iov_len = pagesize;
312
+ }
313
+
314
+ status |= expect_fail(fd, IORING_REGISTER_BUFFERS, iovs, nr, EINVAL);
315
+
316
+ /* reduce to UIO_MAXIOV */
317
+ nr = UIO_MAXIOV;
318
+ printf("io_uring_register(%d, %u, %p, %u)\n",
319
+ fd, IORING_REGISTER_BUFFERS, iovs, nr);
320
+ ret = __sys_io_uring_register(fd, IORING_REGISTER_BUFFERS, iovs, nr);
321
+ if (ret && (errno == ENOMEM || errno == EPERM) && geteuid()) {
322
+ printf("can't register large iovec for regular users, skip\n");
323
+ } else if (ret != 0) {
324
+ printf("expected success, got %d\n", errno);
325
+ status = 1;
326
+ } else {
327
+ __sys_io_uring_register(fd, IORING_UNREGISTER_BUFFERS, 0, 0);
328
+ }
329
+ free(buf);
330
+ free(iovs);
331
+ return status;
332
+ }
333
+
334
+ /*
335
+ * io_uring limit is 1G. iov_len limit is ~OUL, I think
336
+ */
337
+ int
338
+ test_iovec_size(int fd)
339
+ {
340
+ unsigned int status = 0;
341
+ int ret;
342
+ struct iovec iov;
343
+ void *buf;
344
+
345
+ /* NULL pointer for base */
346
+ iov.iov_base = 0;
347
+ iov.iov_len = 4096;
348
+ status |= expect_fail(fd, IORING_REGISTER_BUFFERS, &iov, 1, EFAULT);
349
+
350
+ /* valid base, 0 length */
351
+ iov.iov_base = &buf;
352
+ iov.iov_len = 0;
353
+ status |= expect_fail(fd, IORING_REGISTER_BUFFERS, &iov, 1, EFAULT);
354
+
355
+ /* valid base, length exceeds size */
356
+ /* this requires an unampped page directly after buf */
357
+ buf = mmap(NULL, 2 * pagesize, PROT_READ|PROT_WRITE,
358
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
359
+ assert(buf != MAP_FAILED);
360
+ ret = munmap(buf + pagesize, pagesize);
361
+ assert(ret == 0);
362
+ iov.iov_base = buf;
363
+ iov.iov_len = 2 * pagesize;
364
+ status |= expect_fail(fd, IORING_REGISTER_BUFFERS, &iov, 1, EFAULT);
365
+ munmap(buf, pagesize);
366
+
367
+ /* huge page */
368
+ buf = mmap(NULL, 2*1024*1024, PROT_READ|PROT_WRITE,
369
+ MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_2MB | MAP_ANONYMOUS,
370
+ -1, 0);
371
+ if (buf == MAP_FAILED) {
372
+ printf("Unable to map a huge page. Try increasing "
373
+ "/proc/sys/vm/nr_hugepages by at least 1.\n");
374
+ printf("Skipping the hugepage test\n");
375
+ } else {
376
+ /*
377
+ * This should succeed, so long as RLIMIT_MEMLOCK is
378
+ * not exceeded
379
+ */
380
+ iov.iov_base = buf;
381
+ iov.iov_len = 2*1024*1024;
382
+ ret = __sys_io_uring_register(fd, IORING_REGISTER_BUFFERS, &iov, 1);
383
+ if (ret < 0) {
384
+ if (errno == ENOMEM)
385
+ printf("Unable to test registering of a huge "
386
+ "page. Try increasing the "
387
+ "RLIMIT_MEMLOCK resource limit by at "
388
+ "least 2MB.");
389
+ else {
390
+ printf("expected success, got %d\n", errno);
391
+ status = 1;
392
+ }
393
+ } else {
394
+ printf("Success!\n");
395
+ ret = __sys_io_uring_register(fd,
396
+ IORING_UNREGISTER_BUFFERS, 0, 0);
397
+ if (ret < 0) {
398
+ perror("io_uring_unregister");
399
+ status = 1;
400
+ }
401
+ }
402
+ }
403
+ ret = munmap(iov.iov_base, iov.iov_len);
404
+ assert(ret == 0);
405
+
406
+ /* file-backed buffers -- not supported */
407
+ buf = map_filebacked(2*1024*1024);
408
+ if (!buf)
409
+ status = 1;
410
+ iov.iov_base = buf;
411
+ iov.iov_len = 2*1024*1024;
412
+ printf("reserve file-backed buffers\n");
413
+ status |= expect_fail(fd, IORING_REGISTER_BUFFERS, &iov, 1, EOPNOTSUPP);
414
+ munmap(buf, 2*1024*1024);
415
+
416
+ /* bump up against the soft limit and make sure we get EFAULT
417
+ * or whatever we're supposed to get. NOTE: this requires
418
+ * running the test as non-root. */
419
+ if (getuid() != 0)
420
+ status |= test_memlock_exceeded(fd);
421
+
422
+ return status;
423
+ }
424
+
425
+ void
426
+ dump_sqe(struct io_uring_sqe *sqe)
427
+ {
428
+ printf("\topcode: %d\n", sqe->opcode);
429
+ printf("\tflags: 0x%.8x\n", sqe->flags);
430
+ printf("\tfd: %d\n", sqe->fd);
431
+ if (sqe->opcode == IORING_OP_POLL_ADD)
432
+ printf("\tpoll_events: 0x%.8x\n", sqe->poll_events);
433
+ }
434
+
435
+ int
436
+ ioring_poll(struct io_uring *ring, int fd, int fixed)
437
+ {
438
+ int ret;
439
+ struct io_uring_sqe *sqe;
440
+ struct io_uring_cqe *cqe;
441
+
442
+ sqe = io_uring_get_sqe(ring);
443
+ memset(sqe, 0, sizeof(*sqe));
444
+ sqe->opcode = IORING_OP_POLL_ADD;
445
+ if (fixed)
446
+ sqe->flags = IOSQE_FIXED_FILE;
447
+ sqe->fd = fd;
448
+ sqe->poll_events = POLLIN|POLLOUT;
449
+
450
+ printf("io_uring_submit:\n");
451
+ dump_sqe(sqe);
452
+ ret = io_uring_submit(ring);
453
+ if (ret != 1) {
454
+ printf("failed to submit poll sqe: %d.\n", errno);
455
+ return 1;
456
+ }
457
+
458
+ ret = io_uring_wait_cqe(ring, &cqe);
459
+ if (ret < 0) {
460
+ printf("io_uring_wait_cqe failed with %d\n", ret);
461
+ return 1;
462
+ }
463
+ ret = 0;
464
+ if (cqe->res != POLLOUT) {
465
+ printf("io_uring_wait_cqe: expected 0x%.8x, got 0x%.8x\n",
466
+ POLLOUT, cqe->res);
467
+ ret = 1;
468
+ }
469
+
470
+ io_uring_cqe_seen(ring, cqe);
471
+ return ret;
472
+ }
473
+
474
+ int
475
+ test_poll_ringfd(void)
476
+ {
477
+ int status = 0;
478
+ int ret;
479
+ int fd;
480
+ struct io_uring ring;
481
+
482
+ ret = io_uring_queue_init(1, &ring, 0);
483
+ if (ret) {
484
+ perror("io_uring_queue_init");
485
+ return 1;
486
+ }
487
+ fd = ring.ring_fd;
488
+
489
+ /* try polling the ring fd */
490
+ status = ioring_poll(&ring, fd, 0);
491
+
492
+ /*
493
+ * now register the ring fd, and try the poll again. This should
494
+ * fail, because the kernel does not allow registering of the
495
+ * ring_fd.
496
+ */
497
+ status |= expect_fail(fd, IORING_REGISTER_FILES, &fd, 1, EBADF);
498
+
499
+ /* tear down queue */
500
+ io_uring_queue_exit(&ring);
501
+
502
+ return status;
503
+ }
504
+
505
+ static int test_shmem(void)
506
+ {
507
+ const char pattern = 0xEA;
508
+ const int len = 4096;
509
+ struct io_uring_sqe *sqe;
510
+ struct io_uring_cqe *cqe;
511
+ struct io_uring ring;
512
+ struct iovec iov;
513
+ int memfd, ret, i;
514
+ char *mem;
515
+ int pipefd[2] = {-1, -1};
516
+
517
+ ret = io_uring_queue_init(8, &ring, 0);
518
+ if (ret)
519
+ return 1;
520
+
521
+ if (pipe(pipefd)) {
522
+ perror("pipe");
523
+ return 1;
524
+ }
525
+ memfd = memfd_create("uring-shmem-test", 0);
526
+ if (memfd < 0) {
527
+ fprintf(stderr, "memfd_create() failed %i\n", -errno);
528
+ return 1;
529
+ }
530
+ if (ftruncate(memfd, len)) {
531
+ fprintf(stderr, "can't truncate memfd\n");
532
+ return 1;
533
+ }
534
+ mem = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
535
+ if (!mem) {
536
+ fprintf(stderr, "mmap failed\n");
537
+ return 1;
538
+ }
539
+ for (i = 0; i < len; i++)
540
+ mem[i] = pattern;
541
+
542
+ iov.iov_base = mem;
543
+ iov.iov_len = len;
544
+ ret = io_uring_register_buffers(&ring, &iov, 1);
545
+ if (ret) {
546
+ if (ret == -EOPNOTSUPP) {
547
+ fprintf(stdout, "memfd registration isn't supported, "
548
+ "skip\n");
549
+ goto out;
550
+ }
551
+
552
+ fprintf(stderr, "buffer reg failed: %d\n", ret);
553
+ return 1;
554
+ }
555
+
556
+ /* check that we can read and write from/to shmem reg buffer */
557
+ sqe = io_uring_get_sqe(&ring);
558
+ io_uring_prep_write_fixed(sqe, pipefd[1], mem, 512, 0, 0);
559
+ sqe->user_data = 1;
560
+
561
+ ret = io_uring_submit(&ring);
562
+ if (ret != 1) {
563
+ fprintf(stderr, "submit write failed\n");
564
+ return 1;
565
+ }
566
+ ret = io_uring_wait_cqe(&ring, &cqe);
567
+ if (ret < 0 || cqe->user_data != 1 || cqe->res != 512) {
568
+ fprintf(stderr, "reading from shmem failed\n");
569
+ return 1;
570
+ }
571
+ io_uring_cqe_seen(&ring, cqe);
572
+
573
+ /* clean it, should be populated with the pattern back from the pipe */
574
+ memset(mem, 0, 512);
575
+ sqe = io_uring_get_sqe(&ring);
576
+ io_uring_prep_read_fixed(sqe, pipefd[0], mem, 512, 0, 0);
577
+ sqe->user_data = 2;
578
+
579
+ ret = io_uring_submit(&ring);
580
+ if (ret != 1) {
581
+ fprintf(stderr, "submit write failed\n");
582
+ return 1;
583
+ }
584
+ ret = io_uring_wait_cqe(&ring, &cqe);
585
+ if (ret < 0 || cqe->user_data != 2 || cqe->res != 512) {
586
+ fprintf(stderr, "reading from shmem failed\n");
587
+ return 1;
588
+ }
589
+ io_uring_cqe_seen(&ring, cqe);
590
+
591
+ for (i = 0; i < 512; i++) {
592
+ if (mem[i] != pattern) {
593
+ fprintf(stderr, "data integrity fail\n");
594
+ return 1;
595
+ }
596
+ }
597
+
598
+ ret = io_uring_unregister_buffers(&ring);
599
+ if (ret) {
600
+ fprintf(stderr, "buffer unreg failed: %d\n", ret);
601
+ return 1;
602
+ }
603
+ out:
604
+ io_uring_queue_exit(&ring);
605
+ close(pipefd[0]);
606
+ close(pipefd[1]);
607
+ munmap(mem, len);
608
+ close(memfd);
609
+ return 0;
610
+ }
611
+
612
+ int
613
+ main(int argc, char **argv)
614
+ {
615
+ int fd, ret;
616
+ unsigned int status = 0;
617
+ struct io_uring_params p;
618
+ struct rlimit rlim;
619
+
620
+ if (argc > 1)
621
+ return 0;
622
+
623
+ /* setup globals */
624
+ pagesize = getpagesize();
625
+ ret = getrlimit(RLIMIT_MEMLOCK, &rlim);
626
+ if (ret < 0) {
627
+ perror("getrlimit");
628
+ return 1;
629
+ }
630
+ mlock_limit = rlim.rlim_cur;
631
+ printf("RELIMIT_MEMLOCK: %llu (%llu)\n", (unsigned long long) rlim.rlim_cur,
632
+ (unsigned long long) rlim.rlim_max);
633
+ devnull = open("/dev/null", O_RDWR);
634
+ if (devnull < 0) {
635
+ perror("open /dev/null");
636
+ exit(1);
637
+ }
638
+
639
+ /* invalid fd */
640
+ status |= expect_fail(-1, 0, NULL, 0, EBADF);
641
+ /* valid fd that is not an io_uring fd */
642
+ status |= expect_fail(devnull, 0, NULL, 0, EOPNOTSUPP);
643
+
644
+ /* invalid opcode */
645
+ memset(&p, 0, sizeof(p));
646
+ fd = new_io_uring(1, &p);
647
+ ret = expect_fail(fd, ~0U, NULL, 0, EINVAL);
648
+ if (ret) {
649
+ /* if this succeeds, tear down the io_uring instance
650
+ * and start clean for the next test. */
651
+ close(fd);
652
+ fd = new_io_uring(1, &p);
653
+ }
654
+
655
+ /* IORING_REGISTER_BUFFERS */
656
+ status |= test_iovec_size(fd);
657
+ status |= test_iovec_nr(fd);
658
+ /* IORING_REGISTER_FILES */
659
+ status |= test_max_fds(fd);
660
+ close(fd);
661
+ /* uring poll on the uring fd */
662
+ status |= test_poll_ringfd();
663
+
664
+ if (!status)
665
+ printf("PASS\n");
666
+ else
667
+ printf("FAIL\n");
668
+
669
+ ret = test_shmem();
670
+ if (ret) {
671
+ fprintf(stderr, "test_shmem() failed\n");
672
+ status |= 1;
673
+ }
674
+
675
+ return status;
676
+ }