aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/dgl.c
blob: 3d6d3201928e40416ca6d52216a40d4fc79bed2b (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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
#include <linux/sched.h>
#include <linux/slab.h>

#include <litmus/litmus.h>
#include <litmus/dgl.h>
#include <litmus/sched_trace.h>

#define DEBUG_DGL

#ifdef DEBUG_DGL
#define TRACE(fmt, args...) STRACE(fmt, ## args)
#define TRACE_GREQ(greq, fmt, args...)					       \
	TRACE("(greq-%s/%llu) " fmt, (greq->task ? greq->task->comm : "greq"), \
	      (greq->task ? greq->task->pid : (unsigned long long)greq), ## args)
#else
#define TRACE(fmt, args...)
#define TRACE_GREQ(greq, fmt, args...)
#endif

#define MASK_SIZE     (sizeof(unsigned long) * 8)

/* Return number of MASK_SIZE fields needed to store a mask in d */
#define WP(num, word) (num / word + (num % word != 0))
#define MASK_WORDS(d) WP(d->num_resources, MASK_SIZE)

/* Word, bit -> resource id */
#define ri(w, b) (w * MASK_SIZE + b)

 /* For loop, where @i iterates over each set bit in @bit_arr */
#define for_each_resource(bit_arr, d, w, b, i)				\
	for(w = 0; w < MASK_WORDS(d); ++w)				\
		 for(b = find_first_bit(&bit_arr[w],MASK_SIZE), i = ri(w, b);  \
		     b < MASK_SIZE;					       \
		     b = find_next_bit(&bit_arr[w],MASK_SIZE,b+1), i = ri(w, b))

/* Return resource id in dgl @d for resource @r */
#define resource_id(d, r) ((((void*)r) - (void*)((d)->resources))/ sizeof(*r))

/* Return request group of req @r for resource @i */
#define req_group(r, i) (container_of(((void*)r) - sizeof(*r)*(i),	\
				      struct dgl_group_req, requests))

static inline void set_bit_value(int bit, unsigned long *word, int value)
{
	if (value) {
		__set_bit(bit, word);
	} else {
		clear_bit(bit, word);
	}
}

/*
 * Resource id -> word, bit
 */
static inline void mask_idx(int resource, int *word, int *bit)
{
	*word = resource / MASK_SIZE;
	*bit  = resource % MASK_SIZE;
}

/*
 * Logical OR of words in @arr.
 */
static int arr_to_bool(struct dgl *dgl, unsigned long *arr)
{
	int word, ret = 0;
	for (word = 0; word < MASK_WORDS(dgl) && !ret; word++) {
		ret |= arr[word];
	}
	return (ret != 0);
}

#ifdef DEBUG_DGL
#define DEBUG_BUF_LEN 10000
DEFINE_PER_CPU(char *, debug_buf);

static char* print_queue(char *buf, struct dgl *dgl, struct list_head *list)
{
	struct dgl_req *pos;
	struct dgl_group_req *greq;

	list_for_each_entry(pos, list, list) {
		greq = pos->greq;
		buf += sprintf(buf, "(%s/%d:r%d-p%x-b%x)->", greq->task->comm,
				greq->task->pid, pos->replicas,
				greq->need_prio[0], greq->blocked[0]);
	}
	buf += sprintf(buf, "\n");

	return buf;
}

static char* print_resource(char *buf, struct dgl *dgl,
			    struct dgl_resource *resource)
{
	buf += sprintf(buf, "\tResource %d, real_free: %d, goal_free: %d\n",
		resource_id(dgl, resource),
		resource->real_free,
		resource->goal_free);
	buf += sprintf(buf, "\t  acquired:");
	buf  = print_queue(buf, dgl, &resource->acquired);
	buf += sprintf(buf, "\t  will_acquire:");
	buf  = print_queue(buf, dgl, &resource->will_acquire);
	buf += sprintf(buf, "\t  waiting:");
	buf  = print_queue(buf, dgl, &resource->waiting);
	buf += sprintf(buf, "\t  will_wait:");
	buf  = print_queue(buf, dgl, &resource->will_wait);
	return buf;
}


