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
|
/* much copied shamelessly from Linux's softirq.c
as well as PREEMPT_RT's softirq.c */
#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 <litmus/litmus.h>
#include <litmus/jobs.h>
#include <litmus/sched_plugin.h>
#include <litmus/litmus_softirq.h>
/* counts number of daemons ready to handle litmus irqs. */
static atomic_t num_ready_klitirqds = ATOMIC_INIT(0);
enum pending_flags
{
LIT_TASKLET_LOW = 0x1,
LIT_TASKLET_HI = LIT_TASKLET_LOW<<1
};
/* only support tasklet processing for now. */
struct tasklet_head
{
struct tasklet_struct *head;
struct tasklet_struct **tail;
};
struct klitirqd_info
{
struct task_struct* klitirqd;
raw_spinlock_t lock;
u32 pending;
struct tasklet_head pending_tasklets;
struct tasklet_head pending_tasklets_hi;
};
/* one list for each klitirqd */
static struct klitirqd_info klitirqds[NR_LITMUS_SOFTIRQD];
inline unsigned int klitirqd_id(struct task_struct* tsk)
{
int i;
for(i = 0; i < NR_LITMUS_SOFTIRQD; ++i)
{
if(klitirqds[i].klitirqd == tsk)
{
return i;
}
}
BUG();
return 0;
}
inline static u32 litirq_pending_hi_irqoff(struct klitirqd_info* which)
{
return (which->pending & LIT_TASKLET_HI);
};
inline static u32 litirq_pending_low_irqoff(struct klitirqd_info* which)
{
return (which->pending & LIT_TASKLET_LOW);
};
inline static u32 litirq_pending_irqoff(struct klitirqd_info* which)
{
return(which->pending);
};
inline static u32 litirq_pending(struct klitirqd_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 int needs_prio_change(struct klitirqd_info* which,
struct tasklet_head* pending_tasklets)
{
unsigned long flags;
int ret = 0;
raw_spin_lock_irqsave(&which->lock, flags);
if((pending_tasklets == &which->pending_tasklets_hi) && litirq_pending_hi_irqoff(which))
{
if(which->pending_tasklets_hi.head->owner != tsk_rt(which->klitirqd)->inh_task)
{
ret = 1;
}
}
else if((pending_tasklets == &which->pending_tasklets) && litirq_pending_irqoff(which))
{
if(which->pending_tasklets.head->owner != tsk_rt(which->klitirqd)->inh_task)
{
ret = 1;
}
}
raw_spin_unlock_irqrestore(&which->lock, flags);
TRACE_TASK(which->klitirqd, "priority change needed: %d\n", ret);
return ret;
}
static void __reeval_prio(struct klitirqd_info* which)
{
u32 pending = 0;
struct task_struct* tsk = which->klitirqd;
struct task_struct* new_prio = tsk_rt(tsk)->inh_task;
if(litirq_pending_irqoff(which))
{
pending = 1;
if(which->pending_tasklets.head->owner != tsk_rt(tsk)->inh_task)
{
new_prio = which->pending_tasklets.head->owner;
}
}
if(litirq_pending_hi_irqoff(which))
{
pending = 1;
if(which->pending_tasklets_hi.head->owner != tsk_rt(tsk)->inh_task)
{
new_prio = which->pending_tasklets_hi.head->owner;
}
}
if(!pending)
{
new_prio = NULL;
}
if(new_prio != tsk_rt(tsk)->inh_task)
{
if(new_prio != NULL)
{
if(!in_interrupt())
{
TRACE_CUR("%s: Priority change: %s/%d to %s/%d\n", __FUNCTION__,
((tsk_rt(tsk)->inh_task) ? tsk_rt(tsk)->inh_task : tsk)->comm,
((tsk_rt(tsk)->inh_task) ? tsk_rt(tsk)->inh_task : tsk)->pid,
new_prio->comm, new_prio->pid);
}
else
{
TRACE("%s: Priority change: %s/%d to %s/%d\n", __FUNCTION__,
((tsk_rt(tsk)->inh_task) ? tsk_rt(tsk)->inh_task : tsk)->comm,
((tsk_rt(tsk)->inh_task) ? tsk_rt(tsk)->inh_task : tsk)->pid,
new_prio->comm, new_prio->pid);
}
litmus->set_prio_inh(tsk, new_prio);
}
else
{
if(likely(!in_interrupt()))
{
TRACE_CUR("%s: Priority change: %s/%d to NULL (reverting)\n",
__FUNCTION__, tsk->comm, tsk->pid);
}
else
{
// is this a bug?
TRACE("%s: Priority change: %s/%d to NULL (reverting)\n",
__FUNCTION__, tsk->comm, tsk->pid);
}
litmus->clear_prio_inh(tsk);
}
}
}
static void reeval_prio(struct klitirqd_info* which)
{
unsigned long flags;
raw_spin_lock_irqsave(&which->lock, flags);
__reeval_prio(which);
raw_spin_unlock_irqrestore(&which->lock, flags);
}
static void wakeup_litirqd_locked(struct klitirqd_info* which)
{
/* Interrupts are disabled: no need to stop preemption */
if (which && which->klitirqd && which->klitirqd->state != TASK_RUNNING)
{
__reeval_prio(which); /* configure the proper priority */
TRACE("%s: Waking up klitirqd: %s/%d\n", __FUNCTION__,
which->klitirqd->comm, which->klitirqd->pid);
wake_up_process(which->klitirqd);
}
}
#if 0
static void wakeup_litirqd(struct klitirqd_info* which)
{
/* Interrupts are disabled: no need to stop preemption */
if (which && which->klitirqd && which->klitirqd->state != TASK_RUNNING)
{
reeval_prio(which); /* configure the proper priority */
TRACE("%s: Waking up klitirqd: %s/%d\n", __FUNCTION__,
which->klitirqd->comm, which->klitirqd->pid);
wake_up_process(which->klitirqd);
}
}
void trigger_litirqs(struct task_struct* tsk)
{
struct klitirqd_info* which = &klitirqds[klitirqd_id(tsk)];
TRACE("%s: entering, triggering %s/%d\n", __FUNCTION__, tsk->comm, tsk->pid);
while (litirq_pending(which))
{
wakeup_litirqd(which);
}
TRACE("%s: exiting, done triggering %s/%d\n", __FUNCTION__, tsk->comm, tsk->pid);
}
#endif
/* forward declarations */
static void ___litmus_tasklet_schedule(struct tasklet_struct *t,
struct klitirqd_info *which,
int wakeup);
static void ___litmus_tasklet_hi_schedule(struct tasklet_struct *t,
struct klitirqd_info *which,
int wakeup);
static void do_lit_tasklet(struct klitirqd_info* which,
struct tasklet_head* pending_tasklets)
{
unsigned long flags;
struct tasklet_struct *list;
raw_spin_lock_irqsave(&which->lock, flags);
/* copy out the tasklets for our private use. */
list = pending_tasklets->head;
pending_tasklets->head = NULL;
pending_tasklets->tail = &pending_tasklets->head;
which->pending &= (pending_tasklets == &which->pending_tasklets) ?
~LIT_TASKLET_LOW :
~LIT_TASKLET_HI;
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 ((t->owner == tsk_rt(current)->inh_task) && tasklet_trylock(t)) {
if (!atomic_read(&t->count)) {
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);
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);
}
}
static void do_litirq(struct klitirqd_info* which)
{
u32 pending;
int resched = 0;
if(in_interrupt())
{
TRACE("%s: exiting early: in interrupt context!\n", __FUNCTION__);
return;
}
if(which->klitirqd != current)
{
TRACE_CUR("%s: exiting early: thread/info mismatch! Running %s/%d but given %s/%d.\n",
__FUNCTION__, current->comm, current->pid,
which->klitirqd->comm, which->klitirqd->pid);
return;
}
if(!is_realtime(current))
{
TRACE_CUR("%s: exiting early: klitirqd is not real-time. Sched Policy = %d\n",
__FUNCTION__, current->policy);
return;
}
/* since we only handle tasklets, 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);
resched = needs_prio_change(which, &which->pending_tasklets_hi);
if(resched)
{
TRACE_CUR("%s: HI tasklets of another priority remain. Skipping LOW tasklets.\n", __FUNCTION__);
}
}
if(!resched && (pending & LIT_TASKLET_LOW))
{
TRACE_CUR("%s: Invoking LOW tasklets.\n", __FUNCTION__);
do_lit_tasklet(which, &which->pending_tasklets);
resched = needs_prio_change(which, &which->pending_tasklets);
}
}
}
int set_litmus_daemon_sched(struct klitirqd_info* which)
{
/* set up a daemon job that will never complete.
it should only ever run on behalf of another
real-time task.
TODO: Transition to a new job whenever a
new tasklet is handled */
int ret = 0;
struct task_struct* tsk = which->klitirqd;
struct rt_task tp = {
.exec_cost = 0,
.period = 1000000000, /* dummy 1 second period */
.phase = 0,
.cpu = 0,
.budget_policy = NO_ENFORCEMENT,
.cls = RT_CLASS_BEST_EFFORT
};
struct sched_param param = { .sched_priority = 0};
/* test task params and mark as proxy thread. */
tsk->rt_param.task_params = tp;
tsk->rt_param.is_proxy_thread = 1;
/* litmus_admit_task */
ret = litmus_admit_task(tsk);
/* inform the OS we're SCHED_LITMUS --
must happen after litmus_admit_task() */
sched_setscheduler_nocheck(current, SCHED_LITMUS, ¶m);
return ret;
}
/* TODO: WHAT'S THE DEAL WITH ENABLE/DISABLE BOTTOM HALVES
IN ORIGINAL softirq.c? DO WE NEED IT HERE TOO? */
/* main loop for klitsoftirqd */
static int run_klitirqd(void* dummy)
{
struct klitirqd_info* which = &klitirqds[klitirqd_id(current)];
int rt_status = set_litmus_daemon_sched(which);
if(rt_status != 0)
{
TRACE_CUR("%s: Failed to transition to rt-task.\n", __FUNCTION__);
goto rt_failed;
}
atomic_inc(&num_ready_klitirqds);
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop()) {
preempt_disable();
if (!litirq_pending(which)) {
/* sleep for work */
TRACE_CUR("%s: No more tasklets. Going to sleep.\n", __FUNCTION__);
preempt_enable_no_resched();
schedule();
preempt_disable();
}
__set_current_state(TASK_RUNNING);
while (litirq_pending(which)) {
TRACE_CUR("%s: Executing tasklets.\n", __FUNCTION__);
do_litirq(which);
TRACE_CUR("%s: Setting up next priority.\n", __FUNCTION__);
reeval_prio(which); /* check if we need to change priority here */
preempt_enable_no_resched();
cond_resched();
preempt_disable();
}
preempt_enable();
set_current_state(TASK_INTERRUPTIBLE);
}
__set_current_state(TASK_RUNNING);
atomic_dec(&num_ready_klitirqds);
rt_failed:
litmus_exit_task(current);
return rt_status;
}
/* executed by a kworker from workqueues */
static void launch_klitirqd(struct work_struct *unused)
{
int i;
TRACE("%s: Creating %d klitirqds\n", __FUNCTION__, NR_LITMUS_SOFTIRQD);
/* create the daemon threads */
for(i = 0; i < NR_LITMUS_SOFTIRQD; ++i)
{
klitirqds[i].klitirqd =
kthread_create(run_klitirqd, NULL, "klitirqd_th%d", i);
}
TRACE("%s: Launching %d klitirqds\n", __FUNCTION__, NR_LITMUS_SOFTIRQD);
/* unleash the daemons */
for(i = 0; i < NR_LITMUS_SOFTIRQD; ++i)
{
wake_up_process(klitirqds[i].klitirqd);
}
kfree(unused);
}
void spawn_klitirqd(void)
{
int i;
struct work_struct* delayed_launch;
if(atomic_read(&num_ready_klitirqds) != 0)
{
TRACE("%s: At least one klitirqd is already running! Need to call kill_klitirqd()?\n");
return;
}
/* init the tasklet queues */
for(i = 0; i < NR_LITMUS_SOFTIRQD; ++i)
{
klitirqds[i].pending = 0;
klitirqds[i].pending_tasklets.head = NULL;
klitirqds[i].pending_tasklets.tail = &klitirqds[i].pending_tasklets.head;
klitirqds[i].pending_tasklets_hi.head = NULL;
klitirqds[i].pending_tasklets_hi.tail = &klitirqds[i].pending_tasklets_hi.head;
raw_spin_lock_init(&klitirqds[i].lock);
}
/* wait to flush the initializations to memory since other threads
will access it. */
mb();
/* tell a work queue to launch the threads. we can't make scheduling
calls since we're in an atomic state. */
TRACE("%s: Setting callback up to launch klitirqds\n", __FUNCTION__);
delayed_launch = kmalloc(sizeof(struct work_struct), GFP_ATOMIC);
INIT_WORK(delayed_launch, launch_klitirqd);
schedule_work(delayed_launch);
}
void kill_klitirqd(void)
{
int i;
TRACE("%s: Killing %d klitirqds\n", __FUNCTION__, NR_LITMUS_SOFTIRQD);
for(i = 0; i < NR_LITMUS_SOFTIRQD; ++i)
{
kthread_stop(klitirqds[i].klitirqd);
}
/* TODO: Put pending tasklets SOMEWHERE-- back to the OS? */
}
int klitirqd_is_ready(void)
{
return(atomic_read(&num_ready_klitirqds) == NR_LITMUS_SOFTIRQD);
}
int klitirqd_is_dead(void)
{
return(atomic_read(&num_ready_klitirqds) == 0);
}
static void ___litmus_tasklet_schedule(struct tasklet_struct *t,
struct klitirqd_info *which,
int wakeup)
{
unsigned long flags;
t->next = NULL;
raw_spin_lock_irqsave(&which->lock, flags);
*(which->pending_tasklets.tail) = t;
which->pending_tasklets.tail = &t->next;
which->pending |= LIT_TASKLET_LOW;
if(wakeup)
{
wakeup_litirqd_locked(which); /* wake up the klitirqd */
}
raw_spin_unlock_irqrestore(&which->lock, flags);
}
void __litmus_tasklet_schedule(struct tasklet_struct *t, unsigned int k_id)
{
if(unlikely((t->owner == NULL) || !is_realtime(t->owner)))
{
TRACE("%s: No owner associated with this tasklet!\n", __FUNCTION__);
BUG();
}
if(unlikely(k_id >= NR_LITMUS_SOFTIRQD))
{
TRACE("%s: No klitirqd_th%d!\n", __FUNCTION__, k_id);
BUG();
}
___litmus_tasklet_schedule(t, &klitirqds[k_id], 1);
}
EXPORT_SYMBOL(__litmus_tasklet_schedule);
static void ___litmus_tasklet_hi_schedule(struct tasklet_struct *t,
struct klitirqd_info *which,
int wakeup)
{
unsigned long flags;
t->next = NULL;
raw_spin_lock_irqsave(&which->lock, flags);
*(which->pending_tasklets_hi.tail) = t;
which->pending_tasklets_hi.tail = &t->next;
which->pending |= LIT_TASKLET_HI;
if(wakeup)
{
wakeup_litirqd_locked(which); /* wake up the klitirqd */
}
raw_spin_unlock_irqrestore(&which->lock, flags);
}
void __litmus_tasklet_hi_schedule(struct tasklet_struct *t, unsigned int k_id)
{
if(unlikely((t->owner == NULL) || !is_realtime(t->owner)))
{
TRACE("%s: No owner associated with this tasklet!\n", __FUNCTION__);
BUG();
}
if(unlikely(k_id >= NR_LITMUS_SOFTIRQD))
{
TRACE("%s: No klitirqd_th%d!\n", __FUNCTION__, k_id);
BUG();
}
___litmus_tasklet_hi_schedule(t, &klitirqds[k_id], 1);
}
EXPORT_SYMBOL(__litmus_tasklet_hi_schedule);
void __litmus_tasklet_hi_schedule_first(struct tasklet_struct *t, unsigned int k_id)
{
BUG_ON(!irqs_disabled());
if(unlikely((t->owner == NULL) || !is_realtime(t->owner)))
{
TRACE("%s: No owner associated with this tasklet!\n", __FUNCTION__);
BUG();
}
if(unlikely(k_id >= NR_LITMUS_SOFTIRQD))
{
TRACE("%s: No klitirqd_th%d!\n", __FUNCTION__, k_id);
BUG();
}
raw_spin_lock(&klitirqds[k_id].lock);
t->next = klitirqds[k_id].pending_tasklets_hi.head;
klitirqds[k_id].pending_tasklets_hi.head = t;
klitirqds[k_id].pending |= LIT_TASKLET_HI;
wakeup_litirqd_locked(&klitirqds[k_id]); /* wake up the klitirqd */
raw_spin_unlock(&klitirqds[k_id].lock);
}
EXPORT_SYMBOL(__litmus_tasklet_hi_schedule_first);
|