Re: [RFC PATCH 4/4] x86/TSC: Use RDTSCP

From: Borislav Petkov
Date: Wed Dec 12 2018 - 05:08:20 EST


On Tue, Dec 11, 2018 at 06:24:44PM -0800, Andy Lutomirski wrote:
> This makes me nervous, since no one knows what âserializingâ means.

Why no one? If you wanna say that X86_FEATURE_LFENCE_SERIALIZING is not
really telling, so is X86_FEATURE_LFENCE_RDTSC, TBH. :)

> IIRC AMD specifically documents that MFENCE is required before RDTSC
> to get sensible ordering. So itâs entirely plausible to me that
> LFENCE is okay for Spectre mitigation but MFENCE is needed for RDTSC
> on some CPU.

Look at init_amd(), the if (cpu_has(c, X86_FEATURE_XMM2)) branch where
we make LFENCE serializing. The logic with the new names would be:


asm volatile(ALTERNATIVE_3("rdtsc",
"mfence; rdtsc", X86_FEATURE_MFENCE,
"lfence; rdtsc", X86_FEATURE_LFENCE_SERIALIZING,
"rdtscp", X86_FEATURE_RDTSCP)

RDTSC is put there during build. At boot time:

if CPU has MFENCE
use MFENCE to stop RDTSC speculation
if LFENCE is serializing
use LFENCE...
if CPU has RDTSCP
even better, use that as it is the fastest or on par.

Of course the order of those is important.

Ok?

--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.