Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753728AbeAFBSj (ORCPT + 1 other); Fri, 5 Jan 2018 20:18:39 -0500 Received: from mga07.intel.com ([134.134.136.100]:38624 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753456AbeAFBSg (ORCPT ); Fri, 5 Jan 2018 20:18:36 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,320,1511856000"; d="scan'208";a="17138207" Subject: [PATCH 05/18] x86: implement nospec_barrier() From: Dan Williams To: linux-kernel@vger.kernel.org Cc: Mark Rutland , linux-arch@vger.kernel.org, peterz@infradead.org, gregkh@linuxfoundation.org, torvalds@linux-foundation.org, netdev@vger.kernel.org, Alan Cox , tglx@linutronix.de, Arjan van de Ven , Elena Reshetova , alan@linux.intel.com Date: Fri, 05 Jan 2018 17:10:21 -0800 Message-ID: <151520102116.32271.8160376567325482844.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <151520099201.32271.4677179499894422956.stgit@dwillia2-desk3.amr.corp.intel.com> References: <151520099201.32271.4677179499894422956.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: The new speculative execution barrier, nospec_barrier(), ensures that any userspace controllable speculation doesn't cross the boundary. Any user observable speculative activity on this CPU thread before this point either completes, reaches a state it can no longer cause an observable activity, or is aborted before instructions after the barrier execute. In the x86 case nospec_barrier() resolves to an lfence if X86_FEATURE_LFENCE_RDTSC is present. Other architectures can define their variants. Note the expectation is that this barrier is never used directly, at least outside of architecture specific code. It is implied by the nospec_{array_ptr,ptr} macros. x86, for now, depends on the barrier for protection while other architectures place their speculation prevention in nospec_{ptr,array_ptr} when a barrier instruction is not available or too heavy-weight. In the x86 case lfence is not a fully serializing instruction so it is not as expensive as other barriers. Suggested-by: Peter Zijlstra Suggested-by: Arjan van de Ven Suggested-by: Alan Cox Cc: Mark Rutland Cc: Greg KH Cc: Thomas Gleixner Cc: Alan Cox Signed-off-by: Elena Reshetova Signed-off-by: Dan Williams --- arch/x86/include/asm/barrier.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h index 7fb336210e1b..1148cd9f5ae7 100644 --- a/arch/x86/include/asm/barrier.h +++ b/arch/x86/include/asm/barrier.h @@ -24,6 +24,12 @@ #define wmb() asm volatile("sfence" ::: "memory") #endif +/* + * CPUs without LFENCE don't really speculate much. Possibly fall back to IRET-to-self. + */ +#define __nospec_barrier() alternative("", "lfence", X86_FEATURE_LFENCE_RDTSC) +#define nospec_barrier __nospec_barrier + #ifdef CONFIG_X86_PPRO_FENCE #define dma_rmb() rmb() #else