diff mbox series

selftests/sgx: Fix corrupted cpuid macro invocation

Message ID 20211204202355.23005-1-jarkko@kernel.org
State Accepted
Commit 572a0a647b9b491729d24c083c8410c55bf16326
Headers show
Series selftests/sgx: Fix corrupted cpuid macro invocation | expand

Commit Message

Jarkko Sakkinen Dec. 4, 2021, 8:23 p.m. UTC
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")
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 tools/testing/selftests/sgx/main.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Reinette Chatre Dec. 5, 2021, 12:32 a.m. UTC | #1
Hi Jarkko,

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

On 12/4/2021 12:23 PM, Jarkko Sakkinen wrote:
> 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")
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

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
Dave Hansen Dec. 6, 2021, 10:54 p.m. UTC | #2
On 12/4/21 4:32 PM, Reinette Chatre wrote:
> 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, 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?
Reinette Chatre Dec. 6, 2021, 11:32 p.m. UTC | #3
Hi Dave,

On 12/6/2021 2:54 PM, Dave Hansen wrote:
> On 12/4/21 4:32 PM, Reinette Chatre wrote:
>> 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, 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
Dave Hansen Dec. 6, 2021, 11:36 p.m. UTC | #4
On 12/6/21 3:32 PM, Reinette Chatre wrote:
> 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.
Jarkko Sakkinen Dec. 22, 2021, 12:40 a.m. UTC | #5
On Tue, Dec 14, 2021 at 04:19:24PM -0800, Reinette Chatre wrote:
> Hi Dave and Jarkko,
> 
> On 12/4/2021 12:23 PM, Jarkko Sakkinen wrote:
> > 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")
> > Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> > ---
> >   tools/testing/selftests/sgx/main.c | 5 ++---
> >   1 file changed, 2 insertions(+), 3 deletions(-)
> > 
> > diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
> > index 7e912db4c6c5..370c4995f7c4 100644
> > --- a/tools/testing/selftests/sgx/main.c
> > +++ b/tools/testing/selftests/sgx/main.c
> > @@ -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)
> 
> 
> Shuah confirmed ([1]) that there is no problem including cpuid.h and that
> this is the preferred fix.
> 
> Thank you very much Jarkko.
> 
> Acked-by: Reinette Chatre <reinette.chatre@intel.com>
> 
> Reinette
> 
> [1] https://lore.kernel.org/linux-kselftest/63293c72-55ca-9446-35eb-74aff4c8ba5d@linuxfoundation.org/

OK, cool, thanks!

/Jarkko
diff mbox series

Patch

diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
index 7e912db4c6c5..370c4995f7c4 100644
--- a/tools/testing/selftests/sgx/main.c
+++ b/tools/testing/selftests/sgx/main.c
@@ -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)