rmap.c 53 KB

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