Skip to contentSkip to conversation

syscalls/setrlimit06: Add new test for RLIMIT_CPU

PATCH New Xiao Yang 2 participants 6 messages Jan 31, 2019 – Feb 22, 2019

lore
Share & tools
lore

Xiao Yang

author
#0 lore raw

PATCH 1/1 syscalls/setrlimit06: Add new test for RLIMIT_CPU

Set CPU time limit for a process and check its behavior when reaching CPU time limit.

Note:
This is also a regression test for commit c3bca5d450b62 in kernel:
"posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated"

---
 runtest/syscalls                                  |   1 +
 testcases/kernel/syscalls/setrlimit/.gitignore    |   1 +
 testcases/kernel/syscalls/setrlimit/setrlimit06.c | 108 ++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 testcases/kernel/syscalls/setrlimit/setrlimit06.c
runtest/syscalls +1 -0
index 668c87c..2e36709 100644
@@ -1197,6 +1197,7 @@ setrlimit02 setrlimit02
 setrlimit03 setrlimit03
 setrlimit04 setrlimit04
 setrlimit05 setrlimit05
+setrlimit06 setrlimit06
 
 set_robust_list01 set_robust_list01
 set_thread_area01 set_thread_area01
testcases/kernel/syscalls/setrlimit/.gitignore +1 -0
index e91f7e8..a790eb9 100644
@@ -3,3 +3,4 @@
 /setrlimit03
 /setrlimit04
 /setrlimit05
+/setrlimit06
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 syscalls/setrlimit06: Add new test for RLIMIT_CPU

Hi!

Quoted 68 lines

This is also a regression test for commit c3bca5d450b62 in kernel:
"posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated"

---
 runtest/syscalls                                  |   1 +
 testcases/kernel/syscalls/setrlimit/.gitignore    |   1 +
 testcases/kernel/syscalls/setrlimit/setrlimit06.c | 108 ++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 testcases/kernel/syscalls/setrlimit/setrlimit06.c
runtest/syscalls +1 -0
index 668c87c..2e36709 100644
@@ -1197,6 +1197,7 @@ setrlimit02 setrlimit02
 setrlimit03 setrlimit03
 setrlimit04 setrlimit04
 setrlimit05 setrlimit05
+setrlimit06 setrlimit06
 
 set_robust_list01 set_robust_list01
 set_thread_area01 set_thread_area01
testcases/kernel/syscalls/setrlimit/.gitignore +1 -0
index e91f7e8..a790eb9 100644
@@ -3,3 +3,4 @@
 /setrlimit03
 /setrlimit04
 /setrlimit05
