xref: /linux/kernel/sched/stop_task.c (revision 14c897740388216acd59477a382de327d5e27ba4)
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 
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 rq_flags *rf)
20 {
21 	return sched_stop_runnable(rq);
22 }
23 
24 static void
25 wakeup_preempt_stop(struct rq *rq, struct task_struct *p, int flags)
26 {
27 	/* we're never preempted */
28 }
29 
30 static void set_next_task_stop(struct rq *rq, struct task_struct *stop, enum snt_e type)
31 {
32 	if (type == SNT_REPICK)
33 		return;
34 
35 	stop->se.exec_start = rq_clock_task(rq);
36 }
37 
38 static struct task_struct *pick_task_stop(struct rq *rq, struct rq_flags *rf)
39 {
40 	if (!sched_stop_runnable(rq))
41 		return NULL;
42 
43 	return rq->stop;
44 }
45 
46 static void
47 enqueue_task_stop(struct rq *rq, struct task_struct *p, int flags)
48 {
49 	add_nr_running(rq, 1);
50 }
51 
52 static bool
53 dequeue_task_stop(struct rq *rq, struct task_struct *p, int flags)
54 {
55 	sub_nr_running(rq, 1);
56 	return true;
57 }
58 
59 static void yield_task_stop(struct rq *rq)
60 {
61 	BUG(); /* the stop task should never yield, its pointless. */
62 }
63 
64 static void put_prev_task_stop(struct rq *rq, struct task_struct *prev, struct task_struct *next)
65 {
66 	update_curr_common(rq);
67 }
68 
69 /*
70  * scheduler tick hitting a task of our scheduling class.
71  *
72  * NOTE: This function can be called remotely by the tick offload that
73  * goes along full dynticks. Therefore no local assumption can be made
74  * and everything must be accessed through the @rq and @curr passed in
75  * parameters.
76  */
77 static void task_tick_stop(struct rq *rq, struct task_struct *curr, int queued)
78 {
79 }
80 
81 static void switching_to_stop(struct rq *rq, struct task_struct *p)
82 {
83 	BUG(); /* its impossible to change to this class */
84 }
85 
86 static void
87 prio_changed_stop(struct rq *rq, struct task_struct *p, u64 oldprio)
88 {
89 	if (p->prio == oldprio)
90 		return;
91 
92 	BUG(); /* how!?, what priority? */
93 }
94 
95 static void update_curr_stop(struct rq *rq)
96 {
97 }
98 
99 /*
100  * Simple, special scheduling class for the per-CPU stop tasks:
101  */
102 DEFINE_SCHED_CLASS(stop) = {
103 	.enqueue_task		= enqueue_task_stop,
104 	.dequeue_task		= dequeue_task_stop,
105 	.yield_task		= yield_task_stop,
106 
107 	.wakeup_preempt		= wakeup_preempt_stop,
108 
109 	.pick_task		= pick_task_stop,
110 	.put_prev_task		= put_prev_task_stop,
111 	.set_next_task          = set_next_task_stop,
112 
113 	.balance		= balance_stop,
114 	.select_task_rq		= select_task_rq_stop,
115 	.set_cpus_allowed	= set_cpus_allowed_common,
116 
117 	.task_tick		= task_tick_stop,
118 
119 	.prio_changed		= prio_changed_stop,
120 	.switching_to		= switching_to_stop,
121 	.update_curr		= update_curr_stop,
122 };
123