rmap.c 23 KB

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