0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 extern char __cpuidle_text_start[], __cpuidle_text_end[];
0012
0013
0014
0015
0016
0017 void sched_idle_set_state(struct cpuidle_state *idle_state)
0018 {
0019 idle_set_state(this_rq(), idle_state);
0020 }
0021
0022 static int __read_mostly cpu_idle_force_poll;
0023
0024 void cpu_idle_poll_ctrl(bool enable)
0025 {
0026 if (enable) {
0027 cpu_idle_force_poll++;
0028 } else {
0029 cpu_idle_force_poll--;
0030 WARN_ON_ONCE(cpu_idle_force_poll < 0);
0031 }
0032 }
0033
0034 #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
0035 static int __init cpu_idle_poll_setup(char *__unused)
0036 {
0037 cpu_idle_force_poll = 1;
0038
0039 return 1;
0040 }
0041 __setup("nohlt", cpu_idle_poll_setup);
0042
0043 static int __init cpu_idle_nopoll_setup(char *__unused)
0044 {
0045 cpu_idle_force_poll = 0;
0046
0047 return 1;
0048 }
0049 __setup("hlt", cpu_idle_nopoll_setup);
0050 #endif
0051
0052 static noinline int __cpuidle cpu_idle_poll(void)
0053 {
0054 trace_cpu_idle(0, smp_processor_id());
0055 stop_critical_timings();
0056 ct_idle_enter();
0057 local_irq_enable();
0058
0059 while (!tif_need_resched() &&
0060 (cpu_idle_force_poll || tick_check_broadcast_expired()))
0061 cpu_relax();
0062
0063 ct_idle_exit();
0064 start_critical_timings();
0065 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
0066
0067 return 1;
0068 }
0069
0070
0071 void __weak arch_cpu_idle_prepare(void) { }
0072 void __weak arch_cpu_idle_enter(void) { }
0073 void __weak arch_cpu_idle_exit(void) { }
0074 void __weak arch_cpu_idle_dead(void) { }
0075 void __weak arch_cpu_idle(void)
0076 {
0077 cpu_idle_force_poll = 1;
0078 raw_local_irq_enable();
0079 }
0080
0081
0082
0083
0084
0085
0086 void __cpuidle default_idle_call(void)
0087 {
0088 if (current_clr_polling_and_test()) {
0089 local_irq_enable();
0090 } else {
0091
0092 trace_cpu_idle(1, smp_processor_id());
0093 stop_critical_timings();
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 trace_hardirqs_on_prepare();
0105 lockdep_hardirqs_on_prepare();
0106 ct_idle_enter();
0107 lockdep_hardirqs_on(_THIS_IP_);
0108
0109 arch_cpu_idle();
0110
0111
0112
0113
0114
0115
0116
0117 raw_local_irq_disable();
0118 lockdep_hardirqs_off(_THIS_IP_);
0119 ct_idle_exit();
0120 lockdep_hardirqs_on(_THIS_IP_);
0121 raw_local_irq_enable();
0122
0123 start_critical_timings();
0124 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
0125 }
0126 }
0127
0128 static int call_cpuidle_s2idle(struct cpuidle_driver *drv,
0129 struct cpuidle_device *dev)
0130 {
0131 if (current_clr_polling_and_test())
0132 return -EBUSY;
0133
0134 return cpuidle_enter_s2idle(drv, dev);
0135 }
0136
0137 static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
0138 int next_state)
0139 {
0140
0141
0142
0143
0144 if (current_clr_polling_and_test()) {
0145 dev->last_residency_ns = 0;
0146 local_irq_enable();
0147 return -EBUSY;
0148 }
0149
0150
0151
0152
0153
0154
0155 return cpuidle_enter(drv, dev, next_state);
0156 }
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 static void cpuidle_idle_call(void)
0168 {
0169 struct cpuidle_device *dev = cpuidle_get_device();
0170 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0171 int next_state, entered_state;
0172
0173
0174
0175
0176
0177 if (need_resched()) {
0178 local_irq_enable();
0179 return;
0180 }
0181
0182
0183
0184
0185
0186
0187
0188 if (cpuidle_not_available(drv, dev)) {
0189 tick_nohz_idle_stop_tick();
0190
0191 default_idle_call();
0192 goto exit_idle;
0193 }
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 if (idle_should_enter_s2idle() || dev->forced_idle_latency_limit_ns) {
0206 u64 max_latency_ns;
0207
0208 if (idle_should_enter_s2idle()) {
0209
0210 entered_state = call_cpuidle_s2idle(drv, dev);
0211 if (entered_state > 0)
0212 goto exit_idle;
0213
0214 max_latency_ns = U64_MAX;
0215 } else {
0216 max_latency_ns = dev->forced_idle_latency_limit_ns;
0217 }
0218
0219 tick_nohz_idle_stop_tick();
0220
0221 next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns);
0222 call_cpuidle(drv, dev, next_state);
0223 } else {
0224 bool stop_tick = true;
0225
0226
0227
0228
0229 next_state = cpuidle_select(drv, dev, &stop_tick);
0230
0231 if (stop_tick || tick_nohz_tick_stopped())
0232 tick_nohz_idle_stop_tick();
0233 else
0234 tick_nohz_idle_retain_tick();
0235
0236 entered_state = call_cpuidle(drv, dev, next_state);
0237
0238
0239
0240 cpuidle_reflect(dev, entered_state);
0241 }
0242
0243 exit_idle:
0244 __current_set_polling();
0245
0246
0247
0248
0249 if (WARN_ON_ONCE(irqs_disabled()))
0250 local_irq_enable();
0251 }
0252
0253
0254
0255
0256
0257
0258 static void do_idle(void)
0259 {
0260 int cpu = smp_processor_id();
0261
0262
0263
0264
0265 nohz_run_idle_balance(cpu);
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 __current_set_polling();
0277 tick_nohz_idle_enter();
0278
0279 while (!need_resched()) {
0280 rmb();
0281
0282 local_irq_disable();
0283
0284 if (cpu_is_offline(cpu)) {
0285 tick_nohz_idle_stop_tick();
0286 cpuhp_report_idle_dead();
0287 arch_cpu_idle_dead();
0288 }
0289
0290 arch_cpu_idle_enter();
0291 rcu_nocb_flush_deferred_wakeup();
0292
0293
0294
0295
0296
0297
0298
0299 if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
0300 tick_nohz_idle_restart_tick();
0301 cpu_idle_poll();
0302 } else {
0303 cpuidle_idle_call();
0304 }
0305 arch_cpu_idle_exit();
0306 }
0307
0308
0309
0310
0311
0312
0313
0314
0315 preempt_set_need_resched();
0316 tick_nohz_idle_exit();
0317 __current_clr_polling();
0318
0319
0320
0321
0322
0323
0324 smp_mb__after_atomic();
0325
0326
0327
0328
0329
0330 flush_smp_call_function_queue();
0331 schedule_idle();
0332
0333 if (unlikely(klp_patch_pending(current)))
0334 klp_update_patch_state(current);
0335 }
0336
0337 bool cpu_in_idle(unsigned long pc)
0338 {
0339 return pc >= (unsigned long)__cpuidle_text_start &&
0340 pc < (unsigned long)__cpuidle_text_end;
0341 }
0342
0343 struct idle_timer {
0344 struct hrtimer timer;
0345 int done;
0346 };
0347
0348 static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
0349 {
0350 struct idle_timer *it = container_of(timer, struct idle_timer, timer);
0351
0352 WRITE_ONCE(it->done, 1);
0353 set_tsk_need_resched(current);
0354
0355 return HRTIMER_NORESTART;
0356 }
0357
0358 void play_idle_precise(u64 duration_ns, u64 latency_ns)
0359 {
0360 struct idle_timer it;
0361
0362
0363
0364
0365
0366 WARN_ON_ONCE(current->policy != SCHED_FIFO);
0367 WARN_ON_ONCE(current->nr_cpus_allowed != 1);
0368 WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
0369 WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
0370 WARN_ON_ONCE(!duration_ns);
0371 WARN_ON_ONCE(current->mm);
0372
0373 rcu_sleep_check();
0374 preempt_disable();
0375 current->flags |= PF_IDLE;
0376 cpuidle_use_deepest_state(latency_ns);
0377
0378 it.done = 0;
0379 hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
0380 it.timer.function = idle_inject_timer_fn;
0381 hrtimer_start(&it.timer, ns_to_ktime(duration_ns),
0382 HRTIMER_MODE_REL_PINNED_HARD);
0383
0384 while (!READ_ONCE(it.done))
0385 do_idle();
0386
0387 cpuidle_use_deepest_state(0);
0388 current->flags &= ~PF_IDLE;
0389
0390 preempt_fold_need_resched();
0391 preempt_enable();
0392 }
0393 EXPORT_SYMBOL_GPL(play_idle_precise);
0394
0395 void cpu_startup_entry(enum cpuhp_state state)
0396 {
0397 arch_cpu_idle_prepare();
0398 cpuhp_online_idle(state);
0399 while (1)
0400 do_idle();
0401 }
0402
0403
0404
0405
0406
0407 #ifdef CONFIG_SMP
0408 static int
0409 select_task_rq_idle(struct task_struct *p, int cpu, int flags)
0410 {
0411 return task_cpu(p);
0412 }
0413
0414 static int
0415 balance_idle(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
0416 {
0417 return WARN_ON_ONCE(1);
0418 }
0419 #endif
0420
0421
0422
0423
0424 static void check_preempt_curr_idle(struct rq *rq, struct task_struct *p, int flags)
0425 {
0426 resched_curr(rq);
0427 }
0428
0429 static void put_prev_task_idle(struct rq *rq, struct task_struct *prev)
0430 {
0431 }
0432
0433 static void set_next_task_idle(struct rq *rq, struct task_struct *next, bool first)
0434 {
0435 update_idle_core(rq);
0436 schedstat_inc(rq->sched_goidle);
0437 }
0438
0439 #ifdef CONFIG_SMP
0440 static struct task_struct *pick_task_idle(struct rq *rq)
0441 {
0442 return rq->idle;
0443 }
0444 #endif
0445
0446 struct task_struct *pick_next_task_idle(struct rq *rq)
0447 {
0448 struct task_struct *next = rq->idle;
0449
0450 set_next_task_idle(rq, next, true);
0451
0452 return next;
0453 }
0454
0455
0456
0457
0458
0459 static void
0460 dequeue_task_idle(struct rq *rq, struct task_struct *p, int flags)
0461 {
0462 raw_spin_rq_unlock_irq(rq);
0463 printk(KERN_ERR "bad: scheduling from the idle thread!\n");
0464 dump_stack();
0465 raw_spin_rq_lock_irq(rq);
0466 }
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476 static void task_tick_idle(struct rq *rq, struct task_struct *curr, int queued)
0477 {
0478 }
0479
0480 static void switched_to_idle(struct rq *rq, struct task_struct *p)
0481 {
0482 BUG();
0483 }
0484
0485 static void
0486 prio_changed_idle(struct rq *rq, struct task_struct *p, int oldprio)
0487 {
0488 BUG();
0489 }
0490
0491 static void update_curr_idle(struct rq *rq)
0492 {
0493 }
0494
0495
0496
0497
0498 DEFINE_SCHED_CLASS(idle) = {
0499
0500
0501
0502
0503 .dequeue_task = dequeue_task_idle,
0504
0505 .check_preempt_curr = check_preempt_curr_idle,
0506
0507 .pick_next_task = pick_next_task_idle,
0508 .put_prev_task = put_prev_task_idle,
0509 .set_next_task = set_next_task_idle,
0510
0511 #ifdef CONFIG_SMP
0512 .balance = balance_idle,
0513 .pick_task = pick_task_idle,
0514 .select_task_rq = select_task_rq_idle,
0515 .set_cpus_allowed = set_cpus_allowed_common,
0516 #endif
0517
0518 .task_tick = task_tick_idle,
0519
0520 .prio_changed = prio_changed_idle,
0521 .switched_to = switched_to_idle,
0522 .update_curr = update_curr_idle,
0523 };