blob: 1a0ee6e634c28e04c6d9b1be5369c72cbb762417 [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>
52#include <linux/rmap.h>
53#include <linux/rcupdate.h>
Christoph Lametera48d07a2006-02-01 03:05:38 -080054#include <linux/module.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080055#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070056#include <linux/mmu_notifier.h>
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -080057#include <linux/migrate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <asm/tlbflush.h>
60
Nick Pigginb291f002008-10-18 20:26:44 -070061#include "internal.h"
62
Adrian Bunkfdd2e5f2008-10-18 20:28:38 -070063static struct kmem_cache *anon_vma_cachep;
64
65static inline struct anon_vma *anon_vma_alloc(void)
66{
67 return kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
68}
69
70static inline void anon_vma_free(struct anon_vma *anon_vma)
71{
72 kmem_cache_free(anon_vma_cachep, anon_vma);
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070075/**
76 * anon_vma_prepare - attach an anon_vma to a memory region
77 * @vma: the memory region in question
78 *
79 * This makes sure the memory mapping described by 'vma' has
80 * an 'anon_vma' attached to it, so that we can associate the
81 * anonymous pages mapped into it with that anon_vma.
82 *
83 * The common case will be that we already have one, but if
84 * if not we either need to find an adjacent mapping that we
85 * can re-use the anon_vma from (very common when the only
86 * reason for splitting a vma has been mprotect()), or we
87 * allocate a new one.
88 *
89 * Anon-vma allocations are very subtle, because we may have
90 * optimistically looked up an anon_vma in page_lock_anon_vma()
91 * and that may actually touch the spinlock even in the newly
92 * allocated vma (it depends on RCU to make sure that the
93 * anon_vma isn't actually destroyed).
94 *
95 * As a result, we need to do proper anon_vma locking even
96 * for the new allocation. At the same time, we do not want
97 * to do any locking for the common case of already having
98 * an anon_vma.
99 *
100 * This must be called with the mmap_sem held for reading.
101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102int anon_vma_prepare(struct vm_area_struct *vma)
103{
104 struct anon_vma *anon_vma = vma->anon_vma;
105
106 might_sleep();
107 if (unlikely(!anon_vma)) {
108 struct mm_struct *mm = vma->vm_mm;
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700109 struct anon_vma *allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 anon_vma = find_mergeable_anon_vma(vma);
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700112 allocated = NULL;
113 if (!anon_vma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 anon_vma = anon_vma_alloc();
115 if (unlikely(!anon_vma))
116 return -ENOMEM;
117 allocated = anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700119 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* page_table_lock to protect against threads */
122 spin_lock(&mm->page_table_lock);
123 if (likely(!vma->anon_vma)) {
124 vma->anon_vma = anon_vma;
Christoph Lameter06972122006-06-23 02:03:35 -0700125 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 allocated = NULL;
127 }
128 spin_unlock(&mm->page_table_lock);
129
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700130 spin_unlock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (unlikely(allocated))
132 anon_vma_free(allocated);
133 }
134 return 0;
135}
136
137void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
138{
139 BUG_ON(vma->anon_vma != next->anon_vma);
140 list_del(&next->anon_vma_node);
141}
142
143void __anon_vma_link(struct vm_area_struct *vma)
144{
145 struct anon_vma *anon_vma = vma->anon_vma;
146
Hugh Dickins30acbab2007-06-27 14:09:53 -0700147 if (anon_vma)
Christoph Lameter06972122006-06-23 02:03:35 -0700148 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151void anon_vma_link(struct vm_area_struct *vma)
152{
153 struct anon_vma *anon_vma = vma->anon_vma;
154
155 if (anon_vma) {
156 spin_lock(&anon_vma->lock);
Christoph Lameter06972122006-06-23 02:03:35 -0700157 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 spin_unlock(&anon_vma->lock);
159 }
160}
161
162void anon_vma_unlink(struct vm_area_struct *vma)
163{
164 struct anon_vma *anon_vma = vma->anon_vma;
165 int empty;
166
167 if (!anon_vma)
168 return;
169
170 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 list_del(&vma->anon_vma_node);
172
173 /* We must garbage collect the anon_vma if it's empty */
174 empty = list_empty(&anon_vma->head);
175 spin_unlock(&anon_vma->lock);
176
177 if (empty)
178 anon_vma_free(anon_vma);
179}
180
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700181static void anon_vma_ctor(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Christoph Lametera35afb82007-05-16 22:10:57 -0700183 struct anon_vma *anon_vma = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Christoph Lametera35afb82007-05-16 22:10:57 -0700185 spin_lock_init(&anon_vma->lock);
186 INIT_LIST_HEAD(&anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189void __init anon_vma_init(void)
190{
191 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
Paul Mundt20c2df82007-07-20 10:11:58 +0900192 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
196 * Getting a lock on a stable anon_vma from a page off the LRU is
197 * tricky: page_lock_anon_vma rely on RCU to guard against the races.
198 */
Andi Kleen10be22d2009-09-16 11:50:04 +0200199struct anon_vma *page_lock_anon_vma(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800201 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 unsigned long anon_mapping;
203
204 rcu_read_lock();
205 anon_mapping = (unsigned long) page->mapping;
206 if (!(anon_mapping & PAGE_MAPPING_ANON))
207 goto out;
208 if (!page_mapped(page))
209 goto out;
210
211 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
212 spin_lock(&anon_vma->lock);
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800213 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214out:
215 rcu_read_unlock();
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800216 return NULL;
217}
218
Andi Kleen10be22d2009-09-16 11:50:04 +0200219void page_unlock_anon_vma(struct anon_vma *anon_vma)
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800220{
221 spin_unlock(&anon_vma->lock);
222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800226 * At what user virtual address is page expected in @vma?
227 * Returns virtual address or -EFAULT if page's index/offset is not
228 * within the range mapped the @vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
230static inline unsigned long
231vma_address(struct page *page, struct vm_area_struct *vma)
232{
233 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
234 unsigned long address;
235
236 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
237 if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800238 /* page should be within @vma mapping range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EFAULT;
240 }
241 return address;
242}
243
244/*
Huang Shijiebf89c8c2009-10-01 15:44:04 -0700245 * At what user virtual address is page expected in vma?
246 * checking that the page matches the vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
248unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
249{
250 if (PageAnon(page)) {
251 if ((void *)vma->anon_vma !=
252 (void *)page->mapping - PAGE_MAPPING_ANON)
253 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 */
340static int page_referenced_one(struct page *page,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700341 struct vm_area_struct *vma,
342 unsigned int *mapcount,
343 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct mm_struct *mm = vma->vm_mm;
346 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700348 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int referenced = 0;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 address = vma_address(page, vma);
352 if (address == -EFAULT)
353 goto out;
354
Nick Piggin479db0b2008-08-20 14:09:18 -0700355 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700356 if (!pte)
357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Nick Pigginb291f002008-10-18 20:26:44 -0700359 /*
360 * Don't want to elevate referenced for mlocked page that gets this far,
361 * in order that it progresses to try_to_unmap and is moved to the
362 * unevictable list.
363 */
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800364 if (vma->vm_flags & VM_LOCKED) {
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800365 *mapcount = 1; /* break early from loop */
Minchan Kim03ef83a2009-08-26 14:29:23 -0700366 *vm_flags |= VM_LOCKED;
Nick Pigginb291f002008-10-18 20:26:44 -0700367 goto out_unmap;
368 }
369
Johannes Weiner4917e5d2009-01-06 14:39:17 -0800370 if (ptep_clear_flush_young_notify(vma, address, pte)) {
371 /*
372 * Don't treat a reference through a sequentially read
373 * mapping as such. If the page has been used in
374 * another mapping, we will catch it; if this other
375 * mapping is already gone, the unmap path will have
376 * set PG_referenced or activated the page.
377 */
378 if (likely(!VM_SequentialReadHint(vma)))
379 referenced++;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Hugh Dickinsc0718802005-10-29 18:16:31 -0700382 /* Pretend the page is referenced if the task has the
383 swap token and is in the middle of a page fault. */
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800384 if (mm != current->mm && has_swap_token(mm) &&
Hugh Dickinsc0718802005-10-29 18:16:31 -0700385 rwsem_is_locked(&mm->mmap_sem))
386 referenced++;
387
Nick Pigginb291f002008-10-18 20:26:44 -0700388out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700389 (*mapcount)--;
390 pte_unmap_unlock(pte, ptl);
Huang Shijie273f0472009-12-14 17:58:51 -0800391
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700392 if (referenced)
393 *vm_flags |= vma->vm_flags;
Huang Shijie273f0472009-12-14 17:58:51 -0800394out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return referenced;
396}
397
Balbir Singhbed71612008-02-07 00:14:01 -0800398static int page_referenced_anon(struct page *page,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700399 struct mem_cgroup *mem_cont,
400 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 unsigned int mapcount;
403 struct anon_vma *anon_vma;
404 struct vm_area_struct *vma;
405 int referenced = 0;
406
407 anon_vma = page_lock_anon_vma(page);
408 if (!anon_vma)
409 return referenced;
410
411 mapcount = page_mapcount(page);
412 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Balbir Singhbed71612008-02-07 00:14:01 -0800413 /*
414 * If we are reclaiming on behalf of a cgroup, skip
415 * counting on behalf of references from different
416 * cgroups
417 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800418 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800419 continue;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700420 referenced += page_referenced_one(page, vma,
421 &mapcount, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!mapcount)
423 break;
424 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800425
426 page_unlock_anon_vma(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return referenced;
428}
429
430/**
431 * page_referenced_file - referenced check for object-based rmap
432 * @page: the page we're checking references on.
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700433 * @mem_cont: target memory controller
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700434 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *
436 * For an object-based mapped page, find all the places it is mapped and
437 * check/clear the referenced flag. This is done by following the page->mapping
438 * pointer, then walking the chain of vmas it holds. It returns the number
439 * of references it found.
440 *
441 * This function is only called from page_referenced for object-based pages.
442 */
Balbir Singhbed71612008-02-07 00:14:01 -0800443static int page_referenced_file(struct page *page,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700444 struct mem_cgroup *mem_cont,
445 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 unsigned int mapcount;
448 struct address_space *mapping = page->mapping;
449 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
450 struct vm_area_struct *vma;
451 struct prio_tree_iter iter;
452 int referenced = 0;
453
454 /*
455 * The caller's checks on page->mapping and !PageAnon have made
456 * sure that this is a file page: the check for page->mapping
457 * excludes the case just before it gets set on an anon page.
458 */
459 BUG_ON(PageAnon(page));
460
461 /*
462 * The page lock not only makes sure that page->mapping cannot
463 * suddenly be NULLified by truncation, it makes sure that the
464 * structure at mapping cannot be freed and reused yet,
465 * so we can safely take mapping->i_mmap_lock.
466 */
467 BUG_ON(!PageLocked(page));
468
469 spin_lock(&mapping->i_mmap_lock);
470
471 /*
472 * i_mmap_lock does not stabilize mapcount at all, but mapcount
473 * is more likely to be accurate if we note it after spinning.
474 */
475 mapcount = page_mapcount(page);
476
477 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
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;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700485 referenced += page_referenced_one(page, vma,
486 &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;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (TestClearPageReferenced(page))
513 referenced++;
514
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700515 *vm_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (page_mapped(page) && page->mapping) {
517 if (PageAnon(page))
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700518 referenced += page_referenced_anon(page, mem_cont,
519 vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 else if (is_locked)
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700521 referenced += page_referenced_file(page, mem_cont,
522 vm_flags);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200523 else if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 referenced++;
525 else {
526 if (page->mapping)
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700527 referenced += page_referenced_file(page,
528 mem_cont, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 unlock_page(page);
530 }
531 }
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100532
533 if (page_test_and_clear_young(page))
534 referenced++;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return referenced;
537}
538
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700539static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
540{
541 struct mm_struct *mm = vma->vm_mm;
542 unsigned long address;
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100543 pte_t *pte;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700544 spinlock_t *ptl;
545 int ret = 0;
546
547 address = vma_address(page, vma);
548 if (address == -EFAULT)
549 goto out;
550
Nick Piggin479db0b2008-08-20 14:09:18 -0700551 pte = page_check_address(page, mm, address, &ptl, 1);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700552 if (!pte)
553 goto out;
554
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100555 if (pte_dirty(*pte) || pte_write(*pte)) {
556 pte_t entry;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700557
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100558 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700559 entry = ptep_clear_flush_notify(vma, address, pte);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100560 entry = pte_wrprotect(entry);
561 entry = pte_mkclean(entry);
Al Virod6e88e62006-12-29 16:48:35 -0800562 set_pte_at(mm, address, pte, entry);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100563 ret = 1;
564 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700565
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700566 pte_unmap_unlock(pte, ptl);
567out:
568 return ret;
569}
570
571static int page_mkclean_file(struct address_space *mapping, struct page *page)
572{
573 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
574 struct vm_area_struct *vma;
575 struct prio_tree_iter iter;
576 int ret = 0;
577
578 BUG_ON(PageAnon(page));
579
580 spin_lock(&mapping->i_mmap_lock);
581 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
582 if (vma->vm_flags & VM_SHARED)
583 ret += page_mkclean_one(page, vma);
584 }
585 spin_unlock(&mapping->i_mmap_lock);
586 return ret;
587}
588
589int page_mkclean(struct page *page)
590{
591 int ret = 0;
592
593 BUG_ON(!PageLocked(page));
594
595 if (page_mapped(page)) {
596 struct address_space *mapping = page_mapping(page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100597 if (mapping) {
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700598 ret = page_mkclean_file(mapping, page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100599 if (page_test_dirty(page)) {
600 page_clear_dirty(page);
601 ret = 1;
602 }
Martin Schwidefsky6c210482007-04-27 16:01:57 +0200603 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700604 }
605
606 return ret;
607}
Jaya Kumar60b59be2007-05-08 00:37:37 -0700608EXPORT_SYMBOL_GPL(page_mkclean);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700611 * __page_set_anon_rmap - setup new anonymous rmap
Nick Piggin9617d952006-01-06 00:11:12 -0800612 * @page: the page to add the mapping to
613 * @vma: the vm area in which the mapping is added
614 * @address: the user virtual address mapped
615 */
616static void __page_set_anon_rmap(struct page *page,
617 struct vm_area_struct *vma, unsigned long address)
618{
619 struct anon_vma *anon_vma = vma->anon_vma;
620
621 BUG_ON(!anon_vma);
622 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
623 page->mapping = (struct address_space *) anon_vma;
624
625 page->index = linear_page_index(vma, address);
626
Nick Piggina74609f2006-01-06 00:11:20 -0800627 /*
628 * nr_mapped state can be updated without turning off
629 * interrupts because it is not modified via interrupt.
630 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700631 __inc_zone_page_state(page, NR_ANON_PAGES);
Nick Piggin9617d952006-01-06 00:11:12 -0800632}
633
634/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700635 * __page_check_anon_rmap - sanity check anonymous rmap addition
Nick Pigginc97a9e12007-05-16 22:11:21 -0700636 * @page: the page to add the mapping to
637 * @vma: the vm area in which the mapping is added
638 * @address: the user virtual address mapped
639 */
640static void __page_check_anon_rmap(struct page *page,
641 struct vm_area_struct *vma, unsigned long address)
642{
643#ifdef CONFIG_DEBUG_VM
644 /*
645 * The page's anon-rmap details (mapping and index) are guaranteed to
646 * be set up correctly at this point.
647 *
648 * We have exclusion against page_add_anon_rmap because the caller
649 * always holds the page locked, except if called from page_dup_rmap,
650 * in which case the page is already known to be setup.
651 *
652 * We have exclusion against page_add_new_anon_rmap because those pages
653 * are initially only visible via the pagetables, and the pte is locked
654 * over the call to page_add_new_anon_rmap.
655 */
656 struct anon_vma *anon_vma = vma->anon_vma;
657 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
658 BUG_ON(page->mapping != (struct address_space *)anon_vma);
659 BUG_ON(page->index != linear_page_index(vma, address));
660#endif
661}
662
663/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * page_add_anon_rmap - add pte mapping to an anonymous page
665 * @page: the page to add the mapping to
666 * @vma: the vm area in which the mapping is added
667 * @address: the user virtual address mapped
668 *
Nick Pigginc97a9e12007-05-16 22:11:21 -0700669 * The caller needs to hold the pte lock and the page must be locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
671void page_add_anon_rmap(struct page *page,
672 struct vm_area_struct *vma, unsigned long address)
673{
Nick Pigginc97a9e12007-05-16 22:11:21 -0700674 VM_BUG_ON(!PageLocked(page));
675 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Nick Piggin9617d952006-01-06 00:11:12 -0800676 if (atomic_inc_and_test(&page->_mapcount))
677 __page_set_anon_rmap(page, vma, address);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700678 else
Nick Pigginc97a9e12007-05-16 22:11:21 -0700679 __page_check_anon_rmap(page, vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700682/**
Nick Piggin9617d952006-01-06 00:11:12 -0800683 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
684 * @page: the page to add the mapping to
685 * @vma: the vm area in which the mapping is added
686 * @address: the user virtual address mapped
687 *
688 * Same as page_add_anon_rmap but must only be called on *new* pages.
689 * This means the inc-and-test can be bypassed.
Nick Pigginc97a9e12007-05-16 22:11:21 -0700690 * Page does not have to be locked.
Nick Piggin9617d952006-01-06 00:11:12 -0800691 */
692void page_add_new_anon_rmap(struct page *page,
693 struct vm_area_struct *vma, unsigned long address)
694{
Hugh Dickinsb5934c52009-01-06 14:39:25 -0800695 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Hugh Dickinscbf84b72009-01-06 14:39:27 -0800696 SetPageSwapBacked(page);
697 atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
Nick Piggin9617d952006-01-06 00:11:12 -0800698 __page_set_anon_rmap(page, vma, address);
Hugh Dickinsb5934c52009-01-06 14:39:25 -0800699 if (page_evictable(page, vma))
Hugh Dickinscbf84b72009-01-06 14:39:27 -0800700 lru_cache_add_lru(page, LRU_ACTIVE_ANON);
Hugh Dickinsb5934c52009-01-06 14:39:25 -0800701 else
702 add_page_to_unevictable_list(page);
Nick Piggin9617d952006-01-06 00:11:12 -0800703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/**
706 * page_add_file_rmap - add pte mapping to a file page
707 * @page: the page to add the mapping to
708 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700709 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
711void page_add_file_rmap(struct page *page)
712{
Balbir Singhd69b0422009-06-17 16:26:34 -0700713 if (atomic_inc_and_test(&page->_mapcount)) {
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700714 __inc_zone_page_state(page, NR_FILE_MAPPED);
Balbir Singhd69b0422009-06-17 16:26:34 -0700715 mem_cgroup_update_mapped_file_stat(page, 1);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/**
720 * page_remove_rmap - take down pte mapping from a page
721 * @page: page to remove mapping from
722 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700723 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800725void page_remove_rmap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -0700727 /* page still mapped by someone else? */
728 if (!atomic_add_negative(-1, &page->_mapcount))
729 return;
730
731 /*
732 * Now that the last pte has gone, s390 must transfer dirty
733 * flag from storage key to struct page. We can usually skip
734 * this if the page is anon, so about to be freed; but perhaps
735 * not if it's in swapcache - there might be another pte slot
736 * containing the swap entry, but page not yet written to swap.
737 */
738 if ((!PageAnon(page) || PageSwapCache(page)) && page_test_dirty(page)) {
739 page_clear_dirty(page);
740 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -0700742 if (PageAnon(page)) {
743 mem_cgroup_uncharge_page(page);
744 __dec_zone_page_state(page, NR_ANON_PAGES);
745 } else {
746 __dec_zone_page_state(page, NR_FILE_MAPPED);
747 }
748 mem_cgroup_update_mapped_file_stat(page, -1);
749 /*
750 * It would be tidy to reset the PageAnon mapping here,
751 * but that might overwrite a racing page_add_anon_rmap
752 * which increments mapcount after us but sets mapping
753 * before us: so leave the reset to free_hot_cold_page,
754 * and remember that it's only reliable while mapped.
755 * Leaving it set also helps swapoff to reinstate ptes
756 * faster for those pages still in swapcache.
757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760/*
761 * Subfunctions of try_to_unmap: try_to_unmap_one called
762 * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
763 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800764static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
Andi Kleen14fa31b2009-09-16 11:50:10 +0200765 enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct mm_struct *mm = vma->vm_mm;
768 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 pte_t *pte;
770 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700771 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int ret = SWAP_AGAIN;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 address = vma_address(page, vma);
775 if (address == -EFAULT)
776 goto out;
777
Nick Piggin479db0b2008-08-20 14:09:18 -0700778 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700779 if (!pte)
Nikita Danilov81b40822005-05-01 08:58:36 -0700780 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
783 * If the page is mlock()d, we cannot swap it out.
784 * If it's recently referenced (perhaps page_referenced
785 * skipped over this mm) then we should reactivate it.
786 */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200787 if (!(flags & TTU_IGNORE_MLOCK)) {
Nick Pigginb291f002008-10-18 20:26:44 -0700788 if (vma->vm_flags & VM_LOCKED) {
789 ret = SWAP_MLOCK;
790 goto out_unmap;
791 }
Andi Kleen14fa31b2009-09-16 11:50:10 +0200792 }
793 if (!(flags & TTU_IGNORE_ACCESS)) {
Nick Pigginb291f002008-10-18 20:26:44 -0700794 if (ptep_clear_flush_young_notify(vma, address, pte)) {
795 ret = SWAP_FAIL;
796 goto out_unmap;
797 }
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Nuke the page table entry. */
801 flush_cache_page(vma, address, page_to_pfn(page));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700802 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* Move the dirty bit to the physical page now the pte is gone. */
805 if (pte_dirty(pteval))
806 set_page_dirty(page);
807
Hugh Dickins365e9c872005-10-29 18:16:18 -0700808 /* Update high watermark before we lower rss */
809 update_hiwater_rss(mm);
810
Andi Kleen888b9f72009-09-16 11:50:11 +0200811 if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
812 if (PageAnon(page))
813 dec_mm_counter(mm, anon_rss);
814 else
815 dec_mm_counter(mm, file_rss);
816 set_pte_at(mm, address, pte,
817 swp_entry_to_pte(make_hwpoison_entry(page)));
818 } else if (PageAnon(page)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700819 swp_entry_t entry = { .val = page_private(page) };
Christoph Lameter06972122006-06-23 02:03:35 -0700820
821 if (PageSwapCache(page)) {
822 /*
823 * Store the swap location in the pte.
824 * See handle_pte_fault() ...
825 */
Hugh Dickins570a335b2009-12-14 17:58:46 -0800826 if (swap_duplicate(entry) < 0) {
827 set_pte_at(mm, address, pte, pteval);
828 ret = SWAP_FAIL;
829 goto out_unmap;
830 }
Christoph Lameter06972122006-06-23 02:03:35 -0700831 if (list_empty(&mm->mmlist)) {
832 spin_lock(&mmlist_lock);
833 if (list_empty(&mm->mmlist))
834 list_add(&mm->mmlist, &init_mm.mmlist);
835 spin_unlock(&mmlist_lock);
836 }
Christoph Lameter442c9132006-06-23 02:03:38 -0700837 dec_mm_counter(mm, anon_rss);
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -0800838 } else if (PAGE_MIGRATION) {
Christoph Lameter06972122006-06-23 02:03:35 -0700839 /*
840 * Store the pfn of the page in a special migration
841 * pte. do_swap_page() will wait until the migration
842 * pte is removed and then restart fault handling.
843 */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200844 BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
Christoph Lameter06972122006-06-23 02:03:35 -0700845 entry = make_migration_entry(page, pte_write(pteval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
848 BUG_ON(pte_file(*pte));
Andi Kleen14fa31b2009-09-16 11:50:10 +0200849 } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
Christoph Lameter04e62a22006-06-23 02:03:38 -0700850 /* Establish migration entry for a file page */
851 swp_entry_t entry;
852 entry = make_migration_entry(page, pte_write(pteval));
853 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
854 } else
Hugh Dickins42946212005-10-29 18:16:05 -0700855 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Christoph Lameter04e62a22006-06-23 02:03:38 -0700857
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800858 page_remove_rmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 page_cache_release(page);
860
861out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700862 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863out:
864 return ret;
865}
866
867/*
868 * objrmap doesn't work for nonlinear VMAs because the assumption that
869 * offset-into-file correlates with offset-into-virtual-addresses does not hold.
870 * Consequently, given a particular page and its ->index, we cannot locate the
871 * ptes which are mapping that page without an exhaustive linear search.
872 *
873 * So what this code does is a mini "virtual scan" of each nonlinear VMA which
874 * maps the file to which the target page belongs. The ->vm_private_data field
875 * holds the current cursor into that scan. Successive searches will circulate
876 * around the vma's virtual address space.
877 *
878 * So as more replacement pressure is applied to the pages in a nonlinear VMA,
879 * more scanning pressure is placed against them as well. Eventually pages
880 * will become fully unmapped and are eligible for eviction.
881 *
882 * For very sparsely populated VMAs this is a little inefficient - chances are
883 * there there won't be many ptes located within the scan cluster. In this case
884 * maybe we could scan further - to the end of the pte page, perhaps.
Nick Pigginb291f002008-10-18 20:26:44 -0700885 *
886 * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
887 * acquire it without blocking. If vma locked, mlock the pages in the cluster,
888 * rather than unmapping them. If we encounter the "check_page" that vmscan is
889 * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891#define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
892#define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
893
Nick Pigginb291f002008-10-18 20:26:44 -0700894static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
895 struct vm_area_struct *vma, struct page *check_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct mm_struct *mm = vma->vm_mm;
898 pgd_t *pgd;
899 pud_t *pud;
900 pmd_t *pmd;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700901 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700903 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct page *page;
905 unsigned long address;
906 unsigned long end;
Nick Pigginb291f002008-10-18 20:26:44 -0700907 int ret = SWAP_AGAIN;
908 int locked_vma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 address = (vma->vm_start + cursor) & CLUSTER_MASK;
911 end = address + CLUSTER_SIZE;
912 if (address < vma->vm_start)
913 address = vma->vm_start;
914 if (end > vma->vm_end)
915 end = vma->vm_end;
916
917 pgd = pgd_offset(mm, address);
918 if (!pgd_present(*pgd))
Nick Pigginb291f002008-10-18 20:26:44 -0700919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 pud = pud_offset(pgd, address);
922 if (!pud_present(*pud))
Nick Pigginb291f002008-10-18 20:26:44 -0700923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 pmd = pmd_offset(pud, address);
926 if (!pmd_present(*pmd))
Nick Pigginb291f002008-10-18 20:26:44 -0700927 return ret;
928
929 /*
930 * MLOCK_PAGES => feature is configured.
931 * if we can acquire the mmap_sem for read, and vma is VM_LOCKED,
932 * keep the sem while scanning the cluster for mlocking pages.
933 */
934 if (MLOCK_PAGES && down_read_trylock(&vma->vm_mm->mmap_sem)) {
935 locked_vma = (vma->vm_flags & VM_LOCKED);
936 if (!locked_vma)
937 up_read(&vma->vm_mm->mmap_sem); /* don't need it */
938 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700939
940 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Hugh Dickins365e9c872005-10-29 18:16:18 -0700942 /* Update high watermark before we lower rss */
943 update_hiwater_rss(mm);
944
Hugh Dickinsc0718802005-10-29 18:16:31 -0700945 for (; address < end; pte++, address += PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!pte_present(*pte))
947 continue;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800948 page = vm_normal_page(vma, address, *pte);
949 BUG_ON(!page || PageAnon(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Nick Pigginb291f002008-10-18 20:26:44 -0700951 if (locked_vma) {
952 mlock_vma_page(page); /* no-op if already mlocked */
953 if (page == check_page)
954 ret = SWAP_MLOCK;
955 continue; /* don't unmap */
956 }
957
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700958 if (ptep_clear_flush_young_notify(vma, address, pte))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 continue;
960
961 /* Nuke the page table entry. */
Ben Collinseca35132005-11-29 11:45:26 -0800962 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700963 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* If nonlinear, store the file page offset in the pte. */
966 if (page->index != linear_page_index(vma, address))
967 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
968
969 /* Move the dirty bit to the physical page now the pte is gone. */
970 if (pte_dirty(pteval))
971 set_page_dirty(page);
972
Hugh Dickinsedc315f2009-01-06 14:40:11 -0800973 page_remove_rmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 page_cache_release(page);
Hugh Dickins42946212005-10-29 18:16:05 -0700975 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 (*mapcount)--;
977 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700978 pte_unmap_unlock(pte - 1, ptl);
Nick Pigginb291f002008-10-18 20:26:44 -0700979 if (locked_vma)
980 up_read(&vma->vm_mm->mmap_sem);
981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Nick Pigginb291f002008-10-18 20:26:44 -0700984/*
985 * common handling for pages mapped in VM_LOCKED vmas
986 */
987static int try_to_mlock_page(struct page *page, struct vm_area_struct *vma)
988{
989 int mlocked = 0;
990
991 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
992 if (vma->vm_flags & VM_LOCKED) {
993 mlock_vma_page(page);
994 mlocked++; /* really mlocked the page */
995 }
996 up_read(&vma->vm_mm->mmap_sem);
997 }
998 return mlocked;
999}
1000
1001/**
1002 * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
1003 * rmap method
1004 * @page: the page to unmap/unlock
Huang Shijie8051be52009-12-14 17:58:50 -08001005 * @flags: action and flags
Nick Pigginb291f002008-10-18 20:26:44 -07001006 *
1007 * Find all the mappings of a page using the mapping pointer and the vma chains
1008 * contained in the anon_vma struct it points to.
1009 *
1010 * This function is only called from try_to_unmap/try_to_munlock for
1011 * anonymous pages.
1012 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1013 * where the page was found will be held for write. So, we won't recheck
1014 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1015 * 'LOCKED.
1016 */
Andi Kleen14fa31b2009-09-16 11:50:10 +02001017static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct anon_vma *anon_vma;
1020 struct vm_area_struct *vma;
Nick Pigginb291f002008-10-18 20:26:44 -07001021 unsigned int mlocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int ret = SWAP_AGAIN;
Andi Kleen14fa31b2009-09-16 11:50:10 +02001023 int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Nick Pigginb291f002008-10-18 20:26:44 -07001025 if (MLOCK_PAGES && unlikely(unlock))
1026 ret = SWAP_SUCCESS; /* default for try_to_munlock() */
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 anon_vma = page_lock_anon_vma(page);
1029 if (!anon_vma)
1030 return ret;
1031
1032 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Nick Pigginb291f002008-10-18 20:26:44 -07001033 if (MLOCK_PAGES && unlikely(unlock)) {
1034 if (!((vma->vm_flags & VM_LOCKED) &&
1035 page_mapped_in_vma(page, vma)))
1036 continue; /* must visit all unlocked vmas */
1037 ret = SWAP_MLOCK; /* saw at least one mlocked vma */
1038 } else {
Andi Kleen14fa31b2009-09-16 11:50:10 +02001039 ret = try_to_unmap_one(page, vma, flags);
Nick Pigginb291f002008-10-18 20:26:44 -07001040 if (ret == SWAP_FAIL || !page_mapped(page))
1041 break;
1042 }
1043 if (ret == SWAP_MLOCK) {
1044 mlocked = try_to_mlock_page(page, vma);
1045 if (mlocked)
1046 break; /* stop if actually mlocked page */
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -08001049
1050 page_unlock_anon_vma(anon_vma);
Nick Pigginb291f002008-10-18 20:26:44 -07001051
1052 if (mlocked)
1053 ret = SWAP_MLOCK; /* actually mlocked the page */
1054 else if (ret == SWAP_MLOCK)
1055 ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return ret;
1058}
1059
1060/**
Nick Pigginb291f002008-10-18 20:26:44 -07001061 * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1062 * @page: the page to unmap/unlock
Andi Kleen14fa31b2009-09-16 11:50:10 +02001063 * @flags: action and flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 *
1065 * Find all the mappings of a page using the mapping pointer and the vma chains
1066 * contained in the address_space struct it points to.
1067 *
Nick Pigginb291f002008-10-18 20:26:44 -07001068 * This function is only called from try_to_unmap/try_to_munlock for
1069 * object-based pages.
1070 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1071 * where the page was found will be held for write. So, we won't recheck
1072 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1073 * 'LOCKED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Andi Kleen14fa31b2009-09-16 11:50:10 +02001075static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct address_space *mapping = page->mapping;
1078 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1079 struct vm_area_struct *vma;
1080 struct prio_tree_iter iter;
1081 int ret = SWAP_AGAIN;
1082 unsigned long cursor;
1083 unsigned long max_nl_cursor = 0;
1084 unsigned long max_nl_size = 0;
1085 unsigned int mapcount;
Nick Pigginb291f002008-10-18 20:26:44 -07001086 unsigned int mlocked = 0;
Andi Kleen14fa31b2009-09-16 11:50:10 +02001087 int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
Nick Pigginb291f002008-10-18 20:26:44 -07001088
1089 if (MLOCK_PAGES && unlikely(unlock))
1090 ret = SWAP_SUCCESS; /* default for try_to_munlock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 spin_lock(&mapping->i_mmap_lock);
1093 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Nick Pigginb291f002008-10-18 20:26:44 -07001094 if (MLOCK_PAGES && unlikely(unlock)) {
MinChan Kim508b9f82009-02-11 13:04:27 -08001095 if (!((vma->vm_flags & VM_LOCKED) &&
1096 page_mapped_in_vma(page, vma)))
Nick Pigginb291f002008-10-18 20:26:44 -07001097 continue; /* must visit all vmas */
1098 ret = SWAP_MLOCK;
1099 } else {
Andi Kleen14fa31b2009-09-16 11:50:10 +02001100 ret = try_to_unmap_one(page, vma, flags);
Nick Pigginb291f002008-10-18 20:26:44 -07001101 if (ret == SWAP_FAIL || !page_mapped(page))
1102 goto out;
1103 }
1104 if (ret == SWAP_MLOCK) {
1105 mlocked = try_to_mlock_page(page, vma);
1106 if (mlocked)
Huang Shijie7b511592009-12-14 17:58:51 -08001107 goto out; /* stop if actually mlocked page */
Nick Pigginb291f002008-10-18 20:26:44 -07001108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 if (list_empty(&mapping->i_mmap_nonlinear))
1112 goto out;
1113
1114 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1115 shared.vm_set.list) {
Nick Pigginb291f002008-10-18 20:26:44 -07001116 if (MLOCK_PAGES && unlikely(unlock)) {
1117 if (!(vma->vm_flags & VM_LOCKED))
1118 continue; /* must visit all vmas */
1119 ret = SWAP_MLOCK; /* leave mlocked == 0 */
1120 goto out; /* no need to look further */
1121 }
Andi Kleen14fa31b2009-09-16 11:50:10 +02001122 if (!MLOCK_PAGES && !(flags & TTU_IGNORE_MLOCK) &&
1123 (vma->vm_flags & VM_LOCKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 continue;
1125 cursor = (unsigned long) vma->vm_private_data;
1126 if (cursor > max_nl_cursor)
1127 max_nl_cursor = cursor;
1128 cursor = vma->vm_end - vma->vm_start;
1129 if (cursor > max_nl_size)
1130 max_nl_size = cursor;
1131 }
1132
Nick Pigginb291f002008-10-18 20:26:44 -07001133 if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ret = SWAP_FAIL;
1135 goto out;
1136 }
1137
1138 /*
1139 * We don't try to search for this page in the nonlinear vmas,
1140 * and page_referenced wouldn't have found it anyway. Instead
1141 * just walk the nonlinear vmas trying to age and unmap some.
1142 * The mapcount of the page we came in with is irrelevant,
1143 * but even so use it as a guide to how hard we should try?
1144 */
1145 mapcount = page_mapcount(page);
1146 if (!mapcount)
1147 goto out;
1148 cond_resched_lock(&mapping->i_mmap_lock);
1149
1150 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
1151 if (max_nl_cursor == 0)
1152 max_nl_cursor = CLUSTER_SIZE;
1153
1154 do {
1155 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1156 shared.vm_set.list) {
Andi Kleen14fa31b2009-09-16 11:50:10 +02001157 if (!MLOCK_PAGES && !(flags & TTU_IGNORE_MLOCK) &&
Nick Pigginb291f002008-10-18 20:26:44 -07001158 (vma->vm_flags & VM_LOCKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 continue;
1160 cursor = (unsigned long) vma->vm_private_data;
Hugh Dickins839b9682005-09-03 15:54:43 -07001161 while ( cursor < max_nl_cursor &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 cursor < vma->vm_end - vma->vm_start) {
Nick Pigginb291f002008-10-18 20:26:44 -07001163 ret = try_to_unmap_cluster(cursor, &mapcount,
1164 vma, page);
1165 if (ret == SWAP_MLOCK)
1166 mlocked = 2; /* to return below */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 cursor += CLUSTER_SIZE;
1168 vma->vm_private_data = (void *) cursor;
1169 if ((int)mapcount <= 0)
1170 goto out;
1171 }
1172 vma->vm_private_data = (void *) max_nl_cursor;
1173 }
1174 cond_resched_lock(&mapping->i_mmap_lock);
1175 max_nl_cursor += CLUSTER_SIZE;
1176 } while (max_nl_cursor <= max_nl_size);
1177
1178 /*
1179 * Don't loop forever (perhaps all the remaining pages are
1180 * in locked vmas). Reset cursor on all unreserved nonlinear
1181 * vmas, now forgetting on which ones it had fallen behind.
1182 */
Hugh Dickins101d2be2005-11-21 21:32:16 -08001183 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1184 vma->vm_private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185out:
1186 spin_unlock(&mapping->i_mmap_lock);
Nick Pigginb291f002008-10-18 20:26:44 -07001187 if (mlocked)
1188 ret = SWAP_MLOCK; /* actually mlocked the page */
1189 else if (ret == SWAP_MLOCK)
1190 ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return ret;
1192}
1193
1194/**
1195 * try_to_unmap - try to remove all page table mappings to a page
1196 * @page: the page to get unmapped
Andi Kleen14fa31b2009-09-16 11:50:10 +02001197 * @flags: action and flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
1199 * Tries to remove all the page table entries which are mapping this
1200 * page, used in the pageout path. Caller must hold the page lock.
1201 * Return values are:
1202 *
1203 * SWAP_SUCCESS - we succeeded in removing all mappings
1204 * SWAP_AGAIN - we missed a mapping, try again later
1205 * SWAP_FAIL - the page is unswappable
Nick Pigginb291f002008-10-18 20:26:44 -07001206 * SWAP_MLOCK - page is mlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Andi Kleen14fa31b2009-09-16 11:50:10 +02001208int try_to_unmap(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 int ret;
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 BUG_ON(!PageLocked(page));
1213
1214 if (PageAnon(page))
Andi Kleen14fa31b2009-09-16 11:50:10 +02001215 ret = try_to_unmap_anon(page, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 else
Andi Kleen14fa31b2009-09-16 11:50:10 +02001217 ret = try_to_unmap_file(page, flags);
Nick Pigginb291f002008-10-18 20:26:44 -07001218 if (ret != SWAP_MLOCK && !page_mapped(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ret = SWAP_SUCCESS;
1220 return ret;
1221}
Nikita Danilov81b40822005-05-01 08:58:36 -07001222
Nick Pigginb291f002008-10-18 20:26:44 -07001223/**
1224 * try_to_munlock - try to munlock a page
1225 * @page: the page to be munlocked
1226 *
1227 * Called from munlock code. Checks all of the VMAs mapping the page
1228 * to make sure nobody else has this page mlocked. The page will be
1229 * returned with PG_mlocked cleared if no other vmas have it mlocked.
1230 *
1231 * Return values are:
1232 *
1233 * SWAP_SUCCESS - no vma's holding page mlocked.
1234 * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
1235 * SWAP_MLOCK - page is now mlocked.
1236 */
1237int try_to_munlock(struct page *page)
1238{
1239 VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1240
1241 if (PageAnon(page))
Andi Kleen14fa31b2009-09-16 11:50:10 +02001242 return try_to_unmap_anon(page, TTU_MUNLOCK);
Nick Pigginb291f002008-10-18 20:26:44 -07001243 else
Andi Kleen14fa31b2009-09-16 11:50:10 +02001244 return try_to_unmap_file(page, TTU_MUNLOCK);
Nick Pigginb291f002008-10-18 20:26:44 -07001245}
KOSAKI Motohiro68377652009-06-16 15:32:51 -07001246