Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752813AbeADSSz (ORCPT + 1 other); Thu, 4 Jan 2018 13:18:55 -0500 Received: from mga06.intel.com ([134.134.136.31]:8418 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752615AbeADSSG (ORCPT ); Thu, 4 Jan 2018 13:18:06 -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,315,1511856000"; d="scan'208";a="21446984" From: Tim Chen To: Thomas Gleixner , Andy Lutomirski , Linus Torvalds , Greg KH Cc: Tim Chen , Dave Hansen , Andrea Arcangeli , Andi Kleen , Arjan Van De Ven , linux-kernel@vger.kernel.org Subject: [PATCH 4/7] x86/idle: Disable IBRS entering idle and enable it on wakeup Date: Thu, 4 Jan 2018 09:56:45 -0800 Message-Id: <50b92931dd3cd403d60e69533f5583bbdbbb88d0.1515086770.git.tim.c.chen@linux.intel.com> X-Mailer: git-send-email 2.9.4 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: Clear IBRS on idle entry and set it on idle exit into kernel on mwait. When we are in mwait, we are not running but if we leave IBRS on, it will affect the performance on the sibling hardware thread. So we disable IBRS and reenable it when we wake up. Modify my original implementation of IBRS on mwait idle path based on input from Andrea Arcangeli. Signed-off-by: Tim Chen --- arch/x86/include/asm/mwait.h | 19 +++++++++++++++++++ arch/x86/include/asm/spec_ctrl.h | 37 +++++++++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 9 +++++++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 39a2fb2..5cb3bff 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -6,6 +6,7 @@ #include #include +#include #define MWAIT_SUBSTATE_MASK 0xf #define MWAIT_CSTATE_MASK 0xf @@ -100,15 +101,33 @@ static inline void __sti_mwait(unsigned long eax, unsigned long ecx) static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) { if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) { + bool can_toggle_ibrs = false; if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) { mb(); clflush((void *)¤t_thread_info()->flags); mb(); } + if (irqs_disabled()) { + /* + * CPUs run faster with speculation protection + * disabled. All CPU threads in a core must + * disable speculation protection for it to be + * disabled. Disable it while we are idle so the + * other hyperthread can run fast. + * + * nmi uses the save_paranoid model which + * always enables ibrs on exception entry + * before any indirect jump can run. + */ + can_toggle_ibrs = true; + unprotected_speculation_begin(); + } __monitor((void *)¤t_thread_info()->flags, 0, 0); if (!need_resched()) __mwait(eax, ecx); + if (can_toggle_ibrs) + unprotected_speculation_end(); } current_clr_polling(); } diff --git a/arch/x86/include/asm/spec_ctrl.h b/arch/x86/include/asm/spec_ctrl.h index 16fc4f58..28b0314 100644 --- a/arch/x86/include/asm/spec_ctrl.h +++ b/arch/x86/include/asm/spec_ctrl.h @@ -76,5 +76,42 @@ 10: .endm +#else +#include + +static inline void __disable_indirect_speculation(void) +{ + native_wrmsrl(MSR_IA32_SPEC_CTRL, SPEC_CTRL_FEATURE_ENABLE_IBRS); +} + +static inline void __enable_indirect_speculation(void) +{ + native_wrmsrl(MSR_IA32_SPEC_CTRL, SPEC_CTRL_FEATURE_DISABLE_IBRS); +} + +/* + * Interrupts must be disabled to begin unprotected speculation. + * Otherwise interrupts could be running in unprotected mode. + */ +static inline void unprotected_speculation_begin(void) +{ + WARN_ON_ONCE(!irqs_disabled()); + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL)) + __enable_indirect_speculation(); +} + +static inline void unprotected_speculation_end(void) +{ + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL)) + __disable_indirect_speculation(); + else + /* + * If we intended to disable indirect speculation + * but come here due to mis-speculation, we need + * to stop the mis-speculation with rmb. + */ + rmb(); +} + #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_SPEC_CTRL_H */ diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 5174159..cb14820 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -39,6 +39,7 @@ #include #include #include +#include /* * per-CPU TSS segments. Threads are completely 'soft' on Linux, @@ -461,11 +462,15 @@ static __cpuidle void mwait_idle(void) mb(); /* quirk */ } + unprotected_speculation_begin(); __monitor((void *)¤t_thread_info()->flags, 0, 0); - if (!need_resched()) + if (!need_resched()) { __sti_mwait(0, 0); - else + unprotected_speculation_end(); + } else { + unprotected_speculation_end(); local_irq_enable(); + } trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id()); } else { local_irq_enable(); -- 2.9.4