]>
Commit | Line | Data |
---|---|---|
f8af4da3 | 1 | /* |
31dbd01f IE |
2 | * Memory merging support. |
3 | * | |
4 | * This code enables dynamic sharing of identical pages found in different | |
5 | * memory areas, even if they are not shared by fork() | |
6 | * | |
36b2528d | 7 | * Copyright (C) 2008-2009 Red Hat, Inc. |
31dbd01f IE |
8 | * Authors: |
9 | * Izik Eidus | |
10 | * Andrea Arcangeli | |
11 | * Chris Wright | |
36b2528d | 12 | * Hugh Dickins |
31dbd01f IE |
13 | * |
14 | * This work is licensed under the terms of the GNU GPL, version 2. | |
f8af4da3 HD |
15 | */ |
16 | ||
17 | #include <linux/errno.h> | |
31dbd01f IE |
18 | #include <linux/mm.h> |
19 | #include <linux/fs.h> | |
f8af4da3 | 20 | #include <linux/mman.h> |
31dbd01f IE |
21 | #include <linux/sched.h> |
22 | #include <linux/rwsem.h> | |
23 | #include <linux/pagemap.h> | |
24 | #include <linux/rmap.h> | |
25 | #include <linux/spinlock.h> | |
26 | #include <linux/jhash.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/kthread.h> | |
29 | #include <linux/wait.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/rbtree.h> | |
62b61f61 | 32 | #include <linux/memory.h> |
31dbd01f | 33 | #include <linux/mmu_notifier.h> |
2c6854fd | 34 | #include <linux/swap.h> |
f8af4da3 | 35 | #include <linux/ksm.h> |
4ca3a69b | 36 | #include <linux/hashtable.h> |
878aee7d | 37 | #include <linux/freezer.h> |
72788c38 | 38 | #include <linux/oom.h> |
90bd6fd3 | 39 | #include <linux/numa.h> |
f8af4da3 | 40 | |
31dbd01f | 41 | #include <asm/tlbflush.h> |
73848b46 | 42 | #include "internal.h" |
31dbd01f | 43 | |
e850dcf5 HD |
44 | #ifdef CONFIG_NUMA |
45 | #define NUMA(x) (x) | |
46 | #define DO_NUMA(x) do { (x); } while (0) | |
47 | #else | |
48 | #define NUMA(x) (0) | |
49 | #define DO_NUMA(x) do { } while (0) | |
50 | #endif | |
51 | ||
31dbd01f IE |
52 | /* |
53 | * A few notes about the KSM scanning process, | |
54 | * to make it easier to understand the data structures below: | |
55 | * | |
56 | * In order to reduce excessive scanning, KSM sorts the memory pages by their | |
57 | * contents into a data structure that holds pointers to the pages' locations. | |
58 | * | |
59 | * Since the contents of the pages may change at any moment, KSM cannot just | |
60 | * insert the pages into a normal sorted tree and expect it to find anything. | |
61 | * Therefore KSM uses two data structures - the stable and the unstable tree. | |
62 | * | |
63 | * The stable tree holds pointers to all the merged pages (ksm pages), sorted | |
64 | * by their contents. Because each such page is write-protected, searching on | |
65 | * this tree is fully assured to be working (except when pages are unmapped), | |
66 | * and therefore this tree is called the stable tree. | |
67 | * | |
68 | * In addition to the stable tree, KSM uses a second data structure called the | |
69 | * unstable tree: this tree holds pointers to pages which have been found to | |
70 | * be "unchanged for a period of time". The unstable tree sorts these pages | |
71 | * by their contents, but since they are not write-protected, KSM cannot rely | |
72 | * upon the unstable tree to work correctly - the unstable tree is liable to | |
73 | * be corrupted as its contents are modified, and so it is called unstable. | |
74 | * | |
75 | * KSM solves this problem by several techniques: | |
76 | * | |
77 | * 1) The unstable tree is flushed every time KSM completes scanning all | |
78 | * memory areas, and then the tree is rebuilt again from the beginning. | |
79 | * 2) KSM will only insert into the unstable tree, pages whose hash value | |
80 | * has not changed since the previous scan of all memory areas. | |
81 | * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the | |
82 | * colors of the nodes and not on their contents, assuring that even when | |
83 | * the tree gets "corrupted" it won't get out of balance, so scanning time | |
84 | * remains the same (also, searching and inserting nodes in an rbtree uses | |
85 | * the same algorithm, so we have no overhead when we flush and rebuild). | |
86 | * 4) KSM never flushes the stable tree, which means that even if it were to | |
87 | * take 10 attempts to find a page in the unstable tree, once it is found, | |
88 | * it is secured in the stable tree. (When we scan a new page, we first | |
89 | * compare it against the stable tree, and then against the unstable tree.) | |
90 | */ | |
91 | ||
92 | /** | |
93 | * struct mm_slot - ksm information per mm that is being scanned | |
94 | * @link: link to the mm_slots hash list | |
95 | * @mm_list: link into the mm_slots list, rooted in ksm_mm_head | |
6514d511 | 96 | * @rmap_list: head for this mm_slot's singly-linked list of rmap_items |
31dbd01f IE |
97 | * @mm: the mm that this information is valid for |
98 | */ | |
99 | struct mm_slot { | |
100 | struct hlist_node link; | |
101 | struct list_head mm_list; | |
6514d511 | 102 | struct rmap_item *rmap_list; |
31dbd01f IE |
103 | struct mm_struct *mm; |
104 | }; | |
105 | ||
106 | /** | |
107 | * struct ksm_scan - cursor for scanning | |
108 | * @mm_slot: the current mm_slot we are scanning | |
109 | * @address: the next address inside that to be scanned | |
6514d511 | 110 | * @rmap_list: link to the next rmap to be scanned in the rmap_list |
31dbd01f IE |
111 | * @seqnr: count of completed full scans (needed when removing unstable node) |
112 | * | |
113 | * There is only the one ksm_scan instance of this cursor structure. | |
114 | */ | |
115 | struct ksm_scan { | |
116 | struct mm_slot *mm_slot; | |
117 | unsigned long address; | |
6514d511 | 118 | struct rmap_item **rmap_list; |
31dbd01f IE |
119 | unsigned long seqnr; |
120 | }; | |
121 | ||
7b6ba2c7 HD |
122 | /** |
123 | * struct stable_node - node of the stable rbtree | |
124 | * @node: rb node of this ksm page in the stable tree | |
125 | * @hlist: hlist head of rmap_items using this ksm page | |
62b61f61 | 126 | * @kpfn: page frame number of this ksm page |
7b6ba2c7 HD |
127 | */ |
128 | struct stable_node { | |
129 | struct rb_node node; | |
130 | struct hlist_head hlist; | |
62b61f61 | 131 | unsigned long kpfn; |
7b6ba2c7 HD |
132 | }; |
133 | ||
31dbd01f IE |
134 | /** |
135 | * struct rmap_item - reverse mapping item for virtual addresses | |
6514d511 | 136 | * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list |
db114b83 | 137 | * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree |
31dbd01f IE |
138 | * @mm: the memory structure this rmap_item is pointing into |
139 | * @address: the virtual address this rmap_item tracks (+ flags in low bits) | |
140 | * @oldchecksum: previous checksum of the page at that virtual address | |
e850dcf5 | 141 | * @nid: NUMA node id of unstable tree in which linked (may not match page) |
7b6ba2c7 HD |
142 | * @node: rb node of this rmap_item in the unstable tree |
143 | * @head: pointer to stable_node heading this list in the stable tree | |
144 | * @hlist: link into hlist of rmap_items hanging off that stable_node | |
31dbd01f IE |
145 | */ |
146 | struct rmap_item { | |
6514d511 | 147 | struct rmap_item *rmap_list; |
db114b83 | 148 | struct anon_vma *anon_vma; /* when stable */ |
31dbd01f IE |
149 | struct mm_struct *mm; |
150 | unsigned long address; /* + low bits used for flags below */ | |
7b6ba2c7 | 151 | unsigned int oldchecksum; /* when unstable */ |
90bd6fd3 | 152 | #ifdef CONFIG_NUMA |
e850dcf5 | 153 | int nid; |
90bd6fd3 | 154 | #endif |
31dbd01f | 155 | union { |
7b6ba2c7 HD |
156 | struct rb_node node; /* when node of unstable tree */ |
157 | struct { /* when listed from stable tree */ | |
158 | struct stable_node *head; | |
159 | struct hlist_node hlist; | |
160 | }; | |
31dbd01f IE |
161 | }; |
162 | }; | |
163 | ||
164 | #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */ | |
7b6ba2c7 HD |
165 | #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */ |
166 | #define STABLE_FLAG 0x200 /* is listed from the stable tree */ | |
31dbd01f IE |
167 | |
168 | /* The stable and unstable tree heads */ | |
90bd6fd3 PH |
169 | static struct rb_root root_unstable_tree[MAX_NUMNODES]; |
170 | static struct rb_root root_stable_tree[MAX_NUMNODES]; | |
31dbd01f | 171 | |
4ca3a69b SL |
172 | #define MM_SLOTS_HASH_BITS 10 |
173 | static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); | |
31dbd01f IE |
174 | |
175 | static struct mm_slot ksm_mm_head = { | |
176 | .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list), | |
177 | }; | |
178 | static struct ksm_scan ksm_scan = { | |
179 | .mm_slot = &ksm_mm_head, | |
180 | }; | |
181 | ||
182 | static struct kmem_cache *rmap_item_cache; | |
7b6ba2c7 | 183 | static struct kmem_cache *stable_node_cache; |
31dbd01f IE |
184 | static struct kmem_cache *mm_slot_cache; |
185 | ||
186 | /* The number of nodes in the stable tree */ | |
b4028260 | 187 | static unsigned long ksm_pages_shared; |
31dbd01f | 188 | |
e178dfde | 189 | /* The number of page slots additionally sharing those nodes */ |
b4028260 | 190 | static unsigned long ksm_pages_sharing; |
31dbd01f | 191 | |
473b0ce4 HD |
192 | /* The number of nodes in the unstable tree */ |
193 | static unsigned long ksm_pages_unshared; | |
194 | ||
195 | /* The number of rmap_items in use: to calculate pages_volatile */ | |
196 | static unsigned long ksm_rmap_items; | |
197 | ||
31dbd01f | 198 | /* Number of pages ksmd should scan in one batch */ |
2c6854fd | 199 | static unsigned int ksm_thread_pages_to_scan = 100; |
31dbd01f IE |
200 | |
201 | /* Milliseconds ksmd should sleep between batches */ | |
2ffd8679 | 202 | static unsigned int ksm_thread_sleep_millisecs = 20; |
31dbd01f | 203 | |
e850dcf5 | 204 | #ifdef CONFIG_NUMA |
90bd6fd3 PH |
205 | /* Zeroed when merging across nodes is not allowed */ |
206 | static unsigned int ksm_merge_across_nodes = 1; | |
e850dcf5 HD |
207 | #else |
208 | #define ksm_merge_across_nodes 1U | |
209 | #endif | |
90bd6fd3 | 210 | |
31dbd01f IE |
211 | #define KSM_RUN_STOP 0 |
212 | #define KSM_RUN_MERGE 1 | |
213 | #define KSM_RUN_UNMERGE 2 | |
2c6854fd | 214 | static unsigned int ksm_run = KSM_RUN_STOP; |
31dbd01f IE |
215 | |
216 | static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait); | |
217 | static DEFINE_MUTEX(ksm_thread_mutex); | |
218 | static DEFINE_SPINLOCK(ksm_mmlist_lock); | |
219 | ||
220 | #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\ | |
221 | sizeof(struct __struct), __alignof__(struct __struct),\ | |
222 | (__flags), NULL) | |
223 | ||
224 | static int __init ksm_slab_init(void) | |
225 | { | |
226 | rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0); | |
227 | if (!rmap_item_cache) | |
228 | goto out; | |
229 | ||
7b6ba2c7 HD |
230 | stable_node_cache = KSM_KMEM_CACHE(stable_node, 0); |
231 | if (!stable_node_cache) | |
232 | goto out_free1; | |
233 | ||
31dbd01f IE |
234 | mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0); |
235 | if (!mm_slot_cache) | |
7b6ba2c7 | 236 | goto out_free2; |
31dbd01f IE |
237 | |
238 | return 0; | |
239 | ||
7b6ba2c7 HD |
240 | out_free2: |
241 | kmem_cache_destroy(stable_node_cache); | |
242 | out_free1: | |
31dbd01f IE |
243 | kmem_cache_destroy(rmap_item_cache); |
244 | out: | |
245 | return -ENOMEM; | |
246 | } | |
247 | ||
248 | static void __init ksm_slab_free(void) | |
249 | { | |
250 | kmem_cache_destroy(mm_slot_cache); | |
7b6ba2c7 | 251 | kmem_cache_destroy(stable_node_cache); |
31dbd01f IE |
252 | kmem_cache_destroy(rmap_item_cache); |
253 | mm_slot_cache = NULL; | |
254 | } | |
255 | ||
256 | static inline struct rmap_item *alloc_rmap_item(void) | |
257 | { | |
473b0ce4 HD |
258 | struct rmap_item *rmap_item; |
259 | ||
260 | rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL); | |
261 | if (rmap_item) | |
262 | ksm_rmap_items++; | |
263 | return rmap_item; | |
31dbd01f IE |
264 | } |
265 | ||
266 | static inline void free_rmap_item(struct rmap_item *rmap_item) | |
267 | { | |
473b0ce4 | 268 | ksm_rmap_items--; |
31dbd01f IE |
269 | rmap_item->mm = NULL; /* debug safety */ |
270 | kmem_cache_free(rmap_item_cache, rmap_item); | |
271 | } | |
272 | ||
7b6ba2c7 HD |
273 | static inline struct stable_node *alloc_stable_node(void) |
274 | { | |
275 | return kmem_cache_alloc(stable_node_cache, GFP_KERNEL); | |
276 | } | |
277 | ||
278 | static inline void free_stable_node(struct stable_node *stable_node) | |
279 | { | |
280 | kmem_cache_free(stable_node_cache, stable_node); | |
281 | } | |
282 | ||
31dbd01f IE |
283 | static inline struct mm_slot *alloc_mm_slot(void) |
284 | { | |
285 | if (!mm_slot_cache) /* initialization failed */ | |
286 | return NULL; | |
287 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); | |
288 | } | |
289 | ||
290 | static inline void free_mm_slot(struct mm_slot *mm_slot) | |
291 | { | |
292 | kmem_cache_free(mm_slot_cache, mm_slot); | |
293 | } | |
294 | ||
31dbd01f IE |
295 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
296 | { | |
31dbd01f | 297 | struct hlist_node *node; |
4ca3a69b SL |
298 | struct mm_slot *slot; |
299 | ||
300 | hash_for_each_possible(mm_slots_hash, slot, node, link, (unsigned long)mm) | |
301 | if (slot->mm == mm) | |
302 | return slot; | |
31dbd01f | 303 | |
31dbd01f IE |
304 | return NULL; |
305 | } | |
306 | ||
307 | static void insert_to_mm_slots_hash(struct mm_struct *mm, | |
308 | struct mm_slot *mm_slot) | |
309 | { | |
31dbd01f | 310 | mm_slot->mm = mm; |
4ca3a69b | 311 | hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm); |
31dbd01f IE |
312 | } |
313 | ||
314 | static inline int in_stable_tree(struct rmap_item *rmap_item) | |
315 | { | |
316 | return rmap_item->address & STABLE_FLAG; | |
317 | } | |
318 | ||
a913e182 HD |
319 | /* |
320 | * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's | |
321 | * page tables after it has passed through ksm_exit() - which, if necessary, | |
322 | * takes mmap_sem briefly to serialize against them. ksm_exit() does not set | |
323 | * a special flag: they can just back out as soon as mm_users goes to zero. | |
324 | * ksm_test_exit() is used throughout to make this test for exit: in some | |
325 | * places for correctness, in some places just to avoid unnecessary work. | |
326 | */ | |
327 | static inline bool ksm_test_exit(struct mm_struct *mm) | |
328 | { | |
329 | return atomic_read(&mm->mm_users) == 0; | |
330 | } | |
331 | ||
31dbd01f IE |
332 | /* |
333 | * We use break_ksm to break COW on a ksm page: it's a stripped down | |
334 | * | |
335 | * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1) | |
336 | * put_page(page); | |
337 | * | |
338 | * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma, | |
339 | * in case the application has unmapped and remapped mm,addr meanwhile. | |
340 | * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP | |
341 | * mmap of /dev/mem or /dev/kmem, where we would not want to touch it. | |
342 | */ | |
d952b791 | 343 | static int break_ksm(struct vm_area_struct *vma, unsigned long addr) |
31dbd01f IE |
344 | { |
345 | struct page *page; | |
d952b791 | 346 | int ret = 0; |
31dbd01f IE |
347 | |
348 | do { | |
349 | cond_resched(); | |
350 | page = follow_page(vma, addr, FOLL_GET); | |
22eccdd7 | 351 | if (IS_ERR_OR_NULL(page)) |
31dbd01f IE |
352 | break; |
353 | if (PageKsm(page)) | |
354 | ret = handle_mm_fault(vma->vm_mm, vma, addr, | |
355 | FAULT_FLAG_WRITE); | |
356 | else | |
357 | ret = VM_FAULT_WRITE; | |
358 | put_page(page); | |
d952b791 HD |
359 | } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM))); |
360 | /* | |
361 | * We must loop because handle_mm_fault() may back out if there's | |
362 | * any difficulty e.g. if pte accessed bit gets updated concurrently. | |
363 | * | |
364 | * VM_FAULT_WRITE is what we have been hoping for: it indicates that | |
365 | * COW has been broken, even if the vma does not permit VM_WRITE; | |
366 | * but note that a concurrent fault might break PageKsm for us. | |
367 | * | |
368 | * VM_FAULT_SIGBUS could occur if we race with truncation of the | |
369 | * backing file, which also invalidates anonymous pages: that's | |
370 | * okay, that truncation will have unmapped the PageKsm for us. | |
371 | * | |
372 | * VM_FAULT_OOM: at the time of writing (late July 2009), setting | |
373 | * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the | |
374 | * current task has TIF_MEMDIE set, and will be OOM killed on return | |
375 | * to user; and ksmd, having no mm, would never be chosen for that. | |
376 | * | |
377 | * But if the mm is in a limited mem_cgroup, then the fault may fail | |
378 | * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and | |
379 | * even ksmd can fail in this way - though it's usually breaking ksm | |
380 | * just to undo a merge it made a moment before, so unlikely to oom. | |
381 | * | |
382 | * That's a pity: we might therefore have more kernel pages allocated | |
383 | * than we're counting as nodes in the stable tree; but ksm_do_scan | |
384 | * will retry to break_cow on each pass, so should recover the page | |
385 | * in due course. The important thing is to not let VM_MERGEABLE | |
386 | * be cleared while any such pages might remain in the area. | |
387 | */ | |
388 | return (ret & VM_FAULT_OOM) ? -ENOMEM : 0; | |
31dbd01f IE |
389 | } |
390 | ||
ef694222 BL |
391 | static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm, |
392 | unsigned long addr) | |
393 | { | |
394 | struct vm_area_struct *vma; | |
395 | if (ksm_test_exit(mm)) | |
396 | return NULL; | |
397 | vma = find_vma(mm, addr); | |
398 | if (!vma || vma->vm_start > addr) | |
399 | return NULL; | |
400 | if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) | |
401 | return NULL; | |
402 | return vma; | |
403 | } | |
404 | ||
8dd3557a | 405 | static void break_cow(struct rmap_item *rmap_item) |
31dbd01f | 406 | { |
8dd3557a HD |
407 | struct mm_struct *mm = rmap_item->mm; |
408 | unsigned long addr = rmap_item->address; | |
31dbd01f IE |
409 | struct vm_area_struct *vma; |
410 | ||
4035c07a HD |
411 | /* |
412 | * It is not an accident that whenever we want to break COW | |
413 | * to undo, we also need to drop a reference to the anon_vma. | |
414 | */ | |
9e60109f | 415 | put_anon_vma(rmap_item->anon_vma); |
4035c07a | 416 | |
81464e30 | 417 | down_read(&mm->mmap_sem); |
ef694222 BL |
418 | vma = find_mergeable_vma(mm, addr); |
419 | if (vma) | |
420 | break_ksm(vma, addr); | |
31dbd01f IE |
421 | up_read(&mm->mmap_sem); |
422 | } | |
423 | ||
29ad768c AA |
424 | static struct page *page_trans_compound_anon(struct page *page) |
425 | { | |
426 | if (PageTransCompound(page)) { | |
22e5c47e | 427 | struct page *head = compound_trans_head(page); |
29ad768c | 428 | /* |
22e5c47e AA |
429 | * head may actually be splitted and freed from under |
430 | * us but it's ok here. | |
29ad768c | 431 | */ |
29ad768c AA |
432 | if (PageAnon(head)) |
433 | return head; | |
434 | } | |
435 | return NULL; | |
436 | } | |
437 | ||
31dbd01f IE |
438 | static struct page *get_mergeable_page(struct rmap_item *rmap_item) |
439 | { | |
440 | struct mm_struct *mm = rmap_item->mm; | |
441 | unsigned long addr = rmap_item->address; | |
442 | struct vm_area_struct *vma; | |
443 | struct page *page; | |
444 | ||
445 | down_read(&mm->mmap_sem); | |
ef694222 BL |
446 | vma = find_mergeable_vma(mm, addr); |
447 | if (!vma) | |
31dbd01f IE |
448 | goto out; |
449 | ||
450 | page = follow_page(vma, addr, FOLL_GET); | |
22eccdd7 | 451 | if (IS_ERR_OR_NULL(page)) |
31dbd01f | 452 | goto out; |
29ad768c | 453 | if (PageAnon(page) || page_trans_compound_anon(page)) { |
31dbd01f IE |
454 | flush_anon_page(vma, page, addr); |
455 | flush_dcache_page(page); | |
456 | } else { | |
457 | put_page(page); | |
458 | out: page = NULL; | |
459 | } | |
460 | up_read(&mm->mmap_sem); | |
461 | return page; | |
462 | } | |
463 | ||
90bd6fd3 PH |
464 | /* |
465 | * This helper is used for getting right index into array of tree roots. | |
466 | * When merge_across_nodes knob is set to 1, there are only two rb-trees for | |
467 | * stable and unstable pages from all nodes with roots in index 0. Otherwise, | |
468 | * every node has its own stable and unstable tree. | |
469 | */ | |
470 | static inline int get_kpfn_nid(unsigned long kpfn) | |
471 | { | |
e850dcf5 | 472 | return ksm_merge_across_nodes ? 0 : pfn_to_nid(kpfn); |
90bd6fd3 PH |
473 | } |
474 | ||
4035c07a HD |
475 | static void remove_node_from_stable_tree(struct stable_node *stable_node) |
476 | { | |
477 | struct rmap_item *rmap_item; | |
478 | struct hlist_node *hlist; | |
90bd6fd3 | 479 | int nid; |
4035c07a HD |
480 | |
481 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { | |
482 | if (rmap_item->hlist.next) | |
483 | ksm_pages_sharing--; | |
484 | else | |
485 | ksm_pages_shared--; | |
9e60109f | 486 | put_anon_vma(rmap_item->anon_vma); |
4035c07a HD |
487 | rmap_item->address &= PAGE_MASK; |
488 | cond_resched(); | |
489 | } | |
490 | ||
90bd6fd3 | 491 | nid = get_kpfn_nid(stable_node->kpfn); |
90bd6fd3 | 492 | rb_erase(&stable_node->node, &root_stable_tree[nid]); |
4035c07a HD |
493 | free_stable_node(stable_node); |
494 | } | |
495 | ||
496 | /* | |
497 | * get_ksm_page: checks if the page indicated by the stable node | |
498 | * is still its ksm page, despite having held no reference to it. | |
499 | * In which case we can trust the content of the page, and it | |
500 | * returns the gotten page; but if the page has now been zapped, | |
501 | * remove the stale node from the stable tree and return NULL. | |
502 | * | |
503 | * You would expect the stable_node to hold a reference to the ksm page. | |
504 | * But if it increments the page's count, swapping out has to wait for | |
505 | * ksmd to come around again before it can free the page, which may take | |
506 | * seconds or even minutes: much too unresponsive. So instead we use a | |
507 | * "keyhole reference": access to the ksm page from the stable node peeps | |
508 | * out through its keyhole to see if that page still holds the right key, | |
509 | * pointing back to this stable node. This relies on freeing a PageAnon | |
510 | * page to reset its page->mapping to NULL, and relies on no other use of | |
511 | * a page to put something that might look like our key in page->mapping. | |
512 | * | |
513 | * include/linux/pagemap.h page_cache_get_speculative() is a good reference, | |
514 | * but this is different - made simpler by ksm_thread_mutex being held, but | |
515 | * interesting for assuming that no other use of the struct page could ever | |
516 | * put our expected_mapping into page->mapping (or a field of the union which | |
517 | * coincides with page->mapping). The RCU calls are not for KSM at all, but | |
518 | * to keep the page_count protocol described with page_cache_get_speculative. | |
519 | * | |
520 | * Note: it is possible that get_ksm_page() will return NULL one moment, | |
521 | * then page the next, if the page is in between page_freeze_refs() and | |
522 | * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page | |
523 | * is on its way to being freed; but it is an anomaly to bear in mind. | |
524 | */ | |
525 | static struct page *get_ksm_page(struct stable_node *stable_node) | |
526 | { | |
527 | struct page *page; | |
528 | void *expected_mapping; | |
529 | ||
62b61f61 | 530 | page = pfn_to_page(stable_node->kpfn); |
4035c07a HD |
531 | expected_mapping = (void *)stable_node + |
532 | (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM); | |
533 | rcu_read_lock(); | |
534 | if (page->mapping != expected_mapping) | |
535 | goto stale; | |
536 | if (!get_page_unless_zero(page)) | |
537 | goto stale; | |
538 | if (page->mapping != expected_mapping) { | |
539 | put_page(page); | |
540 | goto stale; | |
541 | } | |
542 | rcu_read_unlock(); | |
543 | return page; | |
544 | stale: | |
545 | rcu_read_unlock(); | |
546 | remove_node_from_stable_tree(stable_node); | |
547 | return NULL; | |
548 | } | |
549 | ||
31dbd01f IE |
550 | /* |
551 | * Removing rmap_item from stable or unstable tree. | |
552 | * This function will clean the information from the stable/unstable tree. | |
553 | */ | |
554 | static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) | |
555 | { | |
7b6ba2c7 HD |
556 | if (rmap_item->address & STABLE_FLAG) { |
557 | struct stable_node *stable_node; | |
5ad64688 | 558 | struct page *page; |
31dbd01f | 559 | |
7b6ba2c7 | 560 | stable_node = rmap_item->head; |
4035c07a HD |
561 | page = get_ksm_page(stable_node); |
562 | if (!page) | |
563 | goto out; | |
5ad64688 | 564 | |
4035c07a | 565 | lock_page(page); |
7b6ba2c7 | 566 | hlist_del(&rmap_item->hlist); |
4035c07a HD |
567 | unlock_page(page); |
568 | put_page(page); | |
08beca44 | 569 | |
4035c07a HD |
570 | if (stable_node->hlist.first) |
571 | ksm_pages_sharing--; | |
572 | else | |
7b6ba2c7 | 573 | ksm_pages_shared--; |
31dbd01f | 574 | |
9e60109f | 575 | put_anon_vma(rmap_item->anon_vma); |
93d17715 | 576 | rmap_item->address &= PAGE_MASK; |
31dbd01f | 577 | |
7b6ba2c7 | 578 | } else if (rmap_item->address & UNSTABLE_FLAG) { |
31dbd01f IE |
579 | unsigned char age; |
580 | /* | |
9ba69294 | 581 | * Usually ksmd can and must skip the rb_erase, because |
31dbd01f | 582 | * root_unstable_tree was already reset to RB_ROOT. |
9ba69294 HD |
583 | * But be careful when an mm is exiting: do the rb_erase |
584 | * if this rmap_item was inserted by this scan, rather | |
585 | * than left over from before. | |
31dbd01f IE |
586 | */ |
587 | age = (unsigned char)(ksm_scan.seqnr - rmap_item->address); | |
cd551f97 | 588 | BUG_ON(age > 1); |
31dbd01f | 589 | if (!age) |
90bd6fd3 | 590 | rb_erase(&rmap_item->node, |
e850dcf5 | 591 | &root_unstable_tree[NUMA(rmap_item->nid)]); |
473b0ce4 | 592 | ksm_pages_unshared--; |
93d17715 | 593 | rmap_item->address &= PAGE_MASK; |
31dbd01f | 594 | } |
4035c07a | 595 | out: |
31dbd01f IE |
596 | cond_resched(); /* we're called from many long loops */ |
597 | } | |
598 | ||
31dbd01f | 599 | static void remove_trailing_rmap_items(struct mm_slot *mm_slot, |
6514d511 | 600 | struct rmap_item **rmap_list) |
31dbd01f | 601 | { |
6514d511 HD |
602 | while (*rmap_list) { |
603 | struct rmap_item *rmap_item = *rmap_list; | |
604 | *rmap_list = rmap_item->rmap_list; | |
31dbd01f | 605 | remove_rmap_item_from_tree(rmap_item); |
31dbd01f IE |
606 | free_rmap_item(rmap_item); |
607 | } | |
608 | } | |
609 | ||
610 | /* | |
e850dcf5 | 611 | * Though it's very tempting to unmerge rmap_items from stable tree rather |
31dbd01f IE |
612 | * than check every pte of a given vma, the locking doesn't quite work for |
613 | * that - an rmap_item is assigned to the stable tree after inserting ksm | |
614 | * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing | |
615 | * rmap_items from parent to child at fork time (so as not to waste time | |
616 | * if exit comes before the next scan reaches it). | |
81464e30 HD |
617 | * |
618 | * Similarly, although we'd like to remove rmap_items (so updating counts | |
619 | * and freeing memory) when unmerging an area, it's easier to leave that | |
620 | * to the next pass of ksmd - consider, for example, how ksmd might be | |
621 | * in cmp_and_merge_page on one of the rmap_items we would be removing. | |
31dbd01f | 622 | */ |
d952b791 HD |
623 | static int unmerge_ksm_pages(struct vm_area_struct *vma, |
624 | unsigned long start, unsigned long end) | |
31dbd01f IE |
625 | { |
626 | unsigned long addr; | |
d952b791 | 627 | int err = 0; |
31dbd01f | 628 | |
d952b791 | 629 | for (addr = start; addr < end && !err; addr += PAGE_SIZE) { |
9ba69294 HD |
630 | if (ksm_test_exit(vma->vm_mm)) |
631 | break; | |
d952b791 HD |
632 | if (signal_pending(current)) |
633 | err = -ERESTARTSYS; | |
634 | else | |
635 | err = break_ksm(vma, addr); | |
636 | } | |
637 | return err; | |
31dbd01f IE |
638 | } |
639 | ||
2ffd8679 HD |
640 | #ifdef CONFIG_SYSFS |
641 | /* | |
642 | * Only called through the sysfs control interface: | |
643 | */ | |
d952b791 | 644 | static int unmerge_and_remove_all_rmap_items(void) |
31dbd01f IE |
645 | { |
646 | struct mm_slot *mm_slot; | |
647 | struct mm_struct *mm; | |
648 | struct vm_area_struct *vma; | |
d952b791 HD |
649 | int err = 0; |
650 | ||
651 | spin_lock(&ksm_mmlist_lock); | |
9ba69294 | 652 | ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next, |
d952b791 HD |
653 | struct mm_slot, mm_list); |
654 | spin_unlock(&ksm_mmlist_lock); | |
31dbd01f | 655 | |
9ba69294 HD |
656 | for (mm_slot = ksm_scan.mm_slot; |
657 | mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) { | |
31dbd01f IE |
658 | mm = mm_slot->mm; |
659 | down_read(&mm->mmap_sem); | |
660 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
9ba69294 HD |
661 | if (ksm_test_exit(mm)) |
662 | break; | |
31dbd01f IE |
663 | if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) |
664 | continue; | |
d952b791 HD |
665 | err = unmerge_ksm_pages(vma, |
666 | vma->vm_start, vma->vm_end); | |
9ba69294 HD |
667 | if (err) |
668 | goto error; | |
31dbd01f | 669 | } |
9ba69294 | 670 | |
6514d511 | 671 | remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list); |
d952b791 HD |
672 | |
673 | spin_lock(&ksm_mmlist_lock); | |
9ba69294 | 674 | ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next, |
d952b791 | 675 | struct mm_slot, mm_list); |
9ba69294 | 676 | if (ksm_test_exit(mm)) { |
4ca3a69b | 677 | hash_del(&mm_slot->link); |
9ba69294 HD |
678 | list_del(&mm_slot->mm_list); |
679 | spin_unlock(&ksm_mmlist_lock); | |
680 | ||
681 | free_mm_slot(mm_slot); | |
682 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | |
683 | up_read(&mm->mmap_sem); | |
684 | mmdrop(mm); | |
685 | } else { | |
686 | spin_unlock(&ksm_mmlist_lock); | |
687 | up_read(&mm->mmap_sem); | |
688 | } | |
31dbd01f IE |
689 | } |
690 | ||
d952b791 | 691 | ksm_scan.seqnr = 0; |
9ba69294 HD |
692 | return 0; |
693 | ||
694 | error: | |
695 | up_read(&mm->mmap_sem); | |
31dbd01f | 696 | spin_lock(&ksm_mmlist_lock); |
d952b791 | 697 | ksm_scan.mm_slot = &ksm_mm_head; |
31dbd01f | 698 | spin_unlock(&ksm_mmlist_lock); |
d952b791 | 699 | return err; |
31dbd01f | 700 | } |
2ffd8679 | 701 | #endif /* CONFIG_SYSFS */ |
31dbd01f | 702 | |
31dbd01f IE |
703 | static u32 calc_checksum(struct page *page) |
704 | { | |
705 | u32 checksum; | |
9b04c5fe | 706 | void *addr = kmap_atomic(page); |
31dbd01f | 707 | checksum = jhash2(addr, PAGE_SIZE / 4, 17); |
9b04c5fe | 708 | kunmap_atomic(addr); |
31dbd01f IE |
709 | return checksum; |
710 | } | |
711 | ||
712 | static int memcmp_pages(struct page *page1, struct page *page2) | |
713 | { | |
714 | char *addr1, *addr2; | |
715 | int ret; | |
716 | ||
9b04c5fe CW |
717 | addr1 = kmap_atomic(page1); |
718 | addr2 = kmap_atomic(page2); | |
31dbd01f | 719 | ret = memcmp(addr1, addr2, PAGE_SIZE); |
9b04c5fe CW |
720 | kunmap_atomic(addr2); |
721 | kunmap_atomic(addr1); | |
31dbd01f IE |
722 | return ret; |
723 | } | |
724 | ||
725 | static inline int pages_identical(struct page *page1, struct page *page2) | |
726 | { | |
727 | return !memcmp_pages(page1, page2); | |
728 | } | |
729 | ||
730 | static int write_protect_page(struct vm_area_struct *vma, struct page *page, | |
731 | pte_t *orig_pte) | |
732 | { | |
733 | struct mm_struct *mm = vma->vm_mm; | |
734 | unsigned long addr; | |
735 | pte_t *ptep; | |
736 | spinlock_t *ptl; | |
737 | int swapped; | |
738 | int err = -EFAULT; | |
6bdb913f HE |
739 | unsigned long mmun_start; /* For mmu_notifiers */ |
740 | unsigned long mmun_end; /* For mmu_notifiers */ | |
31dbd01f IE |
741 | |
742 | addr = page_address_in_vma(page, vma); | |
743 | if (addr == -EFAULT) | |
744 | goto out; | |
745 | ||
29ad768c | 746 | BUG_ON(PageTransCompound(page)); |
6bdb913f HE |
747 | |
748 | mmun_start = addr; | |
749 | mmun_end = addr + PAGE_SIZE; | |
750 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
751 | ||
31dbd01f IE |
752 | ptep = page_check_address(page, mm, addr, &ptl, 0); |
753 | if (!ptep) | |
6bdb913f | 754 | goto out_mn; |
31dbd01f | 755 | |
4e31635c | 756 | if (pte_write(*ptep) || pte_dirty(*ptep)) { |
31dbd01f IE |
757 | pte_t entry; |
758 | ||
759 | swapped = PageSwapCache(page); | |
760 | flush_cache_page(vma, addr, page_to_pfn(page)); | |
761 | /* | |
25985edc | 762 | * Ok this is tricky, when get_user_pages_fast() run it doesn't |
31dbd01f IE |
763 | * take any lock, therefore the check that we are going to make |
764 | * with the pagecount against the mapcount is racey and | |
765 | * O_DIRECT can happen right after the check. | |
766 | * So we clear the pte and flush the tlb before the check | |
767 | * this assure us that no O_DIRECT can happen after the check | |
768 | * or in the middle of the check. | |
769 | */ | |
770 | entry = ptep_clear_flush(vma, addr, ptep); | |
771 | /* | |
772 | * Check that no O_DIRECT or similar I/O is in progress on the | |
773 | * page | |
774 | */ | |
31e855ea | 775 | if (page_mapcount(page) + 1 + swapped != page_count(page)) { |
cb532375 | 776 | set_pte_at(mm, addr, ptep, entry); |
31dbd01f IE |
777 | goto out_unlock; |
778 | } | |
4e31635c HD |
779 | if (pte_dirty(entry)) |
780 | set_page_dirty(page); | |
781 | entry = pte_mkclean(pte_wrprotect(entry)); | |
31dbd01f IE |
782 | set_pte_at_notify(mm, addr, ptep, entry); |
783 | } | |
784 | *orig_pte = *ptep; | |
785 | err = 0; | |
786 | ||
787 | out_unlock: | |
788 | pte_unmap_unlock(ptep, ptl); | |
6bdb913f HE |
789 | out_mn: |
790 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); | |
31dbd01f IE |
791 | out: |
792 | return err; | |
793 | } | |
794 | ||
795 | /** | |
796 | * replace_page - replace page in vma by new ksm page | |
8dd3557a HD |
797 | * @vma: vma that holds the pte pointing to page |
798 | * @page: the page we are replacing by kpage | |
799 | * @kpage: the ksm page we replace page by | |
31dbd01f IE |
800 | * @orig_pte: the original value of the pte |
801 | * | |
802 | * Returns 0 on success, -EFAULT on failure. | |
803 | */ | |
8dd3557a HD |
804 | static int replace_page(struct vm_area_struct *vma, struct page *page, |
805 | struct page *kpage, pte_t orig_pte) | |
31dbd01f IE |
806 | { |
807 | struct mm_struct *mm = vma->vm_mm; | |
31dbd01f IE |
808 | pmd_t *pmd; |
809 | pte_t *ptep; | |
810 | spinlock_t *ptl; | |
811 | unsigned long addr; | |
31dbd01f | 812 | int err = -EFAULT; |
6bdb913f HE |
813 | unsigned long mmun_start; /* For mmu_notifiers */ |
814 | unsigned long mmun_end; /* For mmu_notifiers */ | |
31dbd01f | 815 | |
8dd3557a | 816 | addr = page_address_in_vma(page, vma); |
31dbd01f IE |
817 | if (addr == -EFAULT) |
818 | goto out; | |
819 | ||
6219049a BL |
820 | pmd = mm_find_pmd(mm, addr); |
821 | if (!pmd) | |
31dbd01f | 822 | goto out; |
29ad768c | 823 | BUG_ON(pmd_trans_huge(*pmd)); |
31dbd01f | 824 | |
6bdb913f HE |
825 | mmun_start = addr; |
826 | mmun_end = addr + PAGE_SIZE; | |
827 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
828 | ||
31dbd01f IE |
829 | ptep = pte_offset_map_lock(mm, pmd, addr, &ptl); |
830 | if (!pte_same(*ptep, orig_pte)) { | |
831 | pte_unmap_unlock(ptep, ptl); | |
6bdb913f | 832 | goto out_mn; |
31dbd01f IE |
833 | } |
834 | ||
8dd3557a | 835 | get_page(kpage); |
5ad64688 | 836 | page_add_anon_rmap(kpage, vma, addr); |
31dbd01f IE |
837 | |
838 | flush_cache_page(vma, addr, pte_pfn(*ptep)); | |
839 | ptep_clear_flush(vma, addr, ptep); | |
8dd3557a | 840 | set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); |
31dbd01f | 841 | |
8dd3557a | 842 | page_remove_rmap(page); |
ae52a2ad HD |
843 | if (!page_mapped(page)) |
844 | try_to_free_swap(page); | |
8dd3557a | 845 | put_page(page); |
31dbd01f IE |
846 | |
847 | pte_unmap_unlock(ptep, ptl); | |
848 | err = 0; | |
6bdb913f HE |
849 | out_mn: |
850 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); | |
31dbd01f IE |
851 | out: |
852 | return err; | |
853 | } | |
854 | ||
29ad768c AA |
855 | static int page_trans_compound_anon_split(struct page *page) |
856 | { | |
857 | int ret = 0; | |
858 | struct page *transhuge_head = page_trans_compound_anon(page); | |
859 | if (transhuge_head) { | |
860 | /* Get the reference on the head to split it. */ | |
861 | if (get_page_unless_zero(transhuge_head)) { | |
862 | /* | |
863 | * Recheck we got the reference while the head | |
864 | * was still anonymous. | |
865 | */ | |
866 | if (PageAnon(transhuge_head)) | |
867 | ret = split_huge_page(transhuge_head); | |
868 | else | |
869 | /* | |
870 | * Retry later if split_huge_page run | |
871 | * from under us. | |
872 | */ | |
873 | ret = 1; | |
874 | put_page(transhuge_head); | |
875 | } else | |
876 | /* Retry later if split_huge_page run from under us. */ | |
877 | ret = 1; | |
878 | } | |
879 | return ret; | |
880 | } | |
881 | ||
31dbd01f IE |
882 | /* |
883 | * try_to_merge_one_page - take two pages and merge them into one | |
8dd3557a HD |
884 | * @vma: the vma that holds the pte pointing to page |
885 | * @page: the PageAnon page that we want to replace with kpage | |
80e14822 HD |
886 | * @kpage: the PageKsm page that we want to map instead of page, |
887 | * or NULL the first time when we want to use page as kpage. | |
31dbd01f IE |
888 | * |
889 | * This function returns 0 if the pages were merged, -EFAULT otherwise. | |
890 | */ | |
891 | static int try_to_merge_one_page(struct vm_area_struct *vma, | |
8dd3557a | 892 | struct page *page, struct page *kpage) |
31dbd01f IE |
893 | { |
894 | pte_t orig_pte = __pte(0); | |
895 | int err = -EFAULT; | |
896 | ||
db114b83 HD |
897 | if (page == kpage) /* ksm page forked */ |
898 | return 0; | |
899 | ||
31dbd01f IE |
900 | if (!(vma->vm_flags & VM_MERGEABLE)) |
901 | goto out; | |
29ad768c AA |
902 | if (PageTransCompound(page) && page_trans_compound_anon_split(page)) |
903 | goto out; | |
904 | BUG_ON(PageTransCompound(page)); | |
8dd3557a | 905 | if (!PageAnon(page)) |
31dbd01f IE |
906 | goto out; |
907 | ||
31dbd01f IE |
908 | /* |
909 | * We need the page lock to read a stable PageSwapCache in | |
910 | * write_protect_page(). We use trylock_page() instead of | |
911 | * lock_page() because we don't want to wait here - we | |
912 | * prefer to continue scanning and merging different pages, | |
913 | * then come back to this page when it is unlocked. | |
914 | */ | |
8dd3557a | 915 | if (!trylock_page(page)) |
31e855ea | 916 | goto out; |
31dbd01f IE |
917 | /* |
918 | * If this anonymous page is mapped only here, its pte may need | |
919 | * to be write-protected. If it's mapped elsewhere, all of its | |
920 | * ptes are necessarily already write-protected. But in either | |
921 | * case, we need to lock and check page_count is not raised. | |
922 | */ | |
80e14822 HD |
923 | if (write_protect_page(vma, page, &orig_pte) == 0) { |
924 | if (!kpage) { | |
925 | /* | |
926 | * While we hold page lock, upgrade page from | |
927 | * PageAnon+anon_vma to PageKsm+NULL stable_node: | |
928 | * stable_tree_insert() will update stable_node. | |
929 | */ | |
930 | set_page_stable_node(page, NULL); | |
931 | mark_page_accessed(page); | |
932 | err = 0; | |
933 | } else if (pages_identical(page, kpage)) | |
934 | err = replace_page(vma, page, kpage, orig_pte); | |
935 | } | |
31dbd01f | 936 | |
80e14822 | 937 | if ((vma->vm_flags & VM_LOCKED) && kpage && !err) { |
73848b46 | 938 | munlock_vma_page(page); |
5ad64688 HD |
939 | if (!PageMlocked(kpage)) { |
940 | unlock_page(page); | |
5ad64688 HD |
941 | lock_page(kpage); |
942 | mlock_vma_page(kpage); | |
943 | page = kpage; /* for final unlock */ | |
944 | } | |
945 | } | |
73848b46 | 946 | |
8dd3557a | 947 | unlock_page(page); |
31dbd01f IE |
948 | out: |
949 | return err; | |
950 | } | |
951 | ||
81464e30 HD |
952 | /* |
953 | * try_to_merge_with_ksm_page - like try_to_merge_two_pages, | |
954 | * but no new kernel page is allocated: kpage must already be a ksm page. | |
8dd3557a HD |
955 | * |
956 | * This function returns 0 if the pages were merged, -EFAULT otherwise. | |
81464e30 | 957 | */ |
8dd3557a HD |
958 | static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item, |
959 | struct page *page, struct page *kpage) | |
81464e30 | 960 | { |
8dd3557a | 961 | struct mm_struct *mm = rmap_item->mm; |
81464e30 HD |
962 | struct vm_area_struct *vma; |
963 | int err = -EFAULT; | |
964 | ||
8dd3557a HD |
965 | down_read(&mm->mmap_sem); |
966 | if (ksm_test_exit(mm)) | |
9ba69294 | 967 | goto out; |
8dd3557a HD |
968 | vma = find_vma(mm, rmap_item->address); |
969 | if (!vma || vma->vm_start > rmap_item->address) | |
81464e30 HD |
970 | goto out; |
971 | ||
8dd3557a | 972 | err = try_to_merge_one_page(vma, page, kpage); |
db114b83 HD |
973 | if (err) |
974 | goto out; | |
975 | ||
976 | /* Must get reference to anon_vma while still holding mmap_sem */ | |
9e60109f PZ |
977 | rmap_item->anon_vma = vma->anon_vma; |
978 | get_anon_vma(vma->anon_vma); | |
81464e30 | 979 | out: |
8dd3557a | 980 | up_read(&mm->mmap_sem); |
81464e30 HD |
981 | return err; |
982 | } | |
983 | ||
31dbd01f IE |
984 | /* |
985 | * try_to_merge_two_pages - take two identical pages and prepare them | |
986 | * to be merged into one page. | |
987 | * | |
8dd3557a HD |
988 | * This function returns the kpage if we successfully merged two identical |
989 | * pages into one ksm page, NULL otherwise. | |
31dbd01f | 990 | * |
80e14822 | 991 | * Note that this function upgrades page to ksm page: if one of the pages |
31dbd01f IE |
992 | * is already a ksm page, try_to_merge_with_ksm_page should be used. |
993 | */ | |
8dd3557a HD |
994 | static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item, |
995 | struct page *page, | |
996 | struct rmap_item *tree_rmap_item, | |
997 | struct page *tree_page) | |
31dbd01f | 998 | { |
80e14822 | 999 | int err; |
31dbd01f | 1000 | |
80e14822 | 1001 | err = try_to_merge_with_ksm_page(rmap_item, page, NULL); |
31dbd01f | 1002 | if (!err) { |
8dd3557a | 1003 | err = try_to_merge_with_ksm_page(tree_rmap_item, |
80e14822 | 1004 | tree_page, page); |
31dbd01f | 1005 | /* |
81464e30 HD |
1006 | * If that fails, we have a ksm page with only one pte |
1007 | * pointing to it: so break it. | |
31dbd01f | 1008 | */ |
4035c07a | 1009 | if (err) |
8dd3557a | 1010 | break_cow(rmap_item); |
31dbd01f | 1011 | } |
80e14822 | 1012 | return err ? NULL : page; |
31dbd01f IE |
1013 | } |
1014 | ||
31dbd01f | 1015 | /* |
8dd3557a | 1016 | * stable_tree_search - search for page inside the stable tree |
31dbd01f IE |
1017 | * |
1018 | * This function checks if there is a page inside the stable tree | |
1019 | * with identical content to the page that we are scanning right now. | |
1020 | * | |
7b6ba2c7 | 1021 | * This function returns the stable tree node of identical content if found, |
31dbd01f IE |
1022 | * NULL otherwise. |
1023 | */ | |
62b61f61 | 1024 | static struct page *stable_tree_search(struct page *page) |
31dbd01f | 1025 | { |
90bd6fd3 | 1026 | struct rb_node *node; |
7b6ba2c7 | 1027 | struct stable_node *stable_node; |
90bd6fd3 | 1028 | int nid; |
31dbd01f | 1029 | |
08beca44 HD |
1030 | stable_node = page_stable_node(page); |
1031 | if (stable_node) { /* ksm page forked */ | |
1032 | get_page(page); | |
62b61f61 | 1033 | return page; |
08beca44 HD |
1034 | } |
1035 | ||
90bd6fd3 PH |
1036 | nid = get_kpfn_nid(page_to_pfn(page)); |
1037 | node = root_stable_tree[nid].rb_node; | |
1038 | ||
31dbd01f | 1039 | while (node) { |
4035c07a | 1040 | struct page *tree_page; |
31dbd01f IE |
1041 | int ret; |
1042 | ||
08beca44 | 1043 | cond_resched(); |
7b6ba2c7 | 1044 | stable_node = rb_entry(node, struct stable_node, node); |
4035c07a HD |
1045 | tree_page = get_ksm_page(stable_node); |
1046 | if (!tree_page) | |
1047 | return NULL; | |
31dbd01f | 1048 | |
4035c07a | 1049 | ret = memcmp_pages(page, tree_page); |
31dbd01f | 1050 | |
4035c07a HD |
1051 | if (ret < 0) { |
1052 | put_page(tree_page); | |
31dbd01f | 1053 | node = node->rb_left; |
4035c07a HD |
1054 | } else if (ret > 0) { |
1055 | put_page(tree_page); | |
31dbd01f | 1056 | node = node->rb_right; |
4035c07a | 1057 | } else |
62b61f61 | 1058 | return tree_page; |
31dbd01f IE |
1059 | } |
1060 | ||
1061 | return NULL; | |
1062 | } | |
1063 | ||
1064 | /* | |
e850dcf5 | 1065 | * stable_tree_insert - insert stable tree node pointing to new ksm page |
31dbd01f IE |
1066 | * into the stable tree. |
1067 | * | |
7b6ba2c7 HD |
1068 | * This function returns the stable tree node just allocated on success, |
1069 | * NULL otherwise. | |
31dbd01f | 1070 | */ |
7b6ba2c7 | 1071 | static struct stable_node *stable_tree_insert(struct page *kpage) |
31dbd01f | 1072 | { |
90bd6fd3 PH |
1073 | int nid; |
1074 | unsigned long kpfn; | |
1075 | struct rb_node **new; | |
31dbd01f | 1076 | struct rb_node *parent = NULL; |
7b6ba2c7 | 1077 | struct stable_node *stable_node; |
31dbd01f | 1078 | |
90bd6fd3 PH |
1079 | kpfn = page_to_pfn(kpage); |
1080 | nid = get_kpfn_nid(kpfn); | |
1081 | new = &root_stable_tree[nid].rb_node; | |
1082 | ||
31dbd01f | 1083 | while (*new) { |
4035c07a | 1084 | struct page *tree_page; |
31dbd01f IE |
1085 | int ret; |
1086 | ||
08beca44 | 1087 | cond_resched(); |
7b6ba2c7 | 1088 | stable_node = rb_entry(*new, struct stable_node, node); |
4035c07a HD |
1089 | tree_page = get_ksm_page(stable_node); |
1090 | if (!tree_page) | |
1091 | return NULL; | |
31dbd01f | 1092 | |
4035c07a HD |
1093 | ret = memcmp_pages(kpage, tree_page); |
1094 | put_page(tree_page); | |
31dbd01f IE |
1095 | |
1096 | parent = *new; | |
1097 | if (ret < 0) | |
1098 | new = &parent->rb_left; | |
1099 | else if (ret > 0) | |
1100 | new = &parent->rb_right; | |
1101 | else { | |
1102 | /* | |
1103 | * It is not a bug that stable_tree_search() didn't | |
1104 | * find this node: because at that time our page was | |
1105 | * not yet write-protected, so may have changed since. | |
1106 | */ | |
1107 | return NULL; | |
1108 | } | |
1109 | } | |
1110 | ||
7b6ba2c7 HD |
1111 | stable_node = alloc_stable_node(); |
1112 | if (!stable_node) | |
1113 | return NULL; | |
31dbd01f | 1114 | |
7b6ba2c7 | 1115 | INIT_HLIST_HEAD(&stable_node->hlist); |
90bd6fd3 | 1116 | stable_node->kpfn = kpfn; |
08beca44 | 1117 | set_page_stable_node(kpage, stable_node); |
e850dcf5 HD |
1118 | rb_link_node(&stable_node->node, parent, new); |
1119 | rb_insert_color(&stable_node->node, &root_stable_tree[nid]); | |
08beca44 | 1120 | |
7b6ba2c7 | 1121 | return stable_node; |
31dbd01f IE |
1122 | } |
1123 | ||
1124 | /* | |
8dd3557a HD |
1125 | * unstable_tree_search_insert - search for identical page, |
1126 | * else insert rmap_item into the unstable tree. | |
31dbd01f IE |
1127 | * |
1128 | * This function searches for a page in the unstable tree identical to the | |
1129 | * page currently being scanned; and if no identical page is found in the | |
1130 | * tree, we insert rmap_item as a new object into the unstable tree. | |
1131 | * | |
1132 | * This function returns pointer to rmap_item found to be identical | |
1133 | * to the currently scanned page, NULL otherwise. | |
1134 | * | |
1135 | * This function does both searching and inserting, because they share | |
1136 | * the same walking algorithm in an rbtree. | |
1137 | */ | |
8dd3557a HD |
1138 | static |
1139 | struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item, | |
1140 | struct page *page, | |
1141 | struct page **tree_pagep) | |
31dbd01f | 1142 | { |
90bd6fd3 PH |
1143 | struct rb_node **new; |
1144 | struct rb_root *root; | |
31dbd01f | 1145 | struct rb_node *parent = NULL; |
90bd6fd3 PH |
1146 | int nid; |
1147 | ||
1148 | nid = get_kpfn_nid(page_to_pfn(page)); | |
1149 | root = &root_unstable_tree[nid]; | |
1150 | new = &root->rb_node; | |
31dbd01f IE |
1151 | |
1152 | while (*new) { | |
1153 | struct rmap_item *tree_rmap_item; | |
8dd3557a | 1154 | struct page *tree_page; |
31dbd01f IE |
1155 | int ret; |
1156 | ||
d178f27f | 1157 | cond_resched(); |
31dbd01f | 1158 | tree_rmap_item = rb_entry(*new, struct rmap_item, node); |
8dd3557a | 1159 | tree_page = get_mergeable_page(tree_rmap_item); |
22eccdd7 | 1160 | if (IS_ERR_OR_NULL(tree_page)) |
31dbd01f IE |
1161 | return NULL; |
1162 | ||
1163 | /* | |
8dd3557a | 1164 | * Don't substitute a ksm page for a forked page. |
31dbd01f | 1165 | */ |
8dd3557a HD |
1166 | if (page == tree_page) { |
1167 | put_page(tree_page); | |
31dbd01f IE |
1168 | return NULL; |
1169 | } | |
1170 | ||
90bd6fd3 PH |
1171 | /* |
1172 | * If tree_page has been migrated to another NUMA node, it | |
1173 | * will be flushed out and put into the right unstable tree | |
1174 | * next time: only merge with it if merge_across_nodes. | |
90bd6fd3 PH |
1175 | */ |
1176 | if (!ksm_merge_across_nodes && page_to_nid(tree_page) != nid) { | |
1177 | put_page(tree_page); | |
1178 | return NULL; | |
1179 | } | |
1180 | ||
8dd3557a | 1181 | ret = memcmp_pages(page, tree_page); |
31dbd01f IE |
1182 | |
1183 | parent = *new; | |
1184 | if (ret < 0) { | |
8dd3557a | 1185 | put_page(tree_page); |
31dbd01f IE |
1186 | new = &parent->rb_left; |
1187 | } else if (ret > 0) { | |
8dd3557a | 1188 | put_page(tree_page); |
31dbd01f IE |
1189 | new = &parent->rb_right; |
1190 | } else { | |
8dd3557a | 1191 | *tree_pagep = tree_page; |
31dbd01f IE |
1192 | return tree_rmap_item; |
1193 | } | |
1194 | } | |
1195 | ||
7b6ba2c7 | 1196 | rmap_item->address |= UNSTABLE_FLAG; |
31dbd01f | 1197 | rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK); |
e850dcf5 | 1198 | DO_NUMA(rmap_item->nid = nid); |
31dbd01f | 1199 | rb_link_node(&rmap_item->node, parent, new); |
90bd6fd3 | 1200 | rb_insert_color(&rmap_item->node, root); |
31dbd01f | 1201 | |
473b0ce4 | 1202 | ksm_pages_unshared++; |
31dbd01f IE |
1203 | return NULL; |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * stable_tree_append - add another rmap_item to the linked list of | |
1208 | * rmap_items hanging off a given node of the stable tree, all sharing | |
1209 | * the same ksm page. | |
1210 | */ | |
1211 | static void stable_tree_append(struct rmap_item *rmap_item, | |
7b6ba2c7 | 1212 | struct stable_node *stable_node) |
31dbd01f | 1213 | { |
90bd6fd3 PH |
1214 | /* |
1215 | * Usually rmap_item->nid is already set correctly, | |
1216 | * but it may be wrong after switching merge_across_nodes. | |
1217 | */ | |
e850dcf5 | 1218 | DO_NUMA(rmap_item->nid = get_kpfn_nid(stable_node->kpfn)); |
7b6ba2c7 | 1219 | rmap_item->head = stable_node; |
31dbd01f | 1220 | rmap_item->address |= STABLE_FLAG; |
7b6ba2c7 | 1221 | hlist_add_head(&rmap_item->hlist, &stable_node->hlist); |
e178dfde | 1222 | |
7b6ba2c7 HD |
1223 | if (rmap_item->hlist.next) |
1224 | ksm_pages_sharing++; | |
1225 | else | |
1226 | ksm_pages_shared++; | |
31dbd01f IE |
1227 | } |
1228 | ||
1229 | /* | |
81464e30 HD |
1230 | * cmp_and_merge_page - first see if page can be merged into the stable tree; |
1231 | * if not, compare checksum to previous and if it's the same, see if page can | |
1232 | * be inserted into the unstable tree, or merged with a page already there and | |
1233 | * both transferred to the stable tree. | |
31dbd01f IE |
1234 | * |
1235 | * @page: the page that we are searching identical page to. | |
1236 | * @rmap_item: the reverse mapping into the virtual address of this page | |
1237 | */ | |
1238 | static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) | |
1239 | { | |
31dbd01f | 1240 | struct rmap_item *tree_rmap_item; |
8dd3557a | 1241 | struct page *tree_page = NULL; |
7b6ba2c7 | 1242 | struct stable_node *stable_node; |
8dd3557a | 1243 | struct page *kpage; |
31dbd01f IE |
1244 | unsigned int checksum; |
1245 | int err; | |
1246 | ||
93d17715 | 1247 | remove_rmap_item_from_tree(rmap_item); |
31dbd01f IE |
1248 | |
1249 | /* We first start with searching the page inside the stable tree */ | |
62b61f61 HD |
1250 | kpage = stable_tree_search(page); |
1251 | if (kpage) { | |
08beca44 | 1252 | err = try_to_merge_with_ksm_page(rmap_item, page, kpage); |
31dbd01f IE |
1253 | if (!err) { |
1254 | /* | |
1255 | * The page was successfully merged: | |
1256 | * add its rmap_item to the stable tree. | |
1257 | */ | |
5ad64688 | 1258 | lock_page(kpage); |
62b61f61 | 1259 | stable_tree_append(rmap_item, page_stable_node(kpage)); |
5ad64688 | 1260 | unlock_page(kpage); |
31dbd01f | 1261 | } |
8dd3557a | 1262 | put_page(kpage); |
31dbd01f IE |
1263 | return; |
1264 | } | |
1265 | ||
1266 | /* | |
4035c07a HD |
1267 | * If the hash value of the page has changed from the last time |
1268 | * we calculated it, this page is changing frequently: therefore we | |
1269 | * don't want to insert it in the unstable tree, and we don't want | |
1270 | * to waste our time searching for something identical to it there. | |
31dbd01f IE |
1271 | */ |
1272 | checksum = calc_checksum(page); | |
1273 | if (rmap_item->oldchecksum != checksum) { | |
1274 | rmap_item->oldchecksum = checksum; | |
1275 | return; | |
1276 | } | |
1277 | ||
8dd3557a HD |
1278 | tree_rmap_item = |
1279 | unstable_tree_search_insert(rmap_item, page, &tree_page); | |
31dbd01f | 1280 | if (tree_rmap_item) { |
8dd3557a HD |
1281 | kpage = try_to_merge_two_pages(rmap_item, page, |
1282 | tree_rmap_item, tree_page); | |
1283 | put_page(tree_page); | |
31dbd01f IE |
1284 | /* |
1285 | * As soon as we merge this page, we want to remove the | |
1286 | * rmap_item of the page we have merged with from the unstable | |
1287 | * tree, and insert it instead as new node in the stable tree. | |
1288 | */ | |
8dd3557a | 1289 | if (kpage) { |
93d17715 | 1290 | remove_rmap_item_from_tree(tree_rmap_item); |
473b0ce4 | 1291 | |
5ad64688 | 1292 | lock_page(kpage); |
7b6ba2c7 HD |
1293 | stable_node = stable_tree_insert(kpage); |
1294 | if (stable_node) { | |
1295 | stable_tree_append(tree_rmap_item, stable_node); | |
1296 | stable_tree_append(rmap_item, stable_node); | |
1297 | } | |
5ad64688 | 1298 | unlock_page(kpage); |
7b6ba2c7 | 1299 | |
31dbd01f IE |
1300 | /* |
1301 | * If we fail to insert the page into the stable tree, | |
1302 | * we will have 2 virtual addresses that are pointing | |
1303 | * to a ksm page left outside the stable tree, | |
1304 | * in which case we need to break_cow on both. | |
1305 | */ | |
7b6ba2c7 | 1306 | if (!stable_node) { |
8dd3557a HD |
1307 | break_cow(tree_rmap_item); |
1308 | break_cow(rmap_item); | |
31dbd01f IE |
1309 | } |
1310 | } | |
31dbd01f IE |
1311 | } |
1312 | } | |
1313 | ||
1314 | static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot, | |
6514d511 | 1315 | struct rmap_item **rmap_list, |
31dbd01f IE |
1316 | unsigned long addr) |
1317 | { | |
1318 | struct rmap_item *rmap_item; | |
1319 | ||
6514d511 HD |
1320 | while (*rmap_list) { |
1321 | rmap_item = *rmap_list; | |
93d17715 | 1322 | if ((rmap_item->address & PAGE_MASK) == addr) |
31dbd01f | 1323 | return rmap_item; |
31dbd01f IE |
1324 | if (rmap_item->address > addr) |
1325 | break; | |
6514d511 | 1326 | *rmap_list = rmap_item->rmap_list; |
31dbd01f | 1327 | remove_rmap_item_from_tree(rmap_item); |
31dbd01f IE |
1328 | free_rmap_item(rmap_item); |
1329 | } | |
1330 | ||
1331 | rmap_item = alloc_rmap_item(); | |
1332 | if (rmap_item) { | |
1333 | /* It has already been zeroed */ | |
1334 | rmap_item->mm = mm_slot->mm; | |
1335 | rmap_item->address = addr; | |
6514d511 HD |
1336 | rmap_item->rmap_list = *rmap_list; |
1337 | *rmap_list = rmap_item; | |
31dbd01f IE |
1338 | } |
1339 | return rmap_item; | |
1340 | } | |
1341 | ||
1342 | static struct rmap_item *scan_get_next_rmap_item(struct page **page) | |
1343 | { | |
1344 | struct mm_struct *mm; | |
1345 | struct mm_slot *slot; | |
1346 | struct vm_area_struct *vma; | |
1347 | struct rmap_item *rmap_item; | |
90bd6fd3 | 1348 | int nid; |
31dbd01f IE |
1349 | |
1350 | if (list_empty(&ksm_mm_head.mm_list)) | |
1351 | return NULL; | |
1352 | ||
1353 | slot = ksm_scan.mm_slot; | |
1354 | if (slot == &ksm_mm_head) { | |
2919bfd0 HD |
1355 | /* |
1356 | * A number of pages can hang around indefinitely on per-cpu | |
1357 | * pagevecs, raised page count preventing write_protect_page | |
1358 | * from merging them. Though it doesn't really matter much, | |
1359 | * it is puzzling to see some stuck in pages_volatile until | |
1360 | * other activity jostles them out, and they also prevented | |
1361 | * LTP's KSM test from succeeding deterministically; so drain | |
1362 | * them here (here rather than on entry to ksm_do_scan(), | |
1363 | * so we don't IPI too often when pages_to_scan is set low). | |
1364 | */ | |
1365 | lru_add_drain_all(); | |
1366 | ||
90bd6fd3 PH |
1367 | for (nid = 0; nid < nr_node_ids; nid++) |
1368 | root_unstable_tree[nid] = RB_ROOT; | |
31dbd01f IE |
1369 | |
1370 | spin_lock(&ksm_mmlist_lock); | |
1371 | slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list); | |
1372 | ksm_scan.mm_slot = slot; | |
1373 | spin_unlock(&ksm_mmlist_lock); | |
2b472611 HD |
1374 | /* |
1375 | * Although we tested list_empty() above, a racing __ksm_exit | |
1376 | * of the last mm on the list may have removed it since then. | |
1377 | */ | |
1378 | if (slot == &ksm_mm_head) | |
1379 | return NULL; | |
31dbd01f IE |
1380 | next_mm: |
1381 | ksm_scan.address = 0; | |
6514d511 | 1382 | ksm_scan.rmap_list = &slot->rmap_list; |
31dbd01f IE |
1383 | } |
1384 | ||
1385 | mm = slot->mm; | |
1386 | down_read(&mm->mmap_sem); | |
9ba69294 HD |
1387 | if (ksm_test_exit(mm)) |
1388 | vma = NULL; | |
1389 | else | |
1390 | vma = find_vma(mm, ksm_scan.address); | |
1391 | ||
1392 | for (; vma; vma = vma->vm_next) { | |
31dbd01f IE |
1393 | if (!(vma->vm_flags & VM_MERGEABLE)) |
1394 | continue; | |
1395 | if (ksm_scan.address < vma->vm_start) | |
1396 | ksm_scan.address = vma->vm_start; | |
1397 | if (!vma->anon_vma) | |
1398 | ksm_scan.address = vma->vm_end; | |
1399 | ||
1400 | while (ksm_scan.address < vma->vm_end) { | |
9ba69294 HD |
1401 | if (ksm_test_exit(mm)) |
1402 | break; | |
31dbd01f | 1403 | *page = follow_page(vma, ksm_scan.address, FOLL_GET); |
21ae5b01 AA |
1404 | if (IS_ERR_OR_NULL(*page)) { |
1405 | ksm_scan.address += PAGE_SIZE; | |
1406 | cond_resched(); | |
1407 | continue; | |
1408 | } | |
29ad768c AA |
1409 | if (PageAnon(*page) || |
1410 | page_trans_compound_anon(*page)) { | |
31dbd01f IE |
1411 | flush_anon_page(vma, *page, ksm_scan.address); |
1412 | flush_dcache_page(*page); | |
1413 | rmap_item = get_next_rmap_item(slot, | |
6514d511 | 1414 | ksm_scan.rmap_list, ksm_scan.address); |
31dbd01f | 1415 | if (rmap_item) { |
6514d511 HD |
1416 | ksm_scan.rmap_list = |
1417 | &rmap_item->rmap_list; | |
31dbd01f IE |
1418 | ksm_scan.address += PAGE_SIZE; |
1419 | } else | |
1420 | put_page(*page); | |
1421 | up_read(&mm->mmap_sem); | |
1422 | return rmap_item; | |
1423 | } | |
21ae5b01 | 1424 | put_page(*page); |
31dbd01f IE |
1425 | ksm_scan.address += PAGE_SIZE; |
1426 | cond_resched(); | |
1427 | } | |
1428 | } | |
1429 | ||
9ba69294 HD |
1430 | if (ksm_test_exit(mm)) { |
1431 | ksm_scan.address = 0; | |
6514d511 | 1432 | ksm_scan.rmap_list = &slot->rmap_list; |
9ba69294 | 1433 | } |
31dbd01f IE |
1434 | /* |
1435 | * Nuke all the rmap_items that are above this current rmap: | |
1436 | * because there were no VM_MERGEABLE vmas with such addresses. | |
1437 | */ | |
6514d511 | 1438 | remove_trailing_rmap_items(slot, ksm_scan.rmap_list); |
31dbd01f IE |
1439 | |
1440 | spin_lock(&ksm_mmlist_lock); | |
cd551f97 HD |
1441 | ksm_scan.mm_slot = list_entry(slot->mm_list.next, |
1442 | struct mm_slot, mm_list); | |
1443 | if (ksm_scan.address == 0) { | |
1444 | /* | |
1445 | * We've completed a full scan of all vmas, holding mmap_sem | |
1446 | * throughout, and found no VM_MERGEABLE: so do the same as | |
1447 | * __ksm_exit does to remove this mm from all our lists now. | |
9ba69294 HD |
1448 | * This applies either when cleaning up after __ksm_exit |
1449 | * (but beware: we can reach here even before __ksm_exit), | |
1450 | * or when all VM_MERGEABLE areas have been unmapped (and | |
1451 | * mmap_sem then protects against race with MADV_MERGEABLE). | |
cd551f97 | 1452 | */ |
4ca3a69b | 1453 | hash_del(&slot->link); |
cd551f97 | 1454 | list_del(&slot->mm_list); |
9ba69294 HD |
1455 | spin_unlock(&ksm_mmlist_lock); |
1456 | ||
cd551f97 HD |
1457 | free_mm_slot(slot); |
1458 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | |
9ba69294 HD |
1459 | up_read(&mm->mmap_sem); |
1460 | mmdrop(mm); | |
1461 | } else { | |
1462 | spin_unlock(&ksm_mmlist_lock); | |
1463 | up_read(&mm->mmap_sem); | |
cd551f97 | 1464 | } |
31dbd01f IE |
1465 | |
1466 | /* Repeat until we've completed scanning the whole list */ | |
cd551f97 | 1467 | slot = ksm_scan.mm_slot; |
31dbd01f IE |
1468 | if (slot != &ksm_mm_head) |
1469 | goto next_mm; | |
1470 | ||
31dbd01f IE |
1471 | ksm_scan.seqnr++; |
1472 | return NULL; | |
1473 | } | |
1474 | ||
1475 | /** | |
1476 | * ksm_do_scan - the ksm scanner main worker function. | |
1477 | * @scan_npages - number of pages we want to scan before we return. | |
1478 | */ | |
1479 | static void ksm_do_scan(unsigned int scan_npages) | |
1480 | { | |
1481 | struct rmap_item *rmap_item; | |
22eccdd7 | 1482 | struct page *uninitialized_var(page); |
31dbd01f | 1483 | |
878aee7d | 1484 | while (scan_npages-- && likely(!freezing(current))) { |
31dbd01f IE |
1485 | cond_resched(); |
1486 | rmap_item = scan_get_next_rmap_item(&page); | |
1487 | if (!rmap_item) | |
1488 | return; | |
1489 | if (!PageKsm(page) || !in_stable_tree(rmap_item)) | |
1490 | cmp_and_merge_page(page, rmap_item); | |
1491 | put_page(page); | |
1492 | } | |
1493 | } | |
1494 | ||
6e158384 HD |
1495 | static int ksmd_should_run(void) |
1496 | { | |
1497 | return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list); | |
1498 | } | |
1499 | ||
31dbd01f IE |
1500 | static int ksm_scan_thread(void *nothing) |
1501 | { | |
878aee7d | 1502 | set_freezable(); |
339aa624 | 1503 | set_user_nice(current, 5); |
31dbd01f IE |
1504 | |
1505 | while (!kthread_should_stop()) { | |
6e158384 HD |
1506 | mutex_lock(&ksm_thread_mutex); |
1507 | if (ksmd_should_run()) | |
31dbd01f | 1508 | ksm_do_scan(ksm_thread_pages_to_scan); |
6e158384 HD |
1509 | mutex_unlock(&ksm_thread_mutex); |
1510 | ||
878aee7d AA |
1511 | try_to_freeze(); |
1512 | ||
6e158384 | 1513 | if (ksmd_should_run()) { |
31dbd01f IE |
1514 | schedule_timeout_interruptible( |
1515 | msecs_to_jiffies(ksm_thread_sleep_millisecs)); | |
1516 | } else { | |
878aee7d | 1517 | wait_event_freezable(ksm_thread_wait, |
6e158384 | 1518 | ksmd_should_run() || kthread_should_stop()); |
31dbd01f IE |
1519 | } |
1520 | } | |
1521 | return 0; | |
1522 | } | |
1523 | ||
f8af4da3 HD |
1524 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
1525 | unsigned long end, int advice, unsigned long *vm_flags) | |
1526 | { | |
1527 | struct mm_struct *mm = vma->vm_mm; | |
d952b791 | 1528 | int err; |
f8af4da3 HD |
1529 | |
1530 | switch (advice) { | |
1531 | case MADV_MERGEABLE: | |
1532 | /* | |
1533 | * Be somewhat over-protective for now! | |
1534 | */ | |
1535 | if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE | | |
1536 | VM_PFNMAP | VM_IO | VM_DONTEXPAND | | |
314e51b9 | 1537 | VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP)) |
f8af4da3 HD |
1538 | return 0; /* just ignore the advice */ |
1539 | ||
cc2383ec KK |
1540 | #ifdef VM_SAO |
1541 | if (*vm_flags & VM_SAO) | |
1542 | return 0; | |
1543 | #endif | |
1544 | ||
d952b791 HD |
1545 | if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) { |
1546 | err = __ksm_enter(mm); | |
1547 | if (err) | |
1548 | return err; | |
1549 | } | |
f8af4da3 HD |
1550 | |
1551 | *vm_flags |= VM_MERGEABLE; | |
1552 | break; | |
1553 | ||
1554 | case MADV_UNMERGEABLE: | |
1555 | if (!(*vm_flags & VM_MERGEABLE)) | |
1556 | return 0; /* just ignore the advice */ | |
1557 | ||
d952b791 HD |
1558 | if (vma->anon_vma) { |
1559 | err = unmerge_ksm_pages(vma, start, end); | |
1560 | if (err) | |
1561 | return err; | |
1562 | } | |
f8af4da3 HD |
1563 | |
1564 | *vm_flags &= ~VM_MERGEABLE; | |
1565 | break; | |
1566 | } | |
1567 | ||
1568 | return 0; | |
1569 | } | |
1570 | ||
1571 | int __ksm_enter(struct mm_struct *mm) | |
1572 | { | |
6e158384 HD |
1573 | struct mm_slot *mm_slot; |
1574 | int needs_wakeup; | |
1575 | ||
1576 | mm_slot = alloc_mm_slot(); | |
31dbd01f IE |
1577 | if (!mm_slot) |
1578 | return -ENOMEM; | |
1579 | ||
6e158384 HD |
1580 | /* Check ksm_run too? Would need tighter locking */ |
1581 | needs_wakeup = list_empty(&ksm_mm_head.mm_list); | |
1582 | ||
31dbd01f IE |
1583 | spin_lock(&ksm_mmlist_lock); |
1584 | insert_to_mm_slots_hash(mm, mm_slot); | |
1585 | /* | |
1586 | * Insert just behind the scanning cursor, to let the area settle | |
1587 | * down a little; when fork is followed by immediate exec, we don't | |
1588 | * want ksmd to waste time setting up and tearing down an rmap_list. | |
1589 | */ | |
1590 | list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list); | |
1591 | spin_unlock(&ksm_mmlist_lock); | |
1592 | ||
f8af4da3 | 1593 | set_bit(MMF_VM_MERGEABLE, &mm->flags); |
9ba69294 | 1594 | atomic_inc(&mm->mm_count); |
6e158384 HD |
1595 | |
1596 | if (needs_wakeup) | |
1597 | wake_up_interruptible(&ksm_thread_wait); | |
1598 | ||
f8af4da3 HD |
1599 | return 0; |
1600 | } | |
1601 | ||
1c2fb7a4 | 1602 | void __ksm_exit(struct mm_struct *mm) |
f8af4da3 | 1603 | { |
cd551f97 | 1604 | struct mm_slot *mm_slot; |
9ba69294 | 1605 | int easy_to_free = 0; |
cd551f97 | 1606 | |
31dbd01f | 1607 | /* |
9ba69294 HD |
1608 | * This process is exiting: if it's straightforward (as is the |
1609 | * case when ksmd was never running), free mm_slot immediately. | |
1610 | * But if it's at the cursor or has rmap_items linked to it, use | |
1611 | * mmap_sem to synchronize with any break_cows before pagetables | |
1612 | * are freed, and leave the mm_slot on the list for ksmd to free. | |
1613 | * Beware: ksm may already have noticed it exiting and freed the slot. | |
31dbd01f | 1614 | */ |
9ba69294 | 1615 | |
cd551f97 HD |
1616 | spin_lock(&ksm_mmlist_lock); |
1617 | mm_slot = get_mm_slot(mm); | |
9ba69294 | 1618 | if (mm_slot && ksm_scan.mm_slot != mm_slot) { |
6514d511 | 1619 | if (!mm_slot->rmap_list) { |
4ca3a69b | 1620 | hash_del(&mm_slot->link); |
9ba69294 HD |
1621 | list_del(&mm_slot->mm_list); |
1622 | easy_to_free = 1; | |
1623 | } else { | |
1624 | list_move(&mm_slot->mm_list, | |
1625 | &ksm_scan.mm_slot->mm_list); | |
1626 | } | |
cd551f97 | 1627 | } |
cd551f97 HD |
1628 | spin_unlock(&ksm_mmlist_lock); |
1629 | ||
9ba69294 HD |
1630 | if (easy_to_free) { |
1631 | free_mm_slot(mm_slot); | |
1632 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | |
1633 | mmdrop(mm); | |
1634 | } else if (mm_slot) { | |
9ba69294 HD |
1635 | down_write(&mm->mmap_sem); |
1636 | up_write(&mm->mmap_sem); | |
9ba69294 | 1637 | } |
31dbd01f IE |
1638 | } |
1639 | ||
5ad64688 HD |
1640 | struct page *ksm_does_need_to_copy(struct page *page, |
1641 | struct vm_area_struct *vma, unsigned long address) | |
1642 | { | |
1643 | struct page *new_page; | |
1644 | ||
5ad64688 HD |
1645 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); |
1646 | if (new_page) { | |
1647 | copy_user_highpage(new_page, page, address, vma); | |
1648 | ||
1649 | SetPageDirty(new_page); | |
1650 | __SetPageUptodate(new_page); | |
5ad64688 | 1651 | __set_page_locked(new_page); |
5ad64688 HD |
1652 | } |
1653 | ||
5ad64688 HD |
1654 | return new_page; |
1655 | } | |
1656 | ||
1657 | int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg, | |
1658 | unsigned long *vm_flags) | |
1659 | { | |
1660 | struct stable_node *stable_node; | |
1661 | struct rmap_item *rmap_item; | |
1662 | struct hlist_node *hlist; | |
1663 | unsigned int mapcount = page_mapcount(page); | |
1664 | int referenced = 0; | |
db114b83 | 1665 | int search_new_forks = 0; |
5ad64688 HD |
1666 | |
1667 | VM_BUG_ON(!PageKsm(page)); | |
1668 | VM_BUG_ON(!PageLocked(page)); | |
1669 | ||
1670 | stable_node = page_stable_node(page); | |
1671 | if (!stable_node) | |
1672 | return 0; | |
db114b83 | 1673 | again: |
5ad64688 | 1674 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { |
db114b83 | 1675 | struct anon_vma *anon_vma = rmap_item->anon_vma; |
5beb4930 | 1676 | struct anon_vma_chain *vmac; |
db114b83 | 1677 | struct vm_area_struct *vma; |
5ad64688 | 1678 | |
b6b19f25 | 1679 | anon_vma_lock_read(anon_vma); |
bf181b9f ML |
1680 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
1681 | 0, ULONG_MAX) { | |
5beb4930 | 1682 | vma = vmac->vma; |
db114b83 HD |
1683 | if (rmap_item->address < vma->vm_start || |
1684 | rmap_item->address >= vma->vm_end) | |
1685 | continue; | |
1686 | /* | |
1687 | * Initially we examine only the vma which covers this | |
1688 | * rmap_item; but later, if there is still work to do, | |
1689 | * we examine covering vmas in other mms: in case they | |
1690 | * were forked from the original since ksmd passed. | |
1691 | */ | |
1692 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) | |
1693 | continue; | |
1694 | ||
1695 | if (memcg && !mm_match_cgroup(vma->vm_mm, memcg)) | |
1696 | continue; | |
5ad64688 | 1697 | |
db114b83 | 1698 | referenced += page_referenced_one(page, vma, |
5ad64688 | 1699 | rmap_item->address, &mapcount, vm_flags); |
db114b83 HD |
1700 | if (!search_new_forks || !mapcount) |
1701 | break; | |
1702 | } | |
b6b19f25 | 1703 | anon_vma_unlock_read(anon_vma); |
5ad64688 HD |
1704 | if (!mapcount) |
1705 | goto out; | |
1706 | } | |
db114b83 HD |
1707 | if (!search_new_forks++) |
1708 | goto again; | |
5ad64688 | 1709 | out: |
5ad64688 HD |
1710 | return referenced; |
1711 | } | |
1712 | ||
1713 | int try_to_unmap_ksm(struct page *page, enum ttu_flags flags) | |
1714 | { | |
1715 | struct stable_node *stable_node; | |
1716 | struct hlist_node *hlist; | |
1717 | struct rmap_item *rmap_item; | |
1718 | int ret = SWAP_AGAIN; | |
db114b83 | 1719 | int search_new_forks = 0; |
5ad64688 HD |
1720 | |
1721 | VM_BUG_ON(!PageKsm(page)); | |
1722 | VM_BUG_ON(!PageLocked(page)); | |
1723 | ||
1724 | stable_node = page_stable_node(page); | |
1725 | if (!stable_node) | |
1726 | return SWAP_FAIL; | |
db114b83 | 1727 | again: |
5ad64688 | 1728 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { |
db114b83 | 1729 | struct anon_vma *anon_vma = rmap_item->anon_vma; |
5beb4930 | 1730 | struct anon_vma_chain *vmac; |
db114b83 | 1731 | struct vm_area_struct *vma; |
5ad64688 | 1732 | |
b6b19f25 | 1733 | anon_vma_lock_read(anon_vma); |
bf181b9f ML |
1734 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
1735 | 0, ULONG_MAX) { | |
5beb4930 | 1736 | vma = vmac->vma; |
db114b83 HD |
1737 | if (rmap_item->address < vma->vm_start || |
1738 | rmap_item->address >= vma->vm_end) | |
1739 | continue; | |
1740 | /* | |
1741 | * Initially we examine only the vma which covers this | |
1742 | * rmap_item; but later, if there is still work to do, | |
1743 | * we examine covering vmas in other mms: in case they | |
1744 | * were forked from the original since ksmd passed. | |
1745 | */ | |
1746 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) | |
1747 | continue; | |
1748 | ||
1749 | ret = try_to_unmap_one(page, vma, | |
1750 | rmap_item->address, flags); | |
1751 | if (ret != SWAP_AGAIN || !page_mapped(page)) { | |
b6b19f25 | 1752 | anon_vma_unlock_read(anon_vma); |
db114b83 HD |
1753 | goto out; |
1754 | } | |
1755 | } | |
b6b19f25 | 1756 | anon_vma_unlock_read(anon_vma); |
5ad64688 | 1757 | } |
db114b83 HD |
1758 | if (!search_new_forks++) |
1759 | goto again; | |
5ad64688 | 1760 | out: |
5ad64688 HD |
1761 | return ret; |
1762 | } | |
1763 | ||
e9995ef9 HD |
1764 | #ifdef CONFIG_MIGRATION |
1765 | int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *, | |
1766 | struct vm_area_struct *, unsigned long, void *), void *arg) | |
1767 | { | |
1768 | struct stable_node *stable_node; | |
1769 | struct hlist_node *hlist; | |
1770 | struct rmap_item *rmap_item; | |
1771 | int ret = SWAP_AGAIN; | |
1772 | int search_new_forks = 0; | |
1773 | ||
1774 | VM_BUG_ON(!PageKsm(page)); | |
1775 | VM_BUG_ON(!PageLocked(page)); | |
1776 | ||
1777 | stable_node = page_stable_node(page); | |
1778 | if (!stable_node) | |
1779 | return ret; | |
1780 | again: | |
1781 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { | |
1782 | struct anon_vma *anon_vma = rmap_item->anon_vma; | |
5beb4930 | 1783 | struct anon_vma_chain *vmac; |
e9995ef9 HD |
1784 | struct vm_area_struct *vma; |
1785 | ||
b6b19f25 | 1786 | anon_vma_lock_read(anon_vma); |
bf181b9f ML |
1787 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
1788 | 0, ULONG_MAX) { | |
5beb4930 | 1789 | vma = vmac->vma; |
e9995ef9 HD |
1790 | if (rmap_item->address < vma->vm_start || |
1791 | rmap_item->address >= vma->vm_end) | |
1792 | continue; | |
1793 | /* | |
1794 | * Initially we examine only the vma which covers this | |
1795 | * rmap_item; but later, if there is still work to do, | |
1796 | * we examine covering vmas in other mms: in case they | |
1797 | * were forked from the original since ksmd passed. | |
1798 | */ | |
1799 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) | |
1800 | continue; | |
1801 | ||
1802 | ret = rmap_one(page, vma, rmap_item->address, arg); | |
1803 | if (ret != SWAP_AGAIN) { | |
b6b19f25 | 1804 | anon_vma_unlock_read(anon_vma); |
e9995ef9 HD |
1805 | goto out; |
1806 | } | |
1807 | } | |
b6b19f25 | 1808 | anon_vma_unlock_read(anon_vma); |
e9995ef9 HD |
1809 | } |
1810 | if (!search_new_forks++) | |
1811 | goto again; | |
1812 | out: | |
1813 | return ret; | |
1814 | } | |
1815 | ||
1816 | void ksm_migrate_page(struct page *newpage, struct page *oldpage) | |
1817 | { | |
1818 | struct stable_node *stable_node; | |
1819 | ||
1820 | VM_BUG_ON(!PageLocked(oldpage)); | |
1821 | VM_BUG_ON(!PageLocked(newpage)); | |
1822 | VM_BUG_ON(newpage->mapping != oldpage->mapping); | |
1823 | ||
1824 | stable_node = page_stable_node(newpage); | |
1825 | if (stable_node) { | |
62b61f61 HD |
1826 | VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage)); |
1827 | stable_node->kpfn = page_to_pfn(newpage); | |
e9995ef9 HD |
1828 | } |
1829 | } | |
1830 | #endif /* CONFIG_MIGRATION */ | |
1831 | ||
62b61f61 | 1832 | #ifdef CONFIG_MEMORY_HOTREMOVE |
ee0ea59c HD |
1833 | static void ksm_check_stable_tree(unsigned long start_pfn, |
1834 | unsigned long end_pfn) | |
62b61f61 | 1835 | { |
ee0ea59c | 1836 | struct stable_node *stable_node; |
62b61f61 | 1837 | struct rb_node *node; |
90bd6fd3 | 1838 | int nid; |
62b61f61 | 1839 | |
ee0ea59c HD |
1840 | for (nid = 0; nid < nr_node_ids; nid++) { |
1841 | node = rb_first(&root_stable_tree[nid]); | |
1842 | while (node) { | |
90bd6fd3 PH |
1843 | stable_node = rb_entry(node, struct stable_node, node); |
1844 | if (stable_node->kpfn >= start_pfn && | |
ee0ea59c HD |
1845 | stable_node->kpfn < end_pfn) { |
1846 | /* | |
1847 | * Don't get_ksm_page, page has already gone: | |
1848 | * which is why we keep kpfn instead of page* | |
1849 | */ | |
1850 | remove_node_from_stable_tree(stable_node); | |
1851 | node = rb_first(&root_stable_tree[nid]); | |
1852 | } else | |
1853 | node = rb_next(node); | |
1854 | cond_resched(); | |
90bd6fd3 | 1855 | } |
ee0ea59c | 1856 | } |
62b61f61 HD |
1857 | } |
1858 | ||
1859 | static int ksm_memory_callback(struct notifier_block *self, | |
1860 | unsigned long action, void *arg) | |
1861 | { | |
1862 | struct memory_notify *mn = arg; | |
62b61f61 HD |
1863 | |
1864 | switch (action) { | |
1865 | case MEM_GOING_OFFLINE: | |
1866 | /* | |
1867 | * Keep it very simple for now: just lock out ksmd and | |
1868 | * MADV_UNMERGEABLE while any memory is going offline. | |
a0b0f58c KM |
1869 | * mutex_lock_nested() is necessary because lockdep was alarmed |
1870 | * that here we take ksm_thread_mutex inside notifier chain | |
1871 | * mutex, and later take notifier chain mutex inside | |
1872 | * ksm_thread_mutex to unlock it. But that's safe because both | |
1873 | * are inside mem_hotplug_mutex. | |
62b61f61 | 1874 | */ |
a0b0f58c | 1875 | mutex_lock_nested(&ksm_thread_mutex, SINGLE_DEPTH_NESTING); |
62b61f61 HD |
1876 | break; |
1877 | ||
1878 | case MEM_OFFLINE: | |
1879 | /* | |
1880 | * Most of the work is done by page migration; but there might | |
1881 | * be a few stable_nodes left over, still pointing to struct | |
ee0ea59c HD |
1882 | * pages which have been offlined: prune those from the tree, |
1883 | * otherwise get_ksm_page() might later try to access a | |
1884 | * non-existent struct page. | |
62b61f61 | 1885 | */ |
ee0ea59c HD |
1886 | ksm_check_stable_tree(mn->start_pfn, |
1887 | mn->start_pfn + mn->nr_pages); | |
62b61f61 HD |
1888 | /* fallthrough */ |
1889 | ||
1890 | case MEM_CANCEL_OFFLINE: | |
1891 | mutex_unlock(&ksm_thread_mutex); | |
1892 | break; | |
1893 | } | |
1894 | return NOTIFY_OK; | |
1895 | } | |
1896 | #endif /* CONFIG_MEMORY_HOTREMOVE */ | |
1897 | ||
2ffd8679 HD |
1898 | #ifdef CONFIG_SYSFS |
1899 | /* | |
1900 | * This all compiles without CONFIG_SYSFS, but is a waste of space. | |
1901 | */ | |
1902 | ||
31dbd01f IE |
1903 | #define KSM_ATTR_RO(_name) \ |
1904 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) | |
1905 | #define KSM_ATTR(_name) \ | |
1906 | static struct kobj_attribute _name##_attr = \ | |
1907 | __ATTR(_name, 0644, _name##_show, _name##_store) | |
1908 | ||
1909 | static ssize_t sleep_millisecs_show(struct kobject *kobj, | |
1910 | struct kobj_attribute *attr, char *buf) | |
1911 | { | |
1912 | return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs); | |
1913 | } | |
1914 | ||
1915 | static ssize_t sleep_millisecs_store(struct kobject *kobj, | |
1916 | struct kobj_attribute *attr, | |
1917 | const char *buf, size_t count) | |
1918 | { | |
1919 | unsigned long msecs; | |
1920 | int err; | |
1921 | ||
1922 | err = strict_strtoul(buf, 10, &msecs); | |
1923 | if (err || msecs > UINT_MAX) | |
1924 | return -EINVAL; | |
1925 | ||
1926 | ksm_thread_sleep_millisecs = msecs; | |
1927 | ||
1928 | return count; | |
1929 | } | |
1930 | KSM_ATTR(sleep_millisecs); | |
1931 | ||
1932 | static ssize_t pages_to_scan_show(struct kobject *kobj, | |
1933 | struct kobj_attribute *attr, char *buf) | |
1934 | { | |
1935 | return sprintf(buf, "%u\n", ksm_thread_pages_to_scan); | |
1936 | } | |
1937 | ||
1938 | static ssize_t pages_to_scan_store(struct kobject *kobj, | |
1939 | struct kobj_attribute *attr, | |
1940 | const char *buf, size_t count) | |
1941 | { | |
1942 | int err; | |
1943 | unsigned long nr_pages; | |
1944 | ||
1945 | err = strict_strtoul(buf, 10, &nr_pages); | |
1946 | if (err || nr_pages > UINT_MAX) | |
1947 | return -EINVAL; | |
1948 | ||
1949 | ksm_thread_pages_to_scan = nr_pages; | |
1950 | ||
1951 | return count; | |
1952 | } | |
1953 | KSM_ATTR(pages_to_scan); | |
1954 | ||
1955 | static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr, | |
1956 | char *buf) | |
1957 | { | |
1958 | return sprintf(buf, "%u\n", ksm_run); | |
1959 | } | |
1960 | ||
1961 | static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, | |
1962 | const char *buf, size_t count) | |
1963 | { | |
1964 | int err; | |
1965 | unsigned long flags; | |
1966 | ||
1967 | err = strict_strtoul(buf, 10, &flags); | |
1968 | if (err || flags > UINT_MAX) | |
1969 | return -EINVAL; | |
1970 | if (flags > KSM_RUN_UNMERGE) | |
1971 | return -EINVAL; | |
1972 | ||
1973 | /* | |
1974 | * KSM_RUN_MERGE sets ksmd running, and 0 stops it running. | |
1975 | * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items, | |
d0f209f6 HD |
1976 | * breaking COW to free the pages_shared (but leaves mm_slots |
1977 | * on the list for when ksmd may be set running again). | |
31dbd01f IE |
1978 | */ |
1979 | ||
1980 | mutex_lock(&ksm_thread_mutex); | |
1981 | if (ksm_run != flags) { | |
1982 | ksm_run = flags; | |
d952b791 | 1983 | if (flags & KSM_RUN_UNMERGE) { |
e1e12d2f | 1984 | set_current_oom_origin(); |
d952b791 | 1985 | err = unmerge_and_remove_all_rmap_items(); |
e1e12d2f | 1986 | clear_current_oom_origin(); |
d952b791 HD |
1987 | if (err) { |
1988 | ksm_run = KSM_RUN_STOP; | |
1989 | count = err; | |
1990 | } | |
1991 | } | |
31dbd01f IE |
1992 | } |
1993 | mutex_unlock(&ksm_thread_mutex); | |
1994 | ||
1995 | if (flags & KSM_RUN_MERGE) | |
1996 | wake_up_interruptible(&ksm_thread_wait); | |
1997 | ||
1998 | return count; | |
1999 | } | |
2000 | KSM_ATTR(run); | |
2001 | ||
90bd6fd3 PH |
2002 | #ifdef CONFIG_NUMA |
2003 | static ssize_t merge_across_nodes_show(struct kobject *kobj, | |
2004 | struct kobj_attribute *attr, char *buf) | |
2005 | { | |
2006 | return sprintf(buf, "%u\n", ksm_merge_across_nodes); | |
2007 | } | |
2008 | ||
2009 | static ssize_t merge_across_nodes_store(struct kobject *kobj, | |
2010 | struct kobj_attribute *attr, | |
2011 | const char *buf, size_t count) | |
2012 | { | |
2013 | int err; | |
2014 | unsigned long knob; | |
2015 | ||
2016 | err = kstrtoul(buf, 10, &knob); | |
2017 | if (err) | |
2018 | return err; | |
2019 | if (knob > 1) | |
2020 | return -EINVAL; | |
2021 | ||
2022 | mutex_lock(&ksm_thread_mutex); | |
2023 | if (ksm_merge_across_nodes != knob) { | |
2024 | if (ksm_pages_shared) | |
2025 | err = -EBUSY; | |
2026 | else | |
2027 | ksm_merge_across_nodes = knob; | |
2028 | } | |
2029 | mutex_unlock(&ksm_thread_mutex); | |
2030 | ||
2031 | return err ? err : count; | |
2032 | } | |
2033 | KSM_ATTR(merge_across_nodes); | |
2034 | #endif | |
2035 | ||
b4028260 HD |
2036 | static ssize_t pages_shared_show(struct kobject *kobj, |
2037 | struct kobj_attribute *attr, char *buf) | |
2038 | { | |
2039 | return sprintf(buf, "%lu\n", ksm_pages_shared); | |
2040 | } | |
2041 | KSM_ATTR_RO(pages_shared); | |
2042 | ||
2043 | static ssize_t pages_sharing_show(struct kobject *kobj, | |
2044 | struct kobj_attribute *attr, char *buf) | |
2045 | { | |
e178dfde | 2046 | return sprintf(buf, "%lu\n", ksm_pages_sharing); |
b4028260 HD |
2047 | } |
2048 | KSM_ATTR_RO(pages_sharing); | |
2049 | ||
473b0ce4 HD |
2050 | static ssize_t pages_unshared_show(struct kobject *kobj, |
2051 | struct kobj_attribute *attr, char *buf) | |
2052 | { | |
2053 | return sprintf(buf, "%lu\n", ksm_pages_unshared); | |
2054 | } | |
2055 | KSM_ATTR_RO(pages_unshared); | |
2056 | ||
2057 | static ssize_t pages_volatile_show(struct kobject *kobj, | |
2058 | struct kobj_attribute *attr, char *buf) | |
2059 | { | |
2060 | long ksm_pages_volatile; | |
2061 | ||
2062 | ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared | |
2063 | - ksm_pages_sharing - ksm_pages_unshared; | |
2064 | /* | |
2065 | * It was not worth any locking to calculate that statistic, | |
2066 | * but it might therefore sometimes be negative: conceal that. | |
2067 | */ | |
2068 | if (ksm_pages_volatile < 0) | |
2069 | ksm_pages_volatile = 0; | |
2070 | return sprintf(buf, "%ld\n", ksm_pages_volatile); | |
2071 | } | |
2072 | KSM_ATTR_RO(pages_volatile); | |
2073 | ||
2074 | static ssize_t full_scans_show(struct kobject *kobj, | |
2075 | struct kobj_attribute *attr, char *buf) | |
2076 | { | |
2077 | return sprintf(buf, "%lu\n", ksm_scan.seqnr); | |
2078 | } | |
2079 | KSM_ATTR_RO(full_scans); | |
2080 | ||
31dbd01f IE |
2081 | static struct attribute *ksm_attrs[] = { |
2082 | &sleep_millisecs_attr.attr, | |
2083 | &pages_to_scan_attr.attr, | |
2084 | &run_attr.attr, | |
b4028260 HD |
2085 | &pages_shared_attr.attr, |
2086 | &pages_sharing_attr.attr, | |
473b0ce4 HD |
2087 | &pages_unshared_attr.attr, |
2088 | &pages_volatile_attr.attr, | |
2089 | &full_scans_attr.attr, | |
90bd6fd3 PH |
2090 | #ifdef CONFIG_NUMA |
2091 | &merge_across_nodes_attr.attr, | |
2092 | #endif | |
31dbd01f IE |
2093 | NULL, |
2094 | }; | |
2095 | ||
2096 | static struct attribute_group ksm_attr_group = { | |
2097 | .attrs = ksm_attrs, | |
2098 | .name = "ksm", | |
2099 | }; | |
2ffd8679 | 2100 | #endif /* CONFIG_SYSFS */ |
31dbd01f IE |
2101 | |
2102 | static int __init ksm_init(void) | |
2103 | { | |
2104 | struct task_struct *ksm_thread; | |
2105 | int err; | |
90bd6fd3 | 2106 | int nid; |
31dbd01f IE |
2107 | |
2108 | err = ksm_slab_init(); | |
2109 | if (err) | |
2110 | goto out; | |
2111 | ||
90bd6fd3 PH |
2112 | for (nid = 0; nid < nr_node_ids; nid++) |
2113 | root_stable_tree[nid] = RB_ROOT; | |
2114 | ||
31dbd01f IE |
2115 | ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd"); |
2116 | if (IS_ERR(ksm_thread)) { | |
2117 | printk(KERN_ERR "ksm: creating kthread failed\n"); | |
2118 | err = PTR_ERR(ksm_thread); | |
d9f8984c | 2119 | goto out_free; |
31dbd01f IE |
2120 | } |
2121 | ||
2ffd8679 | 2122 | #ifdef CONFIG_SYSFS |
31dbd01f IE |
2123 | err = sysfs_create_group(mm_kobj, &ksm_attr_group); |
2124 | if (err) { | |
2125 | printk(KERN_ERR "ksm: register sysfs failed\n"); | |
2ffd8679 | 2126 | kthread_stop(ksm_thread); |
d9f8984c | 2127 | goto out_free; |
31dbd01f | 2128 | } |
c73602ad HD |
2129 | #else |
2130 | ksm_run = KSM_RUN_MERGE; /* no way for user to start it */ | |
2131 | ||
2ffd8679 | 2132 | #endif /* CONFIG_SYSFS */ |
31dbd01f | 2133 | |
62b61f61 HD |
2134 | #ifdef CONFIG_MEMORY_HOTREMOVE |
2135 | /* | |
2136 | * Choose a high priority since the callback takes ksm_thread_mutex: | |
2137 | * later callbacks could only be taking locks which nest within that. | |
2138 | */ | |
2139 | hotplug_memory_notifier(ksm_memory_callback, 100); | |
2140 | #endif | |
31dbd01f IE |
2141 | return 0; |
2142 | ||
d9f8984c | 2143 | out_free: |
31dbd01f IE |
2144 | ksm_slab_free(); |
2145 | out: | |
2146 | return err; | |
f8af4da3 | 2147 | } |
31dbd01f | 2148 | module_init(ksm_init) |