public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v3 0/2] Fix ptrace07 hardcoded xstate size issue
@ 2022-10-18 12:10 Pengfei Xu
  2022-10-18 12:10 ` [LTP] [PATCH v3 1/2] cpuid.h: Provide the macro definition __cpuid_count() Pengfei Xu
  2022-10-18 12:10 ` [LTP] [PATCH v3 2/2] ptrace07: fix the broken case caused by hardcoded xstate size Pengfei Xu
  0 siblings, 2 replies; 3+ messages in thread
From: Pengfei Xu @ 2022-10-18 12:10 UTC (permalink / raw)
  To: ltp; +Cc: chang.seok.bae, eric.devolder, Heng Su

Should not use a hardcoded xstate size(512 * 8 = 4096 bytes), and use maximum
XSAVE size specified by CPUID.(EAX=0DH, ECX=0H):EBX instead.
If the CPU's maximum XSAVE size exceeds the hard-coded xstate size 4096 bytes,
it will cause the ptrace07 case to break as below:
"
./ptrace07
tst_test.c:1528: TINFO: Timeout per run is 0h 00m 30s
ptrace07.c:142: TBROK: PTRACE_SETREGSET failed with unexpected error: EFAULT (14)
tst_test.c:1571: TINFO: Killed the leftover descendant processes

Summary:
passed   0
failed   0
broken   1
skipped  0
warnings 0
"

And there is no proper cpuid() function in LTP, so provides the cpuid()
function first in previous commit.

=========
v2 -> v3:
 - Thanks Richard Palethorpe's report, should not use old API cpuid function
   in LTP, and provide the __cpuid_count() function instead
 - Thanks Richard Palethorpe's suggestion, put __cpuid_count() in
   include/lapi/cpuid.h
 - Add the aligned_alloc() successfully check in ptrace07.c
 - Add the cpuid output check prints as Richard Palethorpe's suggestion


Pengfei Xu (2):
  cpuid.h: Provide the macro definition __cpuid_count()
  ptrace07: fix the broken case caused by hardcoded xstate size

 include/lapi/cpuid.h                        | 24 +++++++++++++++++
 testcases/kernel/syscalls/ptrace/ptrace07.c | 29 ++++++++++++++++++---
 2 files changed, 49 insertions(+), 4 deletions(-)
 create mode 100644 include/lapi/cpuid.h

-- 
2.31.1


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

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

* [LTP] [PATCH v3 1/2] cpuid.h: Provide the macro definition __cpuid_count()
  2022-10-18 12:10 [LTP] [PATCH v3 0/2] Fix ptrace07 hardcoded xstate size issue Pengfei Xu
@ 2022-10-18 12:10 ` Pengfei Xu
  2022-10-18 12:10 ` [LTP] [PATCH v3 2/2] ptrace07: fix the broken case caused by hardcoded xstate size Pengfei Xu
  1 sibling, 0 replies; 3+ messages in thread
From: Pengfei Xu @ 2022-10-18 12:10 UTC (permalink / raw)
  To: ltp; +Cc: chang.seok.bae, eric.devolder, Heng Su

Some ltp tests rely on information provided by the cpuid function.
The old cpuid function in the ltp/include/old/ltp_cpuid.h does not work
correctly.

Provide the macro definition __cpuid_count() in ltp.
It references below link:
https://github.com/gcc-mirror/gcc/blob/master/gcc/config/i386/cpuid.h#L233

Makes a little improvement to eliminate the "ERROR: Macros with complex values
should be enclosed in parentheses" warning from kernel/scripts/checkpatch.pl.

Reported-by: Richard Palethorpe <rpalethorpe@suse.de>
Signed-off-by: Pengfei Xu <pengfei.xu@intel.com>
---
 include/lapi/cpuid.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 include/lapi/cpuid.h

diff --git a/include/lapi/cpuid.h b/include/lapi/cpuid.h
new file mode 100644
index 000000000..c58203509
--- /dev/null
+++ b/include/lapi/cpuid.h
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <cpuid.h>
+
+#ifndef LAPI_CPUID_H__
+#define LAPI_CPUID_H__
+
+/*
+ * gcc cpuid.h provides __cpuid_count() since v4.4.
+ * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
+ *
+ * Provide local define for tests needing __cpuid_count() because
+ * ltp needs to work in older environments that do not yet
+ * have __cpuid_count().
+ */
+#ifndef __cpuid_count
+#define __cpuid_count(level, count, a, b, c, d) ({			\
+	__asm__ __volatile__ ("cpuid\n\t"				\
+			      : "=a" (a), "=b" (b), "=c" (c), "=d" (d)	\
+			      : "0" (level), "2" (count));		\
+})
+#endif
+
+#endif /* LAPI_CPUID_H__ */
-- 
2.31.1


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

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

* [LTP] [PATCH v3 2/2] ptrace07: fix the broken case caused by hardcoded xstate size
  2022-10-18 12:10 [LTP] [PATCH v3 0/2] Fix ptrace07 hardcoded xstate size issue Pengfei Xu
  2022-10-18 12:10 ` [LTP] [PATCH v3 1/2] cpuid.h: Provide the macro definition __cpuid_count() Pengfei Xu
@ 2022-10-18 12:10 ` Pengfei Xu
  1 sibling, 0 replies; 3+ messages in thread
From: Pengfei Xu @ 2022-10-18 12:10 UTC (permalink / raw)
  To: ltp; +Cc: chang.seok.bae, eric.devolder, Heng Su

