blob: 0b165a25f22f84695c9c90bd7fbc77fcf8c2a3de [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002/*
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 */
Ingo Molnar97fb7a02018-03-03 14:01:12 +010010#include "sched.h"
Peter Zijlstra34f971f2010-09-22 13:53:15 +020011
12#ifdef CONFIG_SMP
13static int
Valentin Schneider3aef1552020-11-02 18:45:13 +000014select_task_rq_stop(struct task_struct *p, int cpu, int flags)
Peter Zijlstra34f971f2010-09-22 13:53:15 +020015{
16 return task_cpu(p); /* stop tasks as never migrate */
17}
Peter Zijlstra6e2df052019-11-08 11:11:52 +010018
19static int
20balance_stop(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
21{
22 return sched_stop_runnable(rq);
23}
Peter Zijlstra34f971f2010-09-22 13:53:15 +020024#endif /* CONFIG_SMP */
25
26static void
27check_preempt_curr_stop(struct rq *rq, struct task_struct *p, int flags)
28{
Peter Zijlstra1e5a7402010-10-31 12:37:04 +010029 /* we're never preempted */
Peter Zijlstra34f971f2010-09-22 13:53:15 +020030}
31
Peter Zijlstraa0e813f2019-11-08 14:16:00 +010032static void set_next_task_stop(struct rq *rq, struct task_struct *stop, bool first)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +000033{
34 stop->se.exec_start = rq_clock_task(rq);
35}
36
Peter Zijlstra21f56ffe2020-11-17 18:19:32 -050037static struct task_struct *pick_task_stop(struct rq *rq)
Peter Zijlstra34f971f2010-09-22 13:53:15 +020038{
Peter Zijlstra6e2df052019-11-08 11:11:52 +010039 if (!sched_stop_runnable(rq))
Peter Zijlstra606dba22012-02-11 06:05:00 +010040 return NULL;
Peter Zijlstra34f971f2010-09-22 13:53:15 +020041
Peter Zijlstra6e2df052019-11-08 11:11:52 +010042 return rq->stop;
Peter Zijlstra34f971f2010-09-22 13:53:15 +020043}
44
Peter Zijlstra21f56ffe2020-11-17 18:19:32 -050045static struct task_struct *pick_next_task_stop(struct rq *rq)
46{
47 struct task_struct *p = pick_task_stop(rq);
48
49 if (p)
50 set_next_task_stop(rq, p, true);
51
52 return p;
53}
54
Peter Zijlstra34f971f2010-09-22 13:53:15 +020055static void
56enqueue_task_stop(struct rq *rq, struct task_struct *p, int flags)
57{
Kirill Tkhai72465442014-05-09 03:00:14 +040058 add_nr_running(rq, 1);
Peter Zijlstra34f971f2010-09-22 13:53:15 +020059}
60
61static void
62dequeue_task_stop(struct rq *rq, struct task_struct *p, int flags)
63{
Kirill Tkhai72465442014-05-09 03:00:14 +040064 sub_nr_running(rq, 1);
Peter Zijlstra34f971f2010-09-22 13:53:15 +020065}
66
67static void yield_task_stop(struct rq *rq)
68{
69 BUG(); /* the stop task should never yield, its pointless. */
70}
71
Peter Zijlstra6e2df052019-11-08 11:11:52 +010072static void put_prev_task_stop(struct rq *rq, struct task_struct *prev)
Peter Zijlstra34f971f2010-09-22 13:53:15 +020073{
Mike Galbraith8f618962012-08-04 05:44:14 +020074 struct task_struct *curr = rq->curr;
75 u64 delta_exec;
76
Frederic Weisbecker78becc22013-04-12 01:51:02 +020077 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Mike Galbraith8f618962012-08-04 05:44:14 +020078 if (unlikely((s64)delta_exec < 0))
79 delta_exec = 0;
80
Yafang Shaoceeadb82021-09-05 14:35:41 +000081 schedstat_set(curr->stats.exec_max,
82 max(curr->stats.exec_max, delta_exec));
Mike Galbraith8f618962012-08-04 05:44:14 +020083
84 curr->se.sum_exec_runtime += delta_exec;
85 account_group_exec_runtime(curr, delta_exec);
86
Frederic Weisbecker78becc22013-04-12 01:51:02 +020087 curr->se.exec_start = rq_clock_task(rq);
Tejun Heod2cc5ed2017-09-25 08:12:04 -070088 cgroup_account_cputime(curr, delta_exec);
Peter Zijlstra34f971f2010-09-22 13:53:15 +020089}
90
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +010091/*
92 * scheduler tick hitting a task of our scheduling class.
93 *
94 * NOTE: This function can be called remotely by the tick offload that
95 * goes along full dynticks. Therefore no local assumption can be made
96 * and everything must be accessed through the @rq and @curr passed in
97 * parameters.
98 */
Peter Zijlstra34f971f2010-09-22 13:53:15 +020099static void task_tick_stop(struct rq *rq, struct task_struct *curr, int queued)
100{
101}
102
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100103static void switched_to_stop(struct rq *rq, struct task_struct *p)
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200104{
105 BUG(); /* its impossible to change to this class */
106}
107
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100108static void
109prio_changed_stop(struct rq *rq, struct task_struct *p, int oldprio)
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200110{
111 BUG(); /* how!?, what priority? */
112}
113
Thomas Gleixner90e362f2014-11-23 23:04:52 +0100114static void update_curr_stop(struct rq *rq)
115{
116}
117
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200118/*
119 * Simple, special scheduling class for the per-CPU stop tasks:
120 */
Peter Zijlstra43c31ac2020-10-21 15:45:33 +0200121DEFINE_SCHED_CLASS(stop) = {
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200122
123 .enqueue_task = enqueue_task_stop,
124 .dequeue_task = dequeue_task_stop,
125 .yield_task = yield_task_stop,
126
127 .check_preempt_curr = check_preempt_curr_stop,
128
129 .pick_next_task = pick_next_task_stop,
130 .put_prev_task = put_prev_task_stop,
Peter Zijlstra03b7fad2019-05-29 20:36:41 +0000131 .set_next_task = set_next_task_stop,
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200132
133#ifdef CONFIG_SMP
Peter Zijlstra6e2df052019-11-08 11:11:52 +0100134 .balance = balance_stop,
Peter Zijlstra21f56ffe2020-11-17 18:19:32 -0500135 .pick_task = pick_task_stop,
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200136 .select_task_rq = select_task_rq_stop,
Peter Zijlstrac5b28032015-05-15 17:43:35 +0200137 .set_cpus_allowed = set_cpus_allowed_common,
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200138#endif
139
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200140 .task_tick = task_tick_stop,
141
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200142 .prio_changed = prio_changed_stop,
143 .switched_to = switched_to_stop,
Thomas Gleixner90e362f2014-11-23 23:04:52 +0100144 .update_curr = update_curr_stop,
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200145};