/*
 * Print stats and queues of every resource to the trace log.
 */
static void print_state(struct dgl *dgl)
{
	int i;
	char *buf, *start;
	struct dgl_resource *resource;

	start = __get_cpu_var(debug_buf);
	buf   = start + sprintf(start, "\n\t\tDGL: requests: %d\n", dgl->requests);

	for (i = 0; i < dgl->num_resources; ++i) {
		resource = &dgl->resources[i];

		if (!resource) {
			buf += sprintf(buf, "\tResource %d is null!\n", i);
		}

		if (!list_empty(&resource->waiting)    ||
		    !list_empty(&resource->will_wait)  ||
		    !list_empty(&resource->acquired)   ||
		    !list_empty(&resource->will_acquire)) {

			buf = print_resource(buf, dgl, resource);
		}
	}

	buf += sprintf(buf, "Dump complete\n\n");

	BUG_ON(buf - start > DEBUG_BUF_LEN);
	sched_trace_log_message(start);
}

#define BUG_DUMP(dgl, cond)			\
	do {					\
	if (cond) {				\
		STRACE2("BAD: %s", #cond);	\
		print_state(dgl);		\
		BUG();				\
	}} while(0)
#else
#define BUG_DUMP(dgl, cond) BUG_ON(cond)
#endif

static int higher_prio(struct dgl_group_req *a, struct dgl_group_req *b)
{
	return (a->priority < b->priority ||
		(a->priority == b->priority && (a->task->pid < b->task->pid)));
}

static void release_cpu(struct dgl *dgl, struct dgl_group_req *greq)
{
	int cpu = greq->cpu;

	BUG_DUMP(dgl, dgl->acquired[cpu] != greq);

	dgl->acquired[cpu] = NULL;
}

static void take_cpu(struct dgl *dgl, struct dgl_group_req *greq)
{
	int cpu = greq->cpu;

	BUG_DUMP(dgl, dgl->acquired[cpu]);

	dgl->acquired[cpu] = greq;
}

/*
 * Count lower priority replicas of @greq for @list in @resource.
 * @start   If specified, begin search at location at *@start
 */
static int __get_lp_replicas(struct dgl *dgl, struct dgl_group_req *greq,
			     struct dgl_resource *resource,
			     struct dgl_req **start,
			     struct list_head *list)
{
	int replicas = 0;
	struct list_head *pos_l;
	struct dgl_req *pos;

	pos_l = (*start) ? &(*start)->list : list;
	pos = list_entry(pos_l, struct dgl_req, list);

	list_for_each_entry(pos, list, list) {
		if (higher_prio(pos->greq, greq)) {
			break;
		}
		replicas += pos->replicas;
		BUG_DUMP(dgl, replicas > dgl->num_replicas);
	}

	if (start) {
		*start = pos;
	}

	return replicas;
}

/*
 * Count lower priority replicas of @greq in the acquired and will_acquire
 * queues of @resource.
 * @start_a  Request to start counting replicas in acquired list
 * @start_wa "" "" for will_acquire
 */
static int get_lp_replicas(struct dgl *dgl, struct dgl_group_req *greq,
			   struct dgl_resource *resource,
			   struct dgl_req **start_a, struct dgl_req **start_wa)
{
	int reps = 0;

	reps += __get_lp_replicas(dgl, greq, resource, start_a, &resource->acquired);
	reps += __get_lp_replicas(dgl, greq, resource, start_wa, &resource->will_acquire);

	return reps;
}

/*
 * Add @req to @list in priority order.
 * @reverse  Reverse priority
 */
static void add_request(struct dgl *dgl, struct list_head *list,
			struct dgl_req *req, int reverse)
{
	struct list_head *last;
	struct dgl_req *acquired;
	struct dgl_group_req *greqa, *greqb;

	BUG_DUMP(dgl, in_list(&req->list));

	last = list;
	list_for_each_entry(acquired, list, list) {
		if (!reverse) {
			greqa = req->greq;
			greqb = acquired->greq;
		} else {
			greqa = acquired->greq;
			greqb = req->greq;
		}
		if (higher_prio(greqa, greqb)) {
			break;
		}
		last = &acquired->list;
	}
	list_add(&req->list, last);
}

/*
 * Add @req to a waiting or will_wait list.
 */
static void add_waiting(struct dgl *dgl, struct list_head *list, struct dgl_req *req)
{
	BUG_DUMP(dgl, !arr_to_bool(dgl, req->greq->need_prio));
	add_request(dgl, list, req, 0);
}

/*
 * Add @req to an acquired or will_acquire list.
 */
static void add_acquired(struct dgl *dgl, struct list_head *list, struct dgl_req *req)
{
	BUG_DUMP(dgl, arr_to_bool(dgl, req->greq->need_prio));
	add_request(dgl, list, req, 1);
}

/*
 * Cancel preemption of @greq's requests.
 */
static void will_wait_to_acquire(struct dgl *dgl, struct dgl_group_req *greq)
{
	int w, b, i;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	BUG_DUMP(dgl, arr_to_bool(dgl, greq->need_prio));
	BUG_DUMP(dgl, arr_to_bool(dgl, greq->blocked));
	BUG_DUMP(dgl, dgl->acquired[greq->cpu] != greq);

	TRACE_GREQ(greq, "(WILL_WAIT)->(ACQUIRE)\n");

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		resource->goal_free -= our_req->replicas;

		list_del_init(&our_req->list);
		add_acquired(dgl, &resource->acquired, our_req);

		BUG_DUMP(dgl, resource->goal_free < 0);
	}
}

