aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched/idle.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched/idle.c')
-rw-r--r--kernel/sched/idle.c162
1 files changed, 101 insertions, 61 deletions
diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index 513e4dfeeae7..6a4bae0a649d 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -205,76 +205,65 @@ exit_idle:
205 * 205 *
206 * Called with polling cleared. 206 * Called with polling cleared.
207 */ 207 */
208static void cpu_idle_loop(void) 208static void do_idle(void)
209{ 209{
210 int cpu = smp_processor_id(); 210 /*
211 211 * If the arch has a polling bit, we maintain an invariant:
212 while (1) { 212 *
213 /* 213 * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
214 * If the arch has a polling bit, we maintain an invariant: 214 * rq->idle). This means that, if rq->idle has the polling bit set,
215 * 215 * then setting need_resched is guaranteed to cause the CPU to
216 * Our polling bit is clear if we're not scheduled (i.e. if 216 * reschedule.
217 * rq->curr != rq->idle). This means that, if rq->idle has 217 */
218 * the polling bit set, then setting need_resched is
219 * guaranteed to cause the cpu to reschedule.
220 */
221
222 __current_set_polling();
223 quiet_vmstat();
224 tick_nohz_idle_enter();
225 218
226 while (!need_resched()) { 219 __current_set_polling();
227 check_pgt_cache(); 220 tick_nohz_idle_enter();
228 rmb();
229 221
230 if (cpu_is_offline(cpu)) { 222 while (!need_resched()) {
231 cpuhp_report_idle_dead(); 223 check_pgt_cache();
232 arch_cpu_idle_dead(); 224 rmb();
233 }
234 225
235 local_irq_disable(); 226 if (cpu_is_offline(smp_processor_id())) {
236 arch_cpu_idle_enter(); 227 cpuhp_report_idle_dead();
237 228 arch_cpu_idle_dead();
238 /*
239 * In poll mode we reenable interrupts and spin.
240 *
241 * Also if we detected in the wakeup from idle
242 * path that the tick broadcast device expired
243 * for us, we don't want to go deep idle as we
244 * know that the IPI is going to arrive right
245 * away
246 */
247 if (cpu_idle_force_poll || tick_check_broadcast_expired())
248 cpu_idle_poll();
249 else
250 cpuidle_idle_call();
251
252 arch_cpu_idle_exit();
253 } 229 }
254 230
255 /* 231 local_irq_disable();
256 * Since we fell out of the loop above, we know 232 arch_cpu_idle_enter();
257 * TIF_NEED_RESCHED must be set, propagate it into
258 * PREEMPT_NEED_RESCHED.
259 *
260 * This is required because for polling idle loops we will
261 * not have had an IPI to fold the state for us.
262 */
263 preempt_set_need_resched();
264 tick_nohz_idle_exit();
265 __current_clr_polling();
266 233
267 /* 234 /*
268 * We promise to call sched_ttwu_pending and reschedule 235 * In poll mode we reenable interrupts and spin. Also if we
269 * if need_resched is set while polling is set. That 236 * detected in the wakeup from idle path that the tick
270 * means that clearing polling needs to be visible 237 * broadcast device expired for us, we don't want to go deep
271 * before doing these things. 238 * idle as we know that the IPI is going to arrive right away.
272 */ 239 */
273 smp_mb__after_atomic(); 240 if (cpu_idle_force_poll || tick_check_broadcast_expired())
274 241 cpu_idle_poll();
275 sched_ttwu_pending(); 242 else
276 schedule_preempt_disabled(); 243 cpuidle_idle_call();
244 arch_cpu_idle_exit();
277 } 245 }
246
247 /*
248 * Since we fell out of the loop above, we know TIF_NEED_RESCHED must
249 * be set, propagate it into PREEMPT_NEED_RESCHED.
250 *
251 * This is required because for polling idle loops we will not have had
252 * an IPI to fold the state for us.
253 */
254 preempt_set_need_resched();
255 tick_nohz_idle_exit();
256 __current_clr_polling();
257
258 /*
259 * We promise to call sched_ttwu_pending() and reschedule if
260 * need_resched() is set while polling is set. That means that clearing
261 * polling needs to be visible before doing these things.
262 */
263 smp_mb__after_atomic();
264
265 sched_ttwu_pending();
266 schedule_preempt_disabled();
278} 267}
279 268
280bool cpu_in_idle(unsigned long pc) 269bool cpu_in_idle(unsigned long pc)
@@ -283,6 +272,56 @@ bool cpu_in_idle(unsigned long pc)
283 pc < (unsigned long)__cpuidle_text_end; 272 pc < (unsigned long)__cpuidle_text_end;
284} 273}
285 274
275struct idle_timer {
276 struct hrtimer timer;
277 int done;
278};
279
280static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
281{
282 struct idle_timer *it = container_of(timer, struct idle_timer, timer);
283
284 WRITE_ONCE(it->done, 1);
285 set_tsk_need_resched(current);
286
287 return HRTIMER_NORESTART;
288}
289
290void play_idle(unsigned long duration_ms)
291{
292 struct idle_timer it;
293
294 /*
295 * Only FIFO tasks can disable the tick since they don't need the forced
296 * preemption.
297 */
298 WARN_ON_ONCE(current->policy != SCHED_FIFO);
299 WARN_ON_ONCE(current->nr_cpus_allowed != 1);
300 WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
301 WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
302 WARN_ON_ONCE(!duration_ms);
303
304 rcu_sleep_check();
305 preempt_disable();
306 current->flags |= PF_IDLE;
307 cpuidle_use_deepest_state(true);
308
309 it.done = 0;
310 hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
311 it.timer.function = idle_inject_timer_fn;
312 hrtimer_start(&it.timer, ms_to_ktime(duration_ms), HRTIMER_MODE_REL_PINNED);
313
314 while (!READ_ONCE(it.done))
315 do_idle();
316
317 cpuidle_use_deepest_state(false);
318 current->flags &= ~PF_IDLE;
319
320 preempt_fold_need_resched();
321 preempt_enable();
322}
323EXPORT_SYMBOL_GPL(play_idle);
324
286void cpu_startup_entry(enum cpuhp_state state) 325void cpu_startup_entry(enum cpuhp_state state)
287{ 326{
288 /* 327 /*
@@ -302,5 +341,6 @@ void cpu_startup_entry(enum cpuhp_state state)
302#endif 341#endif
303 arch_cpu_idle_prepare(); 342 arch_cpu_idle_prepare();
304 cpuhp_online_idle(state); 343 cpuhp_online_idle(state);
305 cpu_idle_loop(); 344 while (1)
345 do_idle();
306} 346}