Skip to contentSkip to conversation

Add process_mrelease testing suite

PATCH Superseded Andrea Cervesato 3 participants 8 messages May 22, 2024 – Aug 12, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add process_mrelease testing suite

This testing suite is meant to add coverage for process_mrelease() syscall, introduced in the kernel 5.15.

The testing suite is providing following tests:
- process_mrelease01
- process_mrelease02

Since there's no man pages, please consider the following documentation instead:

https://lwn.net/Articles/864184/
https://lwn.net/Articles/865341/

---
Andrea Cervesato (3):
Add process_mrelease syscalls definition
Add process_mrelease01 test
Add process_mrelease02 test

 include/lapi/syscalls/aarch64.in                   |   1 +
 include/lapi/syscalls/arc.in                       |   1 +
 include/lapi/syscalls/arm.in                       |   1 +
 include/lapi/syscalls/hppa.in                      |   1 +
 include/lapi/syscalls/i386.in                      |   1 +
 include/lapi/syscalls/ia64.in                      |   1 +
 include/lapi/syscalls/mips_n32.in                  |   1 +
 include/lapi/syscalls/mips_n64.in                  |   1 +
 include/lapi/syscalls/mips_o32.in                  |   1 +
 include/lapi/syscalls/powerpc.in                   |   1 +
 include/lapi/syscalls/powerpc64.in                 |   1 +
 include/lapi/syscalls/s390.in                      |   1 +
 include/lapi/syscalls/s390x.in                     |   1 +
 include/lapi/syscalls/sh.in                        |   1 +
 include/lapi/syscalls/sparc.in                     |   1 +
 include/lapi/syscalls/sparc64.in                   |   1 +
 include/lapi/syscalls/x86_64.in                    |   1 +
 runtest/syscalls                                   |   3 +
 .../kernel/syscalls/process_mrelease/.gitignore    |   2 +
 .../kernel/syscalls/process_mrelease/Makefile      |   7 ++
 .../syscalls/process_mrelease/process_mrelease01.c | 118 +++++++++++++++++++++
 .../syscalls/process_mrelease/process_mrelease02.c |  75 +++++++++++++
 22 files changed, 222 insertions(+)

---
base-commit: e644691d30c3948a9788b735c51e09ca849ea47f
change-id: 20240522-process_mrelease-3f2632b432e6

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


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

PATCH 1/3 Add process_mrelease syscalls definition

From: Andrea Cervesato <[email protected]>

---
 include/lapi/syscalls/aarch64.in   | 1 +
 include/lapi/syscalls/arc.in       | 1 +
 include/lapi/syscalls/arm.in       | 1 +
 include/lapi/syscalls/hppa.in      | 1 +
 include/lapi/syscalls/i386.in      | 1 +
 include/lapi/syscalls/ia64.in      | 1 +
 include/lapi/syscalls/mips_n32.in  | 1 +
 include/lapi/syscalls/mips_n64.in  | 1 +
 include/lapi/syscalls/mips_o32.in  | 1 +
 include/lapi/syscalls/powerpc.in   | 1 +
 include/lapi/syscalls/powerpc64.in | 1 +
 include/lapi/syscalls/s390.in      | 1 +
 include/lapi/syscalls/s390x.in     | 1 +
 include/lapi/syscalls/sh.in        | 1 +
 include/lapi/syscalls/sparc.in     | 1 +
 include/lapi/syscalls/sparc64.in   | 1 +
 include/lapi/syscalls/x86_64.in    | 1 +
 17 files changed, 17 insertions(+)
include/lapi/syscalls/aarch64.in +1 -0
index 2cb6c2d87..98e6c69c5 100644
@@ -296,5 +296,6 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
 _sysctl 1078
include/lapi/syscalls/arc.in +1 -0
index 3e2ee9061..8ed05ce68 100644
@@ -316,4 +316,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/arm.in +1 -0
index 7bdbca533..d133a44be 100644
@@ -394,4 +394,5 @@ pidfd_getfd (__NR_SYSCALL_BASE+438)
 faccessat2 (__NR_SYSCALL_BASE+439)
 epoll_pwait2 (__NR_SYSCALL_BASE+441)
 quotactl_fd (__NR_SYSCALL_BASE+443)
