aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/bank_proc.c
blob: 07d572833b309f8782b8aafb103e9b9bbf9654bc (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
#include <linux/init.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sysctl.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/random.h>

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

#define LITMUS_LOCKDEP_NAME_MAX_LEN 50

// This is Address Decoding for imx6-sabredsd board
#define CACHE_MASK 0x0000f000      
#define BANK_MASK  0x00007000      
#define OFFSET_SHIFT 12

#define PAGES_PER_COLOR 1024

unsigned long number_banks;
unsigned long number_cachecolors;

struct color_group {
	spinlock_t lock;
	char _lock_name[LITMUS_LOCKDEP_NAME_MAX_LEN];
	struct list_head list;
	atomic_t nr_pages;
};

static struct alloced_pages {
	spinlock_t lock;
	struct list_head list;
} alloced_pages;

struct alloced_page {
	struct page *page;
	struct vm_area_struct *vma;
	struct list_head list;
};

static struct color_group *color_groups;
static struct lock_class_key color_lock_keys[16];

//static struct color_group *color_groups;

/* Decoding page color, 0~15 */ 
static inline unsigned long page_color(struct page *page)
{
	return ((page_to_phys(page)& CACHE_MASK) >> PAGE_SHIFT);
}

/* Decoding page bank number, 0~7 */ 
static inline unsigned long page_bank(struct page *page)
{
	return ((page_to_phys(page)& BANK_MASK) >> PAGE_SHIFT);
}

static unsigned long smallest_nr_pages(void)
{
	unsigned long i, min_pages = -1;
	struct color_group *cgroup;
	for (i = 0; i < number_cachecolors; ++i) {
		cgroup = &color_groups[i];
		if (atomic_read(&cgroup->nr_pages) < min_pages)
			min_pages = atomic_read(&cgroup->nr_pages);
	}
	return min_pages;
}
/*
 * Page's count should be one, it sould not be on any LRU list.
 */
void add_page_to_color_list(struct page *page)
{
	const unsigned long color = page_color(page);
	struct color_group *cgroup = &color_groups[color];
	BUG_ON(in_list(&page->lru) || PageLRU(page));
	BUG_ON(page_count(page) > 1);
	spin_lock(&cgroup->lock);
	list_add_tail(&page->lru, &cgroup->list);
	atomic_inc(&cgroup->nr_pages);
//	SetPageLRU(page);
	spin_unlock(&cgroup->lock);
}

static int do_add_pages(void)
{
	//printk("LITMUS do add pages\n");
	
	struct page *page, *page_tmp;
	LIST_HEAD(free_later);
	unsigned long color;
	int ret = 0;

	while (smallest_nr_pages() < PAGES_PER_COLOR) {
	
		//page = alloc_page(GFP_HIGHUSER | __GFP_MOVABLE);
		page = alloc_page(GFP_HIGHUSER_MOVABLE);
		
		if (unlikely(!page)) {
			printk(KERN_WARNING "Could not allocate pages.\n");
			ret = -ENOMEM;
			goto out;
		}
		color = page_color(page);
		if (atomic_read(&color_groups[color].nr_pages) < PAGES_PER_COLOR) {
	//		SetPageReserved(page);
			add_page_to_color_list(page);
		} else
			list_add_tail(&page->lru, &free_later);
	}
	list_for_each_entry_safe(page, page_tmp, &free_later, lru) {
		list_del(&page->lru);
		__free_page(page);
	}
	/* setup the color queue stuff */
//	ret = setup_flusher_array();
out:
	return ret;
}

extern int l2_usable_sets;

/*
 * provide pages for replacement 
 * node = 0 for Level A, B tasks in Cpu 0
 * node = 1 for Level A, B tasks in Cpu 1
 * node = 2 for Level A, B tasks in Cpu 2
 * node = 3 for Level A, B tasks in Cpu 3
 * node = 4 for Level C tasks 
 */
#if 1
struct page *new_alloc_page(struct page *page, unsigned long node, int **x)
{
	//printk("allocate new page node = %d\n", node);	
//	return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
	struct color_group *cgroup;
	struct page *rPage = NULL;
	unsigned int color;
	get_random_bytes(&color, sizeof(unsigned int));
	
	/*
	if(node ==0){
		color = (color%2)*8+node;
	}else if(node == 1){
		color = (color%2)*8+node;
	}else if(node == 2){
		color = (color%2)*8+;
	}else if(node == 3){
		color = color%2 + 6;
	}else if(node == 4){
		color = color%8 + 8;
	}else{
		goto out;
	}
	*/
	switch(node ){
		case 0:
			color = (color % l2_usable_sets);
			break;
		case 1: 
		case 2: 
		case 3:
		case 4:
			color = (color% (16-l2_usable_sets)) + l2_usable_sets;
			break;
		default:
			goto out;
	}
	/*
	switch(node ){
		case 0:
		case 1: 
		case 2: 
		case 3:
			color = (color%2) * 8 + node;
			break;
		case 4:
			color = (color%8)+4;
			if(color >=8)	
				color+=4;
			break;
		default:
			goto out;
	}
	*/

	//printk("allocate new page color = %d\n", color);
	//TRACE("allocate new page color = %d\n", color);
		
	cgroup = &color_groups[color];
	spin_lock(&cgroup->lock);
	if (unlikely(!atomic_read(&cgroup->nr_pages))) {
		//TRACE_CUR("No free %lu colored pages.\n", color);
		printk(KERN_WARNING "no free %lu colored pages.\n", color);
		goto out_unlock;
	}
	rPage = list_first_entry(&cgroup->list, struct page, lru);
	BUG_ON(page_count(rPage) > 1);
	get_page(rPage);
	list_del(&rPage->lru);
	atomic_dec(&cgroup->nr_pages);
//	ClearPageLRU(rPage);
out_unlock:
	spin_unlock(&cgroup->lock);
out:
	do_add_pages();
	return rPage;
}
#endif

struct page *new_alloc_page_banknr(struct page *page, unsigned long banknr, int **x)
{
	printk("allocate new page bank = %d\n", banknr);	
	struct color_group *cgroup;
	struct page *rPage = NULL;
	unsigned int color;
	get_random_bytes(&color, sizeof(unsigned int));
	
	if((banknr<= 7) && (banknr>=0)){
		color = (color%2) * 8 + banknr;
	}else{
		goto out;
	}
		
	cgroup = &color_groups[color];
	spin_lock(&cgroup->lock);
	if (unlikely(!atomic_read(&cgroup->nr_pages))) {
		TRACE_CUR("No free %lu colored pages.\n", color);
		printk(KERN_WARNING "no free %lu colored pages.\n", color);
		goto out_unlock;
	}
	rPage = list_first_entry(&cgroup->list, struct page, lru);
	BUG_ON(page_count(rPage) > 1);
	get_page(rPage);
	list_del(&rPage->lru);
	atomic_dec(&cgroup->nr_pages);
//	ClearPageLRU(rPage);
out_unlock:
	spin_unlock(&cgroup->lock);
out:
	do_add_pages();
	return rPage;



}

static int __init init_variables(void)
{
	number_banks = 1+(BANK_MASK >> PAGE_SHIFT); 
	number_cachecolors = 1+(CACHE_MASK >> PAGE_SHIFT);
}



static int __init init_color_groups(void)
{
	struct color_group *cgroup;
	unsigned long i;
	int err = 0;

	color_groups = kmalloc(number_cachecolors *
			sizeof(struct color_group), GFP_KERNEL);
	if (!color_groups) {
		printk(KERN_WARNING "Could not allocate color groups.\n");
		err = -ENOMEM;
	}else{

		for (i = 0; i < number_cachecolors; ++i) {
			cgroup = &color_groups[i];
			atomic_set(&cgroup->nr_pages, 0);
			INIT_LIST_HEAD(&cgroup->list);
			spin_lock_init(&cgroup->lock);
//			LOCKDEP_DYNAMIC_ALLOC(&cgroup->lock, &color_lock_keys[i],
//					cgroup->_lock_name, "color%lu", i);
		}
	}
	return err;
}

/*
 * Initialzie the this proc 
 */
static int __init litmus_color_init(void)
{
	int err=0;
	
	INIT_LIST_HEAD(&alloced_pages.list);
	spin_lock_init(&alloced_pages.lock);
	init_variables();
	printk("Cache number = %d , Cache mask = 0x%lx\n", number_cachecolors, CACHE_MASK); 
	printk("Bank number = %d , Bank mask = 0x%lx\n", number_banks, BANK_MASK); 
	init_color_groups();			
	do_add_pages();

	printk(KERN_INFO "Registering LITMUS^RT color and bank proc.\n");
	return err;
}

module_init(litmus_color_init);