1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/* SPDX-License-Identifier: MIT */
/*
* Description: check that racing wakeups don't re-issue a poll multishot,
* this can leak ring provided buffers. also test if ring
* provided buffers for regular receive can leak if we hit a
* poll race.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
#include "liburing.h"
#include "helpers.h"
#define NREQS 64
#define BUF_SIZE 64
static int no_buf_ring;
struct data {
pthread_barrier_t barrier;
int fd;
};
static void *thread(void *data)
{
struct data *d = data;
char buf[BUF_SIZE];
int ret, i, fd;
memset(buf, 0x5a, BUF_SIZE);
pthread_barrier_wait(&d->barrier);
fd = d->fd;
for (i = 0; i < NREQS; i++) {
ret = write(fd, buf, sizeof(buf));
if (ret != BUF_SIZE) {
if (ret < 0) {
perror("write");
printf("bad fd %d\n", fd);
} else
fprintf(stderr, "wrote short %d\n", ret);
}
}
return NULL;
}
static int test(struct io_uring *ring, struct data *d)
{
struct io_uring_buf_ring *br;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
int fd[2], ret, i;
pthread_t t;
void *buf, *ptr;
void *ret2;
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fd) < 0) {
perror("socketpair");
return T_EXIT_FAIL;
}
d->fd = fd[1];
if (posix_memalign((void **) &buf, 16384, BUF_SIZE * NREQS))
return T_EXIT_FAIL;
br = io_uring_setup_buf_ring(ring, NREQS, 1, 0, &ret);
if (!br) {
if (ret == -EINVAL) {
no_buf_ring = 1;
return T_EXIT_SKIP;
}
fprintf(stderr, "buf ring reg %d\n", ret);
return T_EXIT_FAIL;
}
ptr = buf;
for (i = 0; i < NREQS; i++) {
io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1,
io_uring_buf_ring_mask(NREQS), i);
ptr += BUF_SIZE;
}
io_uring_buf_ring_advance(br, NREQS);
pthread_create(&t, NULL, thread, d);
for (i = 0; i < NREQS; i++) {
sqe = io_uring_get_sqe(ring);
io_uring_prep_recv(sqe, fd[0], NULL, 0, 0);
sqe->flags |= IOSQE_BUFFER_SELECT;
sqe->buf_group = 1;
}
pthread_barrier_wait(&d->barrier);
ret = io_uring_submit(ring);
if (ret != NREQS) {
fprintf(stderr, "submit %d\n", ret);
return T_EXIT_FAIL;
}
i = 0;
do {
ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
fprintf(stderr, "cqe wait %d\n", ret);
return T_EXIT_FAIL;
}
i++;
if (cqe->res != BUF_SIZE) {
fprintf(stderr, "Bad cqe res %d\n", cqe->res);
break;
}
if (cqe->flags & IORING_CQE_F_BUFFER) {
int bid = cqe->flags >> 16;
if (bid > NREQS) {
fprintf(stderr, "Bad BID %d\n", bid);
return T_EXIT_FAIL;
}
} else {
fprintf(stderr, "No BID set!\n");
printf("ret=%d\n", cqe->res);
return T_EXIT_FAIL;
}
io_uring_cqe_seen(ring, cqe);
if (i > NREQS) {
fprintf(stderr, "Got too many requests?\n");
return T_EXIT_FAIL;
}
} while (i < NREQS);
pthread_join(t, &ret2);
free(buf);
io_uring_free_buf_ring(ring, br, NREQS, 1);
close(fd[0]);
close(fd[1]);
return T_EXIT_PASS;
}
static int test_mshot(struct io_uring *ring, struct data *d)
{
struct io_uring_buf_ring *br;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
int fd[2], ret, i;
pthread_t t;
void *buf, *ptr;
void *ret2;
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fd) < 0) {
perror("socketpair");
return T_EXIT_FAIL;
}
d->fd = fd[1];
if (posix_memalign((void **) &buf, 16384, BUF_SIZE * NREQS))
return T_EXIT_FAIL;
br = io_uring_setup_buf_ring(ring, NREQS, 1, 0, &ret);
if (!br) {
fprintf(stderr, "buf ring reg %d\n", ret);
return T_EXIT_FAIL;
}
ptr = buf;
for (i = 0; i < NREQS; i++) {
io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1,
io_uring_buf_ring_mask(NREQS), i);
ptr += BUF_SIZE;
}
io_uring_buf_ring_advance(br, NREQS);
pthread_create(&t, NULL, thread, d);
sqe = io_uring_get_sqe(ring);
io_uring_prep_recv_multishot(sqe, fd[0], NULL, 0, 0);
sqe->flags |= IOSQE_BUFFER_SELECT;
sqe->buf_group = 1;
pthread_barrier_wait(&d->barrier);
ret = io_uring_submit(ring);
if (ret != 1) {
fprintf(stderr, "submit %d\n", ret);
return T_EXIT_FAIL;
}
i = 0;
do {
ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
fprintf(stderr, "cqe wait %d\n", ret);
return T_EXIT_FAIL;
}
i++;
if (!(cqe->flags & IORING_CQE_F_MORE))
break;
if (cqe->res != BUF_SIZE) {
fprintf(stderr, "Bad cqe res %d\n", cqe->res);
break;
}
if (cqe->flags & IORING_CQE_F_BUFFER) {
int bid = cqe->flags >> 16;
if (bid > NREQS) {
fprintf(stderr, "Bad BID %d\n", bid);
return T_EXIT_FAIL;
}
} else {
fprintf(stderr, "No BID set!\n");
printf("ret=%d\n", cqe->res);
return T_EXIT_FAIL;
}
io_uring_cqe_seen(ring, cqe);
if (i > NREQS) {
fprintf(stderr, "Got too many requests?\n");
return T_EXIT_FAIL;
}
/*
* We're using unix sockets, and later kernels got support added
* for msg_inq querying. On those kernels, we cannot rely on
* the multishot terminating on a zero receive, as io_uring
* will not do that retry as it KNOWS there's zero bytes
* pending. Hence we need to actively quiet at that point. Inc
* 'i' as well as we don't get the non-MORE CQE posted.
*/
if (i == NREQS) {
i++;
break;
}
} while (1);
if (i != NREQS + 1) {
fprintf(stderr, "Only got %d requests\n", i);
return T_EXIT_FAIL;
}
pthread_join(t, &ret2);
io_uring_free_buf_ring(ring, br, NREQS, 1);
free(buf);
close(fd[0]);
close(fd[1]);
return T_EXIT_PASS;
}
int main(int argc, char *argv[])
{
struct io_uring ring;
struct data d;
int i, ret;
if (argc > 1)
return T_EXIT_SKIP;
pthread_barrier_init(&d.barrier, NULL, 2);
for (i = 0; i < 1000; i++) {
io_uring_queue_init(NREQS, &ring, 0);
ret = test(&ring, &d);
if (ret != T_EXIT_PASS) {
if (no_buf_ring)
break;
fprintf(stderr, "Test failed loop %d\n", i);
return T_EXIT_FAIL;
}
io_uring_queue_exit(&ring);
}
if (no_buf_ring)
return T_EXIT_SKIP;
for (i = 0; i < 1000; i++) {
io_uring_queue_init(NREQS, &ring, 0);
ret = test_mshot(&ring, &d);
if (ret != T_EXIT_PASS) {
fprintf(stderr, "Test mshot failed loop %d\n", i);
return T_EXIT_FAIL;
}
io_uring_queue_exit(&ring);
}
return T_EXIT_PASS;
}
|