+process_mrelease (__NR_SYSCALL_BASE+448)
 futex_waitv (__NR_SYSCALL_BASE+449)
include/lapi/syscalls/hppa.in +1 -0
index 8ebdafafb..578751e2e 100644
@@ -43,4 +43,5 @@ close_range 436
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/i386.in +1 -0
index 1472631c4..59a1d030b 100644
@@ -430,4 +430,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/ia64.in +1 -0
index 0ea6e9722..e410582d8 100644
@@ -343,4 +343,5 @@ pidfd_getfd 1462
 faccessat2 1463
 epoll_pwait2 1465
 quotactl_fd 1467
+process_mrelease 1472
 futex_waitv 1473
include/lapi/syscalls/mips_n32.in +1 -0
index e818c9d92..f2602a02f 100644
@@ -370,4 +370,5 @@ process_madvise 6440
 epoll_pwait2 6441
 mount_setattr 6442
 quotactl_fd 6443
+process_mrelease 6448
 futex_waitv 6449
include/lapi/syscalls/mips_n64.in +1 -0
index 6e15f43b3..f16762c69 100644
@@ -346,4 +346,5 @@ process_madvise 5440
 epoll_pwait2 5441
 mount_setattr 5442
 quotactl_fd 5443
+process_mrelease 5448
 futex_waitv 5449
include/lapi/syscalls/mips_o32.in +1 -0
index 921d5d331..88a0d7ca8 100644
@@ -416,4 +416,5 @@ process_madvise 4440
 epoll_pwait2 4441
 mount_setattr 4442
 quotactl_fd 4443
+process_mrelease 4448
 futex_waitv 4449
include/lapi/syscalls/powerpc.in +1 -0
index 545d9d3d6..20233339d 100644
@@ -423,4 +423,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/powerpc64.in +1 -0
index 545d9d3d6..20233339d 100644
@@ -423,4 +423,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/s390.in +1 -0
index 7213ac5f8..ae0c83636 100644
@@ -410,4 +410,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/s390x.in +1 -0
index 879012e2b..d1476e98f 100644
@@ -358,4 +358,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/sh.in +1 -0
index 7d5192a27..845deff4d 100644
@@ -404,4 +404,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/sparc.in +1 -0
index 91d2fb1c2..ec73eaa2b 100644
@@ -409,4 +409,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/sparc64.in +1 -0
index 1f2fc59b7..ecf8e1d22 100644
@@ -374,4 +374,5 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
include/lapi/syscalls/x86_64.in +1 -0
index dc61aa56e..415f3011d 100644
@@ -351,6 +351,7 @@ pidfd_getfd 438
 faccessat2 439
 epoll_pwait2 441
 quotactl_fd 443
+process_mrelease 448
 futex_waitv 449
 rt_sigaction 512
 rt_sigreturn 513
Signature · 5 lines
2.35.3


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

PATCH 2/3 Add process_mrelease01 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

This test verifies that process_mrelease() syscall is releasing memory from a killed process with memory allocation pending.

---
 runtest/syscalls                                   |   2 +
 .../kernel/syscalls/process_mrelease/.gitignore    |   1 +
 .../kernel/syscalls/process_mrelease/Makefile      |   7 ++
 .../syscalls/process_mrelease/process_mrelease01.c | 118 +++++++++++++++++++++
 4 files changed, 128 insertions(+)
runtest/syscalls +2 -0
index cf06ee563..46a85fd31 100644
@@ -1050,6 +1050,8 @@ preadv203_64 preadv203_64
 
 profil01 profil01
 
+process_mrelease01 process_mrelease01
+
 process_vm_readv01 process_vm01 -r
 process_vm_readv02 process_vm_readv02
 process_vm_readv03 process_vm_readv03
testcases/kernel/syscalls/process_mrelease/.gitignore new file +1 -0
new file mode 100644
index 000000000..673983858
@@ -0,0 +1 @@
+/process_mrelease01
testcases/kernel/syscalls/process_mrelease/Makefile new file +7 -0
new file mode 100644
index 000000000..8cf1b9024
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +118 -0

118 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

Re: [LTP] [PATCH 2/3] Add process_mrelease01 test

Hi!

