1 From b253149b843f89cd300cbdbea27ce1f847506f99 Mon Sep 17 00:00:00 2001
2 From: Len Brown <len.brown@intel.com>
3 Date: Wed, 15 Jan 2014 00:37:34 -0500
4 Subject: sched/idle/x86: Restore mwait_idle() to fix boot hangs, to improve power savings and to improve performance
6 From: Len Brown <len.brown@intel.com>
8 commit b253149b843f89cd300cbdbea27ce1f847506f99 upstream.
10 In Linux-3.9 we removed the mwait_idle() loop:
12 69fb3676df33 ("x86 idle: remove mwait_idle() and "idle=mwait" cmdline param")
14 The reasoning was that modern machines should be sufficiently
15 happy during the boot process using the default_idle() HALT
16 loop, until cpuidle loads and either acpi_idle or intel_idle
17 invoke the newer MWAIT-with-hints idle loop.
19 But two machines reported problems:
21 1. Certain Core2-era machines support MWAIT-C1 and HALT only.
22 MWAIT-C1 is preferred for optimal power and performance.
23 But if they support just C1, cpuidle never loads and
24 so they use the boot-time default idle loop forever.
26 2. Some laptops will boot-hang if HALT is used,
27 but will boot successfully if MWAIT is used.
28 This appears to be a hidden assumption in BIOS SMI,
29 that is presumably valid on the proprietary OS
30 where the BIOS was validated.
32 https://bugzilla.kernel.org/show_bug.cgi?id=60770
34 So here we effectively revert the patch above, restoring
35 the mwait_idle() loop. However, we don't bother restoring
36 the idle=mwait cmdline parameter, since it appears to add
41 For 3.9, simply revert 69fb3676df
42 for 3.10, patch -F3 applies, fuzz needed due to __cpuinit use in
43 context For 3.11, 3.12, 3.13, this patch applies cleanly
45 Tested-by: Mike Galbraith <bitbucket@online.de>
46 Signed-off-by: Len Brown <len.brown@intel.com>
47 Acked-by: Mike Galbraith <bitbucket@online.de>
48 Cc: <stable@vger.kernel.org> # 3.9+
49 Cc: Borislav Petkov <bp@alien8.de>
50 Cc: H. Peter Anvin <hpa@zytor.com>
51 Cc: Ian Malone <ibmalone@gmail.com>
52 Cc: Josh Boyer <jwboyer@redhat.com>
53 Cc: Linus Torvalds <torvalds@linux-foundation.org>
54 Cc: Mike Galbraith <efault@gmx.de>
55 Cc: Peter Zijlstra <peterz@infradead.org>
56 Cc: Thomas Gleixner <tglx@linutronix.de>
57 Link: http://lkml.kernel.org/r/345254a551eb5a6a866e048d7ab570fd2193aca4.1389763084.git.len.brown@intel.com
58 [ Ported to recent kernels. ]
59 [ Mike: 3.10 backport ]
60 Signed-off-by: Ingo Molnar <mingo@kernel.org>
61 Signed-off-by: Mike Galbraith <efault@gmx.de>
62 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
64 arch/x86/kernel/process.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++
65 1 file changed, 49 insertions(+)
67 --- a/arch/x86/kernel/process.c
68 +++ b/arch/x86/kernel/process.c
69 @@ -398,6 +398,52 @@ static void amd_e400_idle(void)
74 + * Intel Core2 and older machines prefer MWAIT over HALT for C1.
75 + * We can't rely on cpuidle installing MWAIT, because it will not load
76 + * on systems that support only C1 -- so the boot default must be MWAIT.
78 + * Some AMD machines are the opposite, they depend on using HALT.
80 + * So for default C1, which is used during boot until cpuidle loads,
81 + * use MWAIT-C1 on Intel HW that has it, else use HALT.
83 +static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
85 + if (c->x86_vendor != X86_VENDOR_INTEL)
88 + if (!cpu_has(c, X86_FEATURE_MWAIT))
95 + * MONITOR/MWAIT with no hints, used for default default C1 state.
96 + * This invokes MWAIT with interrutps enabled and no flags,
97 + * which is backwards compatible with the original MWAIT implementation.
100 +static void mwait_idle(void)
102 + if (!current_set_polling_and_test()) {
103 + if (static_cpu_has(X86_FEATURE_CLFLUSH_MONITOR)) {
105 + clflush((void *)¤t_thread_info()->flags);
109 + __monitor((void *)¤t_thread_info()->flags, 0, 0);
110 + if (!need_resched())
113 + local_irq_enable();
115 + local_irq_enable();
116 + __current_clr_polling();
119 void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
122 @@ -411,6 +457,9 @@ void __cpuinit select_idle_routine(const
123 /* E400: APIC timer interrupt does not wake up CPU from C1e */
124 pr_info("using AMD E400 aware idle routine\n");
125 x86_idle = amd_e400_idle;
126 + } else if (prefer_mwait_c1_over_halt(c)) {
127 + pr_info("using mwait in idle threads\n");
128 + x86_idle = mwait_idle;
130 x86_idle = default_idle;