aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/kfmlp_lock.c
blob: 37302064bd8c138929c6c487301822b93fe710f2 (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
#include <linux/slab.h>
#include <linux/uaccess.h>

#include <litmus/trace.h>
#include <litmus/sched_plugin.h>
#include <litmus/kfmlp_lock.h>

#include <litmus/edf_common.h>

static inline int kfmlp_get_idx(struct kfmlp_semaphore* sem,
								struct kfmlp_queue* queue)
{
	return (queue - &sem->queues[0]);
}

static inline struct kfmlp_queue* kfmlp_get_queue(struct kfmlp_semaphore* sem,
												  struct task_struct* holder)
{
	int i;
	for(i = 0; i < sem->num_resources; ++i)
		if(sem->queues[i].owner == holder)
			return(&sem->queues[i]);
	return(NULL);
}

/* caller is responsible for locking */
static struct task_struct* kfmlp_find_hp_waiter(struct kfmlp_queue *kqueue,
												struct task_struct *skip)
{
	struct list_head	*pos;
	struct task_struct 	*queued, *found = NULL;
	
	list_for_each(pos, &kqueue->wait.task_list) {
		queued  = (struct task_struct*) list_entry(pos, wait_queue_t,
												   task_list)->private;
		
		/* Compare task prios, find high prio task. */
		if (queued != skip && edf_higher_prio(queued, found))
			found = queued;
	}
	return found;
}

static inline struct kfmlp_queue* kfmlp_find_shortest(struct kfmlp_semaphore* sem,
													  struct kfmlp_queue* search_start)
{
	// we start our search at search_start instead of at the beginning of the
	// queue list to load-balance across all resources.
	struct kfmlp_queue* step = search_start;
	struct kfmlp_queue* shortest = sem->shortest_queue;
	
	do
	{
		step = (step+1 != &sem->queues[sem->num_resources]) ?
		step+1 : &sem->queues[0];
		
		if(step->count < shortest->count)
		{
			shortest = step;
			if(step->count == 0)
				break; /* can't get any shorter */
		}
		
	}while(step != search_start);
	
	return(shortest);
}

static struct task_struct* kfmlp_remove_hp_waiter(struct kfmlp_semaphore* sem)
{
	/* must hold sem->lock */
	
	struct kfmlp_queue *my_queue = NULL;
	struct task_struct *max_hp = NULL;
	
	
	struct list_head	*pos;
	struct task_struct 	*queued;
	int i;
	
	for(i = 0; i < sem->num_resources; ++i)
	{
		if( (sem->queues[i].count > 1) &&
		   ((my_queue == NULL) ||
			(edf_higher_prio(sem->queues[i].hp_waiter, my_queue->hp_waiter))) )
		{
			my_queue = &sem->queues[i];
		}
	}
	
	if(my_queue)
	{		
		max_hp = my_queue->hp_waiter;
		
		BUG_ON(!max_hp);
		
		TRACE_CUR("queue %d: stealing %s/%d from queue %d\n",
				  kfmlp_get_idx(sem, my_queue),
				  max_hp->comm, max_hp->pid,
				  kfmlp_get_idx(sem, my_queue));
		
		my_queue->hp_waiter = kfmlp_find_hp_waiter(my_queue, max_hp);
		
		if(tsk_rt(my_queue->owner)->inh_task == max_hp)
		{
			litmus->decrease_prio(my_queue->owner, my_queue->hp_waiter);
		}

		list_for_each(pos, &my_queue->wait.task_list)
		{
			queued  = (struct task_struct*) list_entry(pos, wait_queue_t,
													   task_list)->private;
			/* Compare task prios, find high prio task. */
			if (queued == max_hp)
			{
				/*
				 TRACE_CUR("queue %d: found entry in wait queue.  REMOVING!\n",
				 kfmlp_get_idx(sem, my_queue));
				 */
				__remove_wait_queue(&my_queue->wait,
									list_entry(pos, wait_queue_t, task_list));
				break;
			}
		}
		--(my_queue->count);
	}
	
	return(max_hp);
}

int kfmlp_lock(struct litmus_lock* l)
{
	struct task_struct* t = current;
	struct kfmlp_semaphore *sem = kfmlp_from_lock(l);
	struct kfmlp_queue* my_queue;
	wait_queue_t wait;
	unsigned long flags;
	
	if (!is_realtime(t))
		return -EPERM;
	
	spin_lock_irqsave(&sem->lock, flags);
	
	my_queue = sem->shortest_queue;
	
	if (my_queue->owner) {
		/* resource is not free => must suspend and wait */
		TRACE_CUR("queue %d: Resource is not free => must suspend and wait.\n",
				  kfmlp_get_idx(sem, my_queue));
		
		init_waitqueue_entry(&wait, t);
		
		/* FIXME: interruptible would be nice some day */
		set_task_state(t, TASK_UNINTERRUPTIBLE);
		
		__add_wait_queue_tail_exclusive(&my_queue->wait, &wait);
		
		/* check if we need to activate priority inheritance */
		if (edf_higher_prio(t, my_queue->hp_waiter))
		{
			my_queue->hp_waiter = t;
			if (edf_higher_prio(t, my_queue->owner))
			{
				litmus->increase_prio(my_queue->owner, my_queue->hp_waiter);
			}
		}
		
		++(my_queue->count);
		sem->shortest_queue = kfmlp_find_shortest(sem, my_queue);
		
		/* release lock before sleeping */
		spin_unlock_irqrestore(&sem->lock, flags);
		
		/* We depend on the FIFO order.  Thus, we don't need to recheck
		 * when we wake up; we are guaranteed to have the lock since
		 * there is only one wake up per release (or steal).
		 */
		schedule();
		
		
		if(my_queue->owner == t)
		{
			TRACE_CUR("queue %d: acquired through waiting\n",
					  kfmlp_get_idx(sem, my_queue));
		}
		else
		{
			/* this case may happen if our wait entry was stolen
			 between queues. record where we went. */
			my_queue = kfmlp_get_queue(sem, t);
			
			BUG_ON(!my_queue);
			TRACE_CUR("queue %d: acquired through stealing\n",
					  kfmlp_get_idx(sem, my_queue));
		}
	}
	else
	{
		TRACE_CUR("queue %d: acquired immediately\n",
				  kfmlp_get_idx(sem, my_queue));
		
		my_queue->owner = t;
		
		++(my_queue->count);
		sem->shortest_queue = kfmlp_find_shortest(sem, my_queue);		
		
		spin_unlock_irqrestore(&sem->lock, flags);
	}
	
	return kfmlp_get_idx(sem, my_queue);
}


int kfmlp_unlock(struct litmus_lock* l)
{
	struct task_struct *t = current, *next;
	struct kfmlp_semaphore *sem = kfmlp_from_lock(l);
	struct kfmlp_queue *my_queue;
	unsigned long flags;
	int err = 0;
	
	spin_lock_irqsave(&sem->lock, flags);
	
	my_queue = kfmlp_get_queue(sem, t);
	
	if (!my_queue) {
		err = -EINVAL;
		goto out;
	}
	
	/* check if there are jobs waiting for this resource */
	next = __waitqueue_remove_first(&my_queue->wait);
	if (next) {
		/*
		 TRACE_CUR("queue %d: ASSIGNING %s/%d as owner - next\n",
		 kfmlp_get_idx(sem, my_queue),
		 next->comm, next->pid);
		 */
		/* next becomes the resouce holder */
		my_queue->owner = next;
		
		--(my_queue->count);
		// the '=' of '<=' is a dumb method to attempt to build
		// affinity until tasks can tell us where they ran last...
		if(my_queue->count <= sem->shortest_queue->count)
		{
			sem->shortest_queue = my_queue;
		}	
		
		TRACE_CUR("queue %d: lock ownership passed to %s/%d\n",
				  kfmlp_get_idx(sem, my_queue), next->comm, next->pid);
		
		/* determine new hp_waiter if necessary */
		if (next == my_queue->hp_waiter) {
			TRACE_TASK(next, "was highest-prio waiter\n");
			/* next has the highest priority --- it doesn't need to
			 * inherit.  However, we need to make sure that the
			 * next-highest priority in the queue is reflected in
			 * hp_waiter. */
			my_queue->hp_waiter = kfmlp_find_hp_waiter(my_queue, next);
			if (my_queue->hp_waiter)
				TRACE_TASK(my_queue->hp_waiter, "queue %d: is new highest-prio waiter\n", kfmlp_get_idx(sem, my_queue));
			else
				TRACE("queue %d: no further waiters\n", kfmlp_get_idx(sem, my_queue));
		} else {
			/* Well, if next is not the highest-priority waiter,
			 * then it ought to inherit the highest-priority
			 * waiter's priority. */
			litmus->increase_prio(next, my_queue->hp_waiter);
		}
		
		/* wake up next */
		wake_up_process(next);
	}
	else
	{
		TRACE_CUR("queue %d: looking to steal someone...\n", kfmlp_get_idx(sem, my_queue));
		
		next = kfmlp_remove_hp_waiter(sem); /* returns NULL if nothing to steal */
		
		/*
		 if(next)
		 TRACE_CUR("queue %d: ASSIGNING %s/%d as owner - steal\n",
		 kfmlp_get_idx(sem, my_queue),
		 next->comm, next->pid);
		 */
		
		my_queue->owner = next;
		
		if(next)
		{
			TRACE_CUR("queue %d: lock ownership passed to %s/%d (which was stolen)\n",
					  kfmlp_get_idx(sem, my_queue),
					  next->comm, next->pid);
			
			/* wake up next */
			wake_up_process(next);			
		}
		else
		{
			TRACE_CUR("queue %d: no one to steal.\n", kfmlp_get_idx(sem, my_queue));
			
			--(my_queue->count);
			// the '=' of '<=' is a dumb method to attempt to build
			// affinity until tasks can tell us where they ran last...
			if(my_queue->count <= sem->shortest_queue->count)
			{
				sem->shortest_queue = my_queue;
			}
		}
	}
	
	/* we lose the benefit of priority inheritance (if any) */
	if (tsk_rt(t)->inh_task)
		litmus->decrease_prio(t, NULL);
	
out:
	spin_unlock_irqrestore(&sem->lock, flags);
	
	return err;
}

int kfmlp_close(struct litmus_lock* l)
{
	struct task_struct *t = current;
	struct kfmlp_semaphore *sem = kfmlp_from_lock(l);
	struct kfmlp_queue *my_queue;
	unsigned long flags;
	
	int owner;
	
	spin_lock_irqsave(&sem->lock, flags);
	
	my_queue = kfmlp_get_queue(sem, t);	
	owner = (my_queue) ? (my_queue->owner == t) : 0;
	
	spin_unlock_irqrestore(&sem->lock, flags);
	
	if (owner)
		kfmlp_unlock(l);
	
	return 0;
}

void kfmlp_free(struct litmus_lock* l)
{
	struct kfmlp_semaphore *sem = kfmlp_from_lock(l);
	kfree(sem->queues);
	kfree(sem);
}



struct litmus_lock* kfmlp_new(struct litmus_lock_ops* ops, void* __user args)
{
	struct kfmlp_semaphore* sem;
	int num_resources = 0;
	int i;
	
	if(!access_ok(VERIFY_READ, args, sizeof(num_resources)))
	{
		return(NULL);
	}
	if(__copy_from_user(&num_resources, args, sizeof(num_resources)))
	{
		return(NULL);
	}
	if(num_resources < 1)
	{
		return(NULL);		
	}
	
	sem = kmalloc(sizeof(*sem), GFP_KERNEL);
	if(!sem)
	{
		return(NULL);
	}
	
	sem->queues = kmalloc(sizeof(struct kfmlp_queue)*num_resources, GFP_KERNEL);
	if(!sem->queues)
	{
		kfree(sem);
		return(NULL);
	}
	
	sem->litmus_lock.ops = ops;
	spin_lock_init(&sem->lock);
	sem->num_resources = num_resources;
	
	for(i = 0; i < num_resources; ++i)
	{
		sem->queues[i].owner = NULL;
		sem->queues[i].hp_waiter = NULL;
		init_waitqueue_head(&sem->queues[i].wait);
		sem->queues[i].count = 0;
	}
	
	sem->shortest_queue = &sem->queues[0];

	return &sem->litmus_lock;
}