Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Generic entry points for the idle threads and
0004  * implementation of the idle task scheduling class.
0005  *
0006  * (NOTE: these are not related to SCHED_IDLE batch scheduled
0007  *        tasks which are handled in sched/fair.c )
0008  */
0009 
0010 /* Linker adds these: start and end of __cpuidle functions */
0011 extern char __cpuidle_text_start[], __cpuidle_text_end[];
0012 
0013 /**
0014  * sched_idle_set_state - Record idle state for the current CPU.
0015  * @idle_state: State to record.
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 /* Weak implementations for optional arch specific functions */
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  * default_idle_call - Default CPU idle routine.
0083  *
0084  * To use when the cpuidle framework cannot be used.
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          * arch_cpu_idle() is supposed to enable IRQs, however
0097          * we can't do that because of RCU and tracing.
0098          *
0099          * Trace IRQs enable here, then switch off RCU, and have
0100          * arch_cpu_idle() use raw_local_irq_enable(). Note that
0101          * ct_idle_enter() relies on lockdep IRQ state, so switch that
0102          * last -- this is very similar to the entry code.
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          * OK, so IRQs are enabled here, but RCU needs them disabled to
0113          * turn itself back on.. funny thing is that disabling IRQs
0114          * will cause tracing, which needs RCU. Jump through hoops to
0115          * make it 'work'.
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      * The idle task must be scheduled, it is pointless to go to idle, just
0142      * update no idle residency and return.
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      * Enter the idle state previously returned by the governor decision.
0152      * This function will block until an interrupt occurs and will take
0153      * care of re-enabling the local interrupts
0154      */
0155     return cpuidle_enter(drv, dev, next_state);
0156 }
0157 
0158 /**
0159  * cpuidle_idle_call - the main idle function
0160  *
0161  * NOTE: no locks or semaphores should be used here
0162  *
0163  * On architectures that support TIF_POLLING_NRFLAG, is called with polling
0164  * set, and it returns with polling set.  If it ever stops polling, it
0165  * must clear the polling bit.
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      * Check if the idle task must be rescheduled. If it is the
0175      * case, exit the function after re-enabling the local irq.
0176      */
0177     if (need_resched()) {
0178         local_irq_enable();
0179         return;
0180     }
0181 
0182     /*
0183      * The RCU framework needs to be told that we are entering an idle
0184      * section, so no more rcu read side critical sections and one more
0185      * step to the grace period
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      * Suspend-to-idle ("s2idle") is a system state in which all user space
0197      * has been frozen, all I/O devices have been suspended and the only
0198      * activity happens here and in interrupts (if any). In that case bypass
0199      * the cpuidle governor and go straight for the deepest idle state
0200      * available.  Possibly also suspend the local tick and the entire
0201      * timekeeping to prevent timer interrupts from kicking us out of idle
0202      * until a proper wakeup interrupt happens.
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          * Ask the cpuidle framework to choose a convenient idle state.
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          * Give the governor an opportunity to reflect on the outcome
0239          */
0240         cpuidle_reflect(dev, entered_state);
0241     }
0242 
0243 exit_idle:
0244     __current_set_polling();
0245 
0246     /*
0247      * It is up to the idle functions to reenable local interrupts
0248      */
0249     if (WARN_ON_ONCE(irqs_disabled()))
0250         local_irq_enable();
0251 }
0252 
0253 /*
0254  * Generic idle loop implementation
0255  *
0256  * Called with polling cleared.
0257  */
0258 static void do_idle(void)
0259 {
0260     int cpu = smp_processor_id();
0261 
0262     /*
0263      * Check if we need to update blocked load
0264      */
0265     nohz_run_idle_balance(cpu);
0266 
0267     /*
0268      * If the arch has a polling bit, we maintain an invariant:
0269      *
0270      * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
0271      * rq->idle). This means that, if rq->idle has the polling bit set,
0272      * then setting need_resched is guaranteed to cause the CPU to
0273      * reschedule.
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          * In poll mode we reenable interrupts and spin. Also if we
0295          * detected in the wakeup from idle path that the tick
0296          * broadcast device expired for us, we don't want to go deep
0297          * idle as we know that the IPI is going to arrive right away.
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      * Since we fell out of the loop above, we know TIF_NEED_RESCHED must
0310      * be set, propagate it into PREEMPT_NEED_RESCHED.
0311      *
0312      * This is required because for polling idle loops we will not have had
0313      * an IPI to fold the state for us.
0314      */
0315     preempt_set_need_resched();
0316     tick_nohz_idle_exit();
0317     __current_clr_polling();
0318 
0319     /*
0320      * We promise to call sched_ttwu_pending() and reschedule if
0321      * need_resched() is set while polling is set. That means that clearing
0322      * polling needs to be visible before doing these things.
0323      */
0324     smp_mb__after_atomic();
0325 
0326     /*
0327      * RCU relies on this call to be done outside of an RCU read-side
0328      * critical section.
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      * Only FIFO tasks can disable the tick since they don't need the forced
0364      * preemption.
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  * idle-task scheduling class.
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); /* IDLE tasks as never migrated */
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  * Idle tasks are unconditionally rescheduled:
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  * It is not legal to sleep in the idle task - print a warning
0457  * message if some code attempts to do it:
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  * scheduler tick hitting a task of our scheduling class.
0470  *
0471  * NOTE: This function can be called remotely by the tick offload that
0472  * goes along full dynticks. Therefore no local assumption can be made
0473  * and everything must be accessed through the @rq and @curr passed in
0474  * parameters.
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  * Simple, special scheduling class for the per-CPU idle tasks:
0497  */
0498 DEFINE_SCHED_CLASS(idle) = {
0499 
0500     /* no enqueue/yield_task for idle tasks */
0501 
0502     /* dequeue is not valid, we print a debug message there: */
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 };