Skip to contentSkip to conversation

selftests: Remove duplicate CPUID wrappers

PATCH Superseded Reinette Chatre 2 participants 10 messages Mar 15, 2022 – Apr 20, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover selftests: Remove duplicate CPUID wrappers

Changes since V1:
- V1: https://lore.kernel.org/lkml/[email protected]/
- 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.

Add 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 <[email protected]>
Cc Sandipan Das <[email protected]>
Cc Florian Weimer <[email protected]>
Cc "Desnes A. Nunes do Rosario" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thiago Jung Bauermann <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Michal Suchanek <[email protected]>
Cc Chang S. Bae <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Andy Lutomirski <[email protected]>

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: 09688c0166e76ce2fb85e86b9d99be8b0084cdf9

Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/4 selftests: Provide local define of __cpuid_count()

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.

Provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.

---
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.

 tools/testing/selftests/kselftest.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/kselftest.h +15 -0
index f1180987492c..898d7b2fac6c 100644
@@ -52,6 +52,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
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH V2 1/4] selftests: Provide local define of __cpuid_count()

Quoted 35 lines · Reinette Chatre

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.

Provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.

---
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.

 tools/testing/selftests/kselftest.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/kselftest.h +0 -0
index f1180987492c..898d7b2fac6c 100644

tools/testing/selftests/kselftest.h line 59

Quoted 8 lines
tools/testing/selftests/kselftest.h +8 -0
@@ -52,6 +52,21 @@
+ * 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

Linux C check tool "scripts/checkpatch.pl" shows an error:
"
ERROR: Macros with complex values should be enclosed in parentheses
...

+#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))
"
Googling:
https://www.google.com/search?q=Macros+with+complex+values+should+be+enclosed+in+parentheses&rlz=1C1GCEB_enUS884US884&oq=Macros+with+complex+values+should+be+enclosed+in+parentheses&aqs=chrome.0.69i59j0i5i30l2.313j0j7&sourceid=chrome&ie=UTF-8
-> https://stackoverflow.com/questions/8142280/why-do-we-need-parentheses-around-block-macro

Could we fix it as follow, shall we?
"
#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
"
Thanks!
--Pengfei

#3 lore raw

Re: [PATCH V2 1/4] selftests: Provide local define of __cpuid_count()

Hi Pengfei,

Quoted 31 lines · Pengfei Xu
Quoted 30 lines · Reinette Chatre

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.

Provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.

---
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.

 tools/testing/selftests/kselftest.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Quoted 16 lines

tools/testing/selftests/kselftest.h line 59

Quoted 13 lines
tools/testing/selftests/kselftest.h +8 -0
index f1180987492c..898d7b2fac6c 100644
@@ -52,6 +52,21 @@
+ * 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

Linux C check tool "scripts/checkpatch.pl" shows an error:
"
ERROR: Macros with complex values should be enclosed in parentheses

I encountered this also and that is why this patch contains the "Note to maintainers" above. It is not clear to me whether you considered the note since your response does not acknowledge it.

Quoted 9 lines

...

tools/testing/selftests/kselftest.h +4 -0
+#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))

"
Googling:
https://www.google.com/search?q=Macros+with+complex+values+should+be+enclosed+in+parentheses&rlz=1C1GCEB_enUS884US884&oq=Macros+with+complex+values+should+be+enclosed+in+parentheses&aqs=chrome.0.69i59j0i5i30l2.313j0j7&sourceid=chrome&ie=UTF-8
-> https://stackoverflow.com/questions/8142280/why-do-we-need-parentheses-around-block-macro

More information available in
https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
but from what I understand it does not apply to this macro. Even so, I do not know what checkpatch.pl uses to determine that this is a "Macro with complex values".

Quoted 11 lines

Could we fix it as follow, shall we?
"
#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
"

Sure, I can do so.

Reinette

#4 lore raw

Re: [PATCH V2 1/4] selftests: Provide local define of __cpuid_count()

Quoted 51 lines · Reinette Chatre

Hi Pengfei,

Quoted 31 lines · Pengfei Xu
Quoted 30 lines · Reinette Chatre

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.

Provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.

---
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.

 tools/testing/selftests/kselftest.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Quoted 16 lines

tools/testing/selftests/kselftest.h line 59

