Skip to content

Commit 34f971f

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched: Create special class for stop/migrate work
In order to separate the stop/migrate work thread from the SCHED_FIFO implementation, create a special class for it that is of higher priority than SCHED_FIFO itself. This currently solves a problem where cpu-hotplug consumes so much cpu-time that the SCHED_FIFO class gets throttled, but has the bandwidth replenishment timer pending on the now dead cpu. It is also required for when we add the planned deadline scheduling class above SCHED_FIFO, as the stop/migrate thread still needs to transcent those tasks. Tested-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <1285165776.2275.1022.camel@laptop> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 4924627 commit 34f971f

3 files changed

Lines changed: 158 additions & 12 deletions

File tree

‎kernel/sched.c‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ struct rq {
486486
*/
487487
unsigned long nr_uninterruptible;
488488

489-
struct task_struct *curr, *idle;
489+
struct task_struct *curr, *idle, *stop;
490490
unsigned long next_balance;
491491
struct mm_struct *prev_mm;
492492

@@ -1837,7 +1837,7 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
18371837

18381838
static const struct sched_class rt_sched_class;
18391839

1840-
#define sched_class_highest (&rt_sched_class)
1840+
#define sched_class_highest (&stop_sched_class)
18411841
#define for_each_class(class) \
18421842
for (class = sched_class_highest; class; class = class->next)
18431843

@@ -1917,10 +1917,41 @@ static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
19171917
#include "sched_idletask.c"
19181918
#include "sched_fair.c"
19191919
#include "sched_rt.c"
1920+
#include "sched_stoptask.c"
19201921
#ifdef CONFIG_SCHED_DEBUG
19211922
# include "sched_debug.c"
19221923
#endif
19231924

1925+
void sched_set_stop_task(int cpu, struct task_struct *stop)
1926+
{
1927+
struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1928+
struct task_struct *old_stop = cpu_rq(cpu)->stop;
1929+
1930+
if (stop) {
1931+
/*
1932+
* Make it appear like a SCHED_FIFO task, its something
1933+
* userspace knows about and won't get confused about.
1934+
*
1935+
* Also, it will make PI more or less work without too
1936+
* much confusion -- but then, stop work should not
1937+
* rely on PI working anyway.
1938+
*/
1939+
sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
1940+
1941+
stop->sched_class = &stop_sched_class;
1942+
}
1943+
1944+
cpu_rq(cpu)->stop = stop;
1945+
1946+
if (old_stop) {
1947+
/*
1948+
* Reset it back to a normal scheduling class so that
1949+
* it can die in pieces.
1950+
*/
1951+
old_stop->sched_class = &rt_sched_class;
1952+
}
1953+
}
1954+
19241955
/*
19251956
* __normal_prio - return the priority that is based on the static prio
19261957
*/
@@ -3720,17 +3751,13 @@ pick_next_task(struct rq *rq)
37203751
return p;
37213752
}
37223753

3723-
class = sched_class_highest;
3724-
for ( ; ; ) {
3754+
for_each_class(class) {
37253755
p = class->pick_next_task(rq);
37263756
if (p)
37273757
return p;
3728-
/*
3729-
* Will never be NULL as the idle class always
3730-
* returns a non-NULL p:
3731-
*/
3732-
class = class->next;
37333758
}
3759+
3760+
BUG(); /* the idle class will always have a runnable task */
37343761
}
37353762

37363763
/*
@@ -4659,6 +4686,15 @@ static int __sched_setscheduler(struct task_struct *p, int policy,
46594686
*/
46604687
rq = __task_rq_lock(p);
46614688

