Skip to contentSkip to conversation

selftests/sgx: Fix corrupted cpuid macro invocation

PATCH Applied Jarkko Sakkinen 4 participants 8 messages Dec 4, 2021 – Dec 22, 2021

Subsystems (2)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH 1/1 selftests/sgx: Fix corrupted cpuid macro invocation

Compilation results:

$ make -C tools/testing/selftests/sgx/
make: Entering directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'
gcc -Wall -Werror -g -I../../../../tools/include -fPIC -z noexecstack -c main.c -o /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o
main.c: In function ‘get_total_epc_mem’:
main.c:296:17: error: implicit declaration of function ‘__cpuid’ [-Werror=implicit-function-declaration]
  296 |                 __cpuid(&eax, &ebx, &ecx, &edx);
      |                 ^~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:33: /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o] Error 1
make: Leaving directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'

Include to cpuid.h is missing and the macro usage is incorrect.

Include cpuid.h and use __cpuid_count() macro in order to fix the compilation issue.

Fixes f0ff2447b861 ("selftests/sgx: Add a new kselftest: Unclobbered_vdso_oversubscribed")
---
 tools/testing/selftests/sgx/main.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/sgx/main.c +2 -3
index 7e912db4c6c5..370c4995f7c4 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /*  Copyright(c) 2016-20 Intel Corporation. */
 
+#include <cpuid.h>
 #include <elf.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -291,9 +292,7 @@ static unsigned long get_total_epc_mem(void)
 	int section = 0;
 
 	while (true) {
-		eax = SGX_CPUID;
-		ecx = section + SGX_CPUID_EPC;
-		__cpuid(&eax, &ebx, &ecx, &edx);
+		__cpuid_count(SGX_CPUID, section + SGX_CPUID_EPC, eax, ebx, ecx, edx);
 
 		type = eax & SGX_CPUID_EPC_MASK;
 		if (type == SGX_CPUID_EPC_INVALID)
Signature · 1 line
2.32.0
#1 lore raw

Hi Jarkko,

(Dave: I am very sorry - selftests on x86/sgx on tip.git is currently broken because of this)

Quoted 13 lines · Jarkko Sakkinen

Compilation results:

$ make -C tools/testing/selftests/sgx/
make: Entering directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'
gcc -Wall -Werror -g -I../../../../tools/include -fPIC -z noexecstack -c main.c -o /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o
main.c: In function ‘get_total_epc_mem’:
main.c:296:17: error: implicit declaration of function ‘__cpuid’ [-Werror=implicit-function-declaration]
   296 |                 __cpuid(&eax, &ebx, &ecx, &edx);
       |                 ^~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:33: /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o] Error 1
make: Leaving directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'
Quoted 7 lines

Include to cpuid.h is missing and the macro usage is incorrect.

Include cpuid.h and use __cpuid_count() macro in order to fix the compilation issue.

Fixes f0ff2447b861 ("selftests/sgx: Add a new kselftest: Unclobbered_vdso_oversubscribed")

I am sorry - this was my mistake because of a last minute change to that patch that I submitted with a dependency that only arrives in a later patch that is not upstream yet.

Jarkko, thank you very much for catching this.

I am not sure what the right way is to fix it though - my original intention, what the code uses, was to add a snippet as below as is the custom for all tests needing to run cpuid. There are many usages of cpuid among the selftests but none rely on the cpuid.h to bring in __cpuid_count. I do not know the motivation for this but preferred to stick with the custom for my implementation.

+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)
+ : "memory");
+}

Reinette

#2 lore raw
Quoted 8 lines · Reinette Chatre

I am not sure what the right way is to fix it though - my original intention, what the code uses, was to add a snippet as below as is the custom for all tests needing to run cpuid. There are many usages of cpuid among the selftests but none rely on the cpuid.h to bring in __cpuid_count. I do not know the motivation for this but preferred to stick with the custom for my implementation.

(quoted diff) +1 -0
+static inline void __cpuid(unsigned int *eax, unsigned int *ebx,

Review

Quoted 10 lines
(quoted diff) +10 -0
+               unsigned int *ecx, unsigned int *edx)
+{
+    asm volatile("cpuid"
+        : "=a" (*eax),
+          "=b" (*ebx),
+          "=c" (*ecx),
+          "=d" (*edx)
+        : "0" (*eax), "2" (*ecx)
+        : "memory");
+}

