aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/rt_domain.c
blob: 93f2a35fb29de1e5001731ff944db23b5d9d71e6 (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
/*
 * litmus/rt_domain.c
 *
 * LITMUS real-time infrastructure. This file contains the
 * functions that manipulate RT domains. RT domains are an abstraction
 * of a ready queue and a release queue.
 */

#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/slab.h>

#include <litmus/litmus.h>
#include <litmus/event_group.h>
#include <litmus/sched_plugin.h>
#include <litmus/sched_trace.h>
#include <litmus/rt_domain.h>
#include <litmus/trace.h>
#include <litmus/bheap.h>

/* Uncomment when debugging timer races... */
#if 1
#define VTRACE_TASK TRACE_TASK
#define VTRACE TRACE
#else
#define VTRACE_TASK(t, fmt, args...) /* shut up */
#define VTRACE(fmt, args...) /* be quiet already */
#endif

static int dummy_resched(rt_domain_t *rt)
{
	return 0;
}

static int dummy_order(struct bheap_node* a, struct bheap_node* b)
{
	return 0;
}

/* default implementation: use default lock */
static void default_release_jobs(rt_domain_t* rt, struct bheap* tasks)
{
	merge_ready(rt, tasks);
}

static unsigned int time2slot(lt_t time)
{
	return (unsigned int) time2quanta(time, FLOOR) % RELEASE_QUEUE_SLOTS;
}

static void do_release(struct release_heap *rh)
{
	unsigned long flags;
	TS_RELEASE_START;

	raw_spin_lock_irqsave(&rh->dom->release_lock, flags);
	VTRACE("CB has the release_lock 0x%p\n", &rh->dom->release_lock);
	/* remove from release queue */
	list_del_init(&rh->list);
	raw_spin_unlock_irqrestore(&rh->dom->release_lock, flags);
	VTRACE("CB returned release_lock 0x%p\n", &rh->dom->release_lock);

	/* call release callback */
	rh->dom->release_jobs(rh->dom, &rh->heap);

	TS_RELEASE_END;
}

#ifdef CONFIG_MERGE_TIMERS
static void on_release(struct rt_event *e)
{
	do_release(container_of(e, struct release_heap, event));
}
#else
static enum hrtimer_restart on_release(struct hrtimer *timer)
{
	do_release(container_of(timer, struct release_heap, timer));
	return HRTIMER_NORESTART;
}
#endif

/* allocated in litmus.c */
struct kmem_cache * release_heap_cache;

struct release_heap* release_heap_alloc(int gfp_flags)
{
	struct release_heap* rh;
	rh = kmem_cache_alloc(release_heap_cache, gfp_flags);
	if (rh) {
#ifdef CONFIG_MERGE_TIMERS
		init_event(&rh->event, 0, on_release,
			   event_list_alloc(GFP_ATOMIC));
#else
		/* initialize timer */
		hrtimer_init(&rh->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
		rh->timer.function = on_release;
#endif
	}
	return rh;
}

#ifdef CONFIG_MERGE_TIMERS
extern struct kmem_cache *event_list_cache;
#endif

void release_heap_free(struct release_heap* rh)
{
	/* make sure timer is no longer in use */
#ifdef CONFIG_MERGE_TIMERS
	if (rh->dom) {
		cancel_event(&rh->event);
		kmem_cache_free(event_list_cache, rh->event.event_list);
	}
#else
	hrtimer_cancel(&rh->timer);
#endif
	kmem_cache_free(release_heap_cache, rh);
}

/* Caller must hold release lock.
 * Will return heap for given time. If no such heap exists prior to
 * the invocation it will be created.
 */
static struct release_heap* get_release_heap(rt_domain_t *rt,
					     struct task_struct* t,
					     int use_task_heap)
{
	struct list_head* pos;
	struct release_heap* heap = NULL;
	struct release_heap* rh;
	lt_t release_time = get_release(t);
	unsigned int slot = time2slot(release_time);

	/* initialize pos for the case that the list is empty */
	pos = rt->release_queue.slot[slot].next;
	list_for_each(pos, &rt->release_queue.slot[slot]) {
		rh = list_entry(pos, struct release_heap, list);
		if (release_time == rh->release_time) {
			/* perfect match -- this happens on hyperperiod
			 * boundaries
			 */
			heap = rh;
			break;
		} else if (lt_before(release_time, rh->release_time)) {
			/* we need to insert a new node since rh is
			 * already in the future
			 */
			break;
		}
	}
	if (!heap && use_task_heap) {
		/* use pre-allocated release heap */
		rh = tsk_rt(t)->rel_heap;

		rh->dom = rt;
		rh->release_time = release_time;

		/* add to release queue */
		list_add(&rh->list, pos->prev);
		heap = rh;
	}
	return heap;
}

static void reinit_release_heap(rt_domain_t *rt, struct task_struct* t)
{
	struct release_heap* rh;

	/* use pre-allocated release heap */
	rh = tsk_rt(t)->rel_heap;

#ifdef CONFIG_MERGE_TIMERS
	rh->event.prio = rt->prio;
	cancel_event(&rh->event);
#else
	/* Make sure it is safe to use.  The timer callback could still
	 * be executing on another CPU; hrtimer_cancel() will wait
	 * until the timer callback has completed.  However, under no
	 * circumstances should the timer be active (= yet to be
	 * triggered).
	 *
	 * WARNING: If the CPU still holds the release_lock at this point,
	 *          deadlock may occur!
	 */
	BUG_ON(hrtimer_cancel(&rh->timer));

#ifdef CONFIG_RELEASE_MASTER
	atomic_set(&rh->info.state, HRTIMER_START_ON_INACTIVE);
#endif
#endif
	/* initialize */
	bheap_init(&rh->heap);

}

#ifdef CONFIG_RELEASE_MASTER
static void arm_release_timer_on(struct release_heap *rh, int target_cpu)
#else
static void arm_release_timer(struct release_heap *rh)
#endif
{
#ifdef CONFIG_MERGE_TIMERS
	add_event(rh->dom->event_group, &rh->event, rh->release_time);
#else
	VTRACE("arming timer 0x%p\n", &rh->timer);
	/* we cannot arm the timer using hrtimer_start()
	 * as it may deadlock on rq->lock
	 * PINNED mode is ok on both local and remote CPU
	 */

#ifdef CONFIG_RELEASE_MASTER
	if (rh->dom->release_master == NO_CPU && target_cpu == NO_CPU)
#endif
		__hrtimer_start_range_ns(&rh->timer,
					 ns_to_ktime(rh->release_time),
					 0, HRTIMER_MODE_ABS_PINNED, 0);
#ifdef CONFIG_RELEASE_MASTER
	else
		hrtimer_start_on(/* target_cpu overrides release master */
				 (target_cpu != NO_CPU ?
				  target_cpu : rh->dom->release_master),
				 &rh->info, &rh->timer,
				 ns_to_ktime(rh->release_time),
				 HRTIMER_MODE_ABS_PINNED);
#endif
#endif
}


/* setup_release() - start local release timer or trigger
 *     remote timer (pull timer)
 *
 * Called by add_release() with:
 * - tobe_lock taken
 * - IRQ disabled
 */
#ifdef CONFIG_RELEASE_MASTER
#define setup_release(t) setup_release_on((t), NO_CPU)
static void setup_release_on(rt_domain_t *_rt , int target_cpu)
#else
static void setup_release(rt_domain_t *_rt)
#endif
{
	rt_domain_t *rt = _rt;
	struct list_head list;
	struct list_head *pos, *safe;
	struct task_struct* t;
	struct release_heap* rh;

	VTRACE("setup_release() at %llu\n", litmus_clock());
	list_replace_init(&rt->tobe_released, &list);

	list_for_each_safe(pos, safe, &list) {
		/* pick task of work list */
		t = list_entry(pos, struct task_struct, rt_param.list);
		sched_trace_task_release(t);
		list_del_init(pos);

		/* put into release heap while holding release_lock */
		raw_spin_lock(&rt->release_lock);
		VTRACE_TASK(t, "I have the release_lock 0x%p\n", &rt->release_lock);

		rh = get_release_heap(rt, t, 0);
		if (!rh) {
			/* need to use our own, but drop lock first */
			raw_spin_unlock(&rt->release_lock);
			VTRACE_TASK(t, "Dropped release_lock 0x%p\n",
				    &rt->release_lock);

			reinit_release_heap(rt, t);
			VTRACE_TASK(t, "release_heap ready\n");

			raw_spin_lock(&rt->release_lock);
			VTRACE_TASK(t, "Re-acquired release_lock 0x%p\n",
				    &rt->release_lock);

			rh = get_release_heap(rt, t, 1);
		}
		bheap_insert(rt->order, &rh->heap, tsk_rt(t)->heap_node);
		VTRACE_TASK(t, "setup_release(): added to release heap\n");

		raw_spin_unlock(&rt->release_lock);
		VTRACE_TASK(t, "Returned the release_lock 0x%p\n", &rt->release_lock);

		/* To avoid arming the timer multiple times, we only let the
		 * owner do the arming (which is the "first" task to reference
		 * this release_heap anyway).
		 */
		if (rh == tsk_rt(t)->rel_heap) {
#ifdef CONFIG_RELEASE_MASTER
			arm_release_timer_on(rh, target_cpu);
#else
			arm_release_timer(rh);
#endif
		}
	}
}

void rt_domain_init(rt_domain_t *rt,
		    bheap_prio_t order,
		    check_resched_needed_t check,
		    release_jobs_t release)
{
	int i;

	BUG_ON(!rt);
	if (!check)
		check = dummy_resched;
	if (!release)
		release = default_release_jobs;
	if (!order)
		order = dummy_order;

#if defined(CONFIG_RELEASE_MASTER) && !defined(CONFIG_MERGE_TIMERS)
	rt->release_master = NO_CPU;
#endif

	bheap_init(&rt->ready_queue);
	INIT_LIST_HEAD(&rt->tobe_released);
	for (i = 0; i < RELEASE_QUEUE_SLOTS; i++)
		INIT_LIST_HEAD(&rt->release_queue.slot[i]);

	raw_spin_lock_init(&rt->ready_lock);
	raw_spin_lock_init(&rt->release_lock);
	raw_spin_lock_init(&rt->tobe_lock);

	rt->check_resched 	= check;
	rt->release_jobs	= release;
	rt->order		= order;
}

/* add_ready - add a real-time task to the rt ready queue. It must be runnable.
 * @new:       the newly released task
 */
void __add_ready(rt_domain_t* rt, struct task_struct *new)
{
	VTRACE("rt: adding %s/%d (%llu, %llu) rel=%llu to ready queue at %llu\n",
	      new->comm, new->pid, get_exec_cost(new), get_rt_period(new),
	      get_release(new), litmus_clock());

	BUG_ON(bheap_node_in_heap(tsk_rt(new)->heap_node));

	new->rt_param.domain = rt;
	bheap_insert(rt->order, &rt->ready_queue, tsk_rt(new)->heap_node);
	rt->check_resched(rt);
}

/* merge_ready - Add a sorted set of tasks to the rt ready queue. They must be runnable.
 * @tasks      - the newly released tasks
 */
void __merge_ready(rt_domain_t* rt, struct bheap* tasks)
{
	bheap_union(rt->order, &rt->ready_queue, tasks);
	rt->check_resched(rt);
}


#ifdef CONFIG_RELEASE_MASTER
void __add_release_on(rt_domain_t* rt, struct task_struct *task,
		      int target_cpu)
{
	VTRACE_TASK(task, "add_release_on(), rel=%llu, target=%d\n",
		   get_release(task), target_cpu);
	list_add(&tsk_rt(task)->list, &rt->tobe_released);
	task->rt_param.domain = rt;

	/* start release timer */
	TS_SCHED2_START(task);

	setup_release_on(rt, target_cpu);

	TS_SCHED2_END(task);
}
#endif

/* add_release - add a real-time task to the rt release queue.
 * @task:        the sleeping task
 */
void __add_release(rt_domain_t* rt, struct task_struct *task)
{
	VTRACE_TASK(task, "add_release(), rel=%llu\n", get_release(task));
	list_add(&tsk_rt(task)->list, &rt->tobe_released);
	task->rt_param.domain = rt;

	/* start release timer */
	TS_SCHED2_START(task);

	setup_release(rt);

	TS_SCHED2_END(task);
}

/******************************************************************************
 * domain_t wrapper
 ******************************************************************************/

/* pd_requeue - calls underlying rt_domain add methods.
 * If the task is not yet released, it is inserted into the rt_domain
 * ready queue. Otherwise, it is queued for release.
 *
 * Assumes the caller already holds dom->lock.
 */
static void pd_requeue(domain_t *dom, struct task_struct *task)
{
	rt_domain_t *domain = (rt_domain_t*)dom->data;
	BUG_ON(!task || !is_realtime(task));
	BUG_ON(is_queued(task));
	BUG_ON(get_task_domain(task) != dom);

	if (is_released(task, litmus_clock())) {
		__add_ready(domain, task);
		VTRACE("rt: adding %s/%d (%llu, %llu) rel=%llu to ready queue at %llu\n",
		      task->comm, task->pid, get_exec_cost(task), get_rt_period(task),
		      get_release(task), litmus_clock());
	} else {
		/* task has to wait for next release */
		VTRACE_TASK(task, "add release(), rel=%llu\n", get_release(task));
		add_release(domain, task);
	}
}

/* pd_take_ready - removes and returns the next ready task from the rt_domain
 *
 * Assumes the caller already holds dom->lock.
 */
static struct task_struct* pd_take_ready(domain_t *dom)
{
	return __take_ready((rt_domain_t*)dom->data);
}

/* pd_peek_ready - returns the head of the rt_domain ready queue
 *
 * Assumes the caller already holds dom->lock.
 */
static struct task_struct* pd_peek_ready(domain_t *dom)
{
	return __next_ready((rt_domain_t*)dom->data);
}

static void pd_remove(domain_t *dom, struct task_struct *task)
{
	if (is_queued(task)) {
		remove((rt_domain_t*)dom->data, task);
	}
}

/* pd_domain_init - create a generic domain wrapper for an rt_domain
 */
void pd_domain_init(domain_t *dom,
		    rt_domain_t *domain,
		    bheap_prio_t order,
		    check_resched_needed_t check,
		    release_jobs_t release,
		    preempt_needed_t preempt_needed,
		    task_prio_t priority)
{
	rt_domain_init(domain, order, check, release);
	domain_init(dom, &domain->ready_lock,
		    pd_requeue, pd_peek_ready, pd_take_ready,
		    preempt_needed, priority);
	dom->remove = pd_remove;
	dom->data = domain;
}