Re: ASM flags in general

From: Andy Lutomirski
Date: Mon Jul 27 2015 - 18:43:34 EST


On Mon, Jul 27, 2015 at 2:04 PM, H. Peter Anvin <hpa@xxxxxxxxx> wrote:
> On 07/27/2015 01:38 PM, Andy Lutomirski wrote:
>>
>> As long as we're thinking about this stuff, there are bunch of places
>> where we use exception fixups and do awful things involving translating
>> them to error codes. Ideally they'd use as goto instead, but last time
>> I checked, GCC was quite picky and didn't like output constraints and
>> asm goto at the same time. Maybe GCC could fix this at some point, but
>> using condition code outputs might be reasonable, too.
>>
>> Doing this would make put_user_ex and similar completely unnecessary, I
>> think.
>>
>
> No, I think this is wrong. Exceptions and flags are almost each others
> opposites. Since C doesn't have native exception handling (except
> setjmp/longjmp) we pretty much hack it.
>
> asm goto() would indeed be the better way to do this, but again, would
> in most cases require asm goto to support outputs.
>
> However, get_user_ex and put_user_ex we really don't want to go away.
> They produce extremely efficient code -- just a bunch of mov operations
> -- for the common path, and that's the way we like it.

Wouldn't asm goto be just as good (assuming it supported the right
constraints)? In principle, this:

if (put_user(...))
goto error;
if (put_user(...))
goto error;

should optimize to:

mov [...]
_ASM_EXTABLE(...)
mov [...]
_ASM_EXTABLE(...)

...

extable_landing_pad:
jmp error

IOW, I think that GCC's optimizer should be good enough to keep the
error paths out of line and maybe even to coalesce them,

>
> That being said, there probably are a couple of patterns where we could
> do, say "stc" in the exception path, and emit CF as an output:
>
> bool err;
> int errno;
>
> asm volatile("xor %1,%1\n" /* Clears CF */
> "1: something %3,%0\n"/* Leaves CF unchanged, or clears */
> "2:\n"
> ".section .fixup.\"ax\"\n"
> "3: mov %4,%1\n"
> " stc\n"
> " jmp 2b"
> _ASM_EXTABLE(1b,3b)
> : "=X" (output), "=r" (errno), "=@ccc" (err)
> : "Y" (input), "i" (-EIO));
>
> This would make "err" immediately testable. However, it also might make
> gcc generate extra code to save and restore err, since it wouldn't
> understand the invariant that err = !!errno.

Yeah, this wouldn't be as good as asm goto.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/