/Documentation/crypto/

head>
aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/sched_gq_edf.c
blob: 05862fe5963b7de919dd7a29e7a732e567b2dbe4 (plain) (blame)
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
/* A quantum-based implementation of G-EDF.
 *
 * Based on GSN-EDF.
 */

#include <linux/spinlock.h>
#include <linux/percpu.h>
#include <linux/sched.h>

#include <litmus/litmus.h>
#include <litmus/jobs.h>
#include <litmus/sched_plugin.h>
#include <litmus/edf_common.h>
#include <litmus/sched_trace.h>

#include <litmus/heap.h>

#include <linux/module.h>

/* cpu_state_t - maintain the linked and scheduled state
 */
typedef struct  {
	int 			cpu;
	struct task_struct*	linked;		/* only RT tasks */
	struct task_struct*	scheduled;	/* only RT tasks */
	struct task_struct*	absentee;	/* blocked quantum owner */
	struct heap_node*	hn;
} cpu_state_t;
DEFINE_PER_CPU(cpu_state_t, gq_cpu_entries);

cpu_state_t* gq_cpus[NR_CPUS];


#define NO_CPU 0xffffffff

/* the cpus queue themselves according to priority in here */
static struct heap_node gq_heap_node[NR_CPUS];
static struct heap      gq_cpu_heap;
/* jobs to be merged at the beginning of the next quantum */
static struct heap      gq_released_heap;


static rt_domain_t gqedf;
#define gq_lock (gqedf.ready_lock)

DEFINE_SPINLOCK(gq_release_lock);

static void preempt(cpu_state_t *entry)
{
	if (smp_processor_id() == entry->cpu)
		set_tsk_need_resched(current);
	else
		smp_send_reschedule(entry->cpu);
}

static int cpu_lower_prio(struct heap_node *_a, struct heap_node *_b)
{
	cpu_state_t *a, *b;
	a = _a->value;
	b = _b->value;
	/* Note that a and b are inverted: we want the lowest-priority CPU at
	 * the top of the heap.
	 */
	return edf_higher_prio(b->linked, a->linked);
}

/* update_cpu_position - Move the cpu entry to the correct place to maintain
 *                       order in the cpu queue. Caller must hold gqedf lock.
 */
static void update_cpu_position(cpu_state_t *entry)
{
	if (likely(heap_node_in_heap(entry->hn)))
		heap_delete(cpu_lower_prio, &gq_cpu_heap, entry->hn);
	heap_insert(cpu_lower_prio, &gq_cpu_heap, entry->hn);
}

/* caller must hold gqedf lock */
static cpu_state_t* lowest_prio_cpu(void)
{
	struct heap_node* hn;
	hn = heap_peek(cpu_lower_prio, &gq_cpu_heap);
	return hn->value; //hn ? hn->value : NULL;
}

/* link_task_to_cpu - Update the link of a CPU.
 *                    Handles the case where the to-be-linked task is already
 *                    scheduled on a different CPU.
 */
static noinline void link_task_to_cpu(struct task_struct* linked,
				      cpu_state_t *entry)
{
	cpu_state_t *sched;
	struct task_struct* tmp;
	int on_cpu;

	BUG_ON(linked && !is_realtime(linked));
	/* don't relink tasks that are already linked */
	BUG_ON(linked && tsk_rt(linked)->linked_on != NO_CPU);

	/* Currently linked task is set to be unlinked. */
	if (entry->linked) {
		entry->linked->rt_param.linked_on = NO_CPU;
	}

	/* Link new task to CPU. */
	if (linked) {
		set_rt_flags(linked, RT_F_RUNNING);
		/* handle task is already scheduled somewhere! */
		on_cpu = linked->rt_param.scheduled_on;
		if (on_cpu != NO_CPU) {
			sched = &per_cpu(gq_cpu_entries, on_cpu);
			/* this should only happen if not linked already */
			BUG_ON(sched->linked == linked);

			/* If we are already scheduled on the CPU to which we
			 * wanted to link, we don't need to do the swap --
			 * we just link ourselves to the CPU and depend on
			 * the caller to get things right.
			 */
			if (entry != sched) {
				TRACE_TASK(linked,
					   "already scheduled on %d, updating link.\n",
					   sched->cpu);
				tmp = sched->linked;
				linked->rt_param.linked_on = sched->cpu;
				sched->linked = linked;
				update_cpu_position(sched);
				linked = tmp;
			}
		}
		if (linked) /* might be NULL due to swap */
			linked->rt_param.linked_on = entry->cpu;
	}
	entry->linked = linked;
	if (linked)
		TRACE_TASK(linked, "linked to %d.\n", entry->cpu);
	else
		TRACE("NULL linked to %d.\n", entry->cpu);
	update_cpu_position(entry);
}