+/setrlimit06
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +30 -0
new file mode 100644
index 0000000..d6004a5
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+ * Author: Xiao Yang <[email protected]>
+ */
+
+/*
+ * Description:
+ * Set CPU time limit for a process and check its behavior
+ * when reaching CPU time limit.
+ * 1) Process received SIGXCPU signal when reaching soft limit
+ *    of CPU time.
+ * 2) Process received SIGKILL signal when reaching hard limit
+ *    of CPU time.
+ *
+ * Note:
+ * This is also a regression test for the following kernel bug:
+ * 'c3bca5d450b62 ("posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated")'
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+
+#include "tst_test.h"
+

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 40

Quoted 10 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +10 -0
+static volatile int end;
+
+static void sighandler(int sig)
+{
+	end = sig;
+}
+
+static void setup(void)
+{
+	SAFE_SIGNAL(SIGALRM, SIG_DFL);

The SIGALRM handler should be set to SIG_DFL for the test process in the test library. At least that's what we do in fork_testrun() function in tst_test.c

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 51

Quoted 11 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +11 -0
+	SAFE_SIGNAL(SIGXCPU, sighandler);
+}
+
+static void verify_setrlimit(void)
+{
+	int status;
+	pid_t pid;
+
+	pid = vfork();
+	if (pid == -1)
+		tst_brk(TBROK, "vfork() failed");

Why vfork? You are not supposed to use vfork() for anything but vfork()
+ exec(). As it is the code below invokes undefined behavior.

Quoted 17 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +17 -0
+	if (!pid) {
+		struct rlimit rlim = {
+			.rlim_cur = 2,
+			.rlim_max = 3,
+		};
+
+		TEST(setrlimit(RLIMIT_CPU, &rlim));
+		if (TST_RET == -1) {
+			tst_res(TFAIL | TTERRNO,
+				"setrlimit(RLIMIT_CPU) failed");
+			_exit(1);
+		}
+
+		alarm(10);
+
+		while (1);
+	}

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 79

Quoted 10 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +10 -0
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
+		return;
+
+	if (WIFSIGNALED(status)) {
+		if (WTERMSIG(status) == SIGKILL && end == SIGXCPU) {
+			tst_res(TPASS,
+				"Process received SIGXCPU and SIGKILL when reaching soft and hard limit of CPU time");

This message is far too long, please be short and to the point, somethig as:

	tst_res(TPASS, "Got SIGXCPU then SIGKILL after reaching both limits");

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 85

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGKILL && !end) {
+			tst_res(TFAIL,
+				"Process only received SIGKILL when reaching CPU time hard limit");

Here as well, something as:

	tst_res(TFAIL, "Got only SIGKILL after reaching both limits");

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 91

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM && end == SIGXCPU) {
+			tst_res(TFAIL,
+				"Process only received SIGXCPU when reaching CPU time soft limit");

And here as well.

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 97

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM && !end) {
+			tst_res(TFAIL,
+				"Process didn't receive any signal when reaching soft and hard limit of CPU time");

And here as well.

Quoted 11 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +11 -0
+			return;
+		}
+	}
+
+	tst_res(TBROK, "child %s", tst_strstatus(status));
+}
+
+static struct tst_test test = {
+	.test_all = verify_setrlimit,
+	.setup = setup,
+};
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH 1/1 syscalls/setrlimit06: Add new test for RLIMIT_CPU

Quoted 74 lines · Cyril Hrubis

Hi!

Quoted 73 lines

This is also a regression test for commit c3bca5d450b62 in kernel:
"posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated"

---
  runtest/syscalls                                  |   1 +
  testcases/kernel/syscalls/setrlimit/.gitignore    |   1 +
  testcases/kernel/syscalls/setrlimit/setrlimit06.c | 108 ++++++++++++++++++++++
  3 files changed, 110 insertions(+)

create mode 100644 testcases/kernel/syscalls/setrlimit/setrlimit06.c

runtest/syscalls +1 -0
index 668c87c..2e36709 100644
@@ -1197,6 +1197,7 @@ setrlimit02 setrlimit02
  setrlimit03 setrlimit03
  setrlimit04 setrlimit04
  setrlimit05 setrlimit05
+setrlimit06 setrlimit06
 
  set_robust_list01 set_robust_list01
  set_thread_area01 set_thread_area01
testcases/kernel/syscalls/setrlimit/.gitignore +1 -0
index e91f7e8..a790eb9 100644
@@ -3,3 +3,4 @@
  /setrlimit03
  /setrlimit04
  /setrlimit05
+/setrlimit06
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +35 -0
new file mode 100644
index 0000000..d6004a5
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+ * Author: Xiao Yang<[email protected]>
+ */
+
+/*
+ * Description:
+ * Set CPU time limit for a process and check its behavior
+ * when reaching CPU time limit.
+ * 1) Process received SIGXCPU signal when reaching soft limit
+ *    of CPU time.
+ * 2) Process received SIGKILL signal when reaching hard limit
+ *    of CPU time.
+ *
+ * Note:
+ * This is also a regression test for the following kernel bug:
+ * 'c3bca5d450b62 ("posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated")'
+ */
+
+#define _GNU_SOURCE
+#include<errno.h>
+#include<sys/types.h>
+#include<unistd.h>
+#include<sys/time.h>
+#include<sys/resource.h>
+#include<sys/wait.h>
+
+#include "tst_test.h"
+
+static volatile int end;
+
+static void sighandler(int sig)
+{
+	end = sig;
Quoted 8 lines

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 40

Quoted 5 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +5 -0
+}
+
+static void setup(void)
+{
+	SAFE_SIGNAL(SIGALRM, SIG_DFL);

The SIGALRM handler should be set to SIG_DFL for the test process in the test library. At least that's what we do in fork_testrun() function in tst_test.c

Hi Cyril,

Thanks for your remind, and i will remove it.

Quoted 5 lines
Quoted 5 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +5 -0
+	SAFE_SIGNAL(SIGXCPU, sighandler);
+}
+
+static void verify_setrlimit(void)
+{
Quoted 8 lines

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 51

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+	int status;
+	pid_t pid;
+
+	pid = vfork();
+	if (pid == -1)
+		tst_brk(TBROK, "vfork() failed");

Why vfork? You are not supposed to use vfork() for anything but vfork()
+ exec(). As it is the code below invokes undefined behavior.

By vfork, i want to ensure that the value of end can be shared by child and parnet.
Perhaps, i should use shared memory instead of vfork. :-)

