diff options
author | Mac Mollison <mollison@cs.unc.edu> | 2010-09-22 00:18:10 -0400 |
---|---|---|
committer | Mac Mollison <mollison@cs.unc.edu> | 2010-09-22 00:18:10 -0400 |
commit | 47aa67896622051a9069fd503bf960b57898aece (patch) | |
tree | 76f5817a4c4ee309f1586b8a4d2f6599547de5b5 /litmus/sched_mcrit.c | |
parent | 136a08dbe8c28e751b01e932420f715edb229f6b (diff) |
Cloned GSN-EDF to serve as stub for mcrit
Diffstat (limited to 'litmus/sched_mcrit.c')
-rw-r--r-- | litmus/sched_mcrit.c | 844 |
1 files changed, 844 insertions, 0 deletions
diff --git a/litmus/sched_mcrit.c b/litmus/sched_mcrit.c new file mode 100644 index 000000000000..a5da92d1115c --- /dev/null +++ b/litmus/sched_mcrit.c | |||
@@ -0,0 +1,844 @@ | |||
1 | /* | ||
2 | * litmus/sched_mcrit.c | ||
3 | * | ||
4 | * Mixed Criticality plugin | ||
5 | * | ||
6 | * Cloned from GSN-EDF | ||
7 | * | ||
8 | * "This version uses the simple approach and serializes all scheduling | ||
9 | * decisions by the use of a queue lock. This is probably not the | ||
10 | * best way to do it, but it should suffice for now." -GSN-EDF | ||
11 | */ | ||
12 | |||
13 | #include <linux/spinlock.h> | ||
14 | #include <linux/percpu.h> | ||
15 | #include <linux/sched.h> | ||
16 | |||
17 | #include <litmus/litmus.h> | ||
18 | #include <litmus/jobs.h> | ||
19 | #include <litmus/sched_plugin.h> | ||
20 | #include <litmus/edf_common.h> | ||
21 | #include <litmus/sched_trace.h> | ||
22 | |||
23 | #include <litmus/bheap.h> | ||
24 | |||
25 | #include <linux/module.h> | ||
26 | |||
27 | /* Overview of GSN-EDF operations. | ||
28 | * | ||
29 | * For a detailed explanation of GSN-EDF have a look at the FMLP paper. This | ||
30 | * description only covers how the individual operations are implemented in | ||
31 | * LITMUS. | ||
32 | * | ||
33 | * link_task_to_cpu(T, cpu) - Low-level operation to update the linkage | ||
34 | * structure (NOT the actually scheduled | ||
35 | * task). If there is another linked task To | ||
36 | * already it will set To->linked_on = NO_CPU | ||
37 | * (thereby removing its association with this | ||
38 | * CPU). However, it will not requeue the | ||
39 | * previously linked task (if any). It will set | ||
40 | * T's state to RT_F_RUNNING and check whether | ||
41 | * it is already running somewhere else. If T | ||
42 | * is scheduled somewhere else it will link | ||
43 | * it to that CPU instead (and pull the linked | ||
44 | * task to cpu). T may be NULL. | ||
45 | * | ||
46 | * unlink(T) - Unlink removes T from all scheduler data | ||
47 | * structures. If it is linked to some CPU it | ||
48 | * will link NULL to that CPU. If it is | ||
49 | * currently queued in the mcrit queue it will | ||
50 | * be removed from the rt_domain. It is safe to | ||
51 | * call unlink(T) if T is not linked. T may not | ||
52 | * be NULL. | ||
53 | * | ||
54 | * requeue(T) - Requeue will insert T into the appropriate | ||
55 | * queue. If the system is in real-time mode and | ||
56 | * the T is released already, it will go into the | ||
57 | * ready queue. If the system is not in | ||
58 | * real-time mode is T, then T will go into the | ||
59 | * release queue. If T's release time is in the | ||
60 | * future, it will go into the release | ||
61 | * queue. That means that T's release time/job | ||
62 | * no/etc. has to be updated before requeu(T) is | ||
63 | * called. It is not safe to call requeue(T) | ||
64 | * when T is already queued. T may not be NULL. | ||
65 | * | ||
66 | * mcrit_job_arrival(T) - This is the catch all function when T enters | ||
67 | * the system after either a suspension or at a | ||
68 | * job release. It will queue T (which means it | ||
69 | * is not safe to call mcrit_job_arrival(T) if | ||
70 | * T is already queued) and then check whether a | ||
71 | * preemption is necessary. If a preemption is | ||
72 | * necessary it will update the linkage | ||
73 | * accordingly and cause scheduled to be called | ||
74 | * (either with an IPI or need_resched). It is | ||
75 | * safe to call mcrit_job_arrival(T) if T's | ||
76 | * next job has not been actually released yet | ||
77 | * (releast time in the future). T will be put | ||
78 | * on the release queue in that case. | ||
79 | * | ||
80 | * job_completion(T) - Take care of everything that needs to be done | ||
81 | * to prepare T for its next release and place | ||
82 | * it in the right queue with | ||
83 | * mcrit_job_arrival(). | ||
84 | * | ||
85 | * | ||
86 | * When we now that T is linked to CPU then link_task_to_cpu(NULL, CPU) is | ||
87 | * equivalent to unlink(T). Note that if you unlink a task from a CPU none of | ||
88 | * the functions will automatically propagate pending task from the ready queue | ||
89 | * to a linked task. This is the job of the calling function ( by means of | ||
90 | * __take_ready). | ||
91 | */ | ||
92 | |||
93 | |||
94 | /* cpu_entry_t - maintain the linked and scheduled state | ||
95 | */ | ||
96 | typedef struct { | ||
97 | int cpu; | ||
98 | struct task_struct* linked; /* only RT tasks */ | ||
99 | struct task_struct* scheduled; /* only RT tasks */ | ||
100 | atomic_t will_schedule; /* prevent unneeded IPIs */ | ||
101 | struct bheap_node* hn; | ||
102 | } cpu_entry_t; | ||
103 | DEFINE_PER_CPU(cpu_entry_t, mcrit_cpu_entries); | ||
104 | |||
105 | cpu_entry_t* mcrit_cpus[NR_CPUS]; | ||
106 | |||
107 | #define set_will_schedule() \ | ||
108 | (atomic_set(&__get_cpu_var(mcrit_cpu_entries).will_schedule, 1)) | ||
109 | #define clear_will_schedule() \ | ||
110 | (atomic_set(&__get_cpu_var(mcrit_cpu_entries).will_schedule, 0)) | ||
111 | #define test_will_schedule(cpu) \ | ||
112 | (atomic_read(&per_cpu(mcrit_cpu_entries, cpu).will_schedule)) | ||
113 | |||
114 | |||
115 | /* the cpus queue themselves according to priority in here */ | ||
116 | static struct bheap_node mcrit_heap_node[NR_CPUS]; | ||
117 | static struct bheap mcrit_cpu_heap; | ||
118 | |||
119 | static rt_domain_t mcrit; | ||
120 | #define mcrit_lock (mcrit.ready_lock) | ||
121 | |||
122 | |||
123 | /* Uncomment this if you want to see all scheduling decisions in the | ||
124 | * TRACE() log. | ||
125 | #define WANT_ALL_SCHED_EVENTS | ||
126 | */ | ||
127 | |||
128 | static int cpu_lower_prio(struct bheap_node *_a, struct bheap_node *_b) | ||
129 | { | ||
130 | cpu_entry_t *a, *b; | ||
131 | a = _a->value; | ||
132 | b = _b->value; | ||
133 | /* Note that a and b are inverted: we want the lowest-priority CPU at | ||
134 | * the top of the heap. | ||
135 | */ | ||
136 | return edf_higher_prio(b->linked, a->linked); | ||
137 | } | ||
138 | |||
139 | /* update_cpu_position - Move the cpu entry to the correct place to maintain | ||
140 | * order in the cpu queue. Caller must hold mcrit lock. | ||
141 | */ | ||
142 | static void update_cpu_position(cpu_entry_t *entry) | ||
143 | { | ||
144 | if (likely(bheap_node_in_heap(entry->hn))) | ||
145 | bheap_delete(cpu_lower_prio, &mcrit_cpu_heap, entry->hn); | ||
146 | bheap_insert(cpu_lower_prio, &mcrit_cpu_heap, entry->hn); | ||
147 | } | ||
148 | |||
149 | /* caller must hold mcrit lock */ | ||
150 | static cpu_entry_t* lowest_prio_cpu(void) | ||
151 | { | ||
152 | struct bheap_node* hn; | ||
153 | hn = bheap_peek(cpu_lower_prio, &mcrit_cpu_heap); | ||
154 | return hn->value; | ||
155 | } | ||
156 | |||
157 | |||
158 | /* link_task_to_cpu - Update the link of a CPU. | ||
159 | * Handles the case where the to-be-linked task is already | ||
160 | * scheduled on a different CPU. | ||
161 | */ | ||
162 | static noinline void link_task_to_cpu(struct task_struct* linked, | ||
163 | cpu_entry_t *entry) | ||
164 | { | ||
165 | cpu_entry_t *sched; | ||
166 | struct task_struct* tmp; | ||
167 | int on_cpu; | ||
168 | |||
169 | BUG_ON(linked && !is_realtime(linked)); | ||
170 | |||
171 | /* Currently linked task is set to be unlinked. */ | ||
172 | if (entry->linked) { | ||
173 | entry->linked->rt_param.linked_on = NO_CPU; | ||
174 | } | ||
175 | |||
176 | /* Link new task to CPU. */ | ||
177 | if (linked) { | ||
178 | set_rt_flags(linked, RT_F_RUNNING); | ||
179 | /* handle task is already scheduled somewhere! */ | ||
180 | on_cpu = linked->rt_param.scheduled_on; | ||
181 | if (on_cpu != NO_CPU) { | ||
182 | sched = &per_cpu(mcrit_cpu_entries, on_cpu); | ||
183 | /* this should only happen if not linked already */ | ||
184 | BUG_ON(sched->linked == linked); | ||
185 | |||
186 | /* If we are already scheduled on the CPU to which we | ||
187 | * wanted to link, we don't need to do the swap -- | ||
188 | * we just link ourselves to the CPU and depend on | ||
189 | * the caller to get things right. | ||
190 | */ | ||
191 | if (entry != sched) { | ||
192 | TRACE_TASK(linked, | ||
193 | "already scheduled on %d, updating link.\n", | ||
194 | sched->cpu); | ||
195 | tmp = sched->linked; | ||
196 | linked->rt_param.linked_on = sched->cpu; | ||
197 | sched->linked = linked; | ||
198 | update_cpu_position(sched); | ||
199 | linked = tmp; | ||
200 | } | ||
201 | } | ||
202 | if (linked) /* might be NULL due to swap */ | ||
203 | linked->rt_param.linked_on = entry->cpu; | ||
204 | } | ||
205 | entry->linked = linked; | ||
206 | #ifdef WANT_ALL_SCHED_EVENTS | ||
207 | if (linked) | ||
208 | TRACE_TASK(linked, "linked to %d.\n", entry->cpu); | ||
209 | else | ||
210 | TRACE("NULL linked to %d.\n", entry->cpu); | ||
211 | #endif | ||
212 | update_cpu_position(entry); | ||
213 | } | ||
214 | |||
215 | /* unlink - Make sure a task is not linked any longer to an entry | ||
216 | * where it was linked before. Must hold mcrit_lock. | ||
217 | */ | ||
218 | static noinline void unlink(struct task_struct* t) | ||
219 | { | ||
220 | cpu_entry_t *entry; | ||
221 | |||
222 | if (unlikely(!t)) { | ||
223 | TRACE_BUG_ON(!t); | ||
224 | return; | ||
225 | } | ||
226 | |||
227 | if (t->rt_param.linked_on != NO_CPU) { | ||
228 | /* unlink */ | ||
229 | entry = &per_cpu(mcrit_cpu_entries, t->rt_param.linked_on); | ||
230 | t->rt_param.linked_on = NO_CPU; | ||
231 | link_task_to_cpu(NULL, entry); | ||
232 | } else if (is_queued(t)) { | ||
233 | /* This is an interesting situation: t is scheduled, | ||
234 | * but was just recently unlinked. It cannot be | ||
235 | * linked anywhere else (because then it would have | ||
236 | * been relinked to this CPU), thus it must be in some | ||
237 | * queue. We must remove it from the list in this | ||
238 | * case. | ||
239 | */ | ||
240 | remove(&mcrit, t); | ||
241 | } | ||
242 | } | ||
243 | |||
244 | |||
245 | /* preempt - force a CPU to reschedule | ||
246 | */ | ||
247 | static void preempt(cpu_entry_t *entry) | ||
248 | { | ||
249 | preempt_if_preemptable(entry->scheduled, entry->cpu); | ||
250 | } | ||
251 | |||
252 | /* requeue - Put an unlinked task into gsn-edf domain. | ||
253 | * Caller must hold mcrit_lock. | ||
254 | */ | ||
255 | static noinline void requeue(struct task_struct* task) | ||
256 | { | ||
257 | BUG_ON(!task); | ||
258 | /* sanity check before insertion */ | ||
259 | BUG_ON(is_queued(task)); | ||
260 | |||
261 | if (is_released(task, litmus_clock())) | ||
262 | __add_ready(&mcrit, task); | ||
263 | else { | ||
264 | /* it has got to wait */ | ||
265 | add_release(&mcrit, task); | ||
266 | } | ||
267 | } | ||
268 | |||
269 | /* check for any necessary preemptions */ | ||
270 | static void check_for_preemptions(void) | ||
271 | { | ||
272 | struct task_struct *task; | ||
273 | cpu_entry_t* last; | ||
274 | |||
275 | for(last = lowest_prio_cpu(); | ||
276 | edf_preemption_needed(&mcrit, last->linked); | ||
277 | last = lowest_prio_cpu()) { | ||
278 | /* preemption necessary */ | ||
279 | task = __take_ready(&mcrit); | ||
280 | TRACE("check_for_preemptions: attempting to link task %d to %d\n", | ||
281 | task->pid, last->cpu); | ||
282 | if (last->linked) | ||
283 | requeue(last->linked); | ||
284 | link_task_to_cpu(task, last); | ||
285 | preempt(last); | ||
286 | } | ||
287 | } | ||
288 | |||
289 | /* mcrit_job_arrival: task is either resumed or released */ | ||
290 | static noinline void mcrit_job_arrival(struct task_struct* task) | ||
291 | { | ||
292 | BUG_ON(!task); | ||
293 | |||
294 | requeue(task); | ||
295 | check_for_preemptions(); | ||
296 | } | ||
297 | |||
298 | static void mcrit_release_jobs(rt_domain_t* rt, struct bheap* tasks) | ||
299 | { | ||
300 | unsigned long flags; | ||
301 | |||
302 | raw_spin_lock_irqsave(&mcrit_lock, flags); | ||
303 | |||
304 | __merge_ready(rt, tasks); | ||
305 | check_for_preemptions(); | ||
306 | |||
307 | raw_spin_unlock_irqrestore(&mcrit_lock, flags); | ||
308 | } | ||
309 | |||
310 | /* caller holds mcrit_lock */ | ||
311 | static noinline void job_completion(struct task_struct *t, int forced) | ||
312 | { | ||
313 | BUG_ON(!t); | ||
314 | |||
315 | sched_trace_task_completion(t, forced); | ||
316 | |||
317 | TRACE_TASK(t, "job_completion().\n"); | ||
318 | |||
319 | /* set flags */ | ||
320 | set_rt_flags(t, RT_F_SLEEP); | ||
321 | /* prepare for next period */ | ||
322 | prepare_for_next_period(t); | ||
323 | if (is_released(t, litmus_clock())) | ||
324 | sched_trace_task_release(t); | ||
325 | /* unlink */ | ||
326 | unlink(t); | ||
327 | /* requeue | ||
328 | * But don't requeue a blocking task. */ | ||
329 | if (is_running(t)) | ||
330 | mcrit_job_arrival(t); | ||
331 | } | ||
332 | |||
333 | /* mcrit_tick - this function is called for every local timer | ||
334 | * interrupt. | ||
335 | * | ||
336 | * checks whether the current task has expired and checks | ||
337 | * whether we need to preempt it if it has not expired | ||
338 | */ | ||
339 | static void mcrit_tick(struct task_struct* t) | ||
340 | { | ||
341 | if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) { | ||
342 | if (!is_np(t)) { | ||
343 | /* np tasks will be preempted when they become | ||
344 | * preemptable again | ||
345 | */ | ||
346 | set_tsk_need_resched(t); | ||
347 | set_will_schedule(); | ||
348 | TRACE("mcrit_scheduler_tick: " | ||
349 | "%d is preemptable " | ||
350 | " => FORCE_RESCHED\n", t->pid); | ||
351 | } else if (is_user_np(t)) { | ||
352 | TRACE("mcrit_scheduler_tick: " | ||
353 | "%d is non-preemptable, " | ||
354 | "preemption delayed.\n", t->pid); | ||
355 | request_exit_np(t); | ||
356 | } | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /* Getting schedule() right is a bit tricky. schedule() may not make any | ||
361 | * assumptions on the state of the current task since it may be called for a | ||
362 | * number of reasons. The reasons include a scheduler_tick() determined that it | ||
363 | * was necessary, because sys_exit_np() was called, because some Linux | ||
364 | * subsystem determined so, or even (in the worst case) because there is a bug | ||
365 | * hidden somewhere. Thus, we must take extreme care to determine what the | ||
366 | * current state is. | ||
367 | * | ||
368 | * The CPU could currently be scheduling a task (or not), be linked (or not). | ||
369 | * | ||
370 | * The following assertions for the scheduled task could hold: | ||
371 | * | ||
372 | * - !is_running(scheduled) // the job blocks | ||
373 | * - scheduled->timeslice == 0 // the job completed (forcefully) | ||
374 | * - get_rt_flag() == RT_F_SLEEP // the job completed (by syscall) | ||
375 | * - linked != scheduled // we need to reschedule (for any reason) | ||
376 | * - is_np(scheduled) // rescheduling must be delayed, | ||
377 | * sys_exit_np must be requested | ||
378 | * | ||
379 | * Any of these can occur together. | ||
380 | */ | ||
381 | static struct task_struct* mcrit_schedule(struct task_struct * prev) | ||
382 | { | ||
383 | cpu_entry_t* entry = &__get_cpu_var(mcrit_cpu_entries); | ||
384 | int out_of_time, sleep, preempt, np, exists, blocks; | ||
385 | struct task_struct* next = NULL; | ||
386 | |||
387 | #ifdef CONFIG_RELEASE_MASTER | ||
388 | /* Bail out early if we are the release master. | ||
389 | * The release master never schedules any real-time tasks. | ||
390 | */ | ||
391 | if (mcrit.release_master == entry->cpu) | ||
392 | return NULL; | ||
393 | #endif | ||
394 | |||
395 | raw_spin_lock(&mcrit_lock); | ||
396 | clear_will_schedule(); | ||
397 | |||
398 | /* sanity checking */ | ||
399 | BUG_ON(entry->scheduled && entry->scheduled != prev); | ||
400 | BUG_ON(entry->scheduled && !is_realtime(prev)); | ||
401 | BUG_ON(is_realtime(prev) && !entry->scheduled); | ||
402 | |||
403 | /* (0) Determine state */ | ||
404 | exists = entry->scheduled != NULL; | ||
405 | blocks = exists && !is_running(entry->scheduled); | ||
406 | out_of_time = exists && | ||
407 | budget_enforced(entry->scheduled) && | ||
408 | budget_exhausted(entry->scheduled); | ||
409 | np = exists && is_np(entry->scheduled); | ||
410 | sleep = exists && get_rt_flags(entry->scheduled) == RT_F_SLEEP; | ||
411 | preempt = entry->scheduled != entry->linked; | ||
412 | |||
413 | #ifdef WANT_ALL_SCHED_EVENTS | ||
414 | TRACE_TASK(prev, "invoked mcrit_schedule.\n"); | ||
415 | #endif | ||
416 | |||
417 | if (exists) | ||
418 | TRACE_TASK(prev, | ||
419 | "blocks:%d out_of_time:%d np:%d sleep:%d preempt:%d " | ||
420 | "state:%d sig:%d\n", | ||
421 | blocks, out_of_time, np, sleep, preempt, | ||
422 | prev->state, signal_pending(prev)); | ||
423 | if (entry->linked && preempt) | ||
424 | TRACE_TASK(prev, "will be preempted by %s/%d\n", | ||
425 | entry->linked->comm, entry->linked->pid); | ||
426 | |||
427 | |||
428 | /* If a task blocks we have no choice but to reschedule. | ||
429 | */ | ||
430 | if (blocks) | ||
431 | unlink(entry->scheduled); | ||
432 | |||
433 | /* Request a sys_exit_np() call if we would like to preempt but cannot. | ||
434 | * We need to make sure to update the link structure anyway in case | ||
435 | * that we are still linked. Multiple calls to request_exit_np() don't | ||
436 | * hurt. | ||
437 | */ | ||
438 | if (np && (out_of_time || preempt || sleep)) { | ||
439 | unlink(entry->scheduled); | ||
440 | request_exit_np(entry->scheduled); | ||
441 | } | ||
442 | |||
443 | /* Any task that is preemptable and either exhausts its execution | ||
444 | * budget or wants to sleep completes. We may have to reschedule after | ||
445 | * this. Don't do a job completion if we block (can't have timers running | ||
446 | * for blocked jobs). Preemption go first for the same reason. | ||
447 | */ | ||
448 | if (!np && (out_of_time || sleep) && !blocks && !preempt) | ||
449 | job_completion(entry->scheduled, !sleep); | ||
450 | |||
451 | /* Link pending task if we became unlinked. | ||
452 | */ | ||
453 | if (!entry->linked) | ||
454 | link_task_to_cpu(__take_ready(&mcrit), entry); | ||
455 | |||
456 | /* The final scheduling decision. Do we need to switch for some reason? | ||
457 | * If linked is different from scheduled, then select linked as next. | ||
458 | */ | ||
459 | if ((!np || blocks) && | ||
460 | entry->linked != entry->scheduled) { | ||
461 | /* Schedule a linked job? */ | ||
462 | if (entry->linked) { | ||
463 | entry->linked->rt_param.scheduled_on = entry->cpu; | ||
464 | next = entry->linked; | ||
465 | } | ||
466 | if (entry->scheduled) { | ||
467 | /* not gonna be scheduled soon */ | ||
468 | entry->scheduled->rt_param.scheduled_on = NO_CPU; | ||
469 | TRACE_TASK(entry->scheduled, "scheduled_on = NO_CPU\n"); | ||
470 | } | ||
471 | } else | ||
472 | /* Only override Linux scheduler if we have a real-time task | ||
473 | * scheduled that needs to continue. | ||
474 | */ | ||
475 | if (exists) | ||
476 | next = prev; | ||
477 | |||
478 | raw_spin_unlock(&mcrit_lock); | ||
479 | |||
480 | #ifdef WANT_ALL_SCHED_EVENTS | ||
481 | TRACE("mcrit_lock released, next=0x%p\n", next); | ||
482 | |||
483 | if (next) | ||
484 | TRACE_TASK(next, "scheduled at %llu\n", litmus_clock()); | ||
485 | else if (exists && !next) | ||
486 | TRACE("becomes idle at %llu.\n", litmus_clock()); | ||
487 | #endif | ||
488 | |||
489 | |||
490 | return next; | ||
491 | } | ||
492 | |||
493 | |||
494 | /* _finish_switch - we just finished the switch away from prev | ||
495 | */ | ||
496 | static void mcrit_finish_switch(struct task_struct *prev) | ||
497 | { | ||
498 | cpu_entry_t* entry = &__get_cpu_var(mcrit_cpu_entries); | ||
499 | |||
500 | entry->scheduled = is_realtime(current) ? current : NULL; | ||
501 | #ifdef WANT_ALL_SCHED_EVENTS | ||
502 | TRACE_TASK(prev, "switched away from\n"); | ||
503 | #endif | ||
504 | } | ||
505 | |||
506 | |||
507 | /* Prepare a task for running in RT mode | ||
508 | */ | ||
509 | static void mcrit_task_new(struct task_struct * t, int on_rq, int running) | ||
510 | { | ||
511 | unsigned long flags; | ||
512 | cpu_entry_t* entry; | ||
513 | |||
514 | TRACE("gsn edf: task new %d\n", t->pid); | ||
515 | |||
516 | raw_spin_lock_irqsave(&mcrit_lock, flags); | ||
517 | |||
518 | /* setup job params */ | ||
519 | release_at(t, litmus_clock()); | ||
520 | |||
521 | if (running) { | ||
522 | entry = &per_cpu(mcrit_cpu_entries, task_cpu(t)); | ||
523 | BUG_ON(entry->scheduled); | ||
524 | |||
525 | #ifdef CONFIG_RELEASE_MASTER | ||
526 | if (entry->cpu != mcrit.release_master) { | ||
527 | #endif | ||
528 | entry->scheduled = t; | ||
529 | tsk_rt(t)->scheduled_on = task_cpu(t); | ||
530 | #ifdef CONFIG_RELEASE_MASTER | ||
531 | } else { | ||
532 | /* do not schedule on release master */ | ||
533 | preempt(entry); /* force resched */ | ||
534 | tsk_rt(t)->scheduled_on = NO_CPU; | ||
535 | } | ||
536 | #endif | ||
537 | } else { | ||
538 | t->rt_param.scheduled_on = NO_CPU; | ||
539 | } | ||
540 | t->rt_param.linked_on = NO_CPU; | ||
541 | |||
542 | mcrit_job_arrival(t); | ||
543 | raw_spin_unlock_irqrestore(&mcrit_lock, flags); | ||
544 | } | ||
545 | |||
546 | static void mcrit_task_wake_up(struct task_struct *task) | ||
547 | { | ||
548 | unsigned long flags; | ||
549 | lt_t now; | ||
550 | |||
551 | TRACE_TASK(task, "wake_up at %llu\n", litmus_clock()); | ||
552 | |||
553 | raw_spin_lock_irqsave(&mcrit_lock, flags); | ||
554 | /* We need to take suspensions because of semaphores into | ||
555 | * account! If a job resumes after being suspended due to acquiring | ||
556 | * a semaphore, it should never be treated as a new job release. | ||
557 | */ | ||
558 | if (get_rt_flags(task) == RT_F_EXIT_SEM) { | ||
559 | set_rt_flags(task, RT_F_RUNNING); | ||
560 | } else { | ||
561 | now = litmus_clock(); | ||
562 | if (is_tardy(task, now)) { | ||
563 | /* new sporadic release */ | ||
564 | release_at(task, now); | ||
565 | sched_trace_task_release(task); | ||
566 | } | ||
567 | else { | ||
568 | if (task->rt.time_slice) { | ||
569 | /* came back in time before deadline | ||
570 | */ | ||
571 | set_rt_flags(task, RT_F_RUNNING); | ||
572 | } | ||
573 | } | ||
574 | } | ||
575 | mcrit_job_arrival(task); | ||
576 | raw_spin_unlock_irqrestore(&mcrit_lock, flags); | ||
577 | } | ||
578 | |||
579 | static void mcrit_task_block(struct task_struct *t) | ||
580 | { | ||
581 | unsigned long flags; | ||
582 | |||
583 | TRACE_TASK(t, "block at %llu\n", litmus_clock()); | ||
584 | |||
585 | /* unlink if necessary */ | ||
586 | raw_spin_lock_irqsave(&mcrit_lock, flags); | ||
587 | unlink(t); | ||
588 | raw_spin_unlock_irqrestore(&mcrit_lock, flags); | ||
589 | |||
590 | BUG_ON(!is_realtime(t)); | ||
591 | } | ||
592 | |||
593 | |||
594 | static void mcrit_task_exit(struct task_struct * t) | ||
595 | { | ||
596 | unsigned long flags; | ||
597 | |||
598 | /* unlink if necessary */ | ||
599 | raw_spin_lock_irqsave(&mcrit_lock, flags); | ||
600 | unlink(t); | ||
601 | if (tsk_rt(t)->scheduled_on != NO_CPU) { | ||
602 | mcrit_cpus[tsk_rt(t)->scheduled_on]->scheduled = NULL; | ||
603 | tsk_rt(t)->scheduled_on = NO_CPU; | ||
604 | } | ||
605 | raw_spin_unlock_irqrestore(&mcrit_lock, flags); | ||
606 | |||
607 | BUG_ON(!is_realtime(t)); | ||
608 | TRACE_TASK(t, "RIP\n"); | ||
609 | } | ||
610 | |||
611 | #ifdef CONFIG_FMLP | ||
612 | |||
613 | /* Update the queue position of a task that got it's priority boosted via | ||
614 | * priority inheritance. */ | ||
615 | static void update_queue_position(struct task_struct *holder) | ||
616 | { | ||
617 | /* We don't know whether holder is in the ready queue. It should, but | ||
618 | * on a budget overrun it may already be in a release queue. Hence, | ||
619 | * calling unlink() is not possible since it assumes that the task is | ||
620 | * not in a release queue. However, we can safely check whether | ||
621 | * sem->holder is currently in a queue or scheduled after locking both | ||
622 | * the release and the ready queue lock. */ | ||
623 | |||
624 | /* Assumption: caller holds mcrit_lock */ | ||
625 | |||
626 | int check_preempt = 0; | ||
627 | |||
628 | if (tsk_rt(holder)->linked_on != NO_CPU) { | ||
629 | TRACE_TASK(holder, "%s: linked on %d\n", | ||
630 | __FUNCTION__, tsk_rt(holder)->linked_on); | ||
631 | /* Holder is scheduled; need to re-order CPUs. | ||
632 | * We can't use heap_decrease() here since | ||
633 | * the cpu_heap is ordered in reverse direction, so | ||
634 | * it is actually an increase. */ | ||
635 | bheap_delete(cpu_lower_prio, &mcrit_cpu_heap, | ||
636 | mcrit_cpus[tsk_rt(holder)->linked_on]->hn); | ||
637 | bheap_insert(cpu_lower_prio, &mcrit_cpu_heap, | ||
638 | mcrit_cpus[tsk_rt(holder)->linked_on]->hn); | ||
639 | } else { | ||
640 | /* holder may be queued: first stop queue changes */ | ||
641 | raw_spin_lock(&mcrit.release_lock); | ||
642 | if (is_queued(holder)) { | ||
643 | TRACE_TASK(holder, "%s: is queued\n", | ||
644 | __FUNCTION__); | ||
645 | /* We need to update the position | ||
646 | * of holder in some heap. Note that this | ||
647 | * may be a release heap. */ | ||
648 | check_preempt = | ||
649 | !bheap_decrease(edf_ready_order, | ||
650 | tsk_rt(holder)->heap_node); | ||
651 | } else { | ||
652 | /* Nothing to do: if it is not queued and not linked | ||
653 | * then it is currently being moved by other code | ||
654 | * (e.g., a timer interrupt handler) that will use the | ||
655 | * correct priority when enqueuing the task. */ | ||
656 | TRACE_TASK(holder, "%s: is NOT queued => Done.\n", | ||
657 | __FUNCTION__); | ||
658 | } | ||
659 | raw_spin_unlock(&mcrit.release_lock); | ||
660 | |||
661 | /* If holder was enqueued in a release heap, then the following | ||
662 | * preemption check is pointless, but we can't easily detect | ||
663 | * that case. If you want to fix this, then consider that | ||
664 | * simply adding a state flag requires O(n) time to update when | ||
665 | * releasing n tasks, which conflicts with the goal to have | ||
666 | * O(log n) merges. */ | ||
667 | if (check_preempt) { | ||
668 | /* heap_decrease() hit the top level of the heap: make | ||
669 | * sure preemption checks get the right task, not the | ||
670 | * potentially stale cache. */ | ||
671 | bheap_uncache_min(edf_ready_order, | ||
672 | &mcrit.ready_queue); | ||
673 | check_for_preemptions(); | ||
674 | } | ||
675 | } | ||
676 | } | ||
677 | |||
678 | static long mcrit_pi_block(struct pi_semaphore *sem, | ||
679 | struct task_struct *new_waiter) | ||
680 | { | ||
681 | /* This callback has to handle the situation where a new waiter is | ||
682 | * added to the wait queue of the semaphore. | ||
683 | * | ||
684 | * We must check if has a higher priority than the currently | ||
685 | * highest-priority task, and then potentially reschedule. | ||
686 | */ | ||
687 | |||
688 | BUG_ON(!new_waiter); | ||
689 | |||
690 | if (edf_higher_prio(new_waiter, sem->hp.task)) { | ||
691 | TRACE_TASK(new_waiter, " boosts priority via %p\n", sem); | ||
692 | /* called with IRQs disabled */ | ||
693 | raw_spin_lock(&mcrit_lock); | ||
694 | /* store new highest-priority task */ | ||
695 | sem->hp.task = new_waiter; | ||
696 | if (sem->holder) { | ||
697 | TRACE_TASK(sem->holder, | ||
698 | " holds %p and will inherit from %s/%d\n", | ||
699 | sem, | ||
700 | new_waiter->comm, new_waiter->pid); | ||
701 | /* let holder inherit */ | ||
702 | sem->holder->rt_param.inh_task = new_waiter; | ||
703 | update_queue_position(sem->holder); | ||
704 | } | ||
705 | raw_spin_unlock(&mcrit_lock); | ||
706 | } | ||
707 | |||
708 | return 0; | ||
709 | } | ||
710 | |||
711 | static long mcrit_inherit_priority(struct pi_semaphore *sem, | ||
712 | struct task_struct *new_owner) | ||
713 | { | ||
714 | /* We don't need to acquire the mcrit_lock since at the time of this | ||
715 | * call new_owner isn't actually scheduled yet (it's still sleeping) | ||
716 | * and since the calling function already holds sem->wait.lock, which | ||
717 | * prevents concurrent sem->hp.task changes. | ||
718 | */ | ||
719 | |||
720 | if (sem->hp.task && sem->hp.task != new_owner) { | ||
721 | new_owner->rt_param.inh_task = sem->hp.task; | ||
722 | TRACE_TASK(new_owner, "inherited priority from %s/%d\n", | ||
723 | sem->hp.task->comm, sem->hp.task->pid); | ||
724 | } else | ||
725 | TRACE_TASK(new_owner, | ||
726 | "cannot inherit priority, " | ||
727 | "no higher priority job waits.\n"); | ||
728 | return 0; | ||
729 | } | ||
730 | |||
731 | /* This function is called on a semaphore release, and assumes that | ||
732 | * the current task is also the semaphore holder. | ||
733 | */ | ||
734 | static long mcrit_return_priority(struct pi_semaphore *sem) | ||
735 | { | ||
736 | struct task_struct* t = current; | ||
737 | int ret = 0; | ||
738 | |||
739 | /* Find new highest-priority semaphore task | ||
740 | * if holder task is the current hp.task. | ||
741 | * | ||
742 | * Calling function holds sem->wait.lock. | ||
743 | */ | ||
744 | if (t == sem->hp.task) | ||
745 | edf_set_hp_task(sem); | ||
746 | |||
747 | TRACE_CUR("mcrit_return_priority for lock %p\n", sem); | ||
748 | |||
749 | if (t->rt_param.inh_task) { | ||
750 | /* interrupts already disabled by PI code */ | ||
751 | raw_spin_lock(&mcrit_lock); | ||
752 | |||
753 | /* Reset inh_task to NULL. */ | ||
754 | t->rt_param.inh_task = NULL; | ||
755 | |||
756 | /* Check if rescheduling is necessary */ | ||
757 | unlink(t); | ||
758 | mcrit_job_arrival(t); | ||
759 | raw_spin_unlock(&mcrit_lock); | ||
760 | } | ||
761 | |||
762 | return ret; | ||
763 | } | ||
764 | |||
765 | #endif | ||
766 | |||
767 | static long mcrit_admit_task(struct task_struct* tsk) | ||
768 | { | ||
769 | return 0; | ||
770 | } | ||
771 | |||
772 | static long mcrit_activate_plugin(void) | ||
773 | { | ||
774 | int cpu; | ||
775 | cpu_entry_t *entry; | ||
776 | |||
777 | bheap_init(&mcrit_cpu_heap); | ||
778 | #ifdef CONFIG_RELEASE_MASTER | ||
779 | mcrit.release_master = atomic_read(&release_master_cpu); | ||
780 | #endif | ||
781 | |||
782 | for_each_online_cpu(cpu) { | ||
783 | entry = &per_cpu(mcrit_cpu_entries, cpu); | ||
784 | bheap_node_init(&entry->hn, entry); | ||
785 | atomic_set(&entry->will_schedule, 0); | ||
786 | entry->linked = NULL; | ||
787 | entry->scheduled = NULL; | ||
788 | #ifdef CONFIG_RELEASE_MASTER | ||
789 | if (cpu != mcrit.release_master) { | ||
790 | #endif | ||
791 | TRACE("GSN-EDF: Initializing CPU #%d.\n", cpu); | ||
792 | update_cpu_position(entry); | ||
793 | #ifdef CONFIG_RELEASE_MASTER | ||
794 | } else { | ||
795 | TRACE("GSN-EDF: CPU %d is release master.\n", cpu); | ||
796 | } | ||
797 | #endif | ||
798 | } | ||
799 | return 0; | ||
800 | } | ||
801 | |||
802 | /* Plugin object */ | ||
803 | static struct sched_plugin m_crit_plugin __cacheline_aligned_in_smp = { | ||
804 | .plugin_name = "MCRIT", | ||
805 | .finish_switch = mcrit_finish_switch, | ||
806 | .tick = mcrit_tick, | ||
807 | .task_new = mcrit_task_new, | ||
808 | .complete_job = complete_job, | ||
809 | .task_exit = mcrit_task_exit, | ||
810 | .schedule = mcrit_schedule, | ||
811 | .task_wake_up = mcrit_task_wake_up, | ||
812 | .task_block = mcrit_task_block, | ||
813 | #ifdef CONFIG_FMLP | ||
814 | .fmlp_active = 1, | ||
815 | .pi_block = mcrit_pi_block, | ||
816 | .inherit_priority = mcrit_inherit_priority, | ||
817 | .return_priority = mcrit_return_priority, | ||
818 | #endif | ||
819 | .admit_task = mcrit_admit_task, | ||
820 | .activate_plugin = mcrit_activate_plugin, | ||
821 | }; | ||
822 | |||
823 | |||
824 | static int __init init_m_crit(void) | ||
825 | { | ||
826 | int cpu; | ||
827 | cpu_entry_t *entry; | ||
828 | |||
829 | bheap_init(&mcrit_cpu_heap); | ||
830 | /* initialize CPU state */ | ||
831 | for (cpu = 0; cpu < NR_CPUS; cpu++) { | ||
832 | entry = &per_cpu(mcrit_cpu_entries, cpu); | ||
833 | mcrit_cpus[cpu] = entry; | ||
834 | atomic_set(&entry->will_schedule, 0); | ||
835 | entry->cpu = cpu; | ||
836 | entry->hn = &mcrit_heap_node[cpu]; | ||
837 | bheap_node_init(&entry->hn, entry); | ||
838 | } | ||
839 | edf_domain_init(&mcrit, NULL, mcrit_release_jobs); | ||
840 | return register_sched_plugin(&m_crit_plugin); | ||
841 | } | ||
842 | |||
843 | |||
844 | module_init(init_m_crit); | ||