0275-x86-idle-Disable-IBRS-entering-idle-and-enable-it-on.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Tim Chen <[email protected]>
  3. Date: Mon, 6 Nov 2017 18:19:14 -0800
  4. Subject: [PATCH] x86/idle: Disable IBRS entering idle and enable it on wakeup
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5753
  9. CVE-2017-5715
  10. Clear IBRS on idle entry and set it on idle exit into kernel on mwait.
  11. Signed-off-by: Tim Chen <[email protected]>
  12. Signed-off-by: Andy Whitcroft <[email protected]>
  13. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  14. (cherry picked from commit 5521b04afda1d683c1ebad6c25c2529a88e6f061)
  15. Signed-off-by: Fabian Grünbichler <[email protected]>
  16. ---
  17. arch/x86/include/asm/mwait.h | 8 ++++++++
  18. arch/x86/kernel/process.c | 12 ++++++++++--
  19. arch/x86/lib/delay.c | 10 ++++++++++
  20. 3 files changed, 28 insertions(+), 2 deletions(-)
  21. diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h
  22. index bda3c27f0da0..f15120ada161 100644
  23. --- a/arch/x86/include/asm/mwait.h
  24. +++ b/arch/x86/include/asm/mwait.h
  25. @@ -5,6 +5,8 @@
  26. #include <linux/sched/idle.h>
  27. #include <asm/cpufeature.h>
  28. +#include <asm/spec_ctrl.h>
  29. +#include <asm/microcode.h>
  30. #define MWAIT_SUBSTATE_MASK 0xf
  31. #define MWAIT_CSTATE_MASK 0xf
  32. @@ -105,9 +107,15 @@ static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
  33. mb();
  34. }
  35. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
  36. + native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
  37. +
  38. __monitor((void *)&current_thread_info()->flags, 0, 0);
  39. if (!need_resched())
  40. __mwait(eax, ecx);
  41. +
  42. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
  43. + native_wrmsrl(MSR_IA32_SPEC_CTRL, FEATURE_ENABLE_IBRS);
  44. }
  45. current_clr_polling();
  46. }
  47. diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
  48. index 07e6218ad7d9..3adb3806a284 100644
  49. --- a/arch/x86/kernel/process.c
  50. +++ b/arch/x86/kernel/process.c
  51. @@ -447,11 +447,19 @@ static __cpuidle void mwait_idle(void)
  52. mb(); /* quirk */
  53. }
  54. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
  55. + native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
  56. +
  57. __monitor((void *)&current_thread_info()->flags, 0, 0);
  58. - if (!need_resched())
  59. + if (!need_resched()) {
  60. __sti_mwait(0, 0);
  61. - else
  62. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
  63. + native_wrmsrl(MSR_IA32_SPEC_CTRL, FEATURE_ENABLE_IBRS);
  64. + } else {
  65. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
  66. + native_wrmsrl(MSR_IA32_SPEC_CTRL, FEATURE_ENABLE_IBRS);
  67. local_irq_enable();
  68. + }
  69. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  70. } else {
  71. local_irq_enable();
  72. diff --git a/arch/x86/lib/delay.c b/arch/x86/lib/delay.c
  73. index cf2ac227c2ac..b088463973e4 100644
  74. --- a/arch/x86/lib/delay.c
  75. +++ b/arch/x86/lib/delay.c
  76. @@ -26,6 +26,8 @@
  77. # include <asm/smp.h>
  78. #endif
  79. +#define IBRS_DISABLE_THRESHOLD 1000
  80. +
  81. /* simple loop based delay: */
  82. static void delay_loop(unsigned long loops)
  83. {
  84. @@ -105,6 +107,10 @@ static void delay_mwaitx(unsigned long __loops)
  85. for (;;) {
  86. delay = min_t(u64, MWAITX_MAX_LOOPS, loops);
  87. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
  88. + (delay > IBRS_DISABLE_THRESHOLD))
  89. + native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
  90. +
  91. /*
  92. * Use cpu_tss_rw as a cacheline-aligned, seldomly
  93. * accessed per-cpu variable as the monitor target.
  94. @@ -118,6 +124,10 @@ static void delay_mwaitx(unsigned long __loops)
  95. */
  96. __mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
  97. + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
  98. + (delay > IBRS_DISABLE_THRESHOLD))
  99. + native_wrmsrl(MSR_IA32_SPEC_CTRL, FEATURE_ENABLE_IBRS);
  100. +
  101. end = rdtsc_ordered();
  102. if (loops <= end - start)
  103. --
  104. 2.14.2