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 #include "walt.h"
12
13 #ifdef CONFIG_SMP
14 static int
select_task_rq_stop(struct task_struct * p,int cpu,int sd_flag,int flags)15 select_task_rq_stop(struct task_struct *p, int cpu, int sd_flag, int flags)
16 {
17 return task_cpu(p); /* stop tasks as never migrate */
18 }
19
20 static int
balance_stop(struct rq * rq,struct task_struct * prev,struct rq_flags * rf)21 balance_stop(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
22 {
23 return sched_stop_runnable(rq);
24 }
25 #endif /* CONFIG_SMP */
26
27 static void
check_preempt_curr_stop(struct rq * rq,struct task_struct * p,int flags)28 check_preempt_curr_stop(struct rq *rq, struct task_struct *p, int flags)
29 {
30 /* we're never preempted */
31 }
32
set_next_task_stop(struct rq * rq,struct task_struct * stop,bool first)33 static void set_next_task_stop(struct rq *rq, struct task_struct *stop, bool first)
34 {
35 stop->se.exec_start = rq_clock_task(rq);
36 }
37
pick_next_task_stop(struct rq * rq)38 static struct task_struct *pick_next_task_stop(struct rq *rq)
39 {
40 if (!sched_stop_runnable(rq))
41 return NULL;
42
43 set_next_task_stop(rq, rq->stop, true);
44 return rq->stop;
45 }
46
47 static void
enqueue_task_stop(struct rq * rq,struct task_struct * p,int flags)48 enqueue_task_stop(struct rq *rq, struct task_struct *p, int flags)
49 {
50 add_nr_running(rq, 1);
51 walt_inc_cumulative_runnable_avg(rq, p);
52 }
53
54 static void
dequeue_task_stop(struct rq * rq,struct task_struct * p,int flags)55 dequeue_task_stop(struct rq *rq, struct task_struct *p, int flags)
56 {
57 sub_nr_running(rq, 1);
58 walt_dec_cumulative_runnable_avg(rq, p);
59 }
60
yield_task_stop(struct rq * rq)61 static void yield_task_stop(struct rq *rq)
62 {
63 BUG(); /* the stop task should never yield, its pointless. */
64 }
65
put_prev_task_stop(struct rq * rq,struct task_struct * prev)66 static void put_prev_task_stop(struct rq *rq, struct task_struct *prev)
67 {
68 struct task_struct *curr = rq->curr;
69 u64 delta_exec;
70
71 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
72 if (unlikely((s64)delta_exec < 0))
73 delta_exec = 0;
74
75 schedstat_set(curr->se.statistics.exec_max,
76 max(curr->se.statistics.exec_max, delta_exec));
77
78 curr->se.sum_exec_runtime += delta_exec;
79 account_group_exec_runtime(curr, delta_exec);
80
81 curr->se.exec_start = rq_clock_task(rq);
82 cgroup_account_cputime(curr, delta_exec);
83 }
84
85 /*
86 * scheduler tick hitting a task of our scheduling class.
87 *
88 * NOTE: This function can be called remotely by the tick offload that
89 * goes along full dynticks. Therefore no local assumption can be made
90 * and everything must be accessed through the @rq and @curr passed in
91 * parameters.
92 */
task_tick_stop(struct rq * rq,struct task_struct * curr,int queued)93 static void task_tick_stop(struct rq *rq, struct task_struct *curr, int queued)
94 {
95 }
96
switched_to_stop(struct rq * rq,struct task_struct * p)97 static void switched_to_stop(struct rq *rq, struct task_struct *p)
98 {
99 BUG(); /* its impossible to change to this class */
100 }
101
102 static void
prio_changed_stop(struct rq * rq,struct task_struct * p,int oldprio)103 prio_changed_stop(struct rq *rq, struct task_struct *p, int oldprio)
104 {
105 BUG(); /* how!?, what priority? */
106 }
107
update_curr_stop(struct rq * rq)108 static void update_curr_stop(struct rq *rq)
109 {
110 }
111
112 /*
113 * Simple, special scheduling class for the per-CPU stop tasks:
114 */
115 const struct sched_class stop_sched_class
116 __section("__stop_sched_class") = {
117
118 .enqueue_task = enqueue_task_stop,
119 .dequeue_task = dequeue_task_stop,
120 .yield_task = yield_task_stop,
121
122 .check_preempt_curr = check_preempt_curr_stop,
123
124 .pick_next_task = pick_next_task_stop,
125 .put_prev_task = put_prev_task_stop,
126 .set_next_task = set_next_task_stop,
127
128 #ifdef CONFIG_SMP
129 .balance = balance_stop,
130 .select_task_rq = select_task_rq_stop,
131 .set_cpus_allowed = set_cpus_allowed_common,
132 #endif
133
134 .task_tick = task_tick_stop,
135
136 .prio_changed = prio_changed_stop,
137 .switched_to = switched_to_stop,
138 .update_curr = update_curr_stop,
139 #ifdef CONFIG_SCHED_WALT
140 .fixup_walt_sched_stats = fixup_walt_sched_stats_common,
141 #endif
142 };
143