Quoted 15 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +15 -0
+/*\
+ * [Description]
+ *
+ * This test verifies that process_mrelease() syscall is releasing memory from
+ * a killed process with memory allocation pending.
+ */
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define CHUNK (1 * TST_MB)
+#define MAX_SIZE_MB (128 * TST_MB)
+
+static void *mem;
+static volatile int *mem_size;

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 30

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +10 -0
+
+static void do_child(int size)
+{
+	tst_res(TINFO, "Child: allocate %d bytes", size);
+
+	mem = SAFE_MMAP(NULL,
+		size,
+		PROT_READ | PROT_WRITE,
+		MAP_PRIVATE | MAP_ANON,
+		0, 0);

This does not actually alloate any memory, it set ups the vmas but the actual memory is not allocated until you fault it by accessing it, so you have to actually touch every page to get it faulted, e.g. memset() it to something.

Quoted 16 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +16 -0
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
+
+	tst_res(TINFO, "Child: releasing memory");
+
+	SAFE_MUNMAP(mem, size);
+}
+
+static void run(void)
+{
+	int ret;
+	int pidfd;
+	int status;
+	pid_t pid;
+	volatile int restart;
+
+	for (*mem_size = CHUNK; *mem_size < MAX_SIZE_MB; *mem_size += CHUNK) {

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 58

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +10 -0
+		restart = 0;
+
+		pid = SAFE_FORK();
+		if (!pid) {
+			do_child(*mem_size);
+			exit(0);
+		}
+
+		TST_CHECKPOINT_WAIT(0);
+		tst_disable_oom_protection(pid);

What is this needed for?

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 72

Quoted 13 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +13 -0
+		pidfd = SAFE_PIDFD_OPEN(pid, 0);
+
+		tst_res(TINFO, "Parent: killing child with PID=%d", pid);
+
+		SAFE_KILL(pid, SIGKILL);
+
+		ret = tst_syscall(__NR_process_mrelease, pidfd, 0);
+		if (ret == -1) {
+			if (errno == ESRCH) {
+				tst_res(TINFO, "Parent: child terminated before process_mrelease()."
+					" Increase memory size and restart test");
+
+				restart = 1;

As far as I understand the documentation his condition should not happen until you have called wait() on the process.

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 77

Quoted 5 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +5 -0
+			} else {
+				tst_res(TFAIL | TERRNO, "process_mrelease(%d,0) error", pidfd);
+			}
+		} else {
+			tst_res(TPASS, "process_mrelease(%d,0) passed", pidfd);

Okay it passed, but did we free any memory?

Is the /proc/$pid/ still accessible before we wait on the killed process? If it is we can parse the proc maps before and after process_mrelease and check if the memory was really freed.

Quoted 8 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +8 -0
+		}
+
+		SAFE_WAITPID(-1, &status, 0);
+		SAFE_CLOSE(pidfd);
+
+		if (!restart)
+			break;
+	}

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 95

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +10 -0
+}
+
+static void setup(void)
+{
+	mem_size = SAFE_MMAP(
+		NULL,
+		sizeof(int),
+		PROT_READ | PROT_WRITE,
+		MAP_SHARED | MAP_ANONYMOUS,
+		-1, 0);

Why do we keep the size in shared memory? The forked child has COW access to the parent memory, we can use the variables set in the parent just fine.

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 101

Quoted 6 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +6 -0
+}
+
+static void cleanup(void)
+{
+	if (mem)
+		SAFE_MUNMAP(mem, *mem_size);

This is allocated in chid and never non NULL in parent.

Quoted 23 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +16 -0
+	if (mem_size)
+		SAFE_MUNMAP((void *)mem_size, sizeof(int));
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_root = 1,
+	.forks_child = 1,
+	.min_kver = "5.15",
+	.needs_checkpoints = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_MMU=y",
+	},
+};
Signature · 6 lines
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH 2/3] Add process_mrelease01 test

Hi!

Quoted 22 lines · Cyril Hrubis

Hi!

