stop_task.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * stop-task scheduling class.
  4. *
  5. * The stop task is the highest priority task in the system, it preempts
  6. * everything and will be preempted by nothing.
  7. *
  8. * See kernel/stop_machine.c
  9. */
  10. #include "sched.h"
  11. #ifdef CONFIG_SMP
  12. static int
  13. select_task_rq_stop(struct task_struct *p, int cpu, int sd_flag, int flags)
  14. {
  15. return task_cpu(p); /* stop tasks as never migrate */
  16. }
  17. static int
  18. balance_stop(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
  19. {
  20. return sched_stop_runnable(rq);
  21. }
  22. #endif /* CONFIG_SMP */
  23. static void
  24. check_preempt_curr_stop(struct rq *rq, struct task_struct *p, int flags)
  25. {
  26. /* we're never preempted */
  27. }
  28. static void set_next_task_stop(struct rq *rq, struct task_struct *stop, bool first)
  29. {
  30. stop->se.exec_start = rq_clock_task(rq);
  31. }
  32. static struct task_struct *pick_next_task_stop(struct rq *rq)
  33. {
  34. if (!sched_stop_runnable(rq))
  35. return NULL;
  36. set_next_task_stop(rq, rq->stop, true);
  37. return rq->stop;
  38. }
  39. static void
  40. enqueue_task_stop(struct rq *rq, struct task_struct *p, int flags)
  41. {
  42. add_nr_running(rq, 1);
  43. }
  44. static void
  45. dequeue_task_stop(struct rq *rq, struct task_struct *p, int flags)
  46. {
  47. sub_nr_running(rq, 1);
  48. }
  49. static void yield_task_stop(struct rq *rq)
  50. {
  51. BUG(); /* the stop task should never yield, its pointless. */
  52. }
  53. static void put_prev_task_stop(struct rq *rq, struct task_struct *prev)
  54. {
  55. struct task_struct *curr = rq->curr;
  56. u64 delta_exec;
  57. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  58. if (unlikely((s64)delta_exec < 0))
  59. delta_exec = 0;
  60. schedstat_set(curr->se.statistics.exec_max,
  61. max(curr->se.statistics.exec_max, delta_exec));
  62. curr->se.sum_exec_runtime += delta_exec;
  63. account_group_exec_runtime(curr, delta_exec);
  64. curr->se.exec_start = rq_clock_task(rq);
  65. cgroup_account_cputime(curr, delta_exec);
  66. }
  67. /*
  68. * scheduler tick hitting a task of our scheduling class.
  69. *
  70. * NOTE: This function can be called remotely by the tick offload that
  71. * goes along full dynticks. Therefore no local assumption can be made
  72. * and everything must be accessed through the @rq and @curr passed in
  73. * parameters.
  74. */
  75. static void task_tick_stop(struct rq *rq, struct task_struct *curr, int queued)
  76. {
  77. }
  78. static void switched_to_stop(struct rq *rq, struct task_struct *p)
  79. {
  80. BUG(); /* its impossible to change to this class */
  81. }
  82. static void
  83. prio_changed_stop(struct rq *rq, struct task_struct *p, int oldprio)
  84. {
  85. BUG(); /* how!?, what priority? */
  86. }
  87. static void update_curr_stop(struct rq *rq)
  88. {
  89. }
  90. /*
  91. * Simple, special scheduling class for the per-CPU stop tasks:
  92. */
  93. const struct sched_class stop_sched_class
  94. __section("__stop_sched_class") = {
  95. .enqueue_task = enqueue_task_stop,
  96. .dequeue_task = dequeue_task_stop,
  97. .yield_task = yield_task_stop,
  98. .check_preempt_curr = check_preempt_curr_stop,
  99. .pick_next_task = pick_next_task_stop,
  100. .put_prev_task = put_prev_task_stop,
  101. .set_next_task = set_next_task_stop,
  102. #ifdef CONFIG_SMP
  103. .balance = balance_stop,
  104. .select_task_rq = select_task_rq_stop,
  105. .set_cpus_allowed = set_cpus_allowed_common,
  106. #endif
  107. .task_tick = task_tick_stop,
  108. .prio_changed = prio_changed_stop,
  109. .switched_to = switched_to_stop,
  110. .update_curr = update_curr_stop,
  111. };