diff mbox series

Add case about arch_prctl syscall.

Message ID 20240506070336.2711930-2-lufei@uniontech.com
State Changes Requested
Headers show
Series Add case about arch_prctl syscall. | expand

Commit Message

lufei May 6, 2024, 7:03 a.m. UTC
Add testcase about arch_prctl syscall.

Signed-off-by: Lu Fei <lufei@uniontech.com>
---
 configure.ac                                  |  1 +
 runtest/syscalls                              |  2 +
 .../kernel/syscalls/arch_prctl/.gitignore     |  1 +
 testcases/kernel/syscalls/arch_prctl/Makefile |  8 +++
 .../kernel/syscalls/arch_prctl/arch_prctl01.c | 71 +++++++++++++++++++
 5 files changed, 83 insertions(+)
 create mode 100644 testcases/kernel/syscalls/arch_prctl/.gitignore
 create mode 100644 testcases/kernel/syscalls/arch_prctl/Makefile
 create mode 100644 testcases/kernel/syscalls/arch_prctl/arch_prctl01.c

Comments

Cyril Hrubis May 6, 2024, 9:53 a.m. UTC | #1
Hi!
> +static void run(unsigned int index)
> +{
> +	FILE *fd;

Please do not call FILE * pointer fd, that is confusing since fd is
usually a file descriptor. FILE * pointer is usually just call f.

> +	char needed_flag[] = "cpuid_fault";

I would include the space before flag name just to be extra careful we
do not match anything that has this as a substring. E.g. if in the
future they add something that would be named "no_cpuid_fault" this
would still match strstr(line, "cpuid_fault") but not
strstr(line, " cpuid_fault"); Ideally we would check that there is a
space after the string, but we can't just do that with strstr() since
the flag may be actually last in the list. So the only posibility would
be using the pointer from strstr() and checking that the character after
the matched string is also a space.

> +	char *line = NULL;
> +	size_t len = 0;
> +	bool tag = 0;
> +
> +	fd = SAFE_FOPEN("/proc/cpuinfo", "r");
> +
> +	while (getline(&line, &len, fd) != -1) {
> +		if (strncmp(line, "flag", strlen("flag")) == 0 &&
                                      ^
				      why not just whole "flags" ?
> +				strstr(line, needed_flag) != NULL) {
> +			tag = 1;
> +			break;
> +		}
> +	}

This whole check for flag should be done in the test setup so that we do
not parse the /proc/cpuinfo on each test iteration (-i command line
option).

> +	if (!tag)
> +		tst_brk(TCONF, "CPU need %s flag.", needed_flag);

Why don't we check that the prctl() returns ENODEV when the flag is not
present. That is a valid testcase as well.

> +	// index == 0 for disable cpuid, 1 for enable cpuid.
> +	TST_EXP_PASS(arch_prctl_set(ARCH_SET_CPUID, index));
> +
> +	TEST(arch_prctl_get(ARCH_GET_CPUID));
> +
> +	if (TST_RET == index)
> +		tst_res(TPASS, "get cpuid succeed.");
> +	else
> +		tst_res(TFAIL, "get cpuid failed.");
> +}
> +
> +static struct tst_test test = {
> +	.test = run,
> +	.tcnt = 2,
> +	.min_kver = "4.12",
> +	.supported_archs = (const char *const []){"x86_64", "x86", NULL}
> +};
> +
> +#else /* HAVE_ASM_PRCTL_H */
> +TST_TEST_TCONF("missing <asm/prctl.h>");
> +#endif
> -- 
> 2.39.3
>
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index 1d7e862d8..0dcaddc0f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -41,6 +41,7 @@  AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     asm/ldt.h \
+    asm/prctl.h \
     cpuid.h \
     emmintrin.h \
     ifaddrs.h \
diff --git a/runtest/syscalls b/runtest/syscalls
index 7794f1465..505b4243d 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -31,6 +31,8 @@  alarm05 alarm05
 alarm06 alarm06
 alarm07 alarm07
 
+arch_prctl01 arch_prctl01
+
 bind01 bind01
 bind02 bind02
 bind03 bind03
diff --git a/testcases/kernel/syscalls/arch_prctl/.gitignore b/testcases/kernel/syscalls/arch_prctl/.gitignore
new file mode 100644
index 000000000..24871e249
--- /dev/null
+++ b/testcases/kernel/syscalls/arch_prctl/.gitignore
@@ -0,0 +1 @@ 
+/arch_prctl01
diff --git a/testcases/kernel/syscalls/arch_prctl/Makefile b/testcases/kernel/syscalls/arch_prctl/Makefile
new file mode 100644
index 000000000..272949d57
--- /dev/null
+++ b/testcases/kernel/syscalls/arch_prctl/Makefile
@@ -0,0 +1,8 @@ 
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) UnionTech Software Technology Co.,Ltd. 2024
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/arch_prctl/arch_prctl01.c b/testcases/kernel/syscalls/arch_prctl/arch_prctl01.c
new file mode 100644
index 000000000..5b69f7bf8
--- /dev/null
+++ b/testcases/kernel/syscalls/arch_prctl/arch_prctl01.c
@@ -0,0 +1,71 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) UnionTech Software Technology Co.,Ltd., 2024
+ * Author: Lu Fei <lufei@uniontech.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Simple test on arch_prctl to set and get cpuid instruction of test thread.
+ */
+
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "lapi/syscalls.h"
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_ASM_PRCTL_H
+#include <asm/prctl.h>
+
+static int arch_prctl_get(int code)
+{
+	return tst_syscall(__NR_arch_prctl, code, NULL);
+}
+
+static int arch_prctl_set(int code, unsigned long addr)
+{
+	return tst_syscall(__NR_arch_prctl, code, addr);
+}
+
+static void run(unsigned int index)
+{
+	FILE *fd;
+	char needed_flag[] = "cpuid_fault";
+	char *line = NULL;
+	size_t len = 0;
+	bool tag = 0;
+
+	fd = SAFE_FOPEN("/proc/cpuinfo", "r");
+
+	while (getline(&line, &len, fd) != -1) {
+		if (strncmp(line, "flag", strlen("flag")) == 0 &&
+				strstr(line, needed_flag) != NULL) {
+			tag = 1;
+			break;
+		}
+	}
+	if (!tag)
+		tst_brk(TCONF, "CPU need %s flag.", needed_flag);
+
+	// index == 0 for disable cpuid, 1 for enable cpuid.
+	TST_EXP_PASS(arch_prctl_set(ARCH_SET_CPUID, index));
+
+	TEST(arch_prctl_get(ARCH_GET_CPUID));
+
+	if (TST_RET == index)
+		tst_res(TPASS, "get cpuid succeed.");
+	else
+		tst_res(TFAIL, "get cpuid failed.");
+}
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = 2,
+	.min_kver = "4.12",
+	.supported_archs = (const char *const []){"x86_64", "x86", NULL}
+};
+
+#else /* HAVE_ASM_PRCTL_H */
+TST_TEST_TCONF("missing <asm/prctl.h>");
+#endif