/*
 * Preempt @greq's requests.
 */
static void acquired_to_will_wait(struct dgl *dgl, struct dgl_group_req *greq,
				  struct dgl_group_req *blocked)
{
	int w, b, i;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	BUG_DUMP(dgl, !arr_to_bool(dgl, greq->need_prio));
	BUG_DUMP(dgl, dgl->acquired[greq->cpu] != greq);

	TRACE_GREQ(greq, "(ACQUIRED)->(WILL_WAIT)\n");

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		resource->goal_free += our_req->replicas;

		list_del_init(&our_req->list);
		add_waiting(dgl, &resource->will_wait, our_req);

		BUG_DUMP(dgl, resource->goal_free > dgl->num_replicas);
	}
}

/*
 * Cancel @greq's resource acquisition.
 */
static void will_acquire_to_waiting(struct dgl *dgl, struct dgl_group_req *greq)
{
	int w, b, i;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	BUG_DUMP(dgl, !arr_to_bool(dgl, greq->need_prio));
	BUG_DUMP(dgl, dgl->acquired[greq->cpu] == greq);

	TRACE_GREQ(greq, "(WILL_ACQUIRE)->(WAITING)\n");

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		resource->goal_free += our_req->replicas;

		list_del_init(&our_req->list);
		add_waiting(dgl, &resource->waiting, our_req);

		BUG_DUMP(dgl, resource->goal_free < 0);
	}
}

#define next_list(list, reverse) ((reverse) ? (list)->prev : (list)->next)

/*
 * @return        Next request from list_a U list_b
 * @reverse_prio  Return lower-priority requests
 * @revearse_read Read lists from end to beginning
 * @tmp_a and @tmp_b store the next item in the list so that the returned
 * item can be safely deleted during iteration
 */