Reinette, is there some reason using __cpuid_count() won't work for the SGX test? Or is your concern that it _might_ break something because you haven't tested it?

#3 lore raw

Hi Dave,

Quoted 8 lines · Dave Hansen
Quoted 7 lines · Reinette Chatre

I am not sure what the right way is to fix it though - my original intention, what the code uses, was to add a snippet as below as is the custom for all tests needing to run cpuid. There are many usages of cpuid among the selftests but none rely on the cpuid.h to bring in __cpuid_count. I do not know the motivation for this but preferred to stick with the custom for my implementation.

Quoted 15 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+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)
+        : "memory");
+}

Reinette, is there some reason using __cpuid_count() won't work for the SGX test? Or is your concern that it _might_ break something because you haven't tested it?

As a sanity check I tested with __cpuid_count() and it works.

My concern is actually about the kselftest framework where I could not answer the following question with certainty: why are there so many usages of cpuid but everybody writes their own instead of including cpuid.h? I am concerned that there are some environment constraints that I am not familiar with and thus decided to follow the custom.

One speculation is that since cpuid.h arrives with libgcc's dev package instead of the regular libc dev package there may now be a new dependency on what users need to have on their systems to run these tests.

Reinette

#4 lore raw
Quoted 5 lines · Reinette Chatre

My concern is actually about the kselftest framework where I could not answer the following question with certainty: why are there so many usages of cpuid but everybody writes their own instead of including cpuid.h? I am concerned that there are some environment constraints that I am not familiar with and thus decided to follow the custom.

I'm probably guilty of a case or two. There was no logic behind it, just that I took and out-of-tree test and plopped it into the kernel tree without looking for any selftest infrastructure.

Acked-by on v1 1/1
#5 lore raw
In reply to Jarkko Sakkinen

Hi Dave and Jarkko,

Quoted 37 lines · Jarkko Sakkinen

Compilation results:

$ make -C tools/testing/selftests/sgx/
make: Entering directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'
gcc -Wall -Werror -g -I../../../../tools/include -fPIC -z noexecstack -c main.c -o /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o
main.c: In function ‘get_total_epc_mem’:
main.c:296:17: error: implicit declaration of function ‘__cpuid’ [-Werror=implicit-function-declaration]
   296 |                 __cpuid(&eax, &ebx, &ecx, &edx);
       |                 ^~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:33: /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o] Error 1
make: Leaving directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'

Include to cpuid.h is missing and the macro usage is incorrect.

Include cpuid.h and use __cpuid_count() macro in order to fix the compilation issue.

Fixes f0ff2447b861 ("selftests/sgx: Add a new kselftest: Unclobbered_vdso_oversubscribed")
---
  tools/testing/selftests/sgx/main.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/sgx/main.c +1 -0
index 7e912db4c6c5..370c4995f7c4 100644
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
  /*  Copyright(c) 2016-20 Intel Corporation. */
 
+#include <cpuid.h>
  #include <elf.h>
  #include <errno.h>
  #include <fcntl.h>

tools/testing/selftests/sgx/main.c static unsigned long get_total_epc_mem(void) · line 298

