rmap.c 47 KB

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