rmap.c 50 KB

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