Skip to contentSkip to conversation

selftests/x86: AMX-related test improvements

PATCH Superseded Chang S. Bae 2 participants 8 messages Apr 1, 2022 – Jun 17, 2022

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover selftests/x86: AMX-related test improvements

A couple of test updates are included:

* With this option [1,2], the kernel's altstack check becomes stringent. The x86 sigaltstack test is ignorant about this. Adjust the test now. This check was established [3] to ensure every AMX task's altstack is sufficient (regardless of that option) [4].

* The AMX test wrongly fails on non-AMX machines. Fix the code to skip the test instead.

The series is available in this repository:
git://github.com/intel/amx-linux.git selftest

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/Kconfig#n2432
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/kernel-parameters.txt#n5676
[3] 3aac3ebea08f ("x86/signal: Implement sigaltstack size validation")
[4] 4b7ca609a33d ("x86/signal: Use fpu::__state_user_size for sigalt stack validation")

Chang S. Bae (2):
selftests/x86/signal: Adjust the test to the kernel's altstack check selftests/x86/amx: Fix the test to avoid failure when AMX is
unavailable

 tools/testing/selftests/x86/amx.c         | 42 +++++++++++++++++------
 tools/testing/selftests/x86/sigaltstack.c | 12 ++++++-
 2 files changed, 42 insertions(+), 12 deletions(-)

base-commit: f443e374ae131c168a065ea1748feac6b2e76613

Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 selftests/x86/signal: Adjust the test to the kernel's altstack check

The test assumes an insufficient altstack is allowed. Then it raises a signal to test the delivery failure due to an altstack overflow.

The kernel now provides an option to tweak sigaltstack()'s sanity check to prevent an insufficient altstack. ENOMEM is returned on the check failure.

Adjust the code to skip the test when this option is on.

---
 tools/testing/selftests/x86/sigaltstack.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/sigaltstack.c +11 -1
index f689af75e979..22a88b764a8e 100644
@@ -88,8 +88,18 @@ static void sigalrm(int sig, siginfo_t *info, void *ctx_void)
 
 static void test_sigaltstack(void *altstack, unsigned long size)
 {
-	if (setup_altstack(altstack, size))
+	if (setup_altstack(altstack, size)) {
+		/*
+		 * The kernel may return ENOMEM when the altstack size
+		 * is insufficient. Skip the test in this case.
+		 */
+		if (errno == ENOMEM && size < at_minstack_size) {
+			printf("[SKIP]\tThe running kernel disallows an insufficient size.\n");
+			return;
+		}
+
 		err(1, "sigaltstack()");
+	}
 
 	sigalrm_expected = (size > at_minstack_size) ? true : false;
 
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] selftests/x86/signal: Adjust the test to the kernel's altstack check

Quoted 7 lines · Chang S. Bae

The test assumes an insufficient altstack is allowed. Then it raises a signal to test the delivery failure due to an altstack overflow.

The kernel now provides an option to tweak sigaltstack()'s sanity check to prevent an insufficient altstack. ENOMEM is returned on the check failure.

Adjust the code to skip the test when this option is on.

Mention the option name here and in the Skip message.

Quoted 14 lines
---
  tools/testing/selftests/x86/sigaltstack.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/sigaltstack.c +0 -0
index f689af75e979..22a88b764a8e 100644
@@ -88,8 +88,18 @@ static void sigalrm(int sig, siginfo_t *info, void *ctx_void)
 

tools/testing/selftests/x86/sigaltstack.c static void sigalrm(int sig, siginfo_t *info, void *ctx_void) · line 97