Quoted 21 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +21 -0
+/*\
+ * [Description]
+ *
+ * This test verifies that process_mrelease() syscall is releasing memory from
+ * a killed process with memory allocation pending.
+ */
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define CHUNK (1 * TST_MB)
+#define MAX_SIZE_MB (128 * TST_MB)
+
+static void *mem;
+static volatile int *mem_size;
+
+static void do_child(int size)
+{
+	tst_res(TINFO, "Child: allocate %d bytes", size);
+
+	mem = SAFE_MMAP(NULL,
Quoted 9 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 30

Quoted 4 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +4 -0
+		size,
+		PROT_READ | PROT_WRITE,
+		MAP_PRIVATE | MAP_ANON,
+		0, 0);

This does not actually alloate any memory, it set ups the vmas but the actual memory is not allocated until you fault it by accessing it, so you have to actually touch every page to get it faulted, e.g. memset() it to something.

Yes this is true. I will fix it.

Quoted 19 lines
Quoted 19 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +19 -0
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
+
+	tst_res(TINFO, "Child: releasing memory");
+
+	SAFE_MUNMAP(mem, size);
+}
+
+static void run(void)
+{
+	int ret;
+	int pidfd;
+	int status;
+	pid_t pid;
+	volatile int restart;
+
+	for (*mem_size = CHUNK; *mem_size < MAX_SIZE_MB; *mem_size += CHUNK) {
+		restart = 0;
+
+		pid = SAFE_FORK();
Quoted 8 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 58

Quoted 7 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +7 -0
+		if (!pid) {
+			do_child(*mem_size);
+			exit(0);
+		}
+
+		TST_CHECKPOINT_WAIT(0);
+		tst_disable_oom_protection(pid);

What is this needed for?

process_mrelease() overlaps with OOM, since it's used to free up thread memory instead of OOM. For this reason we need to disable it, so any OOM operation after kill() won't be taken by it.

Quoted 16 lines
Quoted 4 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +3 -0
+		pidfd = SAFE_PIDFD_OPEN(pid, 0);
+
+		tst_res(TINFO, "Parent: killing child with PID=%d", pid);

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 72

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +10 -0
+
+		SAFE_KILL(pid, SIGKILL);
+
+		ret = tst_syscall(__NR_process_mrelease, pidfd, 0);
+		if (ret == -1) {
+			if (errno == ESRCH) {
+				tst_res(TINFO, "Parent: child terminated before process_mrelease()."
+					" Increase memory size and restart test");
+
+				restart = 1;

As far as I understand the documentation his condition should not happen until you have called wait() on the process.

The minimum requirement is that child has been killed and it's accessible from process_mrelease, so if we call wait() on the child, we won't be able to reach it anymore, causing ESRCH in process_mrelease().

Please check mrelease_test.c kselftest.

Quoted 11 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 77

Quoted 6 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +5 -0
+			} else {
+				tst_res(TFAIL | TERRNO, "process_mrelease(%d,0) error", pidfd);
+			}
+		} else {
+			tst_res(TPASS, "process_mrelease(%d,0) passed", pidfd);

Okay it passed, but did we free any memory?

Is the /proc/$pid/ still accessible before we wait on the killed process? If it is we can parse the proc maps before and after process_mrelease and check if the memory was really freed.

Good idea, I will do that.

Quoted 13 lines
Quoted 13 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +13 -0
+		}
+
+		SAFE_WAITPID(-1, &status, 0);
+		SAFE_CLOSE(pidfd);
+
+		if (!restart)
+			break;
+	}
+}
+
+static void setup(void)
+{
+	mem_size = SAFE_MMAP(
Quoted 8 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 95

Quoted 5 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +5 -0
+		NULL,
+		sizeof(int),
+		PROT_READ | PROT_WRITE,
+		MAP_SHARED | MAP_ANONYMOUS,
+		-1, 0);

Why do we keep the size in shared memory? The forked child has COW access to the parent memory, we can use the variables set in the parent just fine.

+1

Quoted 25 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease01.c line 101

Quoted 6 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +6 -0
+}
+
+static void cleanup(void)
+{
+	if (mem)
+		SAFE_MUNMAP(mem, *mem_size);

This is allocated in chid and never non NULL in parent.

Quoted 17 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease01.c new file +16 -0
+	if (mem_size)
+		SAFE_MUNMAP((void *)mem_size, sizeof(int));
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_root = 1,
+	.forks_child = 1,
+	.min_kver = "5.15",
+	.needs_checkpoints = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_MMU=y",
+	},
+};
Quoted 6 lines
Quoted 6 lines
Signature · 5 lines
2.35.3


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

