summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2021-09-19 08:15:07 -0600
committerJens Axboe <axboe@kernel.dk>2021-09-19 08:15:07 -0600
commit98db8e883c0fedcfbf74ba7922821ff3be0e7c98 (patch)
tree45fe05c23e8cc5fa583ec698992a3530d117a557
parent8de1e67876468f1ba123af7266ecf3d5ad57995b (diff)
downloadliburing-98db8e883c0fedcfbf74ba7922821ff3be0e7c98.tar.gz
liburing-98db8e883c0fedcfbf74ba7922821ff3be0e7c98.tar.bz2
src/register: improve RLIMIT_NOFILE increase
Only do it if we run into EMFILE, seems like the prudent thing to do, avoiding any getrlimit checking if the operation succeeds. And more importantly, add to the rlim_cur, don't just set it to what we need. Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--src/register.c55
1 files changed, 33 insertions, 22 deletions
diff --git a/src/register.c b/src/register.c
index bab42d0..5ea4331 100644
--- a/src/register.c
+++ b/src/register.c
@@ -119,16 +119,14 @@ int io_uring_register_files_update(struct io_uring *ring, unsigned off,
return ret;
}
-static int bump_rlimit_nofile(unsigned nr)
+static int increase_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;
+ rlim.rlim_cur += nr;
setrlimit(RLIMIT_NOFILE, &rlim);
}
@@ -144,32 +142,45 @@ int io_uring_register_files_tags(struct io_uring *ring,
.data = (unsigned long)files,
.tags = (unsigned long)tags,
};
- int ret;
-
- ret = bump_rlimit_nofile(nr);
- if (ret)
- return ret;
+ int ret, did_increase = 0;
+
+ do {
+ ret = __sys_io_uring_register(ring->ring_fd,
+ IORING_REGISTER_FILES2, &reg,
+ sizeof(reg));
+ if (ret >= 0)
+ break;
+ if (errno == EMFILE && !did_increase) {
+ did_increase = 1;
+ increase_rlimit_nofile(nr);
+ continue;
+ }
+ break;
+ } while (1);
- ret = __sys_io_uring_register(ring->ring_fd, IORING_REGISTER_FILES2,
- &reg, sizeof(reg));
return ret < 0 ? -errno : ret;
}
int io_uring_register_files(struct io_uring *ring, const int *files,
unsigned nr_files)
{
- int ret;
-
- ret = bump_rlimit_nofile(nr_files);
- if (ret)
- return ret;
+ int ret, did_increase = 0;
+
+ do {
+ ret = __sys_io_uring_register(ring->ring_fd,
+ IORING_REGISTER_FILES, files,
+ nr_files);
+ if (ret >= 0)
+ break;
+ if (errno == EMFILE && !did_increase) {
+ did_increase = 1;
+ increase_rlimit_nofile(nr_files);
+ continue;
+ }
+ break;
+ } while (1);
- ret = __sys_io_uring_register(ring->ring_fd, IORING_REGISTER_FILES,
- files, nr_files);
- if (ret < 0)
- return -errno;
-
- return 0;
+ return ret < 0 ? -errno : ret;
}
int io_uring_unregister_files(struct io_uring *ring)