rmap.c 28 KB

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