rmap.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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, anon_rss);
  727. else
  728. dec_mm_counter(mm, file_rss);
  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, anon_rss);
  750. } else if (PAGE_MIGRATION) {
  751. /*
  752. * Store the pfn of the page in a special migration
  753. * pte. do_swap_page() will wait until the migration
  754. * pte is removed and then restart fault handling.
  755. */
  756. BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
  757. entry = make_migration_entry(page, pte_write(pteval));
  758. }
  759. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  760. BUG_ON(pte_file(*pte));
  761. } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
  762. /* Establish migration entry for a file page */
  763. swp_entry_t entry;
  764. entry = make_migration_entry(page, pte_write(pteval));
  765. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  766. } else
  767. dec_mm_counter(mm, file_rss);
  768. page_remove_rmap(page);
  769. page_cache_release(page);
  770. out_unmap:
  771. pte_unmap_unlock(pte, ptl);
  772. out:
  773. return ret;
  774. out_mlock:
  775. pte_unmap_unlock(pte, ptl);
  776. /*
  777. * We need mmap_sem locking, Otherwise VM_LOCKED check makes
  778. * unstable result and race. Plus, We can't wait here because
  779. * we now hold anon_vma->lock or mapping->i_mmap_lock.
  780. * if trylock failed, the page remain in evictable lru and later
  781. * vmscan could retry to move the page to unevictable lru if the
  782. * page is actually mlocked.
  783. */
  784. if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
  785. if (vma->vm_flags & VM_LOCKED) {
  786. mlock_vma_page(page);
  787. ret = SWAP_MLOCK;
  788. }
  789. up_read(&vma->vm_mm->mmap_sem);
  790. }
  791. return ret;
  792. }
  793. /*
  794. * objrmap doesn't work for nonlinear VMAs because the assumption that
  795. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  796. * Consequently, given a particular page and its ->index, we cannot locate the
  797. * ptes which are mapping that page without an exhaustive linear search.
  798. *
  799. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  800. * maps the file to which the target page belongs. The ->vm_private_data field
  801. * holds the current cursor into that scan. Successive searches will circulate
  802. * around the vma's virtual address space.
  803. *
  804. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  805. * more scanning pressure is placed against them as well. Eventually pages
  806. * will become fully unmapped and are eligible for eviction.
  807. *
  808. * For very sparsely populated VMAs this is a little inefficient - chances are
  809. * there there won't be many ptes located within the scan cluster. In this case
  810. * maybe we could scan further - to the end of the pte page, perhaps.
  811. *
  812. * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
  813. * acquire it without blocking. If vma locked, mlock the pages in the cluster,
  814. * rather than unmapping them. If we encounter the "check_page" that vmscan is
  815. * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
  816. */
  817. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  818. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  819. static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
  820. struct vm_area_struct *vma, struct page *check_page)
  821. {
  822. struct mm_struct *mm = vma->vm_mm;
  823. pgd_t *pgd;
  824. pud_t *pud;
  825. pmd_t *pmd;
  826. pte_t *pte;
  827. pte_t pteval;
  828. spinlock_t *ptl;
  829. struct page *page;
  830. unsigned long address;
  831. unsigned long end;
  832. int ret = SWAP_AGAIN;
  833. int locked_vma = 0;
  834. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  835. end = address + CLUSTER_SIZE;
  836. if (address < vma->vm_start)
  837. address = vma->vm_start;
  838. if (end > vma->vm_end)
  839. end = vma->vm_end;
  840. pgd = pgd_offset(mm, address);
  841. if (!pgd_present(*pgd))
  842. return ret;
  843. pud = pud_offset(pgd, address);
  844. if (!pud_present(*pud))
  845. return ret;
  846. pmd = pmd_offset(pud, address);
  847. if (!pmd_present(*pmd))
  848. return ret;
  849. /*
  850. * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
  851. * keep the sem while scanning the cluster for mlocking pages.
  852. */
  853. if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
  854. locked_vma = (vma->vm_flags & VM_LOCKED);
  855. if (!locked_vma)
  856. up_read(&vma->vm_mm->mmap_sem); /* don't need it */
  857. }
  858. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  859. /* Update high watermark before we lower rss */
  860. update_hiwater_rss(mm);
  861. for (; address < end; pte++, address += PAGE_SIZE) {
  862. if (!pte_present(*pte))
  863. continue;
  864. page = vm_normal_page(vma, address, *pte);
  865. BUG_ON(!page || PageAnon(page));
  866. if (locked_vma) {
  867. mlock_vma_page(page); /* no-op if already mlocked */
  868. if (page == check_page)
  869. ret = SWAP_MLOCK;
  870. continue; /* don't unmap */
  871. }
  872. if (ptep_clear_flush_young_notify(vma, address, pte))
  873. continue;
  874. /* Nuke the page table entry. */
  875. flush_cache_page(vma, address, pte_pfn(*pte));
  876. pteval = ptep_clear_flush_notify(vma, address, pte);
  877. /* If nonlinear, store the file page offset in the pte. */
  878. if (page->index != linear_page_index(vma, address))
  879. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  880. /* Move the dirty bit to the physical page now the pte is gone. */
  881. if (pte_dirty(pteval))
  882. set_page_dirty(page);
  883. page_remove_rmap(page);
  884. page_cache_release(page);
  885. dec_mm_counter(mm, file_rss);
  886. (*mapcount)--;
  887. }
  888. pte_unmap_unlock(pte - 1, ptl);
  889. if (locked_vma)
  890. up_read(&vma->vm_mm->mmap_sem);
  891. return ret;
  892. }
  893. /**
  894. * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
  895. * rmap method
  896. * @page: the page to unmap/unlock
  897. * @flags: action and flags
  898. *
  899. * Find all the mappings of a page using the mapping pointer and the vma chains
  900. * contained in the anon_vma struct it points to.
  901. *
  902. * This function is only called from try_to_unmap/try_to_munlock for
  903. * anonymous pages.
  904. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  905. * where the page was found will be held for write. So, we won't recheck
  906. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  907. * 'LOCKED.
  908. */
  909. static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
  910. {
  911. struct anon_vma *anon_vma;
  912. struct vm_area_struct *vma;
  913. int ret = SWAP_AGAIN;
  914. anon_vma = page_lock_anon_vma(page);
  915. if (!anon_vma)
  916. return ret;
  917. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  918. unsigned long address = vma_address(page, vma);
  919. if (address == -EFAULT)
  920. continue;
  921. ret = try_to_unmap_one(page, vma, address, flags);
  922. if (ret != SWAP_AGAIN || !page_mapped(page))
  923. break;
  924. }
  925. page_unlock_anon_vma(anon_vma);
  926. return ret;
  927. }
  928. /**
  929. * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
  930. * @page: the page to unmap/unlock
  931. * @flags: action and flags
  932. *
  933. * Find all the mappings of a page using the mapping pointer and the vma chains
  934. * contained in the address_space struct it points to.
  935. *
  936. * This function is only called from try_to_unmap/try_to_munlock for
  937. * object-based pages.
  938. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  939. * where the page was found will be held for write. So, we won't recheck
  940. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  941. * 'LOCKED.
  942. */
  943. static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
  944. {
  945. struct address_space *mapping = page->mapping;
  946. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  947. struct vm_area_struct *vma;
  948. struct prio_tree_iter iter;
  949. int ret = SWAP_AGAIN;
  950. unsigned long cursor;
  951. unsigned long max_nl_cursor = 0;
  952. unsigned long max_nl_size = 0;
  953. unsigned int mapcount;
  954. spin_lock(&mapping->i_mmap_lock);
  955. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  956. unsigned long address = vma_address(page, vma);
  957. if (address == -EFAULT)
  958. continue;
  959. ret = try_to_unmap_one(page, vma, address, flags);
  960. if (ret != SWAP_AGAIN || !page_mapped(page))
  961. goto out;
  962. }
  963. if (list_empty(&mapping->i_mmap_nonlinear))
  964. goto out;
  965. /*
  966. * We don't bother to try to find the munlocked page in nonlinears.
  967. * It's costly. Instead, later, page reclaim logic may call
  968. * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily.
  969. */
  970. if (TTU_ACTION(flags) == TTU_MUNLOCK)
  971. goto out;
  972. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  973. shared.vm_set.list) {
  974. cursor = (unsigned long) vma->vm_private_data;
  975. if (cursor > max_nl_cursor)
  976. max_nl_cursor = cursor;
  977. cursor = vma->vm_end - vma->vm_start;
  978. if (cursor > max_nl_size)
  979. max_nl_size = cursor;
  980. }
  981. if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
  982. ret = SWAP_FAIL;
  983. goto out;
  984. }
  985. /*
  986. * We don't try to search for this page in the nonlinear vmas,
  987. * and page_referenced wouldn't have found it anyway. Instead
  988. * just walk the nonlinear vmas trying to age and unmap some.
  989. * The mapcount of the page we came in with is irrelevant,
  990. * but even so use it as a guide to how hard we should try?
  991. */
  992. mapcount = page_mapcount(page);
  993. if (!mapcount)
  994. goto out;
  995. cond_resched_lock(&mapping->i_mmap_lock);
  996. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  997. if (max_nl_cursor == 0)
  998. max_nl_cursor = CLUSTER_SIZE;
  999. do {
  1000. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  1001. shared.vm_set.list) {
  1002. cursor = (unsigned long) vma->vm_private_data;
  1003. while ( cursor < max_nl_cursor &&
  1004. cursor < vma->vm_end - vma->vm_start) {
  1005. if (try_to_unmap_cluster(cursor, &mapcount,
  1006. vma, page) == SWAP_MLOCK)
  1007. ret = SWAP_MLOCK;
  1008. cursor += CLUSTER_SIZE;
  1009. vma->vm_private_data = (void *) cursor;
  1010. if ((int)mapcount <= 0)
  1011. goto out;
  1012. }
  1013. vma->vm_private_data = (void *) max_nl_cursor;
  1014. }
  1015. cond_resched_lock(&mapping->i_mmap_lock);
  1016. max_nl_cursor += CLUSTER_SIZE;
  1017. } while (max_nl_cursor <= max_nl_size);
  1018. /*
  1019. * Don't loop forever (perhaps all the remaining pages are
  1020. * in locked vmas). Reset cursor on all unreserved nonlinear
  1021. * vmas, now forgetting on which ones it had fallen behind.
  1022. */
  1023. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1024. vma->vm_private_data = NULL;
  1025. out:
  1026. spin_unlock(&mapping->i_mmap_lock);
  1027. return ret;
  1028. }
  1029. /**
  1030. * try_to_unmap - try to remove all page table mappings to a page
  1031. * @page: the page to get unmapped
  1032. * @flags: action and flags
  1033. *
  1034. * Tries to remove all the page table entries which are mapping this
  1035. * page, used in the pageout path. Caller must hold the page lock.
  1036. * Return values are:
  1037. *
  1038. * SWAP_SUCCESS - we succeeded in removing all mappings
  1039. * SWAP_AGAIN - we missed a mapping, try again later
  1040. * SWAP_FAIL - the page is unswappable
  1041. * SWAP_MLOCK - page is mlocked.
  1042. */
  1043. int try_to_unmap(struct page *page, enum ttu_flags flags)
  1044. {
  1045. int ret;
  1046. BUG_ON(!PageLocked(page));
  1047. if (unlikely(PageKsm(page)))
  1048. ret = try_to_unmap_ksm(page, flags);
  1049. else if (PageAnon(page))
  1050. ret = try_to_unmap_anon(page, flags);
  1051. else
  1052. ret = try_to_unmap_file(page, flags);
  1053. if (ret != SWAP_MLOCK && !page_mapped(page))
  1054. ret = SWAP_SUCCESS;
  1055. return ret;
  1056. }
  1057. /**
  1058. * try_to_munlock - try to munlock a page
  1059. * @page: the page to be munlocked
  1060. *
  1061. * Called from munlock code. Checks all of the VMAs mapping the page
  1062. * to make sure nobody else has this page mlocked. The page will be
  1063. * returned with PG_mlocked cleared if no other vmas have it mlocked.
  1064. *
  1065. * Return values are:
  1066. *
  1067. * SWAP_AGAIN - no vma is holding page mlocked, or,
  1068. * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
  1069. * SWAP_FAIL - page cannot be located at present
  1070. * SWAP_MLOCK - page is now mlocked.
  1071. */
  1072. int try_to_munlock(struct page *page)
  1073. {
  1074. VM_BUG_ON(!PageLocked(page) || PageLRU(page));
  1075. if (unlikely(PageKsm(page)))
  1076. return try_to_unmap_ksm(page, TTU_MUNLOCK);
  1077. else if (PageAnon(page))
  1078. return try_to_unmap_anon(page, TTU_MUNLOCK);
  1079. else
  1080. return try_to_unmap_file(page, TTU_MUNLOCK);
  1081. }
  1082. #ifdef CONFIG_MIGRATION
  1083. /*
  1084. * rmap_walk() and its helpers rmap_walk_anon() and rmap_walk_file():
  1085. * Called by migrate.c to remove migration ptes, but might be used more later.
  1086. */
  1087. static int rmap_walk_anon(struct page *page, int (*rmap_one)(struct page *,
  1088. struct vm_area_struct *, unsigned long, void *), void *arg)
  1089. {
  1090. struct anon_vma *anon_vma;
  1091. struct vm_area_struct *vma;
  1092. int ret = SWAP_AGAIN;
  1093. /*
  1094. * Note: remove_migration_ptes() cannot use page_lock_anon_vma()
  1095. * because that depends on page_mapped(); but not all its usages
  1096. * are holding mmap_sem, which also gave the necessary guarantee
  1097. * (that this anon_vma's slab has not already been destroyed).
  1098. * This needs to be reviewed later: avoiding page_lock_anon_vma()
  1099. * is risky, and currently limits the usefulness of rmap_walk().
  1100. */
  1101. anon_vma = page_anon_vma(page);
  1102. if (!anon_vma)
  1103. return ret;
  1104. spin_lock(&anon_vma->lock);
  1105. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  1106. unsigned long address = vma_address(page, vma);
  1107. if (address == -EFAULT)
  1108. continue;
  1109. ret = rmap_one(page, vma, address, arg);
  1110. if (ret != SWAP_AGAIN)
  1111. break;
  1112. }
  1113. spin_unlock(&anon_vma->lock);
  1114. return ret;
  1115. }
  1116. static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *,
  1117. struct vm_area_struct *, unsigned long, void *), void *arg)
  1118. {
  1119. struct address_space *mapping = page->mapping;
  1120. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  1121. struct vm_area_struct *vma;
  1122. struct prio_tree_iter iter;
  1123. int ret = SWAP_AGAIN;
  1124. if (!mapping)
  1125. return ret;
  1126. spin_lock(&mapping->i_mmap_lock);
  1127. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1128. unsigned long address = vma_address(page, vma);
  1129. if (address == -EFAULT)
  1130. continue;
  1131. ret = rmap_one(page, vma, address, arg);
  1132. if (ret != SWAP_AGAIN)
  1133. break;
  1134. }
  1135. /*
  1136. * No nonlinear handling: being always shared, nonlinear vmas
  1137. * never contain migration ptes. Decide what to do about this
  1138. * limitation to linear when we need rmap_walk() on nonlinear.
  1139. */
  1140. spin_unlock(&mapping->i_mmap_lock);
  1141. return ret;
  1142. }
  1143. int rmap_walk(struct page *page, int (*rmap_one)(struct page *,
  1144. struct vm_area_struct *, unsigned long, void *), void *arg)
  1145. {
  1146. VM_BUG_ON(!PageLocked(page));
  1147. if (unlikely(PageKsm(page)))
  1148. return rmap_walk_ksm(page, rmap_one, arg);
  1149. else if (PageAnon(page))
  1150. return rmap_walk_anon(page, rmap_one, arg);
  1151. else
  1152. return rmap_walk_file(page, rmap_one, arg);
  1153. }
  1154. #endif /* CONFIG_MIGRATION */