summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmmar Faizi <ammar.faizi@students.amikom.ac.id>2021-10-03 22:34:28 +0700
committerJens Axboe <axboe@kernel.dk>2021-10-03 09:58:39 -0600
commitcccf0fa1762aac3f14323fbfc5cef2c99a03efe4 (patch)
treec5c9756b1495f9ecb5af170f369b436f19e1169f
parentad3dcbde721fedc497042f7a51277dd23f779f2d (diff)
downloadliburing-cccf0fa1762aac3f14323fbfc5cef2c99a03efe4.tar.gz
liburing-cccf0fa1762aac3f14323fbfc5cef2c99a03efe4.tar.bz2
Wrap all syscalls in a kernel style return value
Add new syscall wrapper functions: 1) `uring_mmap` 2) `uring_munmap` 3) `uring_madvise` 4) `uring_getrlimit` 5) `uring_setrlimit` All of them are `static inline`. Use them to wrap the syscalls in a kernel style return value. The main purpose of this change is to make it possible to remove the dependency of `errno` variable in liburing C sources, so that later we can support build without libc. Extra new helpers: 1) `ERR_PTR()` 2) `PTR_ERR()` 3) `IS_ERR()` These helpers are used to deal with syscalls that return a pointer. Currently only `uring_mmap()` that depends on these. Link: https://github.com/axboe/liburing/issues/443#issuecomment-927873932 Cc: Bedirhan KURT <windowz414@gnuweeb.org> Suggested-by: Louvian Lyndal <louvianlyndal@gmail.com> Reviewed-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Ammar Faizi <ammar.faizi@students.amikom.ac.id> Link: https://lore.kernel.org/r/20211003153428.369258-4-ammar.faizi@students.amikom.ac.id Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--src/queue.c1
-rw-r--r--src/register.c10
-rw-r--r--src/setup.c53
-rw-r--r--src/syscall.h58
4 files changed, 90 insertions, 32 deletions
diff --git a/src/queue.c b/src/queue.c
index c2881e9..31aa17c 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -5,7 +5,6 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
-#include <errno.h>
#include <string.h>
#include <stdbool.h>
diff --git a/src/register.c b/src/register.c
index cb09dea..074223f 100644
--- a/src/register.c
+++ b/src/register.c
@@ -6,7 +6,6 @@
#include <sys/mman.h>
#include <sys/resource.h>
#include <unistd.h>
-#include <errno.h>
#include <string.h>
#include "liburing/compat.h"
@@ -104,13 +103,16 @@ int io_uring_register_files_update(struct io_uring *ring, unsigned off,
static int increase_rlimit_nofile(unsigned nr)
{
+ int ret;
struct rlimit rlim;
- if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
- return -errno;
+ ret = uring_getrlimit(RLIMIT_NOFILE, &rlim);
+ if (ret < 0)
+ return ret;
+
if (rlim.rlim_cur < nr) {
rlim.rlim_cur += nr;
- setrlimit(RLIMIT_NOFILE, &rlim);
+ uring_setrlimit(RLIMIT_NOFILE, &rlim);
}
return 0;
diff --git a/src/setup.c b/src/setup.c
index edfe94e..bdbf97c 100644
--- a/src/setup.c
+++ b/src/setup.c
@@ -3,9 +3,7 @@
#include <sys/types.h>
#include <sys/stat.h>
-#include <sys/mman.h>
#include <unistd.h>
-#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
@@ -18,9 +16,9 @@
static void io_uring_unmap_rings(struct io_uring_sq *sq, struct io_uring_cq *cq)
{
- munmap(sq->ring_ptr, sq->ring_sz);
+ uring_munmap(sq->ring_ptr, sq->ring_sz);
if (cq->ring_ptr && cq->ring_ptr != sq->ring_ptr)
- munmap(cq->ring_ptr, cq->ring_sz);
+ uring_munmap(cq->ring_ptr, cq->ring_sz);
}
static int io_uring_mmap(int fd, struct io_uring_params *p,
@@ -37,19 +35,21 @@ static int io_uring_mmap(int fd, struct io_uring_params *p,
sq->ring_sz = cq->ring_sz;
cq->ring_sz = sq->ring_sz;
}
- sq->ring_ptr = mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
- if (sq->ring_ptr == MAP_FAILED)
- return -errno;
+ sq->ring_ptr = uring_mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_POPULATE, fd,
+ IORING_OFF_SQ_RING);
+ if (IS_ERR(sq->ring_ptr))
+ return PTR_ERR(sq->ring_ptr);
if (p->features & IORING_FEAT_SINGLE_MMAP) {
cq->ring_ptr = sq->ring_ptr;
} else {
- cq->ring_ptr = mmap(0, cq->ring_sz, PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
- if (cq->ring_ptr == MAP_FAILED) {
+ cq->ring_ptr = uring_mmap(0, cq->ring_sz, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_POPULATE, fd,
+ IORING_OFF_CQ_RING);
+ if (IS_ERR(cq->ring_ptr)) {
+ ret = PTR_ERR(cq->ring_ptr);
cq->ring_ptr = NULL;
- ret = -errno;
goto err;
}
}
@@ -63,11 +63,10 @@ static int io_uring_mmap(int fd, struct io_uring_params *p,
sq->array = sq->ring_ptr + p->sq_off.array;
size = p->sq_entries * sizeof(struct io_uring_sqe);
- sq->sqes = mmap(0, size, PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_POPULATE, fd,
- IORING_OFF_SQES);
- if (sq->sqes == MAP_FAILED) {
- ret = -errno;
+ sq->sqes = uring_mmap(0, size, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
+ if (IS_ERR(sq->sqes)) {
+ ret = PTR_ERR(sq->sqes);
err:
io_uring_unmap_rings(sq, cq);
return ret;
@@ -116,20 +115,20 @@ int io_uring_ring_dontfork(struct io_uring *ring)
return -EINVAL;
len = *ring->sq.kring_entries * sizeof(struct io_uring_sqe);
- ret = madvise(ring->sq.sqes, len, MADV_DONTFORK);
- if (ret == -1)
- return -errno;
+ ret = uring_madvise(ring->sq.sqes, len, MADV_DONTFORK);
+ if (ret < 0)
+ return ret;
len = ring->sq.ring_sz;
- ret = madvise(ring->sq.ring_ptr, len, MADV_DONTFORK);
- if (ret == -1)
- return -errno;
+ ret = uring_madvise(ring->sq.ring_ptr, len, MADV_DONTFORK);
+ if (ret < 0)
+ return ret;
if (ring->cq.ring_ptr != ring->sq.ring_ptr) {
len = ring->cq.ring_sz;
- ret = madvise(ring->cq.ring_ptr, len, MADV_DONTFORK);
- if (ret == -1)
- return -errno;
+ ret = uring_madvise(ring->cq.ring_ptr, len, MADV_DONTFORK);
+ if (ret < 0)
+ return ret;
}
return 0;
@@ -173,7 +172,7 @@ void io_uring_queue_exit(struct io_uring *ring)
struct io_uring_sq *sq = &ring->sq;
struct io_uring_cq *cq = &ring->cq;
- munmap(sq->sqes, *sq->kring_entries * sizeof(struct io_uring_sqe));
+ uring_munmap(sq->sqes, *sq->kring_entries * sizeof(struct io_uring_sqe));
io_uring_unmap_rings(sq, cq);
close(ring->ring_fd);
}
diff --git a/src/syscall.h b/src/syscall.h
index f7f63aa..a9dd280 100644
--- a/src/syscall.h
+++ b/src/syscall.h
@@ -4,11 +4,15 @@
#include <errno.h>
#include <signal.h>
+#include <stdint.h>
#include <unistd.h>
+#include <stdbool.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/resource.h>
+#include <liburing.h>
+
#ifdef __alpha__
/*
* alpha and mips are exception, other architectures have
@@ -59,7 +63,20 @@ int __sys_io_uring_enter2(int fd, unsigned to_submit, unsigned min_complete,
int __sys_io_uring_register(int fd, unsigned int opcode, const void *arg,
unsigned int nr_args);
+static inline void *ERR_PTR(intptr_t n)
+{
+ return (void *) n;
+}
+static inline intptr_t PTR_ERR(void *ptr)
+{
+ return (intptr_t) ptr;
+}
+
+static inline bool IS_ERR(void *ptr)
+{
+ return uring_unlikely((uintptr_t) ptr >= (uintptr_t) -4095UL);
+}
static inline int ____sys_io_uring_register(int fd, unsigned opcode,
const void *arg, unsigned nr_args)
@@ -98,4 +115,45 @@ static inline int ____sys_io_uring_enter(int fd, unsigned to_submit,
_NSIG / 8);
}
+static inline void *uring_mmap(void *addr, size_t length, int prot, int flags,
+ int fd, off_t offset)
+{
+ void *ret;
+
+ ret = mmap(addr, length, prot, flags, fd, offset);
+ return (ret == MAP_FAILED) ? ERR_PTR(-errno) : ret;
+}
+
+static inline int uring_munmap(void *addr, size_t length)
+{
+ int ret;
+
+ ret = munmap(addr, length);
+ return (ret < 0) ? -errno : ret;
+}
+
+static inline int uring_madvise(void *addr, size_t length, int advice)
+{
+ int ret;
+
+ ret = madvise(addr, length, advice);
+ return (ret < 0) ? -errno : ret;
+}
+
+static inline int uring_getrlimit(int resource, struct rlimit *rlim)
+{
+ int ret;
+
+ ret = getrlimit(resource, rlim);
+ return (ret < 0) ? -errno : ret;
+}
+
+static inline int uring_setrlimit(int resource, const struct rlimit *rlim)
+{
+ int ret;
+
+ ret = setrlimit(resource, rlim);
+ return (ret < 0) ? -errno : ret;
+}
+
#endif