static struct dgl_req *next_union(struct list_head *list_a,
				  struct list_head *list_b,
				  struct list_head **tmp_a,
				  struct list_head **tmp_b,
				  int reverse_prio, int reverse_read)
{
	int higher;
	struct dgl_req *next_a, *next_b;

	next_a = next_b = NULL;

	*tmp_a = (*tmp_a) ? *tmp_a : next_list(list_a, reverse_read);
	*tmp_b = (*tmp_b) ? *tmp_b : next_list(list_b, reverse_read);

	if (*tmp_a != list_a) {
		next_a = list_entry(*tmp_a, struct dgl_req, list);
	}
	if (*tmp_b != list_b) {
		next_b = list_entry(*tmp_b, struct dgl_req, list);
	}

	if (next_a && next_b) {
		higher = higher_prio(next_a->greq, next_b->greq);
		if ((reverse_prio && higher)|| (!reverse_prio && !higher)) {
			*tmp_b = next_list(&next_b->list, reverse_read);
			return next_b;
		} else {
			*tmp_a = next_list(&next_a->list, reverse_read);
			return next_a;
		}
	} else if (next_a) {
		*tmp_a = next_list(&next_a->list, reverse_read);
		return next_a;
	} else if (next_b) {
		*tmp_b = next_list(&next_b->list, reverse_read);
		return next_b;
	} else {
		return NULL;
	}
}

/*
 * @return Next logically waiting request in @resource
 */
static struct dgl_req *next_waiting(struct dgl_resource *resource,
				    struct list_head **pos_a,
				    struct list_head **pos_b,
				    int reverse)
{
	int reverse_prio = (reverse) ? 1 : 0;
	return next_union(&resource->waiting, &resource->will_wait,
			  pos_a, pos_b, reverse_prio, reverse);
}

/*
 * @return Next logically acquired request in @resource
 */
static struct dgl_req *next_acquired(struct dgl_resource *resource,
				     struct list_head **pos_a,
				     struct list_head **pos_b)
{
	return next_union(&resource->acquired, &resource->will_acquire,
			  pos_a, pos_b, 1, 0);
}


/*
 * Recalculate all need_prio bits for requests in @resource.
 */
static void update_prio(struct dgl *dgl, struct dgl_resource *resource)
{
	int lp_replicas, w, b, i;
	struct dgl_req *req, *acq_a, *acq_b;
	struct dgl_group_req *greq;
	struct list_head *tmp_a, *tmp_b;

	lp_replicas = resource->goal_free;
	tmp_a = tmp_b = NULL;
	acq_a = acq_b = NULL;

	i = resource_id(dgl, resource);
	mask_idx(i, &w, &b);

	while ((req = next_waiting(resource, &tmp_a, &tmp_b, 1))) {
		greq = req->greq;

		lp_replicas += get_lp_replicas(dgl, greq, resource,
					       &acq_a, &acq_b);

		set_bit_value(b, &greq->need_prio[w],
			      lp_replicas < req->replicas);

		BUG_DUMP(dgl, lp_replicas > dgl->num_replicas);
	}
}

/*
 * Recalculate all blocked bits for requests in @resource.
 */
static void update_blocked(struct dgl *dgl, struct dgl_resource *resource)
{
	int w, b, i;
	struct dgl_req *req;
	struct dgl_group_req *greq;

	i = resource_id(dgl, resource);
	mask_idx(i, &w, &b);

	list_for_each_entry(req, &resource->will_acquire, list) {
		greq = req->greq;
		set_bit_value(b, &greq->blocked[w],
			      resource->real_free < req->replicas);
	}
}

/*
 * Move @greq from logically running to physically running.
 */
static void will_acquire_to_acquire(struct dgl *dgl, struct dgl_group_req *greq)
{
	int w, b, i;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	BUG_DUMP(dgl, arr_to_bool(dgl, greq->need_prio));
	BUG_DUMP(dgl, arr_to_bool(dgl, greq->blocked));
	BUG_DUMP(dgl, dgl->acquired[greq->cpu] != greq);

	TRACE_GREQ(greq, "(WILL_ACQUIRE)->(ACQUIRE)\n");

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		list_del_init(&our_req->list);
		add_acquired(dgl, &resource->acquired, our_req);

		resource->real_free -= our_req->replicas;
		BUG_DUMP(dgl, (resource->real_free < 0));
	}
}

/*
 * If any requests in @resource are unblocked, schedule them.
 */