Quoted 10 lines
tools/testing/selftests/x86/sigaltstack.c +7 -1
  static void test_sigaltstack(void *altstack, unsigned long size)
  {
-	if (setup_altstack(altstack, size))
+	if (setup_altstack(altstack, size)) {
+		/*
+		 * The kernel may return ENOMEM when the altstack size
+		 * is insufficient. Skip the test in this case.
+		 */
+		if (errno == ENOMEM && size < at_minstack_size) {
+			printf("[SKIP]\tThe running kernel disallows an insufficient size.\n");

Please improve this message to clearly why it is okay to skip the test. Mention that the option to disallowing insufficient is enabled and that the test can't be run.

tools/testing/selftests/x86/sigaltstack.c static void sigalrm(int sig, siginfo_t *info, void *ctx_void) · line 105

Quoted 9 lines
tools/testing/selftests/x86/sigaltstack.c +4 -0
+			return;
+		}
+
  		err(1, "sigaltstack()");
+	}
 
  	sigalrm_expected = (size > at_minstack_size) ? true : false;
 

With these changes:

thanks,
-- Shuah

#3 lore raw

PATCH 2/2 selftests/x86/amx: Fix the test to avoid failure when AMX is unavailable

In reply to Chang S. Bae

When a CPU does not have AMX, the test fails. But this is wrong as it should be runnable regardless. Skip the test instead.

Fixes 6a3e0651b4a ("selftests/x86/amx: Add test cases for AMX state management")
---
 tools/testing/selftests/x86/amx.c | 42 +++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 11 deletions(-)
tools/testing/selftests/x86/amx.c +31 -11
index 3615ef4a48bb..14abb6072a7d 100644
@@ -106,6 +106,12 @@ static void clearhandler(int sig)
 
 #define CPUID_LEAF1_ECX_XSAVE_MASK	(1 << 26)
 #define CPUID_LEAF1_ECX_OSXSAVE_MASK	(1 << 27)
+
+static struct {
+	unsigned xsave:   1;
+	unsigned osxsave: 1;
+} cpuinfo;
+
 static inline void check_cpuid_xsave(void)
 {
 	uint32_t eax, ebx, ecx, edx;
@@ -118,10 +124,8 @@ static inline void check_cpuid_xsave(void)
 	eax = 1;
 	ecx = 0;
 	cpuid(&eax, &ebx, &ecx, &edx);
-	if (!(ecx & CPUID_LEAF1_ECX_XSAVE_MASK))
-		fatal_error("cpuid: no CPU xsave support");
-	if (!(ecx & CPUID_LEAF1_ECX_OSXSAVE_MASK))
-		fatal_error("cpuid: no OS xsave support");
+	cpuinfo.xsave = !!(ecx & CPUID_LEAF1_ECX_XSAVE_MASK);
+	cpuinfo.osxsave = !!(ecx & CPUID_LEAF1_ECX_OSXSAVE_MASK);
 }
 
 static uint32_t xbuf_size;
@@ -161,14 +165,31 @@ static void check_cpuid_xtiledata(void)
 	 * eax: XTILEDATA state component size
 	 * ebx: XTILEDATA state component offset in user buffer
 	 */
-	if (!eax || !ebx)
-		fatal_error("xstate cpuid: invalid tile data size/offset: %d/%d",
-				eax, ebx);
-
 	xtiledata.size	      = eax;
 	xtiledata.xbuf_offset = ebx;
 }
 
+static bool amx_available(void)
+{
+	check_cpuid_xsave();
+	if (!cpuinfo.xsave) {
+		printf("[SKIP]\tcpuid: no CPU xsave support\n");
+		return false;
+	} else if (!cpuinfo.osxsave) {
+		printf("[SKIP]\tcpuid: no OS xsave support\n");
+		return false;
+	}
+
+	check_cpuid_xtiledata();
+	if (!xtiledata.size || !xtiledata.xbuf_offset) {
+		printf("[SKIP]\txstate cpuid: no tile data (size/offset: %d/%d)\n",
+		       xtiledata.size, xtiledata.xbuf_offset);
+		return false;
+	}
+
+	return true;
+}
+
 /* The helpers for managing XSAVE buffer and tile states: */
 
 struct xsave_buffer *alloc_xbuf(void)
