-
Notifications
You must be signed in to change notification settings - Fork 4
/
2011-2183.cpp
338 lines (275 loc) · 8.24 KB
/
2011-2183.cpp
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
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
// #define TEST_TIME
#define POISON_POINTER_DELTA 0
#define GOLDEN_RATIO_PRIME_32 0x9e370001UL
#define MMF_VM_MERGEABLE 16
#define LIST_POISON1 ((void *) (0x00100100 + POISON_POINTER_DELTA))
#define LIST_POISON2 ((void *) (0x00200200 + POISON_POINTER_DELTA))
typedef pthread_mutex_t spinlock_t;
typedef unsigned int u32;
typedef __signed__ int __s32;
struct list_head {
struct list_head *next, *prev;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
struct hlist_head {
struct hlist_node *first;
};
struct rmap_item {
struct rmap_item *rmap_list;
};
struct rw_semaphore {
__s32 activity;
spinlock_t wait_lock;
struct list_head wait_list;
};
struct mm_struct {
struct rw_semaphore mmap_sem;
unsigned long flags;
};
struct mm_slot {
struct hlist_node link;
struct list_head mm_list;
struct rmap_item *rmap_list;
struct mm_struct *mm;
};
struct ksm_scan {
struct mm_slot *mm_slot;
unsigned long address;
struct rmap_item **rmap_list;
unsigned long seqnr;
};
spinlock_t ksm_mmlist_lock;
#define MM_SLOTS_HASH_SHIFT 10
#define MM_SLOTS_HASH_HEADS (1 << MM_SLOTS_HASH_SHIFT)
static struct hlist_head mm_slots_hash[MM_SLOTS_HASH_HEADS];
static struct mm_slot ksm_mm_head;
#define LIST_HEAD_INIT(name) { &(name), &(name) }
// static struct mm_slot ksm_mm_head = {
// .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list)
// };
static struct ksm_scan ksm_scan = {
.mm_slot = &ksm_mm_head
};
#define spin_lock(lock) pthread_mutex_lock(lock)
#define spin_unlock(lock) pthread_mutex_unlock(lock)
#define typeof(a) __typeof__(a)
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
#define list_entry(link, type, member) \
((type *)((char *)(link)-(unsigned long)(&((type *)0)->member)))
static inline void __list_add(struct list_head *neww,
struct list_head *prev, struct list_head *next)
{
next->prev = neww;
neww->next = next;
neww->prev = prev;
prev->next = neww;
}
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_add(struct list_head *neww, struct list_head *head)
{
__list_add(neww, head, head->next);
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (list_head *)LIST_POISON1;
entry->prev = (list_head *)LIST_POISON2;
}
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
*pprev = next;
if (next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = (hlist_node *)LIST_POISON1;
n->pprev = (hlist_node **)LIST_POISON2;
}
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *) &((type *)0)->member)
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each_entry(tpos, pos, head, member) \
for (pos = (head)->first; \
pos && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
#define hash_long(val, bits) hash_32(val, bits)
static inline u32 hash_32(u32 val, unsigned int bits)
{
u32 hash = val * GOLDEN_RATIO_PRIME_32;
return hash >> (32 - bits);
}
static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
{
return hash_long((unsigned long)ptr, bits);
}
void down_read(struct rw_semaphore *sem){}
void down_write(struct rw_semaphore *sem){}
void up_read(struct rw_semaphore *sem){}
void up_write(struct rw_semaphore *sem){}
static inline void mmdrop(struct mm_struct * mm){}
static inline void clear_bit(unsigned long nr, volatile void * addr){}
static inline void free_mm_slot(struct mm_slot *mm_slot){}
static void insert_to_mm_slots_hash(struct mm_struct *mm,
struct mm_slot *mm_slot)
{
struct hlist_head *bucket;
bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)];
mm_slot->mm = mm;
hlist_add_head(&mm_slot->link, bucket);
}
static struct mm_slot *get_mm_slot(struct mm_struct *mm)
{
struct mm_slot *mm_slot;
struct hlist_head *bucket;
struct hlist_node *node;
bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)];
hlist_for_each_entry(mm_slot, node, bucket, link) {
if (mm == mm_slot->mm)
return mm_slot;
}
return NULL;
}
static struct rmap_item *scan_get_next_rmap_item()
{
struct mm_struct *mm;
struct mm_slot *slot;
if (list_empty(&ksm_mm_head.mm_list))
return NULL;
slot = ksm_scan.mm_slot;
if (slot == &ksm_mm_head) {
spin_lock(&ksm_mmlist_lock);
slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
ksm_scan.mm_slot = slot;
spin_unlock(&ksm_mmlist_lock);
next_mm:
ksm_scan.address = 0;
ksm_scan.rmap_list = &slot->rmap_list;
}
mm = slot->mm;
printf("thread 1, slot->mm = %p\n", mm);
down_read(&mm->mmap_sem);
printf("thread 1, mm->mmap_sem = %p\n", &mm->mmap_sem);
spin_lock(&ksm_mmlist_lock);
ksm_scan.mm_slot = list_entry(slot->mm_list.next,
struct mm_slot, mm_list);
if (ksm_scan.address == 0) {
printf("thread 1, ksm_scan.address = 0\n");
hlist_del(&slot->link);
list_del(&slot->mm_list);
spin_unlock(&ksm_mmlist_lock);
free_mm_slot(slot);
clear_bit(MMF_VM_MERGEABLE, &mm->flags);
up_read(&mm->mmap_sem);
mmdrop(mm);
} else {
spin_unlock(&ksm_mmlist_lock);
up_read(&mm->mmap_sem);
}
slot = ksm_scan.mm_slot;
if (slot != &ksm_mm_head)
goto next_mm;
ksm_scan.seqnr++;
return NULL;
}
void __ksm_exit(struct mm_struct *mm)
{
struct mm_slot *mm_slot;
int easy_to_free = 0;
spin_lock(&ksm_mmlist_lock);
mm_slot = get_mm_slot(mm);
printf("thread 2, mm_slot = %p, mm_slot.mm_list = %p, ksm_scan.mm_slot = %p\n", mm_slot, &mm_slot->mm_list, ksm_scan.mm_slot);
if (mm_slot && ksm_scan.mm_slot != mm_slot) {
if (!mm_slot->rmap_list) {
hlist_del(&mm_slot->link);
list_del(&mm_slot->mm_list);
easy_to_free = 1;
printf("thread 2, delete\n");
} else {
printf("thread 2, else\n");
list_move(&mm_slot->mm_list,
&ksm_scan.mm_slot->mm_list);
}
}
spin_unlock(&ksm_mmlist_lock);
if (easy_to_free) {
printf("thread 2, if\n");
free_mm_slot(mm_slot);
clear_bit(MMF_VM_MERGEABLE, &mm->flags);
mmdrop(mm);
} else if (mm_slot) {
printf("thread 2, else if\n");
down_write(&mm->mmap_sem);
up_write(&mm->mmap_sem);
}
}
void * thread_one(void *args){
scan_get_next_rmap_item();
return NULL;
}
void * thread_two(void *args){
__ksm_exit((mm_struct *)args);
return NULL;
}
int main(){
#ifdef TEST_TIME
static double run_time_begin;
static double run_time_end;
static double run_time_total;
run_time_begin = clock();
#endif
ksm_mm_head.mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list);
printf("thread main, ksm_mm_head.mm_list = %p, ksm_mm_head.mm_list.prev = %p, ksm_mm_head.mm_list.next = %p\n", &ksm_mm_head.mm_list, ksm_mm_head.mm_list.prev, ksm_mm_head.mm_list.next);
struct mm_struct mm_test;
struct mm_slot slot_test;
slot_test.rmap_list = NULL;
insert_to_mm_slots_hash(&mm_test, &slot_test);
list_add(&slot_test.mm_list, &ksm_scan.mm_slot->mm_list);
printf("thread main, ksm_mm_head.mm_list = %p, ksm_mm_head.mm_list.prev = %p, ksm_mm_head.mm_list.next = %p\n", &ksm_mm_head.mm_list, ksm_mm_head.mm_list.prev, ksm_mm_head.mm_list.next);
printf("thread main, slot_test.mm = %p, mm_test = %p\n", slot_test.mm, &mm_test);
pthread_mutex_init(&ksm_mmlist_lock, NULL);
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_one, NULL);
pthread_create(&t2, NULL, thread_two, &mm_test);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("thread main, ksm_mm_head.mm_list = %p, ksm_mm_head.mm_list.prev = %p, ksm_mm_head.mm_list.next = %p\n", &ksm_mm_head.mm_list, ksm_mm_head.mm_list.prev, ksm_mm_head.mm_list.next);
printf("\nprogram-successful-exit\n");
#ifdef TEST_TIME
run_time_end = clock();
run_time_total = run_time_end - run_time_begin;
printf("test-the-total-time: %.3lf\n", (double)(run_time_total/CLOCKS_PER_SEC)*1000);
#endif
return 0;
}