static void pull_blocked(struct dgl *dgl, struct dgl_resource *resource)
{
	struct dgl_req *req, *tmp;
	struct dgl_group_req *greq;

	list_for_each_entry_safe(req, tmp, &resource->will_acquire, list) {
		greq = req->greq;
		if (!arr_to_bool(dgl, greq->blocked)) {
			take_cpu(dgl, greq);
			will_acquire_to_acquire(dgl, greq);
			dgl->cpu_acquired(greq);
			update_blocked(dgl, resource);
		}
	}

}

/*
 * Free up enough goal_free in @resource to run @our_req.
 */
static void bump_resource(struct dgl *dgl, struct dgl_resource *resource,
			  struct dgl_group_req *greq, struct dgl_req *our_req)
{
	int w;
	struct list_head *tmp_a, *tmp_b;
	struct dgl_req *next;
	struct dgl_group_req *next_greq;

	tmp_a = tmp_b = NULL;

	while ((next = next_acquired(resource, &tmp_b, &tmp_a))) {
		if (resource->goal_free >= our_req->replicas) {
			break;
		}

		next_greq = next->greq;

		BUG_DUMP(dgl, higher_prio(next_greq, greq));

		for (w = 0; w < MASK_WORDS(dgl); w++) {
			next_greq->need_prio[w] |=
				next_greq->requested[w] &
				greq->requested[w];
		}

		if (dgl->acquired[next_greq->cpu] == next_greq) {
			BUG_DUMP(dgl, dgl->acquired[next_greq->cpu] != next_greq);
				acquired_to_will_wait(dgl, next_greq, greq);
		} else {
			will_acquire_to_waiting(dgl, next_greq);
		}

		dgl->cpu_preempted(next_greq);
	}
}

/*
 * Prepare high-priority request for acquisition.
 */
static void waiting_to_will_acquire(struct dgl *dgl, struct dgl_group_req *greq)
{
	int w, b, i;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	BUG_DUMP(dgl, arr_to_bool(dgl, greq->need_prio));
	BUG_DUMP(dgl, dgl->acquired[greq->cpu] == greq);

	TRACE_GREQ(greq, "(WAITING)->(WILL_ACQUIRE)\n");

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		list_del_init(&our_req->list);

		bump_resource(dgl, resource, greq, our_req);
		resource->goal_free -= our_req->replicas;

		BUG_DUMP(dgl, resource->goal_free < 0);

		add_acquired(dgl, &resource->will_acquire, our_req);
		set_bit_value(b, &greq->blocked[w],
			      resource->real_free < our_req->replicas);
		update_prio(dgl, resource);
	}
}

/*
 * Logically waiting but physically running request moves to physically waiting.
 */
static void will_wait_to_waiting(struct dgl *dgl, struct dgl_group_req *greq)
{
	int w, b, i;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	BUG_DUMP(dgl, !arr_to_bool(dgl, greq->need_prio));
	BUG_DUMP(dgl, dgl->acquired[greq->cpu] == greq);

	TRACE_GREQ(greq, "(WILL_WAIT)->(WAITING)\n");

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		list_del_init(&our_req->list);
		add_waiting(dgl, &resource->waiting, our_req);

		resource->real_free += our_req->replicas;
		update_blocked(dgl, resource);

		BUG_DUMP(dgl, resource->real_free > dgl->num_replicas);
	}

	for_each_resource(greq->requested, dgl, w, b, i) {
		pull_blocked(dgl, &dgl->resources[i]);
	}
}

/*
 * If any requests in @resource have priority to logically acquire, start
 * resource acquisitions.
 */
static void pull_prio(struct dgl *dgl, struct dgl_resource *resource)
{
	struct dgl_req *next;
	struct dgl_group_req *greq;
	struct list_head *tmp_a, *tmp_b;

	tmp_a = tmp_b = NULL;

	while ((next = next_waiting(resource, &tmp_a, &tmp_b, 0))) {
		greq = next->greq;

		if (!arr_to_bool(dgl, greq->need_prio)) {
			if (dgl->acquired[greq->cpu] == greq) {
				will_wait_to_acquire(dgl, greq);
			} else {
				waiting_to_will_acquire(dgl, greq);
			}
			update_prio(dgl, resource);
		}
	}
}

