Re: [PATCH v2 0/6] x86: Clean up fast syscall return validation

From: Brian Gerst
Date: Thu Oct 05 2023 - 12:20:41 EST


On Thu, Oct 5, 2023 at 4:22 AM Ingo Molnar <mingo@xxxxxxxxxx> wrote:
>
>
> * Brian Gerst <brgerst@xxxxxxxxx> wrote:
>
> > This patch set cleans up the tests done to determine if a fast syscall
> > return instruction can be used to return to userspace. It converts the
> > code to C, and refactors existing code to be more readable.
> >
> > v2:
> > - Fix shift value for canonical RIP test and use
> > __is_canonical_address()
> >
> > Brian Gerst (6):
> > x86/entry/64: Remove obsolete comment on tracing vs. SYSRET
> > x86/entry/64: Convert SYSRET validation tests to C
> > x86/entry/compat: Combine return value test from syscall handler
> > x86/entry/32: Convert do_fast_syscall_32() to bool return type
> > x86/entry/32: Remove SEP test for SYSEXIT
> > x86/entry/32: Clean up syscall fast exit tests
> >
> > arch/x86/entry/common.c | 99 +++++++++++++++++++++-----------
> > arch/x86/entry/entry_32.S | 2 +-
> > arch/x86/entry/entry_64.S | 68 +---------------------
> > arch/x86/entry/entry_64_compat.S | 12 ++--
> > arch/x86/include/asm/syscall.h | 6 +-
> > 5 files changed, 77 insertions(+), 110 deletions(-)
>
> Ok, so I've applied patches #1, #3, #4 and #5 to tip:x86/entry,
> (ie. skipped #2 & #6 for now), as they look correct and are good
> improvements. None of these four patches depend on the skipped
> patches in some way I missed, correct?
>
> As for #2, I looked at the before/after disassembly, and the new
> C code in do_syscall_64() looked suboptimal on x86-64 defconfig,
> if I was reading it right.
>
> Mind re-evaluating that, and if you still think the C conversion
> is a good idea, mind putting a before/after analysis of the
> generated instructions into the changelog? This is our primary
> system call return path after all.

Looking at the compiled output, the only suboptimal code appears to be
the canonical address test, where the C code uses the CL register for
the shifts instead of immediates.

180: e9 00 00 00 00 jmp 185 <do_syscall_64+0x85>
181: R_X86_64_PC32 .altinstr_aux-0x4
185: b9 07 00 00 00 mov $0x7,%ecx
18a: eb 05 jmp 191 <do_syscall_64+0x91>
18c: b9 10 00 00 00 mov $0x10,%ecx
191: 48 89 c2 mov %rax,%rdx
194: 48 d3 e2 shl %cl,%rdx
197: 48 d3 fa sar %cl,%rdx
19a: 48 39 d0 cmp %rdx,%rax
19d: 75 39 jne 1d8 <do_syscall_64+0xd8>

Was there anything else specifically that you can point out?

Brian Gerst