rmap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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_mutex (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_mutex; in sys_msync, i_mutex nests within
  27. * down_read of mmap_sem; i_mutex and down_write of mmap_sem are never
  28. * taken together; in truncation, i_mutex 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, isolate_lru_page)
  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 <linux/module.h>
  54. #include <asm/tlbflush.h>
  55. //#define RMAP_DEBUG /* can be enabled only for debugging */
  56. struct kmem_cache *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, struct kmem_cache *cachep,
  147. unsigned long flags)
  148. {
  149. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  150. SLAB_CTOR_CONSTRUCTOR) {
  151. struct anon_vma *anon_vma = data;
  152. spin_lock_init(&anon_vma->lock);
  153. INIT_LIST_HEAD(&anon_vma->head);
  154. }
  155. }
  156. void __init anon_vma_init(void)
  157. {
  158. anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
  159. 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor, NULL);
  160. }
  161. /*
  162. * Getting a lock on a stable anon_vma from a page off the LRU is
  163. * tricky: page_lock_anon_vma rely on RCU to guard against the races.
  164. */
  165. static struct anon_vma *page_lock_anon_vma(struct page *page)
  166. {
  167. struct anon_vma *anon_vma = NULL;
  168. unsigned long anon_mapping;
  169. rcu_read_lock();
  170. anon_mapping = (unsigned long) page->mapping;
  171. if (!(anon_mapping & PAGE_MAPPING_ANON))
  172. goto out;
  173. if (!page_mapped(page))
  174. goto out;
  175. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  176. spin_lock(&anon_vma->lock);
  177. out:
  178. rcu_read_unlock();
  179. return anon_vma;
  180. }
  181. #ifdef CONFIG_MIGRATION
  182. /*
  183. * Remove an anonymous page from swap replacing the swap pte's
  184. * through real pte's pointing to valid pages and then releasing
  185. * the page from the swap cache.
  186. *
  187. * Must hold page lock on page and mmap_sem of one vma that contains
  188. * the page.
  189. */
  190. void remove_from_swap(struct page *page)
  191. {
  192. struct anon_vma *anon_vma;
  193. struct vm_area_struct *vma;
  194. unsigned long mapping;
  195. if (!PageSwapCache(page))
  196. return;
  197. mapping = (unsigned long)page->mapping;
  198. if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
  199. return;
  200. /*
  201. * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
  202. */
  203. anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
  204. spin_lock(&anon_vma->lock);
  205. list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
  206. remove_vma_swap(vma, page);
  207. spin_unlock(&anon_vma->lock);
  208. delete_from_swap_cache(page);
  209. }
  210. EXPORT_SYMBOL(remove_from_swap);
  211. #endif
  212. /*
  213. * At what user virtual address is page expected in vma?
  214. */
  215. static inline unsigned long
  216. vma_address(struct page *page, struct vm_area_struct *vma)
  217. {
  218. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  219. unsigned long address;
  220. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  221. if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
  222. /* page should be within any vma from prio_tree_next */
  223. BUG_ON(!PageAnon(page));
  224. return -EFAULT;
  225. }
  226. return address;
  227. }
  228. /*
  229. * At what user virtual address is page expected in vma? checking that the
  230. * page matches the vma: currently only used on anon pages, by unuse_vma;
  231. */
  232. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  233. {
  234. if (PageAnon(page)) {
  235. if ((void *)vma->anon_vma !=
  236. (void *)page->mapping - PAGE_MAPPING_ANON)
  237. return -EFAULT;
  238. } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
  239. if (!vma->vm_file ||
  240. vma->vm_file->f_mapping != page->mapping)
  241. return -EFAULT;
  242. } else
  243. return -EFAULT;
  244. return vma_address(page, vma);
  245. }
  246. /*
  247. * Check that @page is mapped at @address into @mm.
  248. *
  249. * On success returns with pte mapped and locked.
  250. */
  251. pte_t *page_check_address(struct page *page, struct mm_struct *mm,
  252. unsigned long address, spinlock_t **ptlp)
  253. {
  254. pgd_t *pgd;
  255. pud_t *pud;
  256. pmd_t *pmd;
  257. pte_t *pte;
  258. spinlock_t *ptl;
  259. pgd = pgd_offset(mm, address);
  260. if (!pgd_present(*pgd))
  261. return NULL;
  262. pud = pud_offset(pgd, address);
  263. if (!pud_present(*pud))
  264. return NULL;
  265. pmd = pmd_offset(pud, address);
  266. if (!pmd_present(*pmd))
  267. return NULL;
  268. pte = pte_offset_map(pmd, address);
  269. /* Make a quick check before getting the lock */
  270. if (!pte_present(*pte)) {
  271. pte_unmap(pte);
  272. return NULL;
  273. }
  274. ptl = pte_lockptr(mm, pmd);
  275. spin_lock(ptl);
  276. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  277. *ptlp = ptl;
  278. return pte;
  279. }
  280. pte_unmap_unlock(pte, ptl);
  281. return NULL;
  282. }
  283. /*
  284. * Subfunctions of page_referenced: page_referenced_one called
  285. * repeatedly from either page_referenced_anon or page_referenced_file.
  286. */
  287. static int page_referenced_one(struct page *page,
  288. struct vm_area_struct *vma, unsigned int *mapcount)
  289. {
  290. struct mm_struct *mm = vma->vm_mm;
  291. unsigned long address;
  292. pte_t *pte;
  293. spinlock_t *ptl;
  294. int referenced = 0;
  295. address = vma_address(page, vma);
  296. if (address == -EFAULT)
  297. goto out;
  298. pte = page_check_address(page, mm, address, &ptl);
  299. if (!pte)
  300. goto out;
  301. if (ptep_clear_flush_young(vma, address, pte))
  302. referenced++;
  303. /* Pretend the page is referenced if the task has the
  304. swap token and is in the middle of a page fault. */
  305. if (mm != current->mm && has_swap_token(mm) &&
  306. rwsem_is_locked(&mm->mmap_sem))
  307. referenced++;
  308. (*mapcount)--;
  309. pte_unmap_unlock(pte, ptl);
  310. out:
  311. return referenced;
  312. }
  313. static int page_referenced_anon(struct page *page)
  314. {
  315. unsigned int mapcount;
  316. struct anon_vma *anon_vma;
  317. struct vm_area_struct *vma;
  318. int referenced = 0;
  319. anon_vma = page_lock_anon_vma(page);
  320. if (!anon_vma)
  321. return referenced;
  322. mapcount = page_mapcount(page);
  323. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  324. referenced += page_referenced_one(page, vma, &mapcount);
  325. if (!mapcount)
  326. break;
  327. }
  328. spin_unlock(&anon_vma->lock);
  329. return referenced;
  330. }
  331. /**
  332. * page_referenced_file - referenced check for object-based rmap
  333. * @page: the page we're checking references on.
  334. *
  335. * For an object-based mapped page, find all the places it is mapped and
  336. * check/clear the referenced flag. This is done by following the page->mapping
  337. * pointer, then walking the chain of vmas it holds. It returns the number
  338. * of references it found.
  339. *
  340. * This function is only called from page_referenced for object-based pages.
  341. */
  342. static int page_referenced_file(struct page *page)
  343. {
  344. unsigned int mapcount;
  345. struct address_space *mapping = page->mapping;
  346. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  347. struct vm_area_struct *vma;
  348. struct prio_tree_iter iter;
  349. int referenced = 0;
  350. /*
  351. * The caller's checks on page->mapping and !PageAnon have made
  352. * sure that this is a file page: the check for page->mapping
  353. * excludes the case just before it gets set on an anon page.
  354. */
  355. BUG_ON(PageAnon(page));
  356. /*
  357. * The page lock not only makes sure that page->mapping cannot
  358. * suddenly be NULLified by truncation, it makes sure that the
  359. * structure at mapping cannot be freed and reused yet,
  360. * so we can safely take mapping->i_mmap_lock.
  361. */
  362. BUG_ON(!PageLocked(page));
  363. spin_lock(&mapping->i_mmap_lock);
  364. /*
  365. * i_mmap_lock does not stabilize mapcount at all, but mapcount
  366. * is more likely to be accurate if we note it after spinning.
  367. */
  368. mapcount = page_mapcount(page);
  369. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  370. if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
  371. == (VM_LOCKED|VM_MAYSHARE)) {
  372. referenced++;
  373. break;
  374. }
  375. referenced += page_referenced_one(page, vma, &mapcount);
  376. if (!mapcount)
  377. break;
  378. }
  379. spin_unlock(&mapping->i_mmap_lock);
  380. return referenced;
  381. }
  382. /**
  383. * page_referenced - test if the page was referenced
  384. * @page: the page to test
  385. * @is_locked: caller holds lock on the page
  386. *
  387. * Quick test_and_clear_referenced for all mappings to a page,
  388. * returns the number of ptes which referenced the page.
  389. */
  390. int page_referenced(struct page *page, int is_locked)
  391. {
  392. int referenced = 0;
  393. if (page_test_and_clear_young(page))
  394. referenced++;
  395. if (TestClearPageReferenced(page))
  396. referenced++;
  397. if (page_mapped(page) && page->mapping) {
  398. if (PageAnon(page))
  399. referenced += page_referenced_anon(page);
  400. else if (is_locked)
  401. referenced += page_referenced_file(page);
  402. else if (TestSetPageLocked(page))
  403. referenced++;
  404. else {
  405. if (page->mapping)
  406. referenced += page_referenced_file(page);
  407. unlock_page(page);
  408. }
  409. }
  410. return referenced;
  411. }
  412. /**
  413. * page_set_anon_rmap - setup new anonymous rmap
  414. * @page: the page to add the mapping to
  415. * @vma: the vm area in which the mapping is added
  416. * @address: the user virtual address mapped
  417. */
  418. static void __page_set_anon_rmap(struct page *page,
  419. struct vm_area_struct *vma, unsigned long address)
  420. {
  421. struct anon_vma *anon_vma = vma->anon_vma;
  422. BUG_ON(!anon_vma);
  423. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  424. page->mapping = (struct address_space *) anon_vma;
  425. page->index = linear_page_index(vma, address);
  426. /*
  427. * nr_mapped state can be updated without turning off
  428. * interrupts because it is not modified via interrupt.
  429. */
  430. __inc_page_state(nr_mapped);
  431. }
  432. /**
  433. * page_add_anon_rmap - add pte mapping to an anonymous page
  434. * @page: the page to add the mapping to
  435. * @vma: the vm area in which the mapping is added
  436. * @address: the user virtual address mapped
  437. *
  438. * The caller needs to hold the pte lock.
  439. */
  440. void page_add_anon_rmap(struct page *page,
  441. struct vm_area_struct *vma, unsigned long address)
  442. {
  443. if (atomic_inc_and_test(&page->_mapcount))
  444. __page_set_anon_rmap(page, vma, address);
  445. /* else checking page index and mapping is racy */
  446. }
  447. /*
  448. * page_add_new_anon_rmap - add pte mapping to a new anonymous page
  449. * @page: the page to add the mapping to
  450. * @vma: the vm area in which the mapping is added
  451. * @address: the user virtual address mapped
  452. *
  453. * Same as page_add_anon_rmap but must only be called on *new* pages.
  454. * This means the inc-and-test can be bypassed.
  455. */
  456. void page_add_new_anon_rmap(struct page *page,
  457. struct vm_area_struct *vma, unsigned long address)
  458. {
  459. atomic_set(&page->_mapcount, 0); /* elevate count by 1 (starts at -1) */
  460. __page_set_anon_rmap(page, vma, address);
  461. }
  462. /**
  463. * page_add_file_rmap - add pte mapping to a file page
  464. * @page: the page to add the mapping to
  465. *
  466. * The caller needs to hold the pte lock.
  467. */
  468. void page_add_file_rmap(struct page *page)
  469. {
  470. if (atomic_inc_and_test(&page->_mapcount))
  471. __inc_page_state(nr_mapped);
  472. }
  473. /**
  474. * page_remove_rmap - take down pte mapping from a page
  475. * @page: page to remove mapping from
  476. *
  477. * The caller needs to hold the pte lock.
  478. */
  479. void page_remove_rmap(struct page *page)
  480. {
  481. if (atomic_add_negative(-1, &page->_mapcount)) {
  482. if (page_mapcount(page) < 0) {
  483. printk (KERN_EMERG "Eeek! page_mapcount(page) went negative! (%d)\n", page_mapcount(page));
  484. printk (KERN_EMERG " page->flags = %lx\n", page->flags);
  485. printk (KERN_EMERG " page->count = %x\n", page_count(page));
  486. printk (KERN_EMERG " page->mapping = %p\n", page->mapping);
  487. }
  488. BUG_ON(page_mapcount(page) < 0);
  489. /*
  490. * It would be tidy to reset the PageAnon mapping here,
  491. * but that might overwrite a racing page_add_anon_rmap
  492. * which increments mapcount after us but sets mapping
  493. * before us: so leave the reset to free_hot_cold_page,
  494. * and remember that it's only reliable while mapped.
  495. * Leaving it set also helps swapoff to reinstate ptes
  496. * faster for those pages still in swapcache.
  497. */
  498. if (page_test_and_clear_dirty(page))
  499. set_page_dirty(page);
  500. __dec_page_state(nr_mapped);
  501. }
  502. }
  503. /*
  504. * Subfunctions of try_to_unmap: try_to_unmap_one called
  505. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  506. */
  507. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  508. int ignore_refs)
  509. {
  510. struct mm_struct *mm = vma->vm_mm;
  511. unsigned long address;
  512. pte_t *pte;
  513. pte_t pteval;
  514. spinlock_t *ptl;
  515. int ret = SWAP_AGAIN;
  516. address = vma_address(page, vma);
  517. if (address == -EFAULT)
  518. goto out;
  519. pte = page_check_address(page, mm, address, &ptl);
  520. if (!pte)
  521. goto out;
  522. /*
  523. * If the page is mlock()d, we cannot swap it out.
  524. * If it's recently referenced (perhaps page_referenced
  525. * skipped over this mm) then we should reactivate it.
  526. */
  527. if ((vma->vm_flags & VM_LOCKED) ||
  528. (ptep_clear_flush_young(vma, address, pte)
  529. && !ignore_refs)) {
  530. ret = SWAP_FAIL;
  531. goto out_unmap;
  532. }
  533. /* Nuke the page table entry. */
  534. flush_cache_page(vma, address, page_to_pfn(page));
  535. pteval = ptep_clear_flush(vma, address, pte);
  536. /* Move the dirty bit to the physical page now the pte is gone. */
  537. if (pte_dirty(pteval))
  538. set_page_dirty(page);
  539. /* Update high watermark before we lower rss */
  540. update_hiwater_rss(mm);
  541. if (PageAnon(page)) {
  542. swp_entry_t entry = { .val = page_private(page) };
  543. /*
  544. * Store the swap location in the pte.
  545. * See handle_pte_fault() ...
  546. */
  547. BUG_ON(!PageSwapCache(page));
  548. swap_duplicate(entry);
  549. if (list_empty(&mm->mmlist)) {
  550. spin_lock(&mmlist_lock);
  551. if (list_empty(&mm->mmlist))
  552. list_add(&mm->mmlist, &init_mm.mmlist);
  553. spin_unlock(&mmlist_lock);
  554. }
  555. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  556. BUG_ON(pte_file(*pte));
  557. dec_mm_counter(mm, anon_rss);
  558. } else
  559. dec_mm_counter(mm, file_rss);
  560. page_remove_rmap(page);
  561. page_cache_release(page);
  562. out_unmap:
  563. pte_unmap_unlock(pte, ptl);
  564. out:
  565. return ret;
  566. }
  567. /*
  568. * objrmap doesn't work for nonlinear VMAs because the assumption that
  569. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  570. * Consequently, given a particular page and its ->index, we cannot locate the
  571. * ptes which are mapping that page without an exhaustive linear search.
  572. *
  573. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  574. * maps the file to which the target page belongs. The ->vm_private_data field
  575. * holds the current cursor into that scan. Successive searches will circulate
  576. * around the vma's virtual address space.
  577. *
  578. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  579. * more scanning pressure is placed against them as well. Eventually pages
  580. * will become fully unmapped and are eligible for eviction.
  581. *
  582. * For very sparsely populated VMAs this is a little inefficient - chances are
  583. * there there won't be many ptes located within the scan cluster. In this case
  584. * maybe we could scan further - to the end of the pte page, perhaps.
  585. */
  586. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  587. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  588. static void try_to_unmap_cluster(unsigned long cursor,
  589. unsigned int *mapcount, struct vm_area_struct *vma)
  590. {
  591. struct mm_struct *mm = vma->vm_mm;
  592. pgd_t *pgd;
  593. pud_t *pud;
  594. pmd_t *pmd;
  595. pte_t *pte;
  596. pte_t pteval;
  597. spinlock_t *ptl;
  598. struct page *page;
  599. unsigned long address;
  600. unsigned long end;
  601. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  602. end = address + CLUSTER_SIZE;
  603. if (address < vma->vm_start)
  604. address = vma->vm_start;
  605. if (end > vma->vm_end)
  606. end = vma->vm_end;
  607. pgd = pgd_offset(mm, address);
  608. if (!pgd_present(*pgd))
  609. return;
  610. pud = pud_offset(pgd, address);
  611. if (!pud_present(*pud))
  612. return;
  613. pmd = pmd_offset(pud, address);
  614. if (!pmd_present(*pmd))
  615. return;
  616. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  617. /* Update high watermark before we lower rss */
  618. update_hiwater_rss(mm);
  619. for (; address < end; pte++, address += PAGE_SIZE) {
  620. if (!pte_present(*pte))
  621. continue;
  622. page = vm_normal_page(vma, address, *pte);
  623. BUG_ON(!page || PageAnon(page));
  624. if (ptep_clear_flush_young(vma, address, pte))
  625. continue;
  626. /* Nuke the page table entry. */
  627. flush_cache_page(vma, address, pte_pfn(*pte));
  628. pteval = ptep_clear_flush(vma, address, pte);
  629. /* If nonlinear, store the file page offset in the pte. */
  630. if (page->index != linear_page_index(vma, address))
  631. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  632. /* Move the dirty bit to the physical page now the pte is gone. */
  633. if (pte_dirty(pteval))
  634. set_page_dirty(page);
  635. page_remove_rmap(page);
  636. page_cache_release(page);
  637. dec_mm_counter(mm, file_rss);
  638. (*mapcount)--;
  639. }
  640. pte_unmap_unlock(pte - 1, ptl);
  641. }
  642. static int try_to_unmap_anon(struct page *page, int ignore_refs)
  643. {
  644. struct anon_vma *anon_vma;
  645. struct vm_area_struct *vma;
  646. int ret = SWAP_AGAIN;
  647. anon_vma = page_lock_anon_vma(page);
  648. if (!anon_vma)
  649. return ret;
  650. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  651. ret = try_to_unmap_one(page, vma, ignore_refs);
  652. if (ret == SWAP_FAIL || !page_mapped(page))
  653. break;
  654. }
  655. spin_unlock(&anon_vma->lock);
  656. return ret;
  657. }
  658. /**
  659. * try_to_unmap_file - unmap file page using the object-based rmap method
  660. * @page: the page to unmap
  661. *
  662. * Find all the mappings of a page using the mapping pointer and the vma chains
  663. * contained in the address_space struct it points to.
  664. *
  665. * This function is only called from try_to_unmap for object-based pages.
  666. */
  667. static int try_to_unmap_file(struct page *page, int ignore_refs)
  668. {
  669. struct address_space *mapping = page->mapping;
  670. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  671. struct vm_area_struct *vma;
  672. struct prio_tree_iter iter;
  673. int ret = SWAP_AGAIN;
  674. unsigned long cursor;
  675. unsigned long max_nl_cursor = 0;
  676. unsigned long max_nl_size = 0;
  677. unsigned int mapcount;
  678. spin_lock(&mapping->i_mmap_lock);
  679. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  680. ret = try_to_unmap_one(page, vma, ignore_refs);
  681. if (ret == SWAP_FAIL || !page_mapped(page))
  682. goto out;
  683. }
  684. if (list_empty(&mapping->i_mmap_nonlinear))
  685. goto out;
  686. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  687. shared.vm_set.list) {
  688. if (vma->vm_flags & VM_LOCKED)
  689. continue;
  690. cursor = (unsigned long) vma->vm_private_data;
  691. if (cursor > max_nl_cursor)
  692. max_nl_cursor = cursor;
  693. cursor = vma->vm_end - vma->vm_start;
  694. if (cursor > max_nl_size)
  695. max_nl_size = cursor;
  696. }
  697. if (max_nl_size == 0) { /* any nonlinears locked or reserved */
  698. ret = SWAP_FAIL;
  699. goto out;
  700. }
  701. /*
  702. * We don't try to search for this page in the nonlinear vmas,
  703. * and page_referenced wouldn't have found it anyway. Instead
  704. * just walk the nonlinear vmas trying to age and unmap some.
  705. * The mapcount of the page we came in with is irrelevant,
  706. * but even so use it as a guide to how hard we should try?
  707. */
  708. mapcount = page_mapcount(page);
  709. if (!mapcount)
  710. goto out;
  711. cond_resched_lock(&mapping->i_mmap_lock);
  712. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  713. if (max_nl_cursor == 0)
  714. max_nl_cursor = CLUSTER_SIZE;
  715. do {
  716. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  717. shared.vm_set.list) {
  718. if (vma->vm_flags & VM_LOCKED)
  719. continue;
  720. cursor = (unsigned long) vma->vm_private_data;
  721. while ( cursor < max_nl_cursor &&
  722. cursor < vma->vm_end - vma->vm_start) {
  723. try_to_unmap_cluster(cursor, &mapcount, vma);
  724. cursor += CLUSTER_SIZE;
  725. vma->vm_private_data = (void *) cursor;
  726. if ((int)mapcount <= 0)
  727. goto out;
  728. }
  729. vma->vm_private_data = (void *) max_nl_cursor;
  730. }
  731. cond_resched_lock(&mapping->i_mmap_lock);
  732. max_nl_cursor += CLUSTER_SIZE;
  733. } while (max_nl_cursor <= max_nl_size);
  734. /*
  735. * Don't loop forever (perhaps all the remaining pages are
  736. * in locked vmas). Reset cursor on all unreserved nonlinear
  737. * vmas, now forgetting on which ones it had fallen behind.
  738. */
  739. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  740. vma->vm_private_data = NULL;
  741. out:
  742. spin_unlock(&mapping->i_mmap_lock);
  743. return ret;
  744. }
  745. /**
  746. * try_to_unmap - try to remove all page table mappings to a page
  747. * @page: the page to get unmapped
  748. *
  749. * Tries to remove all the page table entries which are mapping this
  750. * page, used in the pageout path. Caller must hold the page lock.
  751. * Return values are:
  752. *
  753. * SWAP_SUCCESS - we succeeded in removing all mappings
  754. * SWAP_AGAIN - we missed a mapping, try again later
  755. * SWAP_FAIL - the page is unswappable
  756. */
  757. int try_to_unmap(struct page *page, int ignore_refs)
  758. {
  759. int ret;
  760. BUG_ON(!PageLocked(page));
  761. if (PageAnon(page))
  762. ret = try_to_unmap_anon(page, ignore_refs);
  763. else
  764. ret = try_to_unmap_file(page, ignore_refs);
  765. if (!page_mapped(page))
  766. ret = SWAP_SUCCESS;
  767. return ret;
  768. }