2019-01-08 06:51:07 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "io_uring.h"
|
|
|
|
|
#include "liburing.h"
|
|
|
|
|
#include "barrier.h"
|
|
|
|
|
|
2019-01-08 15:14:07 -07:00
|
|
|
static int __io_uring_get_completion(int fd, struct io_uring_cq *cq,
|
2019-01-09 15:26:20 -07:00
|
|
|
struct io_uring_cqe **cqe_ptr, int wait)
|
2019-01-08 06:51:07 -07:00
|
|
|
{
|
|
|
|
|
const unsigned mask = *cq->kring_mask;
|
|
|
|
|
unsigned head;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
*cqe_ptr = NULL;
|
2019-01-08 06:51:07 -07:00
|
|
|
head = *cq->khead;
|
|
|
|
|
do {
|
|
|
|
|
read_barrier();
|
|
|
|
|
if (head != *cq->ktail) {
|
2019-01-09 15:26:20 -07:00
|
|
|
*cqe_ptr = &cq->cqes[head & mask];
|
2019-01-08 06:51:07 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2019-01-08 15:14:07 -07:00
|
|
|
if (!wait)
|
|
|
|
|
break;
|
2019-01-08 06:51:07 -07:00
|
|
|
ret = io_uring_enter(fd, 0, 1, IORING_ENTER_GETEVENTS);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
} while (1);
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
if (*cqe_ptr) {
|
2019-01-08 06:51:07 -07:00
|
|
|
*cq->khead = head + 1;
|
|
|
|
|
write_barrier();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 15:14:07 -07:00
|
|
|
/*
|
|
|
|
|
* Return an IO completion, if one is readily available
|
|
|
|
|
*/
|
2019-01-08 15:59:09 -07:00
|
|
|
int io_uring_get_completion(struct io_uring *ring,
|
2019-01-09 15:26:20 -07:00
|
|
|
struct io_uring_cqe **cqe_ptr)
|
2019-01-08 15:14:07 -07:00
|
|
|
{
|
2019-01-09 15:26:20 -07:00
|
|
|
return __io_uring_get_completion(ring->ring_fd, &ring->cq, cqe_ptr, 0);
|
2019-01-08 15:14:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return an IO completion, waiting for it if necessary
|
|
|
|
|
*/
|
2019-01-08 15:59:09 -07:00
|
|
|
int io_uring_wait_completion(struct io_uring *ring,
|
2019-01-09 15:26:20 -07:00
|
|
|
struct io_uring_cqe **cqe_ptr)
|
2019-01-08 15:14:07 -07:00
|
|
|
{
|
2019-01-09 15:26:20 -07:00
|
|
|
return __io_uring_get_completion(ring->ring_fd, &ring->cq, cqe_ptr, 1);
|
2019-01-08 15:14:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-08 06:51:07 -07:00
|
|
|
/*
|
2019-01-09 15:26:20 -07:00
|
|
|
* Submit sqes acquired from io_uring_get_sqe() to the kernel.
|
2019-01-08 06:51:07 -07:00
|
|
|
*
|
2019-01-09 15:26:20 -07:00
|
|
|
* Returns number of sqes submitted
|
2019-01-08 06:51:07 -07:00
|
|
|
*/
|
2019-01-08 15:59:09 -07:00
|
|
|
int io_uring_submit(struct io_uring *ring)
|
2019-01-08 06:51:07 -07:00
|
|
|
{
|
2019-01-08 15:31:35 -07:00
|
|
|
struct io_uring_sq *sq = &ring->sq;
|
2019-01-08 06:51:07 -07:00
|
|
|
const unsigned mask = *sq->kring_mask;
|
|
|
|
|
unsigned ktail, ktail_next, submitted;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If we have pending IO in the kring, submit it first
|
|
|
|
|
*/
|
|
|
|
|
read_barrier();
|
|
|
|
|
if (*sq->khead != *sq->ktail) {
|
|
|
|
|
submitted = *sq->kring_entries;
|
|
|
|
|
goto submit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
if (sq->sqe_head == sq->sqe_tail)
|
2019-01-08 06:51:07 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/*
|
2019-01-09 15:26:20 -07:00
|
|
|
* Fill in sqes that we have queued up, adding them to the kernel ring
|
2019-01-08 06:51:07 -07:00
|
|
|
*/
|
|
|
|
|
submitted = 0;
|
|
|
|
|
ktail = ktail_next = *sq->ktail;
|
2019-01-09 15:26:20 -07:00
|
|
|
while (sq->sqe_head < sq->sqe_tail) {
|
2019-01-08 06:51:07 -07:00
|
|
|
ktail_next++;
|
|
|
|
|
read_barrier();
|
|
|
|
|
if (ktail_next == *sq->khead)
|
|
|
|
|
break;
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
sq->array[ktail & mask] = sq->sqe_head & mask;
|
2019-01-08 06:51:07 -07:00
|
|
|
ktail = ktail_next;
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
sq->sqe_head++;
|
2019-01-08 06:51:07 -07:00
|
|
|
submitted++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!submitted)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (*sq->ktail != ktail) {
|
|
|
|
|
write_barrier();
|
|
|
|
|
*sq->ktail = ktail;
|
|
|
|
|
write_barrier();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
submit:
|
2019-01-08 15:59:09 -07:00
|
|
|
return io_uring_enter(ring->ring_fd, submitted, 0,
|
|
|
|
|
IORING_ENTER_GETEVENTS);
|
2019-01-08 06:51:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2019-01-09 15:26:20 -07:00
|
|
|
* Return an sqe to fill. Application must later call io_uring_submit()
|
2019-01-08 06:51:07 -07:00
|
|
|
* when it's ready to tell the kernel about it. The caller may call this
|
|
|
|
|
* function multiple times before calling io_uring_submit().
|
|
|
|
|
*
|
2019-01-09 15:26:20 -07:00
|
|
|
* Returns a vacant sqe, or NULL if we're full.
|
2019-01-08 06:51:07 -07:00
|
|
|
*/
|
2019-01-09 15:26:20 -07:00
|
|
|
struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring)
|
2019-01-08 06:51:07 -07:00
|
|
|
{
|
2019-01-08 15:31:35 -07:00
|
|
|
struct io_uring_sq *sq = &ring->sq;
|
2019-01-09 15:26:20 -07:00
|
|
|
unsigned next = sq->sqe_tail + 1;
|
|
|
|
|
struct io_uring_sqe *sqe;
|
2019-01-08 06:51:07 -07:00
|
|
|
|
|
|
|
|
/*
|
2019-01-09 15:26:20 -07:00
|
|
|
* All sqes are used
|
2019-01-08 06:51:07 -07:00
|
|
|
*/
|
2019-01-09 15:26:20 -07:00
|
|
|
if (next - sq->sqe_head > *sq->kring_entries)
|
2019-01-08 06:51:07 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask];
|
|
|
|
|
sq->sqe_tail = next;
|
|
|
|
|
return sqe;
|
2019-01-08 06:51:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int io_uring_mmap(int fd, struct io_uring_params *p,
|
|
|
|
|
struct io_uring_sq *sq, struct io_uring_cq *cq)
|
|
|
|
|
{
|
|
|
|
|
size_t size;
|
|
|
|
|
void *ptr;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
sq->ring_sz = p->sq_off.array + p->sq_entries * sizeof(unsigned);
|
|
|
|
|
ptr = mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
|
|
|
|
|
if (ptr == MAP_FAILED)
|
|
|
|
|
return -errno;
|
|
|
|
|
sq->khead = ptr + p->sq_off.head;
|
|
|
|
|
sq->ktail = ptr + p->sq_off.tail;
|
|
|
|
|
sq->kring_mask = ptr + p->sq_off.ring_mask;
|
|
|
|
|
sq->kring_entries = ptr + p->sq_off.ring_entries;
|
|
|
|
|
sq->kflags = ptr + p->sq_off.flags;
|
|
|
|
|
sq->kdropped = ptr + p->sq_off.dropped;
|
|
|
|
|
sq->array = ptr + p->sq_off.array;
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
size = p->sq_entries * sizeof(struct io_uring_sqe),
|
|
|
|
|
sq->sqes = mmap(0, size, PROT_READ | PROT_WRITE,
|
2019-01-08 06:51:07 -07:00
|
|
|
MAP_SHARED | MAP_POPULATE, fd,
|
2019-01-09 15:26:20 -07:00
|
|
|
IORING_OFF_SQES);
|
|
|
|
|
if (sq->sqes == MAP_FAILED) {
|
2019-01-08 06:51:07 -07:00
|
|
|
ret = -errno;
|
|
|
|
|
err:
|
|
|
|
|
munmap(sq->khead, sq->ring_sz);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
cq->ring_sz = p->cq_off.cqes + p->cq_entries * sizeof(struct io_uring_cqe);
|
2019-01-08 06:51:07 -07:00
|
|
|
ptr = mmap(0, cq->ring_sz, PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
|
|
|
|
|
if (ptr == MAP_FAILED) {
|
|
|
|
|
ret = -errno;
|
2019-01-09 15:26:20 -07:00
|
|
|
munmap(sq->sqes, p->sq_entries * sizeof(struct io_uring_sqe));
|
2019-01-08 06:51:07 -07:00
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
cq->khead = ptr + p->cq_off.head;
|
|
|
|
|
cq->ktail = ptr + p->cq_off.tail;
|
|
|
|
|
cq->kring_mask = ptr + p->cq_off.ring_mask;
|
|
|
|
|
cq->kring_entries = ptr + p->cq_off.ring_entries;
|
|
|
|
|
cq->koverflow = ptr + p->cq_off.overflow;
|
2019-01-09 15:26:20 -07:00
|
|
|
cq->cqes = ptr + p->cq_off.cqes;
|
2019-01-08 15:59:09 -07:00
|
|
|
return 0;
|
2019-01-08 06:51:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2019-01-08 15:59:09 -07:00
|
|
|
* Returns -1 on error, or zero on success. On success, 'ring'
|
|
|
|
|
* contains the necessary information to read/write to the rings.
|
2019-01-08 06:51:07 -07:00
|
|
|
*/
|
|
|
|
|
int io_uring_queue_init(unsigned entries, struct io_uring_params *p,
|
2019-01-10 09:32:41 -07:00
|
|
|
struct iovec *iovecs, unsigned nr_iovecs,
|
|
|
|
|
struct io_uring *ring)
|
2019-01-08 06:51:07 -07:00
|
|
|
{
|
2019-01-08 15:59:09 -07:00
|
|
|
int fd, ret;
|
2019-01-08 06:51:07 -07:00
|
|
|
|
2019-01-10 09:32:41 -07:00
|
|
|
fd = io_uring_setup(entries, iovecs, nr_iovecs, p);
|
2019-01-08 06:51:07 -07:00
|
|
|
if (fd < 0)
|
|
|
|
|
return fd;
|
|
|
|
|
|
2019-01-08 15:31:35 -07:00
|
|
|
memset(ring, 0, sizeof(*ring));
|
2019-01-08 15:59:09 -07:00
|
|
|
ret = io_uring_mmap(fd, p, &ring->sq, &ring->cq);
|
|
|
|
|
if (!ret)
|
|
|
|
|
ring->ring_fd = fd;
|
|
|
|
|
return ret;
|
2019-01-08 06:51:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-08 15:59:09 -07:00
|
|
|
void io_uring_queue_exit(struct io_uring *ring)
|
2019-01-08 06:51:07 -07:00
|
|
|
{
|
2019-01-08 15:31:35 -07:00
|
|
|
struct io_uring_sq *sq = &ring->sq;
|
|
|
|
|
struct io_uring_cq *cq = &ring->cq;
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
munmap(sq->sqes, *sq->kring_entries * sizeof(struct io_uring_sqe));
|
2019-01-08 06:51:07 -07:00
|
|
|
munmap(sq->khead, sq->ring_sz);
|
|
|
|
|
munmap(cq->khead, cq->ring_sz);
|
2019-01-08 15:59:09 -07:00
|
|
|
close(ring->ring_fd);
|
2019-01-08 06:51:07 -07:00
|
|
|
}
|