-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsched_pfp_mrsp.c
1379 lines (1042 loc) · 32.2 KB
/
sched_pfp_mrsp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* litmus/sched_pfp.c
*
* Implementation of partitioned fixed-priority scheduling.
* Based on PSN-EDF.
*/
#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <litmus/litmus.h>
#include <litmus/wait.h>
#include <litmus/jobs.h>
#include <litmus/preempt.h>
#include <litmus/fp_common.h>
#include <litmus/sched_plugin.h>
#include <litmus/sched_trace.h>
#include <litmus/trace.h>
#include <litmus/budget.h>
#include <linux/uaccess.h>
#include <litmus/fdso.h>
int init_finished;
int _flag;
/*typedef struct queue_s {
struct list_head next;
struct task_struct* task;
} queue_t;*/
struct mrsp_semaphore {
struct litmus_lock litmus_lock;
/* lock for mutual access to the struct */
spinlock_t lock;
/* tasks queue for resource access */
struct list_head task_queue;
/* current resource holder */
struct task_struct *owner;
/* priority ceiling for each cpu*/
int prio_ceiling[NR_CPUS];
};
struct mrsp_state {
int cpu_ceiling;
};
typedef struct {
rt_domain_t domain;
struct fp_prio_queue ready_queue;
int cpu;
struct task_struct* scheduled; /* current task scheduled */
struct mrsp_semaphore* sem;
struct mrsp_state* mrsp_ceiling;
/*
* scheduling lock slock
* protects the domain and serializes scheduling decisions
*/
#define slock domain.ready_lock
} pfp_domain_t;
#define MIGRATION (-511)
//#define ASAP
//#define PREEMPTED_MIGRATION
//#define QUEUED_MIGRATION
//#define RUNNING_MIGRATION
DEFINE_PER_CPU(pfp_domain_t, pfp_domains);
pfp_domain_t* pfp_doms[NR_CPUS];
#define local_pfp (&__get_cpu_var(pfp_domains))
#define remote_dom(cpu) (&per_cpu(pfp_domains, cpu).domain)
#define remote_pfp(cpu) (&per_cpu(pfp_domains, cpu))
#define task_dom(task) remote_dom(get_partition(task))
#define task_pfp(task) remote_pfp(get_partition(task))
void print_queue(struct mrsp_semaphore *sem)
{
struct list_head * next;
TRACE("Init Print\n");
list_for_each(next, &(sem->task_queue)) {
queue_t * elem;
elem = list_entry(next, queue_t, next);
TRACE_TASK(elem->task," <- task queue and the home is %d\n", get_home(elem->task));
}
TRACE("Finish Print\n");
}
void print_queue_star(struct mrsp_semaphore *sem)
{
struct list_head * next;
TRACE("[*]Init Print\n");
list_for_each(next, &(sem->task_queue)) {
queue_t * elem;
elem = list_entry(next, queue_t, next);
TRACE_TASK(elem->task,"[*] <- task queue and the home is %d\n", get_home(elem->task));
}
TRACE("[*]Finish Print\n");
}
//
// Add an element to the end of the list, so it will be the last one popped
//
queue_t * queue_add_fifo(struct mrsp_semaphore *sem, struct task_struct* task)
{
/*queue_t * next;
next = (queue_t *) kmalloc(sizeof(queue_t), GFP_KERNEL);
next->task = task;*/
list_add_tail(&tsk_rt(task)->task_params.next->next, &(sem->task_queue) );
return(tsk_rt(task)->task_params.next);
}
//
// Add an element to the head of the list, so it will be the next one popped
//
queue_t * queue_add_lifo(struct mrsp_semaphore *sem, struct task_struct* task)
{
queue_t * next;
next = (queue_t *) kmalloc(sizeof(queue_t), GFP_KERNEL);
next->task = task;
//
// Add to the head for LIFO (stack) queueing
//
list_add(&next->next, &(sem->task_queue) );
return(next);
}
//
// Pop an element off the head of the queue
//
queue_t * queue_pop(struct mrsp_semaphore *sem)
{
//
// Check if the queue is empty
//
if (!list_empty(&(sem->task_queue))) {
queue_t * next;
//
// Get the first entry on the list. This is the one that task_queue.next points to.
//
next = list_entry(sem->task_queue.next,queue_t,next);
list_del(&next->next);
return(next);
} else {
return(NULL);
}
}
//
// Pop an element off the tail of the queue
//
queue_t * queue_pop_tail(struct mrsp_semaphore *sem)
{
//
// Check if the queue is empty
//
if (!list_empty(&(sem->task_queue))) {
queue_t * next;
//
// Get the last entry on the list. This is the one that task_queue.prev points to.
//
next = list_entry(sem->task_queue.prev,queue_t,next);
list_del(&next->next);
return(next);
} else {
return(NULL);
}
}
queue_t * find_queue_entry(struct mrsp_semaphore *sem, int cpu)
{
struct list_head * next;
pfp_domain_t *remote_domain;
list_for_each(next, &(sem->task_queue)) {
queue_t * elem;
elem = list_entry(next, queue_t, next);
if (get_home(elem->task) != cpu) {
remote_domain = task_pfp(elem->task);
if(remote_domain->scheduled == NULL) {
return(elem);
}
if((remote_domain->sem->prio_ceiling[get_home(elem->task)] - 2) < get_priority(remote_domain->scheduled)) {
return(elem);
}
}
}
return(NULL);
}
//
// Delete all entries from the queue that match the data value
//
void del_queue_entry(struct mrsp_semaphore *sem, struct task_struct* task)
{
struct list_head * next;
struct list_head * temp;
//
// Iterate over the list. We need to use the safe iterator because we may want to
// remove things.
//
list_for_each_safe(next, temp, &(sem->task_queue)) {
queue_t * elem;
//
// Get a pointer to the element on the list
//
elem = list_entry(next, queue_t, next);
//
// Delete the element if the data field matches
//
if (elem->task == task) {
list_del(&elem->next);
}
}
}
/*8888888888888888888888888888888888888888888888888888888888*/
/* we assume the lock is being held */
static void preempt(pfp_domain_t *pfp)
{
preempt_if_preemptable(pfp->scheduled, pfp->cpu);
}
static unsigned int priority_index(struct task_struct* t)
{
#ifdef CONFIG_LITMUS_LOCKING
if (unlikely(t->rt_param.inh_task))
/* use effective priority */
t = t->rt_param.inh_task;
if (is_priority_boosted(t)) {
/* zero is reserved for priority-boosted tasks */
return 0;
} else
#endif
return get_priority(t);
}
static void fp_dequeue(pfp_domain_t* pfp, struct task_struct* t)
{
BUG_ON(pfp->scheduled == t && is_queued(t));
if (is_queued(t))
fp_prio_remove(&pfp->ready_queue, t, priority_index(t));
}
static void pfp_release_jobs(rt_domain_t* rt, struct bheap* tasks)
{
pfp_domain_t *pfp = container_of(rt, pfp_domain_t, domain);
unsigned long flags;
struct task_struct* t;
struct bheap_node* hn;
raw_spin_lock_irqsave(&pfp->slock, flags);
while (!bheap_empty(tasks)) {
hn = bheap_take(fp_ready_order, tasks);
t = bheap2task(hn);
TRACE_TASK(t, "released (part:%d prio:%d)\n",
get_partition(t), get_priority(t));
fp_prio_add(&pfp->ready_queue, t, priority_index(t));
}
/* do we need to preempt? */
if (fp_higher_prio(fp_prio_peek(&pfp->ready_queue), pfp->scheduled)) {
TRACE_CUR("preempted by new release\n");
preempt(pfp);
}
raw_spin_unlock_irqrestore(&pfp->slock, flags);
}
static void pfp_preempt_check(pfp_domain_t *pfp)
{
if (fp_higher_prio(fp_prio_peek(&pfp->ready_queue), pfp->scheduled))
preempt(pfp);
}
static void pfp_domain_init(pfp_domain_t* pfp,
int cpu)
{
fp_domain_init(&pfp->domain, NULL, pfp_release_jobs);
pfp->cpu = cpu;
pfp->scheduled = NULL;
fp_prio_queue_init(&pfp->ready_queue);
}
static void requeue(struct task_struct* t, pfp_domain_t *pfp)
{
BUG_ON(!is_running(t));
tsk_rt(t)->completed = 0;
if (is_released(t, litmus_clock()))
fp_prio_add(&pfp->ready_queue, t, priority_index(t));
else
add_release(&pfp->domain, t); /* it has got to wait */
}
static void job_completion(struct task_struct* t, int forced)
{
sched_trace_task_completion(t,forced);
TRACE_TASK(t, "job_completion().\n");
tsk_rt(t)->completed = 0;
prepare_for_next_period(t);
if (is_released(t, litmus_clock()))
sched_trace_task_release(t);
}
static void pfp_tick(struct task_struct *t)
{
pfp_domain_t *pfp = local_pfp;
/* Check for inconsistency. We don't need the lock for this since
* ->scheduled is only changed in schedule, which obviously is not
* executing in parallel on this CPU
*/
BUG_ON(is_realtime(t) && t != pfp->scheduled);
if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) {
if (!is_np(t)) {
litmus_reschedule_local();
TRACE("pfp_scheduler_tick: %d is preemptable => FORCE_RESCHED\n", t->pid);
} else if (is_user_np(t)) {
TRACE("pfp_scheduler_tick: %d is non-preemptable, preemption delayed.\n", t->pid);
request_exit_np(t);
}
}
}
/* Prepare a task for running in RT mode
*/
static void pfp_task_new(struct task_struct * t, int on_rq, int is_scheduled)
{
pfp_domain_t* pfp = task_pfp(t);
unsigned long flags;
/* setup job parameters */
release_at(t, litmus_clock());
raw_spin_lock_irqsave(&pfp->slock, flags);
if (is_scheduled) {
/* there shouldn't be anything else running at the time */
BUG_ON(pfp->scheduled);
pfp->scheduled = t;
} else if (is_running(t)) {
requeue(t, pfp);
/* maybe we have to reschedule */
pfp_preempt_check(pfp);
}
raw_spin_unlock_irqrestore(&pfp->slock, flags);
}
static void pfp_task_wake_up(struct task_struct *task)
{
unsigned long flags;
pfp_domain_t* pfp = task_pfp(task);
lt_t now;
TRACE_TASK(task, "wake_up at %llu\n", litmus_clock());
raw_spin_lock_irqsave(&pfp->slock, flags);
/* Should only be queued when processing a fake-wake up due to a
* migration-related state change. */
if (unlikely(is_queued(task))) {
TRACE_TASK(task, "WARNING: waking task still queued. Is this right?\n");
goto out_unlock;
}
now = litmus_clock();
if (is_sporadic(task) && is_tardy(task, now)
/* We need to take suspensions because of semaphores into
* account! If a job resumes after being suspended due to acquiring
* a semaphore, it should never be treated as a new job release.
*/
&& !is_priority_boosted(task)) {
/* new sporadic release */
release_at(task, now);
sched_trace_task_release(task);
}
/* Only add to ready queue if it is not the currently-scheduled
* task. This could be the case if a task was woken up concurrently
* on a remote CPU before the executing CPU got around to actually
* de-scheduling the task, i.e., wake_up() raced with schedule()
* and won. Also, don't requeue if it is still queued, which can
* happen under the DPCP due wake-ups racing with migrations.
*/
if (pfp->scheduled != task) {
requeue(task, pfp);
pfp_preempt_check(pfp);
}
#ifdef CONFIG_LITMUS_LOCKING
out_unlock:
#endif
raw_spin_unlock_irqrestore(&pfp->slock, flags);
TRACE_TASK(task, "wake up done\n");
}
static void pfp_task_block(struct task_struct *t)
{
/* only running tasks can block, thus t is in no queue */
TRACE_TASK(t, "[pfp_task_block] block at %llu, state=%d\n", litmus_clock(), t->state);
BUG_ON(!is_realtime(t));
/* If this task blocked normally, it shouldn't be queued. The exception is
* if this is a simulated block()/wakeup() pair from the pull-migration code path.
* This should only happen if the DPCP is being used.
*/
#ifdef CONFIG_LITMUS_LOCKING
if (unlikely(is_queued(t)))
TRACE_TASK(t, "WARNING: blocking task still queued. Is this right?\n");
#else
BUG_ON(is_queued(t));
#endif
}
static void pfp_task_exit(struct task_struct * t)
{
unsigned long flags;
pfp_domain_t* pfp = task_pfp(t);
rt_domain_t* dom;
raw_spin_lock_irqsave(&pfp->slock, flags);
if (is_queued(t)) {
BUG(); /* This currently doesn't work. */
/* dequeue */
dom = task_dom(t);
remove(dom, t);
}
if (pfp->scheduled == t) {
pfp->scheduled = NULL;
preempt(pfp);
}
TRACE_TASK(t, "RIP, now reschedule\n");
raw_spin_unlock_irqrestore(&pfp->slock, flags);
}
/* ----------- MRSP SUPPORT ---------- */
/* Promemoria:
- Prima task_new poi _open
- *1
*/
/* need_to_preempt - check whether the task t needs to be preempted
*/
int keep_run(struct fp_prio_queue *q, struct task_struct *t)
{
struct task_struct *pending;
pending = fp_prio_peek(q);
if (!pending)
return 1;
if (get_priority(t) < get_priority(pending))
return 1;
return 0;
}
static void mrsp_migrate_to_from_resource(int target_cpu, struct task_struct* lock_holder)
{
struct task_struct* t = lock_holder;
pfp_domain_t *from;
if (get_partition(t) == target_cpu) {
// If I am in my own cpu, I will check If preemption is need
if (!keep_run(&task_pfp(t)->ready_queue, t))
preempt(task_pfp(t));
return;
} else {
from = task_pfp(t);
tsk_rt(t)->task_params.cpu = target_cpu;
preempt(from);
}
#ifdef RUNNING_MIGRATION
if(_flag == 0) {
TS_MRSP2_START;
TS_MRSP2_END;
_flag++;
t->rt_param.task_params.migrating = 1;
}
#endif
}
static void cpu_again_available_for_migration(int from_cpu, int target_cpu, struct task_struct* owner)
{
pfp_domain_t *from = remote_pfp(from_cpu);
pfp_domain_t *to = remote_pfp(target_cpu);
bool fail = false;
unsigned long flags1;
unsigned long flags2;
preempt_disable();
raw_spin_lock_irqsave(&from->slock, flags1);
// still queued here?
if(is_queued(owner)) {
tsk_rt(owner)->task_params.cpu = target_cpu;
fp_dequeue(from, owner);
} else {
fail = true;
}
raw_spin_unlock_irqrestore(&from->slock, flags1);
if(!fail) {
raw_spin_lock_irqsave(&to->slock, flags2);
owner->rt_param.task_params.priority = (to->sem->prio_ceiling[target_cpu] - 2);
requeue(owner, to);
preempt(to);
raw_spin_unlock_irqrestore(&to->slock, flags2);
}
preempt_enable();
}
static void mrsp_dequeue_and_migrate(int from_cpu, int target_cpu, struct task_struct* owner)
{
pfp_domain_t *from = remote_pfp(from_cpu);
pfp_domain_t *to = remote_pfp(target_cpu);
bool fail = false;
local_irq_disable();
raw_spin_lock(&from->slock);
// Nel frattempo l'owner potrebbe essere diventato running, quindi ricontrollo
if(is_queued(owner)) {
tsk_rt(owner)->task_params.cpu = target_cpu;
fp_dequeue(from, owner);
} else {
fail = true;
}
raw_spin_unlock(&from->slock);
if(!fail) {
raw_spin_lock(&to->slock);
owner->rt_param.task_params.priority = (to->sem->prio_ceiling[target_cpu] - 2);
requeue(owner, to);
raw_spin_unlock(&to->slock);
}
local_irq_enable();
if(!fail) {
preempt_enable_no_resched();
/* deschedule to be migrated */
#ifdef QUEUED_MIGRATION
if(_flag == 0) {
TS_MRSP2_START;
TS_MRSP2_END;
_flag++;
owner->rt_param.task_params.migrating = 1;
}
#endif
schedule();
preempt_disable();
}
}
static void mrsp_wake_up_next_lock_holder(int from_cpu, int target_cpu, struct task_struct* owner)
{
pfp_domain_t *from = remote_pfp(from_cpu);
pfp_domain_t *to = remote_pfp(target_cpu);
bool fail = false;
local_irq_disable();
raw_spin_lock(&from->slock);
// still queued here?
if(is_queued(owner) && tsk_rt(owner)->task_params.cpu == tsk_rt(owner)->task_params.home) {
tsk_rt(owner)->task_params.cpu = target_cpu;
fp_dequeue(from, owner);
} else {
fail = true;
}
raw_spin_unlock(&from->slock);
if(!fail) {
raw_spin_lock(&to->slock);
owner->rt_param.task_params.priority = (to->sem->prio_ceiling[target_cpu] - 2);
requeue(owner, to);
preempt(to);
#ifdef QUEUED_MIGRATION
if(_flag == 0) {
TS_MRSP2_START;
TS_MRSP2_END;
_flag++;
owner->rt_param.task_params.migrating = 1;
}
#endif
raw_spin_unlock(&to->slock);
}
local_irq_enable();
}
static void perform_ASAP(struct task_struct* owner)
{
pfp_domain_t *remote_cpu = remote_pfp(get_partition(owner));
TRACE("[perform_ASAP]\n");
raw_spin_lock(&remote_cpu->slock);
BUG_ON(tsk_rt(owner)->task_params.cpu == tsk_rt(owner)->task_params.home);
BUG_ON(is_queued(owner));
// faccio in modo che venga prerilasciato dal job che sta effettuando busy wait
owner->rt_param.task_params.priority = (remote_cpu->sem->prio_ceiling[get_partition(owner)]);
preempt(remote_cpu);
raw_spin_unlock(&remote_cpu->slock);
}
static void mrsp_init_state(struct mrsp_state* s)
{
s->cpu_ceiling = LITMUS_LOWEST_PRIORITY;
}
static DEFINE_PER_CPU(struct mrsp_state, mrsp_state);
static int cpu_queued(void) {
if(__get_cpu_var(mrsp_state).cpu_ceiling < LITMUS_LOWEST_PRIORITY - 10)
return 1;
return 0;
}
static inline struct mrsp_semaphore* mrsp_from_lock(struct litmus_lock* lock)
{
return container_of(lock, struct mrsp_semaphore, litmus_lock);
}
int pfp_mrsp_lock(struct litmus_lock* l)
{
struct task_struct* t = current;
struct task_struct* owner = NULL;
struct mrsp_semaphore *sem = mrsp_from_lock(l);
queue_t * next;
if (!is_realtime(t))
return -EPERM;
BUG_ON(sem->owner == t);
if(init_finished == 1) {
preempt_disable();
spin_lock(&sem->lock);
//TS_MRSPLOCK_START;
__get_cpu_var(mrsp_state).cpu_ceiling = (sem->prio_ceiling[get_partition(t)]);
queue_add_fifo(sem, t);
next = list_entry(sem->task_queue.next,queue_t,next);
TRACE_TASK(t, "first try\n");
if(sem->owner == NULL && next->task == t) {
sem->owner = t;
t->rt_param.task_params.priority = (sem->prio_ceiling[get_partition(t)] - 2);
} else {
t->rt_param.task_params.priority = (sem->prio_ceiling[get_partition(t)] - 1);
if(sem->owner != NULL)
if(is_running(sem->owner) && is_queued(sem->owner))
owner = sem->owner;
}
//TS_MRSPLOCK_END;
spin_unlock(&sem->lock);
if(owner) {
mrsp_dequeue_and_migrate(get_partition(owner), get_partition(t), owner);
}
preempt_enable();
if(sem->owner != t)
do {
spin_lock(&sem->lock);
TS_MRSPLOCK_START;
next = list_entry(sem->task_queue.next,queue_t,next);
if(sem->owner == NULL && next->task == t) {
sem->owner = t;
t->rt_param.task_params.priority = (sem->prio_ceiling[get_partition(t)] - 2);
}
TS_MRSPLOCK_END;
spin_unlock(&sem->lock);
} while(sem->owner != t);
TRACE_TASK(t, "lock!\n");
}
return 0;
}
int pfp_mrsp_unlock(struct litmus_lock* l)
{
struct task_struct *t = current;
struct mrsp_semaphore *sem = mrsp_from_lock(l);
queue_t *node;
queue_t * next_lock_holder;
int err = 0;
struct task_struct* next_owner = NULL;
int from_cpu;
int target_cpu;
if (!is_realtime(t))
return -EPERM;
if(init_finished == 1) {
preempt_disable();
spin_lock(&sem->lock);
sem->owner = NULL;
(*(pfp_doms[get_home(t)]->mrsp_ceiling)).cpu_ceiling = LITMUS_LOWEST_PRIORITY;
t->rt_param.task_params.priority = t->rt_param.task_params.priority_for_restore;
// remove task from the queue
queue_pop(sem);
//pfp_doms[get_home(t)]->mrsp_ceiling.cpu_ceiling = LITMUS_LOWEST_PRIORITY;
//tsk_rt(t)->task_params.mrsp_ceiling.cpu_ceiling = LITMUS_LOWEST_PRIORITY;
TS_MRSPUNLOCK_START;
// Is the next lock holder queued somewhere?
if(!list_empty(&(sem->task_queue))) {
next_lock_holder = list_entry(sem->task_queue.next,queue_t,next);
if(is_queued(next_lock_holder->task)) {
if(get_partition(next_lock_holder->task) != get_partition(t)) {
node = find_queue_entry(sem,200000);
if(node != NULL) {
next_owner = next_lock_holder->task;
from_cpu = get_partition(next_owner);
target_cpu = get_partition(node->task);
TS_MRSPUNLOCK_END;
}
}
}
}
spin_unlock(&sem->lock);
if(next_owner) {
mrsp_wake_up_next_lock_holder(from_cpu, target_cpu, next_owner);
}
// che sfoggerellini mi orecchi??
if(get_partition(t) != t->rt_param.task_params.home) {
mrsp_migrate_to_from_resource(t->rt_param.task_params.home, t);
}
preempt_enable();
TRACE_TASK(t, "unlock!\n");
}
return err;
}
static void mrsp_update_prio_ceiling(struct mrsp_semaphore* sem,
int effective_prio, int cpu)
{
unsigned long flags;
spin_lock_irqsave(&sem->lock, flags);
sem->prio_ceiling[cpu] = min(sem->prio_ceiling[cpu], effective_prio);
spin_unlock_irqrestore(&sem->lock, flags);
}
int pfp_mrsp_open(struct litmus_lock* l, void* __user config)
{
struct task_struct *t = current;
struct mrsp_semaphore *sem = mrsp_from_lock(l);
int cpu, eprio;
if (!is_realtime(t))
return -EPERM;
cpu = get_partition(t);
TRACE_TASK(t, "[START] pfp_mrsp_open at %llu with priority %d\n", litmus_clock(), sem->prio_ceiling[cpu]);
if (!config)
cpu = get_partition(t);
else if (get_user(cpu, (int*) config))
return -EFAULT;
eprio = tsk_rt(t)->task_params.priority_for_restore;
mrsp_update_prio_ceiling(sem, eprio, cpu);
TRACE_TASK(t, "[END] pfp_mrsp_open at %llu with priority %d\n", litmus_clock(), sem->prio_ceiling[cpu]);
return 0;
}
int pfp_mrsp_close(struct litmus_lock* l)
{
struct task_struct *t = current;
struct mrsp_semaphore *sem = mrsp_from_lock(l);
unsigned long flags;
int owner;
spin_lock_irqsave(&sem->lock, flags);
owner = sem->owner == t;
spin_unlock_irqrestore(&sem->lock, flags);
TRACE("[CLOSE]\n");
if (owner)
pfp_mrsp_unlock(l);
return 0;
}
void pfp_mrsp_free(struct litmus_lock* lock)
{
TRACE("[pfp_mrsp_free]\n");
kfree(mrsp_from_lock(lock));
}
static struct litmus_lock_ops pfp_mrsp_lock_ops = {
.close = pfp_mrsp_close,
.lock = pfp_mrsp_lock,
.open = pfp_mrsp_open,
.unlock = pfp_mrsp_unlock,
.deallocate = pfp_mrsp_free,
};
static void mrsp_init_semaphore(struct mrsp_semaphore* sem)
{
int cpu;
sem->owner = NULL;
INIT_LIST_HEAD(&(sem->task_queue));
spin_lock_init(&sem->lock);
for (cpu = 0; cpu < NR_CPUS; cpu++)
sem->prio_ceiling[cpu] = (LITMUS_MAX_PRIORITY - 2);
}
static struct litmus_lock* pfp_new_mrsp(void)
{
struct mrsp_semaphore* sem;
int cpu;
sem = kmalloc(sizeof(*sem), GFP_KERNEL);
if (!sem)
return NULL;
sem->litmus_lock.ops = &pfp_mrsp_lock_ops;
mrsp_init_semaphore(sem);
for_each_online_cpu(cpu) {
pfp_doms[cpu]->sem = sem;
}
return &sem->litmus_lock;
}
/* **** lock constructor **** */
static long pfp_complete_job(void)
{
TRACE_TASK(current, "pfp_complete_job at %llu\n", litmus_clock());
return complete_job();
}
static long pfp_allocate_lock(struct litmus_lock **lock, int type,
void* __user config)
{
int err = -ENXIO;
switch (type) {
case MRSP_SEM:
*lock = pfp_new_mrsp();
if (*lock)
err = 0;
else
err = -ENOMEM;
break;
};
return err;
}
static struct task_struct* pfp_schedule(struct task_struct * prev)
{
pfp_domain_t* pfp = local_pfp;
struct task_struct* next;
int out_of_time, sleep, preempt, np, exists, blocks, resched, migrate;
// by zeb
int lock_holder, placeholder;
raw_spin_lock(&pfp->slock);