rmap.c 50 KB

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