Quoted 30 lines
Quoted 17 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +17 -0
+	if (!pid) {
+		struct rlimit rlim = {
+			.rlim_cur = 2,
+			.rlim_max = 3,
+		};
+
+		TEST(setrlimit(RLIMIT_CPU,&rlim));
+		if (TST_RET == -1) {
+			tst_res(TFAIL | TTERRNO,
+				"setrlimit(RLIMIT_CPU) failed");
+			_exit(1);
+		}
+
+		alarm(10);
+
+		while (1);
+	}

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 79

Quoted 10 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +10 -0
+
+	SAFE_WAITPID(pid,&status, 0);
+
+	if (WIFEXITED(status)&&  WEXITSTATUS(status) == 1)
+		return;
+
+	if (WIFSIGNALED(status)) {
+		if (WTERMSIG(status) == SIGKILL&&  end == SIGXCPU) {
+			tst_res(TPASS,
+				"Process received SIGXCPU and SIGKILL when reaching soft and hard limit of CPU time");

This message is far too long, please be short and to the point, somethig as:

Quoted 2 lines
	tst_res(TPASS, "Got SIGXCPU then SIGKILL after reaching both limits");

I will shorten all message as you said.

Best Regards,
Xiao Yang

Quoted 37 lines

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 85

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGKILL&&  !end) {
+			tst_res(TFAIL,
+				"Process only received SIGKILL when reaching CPU time hard limit");

Here as well, something as:

	tst_res(TFAIL, "Got only SIGKILL after reaching both limits");

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 91

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM&&  end == SIGXCPU) {
+			tst_res(TFAIL,
+				"Process only received SIGXCPU when reaching CPU time soft limit");

And here as well.

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 97

Quoted 6 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +6 -0
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM&&  !end) {
+			tst_res(TFAIL,
+				"Process didn't receive any signal when reaching soft and hard limit of CPU time");

And here as well.

Quoted 11 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +11 -0
+			return;
+		}
+	}
+
+	tst_res(TBROK, "child %s", tst_strstatus(status));
+}
+
+static struct tst_test test = {
+	.test_all = verify_setrlimit,
+	.setup = setup,
+};
author
#3 lore raw

PATCH v2 1/1 syscalls/setrlimit06: Add new test for RLIMIT_CPU

In reply to Cyril Hrubis

Set CPU time limit for a process and check its behavior when reaching CPU time limit.

Note:
This is also a regression test for commit c3bca5d450b62 in kernel:
"posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated"

---
 runtest/syscalls                                  |   1 +
 testcases/kernel/syscalls/setrlimit/.gitignore    |   1 +
 testcases/kernel/syscalls/setrlimit/setrlimit06.c | 115 ++++++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 testcases/kernel/syscalls/setrlimit/setrlimit06.c
runtest/syscalls +1 -0
index 668c87c..2e36709 100644
@@ -1197,6 +1197,7 @@ setrlimit02 setrlimit02
 setrlimit03 setrlimit03
 setrlimit04 setrlimit04
 setrlimit05 setrlimit05
+setrlimit06 setrlimit06
 
 set_robust_list01 set_robust_list01
 set_thread_area01 set_thread_area01
testcases/kernel/syscalls/setrlimit/.gitignore +1 -0
index e91f7e8..a790eb9 100644
@@ -3,3 +3,4 @@
 /setrlimit03
 /setrlimit04
 /setrlimit05
+/setrlimit06
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH v2 1/1 syscalls/setrlimit06: Add new test for RLIMIT_CPU

Hi Cyril,

