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
|
/* SPDX-License-Identifier: MIT */
/*
* Test reads that will punt to blocking context, with immediate overwrite
* of iovec->iov_base to NULL. If the kernel doesn't properly handle
* reuse of the iovec, we should get -EFAULT.
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include "helpers.h"
#include "liburing.h"
#define STR_SIZE 32768
#define FILE_SIZE 65536
struct thread_data {
int fd1, fd2;
volatile int do_exit;
};
static void *flusher(void *__data)
{
struct thread_data *data = __data;
while (!data->do_exit) {
posix_fadvise(data->fd1, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
posix_fadvise(data->fd2, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
usleep(10);
}
return NULL;
}
static char str1[STR_SIZE];
static char str2[STR_SIZE];
static struct io_uring ring;
static int no_stable;
static int prep(int fd, char *str, int split, int async)
{
struct io_uring_sqe *sqe;
struct iovec iovs[16];
int ret, i;
if (split) {
int vsize = STR_SIZE / 16;
void *ptr = str;
for (i = 0; i < 16; i++) {
iovs[i].iov_base = ptr;
iovs[i].iov_len = vsize;
ptr += vsize;
}
} else {
iovs[0].iov_base = str;
iovs[0].iov_len = STR_SIZE;
}
sqe = io_uring_get_sqe(&ring);
io_uring_prep_readv(sqe, fd, iovs, split ? 16 : 1, 0);
sqe->user_data = fd;
if (async)
sqe->flags = IOSQE_ASYNC;
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "submit got %d\n", ret);
return 1;
}
if (split) {
for (i = 0; i < 16; i++)
iovs[i].iov_base = NULL;
} else {
iovs[0].iov_base = NULL;
}
return 0;
}
static int wait_nr(int nr)
{
int i, ret;
for (i = 0; i < nr; i++) {
struct io_uring_cqe *cqe;
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret)
return ret;
if (cqe->res < 0) {
fprintf(stderr, "cqe->res=%d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(&ring, cqe);
}
return 0;
}
static int test_reuse(int argc, char *argv[], int split, int async)
{
struct thread_data data;
struct io_uring_params p = { };
int fd1, fd2, ret, i;
struct timeval tv;
pthread_t thread;
char *fname1 = ".reuse.1";
int do_unlink = 1;
void *tret;
ret = io_uring_queue_init_params(32, &ring, &p);
if (ret) {
fprintf(stderr, "io_uring_queue_init: %d\n", ret);
return 1;
}
if (!(p.features & IORING_FEAT_SUBMIT_STABLE)) {
fprintf(stdout, "FEAT_SUBMIT_STABLE not there, skipping\n");
io_uring_queue_exit(&ring);
no_stable = 1;
return 0;
}
if (argc > 1) {
fname1 = argv[1];
do_unlink = 0;
} else {
t_create_file(fname1, FILE_SIZE);
}
fd1 = open(fname1, O_RDONLY);
if (do_unlink)
unlink(fname1);
if (fd1 < 0) {
if (errno == EPERM || errno == EACCES)
return T_EXIT_SKIP;
perror("open fname1");
goto err;
}
t_create_file(".reuse.2", FILE_SIZE);
fd2 = open(".reuse.2", O_RDONLY);
unlink(".reuse.2");
if (fd2 < 0) {
perror("open .reuse.2");
goto err;
}
data.fd1 = fd1;
data.fd2 = fd2;
data.do_exit = 0;
pthread_create(&thread, NULL, flusher, &data);
usleep(10000);
gettimeofday(&tv, NULL);
for (i = 0; i < 1000; i++) {
ret = prep(fd1, str1, split, async);
if (ret) {
fprintf(stderr, "prep1 failed: %d\n", ret);
goto err;
}
ret = prep(fd2, str2, split, async);
if (ret) {
fprintf(stderr, "prep1 failed: %d\n", ret);
goto err;
}
ret = wait_nr(2);
if (ret) {
fprintf(stderr, "wait_nr: %d\n", ret);
goto err;
}
if (mtime_since_now(&tv) > 5000)
break;
}
data.do_exit = 1;
pthread_join(thread, &tret);
close(fd2);
close(fd1);
io_uring_queue_exit(&ring);
return 0;
err:
io_uring_queue_exit(&ring);
return 1;
}
int main(int argc, char *argv[])
{
int ret, i;
for (i = 0; i < 4; i++) {
int split, async;
split = (i & 1) != 0;
async = (i & 2) != 0;
ret = test_reuse(argc, argv, split, async);
if (ret == T_EXIT_SKIP)
continue;
if (ret) {
fprintf(stderr, "test_reuse %d %d failed\n", split, async);
return ret;
}
if (no_stable)
break;
}
return 0;
}
|