Re: [PATCH 25/24] x86/traps: Rewrite native_load_gs_index in C code

From: Andy Lutomirski
Date: Mon Sep 13 2021 - 16:01:58 EST




On Thu, Sep 2, 2021, at 3:50 AM, Lai Jiangshan wrote:
> From: Lai Jiangshan <laijs@xxxxxxxxxxxxxxxxx>
>
> There is no constrain/limition to force native_load_gs_index() to be in
> ASM code.
>
> Signed-off-by: Lai Jiangshan <laijs@xxxxxxxxxxxxxxxxx>

>
> #ifdef CONFIG_X86_64
> +
> +/*
> + * Reload gs selector with exception handling
> + * selector: new selector
> + *
> + * Is noinstr as it shouldn't be instrumented.
> + */
> +noinstr void native_load_gs_index(unsigned int selector)
> +{
> + unsigned long flags;
> +
> + local_irq_save(flags);

This patch would be a bit less alarming if you moved the swapgs into asm. Also, this needs a comment explaining why skipping the swapgs back to kernel gs in the exception path is correct.

> + native_swapgs();
> + asm volatile(
> + ".global asm_load_gs_index_gs_change \n"
> + "asm_load_gs_index_gs_change: \n"
> + "1: movl %0, %%gs \n"
> + " swapgs \n"
> + "2: \n"
> + _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_gs)
> + :: "r" (selector) : "memory");
> + alternative("", "mfence", X86_BUG_SWAPGS_FENCE);
> + local_irq_restore(flags);
> +}
> +