rmap.c 55 KB

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