rmap.c 49 KB

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