aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/color_queue.c
blob: 0b87217a931a824176dd2f95a94b800c7306b82b (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
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/sched.h>

#include <litmus/color_queue.h>
#include <litmus/color.h>
#include <litmus/lockdown.h>
#include <litmus/color.h>
#include <litmus/litmus.h>
#include <litmus/trace.h>

/* Uncomment to debug. */
#if 0
#define QTRACE(q, fmt, args...)	TRACE_CUR("q.phase: %llu  q.way: %d  " \
				"q.nr_cpus: %d  q.at_barrier: %d  " fmt, \
				q.phase, q.way, q.nr_cpus, q.at_barrier, \
				## args)
#endif

#ifndef QTRACE
#define QTRACE(q, fmt, args...) do { } while (0)
#endif

struct color_queue {
	struct list_head queue;
	/* the queue of work */

	int nr_cpus;
	/* number of CPUs that have outstanding requests */

	int at_barrier;
	/* number of CPUs that are currently waiting at the barrier */

	int way;
	/* which way the queue is currently reading in */

	u64 phase;
	/* monotonically increasing phase. use 64 bits so it doesn't wrap. */

	raw_spinlock_t lock;
	/* protects the above members */
};

struct cpu_entry {
	int phase;
	/* what phase a CPU is in */

	struct list_head enqueue;
	/* used as a PENDING list of work to enqueue in the color queue */

	int nr_work;
	/* outstanding work elements this CPU has */

	raw_spinlock_t lock;
	/* guard nr_work when CPU is "inside" the queue (its work is added) */
};

DEFINE_PER_CPU(struct cpu_entry, cpu_entries);
/* Per-CPU state. */

extern u32 unlocked_way[];
/* Index i means way i is unlocked. Comes from litmus/lockdown.c */

struct color_queue color_queue;
/* The queue. */

static u32 lockdown_value;
/* Cached lockdown value so that we don't need to look it up each time.
 * Should agree with color_queue.way.
 */

static u32 lockdown_value_idle = UNLOCK_ALL;
/* Lockdown state when the queue is inactive. Lets us lock down some ways
 * if we choose to.
 */

static void ***flusher_pages = NULL;
/* One page per [way, color] pair used for flushing. */

/*
 * Add work to the cpu_entry's PENDING list of work for the color queue.
 */
static void cpu_add_work(struct color_queue_request *req)
{
	struct cpu_entry *entry = &__get_cpu_var(cpu_entries);
	struct list_head *new = &req->list;
	list_add_tail(new, &entry->enqueue);
	entry->nr_work++;
	//TRACE_CUR("cpu->nr_work: %d  added work request pointer %p\n",
	//		entry->nr_work, req);
}

/*
 * Add a pending read in of a page. Does the chunking into work units.
 *
 * You should already set @way (in @color_page_info) by the time you
 * call this!
 *
 * @vaddr_start changes depending upon if this is a read-in or a "flush".
 */
static void color_page_info_add_work(struct color_page_info *info, void *vaddr_start)
{
	const int cpu = smp_processor_id();
	int i;

	//TRACE_CUR("adding work for color_page_info: 0x%p\n", info);

	for (i = 0; i < COLOR_REQUESTS_PER_PAGE; i++) {
		struct color_queue_request *req = &info->requests[i];
		void *work_vaddr = vaddr_start + i * COLOR_QUEUE_REQ_SIZE;

		WARN(req->request_type != COLOR_QUEUE_IDLE,
				"request was not idle!\n");

		req->request_type = COLOR_QUEUE_READ;
		req->cpu = cpu;
		req->request_data.read.vaddr = work_vaddr;
		cpu_add_work(req);
	}
}

static int color_queue_submit_work(void);

/*
 * Assumes this is called on the CPU that @ts ran on and that this CPU that is
 * requesting the work. Do not try and schedule work for someone else!
 *
 * Also assumes that @way set for each color_page_info before calling!
 */
int color_queue_enqueue_read(struct task_struct *ts)
{
	struct color_page_info *cur_info;

	TS_CQ_ENQUEUE_READ_START;

#if 0
	TRACE_CUR("enqueue read prev: %p  next: %p\n",
			tsk_rt(ts)->color_page_info_list.prev,
			tsk_rt(ts)->color_page_info_list.next);
#endif

	list_for_each_entry(cur_info,
			&tsk_rt(ts)->color_page_info_list,
			list)
	{
		void *vaddr_start = cur_info->vaddr;
		/* since this is a read, uses the page's address */

		color_page_info_add_work(cur_info, vaddr_start);
	}

	TS_CQ_ENQUEUE_READ_END;

	return color_queue_submit_work();
}

/*
 * Assumes this is called on the CPU that @ts ran on and that this CPU that is
 * requesting the work. Do not try and schedule work for someone else!
 *
 * Also assumes that @way is still set to the way this page was read into to
 * find the proper address to flush.
 */
int color_queue_enqueue_flush(struct task_struct *ts)
{
	struct color_page_info *cur_info;

	TS_CQ_ENQUEUE_FLUSH_START;

	list_for_each_entry(cur_info,
			&tsk_rt(ts)->color_page_info_list,
			list)
	{
		void *vaddr_start;
		vaddr_start = flusher_pages[cur_info->way][cur_info->color];
		/* now we use the corresponding flusher page */
		color_page_info_add_work(cur_info, vaddr_start);
	}

	TS_CQ_ENQUEUE_FLUSH_END;

	return color_queue_submit_work();
}

static void do_work_read(struct color_queue_request *request)
{
	void *vaddr = request->request_data.read.vaddr;

	/* Don't know which CPU is first to do work in a given phase, so have
	 * all CPUs set the lockdown register to the same value. Also, the
	 * "unlock_val" is also the lock value, since we don't know if other
	 * CPUs are still reading. We could take the unlock out of the read_in
	 * function, but it's one store operation and probably takes a few
	 * nanoseconds...
	 */
	color_read_in_mem(lockdown_value, lockdown_value,
			vaddr, vaddr + COLOR_QUEUE_REQ_SIZE);
}

/*
 * This just does a unit of work, ... son.
 */
static void do_work_son(struct color_queue_request *request)
{
	struct cpu_entry *entry;

	TS_CQ_WORK_DO_WORK_START;
	switch (request->request_type) {
		case COLOR_QUEUE_IDLE:
			TRACE_CUR("idle work in the queue!\n");
			WARN(1, "Idle work in the queue!\n");
			break;
		case COLOR_QUEUE_READ:
			//TRACE_CUR("doing work for CPU %d\n", request->cpu);
			do_work_read(request);
			break;
	}
	TS_CQ_WORK_DO_WORK_END;

	TS_CQ_WORK_NOTIFY_START;
	/* Tell the (possibly remote) CPU that we're a bro and helped out. */
	entry = &per_cpu(cpu_entries, request->cpu);
	raw_spin_lock(&entry->lock);
	entry->nr_work--;
	//TRACE_CUR("after doing work, cpu->nr_work: %d\n", entry->nr_work);
	raw_spin_unlock(&entry->lock);

	/* work is done, set it idle */
	request->request_type = COLOR_QUEUE_IDLE;
	TS_CQ_WORK_NOTIFY_END;
}

static void wait_next_phase(void)
{
	struct cpu_entry *entry = &__get_cpu_var(cpu_entries);
	struct color_queue_request *request;

	TS_CQ_PHASE_WAIT_START;

	for (;;) {
		raw_spin_lock(&color_queue.lock);
		if (entry->phase < color_queue.phase) {
			/* Move on to the next phase (or later). Another CPU already
			 * set up the lockdown value and updated the queue phase.
			 */
			entry->phase = color_queue.phase;
			QTRACE(color_queue,
					"cpu->phase: %d advances to a higher phase\n",
					entry->phase);
			raw_spin_unlock(&color_queue.lock);
			TS_CQ_PHASE_WAIT_END;
			return;
		}

		if (color_queue.nr_cpus == color_queue.at_barrier) {
			int next_way;

			QTRACE(color_queue, "everyone reached the barrier\n");

			/* Ready to start the next phase. */
			if (unlikely(list_empty(&color_queue.queue))) {
				QTRACE(color_queue, "The queue was empty (bad)\n");
				/* This should not happen! Will loop forever? */
				WARN(1, "color queue list was empty!\n");
				raw_spin_unlock(&color_queue.lock);
				continue;
			}
			request = list_first_entry(&color_queue.queue,
					struct color_queue_request, list);
			next_way = request->color_page_info->way;
			lockdown_value = unlocked_way[next_way];
			color_queue.way = next_way;
			color_queue.at_barrier = 0;
			color_queue.phase++;
			QTRACE(color_queue, "bumped the phase and way\n");
			raw_spin_unlock(&color_queue.lock);
		} else if (color_queue.nr_cpus < color_queue.at_barrier) {
			BUG();
		} else {
			/* Wait for work from the last phase to complete. */
			QTRACE(color_queue, "still waiting for others\n");
			raw_spin_unlock(&color_queue.lock);
			cpu_relax();
		}
	}
}

static int color_queue_loop(void)
{
	struct cpu_entry *entry = &__get_cpu_var(cpu_entries);
	struct color_queue_request *request;
	int nr_work, nr_work_done_by_task = 0;

	for (;;) {
		TS_CQ_LOOP_WORK_CHECK_START;
		raw_spin_lock(&entry->lock);
		nr_work = entry->nr_work;
		raw_spin_unlock(&entry->lock);
		TS_CQ_LOOP_WORK_CHECK_END;

		if (0 == nr_work) {
			TS_CQ_LOOP_PEACE_OUT_START;
			/* All the work is done for this CPU. We can leave. */
			raw_spin_lock(&color_queue.lock);
			color_queue.nr_cpus--;
			if (0 == color_queue.nr_cpus) {
				/* Queue is going idle. Restore lockdown. */
				set_lockdown(lockdown_value_idle);
				QTRACE(color_queue, "last CPU in queue.\n");
			}
			QTRACE(color_queue, "just left the queue\n");
			raw_spin_unlock(&color_queue.lock);
			TS_CQ_LOOP_PEACE_OUT_END;
			return nr_work_done_by_task;
		}

		/* Our work is not done, so continue processing more work. */

		TS_CQ_LOOP_BRANCH_START;
		raw_spin_lock(&color_queue.lock);
		if (unlikely(list_empty(&color_queue.queue))) {
			/* can this happen? */
			QTRACE(color_queue, "color queue was empty?\n");
			WARN(1, "color queue list was empty...\n");
			raw_spin_unlock(&color_queue.lock);
			TS_CQ_LOOP_BRANCH_END;
			continue;
		}
		request = list_first_entry(&color_queue.queue,
				struct color_queue_request, list);
		if (color_queue.way == request->color_page_info->way) {
			/* we're going to do this work */
			list_del(&request->list);
			QTRACE(color_queue, "found a piece of work to do: %p\n", request);
			raw_spin_unlock(&color_queue.lock);
			TS_CQ_LOOP_BRANCH_END;
			do_work_son(request);
			++nr_work_done_by_task;
		} else {
			/* we need to wait for the next phase */
			color_queue.at_barrier++;
			QTRACE(color_queue, "begins to wait at barrier...\n");
			raw_spin_unlock(&color_queue.lock);
			TS_CQ_LOOP_BRANCH_END;
			wait_next_phase();
		}
	}
}

/*
 * Actually enqueues the work on the color queue and enters the work loop.
 */
static int color_queue_submit_work(void)
{
	struct cpu_entry *entry;

	TS_CQ_SUBMIT_WORK_START;

	entry = &__get_cpu_var(cpu_entries);

	raw_spin_lock(&color_queue.lock);
	QTRACE(color_queue, "start submit work\n");
	entry->phase = color_queue.phase;
	color_queue.nr_cpus++;
	list_splice_tail_init(&entry->enqueue, &color_queue.queue);
	QTRACE(color_queue, "end submit work\n");
	raw_spin_unlock(&color_queue.lock);

	TS_CQ_SUBMIT_WORK_END;

	return color_queue_loop();
}

void cleanup_color_page_infos(struct list_head *head)
{
	struct color_page_info *cur, *tmp;

	if (NULL == head) {
		TRACE("Head was null.\n");
		return;
	}

	if (NULL == head->next || NULL == head->prev) {
		TRACE_CUR("next or prev was null\n");
		return;
	}

	list_for_each_entry_safe(cur, tmp, head, list) {
		TRACE_CUR("cleanup color_queue_info %p\n", cur);
		list_del(&cur->list);
		kfree(cur);
	}
}

/* called when user does add_pages proc handler */
int setup_flusher_array(void)
{
	int color, way, ret = 0;
	struct page *page;

	if (0 == color_cache_info.ways || 0 == color_cache_info.nr_colors) {
		WARN(1, "Cache information not initialized!\n");
		ret = -EINVAL;
		goto out;
	}

	if (flusher_pages != NULL)
		goto out;

	flusher_pages = (void***) kmalloc(color_cache_info.ways
			* sizeof(*flusher_pages), GFP_KERNEL);
	if (!flusher_pages) {
		printk(KERN_WARNING "No memory for flusher array!\n");
		ret = -EINVAL;
		goto out;
	}

	for (way = 0; way < color_cache_info.ways; way++) {
		void **flusher_color_arr;
		flusher_color_arr = (void**) kmalloc(sizeof(**flusher_pages)
				* color_cache_info.nr_colors, GFP_KERNEL);
		if (!flusher_color_arr) {
			printk(KERN_WARNING "No memory for flusher array!\n");
			ret = -ENOMEM;
			goto out_free;
		}

		flusher_pages[way] = flusher_color_arr;

		for (color = 0; color < color_cache_info.nr_colors; color++) {
			page = get_colored_page(color);
			if (!page) {
				printk(KERN_WARNING "no more colored pages\n");
				ret = -EINVAL;
				goto out_free;
			}
			flusher_pages[way][color] = page_address(page);
			if (!flusher_pages[way][color]) {
				printk(KERN_WARNING "bad page address\n");
				ret = -EINVAL;
				goto out_free;
			}
		}
	}
out:
	return ret;
out_free:
	for (way = 0; way < color_cache_info.ways; way++) {
		for (color = 0; color < color_cache_info.nr_colors; color++) {
			/* not bothering to try and give back colored pages */
		}
		kfree(flusher_pages[way]);
	}
	kfree(flusher_pages);
	flusher_pages = NULL;
	return ret;
}

static int __init init_color_queue(void)
{
	struct cpu_entry *cpu_entry;
	int cpu;

	BUILD_BUG_ON((PAGE_SIZE % COLOR_QUEUE_REQ_SIZE) != 0);

	for_each_online_cpu(cpu) {
		cpu_entry = &per_cpu(cpu_entries, cpu);
		INIT_LIST_HEAD(&cpu_entry->enqueue);
		cpu_entry->nr_work = 0;
		raw_spin_lock_init(&cpu_entry->lock);
	}

	raw_spin_lock_init(&color_queue.lock);
	INIT_LIST_HEAD(&color_queue.queue);

	return 0;
}

module_init(init_color_queue);