blob: 9f2acc998a37d0f293eb52e6efdccefc17dfa272 [file] [log] [blame]
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001/*
Izik Eidus31dbd012009-09-21 17:02:03 -07002 * 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 *
Izik Eidus36b25282009-09-21 17:02:06 -07007 * Copyright (C) 2008-2009 Red Hat, Inc.
Izik Eidus31dbd012009-09-21 17:02:03 -07008 * Authors:
9 * Izik Eidus
10 * Andrea Arcangeli
11 * Chris Wright
Izik Eidus36b25282009-09-21 17:02:06 -070012 * Hugh Dickins
Izik Eidus31dbd012009-09-21 17:02:03 -070013 *
14 * This work is licensed under the terms of the GNU GPL, version 2.
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070015 */
16
17#include <linux/errno.h>
Izik Eidus31dbd012009-09-21 17:02:03 -070018#include <linux/mm.h>
19#include <linux/fs.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070020#include <linux/mman.h>
Izik Eidus31dbd012009-09-21 17:02:03 -070021#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>
Hugh Dickins62b61f62009-12-14 17:59:33 -080032#include <linux/memory.h>
Izik Eidus31dbd012009-09-21 17:02:03 -070033#include <linux/mmu_notifier.h>
Izik Eidus2c6854f2009-09-23 15:56:04 -070034#include <linux/swap.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070035#include <linux/ksm.h>
36
Izik Eidus31dbd012009-09-21 17:02:03 -070037#include <asm/tlbflush.h>
Hugh Dickins73848b42009-12-14 17:59:22 -080038#include "internal.h"
Izik Eidus31dbd012009-09-21 17:02:03 -070039
40/*
41 * A few notes about the KSM scanning process,
42 * to make it easier to understand the data structures below:
43 *
44 * In order to reduce excessive scanning, KSM sorts the memory pages by their
45 * contents into a data structure that holds pointers to the pages' locations.
46 *
47 * Since the contents of the pages may change at any moment, KSM cannot just
48 * insert the pages into a normal sorted tree and expect it to find anything.
49 * Therefore KSM uses two data structures - the stable and the unstable tree.
50 *
51 * The stable tree holds pointers to all the merged pages (ksm pages), sorted
52 * by their contents. Because each such page is write-protected, searching on
53 * this tree is fully assured to be working (except when pages are unmapped),
54 * and therefore this tree is called the stable tree.
55 *
56 * In addition to the stable tree, KSM uses a second data structure called the
57 * unstable tree: this tree holds pointers to pages which have been found to
58 * be "unchanged for a period of time". The unstable tree sorts these pages
59 * by their contents, but since they are not write-protected, KSM cannot rely
60 * upon the unstable tree to work correctly - the unstable tree is liable to
61 * be corrupted as its contents are modified, and so it is called unstable.
62 *
63 * KSM solves this problem by several techniques:
64 *
65 * 1) The unstable tree is flushed every time KSM completes scanning all
66 * memory areas, and then the tree is rebuilt again from the beginning.
67 * 2) KSM will only insert into the unstable tree, pages whose hash value
68 * has not changed since the previous scan of all memory areas.
69 * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
70 * colors of the nodes and not on their contents, assuring that even when
71 * the tree gets "corrupted" it won't get out of balance, so scanning time
72 * remains the same (also, searching and inserting nodes in an rbtree uses
73 * the same algorithm, so we have no overhead when we flush and rebuild).
74 * 4) KSM never flushes the stable tree, which means that even if it were to
75 * take 10 attempts to find a page in the unstable tree, once it is found,
76 * it is secured in the stable tree. (When we scan a new page, we first
77 * compare it against the stable tree, and then against the unstable tree.)
78 */
79
80/**
81 * struct mm_slot - ksm information per mm that is being scanned
82 * @link: link to the mm_slots hash list
83 * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
Hugh Dickins6514d512009-12-14 17:59:19 -080084 * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
Izik Eidus31dbd012009-09-21 17:02:03 -070085 * @mm: the mm that this information is valid for
86 */
87struct mm_slot {
88 struct hlist_node link;
89 struct list_head mm_list;
Hugh Dickins6514d512009-12-14 17:59:19 -080090 struct rmap_item *rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -070091 struct mm_struct *mm;
92};
93
94/**
95 * struct ksm_scan - cursor for scanning
96 * @mm_slot: the current mm_slot we are scanning
97 * @address: the next address inside that to be scanned
Hugh Dickins6514d512009-12-14 17:59:19 -080098 * @rmap_list: link to the next rmap to be scanned in the rmap_list
Izik Eidus31dbd012009-09-21 17:02:03 -070099 * @seqnr: count of completed full scans (needed when removing unstable node)
100 *
101 * There is only the one ksm_scan instance of this cursor structure.
102 */
103struct ksm_scan {
104 struct mm_slot *mm_slot;
105 unsigned long address;
Hugh Dickins6514d512009-12-14 17:59:19 -0800106 struct rmap_item **rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -0700107 unsigned long seqnr;
108};
109
110/**
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800111 * struct stable_node - node of the stable rbtree
112 * @node: rb node of this ksm page in the stable tree
113 * @hlist: hlist head of rmap_items using this ksm page
Hugh Dickins62b61f62009-12-14 17:59:33 -0800114 * @kpfn: page frame number of this ksm page
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800115 */
116struct stable_node {
117 struct rb_node node;
118 struct hlist_head hlist;
Hugh Dickins62b61f62009-12-14 17:59:33 -0800119 unsigned long kpfn;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800120};
121
122/**
Izik Eidus31dbd012009-09-21 17:02:03 -0700123 * struct rmap_item - reverse mapping item for virtual addresses
Hugh Dickins6514d512009-12-14 17:59:19 -0800124 * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800125 * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
Izik Eidus31dbd012009-09-21 17:02:03 -0700126 * @mm: the memory structure this rmap_item is pointing into
127 * @address: the virtual address this rmap_item tracks (+ flags in low bits)
128 * @oldchecksum: previous checksum of the page at that virtual address
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800129 * @node: rb node of this rmap_item in the unstable tree
130 * @head: pointer to stable_node heading this list in the stable tree
131 * @hlist: link into hlist of rmap_items hanging off that stable_node
Izik Eidus31dbd012009-09-21 17:02:03 -0700132 */
133struct rmap_item {
Hugh Dickins6514d512009-12-14 17:59:19 -0800134 struct rmap_item *rmap_list;
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800135 struct anon_vma *anon_vma; /* when stable */
Izik Eidus31dbd012009-09-21 17:02:03 -0700136 struct mm_struct *mm;
137 unsigned long address; /* + low bits used for flags below */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800138 unsigned int oldchecksum; /* when unstable */
Izik Eidus31dbd012009-09-21 17:02:03 -0700139 union {
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800140 struct rb_node node; /* when node of unstable tree */
141 struct { /* when listed from stable tree */
142 struct stable_node *head;
143 struct hlist_node hlist;
144 };
Izik Eidus31dbd012009-09-21 17:02:03 -0700145 };
146};
147
148#define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800149#define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */
150#define STABLE_FLAG 0x200 /* is listed from the stable tree */
Izik Eidus31dbd012009-09-21 17:02:03 -0700151
152/* The stable and unstable tree heads */
153static struct rb_root root_stable_tree = RB_ROOT;
154static struct rb_root root_unstable_tree = RB_ROOT;
155
156#define MM_SLOTS_HASH_HEADS 1024
157static struct hlist_head *mm_slots_hash;
158
159static struct mm_slot ksm_mm_head = {
160 .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
161};
162static struct ksm_scan ksm_scan = {
163 .mm_slot = &ksm_mm_head,
164};
165
166static struct kmem_cache *rmap_item_cache;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800167static struct kmem_cache *stable_node_cache;
Izik Eidus31dbd012009-09-21 17:02:03 -0700168static struct kmem_cache *mm_slot_cache;
169
170/* The number of nodes in the stable tree */
Hugh Dickinsb4028262009-09-21 17:02:09 -0700171static unsigned long ksm_pages_shared;
Izik Eidus31dbd012009-09-21 17:02:03 -0700172
Hugh Dickinse178dfd2009-09-21 17:02:10 -0700173/* The number of page slots additionally sharing those nodes */
Hugh Dickinsb4028262009-09-21 17:02:09 -0700174static unsigned long ksm_pages_sharing;
Izik Eidus31dbd012009-09-21 17:02:03 -0700175
Hugh Dickins473b0ce2009-09-21 17:02:11 -0700176/* The number of nodes in the unstable tree */
177static unsigned long ksm_pages_unshared;
178
179/* The number of rmap_items in use: to calculate pages_volatile */
180static unsigned long ksm_rmap_items;
181
Izik Eidus31dbd012009-09-21 17:02:03 -0700182/* Number of pages ksmd should scan in one batch */
Izik Eidus2c6854f2009-09-23 15:56:04 -0700183static unsigned int ksm_thread_pages_to_scan = 100;
Izik Eidus31dbd012009-09-21 17:02:03 -0700184
185/* Milliseconds ksmd should sleep between batches */
Hugh Dickins2ffd8672009-09-21 17:02:23 -0700186static unsigned int ksm_thread_sleep_millisecs = 20;
Izik Eidus31dbd012009-09-21 17:02:03 -0700187
188#define KSM_RUN_STOP 0
189#define KSM_RUN_MERGE 1
190#define KSM_RUN_UNMERGE 2
Izik Eidus2c6854f2009-09-23 15:56:04 -0700191static unsigned int ksm_run = KSM_RUN_STOP;
Izik Eidus31dbd012009-09-21 17:02:03 -0700192
193static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
194static DEFINE_MUTEX(ksm_thread_mutex);
195static DEFINE_SPINLOCK(ksm_mmlist_lock);
196
197#define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
198 sizeof(struct __struct), __alignof__(struct __struct),\
199 (__flags), NULL)
200
201static int __init ksm_slab_init(void)
202{
203 rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
204 if (!rmap_item_cache)
205 goto out;
206
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800207 stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
208 if (!stable_node_cache)
209 goto out_free1;
210
Izik Eidus31dbd012009-09-21 17:02:03 -0700211 mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
212 if (!mm_slot_cache)
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800213 goto out_free2;
Izik Eidus31dbd012009-09-21 17:02:03 -0700214
215 return 0;
216
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800217out_free2:
218 kmem_cache_destroy(stable_node_cache);
219out_free1:
Izik Eidus31dbd012009-09-21 17:02:03 -0700220 kmem_cache_destroy(rmap_item_cache);
221out:
222 return -ENOMEM;
223}
224
225static void __init ksm_slab_free(void)
226{
227 kmem_cache_destroy(mm_slot_cache);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800228 kmem_cache_destroy(stable_node_cache);
Izik Eidus31dbd012009-09-21 17:02:03 -0700229 kmem_cache_destroy(rmap_item_cache);
230 mm_slot_cache = NULL;
231}
232
233static inline struct rmap_item *alloc_rmap_item(void)
234{
Hugh Dickins473b0ce2009-09-21 17:02:11 -0700235 struct rmap_item *rmap_item;
236
237 rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL);
238 if (rmap_item)
239 ksm_rmap_items++;
240 return rmap_item;
Izik Eidus31dbd012009-09-21 17:02:03 -0700241}
242
243static inline void free_rmap_item(struct rmap_item *rmap_item)
244{
Hugh Dickins473b0ce2009-09-21 17:02:11 -0700245 ksm_rmap_items--;
Izik Eidus31dbd012009-09-21 17:02:03 -0700246 rmap_item->mm = NULL; /* debug safety */
247 kmem_cache_free(rmap_item_cache, rmap_item);
248}
249
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800250static inline struct stable_node *alloc_stable_node(void)
251{
252 return kmem_cache_alloc(stable_node_cache, GFP_KERNEL);
253}
254
255static inline void free_stable_node(struct stable_node *stable_node)
256{
257 kmem_cache_free(stable_node_cache, stable_node);
258}
259
Izik Eidus31dbd012009-09-21 17:02:03 -0700260static inline struct mm_slot *alloc_mm_slot(void)
261{
262 if (!mm_slot_cache) /* initialization failed */
263 return NULL;
264 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
265}
266
267static inline void free_mm_slot(struct mm_slot *mm_slot)
268{
269 kmem_cache_free(mm_slot_cache, mm_slot);
270}
271
272static int __init mm_slots_hash_init(void)
273{
274 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
275 GFP_KERNEL);
276 if (!mm_slots_hash)
277 return -ENOMEM;
278 return 0;
279}
280
281static void __init mm_slots_hash_free(void)
282{
283 kfree(mm_slots_hash);
284}
285
286static struct mm_slot *get_mm_slot(struct mm_struct *mm)
287{
288 struct mm_slot *mm_slot;
289 struct hlist_head *bucket;
290 struct hlist_node *node;
291
292 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
293 % MM_SLOTS_HASH_HEADS];
294 hlist_for_each_entry(mm_slot, node, bucket, link) {
295 if (mm == mm_slot->mm)
296 return mm_slot;
297 }
298 return NULL;
299}
300
301static void insert_to_mm_slots_hash(struct mm_struct *mm,
302 struct mm_slot *mm_slot)
303{
304 struct hlist_head *bucket;
305
306 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
307 % MM_SLOTS_HASH_HEADS];
308 mm_slot->mm = mm;
Izik Eidus31dbd012009-09-21 17:02:03 -0700309 hlist_add_head(&mm_slot->link, bucket);
310}
311
312static inline int in_stable_tree(struct rmap_item *rmap_item)
313{
314 return rmap_item->address & STABLE_FLAG;
315}
316
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800317static void hold_anon_vma(struct rmap_item *rmap_item,
318 struct anon_vma *anon_vma)
319{
320 rmap_item->anon_vma = anon_vma;
Rik van Riel76545062010-08-09 17:18:41 -0700321 get_anon_vma(anon_vma);
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800322}
323
Rik van Riel76545062010-08-09 17:18:41 -0700324static void ksm_drop_anon_vma(struct rmap_item *rmap_item)
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800325{
326 struct anon_vma *anon_vma = rmap_item->anon_vma;
327
Rik van Riel76545062010-08-09 17:18:41 -0700328 drop_anon_vma(anon_vma);
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800329}
330
Izik Eidus31dbd012009-09-21 17:02:03 -0700331/*
Hugh Dickinsa913e182009-09-21 17:02:26 -0700332 * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
333 * page tables after it has passed through ksm_exit() - which, if necessary,
334 * takes mmap_sem briefly to serialize against them. ksm_exit() does not set
335 * a special flag: they can just back out as soon as mm_users goes to zero.
336 * ksm_test_exit() is used throughout to make this test for exit: in some
337 * places for correctness, in some places just to avoid unnecessary work.
338 */
339static inline bool ksm_test_exit(struct mm_struct *mm)
340{
341 return atomic_read(&mm->mm_users) == 0;
342}
343
344/*
Izik Eidus31dbd012009-09-21 17:02:03 -0700345 * We use break_ksm to break COW on a ksm page: it's a stripped down
346 *
347 * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
348 * put_page(page);
349 *
350 * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
351 * in case the application has unmapped and remapped mm,addr meanwhile.
352 * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP
353 * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
354 */
Hugh Dickinsd952b792009-09-21 17:02:16 -0700355static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
Izik Eidus31dbd012009-09-21 17:02:03 -0700356{
357 struct page *page;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700358 int ret = 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700359
360 do {
361 cond_resched();
362 page = follow_page(vma, addr, FOLL_GET);
Dan Carpenter22eccdd2010-04-23 13:18:10 -0400363 if (IS_ERR_OR_NULL(page))
Izik Eidus31dbd012009-09-21 17:02:03 -0700364 break;
365 if (PageKsm(page))
366 ret = handle_mm_fault(vma->vm_mm, vma, addr,
367 FAULT_FLAG_WRITE);
368 else
369 ret = VM_FAULT_WRITE;
370 put_page(page);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700371 } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM)));
372 /*
373 * We must loop because handle_mm_fault() may back out if there's
374 * any difficulty e.g. if pte accessed bit gets updated concurrently.
375 *
376 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
377 * COW has been broken, even if the vma does not permit VM_WRITE;
378 * but note that a concurrent fault might break PageKsm for us.
379 *
380 * VM_FAULT_SIGBUS could occur if we race with truncation of the
381 * backing file, which also invalidates anonymous pages: that's
382 * okay, that truncation will have unmapped the PageKsm for us.
383 *
384 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
385 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
386 * current task has TIF_MEMDIE set, and will be OOM killed on return
387 * to user; and ksmd, having no mm, would never be chosen for that.
388 *
389 * But if the mm is in a limited mem_cgroup, then the fault may fail
390 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
391 * even ksmd can fail in this way - though it's usually breaking ksm
392 * just to undo a merge it made a moment before, so unlikely to oom.
393 *
394 * That's a pity: we might therefore have more kernel pages allocated
395 * than we're counting as nodes in the stable tree; but ksm_do_scan
396 * will retry to break_cow on each pass, so should recover the page
397 * in due course. The important thing is to not let VM_MERGEABLE
398 * be cleared while any such pages might remain in the area.
399 */
400 return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700401}
402
Hugh Dickins8dd35572009-12-14 17:59:18 -0800403static void break_cow(struct rmap_item *rmap_item)
Izik Eidus31dbd012009-09-21 17:02:03 -0700404{
Hugh Dickins8dd35572009-12-14 17:59:18 -0800405 struct mm_struct *mm = rmap_item->mm;
406 unsigned long addr = rmap_item->address;
Izik Eidus31dbd012009-09-21 17:02:03 -0700407 struct vm_area_struct *vma;
408
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800409 /*
410 * It is not an accident that whenever we want to break COW
411 * to undo, we also need to drop a reference to the anon_vma.
412 */
Rik van Riel76545062010-08-09 17:18:41 -0700413 ksm_drop_anon_vma(rmap_item);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800414
Hugh Dickins81464e302009-09-21 17:02:15 -0700415 down_read(&mm->mmap_sem);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700416 if (ksm_test_exit(mm))
417 goto out;
Izik Eidus31dbd012009-09-21 17:02:03 -0700418 vma = find_vma(mm, addr);
419 if (!vma || vma->vm_start > addr)
Hugh Dickins81464e302009-09-21 17:02:15 -0700420 goto out;
Izik Eidus31dbd012009-09-21 17:02:03 -0700421 if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
Hugh Dickins81464e302009-09-21 17:02:15 -0700422 goto out;
Izik Eidus31dbd012009-09-21 17:02:03 -0700423 break_ksm(vma, addr);
Hugh Dickins81464e302009-09-21 17:02:15 -0700424out:
Izik Eidus31dbd012009-09-21 17:02:03 -0700425 up_read(&mm->mmap_sem);
426}
427
428static struct page *get_mergeable_page(struct rmap_item *rmap_item)
429{
430 struct mm_struct *mm = rmap_item->mm;
431 unsigned long addr = rmap_item->address;
432 struct vm_area_struct *vma;
433 struct page *page;
434
435 down_read(&mm->mmap_sem);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700436 if (ksm_test_exit(mm))
437 goto out;
Izik Eidus31dbd012009-09-21 17:02:03 -0700438 vma = find_vma(mm, addr);
439 if (!vma || vma->vm_start > addr)
440 goto out;
441 if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
442 goto out;
443
444 page = follow_page(vma, addr, FOLL_GET);
Dan Carpenter22eccdd2010-04-23 13:18:10 -0400445 if (IS_ERR_OR_NULL(page))
Izik Eidus31dbd012009-09-21 17:02:03 -0700446 goto out;
447 if (PageAnon(page)) {
448 flush_anon_page(vma, page, addr);
449 flush_dcache_page(page);
450 } else {
451 put_page(page);
452out: page = NULL;
453 }
454 up_read(&mm->mmap_sem);
455 return page;
456}
457
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800458static void remove_node_from_stable_tree(struct stable_node *stable_node)
459{
460 struct rmap_item *rmap_item;
461 struct hlist_node *hlist;
462
463 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
464 if (rmap_item->hlist.next)
465 ksm_pages_sharing--;
466 else
467 ksm_pages_shared--;
Rik van Riel76545062010-08-09 17:18:41 -0700468 ksm_drop_anon_vma(rmap_item);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800469 rmap_item->address &= PAGE_MASK;
470 cond_resched();
471 }
472
473 rb_erase(&stable_node->node, &root_stable_tree);
474 free_stable_node(stable_node);
475}
476
477/*
478 * get_ksm_page: checks if the page indicated by the stable node
479 * is still its ksm page, despite having held no reference to it.
480 * In which case we can trust the content of the page, and it
481 * returns the gotten page; but if the page has now been zapped,
482 * remove the stale node from the stable tree and return NULL.
483 *
484 * You would expect the stable_node to hold a reference to the ksm page.
485 * But if it increments the page's count, swapping out has to wait for
486 * ksmd to come around again before it can free the page, which may take
487 * seconds or even minutes: much too unresponsive. So instead we use a
488 * "keyhole reference": access to the ksm page from the stable node peeps
489 * out through its keyhole to see if that page still holds the right key,
490 * pointing back to this stable node. This relies on freeing a PageAnon
491 * page to reset its page->mapping to NULL, and relies on no other use of
492 * a page to put something that might look like our key in page->mapping.
493 *
494 * include/linux/pagemap.h page_cache_get_speculative() is a good reference,
495 * but this is different - made simpler by ksm_thread_mutex being held, but
496 * interesting for assuming that no other use of the struct page could ever
497 * put our expected_mapping into page->mapping (or a field of the union which
498 * coincides with page->mapping). The RCU calls are not for KSM at all, but
499 * to keep the page_count protocol described with page_cache_get_speculative.
500 *
501 * Note: it is possible that get_ksm_page() will return NULL one moment,
502 * then page the next, if the page is in between page_freeze_refs() and
503 * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page
504 * is on its way to being freed; but it is an anomaly to bear in mind.
505 */
506static struct page *get_ksm_page(struct stable_node *stable_node)
507{
508 struct page *page;
509 void *expected_mapping;
510
Hugh Dickins62b61f62009-12-14 17:59:33 -0800511 page = pfn_to_page(stable_node->kpfn);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800512 expected_mapping = (void *)stable_node +
513 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
514 rcu_read_lock();
515 if (page->mapping != expected_mapping)
516 goto stale;
517 if (!get_page_unless_zero(page))
518 goto stale;
519 if (page->mapping != expected_mapping) {
520 put_page(page);
521 goto stale;
522 }
523 rcu_read_unlock();
524 return page;
525stale:
526 rcu_read_unlock();
527 remove_node_from_stable_tree(stable_node);
528 return NULL;
529}
530
Izik Eidus31dbd012009-09-21 17:02:03 -0700531/*
Izik Eidus31dbd012009-09-21 17:02:03 -0700532 * Removing rmap_item from stable or unstable tree.
533 * This function will clean the information from the stable/unstable tree.
534 */
535static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
536{
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800537 if (rmap_item->address & STABLE_FLAG) {
538 struct stable_node *stable_node;
Hugh Dickins5ad64682009-12-14 17:59:24 -0800539 struct page *page;
Izik Eidus31dbd012009-09-21 17:02:03 -0700540
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800541 stable_node = rmap_item->head;
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800542 page = get_ksm_page(stable_node);
543 if (!page)
544 goto out;
545
Hugh Dickins5ad64682009-12-14 17:59:24 -0800546 lock_page(page);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800547 hlist_del(&rmap_item->hlist);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800548 unlock_page(page);
549 put_page(page);
Hugh Dickins08beca42009-12-14 17:59:21 -0800550
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800551 if (stable_node->hlist.first)
552 ksm_pages_sharing--;
553 else
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800554 ksm_pages_shared--;
Izik Eidus31dbd012009-09-21 17:02:03 -0700555
Rik van Riel76545062010-08-09 17:18:41 -0700556 ksm_drop_anon_vma(rmap_item);
Hugh Dickins93d17712009-12-14 17:59:16 -0800557 rmap_item->address &= PAGE_MASK;
Izik Eidus31dbd012009-09-21 17:02:03 -0700558
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800559 } else if (rmap_item->address & UNSTABLE_FLAG) {
Izik Eidus31dbd012009-09-21 17:02:03 -0700560 unsigned char age;
561 /*
Hugh Dickins9ba69292009-09-21 17:02:20 -0700562 * Usually ksmd can and must skip the rb_erase, because
Izik Eidus31dbd012009-09-21 17:02:03 -0700563 * root_unstable_tree was already reset to RB_ROOT.
Hugh Dickins9ba69292009-09-21 17:02:20 -0700564 * But be careful when an mm is exiting: do the rb_erase
565 * if this rmap_item was inserted by this scan, rather
566 * than left over from before.
Izik Eidus31dbd012009-09-21 17:02:03 -0700567 */
568 age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
Hugh Dickinscd551f92009-09-21 17:02:17 -0700569 BUG_ON(age > 1);
Izik Eidus31dbd012009-09-21 17:02:03 -0700570 if (!age)
571 rb_erase(&rmap_item->node, &root_unstable_tree);
Izik Eidus31dbd012009-09-21 17:02:03 -0700572
Hugh Dickins93d17712009-12-14 17:59:16 -0800573 ksm_pages_unshared--;
574 rmap_item->address &= PAGE_MASK;
575 }
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800576out:
Izik Eidus31dbd012009-09-21 17:02:03 -0700577 cond_resched(); /* we're called from many long loops */
578}
579
Izik Eidus31dbd012009-09-21 17:02:03 -0700580static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
Hugh Dickins6514d512009-12-14 17:59:19 -0800581 struct rmap_item **rmap_list)
Izik Eidus31dbd012009-09-21 17:02:03 -0700582{
Hugh Dickins6514d512009-12-14 17:59:19 -0800583 while (*rmap_list) {
584 struct rmap_item *rmap_item = *rmap_list;
585 *rmap_list = rmap_item->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -0700586 remove_rmap_item_from_tree(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -0700587 free_rmap_item(rmap_item);
588 }
589}
590
591/*
592 * Though it's very tempting to unmerge in_stable_tree(rmap_item)s rather
593 * than check every pte of a given vma, the locking doesn't quite work for
594 * that - an rmap_item is assigned to the stable tree after inserting ksm
595 * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing
596 * rmap_items from parent to child at fork time (so as not to waste time
597 * if exit comes before the next scan reaches it).
Hugh Dickins81464e302009-09-21 17:02:15 -0700598 *
599 * Similarly, although we'd like to remove rmap_items (so updating counts
600 * and freeing memory) when unmerging an area, it's easier to leave that
601 * to the next pass of ksmd - consider, for example, how ksmd might be
602 * in cmp_and_merge_page on one of the rmap_items we would be removing.
Izik Eidus31dbd012009-09-21 17:02:03 -0700603 */
Hugh Dickinsd952b792009-09-21 17:02:16 -0700604static int unmerge_ksm_pages(struct vm_area_struct *vma,
605 unsigned long start, unsigned long end)
Izik Eidus31dbd012009-09-21 17:02:03 -0700606{
607 unsigned long addr;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700608 int err = 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700609
Hugh Dickinsd952b792009-09-21 17:02:16 -0700610 for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
Hugh Dickins9ba69292009-09-21 17:02:20 -0700611 if (ksm_test_exit(vma->vm_mm))
612 break;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700613 if (signal_pending(current))
614 err = -ERESTARTSYS;
615 else
616 err = break_ksm(vma, addr);
617 }
618 return err;
Izik Eidus31dbd012009-09-21 17:02:03 -0700619}
620
Hugh Dickins2ffd8672009-09-21 17:02:23 -0700621#ifdef CONFIG_SYSFS
622/*
623 * Only called through the sysfs control interface:
624 */
Hugh Dickinsd952b792009-09-21 17:02:16 -0700625static int unmerge_and_remove_all_rmap_items(void)
Izik Eidus31dbd012009-09-21 17:02:03 -0700626{
627 struct mm_slot *mm_slot;
628 struct mm_struct *mm;
629 struct vm_area_struct *vma;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700630 int err = 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700631
Hugh Dickinsd952b792009-09-21 17:02:16 -0700632 spin_lock(&ksm_mmlist_lock);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700633 ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
Hugh Dickinsd952b792009-09-21 17:02:16 -0700634 struct mm_slot, mm_list);
635 spin_unlock(&ksm_mmlist_lock);
636
Hugh Dickins9ba69292009-09-21 17:02:20 -0700637 for (mm_slot = ksm_scan.mm_slot;
638 mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
Izik Eidus31dbd012009-09-21 17:02:03 -0700639 mm = mm_slot->mm;
640 down_read(&mm->mmap_sem);
641 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Hugh Dickins9ba69292009-09-21 17:02:20 -0700642 if (ksm_test_exit(mm))
643 break;
Izik Eidus31dbd012009-09-21 17:02:03 -0700644 if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
645 continue;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700646 err = unmerge_ksm_pages(vma,
647 vma->vm_start, vma->vm_end);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700648 if (err)
649 goto error;
Izik Eidus31dbd012009-09-21 17:02:03 -0700650 }
Hugh Dickins9ba69292009-09-21 17:02:20 -0700651
Hugh Dickins6514d512009-12-14 17:59:19 -0800652 remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700653
654 spin_lock(&ksm_mmlist_lock);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700655 ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
Hugh Dickinsd952b792009-09-21 17:02:16 -0700656 struct mm_slot, mm_list);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700657 if (ksm_test_exit(mm)) {
658 hlist_del(&mm_slot->link);
659 list_del(&mm_slot->mm_list);
660 spin_unlock(&ksm_mmlist_lock);
661
662 free_mm_slot(mm_slot);
663 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
664 up_read(&mm->mmap_sem);
665 mmdrop(mm);
666 } else {
667 spin_unlock(&ksm_mmlist_lock);
668 up_read(&mm->mmap_sem);
669 }
Izik Eidus31dbd012009-09-21 17:02:03 -0700670 }
671
Hugh Dickinsd952b792009-09-21 17:02:16 -0700672 ksm_scan.seqnr = 0;
Hugh Dickins9ba69292009-09-21 17:02:20 -0700673 return 0;
674
675error:
676 up_read(&mm->mmap_sem);
Izik Eidus31dbd012009-09-21 17:02:03 -0700677 spin_lock(&ksm_mmlist_lock);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700678 ksm_scan.mm_slot = &ksm_mm_head;
Izik Eidus31dbd012009-09-21 17:02:03 -0700679 spin_unlock(&ksm_mmlist_lock);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700680 return err;
Izik Eidus31dbd012009-09-21 17:02:03 -0700681}
Hugh Dickins2ffd8672009-09-21 17:02:23 -0700682#endif /* CONFIG_SYSFS */
Izik Eidus31dbd012009-09-21 17:02:03 -0700683
Izik Eidus31dbd012009-09-21 17:02:03 -0700684static u32 calc_checksum(struct page *page)
685{
686 u32 checksum;
687 void *addr = kmap_atomic(page, KM_USER0);
688 checksum = jhash2(addr, PAGE_SIZE / 4, 17);
689 kunmap_atomic(addr, KM_USER0);
690 return checksum;
691}
692
693static int memcmp_pages(struct page *page1, struct page *page2)
694{
695 char *addr1, *addr2;
696 int ret;
697
698 addr1 = kmap_atomic(page1, KM_USER0);
699 addr2 = kmap_atomic(page2, KM_USER1);
700 ret = memcmp(addr1, addr2, PAGE_SIZE);
701 kunmap_atomic(addr2, KM_USER1);
702 kunmap_atomic(addr1, KM_USER0);
703 return ret;
704}
705
706static inline int pages_identical(struct page *page1, struct page *page2)
707{
708 return !memcmp_pages(page1, page2);
709}
710
711static int write_protect_page(struct vm_area_struct *vma, struct page *page,
712 pte_t *orig_pte)
713{
714 struct mm_struct *mm = vma->vm_mm;
715 unsigned long addr;
716 pte_t *ptep;
717 spinlock_t *ptl;
718 int swapped;
719 int err = -EFAULT;
720
721 addr = page_address_in_vma(page, vma);
722 if (addr == -EFAULT)
723 goto out;
724
725 ptep = page_check_address(page, mm, addr, &ptl, 0);
726 if (!ptep)
727 goto out;
728
729 if (pte_write(*ptep)) {
730 pte_t entry;
731
732 swapped = PageSwapCache(page);
733 flush_cache_page(vma, addr, page_to_pfn(page));
734 /*
735 * Ok this is tricky, when get_user_pages_fast() run it doesnt
736 * take any lock, therefore the check that we are going to make
737 * with the pagecount against the mapcount is racey and
738 * O_DIRECT can happen right after the check.
739 * So we clear the pte and flush the tlb before the check
740 * this assure us that no O_DIRECT can happen after the check
741 * or in the middle of the check.
742 */
743 entry = ptep_clear_flush(vma, addr, ptep);
744 /*
745 * Check that no O_DIRECT or similar I/O is in progress on the
746 * page
747 */
Hugh Dickins31e855e2009-12-14 17:59:17 -0800748 if (page_mapcount(page) + 1 + swapped != page_count(page)) {
Robin Holtcb532372010-03-23 13:35:26 -0700749 set_pte_at(mm, addr, ptep, entry);
Izik Eidus31dbd012009-09-21 17:02:03 -0700750 goto out_unlock;
751 }
752 entry = pte_wrprotect(entry);
753 set_pte_at_notify(mm, addr, ptep, entry);
754 }
755 *orig_pte = *ptep;
756 err = 0;
757
758out_unlock:
759 pte_unmap_unlock(ptep, ptl);
760out:
761 return err;
762}
763
764/**
765 * replace_page - replace page in vma by new ksm page
Hugh Dickins8dd35572009-12-14 17:59:18 -0800766 * @vma: vma that holds the pte pointing to page
767 * @page: the page we are replacing by kpage
768 * @kpage: the ksm page we replace page by
Izik Eidus31dbd012009-09-21 17:02:03 -0700769 * @orig_pte: the original value of the pte
770 *
771 * Returns 0 on success, -EFAULT on failure.
772 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800773static int replace_page(struct vm_area_struct *vma, struct page *page,
774 struct page *kpage, pte_t orig_pte)
Izik Eidus31dbd012009-09-21 17:02:03 -0700775{
776 struct mm_struct *mm = vma->vm_mm;
777 pgd_t *pgd;
778 pud_t *pud;
779 pmd_t *pmd;
780 pte_t *ptep;
781 spinlock_t *ptl;
782 unsigned long addr;
Izik Eidus31dbd012009-09-21 17:02:03 -0700783 int err = -EFAULT;
784
Hugh Dickins8dd35572009-12-14 17:59:18 -0800785 addr = page_address_in_vma(page, vma);
Izik Eidus31dbd012009-09-21 17:02:03 -0700786 if (addr == -EFAULT)
787 goto out;
788
789 pgd = pgd_offset(mm, addr);
790 if (!pgd_present(*pgd))
791 goto out;
792
793 pud = pud_offset(pgd, addr);
794 if (!pud_present(*pud))
795 goto out;
796
797 pmd = pmd_offset(pud, addr);
798 if (!pmd_present(*pmd))
799 goto out;
800
801 ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
802 if (!pte_same(*ptep, orig_pte)) {
803 pte_unmap_unlock(ptep, ptl);
804 goto out;
805 }
806
Hugh Dickins8dd35572009-12-14 17:59:18 -0800807 get_page(kpage);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800808 page_add_anon_rmap(kpage, vma, addr);
Izik Eidus31dbd012009-09-21 17:02:03 -0700809
810 flush_cache_page(vma, addr, pte_pfn(*ptep));
811 ptep_clear_flush(vma, addr, ptep);
Hugh Dickins8dd35572009-12-14 17:59:18 -0800812 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
Izik Eidus31dbd012009-09-21 17:02:03 -0700813
Hugh Dickins8dd35572009-12-14 17:59:18 -0800814 page_remove_rmap(page);
815 put_page(page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700816
817 pte_unmap_unlock(ptep, ptl);
818 err = 0;
819out:
820 return err;
821}
822
823/*
824 * try_to_merge_one_page - take two pages and merge them into one
Hugh Dickins8dd35572009-12-14 17:59:18 -0800825 * @vma: the vma that holds the pte pointing to page
826 * @page: the PageAnon page that we want to replace with kpage
Hugh Dickins80e148222009-12-14 17:59:29 -0800827 * @kpage: the PageKsm page that we want to map instead of page,
828 * or NULL the first time when we want to use page as kpage.
Izik Eidus31dbd012009-09-21 17:02:03 -0700829 *
830 * This function returns 0 if the pages were merged, -EFAULT otherwise.
831 */
832static int try_to_merge_one_page(struct vm_area_struct *vma,
Hugh Dickins8dd35572009-12-14 17:59:18 -0800833 struct page *page, struct page *kpage)
Izik Eidus31dbd012009-09-21 17:02:03 -0700834{
835 pte_t orig_pte = __pte(0);
836 int err = -EFAULT;
837
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800838 if (page == kpage) /* ksm page forked */
839 return 0;
840
Izik Eidus31dbd012009-09-21 17:02:03 -0700841 if (!(vma->vm_flags & VM_MERGEABLE))
842 goto out;
Hugh Dickins8dd35572009-12-14 17:59:18 -0800843 if (!PageAnon(page))
Izik Eidus31dbd012009-09-21 17:02:03 -0700844 goto out;
845
Izik Eidus31dbd012009-09-21 17:02:03 -0700846 /*
847 * We need the page lock to read a stable PageSwapCache in
848 * write_protect_page(). We use trylock_page() instead of
849 * lock_page() because we don't want to wait here - we
850 * prefer to continue scanning and merging different pages,
851 * then come back to this page when it is unlocked.
852 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800853 if (!trylock_page(page))
Hugh Dickins31e855e2009-12-14 17:59:17 -0800854 goto out;
Izik Eidus31dbd012009-09-21 17:02:03 -0700855 /*
856 * If this anonymous page is mapped only here, its pte may need
857 * to be write-protected. If it's mapped elsewhere, all of its
858 * ptes are necessarily already write-protected. But in either
859 * case, we need to lock and check page_count is not raised.
860 */
Hugh Dickins80e148222009-12-14 17:59:29 -0800861 if (write_protect_page(vma, page, &orig_pte) == 0) {
862 if (!kpage) {
863 /*
864 * While we hold page lock, upgrade page from
865 * PageAnon+anon_vma to PageKsm+NULL stable_node:
866 * stable_tree_insert() will update stable_node.
867 */
868 set_page_stable_node(page, NULL);
869 mark_page_accessed(page);
870 err = 0;
871 } else if (pages_identical(page, kpage))
872 err = replace_page(vma, page, kpage, orig_pte);
873 }
Izik Eidus31dbd012009-09-21 17:02:03 -0700874
Hugh Dickins80e148222009-12-14 17:59:29 -0800875 if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
Hugh Dickins73848b42009-12-14 17:59:22 -0800876 munlock_vma_page(page);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800877 if (!PageMlocked(kpage)) {
878 unlock_page(page);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800879 lock_page(kpage);
880 mlock_vma_page(kpage);
881 page = kpage; /* for final unlock */
882 }
883 }
Hugh Dickins73848b42009-12-14 17:59:22 -0800884
Hugh Dickins8dd35572009-12-14 17:59:18 -0800885 unlock_page(page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700886out:
887 return err;
888}
889
890/*
Hugh Dickins81464e302009-09-21 17:02:15 -0700891 * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
892 * but no new kernel page is allocated: kpage must already be a ksm page.
Hugh Dickins8dd35572009-12-14 17:59:18 -0800893 *
894 * This function returns 0 if the pages were merged, -EFAULT otherwise.
Hugh Dickins81464e302009-09-21 17:02:15 -0700895 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800896static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
897 struct page *page, struct page *kpage)
Hugh Dickins81464e302009-09-21 17:02:15 -0700898{
Hugh Dickins8dd35572009-12-14 17:59:18 -0800899 struct mm_struct *mm = rmap_item->mm;
Hugh Dickins81464e302009-09-21 17:02:15 -0700900 struct vm_area_struct *vma;
901 int err = -EFAULT;
902
Hugh Dickins8dd35572009-12-14 17:59:18 -0800903 down_read(&mm->mmap_sem);
904 if (ksm_test_exit(mm))
905 goto out;
906 vma = find_vma(mm, rmap_item->address);
907 if (!vma || vma->vm_start > rmap_item->address)
Hugh Dickins9ba69292009-09-21 17:02:20 -0700908 goto out;
909
Hugh Dickins8dd35572009-12-14 17:59:18 -0800910 err = try_to_merge_one_page(vma, page, kpage);
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800911 if (err)
912 goto out;
913
914 /* Must get reference to anon_vma while still holding mmap_sem */
915 hold_anon_vma(rmap_item, vma->anon_vma);
Hugh Dickins81464e302009-09-21 17:02:15 -0700916out:
Hugh Dickins8dd35572009-12-14 17:59:18 -0800917 up_read(&mm->mmap_sem);
Hugh Dickins81464e302009-09-21 17:02:15 -0700918 return err;
919}
920
921/*
Izik Eidus31dbd012009-09-21 17:02:03 -0700922 * try_to_merge_two_pages - take two identical pages and prepare them
923 * to be merged into one page.
924 *
Hugh Dickins8dd35572009-12-14 17:59:18 -0800925 * This function returns the kpage if we successfully merged two identical
926 * pages into one ksm page, NULL otherwise.
Izik Eidus31dbd012009-09-21 17:02:03 -0700927 *
Hugh Dickins80e148222009-12-14 17:59:29 -0800928 * Note that this function upgrades page to ksm page: if one of the pages
Izik Eidus31dbd012009-09-21 17:02:03 -0700929 * is already a ksm page, try_to_merge_with_ksm_page should be used.
930 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800931static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
932 struct page *page,
933 struct rmap_item *tree_rmap_item,
934 struct page *tree_page)
Izik Eidus31dbd012009-09-21 17:02:03 -0700935{
Hugh Dickins80e148222009-12-14 17:59:29 -0800936 int err;
Izik Eidus31dbd012009-09-21 17:02:03 -0700937
Hugh Dickins80e148222009-12-14 17:59:29 -0800938 err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
Izik Eidus31dbd012009-09-21 17:02:03 -0700939 if (!err) {
Hugh Dickins8dd35572009-12-14 17:59:18 -0800940 err = try_to_merge_with_ksm_page(tree_rmap_item,
Hugh Dickins80e148222009-12-14 17:59:29 -0800941 tree_page, page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700942 /*
Hugh Dickins81464e302009-09-21 17:02:15 -0700943 * If that fails, we have a ksm page with only one pte
944 * pointing to it: so break it.
Izik Eidus31dbd012009-09-21 17:02:03 -0700945 */
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800946 if (err)
Hugh Dickins8dd35572009-12-14 17:59:18 -0800947 break_cow(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -0700948 }
Hugh Dickins80e148222009-12-14 17:59:29 -0800949 return err ? NULL : page;
Izik Eidus31dbd012009-09-21 17:02:03 -0700950}
951
952/*
Hugh Dickins8dd35572009-12-14 17:59:18 -0800953 * stable_tree_search - search for page inside the stable tree
Izik Eidus31dbd012009-09-21 17:02:03 -0700954 *
955 * This function checks if there is a page inside the stable tree
956 * with identical content to the page that we are scanning right now.
957 *
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800958 * This function returns the stable tree node of identical content if found,
Izik Eidus31dbd012009-09-21 17:02:03 -0700959 * NULL otherwise.
960 */
Hugh Dickins62b61f62009-12-14 17:59:33 -0800961static struct page *stable_tree_search(struct page *page)
Izik Eidus31dbd012009-09-21 17:02:03 -0700962{
963 struct rb_node *node = root_stable_tree.rb_node;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800964 struct stable_node *stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -0700965
Hugh Dickins08beca42009-12-14 17:59:21 -0800966 stable_node = page_stable_node(page);
967 if (stable_node) { /* ksm page forked */
968 get_page(page);
Hugh Dickins62b61f62009-12-14 17:59:33 -0800969 return page;
Hugh Dickins08beca42009-12-14 17:59:21 -0800970 }
971
Izik Eidus31dbd012009-09-21 17:02:03 -0700972 while (node) {
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800973 struct page *tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -0700974 int ret;
975
Hugh Dickins08beca42009-12-14 17:59:21 -0800976 cond_resched();
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800977 stable_node = rb_entry(node, struct stable_node, node);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800978 tree_page = get_ksm_page(stable_node);
979 if (!tree_page)
980 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -0700981
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800982 ret = memcmp_pages(page, tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700983
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800984 if (ret < 0) {
985 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700986 node = node->rb_left;
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800987 } else if (ret > 0) {
988 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700989 node = node->rb_right;
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800990 } else
Hugh Dickins62b61f62009-12-14 17:59:33 -0800991 return tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -0700992 }
993
994 return NULL;
995}
996
997/*
998 * stable_tree_insert - insert rmap_item pointing to new ksm page
999 * into the stable tree.
1000 *
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001001 * This function returns the stable tree node just allocated on success,
1002 * NULL otherwise.
Izik Eidus31dbd012009-09-21 17:02:03 -07001003 */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001004static struct stable_node *stable_tree_insert(struct page *kpage)
Izik Eidus31dbd012009-09-21 17:02:03 -07001005{
1006 struct rb_node **new = &root_stable_tree.rb_node;
1007 struct rb_node *parent = NULL;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001008 struct stable_node *stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001009
1010 while (*new) {
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001011 struct page *tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001012 int ret;
1013
Hugh Dickins08beca42009-12-14 17:59:21 -08001014 cond_resched();
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001015 stable_node = rb_entry(*new, struct stable_node, node);
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001016 tree_page = get_ksm_page(stable_node);
1017 if (!tree_page)
1018 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -07001019
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001020 ret = memcmp_pages(kpage, tree_page);
1021 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001022
1023 parent = *new;
1024 if (ret < 0)
1025 new = &parent->rb_left;
1026 else if (ret > 0)
1027 new = &parent->rb_right;
1028 else {
1029 /*
1030 * It is not a bug that stable_tree_search() didn't
1031 * find this node: because at that time our page was
1032 * not yet write-protected, so may have changed since.
1033 */
1034 return NULL;
1035 }
1036 }
1037
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001038 stable_node = alloc_stable_node();
1039 if (!stable_node)
1040 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -07001041
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001042 rb_link_node(&stable_node->node, parent, new);
1043 rb_insert_color(&stable_node->node, &root_stable_tree);
1044
1045 INIT_HLIST_HEAD(&stable_node->hlist);
1046
Hugh Dickins62b61f62009-12-14 17:59:33 -08001047 stable_node->kpfn = page_to_pfn(kpage);
Hugh Dickins08beca42009-12-14 17:59:21 -08001048 set_page_stable_node(kpage, stable_node);
1049
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001050 return stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001051}
1052
1053/*
Hugh Dickins8dd35572009-12-14 17:59:18 -08001054 * unstable_tree_search_insert - search for identical page,
1055 * else insert rmap_item into the unstable tree.
Izik Eidus31dbd012009-09-21 17:02:03 -07001056 *
1057 * This function searches for a page in the unstable tree identical to the
1058 * page currently being scanned; and if no identical page is found in the
1059 * tree, we insert rmap_item as a new object into the unstable tree.
1060 *
1061 * This function returns pointer to rmap_item found to be identical
1062 * to the currently scanned page, NULL otherwise.
1063 *
1064 * This function does both searching and inserting, because they share
1065 * the same walking algorithm in an rbtree.
1066 */
Hugh Dickins8dd35572009-12-14 17:59:18 -08001067static
1068struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
1069 struct page *page,
1070 struct page **tree_pagep)
1071
Izik Eidus31dbd012009-09-21 17:02:03 -07001072{
1073 struct rb_node **new = &root_unstable_tree.rb_node;
1074 struct rb_node *parent = NULL;
1075
1076 while (*new) {
1077 struct rmap_item *tree_rmap_item;
Hugh Dickins8dd35572009-12-14 17:59:18 -08001078 struct page *tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001079 int ret;
1080
Hugh Dickinsd178f272009-11-09 15:58:23 +00001081 cond_resched();
Izik Eidus31dbd012009-09-21 17:02:03 -07001082 tree_rmap_item = rb_entry(*new, struct rmap_item, node);
Hugh Dickins8dd35572009-12-14 17:59:18 -08001083 tree_page = get_mergeable_page(tree_rmap_item);
Dan Carpenter22eccdd2010-04-23 13:18:10 -04001084 if (IS_ERR_OR_NULL(tree_page))
Izik Eidus31dbd012009-09-21 17:02:03 -07001085 return NULL;
1086
1087 /*
Hugh Dickins8dd35572009-12-14 17:59:18 -08001088 * Don't substitute a ksm page for a forked page.
Izik Eidus31dbd012009-09-21 17:02:03 -07001089 */
Hugh Dickins8dd35572009-12-14 17:59:18 -08001090 if (page == tree_page) {
1091 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001092 return NULL;
1093 }
1094
Hugh Dickins8dd35572009-12-14 17:59:18 -08001095 ret = memcmp_pages(page, tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001096
1097 parent = *new;
1098 if (ret < 0) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001099 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001100 new = &parent->rb_left;
1101 } else if (ret > 0) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001102 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001103 new = &parent->rb_right;
1104 } else {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001105 *tree_pagep = tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001106 return tree_rmap_item;
1107 }
1108 }
1109
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001110 rmap_item->address |= UNSTABLE_FLAG;
Izik Eidus31dbd012009-09-21 17:02:03 -07001111 rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
1112 rb_link_node(&rmap_item->node, parent, new);
1113 rb_insert_color(&rmap_item->node, &root_unstable_tree);
1114
Hugh Dickins473b0ce2009-09-21 17:02:11 -07001115 ksm_pages_unshared++;
Izik Eidus31dbd012009-09-21 17:02:03 -07001116 return NULL;
1117}
1118
1119/*
1120 * stable_tree_append - add another rmap_item to the linked list of
1121 * rmap_items hanging off a given node of the stable tree, all sharing
1122 * the same ksm page.
1123 */
1124static void stable_tree_append(struct rmap_item *rmap_item,
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001125 struct stable_node *stable_node)
Izik Eidus31dbd012009-09-21 17:02:03 -07001126{
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001127 rmap_item->head = stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001128 rmap_item->address |= STABLE_FLAG;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001129 hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
Hugh Dickinse178dfd2009-09-21 17:02:10 -07001130
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001131 if (rmap_item->hlist.next)
1132 ksm_pages_sharing++;
1133 else
1134 ksm_pages_shared++;
Izik Eidus31dbd012009-09-21 17:02:03 -07001135}
1136
1137/*
Hugh Dickins81464e302009-09-21 17:02:15 -07001138 * cmp_and_merge_page - first see if page can be merged into the stable tree;
1139 * if not, compare checksum to previous and if it's the same, see if page can
1140 * be inserted into the unstable tree, or merged with a page already there and
1141 * both transferred to the stable tree.
Izik Eidus31dbd012009-09-21 17:02:03 -07001142 *
1143 * @page: the page that we are searching identical page to.
1144 * @rmap_item: the reverse mapping into the virtual address of this page
1145 */
1146static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
1147{
Izik Eidus31dbd012009-09-21 17:02:03 -07001148 struct rmap_item *tree_rmap_item;
Hugh Dickins8dd35572009-12-14 17:59:18 -08001149 struct page *tree_page = NULL;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001150 struct stable_node *stable_node;
Hugh Dickins8dd35572009-12-14 17:59:18 -08001151 struct page *kpage;
Izik Eidus31dbd012009-09-21 17:02:03 -07001152 unsigned int checksum;
1153 int err;
1154
Hugh Dickins93d17712009-12-14 17:59:16 -08001155 remove_rmap_item_from_tree(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001156
1157 /* We first start with searching the page inside the stable tree */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001158 kpage = stable_tree_search(page);
1159 if (kpage) {
Hugh Dickins08beca42009-12-14 17:59:21 -08001160 err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
Izik Eidus31dbd012009-09-21 17:02:03 -07001161 if (!err) {
1162 /*
1163 * The page was successfully merged:
1164 * add its rmap_item to the stable tree.
1165 */
Hugh Dickins5ad64682009-12-14 17:59:24 -08001166 lock_page(kpage);
Hugh Dickins62b61f62009-12-14 17:59:33 -08001167 stable_tree_append(rmap_item, page_stable_node(kpage));
Hugh Dickins5ad64682009-12-14 17:59:24 -08001168 unlock_page(kpage);
Izik Eidus31dbd012009-09-21 17:02:03 -07001169 }
Hugh Dickins8dd35572009-12-14 17:59:18 -08001170 put_page(kpage);
Izik Eidus31dbd012009-09-21 17:02:03 -07001171 return;
1172 }
1173
1174 /*
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001175 * If the hash value of the page has changed from the last time
1176 * we calculated it, this page is changing frequently: therefore we
1177 * don't want to insert it in the unstable tree, and we don't want
1178 * to waste our time searching for something identical to it there.
Izik Eidus31dbd012009-09-21 17:02:03 -07001179 */
1180 checksum = calc_checksum(page);
1181 if (rmap_item->oldchecksum != checksum) {
1182 rmap_item->oldchecksum = checksum;
1183 return;
1184 }
1185
Hugh Dickins8dd35572009-12-14 17:59:18 -08001186 tree_rmap_item =
1187 unstable_tree_search_insert(rmap_item, page, &tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001188 if (tree_rmap_item) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001189 kpage = try_to_merge_two_pages(rmap_item, page,
1190 tree_rmap_item, tree_page);
1191 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001192 /*
1193 * As soon as we merge this page, we want to remove the
1194 * rmap_item of the page we have merged with from the unstable
1195 * tree, and insert it instead as new node in the stable tree.
1196 */
Hugh Dickins8dd35572009-12-14 17:59:18 -08001197 if (kpage) {
Hugh Dickins93d17712009-12-14 17:59:16 -08001198 remove_rmap_item_from_tree(tree_rmap_item);
Hugh Dickins473b0ce2009-09-21 17:02:11 -07001199
Hugh Dickins5ad64682009-12-14 17:59:24 -08001200 lock_page(kpage);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001201 stable_node = stable_tree_insert(kpage);
1202 if (stable_node) {
1203 stable_tree_append(tree_rmap_item, stable_node);
1204 stable_tree_append(rmap_item, stable_node);
1205 }
Hugh Dickins5ad64682009-12-14 17:59:24 -08001206 unlock_page(kpage);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001207
Izik Eidus31dbd012009-09-21 17:02:03 -07001208 /*
1209 * If we fail to insert the page into the stable tree,
1210 * we will have 2 virtual addresses that are pointing
1211 * to a ksm page left outside the stable tree,
1212 * in which case we need to break_cow on both.
1213 */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001214 if (!stable_node) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001215 break_cow(tree_rmap_item);
1216 break_cow(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001217 }
1218 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001219 }
1220}
1221
1222static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
Hugh Dickins6514d512009-12-14 17:59:19 -08001223 struct rmap_item **rmap_list,
Izik Eidus31dbd012009-09-21 17:02:03 -07001224 unsigned long addr)
1225{
1226 struct rmap_item *rmap_item;
1227
Hugh Dickins6514d512009-12-14 17:59:19 -08001228 while (*rmap_list) {
1229 rmap_item = *rmap_list;
Hugh Dickins93d17712009-12-14 17:59:16 -08001230 if ((rmap_item->address & PAGE_MASK) == addr)
Izik Eidus31dbd012009-09-21 17:02:03 -07001231 return rmap_item;
Izik Eidus31dbd012009-09-21 17:02:03 -07001232 if (rmap_item->address > addr)
1233 break;
Hugh Dickins6514d512009-12-14 17:59:19 -08001234 *rmap_list = rmap_item->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -07001235 remove_rmap_item_from_tree(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001236 free_rmap_item(rmap_item);
1237 }
1238
1239 rmap_item = alloc_rmap_item();
1240 if (rmap_item) {
1241 /* It has already been zeroed */
1242 rmap_item->mm = mm_slot->mm;
1243 rmap_item->address = addr;
Hugh Dickins6514d512009-12-14 17:59:19 -08001244 rmap_item->rmap_list = *rmap_list;
1245 *rmap_list = rmap_item;
Izik Eidus31dbd012009-09-21 17:02:03 -07001246 }
1247 return rmap_item;
1248}
1249
1250static struct rmap_item *scan_get_next_rmap_item(struct page **page)
1251{
1252 struct mm_struct *mm;
1253 struct mm_slot *slot;
1254 struct vm_area_struct *vma;
1255 struct rmap_item *rmap_item;
1256
1257 if (list_empty(&ksm_mm_head.mm_list))
1258 return NULL;
1259
1260 slot = ksm_scan.mm_slot;
1261 if (slot == &ksm_mm_head) {
1262 root_unstable_tree = RB_ROOT;
1263
1264 spin_lock(&ksm_mmlist_lock);
1265 slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
1266 ksm_scan.mm_slot = slot;
1267 spin_unlock(&ksm_mmlist_lock);
1268next_mm:
1269 ksm_scan.address = 0;
Hugh Dickins6514d512009-12-14 17:59:19 -08001270 ksm_scan.rmap_list = &slot->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -07001271 }
1272
1273 mm = slot->mm;
1274 down_read(&mm->mmap_sem);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001275 if (ksm_test_exit(mm))
1276 vma = NULL;
1277 else
1278 vma = find_vma(mm, ksm_scan.address);
1279
1280 for (; vma; vma = vma->vm_next) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001281 if (!(vma->vm_flags & VM_MERGEABLE))
1282 continue;
1283 if (ksm_scan.address < vma->vm_start)
1284 ksm_scan.address = vma->vm_start;
1285 if (!vma->anon_vma)
1286 ksm_scan.address = vma->vm_end;
1287
1288 while (ksm_scan.address < vma->vm_end) {
Hugh Dickins9ba69292009-09-21 17:02:20 -07001289 if (ksm_test_exit(mm))
1290 break;
Izik Eidus31dbd012009-09-21 17:02:03 -07001291 *page = follow_page(vma, ksm_scan.address, FOLL_GET);
Dan Carpenter22eccdd2010-04-23 13:18:10 -04001292 if (!IS_ERR_OR_NULL(*page) && PageAnon(*page)) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001293 flush_anon_page(vma, *page, ksm_scan.address);
1294 flush_dcache_page(*page);
1295 rmap_item = get_next_rmap_item(slot,
Hugh Dickins6514d512009-12-14 17:59:19 -08001296 ksm_scan.rmap_list, ksm_scan.address);
Izik Eidus31dbd012009-09-21 17:02:03 -07001297 if (rmap_item) {
Hugh Dickins6514d512009-12-14 17:59:19 -08001298 ksm_scan.rmap_list =
1299 &rmap_item->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -07001300 ksm_scan.address += PAGE_SIZE;
1301 } else
1302 put_page(*page);
1303 up_read(&mm->mmap_sem);
1304 return rmap_item;
1305 }
Dan Carpenter22eccdd2010-04-23 13:18:10 -04001306 if (!IS_ERR_OR_NULL(*page))
Izik Eidus31dbd012009-09-21 17:02:03 -07001307 put_page(*page);
1308 ksm_scan.address += PAGE_SIZE;
1309 cond_resched();
1310 }
1311 }
1312
Hugh Dickins9ba69292009-09-21 17:02:20 -07001313 if (ksm_test_exit(mm)) {
1314 ksm_scan.address = 0;
Hugh Dickins6514d512009-12-14 17:59:19 -08001315 ksm_scan.rmap_list = &slot->rmap_list;
Hugh Dickins9ba69292009-09-21 17:02:20 -07001316 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001317 /*
1318 * Nuke all the rmap_items that are above this current rmap:
1319 * because there were no VM_MERGEABLE vmas with such addresses.
1320 */
Hugh Dickins6514d512009-12-14 17:59:19 -08001321 remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
Izik Eidus31dbd012009-09-21 17:02:03 -07001322
1323 spin_lock(&ksm_mmlist_lock);
Hugh Dickinscd551f92009-09-21 17:02:17 -07001324 ksm_scan.mm_slot = list_entry(slot->mm_list.next,
1325 struct mm_slot, mm_list);
1326 if (ksm_scan.address == 0) {
1327 /*
1328 * We've completed a full scan of all vmas, holding mmap_sem
1329 * throughout, and found no VM_MERGEABLE: so do the same as
1330 * __ksm_exit does to remove this mm from all our lists now.
Hugh Dickins9ba69292009-09-21 17:02:20 -07001331 * This applies either when cleaning up after __ksm_exit
1332 * (but beware: we can reach here even before __ksm_exit),
1333 * or when all VM_MERGEABLE areas have been unmapped (and
1334 * mmap_sem then protects against race with MADV_MERGEABLE).
Hugh Dickinscd551f92009-09-21 17:02:17 -07001335 */
1336 hlist_del(&slot->link);
1337 list_del(&slot->mm_list);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001338 spin_unlock(&ksm_mmlist_lock);
1339
Hugh Dickinscd551f92009-09-21 17:02:17 -07001340 free_mm_slot(slot);
1341 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001342 up_read(&mm->mmap_sem);
1343 mmdrop(mm);
1344 } else {
1345 spin_unlock(&ksm_mmlist_lock);
1346 up_read(&mm->mmap_sem);
Hugh Dickinscd551f92009-09-21 17:02:17 -07001347 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001348
1349 /* Repeat until we've completed scanning the whole list */
Hugh Dickinscd551f92009-09-21 17:02:17 -07001350 slot = ksm_scan.mm_slot;
Izik Eidus31dbd012009-09-21 17:02:03 -07001351 if (slot != &ksm_mm_head)
1352 goto next_mm;
1353
Izik Eidus31dbd012009-09-21 17:02:03 -07001354 ksm_scan.seqnr++;
1355 return NULL;
1356}
1357
1358/**
1359 * ksm_do_scan - the ksm scanner main worker function.
1360 * @scan_npages - number of pages we want to scan before we return.
1361 */
1362static void ksm_do_scan(unsigned int scan_npages)
1363{
1364 struct rmap_item *rmap_item;
Dan Carpenter22eccdd2010-04-23 13:18:10 -04001365 struct page *uninitialized_var(page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001366
1367 while (scan_npages--) {
1368 cond_resched();
1369 rmap_item = scan_get_next_rmap_item(&page);
1370 if (!rmap_item)
1371 return;
1372 if (!PageKsm(page) || !in_stable_tree(rmap_item))
1373 cmp_and_merge_page(page, rmap_item);
1374 put_page(page);
1375 }
1376}
1377
Hugh Dickins6e1583842009-09-21 17:02:14 -07001378static int ksmd_should_run(void)
1379{
1380 return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
1381}
1382
Izik Eidus31dbd012009-09-21 17:02:03 -07001383static int ksm_scan_thread(void *nothing)
1384{
Izik Eidus339aa622009-09-21 17:02:07 -07001385 set_user_nice(current, 5);
Izik Eidus31dbd012009-09-21 17:02:03 -07001386
1387 while (!kthread_should_stop()) {
Hugh Dickins6e1583842009-09-21 17:02:14 -07001388 mutex_lock(&ksm_thread_mutex);
1389 if (ksmd_should_run())
Izik Eidus31dbd012009-09-21 17:02:03 -07001390 ksm_do_scan(ksm_thread_pages_to_scan);
Hugh Dickins6e1583842009-09-21 17:02:14 -07001391 mutex_unlock(&ksm_thread_mutex);
1392
1393 if (ksmd_should_run()) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001394 schedule_timeout_interruptible(
1395 msecs_to_jiffies(ksm_thread_sleep_millisecs));
1396 } else {
1397 wait_event_interruptible(ksm_thread_wait,
Hugh Dickins6e1583842009-09-21 17:02:14 -07001398 ksmd_should_run() || kthread_should_stop());
Izik Eidus31dbd012009-09-21 17:02:03 -07001399 }
1400 }
1401 return 0;
1402}
1403
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001404int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
1405 unsigned long end, int advice, unsigned long *vm_flags)
1406{
1407 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinsd952b792009-09-21 17:02:16 -07001408 int err;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001409
1410 switch (advice) {
1411 case MADV_MERGEABLE:
1412 /*
1413 * Be somewhat over-protective for now!
1414 */
1415 if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
1416 VM_PFNMAP | VM_IO | VM_DONTEXPAND |
1417 VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
Hugh Dickins5ad64682009-12-14 17:59:24 -08001418 VM_NONLINEAR | VM_MIXEDMAP | VM_SAO))
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001419 return 0; /* just ignore the advice */
1420
Hugh Dickinsd952b792009-09-21 17:02:16 -07001421 if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
1422 err = __ksm_enter(mm);
1423 if (err)
1424 return err;
1425 }
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001426
1427 *vm_flags |= VM_MERGEABLE;
1428 break;
1429
1430 case MADV_UNMERGEABLE:
1431 if (!(*vm_flags & VM_MERGEABLE))
1432 return 0; /* just ignore the advice */
1433
Hugh Dickinsd952b792009-09-21 17:02:16 -07001434 if (vma->anon_vma) {
1435 err = unmerge_ksm_pages(vma, start, end);
1436 if (err)
1437 return err;
1438 }
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001439
1440 *vm_flags &= ~VM_MERGEABLE;
1441 break;
1442 }
1443
1444 return 0;
1445}
1446
1447int __ksm_enter(struct mm_struct *mm)
1448{
Hugh Dickins6e1583842009-09-21 17:02:14 -07001449 struct mm_slot *mm_slot;
1450 int needs_wakeup;
1451
1452 mm_slot = alloc_mm_slot();
Izik Eidus31dbd012009-09-21 17:02:03 -07001453 if (!mm_slot)
1454 return -ENOMEM;
1455
Hugh Dickins6e1583842009-09-21 17:02:14 -07001456 /* Check ksm_run too? Would need tighter locking */
1457 needs_wakeup = list_empty(&ksm_mm_head.mm_list);
1458
Izik Eidus31dbd012009-09-21 17:02:03 -07001459 spin_lock(&ksm_mmlist_lock);
1460 insert_to_mm_slots_hash(mm, mm_slot);
1461 /*
1462 * Insert just behind the scanning cursor, to let the area settle
1463 * down a little; when fork is followed by immediate exec, we don't
1464 * want ksmd to waste time setting up and tearing down an rmap_list.
1465 */
1466 list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
1467 spin_unlock(&ksm_mmlist_lock);
1468
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001469 set_bit(MMF_VM_MERGEABLE, &mm->flags);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001470 atomic_inc(&mm->mm_count);
Hugh Dickins6e1583842009-09-21 17:02:14 -07001471
1472 if (needs_wakeup)
1473 wake_up_interruptible(&ksm_thread_wait);
1474
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001475 return 0;
1476}
1477
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -07001478void __ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001479{
Hugh Dickinscd551f92009-09-21 17:02:17 -07001480 struct mm_slot *mm_slot;
Hugh Dickins9ba69292009-09-21 17:02:20 -07001481 int easy_to_free = 0;
Hugh Dickinscd551f92009-09-21 17:02:17 -07001482
Izik Eidus31dbd012009-09-21 17:02:03 -07001483 /*
Hugh Dickins9ba69292009-09-21 17:02:20 -07001484 * This process is exiting: if it's straightforward (as is the
1485 * case when ksmd was never running), free mm_slot immediately.
1486 * But if it's at the cursor or has rmap_items linked to it, use
1487 * mmap_sem to synchronize with any break_cows before pagetables
1488 * are freed, and leave the mm_slot on the list for ksmd to free.
1489 * Beware: ksm may already have noticed it exiting and freed the slot.
Izik Eidus31dbd012009-09-21 17:02:03 -07001490 */
Hugh Dickins9ba69292009-09-21 17:02:20 -07001491
Hugh Dickinscd551f92009-09-21 17:02:17 -07001492 spin_lock(&ksm_mmlist_lock);
1493 mm_slot = get_mm_slot(mm);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001494 if (mm_slot && ksm_scan.mm_slot != mm_slot) {
Hugh Dickins6514d512009-12-14 17:59:19 -08001495 if (!mm_slot->rmap_list) {
Hugh Dickins9ba69292009-09-21 17:02:20 -07001496 hlist_del(&mm_slot->link);
1497 list_del(&mm_slot->mm_list);
1498 easy_to_free = 1;
1499 } else {
1500 list_move(&mm_slot->mm_list,
1501 &ksm_scan.mm_slot->mm_list);
1502 }
Hugh Dickinscd551f92009-09-21 17:02:17 -07001503 }
Hugh Dickinscd551f92009-09-21 17:02:17 -07001504 spin_unlock(&ksm_mmlist_lock);
1505
Hugh Dickins9ba69292009-09-21 17:02:20 -07001506 if (easy_to_free) {
1507 free_mm_slot(mm_slot);
1508 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
1509 mmdrop(mm);
1510 } else if (mm_slot) {
Hugh Dickins9ba69292009-09-21 17:02:20 -07001511 down_write(&mm->mmap_sem);
1512 up_write(&mm->mmap_sem);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001513 }
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001514}
Izik Eidus31dbd012009-09-21 17:02:03 -07001515
Hugh Dickins5ad64682009-12-14 17:59:24 -08001516struct page *ksm_does_need_to_copy(struct page *page,
1517 struct vm_area_struct *vma, unsigned long address)
1518{
1519 struct page *new_page;
1520
1521 unlock_page(page); /* any racers will COW it, not modify it */
1522
1523 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
1524 if (new_page) {
1525 copy_user_highpage(new_page, page, address, vma);
1526
1527 SetPageDirty(new_page);
1528 __SetPageUptodate(new_page);
1529 SetPageSwapBacked(new_page);
1530 __set_page_locked(new_page);
1531
1532 if (page_evictable(new_page, vma))
1533 lru_cache_add_lru(new_page, LRU_ACTIVE_ANON);
1534 else
1535 add_page_to_unevictable_list(new_page);
1536 }
1537
1538 page_cache_release(page);
1539 return new_page;
1540}
1541
1542int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg,
1543 unsigned long *vm_flags)
1544{
1545 struct stable_node *stable_node;
1546 struct rmap_item *rmap_item;
1547 struct hlist_node *hlist;
1548 unsigned int mapcount = page_mapcount(page);
1549 int referenced = 0;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001550 int search_new_forks = 0;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001551
1552 VM_BUG_ON(!PageKsm(page));
1553 VM_BUG_ON(!PageLocked(page));
1554
1555 stable_node = page_stable_node(page);
1556 if (!stable_node)
1557 return 0;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001558again:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001559 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001560 struct anon_vma *anon_vma = rmap_item->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08001561 struct anon_vma_chain *vmac;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001562 struct vm_area_struct *vma;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001563
Rik van Rielcba48b92010-08-09 17:18:38 -07001564 anon_vma_lock(anon_vma);
Rik van Riel5beb4932010-03-05 13:42:07 -08001565 list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
1566 vma = vmac->vma;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001567 if (rmap_item->address < vma->vm_start ||
1568 rmap_item->address >= vma->vm_end)
1569 continue;
1570 /*
1571 * Initially we examine only the vma which covers this
1572 * rmap_item; but later, if there is still work to do,
1573 * we examine covering vmas in other mms: in case they
1574 * were forked from the original since ksmd passed.
1575 */
1576 if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1577 continue;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001578
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001579 if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
1580 continue;
1581
1582 referenced += page_referenced_one(page, vma,
Hugh Dickins5ad64682009-12-14 17:59:24 -08001583 rmap_item->address, &mapcount, vm_flags);
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001584 if (!search_new_forks || !mapcount)
1585 break;
1586 }
Rik van Rielcba48b92010-08-09 17:18:38 -07001587 anon_vma_unlock(anon_vma);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001588 if (!mapcount)
1589 goto out;
1590 }
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001591 if (!search_new_forks++)
1592 goto again;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001593out:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001594 return referenced;
1595}
1596
1597int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
1598{
1599 struct stable_node *stable_node;
1600 struct hlist_node *hlist;
1601 struct rmap_item *rmap_item;
1602 int ret = SWAP_AGAIN;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001603 int search_new_forks = 0;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001604
1605 VM_BUG_ON(!PageKsm(page));
1606 VM_BUG_ON(!PageLocked(page));
1607
1608 stable_node = page_stable_node(page);
1609 if (!stable_node)
1610 return SWAP_FAIL;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001611again:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001612 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001613 struct anon_vma *anon_vma = rmap_item->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08001614 struct anon_vma_chain *vmac;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001615 struct vm_area_struct *vma;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001616
Rik van Rielcba48b92010-08-09 17:18:38 -07001617 anon_vma_lock(anon_vma);
Rik van Riel5beb4932010-03-05 13:42:07 -08001618 list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
1619 vma = vmac->vma;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001620 if (rmap_item->address < vma->vm_start ||
1621 rmap_item->address >= vma->vm_end)
1622 continue;
1623 /*
1624 * Initially we examine only the vma which covers this
1625 * rmap_item; but later, if there is still work to do,
1626 * we examine covering vmas in other mms: in case they
1627 * were forked from the original since ksmd passed.
1628 */
1629 if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1630 continue;
1631
1632 ret = try_to_unmap_one(page, vma,
1633 rmap_item->address, flags);
1634 if (ret != SWAP_AGAIN || !page_mapped(page)) {
Rik van Rielcba48b92010-08-09 17:18:38 -07001635 anon_vma_unlock(anon_vma);
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001636 goto out;
1637 }
1638 }
Rik van Rielcba48b92010-08-09 17:18:38 -07001639 anon_vma_unlock(anon_vma);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001640 }
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001641 if (!search_new_forks++)
1642 goto again;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001643out:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001644 return ret;
1645}
1646
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001647#ifdef CONFIG_MIGRATION
1648int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
1649 struct vm_area_struct *, unsigned long, void *), void *arg)
1650{
1651 struct stable_node *stable_node;
1652 struct hlist_node *hlist;
1653 struct rmap_item *rmap_item;
1654 int ret = SWAP_AGAIN;
1655 int search_new_forks = 0;
1656
1657 VM_BUG_ON(!PageKsm(page));
1658 VM_BUG_ON(!PageLocked(page));
1659
1660 stable_node = page_stable_node(page);
1661 if (!stable_node)
1662 return ret;
1663again:
1664 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1665 struct anon_vma *anon_vma = rmap_item->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08001666 struct anon_vma_chain *vmac;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001667 struct vm_area_struct *vma;
1668
Rik van Rielcba48b92010-08-09 17:18:38 -07001669 anon_vma_lock(anon_vma);
Rik van Riel5beb4932010-03-05 13:42:07 -08001670 list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
1671 vma = vmac->vma;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001672 if (rmap_item->address < vma->vm_start ||
1673 rmap_item->address >= vma->vm_end)
1674 continue;
1675 /*
1676 * Initially we examine only the vma which covers this
1677 * rmap_item; but later, if there is still work to do,
1678 * we examine covering vmas in other mms: in case they
1679 * were forked from the original since ksmd passed.
1680 */
1681 if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1682 continue;
1683
1684 ret = rmap_one(page, vma, rmap_item->address, arg);
1685 if (ret != SWAP_AGAIN) {
Rik van Rielcba48b92010-08-09 17:18:38 -07001686 anon_vma_unlock(anon_vma);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001687 goto out;
1688 }
1689 }
Rik van Rielcba48b92010-08-09 17:18:38 -07001690 anon_vma_unlock(anon_vma);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001691 }
1692 if (!search_new_forks++)
1693 goto again;
1694out:
1695 return ret;
1696}
1697
1698void ksm_migrate_page(struct page *newpage, struct page *oldpage)
1699{
1700 struct stable_node *stable_node;
1701
1702 VM_BUG_ON(!PageLocked(oldpage));
1703 VM_BUG_ON(!PageLocked(newpage));
1704 VM_BUG_ON(newpage->mapping != oldpage->mapping);
1705
1706 stable_node = page_stable_node(newpage);
1707 if (stable_node) {
Hugh Dickins62b61f62009-12-14 17:59:33 -08001708 VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage));
1709 stable_node->kpfn = page_to_pfn(newpage);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001710 }
1711}
1712#endif /* CONFIG_MIGRATION */
1713
Hugh Dickins62b61f62009-12-14 17:59:33 -08001714#ifdef CONFIG_MEMORY_HOTREMOVE
1715static struct stable_node *ksm_check_stable_tree(unsigned long start_pfn,
1716 unsigned long end_pfn)
1717{
1718 struct rb_node *node;
1719
1720 for (node = rb_first(&root_stable_tree); node; node = rb_next(node)) {
1721 struct stable_node *stable_node;
1722
1723 stable_node = rb_entry(node, struct stable_node, node);
1724 if (stable_node->kpfn >= start_pfn &&
1725 stable_node->kpfn < end_pfn)
1726 return stable_node;
1727 }
1728 return NULL;
1729}
1730
1731static int ksm_memory_callback(struct notifier_block *self,
1732 unsigned long action, void *arg)
1733{
1734 struct memory_notify *mn = arg;
1735 struct stable_node *stable_node;
1736
1737 switch (action) {
1738 case MEM_GOING_OFFLINE:
1739 /*
1740 * Keep it very simple for now: just lock out ksmd and
1741 * MADV_UNMERGEABLE while any memory is going offline.
1742 */
1743 mutex_lock(&ksm_thread_mutex);
1744 break;
1745
1746 case MEM_OFFLINE:
1747 /*
1748 * Most of the work is done by page migration; but there might
1749 * be a few stable_nodes left over, still pointing to struct
1750 * pages which have been offlined: prune those from the tree.
1751 */
1752 while ((stable_node = ksm_check_stable_tree(mn->start_pfn,
1753 mn->start_pfn + mn->nr_pages)) != NULL)
1754 remove_node_from_stable_tree(stable_node);
1755 /* fallthrough */
1756
1757 case MEM_CANCEL_OFFLINE:
1758 mutex_unlock(&ksm_thread_mutex);
1759 break;
1760 }
1761 return NOTIFY_OK;
1762}
1763#endif /* CONFIG_MEMORY_HOTREMOVE */
1764
Hugh Dickins2ffd8672009-09-21 17:02:23 -07001765#ifdef CONFIG_SYSFS
1766/*
1767 * This all compiles without CONFIG_SYSFS, but is a waste of space.
1768 */
1769
Izik Eidus31dbd012009-09-21 17:02:03 -07001770#define KSM_ATTR_RO(_name) \
1771 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1772#define KSM_ATTR(_name) \
1773 static struct kobj_attribute _name##_attr = \
1774 __ATTR(_name, 0644, _name##_show, _name##_store)
1775
1776static ssize_t sleep_millisecs_show(struct kobject *kobj,
1777 struct kobj_attribute *attr, char *buf)
1778{
1779 return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
1780}
1781
1782static ssize_t sleep_millisecs_store(struct kobject *kobj,
1783 struct kobj_attribute *attr,
1784 const char *buf, size_t count)
1785{
1786 unsigned long msecs;
1787 int err;
1788
1789 err = strict_strtoul(buf, 10, &msecs);
1790 if (err || msecs > UINT_MAX)
1791 return -EINVAL;
1792
1793 ksm_thread_sleep_millisecs = msecs;
1794
1795 return count;
1796}
1797KSM_ATTR(sleep_millisecs);
1798
1799static ssize_t pages_to_scan_show(struct kobject *kobj,
1800 struct kobj_attribute *attr, char *buf)
1801{
1802 return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
1803}
1804
1805static ssize_t pages_to_scan_store(struct kobject *kobj,
1806 struct kobj_attribute *attr,
1807 const char *buf, size_t count)
1808{
1809 int err;
1810 unsigned long nr_pages;
1811
1812 err = strict_strtoul(buf, 10, &nr_pages);
1813 if (err || nr_pages > UINT_MAX)
1814 return -EINVAL;
1815
1816 ksm_thread_pages_to_scan = nr_pages;
1817
1818 return count;
1819}
1820KSM_ATTR(pages_to_scan);
1821
1822static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
1823 char *buf)
1824{
1825 return sprintf(buf, "%u\n", ksm_run);
1826}
1827
1828static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
1829 const char *buf, size_t count)
1830{
1831 int err;
1832 unsigned long flags;
1833
1834 err = strict_strtoul(buf, 10, &flags);
1835 if (err || flags > UINT_MAX)
1836 return -EINVAL;
1837 if (flags > KSM_RUN_UNMERGE)
1838 return -EINVAL;
1839
1840 /*
1841 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
1842 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
Hugh Dickinsd0f209f2009-12-14 17:59:34 -08001843 * breaking COW to free the pages_shared (but leaves mm_slots
1844 * on the list for when ksmd may be set running again).
Izik Eidus31dbd012009-09-21 17:02:03 -07001845 */
1846
1847 mutex_lock(&ksm_thread_mutex);
1848 if (ksm_run != flags) {
1849 ksm_run = flags;
Hugh Dickinsd952b792009-09-21 17:02:16 -07001850 if (flags & KSM_RUN_UNMERGE) {
Hugh Dickins35451be2009-09-21 17:02:27 -07001851 current->flags |= PF_OOM_ORIGIN;
Hugh Dickinsd952b792009-09-21 17:02:16 -07001852 err = unmerge_and_remove_all_rmap_items();
Hugh Dickins35451be2009-09-21 17:02:27 -07001853 current->flags &= ~PF_OOM_ORIGIN;
Hugh Dickinsd952b792009-09-21 17:02:16 -07001854 if (err) {
1855 ksm_run = KSM_RUN_STOP;
1856 count = err;
1857 }
1858 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001859 }
1860 mutex_unlock(&ksm_thread_mutex);
1861
1862 if (flags & KSM_RUN_MERGE)
1863 wake_up_interruptible(&ksm_thread_wait);
1864
1865 return count;
1866}
1867KSM_ATTR(run);
1868
Hugh Dickinsb4028262009-09-21 17:02:09 -07001869static ssize_t pages_shared_show(struct kobject *kobj,
1870 struct kobj_attribute *attr, char *buf)
1871{
1872 return sprintf(buf, "%lu\n", ksm_pages_shared);
1873}
1874KSM_ATTR_RO(pages_shared);
1875
1876static ssize_t pages_sharing_show(struct kobject *kobj,
1877 struct kobj_attribute *attr, char *buf)
1878{
Hugh Dickinse178dfd2009-09-21 17:02:10 -07001879 return sprintf(buf, "%lu\n", ksm_pages_sharing);
Hugh Dickinsb4028262009-09-21 17:02:09 -07001880}
1881KSM_ATTR_RO(pages_sharing);
1882
Hugh Dickins473b0ce2009-09-21 17:02:11 -07001883static ssize_t pages_unshared_show(struct kobject *kobj,
1884 struct kobj_attribute *attr, char *buf)
1885{
1886 return sprintf(buf, "%lu\n", ksm_pages_unshared);
1887}
1888KSM_ATTR_RO(pages_unshared);
1889
1890static ssize_t pages_volatile_show(struct kobject *kobj,
1891 struct kobj_attribute *attr, char *buf)
1892{
1893 long ksm_pages_volatile;
1894
1895 ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
1896 - ksm_pages_sharing - ksm_pages_unshared;
1897 /*
1898 * It was not worth any locking to calculate that statistic,
1899 * but it might therefore sometimes be negative: conceal that.
1900 */
1901 if (ksm_pages_volatile < 0)
1902 ksm_pages_volatile = 0;
1903 return sprintf(buf, "%ld\n", ksm_pages_volatile);
1904}
1905KSM_ATTR_RO(pages_volatile);
1906
1907static ssize_t full_scans_show(struct kobject *kobj,
1908 struct kobj_attribute *attr, char *buf)
1909{
1910 return sprintf(buf, "%lu\n", ksm_scan.seqnr);
1911}
1912KSM_ATTR_RO(full_scans);
1913
Izik Eidus31dbd012009-09-21 17:02:03 -07001914static struct attribute *ksm_attrs[] = {
1915 &sleep_millisecs_attr.attr,
1916 &pages_to_scan_attr.attr,
1917 &run_attr.attr,
Hugh Dickinsb4028262009-09-21 17:02:09 -07001918 &pages_shared_attr.attr,
1919 &pages_sharing_attr.attr,
Hugh Dickins473b0ce2009-09-21 17:02:11 -07001920 &pages_unshared_attr.attr,
1921 &pages_volatile_attr.attr,
1922 &full_scans_attr.attr,
Izik Eidus31dbd012009-09-21 17:02:03 -07001923 NULL,
1924};
1925
1926static struct attribute_group ksm_attr_group = {
1927 .attrs = ksm_attrs,
1928 .name = "ksm",
1929};
Hugh Dickins2ffd8672009-09-21 17:02:23 -07001930#endif /* CONFIG_SYSFS */
Izik Eidus31dbd012009-09-21 17:02:03 -07001931
1932static int __init ksm_init(void)
1933{
1934 struct task_struct *ksm_thread;
1935 int err;
1936
1937 err = ksm_slab_init();
1938 if (err)
1939 goto out;
1940
1941 err = mm_slots_hash_init();
1942 if (err)
1943 goto out_free1;
1944
1945 ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
1946 if (IS_ERR(ksm_thread)) {
1947 printk(KERN_ERR "ksm: creating kthread failed\n");
1948 err = PTR_ERR(ksm_thread);
1949 goto out_free2;
1950 }
1951
Hugh Dickins2ffd8672009-09-21 17:02:23 -07001952#ifdef CONFIG_SYSFS
Izik Eidus31dbd012009-09-21 17:02:03 -07001953 err = sysfs_create_group(mm_kobj, &ksm_attr_group);
1954 if (err) {
1955 printk(KERN_ERR "ksm: register sysfs failed\n");
Hugh Dickins2ffd8672009-09-21 17:02:23 -07001956 kthread_stop(ksm_thread);
1957 goto out_free2;
Izik Eidus31dbd012009-09-21 17:02:03 -07001958 }
Hugh Dickinsc73602a2009-10-07 16:32:22 -07001959#else
1960 ksm_run = KSM_RUN_MERGE; /* no way for user to start it */
1961
Hugh Dickins2ffd8672009-09-21 17:02:23 -07001962#endif /* CONFIG_SYSFS */
Izik Eidus31dbd012009-09-21 17:02:03 -07001963
Hugh Dickins62b61f62009-12-14 17:59:33 -08001964#ifdef CONFIG_MEMORY_HOTREMOVE
1965 /*
1966 * Choose a high priority since the callback takes ksm_thread_mutex:
1967 * later callbacks could only be taking locks which nest within that.
1968 */
1969 hotplug_memory_notifier(ksm_memory_callback, 100);
1970#endif
Izik Eidus31dbd012009-09-21 17:02:03 -07001971 return 0;
1972
Izik Eidus31dbd012009-09-21 17:02:03 -07001973out_free2:
1974 mm_slots_hash_free();
1975out_free1:
1976 ksm_slab_free();
1977out:
1978 return err;
1979}
1980module_init(ksm_init)