Andrea

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

PATCH 3/3 Add process_mrelease02 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

This test verifies that process_mrelease() syscall correctly raises the errors.

---
 runtest/syscalls                                   |  1 +
 .../kernel/syscalls/process_mrelease/.gitignore    |  1 +
 .../syscalls/process_mrelease/process_mrelease02.c | 75 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)
runtest/syscalls +1 -0
index 46a85fd31..c2fe919f0 100644
@@ -1051,6 +1051,7 @@ preadv203_64 preadv203_64
 profil01 profil01
 
 process_mrelease01 process_mrelease01
+process_mrelease02 process_mrelease02
 
 process_vm_readv01 process_vm01 -r
 process_vm_readv02 process_vm_readv02
testcases/kernel/syscalls/process_mrelease/.gitignore +1 -0
index 673983858..f1e7a8fea 100644
@@ -1 +1,2 @@
 /process_mrelease01
+/process_mrelease02
testcases/kernel/syscalls/process_mrelease/process_mrelease02.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
#6 lore raw

Re: [LTP] [PATCH 3/3] Add process_mrelease02 test

Quoted 57 lines · Andrea Cervesato

From: Andrea Cervesato <[email protected]>

This test verifies that process_mrelease() syscall correctly raises the errors.

---
 runtest/syscalls                                   |  1 +
 .../kernel/syscalls/process_mrelease/.gitignore    |  1 +
 .../syscalls/process_mrelease/process_mrelease02.c | 75 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)
runtest/syscalls +1 -0
index 46a85fd31..c2fe919f0 100644
@@ -1051,6 +1051,7 @@ preadv203_64 preadv203_64
 profil01 profil01
 
 process_mrelease01 process_mrelease01
+process_mrelease02 process_mrelease02
 
 process_vm_readv01 process_vm01 -r
 process_vm_readv02 process_vm_readv02
testcases/kernel/syscalls/process_mrelease/.gitignore +1 -0
index 673983858..f1e7a8fea 100644
@@ -1 +1,2 @@
 /process_mrelease01
+/process_mrelease02
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +20 -0
new file mode 100644
index 000000000..ac13317ee
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that process_mrelease() syscall is raising errors:
+ * * EBADF when a bad file descriptor is given
+ * * EINVAL when flags is not zero
+ * * EINVAL when memory of a task cannot be released because it's still running
+ */
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+static int badfd = -1;
+static int pidfd;
+

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 30

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+static struct tcase {
+	int needs_child;
+	int *fd;
+	int flags;
+	int exp_errno;
+	char *msg;
+} tcases[] = {
+	{0, &badfd,  0, EBADF,  "bad file descriptor"},
+	{1, &pidfd, -1, EINVAL, "flags is not 0"},
+	{1, &pidfd,  0, EINVAL,  "memory of running task cannot be released"},

We can easily trigger ESCHR as well, just fork a child that just exits, get pidfd to that child and then wait the child.

Quoted 11 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +11 -0
+};
+
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	if (tc->needs_child) {
+		pid_t pid;
+
+		pid = SAFE_FORK();
+		if (!pid) {

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 51

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+			tst_res(TINFO, "Keep child alive");
+
+			TST_CHECKPOINT_WAKE_AND_WAIT(0);
+			exit(0);
+		}
+
+		TST_CHECKPOINT_WAIT(0);
+
+		pidfd = SAFE_PIDFD_OPEN(pid, 0);
+	}

We can set up several types of a child processes in the test setup, there is no need to do this on every iteration.

Similarily for the ESCHR case we can just do the same for EINVAL cases. For the invalid flags we would need a process that did actually exit but wasn't waited for. And for the second EINVAL case we would need a running process, so perhaps just a child that sleeps in pause().

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+	TST_EXP_FAIL(tst_syscall(__NR_process_mrelease, *tc->fd, tc->flags),
+		tc->exp_errno,
+		"%s", tc->msg);
+
+	if (tc->needs_child) {
+		SAFE_CLOSE(pidfd);
+
+		TST_CHECKPOINT_WAKE(0);
+	}
+}

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 72

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.forks_child = 1,
+	.min_kver = "5.15",
+	.needs_checkpoints = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_MMU=y",

I do not think this is necessary, LTP does not run on non-MMU targets anyways.

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 75