/* unlink - Make sure a task is not linked any longer to an entry
 *          where it was linked before. Must hold gq_lock.
 */
static noinline void unlink(struct task_struct* t)
{
    	cpu_state_t *entry;

	if (unlikely(!t)) {
		TRACE_BUG_ON(!t);
		return;
	}

	if (t->rt_param.linked_on != NO_CPU) {
		/* unlink */
		entry = &per_cpu(gq_cpu_entries, t->rt_param.linked_on);
		t->rt_param.linked_on = NO_CPU;
		link_task_to_cpu(NULL, entry);
	} else if (is_queued(t)) {
		/* This is an interesting situation: t is scheduled,
		 * but was just recently unlinked.  It cannot be
		 * linked anywhere else (because then it would have
		 * been relinked to this CPU), thus it must be in some
		 * queue. We must remove it from the list in this
		 * case.
		 */
		TRACE_TASK(t, "unlink() -> remove()\n");
		remove(&gqedf, t);
	}
}


/* requeue - Put an unlinked task into gsn-edf domain.
 *           Caller must hold gq_lock.
 */
static noinline void requeue(struct task_struct* task)
{
	BUG_ON(!task);
	/* sanity check before insertion */
	BUG_ON(is_queued(task));

	if (is_released(task, litmus_clock()))
		__add_ready(&gqedf, task);
	else
		/* it has got to wait */
		add_release(&gqedf, task);
}

/* check for any necessary preemptions */
static void link_highest_priority_jobs(void)
{
	struct task_struct *task;
	cpu_state_t* last;

	for(last = lowest_prio_cpu();
//	    last &&
	    edf_preemption_needed(&gqedf, last->linked);
	    last = lowest_prio_cpu()) {
		TRACE("last cpu:%d linked:%s/%d preempt:%d\n",
		      last->cpu,
		      last->linked ? last->linked->comm : "---",
		      last->linked ? last->linked->pid  : 0,
		      edf_preemption_needed(&gqedf, last->linked));
		/* preemption necessary */
		task = __take_ready(&gqedf);
		TRACE("attempting to link task %d to %d at %llu\n",
		      task->pid, last->cpu, litmus_clock());
		if (last->linked) {
			TRACE_TASK(last->linked, "requeued.\n");
			requeue(last->linked);
		}
		link_task_to_cpu(task, last);
	}
}

/* caller holds gq_lock */
static void job_completion(struct task_struct *t, int forced)
{

	sched_trace_task_completion(t, forced);

	TRACE_TASK(t, "job_completion(forced=%d), state:%d\n", forced,
		   t->state);

	/* prepare for next period */
	prepare_for_next_period(t);
	if (is_released(t, litmus_clock()))
		sched_trace_task_release(t);
	/* unlink */
	unlink(t);
	/* requeue
	 * But don't requeue a blocking task. */
	if (is_running(t))
		requeue(t);
	else
		TRACE_TASK(t, "job_completion(): not requeued, is not running. "
			   "state:%d\n", t->state);
}


static void gq_add_released_queue(struct task_struct *t)
{
	spin_lock(&gq_release_lock);
	heap_insert(edf_ready_order, &gq_released_heap, tsk_rt(t)->heap_node);
	spin_unlock(&gq_release_lock);
}

/* caller holds gq_lock, irqs are disabled */
static void merge_released_queue(void)
{
	struct heap_node* hn;
	struct task_struct* t;

	spin_lock(&gq_release_lock);

//	__merge_ready(&gqedf, &gq_released_heap);
	while ((hn = heap_take(edf_ready_order, &gq_released_heap))) {
		t = (struct task_struct*) hn->value;
		TRACE_TASK(t, "merged into ready queue (is_released:%d)\n",
			   is_released(t, litmus_clock()));
		__add_ready(&gqedf, t);
	}

	spin_unlock(&gq_release_lock);
}

/* gq_tick - this function is called for every local timer
 *                         interrupt.
 *
 *                   checks whether the current task has expired and checks
 *                   whether we need to preempt it if it has not expired
 */
static void gq_tick(struct task_struct* t)
{
	unsigned long flags;
	cpu_state_t* entry;

	spin_lock_irqsave(&gq_lock, flags);
	entry =  &__get_cpu_var(gq_cpu_entries);
	entry->absentee = NULL;

	merge_released_queue();
	/* update linked if required */
	link_highest_priority_jobs();

	if (entry->linked != entry->scheduled ||
	    (is_realtime(t) && budget_exhausted(t)))
		/* let's reschedule */
		set_tsk_need_resched(t);

	spin_unlock_irqrestore(&gq_lock, flags);
}

static struct task_struct* gq_schedule(struct task_struct * prev)
{
	cpu_state_t* entry = &__get_cpu_var(gq_cpu_entries);
	int sleep, preempt, exists, blocks, out_of_time;
	struct task_struct* next = NULL;

