Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH v2] unshare_test: set nr_open using soft limit
@ 2025-04-01  1:55 lufei
  2025-04-08 23:42 ` Shuah Khan
  2025-04-14  2:17 ` [PATCH v3] selftests:core: unshare_test: using bits_per_long instead of nr_open+64 lufei
  0 siblings, 2 replies; 6+ messages in thread
From: lufei @ 2025-04-01  1:55 UTC (permalink / raw)
  To: linux-kselftest; +Cc: pvorel, viro, lufei

Set maximum file descriptor number limit by rlimit.rlim_max than
nr_open(hard limit). Hard limit may cause dup2 fail.

Signed-off-by: lufei <lufei@uniontech.com>
---
 tools/testing/selftests/core/unshare_test.c | 24 ++++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/core/unshare_test.c b/tools/testing/selftests/core/unshare_test.c
index 7fec9dfb1b0e..66b651168887 100644
--- a/tools/testing/selftests/core/unshare_test.c
+++ b/tools/testing/selftests/core/unshare_test.c
@@ -30,6 +30,7 @@ TEST(unshare_EMFILE)
 	static char buf[512], buf2[512];
 	struct rlimit rlimit;
 	int nr_open;
+	int rlimit_max;
 
 	fd = open("/proc/sys/fs/nr_open", O_RDWR);
 	ASSERT_GE(fd, 0);
@@ -42,22 +43,24 @@ TEST(unshare_EMFILE)
 
 	ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
 
-	/* bump fs.nr_open */
-	n2 = sprintf(buf2, "%d\n", nr_open + 1024);
+	rlimit_max = rlimit.rlim_max;
+
+	/* bump rlimit.rlim_max */
+	n2 = sprintf(buf2, "%d\n", rlimit_max + 1024);
 	lseek(fd, 0, SEEK_SET);
 	write(fd, buf2, n2);
 
 	/* bump ulimit -n */
-	rlimit.rlim_cur = nr_open + 1024;
-	rlimit.rlim_max = nr_open + 1024;
+	rlimit.rlim_cur = rlimit_max + 1024;
+	rlimit.rlim_max = rlimit_max + 1024;
 	EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
 		lseek(fd, 0, SEEK_SET);
 		write(fd, buf, n);
 		exit(EXIT_FAILURE);
 	}
 
