rmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * mm/rmap.c - physical to virtual reverse mappings
  3. *
  4. * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
  5. * Released under the General Public License (GPL).
  6. *
  7. * Simple, low overhead reverse mapping scheme.
  8. * Please try to keep this thing as modular as possible.
  9. *
  10. * Provides methods for unmapping each kind of mapped page:
  11. * the anon methods track anonymous pages, and
  12. * the file methods track pages belonging to an inode.
  13. *
  14. * Original design by Rik van Riel <riel@conectiva.com.br> 2001
  15. * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
  16. * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
  17. * Contributions by Hugh Dickins <hugh@veritas.com> 2003, 2004
  18. */
  19. /*
  20. * Lock ordering in mm:
  21. *
  22. * inode->i_sem (while writing or truncating, not reading or faulting)
  23. * inode->i_alloc_sem
  24. *
  25. * When a page fault occurs in writing from user to file, down_read
  26. * of mmap_sem nests within i_sem; in sys_msync, i_sem nests within
  27. * down_read of mmap_sem; i_sem and down_write of mmap_sem are never
  28. * taken together; in truncation, i_sem is taken outermost.
  29. *
  30. * mm->mmap_sem
  31. * page->flags PG_locked (lock_page)
  32. * mapping->i_mmap_lock
  33. * anon_vma->lock
  34. * mm->page_table_lock or pte_lock
  35. * zone->lru_lock (in mark_page_accessed)
  36. * swap_lock (in swap_duplicate, swap_info_get)
  37. * mmlist_lock (in mmput, drain_mmlist and others)
  38. * mapping->private_lock (in __set_page_dirty_buffers)
  39. * inode_lock (in set_page_dirty's __mark_inode_dirty)
  40. * sb_lock (within inode_lock in fs/fs-writeback.c)
  41. * mapping->tree_lock (widely used, in set_page_dirty,
  42. * in arch-dependent flush_dcache_mmap_lock,
  43. * within inode_lock in __sync_single_inode)
  44. */
  45. #include <linux/mm.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/swap.h>
  48. #include <linux/swapops.h>
  49. #include <linux/slab.h>
  50. #include <linux/init.h>
  51. #include <linux/rmap.h>
  52. #include <linux/rcupdate.h>
  53. #include <asm/tlbflush.h>
  54. //#define RMAP_DEBUG /* can be enabled only for debugging */
  55. kmem_cache_t *anon_vma_cachep;
  56. static inline void validate_anon_vma(struct vm_area_struct *find_vma)
  57. {
  58. #ifdef RMAP_DEBUG
  59. struct anon_vma *anon_vma = find_vma->anon_vma;
  60. struct vm_area_struct *vma;
  61. unsigned int mapcount = 0;
  62. int found = 0;
  63. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  64. mapcount++;
  65. BUG_ON(mapcount > 100000);
  66. if (vma == find_vma)
  67. found = 1;
  68. }
  69. BUG_ON(!found);
  70. #endif
  71. }
  72. /* This must be called under the mmap_sem. */
  73. int anon_vma_prepare(struct vm_area_struct *vma)
  74. {
  75. struct anon_vma *anon_vma = vma->anon_vma;
  76. might_sleep();
  77. if (unlikely(!anon_vma)) {
  78. struct mm_struct *mm = vma->vm_mm;
  79. struct anon_vma *allocated, *locked;
  80. anon_vma = find_mergeable_anon_vma(vma);
  81. if (anon_vma) {
  82. allocated = NULL;
  83. locked = anon_vma;
  84. spin_lock(&locked->lock);
  85. } else {
  86. anon_vma = anon_vma_alloc();
  87. if (unlikely(!anon_vma))
  88. return -ENOMEM;
  89. allocated = anon_vma;
  90. locked = NULL;
  91. }
  92. /* page_table_lock to protect against threads */
  93. spin_lock(&mm->page_table_lock);
  94. if (likely(!vma->anon_vma)) {
  95. vma->anon_vma = anon_vma;
  96. list_add(&vma->anon_vma_node, &anon_vma->head);
  97. allocated = NULL;
  98. }
  99. spin_unlock(&mm->page_table_lock);
  100. if (locked)
  101. spin_unlock(&locked->lock);
  102. if (unlikely(allocated))
  103. anon_vma_free(allocated);
  104. }
  105. return 0;
  106. }
  107. void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
  108. {
  109. BUG_ON(vma->anon_vma != next->anon_vma);
  110. list_del(&next->anon_vma_node);
  111. }
  112. void __anon_vma_link(struct vm_area_struct *vma)
  113. {
  114. struct anon_vma *anon_vma = vma->anon_vma;
  115. if (anon_vma) {
  116. list_add(&vma->anon_vma_node, &anon_vma->head);
  117. validate_anon_vma(vma);
  118. }
  119. }
  120. void anon_vma_link(struct vm_area_struct *vma)
  121. {
  122. struct anon_vma *anon_vma = vma->anon_vma;
  123. if (anon_vma) {
  124. spin_lock(&anon_vma->lock);
  125. list_add(&vma->anon_vma_node, &anon_vma->head);
  126. validate_anon_vma(vma);
  127. spin_unlock(&anon_vma->lock);
  128. }
  129. }
  130. void anon_vma_unlink(struct vm_area_struct *vma)
  131. {
  132. struct anon_vma *anon_vma = vma->anon_vma;
  133. int empty;
  134. if (!anon_vma)
  135. return;
  136. spin_lock(&anon_vma->lock);
  137. validate_anon_vma(vma);
  138. list_del(&vma->anon_vma_node);
  139. /* We must garbage collect the anon_vma if it's empty */
  140. empty = list_empty(&anon_vma->head);
  141. spin_unlock(&anon_vma->lock);
  142. if (empty)
  143. anon_vma_free(anon_vma);
  144. }
  145. static void anon_vma_ctor(void *data, kmem_cache_t *cachep, unsigned long flags)
  146. {
  147. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  148. SLAB_CTOR_CONSTRUCTOR) {
  149. struct anon_vma *anon_vma = data;
  150. spin_lock_init(&anon_vma->lock);
  151. INIT_LIST_HEAD(&anon_vma->head);
  152. }
  153. }
  154. void __init anon_vma_init(void)
  155. {
  156. anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
  157. 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor, NULL);
  158. }
  159. /*
  160. * Getting a lock on a stable anon_vma from a page off the LRU is
  161. * tricky: page_lock_anon_vma rely on RCU to guard against the races.
  162. */
  163. static struct anon_vma *page_lock_anon_vma(struct page *page)
  164. {
  165. struct anon_vma *anon_vma = NULL;
  166. unsigned long anon_mapping;
  167. rcu_read_lock();
  168. anon_mapping = (unsigned long) page->mapping;
  169. if (!(anon_mapping & PAGE_MAPPING_ANON))
  170. goto out;
  171. if (!page_mapped(page))
  172. goto out;
  173. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  174. spin_lock(&anon_vma->lock);
  175. out:
  176. rcu_read_unlock();
  177. return anon_vma;
  178. }
  179. /*
  180. * At what user virtual address is page expected in vma?
  181. */
  182. static inline unsigned long
  183. vma_address(struct page *page, struct vm_area_struct *vma)
  184. {
  185. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  186. unsigned long address;
  187. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  188. if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
  189. /* page should be within any vma from prio_tree_next */
  190. BUG_ON(!PageAnon(page));
  191. return -EFAULT;
  192. }
  193. return address;
  194. }
  195. /*
  196. * At what user virtual address is page expected in vma? checking that the
  197. * page matches the vma: currently only used by unuse_process, on anon pages.
  198. */
  199. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  200. {
  201. if (PageAnon(page)) {
  202. if ((void *)vma->anon_vma !=
  203. (void *)page->mapping - PAGE_MAPPING_ANON)
  204. return -EFAULT;
  205. } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
  206. if (vma->vm_file->f_mapping != page->mapping)
  207. return -EFAULT;
  208. } else
  209. return -EFAULT;
  210. return vma_address(page, vma);
  211. }
  212. /*
  213. * Check that @page is mapped at @address into @mm.
  214. *
  215. * On success returns with pte mapped and locked.
  216. */
  217. pte_t *page_check_address(struct page *page, struct mm_struct *mm,
  218. unsigned long address, spinlock_t **ptlp)
  219. {
  220. pgd_t *pgd;
  221. pud_t *pud;
  222. pmd_t *pmd;
  223. pte_t *pte;
  224. spinlock_t *ptl;
  225. pgd = pgd_offset(mm, address);
  226. if (!pgd_present(*pgd))
  227. return NULL;
  228. pud = pud_offset(pgd, address);
  229. if (!pud_present(*pud))
  230. return NULL;
  231. pmd = pmd_offset(pud, address);
  232. if (!pmd_present(*pmd))
  233. return NULL;
  234. pte = pte_offset_map(pmd, address);
  235. /* Make a quick check before getting the lock */
  236. if (!pte_present(*pte)) {
  237. pte_unmap(pte);
  238. return NULL;
  239. }
  240. ptl = pte_lockptr(mm, pmd);
  241. spin_lock(ptl);
  242. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  243. *ptlp = ptl;
  244. return pte;
  245. }
  246. pte_unmap_unlock(pte, ptl);
  247. return NULL;
  248. }
  249. /*
  250. * Subfunctions of page_referenced: page_referenced_one called
  251. * repeatedly from either page_referenced_anon or page_referenced_file.
  252. */
  253. static int page_referenced_one(struct page *page,
  254. struct vm_area_struct *vma, unsigned int *mapcount, int ignore_token)
  255. {
  256. struct mm_struct *mm = vma->vm_mm;
  257. unsigned long address;
  258. pte_t *pte;
  259. spinlock_t *ptl;
  260. int referenced = 0;
  261. address = vma_address(page, vma);
  262. if (address == -EFAULT)
  263. goto out;
  264. pte = page_check_address(page, mm, address, &ptl);
  265. if (!pte)
  266. goto out;
  267. if (ptep_clear_flush_young(vma, address, pte))
  268. referenced++;
  269. /* Pretend the page is referenced if the task has the
  270. swap token and is in the middle of a page fault. */
  271. if (mm != current->mm && !ignore_token && has_swap_token(mm) &&
  272. rwsem_is_locked(&mm->mmap_sem))
  273. referenced++;
  274. (*mapcount)--;
  275. pte_unmap_unlock(pte, ptl);
  276. out:
  277. return referenced;
  278. }
  279. static int page_referenced_anon(struct page *page, int ignore_token)
  280. {
  281. unsigned int mapcount;
  282. struct anon_vma *anon_vma;
  283. struct vm_area_struct *vma;
  284. int referenced = 0;
  285. anon_vma = page_lock_anon_vma(page);
  286. if (!anon_vma)
  287. return referenced;
  288. mapcount = page_mapcount(page);
  289. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  290. referenced += page_referenced_one(page, vma, &mapcount,
  291. ignore_token);
  292. if (!mapcount)
  293. break;
  294. }
  295. spin_unlock(&anon_vma->lock);
  296. return referenced;
  297. }
  298. /**
  299. * page_referenced_file - referenced check for object-based rmap
  300. * @page: the page we're checking references on.
  301. *
  302. * For an object-based mapped page, find all the places it is mapped and
  303. * check/clear the referenced flag. This is done by following the page->mapping
  304. * pointer, then walking the chain of vmas it holds. It returns the number
  305. * of references it found.
  306. *
  307. * This function is only called from page_referenced for object-based pages.
  308. */
  309. static int page_referenced_file(struct page *page, int ignore_token)
  310. {
  311. unsigned int mapcount;
  312. struct address_space *mapping = page->mapping;
  313. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  314. struct vm_area_struct *vma;
  315. struct prio_tree_iter iter;
  316. int referenced = 0;
  317. /*
  318. * The caller's checks on page->mapping and !PageAnon have made
  319. * sure that this is a file page: the check for page->mapping
  320. * excludes the case just before it gets set on an anon page.
  321. */
  322. BUG_ON(PageAnon(page));
  323. /*
  324. * The page lock not only makes sure that page->mapping cannot
  325. * suddenly be NULLified by truncation, it makes sure that the
  326. * structure at mapping cannot be freed and reused yet,
  327. * so we can safely take mapping->i_mmap_lock.
  328. */
  329. BUG_ON(!PageLocked(page));
  330. spin_lock(&mapping->i_mmap_lock);
  331. /*
  332. * i_mmap_lock does not stabilize mapcount at all, but mapcount
  333. * is more likely to be accurate if we note it after spinning.
  334. */
  335. mapcount = page_mapcount(page);
  336. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  337. if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
  338. == (VM_LOCKED|VM_MAYSHARE)) {
  339. referenced++;
  340. break;
  341. }
  342. referenced += page_referenced_one(page, vma, &mapcount,
  343. ignore_token);
  344. if (!mapcount)
  345. break;
  346. }
  347. spin_unlock(&mapping->i_mmap_lock);
  348. return referenced;
  349. }
  350. /**
  351. * page_referenced - test if the page was referenced
  352. * @page: the page to test
  353. * @is_locked: caller holds lock on the page
  354. *
  355. * Quick test_and_clear_referenced for all mappings to a page,
  356. * returns the number of ptes which referenced the page.
  357. */
  358. int page_referenced(struct page *page, int is_locked, int ignore_token)
  359. {
  360. int referenced = 0;
  361. if (!swap_token_default_timeout)
  362. ignore_token = 1;
  363. if (page_test_and_clear_young(page))
  364. referenced++;
  365. if (TestClearPageReferenced(page))
  366. referenced++;
  367. if (page_mapped(page) && page->mapping) {
  368. if (PageAnon(page))
  369. referenced += page_referenced_anon(page, ignore_token);
  370. else if (is_locked)
  371. referenced += page_referenced_file(page, ignore_token);
  372. else if (TestSetPageLocked(page))
  373. referenced++;
  374. else {
  375. if (page->mapping)
  376. referenced += page_referenced_file(page,
  377. ignore_token);
  378. unlock_page(page);
  379. }
  380. }
  381. return referenced;
  382. }
  383. /**
  384. * page_add_anon_rmap - add pte mapping to an anonymous page
  385. * @page: the page to add the mapping to
  386. * @vma: the vm area in which the mapping is added
  387. * @address: the user virtual address mapped
  388. *
  389. * The caller needs to hold the pte lock.
  390. */
  391. void page_add_anon_rmap(struct page *page,
  392. struct vm_area_struct *vma, unsigned long address)
  393. {
  394. if (atomic_inc_and_test(&page->_mapcount)) {
  395. struct anon_vma *anon_vma = vma->anon_vma;
  396. BUG_ON(!anon_vma);
  397. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  398. page->mapping = (struct address_space *) anon_vma;
  399. page->index = linear_page_index(vma, address);
  400. inc_page_state(nr_mapped);
  401. }
  402. /* else checking page index and mapping is racy */
  403. }
  404. /**
  405. * page_add_file_rmap - add pte mapping to a file page
  406. * @page: the page to add the mapping to
  407. *
  408. * The caller needs to hold the pte lock.
  409. */
  410. void page_add_file_rmap(struct page *page)
  411. {
  412. BUG_ON(PageAnon(page));
  413. BUG_ON(!pfn_valid(page_to_pfn(page)));
  414. if (atomic_inc_and_test(&page->_mapcount))
  415. inc_page_state(nr_mapped);
  416. }
  417. /**
  418. * page_remove_rmap - take down pte mapping from a page
  419. * @page: page to remove mapping from
  420. *
  421. * The caller needs to hold the pte lock.
  422. */
  423. void page_remove_rmap(struct page *page)
  424. {
  425. if (atomic_add_negative(-1, &page->_mapcount)) {
  426. BUG_ON(page_mapcount(page) < 0);
  427. /*
  428. * It would be tidy to reset the PageAnon mapping here,
  429. * but that might overwrite a racing page_add_anon_rmap
  430. * which increments mapcount after us but sets mapping
  431. * before us: so leave the reset to free_hot_cold_page,
  432. * and remember that it's only reliable while mapped.
  433. * Leaving it set also helps swapoff to reinstate ptes
  434. * faster for those pages still in swapcache.
  435. */
  436. if (page_test_and_clear_dirty(page))
  437. set_page_dirty(page);
  438. dec_page_state(nr_mapped);
  439. }
  440. }
  441. /*
  442. * Subfunctions of try_to_unmap: try_to_unmap_one called
  443. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  444. */
  445. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma)
  446. {
  447. struct mm_struct *mm = vma->vm_mm;
  448. unsigned long address;
  449. pte_t *pte;
  450. pte_t pteval;
  451. spinlock_t *ptl;
  452. int ret = SWAP_AGAIN;
  453. address = vma_address(page, vma);
  454. if (address == -EFAULT)
  455. goto out;
  456. pte = page_check_address(page, mm, address, &ptl);
  457. if (!pte)
  458. goto out;
  459. /*
  460. * If the page is mlock()d, we cannot swap it out.
  461. * If it's recently referenced (perhaps page_referenced
  462. * skipped over this mm) then we should reactivate it.
  463. *
  464. * Pages belonging to VM_RESERVED regions should not happen here.
  465. */
  466. if ((vma->vm_flags & (VM_LOCKED|VM_RESERVED)) ||
  467. ptep_clear_flush_young(vma, address, pte)) {
  468. ret = SWAP_FAIL;
  469. goto out_unmap;
  470. }
  471. /* Nuke the page table entry. */
  472. flush_cache_page(vma, address, page_to_pfn(page));
  473. pteval = ptep_clear_flush(vma, address, pte);
  474. /* Move the dirty bit to the physical page now the pte is gone. */
  475. if (pte_dirty(pteval))
  476. set_page_dirty(page);
  477. /* Update high watermark before we lower rss */
  478. update_hiwater_rss(mm);
  479. if (PageAnon(page)) {
  480. swp_entry_t entry = { .val = page_private(page) };
  481. /*
  482. * Store the swap location in the pte.
  483. * See handle_pte_fault() ...
  484. */
  485. BUG_ON(!PageSwapCache(page));
  486. swap_duplicate(entry);
  487. if (list_empty(&mm->mmlist)) {
  488. spin_lock(&mmlist_lock);
  489. if (list_empty(&mm->mmlist))
  490. list_add(&mm->mmlist, &init_mm.mmlist);
  491. spin_unlock(&mmlist_lock);
  492. }
  493. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  494. BUG_ON(pte_file(*pte));
  495. dec_mm_counter(mm, anon_rss);
  496. } else
  497. dec_mm_counter(mm, file_rss);
  498. page_remove_rmap(page);
  499. page_cache_release(page);
  500. out_unmap:
  501. pte_unmap_unlock(pte, ptl);
  502. out:
  503. return ret;
  504. }
  505. /*
  506. * objrmap doesn't work for nonlinear VMAs because the assumption that
  507. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  508. * Consequently, given a particular page and its ->index, we cannot locate the
  509. * ptes which are mapping that page without an exhaustive linear search.
  510. *
  511. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  512. * maps the file to which the target page belongs. The ->vm_private_data field
  513. * holds the current cursor into that scan. Successive searches will circulate
  514. * around the vma's virtual address space.
  515. *
  516. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  517. * more scanning pressure is placed against them as well. Eventually pages
  518. * will become fully unmapped and are eligible for eviction.
  519. *
  520. * For very sparsely populated VMAs this is a little inefficient - chances are
  521. * there there won't be many ptes located within the scan cluster. In this case
  522. * maybe we could scan further - to the end of the pte page, perhaps.
  523. */
  524. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  525. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  526. static void try_to_unmap_cluster(unsigned long cursor,
  527. unsigned int *mapcount, struct vm_area_struct *vma)
  528. {
  529. struct mm_struct *mm = vma->vm_mm;
  530. pgd_t *pgd;
  531. pud_t *pud;
  532. pmd_t *pmd;
  533. pte_t *pte;
  534. pte_t pteval;
  535. spinlock_t *ptl;
  536. struct page *page;
  537. unsigned long address;
  538. unsigned long end;
  539. unsigned long pfn;
  540. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  541. end = address + CLUSTER_SIZE;
  542. if (address < vma->vm_start)
  543. address = vma->vm_start;
  544. if (end > vma->vm_end)
  545. end = vma->vm_end;
  546. pgd = pgd_offset(mm, address);
  547. if (!pgd_present(*pgd))
  548. return;
  549. pud = pud_offset(pgd, address);
  550. if (!pud_present(*pud))
  551. return;
  552. pmd = pmd_offset(pud, address);
  553. if (!pmd_present(*pmd))
  554. return;
  555. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  556. /* Update high watermark before we lower rss */
  557. update_hiwater_rss(mm);
  558. for (; address < end; pte++, address += PAGE_SIZE) {
  559. if (!pte_present(*pte))
  560. continue;
  561. pfn = pte_pfn(*pte);
  562. if (unlikely(!pfn_valid(pfn))) {
  563. print_bad_pte(vma, *pte, address);
  564. continue;
  565. }
  566. page = pfn_to_page(pfn);
  567. BUG_ON(PageAnon(page));
  568. if (ptep_clear_flush_young(vma, address, pte))
  569. continue;
  570. /* Nuke the page table entry. */
  571. flush_cache_page(vma, address, pfn);
  572. pteval = ptep_clear_flush(vma, address, pte);
  573. /* If nonlinear, store the file page offset in the pte. */
  574. if (page->index != linear_page_index(vma, address))
  575. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  576. /* Move the dirty bit to the physical page now the pte is gone. */
  577. if (pte_dirty(pteval))
  578. set_page_dirty(page);
  579. page_remove_rmap(page);
  580. page_cache_release(page);
  581. dec_mm_counter(mm, file_rss);
  582. (*mapcount)--;
  583. }
  584. pte_unmap_unlock(pte - 1, ptl);
  585. }
  586. static int try_to_unmap_anon(struct page *page)
  587. {
  588. struct anon_vma *anon_vma;
  589. struct vm_area_struct *vma;
  590. int ret = SWAP_AGAIN;
  591. anon_vma = page_lock_anon_vma(page);
  592. if (!anon_vma)
  593. return ret;
  594. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  595. ret = try_to_unmap_one(page, vma);
  596. if (ret == SWAP_FAIL || !page_mapped(page))
  597. break;
  598. }
  599. spin_unlock(&anon_vma->lock);
  600. return ret;
  601. }
  602. /**
  603. * try_to_unmap_file - unmap file page using the object-based rmap method
  604. * @page: the page to unmap
  605. *
  606. * Find all the mappings of a page using the mapping pointer and the vma chains
  607. * contained in the address_space struct it points to.
  608. *
  609. * This function is only called from try_to_unmap for object-based pages.
  610. */
  611. static int try_to_unmap_file(struct page *page)
  612. {
  613. struct address_space *mapping = page->mapping;
  614. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  615. struct vm_area_struct *vma;
  616. struct prio_tree_iter iter;
  617. int ret = SWAP_AGAIN;
  618. unsigned long cursor;
  619. unsigned long max_nl_cursor = 0;
  620. unsigned long max_nl_size = 0;
  621. unsigned int mapcount;
  622. spin_lock(&mapping->i_mmap_lock);
  623. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  624. ret = try_to_unmap_one(page, vma);
  625. if (ret == SWAP_FAIL || !page_mapped(page))
  626. goto out;
  627. }
  628. if (list_empty(&mapping->i_mmap_nonlinear))
  629. goto out;
  630. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  631. shared.vm_set.list) {
  632. if (vma->vm_flags & (VM_LOCKED|VM_RESERVED))
  633. continue;
  634. cursor = (unsigned long) vma->vm_private_data;
  635. if (cursor > max_nl_cursor)
  636. max_nl_cursor = cursor;
  637. cursor = vma->vm_end - vma->vm_start;
  638. if (cursor > max_nl_size)
  639. max_nl_size = cursor;
  640. }
  641. if (max_nl_size == 0) { /* any nonlinears locked or reserved */
  642. ret = SWAP_FAIL;
  643. goto out;
  644. }
  645. /*
  646. * We don't try to search for this page in the nonlinear vmas,
  647. * and page_referenced wouldn't have found it anyway. Instead
  648. * just walk the nonlinear vmas trying to age and unmap some.
  649. * The mapcount of the page we came in with is irrelevant,
  650. * but even so use it as a guide to how hard we should try?
  651. */
  652. mapcount = page_mapcount(page);
  653. if (!mapcount)
  654. goto out;
  655. cond_resched_lock(&mapping->i_mmap_lock);
  656. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  657. if (max_nl_cursor == 0)
  658. max_nl_cursor = CLUSTER_SIZE;
  659. do {
  660. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  661. shared.vm_set.list) {
  662. if (vma->vm_flags & (VM_LOCKED|VM_RESERVED))
  663. continue;
  664. cursor = (unsigned long) vma->vm_private_data;
  665. while ( cursor < max_nl_cursor &&
  666. cursor < vma->vm_end - vma->vm_start) {
  667. try_to_unmap_cluster(cursor, &mapcount, vma);
  668. cursor += CLUSTER_SIZE;
  669. vma->vm_private_data = (void *) cursor;
  670. if ((int)mapcount <= 0)
  671. goto out;
  672. }
  673. vma->vm_private_data = (void *) max_nl_cursor;
  674. }
  675. cond_resched_lock(&mapping->i_mmap_lock);
  676. max_nl_cursor += CLUSTER_SIZE;
  677. } while (max_nl_cursor <= max_nl_size);
  678. /*
  679. * Don't loop forever (perhaps all the remaining pages are
  680. * in locked vmas). Reset cursor on all unreserved nonlinear
  681. * vmas, now forgetting on which ones it had fallen behind.
  682. */
  683. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  684. shared.vm_set.list) {
  685. if (!(vma->vm_flags & VM_RESERVED))
  686. vma->vm_private_data = NULL;
  687. }
  688. out:
  689. spin_unlock(&mapping->i_mmap_lock);
  690. return ret;
  691. }
  692. /**
  693. * try_to_unmap - try to remove all page table mappings to a page
  694. * @page: the page to get unmapped
  695. *
  696. * Tries to remove all the page table entries which are mapping this
  697. * page, used in the pageout path. Caller must hold the page lock.
  698. * Return values are:
  699. *
  700. * SWAP_SUCCESS - we succeeded in removing all mappings
  701. * SWAP_AGAIN - we missed a mapping, try again later
  702. * SWAP_FAIL - the page is unswappable
  703. */
  704. int try_to_unmap(struct page *page)
  705. {
  706. int ret;
  707. BUG_ON(!PageLocked(page));
  708. if (PageAnon(page))
  709. ret = try_to_unmap_anon(page);
  710. else
  711. ret = try_to_unmap_file(page);
  712. if (!page_mapped(page))
  713. ret = SWAP_SUCCESS;
  714. return ret;
  715. }