xref: /linux/mm/ksm.c (revision 6213055f2c068b63078649457391ecea9b489ea3)
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>
364ca3a69bSSasha Levin #include <linux/hashtable.h>
37878aee7dSAndrea Arcangeli #include <linux/freezer.h>
3872788c38SDavid Rientjes #include <linux/oom.h>
3990bd6fd3SPetr Holasek #include <linux/numa.h>
40f8af4da3SHugh Dickins 
4131dbd01fSIzik Eidus #include <asm/tlbflush.h>
4273848b46SHugh Dickins #include "internal.h"
4331dbd01fSIzik Eidus 
44e850dcf5SHugh Dickins #ifdef CONFIG_NUMA
45e850dcf5SHugh Dickins #define NUMA(x)		(x)
46e850dcf5SHugh Dickins #define DO_NUMA(x)	do { (x); } while (0)
47e850dcf5SHugh Dickins #else
48e850dcf5SHugh Dickins #define NUMA(x)		(0)
49e850dcf5SHugh Dickins #define DO_NUMA(x)	do { } while (0)
50e850dcf5SHugh Dickins #endif
51e850dcf5SHugh Dickins 
5231dbd01fSIzik Eidus /*
5331dbd01fSIzik Eidus  * A few notes about the KSM scanning process,
5431dbd01fSIzik Eidus  * to make it easier to understand the data structures below:
5531dbd01fSIzik Eidus  *
5631dbd01fSIzik Eidus  * In order to reduce excessive scanning, KSM sorts the memory pages by their
5731dbd01fSIzik Eidus  * contents into a data structure that holds pointers to the pages' locations.
5831dbd01fSIzik Eidus  *
5931dbd01fSIzik Eidus  * Since the contents of the pages may change at any moment, KSM cannot just
6031dbd01fSIzik Eidus  * insert the pages into a normal sorted tree and expect it to find anything.
6131dbd01fSIzik Eidus  * Therefore KSM uses two data structures - the stable and the unstable tree.
6231dbd01fSIzik Eidus  *
6331dbd01fSIzik Eidus  * The stable tree holds pointers to all the merged pages (ksm pages), sorted
6431dbd01fSIzik Eidus  * by their contents.  Because each such page is write-protected, searching on
6531dbd01fSIzik Eidus  * this tree is fully assured to be working (except when pages are unmapped),
6631dbd01fSIzik Eidus  * and therefore this tree is called the stable tree.
6731dbd01fSIzik Eidus  *
6831dbd01fSIzik Eidus  * In addition to the stable tree, KSM uses a second data structure called the
6931dbd01fSIzik Eidus  * unstable tree: this tree holds pointers to pages which have been found to
7031dbd01fSIzik Eidus  * be "unchanged for a period of time".  The unstable tree sorts these pages
7131dbd01fSIzik Eidus  * by their contents, but since they are not write-protected, KSM cannot rely
7231dbd01fSIzik Eidus  * upon the unstable tree to work correctly - the unstable tree is liable to
7331dbd01fSIzik Eidus  * be corrupted as its contents are modified, and so it is called unstable.
7431dbd01fSIzik Eidus  *
7531dbd01fSIzik Eidus  * KSM solves this problem by several techniques:
7631dbd01fSIzik Eidus  *
7731dbd01fSIzik Eidus  * 1) The unstable tree is flushed every time KSM completes scanning all
7831dbd01fSIzik Eidus  *    memory areas, and then the tree is rebuilt again from the beginning.
7931dbd01fSIzik Eidus  * 2) KSM will only insert into the unstable tree, pages whose hash value
8031dbd01fSIzik Eidus  *    has not changed since the previous scan of all memory areas.
8131dbd01fSIzik Eidus  * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
8231dbd01fSIzik Eidus  *    colors of the nodes and not on their contents, assuring that even when
8331dbd01fSIzik Eidus  *    the tree gets "corrupted" it won't get out of balance, so scanning time
8431dbd01fSIzik Eidus  *    remains the same (also, searching and inserting nodes in an rbtree uses
8531dbd01fSIzik Eidus  *    the same algorithm, so we have no overhead when we flush and rebuild).
8631dbd01fSIzik Eidus  * 4) KSM never flushes the stable tree, which means that even if it were to
8731dbd01fSIzik Eidus  *    take 10 attempts to find a page in the unstable tree, once it is found,
8831dbd01fSIzik Eidus  *    it is secured in the stable tree.  (When we scan a new page, we first
8931dbd01fSIzik Eidus  *    compare it against the stable tree, and then against the unstable tree.)
908fdb3dbfSHugh Dickins  *
918fdb3dbfSHugh Dickins  * If the merge_across_nodes tunable is unset, then KSM maintains multiple
928fdb3dbfSHugh Dickins  * stable trees and multiple unstable trees: one of each for each NUMA node.
9331dbd01fSIzik Eidus  */
9431dbd01fSIzik Eidus 
9531dbd01fSIzik Eidus /**
9631dbd01fSIzik Eidus  * struct mm_slot - ksm information per mm that is being scanned
9731dbd01fSIzik Eidus  * @link: link to the mm_slots hash list
9831dbd01fSIzik Eidus  * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
996514d511SHugh Dickins  * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
10031dbd01fSIzik Eidus  * @mm: the mm that this information is valid for
10131dbd01fSIzik Eidus  */
10231dbd01fSIzik Eidus struct mm_slot {
10331dbd01fSIzik Eidus 	struct hlist_node link;
10431dbd01fSIzik Eidus 	struct list_head mm_list;
1056514d511SHugh Dickins 	struct rmap_item *rmap_list;
10631dbd01fSIzik Eidus 	struct mm_struct *mm;
10731dbd01fSIzik Eidus };
10831dbd01fSIzik Eidus 
10931dbd01fSIzik Eidus /**
11031dbd01fSIzik Eidus  * struct ksm_scan - cursor for scanning
11131dbd01fSIzik Eidus  * @mm_slot: the current mm_slot we are scanning
11231dbd01fSIzik Eidus  * @address: the next address inside that to be scanned
1136514d511SHugh Dickins  * @rmap_list: link to the next rmap to be scanned in the rmap_list
11431dbd01fSIzik Eidus  * @seqnr: count of completed full scans (needed when removing unstable node)
11531dbd01fSIzik Eidus  *
11631dbd01fSIzik Eidus  * There is only the one ksm_scan instance of this cursor structure.
11731dbd01fSIzik Eidus  */
11831dbd01fSIzik Eidus struct ksm_scan {
11931dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
12031dbd01fSIzik Eidus 	unsigned long address;
1216514d511SHugh Dickins 	struct rmap_item **rmap_list;
12231dbd01fSIzik Eidus 	unsigned long seqnr;
12331dbd01fSIzik Eidus };
12431dbd01fSIzik Eidus 
12531dbd01fSIzik Eidus /**
1267b6ba2c7SHugh Dickins  * struct stable_node - node of the stable rbtree
1277b6ba2c7SHugh Dickins  * @node: rb node of this ksm page in the stable tree
1284146d2d6SHugh Dickins  * @head: (overlaying parent) &migrate_nodes indicates temporarily on that list
1294146d2d6SHugh Dickins  * @list: linked into migrate_nodes, pending placement in the proper node tree
1307b6ba2c7SHugh Dickins  * @hlist: hlist head of rmap_items using this ksm page
1314146d2d6SHugh Dickins  * @kpfn: page frame number of this ksm page (perhaps temporarily on wrong nid)
1324146d2d6SHugh Dickins  * @nid: NUMA node id of stable tree in which linked (may not match kpfn)
1337b6ba2c7SHugh Dickins  */
1347b6ba2c7SHugh Dickins struct stable_node {
1354146d2d6SHugh Dickins 	union {
1364146d2d6SHugh Dickins 		struct rb_node node;	/* when node of stable tree */
1374146d2d6SHugh Dickins 		struct {		/* when listed for migration */
1384146d2d6SHugh Dickins 			struct list_head *head;
1394146d2d6SHugh Dickins 			struct list_head list;
1404146d2d6SHugh Dickins 		};
1414146d2d6SHugh Dickins 	};
1427b6ba2c7SHugh Dickins 	struct hlist_head hlist;
14362b61f61SHugh Dickins 	unsigned long kpfn;
1444146d2d6SHugh Dickins #ifdef CONFIG_NUMA
1454146d2d6SHugh Dickins 	int nid;
1464146d2d6SHugh Dickins #endif
1477b6ba2c7SHugh Dickins };
1487b6ba2c7SHugh Dickins 
1497b6ba2c7SHugh Dickins /**
15031dbd01fSIzik Eidus  * struct rmap_item - reverse mapping item for virtual addresses
1516514d511SHugh Dickins  * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
152db114b83SHugh Dickins  * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
153bc56620bSHugh Dickins  * @nid: NUMA node id of unstable tree in which linked (may not match page)
15431dbd01fSIzik Eidus  * @mm: the memory structure this rmap_item is pointing into
15531dbd01fSIzik Eidus  * @address: the virtual address this rmap_item tracks (+ flags in low bits)
15631dbd01fSIzik Eidus  * @oldchecksum: previous checksum of the page at that virtual address
1577b6ba2c7SHugh Dickins  * @node: rb node of this rmap_item in the unstable tree
1587b6ba2c7SHugh Dickins  * @head: pointer to stable_node heading this list in the stable tree
1597b6ba2c7SHugh Dickins  * @hlist: link into hlist of rmap_items hanging off that stable_node
16031dbd01fSIzik Eidus  */
16131dbd01fSIzik Eidus struct rmap_item {
1626514d511SHugh Dickins 	struct rmap_item *rmap_list;
163bc56620bSHugh Dickins 	union {
164db114b83SHugh Dickins 		struct anon_vma *anon_vma;	/* when stable */
165bc56620bSHugh Dickins #ifdef CONFIG_NUMA
166bc56620bSHugh Dickins 		int nid;		/* when node of unstable tree */
167bc56620bSHugh Dickins #endif
168bc56620bSHugh Dickins 	};
16931dbd01fSIzik Eidus 	struct mm_struct *mm;
17031dbd01fSIzik Eidus 	unsigned long address;		/* + low bits used for flags below */
17131dbd01fSIzik Eidus 	unsigned int oldchecksum;	/* when unstable */
17231dbd01fSIzik Eidus 	union {
1737b6ba2c7SHugh Dickins 		struct rb_node node;	/* when node of unstable tree */
1747b6ba2c7SHugh Dickins 		struct {		/* when listed from stable tree */
1757b6ba2c7SHugh Dickins 			struct stable_node *head;
1767b6ba2c7SHugh Dickins 			struct hlist_node hlist;
1777b6ba2c7SHugh Dickins 		};
17831dbd01fSIzik Eidus 	};
17931dbd01fSIzik Eidus };
18031dbd01fSIzik Eidus 
18131dbd01fSIzik Eidus #define SEQNR_MASK	0x0ff	/* low bits of unstable tree seqnr */
1827b6ba2c7SHugh Dickins #define UNSTABLE_FLAG	0x100	/* is a node of the unstable tree */
1837b6ba2c7SHugh Dickins #define STABLE_FLAG	0x200	/* is listed from the stable tree */
18431dbd01fSIzik Eidus 
18531dbd01fSIzik Eidus /* The stable and unstable tree heads */
186ef53d16cSHugh Dickins static struct rb_root one_stable_tree[1] = { RB_ROOT };
187ef53d16cSHugh Dickins static struct rb_root one_unstable_tree[1] = { RB_ROOT };
188ef53d16cSHugh Dickins static struct rb_root *root_stable_tree = one_stable_tree;
189ef53d16cSHugh Dickins static struct rb_root *root_unstable_tree = one_unstable_tree;
19031dbd01fSIzik Eidus 
1914146d2d6SHugh Dickins /* Recently migrated nodes of stable tree, pending proper placement */
1924146d2d6SHugh Dickins static LIST_HEAD(migrate_nodes);
1934146d2d6SHugh Dickins 
1944ca3a69bSSasha Levin #define MM_SLOTS_HASH_BITS 10
1954ca3a69bSSasha Levin static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
19631dbd01fSIzik Eidus 
19731dbd01fSIzik Eidus static struct mm_slot ksm_mm_head = {
19831dbd01fSIzik Eidus 	.mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
19931dbd01fSIzik Eidus };
20031dbd01fSIzik Eidus static struct ksm_scan ksm_scan = {
20131dbd01fSIzik Eidus 	.mm_slot = &ksm_mm_head,
20231dbd01fSIzik Eidus };
20331dbd01fSIzik Eidus 
20431dbd01fSIzik Eidus static struct kmem_cache *rmap_item_cache;
2057b6ba2c7SHugh Dickins static struct kmem_cache *stable_node_cache;
20631dbd01fSIzik Eidus static struct kmem_cache *mm_slot_cache;
20731dbd01fSIzik Eidus 
20831dbd01fSIzik Eidus /* The number of nodes in the stable tree */
209b4028260SHugh Dickins static unsigned long ksm_pages_shared;
21031dbd01fSIzik Eidus 
211e178dfdeSHugh Dickins /* The number of page slots additionally sharing those nodes */
212b4028260SHugh Dickins static unsigned long ksm_pages_sharing;
21331dbd01fSIzik Eidus 
214473b0ce4SHugh Dickins /* The number of nodes in the unstable tree */
215473b0ce4SHugh Dickins static unsigned long ksm_pages_unshared;
216473b0ce4SHugh Dickins 
217473b0ce4SHugh Dickins /* The number of rmap_items in use: to calculate pages_volatile */
218473b0ce4SHugh Dickins static unsigned long ksm_rmap_items;
219473b0ce4SHugh Dickins 
22031dbd01fSIzik Eidus /* Number of pages ksmd should scan in one batch */
2212c6854fdSIzik Eidus static unsigned int ksm_thread_pages_to_scan = 100;
22231dbd01fSIzik Eidus 
22331dbd01fSIzik Eidus /* Milliseconds ksmd should sleep between batches */
2242ffd8679SHugh Dickins static unsigned int ksm_thread_sleep_millisecs = 20;
22531dbd01fSIzik Eidus 
226e850dcf5SHugh Dickins #ifdef CONFIG_NUMA
22790bd6fd3SPetr Holasek /* Zeroed when merging across nodes is not allowed */
22890bd6fd3SPetr Holasek static unsigned int ksm_merge_across_nodes = 1;
229ef53d16cSHugh Dickins static int ksm_nr_node_ids = 1;
230e850dcf5SHugh Dickins #else
231e850dcf5SHugh Dickins #define ksm_merge_across_nodes	1U
232ef53d16cSHugh Dickins #define ksm_nr_node_ids		1
233e850dcf5SHugh Dickins #endif
23490bd6fd3SPetr Holasek 
23531dbd01fSIzik Eidus #define KSM_RUN_STOP	0
23631dbd01fSIzik Eidus #define KSM_RUN_MERGE	1
23731dbd01fSIzik Eidus #define KSM_RUN_UNMERGE	2
238ef4d43a8SHugh Dickins #define KSM_RUN_OFFLINE	4
239ef4d43a8SHugh Dickins static unsigned long ksm_run = KSM_RUN_STOP;
240ef4d43a8SHugh Dickins static void wait_while_offlining(void);
24131dbd01fSIzik Eidus 
24231dbd01fSIzik Eidus static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
24331dbd01fSIzik Eidus static DEFINE_MUTEX(ksm_thread_mutex);
24431dbd01fSIzik Eidus static DEFINE_SPINLOCK(ksm_mmlist_lock);
24531dbd01fSIzik Eidus 
24631dbd01fSIzik Eidus #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
24731dbd01fSIzik Eidus 		sizeof(struct __struct), __alignof__(struct __struct),\
24831dbd01fSIzik Eidus 		(__flags), NULL)
24931dbd01fSIzik Eidus 
25031dbd01fSIzik Eidus static int __init ksm_slab_init(void)
25131dbd01fSIzik Eidus {
25231dbd01fSIzik Eidus 	rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
25331dbd01fSIzik Eidus 	if (!rmap_item_cache)
25431dbd01fSIzik Eidus 		goto out;
25531dbd01fSIzik Eidus 
2567b6ba2c7SHugh Dickins 	stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
2577b6ba2c7SHugh Dickins 	if (!stable_node_cache)
2587b6ba2c7SHugh Dickins 		goto out_free1;
2597b6ba2c7SHugh Dickins 
26031dbd01fSIzik Eidus 	mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
26131dbd01fSIzik Eidus 	if (!mm_slot_cache)
2627b6ba2c7SHugh Dickins 		goto out_free2;
26331dbd01fSIzik Eidus 
26431dbd01fSIzik Eidus 	return 0;
26531dbd01fSIzik Eidus 
2667b6ba2c7SHugh Dickins out_free2:
2677b6ba2c7SHugh Dickins 	kmem_cache_destroy(stable_node_cache);
2687b6ba2c7SHugh Dickins out_free1:
26931dbd01fSIzik Eidus 	kmem_cache_destroy(rmap_item_cache);
27031dbd01fSIzik Eidus out:
27131dbd01fSIzik Eidus 	return -ENOMEM;
27231dbd01fSIzik Eidus }
27331dbd01fSIzik Eidus 
27431dbd01fSIzik Eidus static void __init ksm_slab_free(void)
27531dbd01fSIzik Eidus {
27631dbd01fSIzik Eidus 	kmem_cache_destroy(mm_slot_cache);
2777b6ba2c7SHugh Dickins 	kmem_cache_destroy(stable_node_cache);
27831dbd01fSIzik Eidus 	kmem_cache_destroy(rmap_item_cache);
27931dbd01fSIzik Eidus 	mm_slot_cache = NULL;
28031dbd01fSIzik Eidus }
28131dbd01fSIzik Eidus 
28231dbd01fSIzik Eidus static inline struct rmap_item *alloc_rmap_item(void)
28331dbd01fSIzik Eidus {
284473b0ce4SHugh Dickins 	struct rmap_item *rmap_item;
285473b0ce4SHugh Dickins 
2865b398e41Szhong jiang 	rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL |
2875b398e41Szhong jiang 						__GFP_NORETRY | __GFP_NOWARN);
288473b0ce4SHugh Dickins 	if (rmap_item)
289473b0ce4SHugh Dickins 		ksm_rmap_items++;
290473b0ce4SHugh Dickins 	return rmap_item;
29131dbd01fSIzik Eidus }
29231dbd01fSIzik Eidus 
29331dbd01fSIzik Eidus static inline void free_rmap_item(struct rmap_item *rmap_item)
29431dbd01fSIzik Eidus {
295473b0ce4SHugh Dickins 	ksm_rmap_items--;
29631dbd01fSIzik Eidus 	rmap_item->mm = NULL;	/* debug safety */
29731dbd01fSIzik Eidus 	kmem_cache_free(rmap_item_cache, rmap_item);
29831dbd01fSIzik Eidus }
29931dbd01fSIzik Eidus 
3007b6ba2c7SHugh Dickins static inline struct stable_node *alloc_stable_node(void)
3017b6ba2c7SHugh Dickins {
302*6213055fSzhong jiang 	/*
303*6213055fSzhong jiang 	 * The allocation can take too long with GFP_KERNEL when memory is under
304*6213055fSzhong jiang 	 * pressure, which may lead to hung task warnings.  Adding __GFP_HIGH
305*6213055fSzhong jiang 	 * grants access to memory reserves, helping to avoid this problem.
306*6213055fSzhong jiang 	 */
307*6213055fSzhong jiang 	return kmem_cache_alloc(stable_node_cache, GFP_KERNEL | __GFP_HIGH);
3087b6ba2c7SHugh Dickins }
3097b6ba2c7SHugh Dickins 
3107b6ba2c7SHugh Dickins static inline void free_stable_node(struct stable_node *stable_node)
3117b6ba2c7SHugh Dickins {
3127b6ba2c7SHugh Dickins 	kmem_cache_free(stable_node_cache, stable_node);
3137b6ba2c7SHugh Dickins }
3147b6ba2c7SHugh Dickins 
31531dbd01fSIzik Eidus static inline struct mm_slot *alloc_mm_slot(void)
31631dbd01fSIzik Eidus {
31731dbd01fSIzik Eidus 	if (!mm_slot_cache)	/* initialization failed */
31831dbd01fSIzik Eidus 		return NULL;
31931dbd01fSIzik Eidus 	return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
32031dbd01fSIzik Eidus }
32131dbd01fSIzik Eidus 
32231dbd01fSIzik Eidus static inline void free_mm_slot(struct mm_slot *mm_slot)
32331dbd01fSIzik Eidus {
32431dbd01fSIzik Eidus 	kmem_cache_free(mm_slot_cache, mm_slot);
32531dbd01fSIzik Eidus }
32631dbd01fSIzik Eidus 
32731dbd01fSIzik Eidus static struct mm_slot *get_mm_slot(struct mm_struct *mm)
32831dbd01fSIzik Eidus {
3294ca3a69bSSasha Levin 	struct mm_slot *slot;
33031dbd01fSIzik Eidus 
331b67bfe0dSSasha Levin 	hash_for_each_possible(mm_slots_hash, slot, link, (unsigned long)mm)
3324ca3a69bSSasha Levin 		if (slot->mm == mm)
3334ca3a69bSSasha Levin 			return slot;
3344ca3a69bSSasha Levin 
33531dbd01fSIzik Eidus 	return NULL;
33631dbd01fSIzik Eidus }
33731dbd01fSIzik Eidus 
33831dbd01fSIzik Eidus static void insert_to_mm_slots_hash(struct mm_struct *mm,
33931dbd01fSIzik Eidus 				    struct mm_slot *mm_slot)
34031dbd01fSIzik Eidus {
34131dbd01fSIzik Eidus 	mm_slot->mm = mm;
3424ca3a69bSSasha Levin 	hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm);
34331dbd01fSIzik Eidus }
34431dbd01fSIzik Eidus 
34531dbd01fSIzik Eidus /*
346a913e182SHugh Dickins  * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
347a913e182SHugh Dickins  * page tables after it has passed through ksm_exit() - which, if necessary,
348a913e182SHugh Dickins  * takes mmap_sem briefly to serialize against them.  ksm_exit() does not set
349a913e182SHugh Dickins  * a special flag: they can just back out as soon as mm_users goes to zero.
350a913e182SHugh Dickins  * ksm_test_exit() is used throughout to make this test for exit: in some
351a913e182SHugh Dickins  * places for correctness, in some places just to avoid unnecessary work.
352a913e182SHugh Dickins  */
353a913e182SHugh Dickins static inline bool ksm_test_exit(struct mm_struct *mm)
354a913e182SHugh Dickins {
355a913e182SHugh Dickins 	return atomic_read(&mm->mm_users) == 0;
356a913e182SHugh Dickins }
357a913e182SHugh Dickins 
358a913e182SHugh Dickins /*
35931dbd01fSIzik Eidus  * We use break_ksm to break COW on a ksm page: it's a stripped down
36031dbd01fSIzik Eidus  *
361d4edcf0dSDave Hansen  *	if (get_user_pages(addr, 1, 1, 1, &page, NULL) == 1)
36231dbd01fSIzik Eidus  *		put_page(page);
36331dbd01fSIzik Eidus  *
36431dbd01fSIzik Eidus  * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
36531dbd01fSIzik Eidus  * in case the application has unmapped and remapped mm,addr meanwhile.
36631dbd01fSIzik Eidus  * Could a ksm page appear anywhere else?  Actually yes, in a VM_PFNMAP
36731dbd01fSIzik Eidus  * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
3681b2ee126SDave Hansen  *
3691b2ee126SDave Hansen  * FAULT_FLAG/FOLL_REMOTE are because we do this outside the context
3701b2ee126SDave Hansen  * of the process that owns 'vma'.  We also do not want to enforce
3711b2ee126SDave Hansen  * protection keys here anyway.
37231dbd01fSIzik Eidus  */
373d952b791SHugh Dickins static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
37431dbd01fSIzik Eidus {
37531dbd01fSIzik Eidus 	struct page *page;
376d952b791SHugh Dickins 	int ret = 0;
37731dbd01fSIzik Eidus 
37831dbd01fSIzik Eidus 	do {
37931dbd01fSIzik Eidus 		cond_resched();
3801b2ee126SDave Hansen 		page = follow_page(vma, addr,
3811b2ee126SDave Hansen 				FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE);
38222eccdd7SDan Carpenter 		if (IS_ERR_OR_NULL(page))
38331dbd01fSIzik Eidus 			break;
38431dbd01fSIzik Eidus 		if (PageKsm(page))
385dcddffd4SKirill A. Shutemov 			ret = handle_mm_fault(vma, addr,
386dcddffd4SKirill A. Shutemov 					FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE);
38731dbd01fSIzik Eidus 		else
38831dbd01fSIzik Eidus 			ret = VM_FAULT_WRITE;
38931dbd01fSIzik Eidus 		put_page(page);
39033692f27SLinus Torvalds 	} while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | VM_FAULT_OOM)));
391d952b791SHugh Dickins 	/*
392d952b791SHugh Dickins 	 * We must loop because handle_mm_fault() may back out if there's
393d952b791SHugh Dickins 	 * any difficulty e.g. if pte accessed bit gets updated concurrently.
394d952b791SHugh Dickins 	 *
395d952b791SHugh Dickins 	 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
396d952b791SHugh Dickins 	 * COW has been broken, even if the vma does not permit VM_WRITE;
397d952b791SHugh Dickins 	 * but note that a concurrent fault might break PageKsm for us.
398d952b791SHugh Dickins 	 *
399d952b791SHugh Dickins 	 * VM_FAULT_SIGBUS could occur if we race with truncation of the
400d952b791SHugh Dickins 	 * backing file, which also invalidates anonymous pages: that's
401d952b791SHugh Dickins 	 * okay, that truncation will have unmapped the PageKsm for us.
402d952b791SHugh Dickins 	 *
403d952b791SHugh Dickins 	 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
404d952b791SHugh Dickins 	 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
405d952b791SHugh Dickins 	 * current task has TIF_MEMDIE set, and will be OOM killed on return
406d952b791SHugh Dickins 	 * to user; and ksmd, having no mm, would never be chosen for that.
407d952b791SHugh Dickins 	 *
408d952b791SHugh Dickins 	 * But if the mm is in a limited mem_cgroup, then the fault may fail
409d952b791SHugh Dickins 	 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
410d952b791SHugh Dickins 	 * even ksmd can fail in this way - though it's usually breaking ksm
411d952b791SHugh Dickins 	 * just to undo a merge it made a moment before, so unlikely to oom.
412d952b791SHugh Dickins 	 *
413d952b791SHugh Dickins 	 * That's a pity: we might therefore have more kernel pages allocated
414d952b791SHugh Dickins 	 * than we're counting as nodes in the stable tree; but ksm_do_scan
415d952b791SHugh Dickins 	 * will retry to break_cow on each pass, so should recover the page
416d952b791SHugh Dickins 	 * in due course.  The important thing is to not let VM_MERGEABLE
417d952b791SHugh Dickins 	 * be cleared while any such pages might remain in the area.
418d952b791SHugh Dickins 	 */
419d952b791SHugh Dickins 	return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
42031dbd01fSIzik Eidus }
42131dbd01fSIzik Eidus 
422ef694222SBob Liu static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm,
423ef694222SBob Liu 		unsigned long addr)
424ef694222SBob Liu {
425ef694222SBob Liu 	struct vm_area_struct *vma;
426ef694222SBob Liu 	if (ksm_test_exit(mm))
427ef694222SBob Liu 		return NULL;
428ef694222SBob Liu 	vma = find_vma(mm, addr);
429ef694222SBob Liu 	if (!vma || vma->vm_start > addr)
430ef694222SBob Liu 		return NULL;
431ef694222SBob Liu 	if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
432ef694222SBob Liu 		return NULL;
433ef694222SBob Liu 	return vma;
434ef694222SBob Liu }
435ef694222SBob Liu 
4368dd3557aSHugh Dickins static void break_cow(struct rmap_item *rmap_item)
43731dbd01fSIzik Eidus {
4388dd3557aSHugh Dickins 	struct mm_struct *mm = rmap_item->mm;
4398dd3557aSHugh Dickins 	unsigned long addr = rmap_item->address;
44031dbd01fSIzik Eidus 	struct vm_area_struct *vma;
44131dbd01fSIzik Eidus 
4424035c07aSHugh Dickins 	/*
4434035c07aSHugh Dickins 	 * It is not an accident that whenever we want to break COW
4444035c07aSHugh Dickins 	 * to undo, we also need to drop a reference to the anon_vma.
4454035c07aSHugh Dickins 	 */
4469e60109fSPeter Zijlstra 	put_anon_vma(rmap_item->anon_vma);
4474035c07aSHugh Dickins 
44881464e30SHugh Dickins 	down_read(&mm->mmap_sem);
449ef694222SBob Liu 	vma = find_mergeable_vma(mm, addr);
450ef694222SBob Liu 	if (vma)
45131dbd01fSIzik Eidus 		break_ksm(vma, addr);
45231dbd01fSIzik Eidus 	up_read(&mm->mmap_sem);
45331dbd01fSIzik Eidus }
45431dbd01fSIzik Eidus 
45531dbd01fSIzik Eidus static struct page *get_mergeable_page(struct rmap_item *rmap_item)
45631dbd01fSIzik Eidus {
45731dbd01fSIzik Eidus 	struct mm_struct *mm = rmap_item->mm;
45831dbd01fSIzik Eidus 	unsigned long addr = rmap_item->address;
45931dbd01fSIzik Eidus 	struct vm_area_struct *vma;
46031dbd01fSIzik Eidus 	struct page *page;
46131dbd01fSIzik Eidus 
46231dbd01fSIzik Eidus 	down_read(&mm->mmap_sem);
463ef694222SBob Liu 	vma = find_mergeable_vma(mm, addr);
464ef694222SBob Liu 	if (!vma)
46531dbd01fSIzik Eidus 		goto out;
46631dbd01fSIzik Eidus 
46731dbd01fSIzik Eidus 	page = follow_page(vma, addr, FOLL_GET);
46822eccdd7SDan Carpenter 	if (IS_ERR_OR_NULL(page))
46931dbd01fSIzik Eidus 		goto out;
470f765f540SKirill A. Shutemov 	if (PageAnon(page)) {
47131dbd01fSIzik Eidus 		flush_anon_page(vma, page, addr);
47231dbd01fSIzik Eidus 		flush_dcache_page(page);
47331dbd01fSIzik Eidus 	} else {
47431dbd01fSIzik Eidus 		put_page(page);
475c8f95ed1SAndrea Arcangeli out:
476c8f95ed1SAndrea Arcangeli 		page = NULL;
47731dbd01fSIzik Eidus 	}
47831dbd01fSIzik Eidus 	up_read(&mm->mmap_sem);
47931dbd01fSIzik Eidus 	return page;
48031dbd01fSIzik Eidus }
48131dbd01fSIzik Eidus 
48290bd6fd3SPetr Holasek /*
48390bd6fd3SPetr Holasek  * This helper is used for getting right index into array of tree roots.
48490bd6fd3SPetr Holasek  * When merge_across_nodes knob is set to 1, there are only two rb-trees for
48590bd6fd3SPetr Holasek  * stable and unstable pages from all nodes with roots in index 0. Otherwise,
48690bd6fd3SPetr Holasek  * every node has its own stable and unstable tree.
48790bd6fd3SPetr Holasek  */
48890bd6fd3SPetr Holasek static inline int get_kpfn_nid(unsigned long kpfn)
48990bd6fd3SPetr Holasek {
490d8fc16a8SHugh Dickins 	return ksm_merge_across_nodes ? 0 : NUMA(pfn_to_nid(kpfn));
49190bd6fd3SPetr Holasek }
49290bd6fd3SPetr Holasek 
4934035c07aSHugh Dickins static void remove_node_from_stable_tree(struct stable_node *stable_node)
4944035c07aSHugh Dickins {
4954035c07aSHugh Dickins 	struct rmap_item *rmap_item;
4964035c07aSHugh Dickins 
497b67bfe0dSSasha Levin 	hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
4984035c07aSHugh Dickins 		if (rmap_item->hlist.next)
4994035c07aSHugh Dickins 			ksm_pages_sharing--;
5004035c07aSHugh Dickins 		else
5014035c07aSHugh Dickins 			ksm_pages_shared--;
5029e60109fSPeter Zijlstra 		put_anon_vma(rmap_item->anon_vma);
5034035c07aSHugh Dickins 		rmap_item->address &= PAGE_MASK;
5044035c07aSHugh Dickins 		cond_resched();
5054035c07aSHugh Dickins 	}
5064035c07aSHugh Dickins 
5074146d2d6SHugh Dickins 	if (stable_node->head == &migrate_nodes)
5084146d2d6SHugh Dickins 		list_del(&stable_node->list);
5094146d2d6SHugh Dickins 	else
5104146d2d6SHugh Dickins 		rb_erase(&stable_node->node,
511ef53d16cSHugh Dickins 			 root_stable_tree + NUMA(stable_node->nid));
5124035c07aSHugh Dickins 	free_stable_node(stable_node);
5134035c07aSHugh Dickins }
5144035c07aSHugh Dickins 
5154035c07aSHugh Dickins /*
5164035c07aSHugh Dickins  * get_ksm_page: checks if the page indicated by the stable node
5174035c07aSHugh Dickins  * is still its ksm page, despite having held no reference to it.
5184035c07aSHugh Dickins  * In which case we can trust the content of the page, and it
5194035c07aSHugh Dickins  * returns the gotten page; but if the page has now been zapped,
5204035c07aSHugh Dickins  * remove the stale node from the stable tree and return NULL.
521c8d6553bSHugh Dickins  * But beware, the stable node's page might be being migrated.
5224035c07aSHugh Dickins  *
5234035c07aSHugh Dickins  * You would expect the stable_node to hold a reference to the ksm page.
5244035c07aSHugh Dickins  * But if it increments the page's count, swapping out has to wait for
5254035c07aSHugh Dickins  * ksmd to come around again before it can free the page, which may take
5264035c07aSHugh Dickins  * seconds or even minutes: much too unresponsive.  So instead we use a
5274035c07aSHugh Dickins  * "keyhole reference": access to the ksm page from the stable node peeps
5284035c07aSHugh Dickins  * out through its keyhole to see if that page still holds the right key,
5294035c07aSHugh Dickins  * pointing back to this stable node.  This relies on freeing a PageAnon
5304035c07aSHugh Dickins  * page to reset its page->mapping to NULL, and relies on no other use of
5314035c07aSHugh Dickins  * a page to put something that might look like our key in page->mapping.
5324035c07aSHugh Dickins  * is on its way to being freed; but it is an anomaly to bear in mind.
5334035c07aSHugh Dickins  */
5348fdb3dbfSHugh Dickins static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it)
5354035c07aSHugh Dickins {
5364035c07aSHugh Dickins 	struct page *page;
5374035c07aSHugh Dickins 	void *expected_mapping;
538c8d6553bSHugh Dickins 	unsigned long kpfn;
5394035c07aSHugh Dickins 
540bda807d4SMinchan Kim 	expected_mapping = (void *)((unsigned long)stable_node |
541bda807d4SMinchan Kim 					PAGE_MAPPING_KSM);
542c8d6553bSHugh Dickins again:
5434db0c3c2SJason Low 	kpfn = READ_ONCE(stable_node->kpfn);
544c8d6553bSHugh Dickins 	page = pfn_to_page(kpfn);
545c8d6553bSHugh Dickins 
546c8d6553bSHugh Dickins 	/*
547c8d6553bSHugh Dickins 	 * page is computed from kpfn, so on most architectures reading
548c8d6553bSHugh Dickins 	 * page->mapping is naturally ordered after reading node->kpfn,
549c8d6553bSHugh Dickins 	 * but on Alpha we need to be more careful.
550c8d6553bSHugh Dickins 	 */
551c8d6553bSHugh Dickins 	smp_read_barrier_depends();
5524db0c3c2SJason Low 	if (READ_ONCE(page->mapping) != expected_mapping)
5534035c07aSHugh Dickins 		goto stale;
554c8d6553bSHugh Dickins 
555c8d6553bSHugh Dickins 	/*
556c8d6553bSHugh Dickins 	 * We cannot do anything with the page while its refcount is 0.
557c8d6553bSHugh Dickins 	 * Usually 0 means free, or tail of a higher-order page: in which
558c8d6553bSHugh Dickins 	 * case this node is no longer referenced, and should be freed;
559c8d6553bSHugh Dickins 	 * however, it might mean that the page is under page_freeze_refs().
560c8d6553bSHugh Dickins 	 * The __remove_mapping() case is easy, again the node is now stale;
561c8d6553bSHugh Dickins 	 * but if page is swapcache in migrate_page_move_mapping(), it might
562c8d6553bSHugh Dickins 	 * still be our page, in which case it's essential to keep the node.
563c8d6553bSHugh Dickins 	 */
564c8d6553bSHugh Dickins 	while (!get_page_unless_zero(page)) {
565c8d6553bSHugh Dickins 		/*
566c8d6553bSHugh Dickins 		 * Another check for page->mapping != expected_mapping would
567c8d6553bSHugh Dickins 		 * work here too.  We have chosen the !PageSwapCache test to
568c8d6553bSHugh Dickins 		 * optimize the common case, when the page is or is about to
569c8d6553bSHugh Dickins 		 * be freed: PageSwapCache is cleared (under spin_lock_irq)
570c8d6553bSHugh Dickins 		 * in the freeze_refs section of __remove_mapping(); but Anon
571c8d6553bSHugh Dickins 		 * page->mapping reset to NULL later, in free_pages_prepare().
572c8d6553bSHugh Dickins 		 */
573c8d6553bSHugh Dickins 		if (!PageSwapCache(page))
5744035c07aSHugh Dickins 			goto stale;
575c8d6553bSHugh Dickins 		cpu_relax();
576c8d6553bSHugh Dickins 	}
577c8d6553bSHugh Dickins 
5784db0c3c2SJason Low 	if (READ_ONCE(page->mapping) != expected_mapping) {
5794035c07aSHugh Dickins 		put_page(page);
5804035c07aSHugh Dickins 		goto stale;
5814035c07aSHugh Dickins 	}
582c8d6553bSHugh Dickins 
5838fdb3dbfSHugh Dickins 	if (lock_it) {
5848aafa6a4SHugh Dickins 		lock_page(page);
5854db0c3c2SJason Low 		if (READ_ONCE(page->mapping) != expected_mapping) {
5868aafa6a4SHugh Dickins 			unlock_page(page);
5878aafa6a4SHugh Dickins 			put_page(page);
5888aafa6a4SHugh Dickins 			goto stale;
5898aafa6a4SHugh Dickins 		}
5908aafa6a4SHugh Dickins 	}
5914035c07aSHugh Dickins 	return page;
592c8d6553bSHugh Dickins 
5934035c07aSHugh Dickins stale:
594c8d6553bSHugh Dickins 	/*
595c8d6553bSHugh Dickins 	 * We come here from above when page->mapping or !PageSwapCache
596c8d6553bSHugh Dickins 	 * suggests that the node is stale; but it might be under migration.
597c8d6553bSHugh Dickins 	 * We need smp_rmb(), matching the smp_wmb() in ksm_migrate_page(),
598c8d6553bSHugh Dickins 	 * before checking whether node->kpfn has been changed.
599c8d6553bSHugh Dickins 	 */
600c8d6553bSHugh Dickins 	smp_rmb();
6014db0c3c2SJason Low 	if (READ_ONCE(stable_node->kpfn) != kpfn)
602c8d6553bSHugh Dickins 		goto again;
6034035c07aSHugh Dickins 	remove_node_from_stable_tree(stable_node);
6044035c07aSHugh Dickins 	return NULL;
6054035c07aSHugh Dickins }
6064035c07aSHugh Dickins 
60731dbd01fSIzik Eidus /*
60831dbd01fSIzik Eidus  * Removing rmap_item from stable or unstable tree.
60931dbd01fSIzik Eidus  * This function will clean the information from the stable/unstable tree.
61031dbd01fSIzik Eidus  */
61131dbd01fSIzik Eidus static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
61231dbd01fSIzik Eidus {
6137b6ba2c7SHugh Dickins 	if (rmap_item->address & STABLE_FLAG) {
6147b6ba2c7SHugh Dickins 		struct stable_node *stable_node;
6155ad64688SHugh Dickins 		struct page *page;
61631dbd01fSIzik Eidus 
6177b6ba2c7SHugh Dickins 		stable_node = rmap_item->head;
6188aafa6a4SHugh Dickins 		page = get_ksm_page(stable_node, true);
6194035c07aSHugh Dickins 		if (!page)
6204035c07aSHugh Dickins 			goto out;
6215ad64688SHugh Dickins 
6227b6ba2c7SHugh Dickins 		hlist_del(&rmap_item->hlist);
6235ad64688SHugh Dickins 		unlock_page(page);
6245ad64688SHugh Dickins 		put_page(page);
62508beca44SHugh Dickins 
62698666f8aSAndrea Arcangeli 		if (!hlist_empty(&stable_node->hlist))
6274035c07aSHugh Dickins 			ksm_pages_sharing--;
6284035c07aSHugh Dickins 		else
629b4028260SHugh Dickins 			ksm_pages_shared--;
63031dbd01fSIzik Eidus 
6319e60109fSPeter Zijlstra 		put_anon_vma(rmap_item->anon_vma);
63293d17715SHugh Dickins 		rmap_item->address &= PAGE_MASK;
63331dbd01fSIzik Eidus 
6347b6ba2c7SHugh Dickins 	} else if (rmap_item->address & UNSTABLE_FLAG) {
63531dbd01fSIzik Eidus 		unsigned char age;
63631dbd01fSIzik Eidus 		/*
6379ba69294SHugh Dickins 		 * Usually ksmd can and must skip the rb_erase, because
63831dbd01fSIzik Eidus 		 * root_unstable_tree was already reset to RB_ROOT.
6399ba69294SHugh Dickins 		 * But be careful when an mm is exiting: do the rb_erase
6409ba69294SHugh Dickins 		 * if this rmap_item was inserted by this scan, rather
6419ba69294SHugh Dickins 		 * than left over from before.
64231dbd01fSIzik Eidus 		 */
64331dbd01fSIzik Eidus 		age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
644cd551f97SHugh Dickins 		BUG_ON(age > 1);
64531dbd01fSIzik Eidus 		if (!age)
64690bd6fd3SPetr Holasek 			rb_erase(&rmap_item->node,
647ef53d16cSHugh Dickins 				 root_unstable_tree + NUMA(rmap_item->nid));
64893d17715SHugh Dickins 		ksm_pages_unshared--;
64931dbd01fSIzik Eidus 		rmap_item->address &= PAGE_MASK;
65093d17715SHugh Dickins 	}
6514035c07aSHugh Dickins out:
65231dbd01fSIzik Eidus 	cond_resched();		/* we're called from many long loops */
65331dbd01fSIzik Eidus }
65431dbd01fSIzik Eidus 
65531dbd01fSIzik Eidus static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
6566514d511SHugh Dickins 				       struct rmap_item **rmap_list)
65731dbd01fSIzik Eidus {
6586514d511SHugh Dickins 	while (*rmap_list) {
6596514d511SHugh Dickins 		struct rmap_item *rmap_item = *rmap_list;
6606514d511SHugh Dickins 		*rmap_list = rmap_item->rmap_list;
66131dbd01fSIzik Eidus 		remove_rmap_item_from_tree(rmap_item);
66231dbd01fSIzik Eidus 		free_rmap_item(rmap_item);
66331dbd01fSIzik Eidus 	}
66431dbd01fSIzik Eidus }
66531dbd01fSIzik Eidus 
66631dbd01fSIzik Eidus /*
667e850dcf5SHugh Dickins  * Though it's very tempting to unmerge rmap_items from stable tree rather
66831dbd01fSIzik Eidus  * than check every pte of a given vma, the locking doesn't quite work for
66931dbd01fSIzik Eidus  * that - an rmap_item is assigned to the stable tree after inserting ksm
67031dbd01fSIzik Eidus  * page and upping mmap_sem.  Nor does it fit with the way we skip dup'ing
67131dbd01fSIzik Eidus  * rmap_items from parent to child at fork time (so as not to waste time
67231dbd01fSIzik Eidus  * if exit comes before the next scan reaches it).
67381464e30SHugh Dickins  *
67481464e30SHugh Dickins  * Similarly, although we'd like to remove rmap_items (so updating counts
67581464e30SHugh Dickins  * and freeing memory) when unmerging an area, it's easier to leave that
67681464e30SHugh Dickins  * to the next pass of ksmd - consider, for example, how ksmd might be
67781464e30SHugh Dickins  * in cmp_and_merge_page on one of the rmap_items we would be removing.
67831dbd01fSIzik Eidus  */
679d952b791SHugh Dickins static int unmerge_ksm_pages(struct vm_area_struct *vma,
68031dbd01fSIzik Eidus 			     unsigned long start, unsigned long end)
68131dbd01fSIzik Eidus {
68231dbd01fSIzik Eidus 	unsigned long addr;
683d952b791SHugh Dickins 	int err = 0;
68431dbd01fSIzik Eidus 
685d952b791SHugh Dickins 	for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
6869ba69294SHugh Dickins 		if (ksm_test_exit(vma->vm_mm))
6879ba69294SHugh Dickins 			break;
688d952b791SHugh Dickins 		if (signal_pending(current))
689d952b791SHugh Dickins 			err = -ERESTARTSYS;
690d952b791SHugh Dickins 		else
691d952b791SHugh Dickins 			err = break_ksm(vma, addr);
692d952b791SHugh Dickins 	}
693d952b791SHugh Dickins 	return err;
69431dbd01fSIzik Eidus }
69531dbd01fSIzik Eidus 
6962ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
6972ffd8679SHugh Dickins /*
6982ffd8679SHugh Dickins  * Only called through the sysfs control interface:
6992ffd8679SHugh Dickins  */
700cbf86cfeSHugh Dickins static int remove_stable_node(struct stable_node *stable_node)
701cbf86cfeSHugh Dickins {
702cbf86cfeSHugh Dickins 	struct page *page;
703cbf86cfeSHugh Dickins 	int err;
704cbf86cfeSHugh Dickins 
705cbf86cfeSHugh Dickins 	page = get_ksm_page(stable_node, true);
706cbf86cfeSHugh Dickins 	if (!page) {
707cbf86cfeSHugh Dickins 		/*
708cbf86cfeSHugh Dickins 		 * get_ksm_page did remove_node_from_stable_tree itself.
709cbf86cfeSHugh Dickins 		 */
710cbf86cfeSHugh Dickins 		return 0;
711cbf86cfeSHugh Dickins 	}
712cbf86cfeSHugh Dickins 
7138fdb3dbfSHugh Dickins 	if (WARN_ON_ONCE(page_mapped(page))) {
714cbf86cfeSHugh Dickins 		/*
7158fdb3dbfSHugh Dickins 		 * This should not happen: but if it does, just refuse to let
7168fdb3dbfSHugh Dickins 		 * merge_across_nodes be switched - there is no need to panic.
7178fdb3dbfSHugh Dickins 		 */
7188fdb3dbfSHugh Dickins 		err = -EBUSY;
7198fdb3dbfSHugh Dickins 	} else {
7208fdb3dbfSHugh Dickins 		/*
7218fdb3dbfSHugh Dickins 		 * The stable node did not yet appear stale to get_ksm_page(),
7228fdb3dbfSHugh Dickins 		 * since that allows for an unmapped ksm page to be recognized
7238fdb3dbfSHugh Dickins 		 * right up until it is freed; but the node is safe to remove.
724cbf86cfeSHugh Dickins 		 * This page might be in a pagevec waiting to be freed,
725cbf86cfeSHugh Dickins 		 * or it might be PageSwapCache (perhaps under writeback),
726cbf86cfeSHugh Dickins 		 * or it might have been removed from swapcache a moment ago.
727cbf86cfeSHugh Dickins 		 */
728cbf86cfeSHugh Dickins 		set_page_stable_node(page, NULL);
729cbf86cfeSHugh Dickins 		remove_node_from_stable_tree(stable_node);
730cbf86cfeSHugh Dickins 		err = 0;
731cbf86cfeSHugh Dickins 	}
732cbf86cfeSHugh Dickins 
733cbf86cfeSHugh Dickins 	unlock_page(page);
734cbf86cfeSHugh Dickins 	put_page(page);
735cbf86cfeSHugh Dickins 	return err;
736cbf86cfeSHugh Dickins }
737cbf86cfeSHugh Dickins 
738cbf86cfeSHugh Dickins static int remove_all_stable_nodes(void)
739cbf86cfeSHugh Dickins {
74003640418SGeliang Tang 	struct stable_node *stable_node, *next;
741cbf86cfeSHugh Dickins 	int nid;
742cbf86cfeSHugh Dickins 	int err = 0;
743cbf86cfeSHugh Dickins 
744ef53d16cSHugh Dickins 	for (nid = 0; nid < ksm_nr_node_ids; nid++) {
745cbf86cfeSHugh Dickins 		while (root_stable_tree[nid].rb_node) {
746cbf86cfeSHugh Dickins 			stable_node = rb_entry(root_stable_tree[nid].rb_node,
747cbf86cfeSHugh Dickins 						struct stable_node, node);
748cbf86cfeSHugh Dickins 			if (remove_stable_node(stable_node)) {
749cbf86cfeSHugh Dickins 				err = -EBUSY;
750cbf86cfeSHugh Dickins 				break;	/* proceed to next nid */
751cbf86cfeSHugh Dickins 			}
752cbf86cfeSHugh Dickins 			cond_resched();
753cbf86cfeSHugh Dickins 		}
754cbf86cfeSHugh Dickins 	}
75503640418SGeliang Tang 	list_for_each_entry_safe(stable_node, next, &migrate_nodes, list) {
7564146d2d6SHugh Dickins 		if (remove_stable_node(stable_node))
7574146d2d6SHugh Dickins 			err = -EBUSY;
7584146d2d6SHugh Dickins 		cond_resched();
7594146d2d6SHugh Dickins 	}
760cbf86cfeSHugh Dickins 	return err;
761cbf86cfeSHugh Dickins }
762cbf86cfeSHugh Dickins 
763d952b791SHugh Dickins static int unmerge_and_remove_all_rmap_items(void)
76431dbd01fSIzik Eidus {
76531dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
76631dbd01fSIzik Eidus 	struct mm_struct *mm;
76731dbd01fSIzik Eidus 	struct vm_area_struct *vma;
768d952b791SHugh Dickins 	int err = 0;
76931dbd01fSIzik Eidus 
770d952b791SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
7719ba69294SHugh Dickins 	ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
772d952b791SHugh Dickins 						struct mm_slot, mm_list);
773d952b791SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
774d952b791SHugh Dickins 
7759ba69294SHugh Dickins 	for (mm_slot = ksm_scan.mm_slot;
7769ba69294SHugh Dickins 			mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
77731dbd01fSIzik Eidus 		mm = mm_slot->mm;
77831dbd01fSIzik Eidus 		down_read(&mm->mmap_sem);
77931dbd01fSIzik Eidus 		for (vma = mm->mmap; vma; vma = vma->vm_next) {
7809ba69294SHugh Dickins 			if (ksm_test_exit(mm))
7819ba69294SHugh Dickins 				break;
78231dbd01fSIzik Eidus 			if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
78331dbd01fSIzik Eidus 				continue;
784d952b791SHugh Dickins 			err = unmerge_ksm_pages(vma,
785d952b791SHugh Dickins 						vma->vm_start, vma->vm_end);
7869ba69294SHugh Dickins 			if (err)
7879ba69294SHugh Dickins 				goto error;
788d952b791SHugh Dickins 		}
7899ba69294SHugh Dickins 
7906514d511SHugh Dickins 		remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
7917496fea9SZhou Chengming 		up_read(&mm->mmap_sem);
79231dbd01fSIzik Eidus 
79331dbd01fSIzik Eidus 		spin_lock(&ksm_mmlist_lock);
7949ba69294SHugh Dickins 		ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
795d952b791SHugh Dickins 						struct mm_slot, mm_list);
7969ba69294SHugh Dickins 		if (ksm_test_exit(mm)) {
7974ca3a69bSSasha Levin 			hash_del(&mm_slot->link);
7989ba69294SHugh Dickins 			list_del(&mm_slot->mm_list);
79931dbd01fSIzik Eidus 			spin_unlock(&ksm_mmlist_lock);
8009ba69294SHugh Dickins 
8019ba69294SHugh Dickins 			free_mm_slot(mm_slot);
8029ba69294SHugh Dickins 			clear_bit(MMF_VM_MERGEABLE, &mm->flags);
8039ba69294SHugh Dickins 			mmdrop(mm);
8047496fea9SZhou Chengming 		} else
8059ba69294SHugh Dickins 			spin_unlock(&ksm_mmlist_lock);
80631dbd01fSIzik Eidus 	}
80731dbd01fSIzik Eidus 
808cbf86cfeSHugh Dickins 	/* Clean up stable nodes, but don't worry if some are still busy */
809cbf86cfeSHugh Dickins 	remove_all_stable_nodes();
810d952b791SHugh Dickins 	ksm_scan.seqnr = 0;
8119ba69294SHugh Dickins 	return 0;
8129ba69294SHugh Dickins 
8139ba69294SHugh Dickins error:
8149ba69294SHugh Dickins 	up_read(&mm->mmap_sem);
815d952b791SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
816d952b791SHugh Dickins 	ksm_scan.mm_slot = &ksm_mm_head;
817d952b791SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
818d952b791SHugh Dickins 	return err;
819d952b791SHugh Dickins }
8202ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
821d952b791SHugh Dickins 
82231dbd01fSIzik Eidus static u32 calc_checksum(struct page *page)
82331dbd01fSIzik Eidus {
82431dbd01fSIzik Eidus 	u32 checksum;
8259b04c5feSCong Wang 	void *addr = kmap_atomic(page);
82631dbd01fSIzik Eidus 	checksum = jhash2(addr, PAGE_SIZE / 4, 17);
8279b04c5feSCong Wang 	kunmap_atomic(addr);
82831dbd01fSIzik Eidus 	return checksum;
82931dbd01fSIzik Eidus }
83031dbd01fSIzik Eidus 
83131dbd01fSIzik Eidus static int memcmp_pages(struct page *page1, struct page *page2)
83231dbd01fSIzik Eidus {
83331dbd01fSIzik Eidus 	char *addr1, *addr2;
83431dbd01fSIzik Eidus 	int ret;
83531dbd01fSIzik Eidus 
8369b04c5feSCong Wang 	addr1 = kmap_atomic(page1);
8379b04c5feSCong Wang 	addr2 = kmap_atomic(page2);
83831dbd01fSIzik Eidus 	ret = memcmp(addr1, addr2, PAGE_SIZE);
8399b04c5feSCong Wang 	kunmap_atomic(addr2);
8409b04c5feSCong Wang 	kunmap_atomic(addr1);
84131dbd01fSIzik Eidus 	return ret;
84231dbd01fSIzik Eidus }
84331dbd01fSIzik Eidus 
84431dbd01fSIzik Eidus static inline int pages_identical(struct page *page1, struct page *page2)
84531dbd01fSIzik Eidus {
84631dbd01fSIzik Eidus 	return !memcmp_pages(page1, page2);
84731dbd01fSIzik Eidus }
84831dbd01fSIzik Eidus 
84931dbd01fSIzik Eidus static int write_protect_page(struct vm_area_struct *vma, struct page *page,
85031dbd01fSIzik Eidus 			      pte_t *orig_pte)
85131dbd01fSIzik Eidus {
85231dbd01fSIzik Eidus 	struct mm_struct *mm = vma->vm_mm;
85331dbd01fSIzik Eidus 	unsigned long addr;
85431dbd01fSIzik Eidus 	pte_t *ptep;
85531dbd01fSIzik Eidus 	spinlock_t *ptl;
85631dbd01fSIzik Eidus 	int swapped;
85731dbd01fSIzik Eidus 	int err = -EFAULT;
8586bdb913fSHaggai Eran 	unsigned long mmun_start;	/* For mmu_notifiers */
8596bdb913fSHaggai Eran 	unsigned long mmun_end;		/* For mmu_notifiers */
86031dbd01fSIzik Eidus 
86131dbd01fSIzik Eidus 	addr = page_address_in_vma(page, vma);
86231dbd01fSIzik Eidus 	if (addr == -EFAULT)
86331dbd01fSIzik Eidus 		goto out;
86431dbd01fSIzik Eidus 
86529ad768cSAndrea Arcangeli 	BUG_ON(PageTransCompound(page));
8666bdb913fSHaggai Eran 
8676bdb913fSHaggai Eran 	mmun_start = addr;
8686bdb913fSHaggai Eran 	mmun_end   = addr + PAGE_SIZE;
8696bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
8706bdb913fSHaggai Eran 
87131dbd01fSIzik Eidus 	ptep = page_check_address(page, mm, addr, &ptl, 0);
87231dbd01fSIzik Eidus 	if (!ptep)
8736bdb913fSHaggai Eran 		goto out_mn;
87431dbd01fSIzik Eidus 
8754e31635cSHugh Dickins 	if (pte_write(*ptep) || pte_dirty(*ptep)) {
87631dbd01fSIzik Eidus 		pte_t entry;
87731dbd01fSIzik Eidus 
87831dbd01fSIzik Eidus 		swapped = PageSwapCache(page);
87931dbd01fSIzik Eidus 		flush_cache_page(vma, addr, page_to_pfn(page));
88031dbd01fSIzik Eidus 		/*
88125985edcSLucas De Marchi 		 * Ok this is tricky, when get_user_pages_fast() run it doesn't
88231dbd01fSIzik Eidus 		 * take any lock, therefore the check that we are going to make
88331dbd01fSIzik Eidus 		 * with the pagecount against the mapcount is racey and
88431dbd01fSIzik Eidus 		 * O_DIRECT can happen right after the check.
88531dbd01fSIzik Eidus 		 * So we clear the pte and flush the tlb before the check
88631dbd01fSIzik Eidus 		 * this assure us that no O_DIRECT can happen after the check
88731dbd01fSIzik Eidus 		 * or in the middle of the check.
88831dbd01fSIzik Eidus 		 */
88934ee645eSJoerg Roedel 		entry = ptep_clear_flush_notify(vma, addr, ptep);
89031dbd01fSIzik Eidus 		/*
89131dbd01fSIzik Eidus 		 * Check that no O_DIRECT or similar I/O is in progress on the
89231dbd01fSIzik Eidus 		 * page
89331dbd01fSIzik Eidus 		 */
89431e855eaSHugh Dickins 		if (page_mapcount(page) + 1 + swapped != page_count(page)) {
895cb532375SRobin Holt 			set_pte_at(mm, addr, ptep, entry);
89631dbd01fSIzik Eidus 			goto out_unlock;
89731dbd01fSIzik Eidus 		}
8984e31635cSHugh Dickins 		if (pte_dirty(entry))
8994e31635cSHugh Dickins 			set_page_dirty(page);
9004e31635cSHugh Dickins 		entry = pte_mkclean(pte_wrprotect(entry));
90131dbd01fSIzik Eidus 		set_pte_at_notify(mm, addr, ptep, entry);
90231dbd01fSIzik Eidus 	}
90331dbd01fSIzik Eidus 	*orig_pte = *ptep;
90431dbd01fSIzik Eidus 	err = 0;
90531dbd01fSIzik Eidus 
90631dbd01fSIzik Eidus out_unlock:
90731dbd01fSIzik Eidus 	pte_unmap_unlock(ptep, ptl);
9086bdb913fSHaggai Eran out_mn:
9096bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
91031dbd01fSIzik Eidus out:
91131dbd01fSIzik Eidus 	return err;
91231dbd01fSIzik Eidus }
91331dbd01fSIzik Eidus 
91431dbd01fSIzik Eidus /**
91531dbd01fSIzik Eidus  * replace_page - replace page in vma by new ksm page
9168dd3557aSHugh Dickins  * @vma:      vma that holds the pte pointing to page
9178dd3557aSHugh Dickins  * @page:     the page we are replacing by kpage
9188dd3557aSHugh Dickins  * @kpage:    the ksm page we replace page by
91931dbd01fSIzik Eidus  * @orig_pte: the original value of the pte
92031dbd01fSIzik Eidus  *
92131dbd01fSIzik Eidus  * Returns 0 on success, -EFAULT on failure.
92231dbd01fSIzik Eidus  */
9238dd3557aSHugh Dickins static int replace_page(struct vm_area_struct *vma, struct page *page,
9248dd3557aSHugh Dickins 			struct page *kpage, pte_t orig_pte)
92531dbd01fSIzik Eidus {
92631dbd01fSIzik Eidus 	struct mm_struct *mm = vma->vm_mm;
92731dbd01fSIzik Eidus 	pmd_t *pmd;
92831dbd01fSIzik Eidus 	pte_t *ptep;
92931dbd01fSIzik Eidus 	spinlock_t *ptl;
93031dbd01fSIzik Eidus 	unsigned long addr;
93131dbd01fSIzik Eidus 	int err = -EFAULT;
9326bdb913fSHaggai Eran 	unsigned long mmun_start;	/* For mmu_notifiers */
9336bdb913fSHaggai Eran 	unsigned long mmun_end;		/* For mmu_notifiers */
93431dbd01fSIzik Eidus 
9358dd3557aSHugh Dickins 	addr = page_address_in_vma(page, vma);
93631dbd01fSIzik Eidus 	if (addr == -EFAULT)
93731dbd01fSIzik Eidus 		goto out;
93831dbd01fSIzik Eidus 
9396219049aSBob Liu 	pmd = mm_find_pmd(mm, addr);
9406219049aSBob Liu 	if (!pmd)
94131dbd01fSIzik Eidus 		goto out;
94231dbd01fSIzik Eidus 
9436bdb913fSHaggai Eran 	mmun_start = addr;
9446bdb913fSHaggai Eran 	mmun_end   = addr + PAGE_SIZE;
9456bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
9466bdb913fSHaggai Eran 
94731dbd01fSIzik Eidus 	ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
94831dbd01fSIzik Eidus 	if (!pte_same(*ptep, orig_pte)) {
94931dbd01fSIzik Eidus 		pte_unmap_unlock(ptep, ptl);
9506bdb913fSHaggai Eran 		goto out_mn;
95131dbd01fSIzik Eidus 	}
95231dbd01fSIzik Eidus 
9538dd3557aSHugh Dickins 	get_page(kpage);
954d281ee61SKirill A. Shutemov 	page_add_anon_rmap(kpage, vma, addr, false);
95531dbd01fSIzik Eidus 
95631dbd01fSIzik Eidus 	flush_cache_page(vma, addr, pte_pfn(*ptep));
95734ee645eSJoerg Roedel 	ptep_clear_flush_notify(vma, addr, ptep);
9588dd3557aSHugh Dickins 	set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
95931dbd01fSIzik Eidus 
960d281ee61SKirill A. Shutemov 	page_remove_rmap(page, false);
961ae52a2adSHugh Dickins 	if (!page_mapped(page))
962ae52a2adSHugh Dickins 		try_to_free_swap(page);
9638dd3557aSHugh Dickins 	put_page(page);
96431dbd01fSIzik Eidus 
96531dbd01fSIzik Eidus 	pte_unmap_unlock(ptep, ptl);
96631dbd01fSIzik Eidus 	err = 0;
9676bdb913fSHaggai Eran out_mn:
9686bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
96931dbd01fSIzik Eidus out:
97031dbd01fSIzik Eidus 	return err;
97131dbd01fSIzik Eidus }
97231dbd01fSIzik Eidus 
97331dbd01fSIzik Eidus /*
97431dbd01fSIzik Eidus  * try_to_merge_one_page - take two pages and merge them into one
9758dd3557aSHugh Dickins  * @vma: the vma that holds the pte pointing to page
9768dd3557aSHugh Dickins  * @page: the PageAnon page that we want to replace with kpage
97780e14822SHugh Dickins  * @kpage: the PageKsm page that we want to map instead of page,
97880e14822SHugh Dickins  *         or NULL the first time when we want to use page as kpage.
97931dbd01fSIzik Eidus  *
98031dbd01fSIzik Eidus  * This function returns 0 if the pages were merged, -EFAULT otherwise.
98131dbd01fSIzik Eidus  */
98231dbd01fSIzik Eidus static int try_to_merge_one_page(struct vm_area_struct *vma,
9838dd3557aSHugh Dickins 				 struct page *page, struct page *kpage)
98431dbd01fSIzik Eidus {
98531dbd01fSIzik Eidus 	pte_t orig_pte = __pte(0);
98631dbd01fSIzik Eidus 	int err = -EFAULT;
98731dbd01fSIzik Eidus 
988db114b83SHugh Dickins 	if (page == kpage)			/* ksm page forked */
989db114b83SHugh Dickins 		return 0;
990db114b83SHugh Dickins 
9918dd3557aSHugh Dickins 	if (!PageAnon(page))
99231dbd01fSIzik Eidus 		goto out;
99331dbd01fSIzik Eidus 
99431dbd01fSIzik Eidus 	/*
99531dbd01fSIzik Eidus 	 * We need the page lock to read a stable PageSwapCache in
99631dbd01fSIzik Eidus 	 * write_protect_page().  We use trylock_page() instead of
99731dbd01fSIzik Eidus 	 * lock_page() because we don't want to wait here - we
99831dbd01fSIzik Eidus 	 * prefer to continue scanning and merging different pages,
99931dbd01fSIzik Eidus 	 * then come back to this page when it is unlocked.
100031dbd01fSIzik Eidus 	 */
10018dd3557aSHugh Dickins 	if (!trylock_page(page))
100231e855eaSHugh Dickins 		goto out;
1003f765f540SKirill A. Shutemov 
1004f765f540SKirill A. Shutemov 	if (PageTransCompound(page)) {
1005f765f540SKirill A. Shutemov 		err = split_huge_page(page);
1006f765f540SKirill A. Shutemov 		if (err)
1007f765f540SKirill A. Shutemov 			goto out_unlock;
1008f765f540SKirill A. Shutemov 	}
1009f765f540SKirill A. Shutemov 
101031dbd01fSIzik Eidus 	/*
101131dbd01fSIzik Eidus 	 * If this anonymous page is mapped only here, its pte may need
101231dbd01fSIzik Eidus 	 * to be write-protected.  If it's mapped elsewhere, all of its
101331dbd01fSIzik Eidus 	 * ptes are necessarily already write-protected.  But in either
101431dbd01fSIzik Eidus 	 * case, we need to lock and check page_count is not raised.
101531dbd01fSIzik Eidus 	 */
101680e14822SHugh Dickins 	if (write_protect_page(vma, page, &orig_pte) == 0) {
101780e14822SHugh Dickins 		if (!kpage) {
101880e14822SHugh Dickins 			/*
101980e14822SHugh Dickins 			 * While we hold page lock, upgrade page from
102080e14822SHugh Dickins 			 * PageAnon+anon_vma to PageKsm+NULL stable_node:
102180e14822SHugh Dickins 			 * stable_tree_insert() will update stable_node.
102280e14822SHugh Dickins 			 */
102380e14822SHugh Dickins 			set_page_stable_node(page, NULL);
102480e14822SHugh Dickins 			mark_page_accessed(page);
1025337ed7ebSMinchan Kim 			/*
1026337ed7ebSMinchan Kim 			 * Page reclaim just frees a clean page with no dirty
1027337ed7ebSMinchan Kim 			 * ptes: make sure that the ksm page would be swapped.
1028337ed7ebSMinchan Kim 			 */
1029337ed7ebSMinchan Kim 			if (!PageDirty(page))
1030337ed7ebSMinchan Kim 				SetPageDirty(page);
103180e14822SHugh Dickins 			err = 0;
103280e14822SHugh Dickins 		} else if (pages_identical(page, kpage))
10338dd3557aSHugh Dickins 			err = replace_page(vma, page, kpage, orig_pte);
103480e14822SHugh Dickins 	}
103531dbd01fSIzik Eidus 
103680e14822SHugh Dickins 	if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
103773848b46SHugh Dickins 		munlock_vma_page(page);
10385ad64688SHugh Dickins 		if (!PageMlocked(kpage)) {
10395ad64688SHugh Dickins 			unlock_page(page);
10405ad64688SHugh Dickins 			lock_page(kpage);
10415ad64688SHugh Dickins 			mlock_vma_page(kpage);
10425ad64688SHugh Dickins 			page = kpage;		/* for final unlock */
10435ad64688SHugh Dickins 		}
10445ad64688SHugh Dickins 	}
104573848b46SHugh Dickins 
1046f765f540SKirill A. Shutemov out_unlock:
10478dd3557aSHugh Dickins 	unlock_page(page);
104831dbd01fSIzik Eidus out:
104931dbd01fSIzik Eidus 	return err;
105031dbd01fSIzik Eidus }
105131dbd01fSIzik Eidus 
105231dbd01fSIzik Eidus /*
105381464e30SHugh Dickins  * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
105481464e30SHugh Dickins  * but no new kernel page is allocated: kpage must already be a ksm page.
10558dd3557aSHugh Dickins  *
10568dd3557aSHugh Dickins  * This function returns 0 if the pages were merged, -EFAULT otherwise.
105781464e30SHugh Dickins  */
10588dd3557aSHugh Dickins static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
10598dd3557aSHugh Dickins 				      struct page *page, struct page *kpage)
106081464e30SHugh Dickins {
10618dd3557aSHugh Dickins 	struct mm_struct *mm = rmap_item->mm;
106281464e30SHugh Dickins 	struct vm_area_struct *vma;
106381464e30SHugh Dickins 	int err = -EFAULT;
106481464e30SHugh Dickins 
10658dd3557aSHugh Dickins 	down_read(&mm->mmap_sem);
106685c6e8ddSAndrea Arcangeli 	vma = find_mergeable_vma(mm, rmap_item->address);
106785c6e8ddSAndrea Arcangeli 	if (!vma)
10689ba69294SHugh Dickins 		goto out;
10699ba69294SHugh Dickins 
10708dd3557aSHugh Dickins 	err = try_to_merge_one_page(vma, page, kpage);
1071db114b83SHugh Dickins 	if (err)
1072db114b83SHugh Dickins 		goto out;
1073db114b83SHugh Dickins 
1074bc56620bSHugh Dickins 	/* Unstable nid is in union with stable anon_vma: remove first */
1075bc56620bSHugh Dickins 	remove_rmap_item_from_tree(rmap_item);
1076bc56620bSHugh Dickins 
1077db114b83SHugh Dickins 	/* Must get reference to anon_vma while still holding mmap_sem */
10789e60109fSPeter Zijlstra 	rmap_item->anon_vma = vma->anon_vma;
10799e60109fSPeter Zijlstra 	get_anon_vma(vma->anon_vma);
108081464e30SHugh Dickins out:
10818dd3557aSHugh Dickins 	up_read(&mm->mmap_sem);
108281464e30SHugh Dickins 	return err;
108381464e30SHugh Dickins }
108481464e30SHugh Dickins 
108581464e30SHugh Dickins /*
108631dbd01fSIzik Eidus  * try_to_merge_two_pages - take two identical pages and prepare them
108731dbd01fSIzik Eidus  * to be merged into one page.
108831dbd01fSIzik Eidus  *
10898dd3557aSHugh Dickins  * This function returns the kpage if we successfully merged two identical
10908dd3557aSHugh Dickins  * pages into one ksm page, NULL otherwise.
109131dbd01fSIzik Eidus  *
109280e14822SHugh Dickins  * Note that this function upgrades page to ksm page: if one of the pages
109331dbd01fSIzik Eidus  * is already a ksm page, try_to_merge_with_ksm_page should be used.
109431dbd01fSIzik Eidus  */
10958dd3557aSHugh Dickins static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
10968dd3557aSHugh Dickins 					   struct page *page,
10978dd3557aSHugh Dickins 					   struct rmap_item *tree_rmap_item,
10988dd3557aSHugh Dickins 					   struct page *tree_page)
109931dbd01fSIzik Eidus {
110080e14822SHugh Dickins 	int err;
110131dbd01fSIzik Eidus 
110280e14822SHugh Dickins 	err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
110331dbd01fSIzik Eidus 	if (!err) {
11048dd3557aSHugh Dickins 		err = try_to_merge_with_ksm_page(tree_rmap_item,
110580e14822SHugh Dickins 							tree_page, page);
110631dbd01fSIzik Eidus 		/*
110781464e30SHugh Dickins 		 * If that fails, we have a ksm page with only one pte
110881464e30SHugh Dickins 		 * pointing to it: so break it.
110931dbd01fSIzik Eidus 		 */
11104035c07aSHugh Dickins 		if (err)
11118dd3557aSHugh Dickins 			break_cow(rmap_item);
111231dbd01fSIzik Eidus 	}
111380e14822SHugh Dickins 	return err ? NULL : page;
111431dbd01fSIzik Eidus }
111531dbd01fSIzik Eidus 
111631dbd01fSIzik Eidus /*
11178dd3557aSHugh Dickins  * stable_tree_search - search for page inside the stable tree
111831dbd01fSIzik Eidus  *
111931dbd01fSIzik Eidus  * This function checks if there is a page inside the stable tree
112031dbd01fSIzik Eidus  * with identical content to the page that we are scanning right now.
112131dbd01fSIzik Eidus  *
11227b6ba2c7SHugh Dickins  * This function returns the stable tree node of identical content if found,
112331dbd01fSIzik Eidus  * NULL otherwise.
112431dbd01fSIzik Eidus  */
112562b61f61SHugh Dickins static struct page *stable_tree_search(struct page *page)
112631dbd01fSIzik Eidus {
112790bd6fd3SPetr Holasek 	int nid;
1128ef53d16cSHugh Dickins 	struct rb_root *root;
11294146d2d6SHugh Dickins 	struct rb_node **new;
11304146d2d6SHugh Dickins 	struct rb_node *parent;
11314146d2d6SHugh Dickins 	struct stable_node *stable_node;
11324146d2d6SHugh Dickins 	struct stable_node *page_node;
113331dbd01fSIzik Eidus 
11344146d2d6SHugh Dickins 	page_node = page_stable_node(page);
11354146d2d6SHugh Dickins 	if (page_node && page_node->head != &migrate_nodes) {
11364146d2d6SHugh Dickins 		/* ksm page forked */
113708beca44SHugh Dickins 		get_page(page);
113862b61f61SHugh Dickins 		return page;
113908beca44SHugh Dickins 	}
114008beca44SHugh Dickins 
114190bd6fd3SPetr Holasek 	nid = get_kpfn_nid(page_to_pfn(page));
1142ef53d16cSHugh Dickins 	root = root_stable_tree + nid;
11434146d2d6SHugh Dickins again:
1144ef53d16cSHugh Dickins 	new = &root->rb_node;
11454146d2d6SHugh Dickins 	parent = NULL;
114690bd6fd3SPetr Holasek 
11474146d2d6SHugh Dickins 	while (*new) {
11484035c07aSHugh Dickins 		struct page *tree_page;
114931dbd01fSIzik Eidus 		int ret;
115031dbd01fSIzik Eidus 
115131dbd01fSIzik Eidus 		cond_resched();
11524146d2d6SHugh Dickins 		stable_node = rb_entry(*new, struct stable_node, node);
11538aafa6a4SHugh Dickins 		tree_page = get_ksm_page(stable_node, false);
1154f2e5ff85SAndrea Arcangeli 		if (!tree_page) {
1155f2e5ff85SAndrea Arcangeli 			/*
1156f2e5ff85SAndrea Arcangeli 			 * If we walked over a stale stable_node,
1157f2e5ff85SAndrea Arcangeli 			 * get_ksm_page() will call rb_erase() and it
1158f2e5ff85SAndrea Arcangeli 			 * may rebalance the tree from under us. So
1159f2e5ff85SAndrea Arcangeli 			 * restart the search from scratch. Returning
1160f2e5ff85SAndrea Arcangeli 			 * NULL would be safe too, but we'd generate
1161f2e5ff85SAndrea Arcangeli 			 * false negative insertions just because some
1162f2e5ff85SAndrea Arcangeli 			 * stable_node was stale.
1163f2e5ff85SAndrea Arcangeli 			 */
1164f2e5ff85SAndrea Arcangeli 			goto again;
1165f2e5ff85SAndrea Arcangeli 		}
116631dbd01fSIzik Eidus 
11674035c07aSHugh Dickins 		ret = memcmp_pages(page, tree_page);
1168c8d6553bSHugh Dickins 		put_page(tree_page);
116931dbd01fSIzik Eidus 
11704146d2d6SHugh Dickins 		parent = *new;
1171c8d6553bSHugh Dickins 		if (ret < 0)
11724146d2d6SHugh Dickins 			new = &parent->rb_left;
1173c8d6553bSHugh Dickins 		else if (ret > 0)
11744146d2d6SHugh Dickins 			new = &parent->rb_right;
1175c8d6553bSHugh Dickins 		else {
1176c8d6553bSHugh Dickins 			/*
1177c8d6553bSHugh Dickins 			 * Lock and unlock the stable_node's page (which
1178c8d6553bSHugh Dickins 			 * might already have been migrated) so that page
1179c8d6553bSHugh Dickins 			 * migration is sure to notice its raised count.
1180c8d6553bSHugh Dickins 			 * It would be more elegant to return stable_node
1181c8d6553bSHugh Dickins 			 * than kpage, but that involves more changes.
1182c8d6553bSHugh Dickins 			 */
1183c8d6553bSHugh Dickins 			tree_page = get_ksm_page(stable_node, true);
11844146d2d6SHugh Dickins 			if (tree_page) {
1185c8d6553bSHugh Dickins 				unlock_page(tree_page);
11864146d2d6SHugh Dickins 				if (get_kpfn_nid(stable_node->kpfn) !=
11874146d2d6SHugh Dickins 						NUMA(stable_node->nid)) {
11884146d2d6SHugh Dickins 					put_page(tree_page);
11894146d2d6SHugh Dickins 					goto replace;
11904146d2d6SHugh Dickins 				}
119162b61f61SHugh Dickins 				return tree_page;
119231dbd01fSIzik Eidus 			}
11934146d2d6SHugh Dickins 			/*
11944146d2d6SHugh Dickins 			 * There is now a place for page_node, but the tree may
11954146d2d6SHugh Dickins 			 * have been rebalanced, so re-evaluate parent and new.
11964146d2d6SHugh Dickins 			 */
11974146d2d6SHugh Dickins 			if (page_node)
11984146d2d6SHugh Dickins 				goto again;
11994146d2d6SHugh Dickins 			return NULL;
12004146d2d6SHugh Dickins 		}
1201c8d6553bSHugh Dickins 	}
120231dbd01fSIzik Eidus 
12034146d2d6SHugh Dickins 	if (!page_node)
120431dbd01fSIzik Eidus 		return NULL;
12054146d2d6SHugh Dickins 
12064146d2d6SHugh Dickins 	list_del(&page_node->list);
12074146d2d6SHugh Dickins 	DO_NUMA(page_node->nid = nid);
12084146d2d6SHugh Dickins 	rb_link_node(&page_node->node, parent, new);
1209ef53d16cSHugh Dickins 	rb_insert_color(&page_node->node, root);
12104146d2d6SHugh Dickins 	get_page(page);
12114146d2d6SHugh Dickins 	return page;
12124146d2d6SHugh Dickins 
12134146d2d6SHugh Dickins replace:
12144146d2d6SHugh Dickins 	if (page_node) {
12154146d2d6SHugh Dickins 		list_del(&page_node->list);
12164146d2d6SHugh Dickins 		DO_NUMA(page_node->nid = nid);
1217ef53d16cSHugh Dickins 		rb_replace_node(&stable_node->node, &page_node->node, root);
12184146d2d6SHugh Dickins 		get_page(page);
12194146d2d6SHugh Dickins 	} else {
1220ef53d16cSHugh Dickins 		rb_erase(&stable_node->node, root);
12214146d2d6SHugh Dickins 		page = NULL;
12224146d2d6SHugh Dickins 	}
12234146d2d6SHugh Dickins 	stable_node->head = &migrate_nodes;
12244146d2d6SHugh Dickins 	list_add(&stable_node->list, stable_node->head);
12254146d2d6SHugh Dickins 	return page;
122631dbd01fSIzik Eidus }
122731dbd01fSIzik Eidus 
122831dbd01fSIzik Eidus /*
1229e850dcf5SHugh Dickins  * stable_tree_insert - insert stable tree node pointing to new ksm page
123031dbd01fSIzik Eidus  * into the stable tree.
123131dbd01fSIzik Eidus  *
12327b6ba2c7SHugh Dickins  * This function returns the stable tree node just allocated on success,
12337b6ba2c7SHugh Dickins  * NULL otherwise.
123431dbd01fSIzik Eidus  */
12357b6ba2c7SHugh Dickins static struct stable_node *stable_tree_insert(struct page *kpage)
123631dbd01fSIzik Eidus {
123790bd6fd3SPetr Holasek 	int nid;
123890bd6fd3SPetr Holasek 	unsigned long kpfn;
1239ef53d16cSHugh Dickins 	struct rb_root *root;
124090bd6fd3SPetr Holasek 	struct rb_node **new;
1241f2e5ff85SAndrea Arcangeli 	struct rb_node *parent;
12427b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
124331dbd01fSIzik Eidus 
124490bd6fd3SPetr Holasek 	kpfn = page_to_pfn(kpage);
124590bd6fd3SPetr Holasek 	nid = get_kpfn_nid(kpfn);
1246ef53d16cSHugh Dickins 	root = root_stable_tree + nid;
1247f2e5ff85SAndrea Arcangeli again:
1248f2e5ff85SAndrea Arcangeli 	parent = NULL;
1249ef53d16cSHugh Dickins 	new = &root->rb_node;
125090bd6fd3SPetr Holasek 
125131dbd01fSIzik Eidus 	while (*new) {
12524035c07aSHugh Dickins 		struct page *tree_page;
125331dbd01fSIzik Eidus 		int ret;
125431dbd01fSIzik Eidus 
125531dbd01fSIzik Eidus 		cond_resched();
125608beca44SHugh Dickins 		stable_node = rb_entry(*new, struct stable_node, node);
12578aafa6a4SHugh Dickins 		tree_page = get_ksm_page(stable_node, false);
1258f2e5ff85SAndrea Arcangeli 		if (!tree_page) {
1259f2e5ff85SAndrea Arcangeli 			/*
1260f2e5ff85SAndrea Arcangeli 			 * If we walked over a stale stable_node,
1261f2e5ff85SAndrea Arcangeli 			 * get_ksm_page() will call rb_erase() and it
1262f2e5ff85SAndrea Arcangeli 			 * may rebalance the tree from under us. So
1263f2e5ff85SAndrea Arcangeli 			 * restart the search from scratch. Returning
1264f2e5ff85SAndrea Arcangeli 			 * NULL would be safe too, but we'd generate
1265f2e5ff85SAndrea Arcangeli 			 * false negative insertions just because some
1266f2e5ff85SAndrea Arcangeli 			 * stable_node was stale.
1267f2e5ff85SAndrea Arcangeli 			 */
1268f2e5ff85SAndrea Arcangeli 			goto again;
1269f2e5ff85SAndrea Arcangeli 		}
127031dbd01fSIzik Eidus 
12714035c07aSHugh Dickins 		ret = memcmp_pages(kpage, tree_page);
12724035c07aSHugh Dickins 		put_page(tree_page);
127331dbd01fSIzik Eidus 
127431dbd01fSIzik Eidus 		parent = *new;
127531dbd01fSIzik Eidus 		if (ret < 0)
127631dbd01fSIzik Eidus 			new = &parent->rb_left;
127731dbd01fSIzik Eidus 		else if (ret > 0)
127831dbd01fSIzik Eidus 			new = &parent->rb_right;
127931dbd01fSIzik Eidus 		else {
128031dbd01fSIzik Eidus 			/*
128131dbd01fSIzik Eidus 			 * It is not a bug that stable_tree_search() didn't
128231dbd01fSIzik Eidus 			 * find this node: because at that time our page was
128331dbd01fSIzik Eidus 			 * not yet write-protected, so may have changed since.
128431dbd01fSIzik Eidus 			 */
128531dbd01fSIzik Eidus 			return NULL;
128631dbd01fSIzik Eidus 		}
128731dbd01fSIzik Eidus 	}
128831dbd01fSIzik Eidus 
12897b6ba2c7SHugh Dickins 	stable_node = alloc_stable_node();
12907b6ba2c7SHugh Dickins 	if (!stable_node)
12917b6ba2c7SHugh Dickins 		return NULL;
129231dbd01fSIzik Eidus 
12937b6ba2c7SHugh Dickins 	INIT_HLIST_HEAD(&stable_node->hlist);
129490bd6fd3SPetr Holasek 	stable_node->kpfn = kpfn;
129508beca44SHugh Dickins 	set_page_stable_node(kpage, stable_node);
12964146d2d6SHugh Dickins 	DO_NUMA(stable_node->nid = nid);
1297e850dcf5SHugh Dickins 	rb_link_node(&stable_node->node, parent, new);
1298ef53d16cSHugh Dickins 	rb_insert_color(&stable_node->node, root);
129908beca44SHugh Dickins 
13007b6ba2c7SHugh Dickins 	return stable_node;
130131dbd01fSIzik Eidus }
130231dbd01fSIzik Eidus 
130331dbd01fSIzik Eidus /*
13048dd3557aSHugh Dickins  * unstable_tree_search_insert - search for identical page,
13058dd3557aSHugh Dickins  * else insert rmap_item into the unstable tree.
130631dbd01fSIzik Eidus  *
130731dbd01fSIzik Eidus  * This function searches for a page in the unstable tree identical to the
130831dbd01fSIzik Eidus  * page currently being scanned; and if no identical page is found in the
130931dbd01fSIzik Eidus  * tree, we insert rmap_item as a new object into the unstable tree.
131031dbd01fSIzik Eidus  *
131131dbd01fSIzik Eidus  * This function returns pointer to rmap_item found to be identical
131231dbd01fSIzik Eidus  * to the currently scanned page, NULL otherwise.
131331dbd01fSIzik Eidus  *
131431dbd01fSIzik Eidus  * This function does both searching and inserting, because they share
131531dbd01fSIzik Eidus  * the same walking algorithm in an rbtree.
131631dbd01fSIzik Eidus  */
13178dd3557aSHugh Dickins static
13188dd3557aSHugh Dickins struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
13198dd3557aSHugh Dickins 					      struct page *page,
13208dd3557aSHugh Dickins 					      struct page **tree_pagep)
132131dbd01fSIzik Eidus {
132290bd6fd3SPetr Holasek 	struct rb_node **new;
132390bd6fd3SPetr Holasek 	struct rb_root *root;
132431dbd01fSIzik Eidus 	struct rb_node *parent = NULL;
132590bd6fd3SPetr Holasek 	int nid;
132690bd6fd3SPetr Holasek 
132790bd6fd3SPetr Holasek 	nid = get_kpfn_nid(page_to_pfn(page));
1328ef53d16cSHugh Dickins 	root = root_unstable_tree + nid;
132990bd6fd3SPetr Holasek 	new = &root->rb_node;
133031dbd01fSIzik Eidus 
133131dbd01fSIzik Eidus 	while (*new) {
133231dbd01fSIzik Eidus 		struct rmap_item *tree_rmap_item;
13338dd3557aSHugh Dickins 		struct page *tree_page;
133431dbd01fSIzik Eidus 		int ret;
133531dbd01fSIzik Eidus 
1336d178f27fSHugh Dickins 		cond_resched();
133731dbd01fSIzik Eidus 		tree_rmap_item = rb_entry(*new, struct rmap_item, node);
13388dd3557aSHugh Dickins 		tree_page = get_mergeable_page(tree_rmap_item);
1339c8f95ed1SAndrea Arcangeli 		if (!tree_page)
134031dbd01fSIzik Eidus 			return NULL;
134131dbd01fSIzik Eidus 
134231dbd01fSIzik Eidus 		/*
13438dd3557aSHugh Dickins 		 * Don't substitute a ksm page for a forked page.
134431dbd01fSIzik Eidus 		 */
13458dd3557aSHugh Dickins 		if (page == tree_page) {
13468dd3557aSHugh Dickins 			put_page(tree_page);
134731dbd01fSIzik Eidus 			return NULL;
134831dbd01fSIzik Eidus 		}
134931dbd01fSIzik Eidus 
13508dd3557aSHugh Dickins 		ret = memcmp_pages(page, tree_page);
135131dbd01fSIzik Eidus 
135231dbd01fSIzik Eidus 		parent = *new;
135331dbd01fSIzik Eidus 		if (ret < 0) {
13548dd3557aSHugh Dickins 			put_page(tree_page);
135531dbd01fSIzik Eidus 			new = &parent->rb_left;
135631dbd01fSIzik Eidus 		} else if (ret > 0) {
13578dd3557aSHugh Dickins 			put_page(tree_page);
135831dbd01fSIzik Eidus 			new = &parent->rb_right;
1359b599cbdfSHugh Dickins 		} else if (!ksm_merge_across_nodes &&
1360b599cbdfSHugh Dickins 			   page_to_nid(tree_page) != nid) {
1361b599cbdfSHugh Dickins 			/*
1362b599cbdfSHugh Dickins 			 * If tree_page has been migrated to another NUMA node,
1363b599cbdfSHugh Dickins 			 * it will be flushed out and put in the right unstable
1364b599cbdfSHugh Dickins 			 * tree next time: only merge with it when across_nodes.
1365b599cbdfSHugh Dickins 			 */
1366b599cbdfSHugh Dickins 			put_page(tree_page);
1367b599cbdfSHugh Dickins 			return NULL;
136831dbd01fSIzik Eidus 		} else {
13698dd3557aSHugh Dickins 			*tree_pagep = tree_page;
137031dbd01fSIzik Eidus 			return tree_rmap_item;
137131dbd01fSIzik Eidus 		}
137231dbd01fSIzik Eidus 	}
137331dbd01fSIzik Eidus 
13747b6ba2c7SHugh Dickins 	rmap_item->address |= UNSTABLE_FLAG;
137531dbd01fSIzik Eidus 	rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
1376e850dcf5SHugh Dickins 	DO_NUMA(rmap_item->nid = nid);
137731dbd01fSIzik Eidus 	rb_link_node(&rmap_item->node, parent, new);
137890bd6fd3SPetr Holasek 	rb_insert_color(&rmap_item->node, root);
137931dbd01fSIzik Eidus 
1380473b0ce4SHugh Dickins 	ksm_pages_unshared++;
138131dbd01fSIzik Eidus 	return NULL;
138231dbd01fSIzik Eidus }
138331dbd01fSIzik Eidus 
138431dbd01fSIzik Eidus /*
138531dbd01fSIzik Eidus  * stable_tree_append - add another rmap_item to the linked list of
138631dbd01fSIzik Eidus  * rmap_items hanging off a given node of the stable tree, all sharing
138731dbd01fSIzik Eidus  * the same ksm page.
138831dbd01fSIzik Eidus  */
138931dbd01fSIzik Eidus static void stable_tree_append(struct rmap_item *rmap_item,
13907b6ba2c7SHugh Dickins 			       struct stable_node *stable_node)
139131dbd01fSIzik Eidus {
13927b6ba2c7SHugh Dickins 	rmap_item->head = stable_node;
139331dbd01fSIzik Eidus 	rmap_item->address |= STABLE_FLAG;
13947b6ba2c7SHugh Dickins 	hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
1395e178dfdeSHugh Dickins 
13967b6ba2c7SHugh Dickins 	if (rmap_item->hlist.next)
1397e178dfdeSHugh Dickins 		ksm_pages_sharing++;
13987b6ba2c7SHugh Dickins 	else
13997b6ba2c7SHugh Dickins 		ksm_pages_shared++;
140031dbd01fSIzik Eidus }
140131dbd01fSIzik Eidus 
140231dbd01fSIzik Eidus /*
140381464e30SHugh Dickins  * cmp_and_merge_page - first see if page can be merged into the stable tree;
140481464e30SHugh Dickins  * if not, compare checksum to previous and if it's the same, see if page can
140581464e30SHugh Dickins  * be inserted into the unstable tree, or merged with a page already there and
140681464e30SHugh Dickins  * both transferred to the stable tree.
140731dbd01fSIzik Eidus  *
140831dbd01fSIzik Eidus  * @page: the page that we are searching identical page to.
140931dbd01fSIzik Eidus  * @rmap_item: the reverse mapping into the virtual address of this page
141031dbd01fSIzik Eidus  */
141131dbd01fSIzik Eidus static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
141231dbd01fSIzik Eidus {
141331dbd01fSIzik Eidus 	struct rmap_item *tree_rmap_item;
14148dd3557aSHugh Dickins 	struct page *tree_page = NULL;
14157b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
14168dd3557aSHugh Dickins 	struct page *kpage;
141731dbd01fSIzik Eidus 	unsigned int checksum;
141831dbd01fSIzik Eidus 	int err;
141931dbd01fSIzik Eidus 
14204146d2d6SHugh Dickins 	stable_node = page_stable_node(page);
14214146d2d6SHugh Dickins 	if (stable_node) {
14224146d2d6SHugh Dickins 		if (stable_node->head != &migrate_nodes &&
14234146d2d6SHugh Dickins 		    get_kpfn_nid(stable_node->kpfn) != NUMA(stable_node->nid)) {
14244146d2d6SHugh Dickins 			rb_erase(&stable_node->node,
1425ef53d16cSHugh Dickins 				 root_stable_tree + NUMA(stable_node->nid));
14264146d2d6SHugh Dickins 			stable_node->head = &migrate_nodes;
14274146d2d6SHugh Dickins 			list_add(&stable_node->list, stable_node->head);
14284146d2d6SHugh Dickins 		}
14294146d2d6SHugh Dickins 		if (stable_node->head != &migrate_nodes &&
14304146d2d6SHugh Dickins 		    rmap_item->head == stable_node)
14314146d2d6SHugh Dickins 			return;
14324146d2d6SHugh Dickins 	}
143331dbd01fSIzik Eidus 
143431dbd01fSIzik Eidus 	/* We first start with searching the page inside the stable tree */
143562b61f61SHugh Dickins 	kpage = stable_tree_search(page);
14364146d2d6SHugh Dickins 	if (kpage == page && rmap_item->head == stable_node) {
14374146d2d6SHugh Dickins 		put_page(kpage);
14384146d2d6SHugh Dickins 		return;
14394146d2d6SHugh Dickins 	}
14404146d2d6SHugh Dickins 
14414146d2d6SHugh Dickins 	remove_rmap_item_from_tree(rmap_item);
14424146d2d6SHugh Dickins 
144362b61f61SHugh Dickins 	if (kpage) {
144408beca44SHugh Dickins 		err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
144531dbd01fSIzik Eidus 		if (!err) {
144631dbd01fSIzik Eidus 			/*
144731dbd01fSIzik Eidus 			 * The page was successfully merged:
144831dbd01fSIzik Eidus 			 * add its rmap_item to the stable tree.
144931dbd01fSIzik Eidus 			 */
14505ad64688SHugh Dickins 			lock_page(kpage);
145162b61f61SHugh Dickins 			stable_tree_append(rmap_item, page_stable_node(kpage));
14525ad64688SHugh Dickins 			unlock_page(kpage);
145331dbd01fSIzik Eidus 		}
14548dd3557aSHugh Dickins 		put_page(kpage);
145531dbd01fSIzik Eidus 		return;
145631dbd01fSIzik Eidus 	}
145731dbd01fSIzik Eidus 
145831dbd01fSIzik Eidus 	/*
14594035c07aSHugh Dickins 	 * If the hash value of the page has changed from the last time
14604035c07aSHugh Dickins 	 * we calculated it, this page is changing frequently: therefore we
14614035c07aSHugh Dickins 	 * don't want to insert it in the unstable tree, and we don't want
14624035c07aSHugh Dickins 	 * to waste our time searching for something identical to it there.
146331dbd01fSIzik Eidus 	 */
146431dbd01fSIzik Eidus 	checksum = calc_checksum(page);
146531dbd01fSIzik Eidus 	if (rmap_item->oldchecksum != checksum) {
146631dbd01fSIzik Eidus 		rmap_item->oldchecksum = checksum;
146731dbd01fSIzik Eidus 		return;
146831dbd01fSIzik Eidus 	}
146931dbd01fSIzik Eidus 
14708dd3557aSHugh Dickins 	tree_rmap_item =
14718dd3557aSHugh Dickins 		unstable_tree_search_insert(rmap_item, page, &tree_page);
147231dbd01fSIzik Eidus 	if (tree_rmap_item) {
14738dd3557aSHugh Dickins 		kpage = try_to_merge_two_pages(rmap_item, page,
14748dd3557aSHugh Dickins 						tree_rmap_item, tree_page);
14758dd3557aSHugh Dickins 		put_page(tree_page);
14768dd3557aSHugh Dickins 		if (kpage) {
1477bc56620bSHugh Dickins 			/*
1478bc56620bSHugh Dickins 			 * The pages were successfully merged: insert new
1479bc56620bSHugh Dickins 			 * node in the stable tree and add both rmap_items.
1480bc56620bSHugh Dickins 			 */
14815ad64688SHugh Dickins 			lock_page(kpage);
14827b6ba2c7SHugh Dickins 			stable_node = stable_tree_insert(kpage);
14837b6ba2c7SHugh Dickins 			if (stable_node) {
14847b6ba2c7SHugh Dickins 				stable_tree_append(tree_rmap_item, stable_node);
14857b6ba2c7SHugh Dickins 				stable_tree_append(rmap_item, stable_node);
14867b6ba2c7SHugh Dickins 			}
14875ad64688SHugh Dickins 			unlock_page(kpage);
14887b6ba2c7SHugh Dickins 
148931dbd01fSIzik Eidus 			/*
149031dbd01fSIzik Eidus 			 * If we fail to insert the page into the stable tree,
149131dbd01fSIzik Eidus 			 * we will have 2 virtual addresses that are pointing
149231dbd01fSIzik Eidus 			 * to a ksm page left outside the stable tree,
149331dbd01fSIzik Eidus 			 * in which case we need to break_cow on both.
149431dbd01fSIzik Eidus 			 */
14957b6ba2c7SHugh Dickins 			if (!stable_node) {
14968dd3557aSHugh Dickins 				break_cow(tree_rmap_item);
14978dd3557aSHugh Dickins 				break_cow(rmap_item);
149831dbd01fSIzik Eidus 			}
149931dbd01fSIzik Eidus 		}
150031dbd01fSIzik Eidus 	}
150131dbd01fSIzik Eidus }
150231dbd01fSIzik Eidus 
150331dbd01fSIzik Eidus static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
15046514d511SHugh Dickins 					    struct rmap_item **rmap_list,
150531dbd01fSIzik Eidus 					    unsigned long addr)
150631dbd01fSIzik Eidus {
150731dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
150831dbd01fSIzik Eidus 
15096514d511SHugh Dickins 	while (*rmap_list) {
15106514d511SHugh Dickins 		rmap_item = *rmap_list;
151193d17715SHugh Dickins 		if ((rmap_item->address & PAGE_MASK) == addr)
151231dbd01fSIzik Eidus 			return rmap_item;
151331dbd01fSIzik Eidus 		if (rmap_item->address > addr)
151431dbd01fSIzik Eidus 			break;
15156514d511SHugh Dickins 		*rmap_list = rmap_item->rmap_list;
151631dbd01fSIzik Eidus 		remove_rmap_item_from_tree(rmap_item);
151731dbd01fSIzik Eidus 		free_rmap_item(rmap_item);
151831dbd01fSIzik Eidus 	}
151931dbd01fSIzik Eidus 
152031dbd01fSIzik Eidus 	rmap_item = alloc_rmap_item();
152131dbd01fSIzik Eidus 	if (rmap_item) {
152231dbd01fSIzik Eidus 		/* It has already been zeroed */
152331dbd01fSIzik Eidus 		rmap_item->mm = mm_slot->mm;
152431dbd01fSIzik Eidus 		rmap_item->address = addr;
15256514d511SHugh Dickins 		rmap_item->rmap_list = *rmap_list;
15266514d511SHugh Dickins 		*rmap_list = rmap_item;
152731dbd01fSIzik Eidus 	}
152831dbd01fSIzik Eidus 	return rmap_item;
152931dbd01fSIzik Eidus }
153031dbd01fSIzik Eidus 
153131dbd01fSIzik Eidus static struct rmap_item *scan_get_next_rmap_item(struct page **page)
153231dbd01fSIzik Eidus {
153331dbd01fSIzik Eidus 	struct mm_struct *mm;
153431dbd01fSIzik Eidus 	struct mm_slot *slot;
153531dbd01fSIzik Eidus 	struct vm_area_struct *vma;
153631dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
153790bd6fd3SPetr Holasek 	int nid;
153831dbd01fSIzik Eidus 
153931dbd01fSIzik Eidus 	if (list_empty(&ksm_mm_head.mm_list))
154031dbd01fSIzik Eidus 		return NULL;
154131dbd01fSIzik Eidus 
154231dbd01fSIzik Eidus 	slot = ksm_scan.mm_slot;
154331dbd01fSIzik Eidus 	if (slot == &ksm_mm_head) {
15442919bfd0SHugh Dickins 		/*
15452919bfd0SHugh Dickins 		 * A number of pages can hang around indefinitely on per-cpu
15462919bfd0SHugh Dickins 		 * pagevecs, raised page count preventing write_protect_page
15472919bfd0SHugh Dickins 		 * from merging them.  Though it doesn't really matter much,
15482919bfd0SHugh Dickins 		 * it is puzzling to see some stuck in pages_volatile until
15492919bfd0SHugh Dickins 		 * other activity jostles them out, and they also prevented
15502919bfd0SHugh Dickins 		 * LTP's KSM test from succeeding deterministically; so drain
15512919bfd0SHugh Dickins 		 * them here (here rather than on entry to ksm_do_scan(),
15522919bfd0SHugh Dickins 		 * so we don't IPI too often when pages_to_scan is set low).
15532919bfd0SHugh Dickins 		 */
15542919bfd0SHugh Dickins 		lru_add_drain_all();
15552919bfd0SHugh Dickins 
15564146d2d6SHugh Dickins 		/*
15574146d2d6SHugh Dickins 		 * Whereas stale stable_nodes on the stable_tree itself
15584146d2d6SHugh Dickins 		 * get pruned in the regular course of stable_tree_search(),
15594146d2d6SHugh Dickins 		 * those moved out to the migrate_nodes list can accumulate:
15604146d2d6SHugh Dickins 		 * so prune them once before each full scan.
15614146d2d6SHugh Dickins 		 */
15624146d2d6SHugh Dickins 		if (!ksm_merge_across_nodes) {
156303640418SGeliang Tang 			struct stable_node *stable_node, *next;
15644146d2d6SHugh Dickins 			struct page *page;
15654146d2d6SHugh Dickins 
156603640418SGeliang Tang 			list_for_each_entry_safe(stable_node, next,
156703640418SGeliang Tang 						 &migrate_nodes, list) {
15684146d2d6SHugh Dickins 				page = get_ksm_page(stable_node, false);
15694146d2d6SHugh Dickins 				if (page)
15704146d2d6SHugh Dickins 					put_page(page);
15714146d2d6SHugh Dickins 				cond_resched();
15724146d2d6SHugh Dickins 			}
15734146d2d6SHugh Dickins 		}
15744146d2d6SHugh Dickins 
1575ef53d16cSHugh Dickins 		for (nid = 0; nid < ksm_nr_node_ids; nid++)
157690bd6fd3SPetr Holasek 			root_unstable_tree[nid] = RB_ROOT;
157731dbd01fSIzik Eidus 
157831dbd01fSIzik Eidus 		spin_lock(&ksm_mmlist_lock);
157931dbd01fSIzik Eidus 		slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
158031dbd01fSIzik Eidus 		ksm_scan.mm_slot = slot;
158131dbd01fSIzik Eidus 		spin_unlock(&ksm_mmlist_lock);
15822b472611SHugh Dickins 		/*
15832b472611SHugh Dickins 		 * Although we tested list_empty() above, a racing __ksm_exit
15842b472611SHugh Dickins 		 * of the last mm on the list may have removed it since then.
15852b472611SHugh Dickins 		 */
15862b472611SHugh Dickins 		if (slot == &ksm_mm_head)
15872b472611SHugh Dickins 			return NULL;
158831dbd01fSIzik Eidus next_mm:
158931dbd01fSIzik Eidus 		ksm_scan.address = 0;
15906514d511SHugh Dickins 		ksm_scan.rmap_list = &slot->rmap_list;
159131dbd01fSIzik Eidus 	}
159231dbd01fSIzik Eidus 
159331dbd01fSIzik Eidus 	mm = slot->mm;
159431dbd01fSIzik Eidus 	down_read(&mm->mmap_sem);
15959ba69294SHugh Dickins 	if (ksm_test_exit(mm))
15969ba69294SHugh Dickins 		vma = NULL;
15979ba69294SHugh Dickins 	else
15989ba69294SHugh Dickins 		vma = find_vma(mm, ksm_scan.address);
15999ba69294SHugh Dickins 
16009ba69294SHugh Dickins 	for (; vma; vma = vma->vm_next) {
160131dbd01fSIzik Eidus 		if (!(vma->vm_flags & VM_MERGEABLE))
160231dbd01fSIzik Eidus 			continue;
160331dbd01fSIzik Eidus 		if (ksm_scan.address < vma->vm_start)
160431dbd01fSIzik Eidus 			ksm_scan.address = vma->vm_start;
160531dbd01fSIzik Eidus 		if (!vma->anon_vma)
160631dbd01fSIzik Eidus 			ksm_scan.address = vma->vm_end;
160731dbd01fSIzik Eidus 
160831dbd01fSIzik Eidus 		while (ksm_scan.address < vma->vm_end) {
16099ba69294SHugh Dickins 			if (ksm_test_exit(mm))
16109ba69294SHugh Dickins 				break;
161131dbd01fSIzik Eidus 			*page = follow_page(vma, ksm_scan.address, FOLL_GET);
161221ae5b01SAndrea Arcangeli 			if (IS_ERR_OR_NULL(*page)) {
161321ae5b01SAndrea Arcangeli 				ksm_scan.address += PAGE_SIZE;
161421ae5b01SAndrea Arcangeli 				cond_resched();
161521ae5b01SAndrea Arcangeli 				continue;
161621ae5b01SAndrea Arcangeli 			}
1617f765f540SKirill A. Shutemov 			if (PageAnon(*page)) {
161831dbd01fSIzik Eidus 				flush_anon_page(vma, *page, ksm_scan.address);
161931dbd01fSIzik Eidus 				flush_dcache_page(*page);
162031dbd01fSIzik Eidus 				rmap_item = get_next_rmap_item(slot,
16216514d511SHugh Dickins 					ksm_scan.rmap_list, ksm_scan.address);
162231dbd01fSIzik Eidus 				if (rmap_item) {
16236514d511SHugh Dickins 					ksm_scan.rmap_list =
16246514d511SHugh Dickins 							&rmap_item->rmap_list;
162531dbd01fSIzik Eidus 					ksm_scan.address += PAGE_SIZE;
162631dbd01fSIzik Eidus 				} else
162731dbd01fSIzik Eidus 					put_page(*page);
162831dbd01fSIzik Eidus 				up_read(&mm->mmap_sem);
162931dbd01fSIzik Eidus 				return rmap_item;
163031dbd01fSIzik Eidus 			}
163131dbd01fSIzik Eidus 			put_page(*page);
163231dbd01fSIzik Eidus 			ksm_scan.address += PAGE_SIZE;
163331dbd01fSIzik Eidus 			cond_resched();
163431dbd01fSIzik Eidus 		}
163531dbd01fSIzik Eidus 	}
163631dbd01fSIzik Eidus 
16379ba69294SHugh Dickins 	if (ksm_test_exit(mm)) {
16389ba69294SHugh Dickins 		ksm_scan.address = 0;
16396514d511SHugh Dickins 		ksm_scan.rmap_list = &slot->rmap_list;
16409ba69294SHugh Dickins 	}
164131dbd01fSIzik Eidus 	/*
164231dbd01fSIzik Eidus 	 * Nuke all the rmap_items that are above this current rmap:
164331dbd01fSIzik Eidus 	 * because there were no VM_MERGEABLE vmas with such addresses.
164431dbd01fSIzik Eidus 	 */
16456514d511SHugh Dickins 	remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
164631dbd01fSIzik Eidus 
164731dbd01fSIzik Eidus 	spin_lock(&ksm_mmlist_lock);
1648cd551f97SHugh Dickins 	ksm_scan.mm_slot = list_entry(slot->mm_list.next,
1649cd551f97SHugh Dickins 						struct mm_slot, mm_list);
1650cd551f97SHugh Dickins 	if (ksm_scan.address == 0) {
1651cd551f97SHugh Dickins 		/*
1652cd551f97SHugh Dickins 		 * We've completed a full scan of all vmas, holding mmap_sem
1653cd551f97SHugh Dickins 		 * throughout, and found no VM_MERGEABLE: so do the same as
1654cd551f97SHugh Dickins 		 * __ksm_exit does to remove this mm from all our lists now.
16559ba69294SHugh Dickins 		 * This applies either when cleaning up after __ksm_exit
16569ba69294SHugh Dickins 		 * (but beware: we can reach here even before __ksm_exit),
16579ba69294SHugh Dickins 		 * or when all VM_MERGEABLE areas have been unmapped (and
16589ba69294SHugh Dickins 		 * mmap_sem then protects against race with MADV_MERGEABLE).
1659cd551f97SHugh Dickins 		 */
16604ca3a69bSSasha Levin 		hash_del(&slot->link);
1661cd551f97SHugh Dickins 		list_del(&slot->mm_list);
16629ba69294SHugh Dickins 		spin_unlock(&ksm_mmlist_lock);
16639ba69294SHugh Dickins 
1664cd551f97SHugh Dickins 		free_mm_slot(slot);
1665cd551f97SHugh Dickins 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
16669ba69294SHugh Dickins 		up_read(&mm->mmap_sem);
16679ba69294SHugh Dickins 		mmdrop(mm);
16689ba69294SHugh Dickins 	} else {
1669cd551f97SHugh Dickins 		up_read(&mm->mmap_sem);
16707496fea9SZhou Chengming 		/*
16717496fea9SZhou Chengming 		 * up_read(&mm->mmap_sem) first because after
16727496fea9SZhou Chengming 		 * spin_unlock(&ksm_mmlist_lock) run, the "mm" may
16737496fea9SZhou Chengming 		 * already have been freed under us by __ksm_exit()
16747496fea9SZhou Chengming 		 * because the "mm_slot" is still hashed and
16757496fea9SZhou Chengming 		 * ksm_scan.mm_slot doesn't point to it anymore.
16767496fea9SZhou Chengming 		 */
16777496fea9SZhou Chengming 		spin_unlock(&ksm_mmlist_lock);
16789ba69294SHugh Dickins 	}
167931dbd01fSIzik Eidus 
168031dbd01fSIzik Eidus 	/* Repeat until we've completed scanning the whole list */
1681cd551f97SHugh Dickins 	slot = ksm_scan.mm_slot;
168231dbd01fSIzik Eidus 	if (slot != &ksm_mm_head)
168331dbd01fSIzik Eidus 		goto next_mm;
168431dbd01fSIzik Eidus 
168531dbd01fSIzik Eidus 	ksm_scan.seqnr++;
168631dbd01fSIzik Eidus 	return NULL;
168731dbd01fSIzik Eidus }
168831dbd01fSIzik Eidus 
168931dbd01fSIzik Eidus /**
169031dbd01fSIzik Eidus  * ksm_do_scan  - the ksm scanner main worker function.
169131dbd01fSIzik Eidus  * @scan_npages - number of pages we want to scan before we return.
169231dbd01fSIzik Eidus  */
169331dbd01fSIzik Eidus static void ksm_do_scan(unsigned int scan_npages)
169431dbd01fSIzik Eidus {
169531dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
169622eccdd7SDan Carpenter 	struct page *uninitialized_var(page);
169731dbd01fSIzik Eidus 
1698878aee7dSAndrea Arcangeli 	while (scan_npages-- && likely(!freezing(current))) {
169931dbd01fSIzik Eidus 		cond_resched();
170031dbd01fSIzik Eidus 		rmap_item = scan_get_next_rmap_item(&page);
170131dbd01fSIzik Eidus 		if (!rmap_item)
170231dbd01fSIzik Eidus 			return;
170331dbd01fSIzik Eidus 		cmp_and_merge_page(page, rmap_item);
170431dbd01fSIzik Eidus 		put_page(page);
170531dbd01fSIzik Eidus 	}
170631dbd01fSIzik Eidus }
170731dbd01fSIzik Eidus 
17086e158384SHugh Dickins static int ksmd_should_run(void)
17096e158384SHugh Dickins {
17106e158384SHugh Dickins 	return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
17116e158384SHugh Dickins }
17126e158384SHugh Dickins 
171331dbd01fSIzik Eidus static int ksm_scan_thread(void *nothing)
171431dbd01fSIzik Eidus {
1715878aee7dSAndrea Arcangeli 	set_freezable();
1716339aa624SIzik Eidus 	set_user_nice(current, 5);
171731dbd01fSIzik Eidus 
171831dbd01fSIzik Eidus 	while (!kthread_should_stop()) {
171931dbd01fSIzik Eidus 		mutex_lock(&ksm_thread_mutex);
1720ef4d43a8SHugh Dickins 		wait_while_offlining();
17216e158384SHugh Dickins 		if (ksmd_should_run())
172231dbd01fSIzik Eidus 			ksm_do_scan(ksm_thread_pages_to_scan);
172331dbd01fSIzik Eidus 		mutex_unlock(&ksm_thread_mutex);
17246e158384SHugh Dickins 
1725878aee7dSAndrea Arcangeli 		try_to_freeze();
1726878aee7dSAndrea Arcangeli 
17276e158384SHugh Dickins 		if (ksmd_should_run()) {
172831dbd01fSIzik Eidus 			schedule_timeout_interruptible(
172931dbd01fSIzik Eidus 				msecs_to_jiffies(ksm_thread_sleep_millisecs));
173031dbd01fSIzik Eidus 		} else {
1731878aee7dSAndrea Arcangeli 			wait_event_freezable(ksm_thread_wait,
17326e158384SHugh Dickins 				ksmd_should_run() || kthread_should_stop());
173331dbd01fSIzik Eidus 		}
173431dbd01fSIzik Eidus 	}
173531dbd01fSIzik Eidus 	return 0;
173631dbd01fSIzik Eidus }
173731dbd01fSIzik Eidus 
1738f8af4da3SHugh Dickins int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
1739f8af4da3SHugh Dickins 		unsigned long end, int advice, unsigned long *vm_flags)
1740f8af4da3SHugh Dickins {
1741f8af4da3SHugh Dickins 	struct mm_struct *mm = vma->vm_mm;
1742d952b791SHugh Dickins 	int err;
1743f8af4da3SHugh Dickins 
1744f8af4da3SHugh Dickins 	switch (advice) {
1745f8af4da3SHugh Dickins 	case MADV_MERGEABLE:
1746f8af4da3SHugh Dickins 		/*
1747f8af4da3SHugh Dickins 		 * Be somewhat over-protective for now!
1748f8af4da3SHugh Dickins 		 */
1749f8af4da3SHugh Dickins 		if (*vm_flags & (VM_MERGEABLE | VM_SHARED  | VM_MAYSHARE   |
1750f8af4da3SHugh Dickins 				 VM_PFNMAP    | VM_IO      | VM_DONTEXPAND |
17510661a336SKirill A. Shutemov 				 VM_HUGETLB | VM_MIXEDMAP))
1752f8af4da3SHugh Dickins 			return 0;		/* just ignore the advice */
1753f8af4da3SHugh Dickins 
1754cc2383ecSKonstantin Khlebnikov #ifdef VM_SAO
1755cc2383ecSKonstantin Khlebnikov 		if (*vm_flags & VM_SAO)
1756cc2383ecSKonstantin Khlebnikov 			return 0;
1757cc2383ecSKonstantin Khlebnikov #endif
1758cc2383ecSKonstantin Khlebnikov 
1759d952b791SHugh Dickins 		if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
1760d952b791SHugh Dickins 			err = __ksm_enter(mm);
1761d952b791SHugh Dickins 			if (err)
1762d952b791SHugh Dickins 				return err;
1763d952b791SHugh Dickins 		}
1764f8af4da3SHugh Dickins 
1765f8af4da3SHugh Dickins 		*vm_flags |= VM_MERGEABLE;
1766f8af4da3SHugh Dickins 		break;
1767f8af4da3SHugh Dickins 
1768f8af4da3SHugh Dickins 	case MADV_UNMERGEABLE:
1769f8af4da3SHugh Dickins 		if (!(*vm_flags & VM_MERGEABLE))
1770f8af4da3SHugh Dickins 			return 0;		/* just ignore the advice */
1771f8af4da3SHugh Dickins 
1772d952b791SHugh Dickins 		if (vma->anon_vma) {
1773d952b791SHugh Dickins 			err = unmerge_ksm_pages(vma, start, end);
1774d952b791SHugh Dickins 			if (err)
1775d952b791SHugh Dickins 				return err;
1776d952b791SHugh Dickins 		}
1777f8af4da3SHugh Dickins 
1778f8af4da3SHugh Dickins 		*vm_flags &= ~VM_MERGEABLE;
1779f8af4da3SHugh Dickins 		break;
1780f8af4da3SHugh Dickins 	}
1781f8af4da3SHugh Dickins 
1782f8af4da3SHugh Dickins 	return 0;
1783f8af4da3SHugh Dickins }
1784f8af4da3SHugh Dickins 
1785f8af4da3SHugh Dickins int __ksm_enter(struct mm_struct *mm)
1786f8af4da3SHugh Dickins {
17876e158384SHugh Dickins 	struct mm_slot *mm_slot;
17886e158384SHugh Dickins 	int needs_wakeup;
17896e158384SHugh Dickins 
17906e158384SHugh Dickins 	mm_slot = alloc_mm_slot();
179131dbd01fSIzik Eidus 	if (!mm_slot)
179231dbd01fSIzik Eidus 		return -ENOMEM;
179331dbd01fSIzik Eidus 
17946e158384SHugh Dickins 	/* Check ksm_run too?  Would need tighter locking */
17956e158384SHugh Dickins 	needs_wakeup = list_empty(&ksm_mm_head.mm_list);
17966e158384SHugh Dickins 
179731dbd01fSIzik Eidus 	spin_lock(&ksm_mmlist_lock);
179831dbd01fSIzik Eidus 	insert_to_mm_slots_hash(mm, mm_slot);
179931dbd01fSIzik Eidus 	/*
1800cbf86cfeSHugh Dickins 	 * When KSM_RUN_MERGE (or KSM_RUN_STOP),
1801cbf86cfeSHugh Dickins 	 * insert just behind the scanning cursor, to let the area settle
180231dbd01fSIzik Eidus 	 * down a little; when fork is followed by immediate exec, we don't
180331dbd01fSIzik Eidus 	 * want ksmd to waste time setting up and tearing down an rmap_list.
1804cbf86cfeSHugh Dickins 	 *
1805cbf86cfeSHugh Dickins 	 * But when KSM_RUN_UNMERGE, it's important to insert ahead of its
1806cbf86cfeSHugh Dickins 	 * scanning cursor, otherwise KSM pages in newly forked mms will be
1807cbf86cfeSHugh Dickins 	 * missed: then we might as well insert at the end of the list.
180831dbd01fSIzik Eidus 	 */
1809cbf86cfeSHugh Dickins 	if (ksm_run & KSM_RUN_UNMERGE)
1810cbf86cfeSHugh Dickins 		list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list);
1811cbf86cfeSHugh Dickins 	else
181231dbd01fSIzik Eidus 		list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
181331dbd01fSIzik Eidus 	spin_unlock(&ksm_mmlist_lock);
181431dbd01fSIzik Eidus 
1815f8af4da3SHugh Dickins 	set_bit(MMF_VM_MERGEABLE, &mm->flags);
18169ba69294SHugh Dickins 	atomic_inc(&mm->mm_count);
18176e158384SHugh Dickins 
18186e158384SHugh Dickins 	if (needs_wakeup)
18196e158384SHugh Dickins 		wake_up_interruptible(&ksm_thread_wait);
18206e158384SHugh Dickins 
1821f8af4da3SHugh Dickins 	return 0;
1822f8af4da3SHugh Dickins }
1823f8af4da3SHugh Dickins 
18241c2fb7a4SAndrea Arcangeli void __ksm_exit(struct mm_struct *mm)
1825f8af4da3SHugh Dickins {
1826cd551f97SHugh Dickins 	struct mm_slot *mm_slot;
18279ba69294SHugh Dickins 	int easy_to_free = 0;
1828cd551f97SHugh Dickins 
182931dbd01fSIzik Eidus 	/*
18309ba69294SHugh Dickins 	 * This process is exiting: if it's straightforward (as is the
18319ba69294SHugh Dickins 	 * case when ksmd was never running), free mm_slot immediately.
18329ba69294SHugh Dickins 	 * But if it's at the cursor or has rmap_items linked to it, use
18339ba69294SHugh Dickins 	 * mmap_sem to synchronize with any break_cows before pagetables
18349ba69294SHugh Dickins 	 * are freed, and leave the mm_slot on the list for ksmd to free.
18359ba69294SHugh Dickins 	 * Beware: ksm may already have noticed it exiting and freed the slot.
183631dbd01fSIzik Eidus 	 */
18379ba69294SHugh Dickins 
1838cd551f97SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
1839cd551f97SHugh Dickins 	mm_slot = get_mm_slot(mm);
18409ba69294SHugh Dickins 	if (mm_slot && ksm_scan.mm_slot != mm_slot) {
18416514d511SHugh Dickins 		if (!mm_slot->rmap_list) {
18424ca3a69bSSasha Levin 			hash_del(&mm_slot->link);
1843cd551f97SHugh Dickins 			list_del(&mm_slot->mm_list);
18449ba69294SHugh Dickins 			easy_to_free = 1;
18459ba69294SHugh Dickins 		} else {
18469ba69294SHugh Dickins 			list_move(&mm_slot->mm_list,
18479ba69294SHugh Dickins 				  &ksm_scan.mm_slot->mm_list);
18489ba69294SHugh Dickins 		}
18499ba69294SHugh Dickins 	}
1850cd551f97SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
1851cd551f97SHugh Dickins 
18529ba69294SHugh Dickins 	if (easy_to_free) {
1853cd551f97SHugh Dickins 		free_mm_slot(mm_slot);
1854cd551f97SHugh Dickins 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
18559ba69294SHugh Dickins 		mmdrop(mm);
18569ba69294SHugh Dickins 	} else if (mm_slot) {
18579ba69294SHugh Dickins 		down_write(&mm->mmap_sem);
18589ba69294SHugh Dickins 		up_write(&mm->mmap_sem);
18599ba69294SHugh Dickins 	}
1860f8af4da3SHugh Dickins }
186131dbd01fSIzik Eidus 
1862cbf86cfeSHugh Dickins struct page *ksm_might_need_to_copy(struct page *page,
18635ad64688SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
18645ad64688SHugh Dickins {
1865cbf86cfeSHugh Dickins 	struct anon_vma *anon_vma = page_anon_vma(page);
18665ad64688SHugh Dickins 	struct page *new_page;
18675ad64688SHugh Dickins 
1868cbf86cfeSHugh Dickins 	if (PageKsm(page)) {
1869cbf86cfeSHugh Dickins 		if (page_stable_node(page) &&
1870cbf86cfeSHugh Dickins 		    !(ksm_run & KSM_RUN_UNMERGE))
1871cbf86cfeSHugh Dickins 			return page;	/* no need to copy it */
1872cbf86cfeSHugh Dickins 	} else if (!anon_vma) {
1873cbf86cfeSHugh Dickins 		return page;		/* no need to copy it */
1874cbf86cfeSHugh Dickins 	} else if (anon_vma->root == vma->anon_vma->root &&
1875cbf86cfeSHugh Dickins 		 page->index == linear_page_index(vma, address)) {
1876cbf86cfeSHugh Dickins 		return page;		/* still no need to copy it */
1877cbf86cfeSHugh Dickins 	}
1878cbf86cfeSHugh Dickins 	if (!PageUptodate(page))
1879cbf86cfeSHugh Dickins 		return page;		/* let do_swap_page report the error */
1880cbf86cfeSHugh Dickins 
18815ad64688SHugh Dickins 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
18825ad64688SHugh Dickins 	if (new_page) {
18835ad64688SHugh Dickins 		copy_user_highpage(new_page, page, address, vma);
18845ad64688SHugh Dickins 
18855ad64688SHugh Dickins 		SetPageDirty(new_page);
18865ad64688SHugh Dickins 		__SetPageUptodate(new_page);
188748c935adSKirill A. Shutemov 		__SetPageLocked(new_page);
18885ad64688SHugh Dickins 	}
18895ad64688SHugh Dickins 
18905ad64688SHugh Dickins 	return new_page;
18915ad64688SHugh Dickins }
18925ad64688SHugh Dickins 
1893051ac83aSJoonsoo Kim int rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc)
1894e9995ef9SHugh Dickins {
1895e9995ef9SHugh Dickins 	struct stable_node *stable_node;
1896e9995ef9SHugh Dickins 	struct rmap_item *rmap_item;
1897e9995ef9SHugh Dickins 	int ret = SWAP_AGAIN;
1898e9995ef9SHugh Dickins 	int search_new_forks = 0;
1899e9995ef9SHugh Dickins 
1900309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageKsm(page), page);
19019f32624bSJoonsoo Kim 
19029f32624bSJoonsoo Kim 	/*
19039f32624bSJoonsoo Kim 	 * Rely on the page lock to protect against concurrent modifications
19049f32624bSJoonsoo Kim 	 * to that page's node of the stable tree.
19059f32624bSJoonsoo Kim 	 */
1906309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
1907e9995ef9SHugh Dickins 
1908e9995ef9SHugh Dickins 	stable_node = page_stable_node(page);
1909e9995ef9SHugh Dickins 	if (!stable_node)
1910e9995ef9SHugh Dickins 		return ret;
1911e9995ef9SHugh Dickins again:
1912b67bfe0dSSasha Levin 	hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
1913e9995ef9SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
19145beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1915e9995ef9SHugh Dickins 		struct vm_area_struct *vma;
1916e9995ef9SHugh Dickins 
1917ad12695fSAndrea Arcangeli 		cond_resched();
1918b6b19f25SHugh Dickins 		anon_vma_lock_read(anon_vma);
1919bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1920bf181b9fSMichel Lespinasse 					       0, ULONG_MAX) {
1921ad12695fSAndrea Arcangeli 			cond_resched();
19225beb4930SRik van Riel 			vma = vmac->vma;
1923e9995ef9SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1924e9995ef9SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1925e9995ef9SHugh Dickins 				continue;
1926e9995ef9SHugh Dickins 			/*
1927e9995ef9SHugh Dickins 			 * Initially we examine only the vma which covers this
1928e9995ef9SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1929e9995ef9SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1930e9995ef9SHugh Dickins 			 * were forked from the original since ksmd passed.
1931e9995ef9SHugh Dickins 			 */
1932e9995ef9SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1933e9995ef9SHugh Dickins 				continue;
1934e9995ef9SHugh Dickins 
19350dd1c7bbSJoonsoo Kim 			if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
19360dd1c7bbSJoonsoo Kim 				continue;
19370dd1c7bbSJoonsoo Kim 
1938051ac83aSJoonsoo Kim 			ret = rwc->rmap_one(page, vma,
1939051ac83aSJoonsoo Kim 					rmap_item->address, rwc->arg);
1940e9995ef9SHugh Dickins 			if (ret != SWAP_AGAIN) {
1941b6b19f25SHugh Dickins 				anon_vma_unlock_read(anon_vma);
1942e9995ef9SHugh Dickins 				goto out;
1943e9995ef9SHugh Dickins 			}
19440dd1c7bbSJoonsoo Kim 			if (rwc->done && rwc->done(page)) {
19450dd1c7bbSJoonsoo Kim 				anon_vma_unlock_read(anon_vma);
19460dd1c7bbSJoonsoo Kim 				goto out;
19470dd1c7bbSJoonsoo Kim 			}
1948e9995ef9SHugh Dickins 		}
1949b6b19f25SHugh Dickins 		anon_vma_unlock_read(anon_vma);
1950e9995ef9SHugh Dickins 	}
1951e9995ef9SHugh Dickins 	if (!search_new_forks++)
1952e9995ef9SHugh Dickins 		goto again;
1953e9995ef9SHugh Dickins out:
1954e9995ef9SHugh Dickins 	return ret;
1955e9995ef9SHugh Dickins }
1956e9995ef9SHugh Dickins 
195752629506SJoonsoo Kim #ifdef CONFIG_MIGRATION
1958e9995ef9SHugh Dickins void ksm_migrate_page(struct page *newpage, struct page *oldpage)
1959e9995ef9SHugh Dickins {
1960e9995ef9SHugh Dickins 	struct stable_node *stable_node;
1961e9995ef9SHugh Dickins 
1962309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
1963309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
1964309381feSSasha Levin 	VM_BUG_ON_PAGE(newpage->mapping != oldpage->mapping, newpage);
1965e9995ef9SHugh Dickins 
1966e9995ef9SHugh Dickins 	stable_node = page_stable_node(newpage);
1967e9995ef9SHugh Dickins 	if (stable_node) {
1968309381feSSasha Levin 		VM_BUG_ON_PAGE(stable_node->kpfn != page_to_pfn(oldpage), oldpage);
196962b61f61SHugh Dickins 		stable_node->kpfn = page_to_pfn(newpage);
1970c8d6553bSHugh Dickins 		/*
1971c8d6553bSHugh Dickins 		 * newpage->mapping was set in advance; now we need smp_wmb()
1972c8d6553bSHugh Dickins 		 * to make sure that the new stable_node->kpfn is visible
1973c8d6553bSHugh Dickins 		 * to get_ksm_page() before it can see that oldpage->mapping
1974c8d6553bSHugh Dickins 		 * has gone stale (or that PageSwapCache has been cleared).
1975c8d6553bSHugh Dickins 		 */
1976c8d6553bSHugh Dickins 		smp_wmb();
1977c8d6553bSHugh Dickins 		set_page_stable_node(oldpage, NULL);
1978e9995ef9SHugh Dickins 	}
1979e9995ef9SHugh Dickins }
1980e9995ef9SHugh Dickins #endif /* CONFIG_MIGRATION */
1981e9995ef9SHugh Dickins 
198262b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE
1983ef4d43a8SHugh Dickins static void wait_while_offlining(void)
1984ef4d43a8SHugh Dickins {
1985ef4d43a8SHugh Dickins 	while (ksm_run & KSM_RUN_OFFLINE) {
1986ef4d43a8SHugh Dickins 		mutex_unlock(&ksm_thread_mutex);
1987ef4d43a8SHugh Dickins 		wait_on_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE),
198874316201SNeilBrown 			    TASK_UNINTERRUPTIBLE);
1989ef4d43a8SHugh Dickins 		mutex_lock(&ksm_thread_mutex);
1990ef4d43a8SHugh Dickins 	}
1991ef4d43a8SHugh Dickins }
1992ef4d43a8SHugh Dickins 
1993ee0ea59cSHugh Dickins static void ksm_check_stable_tree(unsigned long start_pfn,
199462b61f61SHugh Dickins 				  unsigned long end_pfn)
199562b61f61SHugh Dickins {
199603640418SGeliang Tang 	struct stable_node *stable_node, *next;
199762b61f61SHugh Dickins 	struct rb_node *node;
199890bd6fd3SPetr Holasek 	int nid;
199962b61f61SHugh Dickins 
2000ef53d16cSHugh Dickins 	for (nid = 0; nid < ksm_nr_node_ids; nid++) {
2001ef53d16cSHugh Dickins 		node = rb_first(root_stable_tree + nid);
2002ee0ea59cSHugh Dickins 		while (node) {
200362b61f61SHugh Dickins 			stable_node = rb_entry(node, struct stable_node, node);
200462b61f61SHugh Dickins 			if (stable_node->kpfn >= start_pfn &&
2005ee0ea59cSHugh Dickins 			    stable_node->kpfn < end_pfn) {
2006ee0ea59cSHugh Dickins 				/*
2007ee0ea59cSHugh Dickins 				 * Don't get_ksm_page, page has already gone:
2008ee0ea59cSHugh Dickins 				 * which is why we keep kpfn instead of page*
2009ee0ea59cSHugh Dickins 				 */
2010ee0ea59cSHugh Dickins 				remove_node_from_stable_tree(stable_node);
2011ef53d16cSHugh Dickins 				node = rb_first(root_stable_tree + nid);
2012ee0ea59cSHugh Dickins 			} else
2013ee0ea59cSHugh Dickins 				node = rb_next(node);
2014ee0ea59cSHugh Dickins 			cond_resched();
201562b61f61SHugh Dickins 		}
2016ee0ea59cSHugh Dickins 	}
201703640418SGeliang Tang 	list_for_each_entry_safe(stable_node, next, &migrate_nodes, list) {
20184146d2d6SHugh Dickins 		if (stable_node->kpfn >= start_pfn &&
20194146d2d6SHugh Dickins 		    stable_node->kpfn < end_pfn)
20204146d2d6SHugh Dickins 			remove_node_from_stable_tree(stable_node);
20214146d2d6SHugh Dickins 		cond_resched();
20224146d2d6SHugh Dickins 	}
202362b61f61SHugh Dickins }
202462b61f61SHugh Dickins 
202562b61f61SHugh Dickins static int ksm_memory_callback(struct notifier_block *self,
202662b61f61SHugh Dickins 			       unsigned long action, void *arg)
202762b61f61SHugh Dickins {
202862b61f61SHugh Dickins 	struct memory_notify *mn = arg;
202962b61f61SHugh Dickins 
203062b61f61SHugh Dickins 	switch (action) {
203162b61f61SHugh Dickins 	case MEM_GOING_OFFLINE:
203262b61f61SHugh Dickins 		/*
2033ef4d43a8SHugh Dickins 		 * Prevent ksm_do_scan(), unmerge_and_remove_all_rmap_items()
2034ef4d43a8SHugh Dickins 		 * and remove_all_stable_nodes() while memory is going offline:
2035ef4d43a8SHugh Dickins 		 * it is unsafe for them to touch the stable tree at this time.
2036ef4d43a8SHugh Dickins 		 * But unmerge_ksm_pages(), rmap lookups and other entry points
2037ef4d43a8SHugh Dickins 		 * which do not need the ksm_thread_mutex are all safe.
203862b61f61SHugh Dickins 		 */
2039ef4d43a8SHugh Dickins 		mutex_lock(&ksm_thread_mutex);
2040ef4d43a8SHugh Dickins 		ksm_run |= KSM_RUN_OFFLINE;
2041ef4d43a8SHugh Dickins 		mutex_unlock(&ksm_thread_mutex);
204262b61f61SHugh Dickins 		break;
204362b61f61SHugh Dickins 
204462b61f61SHugh Dickins 	case MEM_OFFLINE:
204562b61f61SHugh Dickins 		/*
204662b61f61SHugh Dickins 		 * Most of the work is done by page migration; but there might
204762b61f61SHugh Dickins 		 * be a few stable_nodes left over, still pointing to struct
2048ee0ea59cSHugh Dickins 		 * pages which have been offlined: prune those from the tree,
2049ee0ea59cSHugh Dickins 		 * otherwise get_ksm_page() might later try to access a
2050ee0ea59cSHugh Dickins 		 * non-existent struct page.
205162b61f61SHugh Dickins 		 */
2052ee0ea59cSHugh Dickins 		ksm_check_stable_tree(mn->start_pfn,
2053ee0ea59cSHugh Dickins 				      mn->start_pfn + mn->nr_pages);
205462b61f61SHugh Dickins 		/* fallthrough */
205562b61f61SHugh Dickins 
205662b61f61SHugh Dickins 	case MEM_CANCEL_OFFLINE:
2057ef4d43a8SHugh Dickins 		mutex_lock(&ksm_thread_mutex);
2058ef4d43a8SHugh Dickins 		ksm_run &= ~KSM_RUN_OFFLINE;
205962b61f61SHugh Dickins 		mutex_unlock(&ksm_thread_mutex);
2060ef4d43a8SHugh Dickins 
2061ef4d43a8SHugh Dickins 		smp_mb();	/* wake_up_bit advises this */
2062ef4d43a8SHugh Dickins 		wake_up_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE));
206362b61f61SHugh Dickins 		break;
206462b61f61SHugh Dickins 	}
206562b61f61SHugh Dickins 	return NOTIFY_OK;
206662b61f61SHugh Dickins }
2067ef4d43a8SHugh Dickins #else
2068ef4d43a8SHugh Dickins static void wait_while_offlining(void)
2069ef4d43a8SHugh Dickins {
2070ef4d43a8SHugh Dickins }
207162b61f61SHugh Dickins #endif /* CONFIG_MEMORY_HOTREMOVE */
207262b61f61SHugh Dickins 
20732ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
20742ffd8679SHugh Dickins /*
20752ffd8679SHugh Dickins  * This all compiles without CONFIG_SYSFS, but is a waste of space.
20762ffd8679SHugh Dickins  */
20772ffd8679SHugh Dickins 
207831dbd01fSIzik Eidus #define KSM_ATTR_RO(_name) \
207931dbd01fSIzik Eidus 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
208031dbd01fSIzik Eidus #define KSM_ATTR(_name) \
208131dbd01fSIzik Eidus 	static struct kobj_attribute _name##_attr = \
208231dbd01fSIzik Eidus 		__ATTR(_name, 0644, _name##_show, _name##_store)
208331dbd01fSIzik Eidus 
208431dbd01fSIzik Eidus static ssize_t sleep_millisecs_show(struct kobject *kobj,
208531dbd01fSIzik Eidus 				    struct kobj_attribute *attr, char *buf)
208631dbd01fSIzik Eidus {
208731dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
208831dbd01fSIzik Eidus }
208931dbd01fSIzik Eidus 
209031dbd01fSIzik Eidus static ssize_t sleep_millisecs_store(struct kobject *kobj,
209131dbd01fSIzik Eidus 				     struct kobj_attribute *attr,
209231dbd01fSIzik Eidus 				     const char *buf, size_t count)
209331dbd01fSIzik Eidus {
209431dbd01fSIzik Eidus 	unsigned long msecs;
209531dbd01fSIzik Eidus 	int err;
209631dbd01fSIzik Eidus 
20973dbb95f7SJingoo Han 	err = kstrtoul(buf, 10, &msecs);
209831dbd01fSIzik Eidus 	if (err || msecs > UINT_MAX)
209931dbd01fSIzik Eidus 		return -EINVAL;
210031dbd01fSIzik Eidus 
210131dbd01fSIzik Eidus 	ksm_thread_sleep_millisecs = msecs;
210231dbd01fSIzik Eidus 
210331dbd01fSIzik Eidus 	return count;
210431dbd01fSIzik Eidus }
210531dbd01fSIzik Eidus KSM_ATTR(sleep_millisecs);
210631dbd01fSIzik Eidus 
210731dbd01fSIzik Eidus static ssize_t pages_to_scan_show(struct kobject *kobj,
210831dbd01fSIzik Eidus 				  struct kobj_attribute *attr, char *buf)
210931dbd01fSIzik Eidus {
211031dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
211131dbd01fSIzik Eidus }
211231dbd01fSIzik Eidus 
211331dbd01fSIzik Eidus static ssize_t pages_to_scan_store(struct kobject *kobj,
211431dbd01fSIzik Eidus 				   struct kobj_attribute *attr,
211531dbd01fSIzik Eidus 				   const char *buf, size_t count)
211631dbd01fSIzik Eidus {
211731dbd01fSIzik Eidus 	int err;
211831dbd01fSIzik Eidus 	unsigned long nr_pages;
211931dbd01fSIzik Eidus 
21203dbb95f7SJingoo Han 	err = kstrtoul(buf, 10, &nr_pages);
212131dbd01fSIzik Eidus 	if (err || nr_pages > UINT_MAX)
212231dbd01fSIzik Eidus 		return -EINVAL;
212331dbd01fSIzik Eidus 
212431dbd01fSIzik Eidus 	ksm_thread_pages_to_scan = nr_pages;
212531dbd01fSIzik Eidus 
212631dbd01fSIzik Eidus 	return count;
212731dbd01fSIzik Eidus }
212831dbd01fSIzik Eidus KSM_ATTR(pages_to_scan);
212931dbd01fSIzik Eidus 
213031dbd01fSIzik Eidus static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
213131dbd01fSIzik Eidus 			char *buf)
213231dbd01fSIzik Eidus {
2133ef4d43a8SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_run);
213431dbd01fSIzik Eidus }
213531dbd01fSIzik Eidus 
213631dbd01fSIzik Eidus static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
213731dbd01fSIzik Eidus 			 const char *buf, size_t count)
213831dbd01fSIzik Eidus {
213931dbd01fSIzik Eidus 	int err;
214031dbd01fSIzik Eidus 	unsigned long flags;
214131dbd01fSIzik Eidus 
21423dbb95f7SJingoo Han 	err = kstrtoul(buf, 10, &flags);
214331dbd01fSIzik Eidus 	if (err || flags > UINT_MAX)
214431dbd01fSIzik Eidus 		return -EINVAL;
214531dbd01fSIzik Eidus 	if (flags > KSM_RUN_UNMERGE)
214631dbd01fSIzik Eidus 		return -EINVAL;
214731dbd01fSIzik Eidus 
214831dbd01fSIzik Eidus 	/*
214931dbd01fSIzik Eidus 	 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
215031dbd01fSIzik Eidus 	 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
2151d0f209f6SHugh Dickins 	 * breaking COW to free the pages_shared (but leaves mm_slots
2152d0f209f6SHugh Dickins 	 * on the list for when ksmd may be set running again).
215331dbd01fSIzik Eidus 	 */
215431dbd01fSIzik Eidus 
215531dbd01fSIzik Eidus 	mutex_lock(&ksm_thread_mutex);
2156ef4d43a8SHugh Dickins 	wait_while_offlining();
215731dbd01fSIzik Eidus 	if (ksm_run != flags) {
215831dbd01fSIzik Eidus 		ksm_run = flags;
2159d952b791SHugh Dickins 		if (flags & KSM_RUN_UNMERGE) {
2160e1e12d2fSDavid Rientjes 			set_current_oom_origin();
2161d952b791SHugh Dickins 			err = unmerge_and_remove_all_rmap_items();
2162e1e12d2fSDavid Rientjes 			clear_current_oom_origin();
2163d952b791SHugh Dickins 			if (err) {
2164d952b791SHugh Dickins 				ksm_run = KSM_RUN_STOP;
2165d952b791SHugh Dickins 				count = err;
2166d952b791SHugh Dickins 			}
2167d952b791SHugh Dickins 		}
216831dbd01fSIzik Eidus 	}
216931dbd01fSIzik Eidus 	mutex_unlock(&ksm_thread_mutex);
217031dbd01fSIzik Eidus 
217131dbd01fSIzik Eidus 	if (flags & KSM_RUN_MERGE)
217231dbd01fSIzik Eidus 		wake_up_interruptible(&ksm_thread_wait);
217331dbd01fSIzik Eidus 
217431dbd01fSIzik Eidus 	return count;
217531dbd01fSIzik Eidus }
217631dbd01fSIzik Eidus KSM_ATTR(run);
217731dbd01fSIzik Eidus 
217890bd6fd3SPetr Holasek #ifdef CONFIG_NUMA
217990bd6fd3SPetr Holasek static ssize_t merge_across_nodes_show(struct kobject *kobj,
218090bd6fd3SPetr Holasek 				struct kobj_attribute *attr, char *buf)
218190bd6fd3SPetr Holasek {
218290bd6fd3SPetr Holasek 	return sprintf(buf, "%u\n", ksm_merge_across_nodes);
218390bd6fd3SPetr Holasek }
218490bd6fd3SPetr Holasek 
218590bd6fd3SPetr Holasek static ssize_t merge_across_nodes_store(struct kobject *kobj,
218690bd6fd3SPetr Holasek 				   struct kobj_attribute *attr,
218790bd6fd3SPetr Holasek 				   const char *buf, size_t count)
218890bd6fd3SPetr Holasek {
218990bd6fd3SPetr Holasek 	int err;
219090bd6fd3SPetr Holasek 	unsigned long knob;
219190bd6fd3SPetr Holasek 
219290bd6fd3SPetr Holasek 	err = kstrtoul(buf, 10, &knob);
219390bd6fd3SPetr Holasek 	if (err)
219490bd6fd3SPetr Holasek 		return err;
219590bd6fd3SPetr Holasek 	if (knob > 1)
219690bd6fd3SPetr Holasek 		return -EINVAL;
219790bd6fd3SPetr Holasek 
219890bd6fd3SPetr Holasek 	mutex_lock(&ksm_thread_mutex);
2199ef4d43a8SHugh Dickins 	wait_while_offlining();
220090bd6fd3SPetr Holasek 	if (ksm_merge_across_nodes != knob) {
2201cbf86cfeSHugh Dickins 		if (ksm_pages_shared || remove_all_stable_nodes())
220290bd6fd3SPetr Holasek 			err = -EBUSY;
2203ef53d16cSHugh Dickins 		else if (root_stable_tree == one_stable_tree) {
2204ef53d16cSHugh Dickins 			struct rb_root *buf;
2205ef53d16cSHugh Dickins 			/*
2206ef53d16cSHugh Dickins 			 * This is the first time that we switch away from the
2207ef53d16cSHugh Dickins 			 * default of merging across nodes: must now allocate
2208ef53d16cSHugh Dickins 			 * a buffer to hold as many roots as may be needed.
2209ef53d16cSHugh Dickins 			 * Allocate stable and unstable together:
2210ef53d16cSHugh Dickins 			 * MAXSMP NODES_SHIFT 10 will use 16kB.
2211ef53d16cSHugh Dickins 			 */
2212bafe1e14SJoe Perches 			buf = kcalloc(nr_node_ids + nr_node_ids, sizeof(*buf),
2213bafe1e14SJoe Perches 				      GFP_KERNEL);
2214ef53d16cSHugh Dickins 			/* Let us assume that RB_ROOT is NULL is zero */
2215ef53d16cSHugh Dickins 			if (!buf)
2216ef53d16cSHugh Dickins 				err = -ENOMEM;
2217ef53d16cSHugh Dickins 			else {
2218ef53d16cSHugh Dickins 				root_stable_tree = buf;
2219ef53d16cSHugh Dickins 				root_unstable_tree = buf + nr_node_ids;
2220ef53d16cSHugh Dickins 				/* Stable tree is empty but not the unstable */
2221ef53d16cSHugh Dickins 				root_unstable_tree[0] = one_unstable_tree[0];
2222ef53d16cSHugh Dickins 			}
2223ef53d16cSHugh Dickins 		}
2224ef53d16cSHugh Dickins 		if (!err) {
222590bd6fd3SPetr Holasek 			ksm_merge_across_nodes = knob;
2226ef53d16cSHugh Dickins 			ksm_nr_node_ids = knob ? 1 : nr_node_ids;
2227ef53d16cSHugh Dickins 		}
222890bd6fd3SPetr Holasek 	}
222990bd6fd3SPetr Holasek 	mutex_unlock(&ksm_thread_mutex);
223090bd6fd3SPetr Holasek 
223190bd6fd3SPetr Holasek 	return err ? err : count;
223290bd6fd3SPetr Holasek }
223390bd6fd3SPetr Holasek KSM_ATTR(merge_across_nodes);
223490bd6fd3SPetr Holasek #endif
223590bd6fd3SPetr Holasek 
2236b4028260SHugh Dickins static ssize_t pages_shared_show(struct kobject *kobj,
2237b4028260SHugh Dickins 				 struct kobj_attribute *attr, char *buf)
2238b4028260SHugh Dickins {
2239b4028260SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_shared);
2240b4028260SHugh Dickins }
2241b4028260SHugh Dickins KSM_ATTR_RO(pages_shared);
2242b4028260SHugh Dickins 
2243b4028260SHugh Dickins static ssize_t pages_sharing_show(struct kobject *kobj,
2244b4028260SHugh Dickins 				  struct kobj_attribute *attr, char *buf)
2245b4028260SHugh Dickins {
2246e178dfdeSHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_sharing);
2247b4028260SHugh Dickins }
2248b4028260SHugh Dickins KSM_ATTR_RO(pages_sharing);
2249b4028260SHugh Dickins 
2250473b0ce4SHugh Dickins static ssize_t pages_unshared_show(struct kobject *kobj,
2251473b0ce4SHugh Dickins 				   struct kobj_attribute *attr, char *buf)
2252473b0ce4SHugh Dickins {
2253473b0ce4SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_unshared);
2254473b0ce4SHugh Dickins }
2255473b0ce4SHugh Dickins KSM_ATTR_RO(pages_unshared);
2256473b0ce4SHugh Dickins 
2257473b0ce4SHugh Dickins static ssize_t pages_volatile_show(struct kobject *kobj,
2258473b0ce4SHugh Dickins 				   struct kobj_attribute *attr, char *buf)
2259473b0ce4SHugh Dickins {
2260473b0ce4SHugh Dickins 	long ksm_pages_volatile;
2261473b0ce4SHugh Dickins 
2262473b0ce4SHugh Dickins 	ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
2263473b0ce4SHugh Dickins 				- ksm_pages_sharing - ksm_pages_unshared;
2264473b0ce4SHugh Dickins 	/*
2265473b0ce4SHugh Dickins 	 * It was not worth any locking to calculate that statistic,
2266473b0ce4SHugh Dickins 	 * but it might therefore sometimes be negative: conceal that.
2267473b0ce4SHugh Dickins 	 */
2268473b0ce4SHugh Dickins 	if (ksm_pages_volatile < 0)
2269473b0ce4SHugh Dickins 		ksm_pages_volatile = 0;
2270473b0ce4SHugh Dickins 	return sprintf(buf, "%ld\n", ksm_pages_volatile);
2271473b0ce4SHugh Dickins }
2272473b0ce4SHugh Dickins KSM_ATTR_RO(pages_volatile);
2273473b0ce4SHugh Dickins 
2274473b0ce4SHugh Dickins static ssize_t full_scans_show(struct kobject *kobj,
2275473b0ce4SHugh Dickins 			       struct kobj_attribute *attr, char *buf)
2276473b0ce4SHugh Dickins {
2277473b0ce4SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_scan.seqnr);
2278473b0ce4SHugh Dickins }
2279473b0ce4SHugh Dickins KSM_ATTR_RO(full_scans);
2280473b0ce4SHugh Dickins 
228131dbd01fSIzik Eidus static struct attribute *ksm_attrs[] = {
228231dbd01fSIzik Eidus 	&sleep_millisecs_attr.attr,
228331dbd01fSIzik Eidus 	&pages_to_scan_attr.attr,
228431dbd01fSIzik Eidus 	&run_attr.attr,
2285b4028260SHugh Dickins 	&pages_shared_attr.attr,
2286b4028260SHugh Dickins 	&pages_sharing_attr.attr,
2287473b0ce4SHugh Dickins 	&pages_unshared_attr.attr,
2288473b0ce4SHugh Dickins 	&pages_volatile_attr.attr,
2289473b0ce4SHugh Dickins 	&full_scans_attr.attr,
229090bd6fd3SPetr Holasek #ifdef CONFIG_NUMA
229190bd6fd3SPetr Holasek 	&merge_across_nodes_attr.attr,
229290bd6fd3SPetr Holasek #endif
229331dbd01fSIzik Eidus 	NULL,
229431dbd01fSIzik Eidus };
229531dbd01fSIzik Eidus 
229631dbd01fSIzik Eidus static struct attribute_group ksm_attr_group = {
229731dbd01fSIzik Eidus 	.attrs = ksm_attrs,
229831dbd01fSIzik Eidus 	.name = "ksm",
229931dbd01fSIzik Eidus };
23002ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
230131dbd01fSIzik Eidus 
230231dbd01fSIzik Eidus static int __init ksm_init(void)
230331dbd01fSIzik Eidus {
230431dbd01fSIzik Eidus 	struct task_struct *ksm_thread;
230531dbd01fSIzik Eidus 	int err;
230631dbd01fSIzik Eidus 
230731dbd01fSIzik Eidus 	err = ksm_slab_init();
230831dbd01fSIzik Eidus 	if (err)
230931dbd01fSIzik Eidus 		goto out;
231031dbd01fSIzik Eidus 
231131dbd01fSIzik Eidus 	ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
231231dbd01fSIzik Eidus 	if (IS_ERR(ksm_thread)) {
231325acde31SPaul McQuade 		pr_err("ksm: creating kthread failed\n");
231431dbd01fSIzik Eidus 		err = PTR_ERR(ksm_thread);
2315d9f8984cSLai Jiangshan 		goto out_free;
231631dbd01fSIzik Eidus 	}
231731dbd01fSIzik Eidus 
23182ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
231931dbd01fSIzik Eidus 	err = sysfs_create_group(mm_kobj, &ksm_attr_group);
232031dbd01fSIzik Eidus 	if (err) {
232125acde31SPaul McQuade 		pr_err("ksm: register sysfs failed\n");
23222ffd8679SHugh Dickins 		kthread_stop(ksm_thread);
2323d9f8984cSLai Jiangshan 		goto out_free;
232431dbd01fSIzik Eidus 	}
2325c73602adSHugh Dickins #else
2326c73602adSHugh Dickins 	ksm_run = KSM_RUN_MERGE;	/* no way for user to start it */
2327c73602adSHugh Dickins 
23282ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
232931dbd01fSIzik Eidus 
233062b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE
2331ef4d43a8SHugh Dickins 	/* There is no significance to this priority 100 */
233262b61f61SHugh Dickins 	hotplug_memory_notifier(ksm_memory_callback, 100);
233362b61f61SHugh Dickins #endif
233431dbd01fSIzik Eidus 	return 0;
233531dbd01fSIzik Eidus 
2336d9f8984cSLai Jiangshan out_free:
233731dbd01fSIzik Eidus 	ksm_slab_free();
233831dbd01fSIzik Eidus out:
233931dbd01fSIzik Eidus 	return err;
234031dbd01fSIzik Eidus }
2341a64fb3cdSPaul Gortmaker subsys_initcall(ksm_init);
2342