blob: 869aaa3206a27fe297a306a64af7aaa8e5dcdd0e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dickins98f32602009-05-21 20:33:58 +010017 * Contributions by Hugh Dickins 2003, 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20/*
21 * Lock ordering in mm:
22 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080023 * inode->i_mutex (while writing or truncating, not reading or faulting)
Nick Piggin82591e62006-10-19 23:29:10 -070024 * inode->i_alloc_sem (vmtruncate_range)
25 * mm->mmap_sem
26 * page->flags PG_locked (lock_page)
27 * mapping->i_mmap_lock
28 * anon_vma->lock
29 * mm->page_table_lock or pte_lock
30 * zone->lru_lock (in mark_page_accessed, isolate_lru_page)
31 * swap_lock (in swap_duplicate, swap_info_get)
32 * mmlist_lock (in mmput, drain_mmlist and others)
33 * mapping->private_lock (in __set_page_dirty_buffers)
34 * inode_lock (in set_page_dirty's __mark_inode_dirty)
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,
38 * within inode_lock in __sync_single_inode)
Andi Kleen6a460792009-09-16 11:50:15 +020039 *
40 * (code doesn't rely on that order so it could be switched around)
41 * ->tasklist_lock
42 * anon_vma->lock (memory_failure, collect_procs_anon)
43 * pte map lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
46#include <linux/mm.h>
47#include <linux/pagemap.h>
48#include <linux/swap.h>
49#include <linux/swapops.h>
50#include <linux/slab.h>
51#include <linux/init.h>
Hugh Dickins5ad64682009-12-14 17:59:24 -080052#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/rmap.h>
54#include <linux/rcupdate.h>
Christoph Lametera48d07a2006-02-01 03:05:38 -080055#include <linux/module.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080056#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070057#include <linux/mmu_notifier.h>
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -080058#include <linux/migrate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <asm/tlbflush.h>
61
Nick Pigginb291f002008-10-18 20:26:44 -070062#include "internal.h"
63
Adrian Bunkfdd2e5f2008-10-18 20:28:38 -070064static struct kmem_cache *anon_vma_cachep;
65
66static inline struct anon_vma *anon_vma_alloc(void)
67{
68 return kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
69}
70
71static inline void anon_vma_free(struct anon_vma *anon_vma)
72{
73 kmem_cache_free(anon_vma_cachep, anon_vma);
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070076/**
77 * anon_vma_prepare - attach an anon_vma to a memory region
78 * @vma: the memory region in question
79 *
80 * This makes sure the memory mapping described by 'vma' has
81 * an 'anon_vma' attached to it, so that we can associate the
82 * anonymous pages mapped into it with that anon_vma.
83 *
84 * The common case will be that we already have one, but if
85 * if not we either need to find an adjacent mapping that we
86 * can re-use the anon_vma from (very common when the only
87 * reason for splitting a vma has been mprotect()), or we
88 * allocate a new one.
89 *
90 * Anon-vma allocations are very subtle, because we may have
91 * optimistically looked up an anon_vma in page_lock_anon_vma()
92 * and that may actually touch the spinlock even in the newly
93 * allocated vma (it depends on RCU to make sure that the
94 * anon_vma isn't actually destroyed).
95 *
96 * As a result, we need to do proper anon_vma locking even
97 * for the new allocation. At the same time, we do not want
98 * to do any locking for the common case of already having
99 * an anon_vma.
100 *
101 * This must be called with the mmap_sem held for reading.
102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103int anon_vma_prepare(struct vm_area_struct *vma)
104{
105 struct anon_vma *anon_vma = vma->anon_vma;
106
107 might_sleep();
108 if (unlikely(!anon_vma)) {
109 struct mm_struct *mm = vma->vm_mm;
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700110 struct anon_vma *allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 anon_vma = find_mergeable_anon_vma(vma);
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700113 allocated = NULL;
114 if (!anon_vma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 anon_vma = anon_vma_alloc();
116 if (unlikely(!anon_vma))
117 return -ENOMEM;
118 allocated = anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700120 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /* page_table_lock to protect against threads */
123 spin_lock(&mm->page_table_lock);
124 if (likely(!vma->anon_vma)) {
125 vma->anon_vma = anon_vma;
Christoph Lameter06972122006-06-23 02:03:35 -0700126 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 allocated = NULL;
128 }
129 spin_unlock(&mm->page_table_lock);
130
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700131 spin_unlock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (unlikely(allocated))
133 anon_vma_free(allocated);
134 }
135 return 0;
136}
137
138void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
139{
140 BUG_ON(vma->anon_vma != next->anon_vma);
141 list_del(&next->anon_vma_node);
142}
143
144void __anon_vma_link(struct vm_area_struct *vma)
145{
146 struct anon_vma *anon_vma = vma->anon_vma;
147
Hugh Dickins30acbab2007-06-27 14:09:53 -0700148 if (anon_vma)
Christoph Lameter06972122006-06-23 02:03:35 -0700149 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152void anon_vma_link(struct vm_area_struct *vma)
153{
154 struct anon_vma *anon_vma = vma->anon_vma;
155
156 if (anon_vma) {
157 spin_lock(&anon_vma->lock);
Christoph Lameter06972122006-06-23 02:03:35 -0700158 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_unlock(&anon_vma->lock);
160 }
161}
162
163void anon_vma_unlink(struct vm_area_struct *vma)
164{
165 struct anon_vma *anon_vma = vma->anon_vma;
166 int empty;
167
168 if (!anon_vma)
169 return;
170
171 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 list_del(&vma->anon_vma_node);
173
174 /* We must garbage collect the anon_vma if it's empty */
175 empty = list_empty(&anon_vma->head);
176 spin_unlock(&anon_vma->lock);
177
178 if (empty)
179 anon_vma_free(anon_vma);
180}
181
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700182static void anon_vma_ctor(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Christoph Lametera35afb82007-05-16 22:10:57 -0700184 struct anon_vma *anon_vma = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Christoph Lametera35afb82007-05-16 22:10:57 -0700186 spin_lock_init(&anon_vma->lock);
187 INIT_LIST_HEAD(&anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190void __init anon_vma_init(void)
191{
192 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
Paul Mundt20c2df82007-07-20 10:11:58 +0900193 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*
197 * Getting a lock on a stable anon_vma from a page off the LRU is
198 * tricky: page_lock_anon_vma rely on RCU to guard against the races.
199 */
Andi Kleen10be22d2009-09-16 11:50:04 +0200200struct anon_vma *page_lock_anon_vma(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800202 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned long anon_mapping;
204
205 rcu_read_lock();
206 anon_mapping = (unsigned long) page->mapping;
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -0800207 if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto out;
209 if (!page_mapped(page))
210 goto out;
211
212 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
213 spin_lock(&anon_vma->lock);
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800214 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215out:
216 rcu_read_unlock();
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800217 return NULL;
218}
219
Andi Kleen10be22d2009-09-16 11:50:04 +0200220void page_unlock_anon_vma(struct anon_vma *anon_vma)
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800221{
222 spin_unlock(&anon_vma->lock);
223 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/*
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800227 * At what user virtual address is page expected in @vma?
228 * Returns virtual address or -EFAULT if page's index/offset is not
229 * within the range mapped the @vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231static inline unsigned long
232vma_address(struct page *page, struct vm_area_struct *vma)
233{
234 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
235 unsigned long address;
236
237 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
238 if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800239 /* page should be within @vma mapping range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EFAULT;
241 }
242 return address;
243}
244
245/*
Huang Shijiebf89c8c2009-10-01 15:44:04 -0700246 * At what user virtual address is page expected in vma?
247 * checking that the page matches the vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
250{
251 if (PageAnon(page)) {
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -0800252 if (vma->anon_vma != page_anon_vma(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EFAULT;
254 } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800255 if (!vma->vm_file ||
256 vma->vm_file->f_mapping != page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -EFAULT;
258 } else
259 return -EFAULT;
260 return vma_address(page, vma);
261}
262
263/*
Nikita Danilov81b40822005-05-01 08:58:36 -0700264 * Check that @page is mapped at @address into @mm.
265 *
Nick Piggin479db0b2008-08-20 14:09:18 -0700266 * If @sync is false, page_check_address may perform a racy check to avoid
267 * the page table lock when the pte is not present (helpful when reclaiming
268 * highly shared pages).
269 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700270 * On success returns with pte mapped and locked.
Nikita Danilov81b40822005-05-01 08:58:36 -0700271 */
Carsten Otteceffc072005-06-23 22:05:25 -0700272pte_t *page_check_address(struct page *page, struct mm_struct *mm,
Nick Piggin479db0b2008-08-20 14:09:18 -0700273 unsigned long address, spinlock_t **ptlp, int sync)
Nikita Danilov81b40822005-05-01 08:58:36 -0700274{
275 pgd_t *pgd;
276 pud_t *pud;
277 pmd_t *pmd;
278 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700279 spinlock_t *ptl;
Nikita Danilov81b40822005-05-01 08:58:36 -0700280
Nikita Danilov81b40822005-05-01 08:58:36 -0700281 pgd = pgd_offset(mm, address);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700282 if (!pgd_present(*pgd))
283 return NULL;
284
285 pud = pud_offset(pgd, address);
286 if (!pud_present(*pud))
287 return NULL;
288
289 pmd = pmd_offset(pud, address);
290 if (!pmd_present(*pmd))
291 return NULL;
292
293 pte = pte_offset_map(pmd, address);
294 /* Make a quick check before getting the lock */
Nick Piggin479db0b2008-08-20 14:09:18 -0700295 if (!sync && !pte_present(*pte)) {
Hugh Dickinsc0718802005-10-29 18:16:31 -0700296 pte_unmap(pte);
297 return NULL;
Nikita Danilov81b40822005-05-01 08:58:36 -0700298 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700299
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700300 ptl = pte_lockptr(mm, pmd);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700301 spin_lock(ptl);
302 if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
303 *ptlp = ptl;
304 return pte;
305 }
306 pte_unmap_unlock(pte, ptl);
307 return NULL;
Nikita Danilov81b40822005-05-01 08:58:36 -0700308}
309
Nick Pigginb291f002008-10-18 20:26:44 -0700310/**
311 * page_mapped_in_vma - check whether a page is really mapped in a VMA
312 * @page: the page to test
313 * @vma: the VMA to test
314 *
315 * Returns 1 if the page is mapped into the page tables of the VMA, 0
316 * if the page is not mapped into the page tables of this VMA. Only
317 * valid for normal file or anonymous VMAs.
318 */
Andi Kleen6a460792009-09-16 11:50:15 +0200319int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
Nick Pigginb291f002008-10-18 20:26:44 -0700320{
321 unsigned long address;
322 pte_t *pte;
323 spinlock_t *ptl;
324
325 address = vma_address(page, vma);
326 if (address == -EFAULT) /* out of vma range */
327 return 0;
328 pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
329 if (!pte) /* the page is not in this mm */
330 return 0;
331 pte_unmap_unlock(pte, ptl);
332
333 return 1;
334}
335
Nikita Danilov81b40822005-05-01 08:58:36 -0700336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Subfunctions of page_referenced: page_referenced_one called
338 * repeatedly from either page_referenced_anon or page_referenced_file.
339 */
Hugh Dickins5ad64682009-12-14 17:59:24 -0800340int page_referenced_one(struct page *page, struct vm_area_struct *vma,
341 unsigned long address, unsigned int *mapcount,
342 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700346 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int referenced = 0;
348
Nick Piggin479db0b2008-08-20 14:09:18 -0700349 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700350 if (!pte)
351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Nick Pigginb291f002008-10-18 20:26:44 -0700353 /*
354 * Don't want to elevate referenced for mlocked page that gets this far,
355 * in order that it progresses to try_to_unmap and is moved to the
356 * unevictable list.
357 */
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800358 if (vma->vm_flags & VM_LOCKED) {
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800359 *mapcount = 1; /* break early from loop */
Minchan Kim03ef83a2009-08-26 14:29:23 -0700360 *vm_flags |= VM_LOCKED;
Nick Pigginb291f002008-10-18 20:26:44 -0700361 goto out_unmap;
362 }
363
Johannes Weiner4917e5d2009-01-06 14:39:17 -0800364 if (ptep_clear_flush_young_notify(vma, address, pte)) {
365 /*
366 * Don't treat a reference through a sequentially read
367 * mapping as such. If the page has been used in
368 * another mapping, we will catch it; if this other
369 * mapping is already gone, the unmap path will have
370 * set PG_referenced or activated the page.
371 */
372 if (likely(!VM_SequentialReadHint(vma)))
373 referenced++;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Hugh Dickinsc0718802005-10-29 18:16:31 -0700376 /* Pretend the page is referenced if the task has the
377 swap token and is in the middle of a page fault. */
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800378 if (mm != current->mm && has_swap_token(mm) &&
Hugh Dickinsc0718802005-10-29 18:16:31 -0700379 rwsem_is_locked(&mm->mmap_sem))
380 referenced++;
381
Nick Pigginb291f002008-10-18 20:26:44 -0700382out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700383 (*mapcount)--;
384 pte_unmap_unlock(pte, ptl);
Huang Shijie273f0472009-12-14 17:58:51 -0800385
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700386 if (referenced)
387 *vm_flags |= vma->vm_flags;
Huang Shijie273f0472009-12-14 17:58:51 -0800388out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return referenced;
390}
391
Balbir Singhbed71612008-02-07 00:14:01 -0800392static int page_referenced_anon(struct page *page,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700393 struct mem_cgroup *mem_cont,
394 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 unsigned int mapcount;
397 struct anon_vma *anon_vma;
398 struct vm_area_struct *vma;
399 int referenced = 0;
400
401 anon_vma = page_lock_anon_vma(page);
402 if (!anon_vma)
403 return referenced;
404
405 mapcount = page_mapcount(page);
406 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Hugh Dickins1cb17292009-12-14 17:59:01 -0800407 unsigned long address = vma_address(page, vma);
408 if (address == -EFAULT)
409 continue;
Balbir Singhbed71612008-02-07 00:14:01 -0800410 /*
411 * If we are reclaiming on behalf of a cgroup, skip
412 * counting on behalf of references from different
413 * cgroups
414 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800415 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800416 continue;
Hugh Dickins1cb17292009-12-14 17:59:01 -0800417 referenced += page_referenced_one(page, vma, address,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700418 &mapcount, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!mapcount)
420 break;
421 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800422
423 page_unlock_anon_vma(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return referenced;
425}
426
427/**
428 * page_referenced_file - referenced check for object-based rmap
429 * @page: the page we're checking references on.
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700430 * @mem_cont: target memory controller
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700431 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
433 * For an object-based mapped page, find all the places it is mapped and
434 * check/clear the referenced flag. This is done by following the page->mapping
435 * pointer, then walking the chain of vmas it holds. It returns the number
436 * of references it found.
437 *
438 * This function is only called from page_referenced for object-based pages.
439 */
Balbir Singhbed71612008-02-07 00:14:01 -0800440static int page_referenced_file(struct page *page,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700441 struct mem_cgroup *mem_cont,
442 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 unsigned int mapcount;
445 struct address_space *mapping = page->mapping;
446 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
447 struct vm_area_struct *vma;
448 struct prio_tree_iter iter;
449 int referenced = 0;
450
451 /*
452 * The caller's checks on page->mapping and !PageAnon have made
453 * sure that this is a file page: the check for page->mapping
454 * excludes the case just before it gets set on an anon page.
455 */
456 BUG_ON(PageAnon(page));
457
458 /*
459 * The page lock not only makes sure that page->mapping cannot
460 * suddenly be NULLified by truncation, it makes sure that the
461 * structure at mapping cannot be freed and reused yet,
462 * so we can safely take mapping->i_mmap_lock.
463 */
464 BUG_ON(!PageLocked(page));
465
466 spin_lock(&mapping->i_mmap_lock);
467
468 /*
469 * i_mmap_lock does not stabilize mapcount at all, but mapcount
470 * is more likely to be accurate if we note it after spinning.
471 */
472 mapcount = page_mapcount(page);
473
474 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Hugh Dickins1cb17292009-12-14 17:59:01 -0800475 unsigned long address = vma_address(page, vma);
476 if (address == -EFAULT)
477 continue;
Balbir Singhbed71612008-02-07 00:14:01 -0800478 /*
479 * If we are reclaiming on behalf of a cgroup, skip
480 * counting on behalf of references from different
481 * cgroups
482 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800483 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800484 continue;
Hugh Dickins1cb17292009-12-14 17:59:01 -0800485 referenced += page_referenced_one(page, vma, address,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700486 &mapcount, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (!mapcount)
488 break;
489 }
490
491 spin_unlock(&mapping->i_mmap_lock);
492 return referenced;
493}
494
495/**
496 * page_referenced - test if the page was referenced
497 * @page: the page to test
498 * @is_locked: caller holds lock on the page
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700499 * @mem_cont: target memory controller
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700500 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *
502 * Quick test_and_clear_referenced for all mappings to a page,
503 * returns the number of ptes which referenced the page.
504 */
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700505int page_referenced(struct page *page,
506 int is_locked,
507 struct mem_cgroup *mem_cont,
508 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int referenced = 0;
Hugh Dickins5ad64682009-12-14 17:59:24 -0800511 int we_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (TestClearPageReferenced(page))
514 referenced++;
515
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700516 *vm_flags = 0;
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -0800517 if (page_mapped(page) && page_rmapping(page)) {
Hugh Dickins5ad64682009-12-14 17:59:24 -0800518 if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
519 we_locked = trylock_page(page);
520 if (!we_locked) {
521 referenced++;
522 goto out;
523 }
524 }
525 if (unlikely(PageKsm(page)))
526 referenced += page_referenced_ksm(page, mem_cont,
527 vm_flags);
528 else if (PageAnon(page))
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700529 referenced += page_referenced_anon(page, mem_cont,
530 vm_flags);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800531 else if (page->mapping)
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700532 referenced += page_referenced_file(page, mem_cont,
533 vm_flags);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800534 if (we_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Hugh Dickins5ad64682009-12-14 17:59:24 -0800537out:
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100538 if (page_test_and_clear_young(page))
539 referenced++;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return referenced;
542}
543
Hugh Dickins1cb17292009-12-14 17:59:01 -0800544static int page_mkclean_one(struct page *page, struct vm_area_struct *vma,
545 unsigned long address)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700546{
547 struct mm_struct *mm = vma->vm_mm;
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100548 pte_t *pte;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700549 spinlock_t *ptl;
550 int ret = 0;
551
Nick Piggin479db0b2008-08-20 14:09:18 -0700552 pte = page_check_address(page, mm, address, &ptl, 1);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700553 if (!pte)
554 goto out;
555
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100556 if (pte_dirty(*pte) || pte_write(*pte)) {
557 pte_t entry;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700558
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100559 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700560 entry = ptep_clear_flush_notify(vma, address, pte);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100561 entry = pte_wrprotect(entry);
562 entry = pte_mkclean(entry);
Al Virod6e88e62006-12-29 16:48:35 -0800563 set_pte_at(mm, address, pte, entry);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100564 ret = 1;
565 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700566
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700567 pte_unmap_unlock(pte, ptl);
568out:
569 return ret;
570}
571
572static int page_mkclean_file(struct address_space *mapping, struct page *page)
573{
574 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
575 struct vm_area_struct *vma;
576 struct prio_tree_iter iter;
577 int ret = 0;
578
579 BUG_ON(PageAnon(page));
580
581 spin_lock(&mapping->i_mmap_lock);
582 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Hugh Dickins1cb17292009-12-14 17:59:01 -0800583 if (vma->vm_flags & VM_SHARED) {
584 unsigned long address = vma_address(page, vma);
585 if (address == -EFAULT)
586 continue;
587 ret += page_mkclean_one(page, vma, address);
588 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700589 }
590 spin_unlock(&mapping->i_mmap_lock);
591 return ret;
592}
593
594int page_mkclean(struct page *page)
595{
596 int ret = 0;
597
598 BUG_ON(!PageLocked(page));
599
600 if (page_mapped(page)) {
601 struct address_space *mapping = page_mapping(page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100602 if (mapping) {
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700603 ret = page_mkclean_file(mapping, page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100604 if (page_test_dirty(page)) {
605 page_clear_dirty(page);
606 ret = 1;
607 }
Martin Schwidefsky6c210482007-04-27 16:01:57 +0200608 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700609 }
610
611 return ret;
612}
Jaya Kumar60b59be2007-05-08 00:37:37 -0700613EXPORT_SYMBOL_GPL(page_mkclean);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700616 * __page_set_anon_rmap - setup new anonymous rmap
Nick Piggin9617d952006-01-06 00:11:12 -0800617 * @page: the page to add the mapping to
618 * @vma: the vm area in which the mapping is added
619 * @address: the user virtual address mapped
620 */
621static void __page_set_anon_rmap(struct page *page,
622 struct vm_area_struct *vma, unsigned long address)
623{
624 struct anon_vma *anon_vma = vma->anon_vma;
625
626 BUG_ON(!anon_vma);
627 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
628 page->mapping = (struct address_space *) anon_vma;
Nick Piggin9617d952006-01-06 00:11:12 -0800629 page->index = linear_page_index(vma, address);
Nick Piggin9617d952006-01-06 00:11:12 -0800630}
631
632/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700633 * __page_check_anon_rmap - sanity check anonymous rmap addition
Nick Pigginc97a9e12007-05-16 22:11:21 -0700634 * @page: the page to add the mapping to
635 * @vma: the vm area in which the mapping is added
636 * @address: the user virtual address mapped
637 */
638static void __page_check_anon_rmap(struct page *page,
639 struct vm_area_struct *vma, unsigned long address)
640{
641#ifdef CONFIG_DEBUG_VM
642 /*
643 * The page's anon-rmap details (mapping and index) are guaranteed to
644 * be set up correctly at this point.
645 *
646 * We have exclusion against page_add_anon_rmap because the caller
647 * always holds the page locked, except if called from page_dup_rmap,
648 * in which case the page is already known to be setup.
649 *
650 * We have exclusion against page_add_new_anon_rmap because those pages
651 * are initially only visible via the pagetables, and the pte is locked
652 * over the call to page_add_new_anon_rmap.
653 */
654 struct anon_vma *anon_vma = vma->anon_vma;
655 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
656 BUG_ON(page->mapping != (struct address_space *)anon_vma);
657 BUG_ON(page->index != linear_page_index(vma, address));
658#endif
659}
660
661/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * page_add_anon_rmap - add pte mapping to an anonymous page
663 * @page: the page to add the mapping to
664 * @vma: the vm area in which the mapping is added
665 * @address: the user virtual address mapped
666 *
Hugh Dickins5ad64682009-12-14 17:59:24 -0800667 * The caller needs to hold the pte lock, and the page must be locked in
668 * the anon_vma case: to serialize mapping,index checking after setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
670void page_add_anon_rmap(struct page *page,
671 struct vm_area_struct *vma, unsigned long address)
672{
Hugh Dickins5ad64682009-12-14 17:59:24 -0800673 int first = atomic_inc_and_test(&page->_mapcount);
674 if (first)
675 __inc_zone_page_state(page, NR_ANON_PAGES);
676 if (unlikely(PageKsm(page)))
677 return;
678
Nick Pigginc97a9e12007-05-16 22:11:21 -0700679 VM_BUG_ON(!PageLocked(page));
680 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Hugh Dickins5ad64682009-12-14 17:59:24 -0800681 if (first)
Nick Piggin9617d952006-01-06 00:11:12 -0800682 __page_set_anon_rmap(page, vma, address);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700683 else
Nick Pigginc97a9e12007-05-16 22:11:21 -0700684 __page_check_anon_rmap(page, vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700687/**
Nick Piggin9617d952006-01-06 00:11:12 -0800688 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
689 * @page: the page to add the mapping to
690 * @vma: the vm area in which the mapping is added
691 * @address: the user virtual address mapped
692 *
693 * Same as page_add_anon_rmap but must only be called on *new* pages.
694 * This means the inc-and-test can be bypassed.
Nick Pigginc97a9e12007-05-16 22:11:21 -0700695 * Page does not have to be locked.
Nick Piggin9617d952006-01-06 00:11:12 -0800696 */
697void page_add_new_anon_rmap(struct page *page,
698 struct vm_area_struct *vma, unsigned long address)
699{
Hugh Dickinsb5934c52009-01-06 14:39:25 -0800700 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Hugh Dickinscbf84b72009-01-06 14:39:27 -0800701 SetPageSwapBacked(page);
702 atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
Hugh Dickins5ad64682009-12-14 17:59:24 -0800703 __inc_zone_page_state(page, NR_ANON_PAGES);
Nick Piggin9617d952006-01-06 00:11:12 -0800704 __page_set_anon_rmap(page, vma, address);
Hugh Dickinsb5934c52009-01-06 14:39:25 -0800705 if (page_evictable(page, vma))
Hugh Dickinscbf84b72009-01-06 14:39:27 -0800706 lru_cache_add_lru(page, LRU_ACTIVE_ANON);
Hugh Dickinsb5934c52009-01-06 14:39:25 -0800707 else
708 add_page_to_unevictable_list(page);
Nick Piggin9617d952006-01-06 00:11:12 -0800709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/**
712 * page_add_file_rmap - add pte mapping to a file page
713 * @page: the page to add the mapping to
714 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700715 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
717void page_add_file_rmap(struct page *page)
718{
Balbir Singhd69b0422009-06-17 16:26:34 -0700719 if (atomic_inc_and_test(&page->_mapcount)) {
Christoph Lameter65ba55f52006-06-30 01:55:34 -0700720 __inc_zone_page_state(page, NR_FILE_MAPPED);
Balbir Singhd69b0422009-06-17 16:26:34 -0700721 mem_cgroup_update_mapped_file_stat(page, 1);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/**
726 * page_remove_rmap - take down pte mapping from a page
727 * @page: page to remove mapping from
728 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700729 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800731void page_remove_rmap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -0700733 /* page still mapped by someone else? */
734 if (!atomic_add_negative(-1, &page->_mapcount))
735 return;
736
737 /*
738 * Now that the last pte has gone, s390 must transfer dirty
739 * flag from storage key to struct page. We can usually skip
740 * this if the page is anon, so about to be freed; but perhaps
741 * not if it's in swapcache - there might be another pte slot
742 * containing the swap entry, but page not yet written to swap.
743 */
744 if ((!PageAnon(page) || PageSwapCache(page)) && page_test_dirty(page)) {
745 page_clear_dirty(page);
746 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -0700748 if (PageAnon(page)) {
749 mem_cgroup_uncharge_page(page);
750 __dec_zone_page_state(page, NR_ANON_PAGES);
751 } else {
752 __dec_zone_page_state(page, NR_FILE_MAPPED);
753 }
754 mem_cgroup_update_mapped_file_stat(page, -1);
755 /*
756 * It would be tidy to reset the PageAnon mapping here,
757 * but that might overwrite a racing page_add_anon_rmap
758 * which increments mapcount after us but sets mapping
759 * before us: so leave the reset to free_hot_cold_page,
760 * and remember that it's only reliable while mapped.
761 * Leaving it set also helps swapoff to reinstate ptes
762 * faster for those pages still in swapcache.
763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * Subfunctions of try_to_unmap: try_to_unmap_one called
768 * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
769 */
Hugh Dickins5ad64682009-12-14 17:59:24 -0800770int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
771 unsigned long address, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pte_t *pte;
775 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700776 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int ret = SWAP_AGAIN;
778
Nick Piggin479db0b2008-08-20 14:09:18 -0700779 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700780 if (!pte)
Nikita Danilov81b40822005-05-01 08:58:36 -0700781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /*
784 * If the page is mlock()d, we cannot swap it out.
785 * If it's recently referenced (perhaps page_referenced
786 * skipped over this mm) then we should reactivate it.
787 */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200788 if (!(flags & TTU_IGNORE_MLOCK)) {
Nick Pigginb291f002008-10-18 20:26:44 -0700789 if (vma->vm_flags & VM_LOCKED) {
790 ret = SWAP_MLOCK;
791 goto out_unmap;
792 }
Hugh Dickinsaf8e3352009-12-14 17:58:59 -0800793 if (TTU_ACTION(flags) == TTU_MUNLOCK)
Hugh Dickins53f79ac2009-12-14 17:58:58 -0800794 goto out_unmap;
Andi Kleen14fa31b2009-09-16 11:50:10 +0200795 }
796 if (!(flags & TTU_IGNORE_ACCESS)) {
Nick Pigginb291f002008-10-18 20:26:44 -0700797 if (ptep_clear_flush_young_notify(vma, address, pte)) {
798 ret = SWAP_FAIL;
799 goto out_unmap;
800 }
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* Nuke the page table entry. */
804 flush_cache_page(vma, address, page_to_pfn(page));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700805 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* Move the dirty bit to the physical page now the pte is gone. */
808 if (pte_dirty(pteval))
809 set_page_dirty(page);
810
Hugh Dickins365e9c872005-10-29 18:16:18 -0700811 /* Update high watermark before we lower rss */
812 update_hiwater_rss(mm);
813
Andi Kleen888b9f72009-09-16 11:50:11 +0200814 if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
815 if (PageAnon(page))
816 dec_mm_counter(mm, anon_rss);
817 else
818 dec_mm_counter(mm, file_rss);
819 set_pte_at(mm, address, pte,
820 swp_entry_to_pte(make_hwpoison_entry(page)));
821 } else if (PageAnon(page)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700822 swp_entry_t entry = { .val = page_private(page) };
Christoph Lameter06972122006-06-23 02:03:35 -0700823
824 if (PageSwapCache(page)) {
825 /*
826 * Store the swap location in the pte.
827 * See handle_pte_fault() ...
828 */
Hugh Dickins570a3352009-12-14 17:58:46 -0800829 if (swap_duplicate(entry) < 0) {
830 set_pte_at(mm, address, pte, pteval);
831 ret = SWAP_FAIL;
832 goto out_unmap;
833 }
Christoph Lameter06972122006-06-23 02:03:35 -0700834 if (list_empty(&mm->mmlist)) {
835 spin_lock(&mmlist_lock);
836 if (list_empty(&mm->mmlist))
837 list_add(&mm->mmlist, &init_mm.mmlist);
838 spin_unlock(&mmlist_lock);
839 }
Christoph Lameter442c9132006-06-23 02:03:38 -0700840 dec_mm_counter(mm, anon_rss);
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -0800841 } else if (PAGE_MIGRATION) {
Christoph Lameter06972122006-06-23 02:03:35 -0700842 /*
843 * Store the pfn of the page in a special migration
844 * pte. do_swap_page() will wait until the migration
845 * pte is removed and then restart fault handling.
846 */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200847 BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
Christoph Lameter06972122006-06-23 02:03:35 -0700848 entry = make_migration_entry(page, pte_write(pteval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
851 BUG_ON(pte_file(*pte));
Andi Kleen14fa31b2009-09-16 11:50:10 +0200852 } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
Christoph Lameter04e62a22006-06-23 02:03:38 -0700853 /* Establish migration entry for a file page */
854 swp_entry_t entry;
855 entry = make_migration_entry(page, pte_write(pteval));
856 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
857 } else
Hugh Dickins42946212005-10-29 18:16:05 -0700858 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800860 page_remove_rmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 page_cache_release(page);
862
863out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700864 pte_unmap_unlock(pte, ptl);
Hugh Dickins53f79ac2009-12-14 17:58:58 -0800865
Hugh Dickinsaf8e3352009-12-14 17:58:59 -0800866 if (ret == SWAP_MLOCK) {
Hugh Dickins53f79ac2009-12-14 17:58:58 -0800867 ret = SWAP_AGAIN;
868 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
869 if (vma->vm_flags & VM_LOCKED) {
870 mlock_vma_page(page);
871 ret = SWAP_MLOCK;
872 }
873 up_read(&vma->vm_mm->mmap_sem);
874 }
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876out:
877 return ret;
878}
879
880/*
881 * objrmap doesn't work for nonlinear VMAs because the assumption that
882 * offset-into-file correlates with offset-into-virtual-addresses does not hold.
883 * Consequently, given a particular page and its ->index, we cannot locate the
884 * ptes which are mapping that page without an exhaustive linear search.
885 *
886 * So what this code does is a mini "virtual scan" of each nonlinear VMA which
887 * maps the file to which the target page belongs. The ->vm_private_data field
888 * holds the current cursor into that scan. Successive searches will circulate
889 * around the vma's virtual address space.
890 *
891 * So as more replacement pressure is applied to the pages in a nonlinear VMA,
892 * more scanning pressure is placed against them as well. Eventually pages
893 * will become fully unmapped and are eligible for eviction.
894 *
895 * For very sparsely populated VMAs this is a little inefficient - chances are
896 * there there won't be many ptes located within the scan cluster. In this case
897 * maybe we could scan further - to the end of the pte page, perhaps.
Nick Pigginb291f002008-10-18 20:26:44 -0700898 *
899 * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
900 * acquire it without blocking. If vma locked, mlock the pages in the cluster,
901 * rather than unmapping them. If we encounter the "check_page" that vmscan is
902 * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
904#define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
905#define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
906
Nick Pigginb291f002008-10-18 20:26:44 -0700907static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
908 struct vm_area_struct *vma, struct page *check_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct mm_struct *mm = vma->vm_mm;
911 pgd_t *pgd;
912 pud_t *pud;
913 pmd_t *pmd;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700914 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700916 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct page *page;
918 unsigned long address;
919 unsigned long end;
Nick Pigginb291f002008-10-18 20:26:44 -0700920 int ret = SWAP_AGAIN;
921 int locked_vma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 address = (vma->vm_start + cursor) & CLUSTER_MASK;
924 end = address + CLUSTER_SIZE;
925 if (address < vma->vm_start)
926 address = vma->vm_start;
927 if (end > vma->vm_end)
928 end = vma->vm_end;
929
930 pgd = pgd_offset(mm, address);
931 if (!pgd_present(*pgd))
Nick Pigginb291f002008-10-18 20:26:44 -0700932 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 pud = pud_offset(pgd, address);
935 if (!pud_present(*pud))
Nick Pigginb291f002008-10-18 20:26:44 -0700936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 pmd = pmd_offset(pud, address);
939 if (!pmd_present(*pmd))
Nick Pigginb291f002008-10-18 20:26:44 -0700940 return ret;
941
942 /*
Hugh Dickinsaf8e3352009-12-14 17:58:59 -0800943 * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
Nick Pigginb291f002008-10-18 20:26:44 -0700944 * keep the sem while scanning the cluster for mlocking pages.
945 */
Hugh Dickinsaf8e3352009-12-14 17:58:59 -0800946 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
Nick Pigginb291f002008-10-18 20:26:44 -0700947 locked_vma = (vma->vm_flags & VM_LOCKED);
948 if (!locked_vma)
949 up_read(&vma->vm_mm->mmap_sem); /* don't need it */
950 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700951
952 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Hugh Dickins365e9c872005-10-29 18:16:18 -0700954 /* Update high watermark before we lower rss */
955 update_hiwater_rss(mm);
956
Hugh Dickinsc0718802005-10-29 18:16:31 -0700957 for (; address < end; pte++, address += PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!pte_present(*pte))
959 continue;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800960 page = vm_normal_page(vma, address, *pte);
961 BUG_ON(!page || PageAnon(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Nick Pigginb291f002008-10-18 20:26:44 -0700963 if (locked_vma) {
964 mlock_vma_page(page); /* no-op if already mlocked */
965 if (page == check_page)
966 ret = SWAP_MLOCK;
967 continue; /* don't unmap */
968 }
969
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700970 if (ptep_clear_flush_young_notify(vma, address, pte))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 continue;
972
973 /* Nuke the page table entry. */
Ben Collinseca35132005-11-29 11:45:26 -0800974 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700975 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* If nonlinear, store the file page offset in the pte. */
978 if (page->index != linear_page_index(vma, address))
979 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
980
981 /* Move the dirty bit to the physical page now the pte is gone. */
982 if (pte_dirty(pteval))
983 set_page_dirty(page);
984
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800985 page_remove_rmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 page_cache_release(page);
Hugh Dickins42946212005-10-29 18:16:05 -0700987 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 (*mapcount)--;
989 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700990 pte_unmap_unlock(pte - 1, ptl);
Nick Pigginb291f002008-10-18 20:26:44 -0700991 if (locked_vma)
992 up_read(&vma->vm_mm->mmap_sem);
993 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Nick Pigginb291f002008-10-18 20:26:44 -0700996/**
997 * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
998 * rmap method
999 * @page: the page to unmap/unlock
Huang Shijie8051be52009-12-14 17:58:50 -08001000 * @flags: action and flags
Nick Pigginb291f002008-10-18 20:26:44 -07001001 *
1002 * Find all the mappings of a page using the mapping pointer and the vma chains
1003 * contained in the anon_vma struct it points to.
1004 *
1005 * This function is only called from try_to_unmap/try_to_munlock for
1006 * anonymous pages.
1007 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1008 * where the page was found will be held for write. So, we won't recheck
1009 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1010 * 'LOCKED.
1011 */
Andi Kleen14fa31b2009-09-16 11:50:10 +02001012static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct anon_vma *anon_vma;
1015 struct vm_area_struct *vma;
1016 int ret = SWAP_AGAIN;
Nick Pigginb291f002008-10-18 20:26:44 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 anon_vma = page_lock_anon_vma(page);
1019 if (!anon_vma)
1020 return ret;
1021
1022 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Hugh Dickins1cb17292009-12-14 17:59:01 -08001023 unsigned long address = vma_address(page, vma);
1024 if (address == -EFAULT)
1025 continue;
1026 ret = try_to_unmap_one(page, vma, address, flags);
Hugh Dickins53f79ac2009-12-14 17:58:58 -08001027 if (ret != SWAP_AGAIN || !page_mapped(page))
1028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -08001030
1031 page_unlock_anon_vma(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return ret;
1033}
1034
1035/**
Nick Pigginb291f002008-10-18 20:26:44 -07001036 * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1037 * @page: the page to unmap/unlock
Andi Kleen14fa31b2009-09-16 11:50:10 +02001038 * @flags: action and flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 *
1040 * Find all the mappings of a page using the mapping pointer and the vma chains
1041 * contained in the address_space struct it points to.
1042 *
Nick Pigginb291f002008-10-18 20:26:44 -07001043 * This function is only called from try_to_unmap/try_to_munlock for
1044 * object-based pages.
1045 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1046 * where the page was found will be held for write. So, we won't recheck
1047 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1048 * 'LOCKED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Andi Kleen14fa31b2009-09-16 11:50:10 +02001050static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct address_space *mapping = page->mapping;
1053 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1054 struct vm_area_struct *vma;
1055 struct prio_tree_iter iter;
1056 int ret = SWAP_AGAIN;
1057 unsigned long cursor;
1058 unsigned long max_nl_cursor = 0;
1059 unsigned long max_nl_size = 0;
1060 unsigned int mapcount;
1061
1062 spin_lock(&mapping->i_mmap_lock);
1063 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Hugh Dickins1cb17292009-12-14 17:59:01 -08001064 unsigned long address = vma_address(page, vma);
1065 if (address == -EFAULT)
1066 continue;
1067 ret = try_to_unmap_one(page, vma, address, flags);
Hugh Dickins53f79ac2009-12-14 17:58:58 -08001068 if (ret != SWAP_AGAIN || !page_mapped(page))
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 if (list_empty(&mapping->i_mmap_nonlinear))
1073 goto out;
1074
Hugh Dickins53f79ac2009-12-14 17:58:58 -08001075 /*
1076 * We don't bother to try to find the munlocked page in nonlinears.
1077 * It's costly. Instead, later, page reclaim logic may call
1078 * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily.
1079 */
1080 if (TTU_ACTION(flags) == TTU_MUNLOCK)
1081 goto out;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1084 shared.vm_set.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 cursor = (unsigned long) vma->vm_private_data;
1086 if (cursor > max_nl_cursor)
1087 max_nl_cursor = cursor;
1088 cursor = vma->vm_end - vma->vm_start;
1089 if (cursor > max_nl_size)
1090 max_nl_size = cursor;
1091 }
1092
Nick Pigginb291f002008-10-18 20:26:44 -07001093 if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ret = SWAP_FAIL;
1095 goto out;
1096 }
1097
1098 /*
1099 * We don't try to search for this page in the nonlinear vmas,
1100 * and page_referenced wouldn't have found it anyway. Instead
1101 * just walk the nonlinear vmas trying to age and unmap some.
1102 * The mapcount of the page we came in with is irrelevant,
1103 * but even so use it as a guide to how hard we should try?
1104 */
1105 mapcount = page_mapcount(page);
1106 if (!mapcount)
1107 goto out;
1108 cond_resched_lock(&mapping->i_mmap_lock);
1109
1110 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
1111 if (max_nl_cursor == 0)
1112 max_nl_cursor = CLUSTER_SIZE;
1113
1114 do {
1115 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1116 shared.vm_set.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 cursor = (unsigned long) vma->vm_private_data;
Hugh Dickins839b9682005-09-03 15:54:43 -07001118 while ( cursor < max_nl_cursor &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 cursor < vma->vm_end - vma->vm_start) {
Hugh Dickins53f79ac2009-12-14 17:58:58 -08001120 if (try_to_unmap_cluster(cursor, &mapcount,
1121 vma, page) == SWAP_MLOCK)
1122 ret = SWAP_MLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 cursor += CLUSTER_SIZE;
1124 vma->vm_private_data = (void *) cursor;
1125 if ((int)mapcount <= 0)
1126 goto out;
1127 }
1128 vma->vm_private_data = (void *) max_nl_cursor;
1129 }
1130 cond_resched_lock(&mapping->i_mmap_lock);
1131 max_nl_cursor += CLUSTER_SIZE;
1132 } while (max_nl_cursor <= max_nl_size);
1133
1134 /*
1135 * Don't loop forever (perhaps all the remaining pages are
1136 * in locked vmas). Reset cursor on all unreserved nonlinear
1137 * vmas, now forgetting on which ones it had fallen behind.
1138 */
Hugh Dickins101d2be2005-11-21 21:32:16 -08001139 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1140 vma->vm_private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141out:
1142 spin_unlock(&mapping->i_mmap_lock);
1143 return ret;
1144}
1145
1146/**
1147 * try_to_unmap - try to remove all page table mappings to a page
1148 * @page: the page to get unmapped
Andi Kleen14fa31b2009-09-16 11:50:10 +02001149 * @flags: action and flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 *
1151 * Tries to remove all the page table entries which are mapping this
1152 * page, used in the pageout path. Caller must hold the page lock.
1153 * Return values are:
1154 *
1155 * SWAP_SUCCESS - we succeeded in removing all mappings
1156 * SWAP_AGAIN - we missed a mapping, try again later
1157 * SWAP_FAIL - the page is unswappable
Nick Pigginb291f002008-10-18 20:26:44 -07001158 * SWAP_MLOCK - page is mlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Andi Kleen14fa31b2009-09-16 11:50:10 +02001160int try_to_unmap(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 int ret;
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 BUG_ON(!PageLocked(page));
1165
Hugh Dickins5ad64682009-12-14 17:59:24 -08001166 if (unlikely(PageKsm(page)))
1167 ret = try_to_unmap_ksm(page, flags);
1168 else if (PageAnon(page))
Andi Kleen14fa31b2009-09-16 11:50:10 +02001169 ret = try_to_unmap_anon(page, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 else
Andi Kleen14fa31b2009-09-16 11:50:10 +02001171 ret = try_to_unmap_file(page, flags);
Nick Pigginb291f002008-10-18 20:26:44 -07001172 if (ret != SWAP_MLOCK && !page_mapped(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ret = SWAP_SUCCESS;
1174 return ret;
1175}
Nikita Danilov81b40822005-05-01 08:58:36 -07001176
Nick Pigginb291f002008-10-18 20:26:44 -07001177/**
1178 * try_to_munlock - try to munlock a page
1179 * @page: the page to be munlocked
1180 *
1181 * Called from munlock code. Checks all of the VMAs mapping the page
1182 * to make sure nobody else has this page mlocked. The page will be
1183 * returned with PG_mlocked cleared if no other vmas have it mlocked.
1184 *
1185 * Return values are:
1186 *
Hugh Dickins53f79ac2009-12-14 17:58:58 -08001187 * SWAP_AGAIN - no vma is holding page mlocked, or,
Nick Pigginb291f002008-10-18 20:26:44 -07001188 * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
Hugh Dickins5ad64682009-12-14 17:59:24 -08001189 * SWAP_FAIL - page cannot be located at present
Nick Pigginb291f002008-10-18 20:26:44 -07001190 * SWAP_MLOCK - page is now mlocked.
1191 */
1192int try_to_munlock(struct page *page)
1193{
1194 VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1195
Hugh Dickins5ad64682009-12-14 17:59:24 -08001196 if (unlikely(PageKsm(page)))
1197 return try_to_unmap_ksm(page, TTU_MUNLOCK);
1198 else if (PageAnon(page))
Andi Kleen14fa31b2009-09-16 11:50:10 +02001199 return try_to_unmap_anon(page, TTU_MUNLOCK);
Nick Pigginb291f002008-10-18 20:26:44 -07001200 else
Andi Kleen14fa31b2009-09-16 11:50:10 +02001201 return try_to_unmap_file(page, TTU_MUNLOCK);
Nick Pigginb291f002008-10-18 20:26:44 -07001202}