Re: [PATCH V4 04/50] x86/xen: Add xenpv_restore_regs_and_return_to_usermode()
From: H. Peter Anvin
Date: Tue Nov 02 2021 - 07:44:22 EST
On November 2, 2021 10:49:44 AM GMT+01:00, Borislav Petkov <bp@xxxxxxxxx> wrote:
>On Tue, Nov 02, 2021 at 05:19:46PM +0800, Lai Jiangshan wrote:
>> It will add a 5-byte NOP at the beginning of the native
>> swapgs_restore_regs_and_return_to_usermode.
>
>So?
>
It would be interesting to have an "override function with jmp" alternatives macro. It doesn't require any changes to the alternatives mechanism proper (but possibly to objtool): it would just insert an alternatives entry without adding any code including nops to the main path. It would of course only be applicable to a jmp, so a syntax like OVERRIDE_JMP feature, target rather than open-coding the instruction would probably be a good idea.
That would reduce the trade-off to zero.
>> I avoided adding unneeded code in the native code even if it is NOPs
>> and avoided melting xenpv-one into the native one which will reduce
>> the code readability.
>
>How does this reduce code readability?!
>
>diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
>index e38a4cf795d9..bf1de54a1fca 100644
>--- a/arch/x86/entry/entry_64.S
>+++ b/arch/x86/entry/entry_64.S
>@@ -567,6 +567,10 @@ __irqentry_text_end:
>
> SYM_CODE_START_LOCAL(common_interrupt_return)
> SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
>+
>+ ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", \
>+ X86_FEATURE_XENPV
>+
> #ifdef CONFIG_DEBUG_ENTRY
> /* Assert that pt_regs indicates user mode. */
> testb $3, CS(%rsp)
>
>> I will follow your preference since a 5-byte NOP is so negligible in the slow
>> path with an iret instruction.
>
>Yes, we do already gazillion things on those entry and exit paths.
>
>> Or other option that adds macros to wrap the ALTERNATIVE.
>> RESTORE_REGS_AND_RETURN_TO_USERMODE and
>> COND_RESTORE_REGS_AND_RETURN_TO_USERMODE (test %eax before jmp in native case)
>
>No, the main goal is to keep the asm code as readable and as simple as
>possible.
>
>If macros or whatever need to be added, there better be a good reason
>for them. Saving a NOP is not one of them.
>
>Thx.
>
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.