/**
 * add_group_req - put in @greq for resources on @cpu.
 */
void add_group_req(struct dgl *dgl, struct dgl_group_req *greq, int cpu)
{
	int w, b, i, lp_reps;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	greq->cpu = cpu;
	greq->priority = dgl->assign_priority(dgl, greq);

	++dgl->requests;

	TRACE_GREQ(greq, "Group request added for CPU %d\n", cpu);
	BUG_DUMP(dgl, dgl->acquired[cpu] == greq);

	for (w = 0; w < MASK_WORDS(dgl); w++) {
		greq->need_prio[w] = greq->requested[w];
		greq->blocked[w]   = greq->requested[w];
	}

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		resource = &dgl->resources[i];

		add_waiting(dgl, &resource->waiting, our_req);

		lp_reps = get_lp_replicas(dgl, greq, resource, NULL, NULL) +
			resource->goal_free;

		set_bit_value(b, &greq->need_prio[w],
			      lp_reps < our_req->replicas);
	}

	/* Take resources if it has priority */
	if (!arr_to_bool(dgl, greq->need_prio)) {
		for_each_resource(greq->requested, dgl, w, b, i) {
			our_req  = &greq->requests[i];
			resource = &dgl->resources[i];

			pull_prio(dgl, resource);
			set_bit_value(b, &greq->blocked[w],
				      resource->real_free < our_req->replicas);
		}

		/* Take CPUs if its not blocked */
		if (!arr_to_bool(dgl, greq->blocked)) {
			for_each_resource(greq->requested, dgl, w, b, i) {
				pull_blocked(dgl, &dgl->resources[i]);
			}
		}
	}

	print_state(dgl);
}

/**
 * update_group_req - update state of @greq in @dgl.
 */
void update_group_req(struct dgl *dgl, struct dgl_group_req *greq)
{
	if (arr_to_bool(dgl, greq->need_prio) &&
	    !arr_to_bool(dgl, greq->blocked)  &&
	    dgl->acquired[greq->cpu] == greq) {
		TRACE_GREQ(greq, "Blocker has called update\n");
		release_cpu(dgl, greq);
		will_wait_to_waiting(dgl, greq);
		print_state(dgl);
	} else {
		TRACE_GREQ(greq, "Unused update\n");
	}

	BUG_DUMP(dgl, arr_to_bool(dgl, greq->blocked) &&
		 dgl->acquired[greq->cpu] == greq);
}

/**
 * remove_group_req - remove @greq from contention in @dgl.
 */
void remove_group_req(struct dgl *dgl, struct dgl_group_req *greq)
{
	int w, b, i, running, has_prio;
	struct dgl_req *our_req;
	struct dgl_resource *resource;

	TRACE_GREQ(greq, "Removing group request\n");

	running  = dgl->acquired[greq->cpu] == greq;
	has_prio = !arr_to_bool(dgl, greq->need_prio);

	if (dgl->acquired[greq->cpu] == greq) {
		release_cpu(dgl, greq);
	}

	for_each_resource(greq->requested, dgl, w, b, i) {
		our_req  = &greq->requests[i];
		list_del_init(&our_req->list);
		resource = &dgl->resources[i];

		if (running) {
			resource->real_free += our_req->replicas;
			BUG_DUMP(dgl, resource->real_free > dgl->num_replicas);
		}
		if (has_prio) {
			resource->goal_free += our_req->replicas;
			BUG_DUMP(dgl, resource->goal_free > dgl->num_replicas);
		}
	}

	--dgl->requests;

	/* TODO: these updates should be able to move up */
	if (has_prio) {
		for_each_resource(greq->requested, dgl, w, b, i) {
			update_prio(dgl, &dgl->resources[i]);
		}
		for_each_resource(greq->requested, dgl, w, b, i) {
			pull_prio(dgl, &dgl->resources[i]);
		}
	}

	if (running || has_prio) {
		for_each_resource(greq->requested, dgl, w, b, i) {
			update_blocked(dgl, &dgl->resources[i]);
		}
		for_each_resource(greq->requested, dgl, w, b, i) {
			pull_blocked(dgl, &dgl->resources[i]);
		}
	}

	print_state(dgl);
}

