rmap.c 25 KB

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