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
|
/*
* Copyright 2015, Red Hat, Inc.
*
* This test remaps the aio ring buffer and ensures that I/O completions
* can still be reaped from userspace.
*
* Author: Jeff Moyer <jmoyer@redhat.com>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <signal.h>
#include <sched.h>
#include <libaio.h>
#define BUFLEN 4096
#define TEMPLATE "19.XXXXXX"
volatile sig_atomic_t timed_out = 0;
struct aio_ring {
unsigned id; /* kernel internal index number */
unsigned nr; /* number of io_events */
volatile unsigned head;
volatile unsigned tail;
unsigned magic;
unsigned compat_features;
unsigned incompat_features;
unsigned header_length; /* size of aio_ring */
struct io_event io_events[0];
};
int
open_temp_file(void)
{
int fd;
char template[sizeof(TEMPLATE)];
strncpy(template, TEMPLATE, sizeof(template));
fd = mkostemp(template, O_DIRECT);
if (fd < 0 && errno == EINVAL) {
strncpy(template, TEMPLATE, sizeof(template));
fd = mkstemp(template);
}
if (fd < 0) {
perror("mkstemp");
exit(1);
}
unlink(template);
return fd;
}
/*
* mmap will do the address space search for us. when remapping the ring,
* the use of MREMAP_FIXED will cause this mapping to be unmapped.
*
* len - length in bytes
*
* Returns the available virtual address, or MAP_FAILED on error.
*/
void *
find_unused_va(size_t len)
{
return mmap(0, len, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
}
void
alarm_handler(int __attribute__((unused))signo)
{
timed_out = 1;
}
int
user_getevents(io_context_t ctx, int nr_events, struct io_event *event)
{
struct aio_ring *ring = (void *)ctx;
int completed = 0;
timed_out = 0;
signal(SIGALRM, alarm_handler);
alarm(30);
while ((completed < nr_events) && !timed_out) {
unsigned new_head;
if (ring->head == ring->tail) {
sched_yield();
continue;
}
new_head = ring->head;
*event = ring->io_events[new_head];
new_head += 1;
new_head %= ring->nr;
ring->head = new_head;
completed++;
}
alarm(0);
signal(SIGALRM, SIG_DFL);
return completed;
}
struct aio_ring *
remap_ring(struct aio_ring *ring)
{
struct aio_ring *new_ring;
size_t ring_size;
/*
* No need to round up to page size as ring->nr was adjusted
* already to fill the last page in the ring.
*/
ring_size = sizeof(struct aio_ring) + ring->nr * sizeof(struct io_event);
/*
* Remap the ring.
*/
new_ring = find_unused_va(ring_size);
if (new_ring == MAP_FAILED) {
fprintf(stderr, "Unable to find suitable va for ring\n");
return NULL;
}
new_ring = mremap(ring, ring_size, ring_size,
MREMAP_FIXED|MREMAP_MAYMOVE, new_ring);
if (new_ring == MAP_FAILED || new_ring == ring) {
perror("mremap");
return NULL;
}
return new_ring;
}
io_context_t
remap_io_context(io_context_t ctx)
{
struct aio_ring *ring, *new_ring;
ring = (void *)ctx;
new_ring = remap_ring(ring);
if (!new_ring)
return NULL;
ctx = (io_context_t)new_ring;
return ctx;
}
int
do_io(io_context_t ctx, struct iocb *iocbp, int fd)
{
int ret;
char buf[BUFLEN];
io_prep_pwrite(iocbp, fd, buf, BUFLEN, 0);
ret = io_submit(ctx, 1, &iocbp);
if (ret != 1) {
fprintf(stderr, "io_submit failed with %d\n", ret);
return 1;
}
return 0;
}
int
check_completion(io_context_t ctx, struct iocb *iocbp)
{
int ret;
struct io_event event;
ret = user_getevents(ctx, 1, &event);
if (ret != 1) {
fprintf(stderr, "user_getevents timed out.\n");
return 1;
}
if (event.obj != iocbp) {
fprintf(stderr,
"Error: event->opj (%p) does not match iocbp (%p)\n",
event.obj, iocbp);
return 1;
}
return 0;
}
int
test_main()
{
int fd;
int ret;
io_context_t ctx;
struct iocb iocb;
memset(&ctx, 0, sizeof(ctx));
fd = open_temp_file();
ret = io_setup(1, &ctx);
if (ret != 0) {
fprintf(stderr, "io_setup failed with %d\n", ret);
return 1;
}
/*
* First, try remapping the ring buffer in-between io_setup and
* io_submit.
*/
ctx = remap_io_context(ctx);
if (ctx == NULL)
return 1;
ret = do_io(ctx, &iocb, fd);
if (ret != 0)
return 1;
ret = check_completion(ctx, &iocb);
if (ret != 0)
return 1;
/*
* Now remap the ring in between io_submit and getevents.
*/
ret = do_io(ctx, &iocb, fd);
if (ret != 0)
return 1;
ctx = remap_io_context(ctx);
if (ctx == NULL)
return 1;
ret = check_completion(ctx, &iocb);
if (ret != 0)
return 1;
/*
* Success, clean up.
*/
ret = io_destroy(ctx);
if (ret != 0) {
fprintf(stderr, "io_destroy failed with %d\n", ret);
return 1;
}
close(fd);
return 0;
}
/*
* Local variables:
* mode: c
* c-basic-offset: 8
* End:
*/
|