rmap.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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 <linux/hugetlb.h>
  58. #include <asm/tlbflush.h>
  59. #include "internal.h"
  60. static struct kmem_cache *anon_vma_cachep;
  61. static struct kmem_cache *anon_vma_chain_cachep;
  62. static inline struct anon_vma *anon_vma_alloc(void)
  63. {
  64. return kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
  65. }
  66. void anon_vma_free(struct anon_vma *anon_vma)
  67. {
  68. kmem_cache_free(anon_vma_cachep, anon_vma);
  69. }
  70. static inline struct anon_vma_chain *anon_vma_chain_alloc(void)
  71. {
  72. return kmem_cache_alloc(anon_vma_chain_cachep, GFP_KERNEL);
  73. }
  74. static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain)
  75. {
  76. kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
  77. }
  78. /**
  79. * anon_vma_prepare - attach an anon_vma to a memory region
  80. * @vma: the memory region in question
  81. *
  82. * This makes sure the memory mapping described by 'vma' has
  83. * an 'anon_vma' attached to it, so that we can associate the
  84. * anonymous pages mapped into it with that anon_vma.
  85. *
  86. * The common case will be that we already have one, but if
  87. * not we either need to find an adjacent mapping that we
  88. * can re-use the anon_vma from (very common when the only
  89. * reason for splitting a vma has been mprotect()), or we
  90. * allocate a new one.
  91. *
  92. * Anon-vma allocations are very subtle, because we may have
  93. * optimistically looked up an anon_vma in page_lock_anon_vma()
  94. * and that may actually touch the spinlock even in the newly
  95. * allocated vma (it depends on RCU to make sure that the
  96. * anon_vma isn't actually destroyed).
  97. *
  98. * As a result, we need to do proper anon_vma locking even
  99. * for the new allocation. At the same time, we do not want
  100. * to do any locking for the common case of already having
  101. * an anon_vma.
  102. *
  103. * This must be called with the mmap_sem held for reading.
  104. */
  105. int anon_vma_prepare(struct vm_area_struct *vma)
  106. {
  107. struct anon_vma *anon_vma = vma->anon_vma;
  108. struct anon_vma_chain *avc;
  109. might_sleep();
  110. if (unlikely(!anon_vma)) {
  111. struct mm_struct *mm = vma->vm_mm;
  112. struct anon_vma *allocated;
  113. avc = anon_vma_chain_alloc();
  114. if (!avc)
  115. goto out_enomem;
  116. anon_vma = find_mergeable_anon_vma(vma);
  117. allocated = NULL;
  118. if (!anon_vma) {
  119. anon_vma = anon_vma_alloc();
  120. if (unlikely(!anon_vma))
  121. goto out_enomem_free_avc;
  122. allocated = anon_vma;
  123. /*
  124. * This VMA had no anon_vma yet. This anon_vma is
  125. * the root of any anon_vma tree that might form.
  126. */
  127. anon_vma->root = anon_vma;
  128. }
  129. anon_vma_lock(anon_vma);
  130. /* page_table_lock to protect against threads */
  131. spin_lock(&mm->page_table_lock);
  132. if (likely(!vma->anon_vma)) {
  133. vma->anon_vma = anon_vma;
  134. avc->anon_vma = anon_vma;
  135. avc->vma = vma;
  136. list_add(&avc->same_vma, &vma->anon_vma_chain);
  137. list_add_tail(&avc->same_anon_vma, &anon_vma->head);
  138. allocated = NULL;
  139. avc = NULL;
  140. }
  141. spin_unlock(&mm->page_table_lock);
  142. anon_vma_unlock(anon_vma);
  143. if (unlikely(allocated))
  144. anon_vma_free(allocated);
  145. if (unlikely(avc))
  146. anon_vma_chain_free(avc);
  147. }
  148. return 0;
  149. out_enomem_free_avc:
  150. anon_vma_chain_free(avc);
  151. out_enomem:
  152. return -ENOMEM;
  153. }
  154. static void anon_vma_chain_link(struct vm_area_struct *vma,
  155. struct anon_vma_chain *avc,
  156. struct anon_vma *anon_vma)
  157. {
  158. avc->vma = vma;
  159. avc->anon_vma = anon_vma;
  160. list_add(&avc->same_vma, &vma->anon_vma_chain);
  161. anon_vma_lock(anon_vma);
  162. /*
  163. * It's critical to add new vmas to the tail of the anon_vma,
  164. * see comment in huge_memory.c:__split_huge_page().
  165. */
  166. list_add_tail(&avc->same_anon_vma, &anon_vma->head);
  167. anon_vma_unlock(anon_vma);
  168. }
  169. /*
  170. * Attach the anon_vmas from src to dst.
  171. * Returns 0 on success, -ENOMEM on failure.
  172. */
  173. int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
  174. {
  175. struct anon_vma_chain *avc, *pavc;
  176. list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
  177. avc = anon_vma_chain_alloc();
  178. if (!avc)
  179. goto enomem_failure;
  180. anon_vma_chain_link(dst, avc, pavc->anon_vma);
  181. }
  182. return 0;
  183. enomem_failure:
  184. unlink_anon_vmas(dst);
  185. return -ENOMEM;
  186. }
  187. /*
  188. * Attach vma to its own anon_vma, as well as to the anon_vmas that
  189. * the corresponding VMA in the parent process is attached to.
  190. * Returns 0 on success, non-zero on failure.
  191. */
  192. int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
  193. {
  194. struct anon_vma_chain *avc;
  195. struct anon_vma *anon_vma;
  196. /* Don't bother if the parent process has no anon_vma here. */
  197. if (!pvma->anon_vma)
  198. return 0;
  199. /*
  200. * First, attach the new VMA to the parent VMA's anon_vmas,
  201. * so rmap can find non-COWed pages in child processes.
  202. */
  203. if (anon_vma_clone(vma, pvma))
  204. return -ENOMEM;
  205. /* Then add our own anon_vma. */
  206. anon_vma = anon_vma_alloc();
  207. if (!anon_vma)
  208. goto out_error;
  209. avc = anon_vma_chain_alloc();
  210. if (!avc)
  211. goto out_error_free_anon_vma;
  212. /*
  213. * The root anon_vma's spinlock is the lock actually used when we
  214. * lock any of the anon_vmas in this anon_vma tree.
  215. */
  216. anon_vma->root = pvma->anon_vma->root;
  217. /*
  218. * With KSM refcounts, an anon_vma can stay around longer than the
  219. * process it belongs to. The root anon_vma needs to be pinned
  220. * until this anon_vma is freed, because the lock lives in the root.
  221. */
  222. get_anon_vma(anon_vma->root);
  223. /* Mark this anon_vma as the one where our new (COWed) pages go. */
  224. vma->anon_vma = anon_vma;
  225. anon_vma_chain_link(vma, avc, anon_vma);
  226. return 0;
  227. out_error_free_anon_vma:
  228. anon_vma_free(anon_vma);
  229. out_error:
  230. unlink_anon_vmas(vma);
  231. return -ENOMEM;
  232. }
  233. static void anon_vma_unlink(struct anon_vma_chain *anon_vma_chain)
  234. {
  235. struct anon_vma *anon_vma = anon_vma_chain->anon_vma;
  236. int empty;
  237. /* If anon_vma_fork fails, we can get an empty anon_vma_chain. */
  238. if (!anon_vma)
  239. return;
  240. anon_vma_lock(anon_vma);
  241. list_del(&anon_vma_chain->same_anon_vma);
  242. /* We must garbage collect the anon_vma if it's empty */
  243. empty = list_empty(&anon_vma->head) && !anonvma_external_refcount(anon_vma);
  244. anon_vma_unlock(anon_vma);
  245. if (empty) {
  246. /* We no longer need the root anon_vma */
  247. if (anon_vma->root != anon_vma)
  248. drop_anon_vma(anon_vma->root);
  249. anon_vma_free(anon_vma);
  250. }
  251. }
  252. void unlink_anon_vmas(struct vm_area_struct *vma)
  253. {
  254. struct anon_vma_chain *avc, *next;
  255. /*
  256. * Unlink each anon_vma chained to the VMA. This list is ordered
  257. * from newest to oldest, ensuring the root anon_vma gets freed last.
  258. */
  259. list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
  260. anon_vma_unlink(avc);
  261. list_del(&avc->same_vma);
  262. anon_vma_chain_free(avc);
  263. }
  264. }
  265. static void anon_vma_ctor(void *data)
  266. {
  267. struct anon_vma *anon_vma = data;
  268. spin_lock_init(&anon_vma->lock);
  269. anonvma_external_refcount_init(anon_vma);
  270. INIT_LIST_HEAD(&anon_vma->head);
  271. }
  272. void __init anon_vma_init(void)
  273. {
  274. anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
  275. 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
  276. anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, SLAB_PANIC);
  277. }
  278. /*
  279. * Getting a lock on a stable anon_vma from a page off the LRU is
  280. * tricky: page_lock_anon_vma rely on RCU to guard against the races.
  281. */
  282. struct anon_vma *__page_lock_anon_vma(struct page *page)
  283. {
  284. struct anon_vma *anon_vma, *root_anon_vma;
  285. unsigned long anon_mapping;
  286. rcu_read_lock();
  287. anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping);
  288. if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
  289. goto out;
  290. if (!page_mapped(page))
  291. goto out;
  292. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  293. root_anon_vma = ACCESS_ONCE(anon_vma->root);
  294. spin_lock(&root_anon_vma->lock);
  295. /*
  296. * If this page is still mapped, then its anon_vma cannot have been
  297. * freed. But if it has been unmapped, we have no security against
  298. * the anon_vma structure being freed and reused (for another anon_vma:
  299. * SLAB_DESTROY_BY_RCU guarantees that - so the spin_lock above cannot
  300. * corrupt): with anon_vma_prepare() or anon_vma_fork() redirecting
  301. * anon_vma->root before page_unlock_anon_vma() is called to unlock.
  302. */
  303. if (page_mapped(page))
  304. return anon_vma;
  305. spin_unlock(&root_anon_vma->lock);
  306. out:
  307. rcu_read_unlock();
  308. return NULL;
  309. }
  310. void page_unlock_anon_vma(struct anon_vma *anon_vma)
  311. __releases(&anon_vma->root->lock)
  312. __releases(RCU)
  313. {
  314. anon_vma_unlock(anon_vma);
  315. rcu_read_unlock();
  316. }
  317. /*
  318. * At what user virtual address is page expected in @vma?
  319. * Returns virtual address or -EFAULT if page's index/offset is not
  320. * within the range mapped the @vma.
  321. */
  322. inline unsigned long
  323. vma_address(struct page *page, struct vm_area_struct *vma)
  324. {
  325. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  326. unsigned long address;
  327. if (unlikely(is_vm_hugetlb_page(vma)))
  328. pgoff = page->index << huge_page_order(page_hstate(page));
  329. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  330. if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
  331. /* page should be within @vma mapping range */
  332. return -EFAULT;
  333. }
  334. return address;
  335. }
  336. /*
  337. * At what user virtual address is page expected in vma?
  338. * Caller should check the page is actually part of the vma.
  339. */
  340. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  341. {
  342. if (PageAnon(page)) {
  343. struct anon_vma *page__anon_vma = page_anon_vma(page);
  344. /*
  345. * Note: swapoff's unuse_vma() is more efficient with this
  346. * check, and needs it to match anon_vma when KSM is active.
  347. */
  348. if (!vma->anon_vma || !page__anon_vma ||
  349. vma->anon_vma->root != page__anon_vma->root)
  350. return -EFAULT;
  351. } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
  352. if (!vma->vm_file ||
  353. vma->vm_file->f_mapping != page->mapping)
  354. return -EFAULT;
  355. } else
  356. return -EFAULT;
  357. return vma_address(page, vma);
  358. }
  359. /*
  360. * Check that @page is mapped at @address into @mm.
  361. *
  362. * If @sync is false, page_check_address may perform a racy check to avoid
  363. * the page table lock when the pte is not present (helpful when reclaiming
  364. * highly shared pages).
  365. *
  366. * On success returns with pte mapped and locked.
  367. */
  368. pte_t *__page_check_address(struct page *page, struct mm_struct *mm,
  369. unsigned long address, spinlock_t **ptlp, int sync)
  370. {
  371. pgd_t *pgd;
  372. pud_t *pud;
  373. pmd_t *pmd;
  374. pte_t *pte;
  375. spinlock_t *ptl;
  376. if (unlikely(PageHuge(page))) {
  377. pte = huge_pte_offset(mm, address);
  378. ptl = &mm->page_table_lock;
  379. goto check;
  380. }
  381. pgd = pgd_offset(mm, address);
  382. if (!pgd_present(*pgd))
  383. return NULL;
  384. pud = pud_offset(pgd, address);
  385. if (!pud_present(*pud))
  386. return NULL;
  387. pmd = pmd_offset(pud, address);
  388. if (!pmd_present(*pmd))
  389. return NULL;
  390. if (pmd_trans_huge(*pmd))
  391. return NULL;
  392. pte = pte_offset_map(pmd, address);
  393. /* Make a quick check before getting the lock */
  394. if (!sync && !pte_present(*pte)) {
  395. pte_unmap(pte);
  396. return NULL;
  397. }
  398. ptl = pte_lockptr(mm, pmd);
  399. check:
  400. spin_lock(ptl);
  401. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  402. *ptlp = ptl;
  403. return pte;
  404. }
  405. pte_unmap_unlock(pte, ptl);
  406. return NULL;
  407. }
  408. /**
  409. * page_mapped_in_vma - check whether a page is really mapped in a VMA
  410. * @page: the page to test
  411. * @vma: the VMA to test
  412. *
  413. * Returns 1 if the page is mapped into the page tables of the VMA, 0
  414. * if the page is not mapped into the page tables of this VMA. Only
  415. * valid for normal file or anonymous VMAs.
  416. */
  417. int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
  418. {
  419. unsigned long address;
  420. pte_t *pte;
  421. spinlock_t *ptl;
  422. address = vma_address(page, vma);
  423. if (address == -EFAULT) /* out of vma range */
  424. return 0;
  425. pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
  426. if (!pte) /* the page is not in this mm */
  427. return 0;
  428. pte_unmap_unlock(pte, ptl);
  429. return 1;
  430. }
  431. /*
  432. * Subfunctions of page_referenced: page_referenced_one called
  433. * repeatedly from either page_referenced_anon or page_referenced_file.
  434. */
  435. int page_referenced_one(struct page *page, struct vm_area_struct *vma,
  436. unsigned long address, unsigned int *mapcount,
  437. unsigned long *vm_flags)
  438. {
  439. struct mm_struct *mm = vma->vm_mm;
  440. int referenced = 0;
  441. /*
  442. * Don't want to elevate referenced for mlocked page that gets this far,
  443. * in order that it progresses to try_to_unmap and is moved to the
  444. * unevictable list.
  445. */
  446. if (vma->vm_flags & VM_LOCKED) {
  447. *mapcount = 0; /* break early from loop */
  448. *vm_flags |= VM_LOCKED;
  449. goto out;
  450. }
  451. /* Pretend the page is referenced if the task has the
  452. swap token and is in the middle of a page fault. */
  453. if (mm != current->mm && has_swap_token(mm) &&
  454. rwsem_is_locked(&mm->mmap_sem))
  455. referenced++;
  456. if (unlikely(PageTransHuge(page))) {
  457. pmd_t *pmd;
  458. spin_lock(&mm->page_table_lock);
  459. pmd = page_check_address_pmd(page, mm, address,
  460. PAGE_CHECK_ADDRESS_PMD_FLAG);
  461. if (pmd && !pmd_trans_splitting(*pmd) &&
  462. pmdp_clear_flush_young_notify(vma, address, pmd))
  463. referenced++;
  464. spin_unlock(&mm->page_table_lock);
  465. } else {
  466. pte_t *pte;
  467. spinlock_t *ptl;
  468. pte = page_check_address(page, mm, address, &ptl, 0);
  469. if (!pte)
  470. goto out;
  471. if (ptep_clear_flush_young_notify(vma, address, pte)) {
  472. /*
  473. * Don't treat a reference through a sequentially read
  474. * mapping as such. If the page has been used in
  475. * another mapping, we will catch it; if this other
  476. * mapping is already gone, the unmap path will have
  477. * set PG_referenced or activated the page.
  478. */
  479. if (likely(!VM_SequentialReadHint(vma)))
  480. referenced++;
  481. }
  482. pte_unmap_unlock(pte, ptl);
  483. }
  484. (*mapcount)--;
  485. if (referenced)
  486. *vm_flags |= vma->vm_flags;
  487. out:
  488. return referenced;
  489. }
  490. static int page_referenced_anon(struct page *page,
  491. struct mem_cgroup *mem_cont,
  492. unsigned long *vm_flags)
  493. {
  494. unsigned int mapcount;
  495. struct anon_vma *anon_vma;
  496. struct anon_vma_chain *avc;
  497. int referenced = 0;
  498. anon_vma = page_lock_anon_vma(page);
  499. if (!anon_vma)
  500. return referenced;
  501. mapcount = page_mapcount(page);
  502. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  503. struct vm_area_struct *vma = avc->vma;
  504. unsigned long address = vma_address(page, vma);
  505. if (address == -EFAULT)
  506. continue;
  507. /*
  508. * If we are reclaiming on behalf of a cgroup, skip
  509. * counting on behalf of references from different
  510. * cgroups
  511. */
  512. if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
  513. continue;
  514. referenced += page_referenced_one(page, vma, address,
  515. &mapcount, vm_flags);
  516. if (!mapcount)
  517. break;
  518. }
  519. page_unlock_anon_vma(anon_vma);
  520. return referenced;
  521. }
  522. /**
  523. * page_referenced_file - referenced check for object-based rmap
  524. * @page: the page we're checking references on.
  525. * @mem_cont: target memory controller
  526. * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
  527. *
  528. * For an object-based mapped page, find all the places it is mapped and
  529. * check/clear the referenced flag. This is done by following the page->mapping
  530. * pointer, then walking the chain of vmas it holds. It returns the number
  531. * of references it found.
  532. *
  533. * This function is only called from page_referenced for object-based pages.
  534. */
  535. static int page_referenced_file(struct page *page,
  536. struct mem_cgroup *mem_cont,
  537. unsigned long *vm_flags)
  538. {
  539. unsigned int mapcount;
  540. struct address_space *mapping = page->mapping;
  541. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  542. struct vm_area_struct *vma;
  543. struct prio_tree_iter iter;
  544. int referenced = 0;
  545. /*
  546. * The caller's checks on page->mapping and !PageAnon have made
  547. * sure that this is a file page: the check for page->mapping
  548. * excludes the case just before it gets set on an anon page.
  549. */
  550. BUG_ON(PageAnon(page));
  551. /*
  552. * The page lock not only makes sure that page->mapping cannot
  553. * suddenly be NULLified by truncation, it makes sure that the
  554. * structure at mapping cannot be freed and reused yet,
  555. * so we can safely take mapping->i_mmap_lock.
  556. */
  557. BUG_ON(!PageLocked(page));
  558. spin_lock(&mapping->i_mmap_lock);
  559. /*
  560. * i_mmap_lock does not stabilize mapcount at all, but mapcount
  561. * is more likely to be accurate if we note it after spinning.
  562. */
  563. mapcount = page_mapcount(page);
  564. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  565. unsigned long address = vma_address(page, vma);
  566. if (address == -EFAULT)
  567. continue;
  568. /*
  569. * If we are reclaiming on behalf of a cgroup, skip
  570. * counting on behalf of references from different
  571. * cgroups
  572. */
  573. if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
  574. continue;
  575. referenced += page_referenced_one(page, vma, address,
  576. &mapcount, vm_flags);
  577. if (!mapcount)
  578. break;
  579. }
  580. spin_unlock(&mapping->i_mmap_lock);
  581. return referenced;
  582. }
  583. /**
  584. * page_referenced - test if the page was referenced
  585. * @page: the page to test
  586. * @is_locked: caller holds lock on the page
  587. * @mem_cont: target memory controller
  588. * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
  589. *
  590. * Quick test_and_clear_referenced for all mappings to a page,
  591. * returns the number of ptes which referenced the page.
  592. */
  593. int page_referenced(struct page *page,
  594. int is_locked,
  595. struct mem_cgroup *mem_cont,
  596. unsigned long *vm_flags)
  597. {
  598. int referenced = 0;
  599. int we_locked = 0;
  600. *vm_flags = 0;
  601. if (page_mapped(page) && page_rmapping(page)) {
  602. if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
  603. we_locked = trylock_page(page);
  604. if (!we_locked) {
  605. referenced++;
  606. goto out;
  607. }
  608. }
  609. if (unlikely(PageKsm(page)))
  610. referenced += page_referenced_ksm(page, mem_cont,
  611. vm_flags);
  612. else if (PageAnon(page))
  613. referenced += page_referenced_anon(page, mem_cont,
  614. vm_flags);
  615. else if (page->mapping)
  616. referenced += page_referenced_file(page, mem_cont,
  617. vm_flags);
  618. if (we_locked)
  619. unlock_page(page);
  620. }
  621. out:
  622. if (page_test_and_clear_young(page))
  623. referenced++;
  624. return referenced;
  625. }
  626. static int page_mkclean_one(struct page *page, struct vm_area_struct *vma,
  627. unsigned long address)
  628. {
  629. struct mm_struct *mm = vma->vm_mm;
  630. pte_t *pte;
  631. spinlock_t *ptl;
  632. int ret = 0;
  633. pte = page_check_address(page, mm, address, &ptl, 1);
  634. if (!pte)
  635. goto out;
  636. if (pte_dirty(*pte) || pte_write(*pte)) {
  637. pte_t entry;
  638. flush_cache_page(vma, address, pte_pfn(*pte));
  639. entry = ptep_clear_flush_notify(vma, address, pte);
  640. entry = pte_wrprotect(entry);
  641. entry = pte_mkclean(entry);
  642. set_pte_at(mm, address, pte, entry);
  643. ret = 1;
  644. }
  645. pte_unmap_unlock(pte, ptl);
  646. out:
  647. return ret;
  648. }
  649. static int page_mkclean_file(struct address_space *mapping, struct page *page)
  650. {
  651. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  652. struct vm_area_struct *vma;
  653. struct prio_tree_iter iter;
  654. int ret = 0;
  655. BUG_ON(PageAnon(page));
  656. spin_lock(&mapping->i_mmap_lock);
  657. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  658. if (vma->vm_flags & VM_SHARED) {
  659. unsigned long address = vma_address(page, vma);
  660. if (address == -EFAULT)
  661. continue;
  662. ret += page_mkclean_one(page, vma, address);
  663. }
  664. }
  665. spin_unlock(&mapping->i_mmap_lock);
  666. return ret;
  667. }
  668. int page_mkclean(struct page *page)
  669. {
  670. int ret = 0;
  671. BUG_ON(!PageLocked(page));
  672. if (page_mapped(page)) {
  673. struct address_space *mapping = page_mapping(page);
  674. if (mapping) {
  675. ret = page_mkclean_file(mapping, page);
  676. if (page_test_dirty(page)) {
  677. page_clear_dirty(page, 1);
  678. ret = 1;
  679. }
  680. }
  681. }
  682. return ret;
  683. }
  684. EXPORT_SYMBOL_GPL(page_mkclean);
  685. /**
  686. * page_move_anon_rmap - move a page to our anon_vma
  687. * @page: the page to move to our anon_vma
  688. * @vma: the vma the page belongs to
  689. * @address: the user virtual address mapped
  690. *
  691. * When a page belongs exclusively to one process after a COW event,
  692. * that page can be moved into the anon_vma that belongs to just that
  693. * process, so the rmap code will not search the parent or sibling
  694. * processes.
  695. */
  696. void page_move_anon_rmap(struct page *page,
  697. struct vm_area_struct *vma, unsigned long address)
  698. {
  699. struct anon_vma *anon_vma = vma->anon_vma;
  700. VM_BUG_ON(!PageLocked(page));
  701. VM_BUG_ON(!anon_vma);
  702. VM_BUG_ON(page->index != linear_page_index(vma, address));
  703. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  704. page->mapping = (struct address_space *) anon_vma;
  705. }
  706. /**
  707. * __page_set_anon_rmap - set up new anonymous rmap
  708. * @page: Page to add to rmap
  709. * @vma: VM area to add page to.
  710. * @address: User virtual address of the mapping
  711. * @exclusive: the page is exclusively owned by the current process
  712. */
  713. static void __page_set_anon_rmap(struct page *page,
  714. struct vm_area_struct *vma, unsigned long address, int exclusive)
  715. {
  716. struct anon_vma *anon_vma = vma->anon_vma;
  717. BUG_ON(!anon_vma);
  718. if (PageAnon(page))
  719. return;
  720. /*
  721. * If the page isn't exclusively mapped into this vma,
  722. * we must use the _oldest_ possible anon_vma for the
  723. * page mapping!
  724. */
  725. if (!exclusive)
  726. anon_vma = anon_vma->root;
  727. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  728. page->mapping = (struct address_space *) anon_vma;
  729. page->index = linear_page_index(vma, address);
  730. }
  731. /**
  732. * __page_check_anon_rmap - sanity check anonymous rmap addition
  733. * @page: the page to add the mapping to
  734. * @vma: the vm area in which the mapping is added
  735. * @address: the user virtual address mapped
  736. */
  737. static void __page_check_anon_rmap(struct page *page,
  738. struct vm_area_struct *vma, unsigned long address)
  739. {
  740. #ifdef CONFIG_DEBUG_VM
  741. /*
  742. * The page's anon-rmap details (mapping and index) are guaranteed to
  743. * be set up correctly at this point.
  744. *
  745. * We have exclusion against page_add_anon_rmap because the caller
  746. * always holds the page locked, except if called from page_dup_rmap,
  747. * in which case the page is already known to be setup.
  748. *
  749. * We have exclusion against page_add_new_anon_rmap because those pages
  750. * are initially only visible via the pagetables, and the pte is locked
  751. * over the call to page_add_new_anon_rmap.
  752. */
  753. BUG_ON(page_anon_vma(page)->root != vma->anon_vma->root);
  754. BUG_ON(page->index != linear_page_index(vma, address));
  755. #endif
  756. }
  757. /**
  758. * page_add_anon_rmap - add pte mapping to an anonymous page
  759. * @page: the page to add the mapping to
  760. * @vma: the vm area in which the mapping is added
  761. * @address: the user virtual address mapped
  762. *
  763. * The caller needs to hold the pte lock, and the page must be locked in
  764. * the anon_vma case: to serialize mapping,index checking after setting,
  765. * and to ensure that PageAnon is not being upgraded racily to PageKsm
  766. * (but PageKsm is never downgraded to PageAnon).
  767. */
  768. void page_add_anon_rmap(struct page *page,
  769. struct vm_area_struct *vma, unsigned long address)
  770. {
  771. do_page_add_anon_rmap(page, vma, address, 0);
  772. }
  773. /*
  774. * Special version of the above for do_swap_page, which often runs
  775. * into pages that are exclusively owned by the current process.
  776. * Everybody else should continue to use page_add_anon_rmap above.
  777. */
  778. void do_page_add_anon_rmap(struct page *page,
  779. struct vm_area_struct *vma, unsigned long address, int exclusive)
  780. {
  781. int first = atomic_inc_and_test(&page->_mapcount);
  782. if (first) {
  783. if (!PageTransHuge(page))
  784. __inc_zone_page_state(page, NR_ANON_PAGES);
  785. else
  786. __inc_zone_page_state(page,
  787. NR_ANON_TRANSPARENT_HUGEPAGES);
  788. }
  789. if (unlikely(PageKsm(page)))
  790. return;
  791. VM_BUG_ON(!PageLocked(page));
  792. VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  793. if (first)
  794. __page_set_anon_rmap(page, vma, address, exclusive);
  795. else
  796. __page_check_anon_rmap(page, vma, address);
  797. }
  798. /**
  799. * page_add_new_anon_rmap - add pte mapping to a new anonymous page
  800. * @page: the page to add the mapping to
  801. * @vma: the vm area in which the mapping is added
  802. * @address: the user virtual address mapped
  803. *
  804. * Same as page_add_anon_rmap but must only be called on *new* pages.
  805. * This means the inc-and-test can be bypassed.
  806. * Page does not have to be locked.
  807. */
  808. void page_add_new_anon_rmap(struct page *page,
  809. struct vm_area_struct *vma, unsigned long address)
  810. {
  811. VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  812. SetPageSwapBacked(page);
  813. atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
  814. if (!PageTransHuge(page))
  815. __inc_zone_page_state(page, NR_ANON_PAGES);
  816. else
  817. __inc_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
  818. __page_set_anon_rmap(page, vma, address, 1);
  819. if (page_evictable(page, vma))
  820. lru_cache_add_lru(page, LRU_ACTIVE_ANON);
  821. else
  822. add_page_to_unevictable_list(page);
  823. }
  824. /**
  825. * page_add_file_rmap - add pte mapping to a file page
  826. * @page: the page to add the mapping to
  827. *
  828. * The caller needs to hold the pte lock.
  829. */
  830. void page_add_file_rmap(struct page *page)
  831. {
  832. if (atomic_inc_and_test(&page->_mapcount)) {
  833. __inc_zone_page_state(page, NR_FILE_MAPPED);
  834. mem_cgroup_inc_page_stat(page, MEMCG_NR_FILE_MAPPED);
  835. }
  836. }
  837. /**
  838. * page_remove_rmap - take down pte mapping from a page
  839. * @page: page to remove mapping from
  840. *
  841. * The caller needs to hold the pte lock.
  842. */
  843. void page_remove_rmap(struct page *page)
  844. {
  845. /* page still mapped by someone else? */
  846. if (!atomic_add_negative(-1, &page->_mapcount))
  847. return;
  848. /*
  849. * Now that the last pte has gone, s390 must transfer dirty
  850. * flag from storage key to struct page. We can usually skip
  851. * this if the page is anon, so about to be freed; but perhaps
  852. * not if it's in swapcache - there might be another pte slot
  853. * containing the swap entry, but page not yet written to swap.
  854. */
  855. if ((!PageAnon(page) || PageSwapCache(page)) && page_test_dirty(page)) {
  856. page_clear_dirty(page, 1);
  857. set_page_dirty(page);
  858. }
  859. /*
  860. * Hugepages are not counted in NR_ANON_PAGES nor NR_FILE_MAPPED
  861. * and not charged by memcg for now.
  862. */
  863. if (unlikely(PageHuge(page)))
  864. return;
  865. if (PageAnon(page)) {
  866. mem_cgroup_uncharge_page(page);
  867. if (!PageTransHuge(page))
  868. __dec_zone_page_state(page, NR_ANON_PAGES);
  869. else
  870. __dec_zone_page_state(page,
  871. NR_ANON_TRANSPARENT_HUGEPAGES);
  872. } else {
  873. __dec_zone_page_state(page, NR_FILE_MAPPED);
  874. mem_cgroup_dec_page_stat(page, MEMCG_NR_FILE_MAPPED);
  875. }
  876. /*
  877. * It would be tidy to reset the PageAnon mapping here,
  878. * but that might overwrite a racing page_add_anon_rmap
  879. * which increments mapcount after us but sets mapping
  880. * before us: so leave the reset to free_hot_cold_page,
  881. * and remember that it's only reliable while mapped.
  882. * Leaving it set also helps swapoff to reinstate ptes
  883. * faster for those pages still in swapcache.
  884. */
  885. }
  886. /*
  887. * Subfunctions of try_to_unmap: try_to_unmap_one called
  888. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  889. */
  890. int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  891. unsigned long address, enum ttu_flags flags)
  892. {
  893. struct mm_struct *mm = vma->vm_mm;
  894. pte_t *pte;
  895. pte_t pteval;
  896. spinlock_t *ptl;
  897. int ret = SWAP_AGAIN;
  898. pte = page_check_address(page, mm, address, &ptl, 0);
  899. if (!pte)
  900. goto out;
  901. /*
  902. * If the page is mlock()d, we cannot swap it out.
  903. * If it's recently referenced (perhaps page_referenced
  904. * skipped over this mm) then we should reactivate it.
  905. */
  906. if (!(flags & TTU_IGNORE_MLOCK)) {
  907. if (vma->vm_flags & VM_LOCKED)
  908. goto out_mlock;
  909. if (TTU_ACTION(flags) == TTU_MUNLOCK)
  910. goto out_unmap;
  911. }
  912. if (!(flags & TTU_IGNORE_ACCESS)) {
  913. if (ptep_clear_flush_young_notify(vma, address, pte)) {
  914. ret = SWAP_FAIL;
  915. goto out_unmap;
  916. }
  917. }
  918. /* Nuke the page table entry. */
  919. flush_cache_page(vma, address, page_to_pfn(page));
  920. pteval = ptep_clear_flush_notify(vma, address, pte);
  921. /* Move the dirty bit to the physical page now the pte is gone. */
  922. if (pte_dirty(pteval))
  923. set_page_dirty(page);
  924. /* Update high watermark before we lower rss */
  925. update_hiwater_rss(mm);
  926. if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
  927. if (PageAnon(page))
  928. dec_mm_counter(mm, MM_ANONPAGES);
  929. else
  930. dec_mm_counter(mm, MM_FILEPAGES);
  931. set_pte_at(mm, address, pte,
  932. swp_entry_to_pte(make_hwpoison_entry(page)));
  933. } else if (PageAnon(page)) {
  934. swp_entry_t entry = { .val = page_private(page) };
  935. if (PageSwapCache(page)) {
  936. /*
  937. * Store the swap location in the pte.
  938. * See handle_pte_fault() ...
  939. */
  940. if (swap_duplicate(entry) < 0) {
  941. set_pte_at(mm, address, pte, pteval);
  942. ret = SWAP_FAIL;
  943. goto out_unmap;
  944. }
  945. if (list_empty(&mm->mmlist)) {
  946. spin_lock(&mmlist_lock);
  947. if (list_empty(&mm->mmlist))
  948. list_add(&mm->mmlist, &init_mm.mmlist);
  949. spin_unlock(&mmlist_lock);
  950. }
  951. dec_mm_counter(mm, MM_ANONPAGES);
  952. inc_mm_counter(mm, MM_SWAPENTS);
  953. } else if (PAGE_MIGRATION) {
  954. /*
  955. * Store the pfn of the page in a special migration
  956. * pte. do_swap_page() will wait until the migration
  957. * pte is removed and then restart fault handling.
  958. */
  959. BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
  960. entry = make_migration_entry(page, pte_write(pteval));
  961. }
  962. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  963. BUG_ON(pte_file(*pte));
  964. } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
  965. /* Establish migration entry for a file page */
  966. swp_entry_t entry;
  967. entry = make_migration_entry(page, pte_write(pteval));
  968. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  969. } else
  970. dec_mm_counter(mm, MM_FILEPAGES);
  971. page_remove_rmap(page);
  972. page_cache_release(page);
  973. out_unmap:
  974. pte_unmap_unlock(pte, ptl);
  975. out:
  976. return ret;
  977. out_mlock:
  978. pte_unmap_unlock(pte, ptl);
  979. /*
  980. * We need mmap_sem locking, Otherwise VM_LOCKED check makes
  981. * unstable result and race. Plus, We can't wait here because
  982. * we now hold anon_vma->lock or mapping->i_mmap_lock.
  983. * if trylock failed, the page remain in evictable lru and later
  984. * vmscan could retry to move the page to unevictable lru if the
  985. * page is actually mlocked.
  986. */
  987. if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
  988. if (vma->vm_flags & VM_LOCKED) {
  989. mlock_vma_page(page);
  990. ret = SWAP_MLOCK;
  991. }
  992. up_read(&vma->vm_mm->mmap_sem);
  993. }
  994. return ret;
  995. }
  996. /*
  997. * objrmap doesn't work for nonlinear VMAs because the assumption that
  998. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  999. * Consequently, given a particular page and its ->index, we cannot locate the
  1000. * ptes which are mapping that page without an exhaustive linear search.
  1001. *
  1002. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  1003. * maps the file to which the target page belongs. The ->vm_private_data field
  1004. * holds the current cursor into that scan. Successive searches will circulate
  1005. * around the vma's virtual address space.
  1006. *
  1007. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  1008. * more scanning pressure is placed against them as well. Eventually pages
  1009. * will become fully unmapped and are eligible for eviction.
  1010. *
  1011. * For very sparsely populated VMAs this is a little inefficient - chances are
  1012. * there there won't be many ptes located within the scan cluster. In this case
  1013. * maybe we could scan further - to the end of the pte page, perhaps.
  1014. *
  1015. * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
  1016. * acquire it without blocking. If vma locked, mlock the pages in the cluster,
  1017. * rather than unmapping them. If we encounter the "check_page" that vmscan is
  1018. * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
  1019. */
  1020. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  1021. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  1022. static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
  1023. struct vm_area_struct *vma, struct page *check_page)
  1024. {
  1025. struct mm_struct *mm = vma->vm_mm;
  1026. pgd_t *pgd;
  1027. pud_t *pud;
  1028. pmd_t *pmd;
  1029. pte_t *pte;
  1030. pte_t pteval;
  1031. spinlock_t *ptl;
  1032. struct page *page;
  1033. unsigned long address;
  1034. unsigned long end;
  1035. int ret = SWAP_AGAIN;
  1036. int locked_vma = 0;
  1037. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  1038. end = address + CLUSTER_SIZE;
  1039. if (address < vma->vm_start)
  1040. address = vma->vm_start;
  1041. if (end > vma->vm_end)
  1042. end = vma->vm_end;
  1043. pgd = pgd_offset(mm, address);
  1044. if (!pgd_present(*pgd))
  1045. return ret;
  1046. pud = pud_offset(pgd, address);
  1047. if (!pud_present(*pud))
  1048. return ret;
  1049. pmd = pmd_offset(pud, address);
  1050. if (!pmd_present(*pmd))
  1051. return ret;
  1052. /*
  1053. * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
  1054. * keep the sem while scanning the cluster for mlocking pages.
  1055. */
  1056. if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
  1057. locked_vma = (vma->vm_flags & VM_LOCKED);
  1058. if (!locked_vma)
  1059. up_read(&vma->vm_mm->mmap_sem); /* don't need it */
  1060. }
  1061. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  1062. /* Update high watermark before we lower rss */
  1063. update_hiwater_rss(mm);
  1064. for (; address < end; pte++, address += PAGE_SIZE) {
  1065. if (!pte_present(*pte))
  1066. continue;
  1067. page = vm_normal_page(vma, address, *pte);
  1068. BUG_ON(!page || PageAnon(page));
  1069. if (locked_vma) {
  1070. mlock_vma_page(page); /* no-op if already mlocked */
  1071. if (page == check_page)
  1072. ret = SWAP_MLOCK;
  1073. continue; /* don't unmap */
  1074. }
  1075. if (ptep_clear_flush_young_notify(vma, address, pte))
  1076. continue;
  1077. /* Nuke the page table entry. */
  1078. flush_cache_page(vma, address, pte_pfn(*pte));
  1079. pteval = ptep_clear_flush_notify(vma, address, pte);
  1080. /* If nonlinear, store the file page offset in the pte. */
  1081. if (page->index != linear_page_index(vma, address))
  1082. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  1083. /* Move the dirty bit to the physical page now the pte is gone. */
  1084. if (pte_dirty(pteval))
  1085. set_page_dirty(page);
  1086. page_remove_rmap(page);
  1087. page_cache_release(page);
  1088. dec_mm_counter(mm, MM_FILEPAGES);
  1089. (*mapcount)--;
  1090. }
  1091. pte_unmap_unlock(pte - 1, ptl);
  1092. if (locked_vma)
  1093. up_read(&vma->vm_mm->mmap_sem);
  1094. return ret;
  1095. }
  1096. bool is_vma_temporary_stack(struct vm_area_struct *vma)
  1097. {
  1098. int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
  1099. if (!maybe_stack)
  1100. return false;
  1101. if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
  1102. VM_STACK_INCOMPLETE_SETUP)
  1103. return true;
  1104. return false;
  1105. }
  1106. /**
  1107. * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
  1108. * rmap method
  1109. * @page: the page to unmap/unlock
  1110. * @flags: action and flags
  1111. *
  1112. * Find all the mappings of a page using the mapping pointer and the vma chains
  1113. * contained in the anon_vma struct it points to.
  1114. *
  1115. * This function is only called from try_to_unmap/try_to_munlock for
  1116. * anonymous pages.
  1117. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  1118. * where the page was found will be held for write. So, we won't recheck
  1119. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  1120. * 'LOCKED.
  1121. */
  1122. static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
  1123. {
  1124. struct anon_vma *anon_vma;
  1125. struct anon_vma_chain *avc;
  1126. int ret = SWAP_AGAIN;
  1127. anon_vma = page_lock_anon_vma(page);
  1128. if (!anon_vma)
  1129. return ret;
  1130. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  1131. struct vm_area_struct *vma = avc->vma;
  1132. unsigned long address;
  1133. /*
  1134. * During exec, a temporary VMA is setup and later moved.
  1135. * The VMA is moved under the anon_vma lock but not the
  1136. * page tables leading to a race where migration cannot
  1137. * find the migration ptes. Rather than increasing the
  1138. * locking requirements of exec(), migration skips
  1139. * temporary VMAs until after exec() completes.
  1140. */
  1141. if (PAGE_MIGRATION && (flags & TTU_MIGRATION) &&
  1142. is_vma_temporary_stack(vma))
  1143. continue;
  1144. address = vma_address(page, vma);
  1145. if (address == -EFAULT)
  1146. continue;
  1147. ret = try_to_unmap_one(page, vma, address, flags);
  1148. if (ret != SWAP_AGAIN || !page_mapped(page))
  1149. break;
  1150. }
  1151. page_unlock_anon_vma(anon_vma);
  1152. return ret;
  1153. }
  1154. /**
  1155. * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
  1156. * @page: the page to unmap/unlock
  1157. * @flags: action and flags
  1158. *
  1159. * Find all the mappings of a page using the mapping pointer and the vma chains
  1160. * contained in the address_space struct it points to.
  1161. *
  1162. * This function is only called from try_to_unmap/try_to_munlock for
  1163. * object-based pages.
  1164. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  1165. * where the page was found will be held for write. So, we won't recheck
  1166. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  1167. * 'LOCKED.
  1168. */
  1169. static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
  1170. {
  1171. struct address_space *mapping = page->mapping;
  1172. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  1173. struct vm_area_struct *vma;
  1174. struct prio_tree_iter iter;
  1175. int ret = SWAP_AGAIN;
  1176. unsigned long cursor;
  1177. unsigned long max_nl_cursor = 0;
  1178. unsigned long max_nl_size = 0;
  1179. unsigned int mapcount;
  1180. spin_lock(&mapping->i_mmap_lock);
  1181. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1182. unsigned long address = vma_address(page, vma);
  1183. if (address == -EFAULT)
  1184. continue;
  1185. ret = try_to_unmap_one(page, vma, address, flags);
  1186. if (ret != SWAP_AGAIN || !page_mapped(page))
  1187. goto out;
  1188. }
  1189. if (list_empty(&mapping->i_mmap_nonlinear))
  1190. goto out;
  1191. /*
  1192. * We don't bother to try to find the munlocked page in nonlinears.
  1193. * It's costly. Instead, later, page reclaim logic may call
  1194. * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily.
  1195. */
  1196. if (TTU_ACTION(flags) == TTU_MUNLOCK)
  1197. goto out;
  1198. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  1199. shared.vm_set.list) {
  1200. cursor = (unsigned long) vma->vm_private_data;
  1201. if (cursor > max_nl_cursor)
  1202. max_nl_cursor = cursor;
  1203. cursor = vma->vm_end - vma->vm_start;
  1204. if (cursor > max_nl_size)
  1205. max_nl_size = cursor;
  1206. }
  1207. if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
  1208. ret = SWAP_FAIL;
  1209. goto out;
  1210. }
  1211. /*
  1212. * We don't try to search for this page in the nonlinear vmas,
  1213. * and page_referenced wouldn't have found it anyway. Instead
  1214. * just walk the nonlinear vmas trying to age and unmap some.
  1215. * The mapcount of the page we came in with is irrelevant,
  1216. * but even so use it as a guide to how hard we should try?
  1217. */
  1218. mapcount = page_mapcount(page);
  1219. if (!mapcount)
  1220. goto out;
  1221. cond_resched_lock(&mapping->i_mmap_lock);
  1222. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  1223. if (max_nl_cursor == 0)
  1224. max_nl_cursor = CLUSTER_SIZE;
  1225. do {
  1226. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  1227. shared.vm_set.list) {
  1228. cursor = (unsigned long) vma->vm_private_data;
  1229. while ( cursor < max_nl_cursor &&
  1230. cursor < vma->vm_end - vma->vm_start) {
  1231. if (try_to_unmap_cluster(cursor, &mapcount,
  1232. vma, page) == SWAP_MLOCK)
  1233. ret = SWAP_MLOCK;
  1234. cursor += CLUSTER_SIZE;
  1235. vma->vm_private_data = (void *) cursor;
  1236. if ((int)mapcount <= 0)
  1237. goto out;
  1238. }
  1239. vma->vm_private_data = (void *) max_nl_cursor;
  1240. }
  1241. cond_resched_lock(&mapping->i_mmap_lock);
  1242. max_nl_cursor += CLUSTER_SIZE;
  1243. } while (max_nl_cursor <= max_nl_size);
  1244. /*
  1245. * Don't loop forever (perhaps all the remaining pages are
  1246. * in locked vmas). Reset cursor on all unreserved nonlinear
  1247. * vmas, now forgetting on which ones it had fallen behind.
  1248. */
  1249. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1250. vma->vm_private_data = NULL;
  1251. out:
  1252. spin_unlock(&mapping->i_mmap_lock);
  1253. return ret;
  1254. }
  1255. /**
  1256. * try_to_unmap - try to remove all page table mappings to a page
  1257. * @page: the page to get unmapped
  1258. * @flags: action and flags
  1259. *
  1260. * Tries to remove all the page table entries which are mapping this
  1261. * page, used in the pageout path. Caller must hold the page lock.
  1262. * Return values are:
  1263. *
  1264. * SWAP_SUCCESS - we succeeded in removing all mappings
  1265. * SWAP_AGAIN - we missed a mapping, try again later
  1266. * SWAP_FAIL - the page is unswappable
  1267. * SWAP_MLOCK - page is mlocked.
  1268. */
  1269. int try_to_unmap(struct page *page, enum ttu_flags flags)
  1270. {
  1271. int ret;
  1272. BUG_ON(!PageLocked(page));
  1273. VM_BUG_ON(!PageHuge(page) && PageTransHuge(page));
  1274. if (unlikely(PageKsm(page)))
  1275. ret = try_to_unmap_ksm(page, flags);
  1276. else if (PageAnon(page))
  1277. ret = try_to_unmap_anon(page, flags);
  1278. else
  1279. ret = try_to_unmap_file(page, flags);
  1280. if (ret != SWAP_MLOCK && !page_mapped(page))
  1281. ret = SWAP_SUCCESS;
  1282. return ret;
  1283. }
  1284. /**
  1285. * try_to_munlock - try to munlock a page
  1286. * @page: the page to be munlocked
  1287. *
  1288. * Called from munlock code. Checks all of the VMAs mapping the page
  1289. * to make sure nobody else has this page mlocked. The page will be
  1290. * returned with PG_mlocked cleared if no other vmas have it mlocked.
  1291. *
  1292. * Return values are:
  1293. *
  1294. * SWAP_AGAIN - no vma is holding page mlocked, or,
  1295. * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
  1296. * SWAP_FAIL - page cannot be located at present
  1297. * SWAP_MLOCK - page is now mlocked.
  1298. */
  1299. int try_to_munlock(struct page *page)
  1300. {
  1301. VM_BUG_ON(!PageLocked(page) || PageLRU(page));
  1302. if (unlikely(PageKsm(page)))
  1303. return try_to_unmap_ksm(page, TTU_MUNLOCK);
  1304. else if (PageAnon(page))
  1305. return try_to_unmap_anon(page, TTU_MUNLOCK);
  1306. else
  1307. return try_to_unmap_file(page, TTU_MUNLOCK);
  1308. }
  1309. #if defined(CONFIG_KSM) || defined(CONFIG_MIGRATION)
  1310. /*
  1311. * Drop an anon_vma refcount, freeing the anon_vma and anon_vma->root
  1312. * if necessary. Be careful to do all the tests under the lock. Once
  1313. * we know we are the last user, nobody else can get a reference and we
  1314. * can do the freeing without the lock.
  1315. */
  1316. void drop_anon_vma(struct anon_vma *anon_vma)
  1317. {
  1318. BUG_ON(atomic_read(&anon_vma->external_refcount) <= 0);
  1319. if (atomic_dec_and_lock(&anon_vma->external_refcount, &anon_vma->root->lock)) {
  1320. struct anon_vma *root = anon_vma->root;
  1321. int empty = list_empty(&anon_vma->head);
  1322. int last_root_user = 0;
  1323. int root_empty = 0;
  1324. /*
  1325. * The refcount on a non-root anon_vma got dropped. Drop
  1326. * the refcount on the root and check if we need to free it.
  1327. */
  1328. if (empty && anon_vma != root) {
  1329. BUG_ON(atomic_read(&root->external_refcount) <= 0);
  1330. last_root_user = atomic_dec_and_test(&root->external_refcount);
  1331. root_empty = list_empty(&root->head);
  1332. }
  1333. anon_vma_unlock(anon_vma);
  1334. if (empty) {
  1335. anon_vma_free(anon_vma);
  1336. if (root_empty && last_root_user)
  1337. anon_vma_free(root);
  1338. }
  1339. }
  1340. }
  1341. #endif
  1342. #ifdef CONFIG_MIGRATION
  1343. /*
  1344. * rmap_walk() and its helpers rmap_walk_anon() and rmap_walk_file():
  1345. * Called by migrate.c to remove migration ptes, but might be used more later.
  1346. */
  1347. static int rmap_walk_anon(struct page *page, int (*rmap_one)(struct page *,
  1348. struct vm_area_struct *, unsigned long, void *), void *arg)
  1349. {
  1350. struct anon_vma *anon_vma;
  1351. struct anon_vma_chain *avc;
  1352. int ret = SWAP_AGAIN;
  1353. /*
  1354. * Note: remove_migration_ptes() cannot use page_lock_anon_vma()
  1355. * because that depends on page_mapped(); but not all its usages
  1356. * are holding mmap_sem. Users without mmap_sem are required to
  1357. * take a reference count to prevent the anon_vma disappearing
  1358. */
  1359. anon_vma = page_anon_vma(page);
  1360. if (!anon_vma)
  1361. return ret;
  1362. anon_vma_lock(anon_vma);
  1363. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  1364. struct vm_area_struct *vma = avc->vma;
  1365. unsigned long address = vma_address(page, vma);
  1366. if (address == -EFAULT)
  1367. continue;
  1368. ret = rmap_one(page, vma, address, arg);
  1369. if (ret != SWAP_AGAIN)
  1370. break;
  1371. }
  1372. anon_vma_unlock(anon_vma);
  1373. return ret;
  1374. }
  1375. static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *,
  1376. struct vm_area_struct *, unsigned long, void *), void *arg)
  1377. {
  1378. struct address_space *mapping = page->mapping;
  1379. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  1380. struct vm_area_struct *vma;
  1381. struct prio_tree_iter iter;
  1382. int ret = SWAP_AGAIN;
  1383. if (!mapping)
  1384. return ret;
  1385. spin_lock(&mapping->i_mmap_lock);
  1386. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1387. unsigned long address = vma_address(page, vma);
  1388. if (address == -EFAULT)
  1389. continue;
  1390. ret = rmap_one(page, vma, address, arg);
  1391. if (ret != SWAP_AGAIN)
  1392. break;
  1393. }
  1394. /*
  1395. * No nonlinear handling: being always shared, nonlinear vmas
  1396. * never contain migration ptes. Decide what to do about this
  1397. * limitation to linear when we need rmap_walk() on nonlinear.
  1398. */
  1399. spin_unlock(&mapping->i_mmap_lock);
  1400. return ret;
  1401. }
  1402. int rmap_walk(struct page *page, int (*rmap_one)(struct page *,
  1403. struct vm_area_struct *, unsigned long, void *), void *arg)
  1404. {
  1405. VM_BUG_ON(!PageLocked(page));
  1406. if (unlikely(PageKsm(page)))
  1407. return rmap_walk_ksm(page, rmap_one, arg);
  1408. else if (PageAnon(page))
  1409. return rmap_walk_anon(page, rmap_one, arg);
  1410. else
  1411. return rmap_walk_file(page, rmap_one, arg);
  1412. }
  1413. #endif /* CONFIG_MIGRATION */
  1414. #ifdef CONFIG_HUGETLB_PAGE
  1415. /*
  1416. * The following three functions are for anonymous (private mapped) hugepages.
  1417. * Unlike common anonymous pages, anonymous hugepages have no accounting code
  1418. * and no lru code, because we handle hugepages differently from common pages.
  1419. */
  1420. static void __hugepage_set_anon_rmap(struct page *page,
  1421. struct vm_area_struct *vma, unsigned long address, int exclusive)
  1422. {
  1423. struct anon_vma *anon_vma = vma->anon_vma;
  1424. BUG_ON(!anon_vma);
  1425. if (PageAnon(page))
  1426. return;
  1427. if (!exclusive)
  1428. anon_vma = anon_vma->root;
  1429. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  1430. page->mapping = (struct address_space *) anon_vma;
  1431. page->index = linear_page_index(vma, address);
  1432. }
  1433. void hugepage_add_anon_rmap(struct page *page,
  1434. struct vm_area_struct *vma, unsigned long address)
  1435. {
  1436. struct anon_vma *anon_vma = vma->anon_vma;
  1437. int first;
  1438. BUG_ON(!PageLocked(page));
  1439. BUG_ON(!anon_vma);
  1440. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  1441. first = atomic_inc_and_test(&page->_mapcount);
  1442. if (first)
  1443. __hugepage_set_anon_rmap(page, vma, address, 0);
  1444. }
  1445. void hugepage_add_new_anon_rmap(struct page *page,
  1446. struct vm_area_struct *vma, unsigned long address)
  1447. {
  1448. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  1449. atomic_set(&page->_mapcount, 0);
  1450. __hugepage_set_anon_rmap(page, vma, address, 1);
  1451. }
  1452. #endif /* CONFIG_HUGETLB_PAGE */