Skip to content

Commit 98db8e8

Browse files
committed
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>
1 parent 8de1e67 commit 98db8e8

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

src/register.c

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,14 @@ int io_uring_register_files_update(struct io_uring *ring, unsigned off,
119119
return ret;
120120
}
121121

122-
static int bump_rlimit_nofile(unsigned nr)
122+
static int increase_rlimit_nofile(unsigned nr)
123123
{
124124
struct rlimit rlim;
125125

126126
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
127127
return -errno;
128128
if (rlim.rlim_cur < nr) {
129-
if (nr > rlim.rlim_max)
130-
return -EMFILE;
131-
rlim.rlim_cur = nr;
129+
rlim.rlim_cur += nr;
132130
setrlimit(RLIMIT_NOFILE, &rlim);
133131
}
134132

@@ -144,32 +142,45 @@ int io_uring_register_files_tags(struct io_uring *ring,
144142
.data = (unsigned long)files,
145143
.tags = (unsigned long)tags,
146144
};
147-
int ret;
148-
149-
ret = bump_rlimit_nofile(nr);
150-
if (ret)
151-
return ret;
145+
int ret, did_increase = 0;
146+
147+
do {
148+
ret = __sys_io_uring_register(ring->ring_fd,
149+
IORING_REGISTER_FILES2, &reg,
150+
sizeof(reg));
151+
if (ret >= 0)
152+
break;
153+
if (errno == EMFILE && !did_increase) {
154+
did_increase = 1;
155+
increase_rlimit_nofile(nr);
156+
continue;
157+
}
158+
break;
159+
} while (1);
152160

153-
ret = __sys_io_uring_register(ring->ring_fd, IORING_REGISTER_FILES2,
154-
&reg, sizeof(reg));
155161
return ret < 0 ? -errno : ret;
156162
}
157163

158164
int io_uring_register_files(struct io_uring *ring, const int *files,
159165
unsigned nr_files)
160166
{
161-
int ret;
162-
163-
ret = bump_rlimit_nofile(nr_files);
164-
if (ret)
165-
return ret;
167+
int ret, did_increase = 0;
168+
169+
do {
170+
ret = __sys_io_uring_register(ring->ring_fd,
171+
IORING_REGISTER_FILES, files,
172+
nr_files);
173+
if (ret >= 0)
174+
break;
175+
if (errno == EMFILE && !did_increase) {
176+
did_increase = 1;
177+
increase_rlimit_nofile(nr_files);
178+
continue;
179+
}
180+
break;
181+
} while (1);
166182

167-
ret = __sys_io_uring_register(ring->ring_fd, IORING_REGISTER_FILES,
168-
files, nr_files);
169-
if (ret < 0)
170-
return -errno;
171-
172-
return 0;
183+
return ret < 0 ? -errno : ret;
173184
}
174185

175186
int io_uring_unregister_files(struct io_uring *ring)

0 commit comments

Comments
 (0)