rmap.c 55 KB

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