mbox series

[0/3] selftests: Remove duplicate CPUID wrappers

Message ID cover.1644000145.git.reinette.chatre@intel.com (mailing list archive)
Headers show
Series selftests: Remove duplicate CPUID wrappers | expand

Message

Reinette Chatre Feb. 4, 2022, 7:17 p.m. UTC
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 but having one is also
unnecessary because of the existence of a macro that can
be used instead.

This series replaces private CPUID wrappers with calls
to the __cpuid_count() macro from cpuid.h as made available
by gcc and clang/llvm.

Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Ram Pai <linuxram@us.ibm.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 (3):
  selftests/vm/pkeys: Use existing __cpuid_count() macro
  selftests/x86/amx: Use existing __cpuid_count() macro
  selftests/x86/corrupt_xstate_header: Use existing __cpuid_count()
    macro

 tools/testing/selftests/vm/pkey-x86.h         | 22 +++---------------
 tools/testing/selftests/x86/amx.c             | 23 +++++--------------
 .../selftests/x86/corrupt_xstate_header.c     | 17 ++------------
 3 files changed, 11 insertions(+), 51 deletions(-)

Comments

Shuah Khan Feb. 4, 2022, 11:39 p.m. UTC | #1
On 2/4/22 12:17 PM, Reinette Chatre wrote:
> 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 but having one is also
> unnecessary because of the existence of a macro that can
> be used instead.
> 
> This series replaces private CPUID wrappers with calls
> to the __cpuid_count() macro from cpuid.h as made available
> by gcc and clang/llvm.
> 
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: Ram Pai <linuxram@us.ibm.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 (3):
>    selftests/vm/pkeys: Use existing __cpuid_count() macro
>    selftests/x86/amx: Use existing __cpuid_count() macro
>    selftests/x86/corrupt_xstate_header: Use existing __cpuid_count()
>      macro
> 
>   tools/testing/selftests/vm/pkey-x86.h         | 22 +++---------------
>   tools/testing/selftests/x86/amx.c             | 23 +++++--------------
>   .../selftests/x86/corrupt_xstate_header.c     | 17 ++------------
>   3 files changed, 11 insertions(+), 51 deletions(-)
> 

I am all for this cleanup. However, I am not finding __cpuid_count()
marco on my system with gcc:

gcc --version
gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0

My concern is regression on older gcc versions.

thanks,
-- Shuah
Reinette Chatre Feb. 5, 2022, 12:11 a.m. UTC | #2
Hi Shuah,

On 2/4/2022 3:39 PM, Shuah Khan wrote:
> On 2/4/22 12:17 PM, Reinette Chatre wrote:
>> 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 but having one is also
>> unnecessary because of the existence of a macro that can
>> be used instead.
>>
>> This series replaces private CPUID wrappers with calls
>> to the __cpuid_count() macro from cpuid.h as made available
>> by gcc and clang/llvm.
>>
>> Cc: Dave Hansen <dave.hansen@linux.intel.com>
>> Cc: Ram Pai <linuxram@us.ibm.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 (3):
>>    selftests/vm/pkeys: Use existing __cpuid_count() macro
>>    selftests/x86/amx: Use existing __cpuid_count() macro
>>    selftests/x86/corrupt_xstate_header: Use existing __cpuid_count()
>>      macro
>>
>>   tools/testing/selftests/vm/pkey-x86.h         | 22 +++---------------
>>   tools/testing/selftests/x86/amx.c             | 23 +++++--------------
>>   .../selftests/x86/corrupt_xstate_header.c     | 17 ++------------
>>   3 files changed, 11 insertions(+), 51 deletions(-)
>>
> 
> I am all for this cleanup. However, I am not finding __cpuid_count()
> marco on my system with gcc:
> 
> gcc --version
> gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0
> 
> My concern is regression on older gcc versions.

Please see this message from our earlier thread where you were able
to find it on your system:
https://lore.kernel.org/linux-kselftest/63293c72-55ca-9446-35eb-74aff4c8ba5d@linuxfoundation.org/

As mentioned in that thread, on my system it arrived via user space's
libgcc-dev package. This does not seem to be the first time including
files from this source - I did a quick check and from what I can tell
existing kselftest includes like stdarg.h, stdbool.h, stdatomic.h,
unwind.h, x86intrin.h ... arrive via libgcc-dev.

