rmap.c 26 KB

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