blob: e02430fb26f6b9e2ae1e372bcc97fa15bcf43ee4 [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>
Sasha Levin4ca3a692013-02-22 16:32:28 -080036#include <linux/hashtable.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080037#include <linux/freezer.h>
David Rientjes72788c32011-05-24 17:11:40 -070038#include <linux/oom.h>
Petr Holasek90bd6fd2013-02-22 16:35:00 -080039#include <linux/numa.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070040
Izik Eidus31dbd012009-09-21 17:02:03 -070041#include <asm/tlbflush.h>
Hugh Dickins73848b42009-12-14 17:59:22 -080042#include "internal.h"
Izik Eidus31dbd012009-09-21 17:02:03 -070043
Hugh Dickinse850dcf2013-02-22 16:35:03 -080044#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
Izik Eidus31dbd012009-09-21 17:02:03 -070052/*
53 * A few notes about the KSM scanning process,
54 * to make it easier to understand the data structures below:
55 *
56 * In order to reduce excessive scanning, KSM sorts the memory pages by their
57 * contents into a data structure that holds pointers to the pages' locations.
58 *
59 * Since the contents of the pages may change at any moment, KSM cannot just
60 * insert the pages into a normal sorted tree and expect it to find anything.
61 * Therefore KSM uses two data structures - the stable and the unstable tree.
62 *
63 * The stable tree holds pointers to all the merged pages (ksm pages), sorted
64 * by their contents. Because each such page is write-protected, searching on
65 * this tree is fully assured to be working (except when pages are unmapped),
66 * and therefore this tree is called the stable tree.
67 *
68 * In addition to the stable tree, KSM uses a second data structure called the
69 * unstable tree: this tree holds pointers to pages which have been found to
70 * be "unchanged for a period of time". The unstable tree sorts these pages
71 * by their contents, but since they are not write-protected, KSM cannot rely
72 * upon the unstable tree to work correctly - the unstable tree is liable to
73 * be corrupted as its contents are modified, and so it is called unstable.
74 *
75 * KSM solves this problem by several techniques:
76 *
77 * 1) The unstable tree is flushed every time KSM completes scanning all
78 * memory areas, and then the tree is rebuilt again from the beginning.
79 * 2) KSM will only insert into the unstable tree, pages whose hash value
80 * has not changed since the previous scan of all memory areas.
81 * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
82 * colors of the nodes and not on their contents, assuring that even when
83 * the tree gets "corrupted" it won't get out of balance, so scanning time
84 * remains the same (also, searching and inserting nodes in an rbtree uses
85 * the same algorithm, so we have no overhead when we flush and rebuild).
86 * 4) KSM never flushes the stable tree, which means that even if it were to
87 * take 10 attempts to find a page in the unstable tree, once it is found,
88 * it is secured in the stable tree. (When we scan a new page, we first
89 * compare it against the stable tree, and then against the unstable tree.)
90 */
91
92/**
93 * struct mm_slot - ksm information per mm that is being scanned
94 * @link: link to the mm_slots hash list
95 * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
Hugh Dickins6514d512009-12-14 17:59:19 -080096 * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
Izik Eidus31dbd012009-09-21 17:02:03 -070097 * @mm: the mm that this information is valid for
98 */
99struct mm_slot {
100 struct hlist_node link;
101 struct list_head mm_list;
Hugh Dickins6514d512009-12-14 17:59:19 -0800102 struct rmap_item *rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -0700103 struct mm_struct *mm;
104};
105
106/**
107 * struct ksm_scan - cursor for scanning
108 * @mm_slot: the current mm_slot we are scanning
109 * @address: the next address inside that to be scanned
Hugh Dickins6514d512009-12-14 17:59:19 -0800110 * @rmap_list: link to the next rmap to be scanned in the rmap_list
Izik Eidus31dbd012009-09-21 17:02:03 -0700111 * @seqnr: count of completed full scans (needed when removing unstable node)
112 *
113 * There is only the one ksm_scan instance of this cursor structure.
114 */
115struct ksm_scan {
116 struct mm_slot *mm_slot;
117 unsigned long address;
Hugh Dickins6514d512009-12-14 17:59:19 -0800118 struct rmap_item **rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -0700119 unsigned long seqnr;
120};
121
122/**
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800123 * struct stable_node - node of the stable rbtree
124 * @node: rb node of this ksm page in the stable tree
125 * @hlist: hlist head of rmap_items using this ksm page
Hugh Dickins62b61f62009-12-14 17:59:33 -0800126 * @kpfn: page frame number of this ksm page
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800127 */
128struct stable_node {
129 struct rb_node node;
130 struct hlist_head hlist;
Hugh Dickins62b61f62009-12-14 17:59:33 -0800131 unsigned long kpfn;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800132};
133
134/**
Izik Eidus31dbd012009-09-21 17:02:03 -0700135 * struct rmap_item - reverse mapping item for virtual addresses
Hugh Dickins6514d512009-12-14 17:59:19 -0800136 * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800137 * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
Izik Eidus31dbd012009-09-21 17:02:03 -0700138 * @mm: the memory structure this rmap_item is pointing into
139 * @address: the virtual address this rmap_item tracks (+ flags in low bits)
140 * @oldchecksum: previous checksum of the page at that virtual address
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800141 * @nid: NUMA node id of unstable tree in which linked (may not match page)
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800142 * @node: rb node of this rmap_item in the unstable tree
143 * @head: pointer to stable_node heading this list in the stable tree
144 * @hlist: link into hlist of rmap_items hanging off that stable_node
Izik Eidus31dbd012009-09-21 17:02:03 -0700145 */
146struct rmap_item {
Hugh Dickins6514d512009-12-14 17:59:19 -0800147 struct rmap_item *rmap_list;
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800148 struct anon_vma *anon_vma; /* when stable */
Izik Eidus31dbd012009-09-21 17:02:03 -0700149 struct mm_struct *mm;
150 unsigned long address; /* + low bits used for flags below */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800151 unsigned int oldchecksum; /* when unstable */
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800152#ifdef CONFIG_NUMA
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800153 int nid;
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800154#endif
Izik Eidus31dbd012009-09-21 17:02:03 -0700155 union {
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800156 struct rb_node node; /* when node of unstable tree */
157 struct { /* when listed from stable tree */
158 struct stable_node *head;
159 struct hlist_node hlist;
160 };
Izik Eidus31dbd012009-09-21 17:02:03 -0700161 };
162};
163
164#define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800165#define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */
166#define STABLE_FLAG 0x200 /* is listed from the stable tree */
Izik Eidus31dbd012009-09-21 17:02:03 -0700167
168/* The stable and unstable tree heads */
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800169static struct rb_root root_unstable_tree[MAX_NUMNODES];
170static struct rb_root root_stable_tree[MAX_NUMNODES];
Izik Eidus31dbd012009-09-21 17:02:03 -0700171
Sasha Levin4ca3a692013-02-22 16:32:28 -0800172#define MM_SLOTS_HASH_BITS 10
173static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
Izik Eidus31dbd012009-09-21 17:02:03 -0700174
175static struct mm_slot ksm_mm_head = {
176 .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
177};
178static struct ksm_scan ksm_scan = {
179 .mm_slot = &ksm_mm_head,
180};
181
182static struct kmem_cache *rmap_item_cache;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800183static struct kmem_cache *stable_node_cache;
Izik Eidus31dbd012009-09-21 17:02:03 -0700184static struct kmem_cache *mm_slot_cache;
185
186/* The number of nodes in the stable tree */
Hugh Dickinsb4028262009-09-21 17:02:09 -0700187static unsigned long ksm_pages_shared;
Izik Eidus31dbd012009-09-21 17:02:03 -0700188
Hugh Dickinse178dfd2009-09-21 17:02:10 -0700189/* The number of page slots additionally sharing those nodes */
Hugh Dickinsb4028262009-09-21 17:02:09 -0700190static unsigned long ksm_pages_sharing;
Izik Eidus31dbd012009-09-21 17:02:03 -0700191
Hugh Dickins473b0ce2009-09-21 17:02:11 -0700192/* The number of nodes in the unstable tree */
193static unsigned long ksm_pages_unshared;
194
195/* The number of rmap_items in use: to calculate pages_volatile */
196static unsigned long ksm_rmap_items;
197
Izik Eidus31dbd012009-09-21 17:02:03 -0700198/* Number of pages ksmd should scan in one batch */
Izik Eidus2c6854f2009-09-23 15:56:04 -0700199static unsigned int ksm_thread_pages_to_scan = 100;
Izik Eidus31dbd012009-09-21 17:02:03 -0700200
201/* Milliseconds ksmd should sleep between batches */
Hugh Dickins2ffd8672009-09-21 17:02:23 -0700202static unsigned int ksm_thread_sleep_millisecs = 20;
Izik Eidus31dbd012009-09-21 17:02:03 -0700203
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800204#ifdef CONFIG_NUMA
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800205/* Zeroed when merging across nodes is not allowed */
206static unsigned int ksm_merge_across_nodes = 1;
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800207#else
208#define ksm_merge_across_nodes 1U
209#endif
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800210
Izik Eidus31dbd012009-09-21 17:02:03 -0700211#define KSM_RUN_STOP 0
212#define KSM_RUN_MERGE 1
213#define KSM_RUN_UNMERGE 2
Izik Eidus2c6854f2009-09-23 15:56:04 -0700214static unsigned int ksm_run = KSM_RUN_STOP;
Izik Eidus31dbd012009-09-21 17:02:03 -0700215
216static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
217static DEFINE_MUTEX(ksm_thread_mutex);
218static DEFINE_SPINLOCK(ksm_mmlist_lock);
219
220#define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
221 sizeof(struct __struct), __alignof__(struct __struct),\
222 (__flags), NULL)
223
224static int __init ksm_slab_init(void)
225{
226 rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
227 if (!rmap_item_cache)
228 goto out;
229
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800230 stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
231 if (!stable_node_cache)
232 goto out_free1;
233
Izik Eidus31dbd012009-09-21 17:02:03 -0700234 mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
235 if (!mm_slot_cache)
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800236 goto out_free2;
Izik Eidus31dbd012009-09-21 17:02:03 -0700237
238 return 0;
239
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800240out_free2:
241 kmem_cache_destroy(stable_node_cache);
242out_free1:
Izik Eidus31dbd012009-09-21 17:02:03 -0700243 kmem_cache_destroy(rmap_item_cache);
244out:
245 return -ENOMEM;
246}
247
248static void __init ksm_slab_free(void)
249{
250 kmem_cache_destroy(mm_slot_cache);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800251 kmem_cache_destroy(stable_node_cache);
Izik Eidus31dbd012009-09-21 17:02:03 -0700252 kmem_cache_destroy(rmap_item_cache);
253 mm_slot_cache = NULL;
254}
255
256static inline struct rmap_item *alloc_rmap_item(void)
257{
Hugh Dickins473b0ce2009-09-21 17:02:11 -0700258 struct rmap_item *rmap_item;
259
260 rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL);
261 if (rmap_item)
262 ksm_rmap_items++;
263 return rmap_item;
Izik Eidus31dbd012009-09-21 17:02:03 -0700264}
265
266static inline void free_rmap_item(struct rmap_item *rmap_item)
267{
Hugh Dickins473b0ce2009-09-21 17:02:11 -0700268 ksm_rmap_items--;
Izik Eidus31dbd012009-09-21 17:02:03 -0700269 rmap_item->mm = NULL; /* debug safety */
270 kmem_cache_free(rmap_item_cache, rmap_item);
271}
272
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800273static inline struct stable_node *alloc_stable_node(void)
274{
275 return kmem_cache_alloc(stable_node_cache, GFP_KERNEL);
276}
277
278static inline void free_stable_node(struct stable_node *stable_node)
279{
280 kmem_cache_free(stable_node_cache, stable_node);
281}
282
Izik Eidus31dbd012009-09-21 17:02:03 -0700283static inline struct mm_slot *alloc_mm_slot(void)
284{
285 if (!mm_slot_cache) /* initialization failed */
286 return NULL;
287 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
288}
289
290static inline void free_mm_slot(struct mm_slot *mm_slot)
291{
292 kmem_cache_free(mm_slot_cache, mm_slot);
293}
294
Izik Eidus31dbd012009-09-21 17:02:03 -0700295static struct mm_slot *get_mm_slot(struct mm_struct *mm)
296{
Izik Eidus31dbd012009-09-21 17:02:03 -0700297 struct hlist_node *node;
Sasha Levin4ca3a692013-02-22 16:32:28 -0800298 struct mm_slot *slot;
Izik Eidus31dbd012009-09-21 17:02:03 -0700299
Sasha Levin4ca3a692013-02-22 16:32:28 -0800300 hash_for_each_possible(mm_slots_hash, slot, node, link, (unsigned long)mm)
301 if (slot->mm == mm)
302 return slot;
303
Izik Eidus31dbd012009-09-21 17:02:03 -0700304 return NULL;
305}
306
307static void insert_to_mm_slots_hash(struct mm_struct *mm,
308 struct mm_slot *mm_slot)
309{
Izik Eidus31dbd012009-09-21 17:02:03 -0700310 mm_slot->mm = mm;
Sasha Levin4ca3a692013-02-22 16:32:28 -0800311 hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm);
Izik Eidus31dbd012009-09-21 17:02:03 -0700312}
313
314static inline int in_stable_tree(struct rmap_item *rmap_item)
315{
316 return rmap_item->address & STABLE_FLAG;
317}
318
319/*
Hugh Dickinsa913e182009-09-21 17:02:26 -0700320 * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
321 * page tables after it has passed through ksm_exit() - which, if necessary,
322 * takes mmap_sem briefly to serialize against them. ksm_exit() does not set
323 * a special flag: they can just back out as soon as mm_users goes to zero.
324 * ksm_test_exit() is used throughout to make this test for exit: in some
325 * places for correctness, in some places just to avoid unnecessary work.
326 */
327static inline bool ksm_test_exit(struct mm_struct *mm)
328{
329 return atomic_read(&mm->mm_users) == 0;
330}
331
332/*
Izik Eidus31dbd012009-09-21 17:02:03 -0700333 * We use break_ksm to break COW on a ksm page: it's a stripped down
334 *
335 * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
336 * put_page(page);
337 *
338 * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
339 * in case the application has unmapped and remapped mm,addr meanwhile.
340 * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP
341 * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
342 */
Hugh Dickinsd952b792009-09-21 17:02:16 -0700343static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
Izik Eidus31dbd012009-09-21 17:02:03 -0700344{
345 struct page *page;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700346 int ret = 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700347
348 do {
349 cond_resched();
350 page = follow_page(vma, addr, FOLL_GET);
Dan Carpenter22eccdd2010-04-23 13:18:10 -0400351 if (IS_ERR_OR_NULL(page))
Izik Eidus31dbd012009-09-21 17:02:03 -0700352 break;
353 if (PageKsm(page))
354 ret = handle_mm_fault(vma->vm_mm, vma, addr,
355 FAULT_FLAG_WRITE);
356 else
357 ret = VM_FAULT_WRITE;
358 put_page(page);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700359 } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM)));
360 /*
361 * We must loop because handle_mm_fault() may back out if there's
362 * any difficulty e.g. if pte accessed bit gets updated concurrently.
363 *
364 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
365 * COW has been broken, even if the vma does not permit VM_WRITE;
366 * but note that a concurrent fault might break PageKsm for us.
367 *
368 * VM_FAULT_SIGBUS could occur if we race with truncation of the
369 * backing file, which also invalidates anonymous pages: that's
370 * okay, that truncation will have unmapped the PageKsm for us.
371 *
372 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
373 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
374 * current task has TIF_MEMDIE set, and will be OOM killed on return
375 * to user; and ksmd, having no mm, would never be chosen for that.
376 *
377 * But if the mm is in a limited mem_cgroup, then the fault may fail
378 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
379 * even ksmd can fail in this way - though it's usually breaking ksm
380 * just to undo a merge it made a moment before, so unlikely to oom.
381 *
382 * That's a pity: we might therefore have more kernel pages allocated
383 * than we're counting as nodes in the stable tree; but ksm_do_scan
384 * will retry to break_cow on each pass, so should recover the page
385 * in due course. The important thing is to not let VM_MERGEABLE
386 * be cleared while any such pages might remain in the area.
387 */
388 return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700389}
390
Bob Liuef694222012-03-21 16:34:11 -0700391static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm,
392 unsigned long addr)
393{
394 struct vm_area_struct *vma;
395 if (ksm_test_exit(mm))
396 return NULL;
397 vma = find_vma(mm, addr);
398 if (!vma || vma->vm_start > addr)
399 return NULL;
400 if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
401 return NULL;
402 return vma;
403}
404
Hugh Dickins8dd35572009-12-14 17:59:18 -0800405static void break_cow(struct rmap_item *rmap_item)
Izik Eidus31dbd012009-09-21 17:02:03 -0700406{
Hugh Dickins8dd35572009-12-14 17:59:18 -0800407 struct mm_struct *mm = rmap_item->mm;
408 unsigned long addr = rmap_item->address;
Izik Eidus31dbd012009-09-21 17:02:03 -0700409 struct vm_area_struct *vma;
410
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800411 /*
412 * It is not an accident that whenever we want to break COW
413 * to undo, we also need to drop a reference to the anon_vma.
414 */
Peter Zijlstra9e601092011-03-22 16:32:46 -0700415 put_anon_vma(rmap_item->anon_vma);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800416
Hugh Dickins81464e302009-09-21 17:02:15 -0700417 down_read(&mm->mmap_sem);
Bob Liuef694222012-03-21 16:34:11 -0700418 vma = find_mergeable_vma(mm, addr);
419 if (vma)
420 break_ksm(vma, addr);
Izik Eidus31dbd012009-09-21 17:02:03 -0700421 up_read(&mm->mmap_sem);
422}
423
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800424static struct page *page_trans_compound_anon(struct page *page)
425{
426 if (PageTransCompound(page)) {
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800427 struct page *head = compound_trans_head(page);
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800428 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800429 * head may actually be splitted and freed from under
430 * us but it's ok here.
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800431 */
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800432 if (PageAnon(head))
433 return head;
434 }
435 return NULL;
436}
437
Izik Eidus31dbd012009-09-21 17:02:03 -0700438static struct page *get_mergeable_page(struct rmap_item *rmap_item)
439{
440 struct mm_struct *mm = rmap_item->mm;
441 unsigned long addr = rmap_item->address;
442 struct vm_area_struct *vma;
443 struct page *page;
444
445 down_read(&mm->mmap_sem);
Bob Liuef694222012-03-21 16:34:11 -0700446 vma = find_mergeable_vma(mm, addr);
447 if (!vma)
Izik Eidus31dbd012009-09-21 17:02:03 -0700448 goto out;
449
450 page = follow_page(vma, addr, FOLL_GET);
Dan Carpenter22eccdd2010-04-23 13:18:10 -0400451 if (IS_ERR_OR_NULL(page))
Izik Eidus31dbd012009-09-21 17:02:03 -0700452 goto out;
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800453 if (PageAnon(page) || page_trans_compound_anon(page)) {
Izik Eidus31dbd012009-09-21 17:02:03 -0700454 flush_anon_page(vma, page, addr);
455 flush_dcache_page(page);
456 } else {
457 put_page(page);
458out: page = NULL;
459 }
460 up_read(&mm->mmap_sem);
461 return page;
462}
463
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800464/*
465 * This helper is used for getting right index into array of tree roots.
466 * When merge_across_nodes knob is set to 1, there are only two rb-trees for
467 * stable and unstable pages from all nodes with roots in index 0. Otherwise,
468 * every node has its own stable and unstable tree.
469 */
470static inline int get_kpfn_nid(unsigned long kpfn)
471{
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800472 return ksm_merge_across_nodes ? 0 : pfn_to_nid(kpfn);
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800473}
474
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800475static void remove_node_from_stable_tree(struct stable_node *stable_node)
476{
477 struct rmap_item *rmap_item;
478 struct hlist_node *hlist;
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800479 int nid;
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800480
481 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
482 if (rmap_item->hlist.next)
483 ksm_pages_sharing--;
484 else
485 ksm_pages_shared--;
Peter Zijlstra9e601092011-03-22 16:32:46 -0700486 put_anon_vma(rmap_item->anon_vma);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800487 rmap_item->address &= PAGE_MASK;
488 cond_resched();
489 }
490
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800491 nid = get_kpfn_nid(stable_node->kpfn);
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800492 rb_erase(&stable_node->node, &root_stable_tree[nid]);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800493 free_stable_node(stable_node);
494}
495
496/*
497 * get_ksm_page: checks if the page indicated by the stable node
498 * is still its ksm page, despite having held no reference to it.
499 * In which case we can trust the content of the page, and it
500 * returns the gotten page; but if the page has now been zapped,
501 * remove the stale node from the stable tree and return NULL.
502 *
503 * You would expect the stable_node to hold a reference to the ksm page.
504 * But if it increments the page's count, swapping out has to wait for
505 * ksmd to come around again before it can free the page, which may take
506 * seconds or even minutes: much too unresponsive. So instead we use a
507 * "keyhole reference": access to the ksm page from the stable node peeps
508 * out through its keyhole to see if that page still holds the right key,
509 * pointing back to this stable node. This relies on freeing a PageAnon
510 * page to reset its page->mapping to NULL, and relies on no other use of
511 * a page to put something that might look like our key in page->mapping.
512 *
513 * include/linux/pagemap.h page_cache_get_speculative() is a good reference,
514 * but this is different - made simpler by ksm_thread_mutex being held, but
515 * interesting for assuming that no other use of the struct page could ever
516 * put our expected_mapping into page->mapping (or a field of the union which
Hugh Dickins8aafa6a2013-02-22 16:35:06 -0800517 * coincides with page->mapping).
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800518 *
519 * Note: it is possible that get_ksm_page() will return NULL one moment,
520 * then page the next, if the page is in between page_freeze_refs() and
521 * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page
522 * is on its way to being freed; but it is an anomaly to bear in mind.
523 */
Hugh Dickins8aafa6a2013-02-22 16:35:06 -0800524static struct page *get_ksm_page(struct stable_node *stable_node, bool locked)
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800525{
526 struct page *page;
527 void *expected_mapping;
528
Hugh Dickins62b61f62009-12-14 17:59:33 -0800529 page = pfn_to_page(stable_node->kpfn);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800530 expected_mapping = (void *)stable_node +
531 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800532 if (page->mapping != expected_mapping)
533 goto stale;
534 if (!get_page_unless_zero(page))
535 goto stale;
536 if (page->mapping != expected_mapping) {
537 put_page(page);
538 goto stale;
539 }
Hugh Dickins8aafa6a2013-02-22 16:35:06 -0800540 if (locked) {
541 lock_page(page);
542 if (page->mapping != expected_mapping) {
543 unlock_page(page);
544 put_page(page);
545 goto stale;
546 }
547 }
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800548 return page;
549stale:
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800550 remove_node_from_stable_tree(stable_node);
551 return NULL;
552}
553
Izik Eidus31dbd012009-09-21 17:02:03 -0700554/*
Izik Eidus31dbd012009-09-21 17:02:03 -0700555 * Removing rmap_item from stable or unstable tree.
556 * This function will clean the information from the stable/unstable tree.
557 */
558static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
559{
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800560 if (rmap_item->address & STABLE_FLAG) {
561 struct stable_node *stable_node;
Hugh Dickins5ad64682009-12-14 17:59:24 -0800562 struct page *page;
Izik Eidus31dbd012009-09-21 17:02:03 -0700563
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800564 stable_node = rmap_item->head;
Hugh Dickins8aafa6a2013-02-22 16:35:06 -0800565 page = get_ksm_page(stable_node, true);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800566 if (!page)
567 goto out;
568
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800569 hlist_del(&rmap_item->hlist);
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800570 unlock_page(page);
571 put_page(page);
Hugh Dickins08beca42009-12-14 17:59:21 -0800572
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800573 if (stable_node->hlist.first)
574 ksm_pages_sharing--;
575 else
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800576 ksm_pages_shared--;
Izik Eidus31dbd012009-09-21 17:02:03 -0700577
Peter Zijlstra9e601092011-03-22 16:32:46 -0700578 put_anon_vma(rmap_item->anon_vma);
Hugh Dickins93d17712009-12-14 17:59:16 -0800579 rmap_item->address &= PAGE_MASK;
Izik Eidus31dbd012009-09-21 17:02:03 -0700580
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -0800581 } else if (rmap_item->address & UNSTABLE_FLAG) {
Izik Eidus31dbd012009-09-21 17:02:03 -0700582 unsigned char age;
583 /*
Hugh Dickins9ba69292009-09-21 17:02:20 -0700584 * Usually ksmd can and must skip the rb_erase, because
Izik Eidus31dbd012009-09-21 17:02:03 -0700585 * root_unstable_tree was already reset to RB_ROOT.
Hugh Dickins9ba69292009-09-21 17:02:20 -0700586 * But be careful when an mm is exiting: do the rb_erase
587 * if this rmap_item was inserted by this scan, rather
588 * than left over from before.
Izik Eidus31dbd012009-09-21 17:02:03 -0700589 */
590 age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
Hugh Dickinscd551f92009-09-21 17:02:17 -0700591 BUG_ON(age > 1);
Izik Eidus31dbd012009-09-21 17:02:03 -0700592 if (!age)
Petr Holasek90bd6fd2013-02-22 16:35:00 -0800593 rb_erase(&rmap_item->node,
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800594 &root_unstable_tree[NUMA(rmap_item->nid)]);
Hugh Dickins93d17712009-12-14 17:59:16 -0800595 ksm_pages_unshared--;
596 rmap_item->address &= PAGE_MASK;
597 }
Hugh Dickins4035c07a2009-12-14 17:59:27 -0800598out:
Izik Eidus31dbd012009-09-21 17:02:03 -0700599 cond_resched(); /* we're called from many long loops */
600}
601
Izik Eidus31dbd012009-09-21 17:02:03 -0700602static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
Hugh Dickins6514d512009-12-14 17:59:19 -0800603 struct rmap_item **rmap_list)
Izik Eidus31dbd012009-09-21 17:02:03 -0700604{
Hugh Dickins6514d512009-12-14 17:59:19 -0800605 while (*rmap_list) {
606 struct rmap_item *rmap_item = *rmap_list;
607 *rmap_list = rmap_item->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -0700608 remove_rmap_item_from_tree(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -0700609 free_rmap_item(rmap_item);
610 }
611}
612
613/*
Hugh Dickinse850dcf2013-02-22 16:35:03 -0800614 * Though it's very tempting to unmerge rmap_items from stable tree rather
Izik Eidus31dbd012009-09-21 17:02:03 -0700615 * than check every pte of a given vma, the locking doesn't quite work for
616 * that - an rmap_item is assigned to the stable tree after inserting ksm
617 * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing
618 * rmap_items from parent to child at fork time (so as not to waste time
619 * if exit comes before the next scan reaches it).
Hugh Dickins81464e302009-09-21 17:02:15 -0700620 *
621 * Similarly, although we'd like to remove rmap_items (so updating counts
622 * and freeing memory) when unmerging an area, it's easier to leave that
623 * to the next pass of ksmd - consider, for example, how ksmd might be
624 * in cmp_and_merge_page on one of the rmap_items we would be removing.
Izik Eidus31dbd012009-09-21 17:02:03 -0700625 */
Hugh Dickinsd952b792009-09-21 17:02:16 -0700626static int unmerge_ksm_pages(struct vm_area_struct *vma,
627 unsigned long start, unsigned long end)
Izik Eidus31dbd012009-09-21 17:02:03 -0700628{
629 unsigned long addr;
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 for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
Hugh Dickins9ba69292009-09-21 17:02:20 -0700633 if (ksm_test_exit(vma->vm_mm))
634 break;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700635 if (signal_pending(current))
636 err = -ERESTARTSYS;
637 else
638 err = break_ksm(vma, addr);
639 }
640 return err;
Izik Eidus31dbd012009-09-21 17:02:03 -0700641}
642
Hugh Dickins2ffd8672009-09-21 17:02:23 -0700643#ifdef CONFIG_SYSFS
644/*
645 * Only called through the sysfs control interface:
646 */
Hugh Dickinsd952b792009-09-21 17:02:16 -0700647static int unmerge_and_remove_all_rmap_items(void)
Izik Eidus31dbd012009-09-21 17:02:03 -0700648{
649 struct mm_slot *mm_slot;
650 struct mm_struct *mm;
651 struct vm_area_struct *vma;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700652 int err = 0;
Izik Eidus31dbd012009-09-21 17:02:03 -0700653
Hugh Dickinsd952b792009-09-21 17:02:16 -0700654 spin_lock(&ksm_mmlist_lock);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700655 ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
Hugh Dickinsd952b792009-09-21 17:02:16 -0700656 struct mm_slot, mm_list);
657 spin_unlock(&ksm_mmlist_lock);
658
Hugh Dickins9ba69292009-09-21 17:02:20 -0700659 for (mm_slot = ksm_scan.mm_slot;
660 mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
Izik Eidus31dbd012009-09-21 17:02:03 -0700661 mm = mm_slot->mm;
662 down_read(&mm->mmap_sem);
663 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Hugh Dickins9ba69292009-09-21 17:02:20 -0700664 if (ksm_test_exit(mm))
665 break;
Izik Eidus31dbd012009-09-21 17:02:03 -0700666 if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
667 continue;
Hugh Dickinsd952b792009-09-21 17:02:16 -0700668 err = unmerge_ksm_pages(vma,
669 vma->vm_start, vma->vm_end);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700670 if (err)
671 goto error;
Izik Eidus31dbd012009-09-21 17:02:03 -0700672 }
Hugh Dickins9ba69292009-09-21 17:02:20 -0700673
Hugh Dickins6514d512009-12-14 17:59:19 -0800674 remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700675
676 spin_lock(&ksm_mmlist_lock);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700677 ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
Hugh Dickinsd952b792009-09-21 17:02:16 -0700678 struct mm_slot, mm_list);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700679 if (ksm_test_exit(mm)) {
Sasha Levin4ca3a692013-02-22 16:32:28 -0800680 hash_del(&mm_slot->link);
Hugh Dickins9ba69292009-09-21 17:02:20 -0700681 list_del(&mm_slot->mm_list);
682 spin_unlock(&ksm_mmlist_lock);
683
684 free_mm_slot(mm_slot);
685 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
686 up_read(&mm->mmap_sem);
687 mmdrop(mm);
688 } else {
689 spin_unlock(&ksm_mmlist_lock);
690 up_read(&mm->mmap_sem);
691 }
Izik Eidus31dbd012009-09-21 17:02:03 -0700692 }
693
Hugh Dickinsd952b792009-09-21 17:02:16 -0700694 ksm_scan.seqnr = 0;
Hugh Dickins9ba69292009-09-21 17:02:20 -0700695 return 0;
696
697error:
698 up_read(&mm->mmap_sem);
Izik Eidus31dbd012009-09-21 17:02:03 -0700699 spin_lock(&ksm_mmlist_lock);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700700 ksm_scan.mm_slot = &ksm_mm_head;
Izik Eidus31dbd012009-09-21 17:02:03 -0700701 spin_unlock(&ksm_mmlist_lock);
Hugh Dickinsd952b792009-09-21 17:02:16 -0700702 return err;
Izik Eidus31dbd012009-09-21 17:02:03 -0700703}
Hugh Dickins2ffd8672009-09-21 17:02:23 -0700704#endif /* CONFIG_SYSFS */
Izik Eidus31dbd012009-09-21 17:02:03 -0700705
Izik Eidus31dbd012009-09-21 17:02:03 -0700706static u32 calc_checksum(struct page *page)
707{
708 u32 checksum;
Cong Wang9b04c5f2011-11-25 23:14:39 +0800709 void *addr = kmap_atomic(page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700710 checksum = jhash2(addr, PAGE_SIZE / 4, 17);
Cong Wang9b04c5f2011-11-25 23:14:39 +0800711 kunmap_atomic(addr);
Izik Eidus31dbd012009-09-21 17:02:03 -0700712 return checksum;
713}
714
715static int memcmp_pages(struct page *page1, struct page *page2)
716{
717 char *addr1, *addr2;
718 int ret;
719
Cong Wang9b04c5f2011-11-25 23:14:39 +0800720 addr1 = kmap_atomic(page1);
721 addr2 = kmap_atomic(page2);
Izik Eidus31dbd012009-09-21 17:02:03 -0700722 ret = memcmp(addr1, addr2, PAGE_SIZE);
Cong Wang9b04c5f2011-11-25 23:14:39 +0800723 kunmap_atomic(addr2);
724 kunmap_atomic(addr1);
Izik Eidus31dbd012009-09-21 17:02:03 -0700725 return ret;
726}
727
728static inline int pages_identical(struct page *page1, struct page *page2)
729{
730 return !memcmp_pages(page1, page2);
731}
732
733static int write_protect_page(struct vm_area_struct *vma, struct page *page,
734 pte_t *orig_pte)
735{
736 struct mm_struct *mm = vma->vm_mm;
737 unsigned long addr;
738 pte_t *ptep;
739 spinlock_t *ptl;
740 int swapped;
741 int err = -EFAULT;
Haggai Eran6bdb9132012-10-08 16:33:35 -0700742 unsigned long mmun_start; /* For mmu_notifiers */
743 unsigned long mmun_end; /* For mmu_notifiers */
Izik Eidus31dbd012009-09-21 17:02:03 -0700744
745 addr = page_address_in_vma(page, vma);
746 if (addr == -EFAULT)
747 goto out;
748
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800749 BUG_ON(PageTransCompound(page));
Haggai Eran6bdb9132012-10-08 16:33:35 -0700750
751 mmun_start = addr;
752 mmun_end = addr + PAGE_SIZE;
753 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
754
Izik Eidus31dbd012009-09-21 17:02:03 -0700755 ptep = page_check_address(page, mm, addr, &ptl, 0);
756 if (!ptep)
Haggai Eran6bdb9132012-10-08 16:33:35 -0700757 goto out_mn;
Izik Eidus31dbd012009-09-21 17:02:03 -0700758
Hugh Dickins4e316352010-10-02 17:49:08 -0700759 if (pte_write(*ptep) || pte_dirty(*ptep)) {
Izik Eidus31dbd012009-09-21 17:02:03 -0700760 pte_t entry;
761
762 swapped = PageSwapCache(page);
763 flush_cache_page(vma, addr, page_to_pfn(page));
764 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300765 * Ok this is tricky, when get_user_pages_fast() run it doesn't
Izik Eidus31dbd012009-09-21 17:02:03 -0700766 * take any lock, therefore the check that we are going to make
767 * with the pagecount against the mapcount is racey and
768 * O_DIRECT can happen right after the check.
769 * So we clear the pte and flush the tlb before the check
770 * this assure us that no O_DIRECT can happen after the check
771 * or in the middle of the check.
772 */
773 entry = ptep_clear_flush(vma, addr, ptep);
774 /*
775 * Check that no O_DIRECT or similar I/O is in progress on the
776 * page
777 */
Hugh Dickins31e855e2009-12-14 17:59:17 -0800778 if (page_mapcount(page) + 1 + swapped != page_count(page)) {
Robin Holtcb532372010-03-23 13:35:26 -0700779 set_pte_at(mm, addr, ptep, entry);
Izik Eidus31dbd012009-09-21 17:02:03 -0700780 goto out_unlock;
781 }
Hugh Dickins4e316352010-10-02 17:49:08 -0700782 if (pte_dirty(entry))
783 set_page_dirty(page);
784 entry = pte_mkclean(pte_wrprotect(entry));
Izik Eidus31dbd012009-09-21 17:02:03 -0700785 set_pte_at_notify(mm, addr, ptep, entry);
786 }
787 *orig_pte = *ptep;
788 err = 0;
789
790out_unlock:
791 pte_unmap_unlock(ptep, ptl);
Haggai Eran6bdb9132012-10-08 16:33:35 -0700792out_mn:
793 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Izik Eidus31dbd012009-09-21 17:02:03 -0700794out:
795 return err;
796}
797
798/**
799 * replace_page - replace page in vma by new ksm page
Hugh Dickins8dd35572009-12-14 17:59:18 -0800800 * @vma: vma that holds the pte pointing to page
801 * @page: the page we are replacing by kpage
802 * @kpage: the ksm page we replace page by
Izik Eidus31dbd012009-09-21 17:02:03 -0700803 * @orig_pte: the original value of the pte
804 *
805 * Returns 0 on success, -EFAULT on failure.
806 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800807static int replace_page(struct vm_area_struct *vma, struct page *page,
808 struct page *kpage, pte_t orig_pte)
Izik Eidus31dbd012009-09-21 17:02:03 -0700809{
810 struct mm_struct *mm = vma->vm_mm;
Izik Eidus31dbd012009-09-21 17:02:03 -0700811 pmd_t *pmd;
812 pte_t *ptep;
813 spinlock_t *ptl;
814 unsigned long addr;
Izik Eidus31dbd012009-09-21 17:02:03 -0700815 int err = -EFAULT;
Haggai Eran6bdb9132012-10-08 16:33:35 -0700816 unsigned long mmun_start; /* For mmu_notifiers */
817 unsigned long mmun_end; /* For mmu_notifiers */
Izik Eidus31dbd012009-09-21 17:02:03 -0700818
Hugh Dickins8dd35572009-12-14 17:59:18 -0800819 addr = page_address_in_vma(page, vma);
Izik Eidus31dbd012009-09-21 17:02:03 -0700820 if (addr == -EFAULT)
821 goto out;
822
Bob Liu62190492012-12-11 16:00:37 -0800823 pmd = mm_find_pmd(mm, addr);
824 if (!pmd)
Izik Eidus31dbd012009-09-21 17:02:03 -0700825 goto out;
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800826 BUG_ON(pmd_trans_huge(*pmd));
Izik Eidus31dbd012009-09-21 17:02:03 -0700827
Haggai Eran6bdb9132012-10-08 16:33:35 -0700828 mmun_start = addr;
829 mmun_end = addr + PAGE_SIZE;
830 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
831
Izik Eidus31dbd012009-09-21 17:02:03 -0700832 ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
833 if (!pte_same(*ptep, orig_pte)) {
834 pte_unmap_unlock(ptep, ptl);
Haggai Eran6bdb9132012-10-08 16:33:35 -0700835 goto out_mn;
Izik Eidus31dbd012009-09-21 17:02:03 -0700836 }
837
Hugh Dickins8dd35572009-12-14 17:59:18 -0800838 get_page(kpage);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800839 page_add_anon_rmap(kpage, vma, addr);
Izik Eidus31dbd012009-09-21 17:02:03 -0700840
841 flush_cache_page(vma, addr, pte_pfn(*ptep));
842 ptep_clear_flush(vma, addr, ptep);
Hugh Dickins8dd35572009-12-14 17:59:18 -0800843 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
Izik Eidus31dbd012009-09-21 17:02:03 -0700844
Hugh Dickins8dd35572009-12-14 17:59:18 -0800845 page_remove_rmap(page);
Hugh Dickinsae52a2a2011-01-13 15:46:28 -0800846 if (!page_mapped(page))
847 try_to_free_swap(page);
Hugh Dickins8dd35572009-12-14 17:59:18 -0800848 put_page(page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700849
850 pte_unmap_unlock(ptep, ptl);
851 err = 0;
Haggai Eran6bdb9132012-10-08 16:33:35 -0700852out_mn:
853 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Izik Eidus31dbd012009-09-21 17:02:03 -0700854out:
855 return err;
856}
857
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800858static int page_trans_compound_anon_split(struct page *page)
859{
860 int ret = 0;
861 struct page *transhuge_head = page_trans_compound_anon(page);
862 if (transhuge_head) {
863 /* Get the reference on the head to split it. */
864 if (get_page_unless_zero(transhuge_head)) {
865 /*
866 * Recheck we got the reference while the head
867 * was still anonymous.
868 */
869 if (PageAnon(transhuge_head))
870 ret = split_huge_page(transhuge_head);
871 else
872 /*
873 * Retry later if split_huge_page run
874 * from under us.
875 */
876 ret = 1;
877 put_page(transhuge_head);
878 } else
879 /* Retry later if split_huge_page run from under us. */
880 ret = 1;
881 }
882 return ret;
883}
884
Izik Eidus31dbd012009-09-21 17:02:03 -0700885/*
886 * try_to_merge_one_page - take two pages and merge them into one
Hugh Dickins8dd35572009-12-14 17:59:18 -0800887 * @vma: the vma that holds the pte pointing to page
888 * @page: the PageAnon page that we want to replace with kpage
Hugh Dickins80e148222009-12-14 17:59:29 -0800889 * @kpage: the PageKsm page that we want to map instead of page,
890 * or NULL the first time when we want to use page as kpage.
Izik Eidus31dbd012009-09-21 17:02:03 -0700891 *
892 * This function returns 0 if the pages were merged, -EFAULT otherwise.
893 */
894static int try_to_merge_one_page(struct vm_area_struct *vma,
Hugh Dickins8dd35572009-12-14 17:59:18 -0800895 struct page *page, struct page *kpage)
Izik Eidus31dbd012009-09-21 17:02:03 -0700896{
897 pte_t orig_pte = __pte(0);
898 int err = -EFAULT;
899
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800900 if (page == kpage) /* ksm page forked */
901 return 0;
902
Izik Eidus31dbd012009-09-21 17:02:03 -0700903 if (!(vma->vm_flags & VM_MERGEABLE))
904 goto out;
Andrea Arcangeli29ad7682011-01-13 15:47:19 -0800905 if (PageTransCompound(page) && page_trans_compound_anon_split(page))
906 goto out;
907 BUG_ON(PageTransCompound(page));
Hugh Dickins8dd35572009-12-14 17:59:18 -0800908 if (!PageAnon(page))
Izik Eidus31dbd012009-09-21 17:02:03 -0700909 goto out;
910
Izik Eidus31dbd012009-09-21 17:02:03 -0700911 /*
912 * We need the page lock to read a stable PageSwapCache in
913 * write_protect_page(). We use trylock_page() instead of
914 * lock_page() because we don't want to wait here - we
915 * prefer to continue scanning and merging different pages,
916 * then come back to this page when it is unlocked.
917 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800918 if (!trylock_page(page))
Hugh Dickins31e855e2009-12-14 17:59:17 -0800919 goto out;
Izik Eidus31dbd012009-09-21 17:02:03 -0700920 /*
921 * If this anonymous page is mapped only here, its pte may need
922 * to be write-protected. If it's mapped elsewhere, all of its
923 * ptes are necessarily already write-protected. But in either
924 * case, we need to lock and check page_count is not raised.
925 */
Hugh Dickins80e148222009-12-14 17:59:29 -0800926 if (write_protect_page(vma, page, &orig_pte) == 0) {
927 if (!kpage) {
928 /*
929 * While we hold page lock, upgrade page from
930 * PageAnon+anon_vma to PageKsm+NULL stable_node:
931 * stable_tree_insert() will update stable_node.
932 */
933 set_page_stable_node(page, NULL);
934 mark_page_accessed(page);
935 err = 0;
936 } else if (pages_identical(page, kpage))
937 err = replace_page(vma, page, kpage, orig_pte);
938 }
Izik Eidus31dbd012009-09-21 17:02:03 -0700939
Hugh Dickins80e148222009-12-14 17:59:29 -0800940 if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
Hugh Dickins73848b42009-12-14 17:59:22 -0800941 munlock_vma_page(page);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800942 if (!PageMlocked(kpage)) {
943 unlock_page(page);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800944 lock_page(kpage);
945 mlock_vma_page(kpage);
946 page = kpage; /* for final unlock */
947 }
948 }
Hugh Dickins73848b42009-12-14 17:59:22 -0800949
Hugh Dickins8dd35572009-12-14 17:59:18 -0800950 unlock_page(page);
Izik Eidus31dbd012009-09-21 17:02:03 -0700951out:
952 return err;
953}
954
955/*
Hugh Dickins81464e302009-09-21 17:02:15 -0700956 * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
957 * but no new kernel page is allocated: kpage must already be a ksm page.
Hugh Dickins8dd35572009-12-14 17:59:18 -0800958 *
959 * This function returns 0 if the pages were merged, -EFAULT otherwise.
Hugh Dickins81464e302009-09-21 17:02:15 -0700960 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800961static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
962 struct page *page, struct page *kpage)
Hugh Dickins81464e302009-09-21 17:02:15 -0700963{
Hugh Dickins8dd35572009-12-14 17:59:18 -0800964 struct mm_struct *mm = rmap_item->mm;
Hugh Dickins81464e302009-09-21 17:02:15 -0700965 struct vm_area_struct *vma;
966 int err = -EFAULT;
967
Hugh Dickins8dd35572009-12-14 17:59:18 -0800968 down_read(&mm->mmap_sem);
969 if (ksm_test_exit(mm))
970 goto out;
971 vma = find_vma(mm, rmap_item->address);
972 if (!vma || vma->vm_start > rmap_item->address)
Hugh Dickins9ba69292009-09-21 17:02:20 -0700973 goto out;
974
Hugh Dickins8dd35572009-12-14 17:59:18 -0800975 err = try_to_merge_one_page(vma, page, kpage);
Hugh Dickinsdb114b82009-12-14 17:59:25 -0800976 if (err)
977 goto out;
978
979 /* Must get reference to anon_vma while still holding mmap_sem */
Peter Zijlstra9e601092011-03-22 16:32:46 -0700980 rmap_item->anon_vma = vma->anon_vma;
981 get_anon_vma(vma->anon_vma);
Hugh Dickins81464e302009-09-21 17:02:15 -0700982out:
Hugh Dickins8dd35572009-12-14 17:59:18 -0800983 up_read(&mm->mmap_sem);
Hugh Dickins81464e302009-09-21 17:02:15 -0700984 return err;
985}
986
987/*
Izik Eidus31dbd012009-09-21 17:02:03 -0700988 * try_to_merge_two_pages - take two identical pages and prepare them
989 * to be merged into one page.
990 *
Hugh Dickins8dd35572009-12-14 17:59:18 -0800991 * This function returns the kpage if we successfully merged two identical
992 * pages into one ksm page, NULL otherwise.
Izik Eidus31dbd012009-09-21 17:02:03 -0700993 *
Hugh Dickins80e148222009-12-14 17:59:29 -0800994 * Note that this function upgrades page to ksm page: if one of the pages
Izik Eidus31dbd012009-09-21 17:02:03 -0700995 * is already a ksm page, try_to_merge_with_ksm_page should be used.
996 */
Hugh Dickins8dd35572009-12-14 17:59:18 -0800997static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
998 struct page *page,
999 struct rmap_item *tree_rmap_item,
1000 struct page *tree_page)
Izik Eidus31dbd012009-09-21 17:02:03 -07001001{
Hugh Dickins80e148222009-12-14 17:59:29 -08001002 int err;
Izik Eidus31dbd012009-09-21 17:02:03 -07001003
Hugh Dickins80e148222009-12-14 17:59:29 -08001004 err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
Izik Eidus31dbd012009-09-21 17:02:03 -07001005 if (!err) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001006 err = try_to_merge_with_ksm_page(tree_rmap_item,
Hugh Dickins80e148222009-12-14 17:59:29 -08001007 tree_page, page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001008 /*
Hugh Dickins81464e302009-09-21 17:02:15 -07001009 * If that fails, we have a ksm page with only one pte
1010 * pointing to it: so break it.
Izik Eidus31dbd012009-09-21 17:02:03 -07001011 */
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001012 if (err)
Hugh Dickins8dd35572009-12-14 17:59:18 -08001013 break_cow(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001014 }
Hugh Dickins80e148222009-12-14 17:59:29 -08001015 return err ? NULL : page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001016}
1017
1018/*
Hugh Dickins8dd35572009-12-14 17:59:18 -08001019 * stable_tree_search - search for page inside the stable tree
Izik Eidus31dbd012009-09-21 17:02:03 -07001020 *
1021 * This function checks if there is a page inside the stable tree
1022 * with identical content to the page that we are scanning right now.
1023 *
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001024 * This function returns the stable tree node of identical content if found,
Izik Eidus31dbd012009-09-21 17:02:03 -07001025 * NULL otherwise.
1026 */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001027static struct page *stable_tree_search(struct page *page)
Izik Eidus31dbd012009-09-21 17:02:03 -07001028{
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001029 struct rb_node *node;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001030 struct stable_node *stable_node;
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001031 int nid;
Izik Eidus31dbd012009-09-21 17:02:03 -07001032
Hugh Dickins08beca42009-12-14 17:59:21 -08001033 stable_node = page_stable_node(page);
1034 if (stable_node) { /* ksm page forked */
1035 get_page(page);
Hugh Dickins62b61f62009-12-14 17:59:33 -08001036 return page;
Hugh Dickins08beca42009-12-14 17:59:21 -08001037 }
1038
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001039 nid = get_kpfn_nid(page_to_pfn(page));
1040 node = root_stable_tree[nid].rb_node;
1041
Izik Eidus31dbd012009-09-21 17:02:03 -07001042 while (node) {
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001043 struct page *tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001044 int ret;
1045
Hugh Dickins08beca42009-12-14 17:59:21 -08001046 cond_resched();
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001047 stable_node = rb_entry(node, struct stable_node, node);
Hugh Dickins8aafa6a2013-02-22 16:35:06 -08001048 tree_page = get_ksm_page(stable_node, false);
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001049 if (!tree_page)
1050 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -07001051
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001052 ret = memcmp_pages(page, tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001053
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001054 if (ret < 0) {
1055 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001056 node = node->rb_left;
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001057 } else if (ret > 0) {
1058 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001059 node = node->rb_right;
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001060 } else
Hugh Dickins62b61f62009-12-14 17:59:33 -08001061 return tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001062 }
1063
1064 return NULL;
1065}
1066
1067/*
Hugh Dickinse850dcf2013-02-22 16:35:03 -08001068 * stable_tree_insert - insert stable tree node pointing to new ksm page
Izik Eidus31dbd012009-09-21 17:02:03 -07001069 * into the stable tree.
1070 *
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001071 * This function returns the stable tree node just allocated on success,
1072 * NULL otherwise.
Izik Eidus31dbd012009-09-21 17:02:03 -07001073 */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001074static struct stable_node *stable_tree_insert(struct page *kpage)
Izik Eidus31dbd012009-09-21 17:02:03 -07001075{
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001076 int nid;
1077 unsigned long kpfn;
1078 struct rb_node **new;
Izik Eidus31dbd012009-09-21 17:02:03 -07001079 struct rb_node *parent = NULL;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001080 struct stable_node *stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001081
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001082 kpfn = page_to_pfn(kpage);
1083 nid = get_kpfn_nid(kpfn);
1084 new = &root_stable_tree[nid].rb_node;
1085
Izik Eidus31dbd012009-09-21 17:02:03 -07001086 while (*new) {
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001087 struct page *tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001088 int ret;
1089
Hugh Dickins08beca42009-12-14 17:59:21 -08001090 cond_resched();
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001091 stable_node = rb_entry(*new, struct stable_node, node);
Hugh Dickins8aafa6a2013-02-22 16:35:06 -08001092 tree_page = get_ksm_page(stable_node, false);
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001093 if (!tree_page)
1094 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -07001095
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001096 ret = memcmp_pages(kpage, tree_page);
1097 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001098
1099 parent = *new;
1100 if (ret < 0)
1101 new = &parent->rb_left;
1102 else if (ret > 0)
1103 new = &parent->rb_right;
1104 else {
1105 /*
1106 * It is not a bug that stable_tree_search() didn't
1107 * find this node: because at that time our page was
1108 * not yet write-protected, so may have changed since.
1109 */
1110 return NULL;
1111 }
1112 }
1113
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001114 stable_node = alloc_stable_node();
1115 if (!stable_node)
1116 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -07001117
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001118 INIT_HLIST_HEAD(&stable_node->hlist);
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001119 stable_node->kpfn = kpfn;
Hugh Dickins08beca42009-12-14 17:59:21 -08001120 set_page_stable_node(kpage, stable_node);
Hugh Dickinse850dcf2013-02-22 16:35:03 -08001121 rb_link_node(&stable_node->node, parent, new);
1122 rb_insert_color(&stable_node->node, &root_stable_tree[nid]);
Hugh Dickins08beca42009-12-14 17:59:21 -08001123
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001124 return stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001125}
1126
1127/*
Hugh Dickins8dd35572009-12-14 17:59:18 -08001128 * unstable_tree_search_insert - search for identical page,
1129 * else insert rmap_item into the unstable tree.
Izik Eidus31dbd012009-09-21 17:02:03 -07001130 *
1131 * This function searches for a page in the unstable tree identical to the
1132 * page currently being scanned; and if no identical page is found in the
1133 * tree, we insert rmap_item as a new object into the unstable tree.
1134 *
1135 * This function returns pointer to rmap_item found to be identical
1136 * to the currently scanned page, NULL otherwise.
1137 *
1138 * This function does both searching and inserting, because they share
1139 * the same walking algorithm in an rbtree.
1140 */
Hugh Dickins8dd35572009-12-14 17:59:18 -08001141static
1142struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
1143 struct page *page,
1144 struct page **tree_pagep)
Izik Eidus31dbd012009-09-21 17:02:03 -07001145{
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001146 struct rb_node **new;
1147 struct rb_root *root;
Izik Eidus31dbd012009-09-21 17:02:03 -07001148 struct rb_node *parent = NULL;
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001149 int nid;
1150
1151 nid = get_kpfn_nid(page_to_pfn(page));
1152 root = &root_unstable_tree[nid];
1153 new = &root->rb_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001154
1155 while (*new) {
1156 struct rmap_item *tree_rmap_item;
Hugh Dickins8dd35572009-12-14 17:59:18 -08001157 struct page *tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001158 int ret;
1159
Hugh Dickinsd178f272009-11-09 15:58:23 +00001160 cond_resched();
Izik Eidus31dbd012009-09-21 17:02:03 -07001161 tree_rmap_item = rb_entry(*new, struct rmap_item, node);
Hugh Dickins8dd35572009-12-14 17:59:18 -08001162 tree_page = get_mergeable_page(tree_rmap_item);
Dan Carpenter22eccdd2010-04-23 13:18:10 -04001163 if (IS_ERR_OR_NULL(tree_page))
Izik Eidus31dbd012009-09-21 17:02:03 -07001164 return NULL;
1165
1166 /*
Hugh Dickins8dd35572009-12-14 17:59:18 -08001167 * Don't substitute a ksm page for a forked page.
Izik Eidus31dbd012009-09-21 17:02:03 -07001168 */
Hugh Dickins8dd35572009-12-14 17:59:18 -08001169 if (page == tree_page) {
1170 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001171 return NULL;
1172 }
1173
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001174 /*
1175 * If tree_page has been migrated to another NUMA node, it
1176 * will be flushed out and put into the right unstable tree
1177 * next time: only merge with it if merge_across_nodes.
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001178 */
1179 if (!ksm_merge_across_nodes && page_to_nid(tree_page) != nid) {
1180 put_page(tree_page);
1181 return NULL;
1182 }
1183
Hugh Dickins8dd35572009-12-14 17:59:18 -08001184 ret = memcmp_pages(page, tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001185
1186 parent = *new;
1187 if (ret < 0) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001188 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001189 new = &parent->rb_left;
1190 } else if (ret > 0) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001191 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001192 new = &parent->rb_right;
1193 } else {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001194 *tree_pagep = tree_page;
Izik Eidus31dbd012009-09-21 17:02:03 -07001195 return tree_rmap_item;
1196 }
1197 }
1198
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001199 rmap_item->address |= UNSTABLE_FLAG;
Izik Eidus31dbd012009-09-21 17:02:03 -07001200 rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
Hugh Dickinse850dcf2013-02-22 16:35:03 -08001201 DO_NUMA(rmap_item->nid = nid);
Izik Eidus31dbd012009-09-21 17:02:03 -07001202 rb_link_node(&rmap_item->node, parent, new);
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001203 rb_insert_color(&rmap_item->node, root);
Izik Eidus31dbd012009-09-21 17:02:03 -07001204
Hugh Dickins473b0ce2009-09-21 17:02:11 -07001205 ksm_pages_unshared++;
Izik Eidus31dbd012009-09-21 17:02:03 -07001206 return NULL;
1207}
1208
1209/*
1210 * stable_tree_append - add another rmap_item to the linked list of
1211 * rmap_items hanging off a given node of the stable tree, all sharing
1212 * the same ksm page.
1213 */
1214static void stable_tree_append(struct rmap_item *rmap_item,
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001215 struct stable_node *stable_node)
Izik Eidus31dbd012009-09-21 17:02:03 -07001216{
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001217 /*
1218 * Usually rmap_item->nid is already set correctly,
1219 * but it may be wrong after switching merge_across_nodes.
1220 */
Hugh Dickinse850dcf2013-02-22 16:35:03 -08001221 DO_NUMA(rmap_item->nid = get_kpfn_nid(stable_node->kpfn));
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001222 rmap_item->head = stable_node;
Izik Eidus31dbd012009-09-21 17:02:03 -07001223 rmap_item->address |= STABLE_FLAG;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001224 hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
Hugh Dickinse178dfd2009-09-21 17:02:10 -07001225
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001226 if (rmap_item->hlist.next)
1227 ksm_pages_sharing++;
1228 else
1229 ksm_pages_shared++;
Izik Eidus31dbd012009-09-21 17:02:03 -07001230}
1231
1232/*
Hugh Dickins81464e302009-09-21 17:02:15 -07001233 * cmp_and_merge_page - first see if page can be merged into the stable tree;
1234 * if not, compare checksum to previous and if it's the same, see if page can
1235 * be inserted into the unstable tree, or merged with a page already there and
1236 * both transferred to the stable tree.
Izik Eidus31dbd012009-09-21 17:02:03 -07001237 *
1238 * @page: the page that we are searching identical page to.
1239 * @rmap_item: the reverse mapping into the virtual address of this page
1240 */
1241static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
1242{
Izik Eidus31dbd012009-09-21 17:02:03 -07001243 struct rmap_item *tree_rmap_item;
Hugh Dickins8dd35572009-12-14 17:59:18 -08001244 struct page *tree_page = NULL;
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001245 struct stable_node *stable_node;
Hugh Dickins8dd35572009-12-14 17:59:18 -08001246 struct page *kpage;
Izik Eidus31dbd012009-09-21 17:02:03 -07001247 unsigned int checksum;
1248 int err;
1249
Hugh Dickins93d17712009-12-14 17:59:16 -08001250 remove_rmap_item_from_tree(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001251
1252 /* We first start with searching the page inside the stable tree */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001253 kpage = stable_tree_search(page);
1254 if (kpage) {
Hugh Dickins08beca42009-12-14 17:59:21 -08001255 err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
Izik Eidus31dbd012009-09-21 17:02:03 -07001256 if (!err) {
1257 /*
1258 * The page was successfully merged:
1259 * add its rmap_item to the stable tree.
1260 */
Hugh Dickins5ad64682009-12-14 17:59:24 -08001261 lock_page(kpage);
Hugh Dickins62b61f62009-12-14 17:59:33 -08001262 stable_tree_append(rmap_item, page_stable_node(kpage));
Hugh Dickins5ad64682009-12-14 17:59:24 -08001263 unlock_page(kpage);
Izik Eidus31dbd012009-09-21 17:02:03 -07001264 }
Hugh Dickins8dd35572009-12-14 17:59:18 -08001265 put_page(kpage);
Izik Eidus31dbd012009-09-21 17:02:03 -07001266 return;
1267 }
1268
1269 /*
Hugh Dickins4035c07a2009-12-14 17:59:27 -08001270 * If the hash value of the page has changed from the last time
1271 * we calculated it, this page is changing frequently: therefore we
1272 * don't want to insert it in the unstable tree, and we don't want
1273 * to waste our time searching for something identical to it there.
Izik Eidus31dbd012009-09-21 17:02:03 -07001274 */
1275 checksum = calc_checksum(page);
1276 if (rmap_item->oldchecksum != checksum) {
1277 rmap_item->oldchecksum = checksum;
1278 return;
1279 }
1280
Hugh Dickins8dd35572009-12-14 17:59:18 -08001281 tree_rmap_item =
1282 unstable_tree_search_insert(rmap_item, page, &tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001283 if (tree_rmap_item) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001284 kpage = try_to_merge_two_pages(rmap_item, page,
1285 tree_rmap_item, tree_page);
1286 put_page(tree_page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001287 /*
1288 * As soon as we merge this page, we want to remove the
1289 * rmap_item of the page we have merged with from the unstable
1290 * tree, and insert it instead as new node in the stable tree.
1291 */
Hugh Dickins8dd35572009-12-14 17:59:18 -08001292 if (kpage) {
Hugh Dickins93d17712009-12-14 17:59:16 -08001293 remove_rmap_item_from_tree(tree_rmap_item);
Hugh Dickins473b0ce2009-09-21 17:02:11 -07001294
Hugh Dickins5ad64682009-12-14 17:59:24 -08001295 lock_page(kpage);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001296 stable_node = stable_tree_insert(kpage);
1297 if (stable_node) {
1298 stable_tree_append(tree_rmap_item, stable_node);
1299 stable_tree_append(rmap_item, stable_node);
1300 }
Hugh Dickins5ad64682009-12-14 17:59:24 -08001301 unlock_page(kpage);
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001302
Izik Eidus31dbd012009-09-21 17:02:03 -07001303 /*
1304 * If we fail to insert the page into the stable tree,
1305 * we will have 2 virtual addresses that are pointing
1306 * to a ksm page left outside the stable tree,
1307 * in which case we need to break_cow on both.
1308 */
Hugh Dickins7b6ba2c2009-12-14 17:59:20 -08001309 if (!stable_node) {
Hugh Dickins8dd35572009-12-14 17:59:18 -08001310 break_cow(tree_rmap_item);
1311 break_cow(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001312 }
1313 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001314 }
1315}
1316
1317static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
Hugh Dickins6514d512009-12-14 17:59:19 -08001318 struct rmap_item **rmap_list,
Izik Eidus31dbd012009-09-21 17:02:03 -07001319 unsigned long addr)
1320{
1321 struct rmap_item *rmap_item;
1322
Hugh Dickins6514d512009-12-14 17:59:19 -08001323 while (*rmap_list) {
1324 rmap_item = *rmap_list;
Hugh Dickins93d17712009-12-14 17:59:16 -08001325 if ((rmap_item->address & PAGE_MASK) == addr)
Izik Eidus31dbd012009-09-21 17:02:03 -07001326 return rmap_item;
Izik Eidus31dbd012009-09-21 17:02:03 -07001327 if (rmap_item->address > addr)
1328 break;
Hugh Dickins6514d512009-12-14 17:59:19 -08001329 *rmap_list = rmap_item->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -07001330 remove_rmap_item_from_tree(rmap_item);
Izik Eidus31dbd012009-09-21 17:02:03 -07001331 free_rmap_item(rmap_item);
1332 }
1333
1334 rmap_item = alloc_rmap_item();
1335 if (rmap_item) {
1336 /* It has already been zeroed */
1337 rmap_item->mm = mm_slot->mm;
1338 rmap_item->address = addr;
Hugh Dickins6514d512009-12-14 17:59:19 -08001339 rmap_item->rmap_list = *rmap_list;
1340 *rmap_list = rmap_item;
Izik Eidus31dbd012009-09-21 17:02:03 -07001341 }
1342 return rmap_item;
1343}
1344
1345static struct rmap_item *scan_get_next_rmap_item(struct page **page)
1346{
1347 struct mm_struct *mm;
1348 struct mm_slot *slot;
1349 struct vm_area_struct *vma;
1350 struct rmap_item *rmap_item;
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001351 int nid;
Izik Eidus31dbd012009-09-21 17:02:03 -07001352
1353 if (list_empty(&ksm_mm_head.mm_list))
1354 return NULL;
1355
1356 slot = ksm_scan.mm_slot;
1357 if (slot == &ksm_mm_head) {
Hugh Dickins2919bfd2011-01-13 15:47:29 -08001358 /*
1359 * A number of pages can hang around indefinitely on per-cpu
1360 * pagevecs, raised page count preventing write_protect_page
1361 * from merging them. Though it doesn't really matter much,
1362 * it is puzzling to see some stuck in pages_volatile until
1363 * other activity jostles them out, and they also prevented
1364 * LTP's KSM test from succeeding deterministically; so drain
1365 * them here (here rather than on entry to ksm_do_scan(),
1366 * so we don't IPI too often when pages_to_scan is set low).
1367 */
1368 lru_add_drain_all();
1369
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001370 for (nid = 0; nid < nr_node_ids; nid++)
1371 root_unstable_tree[nid] = RB_ROOT;
Izik Eidus31dbd012009-09-21 17:02:03 -07001372
1373 spin_lock(&ksm_mmlist_lock);
1374 slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
1375 ksm_scan.mm_slot = slot;
1376 spin_unlock(&ksm_mmlist_lock);
Hugh Dickins2b472612011-06-15 15:08:58 -07001377 /*
1378 * Although we tested list_empty() above, a racing __ksm_exit
1379 * of the last mm on the list may have removed it since then.
1380 */
1381 if (slot == &ksm_mm_head)
1382 return NULL;
Izik Eidus31dbd012009-09-21 17:02:03 -07001383next_mm:
1384 ksm_scan.address = 0;
Hugh Dickins6514d512009-12-14 17:59:19 -08001385 ksm_scan.rmap_list = &slot->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -07001386 }
1387
1388 mm = slot->mm;
1389 down_read(&mm->mmap_sem);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001390 if (ksm_test_exit(mm))
1391 vma = NULL;
1392 else
1393 vma = find_vma(mm, ksm_scan.address);
1394
1395 for (; vma; vma = vma->vm_next) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001396 if (!(vma->vm_flags & VM_MERGEABLE))
1397 continue;
1398 if (ksm_scan.address < vma->vm_start)
1399 ksm_scan.address = vma->vm_start;
1400 if (!vma->anon_vma)
1401 ksm_scan.address = vma->vm_end;
1402
1403 while (ksm_scan.address < vma->vm_end) {
Hugh Dickins9ba69292009-09-21 17:02:20 -07001404 if (ksm_test_exit(mm))
1405 break;
Izik Eidus31dbd012009-09-21 17:02:03 -07001406 *page = follow_page(vma, ksm_scan.address, FOLL_GET);
Andrea Arcangeli21ae5b02011-01-13 15:47:00 -08001407 if (IS_ERR_OR_NULL(*page)) {
1408 ksm_scan.address += PAGE_SIZE;
1409 cond_resched();
1410 continue;
1411 }
Andrea Arcangeli29ad7682011-01-13 15:47:19 -08001412 if (PageAnon(*page) ||
1413 page_trans_compound_anon(*page)) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001414 flush_anon_page(vma, *page, ksm_scan.address);
1415 flush_dcache_page(*page);
1416 rmap_item = get_next_rmap_item(slot,
Hugh Dickins6514d512009-12-14 17:59:19 -08001417 ksm_scan.rmap_list, ksm_scan.address);
Izik Eidus31dbd012009-09-21 17:02:03 -07001418 if (rmap_item) {
Hugh Dickins6514d512009-12-14 17:59:19 -08001419 ksm_scan.rmap_list =
1420 &rmap_item->rmap_list;
Izik Eidus31dbd012009-09-21 17:02:03 -07001421 ksm_scan.address += PAGE_SIZE;
1422 } else
1423 put_page(*page);
1424 up_read(&mm->mmap_sem);
1425 return rmap_item;
1426 }
Andrea Arcangeli21ae5b02011-01-13 15:47:00 -08001427 put_page(*page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001428 ksm_scan.address += PAGE_SIZE;
1429 cond_resched();
1430 }
1431 }
1432
Hugh Dickins9ba69292009-09-21 17:02:20 -07001433 if (ksm_test_exit(mm)) {
1434 ksm_scan.address = 0;
Hugh Dickins6514d512009-12-14 17:59:19 -08001435 ksm_scan.rmap_list = &slot->rmap_list;
Hugh Dickins9ba69292009-09-21 17:02:20 -07001436 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001437 /*
1438 * Nuke all the rmap_items that are above this current rmap:
1439 * because there were no VM_MERGEABLE vmas with such addresses.
1440 */
Hugh Dickins6514d512009-12-14 17:59:19 -08001441 remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
Izik Eidus31dbd012009-09-21 17:02:03 -07001442
1443 spin_lock(&ksm_mmlist_lock);
Hugh Dickinscd551f92009-09-21 17:02:17 -07001444 ksm_scan.mm_slot = list_entry(slot->mm_list.next,
1445 struct mm_slot, mm_list);
1446 if (ksm_scan.address == 0) {
1447 /*
1448 * We've completed a full scan of all vmas, holding mmap_sem
1449 * throughout, and found no VM_MERGEABLE: so do the same as
1450 * __ksm_exit does to remove this mm from all our lists now.
Hugh Dickins9ba69292009-09-21 17:02:20 -07001451 * This applies either when cleaning up after __ksm_exit
1452 * (but beware: we can reach here even before __ksm_exit),
1453 * or when all VM_MERGEABLE areas have been unmapped (and
1454 * mmap_sem then protects against race with MADV_MERGEABLE).
Hugh Dickinscd551f92009-09-21 17:02:17 -07001455 */
Sasha Levin4ca3a692013-02-22 16:32:28 -08001456 hash_del(&slot->link);
Hugh Dickinscd551f92009-09-21 17:02:17 -07001457 list_del(&slot->mm_list);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001458 spin_unlock(&ksm_mmlist_lock);
1459
Hugh Dickinscd551f92009-09-21 17:02:17 -07001460 free_mm_slot(slot);
1461 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001462 up_read(&mm->mmap_sem);
1463 mmdrop(mm);
1464 } else {
1465 spin_unlock(&ksm_mmlist_lock);
1466 up_read(&mm->mmap_sem);
Hugh Dickinscd551f92009-09-21 17:02:17 -07001467 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001468
1469 /* Repeat until we've completed scanning the whole list */
Hugh Dickinscd551f92009-09-21 17:02:17 -07001470 slot = ksm_scan.mm_slot;
Izik Eidus31dbd012009-09-21 17:02:03 -07001471 if (slot != &ksm_mm_head)
1472 goto next_mm;
1473
Izik Eidus31dbd012009-09-21 17:02:03 -07001474 ksm_scan.seqnr++;
1475 return NULL;
1476}
1477
1478/**
1479 * ksm_do_scan - the ksm scanner main worker function.
1480 * @scan_npages - number of pages we want to scan before we return.
1481 */
1482static void ksm_do_scan(unsigned int scan_npages)
1483{
1484 struct rmap_item *rmap_item;
Dan Carpenter22eccdd2010-04-23 13:18:10 -04001485 struct page *uninitialized_var(page);
Izik Eidus31dbd012009-09-21 17:02:03 -07001486
Andrea Arcangeli878aee72011-01-13 15:47:10 -08001487 while (scan_npages-- && likely(!freezing(current))) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001488 cond_resched();
1489 rmap_item = scan_get_next_rmap_item(&page);
1490 if (!rmap_item)
1491 return;
1492 if (!PageKsm(page) || !in_stable_tree(rmap_item))
1493 cmp_and_merge_page(page, rmap_item);
1494 put_page(page);
1495 }
1496}
1497
Hugh Dickins6e1583842009-09-21 17:02:14 -07001498static int ksmd_should_run(void)
1499{
1500 return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
1501}
1502
Izik Eidus31dbd012009-09-21 17:02:03 -07001503static int ksm_scan_thread(void *nothing)
1504{
Andrea Arcangeli878aee72011-01-13 15:47:10 -08001505 set_freezable();
Izik Eidus339aa622009-09-21 17:02:07 -07001506 set_user_nice(current, 5);
Izik Eidus31dbd012009-09-21 17:02:03 -07001507
1508 while (!kthread_should_stop()) {
Hugh Dickins6e1583842009-09-21 17:02:14 -07001509 mutex_lock(&ksm_thread_mutex);
1510 if (ksmd_should_run())
Izik Eidus31dbd012009-09-21 17:02:03 -07001511 ksm_do_scan(ksm_thread_pages_to_scan);
Hugh Dickins6e1583842009-09-21 17:02:14 -07001512 mutex_unlock(&ksm_thread_mutex);
1513
Andrea Arcangeli878aee72011-01-13 15:47:10 -08001514 try_to_freeze();
1515
Hugh Dickins6e1583842009-09-21 17:02:14 -07001516 if (ksmd_should_run()) {
Izik Eidus31dbd012009-09-21 17:02:03 -07001517 schedule_timeout_interruptible(
1518 msecs_to_jiffies(ksm_thread_sleep_millisecs));
1519 } else {
Andrea Arcangeli878aee72011-01-13 15:47:10 -08001520 wait_event_freezable(ksm_thread_wait,
Hugh Dickins6e1583842009-09-21 17:02:14 -07001521 ksmd_should_run() || kthread_should_stop());
Izik Eidus31dbd012009-09-21 17:02:03 -07001522 }
1523 }
1524 return 0;
1525}
1526
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001527int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
1528 unsigned long end, int advice, unsigned long *vm_flags)
1529{
1530 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinsd952b792009-09-21 17:02:16 -07001531 int err;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001532
1533 switch (advice) {
1534 case MADV_MERGEABLE:
1535 /*
1536 * Be somewhat over-protective for now!
1537 */
1538 if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
1539 VM_PFNMAP | VM_IO | VM_DONTEXPAND |
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001540 VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP))
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001541 return 0; /* just ignore the advice */
1542
Konstantin Khlebnikovcc2383e2012-10-08 16:28:37 -07001543#ifdef VM_SAO
1544 if (*vm_flags & VM_SAO)
1545 return 0;
1546#endif
1547
Hugh Dickinsd952b792009-09-21 17:02:16 -07001548 if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
1549 err = __ksm_enter(mm);
1550 if (err)
1551 return err;
1552 }
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001553
1554 *vm_flags |= VM_MERGEABLE;
1555 break;
1556
1557 case MADV_UNMERGEABLE:
1558 if (!(*vm_flags & VM_MERGEABLE))
1559 return 0; /* just ignore the advice */
1560
Hugh Dickinsd952b792009-09-21 17:02:16 -07001561 if (vma->anon_vma) {
1562 err = unmerge_ksm_pages(vma, start, end);
1563 if (err)
1564 return err;
1565 }
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001566
1567 *vm_flags &= ~VM_MERGEABLE;
1568 break;
1569 }
1570
1571 return 0;
1572}
1573
1574int __ksm_enter(struct mm_struct *mm)
1575{
Hugh Dickins6e1583842009-09-21 17:02:14 -07001576 struct mm_slot *mm_slot;
1577 int needs_wakeup;
1578
1579 mm_slot = alloc_mm_slot();
Izik Eidus31dbd012009-09-21 17:02:03 -07001580 if (!mm_slot)
1581 return -ENOMEM;
1582
Hugh Dickins6e1583842009-09-21 17:02:14 -07001583 /* Check ksm_run too? Would need tighter locking */
1584 needs_wakeup = list_empty(&ksm_mm_head.mm_list);
1585
Izik Eidus31dbd012009-09-21 17:02:03 -07001586 spin_lock(&ksm_mmlist_lock);
1587 insert_to_mm_slots_hash(mm, mm_slot);
1588 /*
1589 * Insert just behind the scanning cursor, to let the area settle
1590 * down a little; when fork is followed by immediate exec, we don't
1591 * want ksmd to waste time setting up and tearing down an rmap_list.
1592 */
1593 list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
1594 spin_unlock(&ksm_mmlist_lock);
1595
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001596 set_bit(MMF_VM_MERGEABLE, &mm->flags);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001597 atomic_inc(&mm->mm_count);
Hugh Dickins6e1583842009-09-21 17:02:14 -07001598
1599 if (needs_wakeup)
1600 wake_up_interruptible(&ksm_thread_wait);
1601
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001602 return 0;
1603}
1604
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -07001605void __ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001606{
Hugh Dickinscd551f92009-09-21 17:02:17 -07001607 struct mm_slot *mm_slot;
Hugh Dickins9ba69292009-09-21 17:02:20 -07001608 int easy_to_free = 0;
Hugh Dickinscd551f92009-09-21 17:02:17 -07001609
Izik Eidus31dbd012009-09-21 17:02:03 -07001610 /*
Hugh Dickins9ba69292009-09-21 17:02:20 -07001611 * This process is exiting: if it's straightforward (as is the
1612 * case when ksmd was never running), free mm_slot immediately.
1613 * But if it's at the cursor or has rmap_items linked to it, use
1614 * mmap_sem to synchronize with any break_cows before pagetables
1615 * are freed, and leave the mm_slot on the list for ksmd to free.
1616 * Beware: ksm may already have noticed it exiting and freed the slot.
Izik Eidus31dbd012009-09-21 17:02:03 -07001617 */
Hugh Dickins9ba69292009-09-21 17:02:20 -07001618
Hugh Dickinscd551f92009-09-21 17:02:17 -07001619 spin_lock(&ksm_mmlist_lock);
1620 mm_slot = get_mm_slot(mm);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001621 if (mm_slot && ksm_scan.mm_slot != mm_slot) {
Hugh Dickins6514d512009-12-14 17:59:19 -08001622 if (!mm_slot->rmap_list) {
Sasha Levin4ca3a692013-02-22 16:32:28 -08001623 hash_del(&mm_slot->link);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001624 list_del(&mm_slot->mm_list);
1625 easy_to_free = 1;
1626 } else {
1627 list_move(&mm_slot->mm_list,
1628 &ksm_scan.mm_slot->mm_list);
1629 }
Hugh Dickinscd551f92009-09-21 17:02:17 -07001630 }
Hugh Dickinscd551f92009-09-21 17:02:17 -07001631 spin_unlock(&ksm_mmlist_lock);
1632
Hugh Dickins9ba69292009-09-21 17:02:20 -07001633 if (easy_to_free) {
1634 free_mm_slot(mm_slot);
1635 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
1636 mmdrop(mm);
1637 } else if (mm_slot) {
Hugh Dickins9ba69292009-09-21 17:02:20 -07001638 down_write(&mm->mmap_sem);
1639 up_write(&mm->mmap_sem);
Hugh Dickins9ba69292009-09-21 17:02:20 -07001640 }
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001641}
Izik Eidus31dbd012009-09-21 17:02:03 -07001642
Hugh Dickins5ad64682009-12-14 17:59:24 -08001643struct page *ksm_does_need_to_copy(struct page *page,
1644 struct vm_area_struct *vma, unsigned long address)
1645{
1646 struct page *new_page;
1647
Hugh Dickins5ad64682009-12-14 17:59:24 -08001648 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
1649 if (new_page) {
1650 copy_user_highpage(new_page, page, address, vma);
1651
1652 SetPageDirty(new_page);
1653 __SetPageUptodate(new_page);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001654 __set_page_locked(new_page);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001655 }
1656
Hugh Dickins5ad64682009-12-14 17:59:24 -08001657 return new_page;
1658}
1659
1660int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg,
1661 unsigned long *vm_flags)
1662{
1663 struct stable_node *stable_node;
1664 struct rmap_item *rmap_item;
1665 struct hlist_node *hlist;
1666 unsigned int mapcount = page_mapcount(page);
1667 int referenced = 0;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001668 int search_new_forks = 0;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001669
1670 VM_BUG_ON(!PageKsm(page));
1671 VM_BUG_ON(!PageLocked(page));
1672
1673 stable_node = page_stable_node(page);
1674 if (!stable_node)
1675 return 0;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001676again:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001677 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001678 struct anon_vma *anon_vma = rmap_item->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08001679 struct anon_vma_chain *vmac;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001680 struct vm_area_struct *vma;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001681
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001682 anon_vma_lock_read(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -07001683 anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1684 0, ULONG_MAX) {
Rik van Riel5beb4932010-03-05 13:42:07 -08001685 vma = vmac->vma;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001686 if (rmap_item->address < vma->vm_start ||
1687 rmap_item->address >= vma->vm_end)
1688 continue;
1689 /*
1690 * Initially we examine only the vma which covers this
1691 * rmap_item; but later, if there is still work to do,
1692 * we examine covering vmas in other mms: in case they
1693 * were forked from the original since ksmd passed.
1694 */
1695 if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1696 continue;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001697
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001698 if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
1699 continue;
1700
1701 referenced += page_referenced_one(page, vma,
Hugh Dickins5ad64682009-12-14 17:59:24 -08001702 rmap_item->address, &mapcount, vm_flags);
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001703 if (!search_new_forks || !mapcount)
1704 break;
1705 }
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001706 anon_vma_unlock_read(anon_vma);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001707 if (!mapcount)
1708 goto out;
1709 }
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001710 if (!search_new_forks++)
1711 goto again;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001712out:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001713 return referenced;
1714}
1715
1716int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
1717{
1718 struct stable_node *stable_node;
1719 struct hlist_node *hlist;
1720 struct rmap_item *rmap_item;
1721 int ret = SWAP_AGAIN;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001722 int search_new_forks = 0;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001723
1724 VM_BUG_ON(!PageKsm(page));
1725 VM_BUG_ON(!PageLocked(page));
1726
1727 stable_node = page_stable_node(page);
1728 if (!stable_node)
1729 return SWAP_FAIL;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001730again:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001731 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001732 struct anon_vma *anon_vma = rmap_item->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08001733 struct anon_vma_chain *vmac;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001734 struct vm_area_struct *vma;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001735
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001736 anon_vma_lock_read(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -07001737 anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1738 0, ULONG_MAX) {
Rik van Riel5beb4932010-03-05 13:42:07 -08001739 vma = vmac->vma;
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001740 if (rmap_item->address < vma->vm_start ||
1741 rmap_item->address >= vma->vm_end)
1742 continue;
1743 /*
1744 * Initially we examine only the vma which covers this
1745 * rmap_item; but later, if there is still work to do,
1746 * we examine covering vmas in other mms: in case they
1747 * were forked from the original since ksmd passed.
1748 */
1749 if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1750 continue;
1751
1752 ret = try_to_unmap_one(page, vma,
1753 rmap_item->address, flags);
1754 if (ret != SWAP_AGAIN || !page_mapped(page)) {
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001755 anon_vma_unlock_read(anon_vma);
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001756 goto out;
1757 }
1758 }
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001759 anon_vma_unlock_read(anon_vma);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001760 }
Hugh Dickinsdb114b82009-12-14 17:59:25 -08001761 if (!search_new_forks++)
1762 goto again;
Hugh Dickins5ad64682009-12-14 17:59:24 -08001763out:
Hugh Dickins5ad64682009-12-14 17:59:24 -08001764 return ret;
1765}
1766
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001767#ifdef CONFIG_MIGRATION
1768int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
1769 struct vm_area_struct *, unsigned long, void *), void *arg)
1770{
1771 struct stable_node *stable_node;
1772 struct hlist_node *hlist;
1773 struct rmap_item *rmap_item;
1774 int ret = SWAP_AGAIN;
1775 int search_new_forks = 0;
1776
1777 VM_BUG_ON(!PageKsm(page));
1778 VM_BUG_ON(!PageLocked(page));
1779
1780 stable_node = page_stable_node(page);
1781 if (!stable_node)
1782 return ret;
1783again:
1784 hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1785 struct anon_vma *anon_vma = rmap_item->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08001786 struct anon_vma_chain *vmac;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001787 struct vm_area_struct *vma;
1788
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001789 anon_vma_lock_read(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -07001790 anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1791 0, ULONG_MAX) {
Rik van Riel5beb4932010-03-05 13:42:07 -08001792 vma = vmac->vma;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001793 if (rmap_item->address < vma->vm_start ||
1794 rmap_item->address >= vma->vm_end)
1795 continue;
1796 /*
1797 * Initially we examine only the vma which covers this
1798 * rmap_item; but later, if there is still work to do,
1799 * we examine covering vmas in other mms: in case they
1800 * were forked from the original since ksmd passed.
1801 */
1802 if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1803 continue;
1804
1805 ret = rmap_one(page, vma, rmap_item->address, arg);
1806 if (ret != SWAP_AGAIN) {
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001807 anon_vma_unlock_read(anon_vma);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001808 goto out;
1809 }
1810 }
Hugh Dickinsb6b19f22012-12-19 17:44:29 -08001811 anon_vma_unlock_read(anon_vma);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001812 }
1813 if (!search_new_forks++)
1814 goto again;
1815out:
1816 return ret;
1817}
1818
1819void ksm_migrate_page(struct page *newpage, struct page *oldpage)
1820{
1821 struct stable_node *stable_node;
1822
1823 VM_BUG_ON(!PageLocked(oldpage));
1824 VM_BUG_ON(!PageLocked(newpage));
1825 VM_BUG_ON(newpage->mapping != oldpage->mapping);
1826
1827 stable_node = page_stable_node(newpage);
1828 if (stable_node) {
Hugh Dickins62b61f62009-12-14 17:59:33 -08001829 VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage));
1830 stable_node->kpfn = page_to_pfn(newpage);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08001831 }
1832}
1833#endif /* CONFIG_MIGRATION */
1834
Hugh Dickins62b61f62009-12-14 17:59:33 -08001835#ifdef CONFIG_MEMORY_HOTREMOVE
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001836static void ksm_check_stable_tree(unsigned long start_pfn,
1837 unsigned long end_pfn)
Hugh Dickins62b61f62009-12-14 17:59:33 -08001838{
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001839 struct stable_node *stable_node;
Hugh Dickins62b61f62009-12-14 17:59:33 -08001840 struct rb_node *node;
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001841 int nid;
Hugh Dickins62b61f62009-12-14 17:59:33 -08001842
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001843 for (nid = 0; nid < nr_node_ids; nid++) {
1844 node = rb_first(&root_stable_tree[nid]);
1845 while (node) {
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001846 stable_node = rb_entry(node, struct stable_node, node);
1847 if (stable_node->kpfn >= start_pfn &&
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001848 stable_node->kpfn < end_pfn) {
1849 /*
1850 * Don't get_ksm_page, page has already gone:
1851 * which is why we keep kpfn instead of page*
1852 */
1853 remove_node_from_stable_tree(stable_node);
1854 node = rb_first(&root_stable_tree[nid]);
1855 } else
1856 node = rb_next(node);
1857 cond_resched();
Petr Holasek90bd6fd2013-02-22 16:35:00 -08001858 }
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001859 }
Hugh Dickins62b61f62009-12-14 17:59:33 -08001860}
1861
1862static int ksm_memory_callback(struct notifier_block *self,
1863 unsigned long action, void *arg)
1864{
1865 struct memory_notify *mn = arg;
Hugh Dickins62b61f62009-12-14 17:59:33 -08001866
1867 switch (action) {
1868 case MEM_GOING_OFFLINE:
1869 /*
1870 * Keep it very simple for now: just lock out ksmd and
1871 * MADV_UNMERGEABLE while any memory is going offline.
KOSAKI Motohiroa0b0f582010-12-02 14:31:20 -08001872 * mutex_lock_nested() is necessary because lockdep was alarmed
1873 * that here we take ksm_thread_mutex inside notifier chain
1874 * mutex, and later take notifier chain mutex inside
1875 * ksm_thread_mutex to unlock it. But that's safe because both
1876 * are inside mem_hotplug_mutex.
Hugh Dickins62b61f62009-12-14 17:59:33 -08001877 */
KOSAKI Motohiroa0b0f582010-12-02 14:31:20 -08001878 mutex_lock_nested(&ksm_thread_mutex, SINGLE_DEPTH_NESTING);
Hugh Dickins62b61f62009-12-14 17:59:33 -08001879 break;
1880
1881 case MEM_OFFLINE:
1882 /*
1883 * Most of the work is done by page migration; but there might
1884 * be a few stable_nodes left over, still pointing to struct
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001885 * pages which have been offlined: prune those from the tree,
1886 * otherwise get_ksm_page() might later try to access a
1887 * non-existent struct page.
Hugh Dickins62b61f62009-12-14 17:59:33 -08001888 */
Hugh Dickinsee0ea592013-02-22 16:35:05 -08001889 ksm_check_stable_tree(mn->start_pfn,
1890 mn->start_pfn + mn->nr_pages);
Hugh Dickins62b61f62009-12-14 17:59:33 -08001891 /* fallthrough */
1892
1893 case MEM_CANCEL_OFFLINE:
1894 mutex_unlock(&ksm_thread_mutex);
1895 break;
1896 }
1897 return NOTIFY_OK;
1898}
1899#endif /* CONFIG_MEMORY_HOTREMOVE */
1900
Hugh Dickins2ffd8672009-09-21 17:02:23 -07001901#ifdef CONFIG_SYSFS
1902/*
1903 * This all compiles without CONFIG_SYSFS, but is a waste of space.
1904 */
1905
Izik Eidus31dbd012009-09-21 17:02:03 -07001906#define KSM_ATTR_RO(_name) \
1907 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1908#define KSM_ATTR(_name) \
1909 static struct kobj_attribute _name##_attr = \
1910 __ATTR(_name, 0644, _name##_show, _name##_store)
1911
1912static ssize_t sleep_millisecs_show(struct kobject *kobj,
1913 struct kobj_attribute *attr, char *buf)
1914{
1915 return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
1916}
1917
1918static ssize_t sleep_millisecs_store(struct kobject *kobj,
1919 struct kobj_attribute *attr,
1920 const char *buf, size_t count)
1921{
1922 unsigned long msecs;
1923 int err;
1924
1925 err = strict_strtoul(buf, 10, &msecs);
1926 if (err || msecs > UINT_MAX)
1927 return -EINVAL;
1928
1929 ksm_thread_sleep_millisecs = msecs;
1930
1931 return count;
1932}
1933KSM_ATTR(sleep_millisecs);
1934
1935static ssize_t pages_to_scan_show(struct kobject *kobj,
1936 struct kobj_attribute *attr, char *buf)
1937{
1938 return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
1939}
1940
1941static ssize_t pages_to_scan_store(struct kobject *kobj,
1942 struct kobj_attribute *attr,
1943 const char *buf, size_t count)
1944{
1945 int err;
1946 unsigned long nr_pages;
1947
1948 err = strict_strtoul(buf, 10, &nr_pages);
1949 if (err || nr_pages > UINT_MAX)
1950 return -EINVAL;
1951
1952 ksm_thread_pages_to_scan = nr_pages;
1953
1954 return count;
1955}
1956KSM_ATTR(pages_to_scan);
1957
1958static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
1959 char *buf)
1960{
1961 return sprintf(buf, "%u\n", ksm_run);
1962}
1963
1964static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
1965 const char *buf, size_t count)
1966{
1967 int err;
1968 unsigned long flags;
1969
1970 err = strict_strtoul(buf, 10, &flags);
1971 if (err || flags > UINT_MAX)
1972 return -EINVAL;
1973 if (flags > KSM_RUN_UNMERGE)
1974 return -EINVAL;
1975
1976 /*
1977 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
1978 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
Hugh Dickinsd0f209f2009-12-14 17:59:34 -08001979 * breaking COW to free the pages_shared (but leaves mm_slots
1980 * on the list for when ksmd may be set running again).
Izik Eidus31dbd012009-09-21 17:02:03 -07001981 */
1982
1983 mutex_lock(&ksm_thread_mutex);
1984 if (ksm_run != flags) {
1985 ksm_run = flags;
Hugh Dickinsd952b792009-09-21 17:02:16 -07001986 if (flags & KSM_RUN_UNMERGE) {
David Rientjese1e12d22012-12-11 16:02:56 -08001987 set_current_oom_origin();
Hugh Dickinsd952b792009-09-21 17:02:16 -07001988 err = unmerge_and_remove_all_rmap_items();
David Rientjese1e12d22012-12-11 16:02:56 -08001989 clear_current_oom_origin();
Hugh Dickinsd952b792009-09-21 17:02:16 -07001990 if (err) {
1991 ksm_run = KSM_RUN_STOP;
1992 count = err;
1993 }
1994 }
Izik Eidus31dbd012009-09-21 17:02:03 -07001995 }
1996 mutex_unlock(&ksm_thread_mutex);
1997
1998 if (flags & KSM_RUN_MERGE)
1999 wake_up_interruptible(&ksm_thread_wait);
2000
2001 return count;
2002}
2003KSM_ATTR(run);
2004
Petr Holasek90bd6fd2013-02-22 16:35:00 -08002005#ifdef CONFIG_NUMA
2006static ssize_t merge_across_nodes_show(struct kobject *kobj,
2007 struct kobj_attribute *attr, char *buf)
2008{
2009 return sprintf(buf, "%u\n", ksm_merge_across_nodes);
2010}
2011
2012static ssize_t merge_across_nodes_store(struct kobject *kobj,
2013 struct kobj_attribute *attr,
2014 const char *buf, size_t count)
2015{
2016 int err;
2017 unsigned long knob;
2018
2019 err = kstrtoul(buf, 10, &knob);
2020 if (err)
2021 return err;
2022 if (knob > 1)
2023 return -EINVAL;
2024
2025 mutex_lock(&ksm_thread_mutex);
2026 if (ksm_merge_across_nodes != knob) {
2027 if (ksm_pages_shared)
2028 err = -EBUSY;
2029 else
2030 ksm_merge_across_nodes = knob;
2031 }
2032 mutex_unlock(&ksm_thread_mutex);
2033
2034 return err ? err : count;
2035}
2036KSM_ATTR(merge_across_nodes);
2037#endif
2038
Hugh Dickinsb4028262009-09-21 17:02:09 -07002039static ssize_t pages_shared_show(struct kobject *kobj,
2040 struct kobj_attribute *attr, char *buf)
2041{
2042 return sprintf(buf, "%lu\n", ksm_pages_shared);
2043}
2044KSM_ATTR_RO(pages_shared);
2045
2046static ssize_t pages_sharing_show(struct kobject *kobj,
2047 struct kobj_attribute *attr, char *buf)
2048{
Hugh Dickinse178dfd2009-09-21 17:02:10 -07002049 return sprintf(buf, "%lu\n", ksm_pages_sharing);
Hugh Dickinsb4028262009-09-21 17:02:09 -07002050}
2051KSM_ATTR_RO(pages_sharing);
2052
Hugh Dickins473b0ce2009-09-21 17:02:11 -07002053static ssize_t pages_unshared_show(struct kobject *kobj,
2054 struct kobj_attribute *attr, char *buf)
2055{
2056 return sprintf(buf, "%lu\n", ksm_pages_unshared);
2057}
2058KSM_ATTR_RO(pages_unshared);
2059
2060static ssize_t pages_volatile_show(struct kobject *kobj,
2061 struct kobj_attribute *attr, char *buf)
2062{
2063 long ksm_pages_volatile;
2064
2065 ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
2066 - ksm_pages_sharing - ksm_pages_unshared;
2067 /*
2068 * It was not worth any locking to calculate that statistic,
2069 * but it might therefore sometimes be negative: conceal that.
2070 */
2071 if (ksm_pages_volatile < 0)
2072 ksm_pages_volatile = 0;
2073 return sprintf(buf, "%ld\n", ksm_pages_volatile);
2074}
2075KSM_ATTR_RO(pages_volatile);
2076
2077static ssize_t full_scans_show(struct kobject *kobj,
2078 struct kobj_attribute *attr, char *buf)
2079{
2080 return sprintf(buf, "%lu\n", ksm_scan.seqnr);
2081}
2082KSM_ATTR_RO(full_scans);
2083
Izik Eidus31dbd012009-09-21 17:02:03 -07002084static struct attribute *ksm_attrs[] = {
2085 &sleep_millisecs_attr.attr,
2086 &pages_to_scan_attr.attr,
2087 &run_attr.attr,
Hugh Dickinsb4028262009-09-21 17:02:09 -07002088 &pages_shared_attr.attr,
2089 &pages_sharing_attr.attr,
Hugh Dickins473b0ce2009-09-21 17:02:11 -07002090 &pages_unshared_attr.attr,
2091 &pages_volatile_attr.attr,
2092 &full_scans_attr.attr,
Petr Holasek90bd6fd2013-02-22 16:35:00 -08002093#ifdef CONFIG_NUMA
2094 &merge_across_nodes_attr.attr,
2095#endif
Izik Eidus31dbd012009-09-21 17:02:03 -07002096 NULL,
2097};
2098
2099static struct attribute_group ksm_attr_group = {
2100 .attrs = ksm_attrs,
2101 .name = "ksm",
2102};
Hugh Dickins2ffd8672009-09-21 17:02:23 -07002103#endif /* CONFIG_SYSFS */
Izik Eidus31dbd012009-09-21 17:02:03 -07002104
2105static int __init ksm_init(void)
2106{
2107 struct task_struct *ksm_thread;
2108 int err;
Petr Holasek90bd6fd2013-02-22 16:35:00 -08002109 int nid;
Izik Eidus31dbd012009-09-21 17:02:03 -07002110
2111 err = ksm_slab_init();
2112 if (err)
2113 goto out;
2114
Petr Holasek90bd6fd2013-02-22 16:35:00 -08002115 for (nid = 0; nid < nr_node_ids; nid++)
2116 root_stable_tree[nid] = RB_ROOT;
2117
Izik Eidus31dbd012009-09-21 17:02:03 -07002118 ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
2119 if (IS_ERR(ksm_thread)) {
2120 printk(KERN_ERR "ksm: creating kthread failed\n");
2121 err = PTR_ERR(ksm_thread);
Lai Jiangshand9f89842010-08-09 17:20:02 -07002122 goto out_free;
Izik Eidus31dbd012009-09-21 17:02:03 -07002123 }
2124
Hugh Dickins2ffd8672009-09-21 17:02:23 -07002125#ifdef CONFIG_SYSFS
Izik Eidus31dbd012009-09-21 17:02:03 -07002126 err = sysfs_create_group(mm_kobj, &ksm_attr_group);
2127 if (err) {
2128 printk(KERN_ERR "ksm: register sysfs failed\n");
Hugh Dickins2ffd8672009-09-21 17:02:23 -07002129 kthread_stop(ksm_thread);
Lai Jiangshand9f89842010-08-09 17:20:02 -07002130 goto out_free;
Izik Eidus31dbd012009-09-21 17:02:03 -07002131 }
Hugh Dickinsc73602a2009-10-07 16:32:22 -07002132#else
2133 ksm_run = KSM_RUN_MERGE; /* no way for user to start it */
2134
Hugh Dickins2ffd8672009-09-21 17:02:23 -07002135#endif /* CONFIG_SYSFS */
Izik Eidus31dbd012009-09-21 17:02:03 -07002136
Hugh Dickins62b61f62009-12-14 17:59:33 -08002137#ifdef CONFIG_MEMORY_HOTREMOVE
2138 /*
2139 * Choose a high priority since the callback takes ksm_thread_mutex:
2140 * later callbacks could only be taking locks which nest within that.
2141 */
2142 hotplug_memory_notifier(ksm_memory_callback, 100);
2143#endif
Izik Eidus31dbd012009-09-21 17:02:03 -07002144 return 0;
2145
Lai Jiangshand9f89842010-08-09 17:20:02 -07002146out_free:
Izik Eidus31dbd012009-09-21 17:02:03 -07002147 ksm_slab_free();
2148out:
2149 return err;
2150}
2151module_init(ksm_init)