blob: e8d639b16c6d3ceaa5b130362be36b9ae6926277 [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
17 * Contributions by Hugh Dickins <hugh@veritas.com> 2003, 2004
18 */
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
41#include <linux/mm.h>
42#include <linux/pagemap.h>
43#include <linux/swap.h>
44#include <linux/swapops.h>
45#include <linux/slab.h>
46#include <linux/init.h>
47#include <linux/rmap.h>
48#include <linux/rcupdate.h>
Christoph Lametera48d07a2006-02-01 03:05:38 -080049#include <linux/module.h>
Nick Piggin7de6b802006-12-22 01:09:33 -080050#include <linux/kallsyms.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080051#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070052#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <asm/tlbflush.h>
55
Pekka Enbergfcc234f2006-03-22 00:08:13 -080056struct kmem_cache *anon_vma_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070058/**
59 * anon_vma_prepare - attach an anon_vma to a memory region
60 * @vma: the memory region in question
61 *
62 * This makes sure the memory mapping described by 'vma' has
63 * an 'anon_vma' attached to it, so that we can associate the
64 * anonymous pages mapped into it with that anon_vma.
65 *
66 * The common case will be that we already have one, but if
67 * if not we either need to find an adjacent mapping that we
68 * can re-use the anon_vma from (very common when the only
69 * reason for splitting a vma has been mprotect()), or we
70 * allocate a new one.
71 *
72 * Anon-vma allocations are very subtle, because we may have
73 * optimistically looked up an anon_vma in page_lock_anon_vma()
74 * and that may actually touch the spinlock even in the newly
75 * allocated vma (it depends on RCU to make sure that the
76 * anon_vma isn't actually destroyed).
77 *
78 * As a result, we need to do proper anon_vma locking even
79 * for the new allocation. At the same time, we do not want
80 * to do any locking for the common case of already having
81 * an anon_vma.
82 *
83 * This must be called with the mmap_sem held for reading.
84 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085int anon_vma_prepare(struct vm_area_struct *vma)
86{
87 struct anon_vma *anon_vma = vma->anon_vma;
88
89 might_sleep();
90 if (unlikely(!anon_vma)) {
91 struct mm_struct *mm = vma->vm_mm;
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070092 struct anon_vma *allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 anon_vma = find_mergeable_anon_vma(vma);
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070095 allocated = NULL;
96 if (!anon_vma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 anon_vma = anon_vma_alloc();
98 if (unlikely(!anon_vma))
99 return -ENOMEM;
100 allocated = anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700102 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* page_table_lock to protect against threads */
105 spin_lock(&mm->page_table_lock);
106 if (likely(!vma->anon_vma)) {
107 vma->anon_vma = anon_vma;
Christoph Lameter06972122006-06-23 02:03:35 -0700108 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 allocated = NULL;
110 }
111 spin_unlock(&mm->page_table_lock);
112
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700113 spin_unlock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (unlikely(allocated))
115 anon_vma_free(allocated);
116 }
117 return 0;
118}
119
120void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
121{
122 BUG_ON(vma->anon_vma != next->anon_vma);
123 list_del(&next->anon_vma_node);
124}
125
126void __anon_vma_link(struct vm_area_struct *vma)
127{
128 struct anon_vma *anon_vma = vma->anon_vma;
129
Hugh Dickins30acbab2007-06-27 14:09:53 -0700130 if (anon_vma)
Christoph Lameter06972122006-06-23 02:03:35 -0700131 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134void anon_vma_link(struct vm_area_struct *vma)
135{
136 struct anon_vma *anon_vma = vma->anon_vma;
137
138 if (anon_vma) {
139 spin_lock(&anon_vma->lock);
Christoph Lameter06972122006-06-23 02:03:35 -0700140 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_unlock(&anon_vma->lock);
142 }
143}
144
145void anon_vma_unlink(struct vm_area_struct *vma)
146{
147 struct anon_vma *anon_vma = vma->anon_vma;
148 int empty;
149
150 if (!anon_vma)
151 return;
152
153 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 list_del(&vma->anon_vma_node);
155
156 /* We must garbage collect the anon_vma if it's empty */
157 empty = list_empty(&anon_vma->head);
158 spin_unlock(&anon_vma->lock);
159
160 if (empty)
161 anon_vma_free(anon_vma);
162}
163
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700164static void anon_vma_ctor(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Christoph Lametera35afb82007-05-16 22:10:57 -0700166 struct anon_vma *anon_vma = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Christoph Lametera35afb82007-05-16 22:10:57 -0700168 spin_lock_init(&anon_vma->lock);
169 INIT_LIST_HEAD(&anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172void __init anon_vma_init(void)
173{
174 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
Paul Mundt20c2df82007-07-20 10:11:58 +0900175 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/*
179 * Getting a lock on a stable anon_vma from a page off the LRU is
180 * tricky: page_lock_anon_vma rely on RCU to guard against the races.
181 */
182static struct anon_vma *page_lock_anon_vma(struct page *page)
183{
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800184 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned long anon_mapping;
186
187 rcu_read_lock();
188 anon_mapping = (unsigned long) page->mapping;
189 if (!(anon_mapping & PAGE_MAPPING_ANON))
190 goto out;
191 if (!page_mapped(page))
192 goto out;
193
194 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
195 spin_lock(&anon_vma->lock);
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800196 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197out:
198 rcu_read_unlock();
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800199 return NULL;
200}
201
202static void page_unlock_anon_vma(struct anon_vma *anon_vma)
203{
204 spin_unlock(&anon_vma->lock);
205 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/*
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800209 * At what user virtual address is page expected in @vma?
210 * Returns virtual address or -EFAULT if page's index/offset is not
211 * within the range mapped the @vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
213static inline unsigned long
214vma_address(struct page *page, struct vm_area_struct *vma)
215{
216 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
217 unsigned long address;
218
219 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
220 if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800221 /* page should be within @vma mapping range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EFAULT;
223 }
224 return address;
225}
226
227/*
228 * At what user virtual address is page expected in vma? checking that the
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800229 * page matches the vma: currently only used on anon pages, by unuse_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
232{
233 if (PageAnon(page)) {
234 if ((void *)vma->anon_vma !=
235 (void *)page->mapping - PAGE_MAPPING_ANON)
236 return -EFAULT;
237 } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800238 if (!vma->vm_file ||
239 vma->vm_file->f_mapping != page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EFAULT;
241 } else
242 return -EFAULT;
243 return vma_address(page, vma);
244}
245
246/*
Nikita Danilov81b40822005-05-01 08:58:36 -0700247 * Check that @page is mapped at @address into @mm.
248 *
Nick Piggin479db0b2008-08-20 14:09:18 -0700249 * If @sync is false, page_check_address may perform a racy check to avoid
250 * the page table lock when the pte is not present (helpful when reclaiming
251 * highly shared pages).
252 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700253 * On success returns with pte mapped and locked.
Nikita Danilov81b40822005-05-01 08:58:36 -0700254 */
Carsten Otteceffc072005-06-23 22:05:25 -0700255pte_t *page_check_address(struct page *page, struct mm_struct *mm,
Nick Piggin479db0b2008-08-20 14:09:18 -0700256 unsigned long address, spinlock_t **ptlp, int sync)
Nikita Danilov81b40822005-05-01 08:58:36 -0700257{
258 pgd_t *pgd;
259 pud_t *pud;
260 pmd_t *pmd;
261 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700262 spinlock_t *ptl;
Nikita Danilov81b40822005-05-01 08:58:36 -0700263
Nikita Danilov81b40822005-05-01 08:58:36 -0700264 pgd = pgd_offset(mm, address);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700265 if (!pgd_present(*pgd))
266 return NULL;
267
268 pud = pud_offset(pgd, address);
269 if (!pud_present(*pud))
270 return NULL;
271
272 pmd = pmd_offset(pud, address);
273 if (!pmd_present(*pmd))
274 return NULL;
275
276 pte = pte_offset_map(pmd, address);
277 /* Make a quick check before getting the lock */
Nick Piggin479db0b2008-08-20 14:09:18 -0700278 if (!sync && !pte_present(*pte)) {
Hugh Dickinsc0718802005-10-29 18:16:31 -0700279 pte_unmap(pte);
280 return NULL;
Nikita Danilov81b40822005-05-01 08:58:36 -0700281 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700282
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700283 ptl = pte_lockptr(mm, pmd);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700284 spin_lock(ptl);
285 if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
286 *ptlp = ptl;
287 return pte;
288 }
289 pte_unmap_unlock(pte, ptl);
290 return NULL;
Nikita Danilov81b40822005-05-01 08:58:36 -0700291}
292
293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * Subfunctions of page_referenced: page_referenced_one called
295 * repeatedly from either page_referenced_anon or page_referenced_file.
296 */
297static int page_referenced_one(struct page *page,
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800298 struct vm_area_struct *vma, unsigned int *mapcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct mm_struct *mm = vma->vm_mm;
301 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700303 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int referenced = 0;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 address = vma_address(page, vma);
307 if (address == -EFAULT)
308 goto out;
309
Nick Piggin479db0b2008-08-20 14:09:18 -0700310 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700311 if (!pte)
312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800314 if (vma->vm_flags & VM_LOCKED) {
315 referenced++;
316 *mapcount = 1; /* break early from loop */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700317 } else if (ptep_clear_flush_young_notify(vma, address, pte))
Hugh Dickinsc0718802005-10-29 18:16:31 -0700318 referenced++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Hugh Dickinsc0718802005-10-29 18:16:31 -0700320 /* Pretend the page is referenced if the task has the
321 swap token and is in the middle of a page fault. */
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800322 if (mm != current->mm && has_swap_token(mm) &&
Hugh Dickinsc0718802005-10-29 18:16:31 -0700323 rwsem_is_locked(&mm->mmap_sem))
324 referenced++;
325
326 (*mapcount)--;
327 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328out:
329 return referenced;
330}
331
Balbir Singhbed71612008-02-07 00:14:01 -0800332static int page_referenced_anon(struct page *page,
333 struct mem_cgroup *mem_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 unsigned int mapcount;
336 struct anon_vma *anon_vma;
337 struct vm_area_struct *vma;
338 int referenced = 0;
339
340 anon_vma = page_lock_anon_vma(page);
341 if (!anon_vma)
342 return referenced;
343
344 mapcount = page_mapcount(page);
345 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Balbir Singhbed71612008-02-07 00:14:01 -0800346 /*
347 * If we are reclaiming on behalf of a cgroup, skip
348 * counting on behalf of references from different
349 * cgroups
350 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800351 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800352 continue;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800353 referenced += page_referenced_one(page, vma, &mapcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!mapcount)
355 break;
356 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800357
358 page_unlock_anon_vma(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return referenced;
360}
361
362/**
363 * page_referenced_file - referenced check for object-based rmap
364 * @page: the page we're checking references on.
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700365 * @mem_cont: target memory controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
367 * For an object-based mapped page, find all the places it is mapped and
368 * check/clear the referenced flag. This is done by following the page->mapping
369 * pointer, then walking the chain of vmas it holds. It returns the number
370 * of references it found.
371 *
372 * This function is only called from page_referenced for object-based pages.
373 */
Balbir Singhbed71612008-02-07 00:14:01 -0800374static int page_referenced_file(struct page *page,
375 struct mem_cgroup *mem_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 unsigned int mapcount;
378 struct address_space *mapping = page->mapping;
379 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
380 struct vm_area_struct *vma;
381 struct prio_tree_iter iter;
382 int referenced = 0;
383
384 /*
385 * The caller's checks on page->mapping and !PageAnon have made
386 * sure that this is a file page: the check for page->mapping
387 * excludes the case just before it gets set on an anon page.
388 */
389 BUG_ON(PageAnon(page));
390
391 /*
392 * The page lock not only makes sure that page->mapping cannot
393 * suddenly be NULLified by truncation, it makes sure that the
394 * structure at mapping cannot be freed and reused yet,
395 * so we can safely take mapping->i_mmap_lock.
396 */
397 BUG_ON(!PageLocked(page));
398
399 spin_lock(&mapping->i_mmap_lock);
400
401 /*
402 * i_mmap_lock does not stabilize mapcount at all, but mapcount
403 * is more likely to be accurate if we note it after spinning.
404 */
405 mapcount = page_mapcount(page);
406
407 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Balbir Singhbed71612008-02-07 00:14:01 -0800408 /*
409 * If we are reclaiming on behalf of a cgroup, skip
410 * counting on behalf of references from different
411 * cgroups
412 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800413 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800414 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
416 == (VM_LOCKED|VM_MAYSHARE)) {
417 referenced++;
418 break;
419 }
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800420 referenced += page_referenced_one(page, vma, &mapcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!mapcount)
422 break;
423 }
424
425 spin_unlock(&mapping->i_mmap_lock);
426 return referenced;
427}
428
429/**
430 * page_referenced - test if the page was referenced
431 * @page: the page to test
432 * @is_locked: caller holds lock on the page
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700433 * @mem_cont: target memory controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *
435 * Quick test_and_clear_referenced for all mappings to a page,
436 * returns the number of ptes which referenced the page.
437 */
Balbir Singhbed71612008-02-07 00:14:01 -0800438int page_referenced(struct page *page, int is_locked,
439 struct mem_cgroup *mem_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 int referenced = 0;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (TestClearPageReferenced(page))
444 referenced++;
445
446 if (page_mapped(page) && page->mapping) {
447 if (PageAnon(page))
Balbir Singhbed71612008-02-07 00:14:01 -0800448 referenced += page_referenced_anon(page, mem_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 else if (is_locked)
Balbir Singhbed71612008-02-07 00:14:01 -0800450 referenced += page_referenced_file(page, mem_cont);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200451 else if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 referenced++;
453 else {
454 if (page->mapping)
Balbir Singhbed71612008-02-07 00:14:01 -0800455 referenced +=
456 page_referenced_file(page, mem_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 unlock_page(page);
458 }
459 }
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100460
461 if (page_test_and_clear_young(page))
462 referenced++;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return referenced;
465}
466
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700467static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
468{
469 struct mm_struct *mm = vma->vm_mm;
470 unsigned long address;
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100471 pte_t *pte;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700472 spinlock_t *ptl;
473 int ret = 0;
474
475 address = vma_address(page, vma);
476 if (address == -EFAULT)
477 goto out;
478
Nick Piggin479db0b2008-08-20 14:09:18 -0700479 pte = page_check_address(page, mm, address, &ptl, 1);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700480 if (!pte)
481 goto out;
482
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100483 if (pte_dirty(*pte) || pte_write(*pte)) {
484 pte_t entry;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700485
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100486 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700487 entry = ptep_clear_flush_notify(vma, address, pte);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100488 entry = pte_wrprotect(entry);
489 entry = pte_mkclean(entry);
Al Virod6e88e62006-12-29 16:48:35 -0800490 set_pte_at(mm, address, pte, entry);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100491 ret = 1;
492 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700493
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700494 pte_unmap_unlock(pte, ptl);
495out:
496 return ret;
497}
498
499static int page_mkclean_file(struct address_space *mapping, struct page *page)
500{
501 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
502 struct vm_area_struct *vma;
503 struct prio_tree_iter iter;
504 int ret = 0;
505
506 BUG_ON(PageAnon(page));
507
508 spin_lock(&mapping->i_mmap_lock);
509 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
510 if (vma->vm_flags & VM_SHARED)
511 ret += page_mkclean_one(page, vma);
512 }
513 spin_unlock(&mapping->i_mmap_lock);
514 return ret;
515}
516
517int page_mkclean(struct page *page)
518{
519 int ret = 0;
520
521 BUG_ON(!PageLocked(page));
522
523 if (page_mapped(page)) {
524 struct address_space *mapping = page_mapping(page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100525 if (mapping) {
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700526 ret = page_mkclean_file(mapping, page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100527 if (page_test_dirty(page)) {
528 page_clear_dirty(page);
529 ret = 1;
530 }
Martin Schwidefsky6c210482007-04-27 16:01:57 +0200531 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700532 }
533
534 return ret;
535}
Jaya Kumar60b59be2007-05-08 00:37:37 -0700536EXPORT_SYMBOL_GPL(page_mkclean);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700539 * __page_set_anon_rmap - setup new anonymous rmap
Nick Piggin9617d952006-01-06 00:11:12 -0800540 * @page: the page to add the mapping to
541 * @vma: the vm area in which the mapping is added
542 * @address: the user virtual address mapped
543 */
544static void __page_set_anon_rmap(struct page *page,
545 struct vm_area_struct *vma, unsigned long address)
546{
547 struct anon_vma *anon_vma = vma->anon_vma;
548
549 BUG_ON(!anon_vma);
550 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
551 page->mapping = (struct address_space *) anon_vma;
552
553 page->index = linear_page_index(vma, address);
554
Nick Piggina74609f2006-01-06 00:11:20 -0800555 /*
556 * nr_mapped state can be updated without turning off
557 * interrupts because it is not modified via interrupt.
558 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700559 __inc_zone_page_state(page, NR_ANON_PAGES);
Nick Piggin9617d952006-01-06 00:11:12 -0800560}
561
562/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700563 * __page_check_anon_rmap - sanity check anonymous rmap addition
Nick Pigginc97a9e12007-05-16 22:11:21 -0700564 * @page: the page to add the mapping to
565 * @vma: the vm area in which the mapping is added
566 * @address: the user virtual address mapped
567 */
568static void __page_check_anon_rmap(struct page *page,
569 struct vm_area_struct *vma, unsigned long address)
570{
571#ifdef CONFIG_DEBUG_VM
572 /*
573 * The page's anon-rmap details (mapping and index) are guaranteed to
574 * be set up correctly at this point.
575 *
576 * We have exclusion against page_add_anon_rmap because the caller
577 * always holds the page locked, except if called from page_dup_rmap,
578 * in which case the page is already known to be setup.
579 *
580 * We have exclusion against page_add_new_anon_rmap because those pages
581 * are initially only visible via the pagetables, and the pte is locked
582 * over the call to page_add_new_anon_rmap.
583 */
584 struct anon_vma *anon_vma = vma->anon_vma;
585 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
586 BUG_ON(page->mapping != (struct address_space *)anon_vma);
587 BUG_ON(page->index != linear_page_index(vma, address));
588#endif
589}
590
591/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * page_add_anon_rmap - add pte mapping to an anonymous page
593 * @page: the page to add the mapping to
594 * @vma: the vm area in which the mapping is added
595 * @address: the user virtual address mapped
596 *
Nick Pigginc97a9e12007-05-16 22:11:21 -0700597 * The caller needs to hold the pte lock and the page must be locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
599void page_add_anon_rmap(struct page *page,
600 struct vm_area_struct *vma, unsigned long address)
601{
Nick Pigginc97a9e12007-05-16 22:11:21 -0700602 VM_BUG_ON(!PageLocked(page));
603 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Nick Piggin9617d952006-01-06 00:11:12 -0800604 if (atomic_inc_and_test(&page->_mapcount))
605 __page_set_anon_rmap(page, vma, address);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700606 else
Nick Pigginc97a9e12007-05-16 22:11:21 -0700607 __page_check_anon_rmap(page, vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700610/**
Nick Piggin9617d952006-01-06 00:11:12 -0800611 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
612 * @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 *
616 * Same as page_add_anon_rmap but must only be called on *new* pages.
617 * This means the inc-and-test can be bypassed.
Nick Pigginc97a9e12007-05-16 22:11:21 -0700618 * Page does not have to be locked.
Nick Piggin9617d952006-01-06 00:11:12 -0800619 */
620void page_add_new_anon_rmap(struct page *page,
621 struct vm_area_struct *vma, unsigned long address)
622{
Nick Pigginc97a9e12007-05-16 22:11:21 -0700623 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Nick Piggin9617d952006-01-06 00:11:12 -0800624 atomic_set(&page->_mapcount, 0); /* elevate count by 1 (starts at -1) */
625 __page_set_anon_rmap(page, vma, address);
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
629 * page_add_file_rmap - add pte mapping to a file page
630 * @page: the page to add the mapping to
631 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700632 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
634void page_add_file_rmap(struct page *page)
635{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (atomic_inc_and_test(&page->_mapcount))
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700637 __inc_zone_page_state(page, NR_FILE_MAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Nick Pigginc97a9e12007-05-16 22:11:21 -0700640#ifdef CONFIG_DEBUG_VM
641/**
642 * page_dup_rmap - duplicate pte mapping to a page
643 * @page: the page to add the mapping to
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700644 * @vma: the vm area being duplicated
645 * @address: the user virtual address mapped
Nick Pigginc97a9e12007-05-16 22:11:21 -0700646 *
647 * For copy_page_range only: minimal extract from page_add_file_rmap /
648 * page_add_anon_rmap, avoiding unnecessary tests (already checked) so it's
649 * quicker.
650 *
651 * The caller needs to hold the pte lock.
652 */
653void page_dup_rmap(struct page *page, struct vm_area_struct *vma, unsigned long address)
654{
655 BUG_ON(page_mapcount(page) == 0);
656 if (PageAnon(page))
657 __page_check_anon_rmap(page, vma, address);
658 atomic_inc(&page->_mapcount);
659}
660#endif
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/**
663 * page_remove_rmap - take down pte mapping from a page
664 * @page: page to remove mapping from
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700665 * @vma: the vm area in which the mapping is removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700667 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Nick Piggin7de6b802006-12-22 01:09:33 -0800669void page_remove_rmap(struct page *page, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (atomic_add_negative(-1, &page->_mapcount)) {
Nick Pigginb7ab7952006-03-22 00:08:42 -0800672 if (unlikely(page_mapcount(page) < 0)) {
Dave Jonesef2bf0d2006-01-08 01:01:00 -0800673 printk (KERN_EMERG "Eeek! page_mapcount(page) went negative! (%d)\n", page_mapcount(page));
Nick Piggin7de6b802006-12-22 01:09:33 -0800674 printk (KERN_EMERG " page pfn = %lx\n", page_to_pfn(page));
Dave Jonesef2bf0d2006-01-08 01:01:00 -0800675 printk (KERN_EMERG " page->flags = %lx\n", page->flags);
676 printk (KERN_EMERG " page->count = %x\n", page_count(page));
677 printk (KERN_EMERG " page->mapping = %p\n", page->mapping);
Nick Piggin7de6b802006-12-22 01:09:33 -0800678 print_symbol (KERN_EMERG " vma->vm_ops = %s\n", (unsigned long)vma->vm_ops);
Nick Piggin54cb8822007-07-19 01:46:59 -0700679 if (vma->vm_ops) {
Nick Piggin54cb8822007-07-19 01:46:59 -0700680 print_symbol (KERN_EMERG " vma->vm_ops->fault = %s\n", (unsigned long)vma->vm_ops->fault);
681 }
Nick Piggin7de6b802006-12-22 01:09:33 -0800682 if (vma->vm_file && vma->vm_file->f_op)
683 print_symbol (KERN_EMERG " vma->vm_file->f_op->mmap = %s\n", (unsigned long)vma->vm_file->f_op->mmap);
Dave Jonesb16bc642006-10-11 01:21:27 -0700684 BUG();
Dave Jonesef2bf0d2006-01-08 01:01:00 -0800685 }
Dave Jonesb16bc642006-10-11 01:21:27 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /*
Hugh Dickins16f8c5b2008-08-20 14:09:04 -0700688 * Now that the last pte has gone, s390 must transfer dirty
689 * flag from storage key to struct page. We can usually skip
690 * this if the page is anon, so about to be freed; but perhaps
691 * not if it's in swapcache - there might be another pte slot
692 * containing the swap entry, but page not yet written to swap.
693 */
694 if ((!PageAnon(page) || PageSwapCache(page)) &&
695 page_test_dirty(page)) {
696 page_clear_dirty(page);
697 set_page_dirty(page);
698 }
699
700 mem_cgroup_uncharge_page(page);
701 __dec_zone_page_state(page,
702 PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
703 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * It would be tidy to reset the PageAnon mapping here,
705 * but that might overwrite a racing page_add_anon_rmap
706 * which increments mapcount after us but sets mapping
707 * before us: so leave the reset to free_hot_cold_page,
708 * and remember that it's only reliable while mapped.
709 * Leaving it set also helps swapoff to reinstate ptes
710 * faster for those pages still in swapcache.
711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713}
714
715/*
716 * Subfunctions of try_to_unmap: try_to_unmap_one called
717 * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
718 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800719static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
Christoph Lameter73523492006-06-23 02:03:27 -0700720 int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct mm_struct *mm = vma->vm_mm;
723 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 pte_t *pte;
725 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700726 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int ret = SWAP_AGAIN;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 address = vma_address(page, vma);
730 if (address == -EFAULT)
731 goto out;
732
Nick Piggin479db0b2008-08-20 14:09:18 -0700733 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700734 if (!pte)
Nikita Danilov81b40822005-05-01 08:58:36 -0700735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * If the page is mlock()d, we cannot swap it out.
739 * If it's recently referenced (perhaps page_referenced
740 * skipped over this mm) then we should reactivate it.
741 */
Christoph Lametere6a15302006-06-25 05:46:49 -0700742 if (!migration && ((vma->vm_flags & VM_LOCKED) ||
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700743 (ptep_clear_flush_young_notify(vma, address, pte)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 ret = SWAP_FAIL;
745 goto out_unmap;
746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Nuke the page table entry. */
749 flush_cache_page(vma, address, page_to_pfn(page));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700750 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* Move the dirty bit to the physical page now the pte is gone. */
753 if (pte_dirty(pteval))
754 set_page_dirty(page);
755
Hugh Dickins365e9c872005-10-29 18:16:18 -0700756 /* Update high watermark before we lower rss */
757 update_hiwater_rss(mm);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (PageAnon(page)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700760 swp_entry_t entry = { .val = page_private(page) };
Christoph Lameter06972122006-06-23 02:03:35 -0700761
762 if (PageSwapCache(page)) {
763 /*
764 * Store the swap location in the pte.
765 * See handle_pte_fault() ...
766 */
767 swap_duplicate(entry);
768 if (list_empty(&mm->mmlist)) {
769 spin_lock(&mmlist_lock);
770 if (list_empty(&mm->mmlist))
771 list_add(&mm->mmlist, &init_mm.mmlist);
772 spin_unlock(&mmlist_lock);
773 }
Christoph Lameter442c9132006-06-23 02:03:38 -0700774 dec_mm_counter(mm, anon_rss);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700775#ifdef CONFIG_MIGRATION
Christoph Lameter06972122006-06-23 02:03:35 -0700776 } else {
777 /*
778 * Store the pfn of the page in a special migration
779 * pte. do_swap_page() will wait until the migration
780 * pte is removed and then restart fault handling.
781 */
782 BUG_ON(!migration);
783 entry = make_migration_entry(page, pte_write(pteval));
Christoph Lameter04e62a22006-06-23 02:03:38 -0700784#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
787 BUG_ON(pte_file(*pte));
Hugh Dickins42946212005-10-29 18:16:05 -0700788 } else
Christoph Lameter04e62a22006-06-23 02:03:38 -0700789#ifdef CONFIG_MIGRATION
790 if (migration) {
791 /* Establish migration entry for a file page */
792 swp_entry_t entry;
793 entry = make_migration_entry(page, pte_write(pteval));
794 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
795 } else
796#endif
Hugh Dickins42946212005-10-29 18:16:05 -0700797 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Christoph Lameter04e62a22006-06-23 02:03:38 -0700799
Nick Piggin7de6b802006-12-22 01:09:33 -0800800 page_remove_rmap(page, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 page_cache_release(page);
802
803out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700804 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805out:
806 return ret;
807}
808
809/*
810 * objrmap doesn't work for nonlinear VMAs because the assumption that
811 * offset-into-file correlates with offset-into-virtual-addresses does not hold.
812 * Consequently, given a particular page and its ->index, we cannot locate the
813 * ptes which are mapping that page without an exhaustive linear search.
814 *
815 * So what this code does is a mini "virtual scan" of each nonlinear VMA which
816 * maps the file to which the target page belongs. The ->vm_private_data field
817 * holds the current cursor into that scan. Successive searches will circulate
818 * around the vma's virtual address space.
819 *
820 * So as more replacement pressure is applied to the pages in a nonlinear VMA,
821 * more scanning pressure is placed against them as well. Eventually pages
822 * will become fully unmapped and are eligible for eviction.
823 *
824 * For very sparsely populated VMAs this is a little inefficient - chances are
825 * there there won't be many ptes located within the scan cluster. In this case
826 * maybe we could scan further - to the end of the pte page, perhaps.
827 */
828#define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
829#define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
830
831static void try_to_unmap_cluster(unsigned long cursor,
832 unsigned int *mapcount, struct vm_area_struct *vma)
833{
834 struct mm_struct *mm = vma->vm_mm;
835 pgd_t *pgd;
836 pud_t *pud;
837 pmd_t *pmd;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700838 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700840 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct page *page;
842 unsigned long address;
843 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 address = (vma->vm_start + cursor) & CLUSTER_MASK;
846 end = address + CLUSTER_SIZE;
847 if (address < vma->vm_start)
848 address = vma->vm_start;
849 if (end > vma->vm_end)
850 end = vma->vm_end;
851
852 pgd = pgd_offset(mm, address);
853 if (!pgd_present(*pgd))
Hugh Dickinsc0718802005-10-29 18:16:31 -0700854 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 pud = pud_offset(pgd, address);
857 if (!pud_present(*pud))
Hugh Dickinsc0718802005-10-29 18:16:31 -0700858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 pmd = pmd_offset(pud, address);
861 if (!pmd_present(*pmd))
Hugh Dickinsc0718802005-10-29 18:16:31 -0700862 return;
863
864 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Hugh Dickins365e9c872005-10-29 18:16:18 -0700866 /* Update high watermark before we lower rss */
867 update_hiwater_rss(mm);
868
Hugh Dickinsc0718802005-10-29 18:16:31 -0700869 for (; address < end; pte++, address += PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!pte_present(*pte))
871 continue;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800872 page = vm_normal_page(vma, address, *pte);
873 BUG_ON(!page || PageAnon(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700875 if (ptep_clear_flush_young_notify(vma, address, pte))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 continue;
877
878 /* Nuke the page table entry. */
Ben Collinseca35132005-11-29 11:45:26 -0800879 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700880 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* If nonlinear, store the file page offset in the pte. */
883 if (page->index != linear_page_index(vma, address))
884 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
885
886 /* Move the dirty bit to the physical page now the pte is gone. */
887 if (pte_dirty(pteval))
888 set_page_dirty(page);
889
Nick Piggin7de6b802006-12-22 01:09:33 -0800890 page_remove_rmap(page, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 page_cache_release(page);
Hugh Dickins42946212005-10-29 18:16:05 -0700892 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 (*mapcount)--;
894 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700895 pte_unmap_unlock(pte - 1, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Christoph Lameter73523492006-06-23 02:03:27 -0700898static int try_to_unmap_anon(struct page *page, int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 struct anon_vma *anon_vma;
901 struct vm_area_struct *vma;
902 int ret = SWAP_AGAIN;
903
904 anon_vma = page_lock_anon_vma(page);
905 if (!anon_vma)
906 return ret;
907
908 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Christoph Lameter73523492006-06-23 02:03:27 -0700909 ret = try_to_unmap_one(page, vma, migration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (ret == SWAP_FAIL || !page_mapped(page))
911 break;
912 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800913
914 page_unlock_anon_vma(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return ret;
916}
917
918/**
919 * try_to_unmap_file - unmap file page using the object-based rmap method
920 * @page: the page to unmap
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700921 * @migration: migration flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 *
923 * Find all the mappings of a page using the mapping pointer and the vma chains
924 * contained in the address_space struct it points to.
925 *
926 * This function is only called from try_to_unmap for object-based pages.
927 */
Christoph Lameter73523492006-06-23 02:03:27 -0700928static int try_to_unmap_file(struct page *page, int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct address_space *mapping = page->mapping;
931 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
932 struct vm_area_struct *vma;
933 struct prio_tree_iter iter;
934 int ret = SWAP_AGAIN;
935 unsigned long cursor;
936 unsigned long max_nl_cursor = 0;
937 unsigned long max_nl_size = 0;
938 unsigned int mapcount;
939
940 spin_lock(&mapping->i_mmap_lock);
941 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Christoph Lameter73523492006-06-23 02:03:27 -0700942 ret = try_to_unmap_one(page, vma, migration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (ret == SWAP_FAIL || !page_mapped(page))
944 goto out;
945 }
946
947 if (list_empty(&mapping->i_mmap_nonlinear))
948 goto out;
949
950 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
951 shared.vm_set.list) {
Christoph Lametere6a15302006-06-25 05:46:49 -0700952 if ((vma->vm_flags & VM_LOCKED) && !migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 continue;
954 cursor = (unsigned long) vma->vm_private_data;
955 if (cursor > max_nl_cursor)
956 max_nl_cursor = cursor;
957 cursor = vma->vm_end - vma->vm_start;
958 if (cursor > max_nl_size)
959 max_nl_size = cursor;
960 }
961
962 if (max_nl_size == 0) { /* any nonlinears locked or reserved */
963 ret = SWAP_FAIL;
964 goto out;
965 }
966
967 /*
968 * We don't try to search for this page in the nonlinear vmas,
969 * and page_referenced wouldn't have found it anyway. Instead
970 * just walk the nonlinear vmas trying to age and unmap some.
971 * The mapcount of the page we came in with is irrelevant,
972 * but even so use it as a guide to how hard we should try?
973 */
974 mapcount = page_mapcount(page);
975 if (!mapcount)
976 goto out;
977 cond_resched_lock(&mapping->i_mmap_lock);
978
979 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
980 if (max_nl_cursor == 0)
981 max_nl_cursor = CLUSTER_SIZE;
982
983 do {
984 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
985 shared.vm_set.list) {
Christoph Lametere6a15302006-06-25 05:46:49 -0700986 if ((vma->vm_flags & VM_LOCKED) && !migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 continue;
988 cursor = (unsigned long) vma->vm_private_data;
Hugh Dickins839b9682005-09-03 15:54:43 -0700989 while ( cursor < max_nl_cursor &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 cursor < vma->vm_end - vma->vm_start) {
991 try_to_unmap_cluster(cursor, &mapcount, vma);
992 cursor += CLUSTER_SIZE;
993 vma->vm_private_data = (void *) cursor;
994 if ((int)mapcount <= 0)
995 goto out;
996 }
997 vma->vm_private_data = (void *) max_nl_cursor;
998 }
999 cond_resched_lock(&mapping->i_mmap_lock);
1000 max_nl_cursor += CLUSTER_SIZE;
1001 } while (max_nl_cursor <= max_nl_size);
1002
1003 /*
1004 * Don't loop forever (perhaps all the remaining pages are
1005 * in locked vmas). Reset cursor on all unreserved nonlinear
1006 * vmas, now forgetting on which ones it had fallen behind.
1007 */
Hugh Dickins101d2be2005-11-21 21:32:16 -08001008 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1009 vma->vm_private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010out:
1011 spin_unlock(&mapping->i_mmap_lock);
1012 return ret;
1013}
1014
1015/**
1016 * try_to_unmap - try to remove all page table mappings to a page
1017 * @page: the page to get unmapped
Randy Dunlap43d8eac2008-03-19 17:00:43 -07001018 * @migration: migration flag
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 *
1020 * Tries to remove all the page table entries which are mapping this
1021 * page, used in the pageout path. Caller must hold the page lock.
1022 * Return values are:
1023 *
1024 * SWAP_SUCCESS - we succeeded in removing all mappings
1025 * SWAP_AGAIN - we missed a mapping, try again later
1026 * SWAP_FAIL - the page is unswappable
1027 */
Christoph Lameter73523492006-06-23 02:03:27 -07001028int try_to_unmap(struct page *page, int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 int ret;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 BUG_ON(!PageLocked(page));
1033
1034 if (PageAnon(page))
Christoph Lameter73523492006-06-23 02:03:27 -07001035 ret = try_to_unmap_anon(page, migration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 else
Christoph Lameter73523492006-06-23 02:03:27 -07001037 ret = try_to_unmap_file(page, migration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (!page_mapped(page))
1040 ret = SWAP_SUCCESS;
1041 return ret;
1042}
Nikita Danilov81b40822005-05-01 08:58:36 -07001043