Quoted 13 lines
tools/testing/selftests/kselftest.h +8 -0
index f1180987492c..898d7b2fac6c 100644
@@ -52,6 +52,21 @@
+ * 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

Linux C check tool "scripts/checkpatch.pl" shows an error:
"
ERROR: Macros with complex values should be enclosed in parentheses

Quoted 4 lines

I encountered this also and that is why this patch contains the "Note to maintainers" above. It is not clear to me whether you considered the note since your response does not acknowledge it.

Sorry, I just made a suggestion to fix this problem mentioned by the script. I didn't notice and reply for the note.

Quoted 10 lines
Quoted 9 lines

...

tools/testing/selftests/kselftest.h +4 -0
+#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))

"
Googling:
https://www.google.com/search?q=Macros+with+complex+values+should+be+enclosed+in+parentheses&rlz=1C1GCEB_enUS884US884&oq=Macros+with+complex+values+should+be+enclosed+in+parentheses&aqs=chrome.0.69i59j0i5i30l2.313j0j7&sourceid=chrome&ie=UTF-8
-> https://stackoverflow.com/questions/8142280/why-do-we-need-parentheses-around-block-macro

Quoted 6 lines

More information available in
https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
but from what I understand it does not apply to this macro. Even so, I do not know what checkpatch.pl uses to determine that this is a "Macro with complex values".

Checked checkpatch.pl and it seems to suggest using ({ }) for any asm macro definition.

Quoted 12 lines
Quoted 11 lines

Could we fix it as follow, shall we?
"
#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
"

Quoted 2 lines

Sure, I can do so.

I just made a suggestion to fix the problem reported by the checkpatch.pl. But I didn't think deeply enough before: I'm not sure is there any real improvment or help after the fix.

Thanks!
--Pengfei

Quoted 1 line

Reinette

#5 lore raw

Re: [PATCH V2 1/4] selftests: Provide local define of __cpuid_count()

Hi Pengfei,

Quoted 90 lines · Pengfei Xu
Quoted 51 lines · Reinette Chatre

Hi Pengfei,

Quoted 31 lines · Pengfei Xu
Quoted 30 lines · Reinette Chatre

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.

Provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.

---
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.

 tools/testing/selftests/kselftest.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Quoted 16 lines

tools/testing/selftests/kselftest.h line 59

Quoted 13 lines
tools/testing/selftests/kselftest.h +8 -0
index f1180987492c..898d7b2fac6c 100644
@@ -52,6 +52,21 @@
+ * 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

Linux C check tool "scripts/checkpatch.pl" shows an error:
"
ERROR: Macros with complex values should be enclosed in parentheses

Quoted 4 lines

I encountered this also and that is why this patch contains the "Note to maintainers" above. It is not clear to me whether you considered the note since your response does not acknowledge it.

Sorry, I just made a suggestion to fix this problem mentioned by the script. I didn't notice and reply for the note.

Quoted 10 lines
Quoted 9 lines

...

tools/testing/selftests/kselftest.h +4 -0
+#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))

"
Googling:
https://www.google.com/search?q=Macros+with+complex+values+should+be+enclosed+in+parentheses&rlz=1C1GCEB_enUS884US884&oq=Macros+with+complex+values+should+be+enclosed+in+parentheses&aqs=chrome.0.69i59j0i5i30l2.313j0j7&sourceid=chrome&ie=UTF-8
-> https://stackoverflow.com/questions/8142280/why-do-we-need-parentheses-around-block-macro

Quoted 6 lines

More information available in
https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
but from what I understand it does not apply to this macro. Even so, I do not know what checkpatch.pl uses to determine that this is a "Macro with complex values".

Checked checkpatch.pl and it seems to suggest using ({ }) for any asm macro definition.

Quoted 12 lines
Quoted 11 lines

Could we fix it as follow, shall we?
"
#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
"

Quoted 5 lines
Quoted 2 lines

Sure, I can do so.

I just made a suggestion to fix the problem reported by the checkpatch.pl. But I didn't think deeply enough before: I'm not sure is there any real improvment or help after the fix.

In this case I would prefer to not implicitly follow the checkpatch.pl without understanding what the concern is.

The goal of this change is to make the __cpuid_count() macro available within kselftest and it does so by duplicating gcc's __cpuid_count() macro.

The macro style is not unique and you would, for example, encounter the same checkpatch.pl complaint if you run:
./scripts/checkpatch.pl -f arch/x86/kernel/fpu/xstate.h

