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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
|
#include <linux/interrupt.h>
#include <linux/percpu.h>
#include <linux/cpu.h>
#include <linux/kthread.h>
#include <linux/ftrace.h>
#include <linux/smp.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/cpuset.h>
#include <litmus/litmus.h>
#include <litmus/sched_trace.h>
#include <litmus/jobs.h>
#include <litmus/sched_plugin.h>
#include <litmus/litmus_softirq.h>
/* TODO: Remove unneeded mb() and other barriers. */
enum pending_flags
{
LIT_TASKLET_LOW = 0x1,
LIT_TASKLET_HI = LIT_TASKLET_LOW<<1,
LIT_WORK = LIT_TASKLET_HI<<1
};
struct klmirqd_registration
{
raw_spinlock_t lock;
u32 nr_threads;
unsigned int initialized:1;
unsigned int shuttingdown:1;
struct list_head threads;
};
static atomic_t klmirqd_id_gen = ATOMIC_INIT(-1);
static struct klmirqd_registration klmirqd_state;
void init_klmirqd(void)
{
raw_spin_lock_init(&klmirqd_state.lock);
klmirqd_state.nr_threads = 0;
klmirqd_state.initialized = 1;
klmirqd_state.shuttingdown = 0;
INIT_LIST_HEAD(&klmirqd_state.threads);
}
static int __klmirqd_is_ready(void)
{
return (klmirqd_state.initialized == 1 && klmirqd_state.shuttingdown == 0);
}
int klmirqd_is_ready(void)
{
unsigned long flags;
int ret;
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
ret = __klmirqd_is_ready();
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
return ret;
}
int klmirqd_is_dead(void)
{
return(!klmirqd_is_ready());
}
void kill_klmirqd(void)
{
if(!klmirqd_is_dead())
{
unsigned long flags;
struct list_head *pos;
struct list_head *q;
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
TRACE("%s: Killing all klmirqd threads! (%d of them)\n", __FUNCTION__, klmirqd_state.nr_threads);
klmirqd_state.shuttingdown = 1;
list_for_each_safe(pos, q, &klmirqd_state.threads) {
struct klmirqd_info* info = list_entry(pos, struct klmirqd_info, klmirqd_reg);
if(info->terminating != 1)
{
info->terminating = 1;
mb(); /* just to be sure? */
flush_pending(info->klmirqd);
/* signal termination */
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
kthread_stop(info->klmirqd);
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
}
}
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
}
}
void kill_klmirqd_thread(struct task_struct* klmirqd_thread)
{
unsigned long flags;
struct klmirqd_info* info;
if (!tsk_rt(klmirqd_thread)->is_interrupt_thread) {
TRACE("%s/%d is not a klmirqd thread\n", klmirqd_thread->comm, klmirqd_thread->pid);
return;
}
TRACE("%s: Killing klmirqd thread %s/%d\n", __FUNCTION__, klmirqd_thread->comm, klmirqd_thread->pid);
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
info = tsk_rt(klmirqd_thread)->klmirqd_info;
if(info->terminating != 1) {
info->terminating = 1;
mb();
flush_pending(klmirqd_thread);
kthread_stop(klmirqd_thread);
}
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
}
struct klmirqd_launch_data
{
int cpu_affinity;
klmirqd_callback_t* cb;
char name[MAX_KLMIRQD_NAME_LEN+1];
struct work_struct work;
};
static int run_klmirqd(void* callback);
/* executed by a kworker from workqueues */
static void __launch_klmirqd_thread(struct work_struct *work)
{
int id;
struct task_struct* thread = NULL;
struct klmirqd_launch_data* launch_data =
container_of(work, struct klmirqd_launch_data, work);
TRACE("Creating klmirqd thread\n");
if (launch_data->cpu_affinity != -1) {
if (launch_data->name[0] == '\0') {
id = atomic_inc_return(&klmirqd_id_gen);
TRACE("Launching klmirqd_th%d/%d\n", id, launch_data->cpu_affinity);
thread = kthread_create(
run_klmirqd,
/* treat the affinity as a pointer, we'll cast it back later */
(void*)launch_data->cb,
"klmirqd_th%d/%d",
id,
launch_data->cpu_affinity);
}
else {
TRACE("Launching %s/%d\n", launch_data->name, launch_data->cpu_affinity);
thread = kthread_create(
run_klmirqd,
/* treat the affinity as a pointer, we'll cast it back later */
(void*)launch_data->cb,
"%s/%d",
launch_data->name,
launch_data->cpu_affinity);
}
/* litmus will put is in the right cluster. */
kthread_bind(thread, launch_data->cpu_affinity);
}
else {
if (launch_data->name[0] == '\0') {
id = atomic_inc_return(&klmirqd_id_gen);
TRACE("Launching klmirqd_th%d\n", id);
thread = kthread_create(
run_klmirqd,
/* treat the affinity as a pointer, we'll cast it back later */
(void*)launch_data->cb,
"klmirqd_th%d",
id);
}
else {
TRACE("Launching %s\n", launch_data->name);
thread = kthread_create(
run_klmirqd,
/* treat the affinity as a pointer, we'll cast it back later */
(void*)launch_data->cb,
launch_data->name);
}
}
if (thread) {
wake_up_process(thread);
}
else {
TRACE("Could not create thread!\n");
}
kfree(launch_data);
}
int launch_klmirqd_thread(char* name, int cpu, klmirqd_callback_t* cb)
{
struct klmirqd_launch_data* delayed_launch;
if (!klmirqd_is_ready()) {
TRACE("klmirqd is not ready. Check that it was initialized!\n");
return -1;
}
/* tell a work queue to launch the threads. we can't make scheduling
calls since we're in an atomic state. */
delayed_launch = kmalloc(sizeof(struct klmirqd_launch_data), GFP_ATOMIC);
delayed_launch->cpu_affinity = cpu;
delayed_launch->cb = cb;
INIT_WORK(&delayed_launch->work, __launch_klmirqd_thread);
if(name) {
snprintf(delayed_launch->name, MAX_KLMIRQD_NAME_LEN+1, "%s", name);
}
else {
delayed_launch->name[0] = '\0';
}
schedule_work(&delayed_launch->work);
return 0;
}
#define KLMIRQD_SLICE_NR_JIFFIES 1
#define KLMIRQD_SLICE_NS ((NSEC_PER_SEC / HZ) * KLMIRQD_SLICE_NR_JIFFIES)
static int become_litmus_daemon(struct task_struct* tsk)
{
int ret = 0;
struct rt_task tp = {
.period = KLMIRQD_SLICE_NS, /* dummy 1 second period */
.relative_deadline = KLMIRQD_SLICE_NS,
.exec_cost = KLMIRQD_SLICE_NS,
.phase = 0,
.cpu = task_cpu(current),
.budget_policy = NO_ENFORCEMENT,
.budget_signal_policy = NO_SIGNALS,
.cls = RT_CLASS_BEST_EFFORT
};
struct sched_param param = { .sched_priority = 0};
TRACE_CUR("Setting %s/%d as daemon thread.\n", tsk->comm, tsk->pid);
/* set task params */
tsk_rt(tsk)->task_params = tp;
tsk_rt(tsk)->is_interrupt_thread = 1;
/* inform the OS we're SCHED_LITMUS --
sched_setscheduler_nocheck() calls litmus_admit_task(). */
sched_setscheduler_nocheck(tsk, SCHED_LITMUS, ¶m);
return ret;
}
static int become_normal_daemon(struct task_struct* tsk)
{
int ret = 0;
struct sched_param param = { .sched_priority = 0};
sched_setscheduler_nocheck(tsk, SCHED_NORMAL, ¶m);
return ret;
}
static int register_klmirqd(struct task_struct* tsk)
{
int retval = 0;
unsigned long flags;
struct klmirqd_info *info = NULL;
if (!tsk_rt(tsk)->is_interrupt_thread) {
TRACE("Only proxy threads already running in Litmus may become klmirqd threads!\n");
WARN_ON(1);
retval = -1;
goto out;
}
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
if (!__klmirqd_is_ready()) {
TRACE("klmirqd is not ready! Did you forget to initialize it?\n");
WARN_ON(1);
retval = -1;
goto out_unlock;
}
/* allocate and initialize klmirqd data for the thread */
info = kmalloc(sizeof(struct klmirqd_info), GFP_KERNEL);
if (!info) {
TRACE("Failed to allocate klmirqd_info struct!\n");
retval = -1; /* todo: pick better code */
goto out_unlock;
}
memset(info, 0, sizeof(struct klmirqd_info));
info->klmirqd = tsk;
info->pending_tasklets_hi.tail = &info->pending_tasklets_hi.head;
info->pending_tasklets.tail = &info->pending_tasklets.head;
INIT_LIST_HEAD(&info->worklist);
INIT_LIST_HEAD(&info->klmirqd_reg);
raw_spin_lock_init(&info->lock);
/* now register with klmirqd */
list_add_tail(&info->klmirqd_reg, &klmirqd_state.threads);
++klmirqd_state.nr_threads;
/* update the task struct to point to klmirqd info */
tsk_rt(tsk)->klmirqd_info = info;
out_unlock:
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
out:
return retval;
}
static int unregister_klmirqd(struct task_struct* tsk)
{
int retval = 0;
unsigned long flags;
struct klmirqd_info *info = tsk_rt(tsk)->klmirqd_info;
if (!tsk_rt(tsk)->is_interrupt_thread || !info) {
TRACE("%s/%d is not a klmirqd thread!\n", tsk->comm, tsk->pid);
WARN_ON(1);
retval = -1;
goto out;
}
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
/* remove the entry in the klmirqd thread list */
list_del(&info->klmirqd_reg);
mb();
--klmirqd_state.nr_threads;
/* remove link to klmirqd info from thread */
tsk_rt(tsk)->klmirqd_info = NULL;
/* clean up memory */
kfree(info);
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
out:
return retval;
}
int proc_read_klmirqd_stats(char *page, char **start,
off_t off, int count,
int *eof, void *data)
{
unsigned long flags;
int len;
raw_spin_lock_irqsave(&klmirqd_state.lock, flags);
if (klmirqd_state.initialized) {
if (!klmirqd_state.shuttingdown) {
struct list_head *pos;
len = snprintf(page, PAGE_SIZE,
"num ready klmirqds: %d\n\n",
klmirqd_state.nr_threads);
list_for_each(pos, &klmirqd_state.threads) {
struct klmirqd_info* info = list_entry(pos, struct klmirqd_info, klmirqd_reg);
len +=
snprintf(page + len - 1, PAGE_SIZE, /* -1 to strip off \0 */
"klmirqd_thread: %s/%d\n"
"\tcurrent_owner: %s/%d\n"
"\tpending: %x\n"
"\tnum hi: %d\n"
"\tnum low: %d\n"
"\tnum work: %d\n\n",
info->klmirqd->comm, info->klmirqd->pid,
(info->current_owner != NULL) ?
info->current_owner->comm : "(null)",
(info->current_owner != NULL) ?
info->current_owner->pid : 0,
info->pending,
atomic_read(&info->num_hi_pending),
atomic_read(&info->num_low_pending),
atomic_read(&info->num_work_pending));
}
}
else {
len = snprintf(page, PAGE_SIZE, "klmirqd is shutting down\n");
}
}
else {
len = snprintf(page, PAGE_SIZE, "klmirqd is not initialized!\n");
}
raw_spin_unlock_irqrestore(&klmirqd_state.lock, flags);
return(len);
}
#if 0
static atomic_t dump_id = ATOMIC_INIT(0);
static void __dump_state(struct klmirqd_info* which, const char* caller)
{
struct tasklet_struct* list;
int id = atomic_inc_return(&dump_id);
//if(in_interrupt())
{
if(which->current_owner)
{
TRACE("(id: %d caller: %s)\n"
"klmirqd: %s/%d\n"
"current owner: %s/%d\n"
"pending: %x\n",
id, caller,
which->klmirqd->comm, which->klmirqd->pid,
which->current_owner->comm, which->current_owner->pid,
which->pending);
}
else
{
TRACE("(id: %d caller: %s)\n"
"klmirqd: %s/%d\n"
"current owner: %p\n"
"pending: %x\n",
id, caller,
which->klmirqd->comm, which->klmirqd->pid,
NULL,
which->pending);
}
list = which->pending_tasklets.head;
while(list)
{
struct tasklet_struct *t = list;
list = list->next; /* advance */
if(t->owner)
TRACE("(id: %d caller: %s) Tasklet: %x, Owner = %s/%d\n", id, caller, t, t->owner->comm, t->owner->pid);
else
TRACE("(id: %d caller: %s) Tasklet: %x, Owner = %p\n", id, caller, t, NULL);
}
}
}
static void dump_state(struct klmirqd_info* which, const char* caller)
{
unsigned long flags;
raw_spin_lock_irqsave(&which->lock, flags);
__dump_state(which, caller);
raw_spin_unlock_irqrestore(&which->lock, flags);
}
#endif
/* forward declarations */
static void ___litmus_tasklet_schedule(struct tasklet_struct *t,
struct klmirqd_info *which,
int wakeup);
static void ___litmus_tasklet_hi_schedule(struct tasklet_struct *t,
struct klmirqd_info *which,
int wakeup);
static void ___litmus_schedule_work(struct work_struct *w,
struct klmirqd_info *which,
int wakeup);
inline static u32 litirq_pending_hi_irqoff(struct klmirqd_info* which)
{
return (which->pending & LIT_TASKLET_HI);
}
inline static u32 litirq_pending_low_irqoff(struct klmirqd_info* which)
{
return (which->pending & LIT_TASKLET_LOW);
}
inline static u32 litirq_pending_work_irqoff(struct klmirqd_info* which)
{
return (which->pending & LIT_WORK);
}
inline static u32 litirq_pending_irqoff(struct klmirqd_info* which)
{
return(which->pending);
}
inline static u32 litirq_pending(struct klmirqd_info* which)
{
unsigned long flags;
u32 pending;
raw_spin_lock_irqsave(&which->lock, flags);
pending = litirq_pending_irqoff(which);
raw_spin_unlock_irqrestore(&which->lock, flags);
return pending;
};
static void wakeup_litirqd_locked(struct klmirqd_info* which)
{
/* Interrupts are disabled: no need to stop preemption */
if (which && which->klmirqd)
{
if(which->klmirqd->state != TASK_RUNNING)
{
TRACE("%s: Waking up klmirqd: %s/%d\n", __FUNCTION__,
which->klmirqd->comm, which->klmirqd->pid);
wake_up_process(which->klmirqd);
}
}
}
static void do_lit_tasklet(struct klmirqd_info* which,
struct tasklet_head* pending_tasklets)
{
unsigned long flags;
struct tasklet_struct *list;
atomic_t* count;
raw_spin_lock_irqsave(&which->lock, flags);
//__dump_state(which, "do_lit_tasklet: before steal");
/* copy out the tasklets for our private use. */
list = pending_tasklets->head;
pending_tasklets->head = NULL;
pending_tasklets->tail = &pending_tasklets->head;
/* remove pending flag */
which->pending &= (pending_tasklets == &which->pending_tasklets) ?
~LIT_TASKLET_LOW :
~LIT_TASKLET_HI;
count = (pending_tasklets == &which->pending_tasklets) ?
&which->num_low_pending:
&which->num_hi_pending;
//__dump_state(which, "do_lit_tasklet: after steal");
raw_spin_unlock_irqrestore(&which->lock, flags);
while(list)
{
struct tasklet_struct *t = list;
/* advance, lest we forget */
list = list->next;
/* execute tasklet if it has my priority and is free */
if (tasklet_trylock(t)) {
if (!atomic_read(&t->count)) {
sched_trace_tasklet_begin(t->owner);
if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
{
BUG();
}
TRACE_CUR("%s: Invoking tasklet.\n", __FUNCTION__);
t->func(t->data);
tasklet_unlock(t);
atomic_dec(count);
sched_trace_tasklet_end(t->owner, 0ul);
continue; /* process more tasklets */
}
tasklet_unlock(t);
}
TRACE_CUR("%s: Could not invoke tasklet. Requeuing.\n", __FUNCTION__);
/* couldn't process tasklet. put it back at the end of the queue. */
if(pending_tasklets == &which->pending_tasklets)
___litmus_tasklet_schedule(t, which, 0);
else
___litmus_tasklet_hi_schedule(t, which, 0);
}
}
// returns 1 if priorities need to be changed to continue processing
// pending tasklets.
static void do_litirq(struct klmirqd_info* which)
{
u32 pending;
if(in_interrupt())
{
TRACE("%s: exiting early: in interrupt context!\n", __FUNCTION__);
return;
}
if(which->klmirqd != current)
{
TRACE_CUR("%s: exiting early: thread/info mismatch! Running %s/%d but given %s/%d.\n",
__FUNCTION__, current->comm, current->pid,
which->klmirqd->comm, which->klmirqd->pid);
return;
}
if(!is_realtime(current))
{
TRACE_CUR("%s: exiting early: klmirqd is not real-time. Sched Policy = %d\n",
__FUNCTION__, current->policy);
return;
}
/* We only handle tasklets & work objects, no need for RCU triggers? */
pending = litirq_pending(which);
if(pending) {
/* extract the work to do and do it! */
if(pending & LIT_TASKLET_HI) {
TRACE_CUR("%s: Invoking HI tasklets.\n", __FUNCTION__);
do_lit_tasklet(which, &which->pending_tasklets_hi);
}
if(pending & LIT_TASKLET_LOW) {
TRACE_CUR("%s: Invoking LOW tasklets.\n", __FUNCTION__);
do_lit_tasklet(which, &which->pending_tasklets);
}
}
}
static void do_work(struct klmirqd_info* which)
{
unsigned long flags;
struct work_struct* work;
work_func_t f;
// only execute one work-queue item to yield to tasklets.
// ...is this a good idea, or should we just batch them?
raw_spin_lock_irqsave(&which->lock, flags);
if(!litirq_pending_work_irqoff(which))
{
raw_spin_unlock_irqrestore(&which->lock, flags);
goto no_work;
}
work = list_first_entry(&which->worklist, struct work_struct, entry);
list_del_init(&work->entry);
if(list_empty(&which->worklist))
{
which->pending &= ~LIT_WORK;
}
raw_spin_unlock_irqrestore(&which->lock, flags);
TRACE_CUR("%s: Invoking work object.\n", __FUNCTION__);
// do the work!
work_clear_pending(work);
f = work->func;
f(work); /* can't touch 'work' after this point,
the user may have freed it. */
atomic_dec(&which->num_work_pending);
no_work:
return;
}
/* main loop for klitsoftirqd */
static int run_klmirqd(void* callback)
{
int retval = 0;
struct klmirqd_info* info = NULL;
klmirqd_callback_t* cb = (klmirqd_callback_t*)(callback);
retval = become_litmus_daemon(current);
if (retval != 0) {
TRACE_CUR("%s: Failed to transition to rt-task.\n", __FUNCTION__);
goto failed;
}
retval = register_klmirqd(current);
if (retval != 0) {
TRACE_CUR("%s: Failed to become a klmirqd thread.\n", __FUNCTION__);
goto failed_sched_normal;
}
if (cb && cb->func) {
retval = cb->func(cb->arg);
if (retval != 0) {
TRACE_CUR("%s: klmirqd callback reported failure. retval = %d\n", __FUNCTION__, retval);
goto failed_unregister;
}
}
/* enter the interrupt handling workloop */
info = tsk_rt(current)->klmirqd_info;
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop())
{
preempt_disable();
if (!litirq_pending(info))
{
/* sleep for work */
TRACE_CUR("%s: No more tasklets or work objects. Going to sleep.\n",
__FUNCTION__);
preempt_enable_no_resched();
schedule();
if(kthread_should_stop()) /* bail out */
{
TRACE_CUR("%s:%d: Signaled to terminate.\n", __FUNCTION__, __LINE__);
continue;
}
preempt_disable();
}
__set_current_state(TASK_RUNNING);
while (litirq_pending(info))
{
preempt_enable_no_resched();
if(kthread_should_stop())
{
TRACE_CUR("%s:%d: Signaled to terminate.\n", __FUNCTION__, __LINE__);
break;
}
preempt_disable();
/* Double check that there's still pending work and the owner hasn't
* changed. Pending items may have been flushed while we were sleeping.
*/
if(litirq_pending(info))
{
TRACE_CUR("%s: Executing tasklets and/or work objects.\n",
__FUNCTION__);
do_litirq(info);
preempt_enable_no_resched();
// work objects are preemptible.
do_work(info);
}
else
{
TRACE_CUR("%s: Pending work was flushed!\n", __FUNCTION__);
preempt_enable_no_resched();
}
cond_resched();
preempt_disable();
}
preempt_enable();
set_current_state(TASK_INTERRUPTIBLE);
}
__set_current_state(TASK_RUNNING);
failed_unregister:
/* remove our registration from klmirqd */
unregister_klmirqd(current);
failed_sched_normal:
become_normal_daemon(current);
failed:
return retval;
}
void flush_pending(struct task_struct* tsk)
{
unsigned long flags;
struct tasklet_struct *list;
u32 work_flushed = 0;
struct klmirqd_info *which;
if (!tsk_rt(tsk)->is_interrupt_thread) {
TRACE("%s/%d is not a proxy thread\n", tsk->comm, tsk->pid);
WARN_ON(1);
return;
}
which = tsk_rt(tsk)->klmirqd_info;
if (!which) {
TRACE("%s/%d is not a klmirqd thread!\n", tsk->comm, tsk->pid);
WARN_ON(1);
return;
}
raw_spin_lock_irqsave(&which->lock, flags);
//__dump_state(which, "flush_pending: before");
// flush hi tasklets.
if(litirq_pending_hi_irqoff(which))
{
which->pending &= ~LIT_TASKLET_HI;
list = which->pending_tasklets_hi.head;
which->pending_tasklets_hi.head = NULL;
which->pending_tasklets_hi.tail = &which->pending_tasklets_hi.head;
TRACE("%s: Handing HI tasklets back to Linux.\n", __FUNCTION__);
while(list)
{
struct tasklet_struct *t = list;
list = list->next;
if(unlikely(!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state)))
{
BUG();
}
work_flushed |= LIT_TASKLET_HI;
t->owner = NULL;
// WTF?
if(!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
{
atomic_dec(&which->num_hi_pending);
___tasklet_hi_schedule(t);
}
else
{
TRACE("%s: dropped hi tasklet??\n", __FUNCTION__);
BUG();
}
}
}
// flush low tasklets.
if(litirq_pending_low_irqoff(which))
{
which->pending &= ~LIT_TASKLET_LOW;
list = which->pending_tasklets.head;
which->pending_tasklets.head = NULL;
which->pending_tasklets.tail = &which->pending_tasklets.head;
TRACE("%s: Handing LOW tasklets back to Linux.\n", __FUNCTION__);
while(list)
{
struct tasklet_struct *t = list;
list = list->next;
if(unlikely(!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state)))
{
BUG();
}
work_flushed |= LIT_TASKLET_LOW;
t->owner = NULL;
// sched_trace_tasklet_end(owner, 1ul);
if(!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
{
atomic_dec(&which->num_low_pending);
___tasklet_schedule(t);
}
else
{
TRACE("%s: dropped tasklet??\n", __FUNCTION__);
BUG();
}
}
}
// flush work objects
if(litirq_pending_work_irqoff(which))
{
which->pending &= ~LIT_WORK;
TRACE("%s: Handing work objects back to Linux.\n", __FUNCTION__);
while(!list_empty(&which->worklist))
{
struct work_struct* work =
list_first_entry(&which->worklist, struct work_struct, entry);
list_del_init(&work->entry);
work_flushed |= LIT_WORK;
atomic_dec(&which->num_work_pending);
work->owner = NULL;
// sched_trace_work_end(owner, current, 1ul);
__schedule_work(work);
}
}
//__dump_state(which, "flush_pending: after (before reeval prio)");
mb(); /* commit changes to pending flags */
raw_spin_unlock_irqrestore(&which->lock, flags);
}
static void ___litmus_tasklet_schedule(struct tasklet_struct *t,
struct klmirqd_info *which,
int wakeup)
{
unsigned long flags;
u32 old_pending;
t->next = NULL;
raw_spin_lock_irqsave(&which->lock, flags);
//__dump_state(which, "___litmus_tasklet_schedule: before queuing");
*(which->pending_tasklets.tail) = t;
which->pending_tasklets.tail = &t->next;
old_pending = which->pending;
which->pending |= LIT_TASKLET_LOW;
atomic_inc(&which->num_low_pending);
mb();
if(!old_pending && wakeup)
{
wakeup_litirqd_locked(which); /* wake up the klmirqd */
}
//__dump_state(which, "___litmus_tasklet_schedule: after queuing");
raw_spin_unlock_irqrestore(&which->lock, flags);
}
int __litmus_tasklet_schedule(struct tasklet_struct *t, struct task_struct* klmirqd_thread)
{
int ret = 0; /* assume failure */
struct klmirqd_info* info;
if (unlikely(!is_realtime(klmirqd_thread) ||
!tsk_rt(klmirqd_thread)->is_interrupt_thread ||
!tsk_rt(klmirqd_thread)->klmirqd_info)) {
TRACE("%s: %s/%d can't handle tasklets\n", klmirqd_thread->comm, klmirqd_thread->pid);
return ret;
}
info = tsk_rt(klmirqd_thread)->klmirqd_info;
if (likely(!info->terminating)) {
ret = 1;
___litmus_tasklet_schedule(t, info, 1);
}
else {
TRACE("%s: Tasklet rejected because %s/%d is terminating\n", klmirqd_thread->comm, klmirqd_thread->pid);
}
return(ret);
}
EXPORT_SYMBOL(__litmus_tasklet_schedule);
static void ___litmus_tasklet_hi_schedule(struct tasklet_struct *t,
struct klmirqd_info *which,
int wakeup)
{
unsigned long flags;
u32 old_pending;
t->next = NULL;
raw_spin_lock_irqsave(&which->lock, flags);
*(which->pending_tasklets_hi.tail) = t;
which->pending_tasklets_hi.tail = &t->next;
old_pending = which->pending;
which->pending |= LIT_TASKLET_HI;
atomic_inc(&which->num_hi_pending);
mb();
if(!old_pending && wakeup)
{
wakeup_litirqd_locked(which); /* wake up the klmirqd */
}
raw_spin_unlock_irqrestore(&which->lock, flags);
}
int __litmus_tasklet_hi_schedule(struct tasklet_struct *t, struct task_struct* klmirqd_thread)
{
int ret = 0; /* assume failure */
struct klmirqd_info* info;
if (unlikely(!is_realtime(klmirqd_thread) ||
!tsk_rt(klmirqd_thread)->is_interrupt_thread ||
!tsk_rt(klmirqd_thread)->klmirqd_info)) {
TRACE("%s: %s/%d can't handle tasklets\n", klmirqd_thread->comm, klmirqd_thread->pid);
return ret;
}
info = tsk_rt(klmirqd_thread)->klmirqd_info;
if (likely(!info->terminating)) {
ret = 1;
___litmus_tasklet_hi_schedule(t, info, 1);
}
else {
TRACE("%s: Tasklet rejected because %s/%d is terminating\n", klmirqd_thread->comm, klmirqd_thread->pid);
}
return(ret);
}
EXPORT_SYMBOL(__litmus_tasklet_hi_schedule);
int __litmus_tasklet_hi_schedule_first(struct tasklet_struct *t, struct task_struct* klmirqd_thread)
{
int ret = 0; /* assume failure */
u32 old_pending;
struct klmirqd_info* info;
BUG_ON(!irqs_disabled());
if (unlikely(!is_realtime(klmirqd_thread) ||
!tsk_rt(klmirqd_thread)->is_interrupt_thread ||
!tsk_rt(klmirqd_thread)->klmirqd_info)) {
TRACE("%s: %s/%d can't handle tasklets\n", klmirqd_thread->comm, klmirqd_thread->pid);
return ret;
}
info = tsk_rt(klmirqd_thread)->klmirqd_info;
if (likely(!info->terminating)) {
raw_spin_lock(&info->lock);
ret = 1; // success!
t->next = info->pending_tasklets_hi.head;
info->pending_tasklets_hi.head = t;
old_pending = info->pending;
info->pending |= LIT_TASKLET_HI;
atomic_inc(&info->num_hi_pending);
mb();
if(!old_pending) {
wakeup_litirqd_locked(info); /* wake up the klmirqd */
}
raw_spin_unlock(&info->lock);
}
else {
TRACE("%s: Tasklet rejected because %s/%d is terminating\n", klmirqd_thread->comm, klmirqd_thread->pid);
}
return(ret);
}
EXPORT_SYMBOL(__litmus_tasklet_hi_schedule_first);
static void ___litmus_schedule_work(struct work_struct *w,
struct klmirqd_info *which,
int wakeup)
{
unsigned long flags;
u32 old_pending;
raw_spin_lock_irqsave(&which->lock, flags);
work_pending(w);
list_add_tail(&w->entry, &which->worklist);
old_pending = which->pending;
which->pending |= LIT_WORK;
atomic_inc(&which->num_work_pending);
mb();
if(!old_pending && wakeup)
{
wakeup_litirqd_locked(which); /* wakeup the klmirqd */
}
raw_spin_unlock_irqrestore(&which->lock, flags);
}
int __litmus_schedule_work(struct work_struct *w, struct task_struct* klmirqd_thread)
{
int ret = 1; /* assume success */
struct klmirqd_info* info;
if (unlikely(!is_realtime(klmirqd_thread) ||
!tsk_rt(klmirqd_thread)->is_interrupt_thread ||
!tsk_rt(klmirqd_thread)->klmirqd_info)) {
TRACE("%s: %s/%d can't handle work items\n", klmirqd_thread->comm, klmirqd_thread->pid);
return ret;
}
info = tsk_rt(klmirqd_thread)->klmirqd_info;
if (likely(!info->terminating)) {
___litmus_schedule_work(w, info, 1);
}
else {
TRACE("%s: Work rejected because %s/%d is terminating\n", klmirqd_thread->comm, klmirqd_thread->pid);
ret = 0;
}
return(ret);
}
EXPORT_SYMBOL(__litmus_schedule_work);
|