Changes since V2: - V2: https://lore.kernel.org/lkml/cover.1647360971.git.reinette.chatre@intel.com/ - Rebased against v5.18-rc4, no functional changes. - Add text in cover letter and first patch to highlight that the __cpuid_count() macro provided is not a new implementation but copied from gcc.
Changes since V1: - V1: https://lore.kernel.org/lkml/cover.1644000145.git.reinette.chatre@intel.com/ - Change solution to not use __cpuid_count() from compiler's cpuid.h but instead use a local define of __cpuid_count() provided in kselftest.h to ensure tests continue working in all supported environments. (Shuah) - Rewrite cover letter and changelogs to reflect new solution.
A few tests that require running CPUID do so with a private implementation of a wrapper for CPUID. This duplication of the CPUID wrapper should be avoided.
Both gcc and clang/LLVM provide wrappers for CPUID but the wrappers are not available in the minimal required version of gcc, v3.2, that the selftests need to be used in. __cpuid_count() was added to gcc in v4.4, which is ok for kernels after v4.19 when the gcc minimal required version was changed to v4.6.
Copy gcc's __cpuid_count() to provide a local define of __cpuid_count() to kselftest.h to ensure that selftests can still work in environments with older stable kernels (v4.9 and v4.14 that have the minimal required version of gcc of v3.2). Update tests with private CPUID wrappers to use the new macro.
Cc: Dave Hansen dave.hansen@linux.intel.com Cc: Sandipan Das sandipan@linux.ibm.com Cc: Florian Weimer fweimer@redhat.com Cc: "Desnes A. Nunes do Rosario" desnesn@linux.vnet.ibm.com Cc: Ingo Molnar mingo@kernel.org Cc: Thiago Jung Bauermann bauerman@linux.ibm.com Cc: Michael Ellerman mpe@ellerman.id.au Cc: Michal Suchanek msuchanek@suse.de Cc: linux-mm@kvack.org Cc: Chang S. Bae chang.seok.bae@intel.com Cc: Borislav Petkov bp@suse.de Cc: Thomas Gleixner tglx@linutronix.de Cc: Ingo Molnar mingo@redhat.com Cc: "H. Peter Anvin" hpa@zytor.com Cc: x86@kernel.org Cc: Andy Lutomirski luto@kernel.org
Reinette Chatre (4): selftests: Provide local define of __cpuid_count() selftests/vm/pkeys: Use provided __cpuid_count() macro selftests/x86/amx: Use provided __cpuid_count() macro selftests/x86/corrupt_xstate_header: Use provided __cpuid_count() macro
tools/testing/selftests/kselftest.h | 15 ++++++++++++ tools/testing/selftests/vm/pkey-x86.h | 21 ++-------------- tools/testing/selftests/x86/amx.c | 24 ++++++------------- .../selftests/x86/corrupt_xstate_header.c | 16 ++----------- 4 files changed, 26 insertions(+), 50 deletions(-)
base-commit: af2d861d4cd2a4da5137f795ee3509e6f944a25b
Some selftests depend on information provided by the CPUID instruction. To support this dependency the selftests implement private wrappers for CPUID.
Duplication of the CPUID wrappers should be avoided.
Both gcc and clang/LLVM provide __cpuid_count() macros but neither the macro nor its header file are available in all the compiler versions that need to be supported by the selftests. __cpuid_count() as provided by gcc is available starting with gcc v4.4, so it is not available if the latest tests need to be run in all the environments required to support kernels v4.9 and v4.14 that have the minimal required gcc v3.2.
Duplicate gcc's __cpuid_count() macro to provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.
Suggested-by: Shuah Khan skhan@linuxfoundation.org Signed-off-by: Reinette Chatre reinette.chatre@intel.com --- Note to maintainers: - Macro is identical to the one provided by gcc, but not liked by checkpatch.pl with message "Macros with complex values should be enclosed in parentheses". Similar style is used in kernel, for example in arch/x86/kernel/fpu/xstate.h.
Changes since V2: - Highlight in changelog that macro is copied from gcc.
tools/testing/selftests/kselftest.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h index b8f248018174..33a0dbd26bd3 100644 --- a/tools/testing/selftests/kselftest.h +++ b/tools/testing/selftests/kselftest.h @@ -53,6 +53,21 @@ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) #endif
+/* + * 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 + * selftests need 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 + /* define kselftest exit codes */ #define KSFT_PASS 0 #define KSFT_FAIL 1
kselftest.h makes the __cpuid_count() macro available to conveniently call the CPUID instruction.
Remove the local CPUID wrapper and use __cpuid_count() from already included kselftest.h instead.
__cpuid_count() from kselftest.h is used instead of the macro provided by the compiler since gcc v4.4 (via cpuid.h) because the selftest needs to be compiled with gcc v3.2, the minimal required version for stable kernels.
Cc: Dave Hansen dave.hansen@linux.intel.com Cc: Sandipan Das sandipan@linux.ibm.com Cc: Florian Weimer fweimer@redhat.com Cc: "Desnes A. Nunes do Rosario" desnesn@linux.vnet.ibm.com Cc: Ingo Molnar mingo@kernel.org Cc: Thiago Jung Bauermann bauerman@linux.ibm.com Cc: Michael Ellerman mpe@ellerman.id.au Cc: Michal Suchanek msuchanek@suse.de Cc: linux-mm@kvack.org Signed-off-by: Reinette Chatre reinette.chatre@intel.com --- No changes since V2.
Changes since V1: - Update changelog - Remove Ram Pai from cc list (email address no longer valid) - No longer include cpuid.h but obtain __cpuid_count() from kselftest.h.
tools/testing/selftests/vm/pkey-x86.h | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-)
diff --git a/tools/testing/selftests/vm/pkey-x86.h b/tools/testing/selftests/vm/pkey-x86.h index e4a4ce2b826d..b078ce9c6d2a 100644 --- a/tools/testing/selftests/vm/pkey-x86.h +++ b/tools/testing/selftests/vm/pkey-x86.h @@ -80,19 +80,6 @@ static inline void __write_pkey_reg(u64 pkey_reg) assert(pkey_reg == __read_pkey_reg()); }
-static inline void __cpuid(unsigned int *eax, unsigned int *ebx, - unsigned int *ecx, unsigned int *edx) -{ - /* ecx is often an input as well as an output. */ - asm volatile( - "cpuid;" - : "=a" (*eax), - "=b" (*ebx), - "=c" (*ecx), - "=d" (*edx) - : "0" (*eax), "2" (*ecx)); -} - /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */ #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */ #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */ @@ -104,9 +91,7 @@ static inline int cpu_has_pkeys(void) unsigned int ecx; unsigned int edx;
- eax = 0x7; - ecx = 0x0; - __cpuid(&eax, &ebx, &ecx, &edx); + __cpuid_count(0x7, 0x0, eax, ebx, ecx, edx);
if (!(ecx & X86_FEATURE_PKU)) { dprintf2("cpu does not have PKU\n"); @@ -142,9 +127,7 @@ int pkey_reg_xstate_offset(void) /* assume that XSTATE_PKEY is set in XCR0 */ leaf = XSTATE_PKEY_BIT; { - eax = XSTATE_CPUID; - ecx = leaf; - __cpuid(&eax, &ebx, &ecx, &edx); + __cpuid_count(XSTATE_CPUID, leaf, eax, ebx, ecx, edx);
if (leaf == XSTATE_PKEY_BIT) { xstate_offset = ebx;
kselftest.h makes the __cpuid_count() macro available to conveniently call the CPUID instruction.
Remove the local CPUID wrapper and use __cpuid_count() from kselftest.h instead.
__cpuid_count() from kselftest.h is used instead of the macro provided by the compiler since gcc v4.4 (via cpuid.h) because the selftest needs to be supported with gcc v3.2, the minimal required version for stable kernels.
Cc: Chang S. Bae chang.seok.bae@intel.com Cc: Dave Hansen dave.hansen@linux.intel.com Cc: Borislav Petkov bp@suse.de Cc: Thomas Gleixner tglx@linutronix.de Cc: Ingo Molnar mingo@redhat.com Cc: "H. Peter Anvin" hpa@zytor.com Cc: x86@kernel.org Signed-off-by: Reinette Chatre reinette.chatre@intel.com --- No changes since V2.
Changes since V1: - Update changelog - No longer include cpuid.h but obtain __cpuid_count() from kselftest.h.
tools/testing/selftests/x86/amx.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
diff --git a/tools/testing/selftests/x86/amx.c b/tools/testing/selftests/x86/amx.c index 2189f0322d8b..625e42901237 100644 --- a/tools/testing/selftests/x86/amx.c +++ b/tools/testing/selftests/x86/amx.c @@ -17,6 +17,8 @@ #include <sys/syscall.h> #include <sys/wait.h>
+#include "../kselftest.h" /* For __cpuid_count() */ + #ifndef __x86_64__ # error This test is 64-bit only #endif @@ -45,13 +47,6 @@ static inline uint64_t xgetbv(uint32_t index) return eax + ((uint64_t)edx << 32); }
-static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) -{ - asm volatile("cpuid;" - : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) - : "0" (*eax), "2" (*ecx)); -} - static inline void xsave(struct xsave_buffer *xbuf, uint64_t rfbm) { uint32_t rfbm_lo = rfbm; @@ -115,9 +110,7 @@ static inline void check_cpuid_xsave(void) * support for the XSAVE feature set, including * XGETBV. */ - eax = 1; - ecx = 0; - cpuid(&eax, &ebx, &ecx, &edx); + __cpuid_count(1, 0, 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)) @@ -140,9 +133,8 @@ static void check_cpuid_xtiledata(void) { uint32_t eax, ebx, ecx, edx;
- eax = CPUID_LEAF_XSTATE; - ecx = CPUID_SUBLEAF_XSTATE_USER; - cpuid(&eax, &ebx, &ecx, &edx); + __cpuid_count(CPUID_LEAF_XSTATE, CPUID_SUBLEAF_XSTATE_USER, + eax, ebx, ecx, edx);
/* * EBX enumerates the size (in bytes) required by the XSAVE @@ -153,10 +145,8 @@ static void check_cpuid_xtiledata(void) */ xbuf_size = ebx;
- eax = CPUID_LEAF_XSTATE; - ecx = XFEATURE_XTILEDATA; - - cpuid(&eax, &ebx, &ecx, &edx); + __cpuid_count(CPUID_LEAF_XSTATE, XFEATURE_XTILEDATA, + eax, ebx, ecx, edx); /* * eax: XTILEDATA state component size * ebx: XTILEDATA state component offset in user buffer
kselftest.h makes the __cpuid_count() macro available to conveniently call the CPUID instruction.
Remove the local CPUID wrapper and use __cpuid_count() from kselftest.h instead.
__cpuid_count() from kselftest.h is used instead of the macro provided by the compiler since gcc v4.4 (via cpuid.h) because the selftest needs to be supported with gcc v3.2, the minimal required version for stable kernels.
Cc: Andy Lutomirski luto@kernel.org Cc: Thomas Gleixner tglx@linutronix.de Cc: Ingo Molnar mingo@redhat.com Cc: Borislav Petkov bp@suse.de Cc: Dave Hansen dave.hansen@linux.intel.com Cc: "H. Peter Anvin" hpa@zytor.com Cc: x86@kernel.org Signed-off-by: Reinette Chatre reinette.chatre@intel.com --- No changes since V2.
Changes since V1: - Update changelog - No longer include cpuid.h but obtain __cpuid_count() from kselftest.h.
.../selftests/x86/corrupt_xstate_header.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
diff --git a/tools/testing/selftests/x86/corrupt_xstate_header.c b/tools/testing/selftests/x86/corrupt_xstate_header.c index ab8599c10ce5..cf9ce8fbb656 100644 --- a/tools/testing/selftests/x86/corrupt_xstate_header.c +++ b/tools/testing/selftests/x86/corrupt_xstate_header.c @@ -17,25 +17,13 @@ #include <stdint.h> #include <sys/wait.h>
-static inline void __cpuid(unsigned int *eax, unsigned int *ebx, - unsigned int *ecx, unsigned int *edx) -{ - asm volatile( - "cpuid;" - : "=a" (*eax), - "=b" (*ebx), - "=c" (*ecx), - "=d" (*edx) - : "0" (*eax), "2" (*ecx)); -} +#include "../kselftest.h" /* For __cpuid_count() */
static inline int xsave_enabled(void) { unsigned int eax, ebx, ecx, edx;
- eax = 0x1; - ecx = 0x0; - __cpuid(&eax, &ebx, &ecx, &edx); + __cpuid_count(0x1, 0x0, eax, ebx, ecx, edx);
/* Is CR4.OSXSAVE enabled ? */ return ecx & (1U << 27);
On 4/25/22 3:01 PM, Reinette Chatre wrote:
Changes since V2:
- V2: https://lore.kernel.org/lkml/cover.1647360971.git.reinette.chatre@intel.com/
- Rebased against v5.18-rc4, no functional changes.
- Add text in cover letter and first patch to highlight that the __cpuid_count() macro provided is not a new implementation but copied from gcc.
Changes since V1:
- V1: https://lore.kernel.org/lkml/cover.1644000145.git.reinette.chatre@intel.com/
- Change solution to not use __cpuid_count() from compiler's cpuid.h but instead use a local define of __cpuid_count() provided in kselftest.h to ensure tests continue working in all supported environments. (Shuah)
- Rewrite cover letter and changelogs to reflect new solution.
A few tests that require running CPUID do so with a private implementation of a wrapper for CPUID. This duplication of the CPUID wrapper should be avoided.
Both gcc and clang/LLVM provide wrappers for CPUID but the wrappers are not available in the minimal required version of gcc, v3.2, that the selftests need to be used in. __cpuid_count() was added to gcc in v4.4, which is ok for kernels after v4.19 when the gcc minimal required version was changed to v4.6.
Copy gcc's __cpuid_count() to provide a local define of __cpuid_count() to kselftest.h to ensure that selftests can still work in environments with older stable kernels (v4.9 and v4.14 that have the minimal required version of gcc of v3.2). Update tests with private CPUID wrappers to use the new macro.
Cc: Dave Hansen dave.hansen@linux.intel.com Cc: Sandipan Das sandipan@linux.ibm.com Cc: Florian Weimer fweimer@redhat.com Cc: "Desnes A. Nunes do Rosario" desnesn@linux.vnet.ibm.com Cc: Ingo Molnar mingo@kernel.org Cc: Thiago Jung Bauermann bauerman@linux.ibm.com Cc: Michael Ellerman mpe@ellerman.id.au Cc: Michal Suchanek msuchanek@suse.de Cc: linux-mm@kvack.org Cc: Chang S. Bae chang.seok.bae@intel.com Cc: Borislav Petkov bp@suse.de Cc: Thomas Gleixner tglx@linutronix.de Cc: Ingo Molnar mingo@redhat.com Cc: "H. Peter Anvin" hpa@zytor.com Cc: x86@kernel.org Cc: Andy Lutomirski luto@kernel.org
Reinette Chatre (4): selftests: Provide local define of __cpuid_count() selftests/vm/pkeys: Use provided __cpuid_count() macro selftests/x86/amx: Use provided __cpuid_count() macro selftests/x86/corrupt_xstate_header: Use provided __cpuid_count() macro
tools/testing/selftests/kselftest.h | 15 ++++++++++++ tools/testing/selftests/vm/pkey-x86.h | 21 ++-------------- tools/testing/selftests/x86/amx.c | 24 ++++++------------- .../selftests/x86/corrupt_xstate_header.c | 16 ++----------- 4 files changed, 26 insertions(+), 50 deletions(-)
base-commit: af2d861d4cd2a4da5137f795ee3509e6f944a25b
Thank you. I will queue this up for Linux 5.19-rc1
thanks, -- Shuah
linux-kselftest-mirror@lists.linaro.org