Should not use a hardcoded xstate size(512 * 8 = 4096 bytes), and use maximum
XSAVE size specified by CPUID.(EAX=0DH, ECX=0H):EBX instead.
If the CPU's maximum XSAVE size exceeds the hard-coded xstate size 4096 bytes,
it will cause the ptrace07 case to break as below:
"
./ptrace07
tst_test.c:1528: TINFO: Timeout per run is 0h 00m 30s
ptrace07.c:142: TBROK: PTRACE_SETREGSET failed with unexpected error: EFAULT (14)
tst_test.c:1571: TINFO: Killed the leftover descendant processes

Summary:
passed   0
failed   0
broken   1
skipped  0
warnings 0
"

Reported-by: Eric DeVolder <eric.devolder@oracle.com>
Reviewed-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Pengfei Xu <pengfei.xu@intel.com>
---
 testcases/kernel/syscalls/ptrace/ptrace07.c | 29 ++++++++++++++++++---
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/testcases/kernel/syscalls/ptrace/ptrace07.c b/testcases/kernel/syscalls/ptrace/ptrace07.c
index da62cadb0..c2c2ad77c 100644
--- a/testcases/kernel/syscalls/ptrace/ptrace07.c
+++ b/testcases/kernel/syscalls/ptrace/ptrace07.c
@@ -35,6 +35,7 @@
 #include "config.h"
 #include "ptrace.h"
 #include "tst_test.h"
+#include "cpuid.h"
 
 #ifndef PTRACE_GETREGSET
 # define PTRACE_GETREGSET 0x4204
@@ -48,6 +49,8 @@
 # define NT_X86_XSTATE 0x202
 #endif
 
+#define CPUID_LEAF_XSTATE 0xd
+
 static void check_regs_loop(uint32_t initval)
 {
 	const unsigned long num_iters = 1000000000;
@@ -83,11 +86,22 @@ static void do_test(void)
 	int i;
 	int num_cpus = tst_ncpus();
 	pid_t pid;
-	uint64_t xstate[512];
-	struct iovec iov = { .iov_base = xstate, .iov_len = sizeof(xstate) };
+	uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
+	uint64_t *xstate;
+	/*
+	 * CPUID.(EAX=0DH, ECX=0H):EBX: maximum size (bytes, from the beginning
+	 * of the XSAVE/XRSTOR save area) required by enabled features in XCR0.
+	 */
+	__cpuid_count(CPUID_LEAF_XSTATE, ecx, eax, ebx, ecx, edx);
+	xstate = aligned_alloc(64, ebx);
+	struct iovec iov = { .iov_base = xstate, .iov_len = ebx };
 	int status;
 	bool okay;
 
+	if (!xstate)
+		tst_brk(TBROK, "aligned_alloc() failed for xstate buffer");
+	tst_res(TINFO, "CPUID.(EAX=%u, ECX=0):EAX=%u, EBX=%u, ECX=%u, EDX=%u",
+		CPUID_LEAF_XSTATE, eax, ebx, ecx, edx);
 	pid = SAFE_FORK();
 	if (pid == 0) {
 		TST_CHECKPOINT_WAKE(0);
@@ -102,12 +116,15 @@ static void do_test(void)
 	sched_yield();
 
 	TEST(ptrace(PTRACE_ATTACH, pid, 0, 0));
-	if (TST_RET != 0)
+	if (TST_RET != 0) {
+		free(xstate);
 		tst_brk(TBROK | TTERRNO, "PTRACE_ATTACH failed");
+	}
 
 	SAFE_WAITPID(pid, NULL, 0);
 	TEST(ptrace(PTRACE_GETREGSET, pid, NT_X86_XSTATE, &iov));
 	if (TST_RET != 0) {
+		free(xstate);
 		if (TST_ERR == EIO)
 			tst_brk(TCONF, "GETREGSET/SETREGSET is unsupported");
 
@@ -138,6 +155,7 @@ static void do_test(void)
 		tst_res(TINFO,
 			"PTRACE_SETREGSET with reserved bits failed with EINVAL");
 	} else {
+		free(xstate);
 		tst_brk(TBROK | TTERRNO,
 			"PTRACE_SETREGSET failed with unexpected error");
 	}
@@ -152,8 +170,10 @@ static void do_test(void)
 	 * worry about potential stops after this point.
 	 */
 	TEST(ptrace(PTRACE_DETACH, pid, 0, 0));
-	if (TST_RET != 0)
+	if (TST_RET != 0) {
+		free(xstate);
 		tst_brk(TBROK | TTERRNO, "PTRACE_DETACH failed");
+	}
 
 	/* If child 'pid' crashes, only report it as info. */
 	SAFE_WAITPID(pid, &status, 0);
@@ -173,6 +193,7 @@ static void do_test(void)
 	}
 	if (okay)
 		tst_res(TPASS, "wasn't able to set invalid FPU state");
+	free(xstate);
 }
 
 static struct tst_test test = {
-- 
2.31.1


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

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

end of thread, other threads:[~2022-10-18 12:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-18 12:10 [LTP] [PATCH v3 0/2] Fix ptrace07 hardcoded xstate size issue Pengfei Xu
2022-10-18 12:10 ` [LTP] [PATCH v3 1/2] cpuid.h: Provide the macro definition __cpuid_count() Pengfei Xu
2022-10-18 12:10 ` [LTP] [PATCH v3 2/2] ptrace07: fix the broken case caused by hardcoded xstate size Pengfei Xu

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