/**
 * set_req - create request for @replicas of @resource.
 */
void set_req(struct dgl *dgl, struct dgl_group_req *greq,
	     int resource, int replicas)
{
	int word, bit;
	struct dgl_req *req;

	if (replicas > dgl->num_replicas)
		replicas = dgl->num_replicas;

	mask_idx(resource, &word, &bit);
	__set_bit(bit, &greq->requested[word]);

	TRACE_GREQ(greq, "requesting %d of %d\n", replicas, resource);

	req = &greq->requests[resource];

	req->greq = greq;
	req->replicas = replicas;

	INIT_LIST_HEAD(&req->list);
	BUG_DUMP(dgl, in_list(&req->list));
}

static void dummy_acquired(struct dgl_group_req *greq){}

static unsigned long long timestamp_prio (struct dgl *dgl, struct dgl_group_req *greq)
{
	return dgl->ts++;
}

void dgl_init(struct dgl *dgl, unsigned long num_resources,
	      unsigned long num_replicas)
{
	int i;
	struct dgl_resource *resource;

	dgl->num_replicas  = num_replicas;
	dgl->num_resources = num_resources;

	dgl->resources = kmalloc(sizeof(*dgl->resources) * num_resources,
				 GFP_ATOMIC);
	dgl->acquired  = kmalloc(sizeof(*dgl->acquired) * num_online_cpus(),
				 GFP_ATOMIC);

	for (i = 0; i < num_online_cpus(); ++i)
		dgl->acquired[i] = NULL;

	for (i = 0; i < num_resources; i++) {
		resource = &dgl->resources[i];

		INIT_LIST_HEAD(&resource->waiting);
		INIT_LIST_HEAD(&resource->acquired);
		INIT_LIST_HEAD(&resource->will_wait);
		INIT_LIST_HEAD(&resource->will_acquire);
		resource->real_free = dgl->num_replicas;
		resource->goal_free = dgl->num_replicas;
	}

	dgl->requests = 0;
	dgl->ts = 0;
	dgl->cpu_acquired = dummy_acquired;
	dgl->cpu_preempted = NULL;
	dgl->assign_priority = timestamp_prio;
}

void dgl_free(struct dgl *dgl)
{
	kfree(dgl->resources);
	kfree(dgl->acquired);
}

void dgl_group_req_init(struct dgl *dgl, struct dgl_group_req *greq)
{
	int i;

	greq->requested = kmalloc(sizeof(*greq->requested) * MASK_WORDS(dgl),
				  GFP_ATOMIC);
	greq->need_prio = kmalloc(sizeof(*greq->need_prio) * MASK_WORDS(dgl),
				  GFP_ATOMIC);
	greq->blocked   = kmalloc(sizeof(*greq->need_prio) * MASK_WORDS(dgl),
				  GFP_ATOMIC);
	greq->requests  = kmalloc(sizeof(*greq->requests) * dgl->num_resources,
				  GFP_ATOMIC);

	BUG_DUMP(dgl, !greq->requested);
	BUG_DUMP(dgl, !greq->need_prio);
	BUG_DUMP(dgl, !greq->requests);

	greq->cpu = NO_CPU;
	for (i = 0; i < MASK_WORDS(dgl); ++i) {
		greq->requested[i] = 0;
		greq->need_prio[i] = 0;
	}
}

void dgl_group_req_free(struct dgl_group_req *greq)
{
	kfree(greq->requested);
	kfree(greq->need_prio);
	kfree(greq->requests);
}

#ifdef DEBUG_DGL
static int __init init_dgl_debug(void)
{
	int cpu;
	char **buf;

	for_each_online_cpu(cpu) {
		buf = &per_cpu(debug_buf, cpu);
		*buf = kmalloc(DEBUG_BUF_LEN * sizeof(char), GFP_ATOMIC);
	}

	return 0;
}

module_init(init_dgl_debug);

#endif