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