rmap.c 44 KB

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