aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/sched_pfp.c
diff options
context:
space:
mode:
Diffstat (limited to 'litmus/sched_pfp.c')
-rw-r--r--litmus/sched_pfp.c1685
1 files changed, 1685 insertions, 0 deletions
diff --git a/litmus/sched_pfp.c b/litmus/sched_pfp.c
new file mode 100644
index 000000000000..62be699629b1
--- /dev/null
+++ b/litmus/sched_pfp.c
@@ -0,0 +1,1685 @@
1/*
2 * litmus/sched_pfp.c
3 *
4 * Implementation of partitioned fixed-priority scheduling.
5 * Based on PSN-EDF.
6 */
7
8#include <linux/percpu.h>
9#include <linux/sched.h>
10#include <linux/list.h>
11#include <linux/spinlock.h>
12#include <linux/module.h>
13
14#include <litmus/litmus.h>
15#include <litmus/wait.h>
16#include <litmus/jobs.h>
17#include <litmus/preempt.h>
18#include <litmus/fp_common.h>
19#include <litmus/sched_plugin.h>
20#include <litmus/sched_trace.h>
21#include <litmus/trace.h>
22#include <litmus/budget.h>
23
24#include <linux/uaccess.h>
25
26
27typedef struct {
28 rt_domain_t domain;
29 struct fp_prio_queue ready_queue;
30 int cpu;
31 struct task_struct* scheduled; /* only RT tasks */
32/*
33 * scheduling lock slock
34 * protects the domain and serializes scheduling decisions
35 */
36#define slock domain.ready_lock
37
38} pfp_domain_t;
39
40DEFINE_PER_CPU(pfp_domain_t, pfp_domains);
41
42pfp_domain_t* pfp_doms[NR_CPUS];
43
44#define local_pfp (&__get_cpu_var(pfp_domains))
45#define remote_dom(cpu) (&per_cpu(pfp_domains, cpu).domain)
46#define remote_pfp(cpu) (&per_cpu(pfp_domains, cpu))
47#define task_dom(task) remote_dom(get_partition(task))
48#define task_pfp(task) remote_pfp(get_partition(task))
49
50/* we assume the lock is being held */
51static void preempt(pfp_domain_t *pfp)
52{
53 preempt_if_preemptable(pfp->scheduled, pfp->cpu);
54}
55
56static unsigned int priority_index(struct task_struct* t)
57{
58#ifdef CONFIG_LOCKING
59 if (unlikely(t->rt_param.inh_task))
60 /* use effective priority */
61 t = t->rt_param.inh_task;
62
63 if (is_priority_boosted(t)) {
64 /* zero is reserved for priority-boosted tasks */
65 return 0;
66 } else
67#endif
68 return get_priority(t);
69}
70
71
72static void pfp_release_jobs(rt_domain_t* rt, struct bheap* tasks)
73{
74 pfp_domain_t *pfp = container_of(rt, pfp_domain_t, domain);
75 unsigned long flags;
76 struct task_struct* t;
77 struct bheap_node* hn;
78
79 raw_spin_lock_irqsave(&pfp->slock, flags);
80
81 while (!bheap_empty(tasks)) {
82 hn = bheap_take(fp_ready_order, tasks);
83 t = bheap2task(hn);
84 TRACE_TASK(t, "released (part:%d prio:%d)\n",
85 get_partition(t), get_priority(t));
86 fp_prio_add(&pfp->ready_queue, t, priority_index(t));
87 }
88
89 /* do we need to preempt? */
90 if (fp_higher_prio(fp_prio_peek(&pfp->ready_queue), pfp->scheduled)) {
91 TRACE_CUR("preempted by new release\n");
92 preempt(pfp);
93 }
94
95 raw_spin_unlock_irqrestore(&pfp->slock, flags);
96}
97
98static void pfp_domain_init(pfp_domain_t* pfp,
99 int cpu)
100{
101 fp_domain_init(&pfp->domain, NULL, pfp_release_jobs);
102 pfp->cpu = cpu;
103 pfp->scheduled = NULL;
104 fp_prio_queue_init(&pfp->ready_queue);
105}
106
107static void requeue(struct task_struct* t, pfp_domain_t *pfp)
108{
109 if (t->state != TASK_RUNNING)
110 TRACE_TASK(t, "requeue: !TASK_RUNNING\n");
111
112 set_rt_flags(t, RT_F_RUNNING);
113 if (is_released(t, litmus_clock()))
114 fp_prio_add(&pfp->ready_queue, t, priority_index(t));
115 else
116 add_release(&pfp->domain, t); /* it has got to wait */
117}
118
119static void job_completion(struct task_struct* t, int forced)
120{
121 sched_trace_task_completion(t,forced);
122 TRACE_TASK(t, "job_completion().\n");
123
124 set_rt_flags(t, RT_F_SLEEP);
125 prepare_for_next_period(t);
126}
127
128static void pfp_tick(struct task_struct *t)
129{
130 pfp_domain_t *pfp = local_pfp;
131
132 /* Check for inconsistency. We don't need the lock for this since
133 * ->scheduled is only changed in schedule, which obviously is not
134 * executing in parallel on this CPU
135 */
136 BUG_ON(is_realtime(t) && t != pfp->scheduled);
137
138 if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) {
139 if (!is_np(t)) {
140 litmus_reschedule_local();
141 TRACE("pfp_scheduler_tick: "
142 "%d is preemptable "
143 " => FORCE_RESCHED\n", t->pid);
144 } else if (is_user_np(t)) {
145 TRACE("pfp_scheduler_tick: "
146 "%d is non-preemptable, "
147 "preemption delayed.\n", t->pid);
148 request_exit_np(t);
149 }
150 }
151}
152
153static struct task_struct* pfp_schedule(struct task_struct * prev)
154{
155 pfp_domain_t* pfp = local_pfp;
156 struct task_struct* next;
157
158 int out_of_time, sleep, preempt, np, exists, blocks, resched, migrate;
159
160 raw_spin_lock(&pfp->slock);
161
162 /* sanity checking
163 * differently from gedf, when a task exits (dead)
164 * pfp->schedule may be null and prev _is_ realtime
165 */
166 BUG_ON(pfp->scheduled && pfp->scheduled != prev);
167 BUG_ON(pfp->scheduled && !is_realtime(prev));
168
169 /* (0) Determine state */
170 exists = pfp->scheduled != NULL;
171 blocks = exists && !is_running(pfp->scheduled);
172 out_of_time = exists &&
173 budget_enforced(pfp->scheduled) &&
174 budget_exhausted(pfp->scheduled);
175 np = exists && is_np(pfp->scheduled);
176 sleep = exists && get_rt_flags(pfp->scheduled) == RT_F_SLEEP;
177 migrate = exists && get_partition(pfp->scheduled) != pfp->cpu;
178 preempt = migrate || fp_preemption_needed(&pfp->ready_queue, prev);
179
180 /* If we need to preempt do so.
181 * The following checks set resched to 1 in case of special
182 * circumstances.
183 */
184 resched = preempt;
185
186 /* If a task blocks we have no choice but to reschedule.
187 */
188 if (blocks)
189 resched = 1;
190
191 /* Request a sys_exit_np() call if we would like to preempt but cannot.
192 * Multiple calls to request_exit_np() don't hurt.
193 */
194 if (np && (out_of_time || preempt || sleep))
195 request_exit_np(pfp->scheduled);
196
197 /* Any task that is preemptable and either exhausts its execution
198 * budget or wants to sleep completes. We may have to reschedule after
199 * this.
200 */
201 if (!np && (out_of_time || sleep) && !blocks && !migrate) {
202 job_completion(pfp->scheduled, !sleep);
203 resched = 1;
204 }
205
206 /* The final scheduling decision. Do we need to switch for some reason?
207 * Switch if we are in RT mode and have no task or if we need to
208 * resched.
209 */
210 next = NULL;
211 if ((!np || blocks) && (resched || !exists)) {
212 /* When preempting a task that does not block, then
213 * re-insert it into either the ready queue or the
214 * release queue (if it completed). requeue() picks
215 * the appropriate queue.
216 */
217 if (pfp->scheduled && !blocks && !migrate)
218 requeue(pfp->scheduled, pfp);
219 next = fp_prio_take(&pfp->ready_queue);
220 } else
221 /* Only override Linux scheduler if we have a real-time task
222 * scheduled that needs to continue.
223 */
224 if (exists)
225 next = prev;
226
227 if (next) {
228 TRACE_TASK(next, "scheduled at %llu\n", litmus_clock());
229 set_rt_flags(next, RT_F_RUNNING);
230 } else {
231 TRACE("becoming idle at %llu\n", litmus_clock());
232 }
233
234 pfp->scheduled = next;
235 sched_state_task_picked();
236 raw_spin_unlock(&pfp->slock);
237
238 return next;
239}
240
241#ifdef CONFIG_LITMUS_LOCKING
242
243/* prev is no longer scheduled --- see if it needs to migrate */
244static void pfp_finish_switch(struct task_struct *prev)
245{
246 pfp_domain_t *to;
247
248 if (is_realtime(prev) &&
249 is_running(prev) &&
250 get_partition(prev) != smp_processor_id()) {
251 TRACE_TASK(prev, "needs to migrate from P%d to P%d\n",
252 smp_processor_id(), get_partition(prev));
253
254 to = task_pfp(prev);
255
256 raw_spin_lock(&to->slock);
257
258 TRACE_TASK(prev, "adding to queue on P%d\n", to->cpu);
259 requeue(prev, to);
260 if (fp_preemption_needed(&to->ready_queue, to->scheduled))
261 preempt(to);
262
263 raw_spin_unlock(&to->slock);
264
265 }
266}
267
268#endif
269
270/* Prepare a task for running in RT mode
271 */
272static void pfp_task_new(struct task_struct * t, int on_rq, int running)
273{
274 pfp_domain_t* pfp = task_pfp(t);
275 unsigned long flags;
276
277 TRACE_TASK(t, "P-FP: task new, cpu = %d\n",
278 t->rt_param.task_params.cpu);
279
280 /* setup job parameters */
281 release_at(t, litmus_clock());
282
283 /* The task should be running in the queue, otherwise signal
284 * code will try to wake it up with fatal consequences.
285 */
286 raw_spin_lock_irqsave(&pfp->slock, flags);
287 if (running) {
288 /* there shouldn't be anything else running at the time */
289 BUG_ON(pfp->scheduled);
290 pfp->scheduled = t;
291 } else {
292 requeue(t, pfp);
293 /* maybe we have to reschedule */
294 preempt(pfp);
295 }
296 raw_spin_unlock_irqrestore(&pfp->slock, flags);
297}
298
299static void pfp_task_wake_up(struct task_struct *task)
300{
301 unsigned long flags;
302 pfp_domain_t* pfp = task_pfp(task);
303 lt_t now;
304
305 TRACE_TASK(task, "wake_up at %llu\n", litmus_clock());
306 raw_spin_lock_irqsave(&pfp->slock, flags);
307
308#ifdef CONFIG_LITMUS_LOCKING
309 /* Should only be queued when processing a fake-wake up due to a
310 * migration-related state change. */
311 if (unlikely(is_queued(task))) {
312 TRACE_TASK(task, "WARNING: waking task still queued. Is this right?\n");
313 goto out_unlock;
314 }
315#else
316 BUG_ON(is_queued(task));
317#endif
318 now = litmus_clock();
319 if (is_tardy(task, now)
320#ifdef CONFIG_LITMUS_LOCKING
321 /* We need to take suspensions because of semaphores into
322 * account! If a job resumes after being suspended due to acquiring
323 * a semaphore, it should never be treated as a new job release.
324 */
325 && !is_priority_boosted(task)
326#endif
327 ) {
328 /* new sporadic release */
329 release_at(task, now);
330 sched_trace_task_release(task);
331 }
332
333 /* Only add to ready queue if it is not the currently-scheduled
334 * task. This could be the case if a task was woken up concurrently
335 * on a remote CPU before the executing CPU got around to actually
336 * de-scheduling the task, i.e., wake_up() raced with schedule()
337 * and won. Also, don't requeue if it is still queued, which can
338 * happen under the DPCP due wake-ups racing with migrations.
339 */
340 if (pfp->scheduled != task)
341 requeue(task, pfp);
342
343out_unlock:
344 raw_spin_unlock_irqrestore(&pfp->slock, flags);
345 TRACE_TASK(task, "wake up done\n");
346}
347
348static void pfp_task_block(struct task_struct *t)
349{
350 /* only running tasks can block, thus t is in no queue */
351 TRACE_TASK(t, "block at %llu, state=%d\n", litmus_clock(), t->state);
352
353 BUG_ON(!is_realtime(t));
354
355 /* If this task blocked normally, it shouldn't be queued. The exception is
356 * if this is a simulated block()/wakeup() pair from the pull-migration code path.
357 * This should only happen if the DPCP is being used.
358 */
359#ifdef CONFIG_LITMUS_LOCKING
360 if (unlikely(is_queued(t)))
361 TRACE_TASK(t, "WARNING: blocking task still queued. Is this right?\n");
362#else
363 BUG_ON(is_queued(t));
364#endif
365}
366
367static void pfp_task_exit(struct task_struct * t)
368{
369 unsigned long flags;
370 pfp_domain_t* pfp = task_pfp(t);
371 rt_domain_t* dom;
372
373 raw_spin_lock_irqsave(&pfp->slock, flags);
374 if (is_queued(t)) {
375 BUG(); /* This currently doesn't work. */
376 /* dequeue */
377 dom = task_dom(t);
378 remove(dom, t);
379 }
380 if (pfp->scheduled == t) {
381 pfp->scheduled = NULL;
382 preempt(pfp);
383 }
384 TRACE_TASK(t, "RIP, now reschedule\n");
385
386 raw_spin_unlock_irqrestore(&pfp->slock, flags);
387}
388
389#ifdef CONFIG_LITMUS_LOCKING
390
391#include <litmus/fdso.h>
392#include <litmus/srp.h>
393
394static void fp_dequeue(pfp_domain_t* pfp, struct task_struct* t)
395{
396 BUG_ON(pfp->scheduled == t && is_queued(t));
397 if (is_queued(t))
398 fp_prio_remove(&pfp->ready_queue, t, priority_index(t));
399}
400
401static void fp_set_prio_inh(pfp_domain_t* pfp, struct task_struct* t,
402 struct task_struct* prio_inh)
403{
404 int requeue;
405
406 if (!t || t->rt_param.inh_task == prio_inh) {
407 /* no update required */
408 if (t)
409 TRACE_TASK(t, "no prio-inh update required\n");
410 return;
411 }
412
413 requeue = is_queued(t);
414 TRACE_TASK(t, "prio-inh: is_queued:%d\n", requeue);
415
416 if (requeue)
417 /* first remove */
418 fp_dequeue(pfp, t);
419
420 t->rt_param.inh_task = prio_inh;
421
422 if (requeue)
423 /* add again to the right queue */
424 fp_prio_add(&pfp->ready_queue, t, priority_index(t));
425}
426
427static int effective_agent_priority(int prio)
428{
429 /* make sure agents have higher priority */
430 return prio - LITMUS_MAX_PRIORITY;
431}
432
433static lt_t prio_point(int eprio)
434{
435 /* make sure we have non-negative prio points */
436 return eprio + LITMUS_MAX_PRIORITY;
437}
438
439static int prio_from_point(lt_t prio_point)
440{
441 return ((int) prio_point) - LITMUS_MAX_PRIORITY;
442}
443
444static void boost_priority(struct task_struct* t, lt_t priority_point)
445{
446 unsigned long flags;
447 pfp_domain_t* pfp = task_pfp(t);
448
449 raw_spin_lock_irqsave(&pfp->slock, flags);
450
451
452 TRACE_TASK(t, "priority boosted at %llu\n", litmus_clock());
453
454 tsk_rt(t)->priority_boosted = 1;
455 /* tie-break by protocol-specific priority point */
456 tsk_rt(t)->boost_start_time = priority_point;
457
458 if (pfp->scheduled != t) {
459 /* holder may be queued: first stop queue changes */
460 raw_spin_lock(&pfp->domain.release_lock);
461 if (is_queued(t) &&
462 /* If it is queued, then we need to re-order. */
463 bheap_decrease(fp_ready_order, tsk_rt(t)->heap_node) &&
464 /* If we bubbled to the top, then we need to check for preemptions. */
465 fp_preemption_needed(&pfp->ready_queue, pfp->scheduled))
466 preempt(pfp);
467 raw_spin_unlock(&pfp->domain.release_lock);
468 } /* else: nothing to do since the job is not queued while scheduled */
469
470 raw_spin_unlock_irqrestore(&pfp->slock, flags);
471}
472
473static void unboost_priority(struct task_struct* t)
474{
475 unsigned long flags;
476 pfp_domain_t* pfp = task_pfp(t);
477 lt_t now;
478
479 raw_spin_lock_irqsave(&pfp->slock, flags);
480 now = litmus_clock();
481
482 /* assumption: this only happens when the job is scheduled */
483 BUG_ON(pfp->scheduled != t);
484
485 TRACE_TASK(t, "priority restored at %llu\n", now);
486
487 /* priority boosted jobs must be scheduled */
488 BUG_ON(pfp->scheduled != t);
489
490 tsk_rt(t)->priority_boosted = 0;
491 tsk_rt(t)->boost_start_time = 0;
492
493 /* check if this changes anything */
494 if (fp_preemption_needed(&pfp->ready_queue, pfp->scheduled))
495 preempt(pfp);
496
497 raw_spin_unlock_irqrestore(&pfp->slock, flags);
498}
499
500/* ******************** SRP support ************************ */
501
502static unsigned int pfp_get_srp_prio(struct task_struct* t)
503{
504 return get_priority(t);
505}
506
507/* ******************** FMLP support ********************** */
508
509struct fmlp_semaphore {
510 struct litmus_lock litmus_lock;
511
512 /* current resource holder */
513 struct task_struct *owner;
514
515 /* FIFO queue of waiting tasks */
516 wait_queue_head_t wait;
517};
518
519static inline struct fmlp_semaphore* fmlp_from_lock(struct litmus_lock* lock)
520{
521 return container_of(lock, struct fmlp_semaphore, litmus_lock);
522}
523int pfp_fmlp_lock(struct litmus_lock* l)
524{
525 struct task_struct* t = current;
526 struct fmlp_semaphore *sem = fmlp_from_lock(l);
527 wait_queue_t wait;
528 unsigned long flags;
529 lt_t time_of_request;
530
531 if (!is_realtime(t))
532 return -EPERM;
533
534 spin_lock_irqsave(&sem->wait.lock, flags);
535
536 /* tie-break by this point in time */
537 time_of_request = litmus_clock();
538
539 /* Priority-boost ourself *before* we suspend so that
540 * our priority is boosted when we resume. */
541 boost_priority(t, time_of_request);
542
543 if (sem->owner) {
544 /* resource is not free => must suspend and wait */
545
546 init_waitqueue_entry(&wait, t);
547
548 /* FIXME: interruptible would be nice some day */
549 set_task_state(t, TASK_UNINTERRUPTIBLE);
550
551 __add_wait_queue_tail_exclusive(&sem->wait, &wait);
552
553 TS_LOCK_SUSPEND;
554
555 /* release lock before sleeping */
556 spin_unlock_irqrestore(&sem->wait.lock, flags);
557
558 /* We depend on the FIFO order. Thus, we don't need to recheck
559 * when we wake up; we are guaranteed to have the lock since
560 * there is only one wake up per release.
561 */
562
563 schedule();
564
565 TS_LOCK_RESUME;
566
567 /* Since we hold the lock, no other task will change
568 * ->owner. We can thus check it without acquiring the spin
569 * lock. */
570 BUG_ON(sem->owner != t);
571 } else {
572 /* it's ours now */
573 sem->owner = t;
574
575 spin_unlock_irqrestore(&sem->wait.lock, flags);
576 }
577
578 return 0;
579}
580
581int pfp_fmlp_unlock(struct litmus_lock* l)
582{
583 struct task_struct *t = current, *next;
584 struct fmlp_semaphore *sem = fmlp_from_lock(l);
585 unsigned long flags;
586 int err = 0;
587
588 spin_lock_irqsave(&sem->wait.lock, flags);
589
590 if (sem->owner != t) {
591 err = -EINVAL;
592 goto out;
593 }
594
595 /* we lose the benefit of priority boosting */
596
597 unboost_priority(t);
598
599 /* check if there are jobs waiting for this resource */
600 next = __waitqueue_remove_first(&sem->wait);
601 if (next) {
602 /* next becomes the resouce holder */
603 sem->owner = next;
604
605 /* Wake up next. The waiting job is already priority-boosted. */
606 wake_up_process(next);
607 } else
608 /* resource becomes available */
609 sem->owner = NULL;
610
611out:
612 spin_unlock_irqrestore(&sem->wait.lock, flags);
613 return err;
614}
615
616int pfp_fmlp_close(struct litmus_lock* l)
617{
618 struct task_struct *t = current;
619 struct fmlp_semaphore *sem = fmlp_from_lock(l);
620 unsigned long flags;
621
622 int owner;
623
624 spin_lock_irqsave(&sem->wait.lock, flags);
625
626 owner = sem->owner == t;
627
628 spin_unlock_irqrestore(&sem->wait.lock, flags);
629
630 if (owner)
631 pfp_fmlp_unlock(l);
632
633 return 0;
634}
635
636void pfp_fmlp_free(struct litmus_lock* lock)
637{
638 kfree(fmlp_from_lock(lock));
639}
640
641static struct litmus_lock_ops pfp_fmlp_lock_ops = {
642 .close = pfp_fmlp_close,
643 .lock = pfp_fmlp_lock,
644 .unlock = pfp_fmlp_unlock,
645 .deallocate = pfp_fmlp_free,
646};
647
648static struct litmus_lock* pfp_new_fmlp(void)
649{
650 struct fmlp_semaphore* sem;
651
652 sem = kmalloc(sizeof(*sem), GFP_KERNEL);
653 if (!sem)
654 return NULL;
655
656 sem->owner = NULL;
657 init_waitqueue_head(&sem->wait);
658 sem->litmus_lock.ops = &pfp_fmlp_lock_ops;
659
660 return &sem->litmus_lock;
661}
662
663/* ******************** MPCP support ********************** */
664
665struct mpcp_semaphore {
666 struct litmus_lock litmus_lock;
667
668 /* current resource holder */
669 struct task_struct *owner;
670
671 /* priority queue of waiting tasks */
672 wait_queue_head_t wait;
673
674 /* priority ceiling per cpu */
675 unsigned int prio_ceiling[NR_CPUS];
676
677 /* should jobs spin "virtually" for this resource? */
678 int vspin;
679};
680
681#define OMEGA_CEILING UINT_MAX
682
683/* Since jobs spin "virtually" while waiting to acquire a lock,
684 * they first must aquire a local per-cpu resource.
685 */
686static DEFINE_PER_CPU(wait_queue_head_t, mpcpvs_vspin_wait);
687static DEFINE_PER_CPU(struct task_struct*, mpcpvs_vspin);
688
689/* called with preemptions off <=> no local modifications */
690static void mpcp_vspin_enter(void)
691{
692 struct task_struct* t = current;
693
694 while (1) {
695 if (__get_cpu_var(mpcpvs_vspin) == NULL) {
696 /* good, we get to issue our request */
697 __get_cpu_var(mpcpvs_vspin) = t;
698 break;
699 } else {
700 /* some job is spinning => enqueue in request queue */
701 prio_wait_queue_t wait;
702 wait_queue_head_t* vspin = &__get_cpu_var(mpcpvs_vspin_wait);
703 unsigned long flags;
704
705 /* ordered by regular priority */
706 init_prio_waitqueue_entry(&wait, t, prio_point(get_priority(t)));
707
708 spin_lock_irqsave(&vspin->lock, flags);
709
710 set_task_state(t, TASK_UNINTERRUPTIBLE);
711
712 __add_wait_queue_prio_exclusive(vspin, &wait);
713
714 spin_unlock_irqrestore(&vspin->lock, flags);
715
716 TS_LOCK_SUSPEND;
717
718 preempt_enable_no_resched();
719
720 schedule();
721
722 preempt_disable();
723
724 TS_LOCK_RESUME;
725 /* Recheck if we got it --- some higher-priority process might
726 * have swooped in. */
727 }
728 }
729 /* ok, now it is ours */
730}
731
732/* called with preemptions off */
733static void mpcp_vspin_exit(void)
734{
735 struct task_struct* t = current, *next;
736 unsigned long flags;
737 wait_queue_head_t* vspin = &__get_cpu_var(mpcpvs_vspin_wait);
738
739 BUG_ON(__get_cpu_var(mpcpvs_vspin) != t);
740
741 /* no spinning job */
742 __get_cpu_var(mpcpvs_vspin) = NULL;
743
744 /* see if anyone is waiting for us to stop "spinning" */
745 spin_lock_irqsave(&vspin->lock, flags);
746 next = __waitqueue_remove_first(vspin);
747
748 if (next)
749 wake_up_process(next);
750
751 spin_unlock_irqrestore(&vspin->lock, flags);
752}
753
754static inline struct mpcp_semaphore* mpcp_from_lock(struct litmus_lock* lock)
755{
756 return container_of(lock, struct mpcp_semaphore, litmus_lock);
757}
758
759int pfp_mpcp_lock(struct litmus_lock* l)
760{
761 struct task_struct* t = current;
762 struct mpcp_semaphore *sem = mpcp_from_lock(l);
763 prio_wait_queue_t wait;
764 unsigned long flags;
765
766 if (!is_realtime(t))
767 return -EPERM;
768
769 preempt_disable();
770
771 if (sem->vspin)
772 mpcp_vspin_enter();
773
774 /* Priority-boost ourself *before* we suspend so that
775 * our priority is boosted when we resume. Use the priority
776 * ceiling for the local partition. */
777 boost_priority(t, sem->prio_ceiling[get_partition(t)]);
778
779 spin_lock_irqsave(&sem->wait.lock, flags);
780
781 preempt_enable_no_resched();
782
783 if (sem->owner) {
784 /* resource is not free => must suspend and wait */
785
786 /* ordered by regular priority */
787 init_prio_waitqueue_entry(&wait, t, prio_point(get_priority(t)));
788
789 /* FIXME: interruptible would be nice some day */
790 set_task_state(t, TASK_UNINTERRUPTIBLE);
791
792 __add_wait_queue_prio_exclusive(&sem->wait, &wait);
793
794 TS_LOCK_SUSPEND;
795
796 /* release lock before sleeping */
797 spin_unlock_irqrestore(&sem->wait.lock, flags);
798
799 /* We depend on the FIFO order. Thus, we don't need to recheck
800 * when we wake up; we are guaranteed to have the lock since
801 * there is only one wake up per release.
802 */
803
804 schedule();
805
806 TS_LOCK_RESUME;
807
808 /* Since we hold the lock, no other task will change
809 * ->owner. We can thus check it without acquiring the spin
810 * lock. */
811 BUG_ON(sem->owner != t);
812 } else {
813 /* it's ours now */
814 sem->owner = t;
815
816 spin_unlock_irqrestore(&sem->wait.lock, flags);
817 }
818
819 return 0;
820}
821
822int pfp_mpcp_unlock(struct litmus_lock* l)
823{
824 struct task_struct *t = current, *next;
825 struct mpcp_semaphore *sem = mpcp_from_lock(l);
826 unsigned long flags;
827 int err = 0;
828
829 spin_lock_irqsave(&sem->wait.lock, flags);
830
831 if (sem->owner != t) {
832 err = -EINVAL;
833 goto out;
834 }
835
836 /* we lose the benefit of priority boosting */
837
838 unboost_priority(t);
839
840 /* check if there are jobs waiting for this resource */
841 next = __waitqueue_remove_first(&sem->wait);
842 if (next) {
843 /* next becomes the resouce holder */
844 sem->owner = next;
845
846 /* Wake up next. The waiting job is already priority-boosted. */
847 wake_up_process(next);
848 } else
849 /* resource becomes available */
850 sem->owner = NULL;
851
852out:
853 spin_unlock_irqrestore(&sem->wait.lock, flags);
854
855 if (sem->vspin && err == 0) {
856 preempt_disable();
857 mpcp_vspin_exit();
858 preempt_enable();
859 }
860
861 return err;
862}
863
864int pfp_mpcp_open(struct litmus_lock* l, void* config)
865{
866 struct task_struct *t = current;
867 struct mpcp_semaphore *sem = mpcp_from_lock(l);
868 int cpu, local_cpu;
869 unsigned long flags;
870
871 if (!is_realtime(t))
872 /* we need to know the real-time priority */
873 return -EPERM;
874
875 local_cpu = get_partition(t);
876
877 spin_lock_irqsave(&sem->wait.lock, flags);
878
879 for (cpu = 0; cpu < NR_CPUS; cpu++)
880 if (cpu != local_cpu)
881 {
882 sem->prio_ceiling[cpu] = min(sem->prio_ceiling[cpu],
883 get_priority(t));
884 TRACE_CUR("priority ceiling for sem %p is now %d on cpu %d\n",
885 sem, sem->prio_ceiling[cpu], cpu);
886 }
887
888 spin_unlock_irqrestore(&sem->wait.lock, flags);
889
890 return 0;
891}
892
893int pfp_mpcp_close(struct litmus_lock* l)
894{
895 struct task_struct *t = current;
896 struct mpcp_semaphore *sem = mpcp_from_lock(l);
897 unsigned long flags;
898
899 int owner;
900
901 spin_lock_irqsave(&sem->wait.lock, flags);
902
903 owner = sem->owner == t;
904
905 spin_unlock_irqrestore(&sem->wait.lock, flags);
906
907 if (owner)
908 pfp_mpcp_unlock(l);
909
910 return 0;
911}
912
913void pfp_mpcp_free(struct litmus_lock* lock)
914{
915 kfree(mpcp_from_lock(lock));
916}
917
918static struct litmus_lock_ops pfp_mpcp_lock_ops = {
919 .close = pfp_mpcp_close,
920 .lock = pfp_mpcp_lock,
921 .open = pfp_mpcp_open,
922 .unlock = pfp_mpcp_unlock,
923 .deallocate = pfp_mpcp_free,
924};
925
926static struct litmus_lock* pfp_new_mpcp(int vspin)
927{
928 struct mpcp_semaphore* sem;
929 int cpu;
930
931 sem = kmalloc(sizeof(*sem), GFP_KERNEL);
932 if (!sem)
933 return NULL;
934
935 sem->owner = NULL;
936 init_waitqueue_head(&sem->wait);
937 sem->litmus_lock.ops = &pfp_mpcp_lock_ops;
938
939 for (cpu = 0; cpu < NR_CPUS; cpu++)
940 sem->prio_ceiling[cpu] = OMEGA_CEILING;
941
942 /* mark as virtual spinning */
943 sem->vspin = vspin;
944
945 return &sem->litmus_lock;
946}
947
948
949/* ******************** PCP support ********************** */
950
951
952struct pcp_semaphore {
953 struct litmus_lock litmus_lock;
954
955 struct list_head ceiling;
956
957 /* current resource holder */
958 struct task_struct *owner;
959
960 /* priority ceiling --- can be negative due to DPCP support */
961 int prio_ceiling;
962
963 /* on which processor is this PCP semaphore allocated? */
964 int on_cpu;
965};
966
967static inline struct pcp_semaphore* pcp_from_lock(struct litmus_lock* lock)
968{
969 return container_of(lock, struct pcp_semaphore, litmus_lock);
970}
971
972
973struct pcp_state {
974 struct list_head system_ceiling;
975
976 /* highest-priority waiting task */
977 struct task_struct* hp_waiter;
978
979 /* list of jobs waiting to get past the system ceiling */
980 wait_queue_head_t ceiling_blocked;
981};
982
983static void pcp_init_state(struct pcp_state* s)
984{
985 INIT_LIST_HEAD(&s->system_ceiling);
986 s->hp_waiter = NULL;
987 init_waitqueue_head(&s->ceiling_blocked);
988}
989
990static DEFINE_PER_CPU(struct pcp_state, pcp_state);
991
992/* assumes preemptions are off */
993static struct pcp_semaphore* pcp_get_ceiling(void)
994{
995 struct list_head* top = __get_cpu_var(pcp_state).system_ceiling.next;
996
997 if (top)
998 return list_entry(top, struct pcp_semaphore, ceiling);
999 else
1000 return NULL;
1001}
1002
1003/* assumes preempt off */
1004static void pcp_add_ceiling(struct pcp_semaphore* sem)
1005{
1006 struct list_head *pos;
1007 struct list_head *in_use = &__get_cpu_var(pcp_state).system_ceiling;
1008 struct pcp_semaphore* held;
1009
1010 BUG_ON(sem->on_cpu != smp_processor_id());
1011 BUG_ON(in_list(&sem->ceiling));
1012
1013 list_for_each(pos, in_use) {
1014 held = list_entry(pos, struct pcp_semaphore, ceiling);
1015 if (held->prio_ceiling >= sem->prio_ceiling) {
1016 __list_add(&sem->ceiling, pos->prev, pos);
1017 return;
1018 }
1019 }
1020
1021 /* we hit the end of the list */
1022
1023 list_add_tail(&sem->ceiling, in_use);
1024}
1025
1026/* assumes preempt off */
1027static int pcp_exceeds_ceiling(struct pcp_semaphore* ceiling,
1028 struct task_struct* task,
1029 int effective_prio)
1030{
1031 return ceiling == NULL ||
1032 ceiling->prio_ceiling > effective_prio ||
1033 ceiling->owner == task;
1034}
1035
1036/* assumes preempt off */
1037static void pcp_priority_inheritance(void)
1038{
1039 unsigned long flags;
1040 pfp_domain_t* pfp = local_pfp;
1041
1042 struct pcp_semaphore* ceiling = pcp_get_ceiling();
1043 struct task_struct *blocker, *blocked;
1044
1045 blocker = ceiling ? ceiling->owner : NULL;
1046 blocked = __get_cpu_var(pcp_state).hp_waiter;
1047
1048 raw_spin_lock_irqsave(&pfp->slock, flags);
1049
1050 /* Current is no longer inheriting anything by default. This should be
1051 * the currently scheduled job, and hence not currently queued. */
1052 BUG_ON(current != pfp->scheduled);
1053
1054 fp_set_prio_inh(pfp, current, NULL);
1055 fp_set_prio_inh(pfp, blocked, NULL);
1056 fp_set_prio_inh(pfp, blocker, NULL);
1057
1058
1059 /* Let blocking job inherit priority of blocked job, if required. */
1060 if (blocker && blocked &&
1061 fp_higher_prio(blocked, blocker)) {
1062 TRACE_TASK(blocker, "PCP inherits from %s/%d (prio %u -> %u) \n",
1063 blocked->comm, blocked->pid,
1064 get_priority(blocker), get_priority(blocked));
1065 fp_set_prio_inh(pfp, blocker, blocked);
1066 }
1067
1068 /* check if anything changed */
1069 if (fp_higher_prio(fp_prio_peek(&pfp->ready_queue), pfp->scheduled))
1070 preempt(pfp);
1071
1072 raw_spin_unlock_irqrestore(&pfp->slock, flags);
1073}
1074
1075/* called with preemptions off */
1076static void pcp_raise_ceiling(struct pcp_semaphore* sem,
1077 int effective_prio)
1078{
1079 struct task_struct* t = current;
1080 struct pcp_semaphore* ceiling;
1081 prio_wait_queue_t wait;
1082 unsigned int waiting_higher_prio;
1083
1084 do {
1085 ceiling = pcp_get_ceiling();
1086 if (pcp_exceeds_ceiling(ceiling, t, effective_prio))
1087 break;
1088
1089 TRACE_CUR("PCP ceiling-blocked, wanted sem %p, but %s/%d has the ceiling \n",
1090 sem, ceiling->owner->comm, ceiling->owner->pid);
1091
1092 /* we need to wait until the ceiling is lowered */
1093
1094 /* enqueue in priority order */
1095 init_prio_waitqueue_entry(&wait, t, prio_point(effective_prio));
1096 set_task_state(t, TASK_UNINTERRUPTIBLE);
1097 waiting_higher_prio = add_wait_queue_prio_exclusive(
1098 &__get_cpu_var(pcp_state).ceiling_blocked, &wait);
1099
1100 if (waiting_higher_prio == 0) {
1101 TRACE_CUR("PCP new highest-prio waiter => prio inheritance\n");
1102
1103 /* we are the new highest-priority waiting job
1104 * => update inheritance */
1105 __get_cpu_var(pcp_state).hp_waiter = t;
1106 pcp_priority_inheritance();
1107 }
1108
1109 TS_LOCK_SUSPEND;
1110
1111 preempt_enable_no_resched();
1112 schedule();
1113 preempt_disable();
1114
1115 /* pcp_resume_unblocked() removed us from wait queue */
1116
1117 TS_LOCK_RESUME;
1118 } while(1);
1119
1120 TRACE_CUR("PCP got the ceiling and sem %p\n", sem);
1121
1122 /* We are good to go. The semaphore should be available. */
1123 BUG_ON(sem->owner != NULL);
1124
1125 sem->owner = t;
1126
1127 pcp_add_ceiling(sem);
1128}
1129
1130static void pcp_resume_unblocked(void)
1131{
1132 wait_queue_head_t *blocked = &__get_cpu_var(pcp_state).ceiling_blocked;
1133 unsigned long flags;
1134 prio_wait_queue_t* q;
1135 struct task_struct* t = NULL;
1136
1137 struct pcp_semaphore* ceiling = pcp_get_ceiling();
1138
1139 spin_lock_irqsave(&blocked->lock, flags);
1140
1141 while (waitqueue_active(blocked)) {
1142 /* check first == highest-priority waiting job */
1143 q = list_entry(blocked->task_list.next,
1144 prio_wait_queue_t, wq.task_list);
1145 t = (struct task_struct*) q->wq.private;
1146
1147 /* can it proceed now? => let it go */
1148 if (pcp_exceeds_ceiling(ceiling, t,
1149 prio_from_point(q->priority))) {
1150 __remove_wait_queue(blocked, &q->wq);
1151 wake_up_process(t);
1152 } else {
1153 /* We are done. Update highest-priority waiter. */
1154 __get_cpu_var(pcp_state).hp_waiter = t;
1155 goto out;
1156 }
1157 }
1158 /* If we get here, then there are no more waiting
1159 * jobs. */
1160 __get_cpu_var(pcp_state).hp_waiter = NULL;
1161out:
1162 spin_unlock_irqrestore(&blocked->lock, flags);
1163}
1164
1165/* assumes preempt off */
1166static void pcp_lower_ceiling(struct pcp_semaphore* sem)
1167{
1168 BUG_ON(!in_list(&sem->ceiling));
1169 BUG_ON(sem->owner != current);
1170 BUG_ON(sem->on_cpu != smp_processor_id());
1171
1172 /* remove from ceiling list */
1173 list_del(&sem->ceiling);
1174
1175 /* release */
1176 sem->owner = NULL;
1177
1178 TRACE_CUR("PCP released sem %p\n", sem);
1179
1180 /* Wake up all ceiling-blocked jobs that now pass the ceiling. */
1181 pcp_resume_unblocked();
1182
1183 pcp_priority_inheritance();
1184}
1185
1186static void pcp_update_prio_ceiling(struct pcp_semaphore* sem,
1187 int effective_prio)
1188{
1189 /* This needs to be synchronized on something.
1190 * Might as well use waitqueue lock for the processor.
1191 * We assume this happens only before the task set starts execution,
1192 * (i.e., during initialization), but it may happen on multiple processors
1193 * at the same time.
1194 */
1195 unsigned long flags;
1196
1197 struct pcp_state* s = &per_cpu(pcp_state, sem->on_cpu);
1198
1199 spin_lock_irqsave(&s->ceiling_blocked.lock, flags);
1200
1201 sem->prio_ceiling = min(sem->prio_ceiling, effective_prio);
1202
1203 spin_unlock_irqrestore(&s->ceiling_blocked.lock, flags);
1204}
1205
1206static void pcp_init_semaphore(struct pcp_semaphore* sem, int cpu)
1207{
1208 sem->owner = NULL;
1209 INIT_LIST_HEAD(&sem->ceiling);
1210 sem->prio_ceiling = INT_MAX;
1211 sem->on_cpu = cpu;
1212}
1213
1214int pfp_pcp_lock(struct litmus_lock* l)
1215{
1216 struct task_struct* t = current;
1217 struct pcp_semaphore *sem = pcp_from_lock(l);
1218
1219 int eprio = effective_agent_priority(get_priority(t));
1220 int from = get_partition(t);
1221 int to = sem->on_cpu;
1222
1223 if (!is_realtime(t) || from != to)
1224 return -EPERM;
1225
1226 preempt_disable();
1227
1228 pcp_raise_ceiling(sem, eprio);
1229
1230 preempt_enable();
1231
1232 return 0;
1233}
1234
1235int pfp_pcp_unlock(struct litmus_lock* l)
1236{
1237 struct task_struct *t = current;
1238 struct pcp_semaphore *sem = pcp_from_lock(l);
1239
1240 int err = 0;
1241
1242 preempt_disable();
1243
1244 if (sem->on_cpu != smp_processor_id() || sem->owner != t) {
1245 err = -EINVAL;
1246 goto out;
1247 }
1248
1249 /* give it back */
1250 pcp_lower_ceiling(sem);
1251
1252out:
1253 preempt_enable();
1254
1255 return err;
1256}
1257
1258int pfp_pcp_open(struct litmus_lock* l, void* __user config)
1259{
1260 struct task_struct *t = current;
1261 struct pcp_semaphore *sem = pcp_from_lock(l);
1262
1263 int cpu, eprio;
1264
1265 if (!is_realtime(t))
1266 /* we need to know the real-time priority */
1267 return -EPERM;
1268
1269 if (get_user(cpu, (int*) config))
1270 return -EFAULT;
1271
1272 /* make sure the resource location matches */
1273 if (cpu != sem->on_cpu)
1274 return -EINVAL;
1275
1276 eprio = effective_agent_priority(get_priority(t));
1277
1278 pcp_update_prio_ceiling(sem, eprio);
1279
1280 return 0;
1281}
1282
1283int pfp_pcp_close(struct litmus_lock* l)
1284{
1285 struct task_struct *t = current;
1286 struct pcp_semaphore *sem = pcp_from_lock(l);
1287
1288 int owner = 0;
1289
1290 preempt_disable();
1291
1292 if (sem->on_cpu == smp_processor_id())
1293 owner = sem->owner == t;
1294
1295 preempt_enable();
1296
1297 if (owner)
1298 pfp_pcp_unlock(l);
1299
1300 return 0;
1301}
1302
1303void pfp_pcp_free(struct litmus_lock* lock)
1304{
1305 kfree(pcp_from_lock(lock));
1306}
1307
1308
1309static struct litmus_lock_ops pfp_pcp_lock_ops = {
1310 .close = pfp_pcp_close,
1311 .lock = pfp_pcp_lock,
1312 .open = pfp_pcp_open,
1313 .unlock = pfp_pcp_unlock,
1314 .deallocate = pfp_pcp_free,
1315};
1316
1317
1318static struct litmus_lock* pfp_new_pcp(int on_cpu)
1319{
1320 struct pcp_semaphore* sem;
1321
1322 sem = kmalloc(sizeof(*sem), GFP_KERNEL);
1323 if (!sem)
1324 return NULL;
1325
1326 sem->litmus_lock.ops = &pfp_pcp_lock_ops;
1327 pcp_init_semaphore(sem, on_cpu);
1328
1329 return &sem->litmus_lock;
1330}
1331
1332/* ******************** DPCP support ********************** */
1333
1334struct dpcp_semaphore {
1335 struct litmus_lock litmus_lock;
1336 struct pcp_semaphore pcp;
1337 int owner_cpu;
1338};
1339
1340static inline struct dpcp_semaphore* dpcp_from_lock(struct litmus_lock* lock)
1341{
1342 return container_of(lock, struct dpcp_semaphore, litmus_lock);
1343}
1344
1345/* called with preemptions disabled */
1346static void pfp_migrate_to(int target_cpu)
1347{
1348 struct task_struct* t = current;
1349 pfp_domain_t *from;
1350
1351 if (get_partition(t) == target_cpu)
1352 return;
1353
1354 /* make sure target_cpu makes sense */
1355 BUG_ON(!cpu_online(target_cpu));
1356
1357 local_irq_disable();
1358
1359 /* scheduled task should not be in any ready or release queue */
1360 BUG_ON(is_queued(t));
1361
1362 /* lock both pfp domains in order of address */
1363 from = task_pfp(t);
1364
1365 raw_spin_lock(&from->slock);
1366
1367 /* switch partitions */
1368 tsk_rt(t)->task_params.cpu = target_cpu;
1369
1370 raw_spin_unlock(&from->slock);
1371
1372 /* Don't trace scheduler costs as part of
1373 * locking overhead. Scheduling costs are accounted for
1374 * explicitly. */
1375 TS_LOCK_SUSPEND;
1376
1377 local_irq_enable();
1378 preempt_enable_no_resched();
1379
1380 /* deschedule to be migrated */
1381 schedule();
1382
1383 /* we are now on the target processor */
1384 preempt_disable();
1385
1386 /* start recording costs again */
1387 TS_LOCK_RESUME;
1388
1389 BUG_ON(smp_processor_id() != target_cpu);
1390}
1391
1392int pfp_dpcp_lock(struct litmus_lock* l)
1393{
1394 struct task_struct* t = current;
1395 struct dpcp_semaphore *sem = dpcp_from_lock(l);
1396 int eprio = effective_agent_priority(get_priority(t));
1397 int from = get_partition(t);
1398 int to = sem->pcp.on_cpu;
1399
1400 if (!is_realtime(t))
1401 return -EPERM;
1402
1403 preempt_disable();
1404
1405 /* Priority-boost ourself *before* we suspend so that
1406 * our priority is boosted when we resume. */
1407
1408 boost_priority(t, get_priority(t));
1409
1410 pfp_migrate_to(to);
1411
1412 pcp_raise_ceiling(&sem->pcp, eprio);
1413
1414 /* yep, we got it => execute request */
1415 sem->owner_cpu = from;
1416
1417 preempt_enable();
1418
1419 return 0;
1420}
1421
1422int pfp_dpcp_unlock(struct litmus_lock* l)
1423{
1424 struct task_struct *t = current;
1425 struct dpcp_semaphore *sem = dpcp_from_lock(l);
1426 int err = 0;
1427 int home;
1428
1429 preempt_disable();
1430
1431 if (sem->pcp.on_cpu != smp_processor_id() || sem->pcp.owner != t) {
1432 err = -EINVAL;
1433 goto out;
1434 }
1435
1436 home = sem->owner_cpu;
1437
1438 /* give it back */
1439 pcp_lower_ceiling(&sem->pcp);
1440
1441 /* we lose the benefit of priority boosting */
1442 unboost_priority(t);
1443
1444 pfp_migrate_to(home);
1445
1446out:
1447 preempt_enable();
1448
1449 return err;
1450}
1451
1452int pfp_dpcp_open(struct litmus_lock* l, void* __user config)
1453{
1454 struct task_struct *t = current;
1455 struct dpcp_semaphore *sem = dpcp_from_lock(l);
1456 int cpu, eprio;
1457
1458 if (!is_realtime(t))
1459 /* we need to know the real-time priority */
1460 return -EPERM;
1461
1462 if (get_user(cpu, (int*) config))
1463 return -EFAULT;
1464
1465 /* make sure the resource location matches */
1466 if (cpu != sem->pcp.on_cpu)
1467 return -EINVAL;
1468
1469 eprio = effective_agent_priority(get_priority(t));
1470
1471 pcp_update_prio_ceiling(&sem->pcp, eprio);
1472
1473 return 0;
1474}
1475
1476int pfp_dpcp_close(struct litmus_lock* l)
1477{
1478 struct task_struct *t = current;
1479 struct dpcp_semaphore *sem = dpcp_from_lock(l);
1480 int owner = 0;
1481
1482 preempt_disable();
1483
1484 if (sem->pcp.on_cpu == smp_processor_id())
1485 owner = sem->pcp.owner == t;
1486
1487 preempt_enable();
1488
1489 if (owner)
1490 pfp_dpcp_unlock(l);
1491
1492 return 0;
1493}
1494
1495void pfp_dpcp_free(struct litmus_lock* lock)
1496{
1497 kfree(dpcp_from_lock(lock));
1498}
1499
1500static struct litmus_lock_ops pfp_dpcp_lock_ops = {
1501 .close = pfp_dpcp_close,
1502 .lock = pfp_dpcp_lock,
1503 .open = pfp_dpcp_open,
1504 .unlock = pfp_dpcp_unlock,
1505 .deallocate = pfp_dpcp_free,
1506};
1507
1508static struct litmus_lock* pfp_new_dpcp(int on_cpu)
1509{
1510 struct dpcp_semaphore* sem;
1511
1512 sem = kmalloc(sizeof(*sem), GFP_KERNEL);
1513 if (!sem)
1514 return NULL;
1515
1516 sem->litmus_lock.ops = &pfp_dpcp_lock_ops;
1517 sem->owner_cpu = NO_CPU;
1518 pcp_init_semaphore(&sem->pcp, on_cpu);
1519
1520 return &sem->litmus_lock;
1521}
1522
1523
1524/* **** lock constructor **** */
1525
1526
1527static long pfp_allocate_lock(struct litmus_lock **lock, int type,
1528 void* __user config)
1529{
1530 int err = -ENXIO, cpu;
1531 struct srp_semaphore* srp;
1532
1533 /* P-FP currently supports the SRP for local resources and the FMLP
1534 * for global resources. */
1535 switch (type) {
1536 case FMLP_SEM:
1537 /* FIFO Mutex Locking Protocol */
1538 *lock = pfp_new_fmlp();
1539 if (*lock)
1540 err = 0;
1541 else
1542 err = -ENOMEM;
1543 break;
1544
1545 case MPCP_SEM:
1546 /* Multiprocesor Priority Ceiling Protocol */
1547 *lock = pfp_new_mpcp(0);
1548 if (*lock)
1549 err = 0;
1550 else
1551 err = -ENOMEM;
1552 break;
1553
1554 case MPCP_VS_SEM:
1555 /* Multiprocesor Priority Ceiling Protocol with virtual spinning */
1556 *lock = pfp_new_mpcp(1);
1557 if (*lock)
1558 err = 0;
1559 else
1560 err = -ENOMEM;
1561 break;
1562
1563 case DPCP_SEM:
1564 /* Distributed Priority Ceiling Protocol */
1565 if (get_user(cpu, (int*) config))
1566 return -EFAULT;
1567
1568 if (!cpu_online(cpu))
1569 return -EINVAL;
1570
1571 *lock = pfp_new_dpcp(cpu);
1572 if (*lock)
1573 err = 0;
1574 else
1575 err = -ENOMEM;
1576 break;
1577
1578 case SRP_SEM:
1579 /* Baker's Stack Resource Policy */
1580 srp = allocate_srp_semaphore();
1581 if (srp) {
1582 *lock = &srp->litmus_lock;
1583 err = 0;
1584 } else
1585 err = -ENOMEM;
1586 break;
1587
1588 case PCP_SEM:
1589 /* Priority Ceiling Protocol */
1590 if (get_user(cpu, (int*) config))
1591 return -EFAULT;
1592
1593 if (!cpu_online(cpu))
1594 return -EINVAL;
1595
1596 *lock = pfp_new_pcp(cpu);
1597 if (*lock)
1598 err = 0;
1599 else
1600 err = -ENOMEM;
1601 break;
1602 };
1603
1604 return err;
1605}
1606
1607#endif
1608
1609static long pfp_admit_task(struct task_struct* tsk)
1610{
1611 if (task_cpu(tsk) == tsk->rt_param.task_params.cpu &&
1612#ifdef CONFIG_RELEASE_MASTER
1613 /* don't allow tasks on release master CPU */
1614 task_cpu(tsk) != remote_dom(task_cpu(tsk))->release_master &&
1615#endif
1616 litmus_is_valid_fixed_prio(get_priority(tsk)))
1617 return 0;
1618 else
1619 return -EINVAL;
1620}
1621
1622static long pfp_activate_plugin(void)
1623{
1624#if defined(CONFIG_RELEASE_MASTER) || defined(CONFIG_LITMUS_LOCKING)
1625 int cpu;
1626#endif
1627
1628#ifdef CONFIG_RELEASE_MASTER
1629 for_each_online_cpu(cpu) {
1630 remote_dom(cpu)->release_master = atomic_read(&release_master_cpu);
1631 }
1632#endif
1633
1634#ifdef CONFIG_LITMUS_LOCKING
1635 get_srp_prio = pfp_get_srp_prio;
1636
1637 for_each_online_cpu(cpu) {
1638 init_waitqueue_head(&per_cpu(mpcpvs_vspin_wait, cpu));
1639 per_cpu(mpcpvs_vspin, cpu) = NULL;
1640
1641 pcp_init_state(&per_cpu(pcp_state, cpu));
1642 pfp_doms[cpu] = remote_pfp(cpu);
1643 }
1644
1645#endif
1646
1647 return 0;
1648}
1649
1650
1651/* Plugin object */
1652static struct sched_plugin pfp_plugin __cacheline_aligned_in_smp = {
1653 .plugin_name = "P-FP",
1654 .tick = pfp_tick,
1655 .task_new = pfp_task_new,
1656 .complete_job = complete_job,
1657 .task_exit = pfp_task_exit,
1658 .schedule = pfp_schedule,
1659 .task_wake_up = pfp_task_wake_up,
1660 .task_block = pfp_task_block,
1661 .admit_task = pfp_admit_task,
1662 .activate_plugin = pfp_activate_plugin,
1663#ifdef CONFIG_LITMUS_LOCKING
1664 .allocate_lock = pfp_allocate_lock,
1665 .finish_switch = pfp_finish_switch,
1666#endif
1667};
1668
1669
1670static int __init init_pfp(void)
1671{
1672 int i;
1673
1674 /* We do not really want to support cpu hotplug, do we? ;)
1675 * However, if we are so crazy to do so,
1676 * we cannot use num_online_cpu()
1677 */
1678 for (i = 0; i < num_online_cpus(); i++) {
1679 pfp_domain_init(remote_pfp(i), i);
1680 }
1681 return register_sched_plugin(&pfp_plugin);
1682}
1683
1684module_init(init_pfp);
1685