@@ -826,9 +847,8 @@ static void test_context_switch(void)
 
 int main(void)
 {
-	/* Check hardware availability at first */
-	check_cpuid_xsave();
-	check_cpuid_xtiledata();
+	if (!amx_available())
+		return 0;
 
 	init_stashed_xsave();
 	sethandler(SIGILL, handle_noperm, 0);
Signature · 1 line
2.17.1
maintainer
#4 lore raw

Re: [PATCH 2/2] selftests/x86/amx: Fix the test to avoid failure when AMX is unavailable

Quoted 17 lines · Chang S. Bae

When a CPU does not have AMX, the test fails. But this is wrong as it should be runnable regardless. Skip the test instead.

Fixes 6a3e0651b4a ("selftests/x86/amx: Add test cases for AMX state management")
---
  tools/testing/selftests/x86/amx.c | 42 +++++++++++++++++++++++--------
  1 file changed, 31 insertions(+), 11 deletions(-)
tools/testing/selftests/x86/amx.c +0 -0
index 3615ef4a48bb..14abb6072a7d 100644

tools/testing/selftests/x86/amx.c static void clearhandler(int sig) · line 114

Quoted 9 lines
tools/testing/selftests/x86/amx.c +6 -0
@@ -106,6 +106,12 @@ static void clearhandler(int sig)
 
  #define CPUID_LEAF1_ECX_XSAVE_MASK	(1 << 26)
  #define CPUID_LEAF1_ECX_OSXSAVE_MASK	(1 << 27)
+
+static struct {
+	unsigned xsave:   1;
+	unsigned osxsave: 1;
+} cpuinfo;
+

Why is this needed? Also naming this cpuinfo is confuing.

tools/testing/selftests/x86/amx.c static inline void check_cpuid_xsave(void) · line 128

Quoted 13 lines
tools/testing/selftests/x86/amx.c +2 -4
  static inline void check_cpuid_xsave(void)
  {
  	uint32_t eax, ebx, ecx, edx;
@@ -118,10 +124,8 @@ static inline void check_cpuid_xsave(void)
  	eax = 1;
  	ecx = 0;
  	cpuid(&eax, &ebx, &ecx, &edx);
-	if (!(ecx & CPUID_LEAF1_ECX_XSAVE_MASK))
-		fatal_error("cpuid: no CPU xsave support");
-	if (!(ecx & CPUID_LEAF1_ECX_OSXSAVE_MASK))
-		fatal_error("cpuid: no OS xsave support");
+	cpuinfo.xsave = !!(ecx & CPUID_LEAF1_ECX_XSAVE_MASK);
+	cpuinfo.osxsave = !!(ecx & CPUID_LEAF1_ECX_OSXSAVE_MASK);

Why add this complexity. Why not just Skip here?

Quoted 26 lines
tools/testing/selftests/x86/amx.c +11 -4
  }
 
  static uint32_t xbuf_size;
@@ -161,14 +165,31 @@ static void check_cpuid_xtiledata(void)
  	 * eax: XTILEDATA state component size
  	 * ebx: XTILEDATA state component offset in user buffer
  	 */
-	if (!eax || !ebx)
-		fatal_error("xstate cpuid: invalid tile data size/offset: %d/%d",
-				eax, ebx);
-
  	xtiledata.size	      = eax;
  	xtiledata.xbuf_offset = ebx;
  }
 
+static bool amx_available(void)
+{
+	check_cpuid_xsave();
+	if (!cpuinfo.xsave) {
+		printf("[SKIP]\tcpuid: no CPU xsave support\n");
+		return false;
+	} else if (!cpuinfo.osxsave) {
+		printf("[SKIP]\tcpuid: no OS xsave support\n");
+		return false;
+	}
+

tools/testing/selftests/x86/amx.c static void check_cpuid_xtiledata(void) · line 192

Quoted 10 lines
tools/testing/selftests/x86/amx.c +10 -0
+	check_cpuid_xtiledata();
+	if (!xtiledata.size || !xtiledata.xbuf_offset) {
+		printf("[SKIP]\txstate cpuid: no tile data (size/offset: %d/%d)\n",
+		       xtiledata.size, xtiledata.xbuf_offset);
+		return false;
+	}
+
+	return true;
+}
+

I am not seeing any value in adding this layer of abstraction. Keep it simple and do the handling in main()

