summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2021-09-18 16:18:37 -0600
committerJens Axboe <axboe@kernel.dk>2021-09-18 16:18:37 -0600
commitd9743fec01714a3ea5352db312564a5b3d6ff36d (patch)
treeaf855dae70a48b425b716143fda73b3b5ffda07a
parenta4a314a56d723385d3cbe8cdeac36da3fef40d42 (diff)
downloadliburing-d9743fec01714a3ea5352db312564a5b3d6ff36d.tar.gz
liburing-d9743fec01714a3ea5352db312564a5b3d6ff36d.tar.bz2
src/register: automatically increase RLIMIT_NOFILE, if necessary
A recent change, that also hit stable, means that any file table sizing will be under RLIMIT_NOFILE as well. We can just adjust this as needed if someone wants to register a fileset and the count is larger than RLIMIT_NOFILE rlim_cur. If it's beyond rlim_max, then we return -EMFILE as the kernel would have done. Reported-by: Victor Stewart <v@nametag.social> Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--src/register.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/register.c b/src/register.c
index a4e8c37..bab42d0 100644
--- a/src/register.c
+++ b/src/register.c
@@ -4,6 +4,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
+#include <sys/resource.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
@@ -118,6 +119,21 @@ int io_uring_register_files_update(struct io_uring *ring, unsigned off,
return ret;
}
+static int bump_rlimit_nofile(unsigned nr)
+{
+ struct rlimit rlim;
+
+ if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
+ return -errno;
+ if (rlim.rlim_cur < nr) {
+ if (nr > rlim.rlim_max)
+ return -EMFILE;
+ rlim.rlim_cur = nr;
+ setrlimit(RLIMIT_NOFILE, &rlim);
+ }
+
+ return 0;
+}
int io_uring_register_files_tags(struct io_uring *ring,
const int *files, const __u64 *tags,
@@ -130,6 +146,10 @@ int io_uring_register_files_tags(struct io_uring *ring,
};
int ret;
+ ret = bump_rlimit_nofile(nr);
+ if (ret)
+ return ret;
+
ret = __sys_io_uring_register(ring->ring_fd, IORING_REGISTER_FILES2,
&reg, sizeof(reg));
return ret < 0 ? -errno : ret;
@@ -140,6 +160,10 @@ int io_uring_register_files(struct io_uring *ring, const int *files,
{
int ret;
+ ret = bump_rlimit_nofile(nr_files);
+ if (ret)
+ return ret;
+
ret = __sys_io_uring_register(ring->ring_fd, IORING_REGISTER_FILES,
files, nr_files);
if (ret < 0)