Skip to contentSkip to conversation

syscalls/setrlimit03.c: read /proc/sys/fs/nr_open for RLIMIT_NOFILE limit

PATCH New Tommi Rantala 2 participants 3 messages Jan 30, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/setrlimit03.c: read /proc/sys/fs/nr_open for RLIMIT_NOFILE limit

From: Tommi Rantala <[email protected]>

Since kernel v2.6.25 RLIMIT_NOFILE limit is no longer hardcoded to NR_OPEN, but can be set via /proc/sys/fs/nr_open, see kernel commit 9cfe015aa424b3c003baba3841a60dd9b5ad319b ("get rid of NR_OPEN and introduce a sysctl_nr_open").

nr_open default value is 1024*1024, so setrlimit03 has been passing fine on new kernels, only "unexpectedly succeeding" if nr_open is set to some larger value.

---
 testcases/kernel/syscalls/setrlimit/setrlimit03.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/setrlimit/setrlimit03.c +12 -2
index 29b52aa7f..643432bb4 100644
@@ -36,6 +36,7 @@
 #endif
 
 static struct rlimit rlim1, rlim2;
+static unsigned int nr_open;
 
 static struct tcase {
 	struct rlimit *rlimt;
@@ -51,7 +52,10 @@ static void verify_setrlimit(unsigned int n)
 
 	TEST(setrlimit(RLIMIT_NOFILE, tc->rlimt));
 	if (TST_RET != -1) {
-		tst_res(TFAIL, "call succeeded unexpectedly");
+		tst_res(TFAIL, "call succeeded unexpectedly "
+			"(nr_open=%u rlim_cur=%lu rlim_max=%lu)", nr_open,
+			(unsigned long)(tc->rlimt->rlim_cur),
+			(unsigned long)(tc->rlimt->rlim_max));
 		return;
 	}
 
@@ -65,10 +69,16 @@ static void verify_setrlimit(unsigned int n)
 
 static void setup(void)
 {
+	if (tst_kvercmp(2, 6, 25) < 0) {
+		nr_open = NR_OPEN;
+	} else {
+		SAFE_FILE_SCANF("/proc/sys/fs/nr_open", "%u", &nr_open);
+	}
+
 	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim1);
 	rlim2.rlim_max = rlim1.rlim_cur;
 	rlim2.rlim_cur = rlim1.rlim_max + 1;
-	rlim1.rlim_max = NR_OPEN + 1;
+	rlim1.rlim_max = nr_open + 1;
 }
 
 static struct tst_test test = {
Signature · 1 line
2.19.2
#1 lore raw

PATCH 1/1 syscalls/setrlimit03.c: read /proc/sys/fs/nr_open for RLIMIT_NOFILE limit

Hi!

Quoted 10 lines
---
 testcases/kernel/syscalls/setrlimit/setrlimit03.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/setrlimit/setrlimit03.c +0 -0
index 29b52aa7f..643432bb4 100644

testcases/kernel/syscalls/setrlimit/setrlimit03.c line 39

Quoted 4 lines
testcases/kernel/syscalls/setrlimit/setrlimit03.c +1 -0
@@ -36,6 +36,7 @@
 #endif
 
 static struct rlimit rlim1, rlim2;
+static unsigned int nr_open;

I would have initialized the variable to NR_OPEN here.

Quoted 15 lines
testcases/kernel/syscalls/setrlimit/setrlimit03.c +4 -1
 static struct tcase {
 	struct rlimit *rlimt;
@@ -51,7 +52,10 @@ static void verify_setrlimit(unsigned int n)
 
 	TEST(setrlimit(RLIMIT_NOFILE, tc->rlimt));
 	if (TST_RET != -1) {
-		tst_res(TFAIL, "call succeeded unexpectedly");
+		tst_res(TFAIL, "call succeeded unexpectedly "
+			"(nr_open=%u rlim_cur=%lu rlim_max=%lu)", nr_open,
+			(unsigned long)(tc->rlimt->rlim_cur),
+			(unsigned long)(tc->rlimt->rlim_max));
 		return;
 	}
 

testcases/kernel/syscalls/setrlimit/setrlimit03.c static void verify_setrlimit(unsigned int n) · line 76

Quoted 8 lines
testcases/kernel/syscalls/setrlimit/setrlimit03.c +5 -0
@@ -65,10 +69,16 @@ static void verify_setrlimit(unsigned int n)
 
 static void setup(void)
 {
+	if (tst_kvercmp(2, 6, 25) < 0) {
+		nr_open = NR_OPEN;
+	} else {
+		SAFE_FILE_SCANF("/proc/sys/fs/nr_open", "%u", &nr_open);
+	}

Can we do here without the explicit kernel version check?

What about:

#define NR_OPEN_PATH "/proc/sys/fs/nr_open"

...

	if (acess(NR_OPEN_PATH, F_OK))
		SAFE_FILE_SCANF(NR_OPEN_APTH, "%u", &nr_open);

Otherwise it looks good.

Signature · 2 lines
Cyril Hrubis
[email protected]
#2 lore raw

PATCH 1/1 syscalls/setrlimit03.c: read /proc/sys/fs/nr_open for RLIMIT_NOFILE limit

ke 30. tammik. 2019 klo 18.15 Cyril Hrubis ([email protected]) kirjoitti:

Quoted 10 lines

testcases/kernel/syscalls/setrlimit/setrlimit03.c static void verify_setrlimit(unsigned int n) · line 76

Quoted 7 lines

static void setup(void)
{

testcases/kernel/syscalls/setrlimit/setrlimit03.c +5 -0
+     if (tst_kvercmp(2, 6, 25) < 0) {
+             nr_open = NR_OPEN;
+     } else {
+             SAFE_FILE_SCANF("/proc/sys/fs/nr_open", "%u", &nr_open);
+     }

Can we do here without the explicit kernel version check?

Quoted 8 lines

What about:

#define NR_OPEN_PATH "/proc/sys/fs/nr_open"

...

        if (acess(NR_OPEN_PATH, F_OK))
                SAFE_FILE_SCANF(NR_OPEN_APTH, "%u", &nr_open);

Alright, that should work too. I'll send patch v2 tomorrow.

-Tommi