tools/testing/selftests/x86/amx.c static void test_context_switch(void) · line 851

Quoted 12 lines
tools/testing/selftests/x86/amx.c +2 -3
  /* The helpers for managing XSAVE buffer and tile states: */
 
  struct xsave_buffer *alloc_xbuf(void)
@@ -826,9 +847,8 @@ static void test_context_switch(void)
 
  int main(void)
  {
-	/* Check hardware availability at first */
-	check_cpuid_xsave();
-	check_cpuid_xtiledata();
+	if (!amx_available())
+		return 0;

This should KSFT_SKIP for this to be reported as a skip. Returning 0 will be reported as a Pass.

tools/testing/selftests/x86/amx.c static void test_context_switch(void)

Quoted 4 lines
tools/testing/selftests/x86/amx.c +0 -0
  	init_stashed_xsave();
  	sethandler(SIGILL, handle_noperm, 0);
 

thanks,
-- Shuah

#5 lore raw

Re: [PATCH 2/2] selftests/x86/amx: Fix the test to avoid failure when AMX is unavailable

Quoted 10 lines · Shuah Khan

Review

Quoted 7 lines · Chang S. Bae
(quoted diff) +6 -0
+
+static struct {
+    unsigned xsave:   1;
+    unsigned osxsave: 1;
+} cpuinfo;
+

Why is this needed? Also naming this cpuinfo is confuing.

This came from the below CPUID check which seems to be moot.

Quoted 16 lines
Quoted 5 lines

  static inline void check_cpuid_xsave(void)
  {
      uint32_t eax, ebx, ecx, edx;

Review static inline void check_cpuid_xsave(void) · line 128

Quoted 9 lines
(quoted diff) +2 -4
@@ -118,10 +124,8 @@ static inline void check_cpuid_xsave(void)
       eax = 1;
       ecx = 0;
       cpuid(&eax, &ebx, &ecx, &edx);
-    if (!(ecx & CPUID_LEAF1_ECX_XSAVE_MASK))
-        fatal_error("cpuid: no CPU xsave support");
-    if (!(ecx & CPUID_LEAF1_ECX_OSXSAVE_MASK))
-        fatal_error("cpuid: no OS xsave support");
+    cpuinfo.xsave = !!(ecx & CPUID_LEAF1_ECX_XSAVE_MASK);
+    cpuinfo.osxsave = !!(ecx & CPUID_LEAF1_ECX_OSXSAVE_MASK);

Why add this complexity. Why not just Skip here?

I think these CPUID checks can go away with ARCH_GET_XCOMP_SUPP.

Quoted 32 lines
Quoted 32 lines
(quoted diff) +16 -4
   }
   static uint32_t xbuf_size;
@@ -161,14 +165,31 @@ static void check_cpuid_xtiledata(void)
        * eax: XTILEDATA state component size
        * ebx: XTILEDATA state component offset in user buffer
        */
-    if (!eax || !ebx)
-        fatal_error("xstate cpuid: invalid tile data size/offset: 
 %d/%d",
-                eax, ebx);
-
       xtiledata.size          = eax;
       xtiledata.xbuf_offset = ebx;
   }
+static bool amx_available(void)
+{
+    check_cpuid_xsave();
+    if (!cpuinfo.xsave) {
+        printf("[SKIP]\tcpuid: no CPU xsave support\n");
+        return false;
+    } else if (!cpuinfo.osxsave) {
+        printf("[SKIP]\tcpuid: no OS xsave support\n");
+        return false;
+    }
+
+    check_cpuid_xtiledata();
+    if (!xtiledata.size || !xtiledata.xbuf_offset) {
+        printf("[SKIP]\txstate cpuid: no tile data (size/offset: 
 %d/%d)\n",
+               xtiledata.size, xtiledata.xbuf_offset);
+        return false;
Quoted 8 lines

Review static void check_cpuid_xtiledata(void) · line 193

Quoted 5 lines
(quoted diff) +5 -0
+    }
+
+    return true;
+}
+

