]>
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.) | |
8fdb3dbf HD |
90 | * |
91 | * If the merge_across_nodes tunable is unset, then KSM maintains multiple | |
92 | * stable trees and multiple unstable trees: one of each for each NUMA node. | |
31dbd01f IE |
93 | */ |
94 | ||
95 | /** | |
96 | * struct mm_slot - ksm information per mm that is being scanned | |
97 | * @link: link to the mm_slots hash list | |
98 | * @mm_list: link into the mm_slots list, rooted in ksm_mm_head | |
6514d511 | 99 | * @rmap_list: head for this mm_slot's singly-linked list of rmap_items |
31dbd01f IE |
100 | * @mm: the mm that this information is valid for |
101 | */ | |
102 | struct mm_slot { | |
103 | struct hlist_node link; | |
104 | struct list_head mm_list; | |
6514d511 | 105 | struct rmap_item *rmap_list; |
31dbd01f IE |
106 | struct mm_struct *mm; |
107 | }; | |
108 | ||
109 | /** | |
110 | * struct ksm_scan - cursor for scanning | |
111 | * @mm_slot: the current mm_slot we are scanning | |
112 | * @address: the next address inside that to be scanned | |
6514d511 | 113 | * @rmap_list: link to the next rmap to be scanned in the rmap_list |
31dbd01f IE |
114 | * @seqnr: count of completed full scans (needed when removing unstable node) |
115 | * | |
116 | * There is only the one ksm_scan instance of this cursor structure. | |
117 | */ | |
118 | struct ksm_scan { | |
119 | struct mm_slot *mm_slot; | |
120 | unsigned long address; | |
6514d511 | 121 | struct rmap_item **rmap_list; |
31dbd01f IE |
122 | unsigned long seqnr; |
123 | }; | |
124 | ||
7b6ba2c7 HD |
125 | /** |
126 | * struct stable_node - node of the stable rbtree | |
127 | * @node: rb node of this ksm page in the stable tree | |
4146d2d6 | 128 | * @head: (overlaying parent) &migrate_nodes indicates temporarily on that list |
731b565d | 129 | * @hlist_dup: linked into the stable_node->hlist with a stable_node chain |
4146d2d6 | 130 | * @list: linked into migrate_nodes, pending placement in the proper node tree |
7b6ba2c7 | 131 | * @hlist: hlist head of rmap_items using this ksm page |
4146d2d6 | 132 | * @kpfn: page frame number of this ksm page (perhaps temporarily on wrong nid) |
731b565d AA |
133 | * @chain_prune_time: time of the last full garbage collection |
134 | * @rmap_hlist_len: number of rmap_item entries in hlist or STABLE_NODE_CHAIN | |
4146d2d6 | 135 | * @nid: NUMA node id of stable tree in which linked (may not match kpfn) |
7b6ba2c7 HD |
136 | */ |
137 | struct stable_node { | |
4146d2d6 HD |
138 | union { |
139 | struct rb_node node; /* when node of stable tree */ | |
140 | struct { /* when listed for migration */ | |
141 | struct list_head *head; | |
731b565d AA |
142 | struct { |
143 | struct hlist_node hlist_dup; | |
144 | struct list_head list; | |
145 | }; | |
4146d2d6 HD |
146 | }; |
147 | }; | |
7b6ba2c7 | 148 | struct hlist_head hlist; |
731b565d AA |
149 | union { |
150 | unsigned long kpfn; | |
151 | unsigned long chain_prune_time; | |
152 | }; | |
153 | /* | |
154 | * STABLE_NODE_CHAIN can be any negative number in | |
155 | * rmap_hlist_len negative range, but better not -1 to be able | |
156 | * to reliably detect underflows. | |
157 | */ | |
158 | #define STABLE_NODE_CHAIN -1024 | |
159 | int rmap_hlist_len; | |
4146d2d6 HD |
160 | #ifdef CONFIG_NUMA |
161 | int nid; | |
162 | #endif | |
7b6ba2c7 HD |
163 | }; |
164 | ||
31dbd01f IE |
165 | /** |
166 | * struct rmap_item - reverse mapping item for virtual addresses | |
6514d511 | 167 | * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list |
db114b83 | 168 | * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree |
bc56620b | 169 | * @nid: NUMA node id of unstable tree in which linked (may not match page) |
31dbd01f IE |
170 | * @mm: the memory structure this rmap_item is pointing into |
171 | * @address: the virtual address this rmap_item tracks (+ flags in low bits) | |
172 | * @oldchecksum: previous checksum of the page at that virtual address | |
7b6ba2c7 HD |
173 | * @node: rb node of this rmap_item in the unstable tree |
174 | * @head: pointer to stable_node heading this list in the stable tree | |
175 | * @hlist: link into hlist of rmap_items hanging off that stable_node | |
31dbd01f IE |
176 | */ |
177 | struct rmap_item { | |
6514d511 | 178 | struct rmap_item *rmap_list; |
bc56620b HD |
179 | union { |
180 | struct anon_vma *anon_vma; /* when stable */ | |
181 | #ifdef CONFIG_NUMA | |
182 | int nid; /* when node of unstable tree */ | |
183 | #endif | |
184 | }; | |
31dbd01f IE |
185 | struct mm_struct *mm; |
186 | unsigned long address; /* + low bits used for flags below */ | |
7b6ba2c7 | 187 | unsigned int oldchecksum; /* when unstable */ |
31dbd01f | 188 | union { |
7b6ba2c7 HD |
189 | struct rb_node node; /* when node of unstable tree */ |
190 | struct { /* when listed from stable tree */ | |
191 | struct stable_node *head; | |
192 | struct hlist_node hlist; | |
193 | }; | |
31dbd01f IE |
194 | }; |
195 | }; | |
196 | ||
197 | #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */ | |
7b6ba2c7 HD |
198 | #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */ |
199 | #define STABLE_FLAG 0x200 /* is listed from the stable tree */ | |
31dbd01f IE |
200 | |
201 | /* The stable and unstable tree heads */ | |
ef53d16c HD |
202 | static struct rb_root one_stable_tree[1] = { RB_ROOT }; |
203 | static struct rb_root one_unstable_tree[1] = { RB_ROOT }; | |
204 | static struct rb_root *root_stable_tree = one_stable_tree; | |
205 | static struct rb_root *root_unstable_tree = one_unstable_tree; | |
31dbd01f | 206 | |
4146d2d6 HD |
207 | /* Recently migrated nodes of stable tree, pending proper placement */ |
208 | static LIST_HEAD(migrate_nodes); | |
731b565d | 209 | #define STABLE_NODE_DUP_HEAD ((struct list_head *)&migrate_nodes.prev) |
4146d2d6 | 210 | |
4ca3a69b SL |
211 | #define MM_SLOTS_HASH_BITS 10 |
212 | static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); | |
31dbd01f IE |
213 | |
214 | static struct mm_slot ksm_mm_head = { | |
215 | .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list), | |
216 | }; | |
217 | static struct ksm_scan ksm_scan = { | |
218 | .mm_slot = &ksm_mm_head, | |
219 | }; | |
220 | ||
221 | static struct kmem_cache *rmap_item_cache; | |
7b6ba2c7 | 222 | static struct kmem_cache *stable_node_cache; |
31dbd01f IE |
223 | static struct kmem_cache *mm_slot_cache; |
224 | ||
225 | /* The number of nodes in the stable tree */ | |
b4028260 | 226 | static unsigned long ksm_pages_shared; |
31dbd01f | 227 | |
e178dfde | 228 | /* The number of page slots additionally sharing those nodes */ |
b4028260 | 229 | static unsigned long ksm_pages_sharing; |
31dbd01f | 230 | |
473b0ce4 HD |
231 | /* The number of nodes in the unstable tree */ |
232 | static unsigned long ksm_pages_unshared; | |
233 | ||
234 | /* The number of rmap_items in use: to calculate pages_volatile */ | |
235 | static unsigned long ksm_rmap_items; | |
236 | ||
731b565d AA |
237 | /* The number of stable_node chains */ |
238 | static unsigned long ksm_stable_node_chains; | |
239 | ||
240 | /* The number of stable_node dups linked to the stable_node chains */ | |
241 | static unsigned long ksm_stable_node_dups; | |
242 | ||
243 | /* Delay in pruning stale stable_node_dups in the stable_node_chains */ | |
244 | static int ksm_stable_node_chains_prune_millisecs = 2000; | |
245 | ||
246 | /* Maximum number of page slots sharing a stable node */ | |
247 | static int ksm_max_page_sharing = 256; | |
248 | ||
31dbd01f | 249 | /* Number of pages ksmd should scan in one batch */ |
2c6854fd | 250 | static unsigned int ksm_thread_pages_to_scan = 100; |
31dbd01f IE |
251 | |
252 | /* Milliseconds ksmd should sleep between batches */ | |
2ffd8679 | 253 | static unsigned int ksm_thread_sleep_millisecs = 20; |
31dbd01f | 254 | |
e850dcf5 | 255 | #ifdef CONFIG_NUMA |
90bd6fd3 PH |
256 | /* Zeroed when merging across nodes is not allowed */ |
257 | static unsigned int ksm_merge_across_nodes = 1; | |
ef53d16c | 258 | static int ksm_nr_node_ids = 1; |
e850dcf5 HD |
259 | #else |
260 | #define ksm_merge_across_nodes 1U | |
ef53d16c | 261 | #define ksm_nr_node_ids 1 |
e850dcf5 | 262 | #endif |
90bd6fd3 | 263 | |
31dbd01f IE |
264 | #define KSM_RUN_STOP 0 |
265 | #define KSM_RUN_MERGE 1 | |
266 | #define KSM_RUN_UNMERGE 2 | |
ef4d43a8 HD |
267 | #define KSM_RUN_OFFLINE 4 |
268 | static unsigned long ksm_run = KSM_RUN_STOP; | |
269 | static void wait_while_offlining(void); | |
31dbd01f IE |
270 | |
271 | static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait); | |
272 | static DEFINE_MUTEX(ksm_thread_mutex); | |
273 | static DEFINE_SPINLOCK(ksm_mmlist_lock); | |
274 | ||
275 | #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\ | |
276 | sizeof(struct __struct), __alignof__(struct __struct),\ | |
277 | (__flags), NULL) | |
278 | ||
279 | static int __init ksm_slab_init(void) | |
280 | { | |
281 | rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0); | |
282 | if (!rmap_item_cache) | |
283 | goto out; | |
284 | ||
7b6ba2c7 HD |
285 | stable_node_cache = KSM_KMEM_CACHE(stable_node, 0); |
286 | if (!stable_node_cache) | |
287 | goto out_free1; | |
288 | ||
31dbd01f IE |
289 | mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0); |
290 | if (!mm_slot_cache) | |
7b6ba2c7 | 291 | goto out_free2; |
31dbd01f IE |
292 | |
293 | return 0; | |
294 | ||
7b6ba2c7 HD |
295 | out_free2: |
296 | kmem_cache_destroy(stable_node_cache); | |
297 | out_free1: | |
31dbd01f IE |
298 | kmem_cache_destroy(rmap_item_cache); |
299 | out: | |
300 | return -ENOMEM; | |
301 | } | |
302 | ||
303 | static void __init ksm_slab_free(void) | |
304 | { | |
305 | kmem_cache_destroy(mm_slot_cache); | |
7b6ba2c7 | 306 | kmem_cache_destroy(stable_node_cache); |
31dbd01f IE |
307 | kmem_cache_destroy(rmap_item_cache); |
308 | mm_slot_cache = NULL; | |
309 | } | |
310 | ||
731b565d AA |
311 | static __always_inline bool is_stable_node_chain(struct stable_node *chain) |
312 | { | |
313 | return chain->rmap_hlist_len == STABLE_NODE_CHAIN; | |
314 | } | |
315 | ||
316 | static __always_inline bool is_stable_node_dup(struct stable_node *dup) | |
317 | { | |
318 | return dup->head == STABLE_NODE_DUP_HEAD; | |
319 | } | |
320 | ||
321 | static inline void stable_node_chain_add_dup(struct stable_node *dup, | |
322 | struct stable_node *chain) | |
323 | { | |
324 | VM_BUG_ON(is_stable_node_dup(dup)); | |
325 | dup->head = STABLE_NODE_DUP_HEAD; | |
326 | VM_BUG_ON(!is_stable_node_chain(chain)); | |
327 | hlist_add_head(&dup->hlist_dup, &chain->hlist); | |
328 | ksm_stable_node_dups++; | |
329 | } | |
330 | ||
331 | static inline void __stable_node_dup_del(struct stable_node *dup) | |
332 | { | |
24f72136 | 333 | VM_BUG_ON(!is_stable_node_dup(dup)); |
731b565d AA |
334 | hlist_del(&dup->hlist_dup); |
335 | ksm_stable_node_dups--; | |
336 | } | |
337 | ||
338 | static inline void stable_node_dup_del(struct stable_node *dup) | |
339 | { | |
340 | VM_BUG_ON(is_stable_node_chain(dup)); | |
341 | if (is_stable_node_dup(dup)) | |
342 | __stable_node_dup_del(dup); | |
343 | else | |
344 | rb_erase(&dup->node, root_stable_tree + NUMA(dup->nid)); | |
345 | #ifdef CONFIG_DEBUG_VM | |
346 | dup->head = NULL; | |
347 | #endif | |
348 | } | |
349 | ||
31dbd01f IE |
350 | static inline struct rmap_item *alloc_rmap_item(void) |
351 | { | |
473b0ce4 HD |
352 | struct rmap_item *rmap_item; |
353 | ||
386429be | 354 | rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL | |
355 | __GFP_NORETRY | __GFP_NOWARN); | |
473b0ce4 HD |
356 | if (rmap_item) |
357 | ksm_rmap_items++; | |
358 | return rmap_item; | |
31dbd01f IE |
359 | } |
360 | ||
361 | static inline void free_rmap_item(struct rmap_item *rmap_item) | |
362 | { | |
473b0ce4 | 363 | ksm_rmap_items--; |
31dbd01f IE |
364 | rmap_item->mm = NULL; /* debug safety */ |
365 | kmem_cache_free(rmap_item_cache, rmap_item); | |
366 | } | |
367 | ||
7b6ba2c7 HD |
368 | static inline struct stable_node *alloc_stable_node(void) |
369 | { | |
370 | return kmem_cache_alloc(stable_node_cache, GFP_KERNEL); | |
371 | } | |
372 | ||
373 | static inline void free_stable_node(struct stable_node *stable_node) | |
374 | { | |
731b565d AA |
375 | VM_BUG_ON(stable_node->rmap_hlist_len && |
376 | !is_stable_node_chain(stable_node)); | |
7b6ba2c7 HD |
377 | kmem_cache_free(stable_node_cache, stable_node); |
378 | } | |
379 | ||
31dbd01f IE |
380 | static inline struct mm_slot *alloc_mm_slot(void) |
381 | { | |
382 | if (!mm_slot_cache) /* initialization failed */ | |
383 | return NULL; | |
384 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); | |
385 | } | |
386 | ||
387 | static inline void free_mm_slot(struct mm_slot *mm_slot) | |
388 | { | |
389 | kmem_cache_free(mm_slot_cache, mm_slot); | |
390 | } | |
391 | ||
31dbd01f IE |
392 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
393 | { | |
4ca3a69b SL |
394 | struct mm_slot *slot; |
395 | ||
b67bfe0d | 396 | hash_for_each_possible(mm_slots_hash, slot, link, (unsigned long)mm) |
4ca3a69b SL |
397 | if (slot->mm == mm) |
398 | return slot; | |
31dbd01f | 399 | |
31dbd01f IE |
400 | return NULL; |
401 | } | |
402 | ||
403 | static void insert_to_mm_slots_hash(struct mm_struct *mm, | |
404 | struct mm_slot *mm_slot) | |
405 | { | |
31dbd01f | 406 | mm_slot->mm = mm; |
4ca3a69b | 407 | hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm); |
31dbd01f IE |
408 | } |
409 | ||
a913e182 HD |
410 | /* |
411 | * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's | |
412 | * page tables after it has passed through ksm_exit() - which, if necessary, | |
413 | * takes mmap_sem briefly to serialize against them. ksm_exit() does not set | |
414 | * a special flag: they can just back out as soon as mm_users goes to zero. | |
415 | * ksm_test_exit() is used throughout to make this test for exit: in some | |
416 | * places for correctness, in some places just to avoid unnecessary work. | |
417 | */ | |
418 | static inline bool ksm_test_exit(struct mm_struct *mm) | |
419 | { | |
420 | return atomic_read(&mm->mm_users) == 0; | |
421 | } | |
422 | ||
31dbd01f IE |
423 | /* |
424 | * We use break_ksm to break COW on a ksm page: it's a stripped down | |
425 | * | |
426 | * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1) | |
427 | * put_page(page); | |
428 | * | |
429 | * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma, | |
430 | * in case the application has unmapped and remapped mm,addr meanwhile. | |
431 | * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP | |
432 | * mmap of /dev/mem or /dev/kmem, where we would not want to touch it. | |
433 | */ | |
d952b791 | 434 | static int break_ksm(struct vm_area_struct *vma, unsigned long addr) |
31dbd01f IE |
435 | { |
436 | struct page *page; | |
d952b791 | 437 | int ret = 0; |
31dbd01f IE |
438 | |
439 | do { | |
440 | cond_resched(); | |
5117b3b8 | 441 | page = follow_page(vma, addr, FOLL_GET | FOLL_MIGRATION); |
22eccdd7 | 442 | if (IS_ERR_OR_NULL(page)) |
31dbd01f IE |
443 | break; |
444 | if (PageKsm(page)) | |
445 | ret = handle_mm_fault(vma->vm_mm, vma, addr, | |
446 | FAULT_FLAG_WRITE); | |
447 | else | |
448 | ret = VM_FAULT_WRITE; | |
449 | put_page(page); | |
33692f27 | 450 | } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | VM_FAULT_OOM))); |
d952b791 HD |
451 | /* |
452 | * We must loop because handle_mm_fault() may back out if there's | |
453 | * any difficulty e.g. if pte accessed bit gets updated concurrently. | |
454 | * | |
455 | * VM_FAULT_WRITE is what we have been hoping for: it indicates that | |
456 | * COW has been broken, even if the vma does not permit VM_WRITE; | |
457 | * but note that a concurrent fault might break PageKsm for us. | |
458 | * | |
459 | * VM_FAULT_SIGBUS could occur if we race with truncation of the | |
460 | * backing file, which also invalidates anonymous pages: that's | |
461 | * okay, that truncation will have unmapped the PageKsm for us. | |
462 | * | |
463 | * VM_FAULT_OOM: at the time of writing (late July 2009), setting | |
464 | * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the | |
465 | * current task has TIF_MEMDIE set, and will be OOM killed on return | |
466 | * to user; and ksmd, having no mm, would never be chosen for that. | |
467 | * | |
468 | * But if the mm is in a limited mem_cgroup, then the fault may fail | |
469 | * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and | |
470 | * even ksmd can fail in this way - though it's usually breaking ksm | |
471 | * just to undo a merge it made a moment before, so unlikely to oom. | |
472 | * | |
473 | * That's a pity: we might therefore have more kernel pages allocated | |
474 | * than we're counting as nodes in the stable tree; but ksm_do_scan | |
475 | * will retry to break_cow on each pass, so should recover the page | |
476 | * in due course. The important thing is to not let VM_MERGEABLE | |
477 | * be cleared while any such pages might remain in the area. | |
478 | */ | |
479 | return (ret & VM_FAULT_OOM) ? -ENOMEM : 0; | |
31dbd01f IE |
480 | } |
481 | ||
ef694222 BL |
482 | static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm, |
483 | unsigned long addr) | |
484 | { | |
485 | struct vm_area_struct *vma; | |
486 | if (ksm_test_exit(mm)) | |
487 | return NULL; | |
488 | vma = find_vma(mm, addr); | |
489 | if (!vma || vma->vm_start > addr) | |
490 | return NULL; | |
491 | if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) | |
492 | return NULL; | |
493 | return vma; | |
494 | } | |
495 | ||
8dd3557a | 496 | static void break_cow(struct rmap_item *rmap_item) |
31dbd01f | 497 | { |
8dd3557a HD |
498 | struct mm_struct *mm = rmap_item->mm; |
499 | unsigned long addr = rmap_item->address; | |
31dbd01f IE |
500 | struct vm_area_struct *vma; |
501 | ||
4035c07a HD |
502 | /* |
503 | * It is not an accident that whenever we want to break COW | |
504 | * to undo, we also need to drop a reference to the anon_vma. | |
505 | */ | |
9e60109f | 506 | put_anon_vma(rmap_item->anon_vma); |
4035c07a | 507 | |
81464e30 | 508 | down_read(&mm->mmap_sem); |
ef694222 BL |
509 | vma = find_mergeable_vma(mm, addr); |
510 | if (vma) | |
511 | break_ksm(vma, addr); | |
31dbd01f IE |
512 | up_read(&mm->mmap_sem); |
513 | } | |
514 | ||
29ad768c AA |
515 | static struct page *page_trans_compound_anon(struct page *page) |
516 | { | |
517 | if (PageTransCompound(page)) { | |
668f9abb | 518 | struct page *head = compound_head(page); |
29ad768c | 519 | /* |
22e5c47e AA |
520 | * head may actually be splitted and freed from under |
521 | * us but it's ok here. | |
29ad768c | 522 | */ |
29ad768c AA |
523 | if (PageAnon(head)) |
524 | return head; | |
525 | } | |
526 | return NULL; | |
527 | } | |
528 | ||
31dbd01f IE |
529 | static struct page *get_mergeable_page(struct rmap_item *rmap_item) |
530 | { | |
531 | struct mm_struct *mm = rmap_item->mm; | |
532 | unsigned long addr = rmap_item->address; | |
533 | struct vm_area_struct *vma; | |
534 | struct page *page; | |
535 | ||
536 | down_read(&mm->mmap_sem); | |
ef694222 BL |
537 | vma = find_mergeable_vma(mm, addr); |
538 | if (!vma) | |
31dbd01f IE |
539 | goto out; |
540 | ||
541 | page = follow_page(vma, addr, FOLL_GET); | |
22eccdd7 | 542 | if (IS_ERR_OR_NULL(page)) |
31dbd01f | 543 | goto out; |
29ad768c | 544 | if (PageAnon(page) || page_trans_compound_anon(page)) { |
31dbd01f IE |
545 | flush_anon_page(vma, page, addr); |
546 | flush_dcache_page(page); | |
547 | } else { | |
548 | put_page(page); | |
c8f95ed1 AA |
549 | out: |
550 | page = NULL; | |
31dbd01f IE |
551 | } |
552 | up_read(&mm->mmap_sem); | |
553 | return page; | |
554 | } | |
555 | ||
90bd6fd3 PH |
556 | /* |
557 | * This helper is used for getting right index into array of tree roots. | |
558 | * When merge_across_nodes knob is set to 1, there are only two rb-trees for | |
559 | * stable and unstable pages from all nodes with roots in index 0. Otherwise, | |
560 | * every node has its own stable and unstable tree. | |
561 | */ | |
562 | static inline int get_kpfn_nid(unsigned long kpfn) | |
563 | { | |
d8fc16a8 | 564 | return ksm_merge_across_nodes ? 0 : NUMA(pfn_to_nid(kpfn)); |
90bd6fd3 PH |
565 | } |
566 | ||
731b565d AA |
567 | static struct stable_node *alloc_stable_node_chain(struct stable_node *dup, |
568 | struct rb_root *root) | |
569 | { | |
570 | struct stable_node *chain = alloc_stable_node(); | |
571 | VM_BUG_ON(is_stable_node_chain(dup)); | |
572 | if (likely(chain)) { | |
573 | INIT_HLIST_HEAD(&chain->hlist); | |
574 | chain->chain_prune_time = jiffies; | |
575 | chain->rmap_hlist_len = STABLE_NODE_CHAIN; | |
576 | #if defined (CONFIG_DEBUG_VM) && defined(CONFIG_NUMA) | |
577 | chain->nid = -1; /* debug */ | |
578 | #endif | |
579 | ksm_stable_node_chains++; | |
580 | ||
581 | /* | |
582 | * Put the stable node chain in the first dimension of | |
583 | * the stable tree and at the same time remove the old | |
584 | * stable node. | |
585 | */ | |
586 | rb_replace_node(&dup->node, &chain->node, root); | |
587 | ||
588 | /* | |
589 | * Move the old stable node to the second dimension | |
590 | * queued in the hlist_dup. The invariant is that all | |
591 | * dup stable_nodes in the chain->hlist point to pages | |
592 | * that are wrprotected and have the exact same | |
593 | * content. | |
594 | */ | |
595 | stable_node_chain_add_dup(dup, chain); | |
596 | } | |
597 | return chain; | |
598 | } | |
599 | ||
600 | static inline void free_stable_node_chain(struct stable_node *chain, | |
601 | struct rb_root *root) | |
602 | { | |
603 | rb_erase(&chain->node, root); | |
604 | free_stable_node(chain); | |
605 | ksm_stable_node_chains--; | |
606 | } | |
607 | ||
4035c07a HD |
608 | static void remove_node_from_stable_tree(struct stable_node *stable_node) |
609 | { | |
610 | struct rmap_item *rmap_item; | |
4035c07a | 611 | |
731b565d AA |
612 | /* check it's not STABLE_NODE_CHAIN or negative */ |
613 | BUG_ON(stable_node->rmap_hlist_len < 0); | |
614 | ||
b67bfe0d | 615 | hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) { |
4035c07a HD |
616 | if (rmap_item->hlist.next) |
617 | ksm_pages_sharing--; | |
618 | else | |
619 | ksm_pages_shared--; | |
731b565d AA |
620 | VM_BUG_ON(stable_node->rmap_hlist_len <= 0); |
621 | stable_node->rmap_hlist_len--; | |
9e60109f | 622 | put_anon_vma(rmap_item->anon_vma); |
4035c07a HD |
623 | rmap_item->address &= PAGE_MASK; |
624 | cond_resched(); | |
625 | } | |
626 | ||
731b565d AA |
627 | /* |
628 | * We need the second aligned pointer of the migrate_nodes | |
629 | * list_head to stay clear from the rb_parent_color union | |
630 | * (aligned and different than any node) and also different | |
631 | * from &migrate_nodes. This will verify that future list.h changes | |
632 | * don't break STABLE_NODE_DUP_HEAD. | |
633 | */ | |
634 | #if GCC_VERSION >= 40903 /* only recent gcc can handle it */ | |
635 | BUILD_BUG_ON(STABLE_NODE_DUP_HEAD <= &migrate_nodes); | |
636 | BUILD_BUG_ON(STABLE_NODE_DUP_HEAD >= &migrate_nodes + 1); | |
637 | #endif | |
638 | ||
4146d2d6 HD |
639 | if (stable_node->head == &migrate_nodes) |
640 | list_del(&stable_node->list); | |
641 | else | |
731b565d | 642 | stable_node_dup_del(stable_node); |
4035c07a HD |
643 | free_stable_node(stable_node); |
644 | } | |
645 | ||
646 | /* | |
647 | * get_ksm_page: checks if the page indicated by the stable node | |
648 | * is still its ksm page, despite having held no reference to it. | |
649 | * In which case we can trust the content of the page, and it | |
650 | * returns the gotten page; but if the page has now been zapped, | |
651 | * remove the stale node from the stable tree and return NULL. | |
c8d6553b | 652 | * But beware, the stable node's page might be being migrated. |
4035c07a HD |
653 | * |
654 | * You would expect the stable_node to hold a reference to the ksm page. | |
655 | * But if it increments the page's count, swapping out has to wait for | |
656 | * ksmd to come around again before it can free the page, which may take | |
657 | * seconds or even minutes: much too unresponsive. So instead we use a | |
658 | * "keyhole reference": access to the ksm page from the stable node peeps | |
659 | * out through its keyhole to see if that page still holds the right key, | |
660 | * pointing back to this stable node. This relies on freeing a PageAnon | |
661 | * page to reset its page->mapping to NULL, and relies on no other use of | |
662 | * a page to put something that might look like our key in page->mapping. | |
4035c07a HD |
663 | * is on its way to being freed; but it is an anomaly to bear in mind. |
664 | */ | |
8fdb3dbf | 665 | static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it) |
4035c07a HD |
666 | { |
667 | struct page *page; | |
668 | void *expected_mapping; | |
c8d6553b | 669 | unsigned long kpfn; |
4035c07a | 670 | |
4035c07a HD |
671 | expected_mapping = (void *)stable_node + |
672 | (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM); | |
c8d6553b | 673 | again: |
4db0c3c2 | 674 | kpfn = READ_ONCE(stable_node->kpfn); |
c8d6553b HD |
675 | page = pfn_to_page(kpfn); |
676 | ||
677 | /* | |
678 | * page is computed from kpfn, so on most architectures reading | |
679 | * page->mapping is naturally ordered after reading node->kpfn, | |
680 | * but on Alpha we need to be more careful. | |
681 | */ | |
682 | smp_read_barrier_depends(); | |
4db0c3c2 | 683 | if (READ_ONCE(page->mapping) != expected_mapping) |
4035c07a | 684 | goto stale; |
c8d6553b HD |
685 | |
686 | /* | |
687 | * We cannot do anything with the page while its refcount is 0. | |
688 | * Usually 0 means free, or tail of a higher-order page: in which | |
689 | * case this node is no longer referenced, and should be freed; | |
690 | * however, it might mean that the page is under page_freeze_refs(). | |
691 | * The __remove_mapping() case is easy, again the node is now stale; | |
692 | * but if page is swapcache in migrate_page_move_mapping(), it might | |
693 | * still be our page, in which case it's essential to keep the node. | |
694 | */ | |
695 | while (!get_page_unless_zero(page)) { | |
696 | /* | |
697 | * Another check for page->mapping != expected_mapping would | |
698 | * work here too. We have chosen the !PageSwapCache test to | |
699 | * optimize the common case, when the page is or is about to | |
700 | * be freed: PageSwapCache is cleared (under spin_lock_irq) | |
701 | * in the freeze_refs section of __remove_mapping(); but Anon | |
702 | * page->mapping reset to NULL later, in free_pages_prepare(). | |
703 | */ | |
704 | if (!PageSwapCache(page)) | |
705 | goto stale; | |
706 | cpu_relax(); | |
707 | } | |
708 | ||
4db0c3c2 | 709 | if (READ_ONCE(page->mapping) != expected_mapping) { |
4035c07a HD |
710 | put_page(page); |
711 | goto stale; | |
712 | } | |
c8d6553b | 713 | |
8fdb3dbf | 714 | if (lock_it) { |
8aafa6a4 | 715 | lock_page(page); |
4db0c3c2 | 716 | if (READ_ONCE(page->mapping) != expected_mapping) { |
8aafa6a4 HD |
717 | unlock_page(page); |
718 | put_page(page); | |
719 | goto stale; | |
720 | } | |
721 | } | |
4035c07a | 722 | return page; |
c8d6553b | 723 | |
4035c07a | 724 | stale: |
c8d6553b HD |
725 | /* |
726 | * We come here from above when page->mapping or !PageSwapCache | |
727 | * suggests that the node is stale; but it might be under migration. | |
728 | * We need smp_rmb(), matching the smp_wmb() in ksm_migrate_page(), | |
729 | * before checking whether node->kpfn has been changed. | |
730 | */ | |
731 | smp_rmb(); | |
4db0c3c2 | 732 | if (READ_ONCE(stable_node->kpfn) != kpfn) |
c8d6553b | 733 | goto again; |
4035c07a HD |
734 | remove_node_from_stable_tree(stable_node); |
735 | return NULL; | |
736 | } | |
737 | ||
31dbd01f IE |
738 | /* |
739 | * Removing rmap_item from stable or unstable tree. | |
740 | * This function will clean the information from the stable/unstable tree. | |
741 | */ | |
742 | static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) | |
743 | { | |
7b6ba2c7 HD |
744 | if (rmap_item->address & STABLE_FLAG) { |
745 | struct stable_node *stable_node; | |
5ad64688 | 746 | struct page *page; |
31dbd01f | 747 | |
7b6ba2c7 | 748 | stable_node = rmap_item->head; |
8aafa6a4 | 749 | page = get_ksm_page(stable_node, true); |
4035c07a HD |
750 | if (!page) |
751 | goto out; | |
5ad64688 | 752 | |
7b6ba2c7 | 753 | hlist_del(&rmap_item->hlist); |
4035c07a HD |
754 | unlock_page(page); |
755 | put_page(page); | |
08beca44 | 756 | |
98666f8a | 757 | if (!hlist_empty(&stable_node->hlist)) |
4035c07a HD |
758 | ksm_pages_sharing--; |
759 | else | |
7b6ba2c7 | 760 | ksm_pages_shared--; |
731b565d AA |
761 | VM_BUG_ON(stable_node->rmap_hlist_len <= 0); |
762 | stable_node->rmap_hlist_len--; | |
31dbd01f | 763 | |
9e60109f | 764 | put_anon_vma(rmap_item->anon_vma); |
93d17715 | 765 | rmap_item->address &= PAGE_MASK; |
31dbd01f | 766 | |
7b6ba2c7 | 767 | } else if (rmap_item->address & UNSTABLE_FLAG) { |
31dbd01f IE |
768 | unsigned char age; |
769 | /* | |
9ba69294 | 770 | * Usually ksmd can and must skip the rb_erase, because |
31dbd01f | 771 | * root_unstable_tree was already reset to RB_ROOT. |
9ba69294 HD |
772 | * But be careful when an mm is exiting: do the rb_erase |
773 | * if this rmap_item was inserted by this scan, rather | |
774 | * than left over from before. | |
31dbd01f IE |
775 | */ |
776 | age = (unsigned char)(ksm_scan.seqnr - rmap_item->address); | |
cd551f97 | 777 | BUG_ON(age > 1); |
31dbd01f | 778 | if (!age) |
90bd6fd3 | 779 | rb_erase(&rmap_item->node, |
ef53d16c | 780 | root_unstable_tree + NUMA(rmap_item->nid)); |
473b0ce4 | 781 | ksm_pages_unshared--; |
93d17715 | 782 | rmap_item->address &= PAGE_MASK; |
31dbd01f | 783 | } |
4035c07a | 784 | out: |
31dbd01f IE |
785 | cond_resched(); /* we're called from many long loops */ |
786 | } | |
787 | ||
31dbd01f | 788 | static void remove_trailing_rmap_items(struct mm_slot *mm_slot, |
6514d511 | 789 | struct rmap_item **rmap_list) |
31dbd01f | 790 | { |
6514d511 HD |
791 | while (*rmap_list) { |
792 | struct rmap_item *rmap_item = *rmap_list; | |
793 | *rmap_list = rmap_item->rmap_list; | |
31dbd01f | 794 | remove_rmap_item_from_tree(rmap_item); |
31dbd01f IE |
795 | free_rmap_item(rmap_item); |
796 | } | |
797 | } | |
798 | ||
799 | /* | |
e850dcf5 | 800 | * Though it's very tempting to unmerge rmap_items from stable tree rather |
31dbd01f IE |
801 | * than check every pte of a given vma, the locking doesn't quite work for |
802 | * that - an rmap_item is assigned to the stable tree after inserting ksm | |
803 | * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing | |
804 | * rmap_items from parent to child at fork time (so as not to waste time | |
805 | * if exit comes before the next scan reaches it). | |
81464e30 HD |
806 | * |
807 | * Similarly, although we'd like to remove rmap_items (so updating counts | |
808 | * and freeing memory) when unmerging an area, it's easier to leave that | |
809 | * to the next pass of ksmd - consider, for example, how ksmd might be | |
810 | * in cmp_and_merge_page on one of the rmap_items we would be removing. | |
31dbd01f | 811 | */ |
d952b791 HD |
812 | static int unmerge_ksm_pages(struct vm_area_struct *vma, |
813 | unsigned long start, unsigned long end) | |
31dbd01f IE |
814 | { |
815 | unsigned long addr; | |
d952b791 | 816 | int err = 0; |
31dbd01f | 817 | |
d952b791 | 818 | for (addr = start; addr < end && !err; addr += PAGE_SIZE) { |
9ba69294 HD |
819 | if (ksm_test_exit(vma->vm_mm)) |
820 | break; | |
d952b791 HD |
821 | if (signal_pending(current)) |
822 | err = -ERESTARTSYS; | |
823 | else | |
824 | err = break_ksm(vma, addr); | |
825 | } | |
826 | return err; | |
31dbd01f IE |
827 | } |
828 | ||
2ffd8679 HD |
829 | #ifdef CONFIG_SYSFS |
830 | /* | |
831 | * Only called through the sysfs control interface: | |
832 | */ | |
cbf86cfe HD |
833 | static int remove_stable_node(struct stable_node *stable_node) |
834 | { | |
835 | struct page *page; | |
836 | int err; | |
837 | ||
838 | page = get_ksm_page(stable_node, true); | |
839 | if (!page) { | |
840 | /* | |
841 | * get_ksm_page did remove_node_from_stable_tree itself. | |
842 | */ | |
843 | return 0; | |
844 | } | |
845 | ||
8fdb3dbf HD |
846 | if (WARN_ON_ONCE(page_mapped(page))) { |
847 | /* | |
848 | * This should not happen: but if it does, just refuse to let | |
849 | * merge_across_nodes be switched - there is no need to panic. | |
850 | */ | |
cbf86cfe | 851 | err = -EBUSY; |
8fdb3dbf | 852 | } else { |
cbf86cfe | 853 | /* |
8fdb3dbf HD |
854 | * The stable node did not yet appear stale to get_ksm_page(), |
855 | * since that allows for an unmapped ksm page to be recognized | |
856 | * right up until it is freed; but the node is safe to remove. | |
cbf86cfe HD |
857 | * This page might be in a pagevec waiting to be freed, |
858 | * or it might be PageSwapCache (perhaps under writeback), | |
859 | * or it might have been removed from swapcache a moment ago. | |
860 | */ | |
861 | set_page_stable_node(page, NULL); | |
862 | remove_node_from_stable_tree(stable_node); | |
863 | err = 0; | |
864 | } | |
865 | ||
866 | unlock_page(page); | |
867 | put_page(page); | |
868 | return err; | |
869 | } | |
870 | ||
731b565d AA |
871 | static int remove_stable_node_chain(struct stable_node *stable_node, |
872 | struct rb_root *root) | |
873 | { | |
874 | struct stable_node *dup; | |
875 | struct hlist_node *hlist_safe; | |
876 | ||
877 | if (!is_stable_node_chain(stable_node)) { | |
878 | VM_BUG_ON(is_stable_node_dup(stable_node)); | |
879 | if (remove_stable_node(stable_node)) | |
880 | return true; | |
881 | else | |
882 | return false; | |
883 | } | |
884 | ||
885 | hlist_for_each_entry_safe(dup, hlist_safe, | |
886 | &stable_node->hlist, hlist_dup) { | |
887 | VM_BUG_ON(!is_stable_node_dup(dup)); | |
888 | if (remove_stable_node(dup)) | |
889 | return true; | |
890 | } | |
891 | BUG_ON(!hlist_empty(&stable_node->hlist)); | |
892 | free_stable_node_chain(stable_node, root); | |
893 | return false; | |
894 | } | |
895 | ||
cbf86cfe HD |
896 | static int remove_all_stable_nodes(void) |
897 | { | |
898 | struct stable_node *stable_node; | |
4146d2d6 | 899 | struct list_head *this, *next; |
cbf86cfe HD |
900 | int nid; |
901 | int err = 0; | |
902 | ||
ef53d16c | 903 | for (nid = 0; nid < ksm_nr_node_ids; nid++) { |
cbf86cfe HD |
904 | while (root_stable_tree[nid].rb_node) { |
905 | stable_node = rb_entry(root_stable_tree[nid].rb_node, | |
906 | struct stable_node, node); | |
731b565d AA |
907 | if (remove_stable_node_chain(stable_node, |
908 | root_stable_tree + nid)) { | |
cbf86cfe HD |
909 | err = -EBUSY; |
910 | break; /* proceed to next nid */ | |
911 | } | |
912 | cond_resched(); | |
913 | } | |
914 | } | |
4146d2d6 HD |
915 | list_for_each_safe(this, next, &migrate_nodes) { |
916 | stable_node = list_entry(this, struct stable_node, list); | |
917 | if (remove_stable_node(stable_node)) | |
918 | err = -EBUSY; | |
919 | cond_resched(); | |
920 | } | |
cbf86cfe HD |
921 | return err; |
922 | } | |
923 | ||
d952b791 | 924 | static int unmerge_and_remove_all_rmap_items(void) |
31dbd01f IE |
925 | { |
926 | struct mm_slot *mm_slot; | |
927 | struct mm_struct *mm; | |
928 | struct vm_area_struct *vma; | |
d952b791 HD |
929 | int err = 0; |
930 | ||
931 | spin_lock(&ksm_mmlist_lock); | |
9ba69294 | 932 | ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next, |
d952b791 HD |
933 | struct mm_slot, mm_list); |
934 | spin_unlock(&ksm_mmlist_lock); | |
31dbd01f | 935 | |
9ba69294 HD |
936 | for (mm_slot = ksm_scan.mm_slot; |
937 | mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) { | |
31dbd01f IE |
938 | mm = mm_slot->mm; |
939 | down_read(&mm->mmap_sem); | |
940 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
9ba69294 HD |
941 | if (ksm_test_exit(mm)) |
942 | break; | |
31dbd01f IE |
943 | if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) |
944 | continue; | |
d952b791 HD |
945 | err = unmerge_ksm_pages(vma, |
946 | vma->vm_start, vma->vm_end); | |
9ba69294 HD |
947 | if (err) |
948 | goto error; | |
31dbd01f | 949 | } |
9ba69294 | 950 | |
6514d511 | 951 | remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list); |
d952b791 HD |
952 | |
953 | spin_lock(&ksm_mmlist_lock); | |
9ba69294 | 954 | ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next, |
d952b791 | 955 | struct mm_slot, mm_list); |
9ba69294 | 956 | if (ksm_test_exit(mm)) { |
4ca3a69b | 957 | hash_del(&mm_slot->link); |
9ba69294 HD |
958 | list_del(&mm_slot->mm_list); |
959 | spin_unlock(&ksm_mmlist_lock); | |
960 | ||
961 | free_mm_slot(mm_slot); | |
962 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | |
963 | up_read(&mm->mmap_sem); | |
964 | mmdrop(mm); | |
965 | } else { | |
966 | spin_unlock(&ksm_mmlist_lock); | |
967 | up_read(&mm->mmap_sem); | |
968 | } | |
31dbd01f IE |
969 | } |
970 | ||
cbf86cfe HD |
971 | /* Clean up stable nodes, but don't worry if some are still busy */ |
972 | remove_all_stable_nodes(); | |
d952b791 | 973 | ksm_scan.seqnr = 0; |
9ba69294 HD |
974 | return 0; |
975 | ||
976 | error: | |
977 | up_read(&mm->mmap_sem); | |
31dbd01f | 978 | spin_lock(&ksm_mmlist_lock); |
d952b791 | 979 | ksm_scan.mm_slot = &ksm_mm_head; |
31dbd01f | 980 | spin_unlock(&ksm_mmlist_lock); |
d952b791 | 981 | return err; |
31dbd01f | 982 | } |
2ffd8679 | 983 | #endif /* CONFIG_SYSFS */ |
31dbd01f | 984 | |
31dbd01f IE |
985 | static u32 calc_checksum(struct page *page) |
986 | { | |
987 | u32 checksum; | |
9b04c5fe | 988 | void *addr = kmap_atomic(page); |
31dbd01f | 989 | checksum = jhash2(addr, PAGE_SIZE / 4, 17); |
9b04c5fe | 990 | kunmap_atomic(addr); |
31dbd01f IE |
991 | return checksum; |
992 | } | |
993 | ||
994 | static int memcmp_pages(struct page *page1, struct page *page2) | |
995 | { | |
996 | char *addr1, *addr2; | |
997 | int ret; | |
998 | ||
9b04c5fe CW |
999 | addr1 = kmap_atomic(page1); |
1000 | addr2 = kmap_atomic(page2); | |
31dbd01f | 1001 | ret = memcmp(addr1, addr2, PAGE_SIZE); |
9b04c5fe CW |
1002 | kunmap_atomic(addr2); |
1003 | kunmap_atomic(addr1); | |
31dbd01f IE |
1004 | return ret; |
1005 | } | |
1006 | ||
1007 | static inline int pages_identical(struct page *page1, struct page *page2) | |
1008 | { | |
1009 | return !memcmp_pages(page1, page2); | |
1010 | } | |
1011 | ||
1012 | static int write_protect_page(struct vm_area_struct *vma, struct page *page, | |
1013 | pte_t *orig_pte) | |
1014 | { | |
1015 | struct mm_struct *mm = vma->vm_mm; | |
1016 | unsigned long addr; | |
1017 | pte_t *ptep; | |
1018 | spinlock_t *ptl; | |
1019 | int swapped; | |
1020 | int err = -EFAULT; | |
6bdb913f HE |
1021 | unsigned long mmun_start; /* For mmu_notifiers */ |
1022 | unsigned long mmun_end; /* For mmu_notifiers */ | |
31dbd01f IE |
1023 | |
1024 | addr = page_address_in_vma(page, vma); | |
1025 | if (addr == -EFAULT) | |
1026 | goto out; | |
1027 | ||
29ad768c | 1028 | BUG_ON(PageTransCompound(page)); |
6bdb913f HE |
1029 | |
1030 | mmun_start = addr; | |
1031 | mmun_end = addr + PAGE_SIZE; | |
1032 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
1033 | ||
31dbd01f IE |
1034 | ptep = page_check_address(page, mm, addr, &ptl, 0); |
1035 | if (!ptep) | |
6bdb913f | 1036 | goto out_mn; |
31dbd01f | 1037 | |
4e31635c | 1038 | if (pte_write(*ptep) || pte_dirty(*ptep)) { |
31dbd01f IE |
1039 | pte_t entry; |
1040 | ||
1041 | swapped = PageSwapCache(page); | |
1042 | flush_cache_page(vma, addr, page_to_pfn(page)); | |
1043 | /* | |
25985edc | 1044 | * Ok this is tricky, when get_user_pages_fast() run it doesn't |
31dbd01f IE |
1045 | * take any lock, therefore the check that we are going to make |
1046 | * with the pagecount against the mapcount is racey and | |
1047 | * O_DIRECT can happen right after the check. | |
1048 | * So we clear the pte and flush the tlb before the check | |
1049 | * this assure us that no O_DIRECT can happen after the check | |
1050 | * or in the middle of the check. | |
1051 | */ | |
34ee645e | 1052 | entry = ptep_clear_flush_notify(vma, addr, ptep); |
31dbd01f IE |
1053 | /* |
1054 | * Check that no O_DIRECT or similar I/O is in progress on the | |
1055 | * page | |
1056 | */ | |
31e855ea | 1057 | if (page_mapcount(page) + 1 + swapped != page_count(page)) { |
cb532375 | 1058 | set_pte_at(mm, addr, ptep, entry); |
31dbd01f IE |
1059 | goto out_unlock; |
1060 | } | |
4e31635c HD |
1061 | if (pte_dirty(entry)) |
1062 | set_page_dirty(page); | |
1063 | entry = pte_mkclean(pte_wrprotect(entry)); | |
31dbd01f IE |
1064 | set_pte_at_notify(mm, addr, ptep, entry); |
1065 | } | |
1066 | *orig_pte = *ptep; | |
1067 | err = 0; | |
1068 | ||
1069 | out_unlock: | |
1070 | pte_unmap_unlock(ptep, ptl); | |
6bdb913f HE |
1071 | out_mn: |
1072 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); | |
31dbd01f IE |
1073 | out: |
1074 | return err; | |
1075 | } | |
1076 | ||
1077 | /** | |
1078 | * replace_page - replace page in vma by new ksm page | |
8dd3557a HD |
1079 | * @vma: vma that holds the pte pointing to page |
1080 | * @page: the page we are replacing by kpage | |
1081 | * @kpage: the ksm page we replace page by | |
31dbd01f IE |
1082 | * @orig_pte: the original value of the pte |
1083 | * | |
1084 | * Returns 0 on success, -EFAULT on failure. | |
1085 | */ | |
8dd3557a HD |
1086 | static int replace_page(struct vm_area_struct *vma, struct page *page, |
1087 | struct page *kpage, pte_t orig_pte) | |
31dbd01f IE |
1088 | { |
1089 | struct mm_struct *mm = vma->vm_mm; | |
31dbd01f IE |
1090 | pmd_t *pmd; |
1091 | pte_t *ptep; | |
1092 | spinlock_t *ptl; | |
1093 | unsigned long addr; | |
31dbd01f | 1094 | int err = -EFAULT; |
6bdb913f HE |
1095 | unsigned long mmun_start; /* For mmu_notifiers */ |
1096 | unsigned long mmun_end; /* For mmu_notifiers */ | |
31dbd01f | 1097 | |
8dd3557a | 1098 | addr = page_address_in_vma(page, vma); |
31dbd01f IE |
1099 | if (addr == -EFAULT) |
1100 | goto out; | |
1101 | ||
6219049a BL |
1102 | pmd = mm_find_pmd(mm, addr); |
1103 | if (!pmd) | |
31dbd01f | 1104 | goto out; |
31dbd01f | 1105 | |
6bdb913f HE |
1106 | mmun_start = addr; |
1107 | mmun_end = addr + PAGE_SIZE; | |
1108 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
1109 | ||
31dbd01f IE |
1110 | ptep = pte_offset_map_lock(mm, pmd, addr, &ptl); |
1111 | if (!pte_same(*ptep, orig_pte)) { | |
1112 | pte_unmap_unlock(ptep, ptl); | |
6bdb913f | 1113 | goto out_mn; |
31dbd01f IE |
1114 | } |
1115 | ||
8dd3557a | 1116 | get_page(kpage); |
5ad64688 | 1117 | page_add_anon_rmap(kpage, vma, addr); |
31dbd01f IE |
1118 | |
1119 | flush_cache_page(vma, addr, pte_pfn(*ptep)); | |
34ee645e | 1120 | ptep_clear_flush_notify(vma, addr, ptep); |
8dd3557a | 1121 | set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); |
31dbd01f | 1122 | |
8dd3557a | 1123 | page_remove_rmap(page); |
ae52a2ad HD |
1124 | if (!page_mapped(page)) |
1125 | try_to_free_swap(page); | |
8dd3557a | 1126 | put_page(page); |
31dbd01f IE |
1127 | |
1128 | pte_unmap_unlock(ptep, ptl); | |
1129 | err = 0; | |
6bdb913f HE |
1130 | out_mn: |
1131 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); | |
31dbd01f IE |
1132 | out: |
1133 | return err; | |
1134 | } | |
1135 | ||
29ad768c AA |
1136 | static int page_trans_compound_anon_split(struct page *page) |
1137 | { | |
1138 | int ret = 0; | |
1139 | struct page *transhuge_head = page_trans_compound_anon(page); | |
1140 | if (transhuge_head) { | |
1141 | /* Get the reference on the head to split it. */ | |
1142 | if (get_page_unless_zero(transhuge_head)) { | |
1143 | /* | |
1144 | * Recheck we got the reference while the head | |
1145 | * was still anonymous. | |
1146 | */ | |
1147 | if (PageAnon(transhuge_head)) | |
1148 | ret = split_huge_page(transhuge_head); | |
1149 | else | |
1150 | /* | |
1151 | * Retry later if split_huge_page run | |
1152 | * from under us. | |
1153 | */ | |
1154 | ret = 1; | |
1155 | put_page(transhuge_head); | |
1156 | } else | |
1157 | /* Retry later if split_huge_page run from under us. */ | |
1158 | ret = 1; | |
1159 | } | |
1160 | return ret; | |
1161 | } | |
1162 | ||
31dbd01f IE |
1163 | /* |
1164 | * try_to_merge_one_page - take two pages and merge them into one | |
8dd3557a HD |
1165 | * @vma: the vma that holds the pte pointing to page |
1166 | * @page: the PageAnon page that we want to replace with kpage | |
80e14822 HD |
1167 | * @kpage: the PageKsm page that we want to map instead of page, |
1168 | * or NULL the first time when we want to use page as kpage. | |
31dbd01f IE |
1169 | * |
1170 | * This function returns 0 if the pages were merged, -EFAULT otherwise. | |
1171 | */ | |
1172 | static int try_to_merge_one_page(struct vm_area_struct *vma, | |
8dd3557a | 1173 | struct page *page, struct page *kpage) |
31dbd01f IE |
1174 | { |
1175 | pte_t orig_pte = __pte(0); | |
1176 | int err = -EFAULT; | |
1177 | ||
db114b83 HD |
1178 | if (page == kpage) /* ksm page forked */ |
1179 | return 0; | |
1180 | ||
29ad768c AA |
1181 | if (PageTransCompound(page) && page_trans_compound_anon_split(page)) |
1182 | goto out; | |
1183 | BUG_ON(PageTransCompound(page)); | |
8dd3557a | 1184 | if (!PageAnon(page)) |
31dbd01f IE |
1185 | goto out; |
1186 | ||
31dbd01f IE |
1187 | /* |
1188 | * We need the page lock to read a stable PageSwapCache in | |
1189 | * write_protect_page(). We use trylock_page() instead of | |
1190 | * lock_page() because we don't want to wait here - we | |
1191 | * prefer to continue scanning and merging different pages, | |
1192 | * then come back to this page when it is unlocked. | |
1193 | */ | |
8dd3557a | 1194 | if (!trylock_page(page)) |
31e855ea | 1195 | goto out; |
31dbd01f IE |
1196 | /* |
1197 | * If this anonymous page is mapped only here, its pte may need | |
1198 | * to be write-protected. If it's mapped elsewhere, all of its | |
1199 | * ptes are necessarily already write-protected. But in either | |
1200 | * case, we need to lock and check page_count is not raised. | |
1201 | */ | |
80e14822 HD |
1202 | if (write_protect_page(vma, page, &orig_pte) == 0) { |
1203 | if (!kpage) { | |
1204 | /* | |
1205 | * While we hold page lock, upgrade page from | |
1206 | * PageAnon+anon_vma to PageKsm+NULL stable_node: | |
1207 | * stable_tree_insert() will update stable_node. | |
1208 | */ | |
1209 | set_page_stable_node(page, NULL); | |
1210 | mark_page_accessed(page); | |
1211 | err = 0; | |
1212 | } else if (pages_identical(page, kpage)) | |
1213 | err = replace_page(vma, page, kpage, orig_pte); | |
1214 | } | |
31dbd01f | 1215 | |
80e14822 | 1216 | if ((vma->vm_flags & VM_LOCKED) && kpage && !err) { |
73848b46 | 1217 | munlock_vma_page(page); |
5ad64688 HD |
1218 | if (!PageMlocked(kpage)) { |
1219 | unlock_page(page); | |
5ad64688 HD |
1220 | lock_page(kpage); |
1221 | mlock_vma_page(kpage); | |
1222 | page = kpage; /* for final unlock */ | |
1223 | } | |
1224 | } | |
73848b46 | 1225 | |
8dd3557a | 1226 | unlock_page(page); |
31dbd01f IE |
1227 | out: |
1228 | return err; | |
1229 | } | |
1230 | ||
81464e30 HD |
1231 | /* |
1232 | * try_to_merge_with_ksm_page - like try_to_merge_two_pages, | |
1233 | * but no new kernel page is allocated: kpage must already be a ksm page. | |
8dd3557a HD |
1234 | * |
1235 | * This function returns 0 if the pages were merged, -EFAULT otherwise. | |
81464e30 | 1236 | */ |
8dd3557a HD |
1237 | static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item, |
1238 | struct page *page, struct page *kpage) | |
81464e30 | 1239 | { |
8dd3557a | 1240 | struct mm_struct *mm = rmap_item->mm; |
81464e30 HD |
1241 | struct vm_area_struct *vma; |
1242 | int err = -EFAULT; | |
1243 | ||
8dd3557a | 1244 | down_read(&mm->mmap_sem); |
85c6e8dd AA |
1245 | vma = find_mergeable_vma(mm, rmap_item->address); |
1246 | if (!vma) | |
81464e30 HD |
1247 | goto out; |
1248 | ||
8dd3557a | 1249 | err = try_to_merge_one_page(vma, page, kpage); |
db114b83 HD |
1250 | if (err) |
1251 | goto out; | |
1252 | ||
bc56620b HD |
1253 | /* Unstable nid is in union with stable anon_vma: remove first */ |
1254 | remove_rmap_item_from_tree(rmap_item); | |
1255 | ||
db114b83 | 1256 | /* Must get reference to anon_vma while still holding mmap_sem */ |
9e60109f PZ |
1257 | rmap_item->anon_vma = vma->anon_vma; |
1258 | get_anon_vma(vma->anon_vma); | |
81464e30 | 1259 | out: |
8dd3557a | 1260 | up_read(&mm->mmap_sem); |
81464e30 HD |
1261 | return err; |
1262 | } | |
1263 | ||
31dbd01f IE |
1264 | /* |
1265 | * try_to_merge_two_pages - take two identical pages and prepare them | |
1266 | * to be merged into one page. | |
1267 | * | |
8dd3557a HD |
1268 | * This function returns the kpage if we successfully merged two identical |
1269 | * pages into one ksm page, NULL otherwise. | |
31dbd01f | 1270 | * |
80e14822 | 1271 | * Note that this function upgrades page to ksm page: if one of the pages |
31dbd01f IE |
1272 | * is already a ksm page, try_to_merge_with_ksm_page should be used. |
1273 | */ | |
8dd3557a HD |
1274 | static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item, |
1275 | struct page *page, | |
1276 | struct rmap_item *tree_rmap_item, | |
1277 | struct page *tree_page) | |
31dbd01f | 1278 | { |
80e14822 | 1279 | int err; |
31dbd01f | 1280 | |
80e14822 | 1281 | err = try_to_merge_with_ksm_page(rmap_item, page, NULL); |
31dbd01f | 1282 | if (!err) { |
8dd3557a | 1283 | err = try_to_merge_with_ksm_page(tree_rmap_item, |
80e14822 | 1284 | tree_page, page); |
31dbd01f | 1285 | /* |
81464e30 HD |
1286 | * If that fails, we have a ksm page with only one pte |
1287 | * pointing to it: so break it. | |
31dbd01f | 1288 | */ |
4035c07a | 1289 | if (err) |
8dd3557a | 1290 | break_cow(rmap_item); |
31dbd01f | 1291 | } |
80e14822 | 1292 | return err ? NULL : page; |
31dbd01f IE |
1293 | } |
1294 | ||
731b565d AA |
1295 | static __always_inline |
1296 | bool __is_page_sharing_candidate(struct stable_node *stable_node, int offset) | |
1297 | { | |
1298 | VM_BUG_ON(stable_node->rmap_hlist_len < 0); | |
1299 | /* | |
1300 | * Check that at least one mapping still exists, otherwise | |
1301 | * there's no much point to merge and share with this | |
1302 | * stable_node, as the underlying tree_page of the other | |
1303 | * sharer is going to be freed soon. | |
1304 | */ | |
1305 | return stable_node->rmap_hlist_len && | |
1306 | stable_node->rmap_hlist_len + offset < ksm_max_page_sharing; | |
1307 | } | |
1308 | ||
1309 | static __always_inline | |
1310 | bool is_page_sharing_candidate(struct stable_node *stable_node) | |
1311 | { | |
1312 | return __is_page_sharing_candidate(stable_node, 0); | |
1313 | } | |
1314 | ||
052f849b AA |
1315 | struct page *stable_node_dup(struct stable_node **_stable_node_dup, |
1316 | struct stable_node **_stable_node, | |
1317 | struct rb_root *root, | |
1318 | bool prune_stale_stable_nodes) | |
731b565d | 1319 | { |
24f72136 | 1320 | struct stable_node *dup, *found = NULL, *stable_node = *_stable_node; |
731b565d | 1321 | struct hlist_node *hlist_safe; |
052f849b | 1322 | struct page *_tree_page, *tree_page = NULL; |
731b565d AA |
1323 | int nr = 0; |
1324 | int found_rmap_hlist_len; | |
1325 | ||
1326 | if (!prune_stale_stable_nodes || | |
1327 | time_before(jiffies, stable_node->chain_prune_time + | |
1328 | msecs_to_jiffies( | |
1329 | ksm_stable_node_chains_prune_millisecs))) | |
1330 | prune_stale_stable_nodes = false; | |
1331 | else | |
1332 | stable_node->chain_prune_time = jiffies; | |
1333 | ||
1334 | hlist_for_each_entry_safe(dup, hlist_safe, | |
1335 | &stable_node->hlist, hlist_dup) { | |
1336 | cond_resched(); | |
1337 | /* | |
1338 | * We must walk all stable_node_dup to prune the stale | |
1339 | * stable nodes during lookup. | |
1340 | * | |
1341 | * get_ksm_page can drop the nodes from the | |
1342 | * stable_node->hlist if they point to freed pages | |
1343 | * (that's why we do a _safe walk). The "dup" | |
1344 | * stable_node parameter itself will be freed from | |
1345 | * under us if it returns NULL. | |
1346 | */ | |
1347 | _tree_page = get_ksm_page(dup, false); | |
1348 | if (!_tree_page) | |
1349 | continue; | |
1350 | nr += 1; | |
1351 | if (is_page_sharing_candidate(dup)) { | |
1352 | if (!found || | |
1353 | dup->rmap_hlist_len > found_rmap_hlist_len) { | |
1354 | if (found) | |
052f849b | 1355 | put_page(tree_page); |
731b565d AA |
1356 | found = dup; |
1357 | found_rmap_hlist_len = found->rmap_hlist_len; | |
052f849b | 1358 | tree_page = _tree_page; |
731b565d | 1359 | |
052f849b | 1360 | /* skip put_page for found dup */ |
731b565d AA |
1361 | if (!prune_stale_stable_nodes) |
1362 | break; | |
731b565d AA |
1363 | continue; |
1364 | } | |
1365 | } | |
1366 | put_page(_tree_page); | |
1367 | } | |
1368 | ||
1369 | /* | |
1370 | * nr is relevant only if prune_stale_stable_nodes is true, | |
1371 | * otherwise we may break the loop at nr == 1 even if there | |
1372 | * are multiple entries. | |
1373 | */ | |
1374 | if (prune_stale_stable_nodes && found) { | |
1375 | if (nr == 1) { | |
1376 | /* | |
1377 | * If there's not just one entry it would | |
1378 | * corrupt memory, better BUG_ON. In KSM | |
1379 | * context with no lock held it's not even | |
1380 | * fatal. | |
1381 | */ | |
1382 | BUG_ON(stable_node->hlist.first->next); | |
1383 | ||
1384 | /* | |
1385 | * There's just one entry and it is below the | |
1386 | * deduplication limit so drop the chain. | |
1387 | */ | |
1388 | rb_replace_node(&stable_node->node, &found->node, | |
1389 | root); | |
1390 | free_stable_node(stable_node); | |
1391 | ksm_stable_node_chains--; | |
1392 | ksm_stable_node_dups--; | |
24f72136 | 1393 | /* |
3fc3175e AA |
1394 | * NOTE: the caller depends on the stable_node |
1395 | * to be equal to stable_node_dup if the chain | |
1396 | * was collapsed. | |
24f72136 | 1397 | */ |
3fc3175e AA |
1398 | *_stable_node = found; |
1399 | /* | |
1400 | * Just for robustneess as stable_node is | |
1401 | * otherwise left as a stable pointer, the | |
1402 | * compiler shall optimize it away at build | |
1403 | * time. | |
1404 | */ | |
1405 | stable_node = NULL; | |
731b565d AA |
1406 | } else if (__is_page_sharing_candidate(found, 1)) { |
1407 | /* | |
1408 | * Refile our candidate at the head | |
1409 | * after the prune if our candidate | |
1410 | * can accept one more future sharing | |
1411 | * in addition to the one underway. | |
1412 | */ | |
1413 | hlist_del(&found->hlist_dup); | |
1414 | hlist_add_head(&found->hlist_dup, | |
1415 | &stable_node->hlist); | |
1416 | } | |
1417 | } | |
1418 | ||
052f849b AA |
1419 | *_stable_node_dup = found; |
1420 | return tree_page; | |
731b565d AA |
1421 | } |
1422 | ||
1423 | static struct stable_node *stable_node_dup_any(struct stable_node *stable_node, | |
1424 | struct rb_root *root) | |
1425 | { | |
1426 | if (!is_stable_node_chain(stable_node)) | |
1427 | return stable_node; | |
1428 | if (hlist_empty(&stable_node->hlist)) { | |
1429 | free_stable_node_chain(stable_node, root); | |
1430 | return NULL; | |
1431 | } | |
1432 | return hlist_entry(stable_node->hlist.first, | |
1433 | typeof(*stable_node), hlist_dup); | |
1434 | } | |
1435 | ||
052f849b AA |
1436 | /* |
1437 | * Like for get_ksm_page, this function can free the *_stable_node and | |
1438 | * *_stable_node_dup if the returned tree_page is NULL. | |
1439 | * | |
1440 | * It can also free and overwrite *_stable_node with the found | |
1441 | * stable_node_dup if the chain is collapsed (in which case | |
1442 | * *_stable_node will be equal to *_stable_node_dup like if the chain | |
1443 | * never existed). It's up to the caller to verify tree_page is not | |
1444 | * NULL before dereferencing *_stable_node or *_stable_node_dup. | |
1445 | * | |
1446 | * *_stable_node_dup is really a second output parameter of this | |
1447 | * function and will be overwritten in all cases, the caller doesn't | |
1448 | * need to initialize it. | |
1449 | */ | |
1450 | static struct page *__stable_node_chain(struct stable_node **_stable_node_dup, | |
1451 | struct stable_node **_stable_node, | |
1452 | struct rb_root *root, | |
1453 | bool prune_stale_stable_nodes) | |
731b565d | 1454 | { |
24f72136 | 1455 | struct stable_node *stable_node = *_stable_node; |
731b565d AA |
1456 | if (!is_stable_node_chain(stable_node)) { |
1457 | if (is_page_sharing_candidate(stable_node)) { | |
052f849b AA |
1458 | *_stable_node_dup = stable_node; |
1459 | return get_ksm_page(stable_node, false); | |
731b565d | 1460 | } |
052f849b AA |
1461 | /* |
1462 | * _stable_node_dup set to NULL means the stable_node | |
1463 | * reached the ksm_max_page_sharing limit. | |
1464 | */ | |
1465 | *_stable_node_dup = NULL; | |
731b565d AA |
1466 | return NULL; |
1467 | } | |
052f849b | 1468 | return stable_node_dup(_stable_node_dup, _stable_node, root, |
731b565d AA |
1469 | prune_stale_stable_nodes); |
1470 | } | |
1471 | ||
052f849b AA |
1472 | static __always_inline struct page *chain_prune(struct stable_node **s_n_d, |
1473 | struct stable_node **s_n, | |
1474 | struct rb_root *root) | |
731b565d | 1475 | { |
052f849b | 1476 | return __stable_node_chain(s_n_d, s_n, root, true); |
731b565d AA |
1477 | } |
1478 | ||
052f849b AA |
1479 | static __always_inline struct page *chain(struct stable_node **s_n_d, |
1480 | struct stable_node *s_n, | |
1481 | struct rb_root *root) | |
731b565d | 1482 | { |
052f849b AA |
1483 | struct stable_node *old_stable_node = s_n; |
1484 | struct page *tree_page; | |
1485 | ||
1486 | tree_page = __stable_node_chain(s_n_d, &s_n, root, false); | |
1487 | /* not pruning dups so s_n cannot have changed */ | |
1488 | VM_BUG_ON(s_n != old_stable_node); | |
1489 | return tree_page; | |
731b565d AA |
1490 | } |
1491 | ||
31dbd01f | 1492 | /* |
8dd3557a | 1493 | * stable_tree_search - search for page inside the stable tree |
31dbd01f IE |
1494 | * |
1495 | * This function checks if there is a page inside the stable tree | |
1496 | * with identical content to the page that we are scanning right now. | |
1497 | * | |
7b6ba2c7 | 1498 | * This function returns the stable tree node of identical content if found, |
31dbd01f IE |
1499 | * NULL otherwise. |
1500 | */ | |
62b61f61 | 1501 | static struct page *stable_tree_search(struct page *page) |
31dbd01f | 1502 | { |
90bd6fd3 | 1503 | int nid; |
ef53d16c | 1504 | struct rb_root *root; |
4146d2d6 HD |
1505 | struct rb_node **new; |
1506 | struct rb_node *parent; | |
731b565d | 1507 | struct stable_node *stable_node, *stable_node_dup, *stable_node_any; |
4146d2d6 | 1508 | struct stable_node *page_node; |
31dbd01f | 1509 | |
4146d2d6 HD |
1510 | page_node = page_stable_node(page); |
1511 | if (page_node && page_node->head != &migrate_nodes) { | |
1512 | /* ksm page forked */ | |
08beca44 | 1513 | get_page(page); |
62b61f61 | 1514 | return page; |
08beca44 HD |
1515 | } |
1516 | ||
90bd6fd3 | 1517 | nid = get_kpfn_nid(page_to_pfn(page)); |
ef53d16c | 1518 | root = root_stable_tree + nid; |
4146d2d6 | 1519 | again: |
ef53d16c | 1520 | new = &root->rb_node; |
4146d2d6 | 1521 | parent = NULL; |
90bd6fd3 | 1522 | |
4146d2d6 | 1523 | while (*new) { |
4035c07a | 1524 | struct page *tree_page; |
31dbd01f IE |
1525 | int ret; |
1526 | ||
08beca44 | 1527 | cond_resched(); |
4146d2d6 | 1528 | stable_node = rb_entry(*new, struct stable_node, node); |
731b565d | 1529 | stable_node_any = NULL; |
052f849b | 1530 | tree_page = chain_prune(&stable_node_dup, &stable_node, root); |
24f72136 AA |
1531 | /* |
1532 | * NOTE: stable_node may have been freed by | |
1533 | * chain_prune() if the returned stable_node_dup is | |
1534 | * not NULL. stable_node_dup may have been inserted in | |
1535 | * the rbtree instead as a regular stable_node (in | |
1536 | * order to collapse the stable_node chain if a single | |
3fc3175e AA |
1537 | * stable_node dup was found in it). In such case the |
1538 | * stable_node is overwritten by the calleee to point | |
1539 | * to the stable_node_dup that was collapsed in the | |
1540 | * stable rbtree and stable_node will be equal to | |
1541 | * stable_node_dup like if the chain never existed. | |
24f72136 | 1542 | */ |
731b565d AA |
1543 | if (!stable_node_dup) { |
1544 | /* | |
1545 | * Either all stable_node dups were full in | |
1546 | * this stable_node chain, or this chain was | |
1547 | * empty and should be rb_erased. | |
1548 | */ | |
1549 | stable_node_any = stable_node_dup_any(stable_node, | |
1550 | root); | |
1551 | if (!stable_node_any) { | |
1552 | /* rb_erase just run */ | |
1553 | goto again; | |
1554 | } | |
1555 | /* | |
1556 | * Take any of the stable_node dups page of | |
1557 | * this stable_node chain to let the tree walk | |
1558 | * continue. All KSM pages belonging to the | |
1559 | * stable_node dups in a stable_node chain | |
1560 | * have the same content and they're | |
1561 | * wrprotected at all times. Any will work | |
1562 | * fine to continue the walk. | |
1563 | */ | |
1564 | tree_page = get_ksm_page(stable_node_any, false); | |
1565 | } | |
1566 | VM_BUG_ON(!stable_node_dup ^ !!stable_node_any); | |
f2e5ff85 AA |
1567 | if (!tree_page) { |
1568 | /* | |
1569 | * If we walked over a stale stable_node, | |
1570 | * get_ksm_page() will call rb_erase() and it | |
1571 | * may rebalance the tree from under us. So | |
1572 | * restart the search from scratch. Returning | |
1573 | * NULL would be safe too, but we'd generate | |
1574 | * false negative insertions just because some | |
1575 | * stable_node was stale. | |
1576 | */ | |
1577 | goto again; | |
1578 | } | |
31dbd01f | 1579 | |
4035c07a | 1580 | ret = memcmp_pages(page, tree_page); |
c8d6553b | 1581 | put_page(tree_page); |
31dbd01f | 1582 | |
4146d2d6 | 1583 | parent = *new; |
c8d6553b | 1584 | if (ret < 0) |
4146d2d6 | 1585 | new = &parent->rb_left; |
c8d6553b | 1586 | else if (ret > 0) |
4146d2d6 | 1587 | new = &parent->rb_right; |
c8d6553b | 1588 | else { |
731b565d AA |
1589 | if (page_node) { |
1590 | VM_BUG_ON(page_node->head != &migrate_nodes); | |
1591 | /* | |
1592 | * Test if the migrated page should be merged | |
1593 | * into a stable node dup. If the mapcount is | |
1594 | * 1 we can migrate it with another KSM page | |
1595 | * without adding it to the chain. | |
1596 | */ | |
1597 | if (page_mapcount(page) > 1) | |
1598 | goto chain_append; | |
1599 | } | |
1600 | ||
1601 | if (!stable_node_dup) { | |
1602 | /* | |
1603 | * If the stable_node is a chain and | |
1604 | * we got a payload match in memcmp | |
1605 | * but we cannot merge the scanned | |
1606 | * page in any of the existing | |
1607 | * stable_node dups because they're | |
1608 | * all full, we need to wait the | |
1609 | * scanned page to find itself a match | |
1610 | * in the unstable tree to create a | |
1611 | * brand new KSM page to add later to | |
1612 | * the dups of this stable_node. | |
1613 | */ | |
1614 | return NULL; | |
1615 | } | |
1616 | ||
c8d6553b HD |
1617 | /* |
1618 | * Lock and unlock the stable_node's page (which | |
1619 | * might already have been migrated) so that page | |
1620 | * migration is sure to notice its raised count. | |
1621 | * It would be more elegant to return stable_node | |
1622 | * than kpage, but that involves more changes. | |
1623 | */ | |
731b565d AA |
1624 | tree_page = get_ksm_page(stable_node_dup, true); |
1625 | if (unlikely(!tree_page)) | |
1626 | /* | |
1627 | * The tree may have been rebalanced, | |
1628 | * so re-evaluate parent and new. | |
1629 | */ | |
4146d2d6 | 1630 | goto again; |
731b565d AA |
1631 | unlock_page(tree_page); |
1632 | ||
1633 | if (get_kpfn_nid(stable_node_dup->kpfn) != | |
1634 | NUMA(stable_node_dup->nid)) { | |
1635 | put_page(tree_page); | |
1636 | goto replace; | |
1637 | } | |
1638 | return tree_page; | |
c8d6553b | 1639 | } |
31dbd01f IE |
1640 | } |
1641 | ||
4146d2d6 HD |
1642 | if (!page_node) |
1643 | return NULL; | |
1644 | ||
1645 | list_del(&page_node->list); | |
1646 | DO_NUMA(page_node->nid = nid); | |
1647 | rb_link_node(&page_node->node, parent, new); | |
ef53d16c | 1648 | rb_insert_color(&page_node->node, root); |
731b565d AA |
1649 | out: |
1650 | if (is_page_sharing_candidate(page_node)) { | |
1651 | get_page(page); | |
1652 | return page; | |
1653 | } else | |
1654 | return NULL; | |
4146d2d6 HD |
1655 | |
1656 | replace: | |
24f72136 AA |
1657 | /* |
1658 | * If stable_node was a chain and chain_prune collapsed it, | |
3fc3175e AA |
1659 | * stable_node has been updated to be the new regular |
1660 | * stable_node. A collapse of the chain is indistinguishable | |
1661 | * from the case there was no chain in the stable | |
1662 | * rbtree. Otherwise stable_node is the chain and | |
1663 | * stable_node_dup is the dup to replace. | |
24f72136 | 1664 | */ |
3fc3175e | 1665 | if (stable_node_dup == stable_node) { |
24f72136 AA |
1666 | VM_BUG_ON(is_stable_node_chain(stable_node_dup)); |
1667 | VM_BUG_ON(is_stable_node_dup(stable_node_dup)); | |
731b565d AA |
1668 | /* there is no chain */ |
1669 | if (page_node) { | |
1670 | VM_BUG_ON(page_node->head != &migrate_nodes); | |
1671 | list_del(&page_node->list); | |
1672 | DO_NUMA(page_node->nid = nid); | |
24f72136 AA |
1673 | rb_replace_node(&stable_node_dup->node, |
1674 | &page_node->node, | |
731b565d AA |
1675 | root); |
1676 | if (is_page_sharing_candidate(page_node)) | |
1677 | get_page(page); | |
1678 | else | |
1679 | page = NULL; | |
1680 | } else { | |
24f72136 | 1681 | rb_erase(&stable_node_dup->node, root); |
731b565d AA |
1682 | page = NULL; |
1683 | } | |
4146d2d6 | 1684 | } else { |
731b565d AA |
1685 | VM_BUG_ON(!is_stable_node_chain(stable_node)); |
1686 | __stable_node_dup_del(stable_node_dup); | |
1687 | if (page_node) { | |
1688 | VM_BUG_ON(page_node->head != &migrate_nodes); | |
1689 | list_del(&page_node->list); | |
1690 | DO_NUMA(page_node->nid = nid); | |
1691 | stable_node_chain_add_dup(page_node, stable_node); | |
1692 | if (is_page_sharing_candidate(page_node)) | |
1693 | get_page(page); | |
1694 | else | |
1695 | page = NULL; | |
1696 | } else { | |
1697 | page = NULL; | |
1698 | } | |
4146d2d6 | 1699 | } |
731b565d AA |
1700 | stable_node_dup->head = &migrate_nodes; |
1701 | list_add(&stable_node_dup->list, stable_node_dup->head); | |
4146d2d6 | 1702 | return page; |
731b565d AA |
1703 | |
1704 | chain_append: | |
1705 | /* stable_node_dup could be null if it reached the limit */ | |
1706 | if (!stable_node_dup) | |
1707 | stable_node_dup = stable_node_any; | |
24f72136 AA |
1708 | /* |
1709 | * If stable_node was a chain and chain_prune collapsed it, | |
3fc3175e AA |
1710 | * stable_node has been updated to be the new regular |
1711 | * stable_node. A collapse of the chain is indistinguishable | |
1712 | * from the case there was no chain in the stable | |
1713 | * rbtree. Otherwise stable_node is the chain and | |
1714 | * stable_node_dup is the dup to replace. | |
24f72136 | 1715 | */ |
3fc3175e | 1716 | if (stable_node_dup == stable_node) { |
24f72136 AA |
1717 | VM_BUG_ON(is_stable_node_chain(stable_node_dup)); |
1718 | VM_BUG_ON(is_stable_node_dup(stable_node_dup)); | |
731b565d AA |
1719 | /* chain is missing so create it */ |
1720 | stable_node = alloc_stable_node_chain(stable_node_dup, | |
1721 | root); | |
1722 | if (!stable_node) | |
1723 | return NULL; | |
1724 | } | |
1725 | /* | |
1726 | * Add this stable_node dup that was | |
1727 | * migrated to the stable_node chain | |
1728 | * of the current nid for this page | |
1729 | * content. | |
1730 | */ | |
24f72136 AA |
1731 | VM_BUG_ON(!is_stable_node_chain(stable_node)); |
1732 | VM_BUG_ON(!is_stable_node_dup(stable_node_dup)); | |
731b565d AA |
1733 | VM_BUG_ON(page_node->head != &migrate_nodes); |
1734 | list_del(&page_node->list); | |
1735 | DO_NUMA(page_node->nid = nid); | |
1736 | stable_node_chain_add_dup(page_node, stable_node); | |
1737 | goto out; | |
31dbd01f IE |
1738 | } |
1739 | ||
1740 | /* | |
e850dcf5 | 1741 | * stable_tree_insert - insert stable tree node pointing to new ksm page |
31dbd01f IE |
1742 | * into the stable tree. |
1743 | * | |
7b6ba2c7 HD |
1744 | * This function returns the stable tree node just allocated on success, |
1745 | * NULL otherwise. | |
31dbd01f | 1746 | */ |
7b6ba2c7 | 1747 | static struct stable_node *stable_tree_insert(struct page *kpage) |
31dbd01f | 1748 | { |
90bd6fd3 PH |
1749 | int nid; |
1750 | unsigned long kpfn; | |
ef53d16c | 1751 | struct rb_root *root; |
90bd6fd3 | 1752 | struct rb_node **new; |
f2e5ff85 | 1753 | struct rb_node *parent; |
731b565d AA |
1754 | struct stable_node *stable_node, *stable_node_dup, *stable_node_any; |
1755 | bool need_chain = false; | |
31dbd01f | 1756 | |
90bd6fd3 PH |
1757 | kpfn = page_to_pfn(kpage); |
1758 | nid = get_kpfn_nid(kpfn); | |
ef53d16c | 1759 | root = root_stable_tree + nid; |
f2e5ff85 AA |
1760 | again: |
1761 | parent = NULL; | |
ef53d16c | 1762 | new = &root->rb_node; |
90bd6fd3 | 1763 | |
31dbd01f | 1764 | while (*new) { |
4035c07a | 1765 | struct page *tree_page; |
31dbd01f IE |
1766 | int ret; |
1767 | ||
08beca44 | 1768 | cond_resched(); |
7b6ba2c7 | 1769 | stable_node = rb_entry(*new, struct stable_node, node); |
731b565d | 1770 | stable_node_any = NULL; |
052f849b | 1771 | tree_page = chain(&stable_node_dup, stable_node, root); |
731b565d AA |
1772 | if (!stable_node_dup) { |
1773 | /* | |
1774 | * Either all stable_node dups were full in | |
1775 | * this stable_node chain, or this chain was | |
1776 | * empty and should be rb_erased. | |
1777 | */ | |
1778 | stable_node_any = stable_node_dup_any(stable_node, | |
1779 | root); | |
1780 | if (!stable_node_any) { | |
1781 | /* rb_erase just run */ | |
1782 | goto again; | |
1783 | } | |
1784 | /* | |
1785 | * Take any of the stable_node dups page of | |
1786 | * this stable_node chain to let the tree walk | |
1787 | * continue. All KSM pages belonging to the | |
1788 | * stable_node dups in a stable_node chain | |
1789 | * have the same content and they're | |
1790 | * wrprotected at all times. Any will work | |
1791 | * fine to continue the walk. | |
1792 | */ | |
1793 | tree_page = get_ksm_page(stable_node_any, false); | |
1794 | } | |
1795 | VM_BUG_ON(!stable_node_dup ^ !!stable_node_any); | |
f2e5ff85 AA |
1796 | if (!tree_page) { |
1797 | /* | |
1798 | * If we walked over a stale stable_node, | |
1799 | * get_ksm_page() will call rb_erase() and it | |
1800 | * may rebalance the tree from under us. So | |
1801 | * restart the search from scratch. Returning | |
1802 | * NULL would be safe too, but we'd generate | |
1803 | * false negative insertions just because some | |
1804 | * stable_node was stale. | |
1805 | */ | |
1806 | goto again; | |
1807 | } | |
31dbd01f | 1808 | |
4035c07a HD |
1809 | ret = memcmp_pages(kpage, tree_page); |
1810 | put_page(tree_page); | |
31dbd01f IE |
1811 | |
1812 | parent = *new; | |
1813 | if (ret < 0) | |
1814 | new = &parent->rb_left; | |
1815 | else if (ret > 0) | |
1816 | new = &parent->rb_right; | |
1817 | else { | |
731b565d AA |
1818 | need_chain = true; |
1819 | break; | |
31dbd01f IE |
1820 | } |
1821 | } | |
1822 | ||
731b565d AA |
1823 | stable_node_dup = alloc_stable_node(); |
1824 | if (!stable_node_dup) | |
7b6ba2c7 | 1825 | return NULL; |
31dbd01f | 1826 | |
731b565d AA |
1827 | INIT_HLIST_HEAD(&stable_node_dup->hlist); |
1828 | stable_node_dup->kpfn = kpfn; | |
1829 | set_page_stable_node(kpage, stable_node_dup); | |
1830 | stable_node_dup->rmap_hlist_len = 0; | |
1831 | DO_NUMA(stable_node_dup->nid = nid); | |
1832 | if (!need_chain) { | |
1833 | rb_link_node(&stable_node_dup->node, parent, new); | |
1834 | rb_insert_color(&stable_node_dup->node, root); | |
1835 | } else { | |
1836 | if (!is_stable_node_chain(stable_node)) { | |
1837 | struct stable_node *orig = stable_node; | |
1838 | /* chain is missing so create it */ | |
1839 | stable_node = alloc_stable_node_chain(orig, root); | |
1840 | if (!stable_node) { | |
1841 | free_stable_node(stable_node_dup); | |
1842 | return NULL; | |
1843 | } | |
1844 | } | |
1845 | stable_node_chain_add_dup(stable_node_dup, stable_node); | |
1846 | } | |
08beca44 | 1847 | |
731b565d | 1848 | return stable_node_dup; |
31dbd01f IE |
1849 | } |
1850 | ||
1851 | /* | |
8dd3557a HD |
1852 | * unstable_tree_search_insert - search for identical page, |
1853 | * else insert rmap_item into the unstable tree. | |
31dbd01f IE |
1854 | * |
1855 | * This function searches for a page in the unstable tree identical to the | |
1856 | * page currently being scanned; and if no identical page is found in the | |
1857 | * tree, we insert rmap_item as a new object into the unstable tree. | |
1858 | * | |
1859 | * This function returns pointer to rmap_item found to be identical | |
1860 | * to the currently scanned page, NULL otherwise. | |
1861 | * | |
1862 | * This function does both searching and inserting, because they share | |
1863 | * the same walking algorithm in an rbtree. | |
1864 | */ | |
8dd3557a HD |
1865 | static |
1866 | struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item, | |
1867 | struct page *page, | |
1868 | struct page **tree_pagep) | |
31dbd01f | 1869 | { |
90bd6fd3 PH |
1870 | struct rb_node **new; |
1871 | struct rb_root *root; | |
31dbd01f | 1872 | struct rb_node *parent = NULL; |
90bd6fd3 PH |
1873 | int nid; |
1874 | ||
1875 | nid = get_kpfn_nid(page_to_pfn(page)); | |
ef53d16c | 1876 | root = root_unstable_tree + nid; |
90bd6fd3 | 1877 | new = &root->rb_node; |
31dbd01f IE |
1878 | |
1879 | while (*new) { | |
1880 | struct rmap_item *tree_rmap_item; | |
8dd3557a | 1881 | struct page *tree_page; |
31dbd01f IE |
1882 | int ret; |
1883 | ||
d178f27f | 1884 | cond_resched(); |
31dbd01f | 1885 | tree_rmap_item = rb_entry(*new, struct rmap_item, node); |
8dd3557a | 1886 | tree_page = get_mergeable_page(tree_rmap_item); |
c8f95ed1 | 1887 | if (!tree_page) |
31dbd01f IE |
1888 | return NULL; |
1889 | ||
1890 | /* | |
8dd3557a | 1891 | * Don't substitute a ksm page for a forked page. |
31dbd01f | 1892 | */ |
8dd3557a HD |
1893 | if (page == tree_page) { |
1894 | put_page(tree_page); | |
31dbd01f IE |
1895 | return NULL; |
1896 | } | |
1897 | ||
8dd3557a | 1898 | ret = memcmp_pages(page, tree_page); |
31dbd01f IE |
1899 | |
1900 | parent = *new; | |
1901 | if (ret < 0) { | |
8dd3557a | 1902 | put_page(tree_page); |
31dbd01f IE |
1903 | new = &parent->rb_left; |
1904 | } else if (ret > 0) { | |
8dd3557a | 1905 | put_page(tree_page); |
31dbd01f | 1906 | new = &parent->rb_right; |
b599cbdf HD |
1907 | } else if (!ksm_merge_across_nodes && |
1908 | page_to_nid(tree_page) != nid) { | |
1909 | /* | |
1910 | * If tree_page has been migrated to another NUMA node, | |
1911 | * it will be flushed out and put in the right unstable | |
1912 | * tree next time: only merge with it when across_nodes. | |
1913 | */ | |
1914 | put_page(tree_page); | |
1915 | return NULL; | |
31dbd01f | 1916 | } else { |
8dd3557a | 1917 | *tree_pagep = tree_page; |
31dbd01f IE |
1918 | return tree_rmap_item; |
1919 | } | |
1920 | } | |
1921 | ||
7b6ba2c7 | 1922 | rmap_item->address |= UNSTABLE_FLAG; |
31dbd01f | 1923 | rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK); |
e850dcf5 | 1924 | DO_NUMA(rmap_item->nid = nid); |
31dbd01f | 1925 | rb_link_node(&rmap_item->node, parent, new); |
90bd6fd3 | 1926 | rb_insert_color(&rmap_item->node, root); |
31dbd01f | 1927 | |
473b0ce4 | 1928 | ksm_pages_unshared++; |
31dbd01f IE |
1929 | return NULL; |
1930 | } | |
1931 | ||
1932 | /* | |
1933 | * stable_tree_append - add another rmap_item to the linked list of | |
1934 | * rmap_items hanging off a given node of the stable tree, all sharing | |
1935 | * the same ksm page. | |
1936 | */ | |
1937 | static void stable_tree_append(struct rmap_item *rmap_item, | |
731b565d AA |
1938 | struct stable_node *stable_node, |
1939 | bool max_page_sharing_bypass) | |
31dbd01f | 1940 | { |
731b565d AA |
1941 | /* |
1942 | * rmap won't find this mapping if we don't insert the | |
1943 | * rmap_item in the right stable_node | |
1944 | * duplicate. page_migration could break later if rmap breaks, | |
1945 | * so we can as well crash here. We really need to check for | |
1946 | * rmap_hlist_len == STABLE_NODE_CHAIN, but we can as well check | |
1947 | * for other negative values as an undeflow if detected here | |
1948 | * for the first time (and not when decreasing rmap_hlist_len) | |
1949 | * would be sign of memory corruption in the stable_node. | |
1950 | */ | |
1951 | BUG_ON(stable_node->rmap_hlist_len < 0); | |
1952 | ||
1953 | stable_node->rmap_hlist_len++; | |
1954 | if (!max_page_sharing_bypass) | |
1955 | /* possibly non fatal but unexpected overflow, only warn */ | |
1956 | WARN_ON_ONCE(stable_node->rmap_hlist_len > | |
1957 | ksm_max_page_sharing); | |
1958 | ||
7b6ba2c7 | 1959 | rmap_item->head = stable_node; |
31dbd01f | 1960 | rmap_item->address |= STABLE_FLAG; |
7b6ba2c7 | 1961 | hlist_add_head(&rmap_item->hlist, &stable_node->hlist); |
e178dfde | 1962 | |
7b6ba2c7 HD |
1963 | if (rmap_item->hlist.next) |
1964 | ksm_pages_sharing++; | |
1965 | else | |
1966 | ksm_pages_shared++; | |
31dbd01f IE |
1967 | } |
1968 | ||
1969 | /* | |
81464e30 HD |
1970 | * cmp_and_merge_page - first see if page can be merged into the stable tree; |
1971 | * if not, compare checksum to previous and if it's the same, see if page can | |
1972 | * be inserted into the unstable tree, or merged with a page already there and | |
1973 | * both transferred to the stable tree. | |
31dbd01f IE |
1974 | * |
1975 | * @page: the page that we are searching identical page to. | |
1976 | * @rmap_item: the reverse mapping into the virtual address of this page | |
1977 | */ | |
1978 | static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) | |
1979 | { | |
31dbd01f | 1980 | struct rmap_item *tree_rmap_item; |
8dd3557a | 1981 | struct page *tree_page = NULL; |
7b6ba2c7 | 1982 | struct stable_node *stable_node; |
8dd3557a | 1983 | struct page *kpage; |
31dbd01f IE |
1984 | unsigned int checksum; |
1985 | int err; | |
731b565d | 1986 | bool max_page_sharing_bypass = false; |
31dbd01f | 1987 | |
4146d2d6 HD |
1988 | stable_node = page_stable_node(page); |
1989 | if (stable_node) { | |
1990 | if (stable_node->head != &migrate_nodes && | |
731b565d AA |
1991 | get_kpfn_nid(READ_ONCE(stable_node->kpfn)) != |
1992 | NUMA(stable_node->nid)) { | |
1993 | stable_node_dup_del(stable_node); | |
4146d2d6 HD |
1994 | stable_node->head = &migrate_nodes; |
1995 | list_add(&stable_node->list, stable_node->head); | |
1996 | } | |
1997 | if (stable_node->head != &migrate_nodes && | |
1998 | rmap_item->head == stable_node) | |
1999 | return; | |
731b565d AA |
2000 | /* |
2001 | * If it's a KSM fork, allow it to go over the sharing limit | |
2002 | * without warnings. | |
2003 | */ | |
2004 | if (!is_page_sharing_candidate(stable_node)) | |
2005 | max_page_sharing_bypass = true; | |
4146d2d6 | 2006 | } |
31dbd01f IE |
2007 | |
2008 | /* We first start with searching the page inside the stable tree */ | |
62b61f61 | 2009 | kpage = stable_tree_search(page); |
4146d2d6 HD |
2010 | if (kpage == page && rmap_item->head == stable_node) { |
2011 | put_page(kpage); | |
2012 | return; | |
2013 | } | |
2014 | ||
2015 | remove_rmap_item_from_tree(rmap_item); | |
2016 | ||
62b61f61 | 2017 | if (kpage) { |
08beca44 | 2018 | err = try_to_merge_with_ksm_page(rmap_item, page, kpage); |
31dbd01f IE |
2019 | if (!err) { |
2020 | /* | |
2021 | * The page was successfully merged: | |
2022 | * add its rmap_item to the stable tree. | |
2023 | */ | |
5ad64688 | 2024 | lock_page(kpage); |
731b565d AA |
2025 | stable_tree_append(rmap_item, page_stable_node(kpage), |
2026 | max_page_sharing_bypass); | |
5ad64688 | 2027 | unlock_page(kpage); |
31dbd01f | 2028 | } |
8dd3557a | 2029 | put_page(kpage); |
31dbd01f IE |
2030 | return; |
2031 | } | |
2032 | ||
2033 | /* | |
4035c07a HD |
2034 | * If the hash value of the page has changed from the last time |
2035 | * we calculated it, this page is changing frequently: therefore we | |
2036 | * don't want to insert it in the unstable tree, and we don't want | |
2037 | * to waste our time searching for something identical to it there. | |
31dbd01f IE |
2038 | */ |
2039 | checksum = calc_checksum(page); | |
2040 | if (rmap_item->oldchecksum != checksum) { | |
2041 | rmap_item->oldchecksum = checksum; | |
2042 | return; | |
2043 | } | |
2044 | ||
8dd3557a HD |
2045 | tree_rmap_item = |
2046 | unstable_tree_search_insert(rmap_item, page, &tree_page); | |
31dbd01f | 2047 | if (tree_rmap_item) { |
8dd3557a HD |
2048 | kpage = try_to_merge_two_pages(rmap_item, page, |
2049 | tree_rmap_item, tree_page); | |
2050 | put_page(tree_page); | |
8dd3557a | 2051 | if (kpage) { |
bc56620b HD |
2052 | /* |
2053 | * The pages were successfully merged: insert new | |
2054 | * node in the stable tree and add both rmap_items. | |
2055 | */ | |
5ad64688 | 2056 | lock_page(kpage); |
7b6ba2c7 HD |
2057 | stable_node = stable_tree_insert(kpage); |
2058 | if (stable_node) { | |
731b565d AA |
2059 | stable_tree_append(tree_rmap_item, stable_node, |
2060 | false); | |
2061 | stable_tree_append(rmap_item, stable_node, | |
2062 | false); | |
7b6ba2c7 | 2063 | } |
5ad64688 | 2064 | unlock_page(kpage); |
7b6ba2c7 | 2065 | |
31dbd01f IE |
2066 | /* |
2067 | * If we fail to insert the page into the stable tree, | |
2068 | * we will have 2 virtual addresses that are pointing | |
2069 | * to a ksm page left outside the stable tree, | |
2070 | * in which case we need to break_cow on both. | |
2071 | */ | |
7b6ba2c7 | 2072 | if (!stable_node) { |
8dd3557a HD |
2073 | break_cow(tree_rmap_item); |
2074 | break_cow(rmap_item); | |
31dbd01f IE |
2075 | } |
2076 | } | |
31dbd01f IE |
2077 | } |
2078 | } | |
2079 | ||
2080 | static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot, | |
6514d511 | 2081 | struct rmap_item **rmap_list, |
31dbd01f IE |
2082 | unsigned long addr) |
2083 | { | |
2084 | struct rmap_item *rmap_item; | |
2085 | ||
6514d511 HD |
2086 | while (*rmap_list) { |
2087 | rmap_item = *rmap_list; | |
93d17715 | 2088 | if ((rmap_item->address & PAGE_MASK) == addr) |
31dbd01f | 2089 | return rmap_item; |
31dbd01f IE |
2090 | if (rmap_item->address > addr) |
2091 | break; | |
6514d511 | 2092 | *rmap_list = rmap_item->rmap_list; |
31dbd01f | 2093 | remove_rmap_item_from_tree(rmap_item); |
31dbd01f IE |
2094 | free_rmap_item(rmap_item); |
2095 | } | |
2096 | ||
2097 | rmap_item = alloc_rmap_item(); | |
2098 | if (rmap_item) { | |
2099 | /* It has already been zeroed */ | |
2100 | rmap_item->mm = mm_slot->mm; | |
2101 | rmap_item->address = addr; | |
6514d511 HD |
2102 | rmap_item->rmap_list = *rmap_list; |
2103 | *rmap_list = rmap_item; | |
31dbd01f IE |
2104 | } |
2105 | return rmap_item; | |
2106 | } | |
2107 | ||
2108 | static struct rmap_item *scan_get_next_rmap_item(struct page **page) | |
2109 | { | |
2110 | struct mm_struct *mm; | |
2111 | struct mm_slot *slot; | |
2112 | struct vm_area_struct *vma; | |
2113 | struct rmap_item *rmap_item; | |
90bd6fd3 | 2114 | int nid; |
31dbd01f IE |
2115 | |
2116 | if (list_empty(&ksm_mm_head.mm_list)) | |
2117 | return NULL; | |
2118 | ||
2119 | slot = ksm_scan.mm_slot; | |
2120 | if (slot == &ksm_mm_head) { | |
2919bfd0 HD |
2121 | /* |
2122 | * A number of pages can hang around indefinitely on per-cpu | |
2123 | * pagevecs, raised page count preventing write_protect_page | |
2124 | * from merging them. Though it doesn't really matter much, | |
2125 | * it is puzzling to see some stuck in pages_volatile until | |
2126 | * other activity jostles them out, and they also prevented | |
2127 | * LTP's KSM test from succeeding deterministically; so drain | |
2128 | * them here (here rather than on entry to ksm_do_scan(), | |
2129 | * so we don't IPI too often when pages_to_scan is set low). | |
2130 | */ | |
2131 | lru_add_drain_all(); | |
2132 | ||
4146d2d6 HD |
2133 | /* |
2134 | * Whereas stale stable_nodes on the stable_tree itself | |
2135 | * get pruned in the regular course of stable_tree_search(), | |
2136 | * those moved out to the migrate_nodes list can accumulate: | |
2137 | * so prune them once before each full scan. | |
2138 | */ | |
2139 | if (!ksm_merge_across_nodes) { | |
2140 | struct stable_node *stable_node; | |
2141 | struct list_head *this, *next; | |
2142 | struct page *page; | |
2143 | ||
2144 | list_for_each_safe(this, next, &migrate_nodes) { | |
2145 | stable_node = list_entry(this, | |
2146 | struct stable_node, list); | |
2147 | page = get_ksm_page(stable_node, false); | |
2148 | if (page) | |
2149 | put_page(page); | |
2150 | cond_resched(); | |
2151 | } | |
2152 | } | |
2153 | ||
ef53d16c | 2154 | for (nid = 0; nid < ksm_nr_node_ids; nid++) |
90bd6fd3 | 2155 | root_unstable_tree[nid] = RB_ROOT; |
31dbd01f IE |
2156 | |
2157 | spin_lock(&ksm_mmlist_lock); | |
2158 | slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list); | |
2159 | ksm_scan.mm_slot = slot; | |
2160 | spin_unlock(&ksm_mmlist_lock); | |
2b472611 HD |
2161 | /* |
2162 | * Although we tested list_empty() above, a racing __ksm_exit | |
2163 | * of the last mm on the list may have removed it since then. | |
2164 | */ | |
2165 | if (slot == &ksm_mm_head) | |
2166 | return NULL; | |
31dbd01f IE |
2167 | next_mm: |
2168 | ksm_scan.address = 0; | |
6514d511 | 2169 | ksm_scan.rmap_list = &slot->rmap_list; |
31dbd01f IE |
2170 | } |
2171 | ||
2172 | mm = slot->mm; | |
2173 | down_read(&mm->mmap_sem); | |
9ba69294 HD |
2174 | if (ksm_test_exit(mm)) |
2175 | vma = NULL; | |
2176 | else | |
2177 | vma = find_vma(mm, ksm_scan.address); | |
2178 | ||
2179 | for (; vma; vma = vma->vm_next) { | |
31dbd01f IE |
2180 | if (!(vma->vm_flags & VM_MERGEABLE)) |
2181 | continue; | |
2182 | if (ksm_scan.address < vma->vm_start) | |
2183 | ksm_scan.address = vma->vm_start; | |
2184 | if (!vma->anon_vma) | |
2185 | ksm_scan.address = vma->vm_end; | |
2186 | ||
2187 | while (ksm_scan.address < vma->vm_end) { | |
9ba69294 HD |
2188 | if (ksm_test_exit(mm)) |
2189 | break; | |
31dbd01f | 2190 | *page = follow_page(vma, ksm_scan.address, FOLL_GET); |
21ae5b01 AA |
2191 | if (IS_ERR_OR_NULL(*page)) { |
2192 | ksm_scan.address += PAGE_SIZE; | |
2193 | cond_resched(); | |
2194 | continue; | |
2195 | } | |
29ad768c AA |
2196 | if (PageAnon(*page) || |
2197 | page_trans_compound_anon(*page)) { | |
31dbd01f IE |
2198 | flush_anon_page(vma, *page, ksm_scan.address); |
2199 | flush_dcache_page(*page); | |
2200 | rmap_item = get_next_rmap_item(slot, | |
6514d511 | 2201 | ksm_scan.rmap_list, ksm_scan.address); |
31dbd01f | 2202 | if (rmap_item) { |
6514d511 HD |
2203 | ksm_scan.rmap_list = |
2204 | &rmap_item->rmap_list; | |
31dbd01f IE |
2205 | ksm_scan.address += PAGE_SIZE; |
2206 | } else | |
2207 | put_page(*page); | |
2208 | up_read(&mm->mmap_sem); | |
2209 | return rmap_item; | |
2210 | } | |
21ae5b01 | 2211 | put_page(*page); |
31dbd01f IE |
2212 | ksm_scan.address += PAGE_SIZE; |
2213 | cond_resched(); | |
2214 | } | |
2215 | } | |
2216 | ||
9ba69294 HD |
2217 | if (ksm_test_exit(mm)) { |
2218 | ksm_scan.address = 0; | |
6514d511 | 2219 | ksm_scan.rmap_list = &slot->rmap_list; |
9ba69294 | 2220 | } |
31dbd01f IE |
2221 | /* |
2222 | * Nuke all the rmap_items that are above this current rmap: | |
2223 | * because there were no VM_MERGEABLE vmas with such addresses. | |
2224 | */ | |
6514d511 | 2225 | remove_trailing_rmap_items(slot, ksm_scan.rmap_list); |
31dbd01f IE |
2226 | |
2227 | spin_lock(&ksm_mmlist_lock); | |
cd551f97 HD |
2228 | ksm_scan.mm_slot = list_entry(slot->mm_list.next, |
2229 | struct mm_slot, mm_list); | |
2230 | if (ksm_scan.address == 0) { | |
2231 | /* | |
2232 | * We've completed a full scan of all vmas, holding mmap_sem | |
2233 | * throughout, and found no VM_MERGEABLE: so do the same as | |
2234 | * __ksm_exit does to remove this mm from all our lists now. | |
9ba69294 HD |
2235 | * This applies either when cleaning up after __ksm_exit |
2236 | * (but beware: we can reach here even before __ksm_exit), | |
2237 | * or when all VM_MERGEABLE areas have been unmapped (and | |
2238 | * mmap_sem then protects against race with MADV_MERGEABLE). | |
cd551f97 | 2239 | */ |
4ca3a69b | 2240 | hash_del(&slot->link); |
cd551f97 | 2241 | list_del(&slot->mm_list); |
9ba69294 HD |
2242 | spin_unlock(&ksm_mmlist_lock); |
2243 | ||
cd551f97 HD |
2244 | free_mm_slot(slot); |
2245 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | |
9ba69294 HD |
2246 | up_read(&mm->mmap_sem); |
2247 | mmdrop(mm); | |
2248 | } else { | |
2249 | spin_unlock(&ksm_mmlist_lock); | |
2250 | up_read(&mm->mmap_sem); | |
cd551f97 | 2251 | } |
31dbd01f IE |
2252 | |
2253 | /* Repeat until we've completed scanning the whole list */ | |
cd551f97 | 2254 | slot = ksm_scan.mm_slot; |
31dbd01f IE |
2255 | if (slot != &ksm_mm_head) |
2256 | goto next_mm; | |
2257 | ||
31dbd01f IE |
2258 | ksm_scan.seqnr++; |
2259 | return NULL; | |
2260 | } | |
2261 | ||
2262 | /** | |
2263 | * ksm_do_scan - the ksm scanner main worker function. | |
2264 | * @scan_npages - number of pages we want to scan before we return. | |
2265 | */ | |
2266 | static void ksm_do_scan(unsigned int scan_npages) | |
2267 | { | |
2268 | struct rmap_item *rmap_item; | |
22eccdd7 | 2269 | struct page *uninitialized_var(page); |
31dbd01f | 2270 | |
878aee7d | 2271 | while (scan_npages-- && likely(!freezing(current))) { |
31dbd01f IE |
2272 | cond_resched(); |
2273 | rmap_item = scan_get_next_rmap_item(&page); | |
2274 | if (!rmap_item) | |
2275 | return; | |
4146d2d6 | 2276 | cmp_and_merge_page(page, rmap_item); |
31dbd01f IE |
2277 | put_page(page); |
2278 | } | |
2279 | } | |
2280 | ||
6e158384 HD |
2281 | static int ksmd_should_run(void) |
2282 | { | |
2283 | return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list); | |
2284 | } | |
2285 | ||
31dbd01f IE |
2286 | static int ksm_scan_thread(void *nothing) |
2287 | { | |
878aee7d | 2288 | set_freezable(); |
339aa624 | 2289 | set_user_nice(current, 5); |
31dbd01f IE |
2290 | |
2291 | while (!kthread_should_stop()) { | |
6e158384 | 2292 | mutex_lock(&ksm_thread_mutex); |
ef4d43a8 | 2293 | wait_while_offlining(); |
6e158384 | 2294 | if (ksmd_should_run()) |
31dbd01f | 2295 | ksm_do_scan(ksm_thread_pages_to_scan); |
6e158384 HD |
2296 | mutex_unlock(&ksm_thread_mutex); |
2297 | ||
878aee7d AA |
2298 | try_to_freeze(); |
2299 | ||
6e158384 | 2300 | if (ksmd_should_run()) { |
31dbd01f IE |
2301 | schedule_timeout_interruptible( |
2302 | msecs_to_jiffies(ksm_thread_sleep_millisecs)); | |
2303 | } else { | |
878aee7d | 2304 | wait_event_freezable(ksm_thread_wait, |
6e158384 | 2305 | ksmd_should_run() || kthread_should_stop()); |
31dbd01f IE |
2306 | } |
2307 | } | |
2308 | return 0; | |
2309 | } | |
2310 | ||
f8af4da3 HD |
2311 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
2312 | unsigned long end, int advice, unsigned long *vm_flags) | |
2313 | { | |
2314 | struct mm_struct *mm = vma->vm_mm; | |
d952b791 | 2315 | int err; |
f8af4da3 HD |
2316 | |
2317 | switch (advice) { | |
2318 | case MADV_MERGEABLE: | |
2319 | /* | |
2320 | * Be somewhat over-protective for now! | |
2321 | */ | |
2322 | if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE | | |
2323 | VM_PFNMAP | VM_IO | VM_DONTEXPAND | | |
0661a336 | 2324 | VM_HUGETLB | VM_MIXEDMAP)) |
f8af4da3 HD |
2325 | return 0; /* just ignore the advice */ |
2326 | ||
cc2383ec KK |
2327 | #ifdef VM_SAO |
2328 | if (*vm_flags & VM_SAO) | |
2329 | return 0; | |
2330 | #endif | |
2331 | ||
d952b791 HD |
2332 | if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) { |
2333 | err = __ksm_enter(mm); | |
2334 | if (err) | |
2335 | return err; | |
2336 | } | |
f8af4da3 HD |
2337 | |
2338 | *vm_flags |= VM_MERGEABLE; | |
2339 | break; | |
2340 | ||
2341 | case MADV_UNMERGEABLE: | |
2342 | if (!(*vm_flags & VM_MERGEABLE)) | |
2343 | return 0; /* just ignore the advice */ | |
2344 | ||
d952b791 HD |
2345 | if (vma->anon_vma) { |
2346 | err = unmerge_ksm_pages(vma, start, end); | |
2347 | if (err) | |
2348 | return err; | |
2349 | } | |
f8af4da3 HD |
2350 | |
2351 | *vm_flags &= ~VM_MERGEABLE; | |
2352 | break; | |
2353 | } | |
2354 | ||
2355 | return 0; | |
2356 | } | |
2357 | ||
2358 | int __ksm_enter(struct mm_struct *mm) | |
2359 | { | |
6e158384 HD |
2360 | struct mm_slot *mm_slot; |
2361 | int needs_wakeup; | |
2362 | ||
2363 | mm_slot = alloc_mm_slot(); | |
31dbd01f IE |
2364 | if (!mm_slot) |
2365 | return -ENOMEM; | |
2366 | ||
6e158384 HD |
2367 | /* Check ksm_run too? Would need tighter locking */ |
2368 | needs_wakeup = list_empty(&ksm_mm_head.mm_list); | |
2369 | ||
31dbd01f IE |
2370 | spin_lock(&ksm_mmlist_lock); |
2371 | insert_to_mm_slots_hash(mm, mm_slot); | |
2372 | /* | |
cbf86cfe HD |
2373 | * When KSM_RUN_MERGE (or KSM_RUN_STOP), |
2374 | * insert just behind the scanning cursor, to let the area settle | |
31dbd01f IE |
2375 | * down a little; when fork is followed by immediate exec, we don't |
2376 | * want ksmd to waste time setting up and tearing down an rmap_list. | |
cbf86cfe HD |
2377 | * |
2378 | * But when KSM_RUN_UNMERGE, it's important to insert ahead of its | |
2379 | * scanning cursor, otherwise KSM pages in newly forked mms will be | |
2380 | * missed: then we might as well insert at the end of the list. | |
31dbd01f | 2381 | */ |
cbf86cfe HD |
2382 | if (ksm_run & KSM_RUN_UNMERGE) |
2383 | list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list); | |
2384 | else | |
2385 | list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list); | |
31dbd01f IE |
2386 | spin_unlock(&ksm_mmlist_lock); |
2387 | ||
f8af4da3 | 2388 | set_bit(MMF_VM_MERGEABLE, &mm->flags); |
9ba69294 | 2389 | atomic_inc(&mm->mm_count); |
6e158384 HD |
2390 | |
2391 | if (needs_wakeup) | |
2392 | wake_up_interruptible(&ksm_thread_wait); | |
2393 | ||
f8af4da3 HD |
2394 | return 0; |
2395 | } | |
2396 | ||
1c2fb7a4 | 2397 | void __ksm_exit(struct mm_struct *mm) |
f8af4da3 | 2398 | { |
cd551f97 | 2399 | struct mm_slot *mm_slot; |
9ba69294 | 2400 | int easy_to_free = 0; |
cd551f97 | 2401 | |
31dbd01f | 2402 | /* |
9ba69294 HD |
2403 | * This process is exiting: if it's straightforward (as is the |
2404 | * case when ksmd was never running), free mm_slot immediately. | |
2405 | * But if it's at the cursor or has rmap_items linked to it, use | |
2406 | * mmap_sem to synchronize with any break_cows before pagetables | |
2407 | * are freed, and leave the mm_slot on the list for ksmd to free. | |
2408 | * Beware: ksm may already have noticed it exiting and freed the slot. | |
31dbd01f | 2409 | */ |
9ba69294 | 2410 | |
cd551f97 HD |
2411 | spin_lock(&ksm_mmlist_lock); |
2412 | mm_slot = get_mm_slot(mm); | |
9ba69294 | 2413 | if (mm_slot && ksm_scan.mm_slot != mm_slot) { |
6514d511 | 2414 | if (!mm_slot->rmap_list) { |
4ca3a69b | 2415 | hash_del(&mm_slot->link); |
9ba69294 HD |
2416 | list_del(&mm_slot->mm_list); |
2417 | easy_to_free = 1; | |
2418 | } else { | |
2419 | list_move(&mm_slot->mm_list, | |
2420 | &ksm_scan.mm_slot->mm_list); | |
2421 | } | |
cd551f97 | 2422 | } |
cd551f97 HD |
2423 | spin_unlock(&ksm_mmlist_lock); |
2424 | ||
9ba69294 HD |
2425 | if (easy_to_free) { |
2426 | free_mm_slot(mm_slot); | |
2427 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | |
2428 | mmdrop(mm); | |
2429 | } else if (mm_slot) { | |
9ba69294 HD |
2430 | down_write(&mm->mmap_sem); |
2431 | up_write(&mm->mmap_sem); | |
9ba69294 | 2432 | } |
31dbd01f IE |
2433 | } |
2434 | ||
cbf86cfe | 2435 | struct page *ksm_might_need_to_copy(struct page *page, |
5ad64688 HD |
2436 | struct vm_area_struct *vma, unsigned long address) |
2437 | { | |
cbf86cfe | 2438 | struct anon_vma *anon_vma = page_anon_vma(page); |
5ad64688 HD |
2439 | struct page *new_page; |
2440 | ||
cbf86cfe HD |
2441 | if (PageKsm(page)) { |
2442 | if (page_stable_node(page) && | |
2443 | !(ksm_run & KSM_RUN_UNMERGE)) | |
2444 | return page; /* no need to copy it */ | |
2445 | } else if (!anon_vma) { | |
2446 | return page; /* no need to copy it */ | |
2447 | } else if (anon_vma->root == vma->anon_vma->root && | |
2448 | page->index == linear_page_index(vma, address)) { | |
2449 | return page; /* still no need to copy it */ | |
2450 | } | |
2451 | if (!PageUptodate(page)) | |
2452 | return page; /* let do_swap_page report the error */ | |
2453 | ||
5ad64688 HD |
2454 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); |
2455 | if (new_page) { | |
2456 | copy_user_highpage(new_page, page, address, vma); | |
2457 | ||
2458 | SetPageDirty(new_page); | |
2459 | __SetPageUptodate(new_page); | |
5ad64688 | 2460 | __set_page_locked(new_page); |
5ad64688 HD |
2461 | } |
2462 | ||
5ad64688 HD |
2463 | return new_page; |
2464 | } | |
2465 | ||
051ac83a | 2466 | int rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc) |
e9995ef9 HD |
2467 | { |
2468 | struct stable_node *stable_node; | |
e9995ef9 HD |
2469 | struct rmap_item *rmap_item; |
2470 | int ret = SWAP_AGAIN; | |
2471 | int search_new_forks = 0; | |
2472 | ||
309381fe | 2473 | VM_BUG_ON_PAGE(!PageKsm(page), page); |
9f32624b JK |
2474 | |
2475 | /* | |
2476 | * Rely on the page lock to protect against concurrent modifications | |
2477 | * to that page's node of the stable tree. | |
2478 | */ | |
309381fe | 2479 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
e9995ef9 HD |
2480 | |
2481 | stable_node = page_stable_node(page); | |
2482 | if (!stable_node) | |
2483 | return ret; | |
2484 | again: | |
b67bfe0d | 2485 | hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) { |
e9995ef9 | 2486 | struct anon_vma *anon_vma = rmap_item->anon_vma; |
5beb4930 | 2487 | struct anon_vma_chain *vmac; |
e9995ef9 HD |
2488 | struct vm_area_struct *vma; |
2489 | ||
ad12695f | 2490 | cond_resched(); |
b6b19f25 | 2491 | anon_vma_lock_read(anon_vma); |
bf181b9f ML |
2492 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
2493 | 0, ULONG_MAX) { | |
ad12695f | 2494 | cond_resched(); |
5beb4930 | 2495 | vma = vmac->vma; |
e9995ef9 HD |
2496 | if (rmap_item->address < vma->vm_start || |
2497 | rmap_item->address >= vma->vm_end) | |
2498 | continue; | |
2499 | /* | |
2500 | * Initially we examine only the vma which covers this | |
2501 | * rmap_item; but later, if there is still work to do, | |
2502 | * we examine covering vmas in other mms: in case they | |
2503 | * were forked from the original since ksmd passed. | |
2504 | */ | |
2505 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) | |
2506 | continue; | |
2507 | ||
0dd1c7bb JK |
2508 | if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) |
2509 | continue; | |
2510 | ||
051ac83a JK |
2511 | ret = rwc->rmap_one(page, vma, |
2512 | rmap_item->address, rwc->arg); | |
e9995ef9 | 2513 | if (ret != SWAP_AGAIN) { |
b6b19f25 | 2514 | anon_vma_unlock_read(anon_vma); |
e9995ef9 HD |
2515 | goto out; |
2516 | } | |
0dd1c7bb JK |
2517 | if (rwc->done && rwc->done(page)) { |
2518 | anon_vma_unlock_read(anon_vma); | |
2519 | goto out; | |
2520 | } | |
e9995ef9 | 2521 | } |
b6b19f25 | 2522 | anon_vma_unlock_read(anon_vma); |
e9995ef9 HD |
2523 | } |
2524 | if (!search_new_forks++) | |
2525 | goto again; | |
2526 | out: | |
2527 | return ret; | |
2528 | } | |
2529 | ||
52629506 | 2530 | #ifdef CONFIG_MIGRATION |
e9995ef9 HD |
2531 | void ksm_migrate_page(struct page *newpage, struct page *oldpage) |
2532 | { | |
2533 | struct stable_node *stable_node; | |
2534 | ||
309381fe SL |
2535 | VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage); |
2536 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); | |
2537 | VM_BUG_ON_PAGE(newpage->mapping != oldpage->mapping, newpage); | |
e9995ef9 HD |
2538 | |
2539 | stable_node = page_stable_node(newpage); | |
2540 | if (stable_node) { | |
309381fe | 2541 | VM_BUG_ON_PAGE(stable_node->kpfn != page_to_pfn(oldpage), oldpage); |
62b61f61 | 2542 | stable_node->kpfn = page_to_pfn(newpage); |
c8d6553b HD |
2543 | /* |
2544 | * newpage->mapping was set in advance; now we need smp_wmb() | |
2545 | * to make sure that the new stable_node->kpfn is visible | |
2546 | * to get_ksm_page() before it can see that oldpage->mapping | |
2547 | * has gone stale (or that PageSwapCache has been cleared). | |
2548 | */ | |
2549 | smp_wmb(); | |
2550 | set_page_stable_node(oldpage, NULL); | |
e9995ef9 HD |
2551 | } |
2552 | } | |
2553 | #endif /* CONFIG_MIGRATION */ | |
2554 | ||
62b61f61 | 2555 | #ifdef CONFIG_MEMORY_HOTREMOVE |
ef4d43a8 HD |
2556 | static void wait_while_offlining(void) |
2557 | { | |
2558 | while (ksm_run & KSM_RUN_OFFLINE) { | |
2559 | mutex_unlock(&ksm_thread_mutex); | |
2560 | wait_on_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE), | |
74316201 | 2561 | TASK_UNINTERRUPTIBLE); |
ef4d43a8 HD |
2562 | mutex_lock(&ksm_thread_mutex); |
2563 | } | |
2564 | } | |
2565 | ||
731b565d AA |
2566 | static bool stable_node_dup_remove_range(struct stable_node *stable_node, |
2567 | unsigned long start_pfn, | |
2568 | unsigned long end_pfn) | |
2569 | { | |
2570 | if (stable_node->kpfn >= start_pfn && | |
2571 | stable_node->kpfn < end_pfn) { | |
2572 | /* | |
2573 | * Don't get_ksm_page, page has already gone: | |
2574 | * which is why we keep kpfn instead of page* | |
2575 | */ | |
2576 | remove_node_from_stable_tree(stable_node); | |
2577 | return true; | |
2578 | } | |
2579 | return false; | |
2580 | } | |
2581 | ||
2582 | static bool stable_node_chain_remove_range(struct stable_node *stable_node, | |
2583 | unsigned long start_pfn, | |
2584 | unsigned long end_pfn, | |
2585 | struct rb_root *root) | |
2586 | { | |
2587 | struct stable_node *dup; | |
2588 | struct hlist_node *hlist_safe; | |
2589 | ||
2590 | if (!is_stable_node_chain(stable_node)) { | |
2591 | VM_BUG_ON(is_stable_node_dup(stable_node)); | |
2592 | return stable_node_dup_remove_range(stable_node, start_pfn, | |
2593 | end_pfn); | |
2594 | } | |
2595 | ||
2596 | hlist_for_each_entry_safe(dup, hlist_safe, | |
2597 | &stable_node->hlist, hlist_dup) { | |
2598 | VM_BUG_ON(!is_stable_node_dup(dup)); | |
2599 | stable_node_dup_remove_range(dup, start_pfn, end_pfn); | |
2600 | } | |
2601 | if (hlist_empty(&stable_node->hlist)) { | |
2602 | free_stable_node_chain(stable_node, root); | |
2603 | return true; /* notify caller that tree was rebalanced */ | |
2604 | } else | |
2605 | return false; | |
2606 | } | |
2607 | ||
ee0ea59c HD |
2608 | static void ksm_check_stable_tree(unsigned long start_pfn, |
2609 | unsigned long end_pfn) | |
62b61f61 | 2610 | { |
ee0ea59c | 2611 | struct stable_node *stable_node; |
4146d2d6 | 2612 | struct list_head *this, *next; |
62b61f61 | 2613 | struct rb_node *node; |
90bd6fd3 | 2614 | int nid; |
62b61f61 | 2615 | |
ef53d16c HD |
2616 | for (nid = 0; nid < ksm_nr_node_ids; nid++) { |
2617 | node = rb_first(root_stable_tree + nid); | |
ee0ea59c | 2618 | while (node) { |
90bd6fd3 | 2619 | stable_node = rb_entry(node, struct stable_node, node); |
731b565d AA |
2620 | if (stable_node_chain_remove_range(stable_node, |
2621 | start_pfn, end_pfn, | |
2622 | root_stable_tree + | |
2623 | nid)) | |
ef53d16c | 2624 | node = rb_first(root_stable_tree + nid); |
731b565d | 2625 | else |
ee0ea59c HD |
2626 | node = rb_next(node); |
2627 | cond_resched(); | |
90bd6fd3 | 2628 | } |
ee0ea59c | 2629 | } |
4146d2d6 HD |
2630 | list_for_each_safe(this, next, &migrate_nodes) { |
2631 | stable_node = list_entry(this, struct stable_node, list); | |
2632 | if (stable_node->kpfn >= start_pfn && | |
2633 | stable_node->kpfn < end_pfn) | |
2634 | remove_node_from_stable_tree(stable_node); | |
2635 | cond_resched(); | |
2636 | } | |
62b61f61 HD |
2637 | } |
2638 | ||
2639 | static int ksm_memory_callback(struct notifier_block *self, | |
2640 | unsigned long action, void *arg) | |
2641 | { | |
2642 | struct memory_notify *mn = arg; | |
62b61f61 HD |
2643 | |
2644 | switch (action) { | |
2645 | case MEM_GOING_OFFLINE: | |
2646 | /* | |
ef4d43a8 HD |
2647 | * Prevent ksm_do_scan(), unmerge_and_remove_all_rmap_items() |
2648 | * and remove_all_stable_nodes() while memory is going offline: | |
2649 | * it is unsafe for them to touch the stable tree at this time. | |
2650 | * But unmerge_ksm_pages(), rmap lookups and other entry points | |
2651 | * which do not need the ksm_thread_mutex are all safe. | |
62b61f61 | 2652 | */ |
ef4d43a8 HD |
2653 | mutex_lock(&ksm_thread_mutex); |
2654 | ksm_run |= KSM_RUN_OFFLINE; | |
2655 | mutex_unlock(&ksm_thread_mutex); | |
62b61f61 HD |
2656 | break; |
2657 | ||
2658 | case MEM_OFFLINE: | |
2659 | /* | |
2660 | * Most of the work is done by page migration; but there might | |
2661 | * be a few stable_nodes left over, still pointing to struct | |
ee0ea59c HD |
2662 | * pages which have been offlined: prune those from the tree, |
2663 | * otherwise get_ksm_page() might later try to access a | |
2664 | * non-existent struct page. | |
62b61f61 | 2665 | */ |
ee0ea59c HD |
2666 | ksm_check_stable_tree(mn->start_pfn, |
2667 | mn->start_pfn + mn->nr_pages); | |
62b61f61 HD |
2668 | /* fallthrough */ |
2669 | ||
2670 | case MEM_CANCEL_OFFLINE: | |
ef4d43a8 HD |
2671 | mutex_lock(&ksm_thread_mutex); |
2672 | ksm_run &= ~KSM_RUN_OFFLINE; | |
62b61f61 | 2673 | mutex_unlock(&ksm_thread_mutex); |
ef4d43a8 HD |
2674 | |
2675 | smp_mb(); /* wake_up_bit advises this */ | |
2676 | wake_up_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE)); | |
62b61f61 HD |
2677 | break; |
2678 | } | |
2679 | return NOTIFY_OK; | |
2680 | } | |
ef4d43a8 HD |
2681 | #else |
2682 | static void wait_while_offlining(void) | |
2683 | { | |
2684 | } | |
62b61f61 HD |
2685 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
2686 | ||
2ffd8679 HD |
2687 | #ifdef CONFIG_SYSFS |
2688 | /* | |
2689 | * This all compiles without CONFIG_SYSFS, but is a waste of space. | |
2690 | */ | |
2691 | ||
31dbd01f IE |
2692 | #define KSM_ATTR_RO(_name) \ |
2693 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) | |
2694 | #define KSM_ATTR(_name) \ | |
2695 | static struct kobj_attribute _name##_attr = \ | |
2696 | __ATTR(_name, 0644, _name##_show, _name##_store) | |
2697 | ||
2698 | static ssize_t sleep_millisecs_show(struct kobject *kobj, | |
2699 | struct kobj_attribute *attr, char *buf) | |
2700 | { | |
2701 | return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs); | |
2702 | } | |
2703 | ||
2704 | static ssize_t sleep_millisecs_store(struct kobject *kobj, | |
2705 | struct kobj_attribute *attr, | |
2706 | const char *buf, size_t count) | |
2707 | { | |
2708 | unsigned long msecs; | |
2709 | int err; | |
2710 | ||
3dbb95f7 | 2711 | err = kstrtoul(buf, 10, &msecs); |
31dbd01f IE |
2712 | if (err || msecs > UINT_MAX) |
2713 | return -EINVAL; | |
2714 | ||
2715 | ksm_thread_sleep_millisecs = msecs; | |
2716 | ||
2717 | return count; | |
2718 | } | |
2719 | KSM_ATTR(sleep_millisecs); | |
2720 | ||
2721 | static ssize_t pages_to_scan_show(struct kobject *kobj, | |
2722 | struct kobj_attribute *attr, char *buf) | |
2723 | { | |
2724 | return sprintf(buf, "%u\n", ksm_thread_pages_to_scan); | |
2725 | } | |
2726 | ||
2727 | static ssize_t pages_to_scan_store(struct kobject *kobj, | |
2728 | struct kobj_attribute *attr, | |
2729 | const char *buf, size_t count) | |
2730 | { | |
2731 | int err; | |
2732 | unsigned long nr_pages; | |
2733 | ||
3dbb95f7 | 2734 | err = kstrtoul(buf, 10, &nr_pages); |
31dbd01f IE |
2735 | if (err || nr_pages > UINT_MAX) |
2736 | return -EINVAL; | |
2737 | ||
2738 | ksm_thread_pages_to_scan = nr_pages; | |
2739 | ||
2740 | return count; | |
2741 | } | |
2742 | KSM_ATTR(pages_to_scan); | |
2743 | ||
2744 | static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr, | |
2745 | char *buf) | |
2746 | { | |
ef4d43a8 | 2747 | return sprintf(buf, "%lu\n", ksm_run); |
31dbd01f IE |
2748 | } |
2749 | ||
2750 | static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, | |
2751 | const char *buf, size_t count) | |
2752 | { | |
2753 | int err; | |
2754 | unsigned long flags; | |
2755 | ||
3dbb95f7 | 2756 | err = kstrtoul(buf, 10, &flags); |
31dbd01f IE |
2757 | if (err || flags > UINT_MAX) |
2758 | return -EINVAL; | |
2759 | if (flags > KSM_RUN_UNMERGE) | |
2760 | return -EINVAL; | |
2761 | ||
2762 | /* | |
2763 | * KSM_RUN_MERGE sets ksmd running, and 0 stops it running. | |
2764 | * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items, | |
d0f209f6 HD |
2765 | * breaking COW to free the pages_shared (but leaves mm_slots |
2766 | * on the list for when ksmd may be set running again). | |
31dbd01f IE |
2767 | */ |
2768 | ||
2769 | mutex_lock(&ksm_thread_mutex); | |
ef4d43a8 | 2770 | wait_while_offlining(); |
31dbd01f IE |
2771 | if (ksm_run != flags) { |
2772 | ksm_run = flags; | |
d952b791 | 2773 | if (flags & KSM_RUN_UNMERGE) { |
e1e12d2f | 2774 | set_current_oom_origin(); |
d952b791 | 2775 | err = unmerge_and_remove_all_rmap_items(); |
e1e12d2f | 2776 | clear_current_oom_origin(); |
d952b791 HD |
2777 | if (err) { |
2778 | ksm_run = KSM_RUN_STOP; | |
2779 | count = err; | |
2780 | } | |
2781 | } | |
31dbd01f IE |
2782 | } |
2783 | mutex_unlock(&ksm_thread_mutex); | |
2784 | ||
2785 | if (flags & KSM_RUN_MERGE) | |
2786 | wake_up_interruptible(&ksm_thread_wait); | |
2787 | ||
2788 | return count; | |
2789 | } | |
2790 | KSM_ATTR(run); | |
2791 | ||
90bd6fd3 PH |
2792 | #ifdef CONFIG_NUMA |
2793 | static ssize_t merge_across_nodes_show(struct kobject *kobj, | |
2794 | struct kobj_attribute *attr, char *buf) | |
2795 | { | |
2796 | return sprintf(buf, "%u\n", ksm_merge_across_nodes); | |
2797 | } | |
2798 | ||
2799 | static ssize_t merge_across_nodes_store(struct kobject *kobj, | |
2800 | struct kobj_attribute *attr, | |
2801 | const char *buf, size_t count) | |
2802 | { | |
2803 | int err; | |
2804 | unsigned long knob; | |
2805 | ||
2806 | err = kstrtoul(buf, 10, &knob); | |
2807 | if (err) | |
2808 | return err; | |
2809 | if (knob > 1) | |
2810 | return -EINVAL; | |
2811 | ||
2812 | mutex_lock(&ksm_thread_mutex); | |
ef4d43a8 | 2813 | wait_while_offlining(); |
90bd6fd3 | 2814 | if (ksm_merge_across_nodes != knob) { |
cbf86cfe | 2815 | if (ksm_pages_shared || remove_all_stable_nodes()) |
90bd6fd3 | 2816 | err = -EBUSY; |
ef53d16c HD |
2817 | else if (root_stable_tree == one_stable_tree) { |
2818 | struct rb_root *buf; | |
2819 | /* | |
2820 | * This is the first time that we switch away from the | |
2821 | * default of merging across nodes: must now allocate | |
2822 | * a buffer to hold as many roots as may be needed. | |
2823 | * Allocate stable and unstable together: | |
2824 | * MAXSMP NODES_SHIFT 10 will use 16kB. | |
2825 | */ | |
bafe1e14 JP |
2826 | buf = kcalloc(nr_node_ids + nr_node_ids, sizeof(*buf), |
2827 | GFP_KERNEL); | |
ef53d16c HD |
2828 | /* Let us assume that RB_ROOT is NULL is zero */ |
2829 | if (!buf) | |
2830 | err = -ENOMEM; | |
2831 | else { | |
2832 | root_stable_tree = buf; | |
2833 | root_unstable_tree = buf + nr_node_ids; | |
2834 | /* Stable tree is empty but not the unstable */ | |
2835 | root_unstable_tree[0] = one_unstable_tree[0]; | |
2836 | } | |
2837 | } | |
2838 | if (!err) { | |
90bd6fd3 | 2839 | ksm_merge_across_nodes = knob; |
ef53d16c HD |
2840 | ksm_nr_node_ids = knob ? 1 : nr_node_ids; |
2841 | } | |
90bd6fd3 PH |
2842 | } |
2843 | mutex_unlock(&ksm_thread_mutex); | |
2844 | ||
2845 | return err ? err : count; | |
2846 | } | |
2847 | KSM_ATTR(merge_across_nodes); | |
2848 | #endif | |
2849 | ||
731b565d AA |
2850 | static ssize_t max_page_sharing_show(struct kobject *kobj, |
2851 | struct kobj_attribute *attr, char *buf) | |
2852 | { | |
2853 | return sprintf(buf, "%u\n", ksm_max_page_sharing); | |
2854 | } | |
2855 | ||
2856 | static ssize_t max_page_sharing_store(struct kobject *kobj, | |
2857 | struct kobj_attribute *attr, | |
2858 | const char *buf, size_t count) | |
2859 | { | |
2860 | int err; | |
2861 | int knob; | |
2862 | ||
2863 | err = kstrtoint(buf, 10, &knob); | |
2864 | if (err) | |
2865 | return err; | |
2866 | /* | |
2867 | * When a KSM page is created it is shared by 2 mappings. This | |
2868 | * being a signed comparison, it implicitly verifies it's not | |
2869 | * negative. | |
2870 | */ | |
2871 | if (knob < 2) | |
2872 | return -EINVAL; | |
2873 | ||
2874 | if (READ_ONCE(ksm_max_page_sharing) == knob) | |
2875 | return count; | |
2876 | ||
2877 | mutex_lock(&ksm_thread_mutex); | |
2878 | wait_while_offlining(); | |
2879 | if (ksm_max_page_sharing != knob) { | |
2880 | if (ksm_pages_shared || remove_all_stable_nodes()) | |
2881 | err = -EBUSY; | |
2882 | else | |
2883 | ksm_max_page_sharing = knob; | |
2884 | } | |
2885 | mutex_unlock(&ksm_thread_mutex); | |
2886 | ||
2887 | return err ? err : count; | |
2888 | } | |
2889 | KSM_ATTR(max_page_sharing); | |
2890 | ||
b4028260 HD |
2891 | static ssize_t pages_shared_show(struct kobject *kobj, |
2892 | struct kobj_attribute *attr, char *buf) | |
2893 | { | |
2894 | return sprintf(buf, "%lu\n", ksm_pages_shared); | |
2895 | } | |
2896 | KSM_ATTR_RO(pages_shared); | |
2897 | ||
2898 | static ssize_t pages_sharing_show(struct kobject *kobj, | |
2899 | struct kobj_attribute *attr, char *buf) | |
2900 | { | |
e178dfde | 2901 | return sprintf(buf, "%lu\n", ksm_pages_sharing); |
b4028260 HD |
2902 | } |
2903 | KSM_ATTR_RO(pages_sharing); | |
2904 | ||
473b0ce4 HD |
2905 | static ssize_t pages_unshared_show(struct kobject *kobj, |
2906 | struct kobj_attribute *attr, char *buf) | |
2907 | { | |
2908 | return sprintf(buf, "%lu\n", ksm_pages_unshared); | |
2909 | } | |
2910 | KSM_ATTR_RO(pages_unshared); | |
2911 | ||
2912 | static ssize_t pages_volatile_show(struct kobject *kobj, | |
2913 | struct kobj_attribute *attr, char *buf) | |
2914 | { | |
2915 | long ksm_pages_volatile; | |
2916 | ||
2917 | ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared | |
2918 | - ksm_pages_sharing - ksm_pages_unshared; | |
2919 | /* | |
2920 | * It was not worth any locking to calculate that statistic, | |
2921 | * but it might therefore sometimes be negative: conceal that. | |
2922 | */ | |
2923 | if (ksm_pages_volatile < 0) | |
2924 | ksm_pages_volatile = 0; | |
2925 | return sprintf(buf, "%ld\n", ksm_pages_volatile); | |
2926 | } | |
2927 | KSM_ATTR_RO(pages_volatile); | |
2928 | ||
731b565d AA |
2929 | static ssize_t stable_node_dups_show(struct kobject *kobj, |
2930 | struct kobj_attribute *attr, char *buf) | |
2931 | { | |
2932 | return sprintf(buf, "%lu\n", ksm_stable_node_dups); | |
2933 | } | |
2934 | KSM_ATTR_RO(stable_node_dups); | |
2935 | ||
2936 | static ssize_t stable_node_chains_show(struct kobject *kobj, | |
2937 | struct kobj_attribute *attr, char *buf) | |
2938 | { | |
2939 | return sprintf(buf, "%lu\n", ksm_stable_node_chains); | |
2940 | } | |
2941 | KSM_ATTR_RO(stable_node_chains); | |
2942 | ||
2943 | static ssize_t | |
2944 | stable_node_chains_prune_millisecs_show(struct kobject *kobj, | |
2945 | struct kobj_attribute *attr, | |
2946 | char *buf) | |
2947 | { | |
2948 | return sprintf(buf, "%u\n", ksm_stable_node_chains_prune_millisecs); | |
2949 | } | |
2950 | ||
2951 | static ssize_t | |
2952 | stable_node_chains_prune_millisecs_store(struct kobject *kobj, | |
2953 | struct kobj_attribute *attr, | |
2954 | const char *buf, size_t count) | |
2955 | { | |
2956 | unsigned long msecs; | |
2957 | int err; | |
2958 | ||
2959 | err = kstrtoul(buf, 10, &msecs); | |
2960 | if (err || msecs > UINT_MAX) | |
2961 | return -EINVAL; | |
2962 | ||
2963 | ksm_stable_node_chains_prune_millisecs = msecs; | |
2964 | ||
2965 | return count; | |
2966 | } | |
2967 | KSM_ATTR(stable_node_chains_prune_millisecs); | |
2968 | ||
473b0ce4 HD |
2969 | static ssize_t full_scans_show(struct kobject *kobj, |
2970 | struct kobj_attribute *attr, char *buf) | |
2971 | { | |
2972 | return sprintf(buf, "%lu\n", ksm_scan.seqnr); | |
2973 | } | |
2974 | KSM_ATTR_RO(full_scans); | |
2975 | ||
31dbd01f IE |
2976 | static struct attribute *ksm_attrs[] = { |
2977 | &sleep_millisecs_attr.attr, | |
2978 | &pages_to_scan_attr.attr, | |
2979 | &run_attr.attr, | |
b4028260 HD |
2980 | &pages_shared_attr.attr, |
2981 | &pages_sharing_attr.attr, | |
473b0ce4 HD |
2982 | &pages_unshared_attr.attr, |
2983 | &pages_volatile_attr.attr, | |
2984 | &full_scans_attr.attr, | |
90bd6fd3 PH |
2985 | #ifdef CONFIG_NUMA |
2986 | &merge_across_nodes_attr.attr, | |
2987 | #endif | |
731b565d AA |
2988 | &max_page_sharing_attr.attr, |
2989 | &stable_node_chains_attr.attr, | |
2990 | &stable_node_dups_attr.attr, | |
2991 | &stable_node_chains_prune_millisecs_attr.attr, | |
31dbd01f IE |
2992 | NULL, |
2993 | }; | |
2994 | ||
2995 | static struct attribute_group ksm_attr_group = { | |
2996 | .attrs = ksm_attrs, | |
2997 | .name = "ksm", | |
2998 | }; | |
2ffd8679 | 2999 | #endif /* CONFIG_SYSFS */ |
31dbd01f IE |
3000 | |
3001 | static int __init ksm_init(void) | |
3002 | { | |
3003 | struct task_struct *ksm_thread; | |
3004 | int err; | |
3005 | ||
3006 | err = ksm_slab_init(); | |
3007 | if (err) | |
3008 | goto out; | |
3009 | ||
31dbd01f IE |
3010 | ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd"); |
3011 | if (IS_ERR(ksm_thread)) { | |
25acde31 | 3012 | pr_err("ksm: creating kthread failed\n"); |
31dbd01f | 3013 | err = PTR_ERR(ksm_thread); |
d9f8984c | 3014 | goto out_free; |
31dbd01f IE |
3015 | } |
3016 | ||
2ffd8679 | 3017 | #ifdef CONFIG_SYSFS |
31dbd01f IE |
3018 | err = sysfs_create_group(mm_kobj, &ksm_attr_group); |
3019 | if (err) { | |
25acde31 | 3020 | pr_err("ksm: register sysfs failed\n"); |
2ffd8679 | 3021 | kthread_stop(ksm_thread); |
d9f8984c | 3022 | goto out_free; |
31dbd01f | 3023 | } |
c73602ad HD |
3024 | #else |
3025 | ksm_run = KSM_RUN_MERGE; /* no way for user to start it */ | |
3026 | ||
2ffd8679 | 3027 | #endif /* CONFIG_SYSFS */ |
31dbd01f | 3028 | |
62b61f61 | 3029 | #ifdef CONFIG_MEMORY_HOTREMOVE |
ef4d43a8 | 3030 | /* There is no significance to this priority 100 */ |
62b61f61 HD |
3031 | hotplug_memory_notifier(ksm_memory_callback, 100); |
3032 | #endif | |
31dbd01f IE |
3033 | return 0; |
3034 | ||
d9f8984c | 3035 | out_free: |
31dbd01f IE |
3036 | ksm_slab_free(); |
3037 | out: | |
3038 | return err; | |
f8af4da3 | 3039 | } |
a64fb3cd | 3040 | subsys_initcall(ksm_init); |