Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sh: Idle loop chainsawing for SMP-based light sleep.

This does a bit of chainsawing of the idle loop code to get light sleep
working on SMP. Previously this was forcing secondary CPUs in to sleep
mode with them not coming back if they didn't have their own local
timers. Given that we use clockevents broadcasting by default, the CPU
managing the clockevents can't have IRQs disabled before entering its
sleep state.

This unfortunately leaves us with the age-old need_resched() race in
between local_irq_enable() and cpu_sleep(), but at present this is
unavoidable. After some more experimentation it may be possible to layer
on SR.BL bit manipulation over top of this scheme to inhibit the race
condition, but given the current potential for missing wakeups, this is
left as a future exercise.

Signed-off-by: Paul Mundt <lethal@linux-sh.org>

+70 -25
+4
arch/sh/include/asm/bugs.h
··· 14 14 15 15 #include <asm/processor.h> 16 16 17 + extern void select_idle_routine(void); 18 + 17 19 static void __init check_bugs(void) 18 20 { 19 21 extern unsigned long loops_per_jiffy; 20 22 char *p = &init_utsname()->machine[2]; /* "sh" */ 23 + 24 + select_idle_routine(); 21 25 22 26 current_cpu_data.loops_per_jiffy = loops_per_jiffy; 23 27
+66 -25
arch/sh/kernel/idle.c
··· 21 21 #include <asm/atomic.h> 22 22 23 23 static int hlt_counter; 24 - void (*pm_idle)(void); 24 + void (*pm_idle)(void) = NULL; 25 25 void (*pm_power_off)(void); 26 26 EXPORT_SYMBOL(pm_power_off); 27 27 ··· 39 39 } 40 40 __setup("hlt", hlt_setup); 41 41 42 - void default_idle(void) 42 + static inline int hlt_works(void) 43 43 { 44 - if (!hlt_counter) { 45 - clear_thread_flag(TIF_POLLING_NRFLAG); 46 - smp_mb__after_clear_bit(); 47 - set_bl_bit(); 48 - stop_critical_timings(); 49 - 50 - while (!need_resched()) 51 - cpu_sleep(); 52 - 53 - start_critical_timings(); 54 - clear_bl_bit(); 55 - set_thread_flag(TIF_POLLING_NRFLAG); 56 - } else 57 - while (!need_resched()) 58 - cpu_relax(); 44 + return !hlt_counter; 59 45 } 60 46 47 + /* 48 + * On SMP it's slightly faster (but much more power-consuming!) 49 + * to poll the ->work.need_resched flag instead of waiting for the 50 + * cross-CPU IPI to arrive. Use this option with caution. 51 + */ 52 + static void poll_idle(void) 53 + { 54 + local_irq_enable(); 55 + while (!need_resched()) 56 + cpu_relax(); 57 + } 58 + 59 + void default_idle(void) 60 + { 61 + if (hlt_works()) { 62 + clear_thread_flag(TIF_POLLING_NRFLAG); 63 + smp_mb__after_clear_bit(); 64 + 65 + if (!need_resched()) { 66 + local_irq_enable(); 67 + cpu_sleep(); 68 + } 69 + 70 + set_thread_flag(TIF_POLLING_NRFLAG); 71 + } else 72 + poll_idle(); 73 + } 74 + 75 + /* 76 + * The idle thread. There's no useful work to be done, so just try to conserve 77 + * power and have a low exit latency (ie sit in a loop waiting for somebody to 78 + * say that they'd like to reschedule) 79 + */ 61 80 void cpu_idle(void) 62 81 { 82 + unsigned int cpu = smp_processor_id(); 83 + 63 84 set_thread_flag(TIF_POLLING_NRFLAG); 64 85 65 86 /* endless idle loop with no priority at all */ 66 87 while (1) { 67 - void (*idle)(void) = pm_idle; 68 - 69 - if (!idle) 70 - idle = default_idle; 71 - 72 88 tick_nohz_stop_sched_tick(1); 73 - while (!need_resched()) 74 - idle(); 75 - tick_nohz_restart_sched_tick(); 76 89 90 + while (!need_resched() && cpu_online(cpu)) { 91 + local_irq_disable(); 92 + /* Don't trace irqs off for idle */ 93 + stop_critical_timings(); 94 + pm_idle(); 95 + /* 96 + * Sanity check to ensure that pm_idle() returns 97 + * with IRQs enabled 98 + */ 99 + WARN_ON(irqs_disabled()); 100 + start_critical_timings(); 101 + } 102 + 103 + tick_nohz_restart_sched_tick(); 77 104 preempt_enable_no_resched(); 78 105 schedule(); 79 106 preempt_disable(); 80 107 check_pgt_cache(); 81 108 } 109 + } 110 + 111 + void __cpuinit select_idle_routine(void) 112 + { 113 + /* 114 + * If a platform has set its own idle routine, leave it alone. 115 + */ 116 + if (pm_idle) 117 + return; 118 + 119 + if (hlt_works()) 120 + pm_idle = default_idle; 121 + else 122 + pm_idle = poll_idle; 82 123 } 83 124 84 125 static void do_nothing(void *unused)