xref: /linux/mm/ksm.c (revision 4ca3a69bcb6875c3f20802522c1b4fc56bb14608)
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>
36*4ca3a69bSSasha Levin #include <linux/hashtable.h>
37878aee7dSAndrea Arcangeli #include <linux/freezer.h>
3872788c38SDavid Rientjes #include <linux/oom.h>
39f8af4da3SHugh Dickins 
4031dbd01fSIzik Eidus #include <asm/tlbflush.h>
4173848b46SHugh Dickins #include "internal.h"
4231dbd01fSIzik Eidus 
4331dbd01fSIzik Eidus /*
4431dbd01fSIzik Eidus  * A few notes about the KSM scanning process,
4531dbd01fSIzik Eidus  * to make it easier to understand the data structures below:
4631dbd01fSIzik Eidus  *
4731dbd01fSIzik Eidus  * In order to reduce excessive scanning, KSM sorts the memory pages by their
4831dbd01fSIzik Eidus  * contents into a data structure that holds pointers to the pages' locations.
4931dbd01fSIzik Eidus  *
5031dbd01fSIzik Eidus  * Since the contents of the pages may change at any moment, KSM cannot just
5131dbd01fSIzik Eidus  * insert the pages into a normal sorted tree and expect it to find anything.
5231dbd01fSIzik Eidus  * Therefore KSM uses two data structures - the stable and the unstable tree.
5331dbd01fSIzik Eidus  *
5431dbd01fSIzik Eidus  * The stable tree holds pointers to all the merged pages (ksm pages), sorted
5531dbd01fSIzik Eidus  * by their contents.  Because each such page is write-protected, searching on
5631dbd01fSIzik Eidus  * this tree is fully assured to be working (except when pages are unmapped),
5731dbd01fSIzik Eidus  * and therefore this tree is called the stable tree.
5831dbd01fSIzik Eidus  *
5931dbd01fSIzik Eidus  * In addition to the stable tree, KSM uses a second data structure called the
6031dbd01fSIzik Eidus  * unstable tree: this tree holds pointers to pages which have been found to
6131dbd01fSIzik Eidus  * be "unchanged for a period of time".  The unstable tree sorts these pages
6231dbd01fSIzik Eidus  * by their contents, but since they are not write-protected, KSM cannot rely
6331dbd01fSIzik Eidus  * upon the unstable tree to work correctly - the unstable tree is liable to
6431dbd01fSIzik Eidus  * be corrupted as its contents are modified, and so it is called unstable.
6531dbd01fSIzik Eidus  *
6631dbd01fSIzik Eidus  * KSM solves this problem by several techniques:
6731dbd01fSIzik Eidus  *
6831dbd01fSIzik Eidus  * 1) The unstable tree is flushed every time KSM completes scanning all
6931dbd01fSIzik Eidus  *    memory areas, and then the tree is rebuilt again from the beginning.
7031dbd01fSIzik Eidus  * 2) KSM will only insert into the unstable tree, pages whose hash value
7131dbd01fSIzik Eidus  *    has not changed since the previous scan of all memory areas.
7231dbd01fSIzik Eidus  * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
7331dbd01fSIzik Eidus  *    colors of the nodes and not on their contents, assuring that even when
7431dbd01fSIzik Eidus  *    the tree gets "corrupted" it won't get out of balance, so scanning time
7531dbd01fSIzik Eidus  *    remains the same (also, searching and inserting nodes in an rbtree uses
7631dbd01fSIzik Eidus  *    the same algorithm, so we have no overhead when we flush and rebuild).
7731dbd01fSIzik Eidus  * 4) KSM never flushes the stable tree, which means that even if it were to
7831dbd01fSIzik Eidus  *    take 10 attempts to find a page in the unstable tree, once it is found,
7931dbd01fSIzik Eidus  *    it is secured in the stable tree.  (When we scan a new page, we first
8031dbd01fSIzik Eidus  *    compare it against the stable tree, and then against the unstable tree.)
8131dbd01fSIzik Eidus  */
8231dbd01fSIzik Eidus 
8331dbd01fSIzik Eidus /**
8431dbd01fSIzik Eidus  * struct mm_slot - ksm information per mm that is being scanned
8531dbd01fSIzik Eidus  * @link: link to the mm_slots hash list
8631dbd01fSIzik Eidus  * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
876514d511SHugh Dickins  * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
8831dbd01fSIzik Eidus  * @mm: the mm that this information is valid for
8931dbd01fSIzik Eidus  */
9031dbd01fSIzik Eidus struct mm_slot {
9131dbd01fSIzik Eidus 	struct hlist_node link;
9231dbd01fSIzik Eidus 	struct list_head mm_list;
936514d511SHugh Dickins 	struct rmap_item *rmap_list;
9431dbd01fSIzik Eidus 	struct mm_struct *mm;
9531dbd01fSIzik Eidus };
9631dbd01fSIzik Eidus 
9731dbd01fSIzik Eidus /**
9831dbd01fSIzik Eidus  * struct ksm_scan - cursor for scanning
9931dbd01fSIzik Eidus  * @mm_slot: the current mm_slot we are scanning
10031dbd01fSIzik Eidus  * @address: the next address inside that to be scanned
1016514d511SHugh Dickins  * @rmap_list: link to the next rmap to be scanned in the rmap_list
10231dbd01fSIzik Eidus  * @seqnr: count of completed full scans (needed when removing unstable node)
10331dbd01fSIzik Eidus  *
10431dbd01fSIzik Eidus  * There is only the one ksm_scan instance of this cursor structure.
10531dbd01fSIzik Eidus  */
10631dbd01fSIzik Eidus struct ksm_scan {
10731dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
10831dbd01fSIzik Eidus 	unsigned long address;
1096514d511SHugh Dickins 	struct rmap_item **rmap_list;
11031dbd01fSIzik Eidus 	unsigned long seqnr;
11131dbd01fSIzik Eidus };
11231dbd01fSIzik Eidus 
11331dbd01fSIzik Eidus /**
1147b6ba2c7SHugh Dickins  * struct stable_node - node of the stable rbtree
1157b6ba2c7SHugh Dickins  * @node: rb node of this ksm page in the stable tree
1167b6ba2c7SHugh Dickins  * @hlist: hlist head of rmap_items using this ksm page
11762b61f61SHugh Dickins  * @kpfn: page frame number of this ksm page
1187b6ba2c7SHugh Dickins  */
1197b6ba2c7SHugh Dickins struct stable_node {
1207b6ba2c7SHugh Dickins 	struct rb_node node;
1217b6ba2c7SHugh Dickins 	struct hlist_head hlist;
12262b61f61SHugh Dickins 	unsigned long kpfn;
1237b6ba2c7SHugh Dickins };
1247b6ba2c7SHugh Dickins 
1257b6ba2c7SHugh Dickins /**
12631dbd01fSIzik Eidus  * struct rmap_item - reverse mapping item for virtual addresses
1276514d511SHugh Dickins  * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
128db114b83SHugh Dickins  * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
12931dbd01fSIzik Eidus  * @mm: the memory structure this rmap_item is pointing into
13031dbd01fSIzik Eidus  * @address: the virtual address this rmap_item tracks (+ flags in low bits)
13131dbd01fSIzik Eidus  * @oldchecksum: previous checksum of the page at that virtual address
1327b6ba2c7SHugh Dickins  * @node: rb node of this rmap_item in the unstable tree
1337b6ba2c7SHugh Dickins  * @head: pointer to stable_node heading this list in the stable tree
1347b6ba2c7SHugh Dickins  * @hlist: link into hlist of rmap_items hanging off that stable_node
13531dbd01fSIzik Eidus  */
13631dbd01fSIzik Eidus struct rmap_item {
1376514d511SHugh Dickins 	struct rmap_item *rmap_list;
138db114b83SHugh Dickins 	struct anon_vma *anon_vma;	/* when stable */
13931dbd01fSIzik Eidus 	struct mm_struct *mm;
14031dbd01fSIzik Eidus 	unsigned long address;		/* + low bits used for flags below */
14131dbd01fSIzik Eidus 	unsigned int oldchecksum;	/* when unstable */
14231dbd01fSIzik Eidus 	union {
1437b6ba2c7SHugh Dickins 		struct rb_node node;	/* when node of unstable tree */
1447b6ba2c7SHugh Dickins 		struct {		/* when listed from stable tree */
1457b6ba2c7SHugh Dickins 			struct stable_node *head;
1467b6ba2c7SHugh Dickins 			struct hlist_node hlist;
1477b6ba2c7SHugh Dickins 		};
14831dbd01fSIzik Eidus 	};
14931dbd01fSIzik Eidus };
15031dbd01fSIzik Eidus 
15131dbd01fSIzik Eidus #define SEQNR_MASK	0x0ff	/* low bits of unstable tree seqnr */
1527b6ba2c7SHugh Dickins #define UNSTABLE_FLAG	0x100	/* is a node of the unstable tree */
1537b6ba2c7SHugh Dickins #define STABLE_FLAG	0x200	/* is listed from the stable tree */
15431dbd01fSIzik Eidus 
15531dbd01fSIzik Eidus /* The stable and unstable tree heads */
15631dbd01fSIzik Eidus static struct rb_root root_stable_tree = RB_ROOT;
15731dbd01fSIzik Eidus static struct rb_root root_unstable_tree = RB_ROOT;
15831dbd01fSIzik Eidus 
159*4ca3a69bSSasha Levin #define MM_SLOTS_HASH_BITS 10
160*4ca3a69bSSasha Levin static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
16131dbd01fSIzik Eidus 
16231dbd01fSIzik Eidus static struct mm_slot ksm_mm_head = {
16331dbd01fSIzik Eidus 	.mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
16431dbd01fSIzik Eidus };
16531dbd01fSIzik Eidus static struct ksm_scan ksm_scan = {
16631dbd01fSIzik Eidus 	.mm_slot = &ksm_mm_head,
16731dbd01fSIzik Eidus };
16831dbd01fSIzik Eidus 
16931dbd01fSIzik Eidus static struct kmem_cache *rmap_item_cache;
1707b6ba2c7SHugh Dickins static struct kmem_cache *stable_node_cache;
17131dbd01fSIzik Eidus static struct kmem_cache *mm_slot_cache;
17231dbd01fSIzik Eidus 
17331dbd01fSIzik Eidus /* The number of nodes in the stable tree */
174b4028260SHugh Dickins static unsigned long ksm_pages_shared;
17531dbd01fSIzik Eidus 
176e178dfdeSHugh Dickins /* The number of page slots additionally sharing those nodes */
177b4028260SHugh Dickins static unsigned long ksm_pages_sharing;
17831dbd01fSIzik Eidus 
179473b0ce4SHugh Dickins /* The number of nodes in the unstable tree */
180473b0ce4SHugh Dickins static unsigned long ksm_pages_unshared;
181473b0ce4SHugh Dickins 
182473b0ce4SHugh Dickins /* The number of rmap_items in use: to calculate pages_volatile */
183473b0ce4SHugh Dickins static unsigned long ksm_rmap_items;
184473b0ce4SHugh Dickins 
18531dbd01fSIzik Eidus /* Number of pages ksmd should scan in one batch */
1862c6854fdSIzik Eidus static unsigned int ksm_thread_pages_to_scan = 100;
18731dbd01fSIzik Eidus 
18831dbd01fSIzik Eidus /* Milliseconds ksmd should sleep between batches */
1892ffd8679SHugh Dickins static unsigned int ksm_thread_sleep_millisecs = 20;
19031dbd01fSIzik Eidus 
19131dbd01fSIzik Eidus #define KSM_RUN_STOP	0
19231dbd01fSIzik Eidus #define KSM_RUN_MERGE	1
19331dbd01fSIzik Eidus #define KSM_RUN_UNMERGE	2
1942c6854fdSIzik Eidus static unsigned int ksm_run = KSM_RUN_STOP;
19531dbd01fSIzik Eidus 
19631dbd01fSIzik Eidus static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
19731dbd01fSIzik Eidus static DEFINE_MUTEX(ksm_thread_mutex);
19831dbd01fSIzik Eidus static DEFINE_SPINLOCK(ksm_mmlist_lock);
19931dbd01fSIzik Eidus 
20031dbd01fSIzik Eidus #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
20131dbd01fSIzik Eidus 		sizeof(struct __struct), __alignof__(struct __struct),\
20231dbd01fSIzik Eidus 		(__flags), NULL)
20331dbd01fSIzik Eidus 
20431dbd01fSIzik Eidus static int __init ksm_slab_init(void)
20531dbd01fSIzik Eidus {
20631dbd01fSIzik Eidus 	rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
20731dbd01fSIzik Eidus 	if (!rmap_item_cache)
20831dbd01fSIzik Eidus 		goto out;
20931dbd01fSIzik Eidus 
2107b6ba2c7SHugh Dickins 	stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
2117b6ba2c7SHugh Dickins 	if (!stable_node_cache)
2127b6ba2c7SHugh Dickins 		goto out_free1;
2137b6ba2c7SHugh Dickins 
21431dbd01fSIzik Eidus 	mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
21531dbd01fSIzik Eidus 	if (!mm_slot_cache)
2167b6ba2c7SHugh Dickins 		goto out_free2;
21731dbd01fSIzik Eidus 
21831dbd01fSIzik Eidus 	return 0;
21931dbd01fSIzik Eidus 
2207b6ba2c7SHugh Dickins out_free2:
2217b6ba2c7SHugh Dickins 	kmem_cache_destroy(stable_node_cache);
2227b6ba2c7SHugh Dickins out_free1:
22331dbd01fSIzik Eidus 	kmem_cache_destroy(rmap_item_cache);
22431dbd01fSIzik Eidus out:
22531dbd01fSIzik Eidus 	return -ENOMEM;
22631dbd01fSIzik Eidus }
22731dbd01fSIzik Eidus 
22831dbd01fSIzik Eidus static void __init ksm_slab_free(void)
22931dbd01fSIzik Eidus {
23031dbd01fSIzik Eidus 	kmem_cache_destroy(mm_slot_cache);
2317b6ba2c7SHugh Dickins 	kmem_cache_destroy(stable_node_cache);
23231dbd01fSIzik Eidus 	kmem_cache_destroy(rmap_item_cache);
23331dbd01fSIzik Eidus 	mm_slot_cache = NULL;
23431dbd01fSIzik Eidus }
23531dbd01fSIzik Eidus 
23631dbd01fSIzik Eidus static inline struct rmap_item *alloc_rmap_item(void)
23731dbd01fSIzik Eidus {
238473b0ce4SHugh Dickins 	struct rmap_item *rmap_item;
239473b0ce4SHugh Dickins 
240473b0ce4SHugh Dickins 	rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL);
241473b0ce4SHugh Dickins 	if (rmap_item)
242473b0ce4SHugh Dickins 		ksm_rmap_items++;
243473b0ce4SHugh Dickins 	return rmap_item;
24431dbd01fSIzik Eidus }
24531dbd01fSIzik Eidus 
24631dbd01fSIzik Eidus static inline void free_rmap_item(struct rmap_item *rmap_item)
24731dbd01fSIzik Eidus {
248473b0ce4SHugh Dickins 	ksm_rmap_items--;
24931dbd01fSIzik Eidus 	rmap_item->mm = NULL;	/* debug safety */
25031dbd01fSIzik Eidus 	kmem_cache_free(rmap_item_cache, rmap_item);
25131dbd01fSIzik Eidus }
25231dbd01fSIzik Eidus 
2537b6ba2c7SHugh Dickins static inline struct stable_node *alloc_stable_node(void)
2547b6ba2c7SHugh Dickins {
2557b6ba2c7SHugh Dickins 	return kmem_cache_alloc(stable_node_cache, GFP_KERNEL);
2567b6ba2c7SHugh Dickins }
2577b6ba2c7SHugh Dickins 
2587b6ba2c7SHugh Dickins static inline void free_stable_node(struct stable_node *stable_node)
2597b6ba2c7SHugh Dickins {
2607b6ba2c7SHugh Dickins 	kmem_cache_free(stable_node_cache, stable_node);
2617b6ba2c7SHugh Dickins }
2627b6ba2c7SHugh Dickins 
26331dbd01fSIzik Eidus static inline struct mm_slot *alloc_mm_slot(void)
26431dbd01fSIzik Eidus {
26531dbd01fSIzik Eidus 	if (!mm_slot_cache)	/* initialization failed */
26631dbd01fSIzik Eidus 		return NULL;
26731dbd01fSIzik Eidus 	return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
26831dbd01fSIzik Eidus }
26931dbd01fSIzik Eidus 
27031dbd01fSIzik Eidus static inline void free_mm_slot(struct mm_slot *mm_slot)
27131dbd01fSIzik Eidus {
27231dbd01fSIzik Eidus 	kmem_cache_free(mm_slot_cache, mm_slot);
27331dbd01fSIzik Eidus }
27431dbd01fSIzik Eidus 
27531dbd01fSIzik Eidus static struct mm_slot *get_mm_slot(struct mm_struct *mm)
27631dbd01fSIzik Eidus {
27731dbd01fSIzik Eidus 	struct hlist_node *node;
278*4ca3a69bSSasha Levin 	struct mm_slot *slot;
27931dbd01fSIzik Eidus 
280*4ca3a69bSSasha Levin 	hash_for_each_possible(mm_slots_hash, slot, node, link, (unsigned long)mm)
281*4ca3a69bSSasha Levin 		if (slot->mm == mm)
282*4ca3a69bSSasha Levin 			return slot;
283*4ca3a69bSSasha Levin 
28431dbd01fSIzik Eidus 	return NULL;
28531dbd01fSIzik Eidus }
28631dbd01fSIzik Eidus 
28731dbd01fSIzik Eidus static void insert_to_mm_slots_hash(struct mm_struct *mm,
28831dbd01fSIzik Eidus 				    struct mm_slot *mm_slot)
28931dbd01fSIzik Eidus {
29031dbd01fSIzik Eidus 	mm_slot->mm = mm;
291*4ca3a69bSSasha Levin 	hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm);
29231dbd01fSIzik Eidus }
29331dbd01fSIzik Eidus 
29431dbd01fSIzik Eidus static inline int in_stable_tree(struct rmap_item *rmap_item)
29531dbd01fSIzik Eidus {
29631dbd01fSIzik Eidus 	return rmap_item->address & STABLE_FLAG;
29731dbd01fSIzik Eidus }
29831dbd01fSIzik Eidus 
29931dbd01fSIzik Eidus /*
300a913e182SHugh Dickins  * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
301a913e182SHugh Dickins  * page tables after it has passed through ksm_exit() - which, if necessary,
302a913e182SHugh Dickins  * takes mmap_sem briefly to serialize against them.  ksm_exit() does not set
303a913e182SHugh Dickins  * a special flag: they can just back out as soon as mm_users goes to zero.
304a913e182SHugh Dickins  * ksm_test_exit() is used throughout to make this test for exit: in some
305a913e182SHugh Dickins  * places for correctness, in some places just to avoid unnecessary work.
306a913e182SHugh Dickins  */
307a913e182SHugh Dickins static inline bool ksm_test_exit(struct mm_struct *mm)
308a913e182SHugh Dickins {
309a913e182SHugh Dickins 	return atomic_read(&mm->mm_users) == 0;
310a913e182SHugh Dickins }
311a913e182SHugh Dickins 
312a913e182SHugh Dickins /*
31331dbd01fSIzik Eidus  * We use break_ksm to break COW on a ksm page: it's a stripped down
31431dbd01fSIzik Eidus  *
31531dbd01fSIzik Eidus  *	if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
31631dbd01fSIzik Eidus  *		put_page(page);
31731dbd01fSIzik Eidus  *
31831dbd01fSIzik Eidus  * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
31931dbd01fSIzik Eidus  * in case the application has unmapped and remapped mm,addr meanwhile.
32031dbd01fSIzik Eidus  * Could a ksm page appear anywhere else?  Actually yes, in a VM_PFNMAP
32131dbd01fSIzik Eidus  * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
32231dbd01fSIzik Eidus  */
323d952b791SHugh Dickins static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
32431dbd01fSIzik Eidus {
32531dbd01fSIzik Eidus 	struct page *page;
326d952b791SHugh Dickins 	int ret = 0;
32731dbd01fSIzik Eidus 
32831dbd01fSIzik Eidus 	do {
32931dbd01fSIzik Eidus 		cond_resched();
33031dbd01fSIzik Eidus 		page = follow_page(vma, addr, FOLL_GET);
33122eccdd7SDan Carpenter 		if (IS_ERR_OR_NULL(page))
33231dbd01fSIzik Eidus 			break;
33331dbd01fSIzik Eidus 		if (PageKsm(page))
33431dbd01fSIzik Eidus 			ret = handle_mm_fault(vma->vm_mm, vma, addr,
33531dbd01fSIzik Eidus 							FAULT_FLAG_WRITE);
33631dbd01fSIzik Eidus 		else
33731dbd01fSIzik Eidus 			ret = VM_FAULT_WRITE;
33831dbd01fSIzik Eidus 		put_page(page);
339d952b791SHugh Dickins 	} while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM)));
340d952b791SHugh Dickins 	/*
341d952b791SHugh Dickins 	 * We must loop because handle_mm_fault() may back out if there's
342d952b791SHugh Dickins 	 * any difficulty e.g. if pte accessed bit gets updated concurrently.
343d952b791SHugh Dickins 	 *
344d952b791SHugh Dickins 	 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
345d952b791SHugh Dickins 	 * COW has been broken, even if the vma does not permit VM_WRITE;
346d952b791SHugh Dickins 	 * but note that a concurrent fault might break PageKsm for us.
347d952b791SHugh Dickins 	 *
348d952b791SHugh Dickins 	 * VM_FAULT_SIGBUS could occur if we race with truncation of the
349d952b791SHugh Dickins 	 * backing file, which also invalidates anonymous pages: that's
350d952b791SHugh Dickins 	 * okay, that truncation will have unmapped the PageKsm for us.
351d952b791SHugh Dickins 	 *
352d952b791SHugh Dickins 	 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
353d952b791SHugh Dickins 	 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
354d952b791SHugh Dickins 	 * current task has TIF_MEMDIE set, and will be OOM killed on return
355d952b791SHugh Dickins 	 * to user; and ksmd, having no mm, would never be chosen for that.
356d952b791SHugh Dickins 	 *
357d952b791SHugh Dickins 	 * But if the mm is in a limited mem_cgroup, then the fault may fail
358d952b791SHugh Dickins 	 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
359d952b791SHugh Dickins 	 * even ksmd can fail in this way - though it's usually breaking ksm
360d952b791SHugh Dickins 	 * just to undo a merge it made a moment before, so unlikely to oom.
361d952b791SHugh Dickins 	 *
362d952b791SHugh Dickins 	 * That's a pity: we might therefore have more kernel pages allocated
363d952b791SHugh Dickins 	 * than we're counting as nodes in the stable tree; but ksm_do_scan
364d952b791SHugh Dickins 	 * will retry to break_cow on each pass, so should recover the page
365d952b791SHugh Dickins 	 * in due course.  The important thing is to not let VM_MERGEABLE
366d952b791SHugh Dickins 	 * be cleared while any such pages might remain in the area.
367d952b791SHugh Dickins 	 */
368d952b791SHugh Dickins 	return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
36931dbd01fSIzik Eidus }
37031dbd01fSIzik Eidus 
371ef694222SBob Liu static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm,
372ef694222SBob Liu 		unsigned long addr)
373ef694222SBob Liu {
374ef694222SBob Liu 	struct vm_area_struct *vma;
375ef694222SBob Liu 	if (ksm_test_exit(mm))
376ef694222SBob Liu 		return NULL;
377ef694222SBob Liu 	vma = find_vma(mm, addr);
378ef694222SBob Liu 	if (!vma || vma->vm_start > addr)
379ef694222SBob Liu 		return NULL;
380ef694222SBob Liu 	if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
381ef694222SBob Liu 		return NULL;
382ef694222SBob Liu 	return vma;
383ef694222SBob Liu }
384ef694222SBob Liu 
3858dd3557aSHugh Dickins static void break_cow(struct rmap_item *rmap_item)
38631dbd01fSIzik Eidus {
3878dd3557aSHugh Dickins 	struct mm_struct *mm = rmap_item->mm;
3888dd3557aSHugh Dickins 	unsigned long addr = rmap_item->address;
38931dbd01fSIzik Eidus 	struct vm_area_struct *vma;
39031dbd01fSIzik Eidus 
3914035c07aSHugh Dickins 	/*
3924035c07aSHugh Dickins 	 * It is not an accident that whenever we want to break COW
3934035c07aSHugh Dickins 	 * to undo, we also need to drop a reference to the anon_vma.
3944035c07aSHugh Dickins 	 */
3959e60109fSPeter Zijlstra 	put_anon_vma(rmap_item->anon_vma);
3964035c07aSHugh Dickins 
39781464e30SHugh Dickins 	down_read(&mm->mmap_sem);
398ef694222SBob Liu 	vma = find_mergeable_vma(mm, addr);
399ef694222SBob Liu 	if (vma)
40031dbd01fSIzik Eidus 		break_ksm(vma, addr);
40131dbd01fSIzik Eidus 	up_read(&mm->mmap_sem);
40231dbd01fSIzik Eidus }
40331dbd01fSIzik Eidus 
40429ad768cSAndrea Arcangeli static struct page *page_trans_compound_anon(struct page *page)
40529ad768cSAndrea Arcangeli {
40629ad768cSAndrea Arcangeli 	if (PageTransCompound(page)) {
40722e5c47eSAndrea Arcangeli 		struct page *head = compound_trans_head(page);
40829ad768cSAndrea Arcangeli 		/*
40922e5c47eSAndrea Arcangeli 		 * head may actually be splitted and freed from under
41022e5c47eSAndrea Arcangeli 		 * us but it's ok here.
41129ad768cSAndrea Arcangeli 		 */
41229ad768cSAndrea Arcangeli 		if (PageAnon(head))
41329ad768cSAndrea Arcangeli 			return head;
41429ad768cSAndrea Arcangeli 	}
41529ad768cSAndrea Arcangeli 	return NULL;
41629ad768cSAndrea Arcangeli }
41729ad768cSAndrea Arcangeli 
41831dbd01fSIzik Eidus static struct page *get_mergeable_page(struct rmap_item *rmap_item)
41931dbd01fSIzik Eidus {
42031dbd01fSIzik Eidus 	struct mm_struct *mm = rmap_item->mm;
42131dbd01fSIzik Eidus 	unsigned long addr = rmap_item->address;
42231dbd01fSIzik Eidus 	struct vm_area_struct *vma;
42331dbd01fSIzik Eidus 	struct page *page;
42431dbd01fSIzik Eidus 
42531dbd01fSIzik Eidus 	down_read(&mm->mmap_sem);
426ef694222SBob Liu 	vma = find_mergeable_vma(mm, addr);
427ef694222SBob Liu 	if (!vma)
42831dbd01fSIzik Eidus 		goto out;
42931dbd01fSIzik Eidus 
43031dbd01fSIzik Eidus 	page = follow_page(vma, addr, FOLL_GET);
43122eccdd7SDan Carpenter 	if (IS_ERR_OR_NULL(page))
43231dbd01fSIzik Eidus 		goto out;
43329ad768cSAndrea Arcangeli 	if (PageAnon(page) || page_trans_compound_anon(page)) {
43431dbd01fSIzik Eidus 		flush_anon_page(vma, page, addr);
43531dbd01fSIzik Eidus 		flush_dcache_page(page);
43631dbd01fSIzik Eidus 	} else {
43731dbd01fSIzik Eidus 		put_page(page);
43831dbd01fSIzik Eidus out:		page = NULL;
43931dbd01fSIzik Eidus 	}
44031dbd01fSIzik Eidus 	up_read(&mm->mmap_sem);
44131dbd01fSIzik Eidus 	return page;
44231dbd01fSIzik Eidus }
44331dbd01fSIzik Eidus 
4444035c07aSHugh Dickins static void remove_node_from_stable_tree(struct stable_node *stable_node)
4454035c07aSHugh Dickins {
4464035c07aSHugh Dickins 	struct rmap_item *rmap_item;
4474035c07aSHugh Dickins 	struct hlist_node *hlist;
4484035c07aSHugh Dickins 
4494035c07aSHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
4504035c07aSHugh Dickins 		if (rmap_item->hlist.next)
4514035c07aSHugh Dickins 			ksm_pages_sharing--;
4524035c07aSHugh Dickins 		else
4534035c07aSHugh Dickins 			ksm_pages_shared--;
4549e60109fSPeter Zijlstra 		put_anon_vma(rmap_item->anon_vma);
4554035c07aSHugh Dickins 		rmap_item->address &= PAGE_MASK;
4564035c07aSHugh Dickins 		cond_resched();
4574035c07aSHugh Dickins 	}
4584035c07aSHugh Dickins 
4594035c07aSHugh Dickins 	rb_erase(&stable_node->node, &root_stable_tree);
4604035c07aSHugh Dickins 	free_stable_node(stable_node);
4614035c07aSHugh Dickins }
4624035c07aSHugh Dickins 
4634035c07aSHugh Dickins /*
4644035c07aSHugh Dickins  * get_ksm_page: checks if the page indicated by the stable node
4654035c07aSHugh Dickins  * is still its ksm page, despite having held no reference to it.
4664035c07aSHugh Dickins  * In which case we can trust the content of the page, and it
4674035c07aSHugh Dickins  * returns the gotten page; but if the page has now been zapped,
4684035c07aSHugh Dickins  * remove the stale node from the stable tree and return NULL.
4694035c07aSHugh Dickins  *
4704035c07aSHugh Dickins  * You would expect the stable_node to hold a reference to the ksm page.
4714035c07aSHugh Dickins  * But if it increments the page's count, swapping out has to wait for
4724035c07aSHugh Dickins  * ksmd to come around again before it can free the page, which may take
4734035c07aSHugh Dickins  * seconds or even minutes: much too unresponsive.  So instead we use a
4744035c07aSHugh Dickins  * "keyhole reference": access to the ksm page from the stable node peeps
4754035c07aSHugh Dickins  * out through its keyhole to see if that page still holds the right key,
4764035c07aSHugh Dickins  * pointing back to this stable node.  This relies on freeing a PageAnon
4774035c07aSHugh Dickins  * page to reset its page->mapping to NULL, and relies on no other use of
4784035c07aSHugh Dickins  * a page to put something that might look like our key in page->mapping.
4794035c07aSHugh Dickins  *
4804035c07aSHugh Dickins  * include/linux/pagemap.h page_cache_get_speculative() is a good reference,
4814035c07aSHugh Dickins  * but this is different - made simpler by ksm_thread_mutex being held, but
4824035c07aSHugh Dickins  * interesting for assuming that no other use of the struct page could ever
4834035c07aSHugh Dickins  * put our expected_mapping into page->mapping (or a field of the union which
4844035c07aSHugh Dickins  * coincides with page->mapping).  The RCU calls are not for KSM at all, but
4854035c07aSHugh Dickins  * to keep the page_count protocol described with page_cache_get_speculative.
4864035c07aSHugh Dickins  *
4874035c07aSHugh Dickins  * Note: it is possible that get_ksm_page() will return NULL one moment,
4884035c07aSHugh Dickins  * then page the next, if the page is in between page_freeze_refs() and
4894035c07aSHugh Dickins  * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page
4904035c07aSHugh Dickins  * is on its way to being freed; but it is an anomaly to bear in mind.
4914035c07aSHugh Dickins  */
4924035c07aSHugh Dickins static struct page *get_ksm_page(struct stable_node *stable_node)
4934035c07aSHugh Dickins {
4944035c07aSHugh Dickins 	struct page *page;
4954035c07aSHugh Dickins 	void *expected_mapping;
4964035c07aSHugh Dickins 
49762b61f61SHugh Dickins 	page = pfn_to_page(stable_node->kpfn);
4984035c07aSHugh Dickins 	expected_mapping = (void *)stable_node +
4994035c07aSHugh Dickins 				(PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
5004035c07aSHugh Dickins 	rcu_read_lock();
5014035c07aSHugh Dickins 	if (page->mapping != expected_mapping)
5024035c07aSHugh Dickins 		goto stale;
5034035c07aSHugh Dickins 	if (!get_page_unless_zero(page))
5044035c07aSHugh Dickins 		goto stale;
5054035c07aSHugh Dickins 	if (page->mapping != expected_mapping) {
5064035c07aSHugh Dickins 		put_page(page);
5074035c07aSHugh Dickins 		goto stale;
5084035c07aSHugh Dickins 	}
5094035c07aSHugh Dickins 	rcu_read_unlock();
5104035c07aSHugh Dickins 	return page;
5114035c07aSHugh Dickins stale:
5124035c07aSHugh Dickins 	rcu_read_unlock();
5134035c07aSHugh Dickins 	remove_node_from_stable_tree(stable_node);
5144035c07aSHugh Dickins 	return NULL;
5154035c07aSHugh Dickins }
5164035c07aSHugh Dickins 
51731dbd01fSIzik Eidus /*
51831dbd01fSIzik Eidus  * Removing rmap_item from stable or unstable tree.
51931dbd01fSIzik Eidus  * This function will clean the information from the stable/unstable tree.
52031dbd01fSIzik Eidus  */
52131dbd01fSIzik Eidus static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
52231dbd01fSIzik Eidus {
5237b6ba2c7SHugh Dickins 	if (rmap_item->address & STABLE_FLAG) {
5247b6ba2c7SHugh Dickins 		struct stable_node *stable_node;
5255ad64688SHugh Dickins 		struct page *page;
52631dbd01fSIzik Eidus 
5277b6ba2c7SHugh Dickins 		stable_node = rmap_item->head;
5284035c07aSHugh Dickins 		page = get_ksm_page(stable_node);
5294035c07aSHugh Dickins 		if (!page)
5304035c07aSHugh Dickins 			goto out;
5315ad64688SHugh Dickins 
5324035c07aSHugh Dickins 		lock_page(page);
5337b6ba2c7SHugh Dickins 		hlist_del(&rmap_item->hlist);
5345ad64688SHugh Dickins 		unlock_page(page);
5355ad64688SHugh Dickins 		put_page(page);
53608beca44SHugh Dickins 
5374035c07aSHugh Dickins 		if (stable_node->hlist.first)
5384035c07aSHugh Dickins 			ksm_pages_sharing--;
5394035c07aSHugh Dickins 		else
540b4028260SHugh Dickins 			ksm_pages_shared--;
54131dbd01fSIzik Eidus 
5429e60109fSPeter Zijlstra 		put_anon_vma(rmap_item->anon_vma);
54393d17715SHugh Dickins 		rmap_item->address &= PAGE_MASK;
54431dbd01fSIzik Eidus 
5457b6ba2c7SHugh Dickins 	} else if (rmap_item->address & UNSTABLE_FLAG) {
54631dbd01fSIzik Eidus 		unsigned char age;
54731dbd01fSIzik Eidus 		/*
5489ba69294SHugh Dickins 		 * Usually ksmd can and must skip the rb_erase, because
54931dbd01fSIzik Eidus 		 * root_unstable_tree was already reset to RB_ROOT.
5509ba69294SHugh Dickins 		 * But be careful when an mm is exiting: do the rb_erase
5519ba69294SHugh Dickins 		 * if this rmap_item was inserted by this scan, rather
5529ba69294SHugh Dickins 		 * than left over from before.
55331dbd01fSIzik Eidus 		 */
55431dbd01fSIzik Eidus 		age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
555cd551f97SHugh Dickins 		BUG_ON(age > 1);
55631dbd01fSIzik Eidus 		if (!age)
55731dbd01fSIzik Eidus 			rb_erase(&rmap_item->node, &root_unstable_tree);
55831dbd01fSIzik Eidus 
55993d17715SHugh Dickins 		ksm_pages_unshared--;
56031dbd01fSIzik Eidus 		rmap_item->address &= PAGE_MASK;
56193d17715SHugh Dickins 	}
5624035c07aSHugh Dickins out:
56331dbd01fSIzik Eidus 	cond_resched();		/* we're called from many long loops */
56431dbd01fSIzik Eidus }
56531dbd01fSIzik Eidus 
56631dbd01fSIzik Eidus static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
5676514d511SHugh Dickins 				       struct rmap_item **rmap_list)
56831dbd01fSIzik Eidus {
5696514d511SHugh Dickins 	while (*rmap_list) {
5706514d511SHugh Dickins 		struct rmap_item *rmap_item = *rmap_list;
5716514d511SHugh Dickins 		*rmap_list = rmap_item->rmap_list;
57231dbd01fSIzik Eidus 		remove_rmap_item_from_tree(rmap_item);
57331dbd01fSIzik Eidus 		free_rmap_item(rmap_item);
57431dbd01fSIzik Eidus 	}
57531dbd01fSIzik Eidus }
57631dbd01fSIzik Eidus 
57731dbd01fSIzik Eidus /*
57831dbd01fSIzik Eidus  * Though it's very tempting to unmerge in_stable_tree(rmap_item)s rather
57931dbd01fSIzik Eidus  * than check every pte of a given vma, the locking doesn't quite work for
58031dbd01fSIzik Eidus  * that - an rmap_item is assigned to the stable tree after inserting ksm
58131dbd01fSIzik Eidus  * page and upping mmap_sem.  Nor does it fit with the way we skip dup'ing
58231dbd01fSIzik Eidus  * rmap_items from parent to child at fork time (so as not to waste time
58331dbd01fSIzik Eidus  * if exit comes before the next scan reaches it).
58481464e30SHugh Dickins  *
58581464e30SHugh Dickins  * Similarly, although we'd like to remove rmap_items (so updating counts
58681464e30SHugh Dickins  * and freeing memory) when unmerging an area, it's easier to leave that
58781464e30SHugh Dickins  * to the next pass of ksmd - consider, for example, how ksmd might be
58881464e30SHugh Dickins  * in cmp_and_merge_page on one of the rmap_items we would be removing.
58931dbd01fSIzik Eidus  */
590d952b791SHugh Dickins static int unmerge_ksm_pages(struct vm_area_struct *vma,
59131dbd01fSIzik Eidus 			     unsigned long start, unsigned long end)
59231dbd01fSIzik Eidus {
59331dbd01fSIzik Eidus 	unsigned long addr;
594d952b791SHugh Dickins 	int err = 0;
59531dbd01fSIzik Eidus 
596d952b791SHugh Dickins 	for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
5979ba69294SHugh Dickins 		if (ksm_test_exit(vma->vm_mm))
5989ba69294SHugh Dickins 			break;
599d952b791SHugh Dickins 		if (signal_pending(current))
600d952b791SHugh Dickins 			err = -ERESTARTSYS;
601d952b791SHugh Dickins 		else
602d952b791SHugh Dickins 			err = break_ksm(vma, addr);
603d952b791SHugh Dickins 	}
604d952b791SHugh Dickins 	return err;
60531dbd01fSIzik Eidus }
60631dbd01fSIzik Eidus 
6072ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
6082ffd8679SHugh Dickins /*
6092ffd8679SHugh Dickins  * Only called through the sysfs control interface:
6102ffd8679SHugh Dickins  */
611d952b791SHugh Dickins static int unmerge_and_remove_all_rmap_items(void)
61231dbd01fSIzik Eidus {
61331dbd01fSIzik Eidus 	struct mm_slot *mm_slot;
61431dbd01fSIzik Eidus 	struct mm_struct *mm;
61531dbd01fSIzik Eidus 	struct vm_area_struct *vma;
616d952b791SHugh Dickins 	int err = 0;
61731dbd01fSIzik Eidus 
618d952b791SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
6199ba69294SHugh Dickins 	ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
620d952b791SHugh Dickins 						struct mm_slot, mm_list);
621d952b791SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
622d952b791SHugh Dickins 
6239ba69294SHugh Dickins 	for (mm_slot = ksm_scan.mm_slot;
6249ba69294SHugh Dickins 			mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
62531dbd01fSIzik Eidus 		mm = mm_slot->mm;
62631dbd01fSIzik Eidus 		down_read(&mm->mmap_sem);
62731dbd01fSIzik Eidus 		for (vma = mm->mmap; vma; vma = vma->vm_next) {
6289ba69294SHugh Dickins 			if (ksm_test_exit(mm))
6299ba69294SHugh Dickins 				break;
63031dbd01fSIzik Eidus 			if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
63131dbd01fSIzik Eidus 				continue;
632d952b791SHugh Dickins 			err = unmerge_ksm_pages(vma,
633d952b791SHugh Dickins 						vma->vm_start, vma->vm_end);
6349ba69294SHugh Dickins 			if (err)
6359ba69294SHugh Dickins 				goto error;
636d952b791SHugh Dickins 		}
6379ba69294SHugh Dickins 
6386514d511SHugh Dickins 		remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
63931dbd01fSIzik Eidus 
64031dbd01fSIzik Eidus 		spin_lock(&ksm_mmlist_lock);
6419ba69294SHugh Dickins 		ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
642d952b791SHugh Dickins 						struct mm_slot, mm_list);
6439ba69294SHugh Dickins 		if (ksm_test_exit(mm)) {
644*4ca3a69bSSasha Levin 			hash_del(&mm_slot->link);
6459ba69294SHugh Dickins 			list_del(&mm_slot->mm_list);
64631dbd01fSIzik Eidus 			spin_unlock(&ksm_mmlist_lock);
6479ba69294SHugh Dickins 
6489ba69294SHugh Dickins 			free_mm_slot(mm_slot);
6499ba69294SHugh Dickins 			clear_bit(MMF_VM_MERGEABLE, &mm->flags);
6509ba69294SHugh Dickins 			up_read(&mm->mmap_sem);
6519ba69294SHugh Dickins 			mmdrop(mm);
6529ba69294SHugh Dickins 		} else {
6539ba69294SHugh Dickins 			spin_unlock(&ksm_mmlist_lock);
6549ba69294SHugh Dickins 			up_read(&mm->mmap_sem);
6559ba69294SHugh Dickins 		}
65631dbd01fSIzik Eidus 	}
65731dbd01fSIzik Eidus 
658d952b791SHugh Dickins 	ksm_scan.seqnr = 0;
6599ba69294SHugh Dickins 	return 0;
6609ba69294SHugh Dickins 
6619ba69294SHugh Dickins error:
6629ba69294SHugh Dickins 	up_read(&mm->mmap_sem);
663d952b791SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
664d952b791SHugh Dickins 	ksm_scan.mm_slot = &ksm_mm_head;
665d952b791SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
666d952b791SHugh Dickins 	return err;
667d952b791SHugh Dickins }
6682ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
669d952b791SHugh Dickins 
67031dbd01fSIzik Eidus static u32 calc_checksum(struct page *page)
67131dbd01fSIzik Eidus {
67231dbd01fSIzik Eidus 	u32 checksum;
6739b04c5feSCong Wang 	void *addr = kmap_atomic(page);
67431dbd01fSIzik Eidus 	checksum = jhash2(addr, PAGE_SIZE / 4, 17);
6759b04c5feSCong Wang 	kunmap_atomic(addr);
67631dbd01fSIzik Eidus 	return checksum;
67731dbd01fSIzik Eidus }
67831dbd01fSIzik Eidus 
67931dbd01fSIzik Eidus static int memcmp_pages(struct page *page1, struct page *page2)
68031dbd01fSIzik Eidus {
68131dbd01fSIzik Eidus 	char *addr1, *addr2;
68231dbd01fSIzik Eidus 	int ret;
68331dbd01fSIzik Eidus 
6849b04c5feSCong Wang 	addr1 = kmap_atomic(page1);
6859b04c5feSCong Wang 	addr2 = kmap_atomic(page2);
68631dbd01fSIzik Eidus 	ret = memcmp(addr1, addr2, PAGE_SIZE);
6879b04c5feSCong Wang 	kunmap_atomic(addr2);
6889b04c5feSCong Wang 	kunmap_atomic(addr1);
68931dbd01fSIzik Eidus 	return ret;
69031dbd01fSIzik Eidus }
69131dbd01fSIzik Eidus 
69231dbd01fSIzik Eidus static inline int pages_identical(struct page *page1, struct page *page2)
69331dbd01fSIzik Eidus {
69431dbd01fSIzik Eidus 	return !memcmp_pages(page1, page2);
69531dbd01fSIzik Eidus }
69631dbd01fSIzik Eidus 
69731dbd01fSIzik Eidus static int write_protect_page(struct vm_area_struct *vma, struct page *page,
69831dbd01fSIzik Eidus 			      pte_t *orig_pte)
69931dbd01fSIzik Eidus {
70031dbd01fSIzik Eidus 	struct mm_struct *mm = vma->vm_mm;
70131dbd01fSIzik Eidus 	unsigned long addr;
70231dbd01fSIzik Eidus 	pte_t *ptep;
70331dbd01fSIzik Eidus 	spinlock_t *ptl;
70431dbd01fSIzik Eidus 	int swapped;
70531dbd01fSIzik Eidus 	int err = -EFAULT;
7066bdb913fSHaggai Eran 	unsigned long mmun_start;	/* For mmu_notifiers */
7076bdb913fSHaggai Eran 	unsigned long mmun_end;		/* For mmu_notifiers */
70831dbd01fSIzik Eidus 
70931dbd01fSIzik Eidus 	addr = page_address_in_vma(page, vma);
71031dbd01fSIzik Eidus 	if (addr == -EFAULT)
71131dbd01fSIzik Eidus 		goto out;
71231dbd01fSIzik Eidus 
71329ad768cSAndrea Arcangeli 	BUG_ON(PageTransCompound(page));
7146bdb913fSHaggai Eran 
7156bdb913fSHaggai Eran 	mmun_start = addr;
7166bdb913fSHaggai Eran 	mmun_end   = addr + PAGE_SIZE;
7176bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
7186bdb913fSHaggai Eran 
71931dbd01fSIzik Eidus 	ptep = page_check_address(page, mm, addr, &ptl, 0);
72031dbd01fSIzik Eidus 	if (!ptep)
7216bdb913fSHaggai Eran 		goto out_mn;
72231dbd01fSIzik Eidus 
7234e31635cSHugh Dickins 	if (pte_write(*ptep) || pte_dirty(*ptep)) {
72431dbd01fSIzik Eidus 		pte_t entry;
72531dbd01fSIzik Eidus 
72631dbd01fSIzik Eidus 		swapped = PageSwapCache(page);
72731dbd01fSIzik Eidus 		flush_cache_page(vma, addr, page_to_pfn(page));
72831dbd01fSIzik Eidus 		/*
72925985edcSLucas De Marchi 		 * Ok this is tricky, when get_user_pages_fast() run it doesn't
73031dbd01fSIzik Eidus 		 * take any lock, therefore the check that we are going to make
73131dbd01fSIzik Eidus 		 * with the pagecount against the mapcount is racey and
73231dbd01fSIzik Eidus 		 * O_DIRECT can happen right after the check.
73331dbd01fSIzik Eidus 		 * So we clear the pte and flush the tlb before the check
73431dbd01fSIzik Eidus 		 * this assure us that no O_DIRECT can happen after the check
73531dbd01fSIzik Eidus 		 * or in the middle of the check.
73631dbd01fSIzik Eidus 		 */
73731dbd01fSIzik Eidus 		entry = ptep_clear_flush(vma, addr, ptep);
73831dbd01fSIzik Eidus 		/*
73931dbd01fSIzik Eidus 		 * Check that no O_DIRECT or similar I/O is in progress on the
74031dbd01fSIzik Eidus 		 * page
74131dbd01fSIzik Eidus 		 */
74231e855eaSHugh Dickins 		if (page_mapcount(page) + 1 + swapped != page_count(page)) {
743cb532375SRobin Holt 			set_pte_at(mm, addr, ptep, entry);
74431dbd01fSIzik Eidus 			goto out_unlock;
74531dbd01fSIzik Eidus 		}
7464e31635cSHugh Dickins 		if (pte_dirty(entry))
7474e31635cSHugh Dickins 			set_page_dirty(page);
7484e31635cSHugh Dickins 		entry = pte_mkclean(pte_wrprotect(entry));
74931dbd01fSIzik Eidus 		set_pte_at_notify(mm, addr, ptep, entry);
75031dbd01fSIzik Eidus 	}
75131dbd01fSIzik Eidus 	*orig_pte = *ptep;
75231dbd01fSIzik Eidus 	err = 0;
75331dbd01fSIzik Eidus 
75431dbd01fSIzik Eidus out_unlock:
75531dbd01fSIzik Eidus 	pte_unmap_unlock(ptep, ptl);
7566bdb913fSHaggai Eran out_mn:
7576bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
75831dbd01fSIzik Eidus out:
75931dbd01fSIzik Eidus 	return err;
76031dbd01fSIzik Eidus }
76131dbd01fSIzik Eidus 
76231dbd01fSIzik Eidus /**
76331dbd01fSIzik Eidus  * replace_page - replace page in vma by new ksm page
7648dd3557aSHugh Dickins  * @vma:      vma that holds the pte pointing to page
7658dd3557aSHugh Dickins  * @page:     the page we are replacing by kpage
7668dd3557aSHugh Dickins  * @kpage:    the ksm page we replace page by
76731dbd01fSIzik Eidus  * @orig_pte: the original value of the pte
76831dbd01fSIzik Eidus  *
76931dbd01fSIzik Eidus  * Returns 0 on success, -EFAULT on failure.
77031dbd01fSIzik Eidus  */
7718dd3557aSHugh Dickins static int replace_page(struct vm_area_struct *vma, struct page *page,
7728dd3557aSHugh Dickins 			struct page *kpage, pte_t orig_pte)
77331dbd01fSIzik Eidus {
77431dbd01fSIzik Eidus 	struct mm_struct *mm = vma->vm_mm;
77531dbd01fSIzik Eidus 	pmd_t *pmd;
77631dbd01fSIzik Eidus 	pte_t *ptep;
77731dbd01fSIzik Eidus 	spinlock_t *ptl;
77831dbd01fSIzik Eidus 	unsigned long addr;
77931dbd01fSIzik Eidus 	int err = -EFAULT;
7806bdb913fSHaggai Eran 	unsigned long mmun_start;	/* For mmu_notifiers */
7816bdb913fSHaggai Eran 	unsigned long mmun_end;		/* For mmu_notifiers */
78231dbd01fSIzik Eidus 
7838dd3557aSHugh Dickins 	addr = page_address_in_vma(page, vma);
78431dbd01fSIzik Eidus 	if (addr == -EFAULT)
78531dbd01fSIzik Eidus 		goto out;
78631dbd01fSIzik Eidus 
7876219049aSBob Liu 	pmd = mm_find_pmd(mm, addr);
7886219049aSBob Liu 	if (!pmd)
78931dbd01fSIzik Eidus 		goto out;
79029ad768cSAndrea Arcangeli 	BUG_ON(pmd_trans_huge(*pmd));
79131dbd01fSIzik Eidus 
7926bdb913fSHaggai Eran 	mmun_start = addr;
7936bdb913fSHaggai Eran 	mmun_end   = addr + PAGE_SIZE;
7946bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
7956bdb913fSHaggai Eran 
79631dbd01fSIzik Eidus 	ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
79731dbd01fSIzik Eidus 	if (!pte_same(*ptep, orig_pte)) {
79831dbd01fSIzik Eidus 		pte_unmap_unlock(ptep, ptl);
7996bdb913fSHaggai Eran 		goto out_mn;
80031dbd01fSIzik Eidus 	}
80131dbd01fSIzik Eidus 
8028dd3557aSHugh Dickins 	get_page(kpage);
8035ad64688SHugh Dickins 	page_add_anon_rmap(kpage, vma, addr);
80431dbd01fSIzik Eidus 
80531dbd01fSIzik Eidus 	flush_cache_page(vma, addr, pte_pfn(*ptep));
80631dbd01fSIzik Eidus 	ptep_clear_flush(vma, addr, ptep);
8078dd3557aSHugh Dickins 	set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
80831dbd01fSIzik Eidus 
8098dd3557aSHugh Dickins 	page_remove_rmap(page);
810ae52a2adSHugh Dickins 	if (!page_mapped(page))
811ae52a2adSHugh Dickins 		try_to_free_swap(page);
8128dd3557aSHugh Dickins 	put_page(page);
81331dbd01fSIzik Eidus 
81431dbd01fSIzik Eidus 	pte_unmap_unlock(ptep, ptl);
81531dbd01fSIzik Eidus 	err = 0;
8166bdb913fSHaggai Eran out_mn:
8176bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
81831dbd01fSIzik Eidus out:
81931dbd01fSIzik Eidus 	return err;
82031dbd01fSIzik Eidus }
82131dbd01fSIzik Eidus 
82229ad768cSAndrea Arcangeli static int page_trans_compound_anon_split(struct page *page)
82329ad768cSAndrea Arcangeli {
82429ad768cSAndrea Arcangeli 	int ret = 0;
82529ad768cSAndrea Arcangeli 	struct page *transhuge_head = page_trans_compound_anon(page);
82629ad768cSAndrea Arcangeli 	if (transhuge_head) {
82729ad768cSAndrea Arcangeli 		/* Get the reference on the head to split it. */
82829ad768cSAndrea Arcangeli 		if (get_page_unless_zero(transhuge_head)) {
82929ad768cSAndrea Arcangeli 			/*
83029ad768cSAndrea Arcangeli 			 * Recheck we got the reference while the head
83129ad768cSAndrea Arcangeli 			 * was still anonymous.
83229ad768cSAndrea Arcangeli 			 */
83329ad768cSAndrea Arcangeli 			if (PageAnon(transhuge_head))
83429ad768cSAndrea Arcangeli 				ret = split_huge_page(transhuge_head);
83529ad768cSAndrea Arcangeli 			else
83629ad768cSAndrea Arcangeli 				/*
83729ad768cSAndrea Arcangeli 				 * Retry later if split_huge_page run
83829ad768cSAndrea Arcangeli 				 * from under us.
83929ad768cSAndrea Arcangeli 				 */
84029ad768cSAndrea Arcangeli 				ret = 1;
84129ad768cSAndrea Arcangeli 			put_page(transhuge_head);
84229ad768cSAndrea Arcangeli 		} else
84329ad768cSAndrea Arcangeli 			/* Retry later if split_huge_page run from under us. */
84429ad768cSAndrea Arcangeli 			ret = 1;
84529ad768cSAndrea Arcangeli 	}
84629ad768cSAndrea Arcangeli 	return ret;
84729ad768cSAndrea Arcangeli }
84829ad768cSAndrea Arcangeli 
84931dbd01fSIzik Eidus /*
85031dbd01fSIzik Eidus  * try_to_merge_one_page - take two pages and merge them into one
8518dd3557aSHugh Dickins  * @vma: the vma that holds the pte pointing to page
8528dd3557aSHugh Dickins  * @page: the PageAnon page that we want to replace with kpage
85380e14822SHugh Dickins  * @kpage: the PageKsm page that we want to map instead of page,
85480e14822SHugh Dickins  *         or NULL the first time when we want to use page as kpage.
85531dbd01fSIzik Eidus  *
85631dbd01fSIzik Eidus  * This function returns 0 if the pages were merged, -EFAULT otherwise.
85731dbd01fSIzik Eidus  */
85831dbd01fSIzik Eidus static int try_to_merge_one_page(struct vm_area_struct *vma,
8598dd3557aSHugh Dickins 				 struct page *page, struct page *kpage)
86031dbd01fSIzik Eidus {
86131dbd01fSIzik Eidus 	pte_t orig_pte = __pte(0);
86231dbd01fSIzik Eidus 	int err = -EFAULT;
86331dbd01fSIzik Eidus 
864db114b83SHugh Dickins 	if (page == kpage)			/* ksm page forked */
865db114b83SHugh Dickins 		return 0;
866db114b83SHugh Dickins 
86731dbd01fSIzik Eidus 	if (!(vma->vm_flags & VM_MERGEABLE))
86831dbd01fSIzik Eidus 		goto out;
86929ad768cSAndrea Arcangeli 	if (PageTransCompound(page) && page_trans_compound_anon_split(page))
87029ad768cSAndrea Arcangeli 		goto out;
87129ad768cSAndrea Arcangeli 	BUG_ON(PageTransCompound(page));
8728dd3557aSHugh Dickins 	if (!PageAnon(page))
87331dbd01fSIzik Eidus 		goto out;
87431dbd01fSIzik Eidus 
87531dbd01fSIzik Eidus 	/*
87631dbd01fSIzik Eidus 	 * We need the page lock to read a stable PageSwapCache in
87731dbd01fSIzik Eidus 	 * write_protect_page().  We use trylock_page() instead of
87831dbd01fSIzik Eidus 	 * lock_page() because we don't want to wait here - we
87931dbd01fSIzik Eidus 	 * prefer to continue scanning and merging different pages,
88031dbd01fSIzik Eidus 	 * then come back to this page when it is unlocked.
88131dbd01fSIzik Eidus 	 */
8828dd3557aSHugh Dickins 	if (!trylock_page(page))
88331e855eaSHugh Dickins 		goto out;
88431dbd01fSIzik Eidus 	/*
88531dbd01fSIzik Eidus 	 * If this anonymous page is mapped only here, its pte may need
88631dbd01fSIzik Eidus 	 * to be write-protected.  If it's mapped elsewhere, all of its
88731dbd01fSIzik Eidus 	 * ptes are necessarily already write-protected.  But in either
88831dbd01fSIzik Eidus 	 * case, we need to lock and check page_count is not raised.
88931dbd01fSIzik Eidus 	 */
89080e14822SHugh Dickins 	if (write_protect_page(vma, page, &orig_pte) == 0) {
89180e14822SHugh Dickins 		if (!kpage) {
89280e14822SHugh Dickins 			/*
89380e14822SHugh Dickins 			 * While we hold page lock, upgrade page from
89480e14822SHugh Dickins 			 * PageAnon+anon_vma to PageKsm+NULL stable_node:
89580e14822SHugh Dickins 			 * stable_tree_insert() will update stable_node.
89680e14822SHugh Dickins 			 */
89780e14822SHugh Dickins 			set_page_stable_node(page, NULL);
89880e14822SHugh Dickins 			mark_page_accessed(page);
89980e14822SHugh Dickins 			err = 0;
90080e14822SHugh Dickins 		} else if (pages_identical(page, kpage))
9018dd3557aSHugh Dickins 			err = replace_page(vma, page, kpage, orig_pte);
90280e14822SHugh Dickins 	}
90331dbd01fSIzik Eidus 
90480e14822SHugh Dickins 	if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
90573848b46SHugh Dickins 		munlock_vma_page(page);
9065ad64688SHugh Dickins 		if (!PageMlocked(kpage)) {
9075ad64688SHugh Dickins 			unlock_page(page);
9085ad64688SHugh Dickins 			lock_page(kpage);
9095ad64688SHugh Dickins 			mlock_vma_page(kpage);
9105ad64688SHugh Dickins 			page = kpage;		/* for final unlock */
9115ad64688SHugh Dickins 		}
9125ad64688SHugh Dickins 	}
91373848b46SHugh Dickins 
9148dd3557aSHugh Dickins 	unlock_page(page);
91531dbd01fSIzik Eidus out:
91631dbd01fSIzik Eidus 	return err;
91731dbd01fSIzik Eidus }
91831dbd01fSIzik Eidus 
91931dbd01fSIzik Eidus /*
92081464e30SHugh Dickins  * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
92181464e30SHugh Dickins  * but no new kernel page is allocated: kpage must already be a ksm page.
9228dd3557aSHugh Dickins  *
9238dd3557aSHugh Dickins  * This function returns 0 if the pages were merged, -EFAULT otherwise.
92481464e30SHugh Dickins  */
9258dd3557aSHugh Dickins static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
9268dd3557aSHugh Dickins 				      struct page *page, struct page *kpage)
92781464e30SHugh Dickins {
9288dd3557aSHugh Dickins 	struct mm_struct *mm = rmap_item->mm;
92981464e30SHugh Dickins 	struct vm_area_struct *vma;
93081464e30SHugh Dickins 	int err = -EFAULT;
93181464e30SHugh Dickins 
9328dd3557aSHugh Dickins 	down_read(&mm->mmap_sem);
9338dd3557aSHugh Dickins 	if (ksm_test_exit(mm))
9348dd3557aSHugh Dickins 		goto out;
9358dd3557aSHugh Dickins 	vma = find_vma(mm, rmap_item->address);
9368dd3557aSHugh Dickins 	if (!vma || vma->vm_start > rmap_item->address)
9379ba69294SHugh Dickins 		goto out;
9389ba69294SHugh Dickins 
9398dd3557aSHugh Dickins 	err = try_to_merge_one_page(vma, page, kpage);
940db114b83SHugh Dickins 	if (err)
941db114b83SHugh Dickins 		goto out;
942db114b83SHugh Dickins 
943db114b83SHugh Dickins 	/* Must get reference to anon_vma while still holding mmap_sem */
9449e60109fSPeter Zijlstra 	rmap_item->anon_vma = vma->anon_vma;
9459e60109fSPeter Zijlstra 	get_anon_vma(vma->anon_vma);
94681464e30SHugh Dickins out:
9478dd3557aSHugh Dickins 	up_read(&mm->mmap_sem);
94881464e30SHugh Dickins 	return err;
94981464e30SHugh Dickins }
95081464e30SHugh Dickins 
95181464e30SHugh Dickins /*
95231dbd01fSIzik Eidus  * try_to_merge_two_pages - take two identical pages and prepare them
95331dbd01fSIzik Eidus  * to be merged into one page.
95431dbd01fSIzik Eidus  *
9558dd3557aSHugh Dickins  * This function returns the kpage if we successfully merged two identical
9568dd3557aSHugh Dickins  * pages into one ksm page, NULL otherwise.
95731dbd01fSIzik Eidus  *
95880e14822SHugh Dickins  * Note that this function upgrades page to ksm page: if one of the pages
95931dbd01fSIzik Eidus  * is already a ksm page, try_to_merge_with_ksm_page should be used.
96031dbd01fSIzik Eidus  */
9618dd3557aSHugh Dickins static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
9628dd3557aSHugh Dickins 					   struct page *page,
9638dd3557aSHugh Dickins 					   struct rmap_item *tree_rmap_item,
9648dd3557aSHugh Dickins 					   struct page *tree_page)
96531dbd01fSIzik Eidus {
96680e14822SHugh Dickins 	int err;
96731dbd01fSIzik Eidus 
96880e14822SHugh Dickins 	err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
96931dbd01fSIzik Eidus 	if (!err) {
9708dd3557aSHugh Dickins 		err = try_to_merge_with_ksm_page(tree_rmap_item,
97180e14822SHugh Dickins 							tree_page, page);
97231dbd01fSIzik Eidus 		/*
97381464e30SHugh Dickins 		 * If that fails, we have a ksm page with only one pte
97481464e30SHugh Dickins 		 * pointing to it: so break it.
97531dbd01fSIzik Eidus 		 */
9764035c07aSHugh Dickins 		if (err)
9778dd3557aSHugh Dickins 			break_cow(rmap_item);
97831dbd01fSIzik Eidus 	}
97980e14822SHugh Dickins 	return err ? NULL : page;
98031dbd01fSIzik Eidus }
98131dbd01fSIzik Eidus 
98231dbd01fSIzik Eidus /*
9838dd3557aSHugh Dickins  * stable_tree_search - search for page inside the stable tree
98431dbd01fSIzik Eidus  *
98531dbd01fSIzik Eidus  * This function checks if there is a page inside the stable tree
98631dbd01fSIzik Eidus  * with identical content to the page that we are scanning right now.
98731dbd01fSIzik Eidus  *
9887b6ba2c7SHugh Dickins  * This function returns the stable tree node of identical content if found,
98931dbd01fSIzik Eidus  * NULL otherwise.
99031dbd01fSIzik Eidus  */
99162b61f61SHugh Dickins static struct page *stable_tree_search(struct page *page)
99231dbd01fSIzik Eidus {
99331dbd01fSIzik Eidus 	struct rb_node *node = root_stable_tree.rb_node;
9947b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
99531dbd01fSIzik Eidus 
99608beca44SHugh Dickins 	stable_node = page_stable_node(page);
99708beca44SHugh Dickins 	if (stable_node) {			/* ksm page forked */
99808beca44SHugh Dickins 		get_page(page);
99962b61f61SHugh Dickins 		return page;
100008beca44SHugh Dickins 	}
100108beca44SHugh Dickins 
100231dbd01fSIzik Eidus 	while (node) {
10034035c07aSHugh Dickins 		struct page *tree_page;
100431dbd01fSIzik Eidus 		int ret;
100531dbd01fSIzik Eidus 
100631dbd01fSIzik Eidus 		cond_resched();
100708beca44SHugh Dickins 		stable_node = rb_entry(node, struct stable_node, node);
10084035c07aSHugh Dickins 		tree_page = get_ksm_page(stable_node);
10094035c07aSHugh Dickins 		if (!tree_page)
10104035c07aSHugh Dickins 			return NULL;
101131dbd01fSIzik Eidus 
10124035c07aSHugh Dickins 		ret = memcmp_pages(page, tree_page);
101331dbd01fSIzik Eidus 
10144035c07aSHugh Dickins 		if (ret < 0) {
10154035c07aSHugh Dickins 			put_page(tree_page);
101631dbd01fSIzik Eidus 			node = node->rb_left;
10174035c07aSHugh Dickins 		} else if (ret > 0) {
10184035c07aSHugh Dickins 			put_page(tree_page);
101931dbd01fSIzik Eidus 			node = node->rb_right;
10204035c07aSHugh Dickins 		} else
102162b61f61SHugh Dickins 			return tree_page;
102231dbd01fSIzik Eidus 	}
102331dbd01fSIzik Eidus 
102431dbd01fSIzik Eidus 	return NULL;
102531dbd01fSIzik Eidus }
102631dbd01fSIzik Eidus 
102731dbd01fSIzik Eidus /*
102831dbd01fSIzik Eidus  * stable_tree_insert - insert rmap_item pointing to new ksm page
102931dbd01fSIzik Eidus  * into the stable tree.
103031dbd01fSIzik Eidus  *
10317b6ba2c7SHugh Dickins  * This function returns the stable tree node just allocated on success,
10327b6ba2c7SHugh Dickins  * NULL otherwise.
103331dbd01fSIzik Eidus  */
10347b6ba2c7SHugh Dickins static struct stable_node *stable_tree_insert(struct page *kpage)
103531dbd01fSIzik Eidus {
103631dbd01fSIzik Eidus 	struct rb_node **new = &root_stable_tree.rb_node;
103731dbd01fSIzik Eidus 	struct rb_node *parent = NULL;
10387b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
103931dbd01fSIzik Eidus 
104031dbd01fSIzik Eidus 	while (*new) {
10414035c07aSHugh Dickins 		struct page *tree_page;
104231dbd01fSIzik Eidus 		int ret;
104331dbd01fSIzik Eidus 
104431dbd01fSIzik Eidus 		cond_resched();
104508beca44SHugh Dickins 		stable_node = rb_entry(*new, struct stable_node, node);
10464035c07aSHugh Dickins 		tree_page = get_ksm_page(stable_node);
10474035c07aSHugh Dickins 		if (!tree_page)
10484035c07aSHugh Dickins 			return NULL;
104931dbd01fSIzik Eidus 
10504035c07aSHugh Dickins 		ret = memcmp_pages(kpage, tree_page);
10514035c07aSHugh Dickins 		put_page(tree_page);
105231dbd01fSIzik Eidus 
105331dbd01fSIzik Eidus 		parent = *new;
105431dbd01fSIzik Eidus 		if (ret < 0)
105531dbd01fSIzik Eidus 			new = &parent->rb_left;
105631dbd01fSIzik Eidus 		else if (ret > 0)
105731dbd01fSIzik Eidus 			new = &parent->rb_right;
105831dbd01fSIzik Eidus 		else {
105931dbd01fSIzik Eidus 			/*
106031dbd01fSIzik Eidus 			 * It is not a bug that stable_tree_search() didn't
106131dbd01fSIzik Eidus 			 * find this node: because at that time our page was
106231dbd01fSIzik Eidus 			 * not yet write-protected, so may have changed since.
106331dbd01fSIzik Eidus 			 */
106431dbd01fSIzik Eidus 			return NULL;
106531dbd01fSIzik Eidus 		}
106631dbd01fSIzik Eidus 	}
106731dbd01fSIzik Eidus 
10687b6ba2c7SHugh Dickins 	stable_node = alloc_stable_node();
10697b6ba2c7SHugh Dickins 	if (!stable_node)
10707b6ba2c7SHugh Dickins 		return NULL;
107131dbd01fSIzik Eidus 
10727b6ba2c7SHugh Dickins 	rb_link_node(&stable_node->node, parent, new);
10737b6ba2c7SHugh Dickins 	rb_insert_color(&stable_node->node, &root_stable_tree);
10747b6ba2c7SHugh Dickins 
10757b6ba2c7SHugh Dickins 	INIT_HLIST_HEAD(&stable_node->hlist);
10767b6ba2c7SHugh Dickins 
107762b61f61SHugh Dickins 	stable_node->kpfn = page_to_pfn(kpage);
107808beca44SHugh Dickins 	set_page_stable_node(kpage, stable_node);
107908beca44SHugh Dickins 
10807b6ba2c7SHugh Dickins 	return stable_node;
108131dbd01fSIzik Eidus }
108231dbd01fSIzik Eidus 
108331dbd01fSIzik Eidus /*
10848dd3557aSHugh Dickins  * unstable_tree_search_insert - search for identical page,
10858dd3557aSHugh Dickins  * else insert rmap_item into the unstable tree.
108631dbd01fSIzik Eidus  *
108731dbd01fSIzik Eidus  * This function searches for a page in the unstable tree identical to the
108831dbd01fSIzik Eidus  * page currently being scanned; and if no identical page is found in the
108931dbd01fSIzik Eidus  * tree, we insert rmap_item as a new object into the unstable tree.
109031dbd01fSIzik Eidus  *
109131dbd01fSIzik Eidus  * This function returns pointer to rmap_item found to be identical
109231dbd01fSIzik Eidus  * to the currently scanned page, NULL otherwise.
109331dbd01fSIzik Eidus  *
109431dbd01fSIzik Eidus  * This function does both searching and inserting, because they share
109531dbd01fSIzik Eidus  * the same walking algorithm in an rbtree.
109631dbd01fSIzik Eidus  */
10978dd3557aSHugh Dickins static
10988dd3557aSHugh Dickins struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
10998dd3557aSHugh Dickins 					      struct page *page,
11008dd3557aSHugh Dickins 					      struct page **tree_pagep)
11018dd3557aSHugh Dickins 
110231dbd01fSIzik Eidus {
110331dbd01fSIzik Eidus 	struct rb_node **new = &root_unstable_tree.rb_node;
110431dbd01fSIzik Eidus 	struct rb_node *parent = NULL;
110531dbd01fSIzik Eidus 
110631dbd01fSIzik Eidus 	while (*new) {
110731dbd01fSIzik Eidus 		struct rmap_item *tree_rmap_item;
11088dd3557aSHugh Dickins 		struct page *tree_page;
110931dbd01fSIzik Eidus 		int ret;
111031dbd01fSIzik Eidus 
1111d178f27fSHugh Dickins 		cond_resched();
111231dbd01fSIzik Eidus 		tree_rmap_item = rb_entry(*new, struct rmap_item, node);
11138dd3557aSHugh Dickins 		tree_page = get_mergeable_page(tree_rmap_item);
111422eccdd7SDan Carpenter 		if (IS_ERR_OR_NULL(tree_page))
111531dbd01fSIzik Eidus 			return NULL;
111631dbd01fSIzik Eidus 
111731dbd01fSIzik Eidus 		/*
11188dd3557aSHugh Dickins 		 * Don't substitute a ksm page for a forked page.
111931dbd01fSIzik Eidus 		 */
11208dd3557aSHugh Dickins 		if (page == tree_page) {
11218dd3557aSHugh Dickins 			put_page(tree_page);
112231dbd01fSIzik Eidus 			return NULL;
112331dbd01fSIzik Eidus 		}
112431dbd01fSIzik Eidus 
11258dd3557aSHugh Dickins 		ret = memcmp_pages(page, tree_page);
112631dbd01fSIzik Eidus 
112731dbd01fSIzik Eidus 		parent = *new;
112831dbd01fSIzik Eidus 		if (ret < 0) {
11298dd3557aSHugh Dickins 			put_page(tree_page);
113031dbd01fSIzik Eidus 			new = &parent->rb_left;
113131dbd01fSIzik Eidus 		} else if (ret > 0) {
11328dd3557aSHugh Dickins 			put_page(tree_page);
113331dbd01fSIzik Eidus 			new = &parent->rb_right;
113431dbd01fSIzik Eidus 		} else {
11358dd3557aSHugh Dickins 			*tree_pagep = tree_page;
113631dbd01fSIzik Eidus 			return tree_rmap_item;
113731dbd01fSIzik Eidus 		}
113831dbd01fSIzik Eidus 	}
113931dbd01fSIzik Eidus 
11407b6ba2c7SHugh Dickins 	rmap_item->address |= UNSTABLE_FLAG;
114131dbd01fSIzik Eidus 	rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
114231dbd01fSIzik Eidus 	rb_link_node(&rmap_item->node, parent, new);
114331dbd01fSIzik Eidus 	rb_insert_color(&rmap_item->node, &root_unstable_tree);
114431dbd01fSIzik Eidus 
1145473b0ce4SHugh Dickins 	ksm_pages_unshared++;
114631dbd01fSIzik Eidus 	return NULL;
114731dbd01fSIzik Eidus }
114831dbd01fSIzik Eidus 
114931dbd01fSIzik Eidus /*
115031dbd01fSIzik Eidus  * stable_tree_append - add another rmap_item to the linked list of
115131dbd01fSIzik Eidus  * rmap_items hanging off a given node of the stable tree, all sharing
115231dbd01fSIzik Eidus  * the same ksm page.
115331dbd01fSIzik Eidus  */
115431dbd01fSIzik Eidus static void stable_tree_append(struct rmap_item *rmap_item,
11557b6ba2c7SHugh Dickins 			       struct stable_node *stable_node)
115631dbd01fSIzik Eidus {
11577b6ba2c7SHugh Dickins 	rmap_item->head = stable_node;
115831dbd01fSIzik Eidus 	rmap_item->address |= STABLE_FLAG;
11597b6ba2c7SHugh Dickins 	hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
1160e178dfdeSHugh Dickins 
11617b6ba2c7SHugh Dickins 	if (rmap_item->hlist.next)
1162e178dfdeSHugh Dickins 		ksm_pages_sharing++;
11637b6ba2c7SHugh Dickins 	else
11647b6ba2c7SHugh Dickins 		ksm_pages_shared++;
116531dbd01fSIzik Eidus }
116631dbd01fSIzik Eidus 
116731dbd01fSIzik Eidus /*
116881464e30SHugh Dickins  * cmp_and_merge_page - first see if page can be merged into the stable tree;
116981464e30SHugh Dickins  * if not, compare checksum to previous and if it's the same, see if page can
117081464e30SHugh Dickins  * be inserted into the unstable tree, or merged with a page already there and
117181464e30SHugh Dickins  * both transferred to the stable tree.
117231dbd01fSIzik Eidus  *
117331dbd01fSIzik Eidus  * @page: the page that we are searching identical page to.
117431dbd01fSIzik Eidus  * @rmap_item: the reverse mapping into the virtual address of this page
117531dbd01fSIzik Eidus  */
117631dbd01fSIzik Eidus static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
117731dbd01fSIzik Eidus {
117831dbd01fSIzik Eidus 	struct rmap_item *tree_rmap_item;
11798dd3557aSHugh Dickins 	struct page *tree_page = NULL;
11807b6ba2c7SHugh Dickins 	struct stable_node *stable_node;
11818dd3557aSHugh Dickins 	struct page *kpage;
118231dbd01fSIzik Eidus 	unsigned int checksum;
118331dbd01fSIzik Eidus 	int err;
118431dbd01fSIzik Eidus 
118531dbd01fSIzik Eidus 	remove_rmap_item_from_tree(rmap_item);
118631dbd01fSIzik Eidus 
118731dbd01fSIzik Eidus 	/* We first start with searching the page inside the stable tree */
118862b61f61SHugh Dickins 	kpage = stable_tree_search(page);
118962b61f61SHugh Dickins 	if (kpage) {
119008beca44SHugh Dickins 		err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
119131dbd01fSIzik Eidus 		if (!err) {
119231dbd01fSIzik Eidus 			/*
119331dbd01fSIzik Eidus 			 * The page was successfully merged:
119431dbd01fSIzik Eidus 			 * add its rmap_item to the stable tree.
119531dbd01fSIzik Eidus 			 */
11965ad64688SHugh Dickins 			lock_page(kpage);
119762b61f61SHugh Dickins 			stable_tree_append(rmap_item, page_stable_node(kpage));
11985ad64688SHugh Dickins 			unlock_page(kpage);
119931dbd01fSIzik Eidus 		}
12008dd3557aSHugh Dickins 		put_page(kpage);
120131dbd01fSIzik Eidus 		return;
120231dbd01fSIzik Eidus 	}
120331dbd01fSIzik Eidus 
120431dbd01fSIzik Eidus 	/*
12054035c07aSHugh Dickins 	 * If the hash value of the page has changed from the last time
12064035c07aSHugh Dickins 	 * we calculated it, this page is changing frequently: therefore we
12074035c07aSHugh Dickins 	 * don't want to insert it in the unstable tree, and we don't want
12084035c07aSHugh Dickins 	 * to waste our time searching for something identical to it there.
120931dbd01fSIzik Eidus 	 */
121031dbd01fSIzik Eidus 	checksum = calc_checksum(page);
121131dbd01fSIzik Eidus 	if (rmap_item->oldchecksum != checksum) {
121231dbd01fSIzik Eidus 		rmap_item->oldchecksum = checksum;
121331dbd01fSIzik Eidus 		return;
121431dbd01fSIzik Eidus 	}
121531dbd01fSIzik Eidus 
12168dd3557aSHugh Dickins 	tree_rmap_item =
12178dd3557aSHugh Dickins 		unstable_tree_search_insert(rmap_item, page, &tree_page);
121831dbd01fSIzik Eidus 	if (tree_rmap_item) {
12198dd3557aSHugh Dickins 		kpage = try_to_merge_two_pages(rmap_item, page,
12208dd3557aSHugh Dickins 						tree_rmap_item, tree_page);
12218dd3557aSHugh Dickins 		put_page(tree_page);
122231dbd01fSIzik Eidus 		/*
122331dbd01fSIzik Eidus 		 * As soon as we merge this page, we want to remove the
122431dbd01fSIzik Eidus 		 * rmap_item of the page we have merged with from the unstable
122531dbd01fSIzik Eidus 		 * tree, and insert it instead as new node in the stable tree.
122631dbd01fSIzik Eidus 		 */
12278dd3557aSHugh Dickins 		if (kpage) {
122893d17715SHugh Dickins 			remove_rmap_item_from_tree(tree_rmap_item);
1229473b0ce4SHugh Dickins 
12305ad64688SHugh Dickins 			lock_page(kpage);
12317b6ba2c7SHugh Dickins 			stable_node = stable_tree_insert(kpage);
12327b6ba2c7SHugh Dickins 			if (stable_node) {
12337b6ba2c7SHugh Dickins 				stable_tree_append(tree_rmap_item, stable_node);
12347b6ba2c7SHugh Dickins 				stable_tree_append(rmap_item, stable_node);
12357b6ba2c7SHugh Dickins 			}
12365ad64688SHugh Dickins 			unlock_page(kpage);
12377b6ba2c7SHugh Dickins 
123831dbd01fSIzik Eidus 			/*
123931dbd01fSIzik Eidus 			 * If we fail to insert the page into the stable tree,
124031dbd01fSIzik Eidus 			 * we will have 2 virtual addresses that are pointing
124131dbd01fSIzik Eidus 			 * to a ksm page left outside the stable tree,
124231dbd01fSIzik Eidus 			 * in which case we need to break_cow on both.
124331dbd01fSIzik Eidus 			 */
12447b6ba2c7SHugh Dickins 			if (!stable_node) {
12458dd3557aSHugh Dickins 				break_cow(tree_rmap_item);
12468dd3557aSHugh Dickins 				break_cow(rmap_item);
124731dbd01fSIzik Eidus 			}
124831dbd01fSIzik Eidus 		}
124931dbd01fSIzik Eidus 	}
125031dbd01fSIzik Eidus }
125131dbd01fSIzik Eidus 
125231dbd01fSIzik Eidus static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
12536514d511SHugh Dickins 					    struct rmap_item **rmap_list,
125431dbd01fSIzik Eidus 					    unsigned long addr)
125531dbd01fSIzik Eidus {
125631dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
125731dbd01fSIzik Eidus 
12586514d511SHugh Dickins 	while (*rmap_list) {
12596514d511SHugh Dickins 		rmap_item = *rmap_list;
126093d17715SHugh Dickins 		if ((rmap_item->address & PAGE_MASK) == addr)
126131dbd01fSIzik Eidus 			return rmap_item;
126231dbd01fSIzik Eidus 		if (rmap_item->address > addr)
126331dbd01fSIzik Eidus 			break;
12646514d511SHugh Dickins 		*rmap_list = rmap_item->rmap_list;
126531dbd01fSIzik Eidus 		remove_rmap_item_from_tree(rmap_item);
126631dbd01fSIzik Eidus 		free_rmap_item(rmap_item);
126731dbd01fSIzik Eidus 	}
126831dbd01fSIzik Eidus 
126931dbd01fSIzik Eidus 	rmap_item = alloc_rmap_item();
127031dbd01fSIzik Eidus 	if (rmap_item) {
127131dbd01fSIzik Eidus 		/* It has already been zeroed */
127231dbd01fSIzik Eidus 		rmap_item->mm = mm_slot->mm;
127331dbd01fSIzik Eidus 		rmap_item->address = addr;
12746514d511SHugh Dickins 		rmap_item->rmap_list = *rmap_list;
12756514d511SHugh Dickins 		*rmap_list = rmap_item;
127631dbd01fSIzik Eidus 	}
127731dbd01fSIzik Eidus 	return rmap_item;
127831dbd01fSIzik Eidus }
127931dbd01fSIzik Eidus 
128031dbd01fSIzik Eidus static struct rmap_item *scan_get_next_rmap_item(struct page **page)
128131dbd01fSIzik Eidus {
128231dbd01fSIzik Eidus 	struct mm_struct *mm;
128331dbd01fSIzik Eidus 	struct mm_slot *slot;
128431dbd01fSIzik Eidus 	struct vm_area_struct *vma;
128531dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
128631dbd01fSIzik Eidus 
128731dbd01fSIzik Eidus 	if (list_empty(&ksm_mm_head.mm_list))
128831dbd01fSIzik Eidus 		return NULL;
128931dbd01fSIzik Eidus 
129031dbd01fSIzik Eidus 	slot = ksm_scan.mm_slot;
129131dbd01fSIzik Eidus 	if (slot == &ksm_mm_head) {
12922919bfd0SHugh Dickins 		/*
12932919bfd0SHugh Dickins 		 * A number of pages can hang around indefinitely on per-cpu
12942919bfd0SHugh Dickins 		 * pagevecs, raised page count preventing write_protect_page
12952919bfd0SHugh Dickins 		 * from merging them.  Though it doesn't really matter much,
12962919bfd0SHugh Dickins 		 * it is puzzling to see some stuck in pages_volatile until
12972919bfd0SHugh Dickins 		 * other activity jostles them out, and they also prevented
12982919bfd0SHugh Dickins 		 * LTP's KSM test from succeeding deterministically; so drain
12992919bfd0SHugh Dickins 		 * them here (here rather than on entry to ksm_do_scan(),
13002919bfd0SHugh Dickins 		 * so we don't IPI too often when pages_to_scan is set low).
13012919bfd0SHugh Dickins 		 */
13022919bfd0SHugh Dickins 		lru_add_drain_all();
13032919bfd0SHugh Dickins 
130431dbd01fSIzik Eidus 		root_unstable_tree = RB_ROOT;
130531dbd01fSIzik Eidus 
130631dbd01fSIzik Eidus 		spin_lock(&ksm_mmlist_lock);
130731dbd01fSIzik Eidus 		slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
130831dbd01fSIzik Eidus 		ksm_scan.mm_slot = slot;
130931dbd01fSIzik Eidus 		spin_unlock(&ksm_mmlist_lock);
13102b472611SHugh Dickins 		/*
13112b472611SHugh Dickins 		 * Although we tested list_empty() above, a racing __ksm_exit
13122b472611SHugh Dickins 		 * of the last mm on the list may have removed it since then.
13132b472611SHugh Dickins 		 */
13142b472611SHugh Dickins 		if (slot == &ksm_mm_head)
13152b472611SHugh Dickins 			return NULL;
131631dbd01fSIzik Eidus next_mm:
131731dbd01fSIzik Eidus 		ksm_scan.address = 0;
13186514d511SHugh Dickins 		ksm_scan.rmap_list = &slot->rmap_list;
131931dbd01fSIzik Eidus 	}
132031dbd01fSIzik Eidus 
132131dbd01fSIzik Eidus 	mm = slot->mm;
132231dbd01fSIzik Eidus 	down_read(&mm->mmap_sem);
13239ba69294SHugh Dickins 	if (ksm_test_exit(mm))
13249ba69294SHugh Dickins 		vma = NULL;
13259ba69294SHugh Dickins 	else
13269ba69294SHugh Dickins 		vma = find_vma(mm, ksm_scan.address);
13279ba69294SHugh Dickins 
13289ba69294SHugh Dickins 	for (; vma; vma = vma->vm_next) {
132931dbd01fSIzik Eidus 		if (!(vma->vm_flags & VM_MERGEABLE))
133031dbd01fSIzik Eidus 			continue;
133131dbd01fSIzik Eidus 		if (ksm_scan.address < vma->vm_start)
133231dbd01fSIzik Eidus 			ksm_scan.address = vma->vm_start;
133331dbd01fSIzik Eidus 		if (!vma->anon_vma)
133431dbd01fSIzik Eidus 			ksm_scan.address = vma->vm_end;
133531dbd01fSIzik Eidus 
133631dbd01fSIzik Eidus 		while (ksm_scan.address < vma->vm_end) {
13379ba69294SHugh Dickins 			if (ksm_test_exit(mm))
13389ba69294SHugh Dickins 				break;
133931dbd01fSIzik Eidus 			*page = follow_page(vma, ksm_scan.address, FOLL_GET);
134021ae5b01SAndrea Arcangeli 			if (IS_ERR_OR_NULL(*page)) {
134121ae5b01SAndrea Arcangeli 				ksm_scan.address += PAGE_SIZE;
134221ae5b01SAndrea Arcangeli 				cond_resched();
134321ae5b01SAndrea Arcangeli 				continue;
134421ae5b01SAndrea Arcangeli 			}
134529ad768cSAndrea Arcangeli 			if (PageAnon(*page) ||
134629ad768cSAndrea Arcangeli 			    page_trans_compound_anon(*page)) {
134731dbd01fSIzik Eidus 				flush_anon_page(vma, *page, ksm_scan.address);
134831dbd01fSIzik Eidus 				flush_dcache_page(*page);
134931dbd01fSIzik Eidus 				rmap_item = get_next_rmap_item(slot,
13506514d511SHugh Dickins 					ksm_scan.rmap_list, ksm_scan.address);
135131dbd01fSIzik Eidus 				if (rmap_item) {
13526514d511SHugh Dickins 					ksm_scan.rmap_list =
13536514d511SHugh Dickins 							&rmap_item->rmap_list;
135431dbd01fSIzik Eidus 					ksm_scan.address += PAGE_SIZE;
135531dbd01fSIzik Eidus 				} else
135631dbd01fSIzik Eidus 					put_page(*page);
135731dbd01fSIzik Eidus 				up_read(&mm->mmap_sem);
135831dbd01fSIzik Eidus 				return rmap_item;
135931dbd01fSIzik Eidus 			}
136031dbd01fSIzik Eidus 			put_page(*page);
136131dbd01fSIzik Eidus 			ksm_scan.address += PAGE_SIZE;
136231dbd01fSIzik Eidus 			cond_resched();
136331dbd01fSIzik Eidus 		}
136431dbd01fSIzik Eidus 	}
136531dbd01fSIzik Eidus 
13669ba69294SHugh Dickins 	if (ksm_test_exit(mm)) {
13679ba69294SHugh Dickins 		ksm_scan.address = 0;
13686514d511SHugh Dickins 		ksm_scan.rmap_list = &slot->rmap_list;
13699ba69294SHugh Dickins 	}
137031dbd01fSIzik Eidus 	/*
137131dbd01fSIzik Eidus 	 * Nuke all the rmap_items that are above this current rmap:
137231dbd01fSIzik Eidus 	 * because there were no VM_MERGEABLE vmas with such addresses.
137331dbd01fSIzik Eidus 	 */
13746514d511SHugh Dickins 	remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
137531dbd01fSIzik Eidus 
137631dbd01fSIzik Eidus 	spin_lock(&ksm_mmlist_lock);
1377cd551f97SHugh Dickins 	ksm_scan.mm_slot = list_entry(slot->mm_list.next,
1378cd551f97SHugh Dickins 						struct mm_slot, mm_list);
1379cd551f97SHugh Dickins 	if (ksm_scan.address == 0) {
1380cd551f97SHugh Dickins 		/*
1381cd551f97SHugh Dickins 		 * We've completed a full scan of all vmas, holding mmap_sem
1382cd551f97SHugh Dickins 		 * throughout, and found no VM_MERGEABLE: so do the same as
1383cd551f97SHugh Dickins 		 * __ksm_exit does to remove this mm from all our lists now.
13849ba69294SHugh Dickins 		 * This applies either when cleaning up after __ksm_exit
13859ba69294SHugh Dickins 		 * (but beware: we can reach here even before __ksm_exit),
13869ba69294SHugh Dickins 		 * or when all VM_MERGEABLE areas have been unmapped (and
13879ba69294SHugh Dickins 		 * mmap_sem then protects against race with MADV_MERGEABLE).
1388cd551f97SHugh Dickins 		 */
1389*4ca3a69bSSasha Levin 		hash_del(&slot->link);
1390cd551f97SHugh Dickins 		list_del(&slot->mm_list);
13919ba69294SHugh Dickins 		spin_unlock(&ksm_mmlist_lock);
13929ba69294SHugh Dickins 
1393cd551f97SHugh Dickins 		free_mm_slot(slot);
1394cd551f97SHugh Dickins 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
13959ba69294SHugh Dickins 		up_read(&mm->mmap_sem);
13969ba69294SHugh Dickins 		mmdrop(mm);
13979ba69294SHugh Dickins 	} else {
139831dbd01fSIzik Eidus 		spin_unlock(&ksm_mmlist_lock);
1399cd551f97SHugh Dickins 		up_read(&mm->mmap_sem);
14009ba69294SHugh Dickins 	}
140131dbd01fSIzik Eidus 
140231dbd01fSIzik Eidus 	/* Repeat until we've completed scanning the whole list */
1403cd551f97SHugh Dickins 	slot = ksm_scan.mm_slot;
140431dbd01fSIzik Eidus 	if (slot != &ksm_mm_head)
140531dbd01fSIzik Eidus 		goto next_mm;
140631dbd01fSIzik Eidus 
140731dbd01fSIzik Eidus 	ksm_scan.seqnr++;
140831dbd01fSIzik Eidus 	return NULL;
140931dbd01fSIzik Eidus }
141031dbd01fSIzik Eidus 
141131dbd01fSIzik Eidus /**
141231dbd01fSIzik Eidus  * ksm_do_scan  - the ksm scanner main worker function.
141331dbd01fSIzik Eidus  * @scan_npages - number of pages we want to scan before we return.
141431dbd01fSIzik Eidus  */
141531dbd01fSIzik Eidus static void ksm_do_scan(unsigned int scan_npages)
141631dbd01fSIzik Eidus {
141731dbd01fSIzik Eidus 	struct rmap_item *rmap_item;
141822eccdd7SDan Carpenter 	struct page *uninitialized_var(page);
141931dbd01fSIzik Eidus 
1420878aee7dSAndrea Arcangeli 	while (scan_npages-- && likely(!freezing(current))) {
142131dbd01fSIzik Eidus 		cond_resched();
142231dbd01fSIzik Eidus 		rmap_item = scan_get_next_rmap_item(&page);
142331dbd01fSIzik Eidus 		if (!rmap_item)
142431dbd01fSIzik Eidus 			return;
142531dbd01fSIzik Eidus 		if (!PageKsm(page) || !in_stable_tree(rmap_item))
142631dbd01fSIzik Eidus 			cmp_and_merge_page(page, rmap_item);
142731dbd01fSIzik Eidus 		put_page(page);
142831dbd01fSIzik Eidus 	}
142931dbd01fSIzik Eidus }
143031dbd01fSIzik Eidus 
14316e158384SHugh Dickins static int ksmd_should_run(void)
14326e158384SHugh Dickins {
14336e158384SHugh Dickins 	return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
14346e158384SHugh Dickins }
14356e158384SHugh Dickins 
143631dbd01fSIzik Eidus static int ksm_scan_thread(void *nothing)
143731dbd01fSIzik Eidus {
1438878aee7dSAndrea Arcangeli 	set_freezable();
1439339aa624SIzik Eidus 	set_user_nice(current, 5);
144031dbd01fSIzik Eidus 
144131dbd01fSIzik Eidus 	while (!kthread_should_stop()) {
144231dbd01fSIzik Eidus 		mutex_lock(&ksm_thread_mutex);
14436e158384SHugh Dickins 		if (ksmd_should_run())
144431dbd01fSIzik Eidus 			ksm_do_scan(ksm_thread_pages_to_scan);
144531dbd01fSIzik Eidus 		mutex_unlock(&ksm_thread_mutex);
14466e158384SHugh Dickins 
1447878aee7dSAndrea Arcangeli 		try_to_freeze();
1448878aee7dSAndrea Arcangeli 
14496e158384SHugh Dickins 		if (ksmd_should_run()) {
145031dbd01fSIzik Eidus 			schedule_timeout_interruptible(
145131dbd01fSIzik Eidus 				msecs_to_jiffies(ksm_thread_sleep_millisecs));
145231dbd01fSIzik Eidus 		} else {
1453878aee7dSAndrea Arcangeli 			wait_event_freezable(ksm_thread_wait,
14546e158384SHugh Dickins 				ksmd_should_run() || kthread_should_stop());
145531dbd01fSIzik Eidus 		}
145631dbd01fSIzik Eidus 	}
145731dbd01fSIzik Eidus 	return 0;
145831dbd01fSIzik Eidus }
145931dbd01fSIzik Eidus 
1460f8af4da3SHugh Dickins int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
1461f8af4da3SHugh Dickins 		unsigned long end, int advice, unsigned long *vm_flags)
1462f8af4da3SHugh Dickins {
1463f8af4da3SHugh Dickins 	struct mm_struct *mm = vma->vm_mm;
1464d952b791SHugh Dickins 	int err;
1465f8af4da3SHugh Dickins 
1466f8af4da3SHugh Dickins 	switch (advice) {
1467f8af4da3SHugh Dickins 	case MADV_MERGEABLE:
1468f8af4da3SHugh Dickins 		/*
1469f8af4da3SHugh Dickins 		 * Be somewhat over-protective for now!
1470f8af4da3SHugh Dickins 		 */
1471f8af4da3SHugh Dickins 		if (*vm_flags & (VM_MERGEABLE | VM_SHARED  | VM_MAYSHARE   |
1472f8af4da3SHugh Dickins 				 VM_PFNMAP    | VM_IO      | VM_DONTEXPAND |
1473314e51b9SKonstantin Khlebnikov 				 VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP))
1474f8af4da3SHugh Dickins 			return 0;		/* just ignore the advice */
1475f8af4da3SHugh Dickins 
1476cc2383ecSKonstantin Khlebnikov #ifdef VM_SAO
1477cc2383ecSKonstantin Khlebnikov 		if (*vm_flags & VM_SAO)
1478cc2383ecSKonstantin Khlebnikov 			return 0;
1479cc2383ecSKonstantin Khlebnikov #endif
1480cc2383ecSKonstantin Khlebnikov 
1481d952b791SHugh Dickins 		if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
1482d952b791SHugh Dickins 			err = __ksm_enter(mm);
1483d952b791SHugh Dickins 			if (err)
1484d952b791SHugh Dickins 				return err;
1485d952b791SHugh Dickins 		}
1486f8af4da3SHugh Dickins 
1487f8af4da3SHugh Dickins 		*vm_flags |= VM_MERGEABLE;
1488f8af4da3SHugh Dickins 		break;
1489f8af4da3SHugh Dickins 
1490f8af4da3SHugh Dickins 	case MADV_UNMERGEABLE:
1491f8af4da3SHugh Dickins 		if (!(*vm_flags & VM_MERGEABLE))
1492f8af4da3SHugh Dickins 			return 0;		/* just ignore the advice */
1493f8af4da3SHugh Dickins 
1494d952b791SHugh Dickins 		if (vma->anon_vma) {
1495d952b791SHugh Dickins 			err = unmerge_ksm_pages(vma, start, end);
1496d952b791SHugh Dickins 			if (err)
1497d952b791SHugh Dickins 				return err;
1498d952b791SHugh Dickins 		}
1499f8af4da3SHugh Dickins 
1500f8af4da3SHugh Dickins 		*vm_flags &= ~VM_MERGEABLE;
1501f8af4da3SHugh Dickins 		break;
1502f8af4da3SHugh Dickins 	}
1503f8af4da3SHugh Dickins 
1504f8af4da3SHugh Dickins 	return 0;
1505f8af4da3SHugh Dickins }
1506f8af4da3SHugh Dickins 
1507f8af4da3SHugh Dickins int __ksm_enter(struct mm_struct *mm)
1508f8af4da3SHugh Dickins {
15096e158384SHugh Dickins 	struct mm_slot *mm_slot;
15106e158384SHugh Dickins 	int needs_wakeup;
15116e158384SHugh Dickins 
15126e158384SHugh Dickins 	mm_slot = alloc_mm_slot();
151331dbd01fSIzik Eidus 	if (!mm_slot)
151431dbd01fSIzik Eidus 		return -ENOMEM;
151531dbd01fSIzik Eidus 
15166e158384SHugh Dickins 	/* Check ksm_run too?  Would need tighter locking */
15176e158384SHugh Dickins 	needs_wakeup = list_empty(&ksm_mm_head.mm_list);
15186e158384SHugh Dickins 
151931dbd01fSIzik Eidus 	spin_lock(&ksm_mmlist_lock);
152031dbd01fSIzik Eidus 	insert_to_mm_slots_hash(mm, mm_slot);
152131dbd01fSIzik Eidus 	/*
152231dbd01fSIzik Eidus 	 * Insert just behind the scanning cursor, to let the area settle
152331dbd01fSIzik Eidus 	 * down a little; when fork is followed by immediate exec, we don't
152431dbd01fSIzik Eidus 	 * want ksmd to waste time setting up and tearing down an rmap_list.
152531dbd01fSIzik Eidus 	 */
152631dbd01fSIzik Eidus 	list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
152731dbd01fSIzik Eidus 	spin_unlock(&ksm_mmlist_lock);
152831dbd01fSIzik Eidus 
1529f8af4da3SHugh Dickins 	set_bit(MMF_VM_MERGEABLE, &mm->flags);
15309ba69294SHugh Dickins 	atomic_inc(&mm->mm_count);
15316e158384SHugh Dickins 
15326e158384SHugh Dickins 	if (needs_wakeup)
15336e158384SHugh Dickins 		wake_up_interruptible(&ksm_thread_wait);
15346e158384SHugh Dickins 
1535f8af4da3SHugh Dickins 	return 0;
1536f8af4da3SHugh Dickins }
1537f8af4da3SHugh Dickins 
15381c2fb7a4SAndrea Arcangeli void __ksm_exit(struct mm_struct *mm)
1539f8af4da3SHugh Dickins {
1540cd551f97SHugh Dickins 	struct mm_slot *mm_slot;
15419ba69294SHugh Dickins 	int easy_to_free = 0;
1542cd551f97SHugh Dickins 
154331dbd01fSIzik Eidus 	/*
15449ba69294SHugh Dickins 	 * This process is exiting: if it's straightforward (as is the
15459ba69294SHugh Dickins 	 * case when ksmd was never running), free mm_slot immediately.
15469ba69294SHugh Dickins 	 * But if it's at the cursor or has rmap_items linked to it, use
15479ba69294SHugh Dickins 	 * mmap_sem to synchronize with any break_cows before pagetables
15489ba69294SHugh Dickins 	 * are freed, and leave the mm_slot on the list for ksmd to free.
15499ba69294SHugh Dickins 	 * Beware: ksm may already have noticed it exiting and freed the slot.
155031dbd01fSIzik Eidus 	 */
15519ba69294SHugh Dickins 
1552cd551f97SHugh Dickins 	spin_lock(&ksm_mmlist_lock);
1553cd551f97SHugh Dickins 	mm_slot = get_mm_slot(mm);
15549ba69294SHugh Dickins 	if (mm_slot && ksm_scan.mm_slot != mm_slot) {
15556514d511SHugh Dickins 		if (!mm_slot->rmap_list) {
1556*4ca3a69bSSasha Levin 			hash_del(&mm_slot->link);
1557cd551f97SHugh Dickins 			list_del(&mm_slot->mm_list);
15589ba69294SHugh Dickins 			easy_to_free = 1;
15599ba69294SHugh Dickins 		} else {
15609ba69294SHugh Dickins 			list_move(&mm_slot->mm_list,
15619ba69294SHugh Dickins 				  &ksm_scan.mm_slot->mm_list);
15629ba69294SHugh Dickins 		}
15639ba69294SHugh Dickins 	}
1564cd551f97SHugh Dickins 	spin_unlock(&ksm_mmlist_lock);
1565cd551f97SHugh Dickins 
15669ba69294SHugh Dickins 	if (easy_to_free) {
1567cd551f97SHugh Dickins 		free_mm_slot(mm_slot);
1568cd551f97SHugh Dickins 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
15699ba69294SHugh Dickins 		mmdrop(mm);
15709ba69294SHugh Dickins 	} else if (mm_slot) {
15719ba69294SHugh Dickins 		down_write(&mm->mmap_sem);
15729ba69294SHugh Dickins 		up_write(&mm->mmap_sem);
15739ba69294SHugh Dickins 	}
1574f8af4da3SHugh Dickins }
157531dbd01fSIzik Eidus 
15765ad64688SHugh Dickins struct page *ksm_does_need_to_copy(struct page *page,
15775ad64688SHugh Dickins 			struct vm_area_struct *vma, unsigned long address)
15785ad64688SHugh Dickins {
15795ad64688SHugh Dickins 	struct page *new_page;
15805ad64688SHugh Dickins 
15815ad64688SHugh Dickins 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
15825ad64688SHugh Dickins 	if (new_page) {
15835ad64688SHugh Dickins 		copy_user_highpage(new_page, page, address, vma);
15845ad64688SHugh Dickins 
15855ad64688SHugh Dickins 		SetPageDirty(new_page);
15865ad64688SHugh Dickins 		__SetPageUptodate(new_page);
15875ad64688SHugh Dickins 		__set_page_locked(new_page);
15885ad64688SHugh Dickins 	}
15895ad64688SHugh Dickins 
15905ad64688SHugh Dickins 	return new_page;
15915ad64688SHugh Dickins }
15925ad64688SHugh Dickins 
15935ad64688SHugh Dickins int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg,
15945ad64688SHugh Dickins 			unsigned long *vm_flags)
15955ad64688SHugh Dickins {
15965ad64688SHugh Dickins 	struct stable_node *stable_node;
15975ad64688SHugh Dickins 	struct rmap_item *rmap_item;
15985ad64688SHugh Dickins 	struct hlist_node *hlist;
15995ad64688SHugh Dickins 	unsigned int mapcount = page_mapcount(page);
16005ad64688SHugh Dickins 	int referenced = 0;
1601db114b83SHugh Dickins 	int search_new_forks = 0;
16025ad64688SHugh Dickins 
16035ad64688SHugh Dickins 	VM_BUG_ON(!PageKsm(page));
16045ad64688SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
16055ad64688SHugh Dickins 
16065ad64688SHugh Dickins 	stable_node = page_stable_node(page);
16075ad64688SHugh Dickins 	if (!stable_node)
16085ad64688SHugh Dickins 		return 0;
1609db114b83SHugh Dickins again:
16105ad64688SHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1611db114b83SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
16125beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1613db114b83SHugh Dickins 		struct vm_area_struct *vma;
1614db114b83SHugh Dickins 
1615b6b19f25SHugh Dickins 		anon_vma_lock_read(anon_vma);
1616bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1617bf181b9fSMichel Lespinasse 					       0, ULONG_MAX) {
16185beb4930SRik van Riel 			vma = vmac->vma;
1619db114b83SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1620db114b83SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1621db114b83SHugh Dickins 				continue;
1622db114b83SHugh Dickins 			/*
1623db114b83SHugh Dickins 			 * Initially we examine only the vma which covers this
1624db114b83SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1625db114b83SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1626db114b83SHugh Dickins 			 * were forked from the original since ksmd passed.
1627db114b83SHugh Dickins 			 */
1628db114b83SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
16295ad64688SHugh Dickins 				continue;
16305ad64688SHugh Dickins 
1631db114b83SHugh Dickins 			if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
1632db114b83SHugh Dickins 				continue;
16335ad64688SHugh Dickins 
16345ad64688SHugh Dickins 			referenced += page_referenced_one(page, vma,
16355ad64688SHugh Dickins 				rmap_item->address, &mapcount, vm_flags);
1636db114b83SHugh Dickins 			if (!search_new_forks || !mapcount)
1637db114b83SHugh Dickins 				break;
1638db114b83SHugh Dickins 		}
1639b6b19f25SHugh Dickins 		anon_vma_unlock_read(anon_vma);
16405ad64688SHugh Dickins 		if (!mapcount)
16415ad64688SHugh Dickins 			goto out;
16425ad64688SHugh Dickins 	}
1643db114b83SHugh Dickins 	if (!search_new_forks++)
1644db114b83SHugh Dickins 		goto again;
16455ad64688SHugh Dickins out:
16465ad64688SHugh Dickins 	return referenced;
16475ad64688SHugh Dickins }
16485ad64688SHugh Dickins 
16495ad64688SHugh Dickins int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
16505ad64688SHugh Dickins {
16515ad64688SHugh Dickins 	struct stable_node *stable_node;
16525ad64688SHugh Dickins 	struct hlist_node *hlist;
16535ad64688SHugh Dickins 	struct rmap_item *rmap_item;
16545ad64688SHugh Dickins 	int ret = SWAP_AGAIN;
1655db114b83SHugh Dickins 	int search_new_forks = 0;
16565ad64688SHugh Dickins 
16575ad64688SHugh Dickins 	VM_BUG_ON(!PageKsm(page));
16585ad64688SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
16595ad64688SHugh Dickins 
16605ad64688SHugh Dickins 	stable_node = page_stable_node(page);
16615ad64688SHugh Dickins 	if (!stable_node)
16625ad64688SHugh Dickins 		return SWAP_FAIL;
1663db114b83SHugh Dickins again:
16645ad64688SHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1665db114b83SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
16665beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1667db114b83SHugh Dickins 		struct vm_area_struct *vma;
16685ad64688SHugh Dickins 
1669b6b19f25SHugh Dickins 		anon_vma_lock_read(anon_vma);
1670bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1671bf181b9fSMichel Lespinasse 					       0, ULONG_MAX) {
16725beb4930SRik van Riel 			vma = vmac->vma;
1673db114b83SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1674db114b83SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1675db114b83SHugh Dickins 				continue;
1676db114b83SHugh Dickins 			/*
1677db114b83SHugh Dickins 			 * Initially we examine only the vma which covers this
1678db114b83SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1679db114b83SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1680db114b83SHugh Dickins 			 * were forked from the original since ksmd passed.
1681db114b83SHugh Dickins 			 */
1682db114b83SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1683db114b83SHugh Dickins 				continue;
1684db114b83SHugh Dickins 
1685db114b83SHugh Dickins 			ret = try_to_unmap_one(page, vma,
1686db114b83SHugh Dickins 					rmap_item->address, flags);
1687db114b83SHugh Dickins 			if (ret != SWAP_AGAIN || !page_mapped(page)) {
1688b6b19f25SHugh Dickins 				anon_vma_unlock_read(anon_vma);
16895ad64688SHugh Dickins 				goto out;
16905ad64688SHugh Dickins 			}
1691db114b83SHugh Dickins 		}
1692b6b19f25SHugh Dickins 		anon_vma_unlock_read(anon_vma);
1693db114b83SHugh Dickins 	}
1694db114b83SHugh Dickins 	if (!search_new_forks++)
1695db114b83SHugh Dickins 		goto again;
16965ad64688SHugh Dickins out:
16975ad64688SHugh Dickins 	return ret;
16985ad64688SHugh Dickins }
16995ad64688SHugh Dickins 
1700e9995ef9SHugh Dickins #ifdef CONFIG_MIGRATION
1701e9995ef9SHugh Dickins int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
1702e9995ef9SHugh Dickins 		  struct vm_area_struct *, unsigned long, void *), void *arg)
1703e9995ef9SHugh Dickins {
1704e9995ef9SHugh Dickins 	struct stable_node *stable_node;
1705e9995ef9SHugh Dickins 	struct hlist_node *hlist;
1706e9995ef9SHugh Dickins 	struct rmap_item *rmap_item;
1707e9995ef9SHugh Dickins 	int ret = SWAP_AGAIN;
1708e9995ef9SHugh Dickins 	int search_new_forks = 0;
1709e9995ef9SHugh Dickins 
1710e9995ef9SHugh Dickins 	VM_BUG_ON(!PageKsm(page));
1711e9995ef9SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
1712e9995ef9SHugh Dickins 
1713e9995ef9SHugh Dickins 	stable_node = page_stable_node(page);
1714e9995ef9SHugh Dickins 	if (!stable_node)
1715e9995ef9SHugh Dickins 		return ret;
1716e9995ef9SHugh Dickins again:
1717e9995ef9SHugh Dickins 	hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
1718e9995ef9SHugh Dickins 		struct anon_vma *anon_vma = rmap_item->anon_vma;
17195beb4930SRik van Riel 		struct anon_vma_chain *vmac;
1720e9995ef9SHugh Dickins 		struct vm_area_struct *vma;
1721e9995ef9SHugh Dickins 
1722b6b19f25SHugh Dickins 		anon_vma_lock_read(anon_vma);
1723bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
1724bf181b9fSMichel Lespinasse 					       0, ULONG_MAX) {
17255beb4930SRik van Riel 			vma = vmac->vma;
1726e9995ef9SHugh Dickins 			if (rmap_item->address < vma->vm_start ||
1727e9995ef9SHugh Dickins 			    rmap_item->address >= vma->vm_end)
1728e9995ef9SHugh Dickins 				continue;
1729e9995ef9SHugh Dickins 			/*
1730e9995ef9SHugh Dickins 			 * Initially we examine only the vma which covers this
1731e9995ef9SHugh Dickins 			 * rmap_item; but later, if there is still work to do,
1732e9995ef9SHugh Dickins 			 * we examine covering vmas in other mms: in case they
1733e9995ef9SHugh Dickins 			 * were forked from the original since ksmd passed.
1734e9995ef9SHugh Dickins 			 */
1735e9995ef9SHugh Dickins 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
1736e9995ef9SHugh Dickins 				continue;
1737e9995ef9SHugh Dickins 
1738e9995ef9SHugh Dickins 			ret = rmap_one(page, vma, rmap_item->address, arg);
1739e9995ef9SHugh Dickins 			if (ret != SWAP_AGAIN) {
1740b6b19f25SHugh Dickins 				anon_vma_unlock_read(anon_vma);
1741e9995ef9SHugh Dickins 				goto out;
1742e9995ef9SHugh Dickins 			}
1743e9995ef9SHugh Dickins 		}
1744b6b19f25SHugh Dickins 		anon_vma_unlock_read(anon_vma);
1745e9995ef9SHugh Dickins 	}
1746e9995ef9SHugh Dickins 	if (!search_new_forks++)
1747e9995ef9SHugh Dickins 		goto again;
1748e9995ef9SHugh Dickins out:
1749e9995ef9SHugh Dickins 	return ret;
1750e9995ef9SHugh Dickins }
1751e9995ef9SHugh Dickins 
1752e9995ef9SHugh Dickins void ksm_migrate_page(struct page *newpage, struct page *oldpage)
1753e9995ef9SHugh Dickins {
1754e9995ef9SHugh Dickins 	struct stable_node *stable_node;
1755e9995ef9SHugh Dickins 
1756e9995ef9SHugh Dickins 	VM_BUG_ON(!PageLocked(oldpage));
1757e9995ef9SHugh Dickins 	VM_BUG_ON(!PageLocked(newpage));
1758e9995ef9SHugh Dickins 	VM_BUG_ON(newpage->mapping != oldpage->mapping);
1759e9995ef9SHugh Dickins 
1760e9995ef9SHugh Dickins 	stable_node = page_stable_node(newpage);
1761e9995ef9SHugh Dickins 	if (stable_node) {
176262b61f61SHugh Dickins 		VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage));
176362b61f61SHugh Dickins 		stable_node->kpfn = page_to_pfn(newpage);
1764e9995ef9SHugh Dickins 	}
1765e9995ef9SHugh Dickins }
1766e9995ef9SHugh Dickins #endif /* CONFIG_MIGRATION */
1767e9995ef9SHugh Dickins 
176862b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE
176962b61f61SHugh Dickins static struct stable_node *ksm_check_stable_tree(unsigned long start_pfn,
177062b61f61SHugh Dickins 						 unsigned long end_pfn)
177162b61f61SHugh Dickins {
177262b61f61SHugh Dickins 	struct rb_node *node;
177362b61f61SHugh Dickins 
177462b61f61SHugh Dickins 	for (node = rb_first(&root_stable_tree); node; node = rb_next(node)) {
177562b61f61SHugh Dickins 		struct stable_node *stable_node;
177662b61f61SHugh Dickins 
177762b61f61SHugh Dickins 		stable_node = rb_entry(node, struct stable_node, node);
177862b61f61SHugh Dickins 		if (stable_node->kpfn >= start_pfn &&
177962b61f61SHugh Dickins 		    stable_node->kpfn < end_pfn)
178062b61f61SHugh Dickins 			return stable_node;
178162b61f61SHugh Dickins 	}
178262b61f61SHugh Dickins 	return NULL;
178362b61f61SHugh Dickins }
178462b61f61SHugh Dickins 
178562b61f61SHugh Dickins static int ksm_memory_callback(struct notifier_block *self,
178662b61f61SHugh Dickins 			       unsigned long action, void *arg)
178762b61f61SHugh Dickins {
178862b61f61SHugh Dickins 	struct memory_notify *mn = arg;
178962b61f61SHugh Dickins 	struct stable_node *stable_node;
179062b61f61SHugh Dickins 
179162b61f61SHugh Dickins 	switch (action) {
179262b61f61SHugh Dickins 	case MEM_GOING_OFFLINE:
179362b61f61SHugh Dickins 		/*
179462b61f61SHugh Dickins 		 * Keep it very simple for now: just lock out ksmd and
179562b61f61SHugh Dickins 		 * MADV_UNMERGEABLE while any memory is going offline.
1796a0b0f58cSKOSAKI Motohiro 		 * mutex_lock_nested() is necessary because lockdep was alarmed
1797a0b0f58cSKOSAKI Motohiro 		 * that here we take ksm_thread_mutex inside notifier chain
1798a0b0f58cSKOSAKI Motohiro 		 * mutex, and later take notifier chain mutex inside
1799a0b0f58cSKOSAKI Motohiro 		 * ksm_thread_mutex to unlock it.   But that's safe because both
1800a0b0f58cSKOSAKI Motohiro 		 * are inside mem_hotplug_mutex.
180162b61f61SHugh Dickins 		 */
1802a0b0f58cSKOSAKI Motohiro 		mutex_lock_nested(&ksm_thread_mutex, SINGLE_DEPTH_NESTING);
180362b61f61SHugh Dickins 		break;
180462b61f61SHugh Dickins 
180562b61f61SHugh Dickins 	case MEM_OFFLINE:
180662b61f61SHugh Dickins 		/*
180762b61f61SHugh Dickins 		 * Most of the work is done by page migration; but there might
180862b61f61SHugh Dickins 		 * be a few stable_nodes left over, still pointing to struct
180962b61f61SHugh Dickins 		 * pages which have been offlined: prune those from the tree.
181062b61f61SHugh Dickins 		 */
181162b61f61SHugh Dickins 		while ((stable_node = ksm_check_stable_tree(mn->start_pfn,
181262b61f61SHugh Dickins 					mn->start_pfn + mn->nr_pages)) != NULL)
181362b61f61SHugh Dickins 			remove_node_from_stable_tree(stable_node);
181462b61f61SHugh Dickins 		/* fallthrough */
181562b61f61SHugh Dickins 
181662b61f61SHugh Dickins 	case MEM_CANCEL_OFFLINE:
181762b61f61SHugh Dickins 		mutex_unlock(&ksm_thread_mutex);
181862b61f61SHugh Dickins 		break;
181962b61f61SHugh Dickins 	}
182062b61f61SHugh Dickins 	return NOTIFY_OK;
182162b61f61SHugh Dickins }
182262b61f61SHugh Dickins #endif /* CONFIG_MEMORY_HOTREMOVE */
182362b61f61SHugh Dickins 
18242ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
18252ffd8679SHugh Dickins /*
18262ffd8679SHugh Dickins  * This all compiles without CONFIG_SYSFS, but is a waste of space.
18272ffd8679SHugh Dickins  */
18282ffd8679SHugh Dickins 
182931dbd01fSIzik Eidus #define KSM_ATTR_RO(_name) \
183031dbd01fSIzik Eidus 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
183131dbd01fSIzik Eidus #define KSM_ATTR(_name) \
183231dbd01fSIzik Eidus 	static struct kobj_attribute _name##_attr = \
183331dbd01fSIzik Eidus 		__ATTR(_name, 0644, _name##_show, _name##_store)
183431dbd01fSIzik Eidus 
183531dbd01fSIzik Eidus static ssize_t sleep_millisecs_show(struct kobject *kobj,
183631dbd01fSIzik Eidus 				    struct kobj_attribute *attr, char *buf)
183731dbd01fSIzik Eidus {
183831dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
183931dbd01fSIzik Eidus }
184031dbd01fSIzik Eidus 
184131dbd01fSIzik Eidus static ssize_t sleep_millisecs_store(struct kobject *kobj,
184231dbd01fSIzik Eidus 				     struct kobj_attribute *attr,
184331dbd01fSIzik Eidus 				     const char *buf, size_t count)
184431dbd01fSIzik Eidus {
184531dbd01fSIzik Eidus 	unsigned long msecs;
184631dbd01fSIzik Eidus 	int err;
184731dbd01fSIzik Eidus 
184831dbd01fSIzik Eidus 	err = strict_strtoul(buf, 10, &msecs);
184931dbd01fSIzik Eidus 	if (err || msecs > UINT_MAX)
185031dbd01fSIzik Eidus 		return -EINVAL;
185131dbd01fSIzik Eidus 
185231dbd01fSIzik Eidus 	ksm_thread_sleep_millisecs = msecs;
185331dbd01fSIzik Eidus 
185431dbd01fSIzik Eidus 	return count;
185531dbd01fSIzik Eidus }
185631dbd01fSIzik Eidus KSM_ATTR(sleep_millisecs);
185731dbd01fSIzik Eidus 
185831dbd01fSIzik Eidus static ssize_t pages_to_scan_show(struct kobject *kobj,
185931dbd01fSIzik Eidus 				  struct kobj_attribute *attr, char *buf)
186031dbd01fSIzik Eidus {
186131dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
186231dbd01fSIzik Eidus }
186331dbd01fSIzik Eidus 
186431dbd01fSIzik Eidus static ssize_t pages_to_scan_store(struct kobject *kobj,
186531dbd01fSIzik Eidus 				   struct kobj_attribute *attr,
186631dbd01fSIzik Eidus 				   const char *buf, size_t count)
186731dbd01fSIzik Eidus {
186831dbd01fSIzik Eidus 	int err;
186931dbd01fSIzik Eidus 	unsigned long nr_pages;
187031dbd01fSIzik Eidus 
187131dbd01fSIzik Eidus 	err = strict_strtoul(buf, 10, &nr_pages);
187231dbd01fSIzik Eidus 	if (err || nr_pages > UINT_MAX)
187331dbd01fSIzik Eidus 		return -EINVAL;
187431dbd01fSIzik Eidus 
187531dbd01fSIzik Eidus 	ksm_thread_pages_to_scan = nr_pages;
187631dbd01fSIzik Eidus 
187731dbd01fSIzik Eidus 	return count;
187831dbd01fSIzik Eidus }
187931dbd01fSIzik Eidus KSM_ATTR(pages_to_scan);
188031dbd01fSIzik Eidus 
188131dbd01fSIzik Eidus static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
188231dbd01fSIzik Eidus 			char *buf)
188331dbd01fSIzik Eidus {
188431dbd01fSIzik Eidus 	return sprintf(buf, "%u\n", ksm_run);
188531dbd01fSIzik Eidus }
188631dbd01fSIzik Eidus 
188731dbd01fSIzik Eidus static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
188831dbd01fSIzik Eidus 			 const char *buf, size_t count)
188931dbd01fSIzik Eidus {
189031dbd01fSIzik Eidus 	int err;
189131dbd01fSIzik Eidus 	unsigned long flags;
189231dbd01fSIzik Eidus 
189331dbd01fSIzik Eidus 	err = strict_strtoul(buf, 10, &flags);
189431dbd01fSIzik Eidus 	if (err || flags > UINT_MAX)
189531dbd01fSIzik Eidus 		return -EINVAL;
189631dbd01fSIzik Eidus 	if (flags > KSM_RUN_UNMERGE)
189731dbd01fSIzik Eidus 		return -EINVAL;
189831dbd01fSIzik Eidus 
189931dbd01fSIzik Eidus 	/*
190031dbd01fSIzik Eidus 	 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
190131dbd01fSIzik Eidus 	 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
1902d0f209f6SHugh Dickins 	 * breaking COW to free the pages_shared (but leaves mm_slots
1903d0f209f6SHugh Dickins 	 * on the list for when ksmd may be set running again).
190431dbd01fSIzik Eidus 	 */
190531dbd01fSIzik Eidus 
190631dbd01fSIzik Eidus 	mutex_lock(&ksm_thread_mutex);
190731dbd01fSIzik Eidus 	if (ksm_run != flags) {
190831dbd01fSIzik Eidus 		ksm_run = flags;
1909d952b791SHugh Dickins 		if (flags & KSM_RUN_UNMERGE) {
1910e1e12d2fSDavid Rientjes 			set_current_oom_origin();
1911d952b791SHugh Dickins 			err = unmerge_and_remove_all_rmap_items();
1912e1e12d2fSDavid Rientjes 			clear_current_oom_origin();
1913d952b791SHugh Dickins 			if (err) {
1914d952b791SHugh Dickins 				ksm_run = KSM_RUN_STOP;
1915d952b791SHugh Dickins 				count = err;
1916d952b791SHugh Dickins 			}
1917d952b791SHugh Dickins 		}
191831dbd01fSIzik Eidus 	}
191931dbd01fSIzik Eidus 	mutex_unlock(&ksm_thread_mutex);
192031dbd01fSIzik Eidus 
192131dbd01fSIzik Eidus 	if (flags & KSM_RUN_MERGE)
192231dbd01fSIzik Eidus 		wake_up_interruptible(&ksm_thread_wait);
192331dbd01fSIzik Eidus 
192431dbd01fSIzik Eidus 	return count;
192531dbd01fSIzik Eidus }
192631dbd01fSIzik Eidus KSM_ATTR(run);
192731dbd01fSIzik Eidus 
1928b4028260SHugh Dickins static ssize_t pages_shared_show(struct kobject *kobj,
1929b4028260SHugh Dickins 				 struct kobj_attribute *attr, char *buf)
1930b4028260SHugh Dickins {
1931b4028260SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_shared);
1932b4028260SHugh Dickins }
1933b4028260SHugh Dickins KSM_ATTR_RO(pages_shared);
1934b4028260SHugh Dickins 
1935b4028260SHugh Dickins static ssize_t pages_sharing_show(struct kobject *kobj,
1936b4028260SHugh Dickins 				  struct kobj_attribute *attr, char *buf)
1937b4028260SHugh Dickins {
1938e178dfdeSHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_sharing);
1939b4028260SHugh Dickins }
1940b4028260SHugh Dickins KSM_ATTR_RO(pages_sharing);
1941b4028260SHugh Dickins 
1942473b0ce4SHugh Dickins static ssize_t pages_unshared_show(struct kobject *kobj,
1943473b0ce4SHugh Dickins 				   struct kobj_attribute *attr, char *buf)
1944473b0ce4SHugh Dickins {
1945473b0ce4SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_pages_unshared);
1946473b0ce4SHugh Dickins }
1947473b0ce4SHugh Dickins KSM_ATTR_RO(pages_unshared);
1948473b0ce4SHugh Dickins 
1949473b0ce4SHugh Dickins static ssize_t pages_volatile_show(struct kobject *kobj,
1950473b0ce4SHugh Dickins 				   struct kobj_attribute *attr, char *buf)
1951473b0ce4SHugh Dickins {
1952473b0ce4SHugh Dickins 	long ksm_pages_volatile;
1953473b0ce4SHugh Dickins 
1954473b0ce4SHugh Dickins 	ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
1955473b0ce4SHugh Dickins 				- ksm_pages_sharing - ksm_pages_unshared;
1956473b0ce4SHugh Dickins 	/*
1957473b0ce4SHugh Dickins 	 * It was not worth any locking to calculate that statistic,
1958473b0ce4SHugh Dickins 	 * but it might therefore sometimes be negative: conceal that.
1959473b0ce4SHugh Dickins 	 */
1960473b0ce4SHugh Dickins 	if (ksm_pages_volatile < 0)
1961473b0ce4SHugh Dickins 		ksm_pages_volatile = 0;
1962473b0ce4SHugh Dickins 	return sprintf(buf, "%ld\n", ksm_pages_volatile);
1963473b0ce4SHugh Dickins }
1964473b0ce4SHugh Dickins KSM_ATTR_RO(pages_volatile);
1965473b0ce4SHugh Dickins 
1966473b0ce4SHugh Dickins static ssize_t full_scans_show(struct kobject *kobj,
1967473b0ce4SHugh Dickins 			       struct kobj_attribute *attr, char *buf)
1968473b0ce4SHugh Dickins {
1969473b0ce4SHugh Dickins 	return sprintf(buf, "%lu\n", ksm_scan.seqnr);
1970473b0ce4SHugh Dickins }
1971473b0ce4SHugh Dickins KSM_ATTR_RO(full_scans);
1972473b0ce4SHugh Dickins 
197331dbd01fSIzik Eidus static struct attribute *ksm_attrs[] = {
197431dbd01fSIzik Eidus 	&sleep_millisecs_attr.attr,
197531dbd01fSIzik Eidus 	&pages_to_scan_attr.attr,
197631dbd01fSIzik Eidus 	&run_attr.attr,
1977b4028260SHugh Dickins 	&pages_shared_attr.attr,
1978b4028260SHugh Dickins 	&pages_sharing_attr.attr,
1979473b0ce4SHugh Dickins 	&pages_unshared_attr.attr,
1980473b0ce4SHugh Dickins 	&pages_volatile_attr.attr,
1981473b0ce4SHugh Dickins 	&full_scans_attr.attr,
198231dbd01fSIzik Eidus 	NULL,
198331dbd01fSIzik Eidus };
198431dbd01fSIzik Eidus 
198531dbd01fSIzik Eidus static struct attribute_group ksm_attr_group = {
198631dbd01fSIzik Eidus 	.attrs = ksm_attrs,
198731dbd01fSIzik Eidus 	.name = "ksm",
198831dbd01fSIzik Eidus };
19892ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
199031dbd01fSIzik Eidus 
199131dbd01fSIzik Eidus static int __init ksm_init(void)
199231dbd01fSIzik Eidus {
199331dbd01fSIzik Eidus 	struct task_struct *ksm_thread;
199431dbd01fSIzik Eidus 	int err;
199531dbd01fSIzik Eidus 
199631dbd01fSIzik Eidus 	err = ksm_slab_init();
199731dbd01fSIzik Eidus 	if (err)
199831dbd01fSIzik Eidus 		goto out;
199931dbd01fSIzik Eidus 
200031dbd01fSIzik Eidus 	ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
200131dbd01fSIzik Eidus 	if (IS_ERR(ksm_thread)) {
200231dbd01fSIzik Eidus 		printk(KERN_ERR "ksm: creating kthread failed\n");
200331dbd01fSIzik Eidus 		err = PTR_ERR(ksm_thread);
2004d9f8984cSLai Jiangshan 		goto out_free;
200531dbd01fSIzik Eidus 	}
200631dbd01fSIzik Eidus 
20072ffd8679SHugh Dickins #ifdef CONFIG_SYSFS
200831dbd01fSIzik Eidus 	err = sysfs_create_group(mm_kobj, &ksm_attr_group);
200931dbd01fSIzik Eidus 	if (err) {
201031dbd01fSIzik Eidus 		printk(KERN_ERR "ksm: register sysfs failed\n");
20112ffd8679SHugh Dickins 		kthread_stop(ksm_thread);
2012d9f8984cSLai Jiangshan 		goto out_free;
201331dbd01fSIzik Eidus 	}
2014c73602adSHugh Dickins #else
2015c73602adSHugh Dickins 	ksm_run = KSM_RUN_MERGE;	/* no way for user to start it */
2016c73602adSHugh Dickins 
20172ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */
201831dbd01fSIzik Eidus 
201962b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE
202062b61f61SHugh Dickins 	/*
202162b61f61SHugh Dickins 	 * Choose a high priority since the callback takes ksm_thread_mutex:
202262b61f61SHugh Dickins 	 * later callbacks could only be taking locks which nest within that.
202362b61f61SHugh Dickins 	 */
202462b61f61SHugh Dickins 	hotplug_memory_notifier(ksm_memory_callback, 100);
202562b61f61SHugh Dickins #endif
202631dbd01fSIzik Eidus 	return 0;
202731dbd01fSIzik Eidus 
2028d9f8984cSLai Jiangshan out_free:
202931dbd01fSIzik Eidus 	ksm_slab_free();
203031dbd01fSIzik Eidus out:
203131dbd01fSIzik Eidus 	return err;
203231dbd01fSIzik Eidus }
203331dbd01fSIzik Eidus module_init(ksm_init)
2034