	spin_lock(&gq_lock);

	/* sanity checking */
	BUG_ON(entry->scheduled && entry->scheduled != prev);
	BUG_ON(entry->scheduled && !is_realtime(prev));
	BUG_ON(is_realtime(prev) && !entry->scheduled);
	BUG_ON(entry->scheduled && tsk_rt(entry->scheduled)->scheduled_on != entry->cpu);
	BUG_ON(entry->scheduled && tsk_rt(entry->scheduled)->scheduled_on != entry->cpu);

	/* Determine state */
	exists      = entry->scheduled != NULL;
	blocks      = exists && !is_running(entry->scheduled);
	out_of_time = exists && budget_exhausted(entry->scheduled);
	sleep	    = exists && get_rt_flags(entry->scheduled) == RT_F_SLEEP;
	preempt     = entry->scheduled != entry->linked;

	BUG_ON(blocks && sleep);

	TRACE_TASK(prev, "invoked gq_schedule.\n");

	if (exists)
		TRACE_TASK(prev,
			   "blocks:%d sleep:%d preempt:%d "
			   "state:%d sig:%d out_of_time:%d\n",
			   blocks, sleep, preempt,
			   prev->state, signal_pending(prev),
			   out_of_time);
	if (entry->linked && preempt)
		TRACE_TASK(prev, "will be preempted by %s/%d\n",
			   entry->linked->comm, entry->linked->pid);


	if (blocks) {
		/* Record task identity so that the task
		 * can be rescheduled when it resumes,
		 * but only do so when prev has not been
		 * preempted anyway.
		 */
		if (!preempt) {
			entry->absentee = prev;
			tsk_rt(prev)->last_cpu = entry->cpu;
		}
		unlink(entry->scheduled);
	}

	if (sleep || out_of_time)
		job_completion(entry->scheduled, !sleep);

	/* The final scheduling decision. Do we need to switch for some reason?
	 * If linked is different from scheduled, then select linked as next.
	 */
	TRACE("gq: linked=%p scheduled=%p\n", entry->linked, entry->scheduled);
	if (entry->linked != entry->scheduled) {
		/* Schedule a linked job? */
		if (entry->linked) {
			entry->linked->rt_param.scheduled_on = entry->cpu;
			next = entry->linked;
		}
		if (entry->scheduled) {
			/* kick this task off the CPU */
			entry->scheduled->rt_param.scheduled_on = NO_CPU;
			TRACE_TASK(entry->scheduled, "scheduled_on <- NO_CPU\n");
		}
	} else
		/* Only override Linux scheduler if we have a real-time task
		 * scheduled that needs to continue.
		 */
		if (exists)
			next = prev;

	spin_unlock(&gq_lock);

	TRACE("gq_lock released, next=0x%p\n", next);


	if (next)
		TRACE_TASK(next, "scheduled at %llu\n", litmus_clock());
	else if (exists && !next)
		TRACE("becomes idle at %llu.\n", litmus_clock());


	return next;
}


/* _finish_switch - we just finished the switch away from prev
 */
static void gq_finish_switch(struct task_struct *prev)
{
	cpu_state_t* 	entry = &__get_cpu_var(gq_cpu_entries);

	entry->scheduled = is_realtime(current) ? current : NULL;
	TRACE_TASK(prev, "switched away from\n");
}


/*	Prepare a task for running in RT mode
 */
static void gq_task_new(struct task_struct * t, int on_rq, int running)
{
	unsigned long 		flags;
	cpu_state_t* 		entry;

	TRACE("gsn edf: task new %d\n", t->pid);

	spin_lock_irqsave(&gq_lock, flags);
	if (running) {
		entry = &per_cpu(gq_cpu_entries, task_cpu(t));
		BUG_ON(entry->scheduled);
		entry->scheduled = t;
		t->rt_param.scheduled_on = task_cpu(t);
		set_tsk_need_resched(t);
	} else
		t->rt_param.scheduled_on = NO_CPU;
	t->rt_param.linked_on          = NO_CPU;

	/* setup job params */
	release_at(t, litmus_clock());

	gq_add_released_queue(t);
	
	spin_unlock_irqrestore(&gq_lock, flags);
}

static void gq_task_wake_up(struct task_struct *task)
{
	unsigned long flags;
	cpu_state_t*  entry;
	lt_t now;

	spin_lock_irqsave(&gq_lock, flags);

	now = litmus_clock();
	if (is_tardy(task, now)) {
		TRACE_TASK(task, "wake_up => new release\n");
		/* Since task came back after its deadline, we
		 * assume that resuming indidates a new job release.
		 */
		release_at(task, now);
		sched_trace_task_release(task);
		gq_add_released_queue(task);