Re: [v3 07/12] x86/fsgsbase/64: Introduce the new FIND_PERCPU_BASE macro

From: Nadav Amit
Date: Thu Oct 25 2018 - 21:01:28 EST


From: Andy Lutomirski
Sent: October 26, 2018 at 12:25:17 AM GMT
> To: Bae, Chang Seok <chang.seok.bae@xxxxxxxxx>, Nadav Amit <namit@xxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>, Thomas Gleixner <tglx@xxxxxxxxxxxxx>, Andrew Lutomirski <luto@xxxxxxxxxx>, H. Peter Anvin <hpa@xxxxxxxxx>, Andi Kleen <ak@xxxxxxxxxxxxxxx>, Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>, Metzger, Markus T <markus.t.metzger@xxxxxxxxx>, Ravi V. Shankar <ravi.v.shankar@xxxxxxxxx>, LKML <linux-kernel@xxxxxxxxxxxxxxx>
> Subject: Re: [v3 07/12] x86/fsgsbase/64: Introduce the new FIND_PERCPU_BASE macro
>
>
> On Tue, Oct 23, 2018 at 11:43 AM Chang S. Bae <chang.seok.bae@xxxxxxxxx> wrote:
>> GSBASE is used to find per-CPU data in the kernel. But when it is unknown,
>> the per-CPU base can be found from the per_cpu_offset table with a CPU NR.
>> The CPU NR is extracted from the limit field of the CPUNODE entry in GDT,
>> or by the RDPID instruction.
>>
>> Also, add the GAS-compatible RDPID macro.
>>
>> The new macro will be used on a following patch.
>>
>> Suggested-by: H. Peter Anvin <hpa@xxxxxxxxx>
>> Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
>> Cc: Andi Kleen <ak@xxxxxxxxxxxxxxx>
>> Cc: Andy Lutomirski <luto@xxxxxxxxxx>
>> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
>> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
>> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
>> ---
>> arch/x86/include/asm/fsgsbase.h | 52 +++++++++++++++++++++++++++++++++
>> arch/x86/include/asm/inst.h | 15 ++++++++++
>> 2 files changed, 67 insertions(+)
>>
>> diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h
>> index e500d771155f..0c2d7d8a8c01 100644
>> --- a/arch/x86/include/asm/fsgsbase.h
>> +++ b/arch/x86/include/asm/fsgsbase.h
>> @@ -111,6 +111,58 @@ extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
>> MODRM 0xd0 wrgsbase_opd 1
>> .endm
>>
>> +#if CONFIG_SMP
>> +
>> +/*
>> + * Fetch the per-CPU GSBASE value for this processor and put it in @reg.
>> + * We normally use %gs for accessing per-CPU data, but we are setting up
>> + * %gs here and obviously can not use %gs itself to access per-CPU data.
>> + */
>> +.macro FIND_PERCPU_BASE_RDPID reg:req
>> + /*
>> + * The CPU/node NR is initialized earlier, directly in cpu_init().
> P
>> + */
>> + RDPID \reg
>
> I would suggest that you instead add a macro LOAD_CPU_AND_NODE \reg
> and have that macro contain the alternative. It can switch between
> RDPID and LSL. This way you avoid duplicating the rest of it.
>
> This should end up in the same header as __getcpu() -- it probably
> makes sense to just move __getcpu() for this purpose.
>
> Also, hpa and Nadav, shouldn't asm/inst.h end up in macros.S?

If there are going to be C uses, yes.

Ingo was concerned that the .s file will be too big, so it should not
be overly abused. In addition, I want to send a patch that recompiles all
the .c files in macro.S changes. Iâm worried it will start creating build
problems.