Could you help me review the v2 patch.
Thanks a lot. :-)

Best Regards,
Xiao Yang

Quoted 157 lines · Xiao Yang

Set CPU time limit for a process and check its behavior when reaching CPU time limit.

Note:
This is also a regression test for commit c3bca5d450b62 in kernel:
"posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated"

---
 runtest/syscalls                                  |   1 +
 testcases/kernel/syscalls/setrlimit/.gitignore    |   1 +
 testcases/kernel/syscalls/setrlimit/setrlimit06.c | 115 ++++++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 testcases/kernel/syscalls/setrlimit/setrlimit06.c
runtest/syscalls +1 -0
index 668c87c..2e36709 100644
@@ -1197,6 +1197,7 @@ setrlimit02 setrlimit02
 setrlimit03 setrlimit03
 setrlimit04 setrlimit04
 setrlimit05 setrlimit05
+setrlimit06 setrlimit06
 
 set_robust_list01 set_robust_list01
 set_thread_area01 set_thread_area01
testcases/kernel/syscalls/setrlimit/.gitignore +1 -0
index e91f7e8..a790eb9 100644
@@ -3,3 +3,4 @@
 /setrlimit03
 /setrlimit04
 /setrlimit05
+/setrlimit06
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +115 -0
new file mode 100644
index 0000000..76e84e3
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+ * Author: Xiao Yang <[email protected]>
+ */
+
+/*
+ * Description:
+ * Set CPU time limit for a process and check its behavior
+ * after reaching CPU time limit.
+ * 1) Process got SIGXCPU after reaching soft limit of CPU time.
+ * 2) Process got SIGKILL after reaching hard limit of CPU time.
+ *
+ * Note:
+ * This is also a regression test for the following kernel bug:
+ * 'c3bca5d450b62 ("posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated")'
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+#include "tst_test.h"
+
+static int *end;
+
+static void sighandler(int sig)
+{
+	*end = sig;
+}
+
+static void setup(void)
+{
+	SAFE_SIGNAL(SIGXCPU, sighandler);
+
+	end = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+}
+
+static void cleanup(void)
+{
+	if (end)
+		SAFE_MUNMAP(end, sizeof(int));
+}
+
+static void verify_setrlimit(void)
+{
+	int status;
+	pid_t pid;
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		struct rlimit rlim = {
+			.rlim_cur = 2,
+			.rlim_max = 3,
+		};
+
+		TEST(setrlimit(RLIMIT_CPU, &rlim));
+		if (TST_RET == -1) {
+			tst_res(TFAIL | TTERRNO,
+				"setrlimit(RLIMIT_CPU) failed");
+			exit(1);
+		}
+
+		alarm(10);
+
+		while (1);
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
+		return;
+
+	if (WIFSIGNALED(status)) {
+		if (WTERMSIG(status) == SIGKILL && *end == SIGXCPU) {
+			tst_res(TPASS,
+				"Got SIGXCPU then SIGKILL after reaching both limit");
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGKILL && !*end) {
+			tst_res(TFAIL,
+				"Got only SIGKILL after reaching both limit");
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM && *end == SIGXCPU) {
+			tst_res(TFAIL,
+				"Got only SIGXCPU after reaching both limit");
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM && !*end) {
+			tst_res(TFAIL,
+				"Got no signal after reaching both limit");
+			return;
+		}
+	}
+
+	tst_res(TBROK, "Child %s", tst_strstatus(status));
+}
+
+static struct tst_test test = {
+	.test_all = verify_setrlimit,
+	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1,
+};
#5 lore raw

PATCH v2 1/1 syscalls/setrlimit06: Add new test for RLIMIT_CPU

In reply to Xiao Yang

Hi!
Pushed with a minor change, thanks.

testcases/kernel/syscalls/setrlimit/setrlimit06.c line 56

Quoted 5 lines
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +5 -0
+static void verify_setrlimit(void)
+{
+	int status;
+	pid_t pid;
+

Added *end = 0 here so that it's reset in a case of test looping (the -i parameter).

Quoted 1 line
testcases/kernel/syscalls/setrlimit/setrlimit06.c new file +1 -0
+	pid = SAFE_FORK();
Signature · 2 lines
Cyril Hrubis
[email protected]