Skip to contentSkip to conversation

unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd()

PATCH Superseded Wei Gao via ltp 1 participant 13 messages Dec 19, 2024 – Mar 6, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd()

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare03.c | 91 +++++++++++++++++++
 3 files changed, 93 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c
runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
 unshare01 unshare01
 unshare02 unshare02
+unshare03 unshare03
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
 /unshare01
 /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +91 -0

91 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Hi!

Quoted 1 line · Wei Gao via ltp

Missing an explanation of the commit.

Quoted 33 lines
---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare03.c | 91 +++++++++++++++++++
  3 files changed, 93 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c

runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
  unshare01 unshare01
  unshare02 unshare02
+unshare03 unshare03
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
  /unshare01
  /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +0 -0
new file mode 100644
index 000000000..0ff40b242

testcases/kernel/syscalls/unshare/unshare03.c line 8

Quoted 8 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +8 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Al Viro<[email protected]>
+ * Copyright (C) 2024 Wei Gao<[email protected]>
+ */
+
+/*\
+ * [Description]

No needed anymore.

testcases/kernel/syscalls/unshare/unshare03.c line 11

Quoted 3 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +3 -0
+ *
+ * Test case is adapted from the kernel self test unshare_test.c.
+ * Test coverage for dup_fd() failure handling in unshare_fd()

The test is verifying whether unshare() raises EMFILE error when we attempt to release the file descriptor table shared with the parent process, after opening a new file descriptor in the parent and modifying the maximum number of file descriptors in the child.

testcases/kernel/syscalls/unshare/unshare03.c line 23

Quoted 12 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +12 -0
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/syscall.h>
+#include <sched.h>
+#include <limits.h>
+#include <unistd.h>

All these imports are not needed.

Quoted 14 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +14 -0
+
+#include "tst_test.h"
+#include "config.h"
+#include "lapi/sched.h"
+
+#define FS_NR_OPEN "/proc/sys/fs/nr_open"
+
+#ifdef HAVE_UNSHARE
+
+static void run(void)
+{
+	int nr_open;
+	struct rlimit rlimit;
+	pid_t pid;

testcases/kernel/syscalls/unshare/unshare03.c line 47

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+	struct clone_args args = {
+		.flags = CLONE_FILES,
+		.exit_signal = SIGCHLD,
+	};
+
+	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
+
+	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024);
+
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);

I don't get the point of this call, even in the kselftests. The limits are overridden in the next 2 lines..

testcases/kernel/syscalls/unshare/unshare03.c line 54

Quoted 7 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +7 -0
+
+	rlimit.rlim_cur = nr_open + 1024;
+	rlimit.rlim_max = nr_open + 1024;
+
+	SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
+
+	SAFE_DUP2(2, nr_open + 64);

We have the control over file descriptors in this process, so I don't see why adding such a big number of file descriptors, but I guess nothing happens if we are adding high numbers. Maybe rlimit.rlim_cur/rlim_max + 16 would be enough. And then here we can use nr_open + 8 ...

testcases/kernel/syscalls/unshare/unshare03.c line 58

Quoted 4 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +4 -0
+
+	pid = clone3(&args, sizeof(args));
+
+	if (pid < 0) {

We can use SAFE_CLONE() here

testcases/kernel/syscalls/unshare/unshare03.c line 69

Quoted 11 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +11 -0
+		tst_res(TFAIL | TTERRNO, "clone3() failed");
+		return;
+	}
+
+	if (!pid) {
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, NULL, 0);

No need for this, tst_reap_children() is running automatically at the end of run().

testcases/kernel/syscalls/unshare/unshare03.c line 79

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+}
+
+static void setup(void)
+{
+	clone3_supported_by_kernel();
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_tmpdir = 1,

Temporary folder is not used.

testcases/kernel/syscalls/unshare/unshare03.c line 91

Quoted 13 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +12 -0
+	.needs_root = 1,
+	.test_all = run,
+	.setup = setup,
+	.save_restore = (const struct tst_path_val[]) {
+		{FS_NR_OPEN, NULL, TST_SR_TCONF},
+		{}
+	},
+};
+
+#else
+TST_TEST_TCONF("unshare is undefined.");
+#endif

Kind regards,
Andrea Cervesato

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

PATCH v2 1/1 unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd()

In reply to Wei Gao via ltp

This is new test case adapted from the kernel self test unshare_test.c. It verifies that the kernel correctly handles the EMFILE error condition during file descriptor table unsharing, specifically when the parent process modifies the file descriptor limits and the child process attempts to unshare(CLONE_FILES).

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare03.c | 75 +++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c
runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
 unshare01 unshare01
 unshare02 unshare02
+unshare03 unshare03
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
 /unshare01
 /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +75 -0

75 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Hi!

Quoted 5 lines · Wei Gao via ltp

This is new test case adapted from the kernel self test unshare_test.c. It verifies that the kernel correctly handles the EMFILE error condition during file descriptor table unsharing, specifically when the parent process modifies the file descriptor limits and the child process attempts to unshare(CLONE_FILES).

Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES).

Quoted 35 lines
---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare03.c | 75 +++++++++++++++++++
  3 files changed, 77 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c

runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
  unshare01 unshare01
  unshare02 unshare02
+unshare03 unshare03
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
  /unshare01
  /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +1 -0
new file mode 100644
index 000000000..c8baecc10
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later

testcases/kernel/syscalls/unshare/unshare03.c line 11

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+/*
+ * Copyright (c) 2024 Al Viro <[email protected]>
+ * Copyright (C) 2024 Wei Gao <[email protected]>
+ */
+
+/*\
+ * The test is verifying whether unshare() raises EMFILE error when we
+ * attempt to release the file descriptor table shared with the parent
+ * process, after opening a new file descriptor in the parent and modifying
+ * the maximum number of file descriptors in the child.

Probably we can use the commit message here :-)

Quoted 13 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +13 -0
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+#include "config.h"
+#include "lapi/sched.h"
+
+#define FS_NR_OPEN "/proc/sys/fs/nr_open"
+
+#ifdef HAVE_UNSHARE
+
+static void run(void)

testcases/kernel/syscalls/unshare/unshare03.c line 34

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+{
+	int nr_open;
+	struct rlimit rlimit;
+	pid_t pid;
+	struct tst_clone_args args = {
+		.flags = CLONE_FILES,
+		.exit_signal = SIGCHLD,
+	};
+
+	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);

Here we can print the number of open file descriptors, using tst_res(), to help debugging.

testcases/kernel/syscalls/unshare/unshare03.c line 36

Quoted 2 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +2 -0
+
+	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024);

In the previous version I made a mistake in the review. The original test is using /proc/sys/fs/nr_open to set limits first, then it reads back them from the same file in order to have a starting limit. This is probably needed due to the kernel configurations. So please bring back the SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); line I asked to remove. Sorry for that.

Also, if we are going to use new increments, we need to update the next increments as well according to the previous ones. So feel free to leave it as it was before.

I must have been distracted that day :-)

testcases/kernel/syscalls/unshare/unshare03.c line 41

Quoted 5 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +5 -0
+
+	rlimit.rlim_cur = nr_open + 16;
+	rlimit.rlim_max = nr_open + 16;
+
+	SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);

Here we can print the new limits after updating it, using tst_res(), to help debugging.

testcases/kernel/syscalls/unshare/unshare03.c line 53

Quoted 12 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +12 -0
+
+	SAFE_DUP2(2, nr_open + 8);
+
+	if (!SAFE_CLONE(&args)) {
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
+		TST_CHECKPOINT_WAKE(0);
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+	tst_res(TPASS, "Verify EMFILE error pass");

We don't need this since we already have TST_EXP_FAIL inside the child process.

Quoted 11 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +11 -0
+}
+
+static void setup(void)
+{
+	clone3_supported_by_kernel();
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,

testcases/kernel/syscalls/unshare/unshare03.c line 74

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+	.setup = setup,
+	.needs_checkpoints = 1,
+	.save_restore = (const struct tst_path_val[]) {
+		{FS_NR_OPEN, NULL, TST_SR_TCONF},
+		{}
+	},
+};
+
+#else
+TST_TEST_TCONF("unshare is undefined.");

unshare syscall is undefined.

testcases/kernel/syscalls/unshare/unshare03.c line 75

Quoted 1 line
testcases/kernel/syscalls/unshare/unshare03.c new file +1 -0
+#endif

The rest looks ok.

Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 16 lines · Andrea Cervesato

Hi!

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);

Here we can print the number of open file descriptors, using tst_res(), to help debugging.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024);

In the previous version I made a mistake in the review. The original test is using /proc/sys/fs/nr_open to set limits first, then it reads back them from the same file in order to have a starting limit. This is probably needed due to the kernel configurations. So please bring back the SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); line I asked to remove. Sorry for that.

Quoted 5 lines

Also, if we are going to use new increments, we need to update the next increments as well according to the previous ones. So feel free to leave it as it was before.

I must have been distracted that day :-)

No problem at all :)
I have sent new patch, feedback me if any misunderstanding.

Quoted 6 lines

The rest looks ok.

Andrea

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

PATCH v3 1/1 unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd()

In reply to Wei Gao via ltp

Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES).

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare03.c | 77 +++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c
runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
 unshare01 unshare01
 unshare02 unshare02
+unshare03 unshare03
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
 /unshare01
 /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +77 -0

77 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

PATCH v4 1/1 unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd()

Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES).

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare03.c | 77 +++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c
runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
 unshare01 unshare01
 unshare02 unshare02
+unshare03 unshare03
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
 /unshare01
 /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +77 -0

77 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Hi,

thanks for editing the last version. A couple of comments below.

Quoted 79 lines · Wei Gao via ltp

Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES).

---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare03.c | 77 +++++++++++++++++++
  3 files changed, 79 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c

runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
  unshare01 unshare01
  unshare02 unshare02
+unshare03 unshare03
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
  /unshare01
  /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +41 -0
new file mode 100644
index 000000000..c1f13bcd5
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Al Viro <[email protected]>
+ * Copyright (C) 2024 Wei Gao <[email protected]>
+ */
+
+/*\
+ * This test case based on kernel self-test unshare_test.c to check that
+ * the kernel handles the EMFILE error when a parent process changes file
+ * descriptor limits and the child process tries to unshare (CLONE_FILES).
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+#include "config.h"
+#include "lapi/sched.h"
+
+#define FS_NR_OPEN "/proc/sys/fs/nr_open"
+
+#ifdef HAVE_UNSHARE
+
+static void run(void)
+{
+	int nr_open;
+	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);
+
+	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024);
+
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+
+	rlimit.rlim_cur = nr_open + 1024;
+	rlimit.rlim_max = nr_open + 1024;
+

testcases/kernel/syscalls/unshare/unshare03.c line 51

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+	SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
+		nr_open + 1024);
+
+	SAFE_DUP2(2, nr_open + 64);
+
+	if (!SAFE_CLONE(&args)) {
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
+		TST_CHECKPOINT_WAKE(0);

There's no need to use synchronization mechanisms because at the end of the test we call tst_reap_children() waiting for all the children to be completed.

Quoted 16 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +16 -0
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+}
+
+static void setup(void)
+{
+	clone3_supported_by_kernel();
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.setup = setup,

testcases/kernel/syscalls/unshare/unshare03.c line 77

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+	.needs_checkpoints = 1,
+	.save_restore = (const struct tst_path_val[]) {
+		{FS_NR_OPEN, NULL, TST_SR_TCONF},
+		{}
+	},
+};
+
+#else
+TST_TEST_TCONF("unshare syscall is undefined.");
+#endif

The rest looks good. If you want I can edit the checkpoint thing and merge this patch.

Kind regards,
Andrea Cervesato

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw
Quoted 5 lines · Andrea Cervesato

Hi,

thanks for editing the last version. A couple of comments below.

On 3/4/25 05:06, Wei Gao via ltp wrote:

Quoted 11 lines

testcases/kernel/syscalls/unshare/unshare03.c line 51

Quoted 8 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +8 -0
+		nr_open + 1024);
+
+	SAFE_DUP2(2, nr_open + 64);
+
+	if (!SAFE_CLONE(&args)) {
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
+		TST_CHECKPOINT_WAKE(0);

There's no need to use synchronization mechanisms because at the end of the test we call tst_reap_children() waiting for all the children to be completed.

There are some race condition happen and trigger failure sometimes if you do not use this synchronization(Currently no idea why this happen).
Rerun case 30 times can trigger 2 or 3 cases failed if not use synchronization in my env.

Quoted 33 lines
Quoted 16 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +16 -0
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+}
+
+static void setup(void)
+{
+	clone3_supported_by_kernel();
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.setup = setup,

testcases/kernel/syscalls/unshare/unshare03.c line 77

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +10 -0
+	.needs_checkpoints = 1,
+	.save_restore = (const struct tst_path_val[]) {
+		{FS_NR_OPEN, NULL, TST_SR_TCONF},
+		{}
+	},
+};
+
+#else
+TST_TEST_TCONF("unshare syscall is undefined.");
+#endif

The rest looks good. If you want I can edit the checkpoint thing and merge this patch.

Kind regards,
Andrea Cervesato

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

Hi,

Quoted 10 lines · Wei Gao
Quoted 7 lines

testcases/kernel/syscalls/unshare/unshare03.c line 51

Quoted 4 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +4 -0
+	if (!SAFE_CLONE(&args)) {
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
+		TST_CHECKPOINT_WAKE(0);

There's no need to use synchronization mechanisms because at the end of the test we call tst_reap_children() waiting for all the children to be completed.

There are some race condition happen and trigger failure sometimes if you do not use this synchronization(Currently no idea why this happen).
Rerun case 30 times can trigger 2 or 3 cases failed if not use synchronization in my env.

I'm really trying to reproduce this issue, but I can't. Run the test with 100000 iterations and it didn't stuck. Can you please check again? I tried on TW.

Andrea

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

Hi,

Quoted 7 lines · Wei Gao
Quoted 7 lines

testcases/kernel/syscalls/unshare/unshare03.c line 51

Quoted 4 lines
testcases/kernel/syscalls/unshare/unshare03.c new file +4 -0
+	if (!SAFE_CLONE(&args)) {
+		SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
+		TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
+		TST_CHECKPOINT_WAKE(0);

There's no need to use synchronization mechanisms because at the end of the test we call tst_reap_children() waiting for all the children to be completed.

Quoted 7 lines
Quoted 3 lines

There are some race condition happen and trigger failure sometimes if you do not use this synchronization(Currently no idea why this happen).
Rerun case 30 times can trigger 2 or 3 cases failed if not use synchronization in my env.

I'm really trying to reproduce this issue, but I can't. Run the test with 100000 iterations and it didn't stuck. Can you please check again? I tried on TW.

I have retest on my local machine with following builds, issue seems not happen:
* openSUSE Leap 15.5 5.14.21-150500.53-default
* Debian 6.12.0-rc4-00047-gc2ee9f594da8-dirty

so i suppose we can merge my latest v5 patch without sync.

Quoted 2 lines

Andrea

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

PATCH v5 1/1 unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd()

In reply to Wei Gao via ltp

Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES).

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare03.c | 74 +++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c
runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
 unshare01 unshare01
 unshare02 unshare02
+unshare03 unshare03
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
 /unshare01
 /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +74 -0

74 lines not on this page · raw message

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Andrea Cervesato on v5 1/1
#12 lore raw

Hi!

Pushed with a small edit to define limits on top. Commit title was a bit too long, so I changed it.

testcases/kernel/syscalls/unshare/unshare03.c +0 -0
index 7298cdebe..7c5e71c4e 100644
@@ -17,12 +17,15 @@

 #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,
@@ -32,18 +35,19 @@ static void run(void) SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);

-    SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); +    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_open + 1024;
-    rlimit.rlim_max = nr_open + 1024;
+    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_open + 1024);
+        nr_limit);

-    SAFE_DUP2(2, nr_open + 64);
+    SAFE_DUP2(2, nr_open + NR_OPEN_DUP);

     if (!SAFE_CLONE(&args)) { SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);

Andrea

Quoted 112 lines · Wei Gao via ltp

Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES).

---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare03.c | 74 +++++++++++++++++++
  3 files changed, 76 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c

runtest/syscalls +1 -0
index ded035ee8..10800c1a3 100644
@@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01
 
  unshare01 unshare01
  unshare02 unshare02
+unshare03 unshare03
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index 855ffd055..e5b5c261d 100644
@@ -1,2 +1,3 @@
  /unshare01
  /unshare02
+/unshare03
testcases/kernel/syscalls/unshare/unshare03.c new file +74 -0
new file mode 100644
index 000000000..7298cdebe
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Al Viro <[email protected]>
+ * Copyright (C) 2024 Wei Gao <[email protected]>
+ */
+
+/*\
+ * This test case based on kernel self-test unshare_test.c to check that
+ * the kernel handles the EMFILE error when a parent process changes file
+ * descriptor limits and the child process tries to unshare (CLONE_FILES).
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+#include "config.h"
+#include "lapi/sched.h"
+
+#define FS_NR_OPEN "/proc/sys/fs/nr_open"
+
+#ifdef HAVE_UNSHARE
+
+static void run(void)
+{
+	int nr_open;
+	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);
+
+	SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024);
+
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
+
+	rlimit.rlim_cur = nr_open + 1024;
+	rlimit.rlim_max = nr_open + 1024;
+
+	SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
+	tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
+		nr_open + 1024);
+
+	SAFE_DUP2(2, nr_open + 64);
+
+	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)
+{
+	clone3_supported_by_kernel();
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.setup = setup,
+	.save_restore = (const struct tst_path_val[]) {
+		{FS_NR_OPEN, NULL, TST_SR_TCONF},
+		{}
+	},
+};
+
+#else
+TST_TEST_TCONF("unshare syscall is undefined.");
+#endif
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp