rmap.c 49 KB

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