-	/* get a descriptor past the old fs.nr_open */
-	EXPECT_GE(dup2(2, nr_open + 64), 0) {
+	/* get a descriptor past the old rlimit.rlim_max */
+	EXPECT_GE(dup2(2, rlimit_max + 64), 0) {
 		lseek(fd, 0, SEEK_SET);
 		write(fd, buf, n);
 		exit(EXIT_FAILURE);
@@ -74,15 +77,20 @@ TEST(unshare_EMFILE)
 	if (pid == 0) {
 		int err;
 
-		/* restore fs.nr_open */
+		n2 = sprintf(buf2, "%d\n", rlimit_max);
 		lseek(fd, 0, SEEK_SET);
-		write(fd, buf, n);
+		write(fd, buf2, n2);
+
 		/* ... and now unshare(CLONE_FILES) must fail with EMFILE */
 		err = unshare(CLONE_FILES);
 		EXPECT_EQ(err, -1)
 			exit(EXIT_FAILURE);
 		EXPECT_EQ(errno, EMFILE)
 			exit(EXIT_FAILURE);
+
+		/* restore fs.nr_open */
+		lseek(fd, 0, SEEK_SET);
+		write(fd, buf, n);
 		exit(EXIT_SUCCESS);
 	}
 
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] unshare_test: set nr_open using soft limit
  2025-04-01  1:55 [PATCH v2] unshare_test: set nr_open using soft limit lufei
@ 2025-04-08 23:42 ` Shuah Khan
  2025-04-09  5:56   ` Petr Vorel
  2025-04-14  2:17 ` [PATCH v3] selftests:core: unshare_test: using bits_per_long instead of nr_open+64 lufei
  1 sibling, 1 reply; 6+ messages in thread
From: Shuah Khan @ 2025-04-08 23:42 UTC (permalink / raw)
  To: lufei, pvorel, viro; +Cc: Shuah Khan, linux-kselftest

On 3/31/25 19:55, lufei wrote:
> Set maximum file descriptor number limit by rlimit.rlim_max than
> nr_open(hard limit). Hard limit may cause dup2 fail.
> 
> Signed-off-by: lufei <lufei@uniontech.com>

Petr, Al,

Okay to take this patch?

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] unshare_test: set nr_open using soft limit
  2025-04-08 23:42 ` Shuah Khan
@ 2025-04-09  5:56   ` Petr Vorel
  2025-04-09 21:30     ` Shuah Khan
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Vorel @ 2025-04-09  5:56 UTC (permalink / raw)
  To: Shuah Khan; +Cc: lufei, viro, linux-kselftest

> On 3/31/25 19:55, lufei wrote:
> > Set maximum file descriptor number limit by rlimit.rlim_max than
> > nr_open(hard limit). Hard limit may cause dup2 fail.

> > Signed-off-by: lufei <lufei@uniontech.com>

> Petr, Al,

> Okay to take this patch?

LGTM, hopefully I haven't overlook anything.

Reviewed-by: Petr Vorel <pvorel@suse.cz>

FYI Cyril Hrubis suggested a different approach (for LTP tests which is based on
this Al's test):

https://lore.kernel.org/ltp/Z-u7yYJpTBG8Hi6A@yuki.lan/
https://lore.kernel.org/ltp/Z-vwYZxLms8juTjX@yuki.lan/

	filedescriptor that is >= 64 and set the nr_open to 64.

That would help to avoid using /proc/sys/fs/nr_open.

Kind regards,
Petr

> thanks,
> -- Shuah

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] unshare_test: set nr_open using soft limit
  2025-04-09  5:56   ` Petr Vorel
@ 2025-04-09 21:30     ` Shuah Khan
  2025-04-10  2:05       ` Lu Fei
  0 siblings, 1 reply; 6+ messages in thread
From: Shuah Khan @ 2025-04-09 21:30 UTC (permalink / raw)
  To: Petr Vorel, lufei; +Cc: viro, linux-kselftest, Shuah Khan

On 4/8/25 23:56, Petr Vorel wrote:
>> On 3/31/25 19:55, lufei wrote:
>>> Set maximum file descriptor number limit by rlimit.rlim_max than
>>> nr_open(hard limit). Hard limit may cause dup2 fail.
> 
>>> Signed-off-by: lufei <lufei@uniontech.com>
> 
>> Petr, Al,
> 
>> Okay to take this patch?
> 
> LGTM, hopefully I haven't overlook anything.
> 
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
> 
> FYI Cyril Hrubis suggested a different approach (for LTP tests which is based on
> this Al's test):
> 
> https://lore.kernel.org/ltp/Z-u7yYJpTBG8Hi6A@yuki.lan/
> https://lore.kernel.org/ltp/Z-vwYZxLms8juTjX@yuki.lan/
> 
> 	filedescriptor that is >= 64 and set the nr_open to 64.
> 
> That would help to avoid using /proc/sys/fs/nr_open.
> 

Thank you for this suggestion.

lufei, Can you look into this as a solution? Also the short log
should include the subsystem: e.:

selftests:core: unshare_test: -----

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] unshare_test: set nr_open using soft limit
  2025-04-09 21:30     ` Shuah Khan
@ 2025-04-10  2:05       ` Lu Fei
  0 siblings, 0 replies; 6+ messages in thread
From: Lu Fei @ 2025-04-10  2:05 UTC (permalink / raw)
  To: Shuah Khan, Petr Vorel; +Cc: viro, linux-kselftest

On Wed, Apr 09, 2025 at 03:30:32PM -0600, Shuah Khan wrote:
> On 4/8/25 23:56, Petr Vorel wrote:
> > > On 3/31/25 19:55, lufei wrote:
> > > > Set maximum file descriptor number limit by rlimit.rlim_max than
> > > > nr_open(hard limit). Hard limit may cause dup2 fail.
> > 
> > > > Signed-off-by: lufei <lufei@uniontech.com>
> > 
> > > Petr, Al,
> > 
> > > Okay to take this patch?
> > 
> > LGTM, hopefully I haven't overlook anything.
> > 
> > Reviewed-by: Petr Vorel <pvorel@suse.cz>
> > 
> > FYI Cyril Hrubis suggested a different approach (for LTP tests which is based on
> > this Al's test):
> > 
> > https://lore.kernel.org/ltp/Z-u7yYJpTBG8Hi6A@yuki.lan/
> > https://lore.kernel.org/ltp/Z-vwYZxLms8juTjX@yuki.lan/
> > 
> > 	filedescriptor that is >= 64 and set the nr_open to 64.
> > 
> > That would help to avoid using /proc/sys/fs/nr_open.
> > 
> 
> Thank you for this suggestion.
> 
> lufei, Can you look into this as a solution? Also the short log
> should include the subsystem: e.:
> 
> selftests:core: unshare_test: -----
> 
> thanks,
> -- Shuah
> 

Hi Shuah,

Thanks very much for reply.

I'll submit a v3 patch with dup a filedescriptor >= sizeof(long)*8 and
set nr_open to sizeof(long)*8 to trigger EMFILE.

Hi Petr, Shuah, I don't have a i686 environment, only on x86_64 has been
tested. Is this all right?

I'll re-write short log as asked in v3 patch.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3] selftests:core: unshare_test: using bits_per_long instead of nr_open+64.
  2025-04-01  1:55 [PATCH v2] unshare_test: set nr_open using soft limit lufei
  2025-04-08 23:42 ` Shuah Khan
@ 2025-04-14  2:17 ` lufei
  1 sibling, 0 replies; 6+ messages in thread
From: lufei @ 2025-04-14  2:17 UTC (permalink / raw)
  To: linux-kselftest; +Cc: skhan, pvorel, viro, lufei

dup2(2, nr_open+64) may cause ENOMEM error, change to dup2(2,
bits_per_long +1) and set nr_open to bits_per_long to avoid ENOMEM.

Signed-off-by: lufei <lufei@uniontech.com>
---
 tools/testing/selftests/core/unshare_test.c | 32 +++++++--------------
 1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/tools/testing/selftests/core/unshare_test.c b/tools/testing/selftests/core/unshare_test.c
index 7fec9dfb1b0e..ea4caa7ec28d 100644
--- a/tools/testing/selftests/core/unshare_test.c
+++ b/tools/testing/selftests/core/unshare_test.c
@@ -28,8 +28,8 @@ TEST(unshare_EMFILE)
 	int fd;
 	ssize_t n, n2;
 	static char buf[512], buf2[512];
-	struct rlimit rlimit;
 	int nr_open;
+	int bits_per_long = sizeof(long) * 8;
 
 	fd = open("/proc/sys/fs/nr_open", O_RDWR);
 	ASSERT_GE(fd, 0);
@@ -40,24 +40,8 @@ TEST(unshare_EMFILE)
 
 	ASSERT_EQ(sscanf(buf, "%d", &nr_open), 1);
 
-	ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
-
-	/* bump fs.nr_open */
-	n2 = sprintf(buf2, "%d\n", nr_open + 1024);
-	lseek(fd, 0, SEEK_SET);
-	write(fd, buf2, n2);
-
-	/* bump ulimit -n */
-	rlimit.rlim_cur = nr_open + 1024;
-	rlimit.rlim_max = nr_open + 1024;
-	EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
-		lseek(fd, 0, SEEK_SET);
-		write(fd, buf, n);
-		exit(EXIT_FAILURE);
-	}
-
-	/* get a descriptor past the old fs.nr_open */
-	EXPECT_GE(dup2(2, nr_open + 64), 0) {
+	/* get a descriptor >= bits_per_long */
+	EXPECT_GE(dup2(2, bits_per_long+1), 0) {
 		lseek(fd, 0, SEEK_SET);
 		write(fd, buf, n);
 		exit(EXIT_FAILURE);
@@ -74,15 +58,21 @@ TEST(unshare_EMFILE)
 	if (pid == 0) {
 		int err;
 
-		/* restore fs.nr_open */
+		/* set nr_open == bits_per_long */
+		n2 = sprintf(buf2, "%d\n", bits_per_long);
 		lseek(fd, 0, SEEK_SET);
-		write(fd, buf, n);
+		write(fd, buf2, n2);
+
 		/* ... and now unshare(CLONE_FILES) must fail with EMFILE */
 		err = unshare(CLONE_FILES);
 		EXPECT_EQ(err, -1)
 			exit(EXIT_FAILURE);
 		EXPECT_EQ(errno, EMFILE)
 			exit(EXIT_FAILURE);
+
+		/* restore fs.nr_open */
+		lseek(fd, 0, SEEK_SET);
+		write(fd, buf, n);
 		exit(EXIT_SUCCESS);
 	}
 
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-04-14  2:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01  1:55 [PATCH v2] unshare_test: set nr_open using soft limit lufei
2025-04-08 23:42 ` Shuah Khan
2025-04-09  5:56   ` Petr Vorel
2025-04-09 21:30     ` Shuah Khan
2025-04-10  2:05       ` Lu Fei
2025-04-14  2:17 ` [PATCH v3] selftests:core: unshare_test: using bits_per_long instead of nr_open+64 lufei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox