Skip to content

Commit

Permalink
src/register: improve RLIMIT_NOFILE increase
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
axboe committed Sep 19, 2021
1 parent 8de1e67 commit 98db8e8
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions src/register.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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)
Expand Down

0 comments on commit 98db8e8

Please sign in to comment.