Quoted 3 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +3 -0
+		NULL,
+	},
+};

Also I think that it would make sense to CC Michal Hocko on the v2 since he did review the kernel patches for this syscall.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH 3/3] Add process_mrelease02 test

Hi Cyril,

Quoted 86 lines · Cyril Hrubis
Quoted 57 lines · Andrea Cervesato

From: Andrea Cervesato <[email protected]>

This test verifies that process_mrelease() syscall correctly raises the errors.

---
  runtest/syscalls                                   |  1 +
  .../kernel/syscalls/process_mrelease/.gitignore    |  1 +
  .../syscalls/process_mrelease/process_mrelease02.c | 75 ++++++++++++++++++++++
  3 files changed, 77 insertions(+)
runtest/syscalls +1 -0
index 46a85fd31..c2fe919f0 100644
@@ -1051,6 +1051,7 @@ preadv203_64 preadv203_64
  profil01 profil01
 
  process_mrelease01 process_mrelease01
+process_mrelease02 process_mrelease02
 
  process_vm_readv01 process_vm01 -r
  process_vm_readv02 process_vm_readv02
testcases/kernel/syscalls/process_mrelease/.gitignore +1 -0
index 673983858..f1e7a8fea 100644
@@ -1 +1,2 @@
  /process_mrelease01
+/process_mrelease02
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +20 -0
new file mode 100644
index 000000000..ac13317ee
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that process_mrelease() syscall is raising errors:
+ * * EBADF when a bad file descriptor is given
+ * * EINVAL when flags is not zero
+ * * EINVAL when memory of a task cannot be released because it's still running
+ */
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+static int badfd = -1;
+static int pidfd;
+

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 30

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+static struct tcase {
+	int needs_child;
+	int *fd;
+	int flags;
+	int exp_errno;
+	char *msg;
+} tcases[] = {
+	{0, &badfd,  0, EBADF,  "bad file descriptor"},
+	{1, &pidfd, -1, EINVAL, "flags is not 0"},
+	{1, &pidfd,  0, EINVAL,  "memory of running task cannot be released"},

We can easily trigger ESCHR as well, just fork a child that just exits, get pidfd to that child and then wait the child.

Quoted 15 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +15 -0
+};
+
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	if (tc->needs_child) {
+		pid_t pid;
+
+		pid = SAFE_FORK();
+		if (!pid) {
+			tst_res(TINFO, "Keep child alive");
+
+			TST_CHECKPOINT_WAKE_AND_WAIT(0);
+			exit(0);
Quoted 8 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 51

Quoted 6 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +6 -0
+		}
+
+		TST_CHECKPOINT_WAIT(0);
+
+		pidfd = SAFE_PIDFD_OPEN(pid, 0);
+	}

We can set up several types of a child processes in the test setup, there is no need to do this on every iteration.

After working a bit on this I can say that LTP doesn't allow it. Simply because tst_reap_children() is called before the end of the test, which means we have to end all children, even if they need to be alive. And the have to be alive for the next step. So the first approach is the right one.

Quoted 29 lines

Similarily for the ESCHR case we can just do the same for EINVAL cases. For the invalid flags we would need a process that did actually exit but wasn't waited for. And for the second EINVAL case we would need a running process, so perhaps just a child that sleeps in pause().

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+	TST_EXP_FAIL(tst_syscall(__NR_process_mrelease, *tc->fd, tc->flags),
+		tc->exp_errno,
+		"%s", tc->msg);
+
+	if (tc->needs_child) {
+		SAFE_CLOSE(pidfd);
+
+		TST_CHECKPOINT_WAKE(0);
+	}
+}

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 72

Quoted 10 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +10 -0
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.forks_child = 1,
+	.min_kver = "5.15",
+	.needs_checkpoints = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_MMU=y",

I do not think this is necessary, LTP does not run on non-MMU targets anyways.

Quoted 6 lines

testcases/kernel/syscalls/process_mrelease/process_mrelease02.c line 75

Quoted 3 lines
testcases/kernel/syscalls/process_mrelease/process_mrelease02.c new file +3 -0
+		NULL,
+	},
+};

Also I think that it would make sense to CC Michal Hocko on the v2 since he did review the kernel patches for this syscall.

Andrea

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