rmap.c 53 KB

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