Reinette
Shuah Khan Feb. 7, 2022, 6 p.m. UTC | #3
On 2/4/22 5:11 PM, Reinette Chatre wrote:
> Hi Shuah,
> 
> On 2/4/2022 3:39 PM, Shuah Khan wrote:
>> On 2/4/22 12:17 PM, Reinette Chatre wrote:
>>> 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 but having one is also
>>> unnecessary because of the existence of a macro that can
>>> be used instead.
>>>
>>> This series replaces private CPUID wrappers with calls
>>> to the __cpuid_count() macro from cpuid.h as made available
>>> by gcc and clang/llvm.
>>>
>>> Cc: Dave Hansen <dave.hansen@linux.intel.com>
>>> Cc: Ram Pai <linuxram@us.ibm.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 (3):
>>>     selftests/vm/pkeys: Use existing __cpuid_count() macro
>>>     selftests/x86/amx: Use existing __cpuid_count() macro
>>>     selftests/x86/corrupt_xstate_header: Use existing __cpuid_count()
>>>       macro
>>>
>>>    tools/testing/selftests/vm/pkey-x86.h         | 22 +++---------------
>>>    tools/testing/selftests/x86/amx.c             | 23 +++++--------------
>>>    .../selftests/x86/corrupt_xstate_header.c     | 17 ++------------
>>>    3 files changed, 11 insertions(+), 51 deletions(-)
>>>
>>
>> I am all for this cleanup. However, I am not finding __cpuid_count()
>> marco on my system with gcc:
>>
>> gcc --version
>> gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0
>>
>> My concern is regression on older gcc versions.
> 
> Please see this message from our earlier thread where you were able
> to find it on your system:
> https://lore.kernel.org/linux-kselftest/63293c72-55ca-9446-35eb-74aff4c8ba5d@linuxfoundation.org/
> 

Right. After I sent off the response, I was thinking we discussed
this before. Thanks for the refresh.

> As mentioned in that thread, on my system it arrived via user space's
> libgcc-dev package. This does not seem to be the first time including
> files from this source - I did a quick check and from what I can tell
> existing kselftest includes like stdarg.h, stdbool.h, stdatomic.h,
> unwind.h, x86intrin.h ... arrive via libgcc-dev.
> 

This will work fine on newer versions of gcc/clang. However this could
fail when mainline kselftest is used on stable releases on test rings
and so on, especially if they have older versions of gcc/clang.

We will have to find a solution for this. Instead of deleting the local
define, let's keep it under ifndef __cpuid_count

/usr/lib/gcc/x86_64-linux-gnu/11/include/cpuid.h

#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))

thanks,
-- Shuah
Reinette Chatre Feb. 7, 2022, 7:13 p.m. UTC | #4
Hi Shuah,

On 2/7/2022 10:00 AM, Shuah Khan wrote:

> 
> This will work fine on newer versions of gcc/clang. However this could
> fail when mainline kselftest is used on stable releases on test rings
> and so on, especially if they have older versions of gcc/clang.

Indeed. It thus seems that kselftest has a minimal required version for
gcc/clang that is not the current mainline minimal version but the
minimal version of the oldest supported stable kernel, which is v4.9.

__cpuid_count() was added to gcc in commit:
cb0dee885cb30b4e9beeef070cf000baa7d09abe and thus available since
gcc 4.4.

Looking at Documentation/Changes or later Documentation/process/changes.rst
kernels v4.9 and v4.14 have the minimal required version of
gcc of 3.2. So this change would encounter an issue if mainline
kselftest is used to test a v4.9 or v4.14 kernel on a system that only
supports its minimal gcc.

Kernel v4.19 moved the gcc minimal required version to 4.6 that does
contain this macro.

There does not seem to be a minimum required version of clang/LLVM
in v4.19. The first time I see a minimal version for Clang/LLVM
for a stable kernel is in kernel v5.10 with Clang/LLVM minimal
version 10.0.1 and from what I can tell the __cpuid_count() macro
was added to Clang/LLVM in version 3.4.0 
(commit 4dcb5dbb53ea4fbeab48bc6bc3c4d392361dabc1).

> 
> We will have to find a solution for this. Instead of deleting the local
> define, let's keep it under ifndef __cpuid_count
> 
> /usr/lib/gcc/x86_64-linux-gnu/11/include/cpuid.h
> 
> #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))
> 

Will do. I see that gcc obtained the volatile qualifier in v11.1 so I can use
the most recent macro as you have here.

Reinette