rmap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 (!trylock_page(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. * Now that the last pte has gone, s390 must transfer dirty
  582. * flag from storage key to struct page. We can usually skip
  583. * this if the page is anon, so about to be freed; but perhaps
  584. * not if it's in swapcache - there might be another pte slot
  585. * containing the swap entry, but page not yet written to swap.
  586. */
  587. if ((!PageAnon(page) || PageSwapCache(page)) &&
  588. page_test_dirty(page)) {
  589. page_clear_dirty(page);
  590. set_page_dirty(page);
  591. }
  592. mem_cgroup_uncharge_page(page);
  593. __dec_zone_page_state(page,
  594. PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
  595. /*
  596. * It would be tidy to reset the PageAnon mapping here,
  597. * but that might overwrite a racing page_add_anon_rmap
  598. * which increments mapcount after us but sets mapping
  599. * before us: so leave the reset to free_hot_cold_page,
  600. * and remember that it's only reliable while mapped.
  601. * Leaving it set also helps swapoff to reinstate ptes
  602. * faster for those pages still in swapcache.
  603. */
  604. }
  605. }
  606. /*
  607. * Subfunctions of try_to_unmap: try_to_unmap_one called
  608. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  609. */
  610. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  611. int migration)
  612. {
  613. struct mm_struct *mm = vma->vm_mm;
  614. unsigned long address;
  615. pte_t *pte;
  616. pte_t pteval;
  617. spinlock_t *ptl;
  618. int ret = SWAP_AGAIN;
  619. address = vma_address(page, vma);
  620. if (address == -EFAULT)
  621. goto out;
  622. pte = page_check_address(page, mm, address, &ptl);
  623. if (!pte)
  624. goto out;
  625. /*
  626. * If the page is mlock()d, we cannot swap it out.
  627. * If it's recently referenced (perhaps page_referenced
  628. * skipped over this mm) then we should reactivate it.
  629. */
  630. if (!migration && ((vma->vm_flags & VM_LOCKED) ||
  631. (ptep_clear_flush_young_notify(vma, address, pte)))) {
  632. ret = SWAP_FAIL;
  633. goto out_unmap;
  634. }
  635. /* Nuke the page table entry. */
  636. flush_cache_page(vma, address, page_to_pfn(page));
  637. pteval = ptep_clear_flush_notify(vma, address, pte);
  638. /* Move the dirty bit to the physical page now the pte is gone. */
  639. if (pte_dirty(pteval))
  640. set_page_dirty(page);
  641. /* Update high watermark before we lower rss */
  642. update_hiwater_rss(mm);
  643. if (PageAnon(page)) {
  644. swp_entry_t entry = { .val = page_private(page) };
  645. if (PageSwapCache(page)) {
  646. /*
  647. * Store the swap location in the pte.
  648. * See handle_pte_fault() ...
  649. */
  650. swap_duplicate(entry);
  651. if (list_empty(&mm->mmlist)) {
  652. spin_lock(&mmlist_lock);
  653. if (list_empty(&mm->mmlist))
  654. list_add(&mm->mmlist, &init_mm.mmlist);
  655. spin_unlock(&mmlist_lock);
  656. }
  657. dec_mm_counter(mm, anon_rss);
  658. #ifdef CONFIG_MIGRATION
  659. } else {
  660. /*
  661. * Store the pfn of the page in a special migration
  662. * pte. do_swap_page() will wait until the migration
  663. * pte is removed and then restart fault handling.
  664. */
  665. BUG_ON(!migration);
  666. entry = make_migration_entry(page, pte_write(pteval));
  667. #endif
  668. }
  669. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  670. BUG_ON(pte_file(*pte));
  671. } else
  672. #ifdef CONFIG_MIGRATION
  673. if (migration) {
  674. /* Establish migration entry for a file page */
  675. swp_entry_t entry;
  676. entry = make_migration_entry(page, pte_write(pteval));
  677. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  678. } else
  679. #endif
  680. dec_mm_counter(mm, file_rss);
  681. page_remove_rmap(page, vma);
  682. page_cache_release(page);
  683. out_unmap:
  684. pte_unmap_unlock(pte, ptl);
  685. out:
  686. return ret;
  687. }
  688. /*
  689. * objrmap doesn't work for nonlinear VMAs because the assumption that
  690. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  691. * Consequently, given a particular page and its ->index, we cannot locate the
  692. * ptes which are mapping that page without an exhaustive linear search.
  693. *
  694. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  695. * maps the file to which the target page belongs. The ->vm_private_data field
  696. * holds the current cursor into that scan. Successive searches will circulate
  697. * around the vma's virtual address space.
  698. *
  699. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  700. * more scanning pressure is placed against them as well. Eventually pages
  701. * will become fully unmapped and are eligible for eviction.
  702. *
  703. * For very sparsely populated VMAs this is a little inefficient - chances are
  704. * there there won't be many ptes located within the scan cluster. In this case
  705. * maybe we could scan further - to the end of the pte page, perhaps.
  706. */
  707. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  708. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  709. static void try_to_unmap_cluster(unsigned long cursor,
  710. unsigned int *mapcount, struct vm_area_struct *vma)
  711. {
  712. struct mm_struct *mm = vma->vm_mm;
  713. pgd_t *pgd;
  714. pud_t *pud;
  715. pmd_t *pmd;
  716. pte_t *pte;
  717. pte_t pteval;
  718. spinlock_t *ptl;
  719. struct page *page;
  720. unsigned long address;
  721. unsigned long end;
  722. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  723. end = address + CLUSTER_SIZE;
  724. if (address < vma->vm_start)
  725. address = vma->vm_start;
  726. if (end > vma->vm_end)
  727. end = vma->vm_end;
  728. pgd = pgd_offset(mm, address);
  729. if (!pgd_present(*pgd))
  730. return;
  731. pud = pud_offset(pgd, address);
  732. if (!pud_present(*pud))
  733. return;
  734. pmd = pmd_offset(pud, address);
  735. if (!pmd_present(*pmd))
  736. return;
  737. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  738. /* Update high watermark before we lower rss */
  739. update_hiwater_rss(mm);
  740. for (; address < end; pte++, address += PAGE_SIZE) {
  741. if (!pte_present(*pte))
  742. continue;
  743. page = vm_normal_page(vma, address, *pte);
  744. BUG_ON(!page || PageAnon(page));
  745. if (ptep_clear_flush_young_notify(vma, address, pte))
  746. continue;
  747. /* Nuke the page table entry. */
  748. flush_cache_page(vma, address, pte_pfn(*pte));
  749. pteval = ptep_clear_flush_notify(vma, address, pte);
  750. /* If nonlinear, store the file page offset in the pte. */
  751. if (page->index != linear_page_index(vma, address))
  752. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  753. /* Move the dirty bit to the physical page now the pte is gone. */
  754. if (pte_dirty(pteval))
  755. set_page_dirty(page);
  756. page_remove_rmap(page, vma);
  757. page_cache_release(page);
  758. dec_mm_counter(mm, file_rss);
  759. (*mapcount)--;
  760. }
  761. pte_unmap_unlock(pte - 1, ptl);
  762. }
  763. static int try_to_unmap_anon(struct page *page, int migration)
  764. {
  765. struct anon_vma *anon_vma;
  766. struct vm_area_struct *vma;
  767. int ret = SWAP_AGAIN;
  768. anon_vma = page_lock_anon_vma(page);
  769. if (!anon_vma)
  770. return ret;
  771. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  772. ret = try_to_unmap_one(page, vma, migration);
  773. if (ret == SWAP_FAIL || !page_mapped(page))
  774. break;
  775. }
  776. page_unlock_anon_vma(anon_vma);
  777. return ret;
  778. }
  779. /**
  780. * try_to_unmap_file - unmap file page using the object-based rmap method
  781. * @page: the page to unmap
  782. * @migration: migration flag
  783. *
  784. * Find all the mappings of a page using the mapping pointer and the vma chains
  785. * contained in the address_space struct it points to.
  786. *
  787. * This function is only called from try_to_unmap for object-based pages.
  788. */
  789. static int try_to_unmap_file(struct page *page, int migration)
  790. {
  791. struct address_space *mapping = page->mapping;
  792. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  793. struct vm_area_struct *vma;
  794. struct prio_tree_iter iter;
  795. int ret = SWAP_AGAIN;
  796. unsigned long cursor;
  797. unsigned long max_nl_cursor = 0;
  798. unsigned long max_nl_size = 0;
  799. unsigned int mapcount;
  800. spin_lock(&mapping->i_mmap_lock);
  801. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  802. ret = try_to_unmap_one(page, vma, migration);
  803. if (ret == SWAP_FAIL || !page_mapped(page))
  804. goto out;
  805. }
  806. if (list_empty(&mapping->i_mmap_nonlinear))
  807. goto out;
  808. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  809. shared.vm_set.list) {
  810. if ((vma->vm_flags & VM_LOCKED) && !migration)
  811. continue;
  812. cursor = (unsigned long) vma->vm_private_data;
  813. if (cursor > max_nl_cursor)
  814. max_nl_cursor = cursor;
  815. cursor = vma->vm_end - vma->vm_start;
  816. if (cursor > max_nl_size)
  817. max_nl_size = cursor;
  818. }
  819. if (max_nl_size == 0) { /* any nonlinears locked or reserved */
  820. ret = SWAP_FAIL;
  821. goto out;
  822. }
  823. /*
  824. * We don't try to search for this page in the nonlinear vmas,
  825. * and page_referenced wouldn't have found it anyway. Instead
  826. * just walk the nonlinear vmas trying to age and unmap some.
  827. * The mapcount of the page we came in with is irrelevant,
  828. * but even so use it as a guide to how hard we should try?
  829. */
  830. mapcount = page_mapcount(page);
  831. if (!mapcount)
  832. goto out;
  833. cond_resched_lock(&mapping->i_mmap_lock);
  834. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  835. if (max_nl_cursor == 0)
  836. max_nl_cursor = CLUSTER_SIZE;
  837. do {
  838. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  839. shared.vm_set.list) {
  840. if ((vma->vm_flags & VM_LOCKED) && !migration)
  841. continue;
  842. cursor = (unsigned long) vma->vm_private_data;
  843. while ( cursor < max_nl_cursor &&
  844. cursor < vma->vm_end - vma->vm_start) {
  845. try_to_unmap_cluster(cursor, &mapcount, vma);
  846. cursor += CLUSTER_SIZE;
  847. vma->vm_private_data = (void *) cursor;
  848. if ((int)mapcount <= 0)
  849. goto out;
  850. }
  851. vma->vm_private_data = (void *) max_nl_cursor;
  852. }
  853. cond_resched_lock(&mapping->i_mmap_lock);
  854. max_nl_cursor += CLUSTER_SIZE;
  855. } while (max_nl_cursor <= max_nl_size);
  856. /*
  857. * Don't loop forever (perhaps all the remaining pages are
  858. * in locked vmas). Reset cursor on all unreserved nonlinear
  859. * vmas, now forgetting on which ones it had fallen behind.
  860. */
  861. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  862. vma->vm_private_data = NULL;
  863. out:
  864. spin_unlock(&mapping->i_mmap_lock);
  865. return ret;
  866. }
  867. /**
  868. * try_to_unmap - try to remove all page table mappings to a page
  869. * @page: the page to get unmapped
  870. * @migration: migration flag
  871. *
  872. * Tries to remove all the page table entries which are mapping this
  873. * page, used in the pageout path. Caller must hold the page lock.
  874. * Return values are:
  875. *
  876. * SWAP_SUCCESS - we succeeded in removing all mappings
  877. * SWAP_AGAIN - we missed a mapping, try again later
  878. * SWAP_FAIL - the page is unswappable
  879. */
  880. int try_to_unmap(struct page *page, int migration)
  881. {
  882. int ret;
  883. BUG_ON(!PageLocked(page));
  884. if (PageAnon(page))
  885. ret = try_to_unmap_anon(page, migration);
  886. else
  887. ret = try_to_unmap_file(page, migration);
  888. if (!page_mapped(page))
  889. ret = SWAP_SUCCESS;
  890. return ret;
  891. }