Re: [PATCH] x86/memcpy: Introduce memcpy_mcsafe_fast

From: Andy Lutomirski
Date: Sat Apr 18 2020 - 16:30:13 EST




--Andy

> On Apr 18, 2020, at 12:42 PM, Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
>>> On Fri, Apr 17, 2020 at 5:12 PM Dan Williams <dan.j.williams@xxxxxxxxx> wrote:
>>>
>>> @@ -106,12 +108,10 @@ static __always_inline __must_check unsigned long
>>> memcpy_mcsafe(void *dst, const void *src, size_t cnt)
>>> {
>>> #ifdef CONFIG_X86_MCE
>>> - i(static_branch_unlikely(&mcsafe_key))
>>> - return __memcpy_mcsafe(dst, src, cnt);
>>> - else
>>> + if (static_branch_unlikely(&mcsafe_slow_key))
>>> + return memcpy_mcsafe_slow(dst, src, cnt);
>>> #endif
>>> - memcpy(dst, src, cnt);
>>> - return 0;
>>> + return memcpy_mcsafe_fast(dst, src, cnt);
>>> }
>
> It strikes me that I see no advantages to making this an inline function at all.
>
> Even for the good case - where it turns into just a memcpy because MCE
> is entirely disabled - it doesn't seem to matter.
>
> The only case that really helps is when the memcpy can be turned into
> a single access. Which - and I checked - does exist, with people doing
>
> r = memcpy_mcsafe(&sb_seq_count, &sb(wc)->seq_count, sizeof(uint64_t));
>
> to read a single 64-bit field which looks aligned to me.
>
> But that code is incredible garbage anyway, since even on a broken
> machine, there's no actual reason to use the slow variant for that
> whole access that I can tell. The macs-safe copy routines do not do
> anything worthwhile for a single access.

Maybe Iâm missing something obvious, but whatâs the alternative? The _mcsafe variants donât just avoid the REP mess â they also tell the kernel that this particular access is recoverable via extable. With a regular memory access, the CPU may not explode, but do_machine_check() will, at very best, OOPS, and even that requires a certain degree of optimism. A panic is more likely.