Quoted 10 lines
tools/testing/selftests/sgx/main.c +1 -3
@@ -291,9 +292,7 @@ static unsigned long get_total_epc_mem(void)
  	int section = 0;
 
  	while (true) {
-		eax = SGX_CPUID;
-		ecx = section + SGX_CPUID_EPC;
-		__cpuid(&eax, &ebx, &ecx, &edx);
+		__cpuid_count(SGX_CPUID, section + SGX_CPUID_EPC, eax, ebx, ecx, edx);
 
  		type = eax & SGX_CPUID_EPC_MASK;
  		if (type == SGX_CPUID_EPC_INVALID)

Shuah confirmed ([1]) that there is no problem including cpuid.h and that this is the preferred fix.

Thank you very much Jarkko.

Reinette

[1] https://lore.kernel.org/linux-kselftest/[email protected]/

#7 lore raw
In reply to Jarkko Sakkinen
The following commit has been merged into the x86/sgx branch of tip:

The following commit has been merged into the x86/sgx branch of tip:

Commit-ID:     572a0a647b9b491729d24c083c8410c55bf16326
Gitweb:        https://git.kernel.org/tip/572a0a647b9b491729d24c083c8410c55bf16326
Author:        Jarkko Sakkinen <[email protected]>
AuthorDate:    Sat, 04 Dec 2021 22:23:55 +02:00
Committer:     Dave Hansen <[email protected]>
CommitterDate: Fri, 17 Dec 2021 08:52:33 -08:00

selftests/sgx: Fix corrupted cpuid macro invocation

The SGX selftest fails to build on tip/x86/sgx:

	main.c: In function ‘get_total_epc_mem’:
	main.c:296:17: error: implicit declaration of function ‘__cpuid’ [-Werror=implicit-function-declaration]
	  296 |                 __cpuid(&eax, &ebx, &ecx, &edx);
	      |                 ^~~~~~~

Include cpuid.h and use __cpuid_count() macro in order to fix the compilation issue.

[ dhansen: tweak commit message ]

Fixes f0ff2447b861 ("selftests/sgx: Add a new kselftest: Unclobbered_vdso_oversubscribed")
Cc Shuah Khan <[email protected]>
---
 tools/testing/selftests/sgx/main.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/sgx/main.c +2 -3
index 7e912db..370c499 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /*  Copyright(c) 2016-20 Intel Corporation. */
 
+#include <cpuid.h>
 #include <elf.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -291,9 +292,7 @@ static unsigned long get_total_epc_mem(void)
 	int section = 0;
 
 	while (true) {
-		eax = SGX_CPUID;
-		ecx = section + SGX_CPUID_EPC;
-		__cpuid(&eax, &ebx, &ecx, &edx);
+		__cpuid_count(SGX_CPUID, section + SGX_CPUID_EPC, eax, ebx, ecx, edx);
 
 		type = eax & SGX_CPUID_EPC_MASK;
 		if (type == SGX_CPUID_EPC_INVALID)
author maintainer
#6 lore raw
In reply to Reinette Chatre
Quoted 52 lines · Reinette Chatre

Hi Dave and Jarkko,

Quoted 36 lines · Jarkko Sakkinen

Compilation results:

$ make -C tools/testing/selftests/sgx/
make: Entering directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'
gcc -Wall -Werror -g -I../../../../tools/include -fPIC -z noexecstack -c main.c -o /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o
main.c: In function ‘get_total_epc_mem’:
main.c:296:17: error: implicit declaration of function ‘__cpuid’ [-Werror=implicit-function-declaration]
   296 |                 __cpuid(&eax, &ebx, &ecx, &edx);
       |                 ^~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:33: /home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx/main.o] Error 1
make: Leaving directory '/home/jarkko/Projects/linux-sgx/tools/testing/selftests/sgx'

Include to cpuid.h is missing and the macro usage is incorrect.

Include cpuid.h and use __cpuid_count() macro in order to fix the compilation issue.

Fixes f0ff2447b861 ("selftests/sgx: Add a new kselftest: Unclobbered_vdso_oversubscribed")
---
  tools/testing/selftests/sgx/main.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/sgx/main.c +1 -0
index 7e912db4c6c5..370c4995f7c4 100644
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
  /*  Copyright(c) 2016-20 Intel Corporation. */
+#include <cpuid.h>
  #include <elf.h>
  #include <errno.h>
  #include <fcntl.h>

tools/testing/selftests/sgx/main.c static unsigned long get_total_epc_mem(void) · line 296

Quoted 8 lines
tools/testing/selftests/sgx/main.c +1 -3
@@ -291,9 +292,7 @@ static unsigned long get_total_epc_mem(void)
  	int section = 0;
  	while (true) {
-		eax = SGX_CPUID;
-		ecx = section + SGX_CPUID_EPC;
-		__cpuid(&eax, &ebx, &ecx, &edx);
+		__cpuid_count(SGX_CPUID, section + SGX_CPUID_EPC, eax, ebx, ecx, edx);
  		type = eax & SGX_CPUID_EPC_MASK;
  		if (type == SGX_CPUID_EPC_INVALID)

Shuah confirmed ([1]) that there is no problem including cpuid.h and that this is the preferred fix.

Quoted 7 lines

Thank you very much Jarkko.

Reinette

[1] https://lore.kernel.org/linux-kselftest/[email protected]/

OK, cool, thanks!

/Jarkko