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

Merge branch 'pm-cpuidle'

* pm-cpuidle:
cpuidle: Do not use CPUIDLE_DRIVER_STATE_START in cpuidle.c
cpuidle: Select a different state on tick_broadcast_enter() failures
sched / idle: Call default_idle_call() from cpuidle_enter_state()
sched / idle: Call idle_set_state() from cpuidle_enter_state()
cpuidle: Fix the kerneldoc comment for cpuidle_enter_state()
sched / idle: Eliminate the "reflect" check from cpuidle_idle_call()
cpuidle: Check the sign of index in cpuidle_reflect()
sched / idle: Move the default idle call code to a separate function

+93 -67
+28 -10
drivers/cpuidle/cpuidle.c
··· 65 65 return -ENODEV; 66 66 67 67 /* Find lowest-power state that supports long-term idle */ 68 - for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--) 68 + for (i = drv->state_count - 1; i >= 0; i--) 69 69 if (drv->states[i].enter_dead) 70 70 return drv->states[i].enter_dead(dev, i); 71 71 ··· 73 73 } 74 74 75 75 static int find_deepest_state(struct cpuidle_driver *drv, 76 - struct cpuidle_device *dev, bool freeze) 76 + struct cpuidle_device *dev, 77 + unsigned int max_latency, 78 + unsigned int forbidden_flags, 79 + bool freeze) 77 80 { 78 81 unsigned int latency_req = 0; 79 - int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1; 82 + int i, ret = -ENXIO; 80 83 81 - for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) { 84 + for (i = 0; i < drv->state_count; i++) { 82 85 struct cpuidle_state *s = &drv->states[i]; 83 86 struct cpuidle_state_usage *su = &dev->states_usage[i]; 84 87 85 88 if (s->disabled || su->disable || s->exit_latency <= latency_req 89 + || s->exit_latency > max_latency 90 + || (s->flags & forbidden_flags) 86 91 || (freeze && !s->enter_freeze)) 87 92 continue; 88 93 ··· 105 100 int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 106 101 struct cpuidle_device *dev) 107 102 { 108 - return find_deepest_state(drv, dev, false); 103 + return find_deepest_state(drv, dev, UINT_MAX, 0, false); 109 104 } 110 105 111 106 static void enter_freeze_proper(struct cpuidle_driver *drv, ··· 144 139 * that interrupts won't be enabled when it exits and allows the tick to 145 140 * be frozen safely. 146 141 */ 147 - index = find_deepest_state(drv, dev, true); 142 + index = find_deepest_state(drv, dev, UINT_MAX, 0, true); 148 143 if (index >= 0) 149 144 enter_freeze_proper(drv, dev, index); 150 145 ··· 155 150 * cpuidle_enter_state - enter the state and update stats 156 151 * @dev: cpuidle device for this cpu 157 152 * @drv: cpuidle driver for this cpu 158 - * @next_state: index into drv->states of the state to enter 153 + * @index: index into the states table in @drv of the state to enter 159 154 */ 160 155 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, 161 156 int index) ··· 172 167 * local timer will be shut down. If a local timer is used from another 173 168 * CPU as a broadcast timer, this call may fail if it is not available. 174 169 */ 175 - if (broadcast && tick_broadcast_enter()) 176 - return -EBUSY; 170 + if (broadcast && tick_broadcast_enter()) { 171 + index = find_deepest_state(drv, dev, target_state->exit_latency, 172 + CPUIDLE_FLAG_TIMER_STOP, false); 173 + if (index < 0) { 174 + default_idle_call(); 175 + return -EBUSY; 176 + } 177 + target_state = &drv->states[index]; 178 + } 179 + 180 + /* Take note of the planned idle state. */ 181 + sched_idle_set_state(target_state); 177 182 178 183 trace_cpu_idle_rcuidle(index, dev->cpu); 179 184 time_start = ktime_get(); ··· 192 177 193 178 time_end = ktime_get(); 194 179 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); 180 + 181 + /* The cpu is no longer idle or about to enter idle. */ 182 + sched_idle_set_state(NULL); 195 183 196 184 if (broadcast) { 197 185 if (WARN_ON_ONCE(!irqs_disabled())) ··· 267 249 */ 268 250 void cpuidle_reflect(struct cpuidle_device *dev, int index) 269 251 { 270 - if (cpuidle_curr_governor->reflect) 252 + if (cpuidle_curr_governor->reflect && index >= 0) 271 253 cpuidle_curr_governor->reflect(dev, index); 272 254 } 273 255
+2 -2
drivers/cpuidle/governors/menu.c
··· 367 367 static void menu_reflect(struct cpuidle_device *dev, int index) 368 368 { 369 369 struct menu_device *data = this_cpu_ptr(&menu_devices); 370 + 370 371 data->last_state_idx = index; 371 - if (index >= 0) 372 - data->needs_update = 1; 372 + data->needs_update = 1; 373 373 } 374 374 375 375 /**
+4
include/linux/cpuidle.h
··· 200 200 struct cpuidle_device *dev) {return NULL; } 201 201 #endif 202 202 203 + /* kernel/sched/idle.c */ 204 + extern void sched_idle_set_state(struct cpuidle_state *idle_state); 205 + extern void default_idle_call(void); 206 + 203 207 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 204 208 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a); 205 209 #else
+59 -55
kernel/sched/idle.c
··· 15 15 16 16 #include "sched.h" 17 17 18 + /** 19 + * sched_idle_set_state - Record idle state for the current CPU. 20 + * @idle_state: State to record. 21 + */ 22 + void sched_idle_set_state(struct cpuidle_state *idle_state) 23 + { 24 + idle_set_state(this_rq(), idle_state); 25 + } 26 + 18 27 static int __read_mostly cpu_idle_force_poll; 19 28 20 29 void cpu_idle_poll_ctrl(bool enable) ··· 77 68 } 78 69 79 70 /** 71 + * default_idle_call - Default CPU idle routine. 72 + * 73 + * To use when the cpuidle framework cannot be used. 74 + */ 75 + void default_idle_call(void) 76 + { 77 + if (current_clr_polling_and_test()) 78 + local_irq_enable(); 79 + else 80 + arch_cpu_idle(); 81 + } 82 + 83 + static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev, 84 + int next_state) 85 + { 86 + /* Fall back to the default arch idle method on errors. */ 87 + if (next_state < 0) { 88 + default_idle_call(); 89 + return next_state; 90 + } 91 + 92 + /* 93 + * The idle task must be scheduled, it is pointless to go to idle, just 94 + * update no idle residency and return. 95 + */ 96 + if (current_clr_polling_and_test()) { 97 + dev->last_residency = 0; 98 + local_irq_enable(); 99 + return -EBUSY; 100 + } 101 + 102 + /* 103 + * Enter the idle state previously returned by the governor decision. 104 + * This function will block until an interrupt occurs and will take 105 + * care of re-enabling the local interrupts 106 + */ 107 + return cpuidle_enter(drv, dev, next_state); 108 + } 109 + 110 + /** 80 111 * cpuidle_idle_call - the main idle function 81 112 * 82 113 * NOTE: no locks or semaphores should be used here ··· 130 81 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 131 82 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 132 83 int next_state, entered_state; 133 - bool reflect; 134 84 135 85 /* 136 86 * Check if the idle task must be rescheduled. If it is the ··· 153 105 */ 154 106 rcu_idle_enter(); 155 107 156 - if (cpuidle_not_available(drv, dev)) 157 - goto use_default; 108 + if (cpuidle_not_available(drv, dev)) { 109 + default_idle_call(); 110 + goto exit_idle; 111 + } 158 112 159 113 /* 160 114 * Suspend-to-idle ("freeze") is a system state in which all user space ··· 174 124 goto exit_idle; 175 125 } 176 126 177 - reflect = false; 178 127 next_state = cpuidle_find_deepest_state(drv, dev); 128 + call_cpuidle(drv, dev, next_state); 179 129 } else { 180 - reflect = true; 181 130 /* 182 131 * Ask the cpuidle framework to choose a convenient idle state. 183 132 */ 184 133 next_state = cpuidle_select(drv, dev); 185 - } 186 - /* Fall back to the default arch idle method on errors. */ 187 - if (next_state < 0) 188 - goto use_default; 189 - 190 - /* 191 - * The idle task must be scheduled, it is pointless to 192 - * go to idle, just update no idle residency and get 193 - * out of this function 194 - */ 195 - if (current_clr_polling_and_test()) { 196 - dev->last_residency = 0; 197 - entered_state = next_state; 198 - local_irq_enable(); 199 - goto exit_idle; 200 - } 201 - 202 - /* Take note of the planned idle state. */ 203 - idle_set_state(this_rq(), &drv->states[next_state]); 204 - 205 - /* 206 - * Enter the idle state previously returned by the governor decision. 207 - * This function will block until an interrupt occurs and will take 208 - * care of re-enabling the local interrupts 209 - */ 210 - entered_state = cpuidle_enter(drv, dev, next_state); 211 - 212 - /* The cpu is no longer idle or about to enter idle. */ 213 - idle_set_state(this_rq(), NULL); 214 - 215 - if (entered_state == -EBUSY) 216 - goto use_default; 217 - 218 - /* 219 - * Give the governor an opportunity to reflect on the outcome 220 - */ 221 - if (reflect) 134 + entered_state = call_cpuidle(drv, dev, next_state); 135 + /* 136 + * Give the governor an opportunity to reflect on the outcome 137 + */ 222 138 cpuidle_reflect(dev, entered_state); 139 + } 223 140 224 141 exit_idle: 225 142 __current_set_polling(); ··· 199 182 200 183 rcu_idle_exit(); 201 184 start_critical_timings(); 202 - return; 203 - 204 - use_default: 205 - /* 206 - * We can't use the cpuidle framework, let's use the default 207 - * idle routine. 208 - */ 209 - if (current_clr_polling_and_test()) 210 - local_irq_enable(); 211 - else 212 - arch_cpu_idle(); 213 - 214 - goto exit_idle; 215 185 } 216 186 217 187 DEFINE_PER_CPU(bool, cpu_dead_idle);