I am not seeing any value in adding this layer of abstraction. Keep it simple and do the handling in main()

Sure.

Quoted 14 lines

Review static void test_context_switch(void) · line 850

Quoted 11 lines
(quoted diff) +2 -3
   /* The helpers for managing XSAVE buffer and tile states: */
   struct xsave_buffer *alloc_xbuf(void)
@@ -826,9 +847,8 @@ static void test_context_switch(void)
   int main(void)
   {
-    /* Check hardware availability at first */
-    check_cpuid_xsave();
-    check_cpuid_xtiledata();
+    if (!amx_available())
+        return 0;

This should KSFT_SKIP for this to be reported as a skip. Returning 0 will be reported as a Pass.

I think that's a good point, thanks.

Now, along with the on-going documentation [1], this test code can be simplified like the below changes, instead of having those cpuid functions:

tools/testing/selftests/x86/amx.c +9 -3
index 625e42901237..83705c472a5c 100644
@@ -348,6 +348,7 @@ enum expected_result { FAIL_EXPECTED, SUCCESS_EXPECTED };
 
 /* arch_prctl() and sigaltstack() test */
 
+#define ARCH_GET_XCOMP_SUPP    0x1021
 #define ARCH_GET_XCOMP_PERM    0x1022
 #define ARCH_REQ_XCOMP_PERM    0x1023
 
@@ -828,9 +829,14 @@ static void test_context_switch(void)
 
 int main(void)
 {
-       /* Check hardware availability at first */
-       check_cpuid_xsave();
-       check_cpuid_xtiledata();
+       unsigned long features;
+       long rc;
+
+       rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_SUPP, &features);
+       if (rc || (features & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE) {
+               printf("[SKIP]\tno AMX support\n");
+               exit(KSFT_FAIL);
+       }
 
        init_stashed_xsave();
        sethandler(SIGILL, handle_noperm, 0);

Thanks,
Chang

[1] https://lore.kernel.org/lkml/[email protected]/

maintainer
#6 lore raw

Re: [PATCH 2/2] selftests/x86/amx: Fix the test to avoid failure when AMX is unavailable

Quoted 5 lines · Chang S. Bae
Quoted 4 lines · Shuah Khan
Quoted 1 line · Chang S. Bae
Quoted 7 lines
Quoted 2 lines

This should KSFT_SKIP for this to be reported as a skip. Returning 0 will be reported as a Pass.

I think that's a good point, thanks.

Now, along with the on-going documentation [1], this test code can be simplified like the below changes, instead of having those cpuid functions:

Simplifying is always good. Send me v2 and I will review it.

thanks,
-- Shuah

maintainer
#7 lore raw
In reply to Chang S. Bae
Quoted 23 lines · Chang S. Bae

A couple of test updates are included:

* With this option [1,2], the kernel's altstack check becomes stringent. The x86 sigaltstack test is ignorant about this. Adjust the test now. This check was established [3] to ensure every AMX task's altstack is sufficient (regardless of that option) [4].

* The AMX test wrongly fails on non-AMX machines. Fix the code to skip the test instead.

The series is available in this repository:
git://github.com/intel/amx-linux.git selftest

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/Kconfig#n2432
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/kernel-parameters.txt#n5676
[3] 3aac3ebea08f ("x86/signal: Implement sigaltstack size validation")
[4] 4b7ca609a33d ("x86/signal: Use fpu::__state_user_size for sigalt stack validation")

Chang S. Bae (2):
selftests/x86/signal: Adjust the test to the kernel's altstack check selftests/x86/amx: Fix the test to avoid failure when AMX is
unavailable

Quoted 7 lines
  tools/testing/selftests/x86/amx.c         | 42 +++++++++++++++++------
  tools/testing/selftests/x86/sigaltstack.c | 12 ++++++-
  2 files changed, 42 insertions(+), 12 deletions(-)

base-commit: f443e374ae131c168a065ea1748feac6b2e76613

These are fixes and I can take them through kselftest once the review comments are addressed and get an ack from x86 maintainers.

thanks,
-- Shuah