Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mm/rmap.c - physical to virtual reverse mappings |
| 3 | * |
| 4 | * Copyright 2001, Rik van Riel <riel@conectiva.com.br> |
| 5 | * Released under the General Public License (GPL). |
| 6 | * |
| 7 | * Simple, low overhead reverse mapping scheme. |
| 8 | * Please try to keep this thing as modular as possible. |
| 9 | * |
| 10 | * Provides methods for unmapping each kind of mapped page: |
| 11 | * the anon methods track anonymous pages, and |
| 12 | * the file methods track pages belonging to an inode. |
| 13 | * |
| 14 | * Original design by Rik van Riel <riel@conectiva.com.br> 2001 |
| 15 | * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004 |
| 16 | * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004 |
Hugh Dickins | 98f3260 | 2009-05-21 20:33:58 +0100 | [diff] [blame] | 17 | * Contributions by Hugh Dickins 2003, 2004 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Lock ordering in mm: |
| 22 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 23 | * inode->i_mutex (while writing or truncating, not reading or faulting) |
Nick Piggin | 82591e6 | 2006-10-19 23:29:10 -0700 | [diff] [blame] | 24 | * mm->mmap_sem |
| 25 | * page->flags PG_locked (lock_page) |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 26 | * mapping->i_mmap_mutex |
Peter Zijlstra | 2b575eb | 2011-05-24 17:12:11 -0700 | [diff] [blame] | 27 | * anon_vma->mutex |
Nick Piggin | 82591e6 | 2006-10-19 23:29:10 -0700 | [diff] [blame] | 28 | * mm->page_table_lock or pte_lock |
| 29 | * zone->lru_lock (in mark_page_accessed, isolate_lru_page) |
| 30 | * swap_lock (in swap_duplicate, swap_info_get) |
| 31 | * mmlist_lock (in mmput, drain_mmlist and others) |
| 32 | * mapping->private_lock (in __set_page_dirty_buffers) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 33 | * inode->i_lock (in set_page_dirty's __mark_inode_dirty) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 34 | * bdi.wb->list_lock (in set_page_dirty's __mark_inode_dirty) |
Nick Piggin | 82591e6 | 2006-10-19 23:29:10 -0700 | [diff] [blame] | 35 | * sb_lock (within inode_lock in fs/fs-writeback.c) |
| 36 | * mapping->tree_lock (widely used, in set_page_dirty, |
| 37 | * in arch-dependent flush_dcache_mmap_lock, |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 38 | * within bdi.wb->list_lock in __sync_single_inode) |
Andi Kleen | 6a46079 | 2009-09-16 11:50:15 +0200 | [diff] [blame] | 39 | * |
Peter Zijlstra | 9b67932 | 2011-06-27 16:18:09 -0700 | [diff] [blame] | 40 | * anon_vma->mutex,mapping->i_mutex (memory_failure, collect_procs_anon) |
| 41 | * ->tasklist_lock |
Andi Kleen | 6a46079 | 2009-09-16 11:50:15 +0200 | [diff] [blame] | 42 | * pte map lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | #include <linux/mm.h> |
| 46 | #include <linux/pagemap.h> |
| 47 | #include <linux/swap.h> |
| 48 | #include <linux/swapops.h> |
| 49 | #include <linux/slab.h> |
| 50 | #include <linux/init.h> |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 51 | #include <linux/ksm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/rmap.h> |
| 53 | #include <linux/rcupdate.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 54 | #include <linux/export.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 55 | #include <linux/memcontrol.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 56 | #include <linux/mmu_notifier.h> |
KOSAKI Motohiro | 64cdd54 | 2009-01-06 14:39:16 -0800 | [diff] [blame] | 57 | #include <linux/migrate.h> |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 58 | #include <linux/hugetlb.h> |
Jan Kara | 89bc00f | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 59 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | #include <asm/tlbflush.h> |
| 62 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 63 | #include "internal.h" |
| 64 | |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 65 | static struct kmem_cache *anon_vma_cachep; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 66 | static struct kmem_cache *anon_vma_chain_cachep; |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 67 | |
| 68 | static inline struct anon_vma *anon_vma_alloc(void) |
| 69 | { |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 70 | struct anon_vma *anon_vma; |
| 71 | |
| 72 | anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); |
| 73 | if (anon_vma) { |
| 74 | atomic_set(&anon_vma->refcount, 1); |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 75 | anon_vma->degree = 1; /* Reference for first vma */ |
| 76 | anon_vma->parent = anon_vma; |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Initialise the anon_vma root to point to itself. If called |
| 79 | * from fork, the root will be reset to the parents anon_vma. |
| 80 | */ |
| 81 | anon_vma->root = anon_vma; |
| 82 | } |
| 83 | |
| 84 | return anon_vma; |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 87 | static inline void anon_vma_free(struct anon_vma *anon_vma) |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 88 | { |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 89 | VM_BUG_ON(atomic_read(&anon_vma->refcount)); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Synchronize against page_lock_anon_vma() such that |
| 93 | * we can safely hold the lock without the anon_vma getting |
| 94 | * freed. |
| 95 | * |
| 96 | * Relies on the full mb implied by the atomic_dec_and_test() from |
| 97 | * put_anon_vma() against the acquire barrier implied by |
| 98 | * mutex_trylock() from page_lock_anon_vma(). This orders: |
| 99 | * |
| 100 | * page_lock_anon_vma() VS put_anon_vma() |
| 101 | * mutex_trylock() atomic_dec_and_test() |
| 102 | * LOCK MB |
| 103 | * atomic_read() mutex_is_locked() |
| 104 | * |
| 105 | * LOCK should suffice since the actual taking of the lock must |
| 106 | * happen _before_ what follows. |
| 107 | */ |
Hugh Dickins | af3de04 | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 108 | might_sleep(); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 109 | if (mutex_is_locked(&anon_vma->root->mutex)) { |
| 110 | anon_vma_lock(anon_vma); |
| 111 | anon_vma_unlock(anon_vma); |
| 112 | } |
| 113 | |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 114 | kmem_cache_free(anon_vma_cachep, anon_vma); |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 117 | static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 118 | { |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 119 | return kmem_cache_alloc(anon_vma_chain_cachep, gfp); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Namhyung Kim | e574b5f | 2010-10-26 14:22:02 -0700 | [diff] [blame] | 122 | static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 123 | { |
| 124 | kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain); |
| 125 | } |
| 126 | |
Kautuk Consul | 6583a84 | 2012-03-21 16:34:01 -0700 | [diff] [blame] | 127 | static void anon_vma_chain_link(struct vm_area_struct *vma, |
| 128 | struct anon_vma_chain *avc, |
| 129 | struct anon_vma *anon_vma) |
| 130 | { |
| 131 | avc->vma = vma; |
| 132 | avc->anon_vma = anon_vma; |
| 133 | list_add(&avc->same_vma, &vma->anon_vma_chain); |
| 134 | |
| 135 | /* |
| 136 | * It's critical to add new vmas to the tail of the anon_vma, |
| 137 | * see comment in huge_memory.c:__split_huge_page(). |
| 138 | */ |
| 139 | list_add_tail(&avc->same_anon_vma, &anon_vma->head); |
| 140 | } |
| 141 | |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 142 | /** |
| 143 | * anon_vma_prepare - attach an anon_vma to a memory region |
| 144 | * @vma: the memory region in question |
| 145 | * |
| 146 | * This makes sure the memory mapping described by 'vma' has |
| 147 | * an 'anon_vma' attached to it, so that we can associate the |
| 148 | * anonymous pages mapped into it with that anon_vma. |
| 149 | * |
| 150 | * The common case will be that we already have one, but if |
Figo.zhang | 23a0790 | 2010-12-27 15:14:06 +0100 | [diff] [blame] | 151 | * not we either need to find an adjacent mapping that we |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 152 | * can re-use the anon_vma from (very common when the only |
| 153 | * reason for splitting a vma has been mprotect()), or we |
| 154 | * allocate a new one. |
| 155 | * |
| 156 | * Anon-vma allocations are very subtle, because we may have |
| 157 | * optimistically looked up an anon_vma in page_lock_anon_vma() |
| 158 | * and that may actually touch the spinlock even in the newly |
| 159 | * allocated vma (it depends on RCU to make sure that the |
| 160 | * anon_vma isn't actually destroyed). |
| 161 | * |
| 162 | * As a result, we need to do proper anon_vma locking even |
| 163 | * for the new allocation. At the same time, we do not want |
| 164 | * to do any locking for the common case of already having |
| 165 | * an anon_vma. |
| 166 | * |
| 167 | * This must be called with the mmap_sem held for reading. |
| 168 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | int anon_vma_prepare(struct vm_area_struct *vma) |
| 170 | { |
| 171 | struct anon_vma *anon_vma = vma->anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 172 | struct anon_vma_chain *avc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | might_sleep(); |
| 175 | if (unlikely(!anon_vma)) { |
| 176 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 177 | struct anon_vma *allocated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 179 | avc = anon_vma_chain_alloc(GFP_KERNEL); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 180 | if (!avc) |
| 181 | goto out_enomem; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | anon_vma = find_mergeable_anon_vma(vma); |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 184 | allocated = NULL; |
| 185 | if (!anon_vma) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | anon_vma = anon_vma_alloc(); |
| 187 | if (unlikely(!anon_vma)) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 188 | goto out_enomem_free_avc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | allocated = anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Rik van Riel | cba48b9 | 2010-08-09 17:18:38 -0700 | [diff] [blame] | 192 | anon_vma_lock(anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* page_table_lock to protect against threads */ |
| 194 | spin_lock(&mm->page_table_lock); |
| 195 | if (likely(!vma->anon_vma)) { |
| 196 | vma->anon_vma = anon_vma; |
Kautuk Consul | 6583a84 | 2012-03-21 16:34:01 -0700 | [diff] [blame] | 197 | anon_vma_chain_link(vma, avc, anon_vma); |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 198 | /* vma reference or self-parent link for new root */ |
| 199 | anon_vma->degree++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | allocated = NULL; |
Oleg Nesterov | 31f2b0e | 2010-04-23 13:18:01 -0400 | [diff] [blame] | 201 | avc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | spin_unlock(&mm->page_table_lock); |
Rik van Riel | cba48b9 | 2010-08-09 17:18:38 -0700 | [diff] [blame] | 204 | anon_vma_unlock(anon_vma); |
Oleg Nesterov | 31f2b0e | 2010-04-23 13:18:01 -0400 | [diff] [blame] | 205 | |
| 206 | if (unlikely(allocated)) |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 207 | put_anon_vma(allocated); |
Oleg Nesterov | 31f2b0e | 2010-04-23 13:18:01 -0400 | [diff] [blame] | 208 | if (unlikely(avc)) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 209 | anon_vma_chain_free(avc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | return 0; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 212 | |
| 213 | out_enomem_free_avc: |
| 214 | anon_vma_chain_free(avc); |
| 215 | out_enomem: |
| 216 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 219 | /* |
| 220 | * This is a useful helper function for locking the anon_vma root as |
| 221 | * we traverse the vma->anon_vma_chain, looping over anon_vma's that |
| 222 | * have the same vma. |
| 223 | * |
| 224 | * Such anon_vma's should have the same root, so you'd expect to see |
| 225 | * just a single mutex_lock for the whole traversal. |
| 226 | */ |
| 227 | static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma) |
| 228 | { |
| 229 | struct anon_vma *new_root = anon_vma->root; |
| 230 | if (new_root != root) { |
| 231 | if (WARN_ON_ONCE(root)) |
| 232 | mutex_unlock(&root->mutex); |
| 233 | root = new_root; |
| 234 | mutex_lock(&root->mutex); |
| 235 | } |
| 236 | return root; |
| 237 | } |
| 238 | |
| 239 | static inline void unlock_anon_vma_root(struct anon_vma *root) |
| 240 | { |
| 241 | if (root) |
| 242 | mutex_unlock(&root->mutex); |
| 243 | } |
| 244 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 245 | /* |
| 246 | * Attach the anon_vmas from src to dst. |
| 247 | * Returns 0 on success, -ENOMEM on failure. |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 248 | * |
| 249 | * If dst->anon_vma is NULL this function tries to find and reuse existing |
| 250 | * anon_vma which has no vmas and only one child anon_vma. This prevents |
| 251 | * degradation of anon_vma hierarchy to endless linear chain in case of |
| 252 | * constantly forking task. On the other hand, an anon_vma with more than one |
| 253 | * child isn't reused even if there was no alive vma, thus rmap walker has a |
| 254 | * good chance of avoiding scanning the whole hierarchy when it searches where |
| 255 | * page is mapped. |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 256 | */ |
| 257 | int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 259 | struct anon_vma_chain *avc, *pavc; |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 260 | struct anon_vma *root = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Linus Torvalds | 646d87b | 2010-04-11 17:15:03 -0700 | [diff] [blame] | 262 | list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) { |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 263 | struct anon_vma *anon_vma; |
| 264 | |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 265 | avc = anon_vma_chain_alloc(GFP_NOWAIT | __GFP_NOWARN); |
| 266 | if (unlikely(!avc)) { |
| 267 | unlock_anon_vma_root(root); |
| 268 | root = NULL; |
| 269 | avc = anon_vma_chain_alloc(GFP_KERNEL); |
| 270 | if (!avc) |
| 271 | goto enomem_failure; |
| 272 | } |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 273 | anon_vma = pavc->anon_vma; |
| 274 | root = lock_anon_vma_root(root, anon_vma); |
| 275 | anon_vma_chain_link(dst, avc, anon_vma); |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Reuse existing anon_vma if its degree lower than two, |
| 279 | * that means it has no vma and only one anon_vma child. |
| 280 | * |
| 281 | * Do not chose parent anon_vma, otherwise first child |
| 282 | * will always reuse it. Root anon_vma is never reused: |
| 283 | * it has self-parent reference and at least one child. |
| 284 | */ |
| 285 | if (!dst->anon_vma && anon_vma != src->anon_vma && |
| 286 | anon_vma->degree < 2) |
| 287 | dst->anon_vma = anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 289 | if (dst->anon_vma) |
| 290 | dst->anon_vma->degree++; |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 291 | unlock_anon_vma_root(root); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 292 | return 0; |
| 293 | |
| 294 | enomem_failure: |
Leon Yu | 7ac06e3 | 2015-03-25 15:55:11 -0700 | [diff] [blame] | 295 | /* |
| 296 | * dst->anon_vma is dropped here otherwise its degree can be incorrectly |
| 297 | * decremented in unlink_anon_vmas(). |
| 298 | * We can safely do this because callers of anon_vma_clone() don't care |
| 299 | * about dst->anon_vma if anon_vma_clone() failed. |
| 300 | */ |
| 301 | dst->anon_vma = NULL; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 302 | unlink_anon_vmas(dst); |
| 303 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 306 | /* |
Andrea Arcangeli | 948f017 | 2012-01-10 15:08:05 -0800 | [diff] [blame] | 307 | * Some rmap walk that needs to find all ptes/hugepmds without false |
| 308 | * negatives (like migrate and split_huge_page) running concurrent |
| 309 | * with operations that copy or move pagetables (like mremap() and |
| 310 | * fork()) to be safe. They depend on the anon_vma "same_anon_vma" |
| 311 | * list to be in a certain order: the dst_vma must be placed after the |
| 312 | * src_vma in the list. This is always guaranteed by fork() but |
| 313 | * mremap() needs to call this function to enforce it in case the |
| 314 | * dst_vma isn't newly allocated and chained with the anon_vma_clone() |
| 315 | * function but just an extension of a pre-existing vma through |
| 316 | * vma_merge. |
| 317 | * |
| 318 | * NOTE: the same_anon_vma list can still be changed by other |
| 319 | * processes while mremap runs because mremap doesn't hold the |
| 320 | * anon_vma mutex to prevent modifications to the list while it |
| 321 | * runs. All we need to enforce is that the relative order of this |
| 322 | * process vmas isn't changing (we don't care about other vmas |
| 323 | * order). Each vma corresponds to an anon_vma_chain structure so |
| 324 | * there's no risk that other processes calling anon_vma_moveto_tail() |
| 325 | * and changing the same_anon_vma list under mremap() will screw with |
| 326 | * the relative order of this process vmas in the list, because we |
| 327 | * they can't alter the order of any vma that belongs to this |
| 328 | * process. And there can't be another anon_vma_moveto_tail() running |
| 329 | * concurrently with mremap() coming from this process because we hold |
| 330 | * the mmap_sem for the whole mremap(). fork() ordering dependency |
| 331 | * also shouldn't be affected because fork() only cares that the |
| 332 | * parent vmas are placed in the list before the child vmas and |
| 333 | * anon_vma_moveto_tail() won't reorder vmas from either the fork() |
| 334 | * parent or child. |
| 335 | */ |
| 336 | void anon_vma_moveto_tail(struct vm_area_struct *dst) |
| 337 | { |
| 338 | struct anon_vma_chain *pavc; |
| 339 | struct anon_vma *root = NULL; |
| 340 | |
| 341 | list_for_each_entry_reverse(pavc, &dst->anon_vma_chain, same_vma) { |
| 342 | struct anon_vma *anon_vma = pavc->anon_vma; |
| 343 | VM_BUG_ON(pavc->vma != dst); |
| 344 | root = lock_anon_vma_root(root, anon_vma); |
| 345 | list_del(&pavc->same_anon_vma); |
| 346 | list_add_tail(&pavc->same_anon_vma, &anon_vma->head); |
| 347 | } |
| 348 | unlock_anon_vma_root(root); |
| 349 | } |
| 350 | |
| 351 | /* |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 352 | * Attach vma to its own anon_vma, as well as to the anon_vmas that |
| 353 | * the corresponding VMA in the parent process is attached to. |
| 354 | * Returns 0 on success, non-zero on failure. |
| 355 | */ |
| 356 | int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 358 | struct anon_vma_chain *avc; |
| 359 | struct anon_vma *anon_vma; |
| 360 | |
| 361 | /* Don't bother if the parent process has no anon_vma here. */ |
| 362 | if (!pvma->anon_vma) |
| 363 | return 0; |
| 364 | |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 365 | /* Drop inherited anon_vma, we'll reuse existing or allocate new. */ |
| 366 | vma->anon_vma = NULL; |
| 367 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 368 | /* |
| 369 | * First, attach the new VMA to the parent VMA's anon_vmas, |
| 370 | * so rmap can find non-COWed pages in child processes. |
| 371 | */ |
| 372 | if (anon_vma_clone(vma, pvma)) |
| 373 | return -ENOMEM; |
| 374 | |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 375 | /* An existing anon_vma has been reused, all done then. */ |
| 376 | if (vma->anon_vma) |
| 377 | return 0; |
| 378 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 379 | /* Then add our own anon_vma. */ |
| 380 | anon_vma = anon_vma_alloc(); |
| 381 | if (!anon_vma) |
| 382 | goto out_error; |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 383 | avc = anon_vma_chain_alloc(GFP_KERNEL); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 384 | if (!avc) |
| 385 | goto out_error_free_anon_vma; |
Rik van Riel | 5c341ee | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * The root anon_vma's spinlock is the lock actually used when we |
| 389 | * lock any of the anon_vmas in this anon_vma tree. |
| 390 | */ |
| 391 | anon_vma->root = pvma->anon_vma->root; |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 392 | anon_vma->parent = pvma->anon_vma; |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 393 | /* |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 394 | * With refcounts, an anon_vma can stay around longer than the |
| 395 | * process it belongs to. The root anon_vma needs to be pinned until |
| 396 | * this anon_vma is freed, because the lock lives in the root. |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 397 | */ |
| 398 | get_anon_vma(anon_vma->root); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 399 | /* Mark this anon_vma as the one where our new (COWed) pages go. */ |
| 400 | vma->anon_vma = anon_vma; |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 401 | anon_vma_lock(anon_vma); |
Rik van Riel | 5c341ee | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 402 | anon_vma_chain_link(vma, avc, anon_vma); |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 403 | anon_vma->parent->degree++; |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 404 | anon_vma_unlock(anon_vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | |
| 408 | out_error_free_anon_vma: |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 409 | put_anon_vma(anon_vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 410 | out_error: |
Rik van Riel | 4946d54 | 2010-04-05 12:13:33 -0400 | [diff] [blame] | 411 | unlink_anon_vmas(vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 412 | return -ENOMEM; |
| 413 | } |
| 414 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 415 | void unlink_anon_vmas(struct vm_area_struct *vma) |
| 416 | { |
| 417 | struct anon_vma_chain *avc, *next; |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 418 | struct anon_vma *root = NULL; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 419 | |
Rik van Riel | 5c341ee | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 420 | /* |
| 421 | * Unlink each anon_vma chained to the VMA. This list is ordered |
| 422 | * from newest to oldest, ensuring the root anon_vma gets freed last. |
| 423 | */ |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 424 | list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 425 | struct anon_vma *anon_vma = avc->anon_vma; |
| 426 | |
| 427 | root = lock_anon_vma_root(root, anon_vma); |
| 428 | list_del(&avc->same_anon_vma); |
| 429 | |
| 430 | /* |
| 431 | * Leave empty anon_vmas on the list - we'll need |
| 432 | * to free them outside the lock. |
| 433 | */ |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 434 | if (list_empty(&anon_vma->head)) { |
| 435 | anon_vma->parent->degree--; |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 436 | continue; |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 437 | } |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 438 | |
| 439 | list_del(&avc->same_vma); |
| 440 | anon_vma_chain_free(avc); |
| 441 | } |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 442 | if (vma->anon_vma) |
| 443 | vma->anon_vma->degree--; |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 444 | unlock_anon_vma_root(root); |
| 445 | |
| 446 | /* |
| 447 | * Iterate the list once more, it now only contains empty and unlinked |
| 448 | * anon_vmas, destroy them. Could not do before due to __put_anon_vma() |
| 449 | * needing to acquire the anon_vma->root->mutex. |
| 450 | */ |
| 451 | list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { |
| 452 | struct anon_vma *anon_vma = avc->anon_vma; |
| 453 | |
Konstantin Khlebnikov | 6ce254f | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 454 | BUG_ON(anon_vma->degree); |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 455 | put_anon_vma(anon_vma); |
| 456 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 457 | list_del(&avc->same_vma); |
| 458 | anon_vma_chain_free(avc); |
| 459 | } |
| 460 | } |
| 461 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 462 | static void anon_vma_ctor(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 464 | struct anon_vma *anon_vma = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Peter Zijlstra | 2b575eb | 2011-05-24 17:12:11 -0700 | [diff] [blame] | 466 | mutex_init(&anon_vma->mutex); |
Peter Zijlstra | 8381326 | 2011-03-22 16:32:48 -0700 | [diff] [blame] | 467 | atomic_set(&anon_vma->refcount, 0); |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 468 | INIT_LIST_HEAD(&anon_vma->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void __init anon_vma_init(void) |
| 472 | { |
| 473 | anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 474 | 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 475 | anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, SLAB_PANIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* |
Peter Zijlstra | 6111e4c | 2011-05-24 17:12:08 -0700 | [diff] [blame] | 479 | * Getting a lock on a stable anon_vma from a page off the LRU is tricky! |
| 480 | * |
| 481 | * Since there is no serialization what so ever against page_remove_rmap() |
| 482 | * the best this function can do is return a locked anon_vma that might |
| 483 | * have been relevant to this page. |
| 484 | * |
| 485 | * The page might have been remapped to a different anon_vma or the anon_vma |
| 486 | * returned may already be freed (and even reused). |
| 487 | * |
Peter Zijlstra | bc658c9 | 2011-05-29 10:33:44 +0200 | [diff] [blame] | 488 | * In case it was remapped to a different anon_vma, the new anon_vma will be a |
| 489 | * child of the old anon_vma, and the anon_vma lifetime rules will therefore |
| 490 | * ensure that any anon_vma obtained from the page will still be valid for as |
| 491 | * long as we observe page_mapped() [ hence all those page_mapped() tests ]. |
| 492 | * |
Peter Zijlstra | 6111e4c | 2011-05-24 17:12:08 -0700 | [diff] [blame] | 493 | * All users of this function must be very careful when walking the anon_vma |
| 494 | * chain and verify that the page in question is indeed mapped in it |
| 495 | * [ something equivalent to page_mapped_in_vma() ]. |
| 496 | * |
| 497 | * Since anon_vma's slab is DESTROY_BY_RCU and we know from page_remove_rmap() |
| 498 | * that the anon_vma pointer from page->mapping is valid if there is a |
| 499 | * mapcount, we can dereference the anon_vma after observing those. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | */ |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 501 | struct anon_vma *page_get_anon_vma(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 503 | struct anon_vma *anon_vma = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | unsigned long anon_mapping; |
| 505 | |
| 506 | rcu_read_lock(); |
Hugh Dickins | 80e1482 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 507 | anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping); |
Hugh Dickins | 3ca7b3c | 2009-12-14 17:58:57 -0800 | [diff] [blame] | 508 | if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | goto out; |
| 510 | if (!page_mapped(page)) |
| 511 | goto out; |
| 512 | |
| 513 | anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 514 | if (!atomic_inc_not_zero(&anon_vma->refcount)) { |
| 515 | anon_vma = NULL; |
| 516 | goto out; |
| 517 | } |
Hugh Dickins | f181942 | 2010-08-25 23:12:54 -0700 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * If this page is still mapped, then its anon_vma cannot have been |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 521 | * freed. But if it has been unmapped, we have no security against the |
| 522 | * anon_vma structure being freed and reused (for another anon_vma: |
| 523 | * SLAB_DESTROY_BY_RCU guarantees that - so the atomic_inc_not_zero() |
| 524 | * above cannot corrupt). |
Hugh Dickins | f181942 | 2010-08-25 23:12:54 -0700 | [diff] [blame] | 525 | */ |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 526 | if (!page_mapped(page)) { |
Hugh Dickins | af3de04 | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 527 | rcu_read_unlock(); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 528 | put_anon_vma(anon_vma); |
Hugh Dickins | af3de04 | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 529 | return NULL; |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | out: |
| 532 | rcu_read_unlock(); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 533 | |
| 534 | return anon_vma; |
| 535 | } |
| 536 | |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 537 | /* |
| 538 | * Similar to page_get_anon_vma() except it locks the anon_vma. |
| 539 | * |
| 540 | * Its a little more complex as it tries to keep the fast path to a single |
| 541 | * atomic op -- the trylock. If we fail the trylock, we fall back to getting a |
| 542 | * reference like with page_get_anon_vma() and then block on the mutex. |
| 543 | */ |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 544 | struct anon_vma *page_lock_anon_vma(struct page *page) |
| 545 | { |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 546 | struct anon_vma *anon_vma = NULL; |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 547 | struct anon_vma *root_anon_vma; |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 548 | unsigned long anon_mapping; |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 549 | |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 550 | rcu_read_lock(); |
| 551 | anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping); |
| 552 | if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) |
| 553 | goto out; |
| 554 | if (!page_mapped(page)) |
| 555 | goto out; |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 556 | |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 557 | anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 558 | root_anon_vma = ACCESS_ONCE(anon_vma->root); |
| 559 | if (mutex_trylock(&root_anon_vma->mutex)) { |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 560 | /* |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 561 | * If the page is still mapped, then this anon_vma is still |
| 562 | * its anon_vma, and holding the mutex ensures that it will |
Peter Zijlstra | bc658c9 | 2011-05-29 10:33:44 +0200 | [diff] [blame] | 563 | * not go away, see anon_vma_free(). |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 564 | */ |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 565 | if (!page_mapped(page)) { |
| 566 | mutex_unlock(&root_anon_vma->mutex); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 567 | anon_vma = NULL; |
| 568 | } |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | /* trylock failed, we got to sleep */ |
| 573 | if (!atomic_inc_not_zero(&anon_vma->refcount)) { |
| 574 | anon_vma = NULL; |
| 575 | goto out; |
| 576 | } |
| 577 | |
| 578 | if (!page_mapped(page)) { |
Hugh Dickins | af3de04 | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 579 | rcu_read_unlock(); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 580 | put_anon_vma(anon_vma); |
Hugh Dickins | af3de04 | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 581 | return NULL; |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* we pinned the anon_vma, its safe to sleep */ |
| 585 | rcu_read_unlock(); |
| 586 | anon_vma_lock(anon_vma); |
| 587 | |
| 588 | if (atomic_dec_and_test(&anon_vma->refcount)) { |
| 589 | /* |
| 590 | * Oops, we held the last refcount, release the lock |
| 591 | * and bail -- can't simply use put_anon_vma() because |
| 592 | * we'll deadlock on the anon_vma_lock() recursion. |
| 593 | */ |
| 594 | anon_vma_unlock(anon_vma); |
| 595 | __put_anon_vma(anon_vma); |
| 596 | anon_vma = NULL; |
| 597 | } |
| 598 | |
| 599 | return anon_vma; |
| 600 | |
| 601 | out: |
| 602 | rcu_read_unlock(); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 603 | return anon_vma; |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Andi Kleen | 10be22d | 2009-09-16 11:50:04 +0200 | [diff] [blame] | 606 | void page_unlock_anon_vma(struct anon_vma *anon_vma) |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 607 | { |
Rik van Riel | cba48b9 | 2010-08-09 17:18:38 -0700 | [diff] [blame] | 608 | anon_vma_unlock(anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
Lee Schermerhorn | 3ad33b2 | 2007-11-14 16:59:10 -0800 | [diff] [blame] | 612 | * At what user virtual address is page expected in @vma? |
| 613 | * Returns virtual address or -EFAULT if page's index/offset is not |
| 614 | * within the range mapped the @vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 616 | inline unsigned long |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | vma_address(struct page *page, struct vm_area_struct *vma) |
| 618 | { |
| 619 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
| 620 | unsigned long address; |
| 621 | |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 622 | if (unlikely(is_vm_hugetlb_page(vma))) |
| 623 | pgoff = page->index << huge_page_order(page_hstate(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); |
| 625 | if (unlikely(address < vma->vm_start || address >= vma->vm_end)) { |
Lee Schermerhorn | 3ad33b2 | 2007-11-14 16:59:10 -0800 | [diff] [blame] | 626 | /* page should be within @vma mapping range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return -EFAULT; |
| 628 | } |
| 629 | return address; |
| 630 | } |
| 631 | |
| 632 | /* |
Huang Shijie | bf89c8c | 2009-10-01 15:44:04 -0700 | [diff] [blame] | 633 | * At what user virtual address is page expected in vma? |
Naoya Horiguchi | ab941e0 | 2010-05-11 14:06:55 -0700 | [diff] [blame] | 634 | * Caller should check the page is actually part of the vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | */ |
| 636 | unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma) |
| 637 | { |
Andrea Arcangeli | 21d0d44 | 2010-08-09 17:19:10 -0700 | [diff] [blame] | 638 | if (PageAnon(page)) { |
Hugh Dickins | 4829b90 | 2010-10-02 17:46:06 -0700 | [diff] [blame] | 639 | struct anon_vma *page__anon_vma = page_anon_vma(page); |
| 640 | /* |
| 641 | * Note: swapoff's unuse_vma() is more efficient with this |
| 642 | * check, and needs it to match anon_vma when KSM is active. |
| 643 | */ |
| 644 | if (!vma->anon_vma || !page__anon_vma || |
| 645 | vma->anon_vma->root != page__anon_vma->root) |
Andrea Arcangeli | 21d0d44 | 2010-08-09 17:19:10 -0700 | [diff] [blame] | 646 | return -EFAULT; |
| 647 | } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) { |
Hugh Dickins | ee498ed | 2005-11-21 21:32:18 -0800 | [diff] [blame] | 648 | if (!vma->vm_file || |
| 649 | vma->vm_file->f_mapping != page->mapping) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return -EFAULT; |
| 651 | } else |
| 652 | return -EFAULT; |
| 653 | return vma_address(page, vma); |
| 654 | } |
| 655 | |
| 656 | /* |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 657 | * Check that @page is mapped at @address into @mm. |
| 658 | * |
Nick Piggin | 479db0b | 2008-08-20 14:09:18 -0700 | [diff] [blame] | 659 | * If @sync is false, page_check_address may perform a racy check to avoid |
| 660 | * the page table lock when the pte is not present (helpful when reclaiming |
| 661 | * highly shared pages). |
| 662 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 663 | * On success returns with pte mapped and locked. |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 664 | */ |
Namhyung Kim | e9a81a8 | 2010-10-26 14:22:01 -0700 | [diff] [blame] | 665 | pte_t *__page_check_address(struct page *page, struct mm_struct *mm, |
Nick Piggin | 479db0b | 2008-08-20 14:09:18 -0700 | [diff] [blame] | 666 | unsigned long address, spinlock_t **ptlp, int sync) |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 667 | { |
| 668 | pgd_t *pgd; |
| 669 | pud_t *pud; |
| 670 | pmd_t *pmd; |
| 671 | pte_t *pte; |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 672 | spinlock_t *ptl; |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 673 | |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 674 | if (unlikely(PageHuge(page))) { |
Jianguo Wu | 90d23d2 | 2013-12-18 17:08:59 -0800 | [diff] [blame] | 675 | /* when pud is not present, pte will be NULL */ |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 676 | pte = huge_pte_offset(mm, address); |
Jianguo Wu | 90d23d2 | 2013-12-18 17:08:59 -0800 | [diff] [blame] | 677 | if (!pte) |
| 678 | return NULL; |
| 679 | |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 680 | ptl = &mm->page_table_lock; |
| 681 | goto check; |
| 682 | } |
| 683 | |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 684 | pgd = pgd_offset(mm, address); |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 685 | if (!pgd_present(*pgd)) |
| 686 | return NULL; |
| 687 | |
| 688 | pud = pud_offset(pgd, address); |
| 689 | if (!pud_present(*pud)) |
| 690 | return NULL; |
| 691 | |
| 692 | pmd = pmd_offset(pud, address); |
| 693 | if (!pmd_present(*pmd)) |
| 694 | return NULL; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 695 | if (pmd_trans_huge(*pmd)) |
| 696 | return NULL; |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 697 | |
| 698 | pte = pte_offset_map(pmd, address); |
| 699 | /* Make a quick check before getting the lock */ |
Nick Piggin | 479db0b | 2008-08-20 14:09:18 -0700 | [diff] [blame] | 700 | if (!sync && !pte_present(*pte)) { |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 701 | pte_unmap(pte); |
| 702 | return NULL; |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 703 | } |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 704 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 705 | ptl = pte_lockptr(mm, pmd); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 706 | check: |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 707 | spin_lock(ptl); |
| 708 | if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) { |
| 709 | *ptlp = ptl; |
| 710 | return pte; |
| 711 | } |
| 712 | pte_unmap_unlock(pte, ptl); |
| 713 | return NULL; |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 716 | /** |
| 717 | * page_mapped_in_vma - check whether a page is really mapped in a VMA |
| 718 | * @page: the page to test |
| 719 | * @vma: the VMA to test |
| 720 | * |
| 721 | * Returns 1 if the page is mapped into the page tables of the VMA, 0 |
| 722 | * if the page is not mapped into the page tables of this VMA. Only |
| 723 | * valid for normal file or anonymous VMAs. |
| 724 | */ |
Andi Kleen | 6a46079 | 2009-09-16 11:50:15 +0200 | [diff] [blame] | 725 | int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 726 | { |
| 727 | unsigned long address; |
| 728 | pte_t *pte; |
| 729 | spinlock_t *ptl; |
| 730 | |
| 731 | address = vma_address(page, vma); |
| 732 | if (address == -EFAULT) /* out of vma range */ |
| 733 | return 0; |
| 734 | pte = page_check_address(page, vma->vm_mm, address, &ptl, 1); |
| 735 | if (!pte) /* the page is not in this mm */ |
| 736 | return 0; |
| 737 | pte_unmap_unlock(pte, ptl); |
| 738 | |
| 739 | return 1; |
| 740 | } |
| 741 | |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 742 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | * Subfunctions of page_referenced: page_referenced_one called |
| 744 | * repeatedly from either page_referenced_anon or page_referenced_file. |
| 745 | */ |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 746 | int page_referenced_one(struct page *page, struct vm_area_struct *vma, |
| 747 | unsigned long address, unsigned int *mapcount, |
| 748 | unsigned long *vm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
| 750 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | int referenced = 0; |
| 752 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 753 | if (unlikely(PageTransHuge(page))) { |
| 754 | pmd_t *pmd; |
| 755 | |
| 756 | spin_lock(&mm->page_table_lock); |
Andrea Arcangeli | 2da28bf | 2011-03-11 14:58:29 -0800 | [diff] [blame] | 757 | /* |
| 758 | * rmap might return false positives; we must filter |
| 759 | * these out using page_check_address_pmd(). |
| 760 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 761 | pmd = page_check_address_pmd(page, mm, address, |
| 762 | PAGE_CHECK_ADDRESS_PMD_FLAG); |
Andrea Arcangeli | 2da28bf | 2011-03-11 14:58:29 -0800 | [diff] [blame] | 763 | if (!pmd) { |
| 764 | spin_unlock(&mm->page_table_lock); |
| 765 | goto out; |
| 766 | } |
| 767 | |
| 768 | if (vma->vm_flags & VM_LOCKED) { |
| 769 | spin_unlock(&mm->page_table_lock); |
| 770 | *mapcount = 0; /* break early from loop */ |
| 771 | *vm_flags |= VM_LOCKED; |
| 772 | goto out; |
| 773 | } |
| 774 | |
| 775 | /* go ahead even if the pmd is pmd_trans_splitting() */ |
| 776 | if (pmdp_clear_flush_young_notify(vma, address, pmd)) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 777 | referenced++; |
| 778 | spin_unlock(&mm->page_table_lock); |
| 779 | } else { |
| 780 | pte_t *pte; |
| 781 | spinlock_t *ptl; |
| 782 | |
Andrea Arcangeli | 2da28bf | 2011-03-11 14:58:29 -0800 | [diff] [blame] | 783 | /* |
| 784 | * rmap might return false positives; we must filter |
| 785 | * these out using page_check_address(). |
| 786 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 787 | pte = page_check_address(page, mm, address, &ptl, 0); |
| 788 | if (!pte) |
| 789 | goto out; |
| 790 | |
Andrea Arcangeli | 2da28bf | 2011-03-11 14:58:29 -0800 | [diff] [blame] | 791 | if (vma->vm_flags & VM_LOCKED) { |
| 792 | pte_unmap_unlock(pte, ptl); |
| 793 | *mapcount = 0; /* break early from loop */ |
| 794 | *vm_flags |= VM_LOCKED; |
| 795 | goto out; |
| 796 | } |
| 797 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 798 | if (ptep_clear_flush_young_notify(vma, address, pte)) { |
| 799 | /* |
| 800 | * Don't treat a reference through a sequentially read |
| 801 | * mapping as such. If the page has been used in |
| 802 | * another mapping, we will catch it; if this other |
| 803 | * mapping is already gone, the unmap path will have |
| 804 | * set PG_referenced or activated the page. |
| 805 | */ |
| 806 | if (likely(!VM_SequentialReadHint(vma))) |
| 807 | referenced++; |
| 808 | } |
| 809 | pte_unmap_unlock(pte, ptl); |
| 810 | } |
| 811 | |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 812 | (*mapcount)--; |
Huang Shijie | 273f047 | 2009-12-14 17:58:51 -0800 | [diff] [blame] | 813 | |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 814 | if (referenced) |
| 815 | *vm_flags |= vma->vm_flags; |
Huang Shijie | 273f047 | 2009-12-14 17:58:51 -0800 | [diff] [blame] | 816 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | return referenced; |
| 818 | } |
| 819 | |
Balbir Singh | bed7161 | 2008-02-07 00:14:01 -0800 | [diff] [blame] | 820 | static int page_referenced_anon(struct page *page, |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 821 | struct mem_cgroup *memcg, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 822 | unsigned long *vm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
| 824 | unsigned int mapcount; |
| 825 | struct anon_vma *anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 826 | struct anon_vma_chain *avc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | int referenced = 0; |
| 828 | |
| 829 | anon_vma = page_lock_anon_vma(page); |
| 830 | if (!anon_vma) |
| 831 | return referenced; |
| 832 | |
| 833 | mapcount = page_mapcount(page); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 834 | list_for_each_entry(avc, &anon_vma->head, same_anon_vma) { |
| 835 | struct vm_area_struct *vma = avc->vma; |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 836 | unsigned long address = vma_address(page, vma); |
| 837 | if (address == -EFAULT) |
| 838 | continue; |
Balbir Singh | bed7161 | 2008-02-07 00:14:01 -0800 | [diff] [blame] | 839 | /* |
| 840 | * If we are reclaiming on behalf of a cgroup, skip |
| 841 | * counting on behalf of references from different |
| 842 | * cgroups |
| 843 | */ |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 844 | if (memcg && !mm_match_cgroup(vma->vm_mm, memcg)) |
Balbir Singh | bed7161 | 2008-02-07 00:14:01 -0800 | [diff] [blame] | 845 | continue; |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 846 | referenced += page_referenced_one(page, vma, address, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 847 | &mapcount, vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | if (!mapcount) |
| 849 | break; |
| 850 | } |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 851 | |
| 852 | page_unlock_anon_vma(anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | return referenced; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * page_referenced_file - referenced check for object-based rmap |
| 858 | * @page: the page we're checking references on. |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 859 | * @memcg: target memory control group |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 860 | * @vm_flags: collect encountered vma->vm_flags who actually referenced the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | * |
| 862 | * For an object-based mapped page, find all the places it is mapped and |
| 863 | * check/clear the referenced flag. This is done by following the page->mapping |
| 864 | * pointer, then walking the chain of vmas it holds. It returns the number |
| 865 | * of references it found. |
| 866 | * |
| 867 | * This function is only called from page_referenced for object-based pages. |
| 868 | */ |
Balbir Singh | bed7161 | 2008-02-07 00:14:01 -0800 | [diff] [blame] | 869 | static int page_referenced_file(struct page *page, |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 870 | struct mem_cgroup *memcg, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 871 | unsigned long *vm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
| 873 | unsigned int mapcount; |
| 874 | struct address_space *mapping = page->mapping; |
| 875 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
| 876 | struct vm_area_struct *vma; |
| 877 | struct prio_tree_iter iter; |
| 878 | int referenced = 0; |
| 879 | |
| 880 | /* |
| 881 | * The caller's checks on page->mapping and !PageAnon have made |
| 882 | * sure that this is a file page: the check for page->mapping |
| 883 | * excludes the case just before it gets set on an anon page. |
| 884 | */ |
| 885 | BUG_ON(PageAnon(page)); |
| 886 | |
| 887 | /* |
| 888 | * The page lock not only makes sure that page->mapping cannot |
| 889 | * suddenly be NULLified by truncation, it makes sure that the |
| 890 | * structure at mapping cannot be freed and reused yet, |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 891 | * so we can safely take mapping->i_mmap_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | */ |
| 893 | BUG_ON(!PageLocked(page)); |
| 894 | |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 895 | mutex_lock(&mapping->i_mmap_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
| 897 | /* |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 898 | * i_mmap_mutex does not stabilize mapcount at all, but mapcount |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | * is more likely to be accurate if we note it after spinning. |
| 900 | */ |
| 901 | mapcount = page_mapcount(page); |
| 902 | |
| 903 | vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 904 | unsigned long address = vma_address(page, vma); |
| 905 | if (address == -EFAULT) |
| 906 | continue; |
Balbir Singh | bed7161 | 2008-02-07 00:14:01 -0800 | [diff] [blame] | 907 | /* |
| 908 | * If we are reclaiming on behalf of a cgroup, skip |
| 909 | * counting on behalf of references from different |
| 910 | * cgroups |
| 911 | */ |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 912 | if (memcg && !mm_match_cgroup(vma->vm_mm, memcg)) |
Balbir Singh | bed7161 | 2008-02-07 00:14:01 -0800 | [diff] [blame] | 913 | continue; |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 914 | referenced += page_referenced_one(page, vma, address, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 915 | &mapcount, vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | if (!mapcount) |
| 917 | break; |
| 918 | } |
| 919 | |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 920 | mutex_unlock(&mapping->i_mmap_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return referenced; |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * page_referenced - test if the page was referenced |
| 926 | * @page: the page to test |
| 927 | * @is_locked: caller holds lock on the page |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 928 | * @memcg: target memory cgroup |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 929 | * @vm_flags: collect encountered vma->vm_flags who actually referenced the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | * |
| 931 | * Quick test_and_clear_referenced for all mappings to a page, |
| 932 | * returns the number of ptes which referenced the page. |
| 933 | */ |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 934 | int page_referenced(struct page *page, |
| 935 | int is_locked, |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 936 | struct mem_cgroup *memcg, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 937 | unsigned long *vm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | { |
| 939 | int referenced = 0; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 940 | int we_locked = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 942 | *vm_flags = 0; |
Hugh Dickins | 3ca7b3c | 2009-12-14 17:58:57 -0800 | [diff] [blame] | 943 | if (page_mapped(page) && page_rmapping(page)) { |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 944 | if (!is_locked && (!PageAnon(page) || PageKsm(page))) { |
| 945 | we_locked = trylock_page(page); |
| 946 | if (!we_locked) { |
| 947 | referenced++; |
| 948 | goto out; |
| 949 | } |
| 950 | } |
| 951 | if (unlikely(PageKsm(page))) |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 952 | referenced += page_referenced_ksm(page, memcg, |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 953 | vm_flags); |
| 954 | else if (PageAnon(page)) |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 955 | referenced += page_referenced_anon(page, memcg, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 956 | vm_flags); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 957 | else if (page->mapping) |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 958 | referenced += page_referenced_file(page, memcg, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 959 | vm_flags); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 960 | if (we_locked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | unlock_page(page); |
Martin Schwidefsky | 50a1598 | 2011-07-24 10:47:59 +0200 | [diff] [blame] | 962 | |
| 963 | if (page_test_and_clear_young(page_to_pfn(page))) |
| 964 | referenced++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 966 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | return referenced; |
| 968 | } |
| 969 | |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 970 | static int page_mkclean_one(struct page *page, struct vm_area_struct *vma, |
| 971 | unsigned long address) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 972 | { |
| 973 | struct mm_struct *mm = vma->vm_mm; |
Peter Zijlstra | c2fda5f | 2006-12-22 14:25:52 +0100 | [diff] [blame] | 974 | pte_t *pte; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 975 | spinlock_t *ptl; |
| 976 | int ret = 0; |
| 977 | |
Nick Piggin | 479db0b | 2008-08-20 14:09:18 -0700 | [diff] [blame] | 978 | pte = page_check_address(page, mm, address, &ptl, 1); |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 979 | if (!pte) |
| 980 | goto out; |
| 981 | |
Peter Zijlstra | c2fda5f | 2006-12-22 14:25:52 +0100 | [diff] [blame] | 982 | if (pte_dirty(*pte) || pte_write(*pte)) { |
| 983 | pte_t entry; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 984 | |
Peter Zijlstra | c2fda5f | 2006-12-22 14:25:52 +0100 | [diff] [blame] | 985 | flush_cache_page(vma, address, pte_pfn(*pte)); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 986 | entry = ptep_clear_flush_notify(vma, address, pte); |
Peter Zijlstra | c2fda5f | 2006-12-22 14:25:52 +0100 | [diff] [blame] | 987 | entry = pte_wrprotect(entry); |
| 988 | entry = pte_mkclean(entry); |
Al Viro | d6e88e6 | 2006-12-29 16:48:35 -0800 | [diff] [blame] | 989 | set_pte_at(mm, address, pte, entry); |
Peter Zijlstra | c2fda5f | 2006-12-22 14:25:52 +0100 | [diff] [blame] | 990 | ret = 1; |
| 991 | } |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 992 | |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 993 | pte_unmap_unlock(pte, ptl); |
| 994 | out: |
| 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | static int page_mkclean_file(struct address_space *mapping, struct page *page) |
| 999 | { |
| 1000 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
| 1001 | struct vm_area_struct *vma; |
| 1002 | struct prio_tree_iter iter; |
| 1003 | int ret = 0; |
| 1004 | |
| 1005 | BUG_ON(PageAnon(page)); |
| 1006 | |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1007 | mutex_lock(&mapping->i_mmap_mutex); |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1008 | vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 1009 | if (vma->vm_flags & VM_SHARED) { |
| 1010 | unsigned long address = vma_address(page, vma); |
| 1011 | if (address == -EFAULT) |
| 1012 | continue; |
| 1013 | ret += page_mkclean_one(page, vma, address); |
| 1014 | } |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1015 | } |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1016 | mutex_unlock(&mapping->i_mmap_mutex); |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | int page_mkclean(struct page *page) |
| 1021 | { |
| 1022 | int ret = 0; |
| 1023 | |
| 1024 | BUG_ON(!PageLocked(page)); |
| 1025 | |
| 1026 | if (page_mapped(page)) { |
| 1027 | struct address_space *mapping = page_mapping(page); |
Jan Kara | 89bc00f | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 1028 | if (mapping) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1029 | ret = page_mkclean_file(mapping, page); |
| 1030 | } |
| 1031 | |
| 1032 | return ret; |
| 1033 | } |
Jaya Kumar | 60b59be | 2007-05-08 00:37:37 -0700 | [diff] [blame] | 1034 | EXPORT_SYMBOL_GPL(page_mkclean); |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | /** |
Rik van Riel | c44b674 | 2010-03-05 13:42:09 -0800 | [diff] [blame] | 1037 | * page_move_anon_rmap - move a page to our anon_vma |
| 1038 | * @page: the page to move to our anon_vma |
| 1039 | * @vma: the vma the page belongs to |
| 1040 | * @address: the user virtual address mapped |
| 1041 | * |
| 1042 | * When a page belongs exclusively to one process after a COW event, |
| 1043 | * that page can be moved into the anon_vma that belongs to just that |
| 1044 | * process, so the rmap code will not search the parent or sibling |
| 1045 | * processes. |
| 1046 | */ |
| 1047 | void page_move_anon_rmap(struct page *page, |
| 1048 | struct vm_area_struct *vma, unsigned long address) |
| 1049 | { |
| 1050 | struct anon_vma *anon_vma = vma->anon_vma; |
| 1051 | |
| 1052 | VM_BUG_ON(!PageLocked(page)); |
| 1053 | VM_BUG_ON(!anon_vma); |
| 1054 | VM_BUG_ON(page->index != linear_page_index(vma, address)); |
| 1055 | |
| 1056 | anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; |
| 1057 | page->mapping = (struct address_space *) anon_vma; |
| 1058 | } |
| 1059 | |
| 1060 | /** |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1061 | * __page_set_anon_rmap - set up new anonymous rmap |
| 1062 | * @page: Page to add to rmap |
| 1063 | * @vma: VM area to add page to. |
| 1064 | * @address: User virtual address of the mapping |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1065 | * @exclusive: the page is exclusively owned by the current process |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1066 | */ |
| 1067 | static void __page_set_anon_rmap(struct page *page, |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1068 | struct vm_area_struct *vma, unsigned long address, int exclusive) |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1069 | { |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1070 | struct anon_vma *anon_vma = vma->anon_vma; |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1071 | |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1072 | BUG_ON(!anon_vma); |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1073 | |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1074 | if (PageAnon(page)) |
| 1075 | return; |
| 1076 | |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1077 | /* |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1078 | * If the page isn't exclusively mapped into this vma, |
| 1079 | * we must use the _oldest_ possible anon_vma for the |
| 1080 | * page mapping! |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1081 | */ |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1082 | if (!exclusive) |
Andrea Arcangeli | 288468c | 2010-08-09 17:19:09 -0700 | [diff] [blame] | 1083 | anon_vma = anon_vma->root; |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1084 | |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1085 | anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; |
| 1086 | page->mapping = (struct address_space *) anon_vma; |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1087 | page->index = linear_page_index(vma, address); |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /** |
Randy Dunlap | 43d8eac | 2008-03-19 17:00:43 -0700 | [diff] [blame] | 1091 | * __page_check_anon_rmap - sanity check anonymous rmap addition |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1092 | * @page: the page to add the mapping to |
| 1093 | * @vma: the vm area in which the mapping is added |
| 1094 | * @address: the user virtual address mapped |
| 1095 | */ |
| 1096 | static void __page_check_anon_rmap(struct page *page, |
| 1097 | struct vm_area_struct *vma, unsigned long address) |
| 1098 | { |
| 1099 | #ifdef CONFIG_DEBUG_VM |
| 1100 | /* |
| 1101 | * The page's anon-rmap details (mapping and index) are guaranteed to |
| 1102 | * be set up correctly at this point. |
| 1103 | * |
| 1104 | * We have exclusion against page_add_anon_rmap because the caller |
| 1105 | * always holds the page locked, except if called from page_dup_rmap, |
| 1106 | * in which case the page is already known to be setup. |
| 1107 | * |
| 1108 | * We have exclusion against page_add_new_anon_rmap because those pages |
| 1109 | * are initially only visible via the pagetables, and the pte is locked |
| 1110 | * over the call to page_add_new_anon_rmap. |
| 1111 | */ |
Andrea Arcangeli | 44ab57a | 2010-08-09 17:19:10 -0700 | [diff] [blame] | 1112 | BUG_ON(page_anon_vma(page)->root != vma->anon_vma->root); |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1113 | BUG_ON(page->index != linear_page_index(vma, address)); |
| 1114 | #endif |
| 1115 | } |
| 1116 | |
| 1117 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | * page_add_anon_rmap - add pte mapping to an anonymous page |
| 1119 | * @page: the page to add the mapping to |
| 1120 | * @vma: the vm area in which the mapping is added |
| 1121 | * @address: the user virtual address mapped |
| 1122 | * |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1123 | * The caller needs to hold the pte lock, and the page must be locked in |
Hugh Dickins | 80e1482 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1124 | * the anon_vma case: to serialize mapping,index checking after setting, |
| 1125 | * and to ensure that PageAnon is not being upgraded racily to PageKsm |
| 1126 | * (but PageKsm is never downgraded to PageAnon). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | */ |
| 1128 | void page_add_anon_rmap(struct page *page, |
| 1129 | struct vm_area_struct *vma, unsigned long address) |
| 1130 | { |
Rik van Riel | ad8c2ee | 2010-08-09 17:19:48 -0700 | [diff] [blame] | 1131 | do_page_add_anon_rmap(page, vma, address, 0); |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Special version of the above for do_swap_page, which often runs |
| 1136 | * into pages that are exclusively owned by the current process. |
| 1137 | * Everybody else should continue to use page_add_anon_rmap above. |
| 1138 | */ |
| 1139 | void do_page_add_anon_rmap(struct page *page, |
| 1140 | struct vm_area_struct *vma, unsigned long address, int exclusive) |
| 1141 | { |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1142 | int first = atomic_inc_and_test(&page->_mapcount); |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1143 | if (first) { |
| 1144 | if (!PageTransHuge(page)) |
| 1145 | __inc_zone_page_state(page, NR_ANON_PAGES); |
| 1146 | else |
| 1147 | __inc_zone_page_state(page, |
| 1148 | NR_ANON_TRANSPARENT_HUGEPAGES); |
| 1149 | } |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1150 | if (unlikely(PageKsm(page))) |
| 1151 | return; |
| 1152 | |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1153 | VM_BUG_ON(!PageLocked(page)); |
Hugh Dickins | 5dbe0af | 2011-05-28 13:17:04 -0700 | [diff] [blame] | 1154 | /* address might be in next vma when migration races vma_adjust */ |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1155 | if (first) |
Rik van Riel | ad8c2ee | 2010-08-09 17:19:48 -0700 | [diff] [blame] | 1156 | __page_set_anon_rmap(page, vma, address, exclusive); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1157 | else |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1158 | __page_check_anon_rmap(page, vma, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
Randy Dunlap | 43d8eac | 2008-03-19 17:00:43 -0700 | [diff] [blame] | 1161 | /** |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1162 | * page_add_new_anon_rmap - add pte mapping to a new anonymous page |
| 1163 | * @page: the page to add the mapping to |
| 1164 | * @vma: the vm area in which the mapping is added |
| 1165 | * @address: the user virtual address mapped |
| 1166 | * |
| 1167 | * Same as page_add_anon_rmap but must only be called on *new* pages. |
| 1168 | * This means the inc-and-test can be bypassed. |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1169 | * Page does not have to be locked. |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1170 | */ |
| 1171 | void page_add_new_anon_rmap(struct page *page, |
| 1172 | struct vm_area_struct *vma, unsigned long address) |
| 1173 | { |
Hugh Dickins | b5934c5 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 1174 | VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end); |
Hugh Dickins | cbf84b7 | 2009-01-06 14:39:27 -0800 | [diff] [blame] | 1175 | SetPageSwapBacked(page); |
| 1176 | atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */ |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1177 | if (!PageTransHuge(page)) |
| 1178 | __inc_zone_page_state(page, NR_ANON_PAGES); |
| 1179 | else |
| 1180 | __inc_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES); |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1181 | __page_set_anon_rmap(page, vma, address, 1); |
Hugh Dickins | b5934c5 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 1182 | if (page_evictable(page, vma)) |
Hugh Dickins | cbf84b7 | 2009-01-06 14:39:27 -0800 | [diff] [blame] | 1183 | lru_cache_add_lru(page, LRU_ACTIVE_ANON); |
Hugh Dickins | b5934c5 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 1184 | else |
| 1185 | add_page_to_unevictable_list(page); |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | /** |
| 1189 | * page_add_file_rmap - add pte mapping to a file page |
| 1190 | * @page: the page to add the mapping to |
| 1191 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 1192 | * The caller needs to hold the pte lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | */ |
| 1194 | void page_add_file_rmap(struct page *page) |
| 1195 | { |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1196 | bool locked; |
| 1197 | unsigned long flags; |
| 1198 | |
| 1199 | mem_cgroup_begin_update_page_stat(page, &locked, &flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1200 | if (atomic_inc_and_test(&page->_mapcount)) { |
Christoph Lameter | 65ba55f | 2006-06-30 01:55:34 -0700 | [diff] [blame] | 1201 | __inc_zone_page_state(page, NR_FILE_MAPPED); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1202 | mem_cgroup_inc_page_stat(page, MEMCG_NR_FILE_MAPPED); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1203 | } |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1204 | mem_cgroup_end_update_page_stat(page, &locked, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * page_remove_rmap - take down pte mapping from a page |
| 1209 | * @page: page to remove mapping from |
| 1210 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 1211 | * The caller needs to hold the pte lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | */ |
Hugh Dickins | edc315f | 2009-01-06 14:40:11 -0800 | [diff] [blame] | 1213 | void page_remove_rmap(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | { |
Jan Kara | 89bc00f | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 1215 | struct address_space *mapping = page_mapping(page); |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1216 | bool anon = PageAnon(page); |
| 1217 | bool locked; |
| 1218 | unsigned long flags; |
| 1219 | |
| 1220 | /* |
| 1221 | * The anon case has no mem_cgroup page_stat to update; but may |
| 1222 | * uncharge_page() below, where the lock ordering can deadlock if |
| 1223 | * we hold the lock against page_stat move: so avoid it on anon. |
| 1224 | */ |
| 1225 | if (!anon) |
| 1226 | mem_cgroup_begin_update_page_stat(page, &locked, &flags); |
| 1227 | |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1228 | /* page still mapped by someone else? */ |
| 1229 | if (!atomic_add_negative(-1, &page->_mapcount)) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1230 | goto out; |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * Now that the last pte has gone, s390 must transfer dirty |
| 1234 | * flag from storage key to struct page. We can usually skip |
| 1235 | * this if the page is anon, so about to be freed; but perhaps |
| 1236 | * not if it's in swapcache - there might be another pte slot |
| 1237 | * containing the swap entry, but page not yet written to swap. |
Jan Kara | 89bc00f | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 1238 | * |
| 1239 | * And we can skip it on file pages, so long as the filesystem |
| 1240 | * participates in dirty tracking; but need to catch shm and tmpfs |
| 1241 | * and ramfs pages which have been modified since creation by read |
| 1242 | * fault. |
| 1243 | * |
| 1244 | * Note that mapping must be decided above, before decrementing |
| 1245 | * mapcount (which luckily provides a barrier): once page is unmapped, |
| 1246 | * it could be truncated and page->mapping reset to NULL at any moment. |
| 1247 | * Note also that we are relying on page_mapping(page) to set mapping |
| 1248 | * to &swapper_space when PageSwapCache(page). |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1249 | */ |
Jan Kara | 89bc00f | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 1250 | if (mapping && !mapping_cap_account_dirty(mapping) && |
Martin Schwidefsky | 2d42552 | 2011-05-23 10:24:39 +0200 | [diff] [blame] | 1251 | page_test_and_clear_dirty(page_to_pfn(page), 1)) |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1252 | set_page_dirty(page); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1253 | /* |
| 1254 | * Hugepages are not counted in NR_ANON_PAGES nor NR_FILE_MAPPED |
| 1255 | * and not charged by memcg for now. |
| 1256 | */ |
| 1257 | if (unlikely(PageHuge(page))) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1258 | goto out; |
| 1259 | if (anon) { |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1260 | mem_cgroup_uncharge_page(page); |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1261 | if (!PageTransHuge(page)) |
| 1262 | __dec_zone_page_state(page, NR_ANON_PAGES); |
| 1263 | else |
| 1264 | __dec_zone_page_state(page, |
| 1265 | NR_ANON_TRANSPARENT_HUGEPAGES); |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1266 | } else { |
| 1267 | __dec_zone_page_state(page, NR_FILE_MAPPED); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1268 | mem_cgroup_dec_page_stat(page, MEMCG_NR_FILE_MAPPED); |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1269 | } |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1270 | /* |
| 1271 | * It would be tidy to reset the PageAnon mapping here, |
| 1272 | * but that might overwrite a racing page_add_anon_rmap |
| 1273 | * which increments mapcount after us but sets mapping |
| 1274 | * before us: so leave the reset to free_hot_cold_page, |
| 1275 | * and remember that it's only reliable while mapped. |
| 1276 | * Leaving it set also helps swapoff to reinstate ptes |
| 1277 | * faster for those pages still in swapcache. |
| 1278 | */ |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1279 | out: |
| 1280 | if (!anon) |
| 1281 | mem_cgroup_end_update_page_stat(page, &locked, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Subfunctions of try_to_unmap: try_to_unmap_one called |
Wanlong Gao | 99ef031 | 2011-10-31 17:08:51 -0700 | [diff] [blame] | 1286 | * repeatedly from try_to_unmap_ksm, try_to_unmap_anon or try_to_unmap_file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | */ |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1288 | int try_to_unmap_one(struct page *page, struct vm_area_struct *vma, |
| 1289 | unsigned long address, enum ttu_flags flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | { |
| 1291 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | pte_t *pte; |
| 1293 | pte_t pteval; |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1294 | spinlock_t *ptl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | int ret = SWAP_AGAIN; |
| 1296 | |
Nick Piggin | 479db0b | 2008-08-20 14:09:18 -0700 | [diff] [blame] | 1297 | pte = page_check_address(page, mm, address, &ptl, 0); |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1298 | if (!pte) |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 1299 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | /* |
| 1302 | * If the page is mlock()d, we cannot swap it out. |
| 1303 | * If it's recently referenced (perhaps page_referenced |
| 1304 | * skipped over this mm) then we should reactivate it. |
| 1305 | */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1306 | if (!(flags & TTU_IGNORE_MLOCK)) { |
KOSAKI Motohiro | caed0f4 | 2009-12-14 17:59:45 -0800 | [diff] [blame] | 1307 | if (vma->vm_flags & VM_LOCKED) |
| 1308 | goto out_mlock; |
| 1309 | |
Hugh Dickins | af8e335 | 2009-12-14 17:58:59 -0800 | [diff] [blame] | 1310 | if (TTU_ACTION(flags) == TTU_MUNLOCK) |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 1311 | goto out_unmap; |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1312 | } |
| 1313 | if (!(flags & TTU_IGNORE_ACCESS)) { |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1314 | if (ptep_clear_flush_young_notify(vma, address, pte)) { |
| 1315 | ret = SWAP_FAIL; |
| 1316 | goto out_unmap; |
| 1317 | } |
| 1318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | /* Nuke the page table entry. */ |
| 1321 | flush_cache_page(vma, address, page_to_pfn(page)); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1322 | pteval = ptep_clear_flush_notify(vma, address, pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
| 1324 | /* Move the dirty bit to the physical page now the pte is gone. */ |
| 1325 | if (pte_dirty(pteval)) |
| 1326 | set_page_dirty(page); |
| 1327 | |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 1328 | /* Update high watermark before we lower rss */ |
| 1329 | update_hiwater_rss(mm); |
| 1330 | |
Andi Kleen | 888b9f7 | 2009-09-16 11:50:11 +0200 | [diff] [blame] | 1331 | if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) { |
| 1332 | if (PageAnon(page)) |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1333 | dec_mm_counter(mm, MM_ANONPAGES); |
Andi Kleen | 888b9f7 | 2009-09-16 11:50:11 +0200 | [diff] [blame] | 1334 | else |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1335 | dec_mm_counter(mm, MM_FILEPAGES); |
Andi Kleen | 888b9f7 | 2009-09-16 11:50:11 +0200 | [diff] [blame] | 1336 | set_pte_at(mm, address, pte, |
| 1337 | swp_entry_to_pte(make_hwpoison_entry(page))); |
| 1338 | } else if (PageAnon(page)) { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 1339 | swp_entry_t entry = { .val = page_private(page) }; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 1340 | |
| 1341 | if (PageSwapCache(page)) { |
| 1342 | /* |
| 1343 | * Store the swap location in the pte. |
| 1344 | * See handle_pte_fault() ... |
| 1345 | */ |
Hugh Dickins | 570a335 | 2009-12-14 17:58:46 -0800 | [diff] [blame] | 1346 | if (swap_duplicate(entry) < 0) { |
| 1347 | set_pte_at(mm, address, pte, pteval); |
| 1348 | ret = SWAP_FAIL; |
| 1349 | goto out_unmap; |
| 1350 | } |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 1351 | if (list_empty(&mm->mmlist)) { |
| 1352 | spin_lock(&mmlist_lock); |
| 1353 | if (list_empty(&mm->mmlist)) |
| 1354 | list_add(&mm->mmlist, &init_mm.mmlist); |
| 1355 | spin_unlock(&mmlist_lock); |
| 1356 | } |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1357 | dec_mm_counter(mm, MM_ANONPAGES); |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 1358 | inc_mm_counter(mm, MM_SWAPENTS); |
Konstantin Khlebnikov | ce1744f | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1359 | } else if (IS_ENABLED(CONFIG_MIGRATION)) { |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 1360 | /* |
| 1361 | * Store the pfn of the page in a special migration |
| 1362 | * pte. do_swap_page() will wait until the migration |
| 1363 | * pte is removed and then restart fault handling. |
| 1364 | */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1365 | BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 1366 | entry = make_migration_entry(page, pte_write(pteval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | } |
| 1368 | set_pte_at(mm, address, pte, swp_entry_to_pte(entry)); |
| 1369 | BUG_ON(pte_file(*pte)); |
Konstantin Khlebnikov | ce1744f | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1370 | } else if (IS_ENABLED(CONFIG_MIGRATION) && |
| 1371 | (TTU_ACTION(flags) == TTU_MIGRATION)) { |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 1372 | /* Establish migration entry for a file page */ |
| 1373 | swp_entry_t entry; |
| 1374 | entry = make_migration_entry(page, pte_write(pteval)); |
| 1375 | set_pte_at(mm, address, pte, swp_entry_to_pte(entry)); |
| 1376 | } else |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1377 | dec_mm_counter(mm, MM_FILEPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Hugh Dickins | edc315f | 2009-01-06 14:40:11 -0800 | [diff] [blame] | 1379 | page_remove_rmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | page_cache_release(page); |
| 1381 | |
| 1382 | out_unmap: |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1383 | pte_unmap_unlock(pte, ptl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | out: |
| 1385 | return ret; |
KOSAKI Motohiro | caed0f4 | 2009-12-14 17:59:45 -0800 | [diff] [blame] | 1386 | |
| 1387 | out_mlock: |
| 1388 | pte_unmap_unlock(pte, ptl); |
| 1389 | |
| 1390 | |
| 1391 | /* |
| 1392 | * We need mmap_sem locking, Otherwise VM_LOCKED check makes |
| 1393 | * unstable result and race. Plus, We can't wait here because |
Peter Zijlstra | 2b575eb | 2011-05-24 17:12:11 -0700 | [diff] [blame] | 1394 | * we now hold anon_vma->mutex or mapping->i_mmap_mutex. |
KOSAKI Motohiro | caed0f4 | 2009-12-14 17:59:45 -0800 | [diff] [blame] | 1395 | * if trylock failed, the page remain in evictable lru and later |
| 1396 | * vmscan could retry to move the page to unevictable lru if the |
| 1397 | * page is actually mlocked. |
| 1398 | */ |
| 1399 | if (down_read_trylock(&vma->vm_mm->mmap_sem)) { |
| 1400 | if (vma->vm_flags & VM_LOCKED) { |
| 1401 | mlock_vma_page(page); |
| 1402 | ret = SWAP_MLOCK; |
| 1403 | } |
| 1404 | up_read(&vma->vm_mm->mmap_sem); |
| 1405 | } |
| 1406 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * objrmap doesn't work for nonlinear VMAs because the assumption that |
| 1411 | * offset-into-file correlates with offset-into-virtual-addresses does not hold. |
| 1412 | * Consequently, given a particular page and its ->index, we cannot locate the |
| 1413 | * ptes which are mapping that page without an exhaustive linear search. |
| 1414 | * |
| 1415 | * So what this code does is a mini "virtual scan" of each nonlinear VMA which |
| 1416 | * maps the file to which the target page belongs. The ->vm_private_data field |
| 1417 | * holds the current cursor into that scan. Successive searches will circulate |
| 1418 | * around the vma's virtual address space. |
| 1419 | * |
| 1420 | * So as more replacement pressure is applied to the pages in a nonlinear VMA, |
| 1421 | * more scanning pressure is placed against them as well. Eventually pages |
| 1422 | * will become fully unmapped and are eligible for eviction. |
| 1423 | * |
| 1424 | * For very sparsely populated VMAs this is a little inefficient - chances are |
| 1425 | * there there won't be many ptes located within the scan cluster. In this case |
| 1426 | * maybe we could scan further - to the end of the pte page, perhaps. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1427 | * |
| 1428 | * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can |
| 1429 | * acquire it without blocking. If vma locked, mlock the pages in the cluster, |
| 1430 | * rather than unmapping them. If we encounter the "check_page" that vmscan is |
| 1431 | * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | */ |
| 1433 | #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE) |
| 1434 | #define CLUSTER_MASK (~(CLUSTER_SIZE - 1)) |
| 1435 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1436 | static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount, |
| 1437 | struct vm_area_struct *vma, struct page *check_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | { |
| 1439 | struct mm_struct *mm = vma->vm_mm; |
| 1440 | pgd_t *pgd; |
| 1441 | pud_t *pud; |
| 1442 | pmd_t *pmd; |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1443 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | pte_t pteval; |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1445 | spinlock_t *ptl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | struct page *page; |
| 1447 | unsigned long address; |
| 1448 | unsigned long end; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1449 | int ret = SWAP_AGAIN; |
| 1450 | int locked_vma = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | address = (vma->vm_start + cursor) & CLUSTER_MASK; |
| 1453 | end = address + CLUSTER_SIZE; |
| 1454 | if (address < vma->vm_start) |
| 1455 | address = vma->vm_start; |
| 1456 | if (end > vma->vm_end) |
| 1457 | end = vma->vm_end; |
| 1458 | |
| 1459 | pgd = pgd_offset(mm, address); |
| 1460 | if (!pgd_present(*pgd)) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1461 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
| 1463 | pud = pud_offset(pgd, address); |
| 1464 | if (!pud_present(*pud)) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1465 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
| 1467 | pmd = pmd_offset(pud, address); |
| 1468 | if (!pmd_present(*pmd)) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1469 | return ret; |
| 1470 | |
| 1471 | /* |
Hugh Dickins | af8e335 | 2009-12-14 17:58:59 -0800 | [diff] [blame] | 1472 | * If we can acquire the mmap_sem for read, and vma is VM_LOCKED, |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1473 | * keep the sem while scanning the cluster for mlocking pages. |
| 1474 | */ |
Hugh Dickins | af8e335 | 2009-12-14 17:58:59 -0800 | [diff] [blame] | 1475 | if (down_read_trylock(&vma->vm_mm->mmap_sem)) { |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1476 | locked_vma = (vma->vm_flags & VM_LOCKED); |
| 1477 | if (!locked_vma) |
| 1478 | up_read(&vma->vm_mm->mmap_sem); /* don't need it */ |
| 1479 | } |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1480 | |
| 1481 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 1483 | /* Update high watermark before we lower rss */ |
| 1484 | update_hiwater_rss(mm); |
| 1485 | |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1486 | for (; address < end; pte++, address += PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | if (!pte_present(*pte)) |
| 1488 | continue; |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 1489 | page = vm_normal_page(vma, address, *pte); |
| 1490 | BUG_ON(!page || PageAnon(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1492 | if (locked_vma) { |
Vlastimil Babka | 52dd5cb | 2014-04-07 15:37:50 -0700 | [diff] [blame] | 1493 | if (page == check_page) { |
| 1494 | /* we know we have check_page locked */ |
| 1495 | mlock_vma_page(page); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1496 | ret = SWAP_MLOCK; |
Vlastimil Babka | 52dd5cb | 2014-04-07 15:37:50 -0700 | [diff] [blame] | 1497 | } else if (trylock_page(page)) { |
| 1498 | /* |
| 1499 | * If we can lock the page, perform mlock. |
| 1500 | * Otherwise leave the page alone, it will be |
| 1501 | * eventually encountered again later. |
| 1502 | */ |
| 1503 | mlock_vma_page(page); |
| 1504 | unlock_page(page); |
| 1505 | } |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1506 | continue; /* don't unmap */ |
| 1507 | } |
| 1508 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1509 | if (ptep_clear_flush_young_notify(vma, address, pte)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | continue; |
| 1511 | |
| 1512 | /* Nuke the page table entry. */ |
Ben Collins | eca3513 | 2005-11-29 11:45:26 -0800 | [diff] [blame] | 1513 | flush_cache_page(vma, address, pte_pfn(*pte)); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1514 | pteval = ptep_clear_flush_notify(vma, address, pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
| 1516 | /* If nonlinear, store the file page offset in the pte. */ |
| 1517 | if (page->index != linear_page_index(vma, address)) |
| 1518 | set_pte_at(mm, address, pte, pgoff_to_pte(page->index)); |
| 1519 | |
| 1520 | /* Move the dirty bit to the physical page now the pte is gone. */ |
| 1521 | if (pte_dirty(pteval)) |
| 1522 | set_page_dirty(page); |
| 1523 | |
Hugh Dickins | edc315f | 2009-01-06 14:40:11 -0800 | [diff] [blame] | 1524 | page_remove_rmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | page_cache_release(page); |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1526 | dec_mm_counter(mm, MM_FILEPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | (*mapcount)--; |
| 1528 | } |
Hugh Dickins | c071880 | 2005-10-29 18:16:31 -0700 | [diff] [blame] | 1529 | pte_unmap_unlock(pte - 1, ptl); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1530 | if (locked_vma) |
| 1531 | up_read(&vma->vm_mm->mmap_sem); |
| 1532 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1535 | bool is_vma_temporary_stack(struct vm_area_struct *vma) |
Mel Gorman | a8bef8ff | 2010-05-24 14:32:24 -0700 | [diff] [blame] | 1536 | { |
| 1537 | int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP); |
| 1538 | |
| 1539 | if (!maybe_stack) |
| 1540 | return false; |
| 1541 | |
| 1542 | if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) == |
| 1543 | VM_STACK_INCOMPLETE_SETUP) |
| 1544 | return true; |
| 1545 | |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1549 | /** |
| 1550 | * try_to_unmap_anon - unmap or unlock anonymous page using the object-based |
| 1551 | * rmap method |
| 1552 | * @page: the page to unmap/unlock |
Huang Shijie | 8051be5 | 2009-12-14 17:58:50 -0800 | [diff] [blame] | 1553 | * @flags: action and flags |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1554 | * |
| 1555 | * Find all the mappings of a page using the mapping pointer and the vma chains |
| 1556 | * contained in the anon_vma struct it points to. |
| 1557 | * |
| 1558 | * This function is only called from try_to_unmap/try_to_munlock for |
| 1559 | * anonymous pages. |
| 1560 | * When called from try_to_munlock(), the mmap_sem of the mm containing the vma |
| 1561 | * where the page was found will be held for write. So, we won't recheck |
| 1562 | * vm_flags for that VMA. That should be OK, because that vma shouldn't be |
| 1563 | * 'LOCKED. |
| 1564 | */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1565 | static int try_to_unmap_anon(struct page *page, enum ttu_flags flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | { |
| 1567 | struct anon_vma *anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1568 | struct anon_vma_chain *avc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | int ret = SWAP_AGAIN; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | anon_vma = page_lock_anon_vma(page); |
| 1572 | if (!anon_vma) |
| 1573 | return ret; |
| 1574 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1575 | list_for_each_entry(avc, &anon_vma->head, same_anon_vma) { |
| 1576 | struct vm_area_struct *vma = avc->vma; |
Mel Gorman | a8bef8ff | 2010-05-24 14:32:24 -0700 | [diff] [blame] | 1577 | unsigned long address; |
| 1578 | |
| 1579 | /* |
| 1580 | * During exec, a temporary VMA is setup and later moved. |
| 1581 | * The VMA is moved under the anon_vma lock but not the |
| 1582 | * page tables leading to a race where migration cannot |
| 1583 | * find the migration ptes. Rather than increasing the |
| 1584 | * locking requirements of exec(), migration skips |
| 1585 | * temporary VMAs until after exec() completes. |
| 1586 | */ |
Konstantin Khlebnikov | ce1744f | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1587 | if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION) && |
Mel Gorman | a8bef8ff | 2010-05-24 14:32:24 -0700 | [diff] [blame] | 1588 | is_vma_temporary_stack(vma)) |
| 1589 | continue; |
| 1590 | |
| 1591 | address = vma_address(page, vma); |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 1592 | if (address == -EFAULT) |
| 1593 | continue; |
| 1594 | ret = try_to_unmap_one(page, vma, address, flags); |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 1595 | if (ret != SWAP_AGAIN || !page_mapped(page)) |
| 1596 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 1598 | |
| 1599 | page_unlock_anon_vma(anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | return ret; |
| 1601 | } |
| 1602 | |
| 1603 | /** |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1604 | * try_to_unmap_file - unmap/unlock file page using the object-based rmap method |
| 1605 | * @page: the page to unmap/unlock |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1606 | * @flags: action and flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | * |
| 1608 | * Find all the mappings of a page using the mapping pointer and the vma chains |
| 1609 | * contained in the address_space struct it points to. |
| 1610 | * |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1611 | * This function is only called from try_to_unmap/try_to_munlock for |
| 1612 | * object-based pages. |
| 1613 | * When called from try_to_munlock(), the mmap_sem of the mm containing the vma |
| 1614 | * where the page was found will be held for write. So, we won't recheck |
| 1615 | * vm_flags for that VMA. That should be OK, because that vma shouldn't be |
| 1616 | * 'LOCKED. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1618 | static int try_to_unmap_file(struct page *page, enum ttu_flags flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | { |
| 1620 | struct address_space *mapping = page->mapping; |
| 1621 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
| 1622 | struct vm_area_struct *vma; |
| 1623 | struct prio_tree_iter iter; |
| 1624 | int ret = SWAP_AGAIN; |
| 1625 | unsigned long cursor; |
| 1626 | unsigned long max_nl_cursor = 0; |
| 1627 | unsigned long max_nl_size = 0; |
| 1628 | unsigned int mapcount; |
| 1629 | |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1630 | mutex_lock(&mapping->i_mmap_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
Hugh Dickins | 1cb1729 | 2009-12-14 17:59:01 -0800 | [diff] [blame] | 1632 | unsigned long address = vma_address(page, vma); |
| 1633 | if (address == -EFAULT) |
| 1634 | continue; |
| 1635 | ret = try_to_unmap_one(page, vma, address, flags); |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 1636 | if (ret != SWAP_AGAIN || !page_mapped(page)) |
| 1637 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | if (list_empty(&mapping->i_mmap_nonlinear)) |
| 1641 | goto out; |
| 1642 | |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 1643 | /* |
| 1644 | * We don't bother to try to find the munlocked page in nonlinears. |
| 1645 | * It's costly. Instead, later, page reclaim logic may call |
| 1646 | * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily. |
| 1647 | */ |
| 1648 | if (TTU_ACTION(flags) == TTU_MUNLOCK) |
| 1649 | goto out; |
| 1650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | list_for_each_entry(vma, &mapping->i_mmap_nonlinear, |
| 1652 | shared.vm_set.list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | cursor = (unsigned long) vma->vm_private_data; |
| 1654 | if (cursor > max_nl_cursor) |
| 1655 | max_nl_cursor = cursor; |
| 1656 | cursor = vma->vm_end - vma->vm_start; |
| 1657 | if (cursor > max_nl_size) |
| 1658 | max_nl_size = cursor; |
| 1659 | } |
| 1660 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1661 | if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | ret = SWAP_FAIL; |
| 1663 | goto out; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * We don't try to search for this page in the nonlinear vmas, |
| 1668 | * and page_referenced wouldn't have found it anyway. Instead |
| 1669 | * just walk the nonlinear vmas trying to age and unmap some. |
| 1670 | * The mapcount of the page we came in with is irrelevant, |
| 1671 | * but even so use it as a guide to how hard we should try? |
| 1672 | */ |
| 1673 | mapcount = page_mapcount(page); |
| 1674 | if (!mapcount) |
| 1675 | goto out; |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1676 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | |
| 1678 | max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK; |
| 1679 | if (max_nl_cursor == 0) |
| 1680 | max_nl_cursor = CLUSTER_SIZE; |
| 1681 | |
| 1682 | do { |
| 1683 | list_for_each_entry(vma, &mapping->i_mmap_nonlinear, |
| 1684 | shared.vm_set.list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | cursor = (unsigned long) vma->vm_private_data; |
Hugh Dickins | 839b968 | 2005-09-03 15:54:43 -0700 | [diff] [blame] | 1686 | while ( cursor < max_nl_cursor && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | cursor < vma->vm_end - vma->vm_start) { |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 1688 | if (try_to_unmap_cluster(cursor, &mapcount, |
| 1689 | vma, page) == SWAP_MLOCK) |
| 1690 | ret = SWAP_MLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | cursor += CLUSTER_SIZE; |
| 1692 | vma->vm_private_data = (void *) cursor; |
| 1693 | if ((int)mapcount <= 0) |
| 1694 | goto out; |
| 1695 | } |
| 1696 | vma->vm_private_data = (void *) max_nl_cursor; |
| 1697 | } |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1698 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | max_nl_cursor += CLUSTER_SIZE; |
| 1700 | } while (max_nl_cursor <= max_nl_size); |
| 1701 | |
| 1702 | /* |
| 1703 | * Don't loop forever (perhaps all the remaining pages are |
| 1704 | * in locked vmas). Reset cursor on all unreserved nonlinear |
| 1705 | * vmas, now forgetting on which ones it had fallen behind. |
| 1706 | */ |
Hugh Dickins | 101d2be | 2005-11-21 21:32:16 -0800 | [diff] [blame] | 1707 | list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) |
| 1708 | vma->vm_private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | out: |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1710 | mutex_unlock(&mapping->i_mmap_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | return ret; |
| 1712 | } |
| 1713 | |
| 1714 | /** |
| 1715 | * try_to_unmap - try to remove all page table mappings to a page |
| 1716 | * @page: the page to get unmapped |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1717 | * @flags: action and flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | * |
| 1719 | * Tries to remove all the page table entries which are mapping this |
| 1720 | * page, used in the pageout path. Caller must hold the page lock. |
| 1721 | * Return values are: |
| 1722 | * |
| 1723 | * SWAP_SUCCESS - we succeeded in removing all mappings |
| 1724 | * SWAP_AGAIN - we missed a mapping, try again later |
| 1725 | * SWAP_FAIL - the page is unswappable |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1726 | * SWAP_MLOCK - page is mlocked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1728 | int try_to_unmap(struct page *page, enum ttu_flags flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | { |
| 1730 | int ret; |
| 1731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | BUG_ON(!PageLocked(page)); |
Andrea Arcangeli | 91600e9 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 1733 | VM_BUG_ON(!PageHuge(page) && PageTransHuge(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1735 | if (unlikely(PageKsm(page))) |
| 1736 | ret = try_to_unmap_ksm(page, flags); |
| 1737 | else if (PageAnon(page)) |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1738 | ret = try_to_unmap_anon(page, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | else |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1740 | ret = try_to_unmap_file(page, flags); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1741 | if (ret != SWAP_MLOCK && !page_mapped(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | ret = SWAP_SUCCESS; |
| 1743 | return ret; |
| 1744 | } |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 1745 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1746 | /** |
| 1747 | * try_to_munlock - try to munlock a page |
| 1748 | * @page: the page to be munlocked |
| 1749 | * |
| 1750 | * Called from munlock code. Checks all of the VMAs mapping the page |
| 1751 | * to make sure nobody else has this page mlocked. The page will be |
| 1752 | * returned with PG_mlocked cleared if no other vmas have it mlocked. |
| 1753 | * |
| 1754 | * Return values are: |
| 1755 | * |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 1756 | * SWAP_AGAIN - no vma is holding page mlocked, or, |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1757 | * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1758 | * SWAP_FAIL - page cannot be located at present |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1759 | * SWAP_MLOCK - page is now mlocked. |
| 1760 | */ |
| 1761 | int try_to_munlock(struct page *page) |
| 1762 | { |
| 1763 | VM_BUG_ON(!PageLocked(page) || PageLRU(page)); |
| 1764 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1765 | if (unlikely(PageKsm(page))) |
| 1766 | return try_to_unmap_ksm(page, TTU_MUNLOCK); |
| 1767 | else if (PageAnon(page)) |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1768 | return try_to_unmap_anon(page, TTU_MUNLOCK); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1769 | else |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1770 | return try_to_unmap_file(page, TTU_MUNLOCK); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 1771 | } |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1772 | |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 1773 | void __put_anon_vma(struct anon_vma *anon_vma) |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1774 | { |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 1775 | struct anon_vma *root = anon_vma->root; |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1776 | |
Andrey Ryabinin | 425216b | 2014-06-06 19:09:30 +0400 | [diff] [blame] | 1777 | anon_vma_free(anon_vma); |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 1778 | if (root != anon_vma && atomic_dec_and_test(&root->refcount)) |
| 1779 | anon_vma_free(root); |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1780 | } |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1781 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1782 | #ifdef CONFIG_MIGRATION |
| 1783 | /* |
| 1784 | * rmap_walk() and its helpers rmap_walk_anon() and rmap_walk_file(): |
| 1785 | * Called by migrate.c to remove migration ptes, but might be used more later. |
| 1786 | */ |
| 1787 | static int rmap_walk_anon(struct page *page, int (*rmap_one)(struct page *, |
| 1788 | struct vm_area_struct *, unsigned long, void *), void *arg) |
| 1789 | { |
| 1790 | struct anon_vma *anon_vma; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1791 | struct anon_vma_chain *avc; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1792 | int ret = SWAP_AGAIN; |
| 1793 | |
| 1794 | /* |
| 1795 | * Note: remove_migration_ptes() cannot use page_lock_anon_vma() |
| 1796 | * because that depends on page_mapped(); but not all its usages |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1797 | * are holding mmap_sem. Users without mmap_sem are required to |
| 1798 | * take a reference count to prevent the anon_vma disappearing |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1799 | */ |
| 1800 | anon_vma = page_anon_vma(page); |
| 1801 | if (!anon_vma) |
| 1802 | return ret; |
Rik van Riel | cba48b9 | 2010-08-09 17:18:38 -0700 | [diff] [blame] | 1803 | anon_vma_lock(anon_vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1804 | list_for_each_entry(avc, &anon_vma->head, same_anon_vma) { |
| 1805 | struct vm_area_struct *vma = avc->vma; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1806 | unsigned long address = vma_address(page, vma); |
| 1807 | if (address == -EFAULT) |
| 1808 | continue; |
| 1809 | ret = rmap_one(page, vma, address, arg); |
| 1810 | if (ret != SWAP_AGAIN) |
| 1811 | break; |
| 1812 | } |
Rik van Riel | cba48b9 | 2010-08-09 17:18:38 -0700 | [diff] [blame] | 1813 | anon_vma_unlock(anon_vma); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1814 | return ret; |
| 1815 | } |
| 1816 | |
| 1817 | static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *, |
| 1818 | struct vm_area_struct *, unsigned long, void *), void *arg) |
| 1819 | { |
| 1820 | struct address_space *mapping = page->mapping; |
| 1821 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
| 1822 | struct vm_area_struct *vma; |
| 1823 | struct prio_tree_iter iter; |
| 1824 | int ret = SWAP_AGAIN; |
| 1825 | |
| 1826 | if (!mapping) |
| 1827 | return ret; |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1828 | mutex_lock(&mapping->i_mmap_mutex); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1829 | vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
| 1830 | unsigned long address = vma_address(page, vma); |
| 1831 | if (address == -EFAULT) |
| 1832 | continue; |
| 1833 | ret = rmap_one(page, vma, address, arg); |
| 1834 | if (ret != SWAP_AGAIN) |
| 1835 | break; |
| 1836 | } |
| 1837 | /* |
| 1838 | * No nonlinear handling: being always shared, nonlinear vmas |
| 1839 | * never contain migration ptes. Decide what to do about this |
| 1840 | * limitation to linear when we need rmap_walk() on nonlinear. |
| 1841 | */ |
Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 1842 | mutex_unlock(&mapping->i_mmap_mutex); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 1843 | return ret; |
| 1844 | } |
| 1845 | |
| 1846 | int rmap_walk(struct page *page, int (*rmap_one)(struct page *, |
| 1847 | struct vm_area_struct *, unsigned long, void *), void *arg) |
| 1848 | { |
| 1849 | VM_BUG_ON(!PageLocked(page)); |
| 1850 | |
| 1851 | if (unlikely(PageKsm(page))) |
| 1852 | return rmap_walk_ksm(page, rmap_one, arg); |
| 1853 | else if (PageAnon(page)) |
| 1854 | return rmap_walk_anon(page, rmap_one, arg); |
| 1855 | else |
| 1856 | return rmap_walk_file(page, rmap_one, arg); |
| 1857 | } |
| 1858 | #endif /* CONFIG_MIGRATION */ |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1859 | |
Naoya Horiguchi | e3390f6 | 2010-06-15 13:18:13 +0900 | [diff] [blame] | 1860 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1861 | /* |
| 1862 | * The following three functions are for anonymous (private mapped) hugepages. |
| 1863 | * Unlike common anonymous pages, anonymous hugepages have no accounting code |
| 1864 | * and no lru code, because we handle hugepages differently from common pages. |
| 1865 | */ |
| 1866 | static void __hugepage_set_anon_rmap(struct page *page, |
| 1867 | struct vm_area_struct *vma, unsigned long address, int exclusive) |
| 1868 | { |
| 1869 | struct anon_vma *anon_vma = vma->anon_vma; |
Naoya Horiguchi | 433abed | 2010-09-10 13:23:03 +0900 | [diff] [blame] | 1870 | |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1871 | BUG_ON(!anon_vma); |
Naoya Horiguchi | 433abed | 2010-09-10 13:23:03 +0900 | [diff] [blame] | 1872 | |
| 1873 | if (PageAnon(page)) |
| 1874 | return; |
| 1875 | if (!exclusive) |
| 1876 | anon_vma = anon_vma->root; |
| 1877 | |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1878 | anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; |
| 1879 | page->mapping = (struct address_space *) anon_vma; |
| 1880 | page->index = linear_page_index(vma, address); |
| 1881 | } |
| 1882 | |
| 1883 | void hugepage_add_anon_rmap(struct page *page, |
| 1884 | struct vm_area_struct *vma, unsigned long address) |
| 1885 | { |
| 1886 | struct anon_vma *anon_vma = vma->anon_vma; |
| 1887 | int first; |
Naoya Horiguchi | a850ea3 | 2010-09-10 13:23:06 +0900 | [diff] [blame] | 1888 | |
| 1889 | BUG_ON(!PageLocked(page)); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1890 | BUG_ON(!anon_vma); |
Hugh Dickins | 5dbe0af | 2011-05-28 13:17:04 -0700 | [diff] [blame] | 1891 | /* address might be in next vma when migration races vma_adjust */ |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1892 | first = atomic_inc_and_test(&page->_mapcount); |
| 1893 | if (first) |
| 1894 | __hugepage_set_anon_rmap(page, vma, address, 0); |
| 1895 | } |
| 1896 | |
| 1897 | void hugepage_add_new_anon_rmap(struct page *page, |
| 1898 | struct vm_area_struct *vma, unsigned long address) |
| 1899 | { |
| 1900 | BUG_ON(address < vma->vm_start || address >= vma->vm_end); |
| 1901 | atomic_set(&page->_mapcount, 0); |
| 1902 | __hugepage_set_anon_rmap(page, vma, address, 1); |
| 1903 | } |
Naoya Horiguchi | e3390f6 | 2010-06-15 13:18:13 +0900 | [diff] [blame] | 1904 | #endif /* CONFIG_HUGETLB_PAGE */ |