Re: objtool clac/stac handling change..

From: Andy Lutomirski
Date: Fri Jul 03 2020 - 17:41:59 EST


On Fri, Jul 3, 2020 at 2:10 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Fri, Jul 3, 2020 at 2:02 PM Al Viro <viro@xxxxxxxxxxxxxxxxxx> wrote:
> >
> > Actually, for more serious problem consider arch/x86/lib/copy_user_64.S
>
> What? No.
>
> > In case of an unhandled fault on attempt to read an (unaligned) word,
> > the damn thing falls back to this:
> > SYM_CODE_START_LOCAL(.Lcopy_user_handle_tail)
> > movl %edx,%ecx
> > 1: rep movsb
> > 2: mov %ecx,%eax
> > ASM_CLAC
> > ret
> >
> > _ASM_EXTABLE_UA(1b, 2b)
> > SYM_CODE_END(.Lcopy_user_handle_tail)
>
> In the case of "we did an unaligned word at the end of a page, we took
> a fault, and now we have to start all over", the _least_ of our
> problems is that part of "starting over" would now imply doing a
> "stac" again.
>
> Yeah, the "stac" instruction isn't hugely fast, and serializes the
> pipeline, so it's a nasty 20 cycles or something.
>
> But for chissake, this
> (a) happens approximately never
> (b) is after a fault that took a thousand cycles
>
> so the trivial thing to do is to just say "yeah, you need to add the
> STAC when your optimistic thing failed and you have to fall back to
> the byte-at-a-time tail case".
>
> It's particularly trivial since objtool would statically find all
> these places, since it would warn about the ASM_CLAC without a STAC
> (that's assuming Josh's patch, of course).
>

I still feel like the ex_handler-automatically-does-CLAC thing is an
optimization that isn't worth it. Once we pull our heads out of the
giant pile of macros and inlined functions, we're talking about
changing:

stac
1: mov something, somewhere
2: clac
... exception table entry pointing to 2 ...

to

stac
1: mov something, somewhere
clac
2:
... exception table entry pointing to 2 ...

Now maybe the pattern:

if (get_user(...))
goto bad;

gets a bit improved because we don't need to emit a fixup that does
clac; jmp. But on the flip side, the jump folding pattern looks
better like this:

unsafe_uaccess_begin();
if (unsafe_get_user(...))
goto fail;
if (unsafe_get_user(...))
goto fail;
unsafe_uaccess_end();

fail:
unsafe_uaccess_end();

than like:

unsafe_uaccess_begin();
if (unsafe_get_user(...))
goto fail;
if (unsafe_get_user(...))
goto fail;
unsafe_uaccess_end();

fail:
/* not unsafe_uaccess_end(); because unsafe_get_user() has
conditional-CLAC semantics */

And we really really do not want to ever see this:

int err;
unsafe_uaccess_begin();
err = unsafe_get_user(...);
if (err == 0)
unsafe_uaccess_end(); /* WTF?!? */

So I feel like this is all putting the cart before the horse. Can we
make everything work sanely without the automatic CLAC and then, if it
actually improves something, add an optional ex_handler_uaccess_clac
for the cases that benefit? I'm totally fine with burying:

unsafe_get_user_jump(..., error_label);
error_label:
/* we know unsafe_uaccess_end() already happened here */

in an inline function here if it meaningfully improves code
generation, but this is counter-intuitive code and I think we should
treat it as such.

--Andy