Skip to contentSkip to conversation

unshare03: using soft limit of NOFILE

PATCH In review lufei 4 participants 15 messages Mar 14, 2025 – Apr 16, 2025 1 message not in the archive

lore
Share & tools
lore

lufei

author
#0 lore raw

PATCH 1/1 unshare03: using soft limit of NOFILE

I think it's safer to set NOFILE increasing from soft limit than from hard limit.

Hard limit may lead to dup2 ENOMEM error which bring the result to TBROK on little memory machine. (e.g. 2GB memory in my situation, hard limit in /proc/sys/fs/nr_open come out to be 1073741816)

---
 testcases/kernel/syscalls/unshare/unshare03.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +6 -8
index 7c5e71c4e..bb568264c 100644
@@ -24,7 +24,7 @@
 
 static void run(void)
 {
-	int nr_open;
+	int rlim_max;
 	int nr_limit;
 	struct rlimit rlimit;
 	struct tst_clone_args args = {
@@ -32,14 +32,12 @@ static void run(void)
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	rlim_max = rlimit.rlim_max;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
+	nr_limit = rlim_max + NR_OPEN_LIMIT;
 	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
 
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
 	rlimit.rlim_cur = nr_limit;
 	rlimit.rlim_max = nr_limit;
 
@@ -47,10 +45,10 @@ static void run(void)
 	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
 		nr_limit);
 
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, rlim_max + NR_OPEN_DUP);
 
 	if (!SAFE_CLONE(&args)) {
-		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", rlim_max);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}
Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi lufei, Al,

@Al, you're the author of the original test unshare_test.c [1] in kselftest. This is a patch to LTP test unshare03.c, which is based on your test.

Quoted 6 lines

I think it's safer to set NOFILE increasing from soft limit than from hard limit.

Hard limit may lead to dup2 ENOMEM error which bring the result to TBROK on little memory machine. (e.g. 2GB memory in my situation, hard limit in /proc/sys/fs/nr_open come out to be 1073741816)

IMHO lowering number to ~ half (in my case) by using rlimit.rlim_max instead of /proc/sys/fs/nr_open should not affect the functionality of the test, right? Or am I missing something obvious?

@lufei I guess kselftest tools/testing/selftests/core/unshare_test.c would fail for you as well, right?

Kind regards,
Petr

[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=611fbeb44a777e5ab54ab3127ec85f72147911d8

Quoted 49 lines
---
 testcases/kernel/syscalls/unshare/unshare03.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +6 -8
index 7c5e71c4e..bb568264c 100644
@@ -24,7 +24,7 @@
 
 static void run(void)
 {
-	int nr_open;
+	int rlim_max;
 	int nr_limit;
 	struct rlimit rlimit;
 	struct tst_clone_args args = {
@@ -32,14 +32,12 @@ static void run(void)
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	rlim_max = rlimit.rlim_max;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
+	nr_limit = rlim_max + NR_OPEN_LIMIT;
 	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
 
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
 	rlimit.rlim_cur = nr_limit;
 	rlimit.rlim_max = nr_limit;
 
@@ -47,10 +45,10 @@ static void run(void)
 	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
 		nr_limit);
 
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, rlim_max + NR_OPEN_DUP);
 
 	if (!SAFE_CLONE(&args)) {
-		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", rlim_max);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp

A message that is not in the archive (sent off-list or not yet received): 0A99FFBB46DDB0B4+Z+YKSlAwn1vx3Dz4@rocky

#3 lore raw

Hi lufei, Al,

Quoted 8 lines

Hi Petr,

Yes, kselftest tools/testing/selftests/core/unshare_test.c failed as well, dup2 failed:

```
unshare_test.c:60:unshare_EMFILE:Expected dup2(2, nr_open + 64) (-1) >= 0 (0)
```

Thanks for info. Maybe also sending a patch to kselftest?

Kind regards,
Petr

PS: lufei, please keep Cc to keep other informed.

Quoted 77 lines

Thanks for reply.
Best regards.

Quoted 73 lines · Petr Vorel

Hi lufei, Al,

@Al, you're the author of the original test unshare_test.c [1] in kselftest. This is a patch to LTP test unshare03.c, which is based on your test.

Quoted 6 lines

I think it's safer to set NOFILE increasing from soft limit than from hard limit.

Hard limit may lead to dup2 ENOMEM error which bring the result to TBROK on little memory machine. (e.g. 2GB memory in my situation, hard limit in /proc/sys/fs/nr_open come out to be 1073741816)

IMHO lowering number to ~ half (in my case) by using rlimit.rlim_max instead of /proc/sys/fs/nr_open should not affect the functionality of the test, right? Or am I missing something obvious?

@lufei I guess kselftest tools/testing/selftests/core/unshare_test.c would fail for you as well, right?

Kind regards,
Petr

[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=611fbeb44a777e5ab54ab3127ec85f72147911d8

Quoted 49 lines
---
 testcases/kernel/syscalls/unshare/unshare03.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +6 -8
index 7c5e71c4e..bb568264c 100644
@@ -24,7 +24,7 @@
 
 static void run(void)
 {
-	int nr_open;
+	int rlim_max;
 	int nr_limit;
 	struct rlimit rlimit;
 	struct tst_clone_args args = {
@@ -32,14 +32,12 @@ static void run(void)
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	rlim_max = rlimit.rlim_max;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
+	nr_limit = rlim_max + NR_OPEN_LIMIT;
 	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
 
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
 	rlimit.rlim_cur = nr_limit;
 	rlimit.rlim_max = nr_limit;
 
@@ -47,10 +45,10 @@ static void run(void)
 	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
 		nr_limit);
 
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, rlim_max + NR_OPEN_DUP);
 
 	if (!SAFE_CLONE(&args)) {
-		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", rlim_max);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
In reply to lufei

Hi!

Quoted 46 lines

I think it's safer to set NOFILE increasing from soft limit than from hard limit.

Hard limit may lead to dup2 ENOMEM error which bring the result to TBROK on little memory machine. (e.g. 2GB memory in my situation, hard limit in /proc/sys/fs/nr_open come out to be 1073741816)

---
 testcases/kernel/syscalls/unshare/unshare03.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +4 -6
index 7c5e71c4e..bb568264c 100644
@@ -24,7 +24,7 @@
 
 static void run(void)
 {
-	int nr_open;
+	int rlim_max;
 	int nr_limit;
 	struct rlimit rlimit;
 	struct tst_clone_args args = {
@@ -32,14 +32,12 @@ static void run(void)
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	rlim_max = rlimit.rlim_max;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
+	nr_limit = rlim_max + NR_OPEN_LIMIT;
 	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
 
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
 	rlimit.rlim_cur = nr_limit;
 	rlimit.rlim_max = nr_limit;
 
@@ -47,10 +45,10 @@ static void run(void)
 	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
 		nr_limit);

testcases/kernel/syscalls/unshare/unshare03.c static void run(void) · line 54

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c +2 -2
 
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, rlim_max + NR_OPEN_DUP);
 
 	if (!SAFE_CLONE(&args)) {
-		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", rlim_max);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}

Why do we bother with reading the /rpoc/sys/fs/nr_open file? All that we need to to do is to dup() a file descriptor and tnen set the nr_open limit to fd - 2. And if we do so we can drop the rlimit that increases the limit so that it's greater than nr_open as well.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 51 lines

Hi!

Quoted 49 lines

I think it's safer to set NOFILE increasing from soft limit than from hard limit.

Hard limit may lead to dup2 ENOMEM error which bring the result to TBROK on little memory machine. (e.g. 2GB memory in my situation, hard limit in /proc/sys/fs/nr_open come out to be 1073741816)

---
 testcases/kernel/syscalls/unshare/unshare03.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +5 -7
index 7c5e71c4e..bb568264c 100644
@@ -24,7 +24,7 @@
 
 static void run(void)
 {
-	int nr_open;
+	int rlim_max;
 	int nr_limit;
 	struct rlimit rlimit;
 	struct tst_clone_args args = {
@@ -32,14 +32,12 @@ static void run(void)
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	rlim_max = rlimit.rlim_max;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
+	nr_limit = rlim_max + NR_OPEN_LIMIT;
 	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
 
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
 	rlimit.rlim_cur = nr_limit;
 	rlimit.rlim_max = nr_limit;
 
@@ -47,10 +45,10 @@ static void run(void)
 	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
 		nr_limit);
 
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, rlim_max + NR_OPEN_DUP);
Quoted 11 lines

testcases/kernel/syscalls/unshare/unshare03.c static void run(void) · line 54

Quoted 6 lines
testcases/kernel/syscalls/unshare/unshare03.c +1 -1
 	if (!SAFE_CLONE(&args)) {
-		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", rlim_max);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}

Why do we bother with reading the /rpoc/sys/fs/nr_open file? All that we need to to do is to dup() a file descriptor and tnen set the nr_open limit to fd - 2. And if we do so we can drop the rlimit that increases the limit so that it's greater than nr_open as well.

IMHO file descriptor will be 3, fd - 2 == 1. And trying to set 1 to /rpoc/sys/fs/nr_open leads to EINVAL. You probably mean something different.

The test is based on unshare_test.c [1] from Al Viro (611fbeb44a777 [2]). Both tests IMHO use nr_open + 1024 nr_open + 1024 and then call dup2() on nr_open + 64 to avoid later EINVAL when writing /rpoc/sys/fs/nr_open.

Kind regards,
Petr

[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/core/unshare_test.c
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=611fbeb44a777e5ab54ab3127ec85f72147911d8

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi!

Quoted 2 lines

IMHO file descriptor will be 3, fd - 2 == 1. And trying to set 1 to /rpoc/sys/fs/nr_open leads to EINVAL. You probably mean something different.

Ah, we cannot set nr_open to anything that is smaller than BITS_PER_LONG:

...
unsigned int sysctl_nr_open __read_mostly = 1024*1024; unsigned int sysctl_nr_open_min = BITS_PER_LONG; /* our min() is unusable in constant expressions ;-/ */ #define __const_min(x, y) ((x) < (y) ? (x) : (y)) unsigned int sysctl_nr_open_max =

        __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

...

Then we need a filedescriptor that is >= 64 and set the nr_open to 64.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#7 lore raw
In reply to Cyril Hrubis

Hi, Cyril

Thanks very much for the smart suggestion. I'll submit a v2 patch.

Quoted 67 lines · Cyril Hrubis

Hi!

Quoted 46 lines

I think it's safer to set NOFILE increasing from soft limit than from hard limit.

Hard limit may lead to dup2 ENOMEM error which bring the result to TBROK on little memory machine. (e.g. 2GB memory in my situation, hard limit in /proc/sys/fs/nr_open come out to be 1073741816)

---
 testcases/kernel/syscalls/unshare/unshare03.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +4 -6
index 7c5e71c4e..bb568264c 100644
@@ -24,7 +24,7 @@
 
 static void run(void)
 {
-	int nr_open;
+	int rlim_max;
 	int nr_limit;
 	struct rlimit rlimit;
 	struct tst_clone_args args = {
@@ -32,14 +32,12 @@ static void run(void)
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	rlim_max = rlimit.rlim_max;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
+	nr_limit = rlim_max + NR_OPEN_LIMIT;
 	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
 
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
 	rlimit.rlim_cur = nr_limit;
 	rlimit.rlim_max = nr_limit;
 
@@ -47,10 +45,10 @@ static void run(void)
 	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
 		nr_limit);

testcases/kernel/syscalls/unshare/unshare03.c static void run(void) · line 54

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c +2 -2
 
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, rlim_max + NR_OPEN_DUP);
 
 	if (!SAFE_CLONE(&args)) {
-		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", rlim_max);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}

Why do we bother with reading the /rpoc/sys/fs/nr_open file? All that we need to to do is to dup() a file descriptor and tnen set the nr_open limit to fd - 2. And if we do so we can drop the rlimit that increases the limit so that it's greater than nr_open as well.

Signature · 2 lines
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#8 lore raw

PATCH v2 1/1 unshare03: set nr_open with sizeof(long)*8

In reply to lufei

Set nr_open with sizeof(long)*8 to trigger EMFILE, instead of reading number of filedescriptors limit.

---
 testcases/kernel/syscalls/unshare/unshare03.c | 23 ++-----------------
 1 file changed, 2 insertions(+), 21 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +2 -21
index 7c5e71c4e..c3b98930d 100644
@@ -17,44 +17,25 @@
 #include "lapi/sched.h"
 
 #define FS_NR_OPEN "/proc/sys/fs/nr_open"
-#define NR_OPEN_LIMIT 1024
-#define NR_OPEN_DUP 64
 
 #ifdef HAVE_UNSHARE
 
 static void run(void)
 {
-	int nr_open;
-	int nr_limit;
-	struct rlimit rlimit;
 	struct tst_clone_args args = {
 		.flags = CLONE_FILES,
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	int nr_open = sizeof(long) * 8;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
-	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
-
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
-	rlimit.rlim_cur = nr_limit;
-	rlimit.rlim_max = nr_limit;
-
-	SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
-	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
-		nr_limit);
-
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, nr_open + 1);
 
 	if (!SAFE_CLONE(&args)) {
 		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}
-
 }
 
 static void setup(void)
Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by Jan Stancek on v2 1/1
#9 lore raw

Re: [LTP] [PATCH v2] unshare03: set nr_open with sizeof(long)*8

Quoted 3 lines · lufei

Set nr_open with sizeof(long)*8 to trigger EMFILE, instead of reading number of filedescriptors limit.

An explanation for this number would be nice.

Quoted 2 lines

This fixes test for me on 6.15.0-0.rc1

Quoted 62 lines
---
 testcases/kernel/syscalls/unshare/unshare03.c | 23 ++-----------------
 1 file changed, 2 insertions(+), 21 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +2 -22
index 7c5e71c4e..c3b98930d 100644
@@ -17,44 +17,25 @@
 #include "lapi/sched.h"
 
 #define FS_NR_OPEN "/proc/sys/fs/nr_open"
-#define NR_OPEN_LIMIT 1024
-#define NR_OPEN_DUP 64
 
 #ifdef HAVE_UNSHARE
 
 static void run(void)
 {
-       int nr_open;
-       int nr_limit;
-       struct rlimit rlimit;
        struct tst_clone_args args = {
                .flags = CLONE_FILES,
                .exit_signal = SIGCHLD,
        };
 
-       SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-       tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+       int nr_open = sizeof(long) * 8;
 
-       nr_limit = nr_open + NR_OPEN_LIMIT;
-       SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
-
-       SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
-       rlimit.rlim_cur = nr_limit;
-       rlimit.rlim_max = nr_limit;
-
-       SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
-       tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
-               nr_limit);
-
-       SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+       SAFE_DUP2(2, nr_open + 1);
 
        if (!SAFE_CLONE(&args)) {
                SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
                TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
                exit(0);
        }
-
 }
 
 static void setup(void)
--

2.39.3

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] [PATCH v2] unshare03: set nr_open with sizeof(long)*8

In reply to lufei
Quoted 3 lines · lufei

Set nr_open with sizeof(long)*8 to trigger EMFILE, instead of reading number of filedescriptors limit.

Any new changes in Linux that have made the previous way not work now?

Quoted 67 lines
---
 testcases/kernel/syscalls/unshare/unshare03.c | 23 ++-----------------
 1 file changed, 2 insertions(+), 21 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +2 -21
index 7c5e71c4e..c3b98930d 100644
@@ -17,44 +17,25 @@
 #include "lapi/sched.h"
 
 #define FS_NR_OPEN "/proc/sys/fs/nr_open"
-#define NR_OPEN_LIMIT 1024
-#define NR_OPEN_DUP 64
 
 #ifdef HAVE_UNSHARE
 
 static void run(void)
 {
-       int nr_open;
-       int nr_limit;
-       struct rlimit rlimit;
        struct tst_clone_args args = {
                .flags = CLONE_FILES,
                .exit_signal = SIGCHLD,
        };
 
-       SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-       tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+       int nr_open = sizeof(long) * 8;
 
-       nr_limit = nr_open + NR_OPEN_LIMIT;
-       SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
-
-       SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
-       rlimit.rlim_cur = nr_limit;
-       rlimit.rlim_max = nr_limit;
-
-       SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
-       tst_res(TDEBUG, "Set new maximum number of file descriptors to :
 %d",
-               nr_limit);
-
-       SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+       SAFE_DUP2(2, nr_open + 1);
 
        if (!SAFE_CLONE(&args)) {
                SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
                TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
                exit(0);
        }
-
 }
 

static void setup(void)
--
2.39.3

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Li Wang
#11 lore raw

Re: [LTP] [PATCH v2] unshare03: set nr_open with sizeof(long)*8

Quoted 10 lines · Li Wang
Quoted 3 lines · lufei

Set nr_open with sizeof(long)*8 to trigger EMFILE, instead of reading number of filedescriptors limit.

Any new changes in Linux that have made the previous way not work now?

Ah, I see. As you pointed out in v1, that hard limit may lead to dup2 ENOMEM error which brings the result to TBROK ona small RAM system.

So, I agree Jan, we'd better add more description to the patch.

Quoted 76 lines
Quoted 62 lines
---
 testcases/kernel/syscalls/unshare/unshare03.c | 23 ++-----------------
 1 file changed, 2 insertions(+), 21 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +2 -21
index 7c5e71c4e..c3b98930d 100644
@@ -17,44 +17,25 @@
 #include "lapi/sched.h"
 
 #define FS_NR_OPEN "/proc/sys/fs/nr_open"
-#define NR_OPEN_LIMIT 1024
-#define NR_OPEN_DUP 64
 
 #ifdef HAVE_UNSHARE
 
 static void run(void)
 {
-       int nr_open;
-       int nr_limit;
-       struct rlimit rlimit;
        struct tst_clone_args args = {
                .flags = CLONE_FILES,
                .exit_signal = SIGCHLD,
        };
 
-       SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-       tst_res(TDEBUG, "Maximum number of file descriptors: %d",
 nr_open);
+       int nr_open = sizeof(long) * 8;
 
-       nr_limit = nr_open + NR_OPEN_LIMIT;
-       SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
-
-       SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
-       rlimit.rlim_cur = nr_limit;
-       rlimit.rlim_max = nr_limit;
-
-       SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
-       tst_res(TDEBUG, "Set new maximum number of file descriptors to :
 %d",
-               nr_limit);
-
-       SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+       SAFE_DUP2(2, nr_open + 1);
 
        if (!SAFE_CLONE(&args)) {
                SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
                TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
                exit(0);
        }
-
 }
Quoted 9 lines

static void setup(void)
--
2.39.3

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Regards,
Li Wang

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#12 lore raw

Re: [LTP] [PATCH v2] unshare03: set nr_open with sizeof(long)*8

Hi, Li, Jan.

Sorry for so few details about the patch.

The origin code is using nr_open +1024 as fd number limit and then set fd to nr_open + 64, then set nr_open to origin nr_open in child process to make unshare(CLONE_FILES) fails with EMFILE. I tested this on my vm (1cpu, 2GB mem), met dup2 fails with ENOMEM, this make the test case BROKEN. I was try to fix this.

In patch v2, I was try to use rlimit.rlim_max instead of using nr_open(read from /proc/sys/fs/nr_open), this works well. But Cyril gives a better approch, so, I submit patch v3.

Quoted from Cyril's comment:

Quoted 8 lines

Ah, we cannot set nr_open to anything that is smaller than BITS_PER_LONG:
...
unsigned int sysctl_nr_open __read_mostly = 1024*1024; unsigned int sysctl_nr_open_min = BITS_PER_LONG;
/* our min() is unusable in constant expressions ;-/ */ #define __const_min(x, y) ((x) < (y) ? (x) : (y))
unsigned int sysctl_nr_open_max =

Quoted 4 lines
       __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

...

Then we need a filedescriptor that is >= 64 and set the nr_open to 64.

I'm using sizeof(long)*8 is because I was thinking only set filediscriptor >= BITS_PER_LONG and then set nr_open = BITS_PER_LONG could make EMFILE happen. Less than BITS_PER_LONG would trigger other error than EMFILE.

Am I misunderstood Cyril?

Thanks for reply.

Quoted 103 lines · Li Wang
Quoted 10 lines · Li Wang
Quoted 3 lines · lufei

Set nr_open with sizeof(long)*8 to trigger EMFILE, instead of reading number of filedescriptors limit.

Any new changes in Linux that have made the previous way not work now?

Ah, I see. As you pointed out in v1, that hard limit may lead to dup2 ENOMEM error which brings the result to TBROK ona small RAM system.

So, I agree Jan, we'd better add more description to the patch.

Quoted 76 lines
Quoted 62 lines
---
 testcases/kernel/syscalls/unshare/unshare03.c | 23 ++-----------------
 1 file changed, 2 insertions(+), 21 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +2 -21
index 7c5e71c4e..c3b98930d 100644
@@ -17,44 +17,25 @@
 #include "lapi/sched.h"
 
 #define FS_NR_OPEN "/proc/sys/fs/nr_open"
-#define NR_OPEN_LIMIT 1024
-#define NR_OPEN_DUP 64
 
 #ifdef HAVE_UNSHARE
 
 static void run(void)
 {
-       int nr_open;
-       int nr_limit;
-       struct rlimit rlimit;
        struct tst_clone_args args = {
                .flags = CLONE_FILES,
                .exit_signal = SIGCHLD,
        };
 
-       SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-       tst_res(TDEBUG, "Maximum number of file descriptors: %d",
 nr_open);
+       int nr_open = sizeof(long) * 8;
 
-       nr_limit = nr_open + NR_OPEN_LIMIT;
-       SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
-
-       SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
-       rlimit.rlim_cur = nr_limit;
-       rlimit.rlim_max = nr_limit;
-
-       SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
-       tst_res(TDEBUG, "Set new maximum number of file descriptors to :
 %d",
-               nr_limit);
-
-       SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+       SAFE_DUP2(2, nr_open + 1);
 
        if (!SAFE_CLONE(&args)) {
                SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
                TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
                exit(0);
        }
-
 }
Quoted 9 lines

static void setup(void)
--
2.39.3

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Regards,
Li Wang

Signature · 2 lines
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

Re: [LTP] [PATCH v2] unshare03: set nr_open with sizeof(long)*8

Hi!

Quoted 8 lines
Quoted 7 lines

unsigned int sysctl_nr_open __read_mostly = 1024*1024; unsigned int sysctl_nr_open_min = BITS_PER_LONG; /* our min() is unusable in constant expressions ;-/ */ #define __const_min(x, y) ((x) < (y) ? (x) : (y)) unsigned int sysctl_nr_open_max =

       __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

...

Quoted 8 lines
Quoted 1 line

Then we need a filedescriptor that is >= 64 and set the nr_open to 64.

I'm using sizeof(long)*8 is because I was thinking only set filediscriptor >= BITS_PER_LONG and then set nr_open = BITS_PER_LONG could make EMFILE happen. Less than BITS_PER_LONG would trigger other error than EMFILE.

Am I misunderstood Cyril?

The actual patch is fine. What they are asking for is a better description. The patch description should tell _why_ the change is needed.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#14 lore raw

PATCH v3 1/1 unshare03: set nr_open with sizeof(long)*8

In reply to lufei

dup2(2, nr_open+64) may cause ENOMEM error, which makes the testcase BROKEN. Change to dup2(2, sizeof(long)*8 +1) and set nr_open to sizeof(long)*8

---
 testcases/kernel/syscalls/unshare/unshare03.c | 23 ++-----------------
 1 file changed, 2 insertions(+), 21 deletions(-)
testcases/kernel/syscalls/unshare/unshare03.c +2 -21
index 7c5e71c4e..c3b98930d 100644
@@ -17,44 +17,25 @@
 #include "lapi/sched.h"
 
 #define FS_NR_OPEN "/proc/sys/fs/nr_open"
-#define NR_OPEN_LIMIT 1024
-#define NR_OPEN_DUP 64
 
 #ifdef HAVE_UNSHARE
 
 static void run(void)
 {
-	int nr_open;
-	int nr_limit;
-	struct rlimit rlimit;
 	struct tst_clone_args args = {
 		.flags = CLONE_FILES,
 		.exit_signal = SIGCHLD,
 	};
 
-	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
-	tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
+	int nr_open = sizeof(long) * 8;
 
-	nr_limit = nr_open + NR_OPEN_LIMIT;
-	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
-
-	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
-
-	rlimit.rlim_cur = nr_limit;
-	rlimit.rlim_max = nr_limit;
-
-	SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
-	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
-		nr_limit);
-
-	SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
+	SAFE_DUP2(2, nr_open + 1);
 
 	if (!SAFE_CLONE(&args)) {
 		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
 		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
 		exit(0);
 	}
-
 }
 
 static void setup(void)
Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp