x86/cpu/AMD: Make LFENCE a serializing instruction on AMD
cover x86/cpu/AMD: Make LFENCE a serializing instruction on AMD
To aid in speculation control, the LFENCE instruction will be turned into a serializing instruction. There is less performance impact using LFENCE in this way compared to MFENCE.
With LFENCE now being a serializing instruction, it can be used in rdtsc_ordered() in place of MFENCE_RDTSC. The other two patches in this series make this change and remove the MFENCE_RDTSC feature.
The following patches are included in this series:
- Make LFENCE a serializing instruction on AMD
- Change over to LFENCE_RDTSC from MFENCE_RDTSC on AMD
- Remove the MFENCE_RDTSC feature
This patch series is based on tip:x86/pti.
---
Tom Lendacky (3):
x86/cpu/AMD: Make LFENCE a serializing instruction x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC x86/msr: Remove now unused definition of MFENCE_RDTSC feature
arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/include/asm/msr.h | 3 +-- arch/x86/kernel/cpu/amd.c | 13 +++++++++++-- 4 files changed, 15 insertions(+), 5 deletions(-)
Signature · 1 line
Tom Lendacky
PATCH 1/3 x86/cpu/AMD: Make LFENCE a serializing instruction
To aid in speculation control, make LFENCE a serializing instruction. This is done by setting bit 1 of MSR 0xc0011029 (DE_CFG). Some families that support LFENCE do not have this MSR. For these families, the LFENCE instruction is already serializing.
--- arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kernel/cpu/amd.c | 9 +++++++++ 2 files changed, 11 insertions(+)
arch/x86/include/asm/msr-index.h +2 -0
@@ -352,6 +352,8 @@#define FAM10H_MMIO_CONF_BASE_MASK 0xfffffffULL#define FAM10H_MMIO_CONF_BASE_SHIFT 20#define MSR_FAM10H_NODE_ID 0xc001100c +#define MSR_F10H_DECFG 0xc0011029 +#define MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT 1/* K8 MSRs */ #define MSR_K8_TOP_MEM1 0xc001001a
arch/x86/kernel/cpu/amd.c +9 -0
@@ -829,6 +829,15 @@ static void init_amd(struct cpuinfo_x86 *c)set_cpu_cap (c,X86_FEATURE_K8 );if (cpu_has (c,X86_FEATURE_XMM2 )) { +/* +* Use LFENCE for execution serialization. On families which +* don't have that MSR, LFENCE is already serializing. +* msr_set_bit() uses the safe accessors, too, even if the MSR +* is not present. +*/ +msr_set_bit (MSR_F10H_DECFG , +MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT ); +/* MFENCE stops RDTSC speculation */ set_cpu_cap (c,X86_FEATURE_MFENCE_RDTSC ); }
Re: [PATCH v1 1/3] x86/cpu/AMD: Make LFENCE a serializing instruction
Quoted 4 lines · Tom Lendacky
To aid in speculation control, make LFENCE a serializing instruction. This is done by setting bit 1 of MSR
0xc0011029(DE_CFG). Some families that support LFENCE do not have this MSR. For these families, the LFENCE instruction is already serializing.
Does this require a microcode update?
--
Brian Gerst
Re: [PATCH v1 1/3] x86/cpu/AMD: Make LFENCE a serializing instruction
Quoted 7 lines · Brian Gerst
Quoted 4 lines · Tom Lendacky
To aid in speculation control, make LFENCE a serializing instruction. This is done by setting bit 1 of MSR
0xc0011029(DE_CFG). Some families that support LFENCE do not have this MSR. For these families, the LFENCE instruction is already serializing.Does this require a microcode update?
No, it doesn't.
Thanks,
Tom
Quoted 4 lines
--
Brian Gerst
tip-bot for Tom Lendacky
merge bot[tip:x86/pti] x86/cpu/AMD: Make LFENCE a serializing instruction
In reply to Tom Lendackyx86/cpu/AMD: Make LFENCE a serializing instruction
Commit-ID: 0592b0bce1694957fed178fc52f4b11576714b07 Gitweb: https://git.kernel.org/tip/0592b0bce1694957fed178fc52f4b11576714b07 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:46 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100
x86/cpu/AMD: Make LFENCE a serializing instruction
To aid in speculation control, make LFENCE a serializing instruction. This is done by setting bit 1 of MSR 0xc0011029 (DE_CFG). Some families that support LFENCE do not have this MSR. For these families, the LFENCE instruction is already serializing.
--- arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kernel/cpu/amd.c | 9 +++++++++ 2 files changed, 11 insertions(+)
arch/x86/include/asm/msr-index.h +2 -0
@@ -352,6 +352,8 @@#define FAM10H_MMIO_CONF_BASE_MASK 0xfffffffULL#define FAM10H_MMIO_CONF_BASE_SHIFT 20#define MSR_FAM10H_NODE_ID 0xc001100c +#define MSR_F10H_DECFG 0xc0011029 +#define MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT 1/* K8 MSRs */ #define MSR_K8_TOP_MEM1 0xc001001a
arch/x86/kernel/cpu/amd.c +9 -0
@@ -829,6 +829,15 @@ static void init_amd(struct cpuinfo_x86 *c)set_cpu_cap (c,X86_FEATURE_K8 );if (cpu_has (c,X86_FEATURE_XMM2 )) { +/* +* Use LFENCE for execution serialization. On families which +* don't have that MSR, LFENCE is already serializing. +* msr_set_bit() uses the safe accessors, too, even if the MSR +* is not present. +*/ +msr_set_bit (MSR_F10H_DECFG , +MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT ); +/* MFENCE stops RDTSC speculation */ set_cpu_cap (c,X86_FEATURE_MFENCE_RDTSC ); }
PATCH 2/3 x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
In reply to Tom LendackyWith LFENCE now a serializing instruction, set the LFENCE_RDTSC feature since the LFENCE instruction has less overhead than the MFENCE instruction.
--- arch/x86/kernel/cpu/amd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/amd.c +2 -2
@@ -838,8 +838,8 @@ static void init_amd(struct cpuinfo_x86 *c)msr_set_bit (MSR_F10H_DECFG ,MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT ); -/* MFENCE stops RDTSC speculation */ -set_cpu_cap (c,X86_FEATURE_MFENCE_RDTSC ); +/* A serializing LFENCE stops RDTSC speculation */ +set_cpu_cap (c,X86_FEATURE_LFENCE_RDTSC ); }/*
tip-bot for Tom Lendacky
merge bot[tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100
x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
With LFENCE now a serializing instruction, set the LFENCE_RDTSC feature since the LFENCE instruction has less overhead than the MFENCE instruction.
--- arch/x86/kernel/cpu/amd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/amd.c +2 -2
@@ -838,8 +838,8 @@ static void init_amd(struct cpuinfo_x86 *c)msr_set_bit (MSR_F10H_DECFG ,MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT ); -/* MFENCE stops RDTSC speculation */ -set_cpu_cap (c,X86_FEATURE_MFENCE_RDTSC ); +/* A serializing LFENCE stops RDTSC speculation */ +set_cpu_cap (c,X86_FEATURE_LFENCE_RDTSC ); }/*
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 7 lines · tip-bot for Tom Lendacky
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100
Quoted 5 lines
x86/cpu/AMD: Use
LFENCE_RDTSCinstead ofMFENCE_RDTSCWith LFENCE now a serializing instruction, set the
LFENCE_RDTSCfeature since the LFENCE instruction has less overhead than the MFENCE instruction.
Second thoughts on that. As pointed out by someone in one of the insane long threads:
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing MSR_AMD64_DE_CFG
That would bring the guest into a pretty bad state or am I missing something essential here?
I'm dropping these patches until this question is answered.
Thanks,
tglx
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 1 line · Thomas Gleixner
I'm dropping these patches until this question is answered.
I've rebased my retpoline tree on top of tip/x86/pti from before those patches (from my BUG_SPECTRE_Vx patch).
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 5 lines · David
Quoted 1 line · Thomas Gleixner
I'm dropping these patches until this question is answered.
I've rebased my retpoline tree on top of tip/x86/pti from before those patches (from my
BUG_SPECTRE_Vxpatch).
That's not a problem. They still apply
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
In reply to Thomas GleixnerQuoted 17 lines · Thomas Gleixner
Quoted 7 lines · tip-bot for Tom Lendacky
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100Quoted 5 lines
x86/cpu/AMD: Use
LFENCE_RDTSCinstead ofMFENCE_RDTSCWith LFENCE now a serializing instruction, set the
LFENCE_RDTSCfeature since the LFENCE instruction has less overhead than the MFENCE instruction.Second thoughts on that. As pointed out by someone in one of the insane long threads:
Quoted 8 lines
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGThat would bring the guest into a pretty bad state or am I missing something essential here?
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
~Andrew
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 27 lines · Andrew Cooper
Quoted 17 lines · Thomas Gleixner
Quoted 7 lines · tip-bot for Tom Lendacky
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100Quoted 5 lines
x86/cpu/AMD: Use
LFENCE_RDTSCinstead ofMFENCE_RDTSCWith LFENCE now a serializing instruction, set the
LFENCE_RDTSCfeature since the LFENCE instruction has less overhead than the MFENCE instruction.Second thoughts on that. As pointed out by someone in one of the insane long threads:
Quoted 8 lines
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGThat would bring the guest into a pretty bad state or am I missing something essential here?
Quoted 6 lines
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
That still does not help vs. rdtsc_ordered() and LFENCE_RDTSC ...
Thanks,
tglx
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 29 lines · Thomas Gleixner
Quoted 27 lines · Andrew Cooper
Quoted 25 lines · Thomas Gleixner
Quoted 7 lines · tip-bot for Tom Lendacky
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100Quoted 5 lines
x86/cpu/AMD: Use
LFENCE_RDTSCinstead ofMFENCE_RDTSCWith LFENCE now a serializing instruction, set the
LFENCE_RDTSCfeature since the LFENCE instruction has less overhead than the MFENCE instruction.Second thoughts on that. As pointed out by someone in one of the insane long threads:
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGThat would bring the guest into a pretty bad state or am I missing something essential here?
Quoted 6 lines
Quoted 6 lines
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
Correct, the retpoline will work, it just takes more cycles.
Quoted 2 lines
That still does not help vs.
rdtsc_ordered()andLFENCE_RDTSC...
Ok, I can add the read-back check before setting the feature flag(s).
But... what about the case where the guest is a different family than hypervisor? If we're on, say, a Fam15h hypervisor but the guest is started as a Fam0fh guest where the MSR doesn't exist and LFENCE is supposed to be serialized? I'll have to do a rdmsr_safe() and only set the flag(s) if I can successfully read the MSR back and validate the bit.
Thanks,
Tom
Quoted 5 lines
Thanks,
tglx
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 8 lines
Ok, I can add the read-back check before setting the feature flag(s).
But... what about the case where the guest is a different family than hypervisor? If we're on, say, a Fam15h hypervisor but the guest is started as a Fam0fh guest where the MSR doesn't exist and LFENCE is supposed to be serialized? I'll have to do a
rdmsr_safe()and only set the flag(s) if I can successfully read the MSR back and validate the bit.
At some point, if the hypervisor is lying to you about what the CPU is, and you can't check empirically, you have to admit defeat :)
Signature · 1 line
dwmw2
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
In reply to Tom LendackyQuoted 25 lines · Tom Lendacky
Quoted 12 lines · Thomas Gleixner
Quoted 12 lines
Quoted 11 lines
Second thoughts on that. As pointed out by someone in one of the insane long threads:
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGThat would bring the guest into a pretty bad state or am I missing something essential here?
Quoted 6 lines
Quoted 6 lines
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
Correct, the retpoline will work, it just takes more cycles.
Quoted 2 lines
That still does not help vs.
rdtsc_ordered()andLFENCE_RDTSC...
Quoted 7 lines
Ok, I can add the read-back check before setting the feature flag(s).
But... what about the case where the guest is a different family than hypervisor? If we're on, say, a Fam15h hypervisor but the guest is started as a Fam0fh guest where the MSR doesn't exist and LFENCE is supposed to be serialized? I'll have to do a
rdmsr_safe()and only set the flag(s) if I can successfully read the MSR back and validate the bit.
But that still does not make this patch correct and neither the next one.
If you cannot set the flag and you cannot prove that you run on bare metal, then you still need the MFENCE_RDTSC variant as you have no idea what the underlying hypervisor is and how it has the LFENCE configured.
Thanks,
tglx
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 34 lines · Thomas Gleixner
Quoted 25 lines · Tom Lendacky
Quoted 12 lines · Thomas Gleixner
Quoted 12 lines
Quoted 11 lines
Second thoughts on that. As pointed out by someone in one of the insane long threads:
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGThat would bring the guest into a pretty bad state or am I missing something essential here?
Quoted 6 lines
Quoted 6 lines
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
Correct, the retpoline will work, it just takes more cycles.
Quoted 2 lines
That still does not help vs.
rdtsc_ordered()andLFENCE_RDTSC...Quoted 7 lines
Ok, I can add the read-back check before setting the feature flag(s).
But... what about the case where the guest is a different family than hypervisor? If we're on, say, a Fam15h hypervisor but the guest is started as a Fam0fh guest where the MSR doesn't exist and LFENCE is supposed to be serialized? I'll have to do a
rdmsr_safe()and only set the flag(s) if I can successfully read the MSR back and validate the bit.
Quoted 5 lines
But that still does not make this patch correct and neither the next one.
If you cannot set the flag and you cannot prove that you run on bare metal, then you still need the
MFENCE_RDTSCvariant as you have no idea what the underlying hypervisor is and how it has the LFENCE configured.
So now I'm also concerned about setting the retpoline method and using LFENCE as the speculation barrier. If we go back to the original statement:
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing MSR_AMD64_DE_CFG
It looks like I'll need to attempt to write MSR_AMD64_DE_CFG and then read it back checking for whether MSR_F10H_DECFG_LFENCE_SERIALIZE has been set. If the MSR can be read and the bit is set, then I can set RETPOLINE_AMD (once those patches go in) and LFENCE_RDTSC. Otherwise, set (only) MFENCE_RDTSC. This also means we need to use MFENCE as the speculation barrier instead of LFENCE in this situation (another alternative added to the __nospec_barrier() implementation).
Does that sound right? Or does the "cannot prove that you run on bare metal" mess this all up?
Thanks,
Tom
Quoted 5 lines
Thanks,
tglx
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 15 lines · Tom Lendacky
So now I'm also concerned about setting the retpoline method and using LFENCE as the speculation barrier. If we go back to the original statement:
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writingMSR_AMD64_DE_CFGIt looks like I'll need to attempt to write
MSR_AMD64_DE_CFGand then read it back checking for whetherMSR_F10H_DECFG_LFENCE_SERIALIZEhas been set. If the MSR can be read and the bit is set, then I can setRETPOLINE_AMD(once those patches go in) andLFENCE_RDTSC. Otherwise, set (only)MFENCE_RDTSC. This also means we need to use MFENCE as the speculation barrier instead of LFENCE in this situation (another alternative added to the__nospec_barrier()implementation).
Quoted 2 lines
Does that sound right? Or does the "cannot prove that you run on bare metal" mess this all up?
I think that covers it. If the hypervisor would be trapping the read and then lying on the bit being set while it's not, that's really nothing you should care about at all.
Thanks,
tglx
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
In reply to Tom LendackyQuoted 37 lines · Tom Lendacky
Quoted 26 lines · Thomas Gleixner
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Thomas Gleixner
Quoted 7 lines · tip-bot for Tom Lendacky
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100Quoted 5 lines
x86/cpu/AMD: Use
LFENCE_RDTSCinstead ofMFENCE_RDTSCWith LFENCE now a serializing instruction, set the
LFENCE_RDTSCfeature since the LFENCE instruction has less overhead than the MFENCE instruction.Second thoughts on that. As pointed out by someone in one of the insane long threads:
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGQuoted 8 lines
Quoted 8 lines
Quoted 2 lines
That would bring the guest into a pretty bad state or am I missing something essential here?
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
Correct, the retpoline will work, it just takes more cycles.
Quoted 8 lines
Quoted 1 line
That still does not help vs.
rdtsc_ordered()andLFENCE_RDTSC...Ok, I can add the read-back check before setting the feature flag(s).
But... what about the case where the guest is a different family than hypervisor? If we're on, say, a Fam15h hypervisor but the guest is started as a Fam0fh guest where the MSR doesn't exist and LFENCE is supposed to be serialized? I'll have to do a
rdmsr_safe()and only set the flag(s) if I can successfully read the MSR back and validate the bit.
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
The user can get to keep both pieces if they've decided that this was a good thing to try.
~Andrew
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 47 lines · * Andrew Cooper ([email protected]) wrote:
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Thomas Gleixner
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Thomas Gleixner
Quoted 7 lines · tip-bot for Tom Lendacky
Commit-ID: 0bf17c102177d5da9363bf8b1e4704b9996d5079 Gitweb: https://git.kernel.org/tip/0bf17c102177d5da9363bf8b1e4704b9996d5079 Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:07:56 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:40 +0100Quoted 5 lines
x86/cpu/AMD: Use
LFENCE_RDTSCinstead ofMFENCE_RDTSCWith LFENCE now a serializing instruction, set the
LFENCE_RDTSCfeature since the LFENCE instruction has less overhead than the MFENCE instruction.Second thoughts on that. As pointed out by someone in one of the insane long threads:
What happens if the kernel runs as a guest and
- the hypervisor did not set the LFENCE to serializing on the host
- the hypervisor does not allow writing
MSR_AMD64_DE_CFGQuoted 8 lines
Quoted 8 lines
Quoted 2 lines
That would bring the guest into a pretty bad state or am I missing something essential here?
What I did in Xen was to attempt to set it, then read it back and see. If LFENCE still isn't serialising, using repoline is the only available mitigation.
My understanding from the folk at AMD is that retpoline is safe to use, but has higher overhead than the LFENCE approach.
Correct, the retpoline will work, it just takes more cycles.
Quoted 8 lines
Quoted 1 line
That still does not help vs.
rdtsc_ordered()andLFENCE_RDTSC...Ok, I can add the read-back check before setting the feature flag(s).
But... what about the case where the guest is a different family than hypervisor? If we're on, say, a Fam15h hypervisor but the guest is started as a Fam0fh guest where the MSR doesn't exist and LFENCE is supposed to be serialized? I'll have to do a
rdmsr_safe()and only set the flag(s) if I can successfully read the MSR back and validate the bit.
Quoted 3 lines
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
It's not that an unusual thing to do on qemu/kvm - to specify the lowest common denominator of the set of CPUs in your data centre (for any one vendor); it does tend to get some weird combinations.
Dave
Quoted 4 lines
The user can get to keep both pieces if they've decided that this was a good thing to try.
~Andrew
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
It's not that an unusual thing to do on qemu/kvm - to specify the lowest common denominator of the set of CPUs in your data centre (for any one vendor); it does tend to get some weird combinations.
Agreed. But on a hypervisor we pretty much know that:
- the MSR_AMD64_DE_CFG doesn't exist unless you have a fix
- setting the MSR_AMD64_DE_CFG bit to 1 if you have a fix can be done independent of the family
So all KVM needs is a X86_FEATURE_LFENCE_SERIALIZE, it doesn't matter if it's because of the family or because Linux has set MSR_F10H_DE_CFG. The guest will either try setting the MSR bit and #GP, or it will find it already set and do nothing.
Of course no code for this has been written yet.
Paolo
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
It's not that an unusual thing to do on qemu/kvm - to specify the lowest common denominator of the set of CPUs in your data centre (for any one vendor); it does tend to get some weird combinations.
Agreed. But on a hypervisor we pretty much know that:
- the
MSR_AMD64_DE_CFGdoesn't exist unless you have a fix
Not sure what you mean by this... the MSR exists today on many families.
Thanks,
Tom
Quoted 13 lines
- setting the
MSR_AMD64_DE_CFGbit to 1 if you have a fix can be done independent of the familySo all KVM needs is a
X86_FEATURE_LFENCE_SERIALIZE, it doesn't matter if it's because of the family or because Linux has setMSR_F10H_DE_CFG. The guest will either try setting the MSR bit and #GP, or it will find it already set and do nothing.Of course no code for this has been written yet.
Paolo
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 9 lines · Tom Lendacky
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
It's not that an unusual thing to do on qemu/kvm - to specify the lowest common denominator of the set of CPUs in your data centre (for any one vendor); it does tend to get some weird combinations.
Quoted 5 lines
Quoted 3 lines
Agreed. But on a hypervisor we pretty much know that:
- the
MSR_AMD64_DE_CFGdoesn't exist unless you have a fixNot sure what you mean by this... the MSR exists today on many families.
But the hypervisor either should not expose it at all to the guest, or if it does, it should not allow setting that bit to 1.
Paolo
Quoted 16 lines
Thanks,
TomQuoted 13 lines
- setting the
MSR_AMD64_DE_CFGbit to 1 if you have a fix can be done independent of the familySo all KVM needs is a
X86_FEATURE_LFENCE_SERIALIZE, it doesn't matter if it's because of the family or because Linux has setMSR_F10H_DE_CFG. The guest will either try setting the MSR bit and #GP, or it will find it already set and do nothing.Of course no code for this has been written yet.
Paolo
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
In reply to Paolo BonziniQuoted 15 lines · Paolo Bonzini
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
It's not that an unusual thing to do on qemu/kvm - to specify the lowest common denominator of the set of CPUs in your data centre (for any one vendor); it does tend to get some weird combinations.
Agreed. But on a hypervisor we pretty much know that:
- the
MSR_AMD64_DE_CFGdoesn't exist unless you have a fix- setting the
MSR_AMD64_DE_CFGbit to 1 if you have a fix can be done independent of the family
Quoted 7 lines
So all KVM needs is a
X86_FEATURE_LFENCE_SERIALIZE, it doesn't matter if it's because of the family or because Linux has setMSR_F10H_DE_CFG. The guest will either try setting the MSR bit and #GP, or it will find it already set and do nothing.Of course no code for this has been written yet.
Hi Paolo,
What would be the best way to approach the MSR support? I was thinking of just recognizing a write to that MSR but not actually doing anything and, on read, just returning a value with the single bit set if LFENCE is serializing and not worrying about the full contents of the MSR. Or I could save the value so that it could also be host initiated and only allow the LFENCE serialization bit to be set if the LFENCE_RDTSC feature is enabled.
Thanks,
Tom
Quoted 2 lines
Paolo
Re: [tip:x86/pti] x86/cpu/AMD: Use LFENCE_RDTSC instead of MFENCE_RDTSC
Quoted 26 lines · Tom Lendacky
Quoted 15 lines · Paolo Bonzini
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines
If your hypervisor is lying to you about the primary family, then all bets are off. I don't expect there will be any production systems doing this.
It's not that an unusual thing to do on qemu/kvm - to specify the lowest common denominator of the set of CPUs in your data centre (for any one vendor); it does tend to get some weird combinations.
Agreed. But on a hypervisor we pretty much know that:
- the
MSR_AMD64_DE_CFGdoesn't exist unless you have a fix- setting the
MSR_AMD64_DE_CFGbit to 1 if you have a fix can be done independent of the familyQuoted 7 lines
So all KVM needs is a
X86_FEATURE_LFENCE_SERIALIZE, it doesn't matter if it's because of the family or because Linux has setMSR_F10H_DE_CFG. The guest will either try setting the MSR bit and #GP, or it will find it already set and do nothing.Of course no code for this has been written yet.
Hi Paolo,
Quoted 7 lines
What would be the best way to approach the MSR support? I was thinking of just recognizing a write to that MSR but not actually doing anything and, on read, just returning a value with the single bit set if LFENCE is serializing and not worrying about the full contents of the MSR. Or I could save the value so that it could also be host initiated and only allow the LFENCE serialization bit to be set if the
LFENCE_RDTSCfeature is enabled.
Yes, the latter is the correct one. We'll need changes in QEMU to add a new feature bit in "-cpu" too. The "-cpu" feature bit, if set, causes QEMU to set the bit in the MSR at CPU creation time. MSR-based features are not yet a thing in QEMU, but we were planning to add them before this whole kerfuffle started.
But indeed we need to return also whether the feature is supported on the host, which would be similar to the first part (read-only, just returning a value with the single bit set is LFENCE is serializing). We would add KVM_GET_MSRS and KVM_GET_MSR_INDEX_LIST ioctls on the VM file descriptor for that, and a new capability KVM_CAP_GET_HOST_MSR (or KVM_CAP_GET_MSR_VM, you choose :)). QEMU can use these two ioctls to query the available MSR-based CPU features. These can include microcode version, VMX features, LFENCE serialization, IA32_ARCH_FACILITIES, etc.
Thanks,
Paolo
PATCH 3/3 x86/msr: Remove now unused definition of MFENCE_RDTSC feature
In reply to Tom LendackyWith the switch to using LFENCE_RDTSC on AMD platforms there is no longer a need for the MFENCE_RDTSC feature. Remove its usage and definition.
--- arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/msr.h | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -1
@@ -96,7 +96,7 @@#define X86_FEATURE_SYSCALL32 ( 3*32+14)/* "" syscall in IA32 userspace */ #define X86_FEATURE_SYSENTER32 ( 3*32+15)/* "" sysenter in IA32 userspace */ #define X86_FEATURE_REP_GOOD ( 3*32+16)/* REP microcode works well */ -#define X86_FEATURE_MFENCE_RDTSC ( 3*32+17)/* "" MFENCE synchronizes RDTSC */ +/* free, was: #define X86_FEATURE_MFENCE_RDTSC ( 3*32+17) "" MFENCE synchronizes RDTSC */ #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18)/* "" LFENCE synchronizes RDTSC */ #define X86_FEATURE_ACC_POWER ( 3*32+19)/* AMD Accumulated Power Mechanism */ #define X86_FEATURE_NOPL ( 3*32+20)/* The NOPL (0F 1F) instructions */
arch/x86/include/asm/msr.h +1 -2
@@ -214,8 +214,7 @@ static __always_inline unsigned long long rdtsc_ordered(void)* that some other imaginary CPU is updating continuously with a * time stamp. */ -alternative_2 ("" ,"mfence" ,X86_FEATURE_MFENCE_RDTSC , -"lfence" ,X86_FEATURE_LFENCE_RDTSC ); +alternative ("" ,"lfence" ,X86_FEATURE_LFENCE_RDTSC );return rdtsc (); }
tip-bot for Tom Lendacky
merge bot[tip:x86/pti] x86/msr: Remove now unused definition of MFENCE_RDTSC feature
x86/msr: Remove now unused definition of MFENCE_RDTSC feature
Commit-ID: eeab3eee2fa4a8e8eb52e2abf034f14f1d010e0d Gitweb: https://git.kernel.org/tip/eeab3eee2fa4a8e8eb52e2abf034f14f1d010e0d Author: Tom Lendacky <[email protected]> AuthorDate: Fri, 5 Jan 2018 10:08:05 -0600 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 6 Jan 2018 21:57:41 +0100
x86/msr: Remove now unused definition of MFENCE_RDTSC feature
With the switch to using LFENCE_RDTSC on AMD platforms there is no longer a need for the MFENCE_RDTSC feature. Remove its usage and definition.
--- arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/msr.h | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -1
@@ -96,7 +96,7 @@#define X86_FEATURE_SYSCALL32 ( 3*32+14)/* "" syscall in IA32 userspace */ #define X86_FEATURE_SYSENTER32 ( 3*32+15)/* "" sysenter in IA32 userspace */ #define X86_FEATURE_REP_GOOD ( 3*32+16)/* REP microcode works well */ -#define X86_FEATURE_MFENCE_RDTSC ( 3*32+17)/* "" MFENCE synchronizes RDTSC */ +/* free, was: #define X86_FEATURE_MFENCE_RDTSC ( 3*32+17) "" MFENCE synchronizes RDTSC */ #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18)/* "" LFENCE synchronizes RDTSC */ #define X86_FEATURE_ACC_POWER ( 3*32+19)/* AMD Accumulated Power Mechanism */ #define X86_FEATURE_NOPL ( 3*32+20)/* The NOPL (0F 1F) instructions */
arch/x86/include/asm/msr.h +1 -2
@@ -214,8 +214,7 @@ static __always_inline unsigned long long rdtsc_ordered(void)* that some other imaginary CPU is updating continuously with a * time stamp. */ -alternative_2 ("" ,"mfence" ,X86_FEATURE_MFENCE_RDTSC , -"lfence" ,X86_FEATURE_LFENCE_RDTSC ); +alternative ("" ,"lfence" ,X86_FEATURE_LFENCE_RDTSC );return rdtsc (); }
Quoted 13 lines · Tom Lendacky
To aid in speculation control, the LFENCE instruction will be turned into a serializing instruction. There is less performance impact using LFENCE in this way compared to MFENCE.
With LFENCE now being a serializing instruction, it can be used in
rdtsc_ordered()in place ofMFENCE_RDTSC. The other two patches in this series make this change and remove theMFENCE_RDTSCfeature.The following patches are included in this series:
- Make LFENCE a serializing instruction on AMD
- Change over toLFENCE_RDTSCfromMFENCE_RDTSCon AMD
- Remove theMFENCE_RDTSCfeature
Quoted 8 lines
This patch series is based on tip:x86/pti.
---
Tom Lendacky (3):
x86/cpu/AMD: Make LFENCE a serializing instruction x86/cpu/AMD: UseLFENCE_RDTSCinstead ofMFENCE_RDTSCx86/msr: Remove now unused definition ofMFENCE_RDTSCfeature
All three:
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.