rmap.c 27 KB

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