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