From: Nixiaoming <nixiaoming@huawei.com>
To: "arnd@arndb.de" <arnd@arndb.de>,
"viro@zeniv.linux.org.uk" <viro@zeniv.linux.org.uk>,
"brauner@kernel.org" <brauner@kernel.org>,
"jack@suse.cz" <jack@suse.cz>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-fsdevel@vger.kernel.org" <linux-fsdevel@vger.kernel.org>,
"linux-arch@vger.kernel.org" <linux-arch@vger.kernel.org>,
"weiyongjun (A)" <weiyongjun1@huawei.com>,
"Liuyang (Young,C)" <young.liuyang@huawei.com>
Subject: [RFC] RLIMIT_NOFILE: the maximum number of open files or the maximum fd index?
Date: Tue, 24 Dec 2024 01:20:15 +0000 [thread overview]
Message-ID: <4731d54723b841599882a24f7aa73aaa@huawei.com> (raw)
I always thought that RLIMIT_NOFILE limits the number of open files, but when I
read the code for alloc_fd(), I found that RLIMIT_NOFILE is the largest fd index?
Is this a mistake in my understanding, or is it a code implementation error?
-----
alloc_fd code:
diff --git a/fs/file.c b/fs/file.c
index fb1011c..e47ddac 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -561,6 +561,7 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
*/
error = -EMFILE;
if (unlikely(fd >= end))
+ // There may be unclosed fd between [end, max]. the number of open files can be greater than RLIMIT_NOFILE.
goto out;
if (unlikely(fd >= fdt->max_fds)) {
-----
Test Procedure
1. ulimit -n 1024.
2. Create 1000 FDs.
3. ulimit -n 100.
4. Close all FDs less than 100 and continue to hold FDs greater than 100.
5. Open() and check whether the FD is successfully created,
If RLIMIT_NOFILE is the upper limit of the number of opened files, step 5 should fail, but step 5 returns success.
-----
test code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int fd, i;
struct rlimit rl;
rl.rlim_cur = 1024;
rl.rlim_max = 1024;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
perror("setrlimit");
exit(EXIT_FAILURE);
}
for (i = 0; i < 1000; i++) {
fd = open("/dev/null", O_RDWR | O_CLOEXEC);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
}
rl.rlim_cur = 100;
rl.rlim_max = 100;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
perror("setrlimit");
exit(EXIT_FAILURE);
}
for (i = 3; i < 100; i++) {
close(i);
}
fd = open("/dev/null", O_RDWR | O_CLOEXEC);
if (fd == -1) {
if (errno == EMFILE) {
printf("ok\n");
return 0;
} else {
perror("open");
exit(EXIT_FAILURE);
}
} else {
printf("fail: {OPEN_MAX} file descriptors are currently open in the calling process, but open() still returns a success\n");
return -1;
}
return 0;
}
----
Best regards,
Xiaoming Ni
next reply other threads:[~2024-12-24 1:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-24 1:20 Nixiaoming [this message]
2024-12-25 15:44 ` [RFC] RLIMIT_NOFILE: the maximum number of open files or the maximum fd index? Mateusz Guzik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4731d54723b841599882a24f7aa73aaa@huawei.com \
--to=nixiaoming@huawei.com \
--cc=arnd@arndb.de \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-arch@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=weiyongjun1@huawei.com \
--cc=young.liuyang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox