xref: /OK3568_Linux_fs/kernel/mm/ksm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Memory merging support.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This code enables dynamic sharing of identical pages found in different
6*4882a593Smuzhiyun  * memory areas, even if they are not shared by fork()
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2008-2009 Red Hat, Inc.
9*4882a593Smuzhiyun  * Authors:
10*4882a593Smuzhiyun  *	Izik Eidus
11*4882a593Smuzhiyun  *	Andrea Arcangeli
12*4882a593Smuzhiyun  *	Chris Wright
13*4882a593Smuzhiyun  *	Hugh Dickins
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/errno.h>
17*4882a593Smuzhiyun #include <linux/mm.h>
18*4882a593Smuzhiyun #include <linux/fs.h>
19*4882a593Smuzhiyun #include <linux/mman.h>
20*4882a593Smuzhiyun #include <linux/sched.h>
21*4882a593Smuzhiyun #include <linux/sched/mm.h>
22*4882a593Smuzhiyun #include <linux/sched/coredump.h>
23*4882a593Smuzhiyun #include <linux/rwsem.h>
24*4882a593Smuzhiyun #include <linux/pagemap.h>
25*4882a593Smuzhiyun #include <linux/rmap.h>
26*4882a593Smuzhiyun #include <linux/spinlock.h>
27*4882a593Smuzhiyun #include <linux/xxhash.h>
28*4882a593Smuzhiyun #include <linux/delay.h>
29*4882a593Smuzhiyun #include <linux/kthread.h>
30*4882a593Smuzhiyun #include <linux/wait.h>
31*4882a593Smuzhiyun #include <linux/slab.h>
32*4882a593Smuzhiyun #include <linux/rbtree.h>
33*4882a593Smuzhiyun #include <linux/memory.h>
34*4882a593Smuzhiyun #include <linux/mmu_notifier.h>
35*4882a593Smuzhiyun #include <linux/swap.h>
36*4882a593Smuzhiyun #include <linux/ksm.h>
37*4882a593Smuzhiyun #include <linux/hashtable.h>
38*4882a593Smuzhiyun #include <linux/freezer.h>
39*4882a593Smuzhiyun #include <linux/oom.h>
40*4882a593Smuzhiyun #include <linux/numa.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include <asm/tlbflush.h>
43*4882a593Smuzhiyun #include "internal.h"
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #ifdef CONFIG_NUMA
46*4882a593Smuzhiyun #define NUMA(x)		(x)
47*4882a593Smuzhiyun #define DO_NUMA(x)	do { (x); } while (0)
48*4882a593Smuzhiyun #else
49*4882a593Smuzhiyun #define NUMA(x)		(0)
50*4882a593Smuzhiyun #define DO_NUMA(x)	do { } while (0)
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  * DOC: Overview
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * A few notes about the KSM scanning process,
57*4882a593Smuzhiyun  * to make it easier to understand the data structures below:
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * In order to reduce excessive scanning, KSM sorts the memory pages by their
60*4882a593Smuzhiyun  * contents into a data structure that holds pointers to the pages' locations.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Since the contents of the pages may change at any moment, KSM cannot just
63*4882a593Smuzhiyun  * insert the pages into a normal sorted tree and expect it to find anything.
64*4882a593Smuzhiyun  * Therefore KSM uses two data structures - the stable and the unstable tree.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * The stable tree holds pointers to all the merged pages (ksm pages), sorted
67*4882a593Smuzhiyun  * by their contents.  Because each such page is write-protected, searching on
68*4882a593Smuzhiyun  * this tree is fully assured to be working (except when pages are unmapped),
69*4882a593Smuzhiyun  * and therefore this tree is called the stable tree.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * The stable tree node includes information required for reverse
72*4882a593Smuzhiyun  * mapping from a KSM page to virtual addresses that map this page.
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * In order to avoid large latencies of the rmap walks on KSM pages,
75*4882a593Smuzhiyun  * KSM maintains two types of nodes in the stable tree:
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * * the regular nodes that keep the reverse mapping structures in a
78*4882a593Smuzhiyun  *   linked list
79*4882a593Smuzhiyun  * * the "chains" that link nodes ("dups") that represent the same
80*4882a593Smuzhiyun  *   write protected memory content, but each "dup" corresponds to a
81*4882a593Smuzhiyun  *   different KSM page copy of that content
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Internally, the regular nodes, "dups" and "chains" are represented
84*4882a593Smuzhiyun  * using the same struct stable_node structure.
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * In addition to the stable tree, KSM uses a second data structure called the
87*4882a593Smuzhiyun  * unstable tree: this tree holds pointers to pages which have been found to
88*4882a593Smuzhiyun  * be "unchanged for a period of time".  The unstable tree sorts these pages
89*4882a593Smuzhiyun  * by their contents, but since they are not write-protected, KSM cannot rely
90*4882a593Smuzhiyun  * upon the unstable tree to work correctly - the unstable tree is liable to
91*4882a593Smuzhiyun  * be corrupted as its contents are modified, and so it is called unstable.
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * KSM solves this problem by several techniques:
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * 1) The unstable tree is flushed every time KSM completes scanning all
96*4882a593Smuzhiyun  *    memory areas, and then the tree is rebuilt again from the beginning.
97*4882a593Smuzhiyun  * 2) KSM will only insert into the unstable tree, pages whose hash value
98*4882a593Smuzhiyun  *    has not changed since the previous scan of all memory areas.
99*4882a593Smuzhiyun  * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
100*4882a593Smuzhiyun  *    colors of the nodes and not on their contents, assuring that even when
101*4882a593Smuzhiyun  *    the tree gets "corrupted" it won't get out of balance, so scanning time
102*4882a593Smuzhiyun  *    remains the same (also, searching and inserting nodes in an rbtree uses
103*4882a593Smuzhiyun  *    the same algorithm, so we have no overhead when we flush and rebuild).
104*4882a593Smuzhiyun  * 4) KSM never flushes the stable tree, which means that even if it were to
105*4882a593Smuzhiyun  *    take 10 attempts to find a page in the unstable tree, once it is found,
106*4882a593Smuzhiyun  *    it is secured in the stable tree.  (When we scan a new page, we first
107*4882a593Smuzhiyun  *    compare it against the stable tree, and then against the unstable tree.)
108*4882a593Smuzhiyun  *
109*4882a593Smuzhiyun  * If the merge_across_nodes tunable is unset, then KSM maintains multiple
110*4882a593Smuzhiyun  * stable trees and multiple unstable trees: one of each for each NUMA node.
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun  * struct mm_slot - ksm information per mm that is being scanned
115*4882a593Smuzhiyun  * @link: link to the mm_slots hash list
116*4882a593Smuzhiyun  * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
117*4882a593Smuzhiyun  * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
118*4882a593Smuzhiyun  * @mm: the mm that this information is valid for
119*4882a593Smuzhiyun  */
120*4882a593Smuzhiyun struct mm_slot {
121*4882a593Smuzhiyun 	struct hlist_node link;
122*4882a593Smuzhiyun 	struct list_head mm_list;
123*4882a593Smuzhiyun 	struct rmap_item *rmap_list;
124*4882a593Smuzhiyun 	struct mm_struct *mm;
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /**
128*4882a593Smuzhiyun  * struct ksm_scan - cursor for scanning
129*4882a593Smuzhiyun  * @mm_slot: the current mm_slot we are scanning
130*4882a593Smuzhiyun  * @address: the next address inside that to be scanned
131*4882a593Smuzhiyun  * @rmap_list: link to the next rmap to be scanned in the rmap_list
132*4882a593Smuzhiyun  * @seqnr: count of completed full scans (needed when removing unstable node)
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * There is only the one ksm_scan instance of this cursor structure.
135*4882a593Smuzhiyun  */
136*4882a593Smuzhiyun struct ksm_scan {
137*4882a593Smuzhiyun 	struct mm_slot *mm_slot;
138*4882a593Smuzhiyun 	unsigned long address;
139*4882a593Smuzhiyun 	struct rmap_item **rmap_list;
140*4882a593Smuzhiyun 	unsigned long seqnr;
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun  * struct stable_node - node of the stable rbtree
145*4882a593Smuzhiyun  * @node: rb node of this ksm page in the stable tree
146*4882a593Smuzhiyun  * @head: (overlaying parent) &migrate_nodes indicates temporarily on that list
147*4882a593Smuzhiyun  * @hlist_dup: linked into the stable_node->hlist with a stable_node chain
148*4882a593Smuzhiyun  * @list: linked into migrate_nodes, pending placement in the proper node tree
149*4882a593Smuzhiyun  * @hlist: hlist head of rmap_items using this ksm page
150*4882a593Smuzhiyun  * @kpfn: page frame number of this ksm page (perhaps temporarily on wrong nid)
151*4882a593Smuzhiyun  * @chain_prune_time: time of the last full garbage collection
152*4882a593Smuzhiyun  * @rmap_hlist_len: number of rmap_item entries in hlist or STABLE_NODE_CHAIN
153*4882a593Smuzhiyun  * @nid: NUMA node id of stable tree in which linked (may not match kpfn)
154*4882a593Smuzhiyun  */
155*4882a593Smuzhiyun struct stable_node {
156*4882a593Smuzhiyun 	union {
157*4882a593Smuzhiyun 		struct rb_node node;	/* when node of stable tree */
158*4882a593Smuzhiyun 		struct {		/* when listed for migration */
159*4882a593Smuzhiyun 			struct list_head *head;
160*4882a593Smuzhiyun 			struct {
161*4882a593Smuzhiyun 				struct hlist_node hlist_dup;
162*4882a593Smuzhiyun 				struct list_head list;
163*4882a593Smuzhiyun 			};
164*4882a593Smuzhiyun 		};
165*4882a593Smuzhiyun 	};
166*4882a593Smuzhiyun 	struct hlist_head hlist;
167*4882a593Smuzhiyun 	union {
168*4882a593Smuzhiyun 		unsigned long kpfn;
169*4882a593Smuzhiyun 		unsigned long chain_prune_time;
170*4882a593Smuzhiyun 	};
171*4882a593Smuzhiyun 	/*
172*4882a593Smuzhiyun 	 * STABLE_NODE_CHAIN can be any negative number in
173*4882a593Smuzhiyun 	 * rmap_hlist_len negative range, but better not -1 to be able
174*4882a593Smuzhiyun 	 * to reliably detect underflows.
175*4882a593Smuzhiyun 	 */
176*4882a593Smuzhiyun #define STABLE_NODE_CHAIN -1024
177*4882a593Smuzhiyun 	int rmap_hlist_len;
178*4882a593Smuzhiyun #ifdef CONFIG_NUMA
179*4882a593Smuzhiyun 	int nid;
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /**
184*4882a593Smuzhiyun  * struct rmap_item - reverse mapping item for virtual addresses
185*4882a593Smuzhiyun  * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
186*4882a593Smuzhiyun  * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
187*4882a593Smuzhiyun  * @nid: NUMA node id of unstable tree in which linked (may not match page)
188*4882a593Smuzhiyun  * @mm: the memory structure this rmap_item is pointing into
189*4882a593Smuzhiyun  * @address: the virtual address this rmap_item tracks (+ flags in low bits)
190*4882a593Smuzhiyun  * @oldchecksum: previous checksum of the page at that virtual address
191*4882a593Smuzhiyun  * @node: rb node of this rmap_item in the unstable tree
192*4882a593Smuzhiyun  * @head: pointer to stable_node heading this list in the stable tree
193*4882a593Smuzhiyun  * @hlist: link into hlist of rmap_items hanging off that stable_node
194*4882a593Smuzhiyun  */
195*4882a593Smuzhiyun struct rmap_item {
196*4882a593Smuzhiyun 	struct rmap_item *rmap_list;
197*4882a593Smuzhiyun 	union {
198*4882a593Smuzhiyun 		struct anon_vma *anon_vma;	/* when stable */
199*4882a593Smuzhiyun #ifdef CONFIG_NUMA
200*4882a593Smuzhiyun 		int nid;		/* when node of unstable tree */
201*4882a593Smuzhiyun #endif
202*4882a593Smuzhiyun 	};
203*4882a593Smuzhiyun 	struct mm_struct *mm;
204*4882a593Smuzhiyun 	unsigned long address;		/* + low bits used for flags below */
205*4882a593Smuzhiyun 	unsigned int oldchecksum;	/* when unstable */
206*4882a593Smuzhiyun 	union {
207*4882a593Smuzhiyun 		struct rb_node node;	/* when node of unstable tree */
208*4882a593Smuzhiyun 		struct {		/* when listed from stable tree */
209*4882a593Smuzhiyun 			struct stable_node *head;
210*4882a593Smuzhiyun 			struct hlist_node hlist;
211*4882a593Smuzhiyun 		};
212*4882a593Smuzhiyun 	};
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun #define SEQNR_MASK	0x0ff	/* low bits of unstable tree seqnr */
216*4882a593Smuzhiyun #define UNSTABLE_FLAG	0x100	/* is a node of the unstable tree */
217*4882a593Smuzhiyun #define STABLE_FLAG	0x200	/* is listed from the stable tree */
218*4882a593Smuzhiyun #define KSM_FLAG_MASK	(SEQNR_MASK|UNSTABLE_FLAG|STABLE_FLAG)
219*4882a593Smuzhiyun 				/* to mask all the flags */
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /* The stable and unstable tree heads */
222*4882a593Smuzhiyun static struct rb_root one_stable_tree[1] = { RB_ROOT };
223*4882a593Smuzhiyun static struct rb_root one_unstable_tree[1] = { RB_ROOT };
224*4882a593Smuzhiyun static struct rb_root *root_stable_tree = one_stable_tree;
225*4882a593Smuzhiyun static struct rb_root *root_unstable_tree = one_unstable_tree;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /* Recently migrated nodes of stable tree, pending proper placement */
228*4882a593Smuzhiyun static LIST_HEAD(migrate_nodes);
229*4882a593Smuzhiyun #define STABLE_NODE_DUP_HEAD ((struct list_head *)&migrate_nodes.prev)
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun #define MM_SLOTS_HASH_BITS 10
232*4882a593Smuzhiyun static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun static struct mm_slot ksm_mm_head = {
235*4882a593Smuzhiyun 	.mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun static struct ksm_scan ksm_scan = {
238*4882a593Smuzhiyun 	.mm_slot = &ksm_mm_head,
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun static struct kmem_cache *rmap_item_cache;
242*4882a593Smuzhiyun static struct kmem_cache *stable_node_cache;
243*4882a593Smuzhiyun static struct kmem_cache *mm_slot_cache;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /* The number of nodes in the stable tree */
246*4882a593Smuzhiyun static unsigned long ksm_pages_shared;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /* The number of page slots additionally sharing those nodes */
249*4882a593Smuzhiyun static unsigned long ksm_pages_sharing;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /* The number of nodes in the unstable tree */
252*4882a593Smuzhiyun static unsigned long ksm_pages_unshared;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun /* The number of rmap_items in use: to calculate pages_volatile */
255*4882a593Smuzhiyun static unsigned long ksm_rmap_items;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /* The number of stable_node chains */
258*4882a593Smuzhiyun static unsigned long ksm_stable_node_chains;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun /* The number of stable_node dups linked to the stable_node chains */
261*4882a593Smuzhiyun static unsigned long ksm_stable_node_dups;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun /* Delay in pruning stale stable_node_dups in the stable_node_chains */
264*4882a593Smuzhiyun static int ksm_stable_node_chains_prune_millisecs = 2000;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun /* Maximum number of page slots sharing a stable node */
267*4882a593Smuzhiyun static int ksm_max_page_sharing = 256;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /* Number of pages ksmd should scan in one batch */
270*4882a593Smuzhiyun static unsigned int ksm_thread_pages_to_scan = 100;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /* Milliseconds ksmd should sleep between batches */
273*4882a593Smuzhiyun static unsigned int ksm_thread_sleep_millisecs = 20;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /* Checksum of an empty (zeroed) page */
276*4882a593Smuzhiyun static unsigned int zero_checksum __read_mostly;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun /* Whether to merge empty (zeroed) pages with actual zero pages */
279*4882a593Smuzhiyun static bool ksm_use_zero_pages __read_mostly;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun #ifdef CONFIG_NUMA
282*4882a593Smuzhiyun /* Zeroed when merging across nodes is not allowed */
283*4882a593Smuzhiyun static unsigned int ksm_merge_across_nodes = 1;
284*4882a593Smuzhiyun static int ksm_nr_node_ids = 1;
285*4882a593Smuzhiyun #else
286*4882a593Smuzhiyun #define ksm_merge_across_nodes	1U
287*4882a593Smuzhiyun #define ksm_nr_node_ids		1
288*4882a593Smuzhiyun #endif
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun #define KSM_RUN_STOP	0
291*4882a593Smuzhiyun #define KSM_RUN_MERGE	1
292*4882a593Smuzhiyun #define KSM_RUN_UNMERGE	2
293*4882a593Smuzhiyun #define KSM_RUN_OFFLINE	4
294*4882a593Smuzhiyun static unsigned long ksm_run = KSM_RUN_STOP;
295*4882a593Smuzhiyun static void wait_while_offlining(void);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
298*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(ksm_iter_wait);
299*4882a593Smuzhiyun static DEFINE_MUTEX(ksm_thread_mutex);
300*4882a593Smuzhiyun static DEFINE_SPINLOCK(ksm_mmlist_lock);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
303*4882a593Smuzhiyun 		sizeof(struct __struct), __alignof__(struct __struct),\
304*4882a593Smuzhiyun 		(__flags), NULL)
305*4882a593Smuzhiyun 
ksm_slab_init(void)306*4882a593Smuzhiyun static int __init ksm_slab_init(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
309*4882a593Smuzhiyun 	if (!rmap_item_cache)
310*4882a593Smuzhiyun 		goto out;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
313*4882a593Smuzhiyun 	if (!stable_node_cache)
314*4882a593Smuzhiyun 		goto out_free1;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
317*4882a593Smuzhiyun 	if (!mm_slot_cache)
318*4882a593Smuzhiyun 		goto out_free2;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	return 0;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun out_free2:
323*4882a593Smuzhiyun 	kmem_cache_destroy(stable_node_cache);
324*4882a593Smuzhiyun out_free1:
325*4882a593Smuzhiyun 	kmem_cache_destroy(rmap_item_cache);
326*4882a593Smuzhiyun out:
327*4882a593Smuzhiyun 	return -ENOMEM;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
ksm_slab_free(void)330*4882a593Smuzhiyun static void __init ksm_slab_free(void)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	kmem_cache_destroy(mm_slot_cache);
333*4882a593Smuzhiyun 	kmem_cache_destroy(stable_node_cache);
334*4882a593Smuzhiyun 	kmem_cache_destroy(rmap_item_cache);
335*4882a593Smuzhiyun 	mm_slot_cache = NULL;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
is_stable_node_chain(struct stable_node * chain)338*4882a593Smuzhiyun static __always_inline bool is_stable_node_chain(struct stable_node *chain)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	return chain->rmap_hlist_len == STABLE_NODE_CHAIN;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
is_stable_node_dup(struct stable_node * dup)343*4882a593Smuzhiyun static __always_inline bool is_stable_node_dup(struct stable_node *dup)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	return dup->head == STABLE_NODE_DUP_HEAD;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun 
stable_node_chain_add_dup(struct stable_node * dup,struct stable_node * chain)348*4882a593Smuzhiyun static inline void stable_node_chain_add_dup(struct stable_node *dup,
349*4882a593Smuzhiyun 					     struct stable_node *chain)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	VM_BUG_ON(is_stable_node_dup(dup));
352*4882a593Smuzhiyun 	dup->head = STABLE_NODE_DUP_HEAD;
353*4882a593Smuzhiyun 	VM_BUG_ON(!is_stable_node_chain(chain));
354*4882a593Smuzhiyun 	hlist_add_head(&dup->hlist_dup, &chain->hlist);
355*4882a593Smuzhiyun 	ksm_stable_node_dups++;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
__stable_node_dup_del(struct stable_node * dup)358*4882a593Smuzhiyun static inline void __stable_node_dup_del(struct stable_node *dup)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	VM_BUG_ON(!is_stable_node_dup(dup));
361*4882a593Smuzhiyun 	hlist_del(&dup->hlist_dup);
362*4882a593Smuzhiyun 	ksm_stable_node_dups--;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
stable_node_dup_del(struct stable_node * dup)365*4882a593Smuzhiyun static inline void stable_node_dup_del(struct stable_node *dup)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	VM_BUG_ON(is_stable_node_chain(dup));
368*4882a593Smuzhiyun 	if (is_stable_node_dup(dup))
369*4882a593Smuzhiyun 		__stable_node_dup_del(dup);
370*4882a593Smuzhiyun 	else
371*4882a593Smuzhiyun 		rb_erase(&dup->node, root_stable_tree + NUMA(dup->nid));
372*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_VM
373*4882a593Smuzhiyun 	dup->head = NULL;
374*4882a593Smuzhiyun #endif
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
alloc_rmap_item(void)377*4882a593Smuzhiyun static inline struct rmap_item *alloc_rmap_item(void)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	struct rmap_item *rmap_item;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL |
382*4882a593Smuzhiyun 						__GFP_NORETRY | __GFP_NOWARN);
383*4882a593Smuzhiyun 	if (rmap_item)
384*4882a593Smuzhiyun 		ksm_rmap_items++;
385*4882a593Smuzhiyun 	return rmap_item;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
free_rmap_item(struct rmap_item * rmap_item)388*4882a593Smuzhiyun static inline void free_rmap_item(struct rmap_item *rmap_item)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	ksm_rmap_items--;
391*4882a593Smuzhiyun 	rmap_item->mm = NULL;	/* debug safety */
392*4882a593Smuzhiyun 	kmem_cache_free(rmap_item_cache, rmap_item);
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
alloc_stable_node(void)395*4882a593Smuzhiyun static inline struct stable_node *alloc_stable_node(void)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun 	/*
398*4882a593Smuzhiyun 	 * The allocation can take too long with GFP_KERNEL when memory is under
399*4882a593Smuzhiyun 	 * pressure, which may lead to hung task warnings.  Adding __GFP_HIGH
400*4882a593Smuzhiyun 	 * grants access to memory reserves, helping to avoid this problem.
401*4882a593Smuzhiyun 	 */
402*4882a593Smuzhiyun 	return kmem_cache_alloc(stable_node_cache, GFP_KERNEL | __GFP_HIGH);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
free_stable_node(struct stable_node * stable_node)405*4882a593Smuzhiyun static inline void free_stable_node(struct stable_node *stable_node)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	VM_BUG_ON(stable_node->rmap_hlist_len &&
408*4882a593Smuzhiyun 		  !is_stable_node_chain(stable_node));
409*4882a593Smuzhiyun 	kmem_cache_free(stable_node_cache, stable_node);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
alloc_mm_slot(void)412*4882a593Smuzhiyun static inline struct mm_slot *alloc_mm_slot(void)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	if (!mm_slot_cache)	/* initialization failed */
415*4882a593Smuzhiyun 		return NULL;
416*4882a593Smuzhiyun 	return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
free_mm_slot(struct mm_slot * mm_slot)419*4882a593Smuzhiyun static inline void free_mm_slot(struct mm_slot *mm_slot)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	kmem_cache_free(mm_slot_cache, mm_slot);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
get_mm_slot(struct mm_struct * mm)424*4882a593Smuzhiyun static struct mm_slot *get_mm_slot(struct mm_struct *mm)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	struct mm_slot *slot;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	hash_for_each_possible(mm_slots_hash, slot, link, (unsigned long)mm)
429*4882a593Smuzhiyun 		if (slot->mm == mm)
430*4882a593Smuzhiyun 			return slot;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	return NULL;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun 
insert_to_mm_slots_hash(struct mm_struct * mm,struct mm_slot * mm_slot)435*4882a593Smuzhiyun static void insert_to_mm_slots_hash(struct mm_struct *mm,
436*4882a593Smuzhiyun 				    struct mm_slot *mm_slot)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	mm_slot->mm = mm;
439*4882a593Smuzhiyun 	hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm);
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun  * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
444*4882a593Smuzhiyun  * page tables after it has passed through ksm_exit() - which, if necessary,
445*4882a593Smuzhiyun  * takes mmap_lock briefly to serialize against them.  ksm_exit() does not set
446*4882a593Smuzhiyun  * a special flag: they can just back out as soon as mm_users goes to zero.
447*4882a593Smuzhiyun  * ksm_test_exit() is used throughout to make this test for exit: in some
448*4882a593Smuzhiyun  * places for correctness, in some places just to avoid unnecessary work.
449*4882a593Smuzhiyun  */
ksm_test_exit(struct mm_struct * mm)450*4882a593Smuzhiyun static inline bool ksm_test_exit(struct mm_struct *mm)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	return atomic_read(&mm->mm_users) == 0;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun  * We use break_ksm to break COW on a ksm page: it's a stripped down
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  *	if (get_user_pages(addr, 1, FOLL_WRITE, &page, NULL) == 1)
459*4882a593Smuzhiyun  *		put_page(page);
460*4882a593Smuzhiyun  *
461*4882a593Smuzhiyun  * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
462*4882a593Smuzhiyun  * in case the application has unmapped and remapped mm,addr meanwhile.
463*4882a593Smuzhiyun  * Could a ksm page appear anywhere else?  Actually yes, in a VM_PFNMAP
464*4882a593Smuzhiyun  * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
465*4882a593Smuzhiyun  *
466*4882a593Smuzhiyun  * FAULT_FLAG/FOLL_REMOTE are because we do this outside the context
467*4882a593Smuzhiyun  * of the process that owns 'vma'.  We also do not want to enforce
468*4882a593Smuzhiyun  * protection keys here anyway.
469*4882a593Smuzhiyun  */
break_ksm(struct vm_area_struct * vma,unsigned long addr)470*4882a593Smuzhiyun static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	struct page *page;
473*4882a593Smuzhiyun 	vm_fault_t ret = 0;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	do {
476*4882a593Smuzhiyun 		cond_resched();
477*4882a593Smuzhiyun 		page = follow_page(vma, addr,
478*4882a593Smuzhiyun 				FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE);
479*4882a593Smuzhiyun 		if (IS_ERR_OR_NULL(page))
480*4882a593Smuzhiyun 			break;
481*4882a593Smuzhiyun 		if (PageKsm(page))
482*4882a593Smuzhiyun 			ret = handle_mm_fault(vma, addr,
483*4882a593Smuzhiyun 					      FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE,
484*4882a593Smuzhiyun 					      NULL);
485*4882a593Smuzhiyun 		else
486*4882a593Smuzhiyun 			ret = VM_FAULT_WRITE;
487*4882a593Smuzhiyun 		put_user_page(page);
488*4882a593Smuzhiyun 	} while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | VM_FAULT_OOM)));
489*4882a593Smuzhiyun 	/*
490*4882a593Smuzhiyun 	 * We must loop because handle_mm_fault() may back out if there's
491*4882a593Smuzhiyun 	 * any difficulty e.g. if pte accessed bit gets updated concurrently.
492*4882a593Smuzhiyun 	 *
493*4882a593Smuzhiyun 	 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
494*4882a593Smuzhiyun 	 * COW has been broken, even if the vma does not permit VM_WRITE;
495*4882a593Smuzhiyun 	 * but note that a concurrent fault might break PageKsm for us.
496*4882a593Smuzhiyun 	 *
497*4882a593Smuzhiyun 	 * VM_FAULT_SIGBUS could occur if we race with truncation of the
498*4882a593Smuzhiyun 	 * backing file, which also invalidates anonymous pages: that's
499*4882a593Smuzhiyun 	 * okay, that truncation will have unmapped the PageKsm for us.
500*4882a593Smuzhiyun 	 *
501*4882a593Smuzhiyun 	 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
502*4882a593Smuzhiyun 	 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
503*4882a593Smuzhiyun 	 * current task has TIF_MEMDIE set, and will be OOM killed on return
504*4882a593Smuzhiyun 	 * to user; and ksmd, having no mm, would never be chosen for that.
505*4882a593Smuzhiyun 	 *
506*4882a593Smuzhiyun 	 * But if the mm is in a limited mem_cgroup, then the fault may fail
507*4882a593Smuzhiyun 	 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
508*4882a593Smuzhiyun 	 * even ksmd can fail in this way - though it's usually breaking ksm
509*4882a593Smuzhiyun 	 * just to undo a merge it made a moment before, so unlikely to oom.
510*4882a593Smuzhiyun 	 *
511*4882a593Smuzhiyun 	 * That's a pity: we might therefore have more kernel pages allocated
512*4882a593Smuzhiyun 	 * than we're counting as nodes in the stable tree; but ksm_do_scan
513*4882a593Smuzhiyun 	 * will retry to break_cow on each pass, so should recover the page
514*4882a593Smuzhiyun 	 * in due course.  The important thing is to not let VM_MERGEABLE
515*4882a593Smuzhiyun 	 * be cleared while any such pages might remain in the area.
516*4882a593Smuzhiyun 	 */
517*4882a593Smuzhiyun 	return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun 
find_mergeable_vma(struct mm_struct * mm,unsigned long addr)520*4882a593Smuzhiyun static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm,
521*4882a593Smuzhiyun 		unsigned long addr)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	struct vm_area_struct *vma;
524*4882a593Smuzhiyun 	if (ksm_test_exit(mm))
525*4882a593Smuzhiyun 		return NULL;
526*4882a593Smuzhiyun 	vma = find_vma(mm, addr);
527*4882a593Smuzhiyun 	if (!vma || vma->vm_start > addr)
528*4882a593Smuzhiyun 		return NULL;
529*4882a593Smuzhiyun 	if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
530*4882a593Smuzhiyun 		return NULL;
531*4882a593Smuzhiyun 	return vma;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
break_cow(struct rmap_item * rmap_item)534*4882a593Smuzhiyun static void break_cow(struct rmap_item *rmap_item)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	struct mm_struct *mm = rmap_item->mm;
537*4882a593Smuzhiyun 	unsigned long addr = rmap_item->address;
538*4882a593Smuzhiyun 	struct vm_area_struct *vma;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/*
541*4882a593Smuzhiyun 	 * It is not an accident that whenever we want to break COW
542*4882a593Smuzhiyun 	 * to undo, we also need to drop a reference to the anon_vma.
543*4882a593Smuzhiyun 	 */
544*4882a593Smuzhiyun 	put_anon_vma(rmap_item->anon_vma);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	mmap_read_lock(mm);
547*4882a593Smuzhiyun 	vma = find_mergeable_vma(mm, addr);
548*4882a593Smuzhiyun 	if (vma)
549*4882a593Smuzhiyun 		break_ksm(vma, addr);
550*4882a593Smuzhiyun 	mmap_read_unlock(mm);
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
get_mergeable_page(struct rmap_item * rmap_item)553*4882a593Smuzhiyun static struct page *get_mergeable_page(struct rmap_item *rmap_item)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	struct mm_struct *mm = rmap_item->mm;
556*4882a593Smuzhiyun 	unsigned long addr = rmap_item->address;
557*4882a593Smuzhiyun 	struct vm_area_struct *vma;
558*4882a593Smuzhiyun 	struct page *page;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	mmap_read_lock(mm);
561*4882a593Smuzhiyun 	vma = find_mergeable_vma(mm, addr);
562*4882a593Smuzhiyun 	if (!vma)
563*4882a593Smuzhiyun 		goto out;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	page = follow_page(vma, addr, FOLL_GET);
566*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(page))
567*4882a593Smuzhiyun 		goto out;
568*4882a593Smuzhiyun 	if (PageAnon(page)) {
569*4882a593Smuzhiyun 		flush_anon_page(vma, page, addr);
570*4882a593Smuzhiyun 		flush_dcache_page(page);
571*4882a593Smuzhiyun 	} else {
572*4882a593Smuzhiyun 		put_user_page(page);
573*4882a593Smuzhiyun out:
574*4882a593Smuzhiyun 		page = NULL;
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 	mmap_read_unlock(mm);
577*4882a593Smuzhiyun 	return page;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun /*
581*4882a593Smuzhiyun  * This helper is used for getting right index into array of tree roots.
582*4882a593Smuzhiyun  * When merge_across_nodes knob is set to 1, there are only two rb-trees for
583*4882a593Smuzhiyun  * stable and unstable pages from all nodes with roots in index 0. Otherwise,
584*4882a593Smuzhiyun  * every node has its own stable and unstable tree.
585*4882a593Smuzhiyun  */
get_kpfn_nid(unsigned long kpfn)586*4882a593Smuzhiyun static inline int get_kpfn_nid(unsigned long kpfn)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun 	return ksm_merge_across_nodes ? 0 : NUMA(pfn_to_nid(kpfn));
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun 
alloc_stable_node_chain(struct stable_node * dup,struct rb_root * root)591*4882a593Smuzhiyun static struct stable_node *alloc_stable_node_chain(struct stable_node *dup,
592*4882a593Smuzhiyun 						   struct rb_root *root)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	struct stable_node *chain = alloc_stable_node();
595*4882a593Smuzhiyun 	VM_BUG_ON(is_stable_node_chain(dup));
596*4882a593Smuzhiyun 	if (likely(chain)) {
597*4882a593Smuzhiyun 		INIT_HLIST_HEAD(&chain->hlist);
598*4882a593Smuzhiyun 		chain->chain_prune_time = jiffies;
599*4882a593Smuzhiyun 		chain->rmap_hlist_len = STABLE_NODE_CHAIN;
600*4882a593Smuzhiyun #if defined (CONFIG_DEBUG_VM) && defined(CONFIG_NUMA)
601*4882a593Smuzhiyun 		chain->nid = NUMA_NO_NODE; /* debug */
602*4882a593Smuzhiyun #endif
603*4882a593Smuzhiyun 		ksm_stable_node_chains++;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 		/*
606*4882a593Smuzhiyun 		 * Put the stable node chain in the first dimension of
607*4882a593Smuzhiyun 		 * the stable tree and at the same time remove the old
608*4882a593Smuzhiyun 		 * stable node.
609*4882a593Smuzhiyun 		 */
610*4882a593Smuzhiyun 		rb_replace_node(&dup->node, &chain->node, root);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 		/*
613*4882a593Smuzhiyun 		 * Move the old stable node to the second dimension
614*4882a593Smuzhiyun 		 * queued in the hlist_dup. The invariant is that all
615*4882a593Smuzhiyun 		 * dup stable_nodes in the chain->hlist point to pages
616*4882a593Smuzhiyun 		 * that are write protected and have the exact same
617*4882a593Smuzhiyun 		 * content.
618*4882a593Smuzhiyun 		 */
619*4882a593Smuzhiyun 		stable_node_chain_add_dup(dup, chain);
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 	return chain;
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun 
free_stable_node_chain(struct stable_node * chain,struct rb_root * root)624*4882a593Smuzhiyun static inline void free_stable_node_chain(struct stable_node *chain,
625*4882a593Smuzhiyun 					  struct rb_root *root)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	rb_erase(&chain->node, root);
628*4882a593Smuzhiyun 	free_stable_node(chain);
629*4882a593Smuzhiyun 	ksm_stable_node_chains--;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
remove_node_from_stable_tree(struct stable_node * stable_node)632*4882a593Smuzhiyun static void remove_node_from_stable_tree(struct stable_node *stable_node)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	struct rmap_item *rmap_item;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/* check it's not STABLE_NODE_CHAIN or negative */
637*4882a593Smuzhiyun 	BUG_ON(stable_node->rmap_hlist_len < 0);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
640*4882a593Smuzhiyun 		if (rmap_item->hlist.next)
641*4882a593Smuzhiyun 			ksm_pages_sharing--;
642*4882a593Smuzhiyun 		else
643*4882a593Smuzhiyun 			ksm_pages_shared--;
644*4882a593Smuzhiyun 		VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
645*4882a593Smuzhiyun 		stable_node->rmap_hlist_len--;
646*4882a593Smuzhiyun 		put_anon_vma(rmap_item->anon_vma);
647*4882a593Smuzhiyun 		rmap_item->address &= PAGE_MASK;
648*4882a593Smuzhiyun 		cond_resched();
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/*
652*4882a593Smuzhiyun 	 * We need the second aligned pointer of the migrate_nodes
653*4882a593Smuzhiyun 	 * list_head to stay clear from the rb_parent_color union
654*4882a593Smuzhiyun 	 * (aligned and different than any node) and also different
655*4882a593Smuzhiyun 	 * from &migrate_nodes. This will verify that future list.h changes
656*4882a593Smuzhiyun 	 * don't break STABLE_NODE_DUP_HEAD. Only recent gcc can handle it.
657*4882a593Smuzhiyun 	 */
658*4882a593Smuzhiyun #if defined(GCC_VERSION) && GCC_VERSION >= 40903
659*4882a593Smuzhiyun 	BUILD_BUG_ON(STABLE_NODE_DUP_HEAD <= &migrate_nodes);
660*4882a593Smuzhiyun 	BUILD_BUG_ON(STABLE_NODE_DUP_HEAD >= &migrate_nodes + 1);
661*4882a593Smuzhiyun #endif
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (stable_node->head == &migrate_nodes)
664*4882a593Smuzhiyun 		list_del(&stable_node->list);
665*4882a593Smuzhiyun 	else
666*4882a593Smuzhiyun 		stable_node_dup_del(stable_node);
667*4882a593Smuzhiyun 	free_stable_node(stable_node);
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun enum get_ksm_page_flags {
671*4882a593Smuzhiyun 	GET_KSM_PAGE_NOLOCK,
672*4882a593Smuzhiyun 	GET_KSM_PAGE_LOCK,
673*4882a593Smuzhiyun 	GET_KSM_PAGE_TRYLOCK
674*4882a593Smuzhiyun };
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun /*
677*4882a593Smuzhiyun  * get_ksm_page: checks if the page indicated by the stable node
678*4882a593Smuzhiyun  * is still its ksm page, despite having held no reference to it.
679*4882a593Smuzhiyun  * In which case we can trust the content of the page, and it
680*4882a593Smuzhiyun  * returns the gotten page; but if the page has now been zapped,
681*4882a593Smuzhiyun  * remove the stale node from the stable tree and return NULL.
682*4882a593Smuzhiyun  * But beware, the stable node's page might be being migrated.
683*4882a593Smuzhiyun  *
684*4882a593Smuzhiyun  * You would expect the stable_node to hold a reference to the ksm page.
685*4882a593Smuzhiyun  * But if it increments the page's count, swapping out has to wait for
686*4882a593Smuzhiyun  * ksmd to come around again before it can free the page, which may take
687*4882a593Smuzhiyun  * seconds or even minutes: much too unresponsive.  So instead we use a
688*4882a593Smuzhiyun  * "keyhole reference": access to the ksm page from the stable node peeps
689*4882a593Smuzhiyun  * out through its keyhole to see if that page still holds the right key,
690*4882a593Smuzhiyun  * pointing back to this stable node.  This relies on freeing a PageAnon
691*4882a593Smuzhiyun  * page to reset its page->mapping to NULL, and relies on no other use of
692*4882a593Smuzhiyun  * a page to put something that might look like our key in page->mapping.
693*4882a593Smuzhiyun  * is on its way to being freed; but it is an anomaly to bear in mind.
694*4882a593Smuzhiyun  */
get_ksm_page(struct stable_node * stable_node,enum get_ksm_page_flags flags)695*4882a593Smuzhiyun static struct page *get_ksm_page(struct stable_node *stable_node,
696*4882a593Smuzhiyun 				 enum get_ksm_page_flags flags)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	struct page *page;
699*4882a593Smuzhiyun 	void *expected_mapping;
700*4882a593Smuzhiyun 	unsigned long kpfn;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	expected_mapping = (void *)((unsigned long)stable_node |
703*4882a593Smuzhiyun 					PAGE_MAPPING_KSM);
704*4882a593Smuzhiyun again:
705*4882a593Smuzhiyun 	kpfn = READ_ONCE(stable_node->kpfn); /* Address dependency. */
706*4882a593Smuzhiyun 	page = pfn_to_page(kpfn);
707*4882a593Smuzhiyun 	if (READ_ONCE(page->mapping) != expected_mapping)
708*4882a593Smuzhiyun 		goto stale;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/*
711*4882a593Smuzhiyun 	 * We cannot do anything with the page while its refcount is 0.
712*4882a593Smuzhiyun 	 * Usually 0 means free, or tail of a higher-order page: in which
713*4882a593Smuzhiyun 	 * case this node is no longer referenced, and should be freed;
714*4882a593Smuzhiyun 	 * however, it might mean that the page is under page_ref_freeze().
715*4882a593Smuzhiyun 	 * The __remove_mapping() case is easy, again the node is now stale;
716*4882a593Smuzhiyun 	 * the same is in reuse_ksm_page() case; but if page is swapcache
717*4882a593Smuzhiyun 	 * in migrate_page_move_mapping(), it might still be our page,
718*4882a593Smuzhiyun 	 * in which case it's essential to keep the node.
719*4882a593Smuzhiyun 	 */
720*4882a593Smuzhiyun 	while (!get_page_unless_zero(page)) {
721*4882a593Smuzhiyun 		/*
722*4882a593Smuzhiyun 		 * Another check for page->mapping != expected_mapping would
723*4882a593Smuzhiyun 		 * work here too.  We have chosen the !PageSwapCache test to
724*4882a593Smuzhiyun 		 * optimize the common case, when the page is or is about to
725*4882a593Smuzhiyun 		 * be freed: PageSwapCache is cleared (under spin_lock_irq)
726*4882a593Smuzhiyun 		 * in the ref_freeze section of __remove_mapping(); but Anon
727*4882a593Smuzhiyun 		 * page->mapping reset to NULL later, in free_pages_prepare().
728*4882a593Smuzhiyun 		 */
729*4882a593Smuzhiyun 		if (!PageSwapCache(page))
730*4882a593Smuzhiyun 			goto stale;
731*4882a593Smuzhiyun 		cpu_relax();
732*4882a593Smuzhiyun 	}
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	if (READ_ONCE(page->mapping) != expected_mapping) {
735*4882a593Smuzhiyun 		put_page(page);
736*4882a593Smuzhiyun 		goto stale;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	if (flags == GET_KSM_PAGE_TRYLOCK) {
740*4882a593Smuzhiyun 		if (!trylock_page(page)) {
741*4882a593Smuzhiyun 			put_page(page);
742*4882a593Smuzhiyun 			return ERR_PTR(-EBUSY);
743*4882a593Smuzhiyun 		}
744*4882a593Smuzhiyun 	} else if (flags == GET_KSM_PAGE_LOCK)
745*4882a593Smuzhiyun 		lock_page(page);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	if (flags != GET_KSM_PAGE_NOLOCK) {
748*4882a593Smuzhiyun 		if (READ_ONCE(page->mapping) != expected_mapping) {
749*4882a593Smuzhiyun 			unlock_page(page);
750*4882a593Smuzhiyun 			put_page(page);
751*4882a593Smuzhiyun 			goto stale;
752*4882a593Smuzhiyun 		}
753*4882a593Smuzhiyun 	}
754*4882a593Smuzhiyun 	return page;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun stale:
757*4882a593Smuzhiyun 	/*
758*4882a593Smuzhiyun 	 * We come here from above when page->mapping or !PageSwapCache
759*4882a593Smuzhiyun 	 * suggests that the node is stale; but it might be under migration.
760*4882a593Smuzhiyun 	 * We need smp_rmb(), matching the smp_wmb() in ksm_migrate_page(),
761*4882a593Smuzhiyun 	 * before checking whether node->kpfn has been changed.
762*4882a593Smuzhiyun 	 */
763*4882a593Smuzhiyun 	smp_rmb();
764*4882a593Smuzhiyun 	if (READ_ONCE(stable_node->kpfn) != kpfn)
765*4882a593Smuzhiyun 		goto again;
766*4882a593Smuzhiyun 	remove_node_from_stable_tree(stable_node);
767*4882a593Smuzhiyun 	return NULL;
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun /*
771*4882a593Smuzhiyun  * Removing rmap_item from stable or unstable tree.
772*4882a593Smuzhiyun  * This function will clean the information from the stable/unstable tree.
773*4882a593Smuzhiyun  */
remove_rmap_item_from_tree(struct rmap_item * rmap_item)774*4882a593Smuzhiyun static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	if (rmap_item->address & STABLE_FLAG) {
777*4882a593Smuzhiyun 		struct stable_node *stable_node;
778*4882a593Smuzhiyun 		struct page *page;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 		stable_node = rmap_item->head;
781*4882a593Smuzhiyun 		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
782*4882a593Smuzhiyun 		if (!page)
783*4882a593Smuzhiyun 			goto out;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 		hlist_del(&rmap_item->hlist);
786*4882a593Smuzhiyun 		unlock_page(page);
787*4882a593Smuzhiyun 		put_page(page);
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 		if (!hlist_empty(&stable_node->hlist))
790*4882a593Smuzhiyun 			ksm_pages_sharing--;
791*4882a593Smuzhiyun 		else
792*4882a593Smuzhiyun 			ksm_pages_shared--;
793*4882a593Smuzhiyun 		VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
794*4882a593Smuzhiyun 		stable_node->rmap_hlist_len--;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 		put_anon_vma(rmap_item->anon_vma);
797*4882a593Smuzhiyun 		rmap_item->head = NULL;
798*4882a593Smuzhiyun 		rmap_item->address &= PAGE_MASK;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	} else if (rmap_item->address & UNSTABLE_FLAG) {
801*4882a593Smuzhiyun 		unsigned char age;
802*4882a593Smuzhiyun 		/*
803*4882a593Smuzhiyun 		 * Usually ksmd can and must skip the rb_erase, because
804*4882a593Smuzhiyun 		 * root_unstable_tree was already reset to RB_ROOT.
805*4882a593Smuzhiyun 		 * But be careful when an mm is exiting: do the rb_erase
806*4882a593Smuzhiyun 		 * if this rmap_item was inserted by this scan, rather
807*4882a593Smuzhiyun 		 * than left over from before.
808*4882a593Smuzhiyun 		 */
809*4882a593Smuzhiyun 		age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
810*4882a593Smuzhiyun 		BUG_ON(age > 1);
811*4882a593Smuzhiyun 		if (!age)
812*4882a593Smuzhiyun 			rb_erase(&rmap_item->node,
813*4882a593Smuzhiyun 				 root_unstable_tree + NUMA(rmap_item->nid));
814*4882a593Smuzhiyun 		ksm_pages_unshared--;
815*4882a593Smuzhiyun 		rmap_item->address &= PAGE_MASK;
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun out:
818*4882a593Smuzhiyun 	cond_resched();		/* we're called from many long loops */
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
remove_trailing_rmap_items(struct mm_slot * mm_slot,struct rmap_item ** rmap_list)821*4882a593Smuzhiyun static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
822*4882a593Smuzhiyun 				       struct rmap_item **rmap_list)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	while (*rmap_list) {
825*4882a593Smuzhiyun 		struct rmap_item *rmap_item = *rmap_list;
826*4882a593Smuzhiyun 		*rmap_list = rmap_item->rmap_list;
827*4882a593Smuzhiyun 		remove_rmap_item_from_tree(rmap_item);
828*4882a593Smuzhiyun 		free_rmap_item(rmap_item);
829*4882a593Smuzhiyun 	}
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun /*
833*4882a593Smuzhiyun  * Though it's very tempting to unmerge rmap_items from stable tree rather
834*4882a593Smuzhiyun  * than check every pte of a given vma, the locking doesn't quite work for
835*4882a593Smuzhiyun  * that - an rmap_item is assigned to the stable tree after inserting ksm
836*4882a593Smuzhiyun  * page and upping mmap_lock.  Nor does it fit with the way we skip dup'ing
837*4882a593Smuzhiyun  * rmap_items from parent to child at fork time (so as not to waste time
838*4882a593Smuzhiyun  * if exit comes before the next scan reaches it).
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * Similarly, although we'd like to remove rmap_items (so updating counts
841*4882a593Smuzhiyun  * and freeing memory) when unmerging an area, it's easier to leave that
842*4882a593Smuzhiyun  * to the next pass of ksmd - consider, for example, how ksmd might be
843*4882a593Smuzhiyun  * in cmp_and_merge_page on one of the rmap_items we would be removing.
844*4882a593Smuzhiyun  */
unmerge_ksm_pages(struct vm_area_struct * vma,unsigned long start,unsigned long end)845*4882a593Smuzhiyun static int unmerge_ksm_pages(struct vm_area_struct *vma,
846*4882a593Smuzhiyun 			     unsigned long start, unsigned long end)
847*4882a593Smuzhiyun {
848*4882a593Smuzhiyun 	unsigned long addr;
849*4882a593Smuzhiyun 	int err = 0;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
852*4882a593Smuzhiyun 		if (ksm_test_exit(vma->vm_mm))
853*4882a593Smuzhiyun 			break;
854*4882a593Smuzhiyun 		if (signal_pending(current))
855*4882a593Smuzhiyun 			err = -ERESTARTSYS;
856*4882a593Smuzhiyun 		else
857*4882a593Smuzhiyun 			err = break_ksm(vma, addr);
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 	return err;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun 
page_stable_node(struct page * page)862*4882a593Smuzhiyun static inline struct stable_node *page_stable_node(struct page *page)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	return PageKsm(page) ? page_rmapping(page) : NULL;
865*4882a593Smuzhiyun }
866*4882a593Smuzhiyun 
set_page_stable_node(struct page * page,struct stable_node * stable_node)867*4882a593Smuzhiyun static inline void set_page_stable_node(struct page *page,
868*4882a593Smuzhiyun 					struct stable_node *stable_node)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun 	page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun #ifdef CONFIG_SYSFS
874*4882a593Smuzhiyun /*
875*4882a593Smuzhiyun  * Only called through the sysfs control interface:
876*4882a593Smuzhiyun  */
remove_stable_node(struct stable_node * stable_node)877*4882a593Smuzhiyun static int remove_stable_node(struct stable_node *stable_node)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	struct page *page;
880*4882a593Smuzhiyun 	int err;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
883*4882a593Smuzhiyun 	if (!page) {
884*4882a593Smuzhiyun 		/*
885*4882a593Smuzhiyun 		 * get_ksm_page did remove_node_from_stable_tree itself.
886*4882a593Smuzhiyun 		 */
887*4882a593Smuzhiyun 		return 0;
888*4882a593Smuzhiyun 	}
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	/*
891*4882a593Smuzhiyun 	 * Page could be still mapped if this races with __mmput() running in
892*4882a593Smuzhiyun 	 * between ksm_exit() and exit_mmap(). Just refuse to let
893*4882a593Smuzhiyun 	 * merge_across_nodes/max_page_sharing be switched.
894*4882a593Smuzhiyun 	 */
895*4882a593Smuzhiyun 	err = -EBUSY;
896*4882a593Smuzhiyun 	if (!page_mapped(page)) {
897*4882a593Smuzhiyun 		/*
898*4882a593Smuzhiyun 		 * The stable node did not yet appear stale to get_ksm_page(),
899*4882a593Smuzhiyun 		 * since that allows for an unmapped ksm page to be recognized
900*4882a593Smuzhiyun 		 * right up until it is freed; but the node is safe to remove.
901*4882a593Smuzhiyun 		 * This page might be in a pagevec waiting to be freed,
902*4882a593Smuzhiyun 		 * or it might be PageSwapCache (perhaps under writeback),
903*4882a593Smuzhiyun 		 * or it might have been removed from swapcache a moment ago.
904*4882a593Smuzhiyun 		 */
905*4882a593Smuzhiyun 		set_page_stable_node(page, NULL);
906*4882a593Smuzhiyun 		remove_node_from_stable_tree(stable_node);
907*4882a593Smuzhiyun 		err = 0;
908*4882a593Smuzhiyun 	}
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	unlock_page(page);
911*4882a593Smuzhiyun 	put_page(page);
912*4882a593Smuzhiyun 	return err;
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun 
remove_stable_node_chain(struct stable_node * stable_node,struct rb_root * root)915*4882a593Smuzhiyun static int remove_stable_node_chain(struct stable_node *stable_node,
916*4882a593Smuzhiyun 				    struct rb_root *root)
917*4882a593Smuzhiyun {
918*4882a593Smuzhiyun 	struct stable_node *dup;
919*4882a593Smuzhiyun 	struct hlist_node *hlist_safe;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	if (!is_stable_node_chain(stable_node)) {
922*4882a593Smuzhiyun 		VM_BUG_ON(is_stable_node_dup(stable_node));
923*4882a593Smuzhiyun 		if (remove_stable_node(stable_node))
924*4882a593Smuzhiyun 			return true;
925*4882a593Smuzhiyun 		else
926*4882a593Smuzhiyun 			return false;
927*4882a593Smuzhiyun 	}
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	hlist_for_each_entry_safe(dup, hlist_safe,
930*4882a593Smuzhiyun 				  &stable_node->hlist, hlist_dup) {
931*4882a593Smuzhiyun 		VM_BUG_ON(!is_stable_node_dup(dup));
932*4882a593Smuzhiyun 		if (remove_stable_node(dup))
933*4882a593Smuzhiyun 			return true;
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 	BUG_ON(!hlist_empty(&stable_node->hlist));
936*4882a593Smuzhiyun 	free_stable_node_chain(stable_node, root);
937*4882a593Smuzhiyun 	return false;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun 
remove_all_stable_nodes(void)940*4882a593Smuzhiyun static int remove_all_stable_nodes(void)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun 	struct stable_node *stable_node, *next;
943*4882a593Smuzhiyun 	int nid;
944*4882a593Smuzhiyun 	int err = 0;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	for (nid = 0; nid < ksm_nr_node_ids; nid++) {
947*4882a593Smuzhiyun 		while (root_stable_tree[nid].rb_node) {
948*4882a593Smuzhiyun 			stable_node = rb_entry(root_stable_tree[nid].rb_node,
949*4882a593Smuzhiyun 						struct stable_node, node);
950*4882a593Smuzhiyun 			if (remove_stable_node_chain(stable_node,
951*4882a593Smuzhiyun 						     root_stable_tree + nid)) {
952*4882a593Smuzhiyun 				err = -EBUSY;
953*4882a593Smuzhiyun 				break;	/* proceed to next nid */
954*4882a593Smuzhiyun 			}
955*4882a593Smuzhiyun 			cond_resched();
956*4882a593Smuzhiyun 		}
957*4882a593Smuzhiyun 	}
958*4882a593Smuzhiyun 	list_for_each_entry_safe(stable_node, next, &migrate_nodes, list) {
959*4882a593Smuzhiyun 		if (remove_stable_node(stable_node))
960*4882a593Smuzhiyun 			err = -EBUSY;
961*4882a593Smuzhiyun 		cond_resched();
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun 	return err;
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
unmerge_and_remove_all_rmap_items(void)966*4882a593Smuzhiyun static int unmerge_and_remove_all_rmap_items(void)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	struct mm_slot *mm_slot;
969*4882a593Smuzhiyun 	struct mm_struct *mm;
970*4882a593Smuzhiyun 	struct vm_area_struct *vma;
971*4882a593Smuzhiyun 	int err = 0;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	spin_lock(&ksm_mmlist_lock);
974*4882a593Smuzhiyun 	ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
975*4882a593Smuzhiyun 						struct mm_slot, mm_list);
976*4882a593Smuzhiyun 	spin_unlock(&ksm_mmlist_lock);
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	for (mm_slot = ksm_scan.mm_slot;
979*4882a593Smuzhiyun 			mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
980*4882a593Smuzhiyun 		mm = mm_slot->mm;
981*4882a593Smuzhiyun 		mmap_read_lock(mm);
982*4882a593Smuzhiyun 		for (vma = mm->mmap; vma; vma = vma->vm_next) {
983*4882a593Smuzhiyun 			if (ksm_test_exit(mm))
984*4882a593Smuzhiyun 				break;
985*4882a593Smuzhiyun 			if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
986*4882a593Smuzhiyun 				continue;
987*4882a593Smuzhiyun 			err = unmerge_ksm_pages(vma,
988*4882a593Smuzhiyun 						vma->vm_start, vma->vm_end);
989*4882a593Smuzhiyun 			if (err)
990*4882a593Smuzhiyun 				goto error;
991*4882a593Smuzhiyun 		}
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 		remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
994*4882a593Smuzhiyun 		mmap_read_unlock(mm);
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 		spin_lock(&ksm_mmlist_lock);
997*4882a593Smuzhiyun 		ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
998*4882a593Smuzhiyun 						struct mm_slot, mm_list);
999*4882a593Smuzhiyun 		if (ksm_test_exit(mm)) {
1000*4882a593Smuzhiyun 			hash_del(&mm_slot->link);
1001*4882a593Smuzhiyun 			list_del(&mm_slot->mm_list);
1002*4882a593Smuzhiyun 			spin_unlock(&ksm_mmlist_lock);
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 			free_mm_slot(mm_slot);
1005*4882a593Smuzhiyun 			clear_bit(MMF_VM_MERGEABLE, &mm->flags);
1006*4882a593Smuzhiyun 			mmdrop(mm);
1007*4882a593Smuzhiyun 		} else
1008*4882a593Smuzhiyun 			spin_unlock(&ksm_mmlist_lock);
1009*4882a593Smuzhiyun 	}
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	/* Clean up stable nodes, but don't worry if some are still busy */
1012*4882a593Smuzhiyun 	remove_all_stable_nodes();
1013*4882a593Smuzhiyun 	ksm_scan.seqnr = 0;
1014*4882a593Smuzhiyun 	return 0;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun error:
1017*4882a593Smuzhiyun 	mmap_read_unlock(mm);
1018*4882a593Smuzhiyun 	spin_lock(&ksm_mmlist_lock);
1019*4882a593Smuzhiyun 	ksm_scan.mm_slot = &ksm_mm_head;
1020*4882a593Smuzhiyun 	spin_unlock(&ksm_mmlist_lock);
1021*4882a593Smuzhiyun 	return err;
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun #endif /* CONFIG_SYSFS */
1024*4882a593Smuzhiyun 
calc_checksum(struct page * page)1025*4882a593Smuzhiyun static u32 calc_checksum(struct page *page)
1026*4882a593Smuzhiyun {
1027*4882a593Smuzhiyun 	u32 checksum;
1028*4882a593Smuzhiyun 	void *addr = kmap_atomic(page);
1029*4882a593Smuzhiyun 	checksum = xxhash(addr, PAGE_SIZE, 0);
1030*4882a593Smuzhiyun 	kunmap_atomic(addr);
1031*4882a593Smuzhiyun 	return checksum;
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun 
write_protect_page(struct vm_area_struct * vma,struct page * page,pte_t * orig_pte)1034*4882a593Smuzhiyun static int write_protect_page(struct vm_area_struct *vma, struct page *page,
1035*4882a593Smuzhiyun 			      pte_t *orig_pte)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun 	struct mm_struct *mm = vma->vm_mm;
1038*4882a593Smuzhiyun 	struct page_vma_mapped_walk pvmw = {
1039*4882a593Smuzhiyun 		.page = page,
1040*4882a593Smuzhiyun 		.vma = vma,
1041*4882a593Smuzhiyun 	};
1042*4882a593Smuzhiyun 	int swapped;
1043*4882a593Smuzhiyun 	int err = -EFAULT;
1044*4882a593Smuzhiyun 	struct mmu_notifier_range range;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	pvmw.address = page_address_in_vma(page, vma);
1047*4882a593Smuzhiyun 	if (pvmw.address == -EFAULT)
1048*4882a593Smuzhiyun 		goto out;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	BUG_ON(PageTransCompound(page));
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
1053*4882a593Smuzhiyun 				pvmw.address,
1054*4882a593Smuzhiyun 				pvmw.address + PAGE_SIZE);
1055*4882a593Smuzhiyun 	mmu_notifier_invalidate_range_start(&range);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	if (!page_vma_mapped_walk(&pvmw))
1058*4882a593Smuzhiyun 		goto out_mn;
1059*4882a593Smuzhiyun 	if (WARN_ONCE(!pvmw.pte, "Unexpected PMD mapping?"))
1060*4882a593Smuzhiyun 		goto out_unlock;
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	if (pte_write(*pvmw.pte) || pte_dirty(*pvmw.pte) ||
1063*4882a593Smuzhiyun 	    (pte_protnone(*pvmw.pte) && pte_savedwrite(*pvmw.pte)) ||
1064*4882a593Smuzhiyun 						mm_tlb_flush_pending(mm)) {
1065*4882a593Smuzhiyun 		pte_t entry;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 		swapped = PageSwapCache(page);
1068*4882a593Smuzhiyun 		flush_cache_page(vma, pvmw.address, page_to_pfn(page));
1069*4882a593Smuzhiyun 		/*
1070*4882a593Smuzhiyun 		 * Ok this is tricky, when get_user_pages_fast() run it doesn't
1071*4882a593Smuzhiyun 		 * take any lock, therefore the check that we are going to make
1072*4882a593Smuzhiyun 		 * with the pagecount against the mapcount is racey and
1073*4882a593Smuzhiyun 		 * O_DIRECT can happen right after the check.
1074*4882a593Smuzhiyun 		 * So we clear the pte and flush the tlb before the check
1075*4882a593Smuzhiyun 		 * this assure us that no O_DIRECT can happen after the check
1076*4882a593Smuzhiyun 		 * or in the middle of the check.
1077*4882a593Smuzhiyun 		 *
1078*4882a593Smuzhiyun 		 * No need to notify as we are downgrading page table to read
1079*4882a593Smuzhiyun 		 * only not changing it to point to a new page.
1080*4882a593Smuzhiyun 		 *
1081*4882a593Smuzhiyun 		 * See Documentation/vm/mmu_notifier.rst
1082*4882a593Smuzhiyun 		 */
1083*4882a593Smuzhiyun 		entry = ptep_clear_flush(vma, pvmw.address, pvmw.pte);
1084*4882a593Smuzhiyun 		/*
1085*4882a593Smuzhiyun 		 * Check that no O_DIRECT or similar I/O is in progress on the
1086*4882a593Smuzhiyun 		 * page
1087*4882a593Smuzhiyun 		 */
1088*4882a593Smuzhiyun 		if (page_mapcount(page) + 1 + swapped != page_count(page)) {
1089*4882a593Smuzhiyun 			set_pte_at(mm, pvmw.address, pvmw.pte, entry);
1090*4882a593Smuzhiyun 			goto out_unlock;
1091*4882a593Smuzhiyun 		}
1092*4882a593Smuzhiyun 		if (pte_dirty(entry))
1093*4882a593Smuzhiyun 			set_page_dirty(page);
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 		if (pte_protnone(entry))
1096*4882a593Smuzhiyun 			entry = pte_mkclean(pte_clear_savedwrite(entry));
1097*4882a593Smuzhiyun 		else
1098*4882a593Smuzhiyun 			entry = pte_mkclean(pte_wrprotect(entry));
1099*4882a593Smuzhiyun 		set_pte_at_notify(mm, pvmw.address, pvmw.pte, entry);
1100*4882a593Smuzhiyun 	}
1101*4882a593Smuzhiyun 	*orig_pte = *pvmw.pte;
1102*4882a593Smuzhiyun 	err = 0;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun out_unlock:
1105*4882a593Smuzhiyun 	page_vma_mapped_walk_done(&pvmw);
1106*4882a593Smuzhiyun out_mn:
1107*4882a593Smuzhiyun 	mmu_notifier_invalidate_range_end(&range);
1108*4882a593Smuzhiyun out:
1109*4882a593Smuzhiyun 	return err;
1110*4882a593Smuzhiyun }
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun /**
1113*4882a593Smuzhiyun  * replace_page - replace page in vma by new ksm page
1114*4882a593Smuzhiyun  * @vma:      vma that holds the pte pointing to page
1115*4882a593Smuzhiyun  * @page:     the page we are replacing by kpage
1116*4882a593Smuzhiyun  * @kpage:    the ksm page we replace page by
1117*4882a593Smuzhiyun  * @orig_pte: the original value of the pte
1118*4882a593Smuzhiyun  *
1119*4882a593Smuzhiyun  * Returns 0 on success, -EFAULT on failure.
1120*4882a593Smuzhiyun  */
replace_page(struct vm_area_struct * vma,struct page * page,struct page * kpage,pte_t orig_pte)1121*4882a593Smuzhiyun static int replace_page(struct vm_area_struct *vma, struct page *page,
1122*4882a593Smuzhiyun 			struct page *kpage, pte_t orig_pte)
1123*4882a593Smuzhiyun {
1124*4882a593Smuzhiyun 	struct mm_struct *mm = vma->vm_mm;
1125*4882a593Smuzhiyun 	pmd_t *pmd;
1126*4882a593Smuzhiyun 	pte_t *ptep;
1127*4882a593Smuzhiyun 	pte_t newpte;
1128*4882a593Smuzhiyun 	spinlock_t *ptl;
1129*4882a593Smuzhiyun 	unsigned long addr;
1130*4882a593Smuzhiyun 	int err = -EFAULT;
1131*4882a593Smuzhiyun 	struct mmu_notifier_range range;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	addr = page_address_in_vma(page, vma);
1134*4882a593Smuzhiyun 	if (addr == -EFAULT)
1135*4882a593Smuzhiyun 		goto out;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	pmd = mm_find_pmd(mm, addr);
1138*4882a593Smuzhiyun 	if (!pmd)
1139*4882a593Smuzhiyun 		goto out;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, addr,
1142*4882a593Smuzhiyun 				addr + PAGE_SIZE);
1143*4882a593Smuzhiyun 	mmu_notifier_invalidate_range_start(&range);
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
1146*4882a593Smuzhiyun 	if (!pte_same(*ptep, orig_pte)) {
1147*4882a593Smuzhiyun 		pte_unmap_unlock(ptep, ptl);
1148*4882a593Smuzhiyun 		goto out_mn;
1149*4882a593Smuzhiyun 	}
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	/*
1152*4882a593Smuzhiyun 	 * No need to check ksm_use_zero_pages here: we can only have a
1153*4882a593Smuzhiyun 	 * zero_page here if ksm_use_zero_pages was enabled already.
1154*4882a593Smuzhiyun 	 */
1155*4882a593Smuzhiyun 	if (!is_zero_pfn(page_to_pfn(kpage))) {
1156*4882a593Smuzhiyun 		get_page(kpage);
1157*4882a593Smuzhiyun 		page_add_anon_rmap(kpage, vma, addr, false);
1158*4882a593Smuzhiyun 		newpte = mk_pte(kpage, vma->vm_page_prot);
1159*4882a593Smuzhiyun 	} else {
1160*4882a593Smuzhiyun 		newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage),
1161*4882a593Smuzhiyun 					       vma->vm_page_prot));
1162*4882a593Smuzhiyun 		/*
1163*4882a593Smuzhiyun 		 * We're replacing an anonymous page with a zero page, which is
1164*4882a593Smuzhiyun 		 * not anonymous. We need to do proper accounting otherwise we
1165*4882a593Smuzhiyun 		 * will get wrong values in /proc, and a BUG message in dmesg
1166*4882a593Smuzhiyun 		 * when tearing down the mm.
1167*4882a593Smuzhiyun 		 */
1168*4882a593Smuzhiyun 		dec_mm_counter(mm, MM_ANONPAGES);
1169*4882a593Smuzhiyun 	}
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	flush_cache_page(vma, addr, pte_pfn(*ptep));
1172*4882a593Smuzhiyun 	/*
1173*4882a593Smuzhiyun 	 * No need to notify as we are replacing a read only page with another
1174*4882a593Smuzhiyun 	 * read only page with the same content.
1175*4882a593Smuzhiyun 	 *
1176*4882a593Smuzhiyun 	 * See Documentation/vm/mmu_notifier.rst
1177*4882a593Smuzhiyun 	 */
1178*4882a593Smuzhiyun 	ptep_clear_flush(vma, addr, ptep);
1179*4882a593Smuzhiyun 	set_pte_at_notify(mm, addr, ptep, newpte);
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	page_remove_rmap(page, false);
1182*4882a593Smuzhiyun 	if (!page_mapped(page))
1183*4882a593Smuzhiyun 		try_to_free_swap(page);
1184*4882a593Smuzhiyun 	put_page(page);
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	pte_unmap_unlock(ptep, ptl);
1187*4882a593Smuzhiyun 	err = 0;
1188*4882a593Smuzhiyun out_mn:
1189*4882a593Smuzhiyun 	mmu_notifier_invalidate_range_end(&range);
1190*4882a593Smuzhiyun out:
1191*4882a593Smuzhiyun 	return err;
1192*4882a593Smuzhiyun }
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun /*
1195*4882a593Smuzhiyun  * try_to_merge_one_page - take two pages and merge them into one
1196*4882a593Smuzhiyun  * @vma: the vma that holds the pte pointing to page
1197*4882a593Smuzhiyun  * @page: the PageAnon page that we want to replace with kpage
1198*4882a593Smuzhiyun  * @kpage: the PageKsm page that we want to map instead of page,
1199*4882a593Smuzhiyun  *         or NULL the first time when we want to use page as kpage.
1200*4882a593Smuzhiyun  *
1201*4882a593Smuzhiyun  * This function returns 0 if the pages were merged, -EFAULT otherwise.
1202*4882a593Smuzhiyun  */
try_to_merge_one_page(struct vm_area_struct * vma,struct page * page,struct page * kpage)1203*4882a593Smuzhiyun static int try_to_merge_one_page(struct vm_area_struct *vma,
1204*4882a593Smuzhiyun 				 struct page *page, struct page *kpage)
1205*4882a593Smuzhiyun {
1206*4882a593Smuzhiyun 	pte_t orig_pte = __pte(0);
1207*4882a593Smuzhiyun 	int err = -EFAULT;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	if (page == kpage)			/* ksm page forked */
1210*4882a593Smuzhiyun 		return 0;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	if (!PageAnon(page))
1213*4882a593Smuzhiyun 		goto out;
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	/*
1216*4882a593Smuzhiyun 	 * We need the page lock to read a stable PageSwapCache in
1217*4882a593Smuzhiyun 	 * write_protect_page().  We use trylock_page() instead of
1218*4882a593Smuzhiyun 	 * lock_page() because we don't want to wait here - we
1219*4882a593Smuzhiyun 	 * prefer to continue scanning and merging different pages,
1220*4882a593Smuzhiyun 	 * then come back to this page when it is unlocked.
1221*4882a593Smuzhiyun 	 */
1222*4882a593Smuzhiyun 	if (!trylock_page(page))
1223*4882a593Smuzhiyun 		goto out;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	if (PageTransCompound(page)) {
1226*4882a593Smuzhiyun 		if (split_huge_page(page))
1227*4882a593Smuzhiyun 			goto out_unlock;
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/*
1231*4882a593Smuzhiyun 	 * If this anonymous page is mapped only here, its pte may need
1232*4882a593Smuzhiyun 	 * to be write-protected.  If it's mapped elsewhere, all of its
1233*4882a593Smuzhiyun 	 * ptes are necessarily already write-protected.  But in either
1234*4882a593Smuzhiyun 	 * case, we need to lock and check page_count is not raised.
1235*4882a593Smuzhiyun 	 */
1236*4882a593Smuzhiyun 	if (write_protect_page(vma, page, &orig_pte) == 0) {
1237*4882a593Smuzhiyun 		if (!kpage) {
1238*4882a593Smuzhiyun 			/*
1239*4882a593Smuzhiyun 			 * While we hold page lock, upgrade page from
1240*4882a593Smuzhiyun 			 * PageAnon+anon_vma to PageKsm+NULL stable_node:
1241*4882a593Smuzhiyun 			 * stable_tree_insert() will update stable_node.
1242*4882a593Smuzhiyun 			 */
1243*4882a593Smuzhiyun 			set_page_stable_node(page, NULL);
1244*4882a593Smuzhiyun 			mark_page_accessed(page);
1245*4882a593Smuzhiyun 			/*
1246*4882a593Smuzhiyun 			 * Page reclaim just frees a clean page with no dirty
1247*4882a593Smuzhiyun 			 * ptes: make sure that the ksm page would be swapped.
1248*4882a593Smuzhiyun 			 */
1249*4882a593Smuzhiyun 			if (!PageDirty(page))
1250*4882a593Smuzhiyun 				SetPageDirty(page);
1251*4882a593Smuzhiyun 			err = 0;
1252*4882a593Smuzhiyun 		} else if (pages_identical(page, kpage))
1253*4882a593Smuzhiyun 			err = replace_page(vma, page, kpage, orig_pte);
1254*4882a593Smuzhiyun 	}
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
1257*4882a593Smuzhiyun 		munlock_vma_page(page);
1258*4882a593Smuzhiyun 		if (!PageMlocked(kpage)) {
1259*4882a593Smuzhiyun 			unlock_page(page);
1260*4882a593Smuzhiyun 			lock_page(kpage);
1261*4882a593Smuzhiyun 			mlock_vma_page(kpage);
1262*4882a593Smuzhiyun 			page = kpage;		/* for final unlock */
1263*4882a593Smuzhiyun 		}
1264*4882a593Smuzhiyun 	}
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun out_unlock:
1267*4882a593Smuzhiyun 	unlock_page(page);
1268*4882a593Smuzhiyun out:
1269*4882a593Smuzhiyun 	return err;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun /*
1273*4882a593Smuzhiyun  * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
1274*4882a593Smuzhiyun  * but no new kernel page is allocated: kpage must already be a ksm page.
1275*4882a593Smuzhiyun  *
1276*4882a593Smuzhiyun  * This function returns 0 if the pages were merged, -EFAULT otherwise.
1277*4882a593Smuzhiyun  */
try_to_merge_with_ksm_page(struct rmap_item * rmap_item,struct page * page,struct page * kpage)1278*4882a593Smuzhiyun static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
1279*4882a593Smuzhiyun 				      struct page *page, struct page *kpage)
1280*4882a593Smuzhiyun {
1281*4882a593Smuzhiyun 	struct mm_struct *mm = rmap_item->mm;
1282*4882a593Smuzhiyun 	struct vm_area_struct *vma;
1283*4882a593Smuzhiyun 	int err = -EFAULT;
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	mmap_read_lock(mm);
1286*4882a593Smuzhiyun 	vma = find_mergeable_vma(mm, rmap_item->address);
1287*4882a593Smuzhiyun 	if (!vma)
1288*4882a593Smuzhiyun 		goto out;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	err = try_to_merge_one_page(vma, page, kpage);
1291*4882a593Smuzhiyun 	if (err)
1292*4882a593Smuzhiyun 		goto out;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	/* Unstable nid is in union with stable anon_vma: remove first */
1295*4882a593Smuzhiyun 	remove_rmap_item_from_tree(rmap_item);
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	/* Must get reference to anon_vma while still holding mmap_lock */
1298*4882a593Smuzhiyun 	rmap_item->anon_vma = vma->anon_vma;
1299*4882a593Smuzhiyun 	get_anon_vma(vma->anon_vma);
1300*4882a593Smuzhiyun out:
1301*4882a593Smuzhiyun 	mmap_read_unlock(mm);
1302*4882a593Smuzhiyun 	return err;
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun /*
1306*4882a593Smuzhiyun  * try_to_merge_two_pages - take two identical pages and prepare them
1307*4882a593Smuzhiyun  * to be merged into one page.
1308*4882a593Smuzhiyun  *
1309*4882a593Smuzhiyun  * This function returns the kpage if we successfully merged two identical
1310*4882a593Smuzhiyun  * pages into one ksm page, NULL otherwise.
1311*4882a593Smuzhiyun  *
1312*4882a593Smuzhiyun  * Note that this function upgrades page to ksm page: if one of the pages
1313*4882a593Smuzhiyun  * is already a ksm page, try_to_merge_with_ksm_page should be used.
1314*4882a593Smuzhiyun  */
try_to_merge_two_pages(struct rmap_item * rmap_item,struct page * page,struct rmap_item * tree_rmap_item,struct page * tree_page)1315*4882a593Smuzhiyun static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
1316*4882a593Smuzhiyun 					   struct page *page,
1317*4882a593Smuzhiyun 					   struct rmap_item *tree_rmap_item,
1318*4882a593Smuzhiyun 					   struct page *tree_page)
1319*4882a593Smuzhiyun {
1320*4882a593Smuzhiyun 	int err;
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
1323*4882a593Smuzhiyun 	if (!err) {
1324*4882a593Smuzhiyun 		err = try_to_merge_with_ksm_page(tree_rmap_item,
1325*4882a593Smuzhiyun 							tree_page, page);
1326*4882a593Smuzhiyun 		/*
1327*4882a593Smuzhiyun 		 * If that fails, we have a ksm page with only one pte
1328*4882a593Smuzhiyun 		 * pointing to it: so break it.
1329*4882a593Smuzhiyun 		 */
1330*4882a593Smuzhiyun 		if (err)
1331*4882a593Smuzhiyun 			break_cow(rmap_item);
1332*4882a593Smuzhiyun 	}
1333*4882a593Smuzhiyun 	return err ? NULL : page;
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun static __always_inline
__is_page_sharing_candidate(struct stable_node * stable_node,int offset)1337*4882a593Smuzhiyun bool __is_page_sharing_candidate(struct stable_node *stable_node, int offset)
1338*4882a593Smuzhiyun {
1339*4882a593Smuzhiyun 	VM_BUG_ON(stable_node->rmap_hlist_len < 0);
1340*4882a593Smuzhiyun 	/*
1341*4882a593Smuzhiyun 	 * Check that at least one mapping still exists, otherwise
1342*4882a593Smuzhiyun 	 * there's no much point to merge and share with this
1343*4882a593Smuzhiyun 	 * stable_node, as the underlying tree_page of the other
1344*4882a593Smuzhiyun 	 * sharer is going to be freed soon.
1345*4882a593Smuzhiyun 	 */
1346*4882a593Smuzhiyun 	return stable_node->rmap_hlist_len &&
1347*4882a593Smuzhiyun 		stable_node->rmap_hlist_len + offset < ksm_max_page_sharing;
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun static __always_inline
is_page_sharing_candidate(struct stable_node * stable_node)1351*4882a593Smuzhiyun bool is_page_sharing_candidate(struct stable_node *stable_node)
1352*4882a593Smuzhiyun {
1353*4882a593Smuzhiyun 	return __is_page_sharing_candidate(stable_node, 0);
1354*4882a593Smuzhiyun }
1355*4882a593Smuzhiyun 
stable_node_dup(struct stable_node ** _stable_node_dup,struct stable_node ** _stable_node,struct rb_root * root,bool prune_stale_stable_nodes)1356*4882a593Smuzhiyun static struct page *stable_node_dup(struct stable_node **_stable_node_dup,
1357*4882a593Smuzhiyun 				    struct stable_node **_stable_node,
1358*4882a593Smuzhiyun 				    struct rb_root *root,
1359*4882a593Smuzhiyun 				    bool prune_stale_stable_nodes)
1360*4882a593Smuzhiyun {
1361*4882a593Smuzhiyun 	struct stable_node *dup, *found = NULL, *stable_node = *_stable_node;
1362*4882a593Smuzhiyun 	struct hlist_node *hlist_safe;
1363*4882a593Smuzhiyun 	struct page *_tree_page, *tree_page = NULL;
1364*4882a593Smuzhiyun 	int nr = 0;
1365*4882a593Smuzhiyun 	int found_rmap_hlist_len;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	if (!prune_stale_stable_nodes ||
1368*4882a593Smuzhiyun 	    time_before(jiffies, stable_node->chain_prune_time +
1369*4882a593Smuzhiyun 			msecs_to_jiffies(
1370*4882a593Smuzhiyun 				ksm_stable_node_chains_prune_millisecs)))
1371*4882a593Smuzhiyun 		prune_stale_stable_nodes = false;
1372*4882a593Smuzhiyun 	else
1373*4882a593Smuzhiyun 		stable_node->chain_prune_time = jiffies;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	hlist_for_each_entry_safe(dup, hlist_safe,
1376*4882a593Smuzhiyun 				  &stable_node->hlist, hlist_dup) {
1377*4882a593Smuzhiyun 		cond_resched();
1378*4882a593Smuzhiyun 		/*
1379*4882a593Smuzhiyun 		 * We must walk all stable_node_dup to prune the stale
1380*4882a593Smuzhiyun 		 * stable nodes during lookup.
1381*4882a593Smuzhiyun 		 *
1382*4882a593Smuzhiyun 		 * get_ksm_page can drop the nodes from the
1383*4882a593Smuzhiyun 		 * stable_node->hlist if they point to freed pages
1384*4882a593Smuzhiyun 		 * (that's why we do a _safe walk). The "dup"
1385*4882a593Smuzhiyun 		 * stable_node parameter itself will be freed from
1386*4882a593Smuzhiyun 		 * under us if it returns NULL.
1387*4882a593Smuzhiyun 		 */
1388*4882a593Smuzhiyun 		_tree_page = get_ksm_page(dup, GET_KSM_PAGE_NOLOCK);
1389*4882a593Smuzhiyun 		if (!_tree_page)
1390*4882a593Smuzhiyun 			continue;
1391*4882a593Smuzhiyun 		nr += 1;
1392*4882a593Smuzhiyun 		if (is_page_sharing_candidate(dup)) {
1393*4882a593Smuzhiyun 			if (!found ||
1394*4882a593Smuzhiyun 			    dup->rmap_hlist_len > found_rmap_hlist_len) {
1395*4882a593Smuzhiyun 				if (found)
1396*4882a593Smuzhiyun 					put_page(tree_page);
1397*4882a593Smuzhiyun 				found = dup;
1398*4882a593Smuzhiyun 				found_rmap_hlist_len = found->rmap_hlist_len;
1399*4882a593Smuzhiyun 				tree_page = _tree_page;
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 				/* skip put_page for found dup */
1402*4882a593Smuzhiyun 				if (!prune_stale_stable_nodes)
1403*4882a593Smuzhiyun 					break;
1404*4882a593Smuzhiyun 				continue;
1405*4882a593Smuzhiyun 			}
1406*4882a593Smuzhiyun 		}
1407*4882a593Smuzhiyun 		put_page(_tree_page);
1408*4882a593Smuzhiyun 	}
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	if (found) {
1411*4882a593Smuzhiyun 		/*
1412*4882a593Smuzhiyun 		 * nr is counting all dups in the chain only if
1413*4882a593Smuzhiyun 		 * prune_stale_stable_nodes is true, otherwise we may
1414*4882a593Smuzhiyun 		 * break the loop at nr == 1 even if there are
1415*4882a593Smuzhiyun 		 * multiple entries.
1416*4882a593Smuzhiyun 		 */
1417*4882a593Smuzhiyun 		if (prune_stale_stable_nodes && nr == 1) {
1418*4882a593Smuzhiyun 			/*
1419*4882a593Smuzhiyun 			 * If there's not just one entry it would
1420*4882a593Smuzhiyun 			 * corrupt memory, better BUG_ON. In KSM
1421*4882a593Smuzhiyun 			 * context with no lock held it's not even
1422*4882a593Smuzhiyun 			 * fatal.
1423*4882a593Smuzhiyun 			 */
1424*4882a593Smuzhiyun 			BUG_ON(stable_node->hlist.first->next);
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 			/*
1427*4882a593Smuzhiyun 			 * There's just one entry and it is below the
1428*4882a593Smuzhiyun 			 * deduplication limit so drop the chain.
1429*4882a593Smuzhiyun 			 */
1430*4882a593Smuzhiyun 			rb_replace_node(&stable_node->node, &found->node,
1431*4882a593Smuzhiyun 					root);
1432*4882a593Smuzhiyun 			free_stable_node(stable_node);
1433*4882a593Smuzhiyun 			ksm_stable_node_chains--;
1434*4882a593Smuzhiyun 			ksm_stable_node_dups--;
1435*4882a593Smuzhiyun 			/*
1436*4882a593Smuzhiyun 			 * NOTE: the caller depends on the stable_node
1437*4882a593Smuzhiyun 			 * to be equal to stable_node_dup if the chain
1438*4882a593Smuzhiyun 			 * was collapsed.
1439*4882a593Smuzhiyun 			 */
1440*4882a593Smuzhiyun 			*_stable_node = found;
1441*4882a593Smuzhiyun 			/*
1442*4882a593Smuzhiyun 			 * Just for robustneess as stable_node is
1443*4882a593Smuzhiyun 			 * otherwise left as a stable pointer, the
1444*4882a593Smuzhiyun 			 * compiler shall optimize it away at build
1445*4882a593Smuzhiyun 			 * time.
1446*4882a593Smuzhiyun 			 */
1447*4882a593Smuzhiyun 			stable_node = NULL;
1448*4882a593Smuzhiyun 		} else if (stable_node->hlist.first != &found->hlist_dup &&
1449*4882a593Smuzhiyun 			   __is_page_sharing_candidate(found, 1)) {
1450*4882a593Smuzhiyun 			/*
1451*4882a593Smuzhiyun 			 * If the found stable_node dup can accept one
1452*4882a593Smuzhiyun 			 * more future merge (in addition to the one
1453*4882a593Smuzhiyun 			 * that is underway) and is not at the head of
1454*4882a593Smuzhiyun 			 * the chain, put it there so next search will
1455*4882a593Smuzhiyun 			 * be quicker in the !prune_stale_stable_nodes
1456*4882a593Smuzhiyun 			 * case.
1457*4882a593Smuzhiyun 			 *
1458*4882a593Smuzhiyun 			 * NOTE: it would be inaccurate to use nr > 1
1459*4882a593Smuzhiyun 			 * instead of checking the hlist.first pointer
1460*4882a593Smuzhiyun 			 * directly, because in the
1461*4882a593Smuzhiyun 			 * prune_stale_stable_nodes case "nr" isn't
1462*4882a593Smuzhiyun 			 * the position of the found dup in the chain,
1463*4882a593Smuzhiyun 			 * but the total number of dups in the chain.
1464*4882a593Smuzhiyun 			 */
1465*4882a593Smuzhiyun 			hlist_del(&found->hlist_dup);
1466*4882a593Smuzhiyun 			hlist_add_head(&found->hlist_dup,
1467*4882a593Smuzhiyun 				       &stable_node->hlist);
1468*4882a593Smuzhiyun 		}
1469*4882a593Smuzhiyun 	}
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	*_stable_node_dup = found;
1472*4882a593Smuzhiyun 	return tree_page;
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun 
stable_node_dup_any(struct stable_node * stable_node,struct rb_root * root)1475*4882a593Smuzhiyun static struct stable_node *stable_node_dup_any(struct stable_node *stable_node,
1476*4882a593Smuzhiyun 					       struct rb_root *root)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun 	if (!is_stable_node_chain(stable_node))
1479*4882a593Smuzhiyun 		return stable_node;
1480*4882a593Smuzhiyun 	if (hlist_empty(&stable_node->hlist)) {
1481*4882a593Smuzhiyun 		free_stable_node_chain(stable_node, root);
1482*4882a593Smuzhiyun 		return NULL;
1483*4882a593Smuzhiyun 	}
1484*4882a593Smuzhiyun 	return hlist_entry(stable_node->hlist.first,
1485*4882a593Smuzhiyun 			   typeof(*stable_node), hlist_dup);
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun /*
1489*4882a593Smuzhiyun  * Like for get_ksm_page, this function can free the *_stable_node and
1490*4882a593Smuzhiyun  * *_stable_node_dup if the returned tree_page is NULL.
1491*4882a593Smuzhiyun  *
1492*4882a593Smuzhiyun  * It can also free and overwrite *_stable_node with the found
1493*4882a593Smuzhiyun  * stable_node_dup if the chain is collapsed (in which case
1494*4882a593Smuzhiyun  * *_stable_node will be equal to *_stable_node_dup like if the chain
1495*4882a593Smuzhiyun  * never existed). It's up to the caller to verify tree_page is not
1496*4882a593Smuzhiyun  * NULL before dereferencing *_stable_node or *_stable_node_dup.
1497*4882a593Smuzhiyun  *
1498*4882a593Smuzhiyun  * *_stable_node_dup is really a second output parameter of this
1499*4882a593Smuzhiyun  * function and will be overwritten in all cases, the caller doesn't
1500*4882a593Smuzhiyun  * need to initialize it.
1501*4882a593Smuzhiyun  */
__stable_node_chain(struct stable_node ** _stable_node_dup,struct stable_node ** _stable_node,struct rb_root * root,bool prune_stale_stable_nodes)1502*4882a593Smuzhiyun static struct page *__stable_node_chain(struct stable_node **_stable_node_dup,
1503*4882a593Smuzhiyun 					struct stable_node **_stable_node,
1504*4882a593Smuzhiyun 					struct rb_root *root,
1505*4882a593Smuzhiyun 					bool prune_stale_stable_nodes)
1506*4882a593Smuzhiyun {
1507*4882a593Smuzhiyun 	struct stable_node *stable_node = *_stable_node;
1508*4882a593Smuzhiyun 	if (!is_stable_node_chain(stable_node)) {
1509*4882a593Smuzhiyun 		if (is_page_sharing_candidate(stable_node)) {
1510*4882a593Smuzhiyun 			*_stable_node_dup = stable_node;
1511*4882a593Smuzhiyun 			return get_ksm_page(stable_node, GET_KSM_PAGE_NOLOCK);
1512*4882a593Smuzhiyun 		}
1513*4882a593Smuzhiyun 		/*
1514*4882a593Smuzhiyun 		 * _stable_node_dup set to NULL means the stable_node
1515*4882a593Smuzhiyun 		 * reached the ksm_max_page_sharing limit.
1516*4882a593Smuzhiyun 		 */
1517*4882a593Smuzhiyun 		*_stable_node_dup = NULL;
1518*4882a593Smuzhiyun 		return NULL;
1519*4882a593Smuzhiyun 	}
1520*4882a593Smuzhiyun 	return stable_node_dup(_stable_node_dup, _stable_node, root,
1521*4882a593Smuzhiyun 			       prune_stale_stable_nodes);
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun 
chain_prune(struct stable_node ** s_n_d,struct stable_node ** s_n,struct rb_root * root)1524*4882a593Smuzhiyun static __always_inline struct page *chain_prune(struct stable_node **s_n_d,
1525*4882a593Smuzhiyun 						struct stable_node **s_n,
1526*4882a593Smuzhiyun 						struct rb_root *root)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun 	return __stable_node_chain(s_n_d, s_n, root, true);
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun 
chain(struct stable_node ** s_n_d,struct stable_node * s_n,struct rb_root * root)1531*4882a593Smuzhiyun static __always_inline struct page *chain(struct stable_node **s_n_d,
1532*4882a593Smuzhiyun 					  struct stable_node *s_n,
1533*4882a593Smuzhiyun 					  struct rb_root *root)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	struct stable_node *old_stable_node = s_n;
1536*4882a593Smuzhiyun 	struct page *tree_page;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	tree_page = __stable_node_chain(s_n_d, &s_n, root, false);
1539*4882a593Smuzhiyun 	/* not pruning dups so s_n cannot have changed */
1540*4882a593Smuzhiyun 	VM_BUG_ON(s_n != old_stable_node);
1541*4882a593Smuzhiyun 	return tree_page;
1542*4882a593Smuzhiyun }
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun /*
1545*4882a593Smuzhiyun  * stable_tree_search - search for page inside the stable tree
1546*4882a593Smuzhiyun  *
1547*4882a593Smuzhiyun  * This function checks if there is a page inside the stable tree
1548*4882a593Smuzhiyun  * with identical content to the page that we are scanning right now.
1549*4882a593Smuzhiyun  *
1550*4882a593Smuzhiyun  * This function returns the stable tree node of identical content if found,
1551*4882a593Smuzhiyun  * NULL otherwise.
1552*4882a593Smuzhiyun  */
stable_tree_search(struct page * page)1553*4882a593Smuzhiyun static struct page *stable_tree_search(struct page *page)
1554*4882a593Smuzhiyun {
1555*4882a593Smuzhiyun 	int nid;
1556*4882a593Smuzhiyun 	struct rb_root *root;
1557*4882a593Smuzhiyun 	struct rb_node **new;
1558*4882a593Smuzhiyun 	struct rb_node *parent;
1559*4882a593Smuzhiyun 	struct stable_node *stable_node, *stable_node_dup, *stable_node_any;
1560*4882a593Smuzhiyun 	struct stable_node *page_node;
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	page_node = page_stable_node(page);
1563*4882a593Smuzhiyun 	if (page_node && page_node->head != &migrate_nodes) {
1564*4882a593Smuzhiyun 		/* ksm page forked */
1565*4882a593Smuzhiyun 		get_page(page);
1566*4882a593Smuzhiyun 		return page;
1567*4882a593Smuzhiyun 	}
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 	nid = get_kpfn_nid(page_to_pfn(page));
1570*4882a593Smuzhiyun 	root = root_stable_tree + nid;
1571*4882a593Smuzhiyun again:
1572*4882a593Smuzhiyun 	new = &root->rb_node;
1573*4882a593Smuzhiyun 	parent = NULL;
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	while (*new) {
1576*4882a593Smuzhiyun 		struct page *tree_page;
1577*4882a593Smuzhiyun 		int ret;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 		cond_resched();
1580*4882a593Smuzhiyun 		stable_node = rb_entry(*new, struct stable_node, node);
1581*4882a593Smuzhiyun 		stable_node_any = NULL;
1582*4882a593Smuzhiyun 		tree_page = chain_prune(&stable_node_dup, &stable_node,	root);
1583*4882a593Smuzhiyun 		/*
1584*4882a593Smuzhiyun 		 * NOTE: stable_node may have been freed by
1585*4882a593Smuzhiyun 		 * chain_prune() if the returned stable_node_dup is
1586*4882a593Smuzhiyun 		 * not NULL. stable_node_dup may have been inserted in
1587*4882a593Smuzhiyun 		 * the rbtree instead as a regular stable_node (in
1588*4882a593Smuzhiyun 		 * order to collapse the stable_node chain if a single
1589*4882a593Smuzhiyun 		 * stable_node dup was found in it). In such case the
1590*4882a593Smuzhiyun 		 * stable_node is overwritten by the calleee to point
1591*4882a593Smuzhiyun 		 * to the stable_node_dup that was collapsed in the
1592*4882a593Smuzhiyun 		 * stable rbtree and stable_node will be equal to
1593*4882a593Smuzhiyun 		 * stable_node_dup like if the chain never existed.
1594*4882a593Smuzhiyun 		 */
1595*4882a593Smuzhiyun 		if (!stable_node_dup) {
1596*4882a593Smuzhiyun 			/*
1597*4882a593Smuzhiyun 			 * Either all stable_node dups were full in
1598*4882a593Smuzhiyun 			 * this stable_node chain, or this chain was
1599*4882a593Smuzhiyun 			 * empty and should be rb_erased.
1600*4882a593Smuzhiyun 			 */
1601*4882a593Smuzhiyun 			stable_node_any = stable_node_dup_any(stable_node,
1602*4882a593Smuzhiyun 							      root);
1603*4882a593Smuzhiyun 			if (!stable_node_any) {
1604*4882a593Smuzhiyun 				/* rb_erase just run */
1605*4882a593Smuzhiyun 				goto again;
1606*4882a593Smuzhiyun 			}
1607*4882a593Smuzhiyun 			/*
1608*4882a593Smuzhiyun 			 * Take any of the stable_node dups page of
1609*4882a593Smuzhiyun 			 * this stable_node chain to let the tree walk
1610*4882a593Smuzhiyun 			 * continue. All KSM pages belonging to the
1611*4882a593Smuzhiyun 			 * stable_node dups in a stable_node chain
1612*4882a593Smuzhiyun 			 * have the same content and they're
1613*4882a593Smuzhiyun 			 * write protected at all times. Any will work
1614*4882a593Smuzhiyun 			 * fine to continue the walk.
1615*4882a593Smuzhiyun 			 */
1616*4882a593Smuzhiyun 			tree_page = get_ksm_page(stable_node_any,
1617*4882a593Smuzhiyun 						 GET_KSM_PAGE_NOLOCK);
1618*4882a593Smuzhiyun 		}
1619*4882a593Smuzhiyun 		VM_BUG_ON(!stable_node_dup ^ !!stable_node_any);
1620*4882a593Smuzhiyun 		if (!tree_page) {
1621*4882a593Smuzhiyun 			/*
1622*4882a593Smuzhiyun 			 * If we walked over a stale stable_node,
1623*4882a593Smuzhiyun 			 * get_ksm_page() will call rb_erase() and it
1624*4882a593Smuzhiyun 			 * may rebalance the tree from under us. So
1625*4882a593Smuzhiyun 			 * restart the search from scratch. Returning
1626*4882a593Smuzhiyun 			 * NULL would be safe too, but we'd generate
1627*4882a593Smuzhiyun 			 * false negative insertions just because some
1628*4882a593Smuzhiyun 			 * stable_node was stale.
1629*4882a593Smuzhiyun 			 */
1630*4882a593Smuzhiyun 			goto again;
1631*4882a593Smuzhiyun 		}
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 		ret = memcmp_pages(page, tree_page);
1634*4882a593Smuzhiyun 		put_page(tree_page);
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 		parent = *new;
1637*4882a593Smuzhiyun 		if (ret < 0)
1638*4882a593Smuzhiyun 			new = &parent->rb_left;
1639*4882a593Smuzhiyun 		else if (ret > 0)
1640*4882a593Smuzhiyun 			new = &parent->rb_right;
1641*4882a593Smuzhiyun 		else {
1642*4882a593Smuzhiyun 			if (page_node) {
1643*4882a593Smuzhiyun 				VM_BUG_ON(page_node->head != &migrate_nodes);
1644*4882a593Smuzhiyun 				/*
1645*4882a593Smuzhiyun 				 * Test if the migrated page should be merged
1646*4882a593Smuzhiyun 				 * into a stable node dup. If the mapcount is
1647*4882a593Smuzhiyun 				 * 1 we can migrate it with another KSM page
1648*4882a593Smuzhiyun 				 * without adding it to the chain.
1649*4882a593Smuzhiyun 				 */
1650*4882a593Smuzhiyun 				if (page_mapcount(page) > 1)
1651*4882a593Smuzhiyun 					goto chain_append;
1652*4882a593Smuzhiyun 			}
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 			if (!stable_node_dup) {
1655*4882a593Smuzhiyun 				/*
1656*4882a593Smuzhiyun 				 * If the stable_node is a chain and
1657*4882a593Smuzhiyun 				 * we got a payload match in memcmp
1658*4882a593Smuzhiyun 				 * but we cannot merge the scanned
1659*4882a593Smuzhiyun 				 * page in any of the existing
1660*4882a593Smuzhiyun 				 * stable_node dups because they're
1661*4882a593Smuzhiyun 				 * all full, we need to wait the
1662*4882a593Smuzhiyun 				 * scanned page to find itself a match
1663*4882a593Smuzhiyun 				 * in the unstable tree to create a
1664*4882a593Smuzhiyun 				 * brand new KSM page to add later to
1665*4882a593Smuzhiyun 				 * the dups of this stable_node.
1666*4882a593Smuzhiyun 				 */
1667*4882a593Smuzhiyun 				return NULL;
1668*4882a593Smuzhiyun 			}
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 			/*
1671*4882a593Smuzhiyun 			 * Lock and unlock the stable_node's page (which
1672*4882a593Smuzhiyun 			 * might already have been migrated) so that page
1673*4882a593Smuzhiyun 			 * migration is sure to notice its raised count.
1674*4882a593Smuzhiyun 			 * It would be more elegant to return stable_node
1675*4882a593Smuzhiyun 			 * than kpage, but that involves more changes.
1676*4882a593Smuzhiyun 			 */
1677*4882a593Smuzhiyun 			tree_page = get_ksm_page(stable_node_dup,
1678*4882a593Smuzhiyun 						 GET_KSM_PAGE_TRYLOCK);
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 			if (PTR_ERR(tree_page) == -EBUSY)
1681*4882a593Smuzhiyun 				return ERR_PTR(-EBUSY);
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 			if (unlikely(!tree_page))
1684*4882a593Smuzhiyun 				/*
1685*4882a593Smuzhiyun 				 * The tree may have been rebalanced,
1686*4882a593Smuzhiyun 				 * so re-evaluate parent and new.
1687*4882a593Smuzhiyun 				 */
1688*4882a593Smuzhiyun 				goto again;
1689*4882a593Smuzhiyun 			unlock_page(tree_page);
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun 			if (get_kpfn_nid(stable_node_dup->kpfn) !=
1692*4882a593Smuzhiyun 			    NUMA(stable_node_dup->nid)) {
1693*4882a593Smuzhiyun 				put_page(tree_page);
1694*4882a593Smuzhiyun 				goto replace;
1695*4882a593Smuzhiyun 			}
1696*4882a593Smuzhiyun 			return tree_page;
1697*4882a593Smuzhiyun 		}
1698*4882a593Smuzhiyun 	}
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	if (!page_node)
1701*4882a593Smuzhiyun 		return NULL;
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	list_del(&page_node->list);
1704*4882a593Smuzhiyun 	DO_NUMA(page_node->nid = nid);
1705*4882a593Smuzhiyun 	rb_link_node(&page_node->node, parent, new);
1706*4882a593Smuzhiyun 	rb_insert_color(&page_node->node, root);
1707*4882a593Smuzhiyun out:
1708*4882a593Smuzhiyun 	if (is_page_sharing_candidate(page_node)) {
1709*4882a593Smuzhiyun 		get_page(page);
1710*4882a593Smuzhiyun 		return page;
1711*4882a593Smuzhiyun 	} else
1712*4882a593Smuzhiyun 		return NULL;
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun replace:
1715*4882a593Smuzhiyun 	/*
1716*4882a593Smuzhiyun 	 * If stable_node was a chain and chain_prune collapsed it,
1717*4882a593Smuzhiyun 	 * stable_node has been updated to be the new regular
1718*4882a593Smuzhiyun 	 * stable_node. A collapse of the chain is indistinguishable
1719*4882a593Smuzhiyun 	 * from the case there was no chain in the stable
1720*4882a593Smuzhiyun 	 * rbtree. Otherwise stable_node is the chain and
1721*4882a593Smuzhiyun 	 * stable_node_dup is the dup to replace.
1722*4882a593Smuzhiyun 	 */
1723*4882a593Smuzhiyun 	if (stable_node_dup == stable_node) {
1724*4882a593Smuzhiyun 		VM_BUG_ON(is_stable_node_chain(stable_node_dup));
1725*4882a593Smuzhiyun 		VM_BUG_ON(is_stable_node_dup(stable_node_dup));
1726*4882a593Smuzhiyun 		/* there is no chain */
1727*4882a593Smuzhiyun 		if (page_node) {
1728*4882a593Smuzhiyun 			VM_BUG_ON(page_node->head != &migrate_nodes);
1729*4882a593Smuzhiyun 			list_del(&page_node->list);
1730*4882a593Smuzhiyun 			DO_NUMA(page_node->nid = nid);
1731*4882a593Smuzhiyun 			rb_replace_node(&stable_node_dup->node,
1732*4882a593Smuzhiyun 					&page_node->node,
1733*4882a593Smuzhiyun 					root);
1734*4882a593Smuzhiyun 			if (is_page_sharing_candidate(page_node))
1735*4882a593Smuzhiyun 				get_page(page);
1736*4882a593Smuzhiyun 			else
1737*4882a593Smuzhiyun 				page = NULL;
1738*4882a593Smuzhiyun 		} else {
1739*4882a593Smuzhiyun 			rb_erase(&stable_node_dup->node, root);
1740*4882a593Smuzhiyun 			page = NULL;
1741*4882a593Smuzhiyun 		}
1742*4882a593Smuzhiyun 	} else {
1743*4882a593Smuzhiyun 		VM_BUG_ON(!is_stable_node_chain(stable_node));
1744*4882a593Smuzhiyun 		__stable_node_dup_del(stable_node_dup);
1745*4882a593Smuzhiyun 		if (page_node) {
1746*4882a593Smuzhiyun 			VM_BUG_ON(page_node->head != &migrate_nodes);
1747*4882a593Smuzhiyun 			list_del(&page_node->list);
1748*4882a593Smuzhiyun 			DO_NUMA(page_node->nid = nid);
1749*4882a593Smuzhiyun 			stable_node_chain_add_dup(page_node, stable_node);
1750*4882a593Smuzhiyun 			if (is_page_sharing_candidate(page_node))
1751*4882a593Smuzhiyun 				get_page(page);
1752*4882a593Smuzhiyun 			else
1753*4882a593Smuzhiyun 				page = NULL;
1754*4882a593Smuzhiyun 		} else {
1755*4882a593Smuzhiyun 			page = NULL;
1756*4882a593Smuzhiyun 		}
1757*4882a593Smuzhiyun 	}
1758*4882a593Smuzhiyun 	stable_node_dup->head = &migrate_nodes;
1759*4882a593Smuzhiyun 	list_add(&stable_node_dup->list, stable_node_dup->head);
1760*4882a593Smuzhiyun 	return page;
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun chain_append:
1763*4882a593Smuzhiyun 	/* stable_node_dup could be null if it reached the limit */
1764*4882a593Smuzhiyun 	if (!stable_node_dup)
1765*4882a593Smuzhiyun 		stable_node_dup = stable_node_any;
1766*4882a593Smuzhiyun 	/*
1767*4882a593Smuzhiyun 	 * If stable_node was a chain and chain_prune collapsed it,
1768*4882a593Smuzhiyun 	 * stable_node has been updated to be the new regular
1769*4882a593Smuzhiyun 	 * stable_node. A collapse of the chain is indistinguishable
1770*4882a593Smuzhiyun 	 * from the case there was no chain in the stable
1771*4882a593Smuzhiyun 	 * rbtree. Otherwise stable_node is the chain and
1772*4882a593Smuzhiyun 	 * stable_node_dup is the dup to replace.
1773*4882a593Smuzhiyun 	 */
1774*4882a593Smuzhiyun 	if (stable_node_dup == stable_node) {
1775*4882a593Smuzhiyun 		VM_BUG_ON(is_stable_node_chain(stable_node_dup));
1776*4882a593Smuzhiyun 		VM_BUG_ON(is_stable_node_dup(stable_node_dup));
1777*4882a593Smuzhiyun 		/* chain is missing so create it */
1778*4882a593Smuzhiyun 		stable_node = alloc_stable_node_chain(stable_node_dup,
1779*4882a593Smuzhiyun 						      root);
1780*4882a593Smuzhiyun 		if (!stable_node)
1781*4882a593Smuzhiyun 			return NULL;
1782*4882a593Smuzhiyun 	}
1783*4882a593Smuzhiyun 	/*
1784*4882a593Smuzhiyun 	 * Add this stable_node dup that was
1785*4882a593Smuzhiyun 	 * migrated to the stable_node chain
1786*4882a593Smuzhiyun 	 * of the current nid for this page
1787*4882a593Smuzhiyun 	 * content.
1788*4882a593Smuzhiyun 	 */
1789*4882a593Smuzhiyun 	VM_BUG_ON(!is_stable_node_chain(stable_node));
1790*4882a593Smuzhiyun 	VM_BUG_ON(!is_stable_node_dup(stable_node_dup));
1791*4882a593Smuzhiyun 	VM_BUG_ON(page_node->head != &migrate_nodes);
1792*4882a593Smuzhiyun 	list_del(&page_node->list);
1793*4882a593Smuzhiyun 	DO_NUMA(page_node->nid = nid);
1794*4882a593Smuzhiyun 	stable_node_chain_add_dup(page_node, stable_node);
1795*4882a593Smuzhiyun 	goto out;
1796*4882a593Smuzhiyun }
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun /*
1799*4882a593Smuzhiyun  * stable_tree_insert - insert stable tree node pointing to new ksm page
1800*4882a593Smuzhiyun  * into the stable tree.
1801*4882a593Smuzhiyun  *
1802*4882a593Smuzhiyun  * This function returns the stable tree node just allocated on success,
1803*4882a593Smuzhiyun  * NULL otherwise.
1804*4882a593Smuzhiyun  */
stable_tree_insert(struct page * kpage)1805*4882a593Smuzhiyun static struct stable_node *stable_tree_insert(struct page *kpage)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun 	int nid;
1808*4882a593Smuzhiyun 	unsigned long kpfn;
1809*4882a593Smuzhiyun 	struct rb_root *root;
1810*4882a593Smuzhiyun 	struct rb_node **new;
1811*4882a593Smuzhiyun 	struct rb_node *parent;
1812*4882a593Smuzhiyun 	struct stable_node *stable_node, *stable_node_dup, *stable_node_any;
1813*4882a593Smuzhiyun 	bool need_chain = false;
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	kpfn = page_to_pfn(kpage);
1816*4882a593Smuzhiyun 	nid = get_kpfn_nid(kpfn);
1817*4882a593Smuzhiyun 	root = root_stable_tree + nid;
1818*4882a593Smuzhiyun again:
1819*4882a593Smuzhiyun 	parent = NULL;
1820*4882a593Smuzhiyun 	new = &root->rb_node;
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	while (*new) {
1823*4882a593Smuzhiyun 		struct page *tree_page;
1824*4882a593Smuzhiyun 		int ret;
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 		cond_resched();
1827*4882a593Smuzhiyun 		stable_node = rb_entry(*new, struct stable_node, node);
1828*4882a593Smuzhiyun 		stable_node_any = NULL;
1829*4882a593Smuzhiyun 		tree_page = chain(&stable_node_dup, stable_node, root);
1830*4882a593Smuzhiyun 		if (!stable_node_dup) {
1831*4882a593Smuzhiyun 			/*
1832*4882a593Smuzhiyun 			 * Either all stable_node dups were full in
1833*4882a593Smuzhiyun 			 * this stable_node chain, or this chain was
1834*4882a593Smuzhiyun 			 * empty and should be rb_erased.
1835*4882a593Smuzhiyun 			 */
1836*4882a593Smuzhiyun 			stable_node_any = stable_node_dup_any(stable_node,
1837*4882a593Smuzhiyun 							      root);
1838*4882a593Smuzhiyun 			if (!stable_node_any) {
1839*4882a593Smuzhiyun 				/* rb_erase just run */
1840*4882a593Smuzhiyun 				goto again;
1841*4882a593Smuzhiyun 			}
1842*4882a593Smuzhiyun 			/*
1843*4882a593Smuzhiyun 			 * Take any of the stable_node dups page of
1844*4882a593Smuzhiyun 			 * this stable_node chain to let the tree walk
1845*4882a593Smuzhiyun 			 * continue. All KSM pages belonging to the
1846*4882a593Smuzhiyun 			 * stable_node dups in a stable_node chain
1847*4882a593Smuzhiyun 			 * have the same content and they're
1848*4882a593Smuzhiyun 			 * write protected at all times. Any will work
1849*4882a593Smuzhiyun 			 * fine to continue the walk.
1850*4882a593Smuzhiyun 			 */
1851*4882a593Smuzhiyun 			tree_page = get_ksm_page(stable_node_any,
1852*4882a593Smuzhiyun 						 GET_KSM_PAGE_NOLOCK);
1853*4882a593Smuzhiyun 		}
1854*4882a593Smuzhiyun 		VM_BUG_ON(!stable_node_dup ^ !!stable_node_any);
1855*4882a593Smuzhiyun 		if (!tree_page) {
1856*4882a593Smuzhiyun 			/*
1857*4882a593Smuzhiyun 			 * If we walked over a stale stable_node,
1858*4882a593Smuzhiyun 			 * get_ksm_page() will call rb_erase() and it
1859*4882a593Smuzhiyun 			 * may rebalance the tree from under us. So
1860*4882a593Smuzhiyun 			 * restart the search from scratch. Returning
1861*4882a593Smuzhiyun 			 * NULL would be safe too, but we'd generate
1862*4882a593Smuzhiyun 			 * false negative insertions just because some
1863*4882a593Smuzhiyun 			 * stable_node was stale.
1864*4882a593Smuzhiyun 			 */
1865*4882a593Smuzhiyun 			goto again;
1866*4882a593Smuzhiyun 		}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 		ret = memcmp_pages(kpage, tree_page);
1869*4882a593Smuzhiyun 		put_page(tree_page);
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 		parent = *new;
1872*4882a593Smuzhiyun 		if (ret < 0)
1873*4882a593Smuzhiyun 			new = &parent->rb_left;
1874*4882a593Smuzhiyun 		else if (ret > 0)
1875*4882a593Smuzhiyun 			new = &parent->rb_right;
1876*4882a593Smuzhiyun 		else {
1877*4882a593Smuzhiyun 			need_chain = true;
1878*4882a593Smuzhiyun 			break;
1879*4882a593Smuzhiyun 		}
1880*4882a593Smuzhiyun 	}
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	stable_node_dup = alloc_stable_node();
1883*4882a593Smuzhiyun 	if (!stable_node_dup)
1884*4882a593Smuzhiyun 		return NULL;
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun 	INIT_HLIST_HEAD(&stable_node_dup->hlist);
1887*4882a593Smuzhiyun 	stable_node_dup->kpfn = kpfn;
1888*4882a593Smuzhiyun 	set_page_stable_node(kpage, stable_node_dup);
1889*4882a593Smuzhiyun 	stable_node_dup->rmap_hlist_len = 0;
1890*4882a593Smuzhiyun 	DO_NUMA(stable_node_dup->nid = nid);
1891*4882a593Smuzhiyun 	if (!need_chain) {
1892*4882a593Smuzhiyun 		rb_link_node(&stable_node_dup->node, parent, new);
1893*4882a593Smuzhiyun 		rb_insert_color(&stable_node_dup->node, root);
1894*4882a593Smuzhiyun 	} else {
1895*4882a593Smuzhiyun 		if (!is_stable_node_chain(stable_node)) {
1896*4882a593Smuzhiyun 			struct stable_node *orig = stable_node;
1897*4882a593Smuzhiyun 			/* chain is missing so create it */
1898*4882a593Smuzhiyun 			stable_node = alloc_stable_node_chain(orig, root);
1899*4882a593Smuzhiyun 			if (!stable_node) {
1900*4882a593Smuzhiyun 				free_stable_node(stable_node_dup);
1901*4882a593Smuzhiyun 				return NULL;
1902*4882a593Smuzhiyun 			}
1903*4882a593Smuzhiyun 		}
1904*4882a593Smuzhiyun 		stable_node_chain_add_dup(stable_node_dup, stable_node);
1905*4882a593Smuzhiyun 	}
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	return stable_node_dup;
1908*4882a593Smuzhiyun }
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun /*
1911*4882a593Smuzhiyun  * unstable_tree_search_insert - search for identical page,
1912*4882a593Smuzhiyun  * else insert rmap_item into the unstable tree.
1913*4882a593Smuzhiyun  *
1914*4882a593Smuzhiyun  * This function searches for a page in the unstable tree identical to the
1915*4882a593Smuzhiyun  * page currently being scanned; and if no identical page is found in the
1916*4882a593Smuzhiyun  * tree, we insert rmap_item as a new object into the unstable tree.
1917*4882a593Smuzhiyun  *
1918*4882a593Smuzhiyun  * This function returns pointer to rmap_item found to be identical
1919*4882a593Smuzhiyun  * to the currently scanned page, NULL otherwise.
1920*4882a593Smuzhiyun  *
1921*4882a593Smuzhiyun  * This function does both searching and inserting, because they share
1922*4882a593Smuzhiyun  * the same walking algorithm in an rbtree.
1923*4882a593Smuzhiyun  */
1924*4882a593Smuzhiyun static
unstable_tree_search_insert(struct rmap_item * rmap_item,struct page * page,struct page ** tree_pagep)1925*4882a593Smuzhiyun struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
1926*4882a593Smuzhiyun 					      struct page *page,
1927*4882a593Smuzhiyun 					      struct page **tree_pagep)
1928*4882a593Smuzhiyun {
1929*4882a593Smuzhiyun 	struct rb_node **new;
1930*4882a593Smuzhiyun 	struct rb_root *root;
1931*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
1932*4882a593Smuzhiyun 	int nid;
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun 	nid = get_kpfn_nid(page_to_pfn(page));
1935*4882a593Smuzhiyun 	root = root_unstable_tree + nid;
1936*4882a593Smuzhiyun 	new = &root->rb_node;
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 	while (*new) {
1939*4882a593Smuzhiyun 		struct rmap_item *tree_rmap_item;
1940*4882a593Smuzhiyun 		struct page *tree_page;
1941*4882a593Smuzhiyun 		int ret;
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 		cond_resched();
1944*4882a593Smuzhiyun 		tree_rmap_item = rb_entry(*new, struct rmap_item, node);
1945*4882a593Smuzhiyun 		tree_page = get_mergeable_page(tree_rmap_item);
1946*4882a593Smuzhiyun 		if (!tree_page)
1947*4882a593Smuzhiyun 			return NULL;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 		/*
1950*4882a593Smuzhiyun 		 * Don't substitute a ksm page for a forked page.
1951*4882a593Smuzhiyun 		 */
1952*4882a593Smuzhiyun 		if (page == tree_page) {
1953*4882a593Smuzhiyun 			put_user_page(tree_page);
1954*4882a593Smuzhiyun 			return NULL;
1955*4882a593Smuzhiyun 		}
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 		ret = memcmp_pages(page, tree_page);
1958*4882a593Smuzhiyun 
1959*4882a593Smuzhiyun 		parent = *new;
1960*4882a593Smuzhiyun 		if (ret < 0) {
1961*4882a593Smuzhiyun 			put_user_page(tree_page);
1962*4882a593Smuzhiyun 			new = &parent->rb_left;
1963*4882a593Smuzhiyun 		} else if (ret > 0) {
1964*4882a593Smuzhiyun 			put_user_page(tree_page);
1965*4882a593Smuzhiyun 			new = &parent->rb_right;
1966*4882a593Smuzhiyun 		} else if (!ksm_merge_across_nodes &&
1967*4882a593Smuzhiyun 			   page_to_nid(tree_page) != nid) {
1968*4882a593Smuzhiyun 			/*
1969*4882a593Smuzhiyun 			 * If tree_page has been migrated to another NUMA node,
1970*4882a593Smuzhiyun 			 * it will be flushed out and put in the right unstable
1971*4882a593Smuzhiyun 			 * tree next time: only merge with it when across_nodes.
1972*4882a593Smuzhiyun 			 */
1973*4882a593Smuzhiyun 			put_user_page(tree_page);
1974*4882a593Smuzhiyun 			return NULL;
1975*4882a593Smuzhiyun 		} else {
1976*4882a593Smuzhiyun 			*tree_pagep = tree_page;
1977*4882a593Smuzhiyun 			return tree_rmap_item;
1978*4882a593Smuzhiyun 		}
1979*4882a593Smuzhiyun 	}
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	rmap_item->address |= UNSTABLE_FLAG;
1982*4882a593Smuzhiyun 	rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
1983*4882a593Smuzhiyun 	DO_NUMA(rmap_item->nid = nid);
1984*4882a593Smuzhiyun 	rb_link_node(&rmap_item->node, parent, new);
1985*4882a593Smuzhiyun 	rb_insert_color(&rmap_item->node, root);
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun 	ksm_pages_unshared++;
1988*4882a593Smuzhiyun 	return NULL;
1989*4882a593Smuzhiyun }
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun /*
1992*4882a593Smuzhiyun  * stable_tree_append - add another rmap_item to the linked list of
1993*4882a593Smuzhiyun  * rmap_items hanging off a given node of the stable tree, all sharing
1994*4882a593Smuzhiyun  * the same ksm page.
1995*4882a593Smuzhiyun  */
stable_tree_append(struct rmap_item * rmap_item,struct stable_node * stable_node,bool max_page_sharing_bypass)1996*4882a593Smuzhiyun static void stable_tree_append(struct rmap_item *rmap_item,
1997*4882a593Smuzhiyun 			       struct stable_node *stable_node,
1998*4882a593Smuzhiyun 			       bool max_page_sharing_bypass)
1999*4882a593Smuzhiyun {
2000*4882a593Smuzhiyun 	/*
2001*4882a593Smuzhiyun 	 * rmap won't find this mapping if we don't insert the
2002*4882a593Smuzhiyun 	 * rmap_item in the right stable_node
2003*4882a593Smuzhiyun 	 * duplicate. page_migration could break later if rmap breaks,
2004*4882a593Smuzhiyun 	 * so we can as well crash here. We really need to check for
2005*4882a593Smuzhiyun 	 * rmap_hlist_len == STABLE_NODE_CHAIN, but we can as well check
2006*4882a593Smuzhiyun 	 * for other negative values as an underflow if detected here
2007*4882a593Smuzhiyun 	 * for the first time (and not when decreasing rmap_hlist_len)
2008*4882a593Smuzhiyun 	 * would be sign of memory corruption in the stable_node.
2009*4882a593Smuzhiyun 	 */
2010*4882a593Smuzhiyun 	BUG_ON(stable_node->rmap_hlist_len < 0);
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	stable_node->rmap_hlist_len++;
2013*4882a593Smuzhiyun 	if (!max_page_sharing_bypass)
2014*4882a593Smuzhiyun 		/* possibly non fatal but unexpected overflow, only warn */
2015*4882a593Smuzhiyun 		WARN_ON_ONCE(stable_node->rmap_hlist_len >
2016*4882a593Smuzhiyun 			     ksm_max_page_sharing);
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun 	rmap_item->head = stable_node;
2019*4882a593Smuzhiyun 	rmap_item->address |= STABLE_FLAG;
2020*4882a593Smuzhiyun 	hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun 	if (rmap_item->hlist.next)
2023*4882a593Smuzhiyun 		ksm_pages_sharing++;
2024*4882a593Smuzhiyun 	else
2025*4882a593Smuzhiyun 		ksm_pages_shared++;
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun /*
2029*4882a593Smuzhiyun  * cmp_and_merge_page - first see if page can be merged into the stable tree;
2030*4882a593Smuzhiyun  * if not, compare checksum to previous and if it's the same, see if page can
2031*4882a593Smuzhiyun  * be inserted into the unstable tree, or merged with a page already there and
2032*4882a593Smuzhiyun  * both transferred to the stable tree.
2033*4882a593Smuzhiyun  *
2034*4882a593Smuzhiyun  * @page: the page that we are searching identical page to.
2035*4882a593Smuzhiyun  * @rmap_item: the reverse mapping into the virtual address of this page
2036*4882a593Smuzhiyun  */
cmp_and_merge_page(struct page * page,struct rmap_item * rmap_item)2037*4882a593Smuzhiyun static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
2038*4882a593Smuzhiyun {
2039*4882a593Smuzhiyun 	struct mm_struct *mm = rmap_item->mm;
2040*4882a593Smuzhiyun 	struct rmap_item *tree_rmap_item;
2041*4882a593Smuzhiyun 	struct page *tree_page = NULL;
2042*4882a593Smuzhiyun 	struct stable_node *stable_node;
2043*4882a593Smuzhiyun 	struct page *kpage;
2044*4882a593Smuzhiyun 	unsigned int checksum;
2045*4882a593Smuzhiyun 	int err;
2046*4882a593Smuzhiyun 	bool max_page_sharing_bypass = false;
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 	stable_node = page_stable_node(page);
2049*4882a593Smuzhiyun 	if (stable_node) {
2050*4882a593Smuzhiyun 		if (stable_node->head != &migrate_nodes &&
2051*4882a593Smuzhiyun 		    get_kpfn_nid(READ_ONCE(stable_node->kpfn)) !=
2052*4882a593Smuzhiyun 		    NUMA(stable_node->nid)) {
2053*4882a593Smuzhiyun 			stable_node_dup_del(stable_node);
2054*4882a593Smuzhiyun 			stable_node->head = &migrate_nodes;
2055*4882a593Smuzhiyun 			list_add(&stable_node->list, stable_node->head);
2056*4882a593Smuzhiyun 		}
2057*4882a593Smuzhiyun 		if (stable_node->head != &migrate_nodes &&
2058*4882a593Smuzhiyun 		    rmap_item->head == stable_node)
2059*4882a593Smuzhiyun 			return;
2060*4882a593Smuzhiyun 		/*
2061*4882a593Smuzhiyun 		 * If it's a KSM fork, allow it to go over the sharing limit
2062*4882a593Smuzhiyun 		 * without warnings.
2063*4882a593Smuzhiyun 		 */
2064*4882a593Smuzhiyun 		if (!is_page_sharing_candidate(stable_node))
2065*4882a593Smuzhiyun 			max_page_sharing_bypass = true;
2066*4882a593Smuzhiyun 	}
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun 	/* We first start with searching the page inside the stable tree */
2069*4882a593Smuzhiyun 	kpage = stable_tree_search(page);
2070*4882a593Smuzhiyun 	if (kpage == page && rmap_item->head == stable_node) {
2071*4882a593Smuzhiyun 		put_page(kpage);
2072*4882a593Smuzhiyun 		return;
2073*4882a593Smuzhiyun 	}
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun 	remove_rmap_item_from_tree(rmap_item);
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 	if (kpage) {
2078*4882a593Smuzhiyun 		if (PTR_ERR(kpage) == -EBUSY)
2079*4882a593Smuzhiyun 			return;
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun 		err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
2082*4882a593Smuzhiyun 		if (!err) {
2083*4882a593Smuzhiyun 			/*
2084*4882a593Smuzhiyun 			 * The page was successfully merged:
2085*4882a593Smuzhiyun 			 * add its rmap_item to the stable tree.
2086*4882a593Smuzhiyun 			 */
2087*4882a593Smuzhiyun 			lock_page(kpage);
2088*4882a593Smuzhiyun 			stable_tree_append(rmap_item, page_stable_node(kpage),
2089*4882a593Smuzhiyun 					   max_page_sharing_bypass);
2090*4882a593Smuzhiyun 			unlock_page(kpage);
2091*4882a593Smuzhiyun 		}
2092*4882a593Smuzhiyun 		put_page(kpage);
2093*4882a593Smuzhiyun 		return;
2094*4882a593Smuzhiyun 	}
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 	/*
2097*4882a593Smuzhiyun 	 * If the hash value of the page has changed from the last time
2098*4882a593Smuzhiyun 	 * we calculated it, this page is changing frequently: therefore we
2099*4882a593Smuzhiyun 	 * don't want to insert it in the unstable tree, and we don't want
2100*4882a593Smuzhiyun 	 * to waste our time searching for something identical to it there.
2101*4882a593Smuzhiyun 	 */
2102*4882a593Smuzhiyun 	checksum = calc_checksum(page);
2103*4882a593Smuzhiyun 	if (rmap_item->oldchecksum != checksum) {
2104*4882a593Smuzhiyun 		rmap_item->oldchecksum = checksum;
2105*4882a593Smuzhiyun 		return;
2106*4882a593Smuzhiyun 	}
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 	/*
2109*4882a593Smuzhiyun 	 * Same checksum as an empty page. We attempt to merge it with the
2110*4882a593Smuzhiyun 	 * appropriate zero page if the user enabled this via sysfs.
2111*4882a593Smuzhiyun 	 */
2112*4882a593Smuzhiyun 	if (ksm_use_zero_pages && (checksum == zero_checksum)) {
2113*4882a593Smuzhiyun 		struct vm_area_struct *vma;
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 		mmap_read_lock(mm);
2116*4882a593Smuzhiyun 		vma = find_mergeable_vma(mm, rmap_item->address);
2117*4882a593Smuzhiyun 		if (vma) {
2118*4882a593Smuzhiyun 			err = try_to_merge_one_page(vma, page,
2119*4882a593Smuzhiyun 					ZERO_PAGE(rmap_item->address));
2120*4882a593Smuzhiyun 		} else {
2121*4882a593Smuzhiyun 			/*
2122*4882a593Smuzhiyun 			 * If the vma is out of date, we do not need to
2123*4882a593Smuzhiyun 			 * continue.
2124*4882a593Smuzhiyun 			 */
2125*4882a593Smuzhiyun 			err = 0;
2126*4882a593Smuzhiyun 		}
2127*4882a593Smuzhiyun 		mmap_read_unlock(mm);
2128*4882a593Smuzhiyun 		/*
2129*4882a593Smuzhiyun 		 * In case of failure, the page was not really empty, so we
2130*4882a593Smuzhiyun 		 * need to continue. Otherwise we're done.
2131*4882a593Smuzhiyun 		 */
2132*4882a593Smuzhiyun 		if (!err)
2133*4882a593Smuzhiyun 			return;
2134*4882a593Smuzhiyun 	}
2135*4882a593Smuzhiyun 	tree_rmap_item =
2136*4882a593Smuzhiyun 		unstable_tree_search_insert(rmap_item, page, &tree_page);
2137*4882a593Smuzhiyun 	if (tree_rmap_item) {
2138*4882a593Smuzhiyun 		bool split;
2139*4882a593Smuzhiyun 
2140*4882a593Smuzhiyun 		kpage = try_to_merge_two_pages(rmap_item, page,
2141*4882a593Smuzhiyun 						tree_rmap_item, tree_page);
2142*4882a593Smuzhiyun 		/*
2143*4882a593Smuzhiyun 		 * If both pages we tried to merge belong to the same compound
2144*4882a593Smuzhiyun 		 * page, then we actually ended up increasing the reference
2145*4882a593Smuzhiyun 		 * count of the same compound page twice, and split_huge_page
2146*4882a593Smuzhiyun 		 * failed.
2147*4882a593Smuzhiyun 		 * Here we set a flag if that happened, and we use it later to
2148*4882a593Smuzhiyun 		 * try split_huge_page again. Since we call put_page right
2149*4882a593Smuzhiyun 		 * afterwards, the reference count will be correct and
2150*4882a593Smuzhiyun 		 * split_huge_page should succeed.
2151*4882a593Smuzhiyun 		 */
2152*4882a593Smuzhiyun 		split = PageTransCompound(page)
2153*4882a593Smuzhiyun 			&& compound_head(page) == compound_head(tree_page);
2154*4882a593Smuzhiyun 		put_user_page(tree_page);
2155*4882a593Smuzhiyun 		if (kpage) {
2156*4882a593Smuzhiyun 			/*
2157*4882a593Smuzhiyun 			 * The pages were successfully merged: insert new
2158*4882a593Smuzhiyun 			 * node in the stable tree and add both rmap_items.
2159*4882a593Smuzhiyun 			 */
2160*4882a593Smuzhiyun 			lock_page(kpage);
2161*4882a593Smuzhiyun 			stable_node = stable_tree_insert(kpage);
2162*4882a593Smuzhiyun 			if (stable_node) {
2163*4882a593Smuzhiyun 				stable_tree_append(tree_rmap_item, stable_node,
2164*4882a593Smuzhiyun 						   false);
2165*4882a593Smuzhiyun 				stable_tree_append(rmap_item, stable_node,
2166*4882a593Smuzhiyun 						   false);
2167*4882a593Smuzhiyun 			}
2168*4882a593Smuzhiyun 			unlock_page(kpage);
2169*4882a593Smuzhiyun 
2170*4882a593Smuzhiyun 			/*
2171*4882a593Smuzhiyun 			 * If we fail to insert the page into the stable tree,
2172*4882a593Smuzhiyun 			 * we will have 2 virtual addresses that are pointing
2173*4882a593Smuzhiyun 			 * to a ksm page left outside the stable tree,
2174*4882a593Smuzhiyun 			 * in which case we need to break_cow on both.
2175*4882a593Smuzhiyun 			 */
2176*4882a593Smuzhiyun 			if (!stable_node) {
2177*4882a593Smuzhiyun 				break_cow(tree_rmap_item);
2178*4882a593Smuzhiyun 				break_cow(rmap_item);
2179*4882a593Smuzhiyun 			}
2180*4882a593Smuzhiyun 		} else if (split) {
2181*4882a593Smuzhiyun 			/*
2182*4882a593Smuzhiyun 			 * We are here if we tried to merge two pages and
2183*4882a593Smuzhiyun 			 * failed because they both belonged to the same
2184*4882a593Smuzhiyun 			 * compound page. We will split the page now, but no
2185*4882a593Smuzhiyun 			 * merging will take place.
2186*4882a593Smuzhiyun 			 * We do not want to add the cost of a full lock; if
2187*4882a593Smuzhiyun 			 * the page is locked, it is better to skip it and
2188*4882a593Smuzhiyun 			 * perhaps try again later.
2189*4882a593Smuzhiyun 			 */
2190*4882a593Smuzhiyun 			if (!trylock_page(page))
2191*4882a593Smuzhiyun 				return;
2192*4882a593Smuzhiyun 			split_huge_page(page);
2193*4882a593Smuzhiyun 			unlock_page(page);
2194*4882a593Smuzhiyun 		}
2195*4882a593Smuzhiyun 	}
2196*4882a593Smuzhiyun }
2197*4882a593Smuzhiyun 
get_next_rmap_item(struct mm_slot * mm_slot,struct rmap_item ** rmap_list,unsigned long addr)2198*4882a593Smuzhiyun static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
2199*4882a593Smuzhiyun 					    struct rmap_item **rmap_list,
2200*4882a593Smuzhiyun 					    unsigned long addr)
2201*4882a593Smuzhiyun {
2202*4882a593Smuzhiyun 	struct rmap_item *rmap_item;
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 	while (*rmap_list) {
2205*4882a593Smuzhiyun 		rmap_item = *rmap_list;
2206*4882a593Smuzhiyun 		if ((rmap_item->address & PAGE_MASK) == addr)
2207*4882a593Smuzhiyun 			return rmap_item;
2208*4882a593Smuzhiyun 		if (rmap_item->address > addr)
2209*4882a593Smuzhiyun 			break;
2210*4882a593Smuzhiyun 		*rmap_list = rmap_item->rmap_list;
2211*4882a593Smuzhiyun 		remove_rmap_item_from_tree(rmap_item);
2212*4882a593Smuzhiyun 		free_rmap_item(rmap_item);
2213*4882a593Smuzhiyun 	}
2214*4882a593Smuzhiyun 
2215*4882a593Smuzhiyun 	rmap_item = alloc_rmap_item();
2216*4882a593Smuzhiyun 	if (rmap_item) {
2217*4882a593Smuzhiyun 		/* It has already been zeroed */
2218*4882a593Smuzhiyun 		rmap_item->mm = mm_slot->mm;
2219*4882a593Smuzhiyun 		rmap_item->address = addr;
2220*4882a593Smuzhiyun 		rmap_item->rmap_list = *rmap_list;
2221*4882a593Smuzhiyun 		*rmap_list = rmap_item;
2222*4882a593Smuzhiyun 	}
2223*4882a593Smuzhiyun 	return rmap_item;
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun 
scan_get_next_rmap_item(struct page ** page)2226*4882a593Smuzhiyun static struct rmap_item *scan_get_next_rmap_item(struct page **page)
2227*4882a593Smuzhiyun {
2228*4882a593Smuzhiyun 	struct mm_struct *mm;
2229*4882a593Smuzhiyun 	struct mm_slot *slot;
2230*4882a593Smuzhiyun 	struct vm_area_struct *vma;
2231*4882a593Smuzhiyun 	struct rmap_item *rmap_item;
2232*4882a593Smuzhiyun 	int nid;
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun 	if (list_empty(&ksm_mm_head.mm_list))
2235*4882a593Smuzhiyun 		return NULL;
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun 	slot = ksm_scan.mm_slot;
2238*4882a593Smuzhiyun 	if (slot == &ksm_mm_head) {
2239*4882a593Smuzhiyun 		/*
2240*4882a593Smuzhiyun 		 * A number of pages can hang around indefinitely on per-cpu
2241*4882a593Smuzhiyun 		 * pagevecs, raised page count preventing write_protect_page
2242*4882a593Smuzhiyun 		 * from merging them.  Though it doesn't really matter much,
2243*4882a593Smuzhiyun 		 * it is puzzling to see some stuck in pages_volatile until
2244*4882a593Smuzhiyun 		 * other activity jostles them out, and they also prevented
2245*4882a593Smuzhiyun 		 * LTP's KSM test from succeeding deterministically; so drain
2246*4882a593Smuzhiyun 		 * them here (here rather than on entry to ksm_do_scan(),
2247*4882a593Smuzhiyun 		 * so we don't IPI too often when pages_to_scan is set low).
2248*4882a593Smuzhiyun 		 */
2249*4882a593Smuzhiyun 		lru_add_drain_all();
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun 		/*
2252*4882a593Smuzhiyun 		 * Whereas stale stable_nodes on the stable_tree itself
2253*4882a593Smuzhiyun 		 * get pruned in the regular course of stable_tree_search(),
2254*4882a593Smuzhiyun 		 * those moved out to the migrate_nodes list can accumulate:
2255*4882a593Smuzhiyun 		 * so prune them once before each full scan.
2256*4882a593Smuzhiyun 		 */
2257*4882a593Smuzhiyun 		if (!ksm_merge_across_nodes) {
2258*4882a593Smuzhiyun 			struct stable_node *stable_node, *next;
2259*4882a593Smuzhiyun 			struct page *page;
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 			list_for_each_entry_safe(stable_node, next,
2262*4882a593Smuzhiyun 						 &migrate_nodes, list) {
2263*4882a593Smuzhiyun 				page = get_ksm_page(stable_node,
2264*4882a593Smuzhiyun 						    GET_KSM_PAGE_NOLOCK);
2265*4882a593Smuzhiyun 				if (page)
2266*4882a593Smuzhiyun 					put_page(page);
2267*4882a593Smuzhiyun 				cond_resched();
2268*4882a593Smuzhiyun 			}
2269*4882a593Smuzhiyun 		}
2270*4882a593Smuzhiyun 
2271*4882a593Smuzhiyun 		for (nid = 0; nid < ksm_nr_node_ids; nid++)
2272*4882a593Smuzhiyun 			root_unstable_tree[nid] = RB_ROOT;
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 		spin_lock(&ksm_mmlist_lock);
2275*4882a593Smuzhiyun 		slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
2276*4882a593Smuzhiyun 		ksm_scan.mm_slot = slot;
2277*4882a593Smuzhiyun 		spin_unlock(&ksm_mmlist_lock);
2278*4882a593Smuzhiyun 		/*
2279*4882a593Smuzhiyun 		 * Although we tested list_empty() above, a racing __ksm_exit
2280*4882a593Smuzhiyun 		 * of the last mm on the list may have removed it since then.
2281*4882a593Smuzhiyun 		 */
2282*4882a593Smuzhiyun 		if (slot == &ksm_mm_head)
2283*4882a593Smuzhiyun 			return NULL;
2284*4882a593Smuzhiyun next_mm:
2285*4882a593Smuzhiyun 		ksm_scan.address = 0;
2286*4882a593Smuzhiyun 		ksm_scan.rmap_list = &slot->rmap_list;
2287*4882a593Smuzhiyun 	}
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	mm = slot->mm;
2290*4882a593Smuzhiyun 	mmap_read_lock(mm);
2291*4882a593Smuzhiyun 	if (ksm_test_exit(mm))
2292*4882a593Smuzhiyun 		vma = NULL;
2293*4882a593Smuzhiyun 	else
2294*4882a593Smuzhiyun 		vma = find_vma(mm, ksm_scan.address);
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun 	for (; vma; vma = vma->vm_next) {
2297*4882a593Smuzhiyun 		if (!(vma->vm_flags & VM_MERGEABLE))
2298*4882a593Smuzhiyun 			continue;
2299*4882a593Smuzhiyun 		if (ksm_scan.address < vma->vm_start)
2300*4882a593Smuzhiyun 			ksm_scan.address = vma->vm_start;
2301*4882a593Smuzhiyun 		if (!vma->anon_vma)
2302*4882a593Smuzhiyun 			ksm_scan.address = vma->vm_end;
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 		while (ksm_scan.address < vma->vm_end) {
2305*4882a593Smuzhiyun 			if (ksm_test_exit(mm))
2306*4882a593Smuzhiyun 				break;
2307*4882a593Smuzhiyun 			*page = follow_page(vma, ksm_scan.address, FOLL_GET);
2308*4882a593Smuzhiyun 			if (IS_ERR_OR_NULL(*page)) {
2309*4882a593Smuzhiyun 				ksm_scan.address += PAGE_SIZE;
2310*4882a593Smuzhiyun 				cond_resched();
2311*4882a593Smuzhiyun 				continue;
2312*4882a593Smuzhiyun 			}
2313*4882a593Smuzhiyun 			if (PageAnon(*page)) {
2314*4882a593Smuzhiyun 				flush_anon_page(vma, *page, ksm_scan.address);
2315*4882a593Smuzhiyun 				flush_dcache_page(*page);
2316*4882a593Smuzhiyun 				rmap_item = get_next_rmap_item(slot,
2317*4882a593Smuzhiyun 					ksm_scan.rmap_list, ksm_scan.address);
2318*4882a593Smuzhiyun 				if (rmap_item) {
2319*4882a593Smuzhiyun 					ksm_scan.rmap_list =
2320*4882a593Smuzhiyun 							&rmap_item->rmap_list;
2321*4882a593Smuzhiyun 					ksm_scan.address += PAGE_SIZE;
2322*4882a593Smuzhiyun 				} else
2323*4882a593Smuzhiyun 					put_user_page(*page);
2324*4882a593Smuzhiyun 				mmap_read_unlock(mm);
2325*4882a593Smuzhiyun 				return rmap_item;
2326*4882a593Smuzhiyun 			}
2327*4882a593Smuzhiyun 			put_user_page(*page);
2328*4882a593Smuzhiyun 			ksm_scan.address += PAGE_SIZE;
2329*4882a593Smuzhiyun 			cond_resched();
2330*4882a593Smuzhiyun 		}
2331*4882a593Smuzhiyun 	}
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 	if (ksm_test_exit(mm)) {
2334*4882a593Smuzhiyun 		ksm_scan.address = 0;
2335*4882a593Smuzhiyun 		ksm_scan.rmap_list = &slot->rmap_list;
2336*4882a593Smuzhiyun 	}
2337*4882a593Smuzhiyun 	/*
2338*4882a593Smuzhiyun 	 * Nuke all the rmap_items that are above this current rmap:
2339*4882a593Smuzhiyun 	 * because there were no VM_MERGEABLE vmas with such addresses.
2340*4882a593Smuzhiyun 	 */
2341*4882a593Smuzhiyun 	remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	spin_lock(&ksm_mmlist_lock);
2344*4882a593Smuzhiyun 	ksm_scan.mm_slot = list_entry(slot->mm_list.next,
2345*4882a593Smuzhiyun 						struct mm_slot, mm_list);
2346*4882a593Smuzhiyun 	if (ksm_scan.address == 0) {
2347*4882a593Smuzhiyun 		/*
2348*4882a593Smuzhiyun 		 * We've completed a full scan of all vmas, holding mmap_lock
2349*4882a593Smuzhiyun 		 * throughout, and found no VM_MERGEABLE: so do the same as
2350*4882a593Smuzhiyun 		 * __ksm_exit does to remove this mm from all our lists now.
2351*4882a593Smuzhiyun 		 * This applies either when cleaning up after __ksm_exit
2352*4882a593Smuzhiyun 		 * (but beware: we can reach here even before __ksm_exit),
2353*4882a593Smuzhiyun 		 * or when all VM_MERGEABLE areas have been unmapped (and
2354*4882a593Smuzhiyun 		 * mmap_lock then protects against race with MADV_MERGEABLE).
2355*4882a593Smuzhiyun 		 */
2356*4882a593Smuzhiyun 		hash_del(&slot->link);
2357*4882a593Smuzhiyun 		list_del(&slot->mm_list);
2358*4882a593Smuzhiyun 		spin_unlock(&ksm_mmlist_lock);
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun 		free_mm_slot(slot);
2361*4882a593Smuzhiyun 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
2362*4882a593Smuzhiyun 		mmap_read_unlock(mm);
2363*4882a593Smuzhiyun 		mmdrop(mm);
2364*4882a593Smuzhiyun 	} else {
2365*4882a593Smuzhiyun 		mmap_read_unlock(mm);
2366*4882a593Smuzhiyun 		/*
2367*4882a593Smuzhiyun 		 * mmap_read_unlock(mm) first because after
2368*4882a593Smuzhiyun 		 * spin_unlock(&ksm_mmlist_lock) run, the "mm" may
2369*4882a593Smuzhiyun 		 * already have been freed under us by __ksm_exit()
2370*4882a593Smuzhiyun 		 * because the "mm_slot" is still hashed and
2371*4882a593Smuzhiyun 		 * ksm_scan.mm_slot doesn't point to it anymore.
2372*4882a593Smuzhiyun 		 */
2373*4882a593Smuzhiyun 		spin_unlock(&ksm_mmlist_lock);
2374*4882a593Smuzhiyun 	}
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	/* Repeat until we've completed scanning the whole list */
2377*4882a593Smuzhiyun 	slot = ksm_scan.mm_slot;
2378*4882a593Smuzhiyun 	if (slot != &ksm_mm_head)
2379*4882a593Smuzhiyun 		goto next_mm;
2380*4882a593Smuzhiyun 
2381*4882a593Smuzhiyun 	ksm_scan.seqnr++;
2382*4882a593Smuzhiyun 	return NULL;
2383*4882a593Smuzhiyun }
2384*4882a593Smuzhiyun 
2385*4882a593Smuzhiyun /**
2386*4882a593Smuzhiyun  * ksm_do_scan  - the ksm scanner main worker function.
2387*4882a593Smuzhiyun  * @scan_npages:  number of pages we want to scan before we return.
2388*4882a593Smuzhiyun  */
ksm_do_scan(unsigned int scan_npages)2389*4882a593Smuzhiyun static void ksm_do_scan(unsigned int scan_npages)
2390*4882a593Smuzhiyun {
2391*4882a593Smuzhiyun 	struct rmap_item *rmap_item;
2392*4882a593Smuzhiyun 	struct page *page;
2393*4882a593Smuzhiyun 
2394*4882a593Smuzhiyun 	while (scan_npages-- && likely(!freezing(current))) {
2395*4882a593Smuzhiyun 		cond_resched();
2396*4882a593Smuzhiyun 		rmap_item = scan_get_next_rmap_item(&page);
2397*4882a593Smuzhiyun 		if (!rmap_item)
2398*4882a593Smuzhiyun 			return;
2399*4882a593Smuzhiyun 		cmp_and_merge_page(page, rmap_item);
2400*4882a593Smuzhiyun 		put_page(page);
2401*4882a593Smuzhiyun 	}
2402*4882a593Smuzhiyun }
2403*4882a593Smuzhiyun 
ksmd_should_run(void)2404*4882a593Smuzhiyun static int ksmd_should_run(void)
2405*4882a593Smuzhiyun {
2406*4882a593Smuzhiyun 	return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
2407*4882a593Smuzhiyun }
2408*4882a593Smuzhiyun 
ksm_scan_thread(void * nothing)2409*4882a593Smuzhiyun static int ksm_scan_thread(void *nothing)
2410*4882a593Smuzhiyun {
2411*4882a593Smuzhiyun 	unsigned int sleep_ms;
2412*4882a593Smuzhiyun 
2413*4882a593Smuzhiyun 	set_freezable();
2414*4882a593Smuzhiyun 	set_user_nice(current, 5);
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun 	while (!kthread_should_stop()) {
2417*4882a593Smuzhiyun 		mutex_lock(&ksm_thread_mutex);
2418*4882a593Smuzhiyun 		wait_while_offlining();
2419*4882a593Smuzhiyun 		if (ksmd_should_run())
2420*4882a593Smuzhiyun 			ksm_do_scan(ksm_thread_pages_to_scan);
2421*4882a593Smuzhiyun 		mutex_unlock(&ksm_thread_mutex);
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 		try_to_freeze();
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun 		if (ksmd_should_run()) {
2426*4882a593Smuzhiyun 			sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
2427*4882a593Smuzhiyun 			wait_event_interruptible_timeout(ksm_iter_wait,
2428*4882a593Smuzhiyun 				sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
2429*4882a593Smuzhiyun 				msecs_to_jiffies(sleep_ms));
2430*4882a593Smuzhiyun 		} else {
2431*4882a593Smuzhiyun 			wait_event_freezable(ksm_thread_wait,
2432*4882a593Smuzhiyun 				ksmd_should_run() || kthread_should_stop());
2433*4882a593Smuzhiyun 		}
2434*4882a593Smuzhiyun 	}
2435*4882a593Smuzhiyun 	return 0;
2436*4882a593Smuzhiyun }
2437*4882a593Smuzhiyun 
ksm_madvise(struct vm_area_struct * vma,unsigned long start,unsigned long end,int advice,unsigned long * vm_flags)2438*4882a593Smuzhiyun int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
2439*4882a593Smuzhiyun 		unsigned long end, int advice, unsigned long *vm_flags)
2440*4882a593Smuzhiyun {
2441*4882a593Smuzhiyun 	struct mm_struct *mm = vma->vm_mm;
2442*4882a593Smuzhiyun 	int err;
2443*4882a593Smuzhiyun 
2444*4882a593Smuzhiyun 	switch (advice) {
2445*4882a593Smuzhiyun 	case MADV_MERGEABLE:
2446*4882a593Smuzhiyun 		/*
2447*4882a593Smuzhiyun 		 * Be somewhat over-protective for now!
2448*4882a593Smuzhiyun 		 */
2449*4882a593Smuzhiyun 		if (*vm_flags & (VM_MERGEABLE | VM_SHARED  | VM_MAYSHARE   |
2450*4882a593Smuzhiyun 				 VM_PFNMAP    | VM_IO      | VM_DONTEXPAND |
2451*4882a593Smuzhiyun 				 VM_HUGETLB | VM_MIXEDMAP))
2452*4882a593Smuzhiyun 			return 0;		/* just ignore the advice */
2453*4882a593Smuzhiyun 
2454*4882a593Smuzhiyun 		if (vma_is_dax(vma))
2455*4882a593Smuzhiyun 			return 0;
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun #ifdef VM_SAO
2458*4882a593Smuzhiyun 		if (*vm_flags & VM_SAO)
2459*4882a593Smuzhiyun 			return 0;
2460*4882a593Smuzhiyun #endif
2461*4882a593Smuzhiyun #ifdef VM_SPARC_ADI
2462*4882a593Smuzhiyun 		if (*vm_flags & VM_SPARC_ADI)
2463*4882a593Smuzhiyun 			return 0;
2464*4882a593Smuzhiyun #endif
2465*4882a593Smuzhiyun 
2466*4882a593Smuzhiyun 		if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
2467*4882a593Smuzhiyun 			err = __ksm_enter(mm);
2468*4882a593Smuzhiyun 			if (err)
2469*4882a593Smuzhiyun 				return err;
2470*4882a593Smuzhiyun 		}
2471*4882a593Smuzhiyun 
2472*4882a593Smuzhiyun 		*vm_flags |= VM_MERGEABLE;
2473*4882a593Smuzhiyun 		break;
2474*4882a593Smuzhiyun 
2475*4882a593Smuzhiyun 	case MADV_UNMERGEABLE:
2476*4882a593Smuzhiyun 		if (!(*vm_flags & VM_MERGEABLE))
2477*4882a593Smuzhiyun 			return 0;		/* just ignore the advice */
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun 		if (vma->anon_vma) {
2480*4882a593Smuzhiyun 			err = unmerge_ksm_pages(vma, start, end);
2481*4882a593Smuzhiyun 			if (err)
2482*4882a593Smuzhiyun 				return err;
2483*4882a593Smuzhiyun 		}
2484*4882a593Smuzhiyun 
2485*4882a593Smuzhiyun 		*vm_flags &= ~VM_MERGEABLE;
2486*4882a593Smuzhiyun 		break;
2487*4882a593Smuzhiyun 	}
2488*4882a593Smuzhiyun 
2489*4882a593Smuzhiyun 	return 0;
2490*4882a593Smuzhiyun }
2491*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ksm_madvise);
2492*4882a593Smuzhiyun 
__ksm_enter(struct mm_struct * mm)2493*4882a593Smuzhiyun int __ksm_enter(struct mm_struct *mm)
2494*4882a593Smuzhiyun {
2495*4882a593Smuzhiyun 	struct mm_slot *mm_slot;
2496*4882a593Smuzhiyun 	int needs_wakeup;
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 	mm_slot = alloc_mm_slot();
2499*4882a593Smuzhiyun 	if (!mm_slot)
2500*4882a593Smuzhiyun 		return -ENOMEM;
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 	/* Check ksm_run too?  Would need tighter locking */
2503*4882a593Smuzhiyun 	needs_wakeup = list_empty(&ksm_mm_head.mm_list);
2504*4882a593Smuzhiyun 
2505*4882a593Smuzhiyun 	spin_lock(&ksm_mmlist_lock);
2506*4882a593Smuzhiyun 	insert_to_mm_slots_hash(mm, mm_slot);
2507*4882a593Smuzhiyun 	/*
2508*4882a593Smuzhiyun 	 * When KSM_RUN_MERGE (or KSM_RUN_STOP),
2509*4882a593Smuzhiyun 	 * insert just behind the scanning cursor, to let the area settle
2510*4882a593Smuzhiyun 	 * down a little; when fork is followed by immediate exec, we don't
2511*4882a593Smuzhiyun 	 * want ksmd to waste time setting up and tearing down an rmap_list.
2512*4882a593Smuzhiyun 	 *
2513*4882a593Smuzhiyun 	 * But when KSM_RUN_UNMERGE, it's important to insert ahead of its
2514*4882a593Smuzhiyun 	 * scanning cursor, otherwise KSM pages in newly forked mms will be
2515*4882a593Smuzhiyun 	 * missed: then we might as well insert at the end of the list.
2516*4882a593Smuzhiyun 	 */
2517*4882a593Smuzhiyun 	if (ksm_run & KSM_RUN_UNMERGE)
2518*4882a593Smuzhiyun 		list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list);
2519*4882a593Smuzhiyun 	else
2520*4882a593Smuzhiyun 		list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
2521*4882a593Smuzhiyun 	spin_unlock(&ksm_mmlist_lock);
2522*4882a593Smuzhiyun 
2523*4882a593Smuzhiyun 	set_bit(MMF_VM_MERGEABLE, &mm->flags);
2524*4882a593Smuzhiyun 	mmgrab(mm);
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 	if (needs_wakeup)
2527*4882a593Smuzhiyun 		wake_up_interruptible(&ksm_thread_wait);
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	return 0;
2530*4882a593Smuzhiyun }
2531*4882a593Smuzhiyun 
__ksm_exit(struct mm_struct * mm)2532*4882a593Smuzhiyun void __ksm_exit(struct mm_struct *mm)
2533*4882a593Smuzhiyun {
2534*4882a593Smuzhiyun 	struct mm_slot *mm_slot;
2535*4882a593Smuzhiyun 	int easy_to_free = 0;
2536*4882a593Smuzhiyun 
2537*4882a593Smuzhiyun 	/*
2538*4882a593Smuzhiyun 	 * This process is exiting: if it's straightforward (as is the
2539*4882a593Smuzhiyun 	 * case when ksmd was never running), free mm_slot immediately.
2540*4882a593Smuzhiyun 	 * But if it's at the cursor or has rmap_items linked to it, use
2541*4882a593Smuzhiyun 	 * mmap_lock to synchronize with any break_cows before pagetables
2542*4882a593Smuzhiyun 	 * are freed, and leave the mm_slot on the list for ksmd to free.
2543*4882a593Smuzhiyun 	 * Beware: ksm may already have noticed it exiting and freed the slot.
2544*4882a593Smuzhiyun 	 */
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	spin_lock(&ksm_mmlist_lock);
2547*4882a593Smuzhiyun 	mm_slot = get_mm_slot(mm);
2548*4882a593Smuzhiyun 	if (mm_slot && ksm_scan.mm_slot != mm_slot) {
2549*4882a593Smuzhiyun 		if (!mm_slot->rmap_list) {
2550*4882a593Smuzhiyun 			hash_del(&mm_slot->link);
2551*4882a593Smuzhiyun 			list_del(&mm_slot->mm_list);
2552*4882a593Smuzhiyun 			easy_to_free = 1;
2553*4882a593Smuzhiyun 		} else {
2554*4882a593Smuzhiyun 			list_move(&mm_slot->mm_list,
2555*4882a593Smuzhiyun 				  &ksm_scan.mm_slot->mm_list);
2556*4882a593Smuzhiyun 		}
2557*4882a593Smuzhiyun 	}
2558*4882a593Smuzhiyun 	spin_unlock(&ksm_mmlist_lock);
2559*4882a593Smuzhiyun 
2560*4882a593Smuzhiyun 	if (easy_to_free) {
2561*4882a593Smuzhiyun 		free_mm_slot(mm_slot);
2562*4882a593Smuzhiyun 		clear_bit(MMF_VM_MERGEABLE, &mm->flags);
2563*4882a593Smuzhiyun 		mmdrop(mm);
2564*4882a593Smuzhiyun 	} else if (mm_slot) {
2565*4882a593Smuzhiyun 		mmap_write_lock(mm);
2566*4882a593Smuzhiyun 		mmap_write_unlock(mm);
2567*4882a593Smuzhiyun 	}
2568*4882a593Smuzhiyun }
2569*4882a593Smuzhiyun 
ksm_might_need_to_copy(struct page * page,struct vm_area_struct * vma,unsigned long address)2570*4882a593Smuzhiyun struct page *ksm_might_need_to_copy(struct page *page,
2571*4882a593Smuzhiyun 			struct vm_area_struct *vma, unsigned long address)
2572*4882a593Smuzhiyun {
2573*4882a593Smuzhiyun 	struct anon_vma *anon_vma = page_anon_vma(page);
2574*4882a593Smuzhiyun 	struct page *new_page;
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 	if (PageKsm(page)) {
2577*4882a593Smuzhiyun 		if (page_stable_node(page) &&
2578*4882a593Smuzhiyun 		    !(ksm_run & KSM_RUN_UNMERGE))
2579*4882a593Smuzhiyun 			return page;	/* no need to copy it */
2580*4882a593Smuzhiyun 	} else if (!anon_vma) {
2581*4882a593Smuzhiyun 		return page;		/* no need to copy it */
2582*4882a593Smuzhiyun 	} else if (anon_vma->root == vma->anon_vma->root &&
2583*4882a593Smuzhiyun 		 page->index == linear_page_index(vma, address)) {
2584*4882a593Smuzhiyun 		return page;		/* still no need to copy it */
2585*4882a593Smuzhiyun 	}
2586*4882a593Smuzhiyun 	if (!PageUptodate(page))
2587*4882a593Smuzhiyun 		return page;		/* let do_swap_page report the error */
2588*4882a593Smuzhiyun 
2589*4882a593Smuzhiyun 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2590*4882a593Smuzhiyun 	if (new_page && mem_cgroup_charge(new_page, vma->vm_mm, GFP_KERNEL)) {
2591*4882a593Smuzhiyun 		put_page(new_page);
2592*4882a593Smuzhiyun 		new_page = NULL;
2593*4882a593Smuzhiyun 	}
2594*4882a593Smuzhiyun 	if (new_page) {
2595*4882a593Smuzhiyun 		copy_user_highpage(new_page, page, address, vma);
2596*4882a593Smuzhiyun 
2597*4882a593Smuzhiyun 		SetPageDirty(new_page);
2598*4882a593Smuzhiyun 		__SetPageUptodate(new_page);
2599*4882a593Smuzhiyun 		__SetPageLocked(new_page);
2600*4882a593Smuzhiyun 	}
2601*4882a593Smuzhiyun 
2602*4882a593Smuzhiyun 	return new_page;
2603*4882a593Smuzhiyun }
2604*4882a593Smuzhiyun 
rmap_walk_ksm(struct page * page,struct rmap_walk_control * rwc)2605*4882a593Smuzhiyun void rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc)
2606*4882a593Smuzhiyun {
2607*4882a593Smuzhiyun 	struct stable_node *stable_node;
2608*4882a593Smuzhiyun 	struct rmap_item *rmap_item;
2609*4882a593Smuzhiyun 	int search_new_forks = 0;
2610*4882a593Smuzhiyun 
2611*4882a593Smuzhiyun 	VM_BUG_ON_PAGE(!PageKsm(page), page);
2612*4882a593Smuzhiyun 
2613*4882a593Smuzhiyun 	/*
2614*4882a593Smuzhiyun 	 * Rely on the page lock to protect against concurrent modifications
2615*4882a593Smuzhiyun 	 * to that page's node of the stable tree.
2616*4882a593Smuzhiyun 	 */
2617*4882a593Smuzhiyun 	VM_BUG_ON_PAGE(!PageLocked(page), page);
2618*4882a593Smuzhiyun 
2619*4882a593Smuzhiyun 	stable_node = page_stable_node(page);
2620*4882a593Smuzhiyun 	if (!stable_node)
2621*4882a593Smuzhiyun 		return;
2622*4882a593Smuzhiyun again:
2623*4882a593Smuzhiyun 	hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
2624*4882a593Smuzhiyun 		struct anon_vma *anon_vma = rmap_item->anon_vma;
2625*4882a593Smuzhiyun 		struct anon_vma_chain *vmac;
2626*4882a593Smuzhiyun 		struct vm_area_struct *vma;
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun 		cond_resched();
2629*4882a593Smuzhiyun 		if (!anon_vma_trylock_read(anon_vma)) {
2630*4882a593Smuzhiyun 			if (rwc->try_lock) {
2631*4882a593Smuzhiyun 				rwc->contended = true;
2632*4882a593Smuzhiyun 				return;
2633*4882a593Smuzhiyun 			}
2634*4882a593Smuzhiyun 			anon_vma_lock_read(anon_vma);
2635*4882a593Smuzhiyun 		}
2636*4882a593Smuzhiyun 		anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
2637*4882a593Smuzhiyun 					       0, ULONG_MAX) {
2638*4882a593Smuzhiyun 			unsigned long addr;
2639*4882a593Smuzhiyun 
2640*4882a593Smuzhiyun 			cond_resched();
2641*4882a593Smuzhiyun 			vma = vmac->vma;
2642*4882a593Smuzhiyun 
2643*4882a593Smuzhiyun 			/* Ignore the stable/unstable/sqnr flags */
2644*4882a593Smuzhiyun 			addr = rmap_item->address & ~KSM_FLAG_MASK;
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 			if (addr < vma->vm_start || addr >= vma->vm_end)
2647*4882a593Smuzhiyun 				continue;
2648*4882a593Smuzhiyun 			/*
2649*4882a593Smuzhiyun 			 * Initially we examine only the vma which covers this
2650*4882a593Smuzhiyun 			 * rmap_item; but later, if there is still work to do,
2651*4882a593Smuzhiyun 			 * we examine covering vmas in other mms: in case they
2652*4882a593Smuzhiyun 			 * were forked from the original since ksmd passed.
2653*4882a593Smuzhiyun 			 */
2654*4882a593Smuzhiyun 			if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
2655*4882a593Smuzhiyun 				continue;
2656*4882a593Smuzhiyun 
2657*4882a593Smuzhiyun 			if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
2658*4882a593Smuzhiyun 				continue;
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun 			if (!rwc->rmap_one(page, vma, addr, rwc->arg)) {
2661*4882a593Smuzhiyun 				anon_vma_unlock_read(anon_vma);
2662*4882a593Smuzhiyun 				return;
2663*4882a593Smuzhiyun 			}
2664*4882a593Smuzhiyun 			if (rwc->done && rwc->done(page)) {
2665*4882a593Smuzhiyun 				anon_vma_unlock_read(anon_vma);
2666*4882a593Smuzhiyun 				return;
2667*4882a593Smuzhiyun 			}
2668*4882a593Smuzhiyun 		}
2669*4882a593Smuzhiyun 		anon_vma_unlock_read(anon_vma);
2670*4882a593Smuzhiyun 	}
2671*4882a593Smuzhiyun 	if (!search_new_forks++)
2672*4882a593Smuzhiyun 		goto again;
2673*4882a593Smuzhiyun }
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun #ifdef CONFIG_MIGRATION
ksm_migrate_page(struct page * newpage,struct page * oldpage)2676*4882a593Smuzhiyun void ksm_migrate_page(struct page *newpage, struct page *oldpage)
2677*4882a593Smuzhiyun {
2678*4882a593Smuzhiyun 	struct stable_node *stable_node;
2679*4882a593Smuzhiyun 
2680*4882a593Smuzhiyun 	VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
2681*4882a593Smuzhiyun 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
2682*4882a593Smuzhiyun 	VM_BUG_ON_PAGE(newpage->mapping != oldpage->mapping, newpage);
2683*4882a593Smuzhiyun 
2684*4882a593Smuzhiyun 	stable_node = page_stable_node(newpage);
2685*4882a593Smuzhiyun 	if (stable_node) {
2686*4882a593Smuzhiyun 		VM_BUG_ON_PAGE(stable_node->kpfn != page_to_pfn(oldpage), oldpage);
2687*4882a593Smuzhiyun 		stable_node->kpfn = page_to_pfn(newpage);
2688*4882a593Smuzhiyun 		/*
2689*4882a593Smuzhiyun 		 * newpage->mapping was set in advance; now we need smp_wmb()
2690*4882a593Smuzhiyun 		 * to make sure that the new stable_node->kpfn is visible
2691*4882a593Smuzhiyun 		 * to get_ksm_page() before it can see that oldpage->mapping
2692*4882a593Smuzhiyun 		 * has gone stale (or that PageSwapCache has been cleared).
2693*4882a593Smuzhiyun 		 */
2694*4882a593Smuzhiyun 		smp_wmb();
2695*4882a593Smuzhiyun 		set_page_stable_node(oldpage, NULL);
2696*4882a593Smuzhiyun 	}
2697*4882a593Smuzhiyun }
2698*4882a593Smuzhiyun #endif /* CONFIG_MIGRATION */
2699*4882a593Smuzhiyun 
2700*4882a593Smuzhiyun #ifdef CONFIG_MEMORY_HOTREMOVE
wait_while_offlining(void)2701*4882a593Smuzhiyun static void wait_while_offlining(void)
2702*4882a593Smuzhiyun {
2703*4882a593Smuzhiyun 	while (ksm_run & KSM_RUN_OFFLINE) {
2704*4882a593Smuzhiyun 		mutex_unlock(&ksm_thread_mutex);
2705*4882a593Smuzhiyun 		wait_on_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE),
2706*4882a593Smuzhiyun 			    TASK_UNINTERRUPTIBLE);
2707*4882a593Smuzhiyun 		mutex_lock(&ksm_thread_mutex);
2708*4882a593Smuzhiyun 	}
2709*4882a593Smuzhiyun }
2710*4882a593Smuzhiyun 
stable_node_dup_remove_range(struct stable_node * stable_node,unsigned long start_pfn,unsigned long end_pfn)2711*4882a593Smuzhiyun static bool stable_node_dup_remove_range(struct stable_node *stable_node,
2712*4882a593Smuzhiyun 					 unsigned long start_pfn,
2713*4882a593Smuzhiyun 					 unsigned long end_pfn)
2714*4882a593Smuzhiyun {
2715*4882a593Smuzhiyun 	if (stable_node->kpfn >= start_pfn &&
2716*4882a593Smuzhiyun 	    stable_node->kpfn < end_pfn) {
2717*4882a593Smuzhiyun 		/*
2718*4882a593Smuzhiyun 		 * Don't get_ksm_page, page has already gone:
2719*4882a593Smuzhiyun 		 * which is why we keep kpfn instead of page*
2720*4882a593Smuzhiyun 		 */
2721*4882a593Smuzhiyun 		remove_node_from_stable_tree(stable_node);
2722*4882a593Smuzhiyun 		return true;
2723*4882a593Smuzhiyun 	}
2724*4882a593Smuzhiyun 	return false;
2725*4882a593Smuzhiyun }
2726*4882a593Smuzhiyun 
stable_node_chain_remove_range(struct stable_node * stable_node,unsigned long start_pfn,unsigned long end_pfn,struct rb_root * root)2727*4882a593Smuzhiyun static bool stable_node_chain_remove_range(struct stable_node *stable_node,
2728*4882a593Smuzhiyun 					   unsigned long start_pfn,
2729*4882a593Smuzhiyun 					   unsigned long end_pfn,
2730*4882a593Smuzhiyun 					   struct rb_root *root)
2731*4882a593Smuzhiyun {
2732*4882a593Smuzhiyun 	struct stable_node *dup;
2733*4882a593Smuzhiyun 	struct hlist_node *hlist_safe;
2734*4882a593Smuzhiyun 
2735*4882a593Smuzhiyun 	if (!is_stable_node_chain(stable_node)) {
2736*4882a593Smuzhiyun 		VM_BUG_ON(is_stable_node_dup(stable_node));
2737*4882a593Smuzhiyun 		return stable_node_dup_remove_range(stable_node, start_pfn,
2738*4882a593Smuzhiyun 						    end_pfn);
2739*4882a593Smuzhiyun 	}
2740*4882a593Smuzhiyun 
2741*4882a593Smuzhiyun 	hlist_for_each_entry_safe(dup, hlist_safe,
2742*4882a593Smuzhiyun 				  &stable_node->hlist, hlist_dup) {
2743*4882a593Smuzhiyun 		VM_BUG_ON(!is_stable_node_dup(dup));
2744*4882a593Smuzhiyun 		stable_node_dup_remove_range(dup, start_pfn, end_pfn);
2745*4882a593Smuzhiyun 	}
2746*4882a593Smuzhiyun 	if (hlist_empty(&stable_node->hlist)) {
2747*4882a593Smuzhiyun 		free_stable_node_chain(stable_node, root);
2748*4882a593Smuzhiyun 		return true; /* notify caller that tree was rebalanced */
2749*4882a593Smuzhiyun 	} else
2750*4882a593Smuzhiyun 		return false;
2751*4882a593Smuzhiyun }
2752*4882a593Smuzhiyun 
ksm_check_stable_tree(unsigned long start_pfn,unsigned long end_pfn)2753*4882a593Smuzhiyun static void ksm_check_stable_tree(unsigned long start_pfn,
2754*4882a593Smuzhiyun 				  unsigned long end_pfn)
2755*4882a593Smuzhiyun {
2756*4882a593Smuzhiyun 	struct stable_node *stable_node, *next;
2757*4882a593Smuzhiyun 	struct rb_node *node;
2758*4882a593Smuzhiyun 	int nid;
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun 	for (nid = 0; nid < ksm_nr_node_ids; nid++) {
2761*4882a593Smuzhiyun 		node = rb_first(root_stable_tree + nid);
2762*4882a593Smuzhiyun 		while (node) {
2763*4882a593Smuzhiyun 			stable_node = rb_entry(node, struct stable_node, node);
2764*4882a593Smuzhiyun 			if (stable_node_chain_remove_range(stable_node,
2765*4882a593Smuzhiyun 							   start_pfn, end_pfn,
2766*4882a593Smuzhiyun 							   root_stable_tree +
2767*4882a593Smuzhiyun 							   nid))
2768*4882a593Smuzhiyun 				node = rb_first(root_stable_tree + nid);
2769*4882a593Smuzhiyun 			else
2770*4882a593Smuzhiyun 				node = rb_next(node);
2771*4882a593Smuzhiyun 			cond_resched();
2772*4882a593Smuzhiyun 		}
2773*4882a593Smuzhiyun 	}
2774*4882a593Smuzhiyun 	list_for_each_entry_safe(stable_node, next, &migrate_nodes, list) {
2775*4882a593Smuzhiyun 		if (stable_node->kpfn >= start_pfn &&
2776*4882a593Smuzhiyun 		    stable_node->kpfn < end_pfn)
2777*4882a593Smuzhiyun 			remove_node_from_stable_tree(stable_node);
2778*4882a593Smuzhiyun 		cond_resched();
2779*4882a593Smuzhiyun 	}
2780*4882a593Smuzhiyun }
2781*4882a593Smuzhiyun 
ksm_memory_callback(struct notifier_block * self,unsigned long action,void * arg)2782*4882a593Smuzhiyun static int ksm_memory_callback(struct notifier_block *self,
2783*4882a593Smuzhiyun 			       unsigned long action, void *arg)
2784*4882a593Smuzhiyun {
2785*4882a593Smuzhiyun 	struct memory_notify *mn = arg;
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun 	switch (action) {
2788*4882a593Smuzhiyun 	case MEM_GOING_OFFLINE:
2789*4882a593Smuzhiyun 		/*
2790*4882a593Smuzhiyun 		 * Prevent ksm_do_scan(), unmerge_and_remove_all_rmap_items()
2791*4882a593Smuzhiyun 		 * and remove_all_stable_nodes() while memory is going offline:
2792*4882a593Smuzhiyun 		 * it is unsafe for them to touch the stable tree at this time.
2793*4882a593Smuzhiyun 		 * But unmerge_ksm_pages(), rmap lookups and other entry points
2794*4882a593Smuzhiyun 		 * which do not need the ksm_thread_mutex are all safe.
2795*4882a593Smuzhiyun 		 */
2796*4882a593Smuzhiyun 		mutex_lock(&ksm_thread_mutex);
2797*4882a593Smuzhiyun 		ksm_run |= KSM_RUN_OFFLINE;
2798*4882a593Smuzhiyun 		mutex_unlock(&ksm_thread_mutex);
2799*4882a593Smuzhiyun 		break;
2800*4882a593Smuzhiyun 
2801*4882a593Smuzhiyun 	case MEM_OFFLINE:
2802*4882a593Smuzhiyun 		/*
2803*4882a593Smuzhiyun 		 * Most of the work is done by page migration; but there might
2804*4882a593Smuzhiyun 		 * be a few stable_nodes left over, still pointing to struct
2805*4882a593Smuzhiyun 		 * pages which have been offlined: prune those from the tree,
2806*4882a593Smuzhiyun 		 * otherwise get_ksm_page() might later try to access a
2807*4882a593Smuzhiyun 		 * non-existent struct page.
2808*4882a593Smuzhiyun 		 */
2809*4882a593Smuzhiyun 		ksm_check_stable_tree(mn->start_pfn,
2810*4882a593Smuzhiyun 				      mn->start_pfn + mn->nr_pages);
2811*4882a593Smuzhiyun 		fallthrough;
2812*4882a593Smuzhiyun 	case MEM_CANCEL_OFFLINE:
2813*4882a593Smuzhiyun 		mutex_lock(&ksm_thread_mutex);
2814*4882a593Smuzhiyun 		ksm_run &= ~KSM_RUN_OFFLINE;
2815*4882a593Smuzhiyun 		mutex_unlock(&ksm_thread_mutex);
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun 		smp_mb();	/* wake_up_bit advises this */
2818*4882a593Smuzhiyun 		wake_up_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE));
2819*4882a593Smuzhiyun 		break;
2820*4882a593Smuzhiyun 	}
2821*4882a593Smuzhiyun 	return NOTIFY_OK;
2822*4882a593Smuzhiyun }
2823*4882a593Smuzhiyun #else
wait_while_offlining(void)2824*4882a593Smuzhiyun static void wait_while_offlining(void)
2825*4882a593Smuzhiyun {
2826*4882a593Smuzhiyun }
2827*4882a593Smuzhiyun #endif /* CONFIG_MEMORY_HOTREMOVE */
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun #ifdef CONFIG_SYSFS
2830*4882a593Smuzhiyun /*
2831*4882a593Smuzhiyun  * This all compiles without CONFIG_SYSFS, but is a waste of space.
2832*4882a593Smuzhiyun  */
2833*4882a593Smuzhiyun 
2834*4882a593Smuzhiyun #define KSM_ATTR_RO(_name) \
2835*4882a593Smuzhiyun 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
2836*4882a593Smuzhiyun #define KSM_ATTR(_name) \
2837*4882a593Smuzhiyun 	static struct kobj_attribute _name##_attr = \
2838*4882a593Smuzhiyun 		__ATTR(_name, 0644, _name##_show, _name##_store)
2839*4882a593Smuzhiyun 
sleep_millisecs_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2840*4882a593Smuzhiyun static ssize_t sleep_millisecs_show(struct kobject *kobj,
2841*4882a593Smuzhiyun 				    struct kobj_attribute *attr, char *buf)
2842*4882a593Smuzhiyun {
2843*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
2844*4882a593Smuzhiyun }
2845*4882a593Smuzhiyun 
sleep_millisecs_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)2846*4882a593Smuzhiyun static ssize_t sleep_millisecs_store(struct kobject *kobj,
2847*4882a593Smuzhiyun 				     struct kobj_attribute *attr,
2848*4882a593Smuzhiyun 				     const char *buf, size_t count)
2849*4882a593Smuzhiyun {
2850*4882a593Smuzhiyun 	unsigned long msecs;
2851*4882a593Smuzhiyun 	int err;
2852*4882a593Smuzhiyun 
2853*4882a593Smuzhiyun 	err = kstrtoul(buf, 10, &msecs);
2854*4882a593Smuzhiyun 	if (err || msecs > UINT_MAX)
2855*4882a593Smuzhiyun 		return -EINVAL;
2856*4882a593Smuzhiyun 
2857*4882a593Smuzhiyun 	ksm_thread_sleep_millisecs = msecs;
2858*4882a593Smuzhiyun 	wake_up_interruptible(&ksm_iter_wait);
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	return count;
2861*4882a593Smuzhiyun }
2862*4882a593Smuzhiyun KSM_ATTR(sleep_millisecs);
2863*4882a593Smuzhiyun 
pages_to_scan_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2864*4882a593Smuzhiyun static ssize_t pages_to_scan_show(struct kobject *kobj,
2865*4882a593Smuzhiyun 				  struct kobj_attribute *attr, char *buf)
2866*4882a593Smuzhiyun {
2867*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
2868*4882a593Smuzhiyun }
2869*4882a593Smuzhiyun 
pages_to_scan_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)2870*4882a593Smuzhiyun static ssize_t pages_to_scan_store(struct kobject *kobj,
2871*4882a593Smuzhiyun 				   struct kobj_attribute *attr,
2872*4882a593Smuzhiyun 				   const char *buf, size_t count)
2873*4882a593Smuzhiyun {
2874*4882a593Smuzhiyun 	int err;
2875*4882a593Smuzhiyun 	unsigned long nr_pages;
2876*4882a593Smuzhiyun 
2877*4882a593Smuzhiyun 	err = kstrtoul(buf, 10, &nr_pages);
2878*4882a593Smuzhiyun 	if (err || nr_pages > UINT_MAX)
2879*4882a593Smuzhiyun 		return -EINVAL;
2880*4882a593Smuzhiyun 
2881*4882a593Smuzhiyun 	ksm_thread_pages_to_scan = nr_pages;
2882*4882a593Smuzhiyun 
2883*4882a593Smuzhiyun 	return count;
2884*4882a593Smuzhiyun }
2885*4882a593Smuzhiyun KSM_ATTR(pages_to_scan);
2886*4882a593Smuzhiyun 
run_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2887*4882a593Smuzhiyun static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
2888*4882a593Smuzhiyun 			char *buf)
2889*4882a593Smuzhiyun {
2890*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_run);
2891*4882a593Smuzhiyun }
2892*4882a593Smuzhiyun 
run_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)2893*4882a593Smuzhiyun static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
2894*4882a593Smuzhiyun 			 const char *buf, size_t count)
2895*4882a593Smuzhiyun {
2896*4882a593Smuzhiyun 	int err;
2897*4882a593Smuzhiyun 	unsigned long flags;
2898*4882a593Smuzhiyun 
2899*4882a593Smuzhiyun 	err = kstrtoul(buf, 10, &flags);
2900*4882a593Smuzhiyun 	if (err || flags > UINT_MAX)
2901*4882a593Smuzhiyun 		return -EINVAL;
2902*4882a593Smuzhiyun 	if (flags > KSM_RUN_UNMERGE)
2903*4882a593Smuzhiyun 		return -EINVAL;
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun 	/*
2906*4882a593Smuzhiyun 	 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
2907*4882a593Smuzhiyun 	 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
2908*4882a593Smuzhiyun 	 * breaking COW to free the pages_shared (but leaves mm_slots
2909*4882a593Smuzhiyun 	 * on the list for when ksmd may be set running again).
2910*4882a593Smuzhiyun 	 */
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	mutex_lock(&ksm_thread_mutex);
2913*4882a593Smuzhiyun 	wait_while_offlining();
2914*4882a593Smuzhiyun 	if (ksm_run != flags) {
2915*4882a593Smuzhiyun 		ksm_run = flags;
2916*4882a593Smuzhiyun 		if (flags & KSM_RUN_UNMERGE) {
2917*4882a593Smuzhiyun 			set_current_oom_origin();
2918*4882a593Smuzhiyun 			err = unmerge_and_remove_all_rmap_items();
2919*4882a593Smuzhiyun 			clear_current_oom_origin();
2920*4882a593Smuzhiyun 			if (err) {
2921*4882a593Smuzhiyun 				ksm_run = KSM_RUN_STOP;
2922*4882a593Smuzhiyun 				count = err;
2923*4882a593Smuzhiyun 			}
2924*4882a593Smuzhiyun 		}
2925*4882a593Smuzhiyun 	}
2926*4882a593Smuzhiyun 	mutex_unlock(&ksm_thread_mutex);
2927*4882a593Smuzhiyun 
2928*4882a593Smuzhiyun 	if (flags & KSM_RUN_MERGE)
2929*4882a593Smuzhiyun 		wake_up_interruptible(&ksm_thread_wait);
2930*4882a593Smuzhiyun 
2931*4882a593Smuzhiyun 	return count;
2932*4882a593Smuzhiyun }
2933*4882a593Smuzhiyun KSM_ATTR(run);
2934*4882a593Smuzhiyun 
2935*4882a593Smuzhiyun #ifdef CONFIG_NUMA
merge_across_nodes_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2936*4882a593Smuzhiyun static ssize_t merge_across_nodes_show(struct kobject *kobj,
2937*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
2938*4882a593Smuzhiyun {
2939*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", ksm_merge_across_nodes);
2940*4882a593Smuzhiyun }
2941*4882a593Smuzhiyun 
merge_across_nodes_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)2942*4882a593Smuzhiyun static ssize_t merge_across_nodes_store(struct kobject *kobj,
2943*4882a593Smuzhiyun 				   struct kobj_attribute *attr,
2944*4882a593Smuzhiyun 				   const char *buf, size_t count)
2945*4882a593Smuzhiyun {
2946*4882a593Smuzhiyun 	int err;
2947*4882a593Smuzhiyun 	unsigned long knob;
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun 	err = kstrtoul(buf, 10, &knob);
2950*4882a593Smuzhiyun 	if (err)
2951*4882a593Smuzhiyun 		return err;
2952*4882a593Smuzhiyun 	if (knob > 1)
2953*4882a593Smuzhiyun 		return -EINVAL;
2954*4882a593Smuzhiyun 
2955*4882a593Smuzhiyun 	mutex_lock(&ksm_thread_mutex);
2956*4882a593Smuzhiyun 	wait_while_offlining();
2957*4882a593Smuzhiyun 	if (ksm_merge_across_nodes != knob) {
2958*4882a593Smuzhiyun 		if (ksm_pages_shared || remove_all_stable_nodes())
2959*4882a593Smuzhiyun 			err = -EBUSY;
2960*4882a593Smuzhiyun 		else if (root_stable_tree == one_stable_tree) {
2961*4882a593Smuzhiyun 			struct rb_root *buf;
2962*4882a593Smuzhiyun 			/*
2963*4882a593Smuzhiyun 			 * This is the first time that we switch away from the
2964*4882a593Smuzhiyun 			 * default of merging across nodes: must now allocate
2965*4882a593Smuzhiyun 			 * a buffer to hold as many roots as may be needed.
2966*4882a593Smuzhiyun 			 * Allocate stable and unstable together:
2967*4882a593Smuzhiyun 			 * MAXSMP NODES_SHIFT 10 will use 16kB.
2968*4882a593Smuzhiyun 			 */
2969*4882a593Smuzhiyun 			buf = kcalloc(nr_node_ids + nr_node_ids, sizeof(*buf),
2970*4882a593Smuzhiyun 				      GFP_KERNEL);
2971*4882a593Smuzhiyun 			/* Let us assume that RB_ROOT is NULL is zero */
2972*4882a593Smuzhiyun 			if (!buf)
2973*4882a593Smuzhiyun 				err = -ENOMEM;
2974*4882a593Smuzhiyun 			else {
2975*4882a593Smuzhiyun 				root_stable_tree = buf;
2976*4882a593Smuzhiyun 				root_unstable_tree = buf + nr_node_ids;
2977*4882a593Smuzhiyun 				/* Stable tree is empty but not the unstable */
2978*4882a593Smuzhiyun 				root_unstable_tree[0] = one_unstable_tree[0];
2979*4882a593Smuzhiyun 			}
2980*4882a593Smuzhiyun 		}
2981*4882a593Smuzhiyun 		if (!err) {
2982*4882a593Smuzhiyun 			ksm_merge_across_nodes = knob;
2983*4882a593Smuzhiyun 			ksm_nr_node_ids = knob ? 1 : nr_node_ids;
2984*4882a593Smuzhiyun 		}
2985*4882a593Smuzhiyun 	}
2986*4882a593Smuzhiyun 	mutex_unlock(&ksm_thread_mutex);
2987*4882a593Smuzhiyun 
2988*4882a593Smuzhiyun 	return err ? err : count;
2989*4882a593Smuzhiyun }
2990*4882a593Smuzhiyun KSM_ATTR(merge_across_nodes);
2991*4882a593Smuzhiyun #endif
2992*4882a593Smuzhiyun 
use_zero_pages_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2993*4882a593Smuzhiyun static ssize_t use_zero_pages_show(struct kobject *kobj,
2994*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
2995*4882a593Smuzhiyun {
2996*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", ksm_use_zero_pages);
2997*4882a593Smuzhiyun }
use_zero_pages_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)2998*4882a593Smuzhiyun static ssize_t use_zero_pages_store(struct kobject *kobj,
2999*4882a593Smuzhiyun 				   struct kobj_attribute *attr,
3000*4882a593Smuzhiyun 				   const char *buf, size_t count)
3001*4882a593Smuzhiyun {
3002*4882a593Smuzhiyun 	int err;
3003*4882a593Smuzhiyun 	bool value;
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun 	err = kstrtobool(buf, &value);
3006*4882a593Smuzhiyun 	if (err)
3007*4882a593Smuzhiyun 		return -EINVAL;
3008*4882a593Smuzhiyun 
3009*4882a593Smuzhiyun 	ksm_use_zero_pages = value;
3010*4882a593Smuzhiyun 
3011*4882a593Smuzhiyun 	return count;
3012*4882a593Smuzhiyun }
3013*4882a593Smuzhiyun KSM_ATTR(use_zero_pages);
3014*4882a593Smuzhiyun 
max_page_sharing_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3015*4882a593Smuzhiyun static ssize_t max_page_sharing_show(struct kobject *kobj,
3016*4882a593Smuzhiyun 				     struct kobj_attribute *attr, char *buf)
3017*4882a593Smuzhiyun {
3018*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", ksm_max_page_sharing);
3019*4882a593Smuzhiyun }
3020*4882a593Smuzhiyun 
max_page_sharing_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)3021*4882a593Smuzhiyun static ssize_t max_page_sharing_store(struct kobject *kobj,
3022*4882a593Smuzhiyun 				      struct kobj_attribute *attr,
3023*4882a593Smuzhiyun 				      const char *buf, size_t count)
3024*4882a593Smuzhiyun {
3025*4882a593Smuzhiyun 	int err;
3026*4882a593Smuzhiyun 	int knob;
3027*4882a593Smuzhiyun 
3028*4882a593Smuzhiyun 	err = kstrtoint(buf, 10, &knob);
3029*4882a593Smuzhiyun 	if (err)
3030*4882a593Smuzhiyun 		return err;
3031*4882a593Smuzhiyun 	/*
3032*4882a593Smuzhiyun 	 * When a KSM page is created it is shared by 2 mappings. This
3033*4882a593Smuzhiyun 	 * being a signed comparison, it implicitly verifies it's not
3034*4882a593Smuzhiyun 	 * negative.
3035*4882a593Smuzhiyun 	 */
3036*4882a593Smuzhiyun 	if (knob < 2)
3037*4882a593Smuzhiyun 		return -EINVAL;
3038*4882a593Smuzhiyun 
3039*4882a593Smuzhiyun 	if (READ_ONCE(ksm_max_page_sharing) == knob)
3040*4882a593Smuzhiyun 		return count;
3041*4882a593Smuzhiyun 
3042*4882a593Smuzhiyun 	mutex_lock(&ksm_thread_mutex);
3043*4882a593Smuzhiyun 	wait_while_offlining();
3044*4882a593Smuzhiyun 	if (ksm_max_page_sharing != knob) {
3045*4882a593Smuzhiyun 		if (ksm_pages_shared || remove_all_stable_nodes())
3046*4882a593Smuzhiyun 			err = -EBUSY;
3047*4882a593Smuzhiyun 		else
3048*4882a593Smuzhiyun 			ksm_max_page_sharing = knob;
3049*4882a593Smuzhiyun 	}
3050*4882a593Smuzhiyun 	mutex_unlock(&ksm_thread_mutex);
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun 	return err ? err : count;
3053*4882a593Smuzhiyun }
3054*4882a593Smuzhiyun KSM_ATTR(max_page_sharing);
3055*4882a593Smuzhiyun 
pages_shared_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3056*4882a593Smuzhiyun static ssize_t pages_shared_show(struct kobject *kobj,
3057*4882a593Smuzhiyun 				 struct kobj_attribute *attr, char *buf)
3058*4882a593Smuzhiyun {
3059*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_pages_shared);
3060*4882a593Smuzhiyun }
3061*4882a593Smuzhiyun KSM_ATTR_RO(pages_shared);
3062*4882a593Smuzhiyun 
pages_sharing_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3063*4882a593Smuzhiyun static ssize_t pages_sharing_show(struct kobject *kobj,
3064*4882a593Smuzhiyun 				  struct kobj_attribute *attr, char *buf)
3065*4882a593Smuzhiyun {
3066*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_pages_sharing);
3067*4882a593Smuzhiyun }
3068*4882a593Smuzhiyun KSM_ATTR_RO(pages_sharing);
3069*4882a593Smuzhiyun 
pages_unshared_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3070*4882a593Smuzhiyun static ssize_t pages_unshared_show(struct kobject *kobj,
3071*4882a593Smuzhiyun 				   struct kobj_attribute *attr, char *buf)
3072*4882a593Smuzhiyun {
3073*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_pages_unshared);
3074*4882a593Smuzhiyun }
3075*4882a593Smuzhiyun KSM_ATTR_RO(pages_unshared);
3076*4882a593Smuzhiyun 
pages_volatile_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3077*4882a593Smuzhiyun static ssize_t pages_volatile_show(struct kobject *kobj,
3078*4882a593Smuzhiyun 				   struct kobj_attribute *attr, char *buf)
3079*4882a593Smuzhiyun {
3080*4882a593Smuzhiyun 	long ksm_pages_volatile;
3081*4882a593Smuzhiyun 
3082*4882a593Smuzhiyun 	ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
3083*4882a593Smuzhiyun 				- ksm_pages_sharing - ksm_pages_unshared;
3084*4882a593Smuzhiyun 	/*
3085*4882a593Smuzhiyun 	 * It was not worth any locking to calculate that statistic,
3086*4882a593Smuzhiyun 	 * but it might therefore sometimes be negative: conceal that.
3087*4882a593Smuzhiyun 	 */
3088*4882a593Smuzhiyun 	if (ksm_pages_volatile < 0)
3089*4882a593Smuzhiyun 		ksm_pages_volatile = 0;
3090*4882a593Smuzhiyun 	return sprintf(buf, "%ld\n", ksm_pages_volatile);
3091*4882a593Smuzhiyun }
3092*4882a593Smuzhiyun KSM_ATTR_RO(pages_volatile);
3093*4882a593Smuzhiyun 
stable_node_dups_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3094*4882a593Smuzhiyun static ssize_t stable_node_dups_show(struct kobject *kobj,
3095*4882a593Smuzhiyun 				     struct kobj_attribute *attr, char *buf)
3096*4882a593Smuzhiyun {
3097*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_stable_node_dups);
3098*4882a593Smuzhiyun }
3099*4882a593Smuzhiyun KSM_ATTR_RO(stable_node_dups);
3100*4882a593Smuzhiyun 
stable_node_chains_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3101*4882a593Smuzhiyun static ssize_t stable_node_chains_show(struct kobject *kobj,
3102*4882a593Smuzhiyun 				       struct kobj_attribute *attr, char *buf)
3103*4882a593Smuzhiyun {
3104*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_stable_node_chains);
3105*4882a593Smuzhiyun }
3106*4882a593Smuzhiyun KSM_ATTR_RO(stable_node_chains);
3107*4882a593Smuzhiyun 
3108*4882a593Smuzhiyun static ssize_t
stable_node_chains_prune_millisecs_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3109*4882a593Smuzhiyun stable_node_chains_prune_millisecs_show(struct kobject *kobj,
3110*4882a593Smuzhiyun 					struct kobj_attribute *attr,
3111*4882a593Smuzhiyun 					char *buf)
3112*4882a593Smuzhiyun {
3113*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", ksm_stable_node_chains_prune_millisecs);
3114*4882a593Smuzhiyun }
3115*4882a593Smuzhiyun 
3116*4882a593Smuzhiyun static ssize_t
stable_node_chains_prune_millisecs_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)3117*4882a593Smuzhiyun stable_node_chains_prune_millisecs_store(struct kobject *kobj,
3118*4882a593Smuzhiyun 					 struct kobj_attribute *attr,
3119*4882a593Smuzhiyun 					 const char *buf, size_t count)
3120*4882a593Smuzhiyun {
3121*4882a593Smuzhiyun 	unsigned long msecs;
3122*4882a593Smuzhiyun 	int err;
3123*4882a593Smuzhiyun 
3124*4882a593Smuzhiyun 	err = kstrtoul(buf, 10, &msecs);
3125*4882a593Smuzhiyun 	if (err || msecs > UINT_MAX)
3126*4882a593Smuzhiyun 		return -EINVAL;
3127*4882a593Smuzhiyun 
3128*4882a593Smuzhiyun 	ksm_stable_node_chains_prune_millisecs = msecs;
3129*4882a593Smuzhiyun 
3130*4882a593Smuzhiyun 	return count;
3131*4882a593Smuzhiyun }
3132*4882a593Smuzhiyun KSM_ATTR(stable_node_chains_prune_millisecs);
3133*4882a593Smuzhiyun 
full_scans_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)3134*4882a593Smuzhiyun static ssize_t full_scans_show(struct kobject *kobj,
3135*4882a593Smuzhiyun 			       struct kobj_attribute *attr, char *buf)
3136*4882a593Smuzhiyun {
3137*4882a593Smuzhiyun 	return sprintf(buf, "%lu\n", ksm_scan.seqnr);
3138*4882a593Smuzhiyun }
3139*4882a593Smuzhiyun KSM_ATTR_RO(full_scans);
3140*4882a593Smuzhiyun 
3141*4882a593Smuzhiyun static struct attribute *ksm_attrs[] = {
3142*4882a593Smuzhiyun 	&sleep_millisecs_attr.attr,
3143*4882a593Smuzhiyun 	&pages_to_scan_attr.attr,
3144*4882a593Smuzhiyun 	&run_attr.attr,
3145*4882a593Smuzhiyun 	&pages_shared_attr.attr,
3146*4882a593Smuzhiyun 	&pages_sharing_attr.attr,
3147*4882a593Smuzhiyun 	&pages_unshared_attr.attr,
3148*4882a593Smuzhiyun 	&pages_volatile_attr.attr,
3149*4882a593Smuzhiyun 	&full_scans_attr.attr,
3150*4882a593Smuzhiyun #ifdef CONFIG_NUMA
3151*4882a593Smuzhiyun 	&merge_across_nodes_attr.attr,
3152*4882a593Smuzhiyun #endif
3153*4882a593Smuzhiyun 	&max_page_sharing_attr.attr,
3154*4882a593Smuzhiyun 	&stable_node_chains_attr.attr,
3155*4882a593Smuzhiyun 	&stable_node_dups_attr.attr,
3156*4882a593Smuzhiyun 	&stable_node_chains_prune_millisecs_attr.attr,
3157*4882a593Smuzhiyun 	&use_zero_pages_attr.attr,
3158*4882a593Smuzhiyun 	NULL,
3159*4882a593Smuzhiyun };
3160*4882a593Smuzhiyun 
3161*4882a593Smuzhiyun static const struct attribute_group ksm_attr_group = {
3162*4882a593Smuzhiyun 	.attrs = ksm_attrs,
3163*4882a593Smuzhiyun 	.name = "ksm",
3164*4882a593Smuzhiyun };
3165*4882a593Smuzhiyun #endif /* CONFIG_SYSFS */
3166*4882a593Smuzhiyun 
ksm_init(void)3167*4882a593Smuzhiyun static int __init ksm_init(void)
3168*4882a593Smuzhiyun {
3169*4882a593Smuzhiyun 	struct task_struct *ksm_thread;
3170*4882a593Smuzhiyun 	int err;
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun 	/* The correct value depends on page size and endianness */
3173*4882a593Smuzhiyun 	zero_checksum = calc_checksum(ZERO_PAGE(0));
3174*4882a593Smuzhiyun 	/* Default to false for backwards compatibility */
3175*4882a593Smuzhiyun 	ksm_use_zero_pages = false;
3176*4882a593Smuzhiyun 
3177*4882a593Smuzhiyun 	err = ksm_slab_init();
3178*4882a593Smuzhiyun 	if (err)
3179*4882a593Smuzhiyun 		goto out;
3180*4882a593Smuzhiyun 
3181*4882a593Smuzhiyun 	ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
3182*4882a593Smuzhiyun 	if (IS_ERR(ksm_thread)) {
3183*4882a593Smuzhiyun 		pr_err("ksm: creating kthread failed\n");
3184*4882a593Smuzhiyun 		err = PTR_ERR(ksm_thread);
3185*4882a593Smuzhiyun 		goto out_free;
3186*4882a593Smuzhiyun 	}
3187*4882a593Smuzhiyun 
3188*4882a593Smuzhiyun #ifdef CONFIG_SYSFS
3189*4882a593Smuzhiyun 	err = sysfs_create_group(mm_kobj, &ksm_attr_group);
3190*4882a593Smuzhiyun 	if (err) {
3191*4882a593Smuzhiyun 		pr_err("ksm: register sysfs failed\n");
3192*4882a593Smuzhiyun 		kthread_stop(ksm_thread);
3193*4882a593Smuzhiyun 		goto out_free;
3194*4882a593Smuzhiyun 	}
3195*4882a593Smuzhiyun #else
3196*4882a593Smuzhiyun 	ksm_run = KSM_RUN_MERGE;	/* no way for user to start it */
3197*4882a593Smuzhiyun 
3198*4882a593Smuzhiyun #endif /* CONFIG_SYSFS */
3199*4882a593Smuzhiyun 
3200*4882a593Smuzhiyun #ifdef CONFIG_MEMORY_HOTREMOVE
3201*4882a593Smuzhiyun 	/* There is no significance to this priority 100 */
3202*4882a593Smuzhiyun 	hotplug_memory_notifier(ksm_memory_callback, 100);
3203*4882a593Smuzhiyun #endif
3204*4882a593Smuzhiyun 	return 0;
3205*4882a593Smuzhiyun 
3206*4882a593Smuzhiyun out_free:
3207*4882a593Smuzhiyun 	ksm_slab_free();
3208*4882a593Smuzhiyun out:
3209*4882a593Smuzhiyun 	return err;
3210*4882a593Smuzhiyun }
3211*4882a593Smuzhiyun subsys_initcall(ksm_init);
3212