Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1 | /* |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2 | * Memory merging support. |
| 3 | * |
| 4 | * This code enables dynamic sharing of identical pages found in different |
| 5 | * memory areas, even if they are not shared by fork() |
| 6 | * |
Izik Eidus | 36b2528 | 2009-09-21 17:02:06 -0700 | [diff] [blame] | 7 | * Copyright (C) 2008-2009 Red Hat, Inc. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 8 | * Authors: |
| 9 | * Izik Eidus |
| 10 | * Andrea Arcangeli |
| 11 | * Chris Wright |
Izik Eidus | 36b2528 | 2009-09-21 17:02:06 -0700 | [diff] [blame] | 12 | * Hugh Dickins |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2. |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/errno.h> |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 18 | #include <linux/mm.h> |
| 19 | #include <linux/fs.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 20 | #include <linux/mman.h> |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 21 | #include <linux/sched.h> |
| 22 | #include <linux/rwsem.h> |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <linux/rmap.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/jhash.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/kthread.h> |
| 29 | #include <linux/wait.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/rbtree.h> |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 32 | #include <linux/memory.h> |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 33 | #include <linux/mmu_notifier.h> |
Izik Eidus | 2c6854f | 2009-09-23 15:56:04 -0700 | [diff] [blame] | 34 | #include <linux/swap.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 35 | #include <linux/ksm.h> |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 36 | #include <linux/hashtable.h> |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 37 | #include <linux/freezer.h> |
David Rientjes | 72788c3 | 2011-05-24 17:11:40 -0700 | [diff] [blame] | 38 | #include <linux/oom.h> |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 39 | #include <linux/numa.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 40 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 41 | #include <asm/tlbflush.h> |
Hugh Dickins | 73848b4 | 2009-12-14 17:59:22 -0800 | [diff] [blame] | 42 | #include "internal.h" |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 43 | |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 44 | #ifdef CONFIG_NUMA |
| 45 | #define NUMA(x) (x) |
| 46 | #define DO_NUMA(x) do { (x); } while (0) |
| 47 | #else |
| 48 | #define NUMA(x) (0) |
| 49 | #define DO_NUMA(x) do { } while (0) |
| 50 | #endif |
| 51 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 52 | /* |
| 53 | * A few notes about the KSM scanning process, |
| 54 | * to make it easier to understand the data structures below: |
| 55 | * |
| 56 | * In order to reduce excessive scanning, KSM sorts the memory pages by their |
| 57 | * contents into a data structure that holds pointers to the pages' locations. |
| 58 | * |
| 59 | * Since the contents of the pages may change at any moment, KSM cannot just |
| 60 | * insert the pages into a normal sorted tree and expect it to find anything. |
| 61 | * Therefore KSM uses two data structures - the stable and the unstable tree. |
| 62 | * |
| 63 | * The stable tree holds pointers to all the merged pages (ksm pages), sorted |
| 64 | * by their contents. Because each such page is write-protected, searching on |
| 65 | * this tree is fully assured to be working (except when pages are unmapped), |
| 66 | * and therefore this tree is called the stable tree. |
| 67 | * |
| 68 | * In addition to the stable tree, KSM uses a second data structure called the |
| 69 | * unstable tree: this tree holds pointers to pages which have been found to |
| 70 | * be "unchanged for a period of time". The unstable tree sorts these pages |
| 71 | * by their contents, but since they are not write-protected, KSM cannot rely |
| 72 | * upon the unstable tree to work correctly - the unstable tree is liable to |
| 73 | * be corrupted as its contents are modified, and so it is called unstable. |
| 74 | * |
| 75 | * KSM solves this problem by several techniques: |
| 76 | * |
| 77 | * 1) The unstable tree is flushed every time KSM completes scanning all |
| 78 | * memory areas, and then the tree is rebuilt again from the beginning. |
| 79 | * 2) KSM will only insert into the unstable tree, pages whose hash value |
| 80 | * has not changed since the previous scan of all memory areas. |
| 81 | * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the |
| 82 | * colors of the nodes and not on their contents, assuring that even when |
| 83 | * the tree gets "corrupted" it won't get out of balance, so scanning time |
| 84 | * remains the same (also, searching and inserting nodes in an rbtree uses |
| 85 | * the same algorithm, so we have no overhead when we flush and rebuild). |
| 86 | * 4) KSM never flushes the stable tree, which means that even if it were to |
| 87 | * take 10 attempts to find a page in the unstable tree, once it is found, |
| 88 | * it is secured in the stable tree. (When we scan a new page, we first |
| 89 | * compare it against the stable tree, and then against the unstable tree.) |
| 90 | */ |
| 91 | |
| 92 | /** |
| 93 | * struct mm_slot - ksm information per mm that is being scanned |
| 94 | * @link: link to the mm_slots hash list |
| 95 | * @mm_list: link into the mm_slots list, rooted in ksm_mm_head |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 96 | * @rmap_list: head for this mm_slot's singly-linked list of rmap_items |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 97 | * @mm: the mm that this information is valid for |
| 98 | */ |
| 99 | struct mm_slot { |
| 100 | struct hlist_node link; |
| 101 | struct list_head mm_list; |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 102 | struct rmap_item *rmap_list; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 103 | struct mm_struct *mm; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * struct ksm_scan - cursor for scanning |
| 108 | * @mm_slot: the current mm_slot we are scanning |
| 109 | * @address: the next address inside that to be scanned |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 110 | * @rmap_list: link to the next rmap to be scanned in the rmap_list |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 111 | * @seqnr: count of completed full scans (needed when removing unstable node) |
| 112 | * |
| 113 | * There is only the one ksm_scan instance of this cursor structure. |
| 114 | */ |
| 115 | struct ksm_scan { |
| 116 | struct mm_slot *mm_slot; |
| 117 | unsigned long address; |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 118 | struct rmap_item **rmap_list; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 119 | unsigned long seqnr; |
| 120 | }; |
| 121 | |
| 122 | /** |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 123 | * struct stable_node - node of the stable rbtree |
| 124 | * @node: rb node of this ksm page in the stable tree |
| 125 | * @hlist: hlist head of rmap_items using this ksm page |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 126 | * @kpfn: page frame number of this ksm page |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 127 | */ |
| 128 | struct stable_node { |
| 129 | struct rb_node node; |
| 130 | struct hlist_head hlist; |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 131 | unsigned long kpfn; |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | /** |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 135 | * struct rmap_item - reverse mapping item for virtual addresses |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 136 | * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 137 | * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 138 | * @mm: the memory structure this rmap_item is pointing into |
| 139 | * @address: the virtual address this rmap_item tracks (+ flags in low bits) |
| 140 | * @oldchecksum: previous checksum of the page at that virtual address |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 141 | * @nid: NUMA node id of unstable tree in which linked (may not match page) |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 142 | * @node: rb node of this rmap_item in the unstable tree |
| 143 | * @head: pointer to stable_node heading this list in the stable tree |
| 144 | * @hlist: link into hlist of rmap_items hanging off that stable_node |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 145 | */ |
| 146 | struct rmap_item { |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 147 | struct rmap_item *rmap_list; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 148 | struct anon_vma *anon_vma; /* when stable */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 149 | struct mm_struct *mm; |
| 150 | unsigned long address; /* + low bits used for flags below */ |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 151 | unsigned int oldchecksum; /* when unstable */ |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 152 | #ifdef CONFIG_NUMA |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 153 | int nid; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 154 | #endif |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 155 | union { |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 156 | struct rb_node node; /* when node of unstable tree */ |
| 157 | struct { /* when listed from stable tree */ |
| 158 | struct stable_node *head; |
| 159 | struct hlist_node hlist; |
| 160 | }; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 161 | }; |
| 162 | }; |
| 163 | |
| 164 | #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */ |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 165 | #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */ |
| 166 | #define STABLE_FLAG 0x200 /* is listed from the stable tree */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 167 | |
| 168 | /* The stable and unstable tree heads */ |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 169 | static struct rb_root root_unstable_tree[MAX_NUMNODES]; |
| 170 | static struct rb_root root_stable_tree[MAX_NUMNODES]; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 171 | |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 172 | #define MM_SLOTS_HASH_BITS 10 |
| 173 | static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 174 | |
| 175 | static struct mm_slot ksm_mm_head = { |
| 176 | .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list), |
| 177 | }; |
| 178 | static struct ksm_scan ksm_scan = { |
| 179 | .mm_slot = &ksm_mm_head, |
| 180 | }; |
| 181 | |
| 182 | static struct kmem_cache *rmap_item_cache; |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 183 | static struct kmem_cache *stable_node_cache; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 184 | static struct kmem_cache *mm_slot_cache; |
| 185 | |
| 186 | /* The number of nodes in the stable tree */ |
Hugh Dickins | b402826 | 2009-09-21 17:02:09 -0700 | [diff] [blame] | 187 | static unsigned long ksm_pages_shared; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 188 | |
Hugh Dickins | e178dfd | 2009-09-21 17:02:10 -0700 | [diff] [blame] | 189 | /* The number of page slots additionally sharing those nodes */ |
Hugh Dickins | b402826 | 2009-09-21 17:02:09 -0700 | [diff] [blame] | 190 | static unsigned long ksm_pages_sharing; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 191 | |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 192 | /* The number of nodes in the unstable tree */ |
| 193 | static unsigned long ksm_pages_unshared; |
| 194 | |
| 195 | /* The number of rmap_items in use: to calculate pages_volatile */ |
| 196 | static unsigned long ksm_rmap_items; |
| 197 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 198 | /* Number of pages ksmd should scan in one batch */ |
Izik Eidus | 2c6854f | 2009-09-23 15:56:04 -0700 | [diff] [blame] | 199 | static unsigned int ksm_thread_pages_to_scan = 100; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 200 | |
| 201 | /* Milliseconds ksmd should sleep between batches */ |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 202 | static unsigned int ksm_thread_sleep_millisecs = 20; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 203 | |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 204 | #ifdef CONFIG_NUMA |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 205 | /* Zeroed when merging across nodes is not allowed */ |
| 206 | static unsigned int ksm_merge_across_nodes = 1; |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 207 | #else |
| 208 | #define ksm_merge_across_nodes 1U |
| 209 | #endif |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 210 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 211 | #define KSM_RUN_STOP 0 |
| 212 | #define KSM_RUN_MERGE 1 |
| 213 | #define KSM_RUN_UNMERGE 2 |
Izik Eidus | 2c6854f | 2009-09-23 15:56:04 -0700 | [diff] [blame] | 214 | static unsigned int ksm_run = KSM_RUN_STOP; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 215 | |
| 216 | static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait); |
| 217 | static DEFINE_MUTEX(ksm_thread_mutex); |
| 218 | static DEFINE_SPINLOCK(ksm_mmlist_lock); |
| 219 | |
| 220 | #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\ |
| 221 | sizeof(struct __struct), __alignof__(struct __struct),\ |
| 222 | (__flags), NULL) |
| 223 | |
| 224 | static int __init ksm_slab_init(void) |
| 225 | { |
| 226 | rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0); |
| 227 | if (!rmap_item_cache) |
| 228 | goto out; |
| 229 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 230 | stable_node_cache = KSM_KMEM_CACHE(stable_node, 0); |
| 231 | if (!stable_node_cache) |
| 232 | goto out_free1; |
| 233 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 234 | mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0); |
| 235 | if (!mm_slot_cache) |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 236 | goto out_free2; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 240 | out_free2: |
| 241 | kmem_cache_destroy(stable_node_cache); |
| 242 | out_free1: |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 243 | kmem_cache_destroy(rmap_item_cache); |
| 244 | out: |
| 245 | return -ENOMEM; |
| 246 | } |
| 247 | |
| 248 | static void __init ksm_slab_free(void) |
| 249 | { |
| 250 | kmem_cache_destroy(mm_slot_cache); |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 251 | kmem_cache_destroy(stable_node_cache); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 252 | kmem_cache_destroy(rmap_item_cache); |
| 253 | mm_slot_cache = NULL; |
| 254 | } |
| 255 | |
| 256 | static inline struct rmap_item *alloc_rmap_item(void) |
| 257 | { |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 258 | struct rmap_item *rmap_item; |
| 259 | |
| 260 | rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL); |
| 261 | if (rmap_item) |
| 262 | ksm_rmap_items++; |
| 263 | return rmap_item; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static inline void free_rmap_item(struct rmap_item *rmap_item) |
| 267 | { |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 268 | ksm_rmap_items--; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 269 | rmap_item->mm = NULL; /* debug safety */ |
| 270 | kmem_cache_free(rmap_item_cache, rmap_item); |
| 271 | } |
| 272 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 273 | static inline struct stable_node *alloc_stable_node(void) |
| 274 | { |
| 275 | return kmem_cache_alloc(stable_node_cache, GFP_KERNEL); |
| 276 | } |
| 277 | |
| 278 | static inline void free_stable_node(struct stable_node *stable_node) |
| 279 | { |
| 280 | kmem_cache_free(stable_node_cache, stable_node); |
| 281 | } |
| 282 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 283 | static inline struct mm_slot *alloc_mm_slot(void) |
| 284 | { |
| 285 | if (!mm_slot_cache) /* initialization failed */ |
| 286 | return NULL; |
| 287 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); |
| 288 | } |
| 289 | |
| 290 | static inline void free_mm_slot(struct mm_slot *mm_slot) |
| 291 | { |
| 292 | kmem_cache_free(mm_slot_cache, mm_slot); |
| 293 | } |
| 294 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 295 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
| 296 | { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 297 | struct hlist_node *node; |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 298 | struct mm_slot *slot; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 299 | |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 300 | hash_for_each_possible(mm_slots_hash, slot, node, link, (unsigned long)mm) |
| 301 | if (slot->mm == mm) |
| 302 | return slot; |
| 303 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | static void insert_to_mm_slots_hash(struct mm_struct *mm, |
| 308 | struct mm_slot *mm_slot) |
| 309 | { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 310 | mm_slot->mm = mm; |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 311 | hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static inline int in_stable_tree(struct rmap_item *rmap_item) |
| 315 | { |
| 316 | return rmap_item->address & STABLE_FLAG; |
| 317 | } |
| 318 | |
| 319 | /* |
Hugh Dickins | a913e18 | 2009-09-21 17:02:26 -0700 | [diff] [blame] | 320 | * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's |
| 321 | * page tables after it has passed through ksm_exit() - which, if necessary, |
| 322 | * takes mmap_sem briefly to serialize against them. ksm_exit() does not set |
| 323 | * a special flag: they can just back out as soon as mm_users goes to zero. |
| 324 | * ksm_test_exit() is used throughout to make this test for exit: in some |
| 325 | * places for correctness, in some places just to avoid unnecessary work. |
| 326 | */ |
| 327 | static inline bool ksm_test_exit(struct mm_struct *mm) |
| 328 | { |
| 329 | return atomic_read(&mm->mm_users) == 0; |
| 330 | } |
| 331 | |
| 332 | /* |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 333 | * We use break_ksm to break COW on a ksm page: it's a stripped down |
| 334 | * |
| 335 | * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1) |
| 336 | * put_page(page); |
| 337 | * |
| 338 | * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma, |
| 339 | * in case the application has unmapped and remapped mm,addr meanwhile. |
| 340 | * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP |
| 341 | * mmap of /dev/mem or /dev/kmem, where we would not want to touch it. |
| 342 | */ |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 343 | static int break_ksm(struct vm_area_struct *vma, unsigned long addr) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 344 | { |
| 345 | struct page *page; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 346 | int ret = 0; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 347 | |
| 348 | do { |
| 349 | cond_resched(); |
| 350 | page = follow_page(vma, addr, FOLL_GET); |
Dan Carpenter | 22eccdd | 2010-04-23 13:18:10 -0400 | [diff] [blame] | 351 | if (IS_ERR_OR_NULL(page)) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 352 | break; |
| 353 | if (PageKsm(page)) |
| 354 | ret = handle_mm_fault(vma->vm_mm, vma, addr, |
| 355 | FAULT_FLAG_WRITE); |
| 356 | else |
| 357 | ret = VM_FAULT_WRITE; |
| 358 | put_page(page); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 359 | } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM))); |
| 360 | /* |
| 361 | * We must loop because handle_mm_fault() may back out if there's |
| 362 | * any difficulty e.g. if pte accessed bit gets updated concurrently. |
| 363 | * |
| 364 | * VM_FAULT_WRITE is what we have been hoping for: it indicates that |
| 365 | * COW has been broken, even if the vma does not permit VM_WRITE; |
| 366 | * but note that a concurrent fault might break PageKsm for us. |
| 367 | * |
| 368 | * VM_FAULT_SIGBUS could occur if we race with truncation of the |
| 369 | * backing file, which also invalidates anonymous pages: that's |
| 370 | * okay, that truncation will have unmapped the PageKsm for us. |
| 371 | * |
| 372 | * VM_FAULT_OOM: at the time of writing (late July 2009), setting |
| 373 | * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the |
| 374 | * current task has TIF_MEMDIE set, and will be OOM killed on return |
| 375 | * to user; and ksmd, having no mm, would never be chosen for that. |
| 376 | * |
| 377 | * But if the mm is in a limited mem_cgroup, then the fault may fail |
| 378 | * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and |
| 379 | * even ksmd can fail in this way - though it's usually breaking ksm |
| 380 | * just to undo a merge it made a moment before, so unlikely to oom. |
| 381 | * |
| 382 | * That's a pity: we might therefore have more kernel pages allocated |
| 383 | * than we're counting as nodes in the stable tree; but ksm_do_scan |
| 384 | * will retry to break_cow on each pass, so should recover the page |
| 385 | * in due course. The important thing is to not let VM_MERGEABLE |
| 386 | * be cleared while any such pages might remain in the area. |
| 387 | */ |
| 388 | return (ret & VM_FAULT_OOM) ? -ENOMEM : 0; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Bob Liu | ef69422 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 391 | static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm, |
| 392 | unsigned long addr) |
| 393 | { |
| 394 | struct vm_area_struct *vma; |
| 395 | if (ksm_test_exit(mm)) |
| 396 | return NULL; |
| 397 | vma = find_vma(mm, addr); |
| 398 | if (!vma || vma->vm_start > addr) |
| 399 | return NULL; |
| 400 | if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) |
| 401 | return NULL; |
| 402 | return vma; |
| 403 | } |
| 404 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 405 | static void break_cow(struct rmap_item *rmap_item) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 406 | { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 407 | struct mm_struct *mm = rmap_item->mm; |
| 408 | unsigned long addr = rmap_item->address; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 409 | struct vm_area_struct *vma; |
| 410 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 411 | /* |
| 412 | * It is not an accident that whenever we want to break COW |
| 413 | * to undo, we also need to drop a reference to the anon_vma. |
| 414 | */ |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 415 | put_anon_vma(rmap_item->anon_vma); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 416 | |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 417 | down_read(&mm->mmap_sem); |
Bob Liu | ef69422 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 418 | vma = find_mergeable_vma(mm, addr); |
| 419 | if (vma) |
| 420 | break_ksm(vma, addr); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 421 | up_read(&mm->mmap_sem); |
| 422 | } |
| 423 | |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 424 | static struct page *page_trans_compound_anon(struct page *page) |
| 425 | { |
| 426 | if (PageTransCompound(page)) { |
Andrea Arcangeli | 22e5c47 | 2011-01-13 15:47:20 -0800 | [diff] [blame] | 427 | struct page *head = compound_trans_head(page); |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 428 | /* |
Andrea Arcangeli | 22e5c47 | 2011-01-13 15:47:20 -0800 | [diff] [blame] | 429 | * head may actually be splitted and freed from under |
| 430 | * us but it's ok here. |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 431 | */ |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 432 | if (PageAnon(head)) |
| 433 | return head; |
| 434 | } |
| 435 | return NULL; |
| 436 | } |
| 437 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 438 | static struct page *get_mergeable_page(struct rmap_item *rmap_item) |
| 439 | { |
| 440 | struct mm_struct *mm = rmap_item->mm; |
| 441 | unsigned long addr = rmap_item->address; |
| 442 | struct vm_area_struct *vma; |
| 443 | struct page *page; |
| 444 | |
| 445 | down_read(&mm->mmap_sem); |
Bob Liu | ef69422 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 446 | vma = find_mergeable_vma(mm, addr); |
| 447 | if (!vma) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 448 | goto out; |
| 449 | |
| 450 | page = follow_page(vma, addr, FOLL_GET); |
Dan Carpenter | 22eccdd | 2010-04-23 13:18:10 -0400 | [diff] [blame] | 451 | if (IS_ERR_OR_NULL(page)) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 452 | goto out; |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 453 | if (PageAnon(page) || page_trans_compound_anon(page)) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 454 | flush_anon_page(vma, page, addr); |
| 455 | flush_dcache_page(page); |
| 456 | } else { |
| 457 | put_page(page); |
| 458 | out: page = NULL; |
| 459 | } |
| 460 | up_read(&mm->mmap_sem); |
| 461 | return page; |
| 462 | } |
| 463 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 464 | /* |
| 465 | * This helper is used for getting right index into array of tree roots. |
| 466 | * When merge_across_nodes knob is set to 1, there are only two rb-trees for |
| 467 | * stable and unstable pages from all nodes with roots in index 0. Otherwise, |
| 468 | * every node has its own stable and unstable tree. |
| 469 | */ |
| 470 | static inline int get_kpfn_nid(unsigned long kpfn) |
| 471 | { |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 472 | return ksm_merge_across_nodes ? 0 : pfn_to_nid(kpfn); |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 475 | static void remove_node_from_stable_tree(struct stable_node *stable_node) |
| 476 | { |
| 477 | struct rmap_item *rmap_item; |
| 478 | struct hlist_node *hlist; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 479 | int nid; |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 480 | |
| 481 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { |
| 482 | if (rmap_item->hlist.next) |
| 483 | ksm_pages_sharing--; |
| 484 | else |
| 485 | ksm_pages_shared--; |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 486 | put_anon_vma(rmap_item->anon_vma); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 487 | rmap_item->address &= PAGE_MASK; |
| 488 | cond_resched(); |
| 489 | } |
| 490 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 491 | nid = get_kpfn_nid(stable_node->kpfn); |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 492 | rb_erase(&stable_node->node, &root_stable_tree[nid]); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 493 | free_stable_node(stable_node); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * get_ksm_page: checks if the page indicated by the stable node |
| 498 | * is still its ksm page, despite having held no reference to it. |
| 499 | * In which case we can trust the content of the page, and it |
| 500 | * returns the gotten page; but if the page has now been zapped, |
| 501 | * remove the stale node from the stable tree and return NULL. |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 502 | * But beware, the stable node's page might be being migrated. |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 503 | * |
| 504 | * You would expect the stable_node to hold a reference to the ksm page. |
| 505 | * But if it increments the page's count, swapping out has to wait for |
| 506 | * ksmd to come around again before it can free the page, which may take |
| 507 | * seconds or even minutes: much too unresponsive. So instead we use a |
| 508 | * "keyhole reference": access to the ksm page from the stable node peeps |
| 509 | * out through its keyhole to see if that page still holds the right key, |
| 510 | * pointing back to this stable node. This relies on freeing a PageAnon |
| 511 | * page to reset its page->mapping to NULL, and relies on no other use of |
| 512 | * a page to put something that might look like our key in page->mapping. |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 513 | * is on its way to being freed; but it is an anomaly to bear in mind. |
| 514 | */ |
Hugh Dickins | 8aafa6a | 2013-02-22 16:35:06 -0800 | [diff] [blame] | 515 | static struct page *get_ksm_page(struct stable_node *stable_node, bool locked) |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 516 | { |
| 517 | struct page *page; |
| 518 | void *expected_mapping; |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 519 | unsigned long kpfn; |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 520 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 521 | expected_mapping = (void *)stable_node + |
| 522 | (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 523 | again: |
| 524 | kpfn = ACCESS_ONCE(stable_node->kpfn); |
| 525 | page = pfn_to_page(kpfn); |
| 526 | |
| 527 | /* |
| 528 | * page is computed from kpfn, so on most architectures reading |
| 529 | * page->mapping is naturally ordered after reading node->kpfn, |
| 530 | * but on Alpha we need to be more careful. |
| 531 | */ |
| 532 | smp_read_barrier_depends(); |
| 533 | if (ACCESS_ONCE(page->mapping) != expected_mapping) |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 534 | goto stale; |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 535 | |
| 536 | /* |
| 537 | * We cannot do anything with the page while its refcount is 0. |
| 538 | * Usually 0 means free, or tail of a higher-order page: in which |
| 539 | * case this node is no longer referenced, and should be freed; |
| 540 | * however, it might mean that the page is under page_freeze_refs(). |
| 541 | * The __remove_mapping() case is easy, again the node is now stale; |
| 542 | * but if page is swapcache in migrate_page_move_mapping(), it might |
| 543 | * still be our page, in which case it's essential to keep the node. |
| 544 | */ |
| 545 | while (!get_page_unless_zero(page)) { |
| 546 | /* |
| 547 | * Another check for page->mapping != expected_mapping would |
| 548 | * work here too. We have chosen the !PageSwapCache test to |
| 549 | * optimize the common case, when the page is or is about to |
| 550 | * be freed: PageSwapCache is cleared (under spin_lock_irq) |
| 551 | * in the freeze_refs section of __remove_mapping(); but Anon |
| 552 | * page->mapping reset to NULL later, in free_pages_prepare(). |
| 553 | */ |
| 554 | if (!PageSwapCache(page)) |
| 555 | goto stale; |
| 556 | cpu_relax(); |
| 557 | } |
| 558 | |
| 559 | if (ACCESS_ONCE(page->mapping) != expected_mapping) { |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 560 | put_page(page); |
| 561 | goto stale; |
| 562 | } |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 563 | |
Hugh Dickins | 8aafa6a | 2013-02-22 16:35:06 -0800 | [diff] [blame] | 564 | if (locked) { |
| 565 | lock_page(page); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 566 | if (ACCESS_ONCE(page->mapping) != expected_mapping) { |
Hugh Dickins | 8aafa6a | 2013-02-22 16:35:06 -0800 | [diff] [blame] | 567 | unlock_page(page); |
| 568 | put_page(page); |
| 569 | goto stale; |
| 570 | } |
| 571 | } |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 572 | return page; |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 573 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 574 | stale: |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 575 | /* |
| 576 | * We come here from above when page->mapping or !PageSwapCache |
| 577 | * suggests that the node is stale; but it might be under migration. |
| 578 | * We need smp_rmb(), matching the smp_wmb() in ksm_migrate_page(), |
| 579 | * before checking whether node->kpfn has been changed. |
| 580 | */ |
| 581 | smp_rmb(); |
| 582 | if (ACCESS_ONCE(stable_node->kpfn) != kpfn) |
| 583 | goto again; |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 584 | remove_node_from_stable_tree(stable_node); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 588 | /* |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 589 | * Removing rmap_item from stable or unstable tree. |
| 590 | * This function will clean the information from the stable/unstable tree. |
| 591 | */ |
| 592 | static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) |
| 593 | { |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 594 | if (rmap_item->address & STABLE_FLAG) { |
| 595 | struct stable_node *stable_node; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 596 | struct page *page; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 597 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 598 | stable_node = rmap_item->head; |
Hugh Dickins | 8aafa6a | 2013-02-22 16:35:06 -0800 | [diff] [blame] | 599 | page = get_ksm_page(stable_node, true); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 600 | if (!page) |
| 601 | goto out; |
| 602 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 603 | hlist_del(&rmap_item->hlist); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 604 | unlock_page(page); |
| 605 | put_page(page); |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 606 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 607 | if (stable_node->hlist.first) |
| 608 | ksm_pages_sharing--; |
| 609 | else |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 610 | ksm_pages_shared--; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 611 | |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 612 | put_anon_vma(rmap_item->anon_vma); |
Hugh Dickins | 93d1771 | 2009-12-14 17:59:16 -0800 | [diff] [blame] | 613 | rmap_item->address &= PAGE_MASK; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 614 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 615 | } else if (rmap_item->address & UNSTABLE_FLAG) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 616 | unsigned char age; |
| 617 | /* |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 618 | * Usually ksmd can and must skip the rb_erase, because |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 619 | * root_unstable_tree was already reset to RB_ROOT. |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 620 | * But be careful when an mm is exiting: do the rb_erase |
| 621 | * if this rmap_item was inserted by this scan, rather |
| 622 | * than left over from before. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 623 | */ |
| 624 | age = (unsigned char)(ksm_scan.seqnr - rmap_item->address); |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 625 | BUG_ON(age > 1); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 626 | if (!age) |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 627 | rb_erase(&rmap_item->node, |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 628 | &root_unstable_tree[NUMA(rmap_item->nid)]); |
Hugh Dickins | 93d1771 | 2009-12-14 17:59:16 -0800 | [diff] [blame] | 629 | ksm_pages_unshared--; |
| 630 | rmap_item->address &= PAGE_MASK; |
| 631 | } |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 632 | out: |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 633 | cond_resched(); /* we're called from many long loops */ |
| 634 | } |
| 635 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 636 | static void remove_trailing_rmap_items(struct mm_slot *mm_slot, |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 637 | struct rmap_item **rmap_list) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 638 | { |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 639 | while (*rmap_list) { |
| 640 | struct rmap_item *rmap_item = *rmap_list; |
| 641 | *rmap_list = rmap_item->rmap_list; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 642 | remove_rmap_item_from_tree(rmap_item); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 643 | free_rmap_item(rmap_item); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /* |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 648 | * Though it's very tempting to unmerge rmap_items from stable tree rather |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 649 | * than check every pte of a given vma, the locking doesn't quite work for |
| 650 | * that - an rmap_item is assigned to the stable tree after inserting ksm |
| 651 | * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing |
| 652 | * rmap_items from parent to child at fork time (so as not to waste time |
| 653 | * if exit comes before the next scan reaches it). |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 654 | * |
| 655 | * Similarly, although we'd like to remove rmap_items (so updating counts |
| 656 | * and freeing memory) when unmerging an area, it's easier to leave that |
| 657 | * to the next pass of ksmd - consider, for example, how ksmd might be |
| 658 | * in cmp_and_merge_page on one of the rmap_items we would be removing. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 659 | */ |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 660 | static int unmerge_ksm_pages(struct vm_area_struct *vma, |
| 661 | unsigned long start, unsigned long end) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 662 | { |
| 663 | unsigned long addr; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 664 | int err = 0; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 665 | |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 666 | for (addr = start; addr < end && !err; addr += PAGE_SIZE) { |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 667 | if (ksm_test_exit(vma->vm_mm)) |
| 668 | break; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 669 | if (signal_pending(current)) |
| 670 | err = -ERESTARTSYS; |
| 671 | else |
| 672 | err = break_ksm(vma, addr); |
| 673 | } |
| 674 | return err; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 677 | #ifdef CONFIG_SYSFS |
| 678 | /* |
| 679 | * Only called through the sysfs control interface: |
| 680 | */ |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 681 | static int remove_stable_node(struct stable_node *stable_node) |
| 682 | { |
| 683 | struct page *page; |
| 684 | int err; |
| 685 | |
| 686 | page = get_ksm_page(stable_node, true); |
| 687 | if (!page) { |
| 688 | /* |
| 689 | * get_ksm_page did remove_node_from_stable_tree itself. |
| 690 | */ |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | if (WARN_ON_ONCE(page_mapped(page))) |
| 695 | err = -EBUSY; |
| 696 | else { |
| 697 | /* |
| 698 | * This page might be in a pagevec waiting to be freed, |
| 699 | * or it might be PageSwapCache (perhaps under writeback), |
| 700 | * or it might have been removed from swapcache a moment ago. |
| 701 | */ |
| 702 | set_page_stable_node(page, NULL); |
| 703 | remove_node_from_stable_tree(stable_node); |
| 704 | err = 0; |
| 705 | } |
| 706 | |
| 707 | unlock_page(page); |
| 708 | put_page(page); |
| 709 | return err; |
| 710 | } |
| 711 | |
| 712 | static int remove_all_stable_nodes(void) |
| 713 | { |
| 714 | struct stable_node *stable_node; |
| 715 | int nid; |
| 716 | int err = 0; |
| 717 | |
| 718 | for (nid = 0; nid < nr_node_ids; nid++) { |
| 719 | while (root_stable_tree[nid].rb_node) { |
| 720 | stable_node = rb_entry(root_stable_tree[nid].rb_node, |
| 721 | struct stable_node, node); |
| 722 | if (remove_stable_node(stable_node)) { |
| 723 | err = -EBUSY; |
| 724 | break; /* proceed to next nid */ |
| 725 | } |
| 726 | cond_resched(); |
| 727 | } |
| 728 | } |
| 729 | return err; |
| 730 | } |
| 731 | |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 732 | static int unmerge_and_remove_all_rmap_items(void) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 733 | { |
| 734 | struct mm_slot *mm_slot; |
| 735 | struct mm_struct *mm; |
| 736 | struct vm_area_struct *vma; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 737 | int err = 0; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 738 | |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 739 | spin_lock(&ksm_mmlist_lock); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 740 | ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next, |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 741 | struct mm_slot, mm_list); |
| 742 | spin_unlock(&ksm_mmlist_lock); |
| 743 | |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 744 | for (mm_slot = ksm_scan.mm_slot; |
| 745 | mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 746 | mm = mm_slot->mm; |
| 747 | down_read(&mm->mmap_sem); |
| 748 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 749 | if (ksm_test_exit(mm)) |
| 750 | break; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 751 | if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) |
| 752 | continue; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 753 | err = unmerge_ksm_pages(vma, |
| 754 | vma->vm_start, vma->vm_end); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 755 | if (err) |
| 756 | goto error; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 757 | } |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 758 | |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 759 | remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 760 | |
| 761 | spin_lock(&ksm_mmlist_lock); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 762 | ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next, |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 763 | struct mm_slot, mm_list); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 764 | if (ksm_test_exit(mm)) { |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 765 | hash_del(&mm_slot->link); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 766 | list_del(&mm_slot->mm_list); |
| 767 | spin_unlock(&ksm_mmlist_lock); |
| 768 | |
| 769 | free_mm_slot(mm_slot); |
| 770 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); |
| 771 | up_read(&mm->mmap_sem); |
| 772 | mmdrop(mm); |
| 773 | } else { |
| 774 | spin_unlock(&ksm_mmlist_lock); |
| 775 | up_read(&mm->mmap_sem); |
| 776 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 779 | /* Clean up stable nodes, but don't worry if some are still busy */ |
| 780 | remove_all_stable_nodes(); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 781 | ksm_scan.seqnr = 0; |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 782 | return 0; |
| 783 | |
| 784 | error: |
| 785 | up_read(&mm->mmap_sem); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 786 | spin_lock(&ksm_mmlist_lock); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 787 | ksm_scan.mm_slot = &ksm_mm_head; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 788 | spin_unlock(&ksm_mmlist_lock); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 789 | return err; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 790 | } |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 791 | #endif /* CONFIG_SYSFS */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 792 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 793 | static u32 calc_checksum(struct page *page) |
| 794 | { |
| 795 | u32 checksum; |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 796 | void *addr = kmap_atomic(page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 797 | checksum = jhash2(addr, PAGE_SIZE / 4, 17); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 798 | kunmap_atomic(addr); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 799 | return checksum; |
| 800 | } |
| 801 | |
| 802 | static int memcmp_pages(struct page *page1, struct page *page2) |
| 803 | { |
| 804 | char *addr1, *addr2; |
| 805 | int ret; |
| 806 | |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 807 | addr1 = kmap_atomic(page1); |
| 808 | addr2 = kmap_atomic(page2); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 809 | ret = memcmp(addr1, addr2, PAGE_SIZE); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 810 | kunmap_atomic(addr2); |
| 811 | kunmap_atomic(addr1); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | static inline int pages_identical(struct page *page1, struct page *page2) |
| 816 | { |
| 817 | return !memcmp_pages(page1, page2); |
| 818 | } |
| 819 | |
| 820 | static int write_protect_page(struct vm_area_struct *vma, struct page *page, |
| 821 | pte_t *orig_pte) |
| 822 | { |
| 823 | struct mm_struct *mm = vma->vm_mm; |
| 824 | unsigned long addr; |
| 825 | pte_t *ptep; |
| 826 | spinlock_t *ptl; |
| 827 | int swapped; |
| 828 | int err = -EFAULT; |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 829 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 830 | unsigned long mmun_end; /* For mmu_notifiers */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 831 | |
| 832 | addr = page_address_in_vma(page, vma); |
| 833 | if (addr == -EFAULT) |
| 834 | goto out; |
| 835 | |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 836 | BUG_ON(PageTransCompound(page)); |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 837 | |
| 838 | mmun_start = addr; |
| 839 | mmun_end = addr + PAGE_SIZE; |
| 840 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 841 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 842 | ptep = page_check_address(page, mm, addr, &ptl, 0); |
| 843 | if (!ptep) |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 844 | goto out_mn; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 845 | |
Hugh Dickins | 4e31635 | 2010-10-02 17:49:08 -0700 | [diff] [blame] | 846 | if (pte_write(*ptep) || pte_dirty(*ptep)) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 847 | pte_t entry; |
| 848 | |
| 849 | swapped = PageSwapCache(page); |
| 850 | flush_cache_page(vma, addr, page_to_pfn(page)); |
| 851 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 852 | * Ok this is tricky, when get_user_pages_fast() run it doesn't |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 853 | * take any lock, therefore the check that we are going to make |
| 854 | * with the pagecount against the mapcount is racey and |
| 855 | * O_DIRECT can happen right after the check. |
| 856 | * So we clear the pte and flush the tlb before the check |
| 857 | * this assure us that no O_DIRECT can happen after the check |
| 858 | * or in the middle of the check. |
| 859 | */ |
| 860 | entry = ptep_clear_flush(vma, addr, ptep); |
| 861 | /* |
| 862 | * Check that no O_DIRECT or similar I/O is in progress on the |
| 863 | * page |
| 864 | */ |
Hugh Dickins | 31e855e | 2009-12-14 17:59:17 -0800 | [diff] [blame] | 865 | if (page_mapcount(page) + 1 + swapped != page_count(page)) { |
Robin Holt | cb53237 | 2010-03-23 13:35:26 -0700 | [diff] [blame] | 866 | set_pte_at(mm, addr, ptep, entry); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 867 | goto out_unlock; |
| 868 | } |
Hugh Dickins | 4e31635 | 2010-10-02 17:49:08 -0700 | [diff] [blame] | 869 | if (pte_dirty(entry)) |
| 870 | set_page_dirty(page); |
| 871 | entry = pte_mkclean(pte_wrprotect(entry)); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 872 | set_pte_at_notify(mm, addr, ptep, entry); |
| 873 | } |
| 874 | *orig_pte = *ptep; |
| 875 | err = 0; |
| 876 | |
| 877 | out_unlock: |
| 878 | pte_unmap_unlock(ptep, ptl); |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 879 | out_mn: |
| 880 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 881 | out: |
| 882 | return err; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * replace_page - replace page in vma by new ksm page |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 887 | * @vma: vma that holds the pte pointing to page |
| 888 | * @page: the page we are replacing by kpage |
| 889 | * @kpage: the ksm page we replace page by |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 890 | * @orig_pte: the original value of the pte |
| 891 | * |
| 892 | * Returns 0 on success, -EFAULT on failure. |
| 893 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 894 | static int replace_page(struct vm_area_struct *vma, struct page *page, |
| 895 | struct page *kpage, pte_t orig_pte) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 896 | { |
| 897 | struct mm_struct *mm = vma->vm_mm; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 898 | pmd_t *pmd; |
| 899 | pte_t *ptep; |
| 900 | spinlock_t *ptl; |
| 901 | unsigned long addr; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 902 | int err = -EFAULT; |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 903 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 904 | unsigned long mmun_end; /* For mmu_notifiers */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 905 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 906 | addr = page_address_in_vma(page, vma); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 907 | if (addr == -EFAULT) |
| 908 | goto out; |
| 909 | |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 910 | pmd = mm_find_pmd(mm, addr); |
| 911 | if (!pmd) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 912 | goto out; |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 913 | BUG_ON(pmd_trans_huge(*pmd)); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 914 | |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 915 | mmun_start = addr; |
| 916 | mmun_end = addr + PAGE_SIZE; |
| 917 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 918 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 919 | ptep = pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 920 | if (!pte_same(*ptep, orig_pte)) { |
| 921 | pte_unmap_unlock(ptep, ptl); |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 922 | goto out_mn; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 925 | get_page(kpage); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 926 | page_add_anon_rmap(kpage, vma, addr); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 927 | |
| 928 | flush_cache_page(vma, addr, pte_pfn(*ptep)); |
| 929 | ptep_clear_flush(vma, addr, ptep); |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 930 | set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 931 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 932 | page_remove_rmap(page); |
Hugh Dickins | ae52a2a | 2011-01-13 15:46:28 -0800 | [diff] [blame] | 933 | if (!page_mapped(page)) |
| 934 | try_to_free_swap(page); |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 935 | put_page(page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 936 | |
| 937 | pte_unmap_unlock(ptep, ptl); |
| 938 | err = 0; |
Haggai Eran | 6bdb913 | 2012-10-08 16:33:35 -0700 | [diff] [blame] | 939 | out_mn: |
| 940 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 941 | out: |
| 942 | return err; |
| 943 | } |
| 944 | |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 945 | static int page_trans_compound_anon_split(struct page *page) |
| 946 | { |
| 947 | int ret = 0; |
| 948 | struct page *transhuge_head = page_trans_compound_anon(page); |
| 949 | if (transhuge_head) { |
| 950 | /* Get the reference on the head to split it. */ |
| 951 | if (get_page_unless_zero(transhuge_head)) { |
| 952 | /* |
| 953 | * Recheck we got the reference while the head |
| 954 | * was still anonymous. |
| 955 | */ |
| 956 | if (PageAnon(transhuge_head)) |
| 957 | ret = split_huge_page(transhuge_head); |
| 958 | else |
| 959 | /* |
| 960 | * Retry later if split_huge_page run |
| 961 | * from under us. |
| 962 | */ |
| 963 | ret = 1; |
| 964 | put_page(transhuge_head); |
| 965 | } else |
| 966 | /* Retry later if split_huge_page run from under us. */ |
| 967 | ret = 1; |
| 968 | } |
| 969 | return ret; |
| 970 | } |
| 971 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 972 | /* |
| 973 | * try_to_merge_one_page - take two pages and merge them into one |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 974 | * @vma: the vma that holds the pte pointing to page |
| 975 | * @page: the PageAnon page that we want to replace with kpage |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 976 | * @kpage: the PageKsm page that we want to map instead of page, |
| 977 | * or NULL the first time when we want to use page as kpage. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 978 | * |
| 979 | * This function returns 0 if the pages were merged, -EFAULT otherwise. |
| 980 | */ |
| 981 | static int try_to_merge_one_page(struct vm_area_struct *vma, |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 982 | struct page *page, struct page *kpage) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 983 | { |
| 984 | pte_t orig_pte = __pte(0); |
| 985 | int err = -EFAULT; |
| 986 | |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 987 | if (page == kpage) /* ksm page forked */ |
| 988 | return 0; |
| 989 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 990 | if (!(vma->vm_flags & VM_MERGEABLE)) |
| 991 | goto out; |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 992 | if (PageTransCompound(page) && page_trans_compound_anon_split(page)) |
| 993 | goto out; |
| 994 | BUG_ON(PageTransCompound(page)); |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 995 | if (!PageAnon(page)) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 996 | goto out; |
| 997 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 998 | /* |
| 999 | * We need the page lock to read a stable PageSwapCache in |
| 1000 | * write_protect_page(). We use trylock_page() instead of |
| 1001 | * lock_page() because we don't want to wait here - we |
| 1002 | * prefer to continue scanning and merging different pages, |
| 1003 | * then come back to this page when it is unlocked. |
| 1004 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1005 | if (!trylock_page(page)) |
Hugh Dickins | 31e855e | 2009-12-14 17:59:17 -0800 | [diff] [blame] | 1006 | goto out; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1007 | /* |
| 1008 | * If this anonymous page is mapped only here, its pte may need |
| 1009 | * to be write-protected. If it's mapped elsewhere, all of its |
| 1010 | * ptes are necessarily already write-protected. But in either |
| 1011 | * case, we need to lock and check page_count is not raised. |
| 1012 | */ |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1013 | if (write_protect_page(vma, page, &orig_pte) == 0) { |
| 1014 | if (!kpage) { |
| 1015 | /* |
| 1016 | * While we hold page lock, upgrade page from |
| 1017 | * PageAnon+anon_vma to PageKsm+NULL stable_node: |
| 1018 | * stable_tree_insert() will update stable_node. |
| 1019 | */ |
| 1020 | set_page_stable_node(page, NULL); |
| 1021 | mark_page_accessed(page); |
| 1022 | err = 0; |
| 1023 | } else if (pages_identical(page, kpage)) |
| 1024 | err = replace_page(vma, page, kpage, orig_pte); |
| 1025 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1026 | |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1027 | if ((vma->vm_flags & VM_LOCKED) && kpage && !err) { |
Hugh Dickins | 73848b4 | 2009-12-14 17:59:22 -0800 | [diff] [blame] | 1028 | munlock_vma_page(page); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1029 | if (!PageMlocked(kpage)) { |
| 1030 | unlock_page(page); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1031 | lock_page(kpage); |
| 1032 | mlock_vma_page(kpage); |
| 1033 | page = kpage; /* for final unlock */ |
| 1034 | } |
| 1035 | } |
Hugh Dickins | 73848b4 | 2009-12-14 17:59:22 -0800 | [diff] [blame] | 1036 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1037 | unlock_page(page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1038 | out: |
| 1039 | return err; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1043 | * try_to_merge_with_ksm_page - like try_to_merge_two_pages, |
| 1044 | * but no new kernel page is allocated: kpage must already be a ksm page. |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1045 | * |
| 1046 | * This function returns 0 if the pages were merged, -EFAULT otherwise. |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1047 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1048 | static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item, |
| 1049 | struct page *page, struct page *kpage) |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1050 | { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1051 | struct mm_struct *mm = rmap_item->mm; |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1052 | struct vm_area_struct *vma; |
| 1053 | int err = -EFAULT; |
| 1054 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1055 | down_read(&mm->mmap_sem); |
| 1056 | if (ksm_test_exit(mm)) |
| 1057 | goto out; |
| 1058 | vma = find_vma(mm, rmap_item->address); |
| 1059 | if (!vma || vma->vm_start > rmap_item->address) |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1060 | goto out; |
| 1061 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1062 | err = try_to_merge_one_page(vma, page, kpage); |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1063 | if (err) |
| 1064 | goto out; |
| 1065 | |
| 1066 | /* Must get reference to anon_vma while still holding mmap_sem */ |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1067 | rmap_item->anon_vma = vma->anon_vma; |
| 1068 | get_anon_vma(vma->anon_vma); |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1069 | out: |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1070 | up_read(&mm->mmap_sem); |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1071 | return err; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1075 | * try_to_merge_two_pages - take two identical pages and prepare them |
| 1076 | * to be merged into one page. |
| 1077 | * |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1078 | * This function returns the kpage if we successfully merged two identical |
| 1079 | * pages into one ksm page, NULL otherwise. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1080 | * |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1081 | * Note that this function upgrades page to ksm page: if one of the pages |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1082 | * is already a ksm page, try_to_merge_with_ksm_page should be used. |
| 1083 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1084 | static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item, |
| 1085 | struct page *page, |
| 1086 | struct rmap_item *tree_rmap_item, |
| 1087 | struct page *tree_page) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1088 | { |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1089 | int err; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1090 | |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1091 | err = try_to_merge_with_ksm_page(rmap_item, page, NULL); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1092 | if (!err) { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1093 | err = try_to_merge_with_ksm_page(tree_rmap_item, |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1094 | tree_page, page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1095 | /* |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1096 | * If that fails, we have a ksm page with only one pte |
| 1097 | * pointing to it: so break it. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1098 | */ |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1099 | if (err) |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1100 | break_cow(rmap_item); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1101 | } |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1102 | return err ? NULL : page; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | /* |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1106 | * stable_tree_search - search for page inside the stable tree |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1107 | * |
| 1108 | * This function checks if there is a page inside the stable tree |
| 1109 | * with identical content to the page that we are scanning right now. |
| 1110 | * |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1111 | * This function returns the stable tree node of identical content if found, |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1112 | * NULL otherwise. |
| 1113 | */ |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1114 | static struct page *stable_tree_search(struct page *page) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1115 | { |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1116 | struct rb_node *node; |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1117 | struct stable_node *stable_node; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1118 | int nid; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1119 | |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1120 | stable_node = page_stable_node(page); |
| 1121 | if (stable_node) { /* ksm page forked */ |
| 1122 | get_page(page); |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1123 | return page; |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1126 | nid = get_kpfn_nid(page_to_pfn(page)); |
| 1127 | node = root_stable_tree[nid].rb_node; |
| 1128 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1129 | while (node) { |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1130 | struct page *tree_page; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1131 | int ret; |
| 1132 | |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1133 | cond_resched(); |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1134 | stable_node = rb_entry(node, struct stable_node, node); |
Hugh Dickins | 8aafa6a | 2013-02-22 16:35:06 -0800 | [diff] [blame] | 1135 | tree_page = get_ksm_page(stable_node, false); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1136 | if (!tree_page) |
| 1137 | return NULL; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1138 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1139 | ret = memcmp_pages(page, tree_page); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 1140 | put_page(tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1141 | |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 1142 | if (ret < 0) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1143 | node = node->rb_left; |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 1144 | else if (ret > 0) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1145 | node = node->rb_right; |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 1146 | else { |
| 1147 | /* |
| 1148 | * Lock and unlock the stable_node's page (which |
| 1149 | * might already have been migrated) so that page |
| 1150 | * migration is sure to notice its raised count. |
| 1151 | * It would be more elegant to return stable_node |
| 1152 | * than kpage, but that involves more changes. |
| 1153 | */ |
| 1154 | tree_page = get_ksm_page(stable_node, true); |
| 1155 | if (tree_page) |
| 1156 | unlock_page(tree_page); |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1157 | return tree_page; |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 1158 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 1165 | * stable_tree_insert - insert stable tree node pointing to new ksm page |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1166 | * into the stable tree. |
| 1167 | * |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1168 | * This function returns the stable tree node just allocated on success, |
| 1169 | * NULL otherwise. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1170 | */ |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1171 | static struct stable_node *stable_tree_insert(struct page *kpage) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1172 | { |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1173 | int nid; |
| 1174 | unsigned long kpfn; |
| 1175 | struct rb_node **new; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1176 | struct rb_node *parent = NULL; |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1177 | struct stable_node *stable_node; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1178 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1179 | kpfn = page_to_pfn(kpage); |
| 1180 | nid = get_kpfn_nid(kpfn); |
| 1181 | new = &root_stable_tree[nid].rb_node; |
| 1182 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1183 | while (*new) { |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1184 | struct page *tree_page; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1185 | int ret; |
| 1186 | |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1187 | cond_resched(); |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1188 | stable_node = rb_entry(*new, struct stable_node, node); |
Hugh Dickins | 8aafa6a | 2013-02-22 16:35:06 -0800 | [diff] [blame] | 1189 | tree_page = get_ksm_page(stable_node, false); |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1190 | if (!tree_page) |
| 1191 | return NULL; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1192 | |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1193 | ret = memcmp_pages(kpage, tree_page); |
| 1194 | put_page(tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1195 | |
| 1196 | parent = *new; |
| 1197 | if (ret < 0) |
| 1198 | new = &parent->rb_left; |
| 1199 | else if (ret > 0) |
| 1200 | new = &parent->rb_right; |
| 1201 | else { |
| 1202 | /* |
| 1203 | * It is not a bug that stable_tree_search() didn't |
| 1204 | * find this node: because at that time our page was |
| 1205 | * not yet write-protected, so may have changed since. |
| 1206 | */ |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | } |
| 1210 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1211 | stable_node = alloc_stable_node(); |
| 1212 | if (!stable_node) |
| 1213 | return NULL; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1214 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1215 | INIT_HLIST_HEAD(&stable_node->hlist); |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1216 | stable_node->kpfn = kpfn; |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1217 | set_page_stable_node(kpage, stable_node); |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 1218 | rb_link_node(&stable_node->node, parent, new); |
| 1219 | rb_insert_color(&stable_node->node, &root_stable_tree[nid]); |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1220 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1221 | return stable_node; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /* |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1225 | * unstable_tree_search_insert - search for identical page, |
| 1226 | * else insert rmap_item into the unstable tree. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1227 | * |
| 1228 | * This function searches for a page in the unstable tree identical to the |
| 1229 | * page currently being scanned; and if no identical page is found in the |
| 1230 | * tree, we insert rmap_item as a new object into the unstable tree. |
| 1231 | * |
| 1232 | * This function returns pointer to rmap_item found to be identical |
| 1233 | * to the currently scanned page, NULL otherwise. |
| 1234 | * |
| 1235 | * This function does both searching and inserting, because they share |
| 1236 | * the same walking algorithm in an rbtree. |
| 1237 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1238 | static |
| 1239 | struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item, |
| 1240 | struct page *page, |
| 1241 | struct page **tree_pagep) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1242 | { |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1243 | struct rb_node **new; |
| 1244 | struct rb_root *root; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1245 | struct rb_node *parent = NULL; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1246 | int nid; |
| 1247 | |
| 1248 | nid = get_kpfn_nid(page_to_pfn(page)); |
| 1249 | root = &root_unstable_tree[nid]; |
| 1250 | new = &root->rb_node; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1251 | |
| 1252 | while (*new) { |
| 1253 | struct rmap_item *tree_rmap_item; |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1254 | struct page *tree_page; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1255 | int ret; |
| 1256 | |
Hugh Dickins | d178f27 | 2009-11-09 15:58:23 +0000 | [diff] [blame] | 1257 | cond_resched(); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1258 | tree_rmap_item = rb_entry(*new, struct rmap_item, node); |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1259 | tree_page = get_mergeable_page(tree_rmap_item); |
Dan Carpenter | 22eccdd | 2010-04-23 13:18:10 -0400 | [diff] [blame] | 1260 | if (IS_ERR_OR_NULL(tree_page)) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1261 | return NULL; |
| 1262 | |
| 1263 | /* |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1264 | * Don't substitute a ksm page for a forked page. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1265 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1266 | if (page == tree_page) { |
| 1267 | put_page(tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1268 | return NULL; |
| 1269 | } |
| 1270 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1271 | /* |
| 1272 | * If tree_page has been migrated to another NUMA node, it |
| 1273 | * will be flushed out and put into the right unstable tree |
| 1274 | * next time: only merge with it if merge_across_nodes. |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1275 | */ |
| 1276 | if (!ksm_merge_across_nodes && page_to_nid(tree_page) != nid) { |
| 1277 | put_page(tree_page); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1281 | ret = memcmp_pages(page, tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1282 | |
| 1283 | parent = *new; |
| 1284 | if (ret < 0) { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1285 | put_page(tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1286 | new = &parent->rb_left; |
| 1287 | } else if (ret > 0) { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1288 | put_page(tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1289 | new = &parent->rb_right; |
| 1290 | } else { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1291 | *tree_pagep = tree_page; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1292 | return tree_rmap_item; |
| 1293 | } |
| 1294 | } |
| 1295 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1296 | rmap_item->address |= UNSTABLE_FLAG; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1297 | rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK); |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 1298 | DO_NUMA(rmap_item->nid = nid); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1299 | rb_link_node(&rmap_item->node, parent, new); |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1300 | rb_insert_color(&rmap_item->node, root); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1301 | |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 1302 | ksm_pages_unshared++; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1303 | return NULL; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * stable_tree_append - add another rmap_item to the linked list of |
| 1308 | * rmap_items hanging off a given node of the stable tree, all sharing |
| 1309 | * the same ksm page. |
| 1310 | */ |
| 1311 | static void stable_tree_append(struct rmap_item *rmap_item, |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1312 | struct stable_node *stable_node) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1313 | { |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1314 | /* |
| 1315 | * Usually rmap_item->nid is already set correctly, |
| 1316 | * but it may be wrong after switching merge_across_nodes. |
| 1317 | */ |
Hugh Dickins | e850dcf | 2013-02-22 16:35:03 -0800 | [diff] [blame] | 1318 | DO_NUMA(rmap_item->nid = get_kpfn_nid(stable_node->kpfn)); |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1319 | rmap_item->head = stable_node; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1320 | rmap_item->address |= STABLE_FLAG; |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1321 | hlist_add_head(&rmap_item->hlist, &stable_node->hlist); |
Hugh Dickins | e178dfd | 2009-09-21 17:02:10 -0700 | [diff] [blame] | 1322 | |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1323 | if (rmap_item->hlist.next) |
| 1324 | ksm_pages_sharing++; |
| 1325 | else |
| 1326 | ksm_pages_shared++; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /* |
Hugh Dickins | 81464e30 | 2009-09-21 17:02:15 -0700 | [diff] [blame] | 1330 | * cmp_and_merge_page - first see if page can be merged into the stable tree; |
| 1331 | * if not, compare checksum to previous and if it's the same, see if page can |
| 1332 | * be inserted into the unstable tree, or merged with a page already there and |
| 1333 | * both transferred to the stable tree. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1334 | * |
| 1335 | * @page: the page that we are searching identical page to. |
| 1336 | * @rmap_item: the reverse mapping into the virtual address of this page |
| 1337 | */ |
| 1338 | static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) |
| 1339 | { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1340 | struct rmap_item *tree_rmap_item; |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1341 | struct page *tree_page = NULL; |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1342 | struct stable_node *stable_node; |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1343 | struct page *kpage; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1344 | unsigned int checksum; |
| 1345 | int err; |
| 1346 | |
Hugh Dickins | 93d1771 | 2009-12-14 17:59:16 -0800 | [diff] [blame] | 1347 | remove_rmap_item_from_tree(rmap_item); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1348 | |
| 1349 | /* We first start with searching the page inside the stable tree */ |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1350 | kpage = stable_tree_search(page); |
| 1351 | if (kpage) { |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 1352 | err = try_to_merge_with_ksm_page(rmap_item, page, kpage); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1353 | if (!err) { |
| 1354 | /* |
| 1355 | * The page was successfully merged: |
| 1356 | * add its rmap_item to the stable tree. |
| 1357 | */ |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1358 | lock_page(kpage); |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1359 | stable_tree_append(rmap_item, page_stable_node(kpage)); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1360 | unlock_page(kpage); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1361 | } |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1362 | put_page(kpage); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
Hugh Dickins | 4035c07a | 2009-12-14 17:59:27 -0800 | [diff] [blame] | 1367 | * If the hash value of the page has changed from the last time |
| 1368 | * we calculated it, this page is changing frequently: therefore we |
| 1369 | * don't want to insert it in the unstable tree, and we don't want |
| 1370 | * to waste our time searching for something identical to it there. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1371 | */ |
| 1372 | checksum = calc_checksum(page); |
| 1373 | if (rmap_item->oldchecksum != checksum) { |
| 1374 | rmap_item->oldchecksum = checksum; |
| 1375 | return; |
| 1376 | } |
| 1377 | |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1378 | tree_rmap_item = |
| 1379 | unstable_tree_search_insert(rmap_item, page, &tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1380 | if (tree_rmap_item) { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1381 | kpage = try_to_merge_two_pages(rmap_item, page, |
| 1382 | tree_rmap_item, tree_page); |
| 1383 | put_page(tree_page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1384 | /* |
| 1385 | * As soon as we merge this page, we want to remove the |
| 1386 | * rmap_item of the page we have merged with from the unstable |
| 1387 | * tree, and insert it instead as new node in the stable tree. |
| 1388 | */ |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1389 | if (kpage) { |
Hugh Dickins | 93d1771 | 2009-12-14 17:59:16 -0800 | [diff] [blame] | 1390 | remove_rmap_item_from_tree(tree_rmap_item); |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 1391 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1392 | lock_page(kpage); |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1393 | stable_node = stable_tree_insert(kpage); |
| 1394 | if (stable_node) { |
| 1395 | stable_tree_append(tree_rmap_item, stable_node); |
| 1396 | stable_tree_append(rmap_item, stable_node); |
| 1397 | } |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1398 | unlock_page(kpage); |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1399 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1400 | /* |
| 1401 | * If we fail to insert the page into the stable tree, |
| 1402 | * we will have 2 virtual addresses that are pointing |
| 1403 | * to a ksm page left outside the stable tree, |
| 1404 | * in which case we need to break_cow on both. |
| 1405 | */ |
Hugh Dickins | 7b6ba2c | 2009-12-14 17:59:20 -0800 | [diff] [blame] | 1406 | if (!stable_node) { |
Hugh Dickins | 8dd3557 | 2009-12-14 17:59:18 -0800 | [diff] [blame] | 1407 | break_cow(tree_rmap_item); |
| 1408 | break_cow(rmap_item); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot, |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1415 | struct rmap_item **rmap_list, |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1416 | unsigned long addr) |
| 1417 | { |
| 1418 | struct rmap_item *rmap_item; |
| 1419 | |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1420 | while (*rmap_list) { |
| 1421 | rmap_item = *rmap_list; |
Hugh Dickins | 93d1771 | 2009-12-14 17:59:16 -0800 | [diff] [blame] | 1422 | if ((rmap_item->address & PAGE_MASK) == addr) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1423 | return rmap_item; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1424 | if (rmap_item->address > addr) |
| 1425 | break; |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1426 | *rmap_list = rmap_item->rmap_list; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1427 | remove_rmap_item_from_tree(rmap_item); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1428 | free_rmap_item(rmap_item); |
| 1429 | } |
| 1430 | |
| 1431 | rmap_item = alloc_rmap_item(); |
| 1432 | if (rmap_item) { |
| 1433 | /* It has already been zeroed */ |
| 1434 | rmap_item->mm = mm_slot->mm; |
| 1435 | rmap_item->address = addr; |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1436 | rmap_item->rmap_list = *rmap_list; |
| 1437 | *rmap_list = rmap_item; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1438 | } |
| 1439 | return rmap_item; |
| 1440 | } |
| 1441 | |
| 1442 | static struct rmap_item *scan_get_next_rmap_item(struct page **page) |
| 1443 | { |
| 1444 | struct mm_struct *mm; |
| 1445 | struct mm_slot *slot; |
| 1446 | struct vm_area_struct *vma; |
| 1447 | struct rmap_item *rmap_item; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1448 | int nid; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1449 | |
| 1450 | if (list_empty(&ksm_mm_head.mm_list)) |
| 1451 | return NULL; |
| 1452 | |
| 1453 | slot = ksm_scan.mm_slot; |
| 1454 | if (slot == &ksm_mm_head) { |
Hugh Dickins | 2919bfd | 2011-01-13 15:47:29 -0800 | [diff] [blame] | 1455 | /* |
| 1456 | * A number of pages can hang around indefinitely on per-cpu |
| 1457 | * pagevecs, raised page count preventing write_protect_page |
| 1458 | * from merging them. Though it doesn't really matter much, |
| 1459 | * it is puzzling to see some stuck in pages_volatile until |
| 1460 | * other activity jostles them out, and they also prevented |
| 1461 | * LTP's KSM test from succeeding deterministically; so drain |
| 1462 | * them here (here rather than on entry to ksm_do_scan(), |
| 1463 | * so we don't IPI too often when pages_to_scan is set low). |
| 1464 | */ |
| 1465 | lru_add_drain_all(); |
| 1466 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1467 | for (nid = 0; nid < nr_node_ids; nid++) |
| 1468 | root_unstable_tree[nid] = RB_ROOT; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1469 | |
| 1470 | spin_lock(&ksm_mmlist_lock); |
| 1471 | slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list); |
| 1472 | ksm_scan.mm_slot = slot; |
| 1473 | spin_unlock(&ksm_mmlist_lock); |
Hugh Dickins | 2b47261 | 2011-06-15 15:08:58 -0700 | [diff] [blame] | 1474 | /* |
| 1475 | * Although we tested list_empty() above, a racing __ksm_exit |
| 1476 | * of the last mm on the list may have removed it since then. |
| 1477 | */ |
| 1478 | if (slot == &ksm_mm_head) |
| 1479 | return NULL; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1480 | next_mm: |
| 1481 | ksm_scan.address = 0; |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1482 | ksm_scan.rmap_list = &slot->rmap_list; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | mm = slot->mm; |
| 1486 | down_read(&mm->mmap_sem); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1487 | if (ksm_test_exit(mm)) |
| 1488 | vma = NULL; |
| 1489 | else |
| 1490 | vma = find_vma(mm, ksm_scan.address); |
| 1491 | |
| 1492 | for (; vma; vma = vma->vm_next) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1493 | if (!(vma->vm_flags & VM_MERGEABLE)) |
| 1494 | continue; |
| 1495 | if (ksm_scan.address < vma->vm_start) |
| 1496 | ksm_scan.address = vma->vm_start; |
| 1497 | if (!vma->anon_vma) |
| 1498 | ksm_scan.address = vma->vm_end; |
| 1499 | |
| 1500 | while (ksm_scan.address < vma->vm_end) { |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1501 | if (ksm_test_exit(mm)) |
| 1502 | break; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1503 | *page = follow_page(vma, ksm_scan.address, FOLL_GET); |
Andrea Arcangeli | 21ae5b0 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 1504 | if (IS_ERR_OR_NULL(*page)) { |
| 1505 | ksm_scan.address += PAGE_SIZE; |
| 1506 | cond_resched(); |
| 1507 | continue; |
| 1508 | } |
Andrea Arcangeli | 29ad768 | 2011-01-13 15:47:19 -0800 | [diff] [blame] | 1509 | if (PageAnon(*page) || |
| 1510 | page_trans_compound_anon(*page)) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1511 | flush_anon_page(vma, *page, ksm_scan.address); |
| 1512 | flush_dcache_page(*page); |
| 1513 | rmap_item = get_next_rmap_item(slot, |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1514 | ksm_scan.rmap_list, ksm_scan.address); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1515 | if (rmap_item) { |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1516 | ksm_scan.rmap_list = |
| 1517 | &rmap_item->rmap_list; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1518 | ksm_scan.address += PAGE_SIZE; |
| 1519 | } else |
| 1520 | put_page(*page); |
| 1521 | up_read(&mm->mmap_sem); |
| 1522 | return rmap_item; |
| 1523 | } |
Andrea Arcangeli | 21ae5b0 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 1524 | put_page(*page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1525 | ksm_scan.address += PAGE_SIZE; |
| 1526 | cond_resched(); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1530 | if (ksm_test_exit(mm)) { |
| 1531 | ksm_scan.address = 0; |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1532 | ksm_scan.rmap_list = &slot->rmap_list; |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1533 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * Nuke all the rmap_items that are above this current rmap: |
| 1536 | * because there were no VM_MERGEABLE vmas with such addresses. |
| 1537 | */ |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1538 | remove_trailing_rmap_items(slot, ksm_scan.rmap_list); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1539 | |
| 1540 | spin_lock(&ksm_mmlist_lock); |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1541 | ksm_scan.mm_slot = list_entry(slot->mm_list.next, |
| 1542 | struct mm_slot, mm_list); |
| 1543 | if (ksm_scan.address == 0) { |
| 1544 | /* |
| 1545 | * We've completed a full scan of all vmas, holding mmap_sem |
| 1546 | * throughout, and found no VM_MERGEABLE: so do the same as |
| 1547 | * __ksm_exit does to remove this mm from all our lists now. |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1548 | * This applies either when cleaning up after __ksm_exit |
| 1549 | * (but beware: we can reach here even before __ksm_exit), |
| 1550 | * or when all VM_MERGEABLE areas have been unmapped (and |
| 1551 | * mmap_sem then protects against race with MADV_MERGEABLE). |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1552 | */ |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 1553 | hash_del(&slot->link); |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1554 | list_del(&slot->mm_list); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1555 | spin_unlock(&ksm_mmlist_lock); |
| 1556 | |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1557 | free_mm_slot(slot); |
| 1558 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1559 | up_read(&mm->mmap_sem); |
| 1560 | mmdrop(mm); |
| 1561 | } else { |
| 1562 | spin_unlock(&ksm_mmlist_lock); |
| 1563 | up_read(&mm->mmap_sem); |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1564 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* Repeat until we've completed scanning the whole list */ |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1567 | slot = ksm_scan.mm_slot; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1568 | if (slot != &ksm_mm_head) |
| 1569 | goto next_mm; |
| 1570 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1571 | ksm_scan.seqnr++; |
| 1572 | return NULL; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * ksm_do_scan - the ksm scanner main worker function. |
| 1577 | * @scan_npages - number of pages we want to scan before we return. |
| 1578 | */ |
| 1579 | static void ksm_do_scan(unsigned int scan_npages) |
| 1580 | { |
| 1581 | struct rmap_item *rmap_item; |
Dan Carpenter | 22eccdd | 2010-04-23 13:18:10 -0400 | [diff] [blame] | 1582 | struct page *uninitialized_var(page); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1583 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1584 | while (scan_npages-- && likely(!freezing(current))) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1585 | cond_resched(); |
| 1586 | rmap_item = scan_get_next_rmap_item(&page); |
| 1587 | if (!rmap_item) |
| 1588 | return; |
| 1589 | if (!PageKsm(page) || !in_stable_tree(rmap_item)) |
| 1590 | cmp_and_merge_page(page, rmap_item); |
| 1591 | put_page(page); |
| 1592 | } |
| 1593 | } |
| 1594 | |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1595 | static int ksmd_should_run(void) |
| 1596 | { |
| 1597 | return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list); |
| 1598 | } |
| 1599 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1600 | static int ksm_scan_thread(void *nothing) |
| 1601 | { |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1602 | set_freezable(); |
Izik Eidus | 339aa62 | 2009-09-21 17:02:07 -0700 | [diff] [blame] | 1603 | set_user_nice(current, 5); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1604 | |
| 1605 | while (!kthread_should_stop()) { |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1606 | mutex_lock(&ksm_thread_mutex); |
| 1607 | if (ksmd_should_run()) |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1608 | ksm_do_scan(ksm_thread_pages_to_scan); |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1609 | mutex_unlock(&ksm_thread_mutex); |
| 1610 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1611 | try_to_freeze(); |
| 1612 | |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1613 | if (ksmd_should_run()) { |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1614 | schedule_timeout_interruptible( |
| 1615 | msecs_to_jiffies(ksm_thread_sleep_millisecs)); |
| 1616 | } else { |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1617 | wait_event_freezable(ksm_thread_wait, |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1618 | ksmd_should_run() || kthread_should_stop()); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1624 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
| 1625 | unsigned long end, int advice, unsigned long *vm_flags) |
| 1626 | { |
| 1627 | struct mm_struct *mm = vma->vm_mm; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 1628 | int err; |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1629 | |
| 1630 | switch (advice) { |
| 1631 | case MADV_MERGEABLE: |
| 1632 | /* |
| 1633 | * Be somewhat over-protective for now! |
| 1634 | */ |
| 1635 | if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE | |
| 1636 | VM_PFNMAP | VM_IO | VM_DONTEXPAND | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 1637 | VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP)) |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1638 | return 0; /* just ignore the advice */ |
| 1639 | |
Konstantin Khlebnikov | cc2383e | 2012-10-08 16:28:37 -0700 | [diff] [blame] | 1640 | #ifdef VM_SAO |
| 1641 | if (*vm_flags & VM_SAO) |
| 1642 | return 0; |
| 1643 | #endif |
| 1644 | |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 1645 | if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) { |
| 1646 | err = __ksm_enter(mm); |
| 1647 | if (err) |
| 1648 | return err; |
| 1649 | } |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1650 | |
| 1651 | *vm_flags |= VM_MERGEABLE; |
| 1652 | break; |
| 1653 | |
| 1654 | case MADV_UNMERGEABLE: |
| 1655 | if (!(*vm_flags & VM_MERGEABLE)) |
| 1656 | return 0; /* just ignore the advice */ |
| 1657 | |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 1658 | if (vma->anon_vma) { |
| 1659 | err = unmerge_ksm_pages(vma, start, end); |
| 1660 | if (err) |
| 1661 | return err; |
| 1662 | } |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1663 | |
| 1664 | *vm_flags &= ~VM_MERGEABLE; |
| 1665 | break; |
| 1666 | } |
| 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | int __ksm_enter(struct mm_struct *mm) |
| 1672 | { |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1673 | struct mm_slot *mm_slot; |
| 1674 | int needs_wakeup; |
| 1675 | |
| 1676 | mm_slot = alloc_mm_slot(); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1677 | if (!mm_slot) |
| 1678 | return -ENOMEM; |
| 1679 | |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1680 | /* Check ksm_run too? Would need tighter locking */ |
| 1681 | needs_wakeup = list_empty(&ksm_mm_head.mm_list); |
| 1682 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1683 | spin_lock(&ksm_mmlist_lock); |
| 1684 | insert_to_mm_slots_hash(mm, mm_slot); |
| 1685 | /* |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 1686 | * When KSM_RUN_MERGE (or KSM_RUN_STOP), |
| 1687 | * insert just behind the scanning cursor, to let the area settle |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1688 | * down a little; when fork is followed by immediate exec, we don't |
| 1689 | * want ksmd to waste time setting up and tearing down an rmap_list. |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 1690 | * |
| 1691 | * But when KSM_RUN_UNMERGE, it's important to insert ahead of its |
| 1692 | * scanning cursor, otherwise KSM pages in newly forked mms will be |
| 1693 | * missed: then we might as well insert at the end of the list. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1694 | */ |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 1695 | if (ksm_run & KSM_RUN_UNMERGE) |
| 1696 | list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list); |
| 1697 | else |
| 1698 | list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list); |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1699 | spin_unlock(&ksm_mmlist_lock); |
| 1700 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1701 | set_bit(MMF_VM_MERGEABLE, &mm->flags); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1702 | atomic_inc(&mm->mm_count); |
Hugh Dickins | 6e158384 | 2009-09-21 17:02:14 -0700 | [diff] [blame] | 1703 | |
| 1704 | if (needs_wakeup) |
| 1705 | wake_up_interruptible(&ksm_thread_wait); |
| 1706 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 1710 | void __ksm_exit(struct mm_struct *mm) |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1711 | { |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1712 | struct mm_slot *mm_slot; |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1713 | int easy_to_free = 0; |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1714 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1715 | /* |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1716 | * This process is exiting: if it's straightforward (as is the |
| 1717 | * case when ksmd was never running), free mm_slot immediately. |
| 1718 | * But if it's at the cursor or has rmap_items linked to it, use |
| 1719 | * mmap_sem to synchronize with any break_cows before pagetables |
| 1720 | * are freed, and leave the mm_slot on the list for ksmd to free. |
| 1721 | * Beware: ksm may already have noticed it exiting and freed the slot. |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1722 | */ |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1723 | |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1724 | spin_lock(&ksm_mmlist_lock); |
| 1725 | mm_slot = get_mm_slot(mm); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1726 | if (mm_slot && ksm_scan.mm_slot != mm_slot) { |
Hugh Dickins | 6514d51 | 2009-12-14 17:59:19 -0800 | [diff] [blame] | 1727 | if (!mm_slot->rmap_list) { |
Sasha Levin | 4ca3a69 | 2013-02-22 16:32:28 -0800 | [diff] [blame] | 1728 | hash_del(&mm_slot->link); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1729 | list_del(&mm_slot->mm_list); |
| 1730 | easy_to_free = 1; |
| 1731 | } else { |
| 1732 | list_move(&mm_slot->mm_list, |
| 1733 | &ksm_scan.mm_slot->mm_list); |
| 1734 | } |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1735 | } |
Hugh Dickins | cd551f9 | 2009-09-21 17:02:17 -0700 | [diff] [blame] | 1736 | spin_unlock(&ksm_mmlist_lock); |
| 1737 | |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1738 | if (easy_to_free) { |
| 1739 | free_mm_slot(mm_slot); |
| 1740 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); |
| 1741 | mmdrop(mm); |
| 1742 | } else if (mm_slot) { |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1743 | down_write(&mm->mmap_sem); |
| 1744 | up_write(&mm->mmap_sem); |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 1745 | } |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 1746 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 1747 | |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 1748 | struct page *ksm_might_need_to_copy(struct page *page, |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1749 | struct vm_area_struct *vma, unsigned long address) |
| 1750 | { |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 1751 | struct anon_vma *anon_vma = page_anon_vma(page); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1752 | struct page *new_page; |
| 1753 | |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 1754 | if (PageKsm(page)) { |
| 1755 | if (page_stable_node(page) && |
| 1756 | !(ksm_run & KSM_RUN_UNMERGE)) |
| 1757 | return page; /* no need to copy it */ |
| 1758 | } else if (!anon_vma) { |
| 1759 | return page; /* no need to copy it */ |
| 1760 | } else if (anon_vma->root == vma->anon_vma->root && |
| 1761 | page->index == linear_page_index(vma, address)) { |
| 1762 | return page; /* still no need to copy it */ |
| 1763 | } |
| 1764 | if (!PageUptodate(page)) |
| 1765 | return page; /* let do_swap_page report the error */ |
| 1766 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1767 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); |
| 1768 | if (new_page) { |
| 1769 | copy_user_highpage(new_page, page, address, vma); |
| 1770 | |
| 1771 | SetPageDirty(new_page); |
| 1772 | __SetPageUptodate(new_page); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1773 | __set_page_locked(new_page); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1776 | return new_page; |
| 1777 | } |
| 1778 | |
| 1779 | int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg, |
| 1780 | unsigned long *vm_flags) |
| 1781 | { |
| 1782 | struct stable_node *stable_node; |
| 1783 | struct rmap_item *rmap_item; |
| 1784 | struct hlist_node *hlist; |
| 1785 | unsigned int mapcount = page_mapcount(page); |
| 1786 | int referenced = 0; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1787 | int search_new_forks = 0; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1788 | |
| 1789 | VM_BUG_ON(!PageKsm(page)); |
| 1790 | VM_BUG_ON(!PageLocked(page)); |
| 1791 | |
| 1792 | stable_node = page_stable_node(page); |
| 1793 | if (!stable_node) |
| 1794 | return 0; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1795 | again: |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1796 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1797 | struct anon_vma *anon_vma = rmap_item->anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1798 | struct anon_vma_chain *vmac; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1799 | struct vm_area_struct *vma; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1800 | |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1801 | anon_vma_lock_read(anon_vma); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1802 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
| 1803 | 0, ULONG_MAX) { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1804 | vma = vmac->vma; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1805 | if (rmap_item->address < vma->vm_start || |
| 1806 | rmap_item->address >= vma->vm_end) |
| 1807 | continue; |
| 1808 | /* |
| 1809 | * Initially we examine only the vma which covers this |
| 1810 | * rmap_item; but later, if there is still work to do, |
| 1811 | * we examine covering vmas in other mms: in case they |
| 1812 | * were forked from the original since ksmd passed. |
| 1813 | */ |
| 1814 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) |
| 1815 | continue; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1816 | |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1817 | if (memcg && !mm_match_cgroup(vma->vm_mm, memcg)) |
| 1818 | continue; |
| 1819 | |
| 1820 | referenced += page_referenced_one(page, vma, |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1821 | rmap_item->address, &mapcount, vm_flags); |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1822 | if (!search_new_forks || !mapcount) |
| 1823 | break; |
| 1824 | } |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1825 | anon_vma_unlock_read(anon_vma); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1826 | if (!mapcount) |
| 1827 | goto out; |
| 1828 | } |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1829 | if (!search_new_forks++) |
| 1830 | goto again; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1831 | out: |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1832 | return referenced; |
| 1833 | } |
| 1834 | |
| 1835 | int try_to_unmap_ksm(struct page *page, enum ttu_flags flags) |
| 1836 | { |
| 1837 | struct stable_node *stable_node; |
| 1838 | struct hlist_node *hlist; |
| 1839 | struct rmap_item *rmap_item; |
| 1840 | int ret = SWAP_AGAIN; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1841 | int search_new_forks = 0; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1842 | |
| 1843 | VM_BUG_ON(!PageKsm(page)); |
| 1844 | VM_BUG_ON(!PageLocked(page)); |
| 1845 | |
| 1846 | stable_node = page_stable_node(page); |
| 1847 | if (!stable_node) |
| 1848 | return SWAP_FAIL; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1849 | again: |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1850 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1851 | struct anon_vma *anon_vma = rmap_item->anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1852 | struct anon_vma_chain *vmac; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1853 | struct vm_area_struct *vma; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1854 | |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1855 | anon_vma_lock_read(anon_vma); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1856 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
| 1857 | 0, ULONG_MAX) { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1858 | vma = vmac->vma; |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1859 | if (rmap_item->address < vma->vm_start || |
| 1860 | rmap_item->address >= vma->vm_end) |
| 1861 | continue; |
| 1862 | /* |
| 1863 | * Initially we examine only the vma which covers this |
| 1864 | * rmap_item; but later, if there is still work to do, |
| 1865 | * we examine covering vmas in other mms: in case they |
| 1866 | * were forked from the original since ksmd passed. |
| 1867 | */ |
| 1868 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) |
| 1869 | continue; |
| 1870 | |
| 1871 | ret = try_to_unmap_one(page, vma, |
| 1872 | rmap_item->address, flags); |
| 1873 | if (ret != SWAP_AGAIN || !page_mapped(page)) { |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1874 | anon_vma_unlock_read(anon_vma); |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1875 | goto out; |
| 1876 | } |
| 1877 | } |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1878 | anon_vma_unlock_read(anon_vma); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1879 | } |
Hugh Dickins | db114b8 | 2009-12-14 17:59:25 -0800 | [diff] [blame] | 1880 | if (!search_new_forks++) |
| 1881 | goto again; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1882 | out: |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1883 | return ret; |
| 1884 | } |
| 1885 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1886 | #ifdef CONFIG_MIGRATION |
| 1887 | int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *, |
| 1888 | struct vm_area_struct *, unsigned long, void *), void *arg) |
| 1889 | { |
| 1890 | struct stable_node *stable_node; |
| 1891 | struct hlist_node *hlist; |
| 1892 | struct rmap_item *rmap_item; |
| 1893 | int ret = SWAP_AGAIN; |
| 1894 | int search_new_forks = 0; |
| 1895 | |
| 1896 | VM_BUG_ON(!PageKsm(page)); |
| 1897 | VM_BUG_ON(!PageLocked(page)); |
| 1898 | |
| 1899 | stable_node = page_stable_node(page); |
| 1900 | if (!stable_node) |
| 1901 | return ret; |
| 1902 | again: |
| 1903 | hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { |
| 1904 | struct anon_vma *anon_vma = rmap_item->anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1905 | struct anon_vma_chain *vmac; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1906 | struct vm_area_struct *vma; |
| 1907 | |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1908 | anon_vma_lock_read(anon_vma); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1909 | anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root, |
| 1910 | 0, ULONG_MAX) { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1911 | vma = vmac->vma; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1912 | if (rmap_item->address < vma->vm_start || |
| 1913 | rmap_item->address >= vma->vm_end) |
| 1914 | continue; |
| 1915 | /* |
| 1916 | * Initially we examine only the vma which covers this |
| 1917 | * rmap_item; but later, if there is still work to do, |
| 1918 | * we examine covering vmas in other mms: in case they |
| 1919 | * were forked from the original since ksmd passed. |
| 1920 | */ |
| 1921 | if ((rmap_item->mm == vma->vm_mm) == search_new_forks) |
| 1922 | continue; |
| 1923 | |
| 1924 | ret = rmap_one(page, vma, rmap_item->address, arg); |
| 1925 | if (ret != SWAP_AGAIN) { |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1926 | anon_vma_unlock_read(anon_vma); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1927 | goto out; |
| 1928 | } |
| 1929 | } |
Hugh Dickins | b6b19f2 | 2012-12-19 17:44:29 -0800 | [diff] [blame] | 1930 | anon_vma_unlock_read(anon_vma); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1931 | } |
| 1932 | if (!search_new_forks++) |
| 1933 | goto again; |
| 1934 | out: |
| 1935 | return ret; |
| 1936 | } |
| 1937 | |
| 1938 | void ksm_migrate_page(struct page *newpage, struct page *oldpage) |
| 1939 | { |
| 1940 | struct stable_node *stable_node; |
| 1941 | |
| 1942 | VM_BUG_ON(!PageLocked(oldpage)); |
| 1943 | VM_BUG_ON(!PageLocked(newpage)); |
| 1944 | VM_BUG_ON(newpage->mapping != oldpage->mapping); |
| 1945 | |
| 1946 | stable_node = page_stable_node(newpage); |
| 1947 | if (stable_node) { |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1948 | VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage)); |
| 1949 | stable_node->kpfn = page_to_pfn(newpage); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame^] | 1950 | /* |
| 1951 | * newpage->mapping was set in advance; now we need smp_wmb() |
| 1952 | * to make sure that the new stable_node->kpfn is visible |
| 1953 | * to get_ksm_page() before it can see that oldpage->mapping |
| 1954 | * has gone stale (or that PageSwapCache has been cleared). |
| 1955 | */ |
| 1956 | smp_wmb(); |
| 1957 | set_page_stable_node(oldpage, NULL); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | #endif /* CONFIG_MIGRATION */ |
| 1961 | |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1962 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 1963 | static void ksm_check_stable_tree(unsigned long start_pfn, |
| 1964 | unsigned long end_pfn) |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1965 | { |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 1966 | struct stable_node *stable_node; |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1967 | struct rb_node *node; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1968 | int nid; |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1969 | |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 1970 | for (nid = 0; nid < nr_node_ids; nid++) { |
| 1971 | node = rb_first(&root_stable_tree[nid]); |
| 1972 | while (node) { |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1973 | stable_node = rb_entry(node, struct stable_node, node); |
| 1974 | if (stable_node->kpfn >= start_pfn && |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 1975 | stable_node->kpfn < end_pfn) { |
| 1976 | /* |
| 1977 | * Don't get_ksm_page, page has already gone: |
| 1978 | * which is why we keep kpfn instead of page* |
| 1979 | */ |
| 1980 | remove_node_from_stable_tree(stable_node); |
| 1981 | node = rb_first(&root_stable_tree[nid]); |
| 1982 | } else |
| 1983 | node = rb_next(node); |
| 1984 | cond_resched(); |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 1985 | } |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 1986 | } |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | static int ksm_memory_callback(struct notifier_block *self, |
| 1990 | unsigned long action, void *arg) |
| 1991 | { |
| 1992 | struct memory_notify *mn = arg; |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1993 | |
| 1994 | switch (action) { |
| 1995 | case MEM_GOING_OFFLINE: |
| 1996 | /* |
| 1997 | * Keep it very simple for now: just lock out ksmd and |
| 1998 | * MADV_UNMERGEABLE while any memory is going offline. |
KOSAKI Motohiro | a0b0f58 | 2010-12-02 14:31:20 -0800 | [diff] [blame] | 1999 | * mutex_lock_nested() is necessary because lockdep was alarmed |
| 2000 | * that here we take ksm_thread_mutex inside notifier chain |
| 2001 | * mutex, and later take notifier chain mutex inside |
| 2002 | * ksm_thread_mutex to unlock it. But that's safe because both |
| 2003 | * are inside mem_hotplug_mutex. |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 2004 | */ |
KOSAKI Motohiro | a0b0f58 | 2010-12-02 14:31:20 -0800 | [diff] [blame] | 2005 | mutex_lock_nested(&ksm_thread_mutex, SINGLE_DEPTH_NESTING); |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 2006 | break; |
| 2007 | |
| 2008 | case MEM_OFFLINE: |
| 2009 | /* |
| 2010 | * Most of the work is done by page migration; but there might |
| 2011 | * be a few stable_nodes left over, still pointing to struct |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 2012 | * pages which have been offlined: prune those from the tree, |
| 2013 | * otherwise get_ksm_page() might later try to access a |
| 2014 | * non-existent struct page. |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 2015 | */ |
Hugh Dickins | ee0ea59 | 2013-02-22 16:35:05 -0800 | [diff] [blame] | 2016 | ksm_check_stable_tree(mn->start_pfn, |
| 2017 | mn->start_pfn + mn->nr_pages); |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 2018 | /* fallthrough */ |
| 2019 | |
| 2020 | case MEM_CANCEL_OFFLINE: |
| 2021 | mutex_unlock(&ksm_thread_mutex); |
| 2022 | break; |
| 2023 | } |
| 2024 | return NOTIFY_OK; |
| 2025 | } |
| 2026 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
| 2027 | |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 2028 | #ifdef CONFIG_SYSFS |
| 2029 | /* |
| 2030 | * This all compiles without CONFIG_SYSFS, but is a waste of space. |
| 2031 | */ |
| 2032 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2033 | #define KSM_ATTR_RO(_name) \ |
| 2034 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) |
| 2035 | #define KSM_ATTR(_name) \ |
| 2036 | static struct kobj_attribute _name##_attr = \ |
| 2037 | __ATTR(_name, 0644, _name##_show, _name##_store) |
| 2038 | |
| 2039 | static ssize_t sleep_millisecs_show(struct kobject *kobj, |
| 2040 | struct kobj_attribute *attr, char *buf) |
| 2041 | { |
| 2042 | return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs); |
| 2043 | } |
| 2044 | |
| 2045 | static ssize_t sleep_millisecs_store(struct kobject *kobj, |
| 2046 | struct kobj_attribute *attr, |
| 2047 | const char *buf, size_t count) |
| 2048 | { |
| 2049 | unsigned long msecs; |
| 2050 | int err; |
| 2051 | |
| 2052 | err = strict_strtoul(buf, 10, &msecs); |
| 2053 | if (err || msecs > UINT_MAX) |
| 2054 | return -EINVAL; |
| 2055 | |
| 2056 | ksm_thread_sleep_millisecs = msecs; |
| 2057 | |
| 2058 | return count; |
| 2059 | } |
| 2060 | KSM_ATTR(sleep_millisecs); |
| 2061 | |
| 2062 | static ssize_t pages_to_scan_show(struct kobject *kobj, |
| 2063 | struct kobj_attribute *attr, char *buf) |
| 2064 | { |
| 2065 | return sprintf(buf, "%u\n", ksm_thread_pages_to_scan); |
| 2066 | } |
| 2067 | |
| 2068 | static ssize_t pages_to_scan_store(struct kobject *kobj, |
| 2069 | struct kobj_attribute *attr, |
| 2070 | const char *buf, size_t count) |
| 2071 | { |
| 2072 | int err; |
| 2073 | unsigned long nr_pages; |
| 2074 | |
| 2075 | err = strict_strtoul(buf, 10, &nr_pages); |
| 2076 | if (err || nr_pages > UINT_MAX) |
| 2077 | return -EINVAL; |
| 2078 | |
| 2079 | ksm_thread_pages_to_scan = nr_pages; |
| 2080 | |
| 2081 | return count; |
| 2082 | } |
| 2083 | KSM_ATTR(pages_to_scan); |
| 2084 | |
| 2085 | static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 2086 | char *buf) |
| 2087 | { |
| 2088 | return sprintf(buf, "%u\n", ksm_run); |
| 2089 | } |
| 2090 | |
| 2091 | static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 2092 | const char *buf, size_t count) |
| 2093 | { |
| 2094 | int err; |
| 2095 | unsigned long flags; |
| 2096 | |
| 2097 | err = strict_strtoul(buf, 10, &flags); |
| 2098 | if (err || flags > UINT_MAX) |
| 2099 | return -EINVAL; |
| 2100 | if (flags > KSM_RUN_UNMERGE) |
| 2101 | return -EINVAL; |
| 2102 | |
| 2103 | /* |
| 2104 | * KSM_RUN_MERGE sets ksmd running, and 0 stops it running. |
| 2105 | * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items, |
Hugh Dickins | d0f209f | 2009-12-14 17:59:34 -0800 | [diff] [blame] | 2106 | * breaking COW to free the pages_shared (but leaves mm_slots |
| 2107 | * on the list for when ksmd may be set running again). |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2108 | */ |
| 2109 | |
| 2110 | mutex_lock(&ksm_thread_mutex); |
| 2111 | if (ksm_run != flags) { |
| 2112 | ksm_run = flags; |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 2113 | if (flags & KSM_RUN_UNMERGE) { |
David Rientjes | e1e12d2 | 2012-12-11 16:02:56 -0800 | [diff] [blame] | 2114 | set_current_oom_origin(); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 2115 | err = unmerge_and_remove_all_rmap_items(); |
David Rientjes | e1e12d2 | 2012-12-11 16:02:56 -0800 | [diff] [blame] | 2116 | clear_current_oom_origin(); |
Hugh Dickins | d952b79 | 2009-09-21 17:02:16 -0700 | [diff] [blame] | 2117 | if (err) { |
| 2118 | ksm_run = KSM_RUN_STOP; |
| 2119 | count = err; |
| 2120 | } |
| 2121 | } |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2122 | } |
| 2123 | mutex_unlock(&ksm_thread_mutex); |
| 2124 | |
| 2125 | if (flags & KSM_RUN_MERGE) |
| 2126 | wake_up_interruptible(&ksm_thread_wait); |
| 2127 | |
| 2128 | return count; |
| 2129 | } |
| 2130 | KSM_ATTR(run); |
| 2131 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 2132 | #ifdef CONFIG_NUMA |
| 2133 | static ssize_t merge_across_nodes_show(struct kobject *kobj, |
| 2134 | struct kobj_attribute *attr, char *buf) |
| 2135 | { |
| 2136 | return sprintf(buf, "%u\n", ksm_merge_across_nodes); |
| 2137 | } |
| 2138 | |
| 2139 | static ssize_t merge_across_nodes_store(struct kobject *kobj, |
| 2140 | struct kobj_attribute *attr, |
| 2141 | const char *buf, size_t count) |
| 2142 | { |
| 2143 | int err; |
| 2144 | unsigned long knob; |
| 2145 | |
| 2146 | err = kstrtoul(buf, 10, &knob); |
| 2147 | if (err) |
| 2148 | return err; |
| 2149 | if (knob > 1) |
| 2150 | return -EINVAL; |
| 2151 | |
| 2152 | mutex_lock(&ksm_thread_mutex); |
| 2153 | if (ksm_merge_across_nodes != knob) { |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 2154 | if (ksm_pages_shared || remove_all_stable_nodes()) |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 2155 | err = -EBUSY; |
| 2156 | else |
| 2157 | ksm_merge_across_nodes = knob; |
| 2158 | } |
| 2159 | mutex_unlock(&ksm_thread_mutex); |
| 2160 | |
| 2161 | return err ? err : count; |
| 2162 | } |
| 2163 | KSM_ATTR(merge_across_nodes); |
| 2164 | #endif |
| 2165 | |
Hugh Dickins | b402826 | 2009-09-21 17:02:09 -0700 | [diff] [blame] | 2166 | static ssize_t pages_shared_show(struct kobject *kobj, |
| 2167 | struct kobj_attribute *attr, char *buf) |
| 2168 | { |
| 2169 | return sprintf(buf, "%lu\n", ksm_pages_shared); |
| 2170 | } |
| 2171 | KSM_ATTR_RO(pages_shared); |
| 2172 | |
| 2173 | static ssize_t pages_sharing_show(struct kobject *kobj, |
| 2174 | struct kobj_attribute *attr, char *buf) |
| 2175 | { |
Hugh Dickins | e178dfd | 2009-09-21 17:02:10 -0700 | [diff] [blame] | 2176 | return sprintf(buf, "%lu\n", ksm_pages_sharing); |
Hugh Dickins | b402826 | 2009-09-21 17:02:09 -0700 | [diff] [blame] | 2177 | } |
| 2178 | KSM_ATTR_RO(pages_sharing); |
| 2179 | |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 2180 | static ssize_t pages_unshared_show(struct kobject *kobj, |
| 2181 | struct kobj_attribute *attr, char *buf) |
| 2182 | { |
| 2183 | return sprintf(buf, "%lu\n", ksm_pages_unshared); |
| 2184 | } |
| 2185 | KSM_ATTR_RO(pages_unshared); |
| 2186 | |
| 2187 | static ssize_t pages_volatile_show(struct kobject *kobj, |
| 2188 | struct kobj_attribute *attr, char *buf) |
| 2189 | { |
| 2190 | long ksm_pages_volatile; |
| 2191 | |
| 2192 | ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared |
| 2193 | - ksm_pages_sharing - ksm_pages_unshared; |
| 2194 | /* |
| 2195 | * It was not worth any locking to calculate that statistic, |
| 2196 | * but it might therefore sometimes be negative: conceal that. |
| 2197 | */ |
| 2198 | if (ksm_pages_volatile < 0) |
| 2199 | ksm_pages_volatile = 0; |
| 2200 | return sprintf(buf, "%ld\n", ksm_pages_volatile); |
| 2201 | } |
| 2202 | KSM_ATTR_RO(pages_volatile); |
| 2203 | |
| 2204 | static ssize_t full_scans_show(struct kobject *kobj, |
| 2205 | struct kobj_attribute *attr, char *buf) |
| 2206 | { |
| 2207 | return sprintf(buf, "%lu\n", ksm_scan.seqnr); |
| 2208 | } |
| 2209 | KSM_ATTR_RO(full_scans); |
| 2210 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2211 | static struct attribute *ksm_attrs[] = { |
| 2212 | &sleep_millisecs_attr.attr, |
| 2213 | &pages_to_scan_attr.attr, |
| 2214 | &run_attr.attr, |
Hugh Dickins | b402826 | 2009-09-21 17:02:09 -0700 | [diff] [blame] | 2215 | &pages_shared_attr.attr, |
| 2216 | &pages_sharing_attr.attr, |
Hugh Dickins | 473b0ce | 2009-09-21 17:02:11 -0700 | [diff] [blame] | 2217 | &pages_unshared_attr.attr, |
| 2218 | &pages_volatile_attr.attr, |
| 2219 | &full_scans_attr.attr, |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 2220 | #ifdef CONFIG_NUMA |
| 2221 | &merge_across_nodes_attr.attr, |
| 2222 | #endif |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2223 | NULL, |
| 2224 | }; |
| 2225 | |
| 2226 | static struct attribute_group ksm_attr_group = { |
| 2227 | .attrs = ksm_attrs, |
| 2228 | .name = "ksm", |
| 2229 | }; |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 2230 | #endif /* CONFIG_SYSFS */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2231 | |
| 2232 | static int __init ksm_init(void) |
| 2233 | { |
| 2234 | struct task_struct *ksm_thread; |
| 2235 | int err; |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 2236 | int nid; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2237 | |
| 2238 | err = ksm_slab_init(); |
| 2239 | if (err) |
| 2240 | goto out; |
| 2241 | |
Petr Holasek | 90bd6fd | 2013-02-22 16:35:00 -0800 | [diff] [blame] | 2242 | for (nid = 0; nid < nr_node_ids; nid++) |
| 2243 | root_stable_tree[nid] = RB_ROOT; |
| 2244 | |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2245 | ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd"); |
| 2246 | if (IS_ERR(ksm_thread)) { |
| 2247 | printk(KERN_ERR "ksm: creating kthread failed\n"); |
| 2248 | err = PTR_ERR(ksm_thread); |
Lai Jiangshan | d9f8984 | 2010-08-09 17:20:02 -0700 | [diff] [blame] | 2249 | goto out_free; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 2252 | #ifdef CONFIG_SYSFS |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2253 | err = sysfs_create_group(mm_kobj, &ksm_attr_group); |
| 2254 | if (err) { |
| 2255 | printk(KERN_ERR "ksm: register sysfs failed\n"); |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 2256 | kthread_stop(ksm_thread); |
Lai Jiangshan | d9f8984 | 2010-08-09 17:20:02 -0700 | [diff] [blame] | 2257 | goto out_free; |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2258 | } |
Hugh Dickins | c73602a | 2009-10-07 16:32:22 -0700 | [diff] [blame] | 2259 | #else |
| 2260 | ksm_run = KSM_RUN_MERGE; /* no way for user to start it */ |
| 2261 | |
Hugh Dickins | 2ffd867 | 2009-09-21 17:02:23 -0700 | [diff] [blame] | 2262 | #endif /* CONFIG_SYSFS */ |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2263 | |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 2264 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 2265 | /* |
| 2266 | * Choose a high priority since the callback takes ksm_thread_mutex: |
| 2267 | * later callbacks could only be taking locks which nest within that. |
| 2268 | */ |
| 2269 | hotplug_memory_notifier(ksm_memory_callback, 100); |
| 2270 | #endif |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2271 | return 0; |
| 2272 | |
Lai Jiangshan | d9f8984 | 2010-08-09 17:20:02 -0700 | [diff] [blame] | 2273 | out_free: |
Izik Eidus | 31dbd01 | 2009-09-21 17:02:03 -0700 | [diff] [blame] | 2274 | ksm_slab_free(); |
| 2275 | out: |
| 2276 | return err; |
| 2277 | } |
| 2278 | module_init(ksm_init) |