rmap.c 23 KB

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