4689+
/*
4690+
* Changing the policy of the stop threads its a very bad idea
4691+
*/
4692+
if (p == rq->stop) {
4693+
__task_rq_unlock(rq);
4694+
raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4695+
return -EINVAL;
4696+
}
4697+
46624698
#ifdef CONFIG_RT_GROUP_SCHED
46634699
if (user) {
46644700
/*

‎kernel/sched_stoptask.c‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* stop-task scheduling class.
3+
*
4+
* The stop task is the highest priority task in the system, it preempts
5+
* everything and will be preempted by nothing.
6+
*
7+
* See kernel/stop_machine.c
8+
*/
9+
10+
#ifdef CONFIG_SMP
11+
static int
12+
select_task_rq_stop(struct rq *rq, struct task_struct *p,
13+
int sd_flag, int flags)
14+
{
15+
return task_cpu(p); /* stop tasks as never migrate */
16+
}
17+
#endif /* CONFIG_SMP */
18+
19+
static void
20+
check_preempt_curr_stop(struct rq *rq, struct task_struct *p, int flags)
21+
{
22+
resched_task(rq->curr); /* we preempt everything */
23+
}
24+
25+
static struct task_struct *pick_next_task_stop(struct rq *rq)
26+
{
27+
struct task_struct *stop = rq->stop;
28+
29+
if (stop && stop->state == TASK_RUNNING)
30+
return stop;
31+
32+
return NULL;
33+
}
34+
35+
static void
36+
enqueue_task_stop(struct rq *rq, struct task_struct *p, int flags)
37+
{
38+
}
39+
40+
static void
41+
dequeue_task_stop(struct rq *rq, struct task_struct *p, int flags)
42+
{
43+
}
44+
45+
static void yield_task_stop(struct rq *rq)
46+
{
47+
BUG(); /* the stop task should never yield, its pointless. */
48+
}
49+
50+
static void put_prev_task_stop(struct rq *rq, struct task_struct *prev)
51+
{
52+
}
53+
54+
static void task_tick_stop(struct rq *rq, struct task_struct *curr, int queued)
55+
{
56+
}
57+
58+
static void set_curr_task_stop(struct rq *rq)
59+
{
60+
}
61+
62+
static void switched_to_stop(struct rq *rq, struct task_struct *p,
63+
int running)
64+
{
65+
BUG(); /* its impossible to change to this class */
66+
}
67+
68+
static void prio_changed_stop(struct rq *rq, struct task_struct *p,
69+
int oldprio, int running)
70+
{
71+
BUG(); /* how!?, what priority? */
72+
}
73+
74+
static unsigned int
75+
get_rr_interval_stop(struct rq *rq, struct task_struct *task)
76+
{
77+
return 0;
78+
}
79+
80+
/*
81+
* Simple, special scheduling class for the per-CPU stop tasks:
82+
*/
83+
static const struct sched_class stop_sched_class = {
84+
.next = &rt_sched_class,
85+
86+
.enqueue_task = enqueue_task_stop,
87+
.dequeue_task = dequeue_task_stop,
88+
.yield_task = yield_task_stop,
89+
90+
.check_preempt_curr = check_preempt_curr_stop,
91+
92+
.pick_next_task = pick_next_task_stop,
93+
.put_prev_task = put_prev_task_stop,
94+
95+
#ifdef CONFIG_SMP
96+
.select_task_rq = select_task_rq_stop,
97+
#endif
98+
99+
.set_curr_task = set_curr_task_stop,
100+
.task_tick = task_tick_stop,
101+
102+
.get_rr_interval = get_rr_interval_stop,
103+
104+
.prio_changed = prio_changed_stop,
105+
.switched_to = switched_to_stop,
106+
107+
/* no .task_new for stop tasks */
108+
};

‎kernel/stop_machine.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,12 @@ static int cpu_stopper_thread(void *data)
287287
goto repeat;
288288
}
289289

290+
extern void sched_set_stop_task(int cpu, struct task_struct *stop);
291+
290292
/* manage stopper for a cpu, mostly lifted from sched migration thread mgmt */
291293
static int __cpuinit cpu_stop_cpu_callback(struct notifier_block *nfb,
292294
unsigned long action, void *hcpu)
293295
{
294-
struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
295296
unsigned int cpu = (unsigned long)hcpu;
296297
struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
297298
struct task_struct *p;
@@ -304,13 +305,13 @@ static int __cpuinit cpu_stop_cpu_callback(struct notifier_block *nfb,
304305
cpu);
305306
if (IS_ERR(p))
306307
return NOTIFY_BAD;
307-
sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
308308
get_task_struct(p);
309+
kthread_bind(p, cpu);
310+
sched_set_stop_task(cpu, p);
309311
stopper->thread = p;
310312
break;
311313

312314
case CPU_ONLINE:
313-
kthread_bind(stopper->thread, cpu);
314315
/* strictly unnecessary, as first user will wake it */
315316
wake_up_process(stopper->thread);
316317
/* mark enabled */
@@ -325,6 +326,7 @@ static int __cpuinit cpu_stop_cpu_callback(struct notifier_block *nfb,
325326
{
326327
struct cpu_stop_work *work;
327328

329+
sched_set_stop_task(cpu, NULL);
328330
/* kill the stopper */
329331
kthread_stop(stopper->thread);
330332
/* drain remaining works */

0 commit comments

Comments
 (0)