rmap.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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 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. * (code doesn't rely on that order so it could be switched around)
  40. * ->tasklist_lock
  41. * anon_vma->lock (memory_failure, collect_procs_anon)
  42. * pte map lock
  43. */
  44. #include <linux/mm.h>
  45. #include <linux/pagemap.h>
  46. #include <linux/swap.h>
  47. #include <linux/swapops.h>
  48. #include <linux/slab.h>
  49. #include <linux/init.h>
  50. #include <linux/ksm.h>
  51. #include <linux/rmap.h>
  52. #include <linux/rcupdate.h>
  53. #include <linux/module.h>
  54. #include <linux/memcontrol.h>
  55. #include <linux/mmu_notifier.h>
  56. #include <linux/migrate.h>
  57. #include <asm/tlbflush.h>
  58. #include "internal.h"
  59. static struct kmem_cache *anon_vma_cachep;
  60. static inline struct anon_vma *anon_vma_alloc(void)
  61. {
  62. return kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
  63. }
  64. void anon_vma_free(struct anon_vma *anon_vma)
  65. {
  66. kmem_cache_free(anon_vma_cachep, anon_vma);
  67. }
  68. /**
  69. * anon_vma_prepare - attach an anon_vma to a memory region
  70. * @vma: the memory region in question
  71. *
  72. * This makes sure the memory mapping described by 'vma' has
  73. * an 'anon_vma' attached to it, so that we can associate the
  74. * anonymous pages mapped into it with that anon_vma.
  75. *
  76. * The common case will be that we already have one, but if
  77. * if not we either need to find an adjacent mapping that we
  78. * can re-use the anon_vma from (very common when the only
  79. * reason for splitting a vma has been mprotect()), or we
  80. * allocate a new one.
  81. *
  82. * Anon-vma allocations are very subtle, because we may have
  83. * optimistically looked up an anon_vma in page_lock_anon_vma()
  84. * and that may actually touch the spinlock even in the newly
  85. * allocated vma (it depends on RCU to make sure that the
  86. * anon_vma isn't actually destroyed).
  87. *
  88. * As a result, we need to do proper anon_vma locking even
  89. * for the new allocation. At the same time, we do not want
  90. * to do any locking for the common case of already having
  91. * an anon_vma.
  92. *
  93. * This must be called with the mmap_sem held for reading.
  94. */
  95. int anon_vma_prepare(struct vm_area_struct *vma)
  96. {
  97. struct anon_vma *anon_vma = vma->anon_vma;
  98. might_sleep();
  99. if (unlikely(!anon_vma)) {
  100. struct mm_struct *mm = vma->vm_mm;
  101. struct anon_vma *allocated;
  102. anon_vma = find_mergeable_anon_vma(vma);
  103. allocated = NULL;
  104. if (!anon_vma) {
  105. anon_vma = anon_vma_alloc();
  106. if (unlikely(!anon_vma))
  107. return -ENOMEM;
  108. allocated = anon_vma;
  109. }
  110. spin_lock(&anon_vma->lock);
  111. /* page_table_lock to protect against threads */
  112. spin_lock(&mm->page_table_lock);
  113. if (likely(!vma->anon_vma)) {
  114. vma->anon_vma = anon_vma;
  115. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  116. allocated = NULL;
  117. }
  118. spin_unlock(&mm->page_table_lock);
  119. spin_unlock(&anon_vma->lock);
  120. if (unlikely(allocated))
  121. anon_vma_free(allocated);
  122. }
  123. return 0;
  124. }
  125. void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
  126. {
  127. BUG_ON(vma->anon_vma != next->anon_vma);
  128. list_del(&next->anon_vma_node);
  129. }
  130. void __anon_vma_link(struct vm_area_struct *vma)
  131. {
  132. struct anon_vma *anon_vma = vma->anon_vma;
  133. if (anon_vma)
  134. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  135. }
  136. void anon_vma_link(struct vm_area_struct *vma)
  137. {
  138. struct anon_vma *anon_vma = vma->anon_vma;
  139. if (anon_vma) {
  140. spin_lock(&anon_vma->lock);
  141. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  142. spin_unlock(&anon_vma->lock);
  143. }
  144. }
  145. void anon_vma_unlink(struct vm_area_struct *vma)
  146. {
  147. struct anon_vma *anon_vma = vma->anon_vma;
  148. int empty;
  149. if (!anon_vma)
  150. return;
  151. spin_lock(&anon_vma->lock);
  152. list_del(&vma->anon_vma_node);
  153. /* We must garbage collect the anon_vma if it's empty */
  154. empty = list_empty(&anon_vma->head) && !ksm_refcount(anon_vma);
  155. spin_unlock(&anon_vma->lock);
  156. if (empty)
  157. anon_vma_free(anon_vma);
  158. }
  159. static void anon_vma_ctor(void *data)
  160. {
  161. struct anon_vma *anon_vma = data;
  162. spin_lock_init(&anon_vma->lock);
  163. ksm_refcount_init(anon_vma);
  164. INIT_LIST_HEAD(&anon_vma->head);
  165. }
  166. void __init anon_vma_init(void)
  167. {
  168. anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
  169. 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
  170. }
  171. /*
  172. * Getting a lock on a stable anon_vma from a page off the LRU is
  173. * tricky: page_lock_anon_vma rely on RCU to guard against the races.
  174. */
  175. struct anon_vma *page_lock_anon_vma(struct page *page)
  176. {
  177. struct anon_vma *anon_vma;
  178. unsigned long anon_mapping;
  179. rcu_read_lock();
  180. anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping);
  181. if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
  182. goto out;
  183. if (!page_mapped(page))
  184. goto out;
  185. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  186. spin_lock(&anon_vma->lock);
  187. return anon_vma;
  188. out:
  189. rcu_read_unlock();
  190. return NULL;
  191. }
  192. void page_unlock_anon_vma(struct anon_vma *anon_vma)
  193. {
  194. spin_unlock(&anon_vma->lock);
  195. rcu_read_unlock();
  196. }
  197. /*
  198. * At what user virtual address is page expected in @vma?
  199. * Returns virtual address or -EFAULT if page's index/offset is not
  200. * within the range mapped the @vma.
  201. */
  202. static inline unsigned long
  203. vma_address(struct page *page, struct vm_area_struct *vma)
  204. {
  205. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  206. unsigned long address;
  207. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  208. if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
  209. /* page should be within @vma mapping range */
  210. return -EFAULT;
  211. }
  212. return address;
  213. }
  214. /*
  215. * At what user virtual address is page expected in vma?
  216. * checking that the page matches the vma.
  217. */
  218. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  219. {
  220. if (PageAnon(page)) {
  221. if (vma->anon_vma != page_anon_vma(page))
  222. return -EFAULT;
  223. } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
  224. if (!vma->vm_file ||
  225. vma->vm_file->f_mapping != page->mapping)
  226. return -EFAULT;
  227. } else
  228. return -EFAULT;
  229. return vma_address(page, vma);
  230. }
  231. /*
  232. * Check that @page is mapped at @address into @mm.
  233. *
  234. * If @sync is false, page_check_address may perform a racy check to avoid
  235. * the page table lock when the pte is not present (helpful when reclaiming
  236. * highly shared pages).
  237. *
  238. * On success returns with pte mapped and locked.
  239. */
  240. pte_t *page_check_address(struct page *page, struct mm_struct *mm,
  241. unsigned long address, spinlock_t **ptlp, int sync)
  242. {
  243. pgd_t *pgd;
  244. pud_t *pud;
  245. pmd_t *pmd;
  246. pte_t *pte;
  247. spinlock_t *ptl;
  248. pgd = pgd_offset(mm, address);
  249. if (!pgd_present(*pgd))
  250. return NULL;
  251. pud = pud_offset(pgd, address);
  252. if (!pud_present(*pud))
  253. return NULL;
  254. pmd = pmd_offset(pud, address);
  255. if (!pmd_present(*pmd))
  256. return NULL;
  257. pte = pte_offset_map(pmd, address);
  258. /* Make a quick check before getting the lock */
  259. if (!sync && !pte_present(*pte)) {
  260. pte_unmap(pte);
  261. return NULL;
  262. }
  263. ptl = pte_lockptr(mm, pmd);
  264. spin_lock(ptl);
  265. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  266. *ptlp = ptl;
  267. return pte;
  268. }
  269. pte_unmap_unlock(pte, ptl);
  270. return NULL;
  271. }
  272. /**
  273. * page_mapped_in_vma - check whether a page is really mapped in a VMA
  274. * @page: the page to test
  275. * @vma: the VMA to test
  276. *
  277. * Returns 1 if the page is mapped into the page tables of the VMA, 0
  278. * if the page is not mapped into the page tables of this VMA. Only
  279. * valid for normal file or anonymous VMAs.
  280. */
  281. int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
  282. {
  283. unsigned long address;
  284. pte_t *pte;
  285. spinlock_t *ptl;
  286. address = vma_address(page, vma);
  287. if (address == -EFAULT) /* out of vma range */
  288. return 0;
  289. pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
  290. if (!pte) /* the page is not in this mm */
  291. return 0;
  292. pte_unmap_unlock(pte, ptl);
  293. return 1;
  294. }
  295. /*
  296. * Subfunctions of page_referenced: page_referenced_one called
  297. * repeatedly from either page_referenced_anon or page_referenced_file.
  298. */
  299. int page_referenced_one(struct page *page, struct vm_area_struct *vma,
  300. unsigned long address, unsigned int *mapcount,
  301. unsigned long *vm_flags)
  302. {
  303. struct mm_struct *mm = vma->vm_mm;
  304. pte_t *pte;
  305. spinlock_t *ptl;
  306. int referenced = 0;
  307. pte = page_check_address(page, mm, address, &ptl, 0);
  308. if (!pte)
  309. goto out;
  310. /*
  311. * Don't want to elevate referenced for mlocked page that gets this far,
  312. * in order that it progresses to try_to_unmap and is moved to the
  313. * unevictable list.
  314. */
  315. if (vma->vm_flags & VM_LOCKED) {
  316. *mapcount = 1; /* break early from loop */
  317. *vm_flags |= VM_LOCKED;
  318. goto out_unmap;
  319. }
  320. if (ptep_clear_flush_young_notify(vma, address, pte)) {
  321. /*
  322. * Don't treat a reference through a sequentially read
  323. * mapping as such. If the page has been used in
  324. * another mapping, we will catch it; if this other
  325. * mapping is already gone, the unmap path will have
  326. * set PG_referenced or activated the page.
  327. */
  328. if (likely(!VM_SequentialReadHint(vma)))
  329. referenced++;
  330. }
  331. /* Pretend the page is referenced if the task has the
  332. swap token and is in the middle of a page fault. */
  333. if (mm != current->mm && has_swap_token(mm) &&
  334. rwsem_is_locked(&mm->mmap_sem))
  335. referenced++;
  336. out_unmap:
  337. (*mapcount)--;
  338. pte_unmap_unlock(pte, ptl);
  339. if (referenced)
  340. *vm_flags |= vma->vm_flags;
  341. out:
  342. return referenced;
  343. }
  344. static int page_referenced_anon(struct page *page,
  345. struct mem_cgroup *mem_cont,
  346. unsigned long *vm_flags)
  347. {
  348. unsigned int mapcount;
  349. struct anon_vma *anon_vma;
  350. struct vm_area_struct *vma;
  351. int referenced = 0;
  352. anon_vma = page_lock_anon_vma(page);
  353. if (!anon_vma)
  354. return referenced;
  355. mapcount = page_mapcount(page);
  356. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  357. unsigned long address = vma_address(page, vma);
  358. if (address == -EFAULT)
  359. continue;
  360. /*
  361. * If we are reclaiming on behalf of a cgroup, skip
  362. * counting on behalf of references from different
  363. * cgroups
  364. */
  365. if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
  366. continue;
  367. referenced += page_referenced_one(page, vma, address,
  368. &mapcount, vm_flags);
  369. if (!mapcount)
  370. break;
  371. }
  372. page_unlock_anon_vma(anon_vma);
  373. return referenced;
  374. }
  375. /**
  376. * page_referenced_file - referenced check for object-based rmap
  377. * @page: the page we're checking references on.
  378. * @mem_cont: target memory controller
  379. * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
  380. *
  381. * For an object-based mapped page, find all the places it is mapped and
  382. * check/clear the referenced flag. This is done by following the page->mapping
  383. * pointer, then walking the chain of vmas it holds. It returns the number
  384. * of references it found.
  385. *
  386. * This function is only called from page_referenced for object-based pages.
  387. */
  388. static int page_referenced_file(struct page *page,
  389. struct mem_cgroup *mem_cont,
  390. unsigned long *vm_flags)
  391. {
  392. unsigned int mapcount;
  393. struct address_space *mapping = page->mapping;
  394. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  395. struct vm_area_struct *vma;
  396. struct prio_tree_iter iter;
  397. int referenced = 0;
  398. /*
  399. * The caller's checks on page->mapping and !PageAnon have made
  400. * sure that this is a file page: the check for page->mapping
  401. * excludes the case just before it gets set on an anon page.
  402. */
  403. BUG_ON(PageAnon(page));
  404. /*
  405. * The page lock not only makes sure that page->mapping cannot
  406. * suddenly be NULLified by truncation, it makes sure that the
  407. * structure at mapping cannot be freed and reused yet,
  408. * so we can safely take mapping->i_mmap_lock.
  409. */
  410. BUG_ON(!PageLocked(page));
  411. spin_lock(&mapping->i_mmap_lock);
  412. /*
  413. * i_mmap_lock does not stabilize mapcount at all, but mapcount
  414. * is more likely to be accurate if we note it after spinning.
  415. */
  416. mapcount = page_mapcount(page);
  417. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  418. unsigned long address = vma_address(page, vma);
  419. if (address == -EFAULT)
  420. continue;
  421. /*
  422. * If we are reclaiming on behalf of a cgroup, skip
  423. * counting on behalf of references from different
  424. * cgroups
  425. */
  426. if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
  427. continue;
  428. referenced += page_referenced_one(page, vma, address,
  429. &mapcount, vm_flags);
  430. if (!mapcount)
  431. break;
  432. }
  433. spin_unlock(&mapping->i_mmap_lock);
  434. return referenced;
  435. }
  436. /**
  437. * page_referenced - test if the page was referenced
  438. * @page: the page to test
  439. * @is_locked: caller holds lock on the page
  440. * @mem_cont: target memory controller
  441. * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
  442. *
  443. * Quick test_and_clear_referenced for all mappings to a page,
  444. * returns the number of ptes which referenced the page.
  445. */
  446. int page_referenced(struct page *page,
  447. int is_locked,
  448. struct mem_cgroup *mem_cont,
  449. unsigned long *vm_flags)
  450. {
  451. int referenced = 0;
  452. int we_locked = 0;
  453. if (TestClearPageReferenced(page))
  454. referenced++;
  455. *vm_flags = 0;
  456. if (page_mapped(page) && page_rmapping(page)) {
  457. if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
  458. we_locked = trylock_page(page);
  459. if (!we_locked) {
  460. referenced++;
  461. goto out;
  462. }
  463. }
  464. if (unlikely(PageKsm(page)))
  465. referenced += page_referenced_ksm(page, mem_cont,
  466. vm_flags);
  467. else if (PageAnon(page))
  468. referenced += page_referenced_anon(page, mem_cont,
  469. vm_flags);
  470. else if (page->mapping)
  471. referenced += page_referenced_file(page, mem_cont,
  472. vm_flags);
  473. if (we_locked)
  474. unlock_page(page);
  475. }
  476. out:
  477. if (page_test_and_clear_young(page))
  478. referenced++;
  479. return referenced;
  480. }
  481. static int page_mkclean_one(struct page *page, struct vm_area_struct *vma,
  482. unsigned long address)
  483. {
  484. struct mm_struct *mm = vma->vm_mm;
  485. pte_t *pte;
  486. spinlock_t *ptl;
  487. int ret = 0;
  488. pte = page_check_address(page, mm, address, &ptl, 1);
  489. if (!pte)
  490. goto out;
  491. if (pte_dirty(*pte) || pte_write(*pte)) {
  492. pte_t entry;
  493. flush_cache_page(vma, address, pte_pfn(*pte));
  494. entry = ptep_clear_flush_notify(vma, address, pte);
  495. entry = pte_wrprotect(entry);
  496. entry = pte_mkclean(entry);
  497. set_pte_at(mm, address, pte, entry);
  498. ret = 1;
  499. }
  500. pte_unmap_unlock(pte, ptl);
  501. out:
  502. return ret;
  503. }
  504. static int page_mkclean_file(struct address_space *mapping, struct page *page)
  505. {
  506. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  507. struct vm_area_struct *vma;
  508. struct prio_tree_iter iter;
  509. int ret = 0;
  510. BUG_ON(PageAnon(page));
  511. spin_lock(&mapping->i_mmap_lock);
  512. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  513. if (vma->vm_flags & VM_SHARED) {
  514. unsigned long address = vma_address(page, vma);
  515. if (address == -EFAULT)
  516. continue;
  517. ret += page_mkclean_one(page, vma, address);
  518. }
  519. }
  520. spin_unlock(&mapping->i_mmap_lock);
  521. return ret;
  522. }
  523. int page_mkclean(struct page *page)
  524. {
  525. int ret = 0;
  526. BUG_ON(!PageLocked(page));
  527. if (page_mapped(page)) {
  528. struct address_space *mapping = page_mapping(page);
  529. if (mapping) {
  530. ret = page_mkclean_file(mapping, page);
  531. if (page_test_dirty(page)) {
  532. page_clear_dirty(page);
  533. ret = 1;
  534. }
  535. }
  536. }
  537. return ret;
  538. }
  539. EXPORT_SYMBOL_GPL(page_mkclean);
  540. /**
  541. * __page_set_anon_rmap - setup new anonymous rmap
  542. * @page: the page to add the mapping to
  543. * @vma: the vm area in which the mapping is added
  544. * @address: the user virtual address mapped
  545. */
  546. static void __page_set_anon_rmap(struct page *page,
  547. struct vm_area_struct *vma, unsigned long address)
  548. {
  549. struct anon_vma *anon_vma = vma->anon_vma;
  550. BUG_ON(!anon_vma);
  551. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  552. page->mapping = (struct address_space *) anon_vma;
  553. page->index = linear_page_index(vma, address);
  554. }
  555. /**
  556. * __page_check_anon_rmap - sanity check anonymous rmap addition
  557. * @page: the page to add the mapping to
  558. * @vma: the vm area in which the mapping is added
  559. * @address: the user virtual address mapped
  560. */
  561. static void __page_check_anon_rmap(struct page *page,
  562. struct vm_area_struct *vma, unsigned long address)
  563. {
  564. #ifdef CONFIG_DEBUG_VM
  565. /*
  566. * The page's anon-rmap details (mapping and index) are guaranteed to
  567. * be set up correctly at this point.
  568. *
  569. * We have exclusion against page_add_anon_rmap because the caller
  570. * always holds the page locked, except if called from page_dup_rmap,
  571. * in which case the page is already known to be setup.
  572. *
  573. * We have exclusion against page_add_new_anon_rmap because those pages
  574. * are initially only visible via the pagetables, and the pte is locked
  575. * over the call to page_add_new_anon_rmap.
  576. */
  577. struct anon_vma *anon_vma = vma->anon_vma;
  578. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  579. BUG_ON(page->mapping != (struct address_space *)anon_vma);
  580. BUG_ON(page->index != linear_page_index(vma, address));
  581. #endif
  582. }
  583. /**
  584. * page_add_anon_rmap - add pte mapping to an anonymous page
  585. * @page: the page to add the mapping to
  586. * @vma: the vm area in which the mapping is added
  587. * @address: the user virtual address mapped
  588. *
  589. * The caller needs to hold the pte lock, and the page must be locked in
  590. * the anon_vma case: to serialize mapping,index checking after setting,
  591. * and to ensure that PageAnon is not being upgraded racily to PageKsm
  592. * (but PageKsm is never downgraded to PageAnon).
  593. */
  594. void page_add_anon_rmap(struct page *page,
  595. struct vm_area_struct *vma, unsigned long address)
  596. {
  597. int first = atomic_inc_and_test(&page->_mapcount);
  598. if (first)
  599. __inc_zone_page_state(page, NR_ANON_PAGES);
  600. if (unlikely(PageKsm(page)))
  601. return;
  602. VM_BUG_ON(!PageLocked(page));
  603. VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  604. if (first)
  605. __page_set_anon_rmap(page, vma, address);
  606. else
  607. __page_check_anon_rmap(page, vma, address);
  608. }
  609. /**
  610. * page_add_new_anon_rmap - add pte mapping to a new anonymous page
  611. * @page: the page to add the mapping to
  612. * @vma: the vm area in which the mapping is added
  613. * @address: the user virtual address mapped
  614. *
  615. * Same as page_add_anon_rmap but must only be called on *new* pages.
  616. * This means the inc-and-test can be bypassed.
  617. * Page does not have to be locked.
  618. */
  619. void page_add_new_anon_rmap(struct page *page,
  620. struct vm_area_struct *vma, unsigned long address)
  621. {
  622. VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  623. SetPageSwapBacked(page);
  624. atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
  625. __inc_zone_page_state(page, NR_ANON_PAGES);
  626. __page_set_anon_rmap(page, vma, address);
  627. if (page_evictable(page, vma))
  628. lru_cache_add_lru(page, LRU_ACTIVE_ANON);
  629. else
  630. add_page_to_unevictable_list(page);
  631. }
  632. /**
  633. * page_add_file_rmap - add pte mapping to a file page
  634. * @page: the page to add the mapping to
  635. *
  636. * The caller needs to hold the pte lock.
  637. */
  638. void page_add_file_rmap(struct page *page)
  639. {
  640. if (atomic_inc_and_test(&page->_mapcount)) {
  641. __inc_zone_page_state(page, NR_FILE_MAPPED);
  642. mem_cgroup_update_file_mapped(page, 1);
  643. }
  644. }
  645. /**
  646. * page_remove_rmap - take down pte mapping from a page
  647. * @page: page to remove mapping from
  648. *
  649. * The caller needs to hold the pte lock.
  650. */
  651. void page_remove_rmap(struct page *page)
  652. {
  653. /* page still mapped by someone else? */
  654. if (!atomic_add_negative(-1, &page->_mapcount))
  655. return;
  656. /*
  657. * Now that the last pte has gone, s390 must transfer dirty
  658. * flag from storage key to struct page. We can usually skip
  659. * this if the page is anon, so about to be freed; but perhaps
  660. * not if it's in swapcache - there might be another pte slot
  661. * containing the swap entry, but page not yet written to swap.
  662. */
  663. if ((!PageAnon(page) || PageSwapCache(page)) && page_test_dirty(page)) {
  664. page_clear_dirty(page);
  665. set_page_dirty(page);
  666. }
  667. if (PageAnon(page)) {
  668. mem_cgroup_uncharge_page(page);
  669. __dec_zone_page_state(page, NR_ANON_PAGES);
  670. } else {
  671. __dec_zone_page_state(page, NR_FILE_MAPPED);
  672. mem_cgroup_update_file_mapped(page, -1);
  673. }
  674. /*
  675. * It would be tidy to reset the PageAnon mapping here,
  676. * but that might overwrite a racing page_add_anon_rmap
  677. * which increments mapcount after us but sets mapping
  678. * before us: so leave the reset to free_hot_cold_page,
  679. * and remember that it's only reliable while mapped.
  680. * Leaving it set also helps swapoff to reinstate ptes
  681. * faster for those pages still in swapcache.
  682. */
  683. }
  684. /*
  685. * Subfunctions of try_to_unmap: try_to_unmap_one called
  686. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  687. */
  688. int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  689. unsigned long address, enum ttu_flags flags)
  690. {
  691. struct mm_struct *mm = vma->vm_mm;
  692. pte_t *pte;
  693. pte_t pteval;
  694. spinlock_t *ptl;
  695. int ret = SWAP_AGAIN;
  696. pte = page_check_address(page, mm, address, &ptl, 0);
  697. if (!pte)
  698. goto out;
  699. /*
  700. * If the page is mlock()d, we cannot swap it out.
  701. * If it's recently referenced (perhaps page_referenced
  702. * skipped over this mm) then we should reactivate it.
  703. */
  704. if (!(flags & TTU_IGNORE_MLOCK)) {
  705. if (vma->vm_flags & VM_LOCKED)
  706. goto out_mlock;
  707. if (TTU_ACTION(flags) == TTU_MUNLOCK)
  708. goto out_unmap;
  709. }
  710. if (!(flags & TTU_IGNORE_ACCESS)) {
  711. if (ptep_clear_flush_young_notify(vma, address, pte)) {
  712. ret = SWAP_FAIL;
  713. goto out_unmap;
  714. }
  715. }
  716. /* Nuke the page table entry. */
  717. flush_cache_page(vma, address, page_to_pfn(page));
  718. pteval = ptep_clear_flush_notify(vma, address, pte);
  719. /* Move the dirty bit to the physical page now the pte is gone. */
  720. if (pte_dirty(pteval))
  721. set_page_dirty(page);
  722. /* Update high watermark before we lower rss */
  723. update_hiwater_rss(mm);
  724. if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
  725. if (PageAnon(page))
  726. dec_mm_counter(mm, MM_ANONPAGES);
  727. else
  728. dec_mm_counter(mm, MM_FILEPAGES);
  729. set_pte_at(mm, address, pte,
  730. swp_entry_to_pte(make_hwpoison_entry(page)));
  731. } else if (PageAnon(page)) {
  732. swp_entry_t entry = { .val = page_private(page) };
  733. if (PageSwapCache(page)) {
  734. /*
  735. * Store the swap location in the pte.
  736. * See handle_pte_fault() ...
  737. */
  738. if (swap_duplicate(entry) < 0) {
  739. set_pte_at(mm, address, pte, pteval);
  740. ret = SWAP_FAIL;
  741. goto out_unmap;
  742. }
  743. if (list_empty(&mm->mmlist)) {
  744. spin_lock(&mmlist_lock);
  745. if (list_empty(&mm->mmlist))
  746. list_add(&mm->mmlist, &init_mm.mmlist);
  747. spin_unlock(&mmlist_lock);
  748. }
  749. dec_mm_counter(mm, MM_ANONPAGES);
  750. inc_mm_counter(mm, MM_SWAPENTS);
  751. } else if (PAGE_MIGRATION) {
  752. /*
  753. * Store the pfn of the page in a special migration
  754. * pte. do_swap_page() will wait until the migration
  755. * pte is removed and then restart fault handling.
  756. */
  757. BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
  758. entry = make_migration_entry(page, pte_write(pteval));
  759. }
  760. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  761. BUG_ON(pte_file(*pte));
  762. } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
  763. /* Establish migration entry for a file page */
  764. swp_entry_t entry;
  765. entry = make_migration_entry(page, pte_write(pteval));
  766. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  767. } else
  768. dec_mm_counter(mm, MM_FILEPAGES);
  769. page_remove_rmap(page);
  770. page_cache_release(page);
  771. out_unmap:
  772. pte_unmap_unlock(pte, ptl);
  773. out:
  774. return ret;
  775. out_mlock:
  776. pte_unmap_unlock(pte, ptl);
  777. /*
  778. * We need mmap_sem locking, Otherwise VM_LOCKED check makes
  779. * unstable result and race. Plus, We can't wait here because
  780. * we now hold anon_vma->lock or mapping->i_mmap_lock.
  781. * if trylock failed, the page remain in evictable lru and later
  782. * vmscan could retry to move the page to unevictable lru if the
  783. * page is actually mlocked.
  784. */
  785. if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
  786. if (vma->vm_flags & VM_LOCKED) {
  787. mlock_vma_page(page);
  788. ret = SWAP_MLOCK;
  789. }
  790. up_read(&vma->vm_mm->mmap_sem);
  791. }
  792. return ret;
  793. }
  794. /*
  795. * objrmap doesn't work for nonlinear VMAs because the assumption that
  796. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  797. * Consequently, given a particular page and its ->index, we cannot locate the
  798. * ptes which are mapping that page without an exhaustive linear search.
  799. *
  800. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  801. * maps the file to which the target page belongs. The ->vm_private_data field
  802. * holds the current cursor into that scan. Successive searches will circulate
  803. * around the vma's virtual address space.
  804. *
  805. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  806. * more scanning pressure is placed against them as well. Eventually pages
  807. * will become fully unmapped and are eligible for eviction.
  808. *
  809. * For very sparsely populated VMAs this is a little inefficient - chances are
  810. * there there won't be many ptes located within the scan cluster. In this case
  811. * maybe we could scan further - to the end of the pte page, perhaps.
  812. *
  813. * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
  814. * acquire it without blocking. If vma locked, mlock the pages in the cluster,
  815. * rather than unmapping them. If we encounter the "check_page" that vmscan is
  816. * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
  817. */
  818. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  819. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  820. static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
  821. struct vm_area_struct *vma, struct page *check_page)
  822. {
  823. struct mm_struct *mm = vma->vm_mm;
  824. pgd_t *pgd;
  825. pud_t *pud;
  826. pmd_t *pmd;
  827. pte_t *pte;
  828. pte_t pteval;
  829. spinlock_t *ptl;
  830. struct page *page;
  831. unsigned long address;
  832. unsigned long end;
  833. int ret = SWAP_AGAIN;
  834. int locked_vma = 0;
  835. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  836. end = address + CLUSTER_SIZE;
  837. if (address < vma->vm_start)
  838. address = vma->vm_start;
  839. if (end > vma->vm_end)
  840. end = vma->vm_end;
  841. pgd = pgd_offset(mm, address);
  842. if (!pgd_present(*pgd))
  843. return ret;
  844. pud = pud_offset(pgd, address);
  845. if (!pud_present(*pud))
  846. return ret;
  847. pmd = pmd_offset(pud, address);
  848. if (!pmd_present(*pmd))
  849. return ret;
  850. /*
  851. * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
  852. * keep the sem while scanning the cluster for mlocking pages.
  853. */
  854. if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
  855. locked_vma = (vma->vm_flags & VM_LOCKED);
  856. if (!locked_vma)
  857. up_read(&vma->vm_mm->mmap_sem); /* don't need it */
  858. }
  859. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  860. /* Update high watermark before we lower rss */
  861. update_hiwater_rss(mm);
  862. for (; address < end; pte++, address += PAGE_SIZE) {
  863. if (!pte_present(*pte))
  864. continue;
  865. page = vm_normal_page(vma, address, *pte);
  866. BUG_ON(!page || PageAnon(page));
  867. if (locked_vma) {
  868. mlock_vma_page(page); /* no-op if already mlocked */
  869. if (page == check_page)
  870. ret = SWAP_MLOCK;
  871. continue; /* don't unmap */
  872. }
  873. if (ptep_clear_flush_young_notify(vma, address, pte))
  874. continue;
  875. /* Nuke the page table entry. */
  876. flush_cache_page(vma, address, pte_pfn(*pte));
  877. pteval = ptep_clear_flush_notify(vma, address, pte);
  878. /* If nonlinear, store the file page offset in the pte. */
  879. if (page->index != linear_page_index(vma, address))
  880. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  881. /* Move the dirty bit to the physical page now the pte is gone. */
  882. if (pte_dirty(pteval))
  883. set_page_dirty(page);
  884. page_remove_rmap(page);
  885. page_cache_release(page);
  886. dec_mm_counter(mm, MM_FILEPAGES);
  887. (*mapcount)--;
  888. }
  889. pte_unmap_unlock(pte - 1, ptl);
  890. if (locked_vma)
  891. up_read(&vma->vm_mm->mmap_sem);
  892. return ret;
  893. }
  894. /**
  895. * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
  896. * rmap method
  897. * @page: the page to unmap/unlock
  898. * @flags: action and flags
  899. *
  900. * Find all the mappings of a page using the mapping pointer and the vma chains
  901. * contained in the anon_vma struct it points to.
  902. *
  903. * This function is only called from try_to_unmap/try_to_munlock for
  904. * anonymous pages.
  905. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  906. * where the page was found will be held for write. So, we won't recheck
  907. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  908. * 'LOCKED.
  909. */
  910. static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
  911. {
  912. struct anon_vma *anon_vma;
  913. struct vm_area_struct *vma;
  914. int ret = SWAP_AGAIN;
  915. anon_vma = page_lock_anon_vma(page);
  916. if (!anon_vma)
  917. return ret;
  918. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  919. unsigned long address = vma_address(page, vma);
  920. if (address == -EFAULT)
  921. continue;
  922. ret = try_to_unmap_one(page, vma, address, flags);
  923. if (ret != SWAP_AGAIN || !page_mapped(page))
  924. break;
  925. }
  926. page_unlock_anon_vma(anon_vma);
  927. return ret;
  928. }
  929. /**
  930. * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
  931. * @page: the page to unmap/unlock
  932. * @flags: action and flags
  933. *
  934. * Find all the mappings of a page using the mapping pointer and the vma chains
  935. * contained in the address_space struct it points to.
  936. *
  937. * This function is only called from try_to_unmap/try_to_munlock for
  938. * object-based pages.
  939. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  940. * where the page was found will be held for write. So, we won't recheck
  941. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  942. * 'LOCKED.
  943. */
  944. static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
  945. {
  946. struct address_space *mapping = page->mapping;
  947. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  948. struct vm_area_struct *vma;
  949. struct prio_tree_iter iter;
  950. int ret = SWAP_AGAIN;
  951. unsigned long cursor;
  952. unsigned long max_nl_cursor = 0;
  953. unsigned long max_nl_size = 0;
  954. unsigned int mapcount;
  955. spin_lock(&mapping->i_mmap_lock);
  956. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  957. unsigned long address = vma_address(page, vma);
  958. if (address == -EFAULT)
  959. continue;
  960. ret = try_to_unmap_one(page, vma, address, flags);
  961. if (ret != SWAP_AGAIN || !page_mapped(page))
  962. goto out;
  963. }
  964. if (list_empty(&mapping->i_mmap_nonlinear))
  965. goto out;
  966. /*
  967. * We don't bother to try to find the munlocked page in nonlinears.
  968. * It's costly. Instead, later, page reclaim logic may call
  969. * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily.
  970. */
  971. if (TTU_ACTION(flags) == TTU_MUNLOCK)
  972. goto out;
  973. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  974. shared.vm_set.list) {
  975. cursor = (unsigned long) vma->vm_private_data;
  976. if (cursor > max_nl_cursor)
  977. max_nl_cursor = cursor;
  978. cursor = vma->vm_end - vma->vm_start;
  979. if (cursor > max_nl_size)
  980. max_nl_size = cursor;
  981. }
  982. if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
  983. ret = SWAP_FAIL;
  984. goto out;
  985. }
  986. /*
  987. * We don't try to search for this page in the nonlinear vmas,
  988. * and page_referenced wouldn't have found it anyway. Instead
  989. * just walk the nonlinear vmas trying to age and unmap some.
  990. * The mapcount of the page we came in with is irrelevant,
  991. * but even so use it as a guide to how hard we should try?
  992. */
  993. mapcount = page_mapcount(page);
  994. if (!mapcount)
  995. goto out;
  996. cond_resched_lock(&mapping->i_mmap_lock);
  997. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  998. if (max_nl_cursor == 0)
  999. max_nl_cursor = CLUSTER_SIZE;
  1000. do {
  1001. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  1002. shared.vm_set.list) {
  1003. cursor = (unsigned long) vma->vm_private_data;
  1004. while ( cursor < max_nl_cursor &&
  1005. cursor < vma->vm_end - vma->vm_start) {
  1006. if (try_to_unmap_cluster(cursor, &mapcount,
  1007. vma, page) == SWAP_MLOCK)
  1008. ret = SWAP_MLOCK;
  1009. cursor += CLUSTER_SIZE;
  1010. vma->vm_private_data = (void *) cursor;
  1011. if ((int)mapcount <= 0)
  1012. goto out;
  1013. }
  1014. vma->vm_private_data = (void *) max_nl_cursor;
  1015. }
  1016. cond_resched_lock(&mapping->i_mmap_lock);
  1017. max_nl_cursor += CLUSTER_SIZE;
  1018. } while (max_nl_cursor <= max_nl_size);
  1019. /*
  1020. * Don't loop forever (perhaps all the remaining pages are
  1021. * in locked vmas). Reset cursor on all unreserved nonlinear
  1022. * vmas, now forgetting on which ones it had fallen behind.
  1023. */
  1024. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1025. vma->vm_private_data = NULL;
  1026. out:
  1027. spin_unlock(&mapping->i_mmap_lock);
  1028. return ret;
  1029. }
  1030. /**
  1031. * try_to_unmap - try to remove all page table mappings to a page
  1032. * @page: the page to get unmapped
  1033. * @flags: action and flags
  1034. *
  1035. * Tries to remove all the page table entries which are mapping this
  1036. * page, used in the pageout path. Caller must hold the page lock.
  1037. * Return values are:
  1038. *
  1039. * SWAP_SUCCESS - we succeeded in removing all mappings
  1040. * SWAP_AGAIN - we missed a mapping, try again later
  1041. * SWAP_FAIL - the page is unswappable
  1042. * SWAP_MLOCK - page is mlocked.
  1043. */
  1044. int try_to_unmap(struct page *page, enum ttu_flags flags)
  1045. {
  1046. int ret;
  1047. BUG_ON(!PageLocked(page));
  1048. if (unlikely(PageKsm(page)))
  1049. ret = try_to_unmap_ksm(page, flags);
  1050. else if (PageAnon(page))
  1051. ret = try_to_unmap_anon(page, flags);
  1052. else
  1053. ret = try_to_unmap_file(page, flags);
  1054. if (ret != SWAP_MLOCK && !page_mapped(page))
  1055. ret = SWAP_SUCCESS;
  1056. return ret;
  1057. }
  1058. /**
  1059. * try_to_munlock - try to munlock a page
  1060. * @page: the page to be munlocked
  1061. *
  1062. * Called from munlock code. Checks all of the VMAs mapping the page
  1063. * to make sure nobody else has this page mlocked. The page will be
  1064. * returned with PG_mlocked cleared if no other vmas have it mlocked.
  1065. *
  1066. * Return values are:
  1067. *
  1068. * SWAP_AGAIN - no vma is holding page mlocked, or,
  1069. * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
  1070. * SWAP_FAIL - page cannot be located at present
  1071. * SWAP_MLOCK - page is now mlocked.
  1072. */
  1073. int try_to_munlock(struct page *page)
  1074. {
  1075. VM_BUG_ON(!PageLocked(page) || PageLRU(page));
  1076. if (unlikely(PageKsm(page)))
  1077. return try_to_unmap_ksm(page, TTU_MUNLOCK);
  1078. else if (PageAnon(page))
  1079. return try_to_unmap_anon(page, TTU_MUNLOCK);
  1080. else
  1081. return try_to_unmap_file(page, TTU_MUNLOCK);
  1082. }
  1083. #ifdef CONFIG_MIGRATION
  1084. /*
  1085. * rmap_walk() and its helpers rmap_walk_anon() and rmap_walk_file():
  1086. * Called by migrate.c to remove migration ptes, but might be used more later.
  1087. */
  1088. static int rmap_walk_anon(struct page *page, int (*rmap_one)(struct page *,
  1089. struct vm_area_struct *, unsigned long, void *), void *arg)
  1090. {
  1091. struct anon_vma *anon_vma;
  1092. struct vm_area_struct *vma;
  1093. int ret = SWAP_AGAIN;
  1094. /*
  1095. * Note: remove_migration_ptes() cannot use page_lock_anon_vma()
  1096. * because that depends on page_mapped(); but not all its usages
  1097. * are holding mmap_sem, which also gave the necessary guarantee
  1098. * (that this anon_vma's slab has not already been destroyed).
  1099. * This needs to be reviewed later: avoiding page_lock_anon_vma()
  1100. * is risky, and currently limits the usefulness of rmap_walk().
  1101. */
  1102. anon_vma = page_anon_vma(page);
  1103. if (!anon_vma)
  1104. return ret;
  1105. spin_lock(&anon_vma->lock);
  1106. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  1107. unsigned long address = vma_address(page, vma);
  1108. if (address == -EFAULT)
  1109. continue;
  1110. ret = rmap_one(page, vma, address, arg);
  1111. if (ret != SWAP_AGAIN)
  1112. break;
  1113. }
  1114. spin_unlock(&anon_vma->lock);
  1115. return ret;
  1116. }
  1117. static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *,
  1118. struct vm_area_struct *, unsigned long, void *), void *arg)
  1119. {
  1120. struct address_space *mapping = page->mapping;
  1121. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  1122. struct vm_area_struct *vma;
  1123. struct prio_tree_iter iter;
  1124. int ret = SWAP_AGAIN;
  1125. if (!mapping)
  1126. return ret;
  1127. spin_lock(&mapping->i_mmap_lock);
  1128. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1129. unsigned long address = vma_address(page, vma);
  1130. if (address == -EFAULT)
  1131. continue;
  1132. ret = rmap_one(page, vma, address, arg);
  1133. if (ret != SWAP_AGAIN)
  1134. break;
  1135. }
  1136. /*
  1137. * No nonlinear handling: being always shared, nonlinear vmas
  1138. * never contain migration ptes. Decide what to do about this
  1139. * limitation to linear when we need rmap_walk() on nonlinear.
  1140. */
  1141. spin_unlock(&mapping->i_mmap_lock);
  1142. return ret;
  1143. }
  1144. int rmap_walk(struct page *page, int (*rmap_one)(struct page *,
  1145. struct vm_area_struct *, unsigned long, void *), void *arg)
  1146. {
  1147. VM_BUG_ON(!PageLocked(page));
  1148. if (unlikely(PageKsm(page)))
  1149. return rmap_walk_ksm(page, rmap_one, arg);
  1150. else if (PageAnon(page))
  1151. return rmap_walk_anon(page, rmap_one, arg);
  1152. else
  1153. return rmap_walk_file(page, rmap_one, arg);
  1154. }
  1155. #endif /* CONFIG_MIGRATION */