Reinette

#6 lore raw

Re: [PATCH V2 1/4] selftests: Provide local define of __cpuid_count()

Quoted 102 lines · Reinette Chatre

Hi Pengfei,

Quoted 90 lines · Pengfei Xu
Quoted 51 lines · Reinette Chatre

Hi Pengfei,

Quoted 31 lines · Pengfei Xu
Quoted 30 lines · Reinette Chatre

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.

Provide a centrally defined macro for __cpuid_count() to help eliminate the duplicate CPUID wrappers while continuing to compile in older environments.

---
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.

 tools/testing/selftests/kselftest.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Quoted 16 lines

tools/testing/selftests/kselftest.h line 59

Quoted 13 lines
tools/testing/selftests/kselftest.h +8 -0
index f1180987492c..898d7b2fac6c 100644
@@ -52,6 +52,21 @@
+ * 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

Linux C check tool "scripts/checkpatch.pl" shows an error:
"
ERROR: Macros with complex values should be enclosed in parentheses

Quoted 4 lines

I encountered this also and that is why this patch contains the "Note to maintainers" above. It is not clear to me whether you considered the note since your response does not acknowledge it.

Sorry, I just made a suggestion to fix this problem mentioned by the script. I didn't notice and reply for the note.

Quoted 10 lines
Quoted 9 lines

...

tools/testing/selftests/kselftest.h +4 -0
+#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))

"
Googling:
https://www.google.com/search?q=Macros+with+complex+values+should+be+enclosed+in+parentheses&rlz=1C1GCEB_enUS884US884&oq=Macros+with+complex+values+should+be+enclosed+in+parentheses&aqs=chrome.0.69i59j0i5i30l2.313j0j7&sourceid=chrome&ie=UTF-8
-> https://stackoverflow.com/questions/8142280/why-do-we-need-parentheses-around-block-macro

Quoted 6 lines

More information available in
https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
but from what I understand it does not apply to this macro. Even so, I do not know what checkpatch.pl uses to determine that this is a "Macro with complex values".

Checked checkpatch.pl and it seems to suggest using ({ }) for any asm macro definition.

Quoted 12 lines
Quoted 11 lines

Could we fix it as follow, shall we?
"
#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
"

Quoted 5 lines
Quoted 2 lines

Sure, I can do so.

I just made a suggestion to fix the problem reported by the checkpatch.pl. But I didn't think deeply enough before: I'm not sure is there any real improvment or help after the fix.

In this case I would prefer to not implicitly follow the checkpatch.pl without understanding what the concern is.

Quoted 6 lines

The goal of this change is to make the __cpuid_count() macro available within kselftest and it does so by duplicating gcc's __cpuid_count() macro.

The macro style is not unique and you would, for example, encounter the same checkpatch.pl complaint if you run:
./scripts/checkpatch.pl -f arch/x86/kernel/fpu/xstate.h

Ok, no question from my side.

Thanks!
--Pengfei

Quoted 2 lines

Reinette

#7 lore raw

PATCH v2 2/4 selftests/vm/pkeys: Use provided __cpuid_count() macro

In reply to Reinette Chatre

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 <[email protected]>
Cc Sandipan Das <[email protected]>
Cc Florian Weimer <[email protected]>
Cc "Desnes A. Nunes do Rosario" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thiago Jung Bauermann <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Michal Suchanek <[email protected]>

---
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(-)
tools/testing/selftests/vm/pkey-x86.h +2 -19
index e4a4ce2b826d..b078ce9c6d2a 100644
@@ -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;
Signature · 1 line
2.25.1
#8 lore raw

PATCH v2 3/4 selftests/x86/amx: Use provided __cpuid_count() macro

In reply to Reinette Chatre

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 <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

---
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(-)
tools/testing/selftests/x86/amx.c +7 -17
index 3615ef4a48bb..d463ea30d90f 100644
@@ -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
Signature · 1 line
2.25.1
#9 lore raw

PATCH v2 4/4 selftests/x86/corrupt_xstate_header: Use provided __cpuid_count() macro

In reply to Reinette Chatre

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 <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

---
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(-)
tools/testing/selftests/x86/corrupt_xstate_header.c +2 -14
index ab8599c10ce5..cf9ce8fbb656 100644
@@ -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);
Signature · 1 line
2.25.1