xref: /linux/mm/ksm.c (revision 25985edcedea6396277003854657b5f3cb31a628)
1f8af4da3SHugh Dickins /*
231dbd01fSIzik Eidus  * Memory merging support.
331dbd01fSIzik Eidus  *
431dbd01fSIzik Eidus  * This code enables dynamic sharing of identical pages found in different
531dbd01fSIzik Eidus  * memory areas, even if they are not shared by fork()
631dbd01fSIzik Eidus  *
736b2528dSIzik Eidus  * Copyright (C) 2008-2009 Red Hat, Inc.
831dbd01fSIzik Eidus  * Authors:
931dbd01fSIzik Eidus  *	Izik Eidus
1031dbd01fSIzik Eidus  *	Andrea Arcangeli
1131dbd01fSIzik Eidus  *	Chris Wright
1236b2528dSIzik Eidus  *	Hugh Dickins
1331dbd01fSIzik Eidus  *
1431dbd01fSIzik Eidus  * This work is licensed under the terms of the GNU GPL, version 2.
15f8af4da3SHugh Dickins  */
16f8af4da3SHugh Dickins 
17f8af4da3SHugh Dickins #include <linux/errno.h>
1831dbd01fSIzik Eidus #include <linux/mm.h>
1931dbd01fSIzik Eidus #include <linux/fs.h>
20f8af4da3SHugh Dickins #include <linux/mman.h>
2131dbd01fSIzik Eidus #include <linux/sched.h>
2231dbd01fSIzik Eidus #include <linux/rwsem.h>
2331dbd01fSIzik Eidus #include <linux/pagemap.h>
2431dbd01fSIzik Eidus #include <linux/rmap.h>
2531dbd01fSIzik Eidus #include <linux/spinlock.h>
2631dbd01fSIzik Eidus #include <linux/jhash.h>
2731dbd01fSIzik Eidus #include <linux/delay.h>
2831dbd01fSIzik Eidus #include <linux/kthread.h>
2931dbd01fSIzik Eidus #include <linux/wait.h>
3031dbd01fSIzik Eidus #include <linux/slab.h>
3131dbd01fSIzik Eidus #include <linux/rbtree.h>
3262b61f61SHugh Dickins #include <linux/memory.h>
3331dbd01fSIzik Eidus #include <linux/mmu_notifier.h>
342c6854fdSIzik Eidus #include <linux/swap.h>
35f8af4da3SHugh Dickins #include <linux/ksm.h>
36d9f8984cSLai Jiangshan #include <linux/hash.h>
37878aee7dSAndrea Arcangeli #include <linux/freezer.h>
38f8af4da3SHugh Dickins 
3931dbd01fSIzik Eidus #include <asm/tlbflush.h>
4073848b46SHugh Dickins #include "internal.h"
4131dbd01fSIzik Eidus 
4231dbd01fSIzik Eidus /*
4331dbd01fSIzik Eidus  * A few notes about the KSM scanning process,
4431dbd01fSIzik Eidus  * to make it easier to understand the data structures below:
4531dbd01fSIzik Eidus  *
4631dbd01fSIzik Eidus  * In order to reduce excessive scanning, KSM sorts the memory pages by their
4731dbd01fSIzik Eidus  * contents into a data structure that holds pointers to the pages' locations.
4831dbd01fSIzik Eidus  *
4931dbd01fSIzik Eidus  * Since the contents of the pages may change at any moment, KSM cannot just
5031dbd01fSIzik Eidus  * insert the pages into a normal sorted tree and expect it to find anything.
5131dbd01fSIzik Eidus  * Therefore KSM uses two data structures - the stable and the unstable tree.
5231dbd01fSIzik Eidus  *
5331dbd01fSIzik Eidus  * The stable tree holds pointers to all the merged pages (ksm pages), sorted
5431dbd01fSIzik Eidus  * by their contents.  Because each such page is write-protected, searching on
5531dbd01fSIzik Eidus  * this tree is fully assured to be working (except when pages are unmapped),
5631dbd01fSIzik Eidus  * and therefore this tree is called the stable tree.
5731dbd01fSIzik Eidus  *
5831dbd01fSIzik Eidus  * In addition to the stable tree, KSM uses a second data structure called the
5931dbd01fSIzik Eidus  * unstable tree: this tree holds pointers to pages which have been found to
6031dbd01fSIzik Eidus  * be "unchanged for a period of time".  The unstable tree sorts these pages
6131dbd01fSIzik Eidus  * by their contents, but since they are not write-protected, KSM cannot rely
6231dbd01fSIzik Eidus  * upon the unstable tree to work correctly - the unstable tree is liable to
6331dbd01fSIzik Eidus  * be corrupted as its contents are modified, and so it is called unstable.
6431dbd01fSIzik Eidus  *
6531dbd01fSIzik Eidus  * KSM solves this problem by several techniques:
6631dbd01fSIzik Eidus  *
6731dbd01fSIzik Eidus  * 1) The unstable tree is flushed every time KSM completes scanning all
6831dbd01fSIzik Eidus  *    memory areas, and then the tree is rebuilt again from the beginning.
6931dbd01fSIzik Eidus  * 2) KSM will only insert into the unstable tree, pages whose hash value
7031dbd01fSIzik Eidus  *    has not changed since the previous scan of all memory areas.
7131dbd01fSIzik Eidus  * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
7231dbd01fSIzik Eidus  *    colors of the nodes and not on their contents, assuring that even when
7331dbd01fSIzik Eidus  *    the tree gets "corrupted" it won't get out of balance, so scanning time
7431dbd01fSIzik Eidus  *    remains the same (also, searching and inserting nodes in an rbtree uses
7531dbd01fSIzik Eidus  *    the same algorithm, so we have no overhead when we flush and rebuild).
7631dbd01fSIzik Eidus  * 4) KSM never flushes the stable tree, which means that even if it were to
7731dbd01fSIzik Eidus  *    take 10 attempts to find a page in the unstable tree, once it is found,
7831dbd01fSIzik Eidus  *    it is secured in the stable tree.  (When we scan a new page, we first
7931dbd01fSIzik Eidus  *    compare it against the stable tree, and then against the unstable tree.)
8031dbd01fSIzik Eidus  */
8131dbd01fSIzik Eidus 
8231dbd01fSIzik Eidus /**
8331dbd01fSIzik Eidus  * struct mm_slot - ksm information per mm that is being scanned
8431dbd01fSIzik Eidus  * @link: link to the mm_slots hash list
8531dbd01fSIzik Eidus  * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
866514d511SHugh Dickins  * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
8731dbd01fSIzik Eidus  * @mm: the mm that this information is valid for
8831dbd01fSIzik Eidus  */
8931dbd01fSIzik Eidus struct mm_slot {
9031dbd01fSIzik Eidus 	struct hlist_node link;
9131dbd01fSIzik Eidus 	struct list_head mm_list;
926514d511SHugh Dickins 	struct rmap_item *rmap_list;
9331dbd01fSIzik Eidus 	struct mm_struct *mm;
9431dbd01fSIzik Eidus };
9531dbd01fSIzik Eidus 
9631dbd01fSIzik Eidus /**
9731dbd01fSIzik Eidus  * struct ksm_scan - cursor for scanning
9831dbd01fSIzik Eidus  * @mm_slot: the current mm_slot we are scanning
9931dbd01fSIzik Eidus  * @address: the next address inside that to be scanned
1006514d511SHugh Dickins  * @rmap_list: link to the next rmap to be scanned in the rmap_list
10131dbd01fSIzik Eidus  * @seqnr: count of completed full scans (needed when removing unstable node)
10231dbd01fSIzik Eidus  *
10331dbd01fSIzik Eidus  * There is only the one ksm_scan instance of this cursor structure.
10431dbd01fSIzik Eidus  */
10531dbd01fSIzik Eidus struct ksm_scan {
10631dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
10731dbd01fSIzik Eidus 	unsigned long address;
1086514d511SHugh Dickins 	struct rmap_item **rmap_list;
10931dbd01fSIzik Eidus 	unsigned long seqnr;
11031dbd01fSIzik Eidus };
11131dbd01fSIzik Eidus 
11231dbd01fSIzik Eidus /**
1137b6ba2c7SHugh Dickins  * struct stable_node - node of the stable rbtree
1147b6ba2c7SHugh Dickins  * @node: rb node of this ksm page in the stable tree
1157b6ba2c7SHugh Dickins  * @hlist: hlist head of rmap_items using this ksm page
11662b61f61SHugh Dickins  * @kpfn: page frame number of this ksm page
1177b6ba2c7SHugh Dickins  */
1187b6ba2c7SHugh Dickins struct stable_node {
1197b6ba2c7SHugh Dickins 	struct rb_node node;
1207b6ba2c7SHugh Dickins 	struct hlist_head hlist;
12162b61f61SHugh Dickins 	unsigned long kpfn;
1227b6ba2c7SHugh Dickins };
1237b6ba2c7SHugh Dickins 
1247b6ba2c7SHugh Dickins /**
12531dbd01fSIzik Eidus  * struct rmap_item - reverse mapping item for virtual addresses
1266514d511SHugh Dickins  * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
127db114b83SHugh Dickins  * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
12831dbd01fSIzik Eidus  * @mm: the memory structure this rmap_item is pointing into
12931dbd01fSIzik Eidus  * @address: the virtual address this rmap_item tracks (+ flags in low bits)
13031dbd01fSIzik Eidus  * @oldchecksum: previous checksum of the page at that virtual address
1317b6ba2c7SHugh Dickins  * @node: rb node of this rmap_item in the unstable tree
1327b6ba2c7SHugh Dickins  * @head: pointer to stable_node heading this list in the stable tree
1337b6ba2c7SHugh Dickins  * @hlist: link into hlist of rmap_items hanging off that stable_node
13431dbd01fSIzik Eidus  */
13531dbd01fSIzik Eidus struct rmap_item {
1366514d511SHugh Dickins 	struct rmap_item *rmap_list;
137db114b83SHugh Dickins 	struct anon_vma *anon_vma;	/* when stable */
13831dbd01fSIzik Eidus 	struct mm_struct *mm;
13931dbd01fSIzik Eidus 	unsigned long address;		/* + low bits used for flags below */
14031dbd01fSIzik Eidus 	unsigned int oldchecksum;	/* when unstable */
14131dbd01fSIzik Eidus 	union {
1427b6ba2c7SHugh Dickins 		struct rb_node node;	/* when node of unstable tree */
1437b6ba2c7SHugh Dickins 		struct {		/* when listed from stable tree */
1447b6ba2c7SHugh Dickins 			struct stable_node *head;
1457b6ba2c7SHugh Dickins 			struct hlist_node hlist;
1467b6ba2c7SHugh Dickins 		};
14731dbd01fSIzik Eidus 	};
14831dbd01fSIzik Eidus };
14931dbd01fSIzik Eidus 
15031dbd01fSIzik Eidus #define SEQNR_MASK	0x0ff	/* low bits of unstable tree seqnr */
1517b6ba2c7SHugh Dickins #define UNSTABLE_FLAG	0x100	/* is a node of the unstable tree */
1527b6ba2c7SHugh Dickins #define STABLE_FLAG	0x200	/* is listed from the stable tree */
15331dbd01fSIzik Eidus 
15431dbd01fSIzik Eidus /* The stable and unstable tree heads */
15531dbd01fSIzik Eidus static struct rb_root root_stable_tree = RB_ROOT;
15631dbd01fSIzik Eidus static struct rb_root root_unstable_tree = RB_ROOT;
15731dbd01fSIzik Eidus 
158d9f8984cSLai Jiangshan #define MM_SLOTS_HASH_SHIFT 10
159d9f8984cSLai Jiangshan #define MM_SLOTS_HASH_HEADS (1 << MM_SLOTS_HASH_SHIFT)
160d9f8984cSLai Jiangshan static struct hlist_head mm_slots_hash[MM_SLOTS_HASH_HEADS];
16131dbd01fSIzik Eidus 
16231dbd01fSIzik Eidus static struct mm_slot ksm_mm_head = {
16331dbd01fSIzik Eidus 	.mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
16431dbd01fSIzik Eidus };
16531dbd01fSIzik Eidus static struct ksm_scan ksm_scan = {
16631dbd01fSIzik Eidus 	.mm_slot = &ksm_mm_head,
16731dbd01fSIzik Eidus };
16831dbd01fSIzik Eidus 
16931dbd01fSIzik Eidus static struct kmem_cache *rmap_item_cache;
1707b6ba2c7SHugh Dickins static struct kmem_cache *stable_node_cache;
17131dbd01fSIzik Eidus static struct kmem_cache *mm_slot_cache;
17231dbd01fSIzik Eidus 
17331dbd01fSIzik Eidus /* The number of nodes in the stable tree */
174b4028260SHugh Dickins static unsigned long ksm_pages_shared;
17531dbd01fSIzik Eidus 
176e178dfdeSHugh Dickins /* The number of page slots additionally sharing those nodes */
177b4028260SHugh Dickins static unsigned long ksm_pages_sharing;
17831dbd01fSIzik Eidus 
179473b0ce4SHugh Dickins /* The number of nodes in the unstable tree */
180473b0ce4SHugh Dickins static unsigned long ksm_pages_unshared;
181473b0ce4SHugh Dickins 
182473b0ce4SHugh Dickins /* The number of rmap_items in use: to calculate pages_volatile */
183473b0ce4SHugh Dickins static unsigned long ksm_rmap_items;
184473b0ce4SHugh Dickins 
18531dbd01fSIzik Eidus /* Number of pages ksmd should scan in one batch */
1862c6854fdSIzik Eidus static unsigned int ksm_thread_pages_to_scan = 100;
18731dbd01fSIzik Eidus 
18831dbd01fSIzik Eidus /* Milliseconds ksmd should sleep between batches */
1892ffd8679SHugh Dickins static unsigned int ksm_thread_sleep_millisecs = 20;
19031dbd01fSIzik Eidus 
19131dbd01fSIzik Eidus #define KSM_RUN_STOP	0
19231dbd01fSIzik Eidus #define KSM_RUN_MERGE	1
19331dbd01fSIzik Eidus #define KSM_RUN_UNMERGE	2
1942c6854fdSIzik Eidus static unsigned int ksm_run = KSM_RUN_STOP;
19531dbd01fSIzik Eidus 
19631dbd01fSIzik Eidus static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
19731dbd01fSIzik Eidus static DEFINE_MUTEX(ksm_thread_mutex);
19831dbd01fSIzik Eidus static DEFINE_SPINLOCK(ksm_mmlist_lock);
19931dbd01fSIzik Eidus 
20031dbd01fSIzik Eidus #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
20131dbd01fSIzik Eidus 		sizeof(struct __struct), __alignof__(struct __struct),\
20231dbd01fSIzik Eidus 		(__flags), NULL)
20331dbd01fSIzik Eidus 
20431dbd01fSIzik Eidus static int __init ksm_slab_init(void)
20531dbd01fSIzik Eidus {
20631dbd01fSIzik Eidus 	rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
20731dbd01fSIzik Eidus 	if (!rmap_item_cache)
20831dbd01fSIzik Eidus 		goto out;
20931dbd01fSIzik Eidus 
2107b6ba2c7SHugh Dickins 	stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
2117b6ba2c7SHugh Dickins 	if (!stable_node_cache)
2127b6ba2c7SHugh Dickins 		goto out_free1;
2137b6ba2c7SHugh Dickins 
21431dbd01fSIzik Eidus 	mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
21531dbd01fSIzik Eidus 	if (!mm_slot_cache)
2167b6ba2c7SHugh Dickins 		goto out_free2;
21731dbd01fSIzik Eidus 
21831dbd01fSIzik Eidus 	return 0;
21931dbd01fSIzik Eidus 
2207b6ba2c7SHugh Dickins out_free2:
2217b6ba2c7SHugh Dickins 	kmem_cache_destroy(stable_node_cache);
2227b6ba2c7SHugh Dickins out_free1:
22331dbd01fSIzik Eidus 	kmem_cache_destroy(rmap_item_cache);
22431dbd01fSIzik Eidus out:
22531dbd01fSIzik Eidus 	return -ENOMEM;
22631dbd01fSIzik Eidus }
22731dbd01fSIzik Eidus 
22831dbd01fSIzik Eidus static void __init ksm_slab_free(void)
22931dbd01fSIzik Eidus {
23031dbd01fSIzik Eidus 	kmem_cache_destroy(mm_slot_cache);
2317b6ba2c7SHugh Dickins 	kmem_cache_destroy(stable_node_cache);
23231dbd01fSIzik Eidus 	kmem_cache_destroy(rmap_item_cache);
23331dbd01fSIzik Eidus 	mm_slot_cache = NULL;
23431dbd01fSIzik Eidus }
23531dbd01fSIzik Eidus 
23631dbd01fSIzik Eidus static inline struct rmap_item *alloc_rmap_item(void)
23731dbd01fSIzik Eidus {
238473b0ce4SHugh Dickins 	struct rmap_item *rmap_item;
239473b0ce4SHugh Dickins 
240473b0ce4SHugh Dickins 	rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL);
241473b0ce4SHugh Dickins 	if (rmap_item)
242473b0ce4SHugh Dickins 		ksm_rmap_items++;
243473b0ce4SHugh Dickins 	return rmap_item;
24431dbd01fSIzik Eidus }
24531dbd01fSIzik Eidus 
24631dbd01fSIzik Eidus static inline void free_rmap_item(struct rmap_item *rmap_item)
24731dbd01fSIzik Eidus {
248473b0ce4SHugh Dickins 	ksm_rmap_items--;
24931dbd01fSIzik Eidus 	rmap_item->mm = NULL;	/* debug safety */
25031dbd01fSIzik Eidus 	kmem_cache_free(rmap_item_cache, rmap_item);
25131dbd01fSIzik Eidus }
25231dbd01fSIzik Eidus 
2537b6ba2c7SHugh Dickins static inline struct stable_node *alloc_stable_node(void)
2547b6ba2c7SHugh Dickins {
2557b6ba2c7SHugh Dickins 	return kmem_cache_alloc(stable_node_cache, GFP_KERNEL);
2567b6ba2c7SHugh Dickins }
2577b6ba2c7SHugh Dickins 
2587b6ba2c7SHugh Dickins static inline void free_stable_node(struct stable_node *stable_node)
2597b6ba2c7SHugh Dickins {
2607b6ba2c7SHugh Dickins 	kmem_cache_free(stable_node_cache, stable_node);
2617b6ba2c7SHugh Dickins }
2627b6ba2c7SHugh Dickins 
26331dbd01fSIzik Eidus static inline struct mm_slot *alloc_mm_slot(void)
26431dbd01fSIzik Eidus {
26531dbd01fSIzik Eidus 	if (!mm_slot_cache)	/* initialization failed */
26631dbd01fSIzik Eidus 		return NULL;
26731dbd01fSIzik Eidus 	return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
26831dbd01fSIzik Eidus }
26931dbd01fSIzik Eidus 
27031dbd01fSIzik Eidus static inline void free_mm_slot(struct mm_slot *mm_slot)
27131dbd01fSIzik Eidus {
27231dbd01fSIzik Eidus 	kmem_cache_free(mm_slot_cache, mm_slot);
27331dbd01fSIzik Eidus }
27431dbd01fSIzik Eidus 
27531dbd01fSIzik Eidus static struct mm_slot *get_mm_slot(struct mm_struct *mm)
27631dbd01fSIzik Eidus {
27731dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
27831dbd01fSIzik Eidus 	struct hlist_head *bucket;
27931dbd01fSIzik Eidus 	struct hlist_node *node;
28031dbd01fSIzik Eidus 
281d9f8984cSLai Jiangshan 	bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)];
28231dbd01fSIzik Eidus 	hlist_for_each_entry(mm_slot, node, bucket, link) {
28331dbd01fSIzik Eidus 		if (mm == mm_slot->mm)
28431dbd01fSIzik Eidus 			return mm_slot;
28531dbd01fSIzik Eidus 	}
28631dbd01fSIzik Eidus 	return NULL;
28731dbd01fSIzik Eidus }
28831dbd01fSIzik Eidus 
28931dbd01fSIzik Eidus static void insert_to_mm_slots_hash(struct mm_struct *mm,
29031dbd01fSIzik Eidus 				    struct mm_slot *mm_slot)
29131dbd01fSIzik Eidus {
29231dbd01fSIzik Eidus 	struct hlist_head *bucket;
29331dbd01fSIzik Eidus 
294d9f8984cSLai Jiangshan 	bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)];
29531dbd01fSIzik Eidus 	mm_slot->mm = mm;
29631dbd01fSIzik Eidus 	hlist_add_head(&mm_slot->link, bucket);
29731dbd01fSIzik Eidus }
29831dbd01fSIzik Eidus 
29931dbd01fSIzik Eidus static inline int in_stable_tree(struct rmap_item *rmap_item)
30031dbd01fSIzik Eidus {
30131dbd01fSIzik Eidus 	return rmap_item->address & STABLE_FLAG;
30231dbd01fSIzik Eidus }
30331dbd01fSIzik Eidus 
30431dbd01fSIzik Eidus /*
305a913e182SHugh Dickins  * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
306a913e182SHugh Dickins  * page tables after it has passed through ksm_exit() - which, if necessary,
307a913e182SHugh Dickins  * takes mmap_sem briefly to serialize against them.  ksm_exit() does not set
308a913e182SHugh Dickins  * a special flag: they can just back out as soon as mm_users goes to zero.
309a913e182SHugh Dickins  * ksm_test_exit() is used throughout to make this test for exit: in some
310a913e182SHugh Dickins  * places for correctness, in some places just to avoid unnecessary work.
311a913e182SHugh Dickins  */
312a913e182SHugh Dickins static inline bool ksm_test_exit(struct mm_struct *mm)
313a913e182SHugh Dickins {
314a913e182SHugh Dickins 	return atomic_read(&mm->mm_users) == 0;
315a913e182SHugh Dickins }
316a913e182SHugh Dickins 
317a913e182SHugh Dickins /*
31831dbd01fSIzik Eidus  * We use break_ksm to break COW on a ksm page: it's a stripped down
31931dbd01fSIzik Eidus  *
32031dbd01fSIzik Eidus  *	if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
32131dbd01fSIzik Eidus  *		put_page(page);
32231dbd01fSIzik Eidus  *
32331dbd01fSIzik Eidus  * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
32431dbd01fSIzik Eidus  * in case the application has unmapped and remapped mm,addr meanwhile.
32531dbd01fSIzik Eidus  * Could a ksm page appear anywhere else?  Actually yes, in a VM_PFNMAP
32631dbd01fSIzik Eidus  * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
32731dbd01fSIzik Eidus  */
328d952b791SHugh Dickins static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
32931dbd01fSIzik Eidus {
33031dbd01fSIzik Eidus 	struct page *page;
331d952b791SHugh Dickins 	int ret = 0;
33231dbd01fSIzik Eidus 
33331dbd01fSIzik Eidus 	do {
33431dbd01fSIzik Eidus 		cond_resched();
33531dbd01fSIzik Eidus 		page = follow_page(vma, addr, FOLL_GET);
33622eccdd7SDan Carpenter 		if (IS_ERR_OR_NULL(page))
33731dbd01fSIzik Eidus 			break;
33831dbd01fSIzik Eidus 		if (PageKsm(page))
33931dbd01fSIzik Eidus 			ret = handle_mm_fault(vma->vm_mm, vma, addr,
34031dbd01fSIzik Eidus 							FAULT_FLAG_WRITE);
34131dbd01fSIzik Eidus 		else
34231dbd01fSIzik Eidus 			ret = VM_FAULT_WRITE;
34331dbd01fSIzik Eidus 		put_page(page);
344d952b791SHugh Dickins 	} while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM)));
345d952b791SHugh Dickins 	/*
346d952b791SHugh Dickins 	 * We must loop because handle_mm_fault() may back out if there's
347d952b791SHugh Dickins 	 * any difficulty e.g. if pte accessed bit gets updated concurrently.
348d952b791SHugh Dickins 	 *
349d952b791SHugh Dickins 	 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
350d952b791SHugh Dickins 	 * COW has been broken, even if the vma does not permit VM_WRITE;
351d952b791SHugh Dickins 	 * but note that a concurrent fault might break PageKsm for us.
352d952b791SHugh Dickins 	 *
353d952b791SHugh Dickins 	 * VM_FAULT_SIGBUS could occur if we race with truncation of the
354d952b791SHugh Dickins 	 * backing file, which also invalidates anonymous pages: that's
355d952b791SHugh Dickins 	 * okay, that truncation will have unmapped the PageKsm for us.
356d952b791SHugh Dickins 	 *
357d952b791SHugh Dickins 	 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
358d952b791SHugh Dickins 	 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
359d952b791SHugh Dickins 	 * current task has TIF_MEMDIE set, and will be OOM killed on return
360d952b791SHugh Dickins 	 * to user; and ksmd, having no mm, would never be chosen for that.
361d952b791SHugh Dickins 	 *
362d952b791SHugh Dickins 	 * But if the mm is in a limited mem_cgroup, then the fault may fail
363d952b791SHugh Dickins 	 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
364d952b791SHugh Dickins 	 * even ksmd can fail in this way - though it's usually breaking ksm
365d952b791SHugh Dickins 	 * just to undo a merge it made a moment before, so unlikely to oom.
366d952b791SHugh Dickins 	 *
367d952b791SHugh Dickins 	 * That's a pity: we might therefore have more kernel pages allocated
368d952b791SHugh Dickins 	 * than we're counting as nodes in the stable tree; but ksm_do_scan
369d952b791SHugh Dickins 	 * will retry to break_cow on each pass, so should recover the page
370d952b791SHugh Dickins 	 * in due course.  The important thing is to not let VM_MERGEABLE
371d952b791SHugh Dickins 	 * be cleared while any such pages might remain in the area.
372d952b791SHugh Dickins 	 */
373d952b791SHugh Dickins 	return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
37431dbd01fSIzik Eidus }
37531dbd01fSIzik Eidus 
3768dd3557aSHugh Dickins static void break_cow(struct rmap_item *rmap_item)
37731dbd01fSIzik Eidus {
3788dd3557aSHugh Dickins 	struct mm_struct *mm = rmap_item->mm;
3798dd3557aSHugh Dickins 	unsigned long addr = rmap_item->address;
38031dbd01fSIzik Eidus 	struct vm_area_struct *vma;
38131dbd01fSIzik Eidus 
3824035c07aSHugh Dickins 	/*
3834035c07aSHugh Dickins 	 * It is not an accident that whenever we want to break COW
3844035c07aSHugh Dickins 	 * to undo, we also need to drop a reference to the anon_vma.
3854035c07aSHugh Dickins 	 */
3869e60109fSPeter Zijlstra 	put_anon_vma(rmap_item->anon_vma);
3874035c07aSHugh Dickins 
38881464e30SHugh Dickins 	down_read(&mm->mmap_sem);
3899ba69294SHugh Dickins 	if (ksm_test_exit(mm))
3909ba69294SHugh Dickins 		goto out;
39131dbd01fSIzik Eidus 	vma = find_vma(mm, addr);
39231dbd01fSIzik Eidus 	if (!vma || vma->vm_start > addr)
39381464e30SHugh Dickins 		goto out;
39431dbd01fSIzik Eidus 	if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
39581464e30SHugh Dickins 		goto out;
39631dbd01fSIzik Eidus 	break_ksm(vma, addr);
39781464e30SHugh Dickins out:
39831dbd01fSIzik Eidus 	up_read(&mm->mmap_sem);
39931dbd01fSIzik Eidus }
40031dbd01fSIzik Eidus 
40129ad768cSAndrea Arcangeli static struct page *page_trans_compound_anon(struct page *page)
40229ad768cSAndrea Arcangeli {
40329ad768cSAndrea Arcangeli 	if (PageTransCompound(page)) {
40422e5c47eSAndrea Arcangeli 		struct page *head = compound_trans_head(page);
40529ad768cSAndrea Arcangeli 		/*
40622e5c47eSAndrea Arcangeli 		 * head may actually be splitted and freed from under
40722e5c47eSAndrea Arcangeli 		 * us but it's ok here.
40829ad768cSAndrea Arcangeli 		 */
40929ad768cSAndrea Arcangeli 		if (PageAnon(head))
41029ad768cSAndrea Arcangeli 			return head;
41129ad768cSAndrea Arcangeli 	}
41229ad768cSAndrea Arcangeli 	return NULL;
41329ad768cSAndrea Arcangeli }
41429ad768cSAndrea Arcangeli 
41531dbd01fSIzik Eidus static struct page *get_mergeable_page(struct rmap_item *rmap_item)
41631dbd01fSIzik Eidus {
41731dbd01fSIzik Eidus 	struct mm_struct *mm = rmap_item->mm;
41831dbd01fSIzik Eidus 	unsigned long addr = rmap_item->address;
41931dbd01fSIzik Eidus 	struct vm_area_struct *vma;
42031dbd01fSIzik Eidus 	struct page *page;
42131dbd01fSIzik Eidus 
42231dbd01fSIzik Eidus 	down_read(&mm->mmap_sem);
4239ba69294SHugh Dickins 	if (ksm_test_exit(mm))
4249ba69294SHugh Dickins 		goto out;
42531dbd01fSIzik Eidus 	vma = find_vma(mm, addr);
42631dbd01fSIzik Eidus 	if (!vma || vma->vm_start > addr)
42731dbd01fSIzik Eidus 		goto out;
42831dbd01fSIzik Eidus 	if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
42931dbd01fSIzik Eidus 		goto out;
43031dbd01fSIzik Eidus 
43131dbd01fSIzik Eidus 	page = follow_page(vma, addr, FOLL_GET);
43222eccdd7SDan Carpenter 	if (IS_ERR_OR_NULL(page))
43331dbd01fSIzik Eidus 		goto out;
43429ad768cSAndrea Arcangeli 	if (PageAnon(page) || page_trans_compound_anon(page)) {
43531dbd01fSIzik Eidus 		flush_anon_page(vma, page, addr);
43631dbd01fSIzik Eidus 		flush_dcache_page(page);
43731dbd01fSIzik Eidus 	} else {
43831dbd01fSIzik Eidus 		put_page(page);
43931dbd01fSIzik Eidus out:		page = NULL;
44031dbd01fSIzik Eidus 	}
44131dbd01fSIzik Eidus 	up_read(&mm->mmap_sem);
44231dbd01fSIzik Eidus 	return page;
44331dbd01fSIzik Eidus }
44431dbd01fSIzik Eidus 
4454035c07aSHugh Dickins static void remove_node_from_stable_tree(struct stable_node *stable_node)
4464035c07aSHugh Dickins {
4474035c07aSHugh Dickins 	struct rmap_item *rmap_item;
4484035c07aSHugh Dickins 	struct hlist_node *hlist;
4494035c07aSHugh Dickins 
4504035c07aSHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
4514035c07aSHugh Dickins 		if (rmap_item->hlist.next)
4524035c07aSHugh Dickins 			ksm_pages_sharing--;
4534035c07aSHugh Dickins 		else
4544035c07aSHugh Dickins 			ksm_pages_shared--;
4559e60109fSPeter Zijlstra 		put_anon_vma(rmap_item->anon_vma);
4564035c07aSHugh Dickins 		rmap_item->address &= PAGE_MASK;
4574035c07aSHugh Dickins 		cond_resched();
4584035c07aSHugh Dickins 	}
4594035c07aSHugh Dickins 
4604035c07aSHugh Dickins 	rb_erase(&stable_node->node, &root_stable_tree);
4614035c07aSHugh Dickins 	free_stable_node(stable_node);
4624035c07aSHugh Dickins }
4634035c07aSHugh Dickins 
4644035c07aSHugh Dickins /*
4654035c07aSHugh Dickins  * get_ksm_page: checks if the page indicated by the stable node
4664035c07aSHugh Dickins  * is still its ksm page, despite having held no reference to it.
4674035c07aSHugh Dickins  * In which case we can trust the content of the page, and it
4684035c07aSHugh Dickins  * returns the gotten page; but if the page has now been zapped,
4694035c07aSHugh Dickins  * remove the stale node from the stable tree and return NULL.
4704035c07aSHugh Dickins  *
4714035c07aSHugh Dickins  * You would expect the stable_node to hold a reference to the ksm page.
4724035c07aSHugh Dickins  * But if it increments the page's count, swapping out has to wait for
4734035c07aSHugh Dickins  * ksmd to come around again before it can free the page, which may take
4744035c07aSHugh Dickins  * seconds or even minutes: much too unresponsive.  So instead we use a
4754035c07aSHugh Dickins  * "keyhole reference": access to the ksm page from the stable node peeps
4764035c07aSHugh Dickins  * out through its keyhole to see if that page still holds the right key,
4774035c07aSHugh Dickins  * pointing back to this stable node.  This relies on freeing a PageAnon
4784035c07aSHugh Dickins  * page to reset its page->mapping to NULL, and relies on no other use of
4794035c07aSHugh Dickins  * a page to put something that might look like our key in page->mapping.
4804035c07aSHugh Dickins  *
4814035c07aSHugh Dickins  * include/linux/pagemap.h page_cache_get_speculative() is a good reference,
4824035c07aSHugh Dickins  * but this is different - made simpler by ksm_thread_mutex being held, but
4834035c07aSHugh Dickins  * interesting for assuming that no other use of the struct page could ever
4844035c07aSHugh Dickins  * put our expected_mapping into page->mapping (or a field of the union which
4854035c07aSHugh Dickins  * coincides with page->mapping).  The RCU calls are not for KSM at all, but
4864035c07aSHugh Dickins  * to keep the page_count protocol described with page_cache_get_speculative.
4874035c07aSHugh Dickins  *
4884035c07aSHugh Dickins  * Note: it is possible that get_ksm_page() will return NULL one moment,
4894035c07aSHugh Dickins  * then page the next, if the page is in between page_freeze_refs() and
4904035c07aSHugh Dickins  * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page
4914035c07aSHugh Dickins  * is on its way to being freed; but it is an anomaly to bear in mind.
4924035c07aSHugh Dickins  */
4934035c07aSHugh Dickins static struct page *get_ksm_page(struct stable_node *stable_node)
4944035c07aSHugh Dickins {
4954035c07aSHugh Dickins 	struct page *page;
4964035c07aSHugh Dickins 	void *expected_mapping;
4974035c07aSHugh Dickins 
49862b61f61SHugh Dickins 	page = pfn_to_page(stable_node->kpfn);
4994035c07aSHugh Dickins 	expected_mapping = (void *)stable_node +
5004035c07aSHugh Dickins 				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
5014035c07aSHugh Dickins 	rcu_read_lock();
5024035c07aSHugh Dickins 	if (page->mapping != expected_mapping)
5034035c07aSHugh Dickins 		goto stale;
5044035c07aSHugh Dickins 	if (!get_page_unless_zero(page))
5054035c07aSHugh Dickins 		goto stale;
5064035c07aSHugh Dickins 	if (page->mapping != expected_mapping) {
5074035c07aSHugh Dickins 		put_page(page);
5084035c07aSHugh Dickins 		goto stale;
5094035c07aSHugh Dickins 	}
5104035c07aSHugh Dickins 	rcu_read_unlock();
5114035c07aSHugh Dickins 	return page;
5124035c07aSHugh Dickins stale:
5134035c07aSHugh Dickins 	rcu_read_unlock();
5144035c07aSHugh Dickins 	remove_node_from_stable_tree(stable_node);
5154035c07aSHugh Dickins 	return NULL;
5164035c07aSHugh Dickins }
5174035c07aSHugh Dickins 
51831dbd01fSIzik Eidus /*
51931dbd01fSIzik Eidus  * Removing rmap_item from stable or unstable tree.
52031dbd01fSIzik Eidus  * This function will clean the information from the stable/unstable tree.
52131dbd01fSIzik Eidus  */
52231dbd01fSIzik Eidus static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
52331dbd01fSIzik Eidus {
5247b6ba2c7SHugh Dickins 	if (rmap_item->address & STABLE_FLAG) {
5257b6ba2c7SHugh Dickins 		struct stable_node *stable_node;
5265ad64688SHugh Dickins 		struct page *page;
52731dbd01fSIzik Eidus 
5287b6ba2c7SHugh Dickins 		stable_node = rmap_item->head;
5294035c07aSHugh Dickins 		page = get_ksm_page(stable_node);
5304035c07aSHugh Dickins 		if (!page)
5314035c07aSHugh Dickins 			goto out;
5325ad64688SHugh Dickins 
5334035c07aSHugh Dickins 		lock_page(page);
5347b6ba2c7SHugh Dickins 		hlist_del(&rmap_item->hlist);
5355ad64688SHugh Dickins 		unlock_page(page);
5365ad64688SHugh Dickins 		put_page(page);
53708beca44SHugh Dickins 
5384035c07aSHugh Dickins 		if (stable_node->hlist.first)
5394035c07aSHugh Dickins 			ksm_pages_sharing--;
5404035c07aSHugh Dickins 		else
541b4028260SHugh Dickins 			ksm_pages_shared--;
54231dbd01fSIzik Eidus 
5439e60109fSPeter Zijlstra 		put_anon_vma(rmap_item->anon_vma);
54493d17715SHugh Dickins 		rmap_item->address &= PAGE_MASK;
54531dbd01fSIzik Eidus 
5467b6ba2c7SHugh Dickins 	} else if (rmap_item->address & UNSTABLE_FLAG) {
54731dbd01fSIzik Eidus 		unsigned char age;
54831dbd01fSIzik Eidus 		/*
5499ba69294SHugh Dickins 		 * Usually ksmd can and must skip the rb_erase, because
55031dbd01fSIzik Eidus 		 * root_unstable_tree was already reset to RB_ROOT.
5519ba69294SHugh Dickins 		 * But be careful when an mm is exiting: do the rb_erase
5529ba69294SHugh Dickins 		 * if this rmap_item was inserted by this scan, rather
5539ba69294SHugh Dickins 		 * than left over from before.
55431dbd01fSIzik Eidus 		 */
55531dbd01fSIzik Eidus 		age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
556cd551f97SHugh Dickins 		BUG_ON(age > 1);
55731dbd01fSIzik Eidus 		if (!age)
55831dbd01fSIzik Eidus 			rb_erase(&rmap_item->node, &root_unstable_tree);
55931dbd01fSIzik Eidus 
56093d17715SHugh Dickins 		ksm_pages_unshared--;
56131dbd01fSIzik Eidus 		rmap_item->address &= PAGE_MASK;
56293d17715SHugh Dickins 	}
5634035c07aSHugh Dickins out:
56431dbd01fSIzik Eidus 	cond_resched();		/* we're called from many long loops */
56531dbd01fSIzik Eidus }
56631dbd01fSIzik Eidus 
56731dbd01fSIzik Eidus static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
5686514d511SHugh Dickins 				       struct rmap_item **rmap_list)
56931dbd01fSIzik Eidus {
5706514d511SHugh Dickins 	while (*rmap_list) {
5716514d511SHugh Dickins 		struct rmap_item *rmap_item = *rmap_list;
5726514d511SHugh Dickins 		*rmap_list = rmap_item->rmap_list;
57331dbd01fSIzik Eidus 		remove_rmap_item_from_tree(rmap_item);
57431dbd01fSIzik Eidus 		free_rmap_item(rmap_item);
57531dbd01fSIzik Eidus 	}
57631dbd01fSIzik Eidus }
57731dbd01fSIzik Eidus 
57831dbd01fSIzik Eidus /*
57931dbd01fSIzik Eidus  * Though it's very tempting to unmerge in_stable_tree(rmap_item)s rather
58031dbd01fSIzik Eidus  * than check every pte of a given vma, the locking doesn't quite work for
58131dbd01fSIzik Eidus  * that - an rmap_item is assigned to the stable tree after inserting ksm
58231dbd01fSIzik Eidus  * page and upping mmap_sem.  Nor does it fit with the way we skip dup'ing
58331dbd01fSIzik Eidus  * rmap_items from parent to child at fork time (so as not to waste time
58431dbd01fSIzik Eidus  * if exit comes before the next scan reaches it).
58581464e30SHugh Dickins  *
58681464e30SHugh Dickins  * Similarly, although we'd like to remove rmap_items (so updating counts
58781464e30SHugh Dickins  * and freeing memory) when unmerging an area, it's easier to leave that
58881464e30SHugh Dickins  * to the next pass of ksmd - consider, for example, how ksmd might be
58981464e30SHugh Dickins  * in cmp_and_merge_page on one of the rmap_items we would be removing.
59031dbd01fSIzik Eidus  */
591d952b791SHugh Dickins static int unmerge_ksm_pages(struct vm_area_struct *vma,
59231dbd01fSIzik Eidus 			     unsigned long start, unsigned long end)
59331dbd01fSIzik Eidus {
59431dbd01fSIzik Eidus 	unsigned long addr;
595d952b791SHugh Dickins 	int err = 0;
59631dbd01fSIzik Eidus 
597d952b791SHugh Dickins 	for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
5989ba69294SHugh Dickins 		if (ksm_test_exit(vma->vm_mm))
5999ba69294SHugh Dickins 			break;
600d952b791SHugh Dickins 		if (signal_pending(current))
601d952b791SHugh Dickins 			err = -ERESTARTSYS;
602d952b791SHugh Dickins 		else
603d952b791SHugh Dickins 			err = break_ksm(vma, addr);
604d952b791SHugh Dickins 	}
605d952b791SHugh Dickins 	return err;
60631dbd01fSIzik Eidus }
60731dbd01fSIzik Eidus 
6082ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
6092ffd8679SHugh Dickins /*
6102ffd8679SHugh Dickins  * Only called through the sysfs control interface:
6112ffd8679SHugh Dickins  */
612d952b791SHugh Dickins static int unmerge_and_remove_all_rmap_items(void)
61331dbd01fSIzik Eidus {
61431dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
61531dbd01fSIzik Eidus 	struct mm_struct *mm;
61631dbd01fSIzik Eidus 	struct vm_area_struct *vma;
617d952b791SHugh Dickins 	int err = 0;
61831dbd01fSIzik Eidus 
619d952b791SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
6209ba69294SHugh Dickins 	ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
621d952b791SHugh Dickins 						struct mm_slot, mm_list);
622d952b791SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
623d952b791SHugh Dickins 
6249ba69294SHugh Dickins 	for (mm_slot = ksm_scan.mm_slot;
6259ba69294SHugh Dickins 			mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
62631dbd01fSIzik Eidus 		mm = mm_slot->mm;
62731dbd01fSIzik Eidus 		down_read(&mm->mmap_sem);
62831dbd01fSIzik Eidus 		for (vma = mm->mmap; vma; vma = vma->vm_next) {
6299ba69294SHugh Dickins 			if (ksm_test_exit(mm))
6309ba69294SHugh Dickins 				break;
63131dbd01fSIzik Eidus 			if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
63231dbd01fSIzik Eidus 				continue;
633d952b791SHugh Dickins 			err = unmerge_ksm_pages(vma,
634d952b791SHugh Dickins 						vma->vm_start, vma->vm_end);
6359ba69294SHugh Dickins 			if (err)
6369ba69294SHugh Dickins 				goto error;
637d952b791SHugh Dickins 		}
6389ba69294SHugh Dickins 
6396514d511SHugh Dickins 		remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
64031dbd01fSIzik Eidus 
64131dbd01fSIzik Eidus 		spin_lock(&ksm_mmlist_lock);
6429ba69294SHugh Dickins 		ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
643d952b791SHugh Dickins 						struct mm_slot, mm_list);
6449ba69294SHugh Dickins 		if (ksm_test_exit(mm)) {
6459ba69294SHugh Dickins 			hlist_del(&mm_slot->link);
6469ba69294SHugh Dickins 			list_del(&mm_slot->mm_list);
64731dbd01fSIzik Eidus 			spin_unlock(&ksm_mmlist_lock);
6489ba69294SHugh Dickins 
6499ba69294SHugh Dickins 			free_mm_slot(mm_slot);
6509ba69294SHugh Dickins 			clear_bit(MMF_VM_MERGEABLE, &mm->flags);
6519ba69294SHugh Dickins 			up_read(&mm->mmap_sem);
6529ba69294SHugh Dickins 			mmdrop(mm);
6539ba69294SHugh Dickins 		} else {
6549ba69294SHugh Dickins 			spin_unlock(&ksm_mmlist_lock);
6559ba69294SHugh Dickins 			up_read(&mm->mmap_sem);
6569ba69294SHugh Dickins 		}
65731dbd01fSIzik Eidus 	}
65831dbd01fSIzik Eidus 
659d952b791SHugh Dickins 	ksm_scan.seqnr = 0;
6609ba69294SHugh Dickins 	return 0;
6619ba69294SHugh Dickins 
6629ba69294SHugh Dickins error:
6639ba69294SHugh Dickins 	up_read(&mm->mmap_sem);
664d952b791SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
665d952b791SHugh Dickins 	ksm_scan.mm_slot = &ksm_mm_head;
666d952b791SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
667d952b791SHugh Dickins 	return err;
668d952b791SHugh Dickins }
6692ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
670d952b791SHugh Dickins 
67131dbd01fSIzik Eidus static u32 calc_checksum(struct page *page)
67231dbd01fSIzik Eidus {
67331dbd01fSIzik Eidus 	u32 checksum;
67431dbd01fSIzik Eidus 	void *addr = kmap_atomic(page, KM_USER0);
67531dbd01fSIzik Eidus 	checksum = jhash2(addr, PAGE_SIZE / 4, 17);
67631dbd01fSIzik Eidus 	kunmap_atomic(addr, KM_USER0);
67731dbd01fSIzik Eidus 	return checksum;
67831dbd01fSIzik Eidus }
67931dbd01fSIzik Eidus 
68031dbd01fSIzik Eidus static int memcmp_pages(struct page *page1, struct page *page2)
68131dbd01fSIzik Eidus {
68231dbd01fSIzik Eidus 	char *addr1, *addr2;
68331dbd01fSIzik Eidus 	int ret;
68431dbd01fSIzik Eidus 
68531dbd01fSIzik Eidus 	addr1 = kmap_atomic(page1, KM_USER0);
68631dbd01fSIzik Eidus 	addr2 = kmap_atomic(page2, KM_USER1);
68731dbd01fSIzik Eidus 	ret = memcmp(addr1, addr2, PAGE_SIZE);
68831dbd01fSIzik Eidus 	kunmap_atomic(addr2, KM_USER1);
68931dbd01fSIzik Eidus 	kunmap_atomic(addr1, KM_USER0);
69031dbd01fSIzik Eidus 	return ret;
69131dbd01fSIzik Eidus }
69231dbd01fSIzik Eidus 
69331dbd01fSIzik Eidus static inline int pages_identical(struct page *page1, struct page *page2)
69431dbd01fSIzik Eidus {
69531dbd01fSIzik Eidus 	return !memcmp_pages(page1, page2);
69631dbd01fSIzik Eidus }
69731dbd01fSIzik Eidus 
69831dbd01fSIzik Eidus static int write_protect_page(struct vm_area_struct *vma, struct page *page,
69931dbd01fSIzik Eidus 			      pte_t *orig_pte)
70031dbd01fSIzik Eidus {
70131dbd01fSIzik Eidus 	struct mm_struct *mm = vma->vm_mm;
70231dbd01fSIzik Eidus 	unsigned long addr;
70331dbd01fSIzik Eidus 	pte_t *ptep;
70431dbd01fSIzik Eidus 	spinlock_t *ptl;
70531dbd01fSIzik Eidus 	int swapped;
70631dbd01fSIzik Eidus 	int err = -EFAULT;
70731dbd01fSIzik Eidus 
70831dbd01fSIzik Eidus 	addr = page_address_in_vma(page, vma);
70931dbd01fSIzik Eidus 	if (addr == -EFAULT)
71031dbd01fSIzik Eidus 		goto out;
71131dbd01fSIzik Eidus 
71229ad768cSAndrea Arcangeli 	BUG_ON(PageTransCompound(page));
71331dbd01fSIzik Eidus 	ptep = page_check_address(page, mm, addr, &ptl, 0);
71431dbd01fSIzik Eidus 	if (!ptep)
71531dbd01fSIzik Eidus 		goto out;
71631dbd01fSIzik Eidus 
7174e31635cSHugh Dickins 	if (pte_write(*ptep) || pte_dirty(*ptep)) {
71831dbd01fSIzik Eidus 		pte_t entry;
71931dbd01fSIzik Eidus 
72031dbd01fSIzik Eidus 		swapped = PageSwapCache(page);
72131dbd01fSIzik Eidus 		flush_cache_page(vma, addr, page_to_pfn(page));
72231dbd01fSIzik Eidus 		/*
723*25985edcSLucas De Marchi 		 * Ok this is tricky, when get_user_pages_fast() run it doesn't
72431dbd01fSIzik Eidus 		 * take any lock, therefore the check that we are going to make
72531dbd01fSIzik Eidus 		 * with the pagecount against the mapcount is racey and
72631dbd01fSIzik Eidus 		 * O_DIRECT can happen right after the check.
72731dbd01fSIzik Eidus 		 * So we clear the pte and flush the tlb before the check
72831dbd01fSIzik Eidus 		 * this assure us that no O_DIRECT can happen after the check
72931dbd01fSIzik Eidus 		 * or in the middle of the check.
73031dbd01fSIzik Eidus 		 */
73131dbd01fSIzik Eidus 		entry = ptep_clear_flush(vma, addr, ptep);
73231dbd01fSIzik Eidus 		/*
73331dbd01fSIzik Eidus 		 * Check that no O_DIRECT or similar I/O is in progress on the
73431dbd01fSIzik Eidus 		 * page
73531dbd01fSIzik Eidus 		 */
73631e855eaSHugh Dickins 		if (page_mapcount(page) + 1 + swapped != page_count(page)) {
737cb532375SRobin Holt 			set_pte_at(mm, addr, ptep, entry);
73831dbd01fSIzik Eidus 			goto out_unlock;
73931dbd01fSIzik Eidus 		}
7404e31635cSHugh Dickins 		if (pte_dirty(entry))
7414e31635cSHugh Dickins 			set_page_dirty(page);
7424e31635cSHugh Dickins 		entry = pte_mkclean(pte_wrprotect(entry));
74331dbd01fSIzik Eidus 		set_pte_at_notify(mm, addr, ptep, entry);
74431dbd01fSIzik Eidus 	}
74531dbd01fSIzik Eidus 	*orig_pte = *ptep;
74631dbd01fSIzik Eidus 	err = 0;
74731dbd01fSIzik Eidus 
74831dbd01fSIzik Eidus out_unlock:
74931dbd01fSIzik Eidus 	pte_unmap_unlock(ptep, ptl);
75031dbd01fSIzik Eidus out:
75131dbd01fSIzik Eidus 	return err;
75231dbd01fSIzik Eidus }
75331dbd01fSIzik Eidus 
75431dbd01fSIzik Eidus /**
75531dbd01fSIzik Eidus  * replace_page - replace page in vma by new ksm page
7568dd3557aSHugh Dickins  * @vma:      vma that holds the pte pointing to page
7578dd3557aSHugh Dickins  * @page:     the page we are replacing by kpage
7588dd3557aSHugh Dickins  * @kpage:    the ksm page we replace page by
75931dbd01fSIzik Eidus  * @orig_pte: the original value of the pte
76031dbd01fSIzik Eidus  *
76131dbd01fSIzik Eidus  * Returns 0 on success, -EFAULT on failure.
76231dbd01fSIzik Eidus  */
7638dd3557aSHugh Dickins static int replace_page(struct vm_area_struct *vma, struct page *page,
7648dd3557aSHugh Dickins 			struct page *kpage, pte_t orig_pte)
76531dbd01fSIzik Eidus {
76631dbd01fSIzik Eidus 	struct mm_struct *mm = vma->vm_mm;
76731dbd01fSIzik Eidus 	pgd_t *pgd;
76831dbd01fSIzik Eidus 	pud_t *pud;
76931dbd01fSIzik Eidus 	pmd_t *pmd;
77031dbd01fSIzik Eidus 	pte_t *ptep;
77131dbd01fSIzik Eidus 	spinlock_t *ptl;
77231dbd01fSIzik Eidus 	unsigned long addr;
77331dbd01fSIzik Eidus 	int err = -EFAULT;
77431dbd01fSIzik Eidus 
7758dd3557aSHugh Dickins 	addr = page_address_in_vma(page, vma);
77631dbd01fSIzik Eidus 	if (addr == -EFAULT)
77731dbd01fSIzik Eidus 		goto out;
77831dbd01fSIzik Eidus 
77931dbd01fSIzik Eidus 	pgd = pgd_offset(mm, addr);
78031dbd01fSIzik Eidus 	if (!pgd_present(*pgd))
78131dbd01fSIzik Eidus 		goto out;
78231dbd01fSIzik Eidus 
78331dbd01fSIzik Eidus 	pud = pud_offset(pgd, addr);
78431dbd01fSIzik Eidus 	if (!pud_present(*pud))
78531dbd01fSIzik Eidus 		goto out;
78631dbd01fSIzik Eidus 
78731dbd01fSIzik Eidus 	pmd = pmd_offset(pud, addr);
78829ad768cSAndrea Arcangeli 	BUG_ON(pmd_trans_huge(*pmd));
78931dbd01fSIzik Eidus 	if (!pmd_present(*pmd))
79031dbd01fSIzik Eidus 		goto out;
79131dbd01fSIzik Eidus 
79231dbd01fSIzik Eidus 	ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
79331dbd01fSIzik Eidus 	if (!pte_same(*ptep, orig_pte)) {
79431dbd01fSIzik Eidus 		pte_unmap_unlock(ptep, ptl);
79531dbd01fSIzik Eidus 		goto out;
79631dbd01fSIzik Eidus 	}
79731dbd01fSIzik Eidus 
7988dd3557aSHugh Dickins 	get_page(kpage);
7995ad64688SHugh Dickins 	page_add_anon_rmap(kpage, vma, addr);
80031dbd01fSIzik Eidus 
80131dbd01fSIzik Eidus 	flush_cache_page(vma, addr, pte_pfn(*ptep));
80231dbd01fSIzik Eidus 	ptep_clear_flush(vma, addr, ptep);
8038dd3557aSHugh Dickins 	set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
80431dbd01fSIzik Eidus 
8058dd3557aSHugh Dickins 	page_remove_rmap(page);
806ae52a2adSHugh Dickins 	if (!page_mapped(page))
807ae52a2adSHugh Dickins 		try_to_free_swap(page);
8088dd3557aSHugh Dickins 	put_page(page);
80931dbd01fSIzik Eidus 
81031dbd01fSIzik Eidus 	pte_unmap_unlock(ptep, ptl);
81131dbd01fSIzik Eidus 	err = 0;
81231dbd01fSIzik Eidus out:
81331dbd01fSIzik Eidus 	return err;
81431dbd01fSIzik Eidus }
81531dbd01fSIzik Eidus 
81629ad768cSAndrea Arcangeli static int page_trans_compound_anon_split(struct page *page)
81729ad768cSAndrea Arcangeli {
81829ad768cSAndrea Arcangeli 	int ret = 0;
81929ad768cSAndrea Arcangeli 	struct page *transhuge_head = page_trans_compound_anon(page);
82029ad768cSAndrea Arcangeli 	if (transhuge_head) {
82129ad768cSAndrea Arcangeli 		/* Get the reference on the head to split it. */
82229ad768cSAndrea Arcangeli 		if (get_page_unless_zero(transhuge_head)) {
82329ad768cSAndrea Arcangeli 			/*
82429ad768cSAndrea Arcangeli 			 * Recheck we got the reference while the head
82529ad768cSAndrea Arcangeli 			 * was still anonymous.
82629ad768cSAndrea Arcangeli 			 */
82729ad768cSAndrea Arcangeli 			if (PageAnon(transhuge_head))
82829ad768cSAndrea Arcangeli 				ret = split_huge_page(transhuge_head);
82929ad768cSAndrea Arcangeli 			else
83029ad768cSAndrea Arcangeli 				/*
83129ad768cSAndrea Arcangeli 				 * Retry later if split_huge_page run
83229ad768cSAndrea Arcangeli 				 * from under us.
83329ad768cSAndrea Arcangeli 				 */
83429ad768cSAndrea Arcangeli 				ret = 1;
83529ad768cSAndrea Arcangeli 			put_page(transhuge_head);
83629ad768cSAndrea Arcangeli 		} else
83729ad768cSAndrea Arcangeli 			/* Retry later if split_huge_page run from under us. */
83829ad768cSAndrea Arcangeli 			ret = 1;
83929ad768cSAndrea Arcangeli 	}
84029ad768cSAndrea Arcangeli 	return ret;
84129ad768cSAndrea Arcangeli }
84229ad768cSAndrea Arcangeli 
84331dbd01fSIzik Eidus /*
84431dbd01fSIzik Eidus  * try_to_merge_one_page - take two pages and merge them into one
8458dd3557aSHugh Dickins  * @vma: the vma that holds the pte pointing to page
8468dd3557aSHugh Dickins  * @page: the PageAnon page that we want to replace with kpage
84780e14822SHugh Dickins  * @kpage: the PageKsm page that we want to map instead of page,
84880e14822SHugh Dickins  *         or NULL the first time when we want to use page as kpage.
84931dbd01fSIzik Eidus  *
85031dbd01fSIzik Eidus  * This function returns 0 if the pages were merged, -EFAULT otherwise.
85131dbd01fSIzik Eidus  */
85231dbd01fSIzik Eidus static int try_to_merge_one_page(struct vm_area_struct *vma,
8538dd3557aSHugh Dickins 				 struct page *page, struct page *kpage)
85431dbd01fSIzik Eidus {
85531dbd01fSIzik Eidus 	pte_t orig_pte = __pte(0);
85631dbd01fSIzik Eidus 	int err = -EFAULT;
85731dbd01fSIzik Eidus 
858db114b83SHugh Dickins 	if (page == kpage)			/* ksm page forked */
859db114b83SHugh Dickins 		return 0;
860db114b83SHugh Dickins 
86131dbd01fSIzik Eidus 	if (!(vma->vm_flags & VM_MERGEABLE))
86231dbd01fSIzik Eidus 		goto out;
86329ad768cSAndrea Arcangeli 	if (PageTransCompound(page) && page_trans_compound_anon_split(page))
86429ad768cSAndrea Arcangeli 		goto out;
86529ad768cSAndrea Arcangeli 	BUG_ON(PageTransCompound(page));
8668dd3557aSHugh Dickins 	if (!PageAnon(page))
86731dbd01fSIzik Eidus 		goto out;
86831dbd01fSIzik Eidus 
86931dbd01fSIzik Eidus 	/*
87031dbd01fSIzik Eidus 	 * We need the page lock to read a stable PageSwapCache in
87131dbd01fSIzik Eidus 	 * write_protect_page().  We use trylock_page() instead of
87231dbd01fSIzik Eidus 	 * lock_page() because we don't want to wait here - we
87331dbd01fSIzik Eidus 	 * prefer to continue scanning and merging different pages,
87431dbd01fSIzik Eidus 	 * then come back to this page when it is unlocked.
87531dbd01fSIzik Eidus 	 */
8768dd3557aSHugh Dickins 	if (!trylock_page(page))
87731e855eaSHugh Dickins 		goto out;
87831dbd01fSIzik Eidus 	/*
87931dbd01fSIzik Eidus 	 * If this anonymous page is mapped only here, its pte may need
88031dbd01fSIzik Eidus 	 * to be write-protected.  If it's mapped elsewhere, all of its
88131dbd01fSIzik Eidus 	 * ptes are necessarily already write-protected.  But in either
88231dbd01fSIzik Eidus 	 * case, we need to lock and check page_count is not raised.
88331dbd01fSIzik Eidus 	 */
88480e14822SHugh Dickins 	if (write_protect_page(vma, page, &orig_pte) == 0) {
88580e14822SHugh Dickins 		if (!kpage) {
88680e14822SHugh Dickins 			/*
88780e14822SHugh Dickins 			 * While we hold page lock, upgrade page from
88880e14822SHugh Dickins 			 * PageAnon+anon_vma to PageKsm+NULL stable_node:
88980e14822SHugh Dickins 			 * stable_tree_insert() will update stable_node.
89080e14822SHugh Dickins 			 */
89180e14822SHugh Dickins 			set_page_stable_node(page, NULL);
89280e14822SHugh Dickins 			mark_page_accessed(page);
89380e14822SHugh Dickins 			err = 0;
89480e14822SHugh Dickins 		} else if (pages_identical(page, kpage))
8958dd3557aSHugh Dickins 			err = replace_page(vma, page, kpage, orig_pte);
89680e14822SHugh Dickins 	}
89731dbd01fSIzik Eidus 
89880e14822SHugh Dickins 	if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
89973848b46SHugh Dickins 		munlock_vma_page(page);
9005ad64688SHugh Dickins 		if (!PageMlocked(kpage)) {
9015ad64688SHugh Dickins 			unlock_page(page);
9025ad64688SHugh Dickins 			lock_page(kpage);
9035ad64688SHugh Dickins 			mlock_vma_page(kpage);
9045ad64688SHugh Dickins 			page = kpage;		/* for final unlock */
9055ad64688SHugh Dickins 		}
9065ad64688SHugh Dickins 	}
90773848b46SHugh Dickins 
9088dd3557aSHugh Dickins 	unlock_page(page);
90931dbd01fSIzik Eidus out:
91031dbd01fSIzik Eidus 	return err;
91131dbd01fSIzik Eidus }
91231dbd01fSIzik Eidus 
91331dbd01fSIzik Eidus /*
91481464e30SHugh Dickins  * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
91581464e30SHugh Dickins  * but no new kernel page is allocated: kpage must already be a ksm page.
9168dd3557aSHugh Dickins  *
9178dd3557aSHugh Dickins  * This function returns 0 if the pages were merged, -EFAULT otherwise.
91881464e30SHugh Dickins  */
9198dd3557aSHugh Dickins static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
9208dd3557aSHugh Dickins 				      struct page *page, struct page *kpage)
92181464e30SHugh Dickins {
9228dd3557aSHugh Dickins 	struct mm_struct *mm = rmap_item->mm;
92381464e30SHugh Dickins 	struct vm_area_struct *vma;
92481464e30SHugh Dickins 	int err = -EFAULT;
92581464e30SHugh Dickins 
9268dd3557aSHugh Dickins 	down_read(&mm->mmap_sem);
9278dd3557aSHugh Dickins 	if (ksm_test_exit(mm))
9288dd3557aSHugh Dickins 		goto out;
9298dd3557aSHugh Dickins 	vma = find_vma(mm, rmap_item->address);
9308dd3557aSHugh Dickins 	if (!vma || vma->vm_start > rmap_item->address)
9319ba69294SHugh Dickins 		goto out;
9329ba69294SHugh Dickins 
9338dd3557aSHugh Dickins 	err = try_to_merge_one_page(vma, page, kpage);
934db114b83SHugh Dickins 	if (err)
935db114b83SHugh Dickins 		goto out;
936db114b83SHugh Dickins 
937db114b83SHugh Dickins 	/* Must get reference to anon_vma while still holding mmap_sem */
9389e60109fSPeter Zijlstra 	rmap_item->anon_vma = vma->anon_vma;
9399e60109fSPeter Zijlstra 	get_anon_vma(vma->anon_vma);
94081464e30SHugh Dickins out:
9418dd3557aSHugh Dickins 	up_read(&mm->mmap_sem);
94281464e30SHugh Dickins 	return err;
94381464e30SHugh Dickins }
94481464e30SHugh Dickins 
94581464e30SHugh Dickins /*
94631dbd01fSIzik Eidus  * try_to_merge_two_pages - take two identical pages and prepare them
94731dbd01fSIzik Eidus  * to be merged into one page.
94831dbd01fSIzik Eidus  *
9498dd3557aSHugh Dickins  * This function returns the kpage if we successfully merged two identical
9508dd3557aSHugh Dickins  * pages into one ksm page, NULL otherwise.
95131dbd01fSIzik Eidus  *
95280e14822SHugh Dickins  * Note that this function upgrades page to ksm page: if one of the pages
95331dbd01fSIzik Eidus  * is already a ksm page, try_to_merge_with_ksm_page should be used.
95431dbd01fSIzik Eidus  */
9558dd3557aSHugh Dickins static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
9568dd3557aSHugh Dickins 					   struct page *page,
9578dd3557aSHugh Dickins 					   struct rmap_item *tree_rmap_item,
9588dd3557aSHugh Dickins 					   struct page *tree_page)
95931dbd01fSIzik Eidus {
96080e14822SHugh Dickins 	int err;
96131dbd01fSIzik Eidus 
96280e14822SHugh Dickins 	err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
96331dbd01fSIzik Eidus 	if (!err) {
9648dd3557aSHugh Dickins 		err = try_to_merge_with_ksm_page(tree_rmap_item,
96580e14822SHugh Dickins 							tree_page, page);
96631dbd01fSIzik Eidus 		/*
96781464e30SHugh Dickins 		 * If that fails, we have a ksm page with only one pte
96881464e30SHugh Dickins 		 * pointing to it: so break it.
96931dbd01fSIzik Eidus 		 */
9704035c07aSHugh Dickins 		if (err)
9718dd3557aSHugh Dickins 			break_cow(rmap_item);
97231dbd01fSIzik Eidus 	}
97380e14822SHugh Dickins 	return err ? NULL : page;
97431dbd01fSIzik Eidus }
97531dbd01fSIzik Eidus 
97631dbd01fSIzik Eidus /*
9778dd3557aSHugh Dickins  * stable_tree_search - search for page inside the stable tree
97831dbd01fSIzik Eidus  *
97931dbd01fSIzik Eidus  * This function checks if there is a page inside the stable tree
98031dbd01fSIzik Eidus  * with identical content to the page that we are scanning right now.
98131dbd01fSIzik Eidus  *
9827b6ba2c7SHugh Dickins  * This function returns the stable tree node of identical content if found,
98331dbd01fSIzik Eidus  * NULL otherwise.
98431dbd01fSIzik Eidus  */
98562b61f61SHugh Dickins static struct page *stable_tree_search(struct page *page)
98631dbd01fSIzik Eidus {
98731dbd01fSIzik Eidus 	struct rb_node *node = root_stable_tree.rb_node;
9887b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
98931dbd01fSIzik Eidus 
99008beca44SHugh Dickins 	stable_node = page_stable_node(page);
99108beca44SHugh Dickins 	if (stable_node) {			/* ksm page forked */
99208beca44SHugh Dickins 		get_page(page);
99362b61f61SHugh Dickins 		return page;
99408beca44SHugh Dickins 	}
99508beca44SHugh Dickins 
99631dbd01fSIzik Eidus 	while (node) {
9974035c07aSHugh Dickins 		struct page *tree_page;
99831dbd01fSIzik Eidus 		int ret;
99931dbd01fSIzik Eidus 
100031dbd01fSIzik Eidus 		cond_resched();
100108beca44SHugh Dickins 		stable_node = rb_entry(node, struct stable_node, node);
10024035c07aSHugh Dickins 		tree_page = get_ksm_page(stable_node);
10034035c07aSHugh Dickins 		if (!tree_page)
10044035c07aSHugh Dickins 			return NULL;
100531dbd01fSIzik Eidus 
10064035c07aSHugh Dickins 		ret = memcmp_pages(page, tree_page);
100731dbd01fSIzik Eidus 
10084035c07aSHugh Dickins 		if (ret < 0) {
10094035c07aSHugh Dickins 			put_page(tree_page);
101031dbd01fSIzik Eidus 			node = node->rb_left;
10114035c07aSHugh Dickins 		} else if (ret > 0) {
10124035c07aSHugh Dickins 			put_page(tree_page);
101331dbd01fSIzik Eidus 			node = node->rb_right;
10144035c07aSHugh Dickins 		} else
101562b61f61SHugh Dickins 			return tree_page;
101631dbd01fSIzik Eidus 	}
101731dbd01fSIzik Eidus 
101831dbd01fSIzik Eidus 	return NULL;
101931dbd01fSIzik Eidus }
102031dbd01fSIzik Eidus 
102131dbd01fSIzik Eidus /*
102231dbd01fSIzik Eidus  * stable_tree_insert - insert rmap_item pointing to new ksm page
102331dbd01fSIzik Eidus  * into the stable tree.
102431dbd01fSIzik Eidus  *
10257b6ba2c7SHugh Dickins  * This function returns the stable tree node just allocated on success,
10267b6ba2c7SHugh Dickins  * NULL otherwise.
102731dbd01fSIzik Eidus  */
10287b6ba2c7SHugh Dickins static struct stable_node *stable_tree_insert(struct page *kpage)
102931dbd01fSIzik Eidus {
103031dbd01fSIzik Eidus 	struct rb_node **new = &root_stable_tree.rb_node;
103131dbd01fSIzik Eidus 	struct rb_node *parent = NULL;
10327b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
103331dbd01fSIzik Eidus 
103431dbd01fSIzik Eidus 	while (*new) {
10354035c07aSHugh Dickins 		struct page *tree_page;
103631dbd01fSIzik Eidus 		int ret;
103731dbd01fSIzik Eidus 
103831dbd01fSIzik Eidus 		cond_resched();
103908beca44SHugh Dickins 		stable_node = rb_entry(*new, struct stable_node, node);
10404035c07aSHugh Dickins 		tree_page = get_ksm_page(stable_node);
10414035c07aSHugh Dickins 		if (!tree_page)
10424035c07aSHugh Dickins 			return NULL;
104331dbd01fSIzik Eidus 
10444035c07aSHugh Dickins 		ret = memcmp_pages(kpage, tree_page);
10454035c07aSHugh Dickins 		put_page(tree_page);
104631dbd01fSIzik Eidus 
104731dbd01fSIzik Eidus 		parent = *new;
104831dbd01fSIzik Eidus 		if (ret < 0)
104931dbd01fSIzik Eidus 			new = &parent->rb_left;
105031dbd01fSIzik Eidus 		else if (ret > 0)
105131dbd01fSIzik Eidus 			new = &parent->rb_right;
105231dbd01fSIzik Eidus 		else {
105331dbd01fSIzik Eidus 			/*
105431dbd01fSIzik Eidus 			 * It is not a bug that stable_tree_search() didn't
105531dbd01fSIzik Eidus 			 * find this node: because at that time our page was
105631dbd01fSIzik Eidus 			 * not yet write-protected, so may have changed since.
105731dbd01fSIzik Eidus 			 */
105831dbd01fSIzik Eidus 			return NULL;
105931dbd01fSIzik Eidus 		}
106031dbd01fSIzik Eidus 	}
106131dbd01fSIzik Eidus 
10627b6ba2c7SHugh Dickins 	stable_node = alloc_stable_node();
10637b6ba2c7SHugh Dickins 	if (!stable_node)
10647b6ba2c7SHugh Dickins 		return NULL;
106531dbd01fSIzik Eidus 
10667b6ba2c7SHugh Dickins 	rb_link_node(&stable_node->node, parent, new);
10677b6ba2c7SHugh Dickins 	rb_insert_color(&stable_node->node, &root_stable_tree);
10687b6ba2c7SHugh Dickins 
10697b6ba2c7SHugh Dickins 	INIT_HLIST_HEAD(&stable_node->hlist);
10707b6ba2c7SHugh Dickins 
107162b61f61SHugh Dickins 	stable_node->kpfn = page_to_pfn(kpage);
107208beca44SHugh Dickins 	set_page_stable_node(kpage, stable_node);
107308beca44SHugh Dickins 
10747b6ba2c7SHugh Dickins 	return stable_node;
107531dbd01fSIzik Eidus }
107631dbd01fSIzik Eidus 
107731dbd01fSIzik Eidus /*
10788dd3557aSHugh Dickins  * unstable_tree_search_insert - search for identical page,
10798dd3557aSHugh Dickins  * else insert rmap_item into the unstable tree.
108031dbd01fSIzik Eidus  *
108131dbd01fSIzik Eidus  * This function searches for a page in the unstable tree identical to the
108231dbd01fSIzik Eidus  * page currently being scanned; and if no identical page is found in the
108331dbd01fSIzik Eidus  * tree, we insert rmap_item as a new object into the unstable tree.
108431dbd01fSIzik Eidus  *
108531dbd01fSIzik Eidus  * This function returns pointer to rmap_item found to be identical
108631dbd01fSIzik Eidus  * to the currently scanned page, NULL otherwise.
108731dbd01fSIzik Eidus  *
108831dbd01fSIzik Eidus  * This function does both searching and inserting, because they share
108931dbd01fSIzik Eidus  * the same walking algorithm in an rbtree.
109031dbd01fSIzik Eidus  */
10918dd3557aSHugh Dickins static
10928dd3557aSHugh Dickins struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
10938dd3557aSHugh Dickins 					      struct page *page,
10948dd3557aSHugh Dickins 					      struct page **tree_pagep)
10958dd3557aSHugh Dickins 
109631dbd01fSIzik Eidus {
109731dbd01fSIzik Eidus 	struct rb_node **new = &root_unstable_tree.rb_node;
109831dbd01fSIzik Eidus 	struct rb_node *parent = NULL;
109931dbd01fSIzik Eidus 
110031dbd01fSIzik Eidus 	while (*new) {
110131dbd01fSIzik Eidus 		struct rmap_item *tree_rmap_item;
11028dd3557aSHugh Dickins 		struct page *tree_page;
110331dbd01fSIzik Eidus 		int ret;
110431dbd01fSIzik Eidus 
1105d178f27fSHugh Dickins 		cond_resched();
110631dbd01fSIzik Eidus 		tree_rmap_item = rb_entry(*new, struct rmap_item, node);
11078dd3557aSHugh Dickins 		tree_page = get_mergeable_page(tree_rmap_item);
110822eccdd7SDan Carpenter 		if (IS_ERR_OR_NULL(tree_page))
110931dbd01fSIzik Eidus 			return NULL;
111031dbd01fSIzik Eidus 
111131dbd01fSIzik Eidus 		/*
11128dd3557aSHugh Dickins 		 * Don't substitute a ksm page for a forked page.
111331dbd01fSIzik Eidus 		 */
11148dd3557aSHugh Dickins 		if (page == tree_page) {
11158dd3557aSHugh Dickins 			put_page(tree_page);
111631dbd01fSIzik Eidus 			return NULL;
111731dbd01fSIzik Eidus 		}
111831dbd01fSIzik Eidus 
11198dd3557aSHugh Dickins 		ret = memcmp_pages(page, tree_page);
112031dbd01fSIzik Eidus 
112131dbd01fSIzik Eidus 		parent = *new;
112231dbd01fSIzik Eidus 		if (ret < 0) {
11238dd3557aSHugh Dickins 			put_page(tree_page);
112431dbd01fSIzik Eidus 			new = &parent->rb_left;
112531dbd01fSIzik Eidus 		} else if (ret > 0) {
11268dd3557aSHugh Dickins 			put_page(tree_page);
112731dbd01fSIzik Eidus 			new = &parent->rb_right;
112831dbd01fSIzik Eidus 		} else {
11298dd3557aSHugh Dickins 			*tree_pagep = tree_page;
113031dbd01fSIzik Eidus 			return tree_rmap_item;
113131dbd01fSIzik Eidus 		}
113231dbd01fSIzik Eidus 	}
113331dbd01fSIzik Eidus 
11347b6ba2c7SHugh Dickins 	rmap_item->address |= UNSTABLE_FLAG;
113531dbd01fSIzik Eidus 	rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
113631dbd01fSIzik Eidus 	rb_link_node(&rmap_item->node, parent, new);
113731dbd01fSIzik Eidus 	rb_insert_color(&rmap_item->node, &root_unstable_tree);
113831dbd01fSIzik Eidus 
1139473b0ce4SHugh Dickins 	ksm_pages_unshared++;
114031dbd01fSIzik Eidus 	return NULL;
114131dbd01fSIzik Eidus }
114231dbd01fSIzik Eidus 
114331dbd01fSIzik Eidus /*
114431dbd01fSIzik Eidus  * stable_tree_append - add another rmap_item to the linked list of
114531dbd01fSIzik Eidus  * rmap_items hanging off a given node of the stable tree, all sharing
114631dbd01fSIzik Eidus  * the same ksm page.
114731dbd01fSIzik Eidus  */
114831dbd01fSIzik Eidus static void stable_tree_append(struct rmap_item *rmap_item,
11497b6ba2c7SHugh Dickins 			       struct stable_node *stable_node)
115031dbd01fSIzik Eidus {
11517b6ba2c7SHugh Dickins 	rmap_item->head = stable_node;
115231dbd01fSIzik Eidus 	rmap_item->address |= STABLE_FLAG;
11537b6ba2c7SHugh Dickins 	hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
1154e178dfdeSHugh Dickins 
11557b6ba2c7SHugh Dickins 	if (rmap_item->hlist.next)
1156e178dfdeSHugh Dickins 		ksm_pages_sharing++;
11577b6ba2c7SHugh Dickins 	else
11587b6ba2c7SHugh Dickins 		ksm_pages_shared++;
115931dbd01fSIzik Eidus }
116031dbd01fSIzik Eidus 
116131dbd01fSIzik Eidus /*
116281464e30SHugh Dickins  * cmp_and_merge_page - first see if page can be merged into the stable tree;
116381464e30SHugh Dickins  * if not, compare checksum to previous and if it's the same, see if page can
116481464e30SHugh Dickins  * be inserted into the unstable tree, or merged with a page already there and
116581464e30SHugh Dickins  * both transferred to the stable tree.
116631dbd01fSIzik Eidus  *
116731dbd01fSIzik Eidus  * @page: the page that we are searching identical page to.
116831dbd01fSIzik Eidus  * @rmap_item: the reverse mapping into the virtual address of this page
116931dbd01fSIzik Eidus  */
117031dbd01fSIzik Eidus static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
117131dbd01fSIzik Eidus {
117231dbd01fSIzik Eidus 	struct rmap_item *tree_rmap_item;
11738dd3557aSHugh Dickins 	struct page *tree_page = NULL;
11747b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
11758dd3557aSHugh Dickins 	struct page *kpage;
117631dbd01fSIzik Eidus 	unsigned int checksum;
117731dbd01fSIzik Eidus 	int err;
117831dbd01fSIzik Eidus 
117931dbd01fSIzik Eidus 	remove_rmap_item_from_tree(rmap_item);
118031dbd01fSIzik Eidus 
118131dbd01fSIzik Eidus 	/* We first start with searching the page inside the stable tree */
118262b61f61SHugh Dickins 	kpage = stable_tree_search(page);
118362b61f61SHugh Dickins 	if (kpage) {
118408beca44SHugh Dickins 		err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
118531dbd01fSIzik Eidus 		if (!err) {
118631dbd01fSIzik Eidus 			/*
118731dbd01fSIzik Eidus 			 * The page was successfully merged:
118831dbd01fSIzik Eidus 			 * add its rmap_item to the stable tree.
118931dbd01fSIzik Eidus 			 */
11905ad64688SHugh Dickins 			lock_page(kpage);
119162b61f61SHugh Dickins 			stable_tree_append(rmap_item, page_stable_node(kpage));
11925ad64688SHugh Dickins 			unlock_page(kpage);
119331dbd01fSIzik Eidus 		}
11948dd3557aSHugh Dickins 		put_page(kpage);
119531dbd01fSIzik Eidus 		return;
119631dbd01fSIzik Eidus 	}
119731dbd01fSIzik Eidus 
119831dbd01fSIzik Eidus 	/*
11994035c07aSHugh Dickins 	 * If the hash value of the page has changed from the last time
12004035c07aSHugh Dickins 	 * we calculated it, this page is changing frequently: therefore we
12014035c07aSHugh Dickins 	 * don't want to insert it in the unstable tree, and we don't want
12024035c07aSHugh Dickins 	 * to waste our time searching for something identical to it there.
120331dbd01fSIzik Eidus 	 */
120431dbd01fSIzik Eidus 	checksum = calc_checksum(page);
120531dbd01fSIzik Eidus 	if (rmap_item->oldchecksum != checksum) {
120631dbd01fSIzik Eidus 		rmap_item->oldchecksum = checksum;
120731dbd01fSIzik Eidus 		return;
120831dbd01fSIzik Eidus 	}
120931dbd01fSIzik Eidus 
12108dd3557aSHugh Dickins 	tree_rmap_item =
12118dd3557aSHugh Dickins 		unstable_tree_search_insert(rmap_item, page, &tree_page);
121231dbd01fSIzik Eidus 	if (tree_rmap_item) {
12138dd3557aSHugh Dickins 		kpage = try_to_merge_two_pages(rmap_item, page,
12148dd3557aSHugh Dickins 						tree_rmap_item, tree_page);
12158dd3557aSHugh Dickins 		put_page(tree_page);
121631dbd01fSIzik Eidus 		/*
121731dbd01fSIzik Eidus 		 * As soon as we merge this page, we want to remove the
121831dbd01fSIzik Eidus 		 * rmap_item of the page we have merged with from the unstable
121931dbd01fSIzik Eidus 		 * tree, and insert it instead as new node in the stable tree.
122031dbd01fSIzik Eidus 		 */
12218dd3557aSHugh Dickins 		if (kpage) {
122293d17715SHugh Dickins 			remove_rmap_item_from_tree(tree_rmap_item);
1223473b0ce4SHugh Dickins 
12245ad64688SHugh Dickins 			lock_page(kpage);
12257b6ba2c7SHugh Dickins 			stable_node = stable_tree_insert(kpage);
12267b6ba2c7SHugh Dickins 			if (stable_node) {
12277b6ba2c7SHugh Dickins 				stable_tree_append(tree_rmap_item, stable_node);
12287b6ba2c7SHugh Dickins 				stable_tree_append(rmap_item, stable_node);
12297b6ba2c7SHugh Dickins 			}
12305ad64688SHugh Dickins 			unlock_page(kpage);
12317b6ba2c7SHugh Dickins 
123231dbd01fSIzik Eidus 			/*
123331dbd01fSIzik Eidus 			 * If we fail to insert the page into the stable tree,
123431dbd01fSIzik Eidus 			 * we will have 2 virtual addresses that are pointing
123531dbd01fSIzik Eidus 			 * to a ksm page left outside the stable tree,
123631dbd01fSIzik Eidus 			 * in which case we need to break_cow on both.
123731dbd01fSIzik Eidus 			 */
12387b6ba2c7SHugh Dickins 			if (!stable_node) {
12398dd3557aSHugh Dickins 				break_cow(tree_rmap_item);
12408dd3557aSHugh Dickins 				break_cow(rmap_item);
124131dbd01fSIzik Eidus 			}
124231dbd01fSIzik Eidus 		}
124331dbd01fSIzik Eidus 	}
124431dbd01fSIzik Eidus }
124531dbd01fSIzik Eidus 
124631dbd01fSIzik Eidus static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
12476514d511SHugh Dickins 					    struct rmap_item **rmap_list,
124831dbd01fSIzik Eidus 					    unsigned long addr)
124931dbd01fSIzik Eidus {
125031dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
125131dbd01fSIzik Eidus 
12526514d511SHugh Dickins 	while (*rmap_list) {
12536514d511SHugh Dickins 		rmap_item = *rmap_list;
125493d17715SHugh Dickins 		if ((rmap_item->address & PAGE_MASK) == addr)
125531dbd01fSIzik Eidus 			return rmap_item;
125631dbd01fSIzik Eidus 		if (rmap_item->address > addr)
125731dbd01fSIzik Eidus 			break;
12586514d511SHugh Dickins 		*rmap_list = rmap_item->rmap_list;
125931dbd01fSIzik Eidus 		remove_rmap_item_from_tree(rmap_item);
126031dbd01fSIzik Eidus 		free_rmap_item(rmap_item);
126131dbd01fSIzik Eidus 	}
126231dbd01fSIzik Eidus 
126331dbd01fSIzik Eidus 	rmap_item = alloc_rmap_item();
126431dbd01fSIzik Eidus 	if (rmap_item) {
126531dbd01fSIzik Eidus 		/* It has already been zeroed */
126631dbd01fSIzik Eidus 		rmap_item->mm = mm_slot->mm;
126731dbd01fSIzik Eidus 		rmap_item->address = addr;
12686514d511SHugh Dickins 		rmap_item->rmap_list = *rmap_list;
12696514d511SHugh Dickins 		*rmap_list = rmap_item;
127031dbd01fSIzik Eidus 	}
127131dbd01fSIzik Eidus 	return rmap_item;
127231dbd01fSIzik Eidus }
127331dbd01fSIzik Eidus 
127431dbd01fSIzik Eidus static struct rmap_item *scan_get_next_rmap_item(struct page **page)
127531dbd01fSIzik Eidus {
127631dbd01fSIzik Eidus 	struct mm_struct *mm;
127731dbd01fSIzik Eidus 	struct mm_slot *slot;
127831dbd01fSIzik Eidus 	struct vm_area_struct *vma;
127931dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
128031dbd01fSIzik Eidus 
128131dbd01fSIzik Eidus 	if (list_empty(&ksm_mm_head.mm_list))
128231dbd01fSIzik Eidus 		return NULL;
128331dbd01fSIzik Eidus 
128431dbd01fSIzik Eidus 	slot = ksm_scan.mm_slot;
128531dbd01fSIzik Eidus 	if (slot == &ksm_mm_head) {
12862919bfd0SHugh Dickins 		/*
12872919bfd0SHugh Dickins 		 * A number of pages can hang around indefinitely on per-cpu
12882919bfd0SHugh Dickins 		 * pagevecs, raised page count preventing write_protect_page
12892919bfd0SHugh Dickins 		 * from merging them.  Though it doesn't really matter much,
12902919bfd0SHugh Dickins 		 * it is puzzling to see some stuck in pages_volatile until
12912919bfd0SHugh Dickins 		 * other activity jostles them out, and they also prevented
12922919bfd0SHugh Dickins 		 * LTP's KSM test from succeeding deterministically; so drain
12932919bfd0SHugh Dickins 		 * them here (here rather than on entry to ksm_do_scan(),
12942919bfd0SHugh Dickins 		 * so we don't IPI too often when pages_to_scan is set low).
12952919bfd0SHugh Dickins 		 */
12962919bfd0SHugh Dickins 		lru_add_drain_all();
12972919bfd0SHugh Dickins 
129831dbd01fSIzik Eidus 		root_unstable_tree = RB_ROOT;
129931dbd01fSIzik Eidus 
130031dbd01fSIzik Eidus 		spin_lock(&ksm_mmlist_lock);
130131dbd01fSIzik Eidus 		slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
130231dbd01fSIzik Eidus 		ksm_scan.mm_slot = slot;
130331dbd01fSIzik Eidus 		spin_unlock(&ksm_mmlist_lock);
130431dbd01fSIzik Eidus next_mm:
130531dbd01fSIzik Eidus 		ksm_scan.address = 0;
13066514d511SHugh Dickins 		ksm_scan.rmap_list = &slot->rmap_list;
130731dbd01fSIzik Eidus 	}
130831dbd01fSIzik Eidus 
130931dbd01fSIzik Eidus 	mm = slot->mm;
131031dbd01fSIzik Eidus 	down_read(&mm->mmap_sem);
13119ba69294SHugh Dickins 	if (ksm_test_exit(mm))
13129ba69294SHugh Dickins 		vma = NULL;
13139ba69294SHugh Dickins 	else
13149ba69294SHugh Dickins 		vma = find_vma(mm, ksm_scan.address);
13159ba69294SHugh Dickins 
13169ba69294SHugh Dickins 	for (; vma; vma = vma->vm_next) {
131731dbd01fSIzik Eidus 		if (!(vma->vm_flags & VM_MERGEABLE))
131831dbd01fSIzik Eidus 			continue;
131931dbd01fSIzik Eidus 		if (ksm_scan.address < vma->vm_start)
132031dbd01fSIzik Eidus 			ksm_scan.address = vma->vm_start;
132131dbd01fSIzik Eidus 		if (!vma->anon_vma)
132231dbd01fSIzik Eidus 			ksm_scan.address = vma->vm_end;
132331dbd01fSIzik Eidus 
132431dbd01fSIzik Eidus 		while (ksm_scan.address < vma->vm_end) {
13259ba69294SHugh Dickins 			if (ksm_test_exit(mm))
13269ba69294SHugh Dickins 				break;
132731dbd01fSIzik Eidus 			*page = follow_page(vma, ksm_scan.address, FOLL_GET);
132821ae5b01SAndrea Arcangeli 			if (IS_ERR_OR_NULL(*page)) {
132921ae5b01SAndrea Arcangeli 				ksm_scan.address += PAGE_SIZE;
133021ae5b01SAndrea Arcangeli 				cond_resched();
133121ae5b01SAndrea Arcangeli 				continue;
133221ae5b01SAndrea Arcangeli 			}
133329ad768cSAndrea Arcangeli 			if (PageAnon(*page) ||
133429ad768cSAndrea Arcangeli 			    page_trans_compound_anon(*page)) {
133531dbd01fSIzik Eidus 				flush_anon_page(vma, *page, ksm_scan.address);
133631dbd01fSIzik Eidus 				flush_dcache_page(*page);
133731dbd01fSIzik Eidus 				rmap_item = get_next_rmap_item(slot,
13386514d511SHugh Dickins 					ksm_scan.rmap_list, ksm_scan.address);
133931dbd01fSIzik Eidus 				if (rmap_item) {
13406514d511SHugh Dickins 					ksm_scan.rmap_list =
13416514d511SHugh Dickins 							&rmap_item->rmap_list;
134231dbd01fSIzik Eidus 					ksm_scan.address += PAGE_SIZE;
134331dbd01fSIzik Eidus 				} else
134431dbd01fSIzik Eidus 					put_page(*page);
134531dbd01fSIzik Eidus 				up_read(&mm->mmap_sem);
134631dbd01fSIzik Eidus 				return rmap_item;
134731dbd01fSIzik Eidus 			}
134831dbd01fSIzik Eidus 			put_page(*page);
134931dbd01fSIzik Eidus 			ksm_scan.address += PAGE_SIZE;
135031dbd01fSIzik Eidus 			cond_resched();
135131dbd01fSIzik Eidus 		}
135231dbd01fSIzik Eidus 	}
135331dbd01fSIzik Eidus 
13549ba69294SHugh Dickins 	if (ksm_test_exit(mm)) {
13559ba69294SHugh Dickins 		ksm_scan.address = 0;
13566514d511SHugh Dickins 		ksm_scan.rmap_list = &slot->rmap_list;
13579ba69294SHugh Dickins 	}
135831dbd01fSIzik Eidus 	/*
135931dbd01fSIzik Eidus 	 * Nuke all the rmap_items that are above this current rmap:
136031dbd01fSIzik Eidus 	 * because there were no VM_MERGEABLE vmas with such addresses.
136131dbd01fSIzik Eidus 	 */
13626514d511SHugh Dickins 	remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
136331dbd01fSIzik Eidus 
136431dbd01fSIzik Eidus 	spin_lock(&ksm_mmlist_lock);
1365cd551f97SHugh Dickins 	ksm_scan.mm_slot = list_entry(slot->mm_list.next,
1366cd551f97SHugh Dickins 						struct mm_slot, mm_list);
1367cd551f97SHugh Dickins 	if (ksm_scan.address == 0) {
1368cd551f97SHugh Dickins 		/*
1369cd551f97SHugh Dickins 		 * We've completed a full scan of all vmas, holding mmap_sem
1370cd551f97SHugh Dickins 		 * throughout, and found no VM_MERGEABLE: so do the same as
1371cd551f97SHugh Dickins 		 * __ksm_exit does to remove this mm from all our lists now.
13729ba69294SHugh Dickins 		 * This applies either when cleaning up after __ksm_exit
13739ba69294SHugh Dickins 		 * (but beware: we can reach here even before __ksm_exit),
13749ba69294SHugh Dickins 		 * or when all VM_MERGEABLE areas have been unmapped (and
13759ba69294SHugh Dickins 		 * mmap_sem then protects against race with MADV_MERGEABLE).
1376cd551f97SHugh Dickins 		 */
1377cd551f97SHugh Dickins 		hlist_del(&slot->link);
1378cd551f97SHugh Dickins 		list_del(&slot->mm_list);
13799ba69294SHugh Dickins 		spin_unlock(&ksm_mmlist_lock);
13809ba69294SHugh Dickins 
1381cd551f97SHugh Dickins 		free_mm_slot(slot);
1382cd551f97SHugh Dickins 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
13839ba69294SHugh Dickins 		up_read(&mm->mmap_sem);
13849ba69294SHugh Dickins 		mmdrop(mm);
13859ba69294SHugh Dickins 	} else {
138631dbd01fSIzik Eidus 		spin_unlock(&ksm_mmlist_lock);
1387cd551f97SHugh Dickins 		up_read(&mm->mmap_sem);
13889ba69294SHugh Dickins 	}
138931dbd01fSIzik Eidus 
139031dbd01fSIzik Eidus 	/* Repeat until we've completed scanning the whole list */
1391cd551f97SHugh Dickins 	slot = ksm_scan.mm_slot;
139231dbd01fSIzik Eidus 	if (slot != &ksm_mm_head)
139331dbd01fSIzik Eidus 		goto next_mm;
139431dbd01fSIzik Eidus 
139531dbd01fSIzik Eidus 	ksm_scan.seqnr++;
139631dbd01fSIzik Eidus 	return NULL;
139731dbd01fSIzik Eidus }
139831dbd01fSIzik Eidus 
139931dbd01fSIzik Eidus /**
140031dbd01fSIzik Eidus  * ksm_do_scan  - the ksm scanner main worker function.
140131dbd01fSIzik Eidus  * @scan_npages - number of pages we want to scan before we return.
140231dbd01fSIzik Eidus  */
140331dbd01fSIzik Eidus static void ksm_do_scan(unsigned int scan_npages)
140431dbd01fSIzik Eidus {
140531dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
140622eccdd7SDan Carpenter 	struct page *uninitialized_var(page);
140731dbd01fSIzik Eidus 
1408878aee7dSAndrea Arcangeli 	while (scan_npages-- && likely(!freezing(current))) {
140931dbd01fSIzik Eidus 		cond_resched();
141031dbd01fSIzik Eidus 		rmap_item = scan_get_next_rmap_item(&page);
141131dbd01fSIzik Eidus 		if (!rmap_item)
141231dbd01fSIzik Eidus 			return;
141331dbd01fSIzik Eidus 		if (!PageKsm(page) || !in_stable_tree(rmap_item))
141431dbd01fSIzik Eidus 			cmp_and_merge_page(page, rmap_item);
141531dbd01fSIzik Eidus 		put_page(page);
141631dbd01fSIzik Eidus 	}
141731dbd01fSIzik Eidus }
141831dbd01fSIzik Eidus 
14196e158384SHugh Dickins static int ksmd_should_run(void)
14206e158384SHugh Dickins {
14216e158384SHugh Dickins 	return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
14226e158384SHugh Dickins }
14236e158384SHugh Dickins 
142431dbd01fSIzik Eidus static int ksm_scan_thread(void *nothing)
142531dbd01fSIzik Eidus {
1426878aee7dSAndrea Arcangeli 	set_freezable();
1427339aa624SIzik Eidus 	set_user_nice(current, 5);
142831dbd01fSIzik Eidus 
142931dbd01fSIzik Eidus 	while (!kthread_should_stop()) {
143031dbd01fSIzik Eidus 		mutex_lock(&ksm_thread_mutex);
14316e158384SHugh Dickins 		if (ksmd_should_run())
143231dbd01fSIzik Eidus 			ksm_do_scan(ksm_thread_pages_to_scan);
143331dbd01fSIzik Eidus 		mutex_unlock(&ksm_thread_mutex);
14346e158384SHugh Dickins 
1435878aee7dSAndrea Arcangeli 		try_to_freeze();
1436878aee7dSAndrea Arcangeli 
14376e158384SHugh Dickins 		if (ksmd_should_run()) {
143831dbd01fSIzik Eidus 			schedule_timeout_interruptible(
143931dbd01fSIzik Eidus 				msecs_to_jiffies(ksm_thread_sleep_millisecs));
144031dbd01fSIzik Eidus 		} else {
1441878aee7dSAndrea Arcangeli 			wait_event_freezable(ksm_thread_wait,
14426e158384SHugh Dickins 				ksmd_should_run() || kthread_should_stop());
144331dbd01fSIzik Eidus 		}
144431dbd01fSIzik Eidus 	}
144531dbd01fSIzik Eidus 	return 0;
144631dbd01fSIzik Eidus }
144731dbd01fSIzik Eidus 
1448f8af4da3SHugh Dickins int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
1449f8af4da3SHugh Dickins 		unsigned long end, int advice, unsigned long *vm_flags)
1450f8af4da3SHugh Dickins {
1451f8af4da3SHugh Dickins 	struct mm_struct *mm = vma->vm_mm;
1452d952b791SHugh Dickins 	int err;
1453f8af4da3SHugh Dickins 
1454f8af4da3SHugh Dickins 	switch (advice) {
1455f8af4da3SHugh Dickins 	case MADV_MERGEABLE:
1456f8af4da3SHugh Dickins 		/*
1457f8af4da3SHugh Dickins 		 * Be somewhat over-protective for now!
1458f8af4da3SHugh Dickins 		 */
1459f8af4da3SHugh Dickins 		if (*vm_flags & (VM_MERGEABLE | VM_SHARED  | VM_MAYSHARE   |
1460f8af4da3SHugh Dickins 				 VM_PFNMAP    | VM_IO      | VM_DONTEXPAND |
1461f8af4da3SHugh Dickins 				 VM_RESERVED  | VM_HUGETLB | VM_INSERTPAGE |
14625ad64688SHugh Dickins 				 VM_NONLINEAR | VM_MIXEDMAP | VM_SAO))
1463f8af4da3SHugh Dickins 			return 0;		/* just ignore the advice */
1464f8af4da3SHugh Dickins 
1465d952b791SHugh Dickins 		if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
1466d952b791SHugh Dickins 			err = __ksm_enter(mm);
1467d952b791SHugh Dickins 			if (err)
1468d952b791SHugh Dickins 				return err;
1469d952b791SHugh Dickins 		}
1470f8af4da3SHugh Dickins 
1471f8af4da3SHugh Dickins 		*vm_flags |= VM_MERGEABLE;
1472f8af4da3SHugh Dickins 		break;
1473f8af4da3SHugh Dickins 
1474f8af4da3SHugh Dickins 	case MADV_UNMERGEABLE:
1475f8af4da3SHugh Dickins 		if (!(*vm_flags & VM_MERGEABLE))
1476f8af4da3SHugh Dickins 			return 0;		/* just ignore the advice */
1477f8af4da3SHugh Dickins 
1478d952b791SHugh Dickins 		if (vma->anon_vma) {
1479d952b791SHugh Dickins 			err = unmerge_ksm_pages(vma, start, end);
1480d952b791SHugh Dickins 			if (err)
1481d952b791SHugh Dickins 				return err;
1482d952b791SHugh Dickins 		}
1483f8af4da3SHugh Dickins 
1484f8af4da3SHugh Dickins 		*vm_flags &= ~VM_MERGEABLE;
1485f8af4da3SHugh Dickins 		break;
1486f8af4da3SHugh Dickins 	}
1487f8af4da3SHugh Dickins 
1488f8af4da3SHugh Dickins 	return 0;
1489f8af4da3SHugh Dickins }
1490f8af4da3SHugh Dickins 
1491f8af4da3SHugh Dickins int __ksm_enter(struct mm_struct *mm)
1492f8af4da3SHugh Dickins {
14936e158384SHugh Dickins 	struct mm_slot *mm_slot;
14946e158384SHugh Dickins 	int needs_wakeup;
14956e158384SHugh Dickins 
14966e158384SHugh Dickins 	mm_slot = alloc_mm_slot();
149731dbd01fSIzik Eidus 	if (!mm_slot)
149831dbd01fSIzik Eidus 		return -ENOMEM;
149931dbd01fSIzik Eidus 
15006e158384SHugh Dickins 	/* Check ksm_run too?  Would need tighter locking */
15016e158384SHugh Dickins 	needs_wakeup = list_empty(&ksm_mm_head.mm_list);
15026e158384SHugh Dickins 
150331dbd01fSIzik Eidus 	spin_lock(&ksm_mmlist_lock);
150431dbd01fSIzik Eidus 	insert_to_mm_slots_hash(mm, mm_slot);
150531dbd01fSIzik Eidus 	/*
150631dbd01fSIzik Eidus 	 * Insert just behind the scanning cursor, to let the area settle
150731dbd01fSIzik Eidus 	 * down a little; when fork is followed by immediate exec, we don't
150831dbd01fSIzik Eidus 	 * want ksmd to waste time setting up and tearing down an rmap_list.
150931dbd01fSIzik Eidus 	 */
151031dbd01fSIzik Eidus 	list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
151131dbd01fSIzik Eidus 	spin_unlock(&ksm_mmlist_lock);
151231dbd01fSIzik Eidus 
1513f8af4da3SHugh Dickins 	set_bit(MMF_VM_MERGEABLE, &mm->flags);
15149ba69294SHugh Dickins 	atomic_inc(&mm->mm_count);
15156e158384SHugh Dickins 
15166e158384SHugh Dickins 	if (needs_wakeup)
15176e158384SHugh Dickins 		wake_up_interruptible(&ksm_thread_wait);
15186e158384SHugh Dickins 
1519f8af4da3SHugh Dickins 	return 0;
1520f8af4da3SHugh Dickins }
1521f8af4da3SHugh Dickins 
15221c2fb7a4SAndrea Arcangeli void __ksm_exit(struct mm_struct *mm)
1523f8af4da3SHugh Dickins {
1524cd551f97SHugh Dickins 	struct mm_slot *mm_slot;
15259ba69294SHugh Dickins 	int easy_to_free = 0;
1526cd551f97SHugh Dickins 
152731dbd01fSIzik Eidus 	/*
15289ba69294SHugh Dickins 	 * This process is exiting: if it's straightforward (as is the
15299ba69294SHugh Dickins 	 * case when ksmd was never running), free mm_slot immediately.
15309ba69294SHugh Dickins 	 * But if it's at the cursor or has rmap_items linked to it, use
15319ba69294SHugh Dickins 	 * mmap_sem to synchronize with any break_cows before pagetables
15329ba69294SHugh Dickins 	 * are freed, and leave the mm_slot on the list for ksmd to free.
15339ba69294SHugh Dickins 	 * Beware: ksm may already have noticed it exiting and freed the slot.
153431dbd01fSIzik Eidus 	 */
15359ba69294SHugh Dickins 
1536cd551f97SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
1537cd551f97SHugh Dickins 	mm_slot = get_mm_slot(mm);
15389ba69294SHugh Dickins 	if (mm_slot && ksm_scan.mm_slot != mm_slot) {
15396514d511SHugh Dickins 		if (!mm_slot->rmap_list) {
1540cd551f97SHugh Dickins 			hlist_del(&mm_slot->link);
1541cd551f97SHugh Dickins 			list_del(&mm_slot->mm_list);
15429ba69294SHugh Dickins 			easy_to_free = 1;
15439ba69294SHugh Dickins 		} else {
15449ba69294SHugh Dickins 			list_move(&mm_slot->mm_list,
15459ba69294SHugh Dickins 				  &ksm_scan.mm_slot->mm_list);
15469ba69294SHugh Dickins 		}
15479ba69294SHugh Dickins 	}
1548cd551f97SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
1549cd551f97SHugh Dickins 
15509ba69294SHugh Dickins 	if (easy_to_free) {
1551cd551f97SHugh Dickins 		free_mm_slot(mm_slot);
1552cd551f97SHugh Dickins 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
15539ba69294SHugh Dickins 		mmdrop(mm);
15549ba69294SHugh Dickins 	} else if (mm_slot) {
15559ba69294SHugh Dickins 		down_write(&mm->mmap_sem);
15569ba69294SHugh Dickins 		up_write(&mm->mmap_sem);
15579ba69294SHugh Dickins 	}
1558f8af4da3SHugh Dickins }
155931dbd01fSIzik Eidus 
15605ad64688SHugh Dickins struct page *ksm_does_need_to_copy(struct page *page,
15615ad64688SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
15625ad64688SHugh Dickins {
15635ad64688SHugh Dickins 	struct page *new_page;
15645ad64688SHugh Dickins 
15655ad64688SHugh Dickins 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
15665ad64688SHugh Dickins 	if (new_page) {
15675ad64688SHugh Dickins 		copy_user_highpage(new_page, page, address, vma);
15685ad64688SHugh Dickins 
15695ad64688SHugh Dickins 		SetPageDirty(new_page);
15705ad64688SHugh Dickins 		__SetPageUptodate(new_page);
15715ad64688SHugh Dickins 		SetPageSwapBacked(new_page);
15725ad64688SHugh Dickins 		__set_page_locked(new_page);
15735ad64688SHugh Dickins 
15745ad64688SHugh Dickins 		if (page_evictable(new_page, vma))
15755ad64688SHugh Dickins 			lru_cache_add_lru(new_page, LRU_ACTIVE_ANON);
15765ad64688SHugh Dickins 		else
15775ad64688SHugh Dickins 			add_page_to_unevictable_list(new_page);
15785ad64688SHugh Dickins 	}
15795ad64688SHugh Dickins 
15805ad64688SHugh Dickins 	return new_page;
15815ad64688SHugh Dickins }
15825ad64688SHugh Dickins 
15835ad64688SHugh Dickins int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg,
15845ad64688SHugh Dickins 			unsigned long *vm_flags)
15855ad64688SHugh Dickins {
15865ad64688SHugh Dickins 	struct stable_node *stable_node;
15875ad64688SHugh Dickins 	struct rmap_item *rmap_item;
15885ad64688SHugh Dickins 	struct hlist_node *hlist;
15895ad64688SHugh Dickins 	unsigned int mapcount = page_mapcount(page);
15905ad64688SHugh Dickins 	int referenced = 0;
1591db114b83SHugh Dickins 	int search_new_forks = 0;
15925ad64688SHugh Dickins 
15935ad64688SHugh Dickins 	VM_BUG_ON(!PageKsm(page));
15945ad64688SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
15955ad64688SHugh Dickins 
15965ad64688SHugh Dickins 	stable_node = page_stable_node(page);
15975ad64688SHugh Dickins 	if (!stable_node)
15985ad64688SHugh Dickins 		return 0;
1599db114b83SHugh Dickins again:
16005ad64688SHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1601db114b83SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
16025beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1603db114b83SHugh Dickins 		struct vm_area_struct *vma;
1604db114b83SHugh Dickins 
1605cba48b98SRik van Riel 		anon_vma_lock(anon_vma);
16065beb4930SRik van Riel 		list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
16075beb4930SRik van Riel 			vma = vmac->vma;
1608db114b83SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1609db114b83SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1610db114b83SHugh Dickins 				continue;
1611db114b83SHugh Dickins 			/*
1612db114b83SHugh Dickins 			 * Initially we examine only the vma which covers this
1613db114b83SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1614db114b83SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1615db114b83SHugh Dickins 			 * were forked from the original since ksmd passed.
1616db114b83SHugh Dickins 			 */
1617db114b83SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
16185ad64688SHugh Dickins 				continue;
16195ad64688SHugh Dickins 
1620db114b83SHugh Dickins 			if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
1621db114b83SHugh Dickins 				continue;
16225ad64688SHugh Dickins 
16235ad64688SHugh Dickins 			referenced += page_referenced_one(page, vma,
16245ad64688SHugh Dickins 				rmap_item->address, &mapcount, vm_flags);
1625db114b83SHugh Dickins 			if (!search_new_forks || !mapcount)
1626db114b83SHugh Dickins 				break;
1627db114b83SHugh Dickins 		}
1628cba48b98SRik van Riel 		anon_vma_unlock(anon_vma);
16295ad64688SHugh Dickins 		if (!mapcount)
16305ad64688SHugh Dickins 			goto out;
16315ad64688SHugh Dickins 	}
1632db114b83SHugh Dickins 	if (!search_new_forks++)
1633db114b83SHugh Dickins 		goto again;
16345ad64688SHugh Dickins out:
16355ad64688SHugh Dickins 	return referenced;
16365ad64688SHugh Dickins }
16375ad64688SHugh Dickins 
16385ad64688SHugh Dickins int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
16395ad64688SHugh Dickins {
16405ad64688SHugh Dickins 	struct stable_node *stable_node;
16415ad64688SHugh Dickins 	struct hlist_node *hlist;
16425ad64688SHugh Dickins 	struct rmap_item *rmap_item;
16435ad64688SHugh Dickins 	int ret = SWAP_AGAIN;
1644db114b83SHugh Dickins 	int search_new_forks = 0;
16455ad64688SHugh Dickins 
16465ad64688SHugh Dickins 	VM_BUG_ON(!PageKsm(page));
16475ad64688SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
16485ad64688SHugh Dickins 
16495ad64688SHugh Dickins 	stable_node = page_stable_node(page);
16505ad64688SHugh Dickins 	if (!stable_node)
16515ad64688SHugh Dickins 		return SWAP_FAIL;
1652db114b83SHugh Dickins again:
16535ad64688SHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1654db114b83SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
16555beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1656db114b83SHugh Dickins 		struct vm_area_struct *vma;
16575ad64688SHugh Dickins 
1658cba48b98SRik van Riel 		anon_vma_lock(anon_vma);
16595beb4930SRik van Riel 		list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
16605beb4930SRik van Riel 			vma = vmac->vma;
1661db114b83SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1662db114b83SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1663db114b83SHugh Dickins 				continue;
1664db114b83SHugh Dickins 			/*
1665db114b83SHugh Dickins 			 * Initially we examine only the vma which covers this
1666db114b83SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1667db114b83SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1668db114b83SHugh Dickins 			 * were forked from the original since ksmd passed.
1669db114b83SHugh Dickins 			 */
1670db114b83SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1671db114b83SHugh Dickins 				continue;
1672db114b83SHugh Dickins 
1673db114b83SHugh Dickins 			ret = try_to_unmap_one(page, vma,
1674db114b83SHugh Dickins 					rmap_item->address, flags);
1675db114b83SHugh Dickins 			if (ret != SWAP_AGAIN || !page_mapped(page)) {
1676cba48b98SRik van Riel 				anon_vma_unlock(anon_vma);
16775ad64688SHugh Dickins 				goto out;
16785ad64688SHugh Dickins 			}
1679db114b83SHugh Dickins 		}
1680cba48b98SRik van Riel 		anon_vma_unlock(anon_vma);
1681db114b83SHugh Dickins 	}
1682db114b83SHugh Dickins 	if (!search_new_forks++)
1683db114b83SHugh Dickins 		goto again;
16845ad64688SHugh Dickins out:
16855ad64688SHugh Dickins 	return ret;
16865ad64688SHugh Dickins }
16875ad64688SHugh Dickins 
1688e9995ef9SHugh Dickins #ifdef CONFIG_MIGRATION
1689e9995ef9SHugh Dickins int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
1690e9995ef9SHugh Dickins 		  struct vm_area_struct *, unsigned long, void *), void *arg)
1691e9995ef9SHugh Dickins {
1692e9995ef9SHugh Dickins 	struct stable_node *stable_node;
1693e9995ef9SHugh Dickins 	struct hlist_node *hlist;
1694e9995ef9SHugh Dickins 	struct rmap_item *rmap_item;
1695e9995ef9SHugh Dickins 	int ret = SWAP_AGAIN;
1696e9995ef9SHugh Dickins 	int search_new_forks = 0;
1697e9995ef9SHugh Dickins 
1698e9995ef9SHugh Dickins 	VM_BUG_ON(!PageKsm(page));
1699e9995ef9SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
1700e9995ef9SHugh Dickins 
1701e9995ef9SHugh Dickins 	stable_node = page_stable_node(page);
1702e9995ef9SHugh Dickins 	if (!stable_node)
1703e9995ef9SHugh Dickins 		return ret;
1704e9995ef9SHugh Dickins again:
1705e9995ef9SHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1706e9995ef9SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
17075beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1708e9995ef9SHugh Dickins 		struct vm_area_struct *vma;
1709e9995ef9SHugh Dickins 
1710cba48b98SRik van Riel 		anon_vma_lock(anon_vma);
17115beb4930SRik van Riel 		list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
17125beb4930SRik van Riel 			vma = vmac->vma;
1713e9995ef9SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1714e9995ef9SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1715e9995ef9SHugh Dickins 				continue;
1716e9995ef9SHugh Dickins 			/*
1717e9995ef9SHugh Dickins 			 * Initially we examine only the vma which covers this
1718e9995ef9SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1719e9995ef9SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1720e9995ef9SHugh Dickins 			 * were forked from the original since ksmd passed.
1721e9995ef9SHugh Dickins 			 */
1722e9995ef9SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1723e9995ef9SHugh Dickins 				continue;
1724e9995ef9SHugh Dickins 
1725e9995ef9SHugh Dickins 			ret = rmap_one(page, vma, rmap_item->address, arg);
1726e9995ef9SHugh Dickins 			if (ret != SWAP_AGAIN) {
1727cba48b98SRik van Riel 				anon_vma_unlock(anon_vma);
1728e9995ef9SHugh Dickins 				goto out;
1729e9995ef9SHugh Dickins 			}
1730e9995ef9SHugh Dickins 		}
1731cba48b98SRik van Riel 		anon_vma_unlock(anon_vma);
1732e9995ef9SHugh Dickins 	}
1733e9995ef9SHugh Dickins 	if (!search_new_forks++)
1734e9995ef9SHugh Dickins 		goto again;
1735e9995ef9SHugh Dickins out:
1736e9995ef9SHugh Dickins 	return ret;
1737e9995ef9SHugh Dickins }
1738e9995ef9SHugh Dickins 
1739e9995ef9SHugh Dickins void ksm_migrate_page(struct page *newpage, struct page *oldpage)
1740e9995ef9SHugh Dickins {
1741e9995ef9SHugh Dickins 	struct stable_node *stable_node;
1742e9995ef9SHugh Dickins 
1743e9995ef9SHugh Dickins 	VM_BUG_ON(!PageLocked(oldpage));
1744e9995ef9SHugh Dickins 	VM_BUG_ON(!PageLocked(newpage));
1745e9995ef9SHugh Dickins 	VM_BUG_ON(newpage->mapping != oldpage->mapping);
1746e9995ef9SHugh Dickins 
1747e9995ef9SHugh Dickins 	stable_node = page_stable_node(newpage);
1748e9995ef9SHugh Dickins 	if (stable_node) {
174962b61f61SHugh Dickins 		VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage));
175062b61f61SHugh Dickins 		stable_node->kpfn = page_to_pfn(newpage);
1751e9995ef9SHugh Dickins 	}
1752e9995ef9SHugh Dickins }
1753e9995ef9SHugh Dickins #endif /* CONFIG_MIGRATION */
1754e9995ef9SHugh Dickins 
175562b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE
175662b61f61SHugh Dickins static struct stable_node *ksm_check_stable_tree(unsigned long start_pfn,
175762b61f61SHugh Dickins 						 unsigned long end_pfn)
175862b61f61SHugh Dickins {
175962b61f61SHugh Dickins 	struct rb_node *node;
176062b61f61SHugh Dickins 
176162b61f61SHugh Dickins 	for (node = rb_first(&root_stable_tree); node; node = rb_next(node)) {
176262b61f61SHugh Dickins 		struct stable_node *stable_node;
176362b61f61SHugh Dickins 
176462b61f61SHugh Dickins 		stable_node = rb_entry(node, struct stable_node, node);
176562b61f61SHugh Dickins 		if (stable_node->kpfn >= start_pfn &&
176662b61f61SHugh Dickins 		    stable_node->kpfn < end_pfn)
176762b61f61SHugh Dickins 			return stable_node;
176862b61f61SHugh Dickins 	}
176962b61f61SHugh Dickins 	return NULL;
177062b61f61SHugh Dickins }
177162b61f61SHugh Dickins 
177262b61f61SHugh Dickins static int ksm_memory_callback(struct notifier_block *self,
177362b61f61SHugh Dickins 			       unsigned long action, void *arg)
177462b61f61SHugh Dickins {
177562b61f61SHugh Dickins 	struct memory_notify *mn = arg;
177662b61f61SHugh Dickins 	struct stable_node *stable_node;
177762b61f61SHugh Dickins 
177862b61f61SHugh Dickins 	switch (action) {
177962b61f61SHugh Dickins 	case MEM_GOING_OFFLINE:
178062b61f61SHugh Dickins 		/*
178162b61f61SHugh Dickins 		 * Keep it very simple for now: just lock out ksmd and
178262b61f61SHugh Dickins 		 * MADV_UNMERGEABLE while any memory is going offline.
1783a0b0f58cSKOSAKI Motohiro 		 * mutex_lock_nested() is necessary because lockdep was alarmed
1784a0b0f58cSKOSAKI Motohiro 		 * that here we take ksm_thread_mutex inside notifier chain
1785a0b0f58cSKOSAKI Motohiro 		 * mutex, and later take notifier chain mutex inside
1786a0b0f58cSKOSAKI Motohiro 		 * ksm_thread_mutex to unlock it.   But that's safe because both
1787a0b0f58cSKOSAKI Motohiro 		 * are inside mem_hotplug_mutex.
178862b61f61SHugh Dickins 		 */
1789a0b0f58cSKOSAKI Motohiro 		mutex_lock_nested(&ksm_thread_mutex, SINGLE_DEPTH_NESTING);
179062b61f61SHugh Dickins 		break;
179162b61f61SHugh Dickins 
179262b61f61SHugh Dickins 	case MEM_OFFLINE:
179362b61f61SHugh Dickins 		/*
179462b61f61SHugh Dickins 		 * Most of the work is done by page migration; but there might
179562b61f61SHugh Dickins 		 * be a few stable_nodes left over, still pointing to struct
179662b61f61SHugh Dickins 		 * pages which have been offlined: prune those from the tree.
179762b61f61SHugh Dickins 		 */
179862b61f61SHugh Dickins 		while ((stable_node = ksm_check_stable_tree(mn->start_pfn,
179962b61f61SHugh Dickins 					mn->start_pfn + mn->nr_pages)) != NULL)
180062b61f61SHugh Dickins 			remove_node_from_stable_tree(stable_node);
180162b61f61SHugh Dickins 		/* fallthrough */
180262b61f61SHugh Dickins 
180362b61f61SHugh Dickins 	case MEM_CANCEL_OFFLINE:
180462b61f61SHugh Dickins 		mutex_unlock(&ksm_thread_mutex);
180562b61f61SHugh Dickins 		break;
180662b61f61SHugh Dickins 	}
180762b61f61SHugh Dickins 	return NOTIFY_OK;
180862b61f61SHugh Dickins }
180962b61f61SHugh Dickins #endif /* CONFIG_MEMORY_HOTREMOVE */
181062b61f61SHugh Dickins 
18112ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
18122ffd8679SHugh Dickins /*
18132ffd8679SHugh Dickins  * This all compiles without CONFIG_SYSFS, but is a waste of space.
18142ffd8679SHugh Dickins  */
18152ffd8679SHugh Dickins 
181631dbd01fSIzik Eidus #define KSM_ATTR_RO(_name) \
181731dbd01fSIzik Eidus 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
181831dbd01fSIzik Eidus #define KSM_ATTR(_name) \
181931dbd01fSIzik Eidus 	static struct kobj_attribute _name##_attr = \
182031dbd01fSIzik Eidus 		__ATTR(_name, 0644, _name##_show, _name##_store)
182131dbd01fSIzik Eidus 
182231dbd01fSIzik Eidus static ssize_t sleep_millisecs_show(struct kobject *kobj,
182331dbd01fSIzik Eidus 				    struct kobj_attribute *attr, char *buf)
182431dbd01fSIzik Eidus {
182531dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
182631dbd01fSIzik Eidus }
182731dbd01fSIzik Eidus 
182831dbd01fSIzik Eidus static ssize_t sleep_millisecs_store(struct kobject *kobj,
182931dbd01fSIzik Eidus 				     struct kobj_attribute *attr,
183031dbd01fSIzik Eidus 				     const char *buf, size_t count)
183131dbd01fSIzik Eidus {
183231dbd01fSIzik Eidus 	unsigned long msecs;
183331dbd01fSIzik Eidus 	int err;
183431dbd01fSIzik Eidus 
183531dbd01fSIzik Eidus 	err = strict_strtoul(buf, 10, &msecs);
183631dbd01fSIzik Eidus 	if (err || msecs > UINT_MAX)
183731dbd01fSIzik Eidus 		return -EINVAL;
183831dbd01fSIzik Eidus 
183931dbd01fSIzik Eidus 	ksm_thread_sleep_millisecs = msecs;
184031dbd01fSIzik Eidus 
184131dbd01fSIzik Eidus 	return count;
184231dbd01fSIzik Eidus }
184331dbd01fSIzik Eidus KSM_ATTR(sleep_millisecs);
184431dbd01fSIzik Eidus 
184531dbd01fSIzik Eidus static ssize_t pages_to_scan_show(struct kobject *kobj,
184631dbd01fSIzik Eidus 				  struct kobj_attribute *attr, char *buf)
184731dbd01fSIzik Eidus {
184831dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
184931dbd01fSIzik Eidus }
185031dbd01fSIzik Eidus 
185131dbd01fSIzik Eidus static ssize_t pages_to_scan_store(struct kobject *kobj,
185231dbd01fSIzik Eidus 				   struct kobj_attribute *attr,
185331dbd01fSIzik Eidus 				   const char *buf, size_t count)
185431dbd01fSIzik Eidus {
185531dbd01fSIzik Eidus 	int err;
185631dbd01fSIzik Eidus 	unsigned long nr_pages;
185731dbd01fSIzik Eidus 
185831dbd01fSIzik Eidus 	err = strict_strtoul(buf, 10, &nr_pages);
185931dbd01fSIzik Eidus 	if (err || nr_pages > UINT_MAX)
186031dbd01fSIzik Eidus 		return -EINVAL;
186131dbd01fSIzik Eidus 
186231dbd01fSIzik Eidus 	ksm_thread_pages_to_scan = nr_pages;
186331dbd01fSIzik Eidus 
186431dbd01fSIzik Eidus 	return count;
186531dbd01fSIzik Eidus }
186631dbd01fSIzik Eidus KSM_ATTR(pages_to_scan);
186731dbd01fSIzik Eidus 
186831dbd01fSIzik Eidus static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
186931dbd01fSIzik Eidus 			char *buf)
187031dbd01fSIzik Eidus {
187131dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_run);
187231dbd01fSIzik Eidus }
187331dbd01fSIzik Eidus 
187431dbd01fSIzik Eidus static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
187531dbd01fSIzik Eidus 			 const char *buf, size_t count)
187631dbd01fSIzik Eidus {
187731dbd01fSIzik Eidus 	int err;
187831dbd01fSIzik Eidus 	unsigned long flags;
187931dbd01fSIzik Eidus 
188031dbd01fSIzik Eidus 	err = strict_strtoul(buf, 10, &flags);
188131dbd01fSIzik Eidus 	if (err || flags > UINT_MAX)
188231dbd01fSIzik Eidus 		return -EINVAL;
188331dbd01fSIzik Eidus 	if (flags > KSM_RUN_UNMERGE)
188431dbd01fSIzik Eidus 		return -EINVAL;
188531dbd01fSIzik Eidus 
188631dbd01fSIzik Eidus 	/*
188731dbd01fSIzik Eidus 	 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
188831dbd01fSIzik Eidus 	 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
1889d0f209f6SHugh Dickins 	 * breaking COW to free the pages_shared (but leaves mm_slots
1890d0f209f6SHugh Dickins 	 * on the list for when ksmd may be set running again).
189131dbd01fSIzik Eidus 	 */
189231dbd01fSIzik Eidus 
189331dbd01fSIzik Eidus 	mutex_lock(&ksm_thread_mutex);
189431dbd01fSIzik Eidus 	if (ksm_run != flags) {
189531dbd01fSIzik Eidus 		ksm_run = flags;
1896d952b791SHugh Dickins 		if (flags & KSM_RUN_UNMERGE) {
189735451beeSHugh Dickins 			current->flags |= PF_OOM_ORIGIN;
1898d952b791SHugh Dickins 			err = unmerge_and_remove_all_rmap_items();
189935451beeSHugh Dickins 			current->flags &= ~PF_OOM_ORIGIN;
1900d952b791SHugh Dickins 			if (err) {
1901d952b791SHugh Dickins 				ksm_run = KSM_RUN_STOP;
1902d952b791SHugh Dickins 				count = err;
1903d952b791SHugh Dickins 			}
1904d952b791SHugh Dickins 		}
190531dbd01fSIzik Eidus 	}
190631dbd01fSIzik Eidus 	mutex_unlock(&ksm_thread_mutex);
190731dbd01fSIzik Eidus 
190831dbd01fSIzik Eidus 	if (flags & KSM_RUN_MERGE)
190931dbd01fSIzik Eidus 		wake_up_interruptible(&ksm_thread_wait);
191031dbd01fSIzik Eidus 
191131dbd01fSIzik Eidus 	return count;
191231dbd01fSIzik Eidus }
191331dbd01fSIzik Eidus KSM_ATTR(run);
191431dbd01fSIzik Eidus 
1915b4028260SHugh Dickins static ssize_t pages_shared_show(struct kobject *kobj,
1916b4028260SHugh Dickins 				 struct kobj_attribute *attr, char *buf)
1917b4028260SHugh Dickins {
1918b4028260SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_shared);
1919b4028260SHugh Dickins }
1920b4028260SHugh Dickins KSM_ATTR_RO(pages_shared);
1921b4028260SHugh Dickins 
1922b4028260SHugh Dickins static ssize_t pages_sharing_show(struct kobject *kobj,
1923b4028260SHugh Dickins 				  struct kobj_attribute *attr, char *buf)
1924b4028260SHugh Dickins {
1925e178dfdeSHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_sharing);
1926b4028260SHugh Dickins }
1927b4028260SHugh Dickins KSM_ATTR_RO(pages_sharing);
1928b4028260SHugh Dickins 
1929473b0ce4SHugh Dickins static ssize_t pages_unshared_show(struct kobject *kobj,
1930473b0ce4SHugh Dickins 				   struct kobj_attribute *attr, char *buf)
1931473b0ce4SHugh Dickins {
1932473b0ce4SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_unshared);
1933473b0ce4SHugh Dickins }
1934473b0ce4SHugh Dickins KSM_ATTR_RO(pages_unshared);
1935473b0ce4SHugh Dickins 
1936473b0ce4SHugh Dickins static ssize_t pages_volatile_show(struct kobject *kobj,
1937473b0ce4SHugh Dickins 				   struct kobj_attribute *attr, char *buf)
1938473b0ce4SHugh Dickins {
1939473b0ce4SHugh Dickins 	long ksm_pages_volatile;
1940473b0ce4SHugh Dickins 
1941473b0ce4SHugh Dickins 	ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
1942473b0ce4SHugh Dickins 				- ksm_pages_sharing - ksm_pages_unshared;
1943473b0ce4SHugh Dickins 	/*
1944473b0ce4SHugh Dickins 	 * It was not worth any locking to calculate that statistic,
1945473b0ce4SHugh Dickins 	 * but it might therefore sometimes be negative: conceal that.
1946473b0ce4SHugh Dickins 	 */
1947473b0ce4SHugh Dickins 	if (ksm_pages_volatile < 0)
1948473b0ce4SHugh Dickins 		ksm_pages_volatile = 0;
1949473b0ce4SHugh Dickins 	return sprintf(buf, "%ld\n", ksm_pages_volatile);
1950473b0ce4SHugh Dickins }
1951473b0ce4SHugh Dickins KSM_ATTR_RO(pages_volatile);
1952473b0ce4SHugh Dickins 
1953473b0ce4SHugh Dickins static ssize_t full_scans_show(struct kobject *kobj,
1954473b0ce4SHugh Dickins 			       struct kobj_attribute *attr, char *buf)
1955473b0ce4SHugh Dickins {
1956473b0ce4SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_scan.seqnr);
1957473b0ce4SHugh Dickins }
1958473b0ce4SHugh Dickins KSM_ATTR_RO(full_scans);
1959473b0ce4SHugh Dickins 
196031dbd01fSIzik Eidus static struct attribute *ksm_attrs[] = {
196131dbd01fSIzik Eidus 	&sleep_millisecs_attr.attr,
196231dbd01fSIzik Eidus 	&pages_to_scan_attr.attr,
196331dbd01fSIzik Eidus 	&run_attr.attr,
1964b4028260SHugh Dickins 	&pages_shared_attr.attr,
1965b4028260SHugh Dickins 	&pages_sharing_attr.attr,
1966473b0ce4SHugh Dickins 	&pages_unshared_attr.attr,
1967473b0ce4SHugh Dickins 	&pages_volatile_attr.attr,
1968473b0ce4SHugh Dickins 	&full_scans_attr.attr,
196931dbd01fSIzik Eidus 	NULL,
197031dbd01fSIzik Eidus };
197131dbd01fSIzik Eidus 
197231dbd01fSIzik Eidus static struct attribute_group ksm_attr_group = {
197331dbd01fSIzik Eidus 	.attrs = ksm_attrs,
197431dbd01fSIzik Eidus 	.name = "ksm",
197531dbd01fSIzik Eidus };
19762ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
197731dbd01fSIzik Eidus 
197831dbd01fSIzik Eidus static int __init ksm_init(void)
197931dbd01fSIzik Eidus {
198031dbd01fSIzik Eidus 	struct task_struct *ksm_thread;
198131dbd01fSIzik Eidus 	int err;
198231dbd01fSIzik Eidus 
198331dbd01fSIzik Eidus 	err = ksm_slab_init();
198431dbd01fSIzik Eidus 	if (err)
198531dbd01fSIzik Eidus 		goto out;
198631dbd01fSIzik Eidus 
198731dbd01fSIzik Eidus 	ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
198831dbd01fSIzik Eidus 	if (IS_ERR(ksm_thread)) {
198931dbd01fSIzik Eidus 		printk(KERN_ERR "ksm: creating kthread failed\n");
199031dbd01fSIzik Eidus 		err = PTR_ERR(ksm_thread);
1991d9f8984cSLai Jiangshan 		goto out_free;
199231dbd01fSIzik Eidus 	}
199331dbd01fSIzik Eidus 
19942ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
199531dbd01fSIzik Eidus 	err = sysfs_create_group(mm_kobj, &ksm_attr_group);
199631dbd01fSIzik Eidus 	if (err) {
199731dbd01fSIzik Eidus 		printk(KERN_ERR "ksm: register sysfs failed\n");
19982ffd8679SHugh Dickins 		kthread_stop(ksm_thread);
1999d9f8984cSLai Jiangshan 		goto out_free;
200031dbd01fSIzik Eidus 	}
2001c73602adSHugh Dickins #else
2002c73602adSHugh Dickins 	ksm_run = KSM_RUN_MERGE;	/* no way for user to start it */
2003c73602adSHugh Dickins 
20042ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
200531dbd01fSIzik Eidus 
200662b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE
200762b61f61SHugh Dickins 	/*
200862b61f61SHugh Dickins 	 * Choose a high priority since the callback takes ksm_thread_mutex:
200962b61f61SHugh Dickins 	 * later callbacks could only be taking locks which nest within that.
201062b61f61SHugh Dickins 	 */
201162b61f61SHugh Dickins 	hotplug_memory_notifier(ksm_memory_callback, 100);
201262b61f61SHugh Dickins #endif
201331dbd01fSIzik Eidus 	return 0;
201431dbd01fSIzik Eidus 
2015d9f8984cSLai Jiangshan out_free:
201631dbd01fSIzik Eidus 	ksm_slab_free();
201731dbd01fSIzik Eidus out:
201831dbd01fSIzik Eidus 	return err;
201931dbd01fSIzik Eidus }
202031dbd01fSIzik Eidus module_init(ksm_init)
2021