rmap.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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. VM_WARN_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, anon_vma_ctor);
  383. anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, SLAB_PANIC);
  384. }
  385. /*
  386. * Getting a lock on a stable anon_vma from a page off the LRU is tricky!
  387. *
  388. * Since there is no serialization what so ever against page_remove_rmap()
  389. * the best this function can do is return a locked anon_vma that might
  390. * have been relevant to this page.
  391. *
  392. * The page might have been remapped to a different anon_vma or the anon_vma
  393. * returned may already be freed (and even reused).
  394. *
  395. * In case it was remapped to a different anon_vma, the new anon_vma will be a
  396. * child of the old anon_vma, and the anon_vma lifetime rules will therefore
  397. * ensure that any anon_vma obtained from the page will still be valid for as
  398. * long as we observe page_mapped() [ hence all those page_mapped() tests ].
  399. *
  400. * All users of this function must be very careful when walking the anon_vma
  401. * chain and verify that the page in question is indeed mapped in it
  402. * [ something equivalent to page_mapped_in_vma() ].
  403. *
  404. * Since anon_vma's slab is DESTROY_BY_RCU and we know from page_remove_rmap()
  405. * that the anon_vma pointer from page->mapping is valid if there is a
  406. * mapcount, we can dereference the anon_vma after observing those.
  407. */
  408. struct anon_vma *page_get_anon_vma(struct page *page)
  409. {
  410. struct anon_vma *anon_vma = NULL;
  411. unsigned long anon_mapping;
  412. rcu_read_lock();
  413. anon_mapping = (unsigned long)READ_ONCE(page->mapping);
  414. if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
  415. goto out;
  416. if (!page_mapped(page))
  417. goto out;
  418. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  419. if (!atomic_inc_not_zero(&anon_vma->refcount)) {
  420. anon_vma = NULL;
  421. goto out;
  422. }
  423. /*
  424. * If this page is still mapped, then its anon_vma cannot have been
  425. * freed. But if it has been unmapped, we have no security against the
  426. * anon_vma structure being freed and reused (for another anon_vma:
  427. * SLAB_DESTROY_BY_RCU guarantees that - so the atomic_inc_not_zero()
  428. * above cannot corrupt).
  429. */
  430. if (!page_mapped(page)) {
  431. rcu_read_unlock();
  432. put_anon_vma(anon_vma);
  433. return NULL;
  434. }
  435. out:
  436. rcu_read_unlock();
  437. return anon_vma;
  438. }
  439. /*
  440. * Similar to page_get_anon_vma() except it locks the anon_vma.
  441. *
  442. * Its a little more complex as it tries to keep the fast path to a single
  443. * atomic op -- the trylock. If we fail the trylock, we fall back to getting a
  444. * reference like with page_get_anon_vma() and then block on the mutex.
  445. */
  446. struct anon_vma *page_lock_anon_vma_read(struct page *page)
  447. {
  448. struct anon_vma *anon_vma = NULL;
  449. struct anon_vma *root_anon_vma;
  450. unsigned long anon_mapping;
  451. rcu_read_lock();
  452. anon_mapping = (unsigned long)READ_ONCE(page->mapping);
  453. if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
  454. goto out;
  455. if (!page_mapped(page))
  456. goto out;
  457. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  458. root_anon_vma = READ_ONCE(anon_vma->root);
  459. if (down_read_trylock(&root_anon_vma->rwsem)) {
  460. /*
  461. * If the page is still mapped, then this anon_vma is still
  462. * its anon_vma, and holding the mutex ensures that it will
  463. * not go away, see anon_vma_free().
  464. */
  465. if (!page_mapped(page)) {
  466. up_read(&root_anon_vma->rwsem);
  467. anon_vma = NULL;
  468. }
  469. goto out;
  470. }
  471. /* trylock failed, we got to sleep */
  472. if (!atomic_inc_not_zero(&anon_vma->refcount)) {
  473. anon_vma = NULL;
  474. goto out;
  475. }
  476. if (!page_mapped(page)) {
  477. rcu_read_unlock();
  478. put_anon_vma(anon_vma);
  479. return NULL;
  480. }
  481. /* we pinned the anon_vma, its safe to sleep */
  482. rcu_read_unlock();
  483. anon_vma_lock_read(anon_vma);
  484. if (atomic_dec_and_test(&anon_vma->refcount)) {
  485. /*
  486. * Oops, we held the last refcount, release the lock
  487. * and bail -- can't simply use put_anon_vma() because
  488. * we'll deadlock on the anon_vma_lock_write() recursion.
  489. */
  490. anon_vma_unlock_read(anon_vma);
  491. __put_anon_vma(anon_vma);
  492. anon_vma = NULL;
  493. }
  494. return anon_vma;
  495. out:
  496. rcu_read_unlock();
  497. return anon_vma;
  498. }
  499. void page_unlock_anon_vma_read(struct anon_vma *anon_vma)
  500. {
  501. anon_vma_unlock_read(anon_vma);
  502. }
  503. /*
  504. * At what user virtual address is page expected in @vma?
  505. */
  506. static inline unsigned long
  507. __vma_address(struct page *page, struct vm_area_struct *vma)
  508. {
  509. pgoff_t pgoff = page_to_pgoff(page);
  510. return vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  511. }
  512. inline unsigned long
  513. vma_address(struct page *page, struct vm_area_struct *vma)
  514. {
  515. unsigned long address = __vma_address(page, vma);
  516. /* page should be within @vma mapping range */
  517. VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
  518. return address;
  519. }
  520. #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
  521. /*
  522. * Flush TLB entries for recently unmapped pages from remote CPUs. It is
  523. * important if a PTE was dirty when it was unmapped that it's flushed
  524. * before any IO is initiated on the page to prevent lost writes. Similarly,
  525. * it must be flushed before freeing to prevent data leakage.
  526. */
  527. void try_to_unmap_flush(void)
  528. {
  529. struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
  530. int cpu;
  531. if (!tlb_ubc->flush_required)
  532. return;
  533. cpu = get_cpu();
  534. if (cpumask_test_cpu(cpu, &tlb_ubc->cpumask)) {
  535. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  536. local_flush_tlb();
  537. trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
  538. }
  539. if (cpumask_any_but(&tlb_ubc->cpumask, cpu) < nr_cpu_ids)
  540. flush_tlb_others(&tlb_ubc->cpumask, NULL, 0, TLB_FLUSH_ALL);
  541. cpumask_clear(&tlb_ubc->cpumask);
  542. tlb_ubc->flush_required = false;
  543. tlb_ubc->writable = false;
  544. put_cpu();
  545. }
  546. /* Flush iff there are potentially writable TLB entries that can race with IO */
  547. void try_to_unmap_flush_dirty(void)
  548. {
  549. struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
  550. if (tlb_ubc->writable)
  551. try_to_unmap_flush();
  552. }
  553. static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
  554. struct page *page, bool writable)
  555. {
  556. struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
  557. cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm));
  558. tlb_ubc->flush_required = true;
  559. /*
  560. * Ensure compiler does not re-order the setting of tlb_flush_batched
  561. * before the PTE is cleared.
  562. */
  563. barrier();
  564. mm->tlb_flush_batched = true;
  565. /*
  566. * If the PTE was dirty then it's best to assume it's writable. The
  567. * caller must use try_to_unmap_flush_dirty() or try_to_unmap_flush()
  568. * before the page is queued for IO.
  569. */
  570. if (writable)
  571. tlb_ubc->writable = true;
  572. }
  573. /*
  574. * Returns true if the TLB flush should be deferred to the end of a batch of
  575. * unmap operations to reduce IPIs.
  576. */
  577. static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
  578. {
  579. bool should_defer = false;
  580. if (!(flags & TTU_BATCH_FLUSH))
  581. return false;
  582. /* If remote CPUs need to be flushed then defer batch the flush */
  583. if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids)
  584. should_defer = true;
  585. put_cpu();
  586. return should_defer;
  587. }
  588. /*
  589. * Reclaim unmaps pages under the PTL but do not flush the TLB prior to
  590. * releasing the PTL if TLB flushes are batched. It's possible for a parallel
  591. * operation such as mprotect or munmap to race between reclaim unmapping
  592. * the page and flushing the page. If this race occurs, it potentially allows
  593. * access to data via a stale TLB entry. Tracking all mm's that have TLB
  594. * batching in flight would be expensive during reclaim so instead track
  595. * whether TLB batching occurred in the past and if so then do a flush here
  596. * if required. This will cost one additional flush per reclaim cycle paid
  597. * by the first operation at risk such as mprotect and mumap.
  598. *
  599. * This must be called under the PTL so that an access to tlb_flush_batched
  600. * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronise
  601. * via the PTL.
  602. */
  603. void flush_tlb_batched_pending(struct mm_struct *mm)
  604. {
  605. if (mm->tlb_flush_batched) {
  606. flush_tlb_mm(mm);
  607. /*
  608. * Do not allow the compiler to re-order the clearing of
  609. * tlb_flush_batched before the tlb is flushed.
  610. */
  611. barrier();
  612. mm->tlb_flush_batched = false;
  613. }
  614. }
  615. #else
  616. static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
  617. struct page *page, bool writable)
  618. {
  619. }
  620. static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
  621. {
  622. return false;
  623. }
  624. #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
  625. /*
  626. * At what user virtual address is page expected in vma?
  627. * Caller should check the page is actually part of the vma.
  628. */
  629. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  630. {
  631. unsigned long address;
  632. if (PageAnon(page)) {
  633. struct anon_vma *page__anon_vma = page_anon_vma(page);
  634. /*
  635. * Note: swapoff's unuse_vma() is more efficient with this
  636. * check, and needs it to match anon_vma when KSM is active.
  637. */
  638. if (!vma->anon_vma || !page__anon_vma ||
  639. vma->anon_vma->root != page__anon_vma->root)
  640. return -EFAULT;
  641. } else if (page->mapping) {
  642. if (!vma->vm_file || vma->vm_file->f_mapping != page->mapping)
  643. return -EFAULT;
  644. } else
  645. return -EFAULT;
  646. address = __vma_address(page, vma);
  647. if (unlikely(address < vma->vm_start || address >= vma->vm_end))
  648. return -EFAULT;
  649. return address;
  650. }
  651. pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address)
  652. {
  653. pgd_t *pgd;
  654. pud_t *pud;
  655. pmd_t *pmd = NULL;
  656. pmd_t pmde;
  657. pgd = pgd_offset(mm, address);
  658. if (!pgd_present(*pgd))
  659. goto out;
  660. pud = pud_offset(pgd, address);
  661. if (!pud_present(*pud))
  662. goto out;
  663. pmd = pmd_offset(pud, address);
  664. /*
  665. * Some THP functions use the sequence pmdp_huge_clear_flush(), set_pmd_at()
  666. * without holding anon_vma lock for write. So when looking for a
  667. * genuine pmde (in which to find pte), test present and !THP together.
  668. */
  669. pmde = *pmd;
  670. barrier();
  671. if (!pmd_present(pmde) || pmd_trans_huge(pmde))
  672. pmd = NULL;
  673. out:
  674. return pmd;
  675. }
  676. /*
  677. * Check that @page is mapped at @address into @mm.
  678. *
  679. * If @sync is false, page_check_address may perform a racy check to avoid
  680. * the page table lock when the pte is not present (helpful when reclaiming
  681. * highly shared pages).
  682. *
  683. * On success returns with pte mapped and locked.
  684. */
  685. pte_t *__page_check_address(struct page *page, struct mm_struct *mm,
  686. unsigned long address, spinlock_t **ptlp, int sync)
  687. {
  688. pmd_t *pmd;
  689. pte_t *pte;
  690. spinlock_t *ptl;
  691. if (unlikely(PageHuge(page))) {
  692. /* when pud is not present, pte will be NULL */
  693. pte = huge_pte_offset(mm, address);
  694. if (!pte)
  695. return NULL;
  696. ptl = huge_pte_lockptr(page_hstate(page), mm, pte);
  697. goto check;
  698. }
  699. pmd = mm_find_pmd(mm, address);
  700. if (!pmd)
  701. return NULL;
  702. pte = pte_offset_map(pmd, address);
  703. /* Make a quick check before getting the lock */
  704. if (!sync && !pte_present(*pte)) {
  705. pte_unmap(pte);
  706. return NULL;
  707. }
  708. ptl = pte_lockptr(mm, pmd);
  709. check:
  710. spin_lock(ptl);
  711. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  712. *ptlp = ptl;
  713. return pte;
  714. }
  715. pte_unmap_unlock(pte, ptl);
  716. return NULL;
  717. }
  718. /**
  719. * page_mapped_in_vma - check whether a page is really mapped in a VMA
  720. * @page: the page to test
  721. * @vma: the VMA to test
  722. *
  723. * Returns 1 if the page is mapped into the page tables of the VMA, 0
  724. * if the page is not mapped into the page tables of this VMA. Only
  725. * valid for normal file or anonymous VMAs.
  726. */
  727. int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
  728. {
  729. unsigned long address;
  730. pte_t *pte;
  731. spinlock_t *ptl;
  732. address = __vma_address(page, vma);
  733. if (unlikely(address < vma->vm_start || address >= vma->vm_end))
  734. return 0;
  735. pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
  736. if (!pte) /* the page is not in this mm */
  737. return 0;
  738. pte_unmap_unlock(pte, ptl);
  739. return 1;
  740. }
  741. struct page_referenced_arg {
  742. int mapcount;
  743. int referenced;
  744. unsigned long vm_flags;
  745. struct mem_cgroup *memcg;
  746. };
  747. /*
  748. * arg: page_referenced_arg will be passed
  749. */
  750. static int page_referenced_one(struct page *page, struct vm_area_struct *vma,
  751. unsigned long address, void *arg)
  752. {
  753. struct mm_struct *mm = vma->vm_mm;
  754. spinlock_t *ptl;
  755. int referenced = 0;
  756. struct page_referenced_arg *pra = arg;
  757. if (unlikely(PageTransHuge(page))) {
  758. pmd_t *pmd;
  759. /*
  760. * rmap might return false positives; we must filter
  761. * these out using page_check_address_pmd().
  762. */
  763. pmd = page_check_address_pmd(page, mm, address,
  764. PAGE_CHECK_ADDRESS_PMD_FLAG, &ptl);
  765. if (!pmd)
  766. return SWAP_AGAIN;
  767. if (vma->vm_flags & VM_LOCKED) {
  768. spin_unlock(ptl);
  769. pra->vm_flags |= VM_LOCKED;
  770. return SWAP_FAIL; /* To break the loop */
  771. }
  772. /* go ahead even if the pmd is pmd_trans_splitting() */
  773. if (pmdp_clear_flush_young_notify(vma, address, pmd))
  774. referenced++;
  775. spin_unlock(ptl);
  776. } else {
  777. pte_t *pte;
  778. /*
  779. * rmap might return false positives; we must filter
  780. * these out using page_check_address().
  781. */
  782. pte = page_check_address(page, mm, address, &ptl, 0);
  783. if (!pte)
  784. return SWAP_AGAIN;
  785. if (vma->vm_flags & VM_LOCKED) {
  786. pte_unmap_unlock(pte, ptl);
  787. pra->vm_flags |= VM_LOCKED;
  788. return SWAP_FAIL; /* To break the loop */
  789. }
  790. if (ptep_clear_flush_young_notify(vma, address, pte)) {
  791. /*
  792. * Don't treat a reference through a sequentially read
  793. * mapping as such. If the page has been used in
  794. * another mapping, we will catch it; if this other
  795. * mapping is already gone, the unmap path will have
  796. * set PG_referenced or activated the page.
  797. */
  798. if (likely(!(vma->vm_flags & VM_SEQ_READ)))
  799. referenced++;
  800. }
  801. pte_unmap_unlock(pte, ptl);
  802. }
  803. if (referenced)
  804. clear_page_idle(page);
  805. if (test_and_clear_page_young(page))
  806. referenced++;
  807. if (referenced) {
  808. pra->referenced++;
  809. pra->vm_flags |= vma->vm_flags;
  810. }
  811. pra->mapcount--;
  812. if (!pra->mapcount)
  813. return SWAP_SUCCESS; /* To break the loop */
  814. return SWAP_AGAIN;
  815. }
  816. static bool invalid_page_referenced_vma(struct vm_area_struct *vma, void *arg)
  817. {
  818. struct page_referenced_arg *pra = arg;
  819. struct mem_cgroup *memcg = pra->memcg;
  820. if (!mm_match_cgroup(vma->vm_mm, memcg))
  821. return true;
  822. return false;
  823. }
  824. /**
  825. * page_referenced - test if the page was referenced
  826. * @page: the page to test
  827. * @is_locked: caller holds lock on the page
  828. * @memcg: target memory cgroup
  829. * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
  830. *
  831. * Quick test_and_clear_referenced for all mappings to a page,
  832. * returns the number of ptes which referenced the page.
  833. */
  834. int page_referenced(struct page *page,
  835. int is_locked,
  836. struct mem_cgroup *memcg,
  837. unsigned long *vm_flags)
  838. {
  839. int ret;
  840. int we_locked = 0;
  841. struct page_referenced_arg pra = {
  842. .mapcount = page_mapcount(page),
  843. .memcg = memcg,
  844. };
  845. struct rmap_walk_control rwc = {
  846. .rmap_one = page_referenced_one,
  847. .arg = (void *)&pra,
  848. .anon_lock = page_lock_anon_vma_read,
  849. };
  850. *vm_flags = 0;
  851. if (!page_mapped(page))
  852. return 0;
  853. if (!page_rmapping(page))
  854. return 0;
  855. if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
  856. we_locked = trylock_page(page);
  857. if (!we_locked)
  858. return 1;
  859. }
  860. /*
  861. * If we are reclaiming on behalf of a cgroup, skip
  862. * counting on behalf of references from different
  863. * cgroups
  864. */
  865. if (memcg) {
  866. rwc.invalid_vma = invalid_page_referenced_vma;
  867. }
  868. ret = rmap_walk(page, &rwc);
  869. *vm_flags = pra.vm_flags;
  870. if (we_locked)
  871. unlock_page(page);
  872. return pra.referenced;
  873. }
  874. static int page_mkclean_one(struct page *page, struct vm_area_struct *vma,
  875. unsigned long address, void *arg)
  876. {
  877. struct mm_struct *mm = vma->vm_mm;
  878. pte_t *pte;
  879. spinlock_t *ptl;
  880. int ret = 0;
  881. int *cleaned = arg;
  882. pte = page_check_address(page, mm, address, &ptl, 1);
  883. if (!pte)
  884. goto out;
  885. if (pte_dirty(*pte) || pte_write(*pte)) {
  886. pte_t entry;
  887. flush_cache_page(vma, address, pte_pfn(*pte));
  888. entry = ptep_clear_flush(vma, address, pte);
  889. entry = pte_wrprotect(entry);
  890. entry = pte_mkclean(entry);
  891. set_pte_at(mm, address, pte, entry);
  892. ret = 1;
  893. }
  894. pte_unmap_unlock(pte, ptl);
  895. if (ret) {
  896. mmu_notifier_invalidate_page(mm, address);
  897. (*cleaned)++;
  898. }
  899. out:
  900. return SWAP_AGAIN;
  901. }
  902. static bool invalid_mkclean_vma(struct vm_area_struct *vma, void *arg)
  903. {
  904. if (vma->vm_flags & VM_SHARED)
  905. return false;
  906. return true;
  907. }
  908. int page_mkclean(struct page *page)
  909. {
  910. int cleaned = 0;
  911. struct address_space *mapping;
  912. struct rmap_walk_control rwc = {
  913. .arg = (void *)&cleaned,
  914. .rmap_one = page_mkclean_one,
  915. .invalid_vma = invalid_mkclean_vma,
  916. };
  917. BUG_ON(!PageLocked(page));
  918. if (!page_mapped(page))
  919. return 0;
  920. mapping = page_mapping(page);
  921. if (!mapping)
  922. return 0;
  923. rmap_walk(page, &rwc);
  924. return cleaned;
  925. }
  926. EXPORT_SYMBOL_GPL(page_mkclean);
  927. /**
  928. * page_move_anon_rmap - move a page to our anon_vma
  929. * @page: the page to move to our anon_vma
  930. * @vma: the vma the page belongs to
  931. * @address: the user virtual address mapped
  932. *
  933. * When a page belongs exclusively to one process after a COW event,
  934. * that page can be moved into the anon_vma that belongs to just that
  935. * process, so the rmap code will not search the parent or sibling
  936. * processes.
  937. */
  938. void page_move_anon_rmap(struct page *page,
  939. struct vm_area_struct *vma, unsigned long address)
  940. {
  941. struct anon_vma *anon_vma = vma->anon_vma;
  942. VM_BUG_ON_PAGE(!PageLocked(page), page);
  943. VM_BUG_ON_VMA(!anon_vma, vma);
  944. VM_BUG_ON_PAGE(page->index != linear_page_index(vma, address), page);
  945. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  946. /*
  947. * Ensure that anon_vma and the PAGE_MAPPING_ANON bit are written
  948. * simultaneously, so a concurrent reader (eg page_referenced()'s
  949. * PageAnon()) will not see one without the other.
  950. */
  951. WRITE_ONCE(page->mapping, (struct address_space *) anon_vma);
  952. }
  953. /**
  954. * __page_set_anon_rmap - set up new anonymous rmap
  955. * @page: Page to add to rmap
  956. * @vma: VM area to add page to.
  957. * @address: User virtual address of the mapping
  958. * @exclusive: the page is exclusively owned by the current process
  959. */
  960. static void __page_set_anon_rmap(struct page *page,
  961. struct vm_area_struct *vma, unsigned long address, int exclusive)
  962. {
  963. struct anon_vma *anon_vma = vma->anon_vma;
  964. BUG_ON(!anon_vma);
  965. if (PageAnon(page))
  966. return;
  967. /*
  968. * If the page isn't exclusively mapped into this vma,
  969. * we must use the _oldest_ possible anon_vma for the
  970. * page mapping!
  971. */
  972. if (!exclusive)
  973. anon_vma = anon_vma->root;
  974. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  975. page->mapping = (struct address_space *) anon_vma;
  976. page->index = linear_page_index(vma, address);
  977. }
  978. /**
  979. * __page_check_anon_rmap - sanity check anonymous rmap addition
  980. * @page: the page to add the mapping to
  981. * @vma: the vm area in which the mapping is added
  982. * @address: the user virtual address mapped
  983. */
  984. static void __page_check_anon_rmap(struct page *page,
  985. struct vm_area_struct *vma, unsigned long address)
  986. {
  987. #ifdef CONFIG_DEBUG_VM
  988. /*
  989. * The page's anon-rmap details (mapping and index) are guaranteed to
  990. * be set up correctly at this point.
  991. *
  992. * We have exclusion against page_add_anon_rmap because the caller
  993. * always holds the page locked, except if called from page_dup_rmap,
  994. * in which case the page is already known to be setup.
  995. *
  996. * We have exclusion against page_add_new_anon_rmap because those pages
  997. * are initially only visible via the pagetables, and the pte is locked
  998. * over the call to page_add_new_anon_rmap.
  999. */
  1000. BUG_ON(page_anon_vma(page)->root != vma->anon_vma->root);
  1001. BUG_ON(page->index != linear_page_index(vma, address));
  1002. #endif
  1003. }
  1004. /**
  1005. * page_add_anon_rmap - add pte mapping to an anonymous page
  1006. * @page: the page to add the mapping to
  1007. * @vma: the vm area in which the mapping is added
  1008. * @address: the user virtual address mapped
  1009. *
  1010. * The caller needs to hold the pte lock, and the page must be locked in
  1011. * the anon_vma case: to serialize mapping,index checking after setting,
  1012. * and to ensure that PageAnon is not being upgraded racily to PageKsm
  1013. * (but PageKsm is never downgraded to PageAnon).
  1014. */
  1015. void page_add_anon_rmap(struct page *page,
  1016. struct vm_area_struct *vma, unsigned long address)
  1017. {
  1018. do_page_add_anon_rmap(page, vma, address, 0);
  1019. }
  1020. /*
  1021. * Special version of the above for do_swap_page, which often runs
  1022. * into pages that are exclusively owned by the current process.
  1023. * Everybody else should continue to use page_add_anon_rmap above.
  1024. */
  1025. void do_page_add_anon_rmap(struct page *page,
  1026. struct vm_area_struct *vma, unsigned long address, int exclusive)
  1027. {
  1028. int first = atomic_inc_and_test(&page->_mapcount);
  1029. if (first) {
  1030. /*
  1031. * We use the irq-unsafe __{inc|mod}_zone_page_stat because
  1032. * these counters are not modified in interrupt context, and
  1033. * pte lock(a spinlock) is held, which implies preemption
  1034. * disabled.
  1035. */
  1036. if (PageTransHuge(page))
  1037. __inc_zone_page_state(page,
  1038. NR_ANON_TRANSPARENT_HUGEPAGES);
  1039. __mod_zone_page_state(page_zone(page), NR_ANON_PAGES,
  1040. hpage_nr_pages(page));
  1041. }
  1042. if (unlikely(PageKsm(page)))
  1043. return;
  1044. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1045. /* address might be in next vma when migration races vma_adjust */
  1046. if (first)
  1047. __page_set_anon_rmap(page, vma, address, exclusive);
  1048. else
  1049. __page_check_anon_rmap(page, vma, address);
  1050. }
  1051. /**
  1052. * page_add_new_anon_rmap - add pte mapping to a new anonymous page
  1053. * @page: the page to add the mapping to
  1054. * @vma: the vm area in which the mapping is added
  1055. * @address: the user virtual address mapped
  1056. *
  1057. * Same as page_add_anon_rmap but must only be called on *new* pages.
  1058. * This means the inc-and-test can be bypassed.
  1059. * Page does not have to be locked.
  1060. */
  1061. void page_add_new_anon_rmap(struct page *page,
  1062. struct vm_area_struct *vma, unsigned long address)
  1063. {
  1064. VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
  1065. SetPageSwapBacked(page);
  1066. atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
  1067. if (PageTransHuge(page))
  1068. __inc_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
  1069. __mod_zone_page_state(page_zone(page), NR_ANON_PAGES,
  1070. hpage_nr_pages(page));
  1071. __page_set_anon_rmap(page, vma, address, 1);
  1072. }
  1073. /**
  1074. * page_add_file_rmap - add pte mapping to a file page
  1075. * @page: the page to add the mapping to
  1076. *
  1077. * The caller needs to hold the pte lock.
  1078. */
  1079. void page_add_file_rmap(struct page *page)
  1080. {
  1081. struct mem_cgroup *memcg;
  1082. memcg = mem_cgroup_begin_page_stat(page);
  1083. if (atomic_inc_and_test(&page->_mapcount)) {
  1084. __inc_zone_page_state(page, NR_FILE_MAPPED);
  1085. mem_cgroup_inc_page_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
  1086. }
  1087. mem_cgroup_end_page_stat(memcg);
  1088. }
  1089. static void page_remove_file_rmap(struct page *page)
  1090. {
  1091. struct mem_cgroup *memcg;
  1092. memcg = mem_cgroup_begin_page_stat(page);
  1093. /* page still mapped by someone else? */
  1094. if (!atomic_add_negative(-1, &page->_mapcount))
  1095. goto out;
  1096. /* Hugepages are not counted in NR_FILE_MAPPED for now. */
  1097. if (unlikely(PageHuge(page)))
  1098. goto out;
  1099. /*
  1100. * We use the irq-unsafe __{inc|mod}_zone_page_stat because
  1101. * these counters are not modified in interrupt context, and
  1102. * pte lock(a spinlock) is held, which implies preemption disabled.
  1103. */
  1104. __dec_zone_page_state(page, NR_FILE_MAPPED);
  1105. mem_cgroup_dec_page_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
  1106. if (unlikely(PageMlocked(page)))
  1107. clear_page_mlock(page);
  1108. out:
  1109. mem_cgroup_end_page_stat(memcg);
  1110. }
  1111. /**
  1112. * page_remove_rmap - take down pte mapping from a page
  1113. * @page: page to remove mapping from
  1114. *
  1115. * The caller needs to hold the pte lock.
  1116. */
  1117. void page_remove_rmap(struct page *page)
  1118. {
  1119. if (!PageAnon(page)) {
  1120. page_remove_file_rmap(page);
  1121. return;
  1122. }
  1123. /* page still mapped by someone else? */
  1124. if (!atomic_add_negative(-1, &page->_mapcount))
  1125. return;
  1126. /* Hugepages are not counted in NR_ANON_PAGES for now. */
  1127. if (unlikely(PageHuge(page)))
  1128. return;
  1129. /*
  1130. * We use the irq-unsafe __{inc|mod}_zone_page_stat because
  1131. * these counters are not modified in interrupt context, and
  1132. * pte lock(a spinlock) is held, which implies preemption disabled.
  1133. */
  1134. if (PageTransHuge(page))
  1135. __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
  1136. __mod_zone_page_state(page_zone(page), NR_ANON_PAGES,
  1137. -hpage_nr_pages(page));
  1138. if (unlikely(PageMlocked(page)))
  1139. clear_page_mlock(page);
  1140. /*
  1141. * It would be tidy to reset the PageAnon mapping here,
  1142. * but that might overwrite a racing page_add_anon_rmap
  1143. * which increments mapcount after us but sets mapping
  1144. * before us: so leave the reset to free_hot_cold_page,
  1145. * and remember that it's only reliable while mapped.
  1146. * Leaving it set also helps swapoff to reinstate ptes
  1147. * faster for those pages still in swapcache.
  1148. */
  1149. }
  1150. /*
  1151. * @arg: enum ttu_flags will be passed to this argument
  1152. */
  1153. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  1154. unsigned long address, void *arg)
  1155. {
  1156. struct mm_struct *mm = vma->vm_mm;
  1157. pte_t *pte;
  1158. pte_t pteval;
  1159. spinlock_t *ptl;
  1160. int ret = SWAP_AGAIN;
  1161. unsigned long sh_address;
  1162. bool pmd_sharing_possible = false;
  1163. unsigned long spmd_start, spmd_end;
  1164. enum ttu_flags flags = (enum ttu_flags)arg;
  1165. /* munlock has nothing to gain from examining un-locked vmas */
  1166. if ((flags & TTU_MUNLOCK) && !(vma->vm_flags & VM_LOCKED))
  1167. goto out;
  1168. /*
  1169. * Only use the range_start/end mmu notifiers if huge pmd sharing
  1170. * is possible. In the normal case, mmu_notifier_invalidate_page
  1171. * is sufficient as we only unmap a page. However, if we unshare
  1172. * a pmd, we will unmap a PUD_SIZE range.
  1173. */
  1174. if (PageHuge(page)) {
  1175. spmd_start = address;
  1176. spmd_end = spmd_start + vma_mmu_pagesize(vma);
  1177. /*
  1178. * Check if pmd sharing is possible. If possible, we could
  1179. * unmap a PUD_SIZE range. spmd_start/spmd_end will be
  1180. * modified if sharing is possible.
  1181. */
  1182. adjust_range_if_pmd_sharing_possible(vma, &spmd_start,
  1183. &spmd_end);
  1184. if (spmd_end - spmd_start != vma_mmu_pagesize(vma)) {
  1185. sh_address = address;
  1186. pmd_sharing_possible = true;
  1187. mmu_notifier_invalidate_range_start(vma->vm_mm,
  1188. spmd_start, spmd_end);
  1189. }
  1190. }
  1191. pte = page_check_address(page, mm, address, &ptl, 0);
  1192. if (!pte)
  1193. goto out;
  1194. /*
  1195. * If the page is mlock()d, we cannot swap it out.
  1196. * If it's recently referenced (perhaps page_referenced
  1197. * skipped over this mm) then we should reactivate it.
  1198. */
  1199. if (!(flags & TTU_IGNORE_MLOCK)) {
  1200. if (vma->vm_flags & VM_LOCKED) {
  1201. /* Holding pte lock, we do *not* need mmap_sem here */
  1202. mlock_vma_page(page);
  1203. ret = SWAP_MLOCK;
  1204. goto out_unmap;
  1205. }
  1206. if (flags & TTU_MUNLOCK)
  1207. goto out_unmap;
  1208. }
  1209. if (!(flags & TTU_IGNORE_ACCESS)) {
  1210. if (ptep_clear_flush_young_notify(vma, address, pte)) {
  1211. ret = SWAP_FAIL;
  1212. goto out_unmap;
  1213. }
  1214. }
  1215. /*
  1216. * Call huge_pmd_unshare to potentially unshare a huge pmd. Pass
  1217. * sh_address as it will be modified if unsharing is successful.
  1218. */
  1219. if (PageHuge(page) && huge_pmd_unshare(mm, &sh_address, pte)) {
  1220. /*
  1221. * huge_pmd_unshare unmapped an entire PMD page. There is
  1222. * no way of knowing exactly which PMDs may be cached for
  1223. * this mm, so flush them all. spmd_start/spmd_end cover
  1224. * this PUD_SIZE range.
  1225. */
  1226. flush_cache_range(vma, spmd_start, spmd_end);
  1227. flush_tlb_range(vma, spmd_start, spmd_end);
  1228. /*
  1229. * The ref count of the PMD page was dropped which is part
  1230. * of the way map counting is done for shared PMDs. When
  1231. * there is no other sharing, huge_pmd_unshare returns false
  1232. * and we will unmap the actual page and drop map count
  1233. * to zero.
  1234. */
  1235. goto out_unmap;
  1236. }
  1237. /* Nuke the page table entry. */
  1238. flush_cache_page(vma, address, page_to_pfn(page));
  1239. if (should_defer_flush(mm, flags)) {
  1240. /*
  1241. * We clear the PTE but do not flush so potentially a remote
  1242. * CPU could still be writing to the page. If the entry was
  1243. * previously clean then the architecture must guarantee that
  1244. * a clear->dirty transition on a cached TLB entry is written
  1245. * through and traps if the PTE is unmapped.
  1246. */
  1247. pteval = ptep_get_and_clear(mm, address, pte);
  1248. set_tlb_ubc_flush_pending(mm, page, pte_dirty(pteval));
  1249. } else {
  1250. pteval = ptep_clear_flush(vma, address, pte);
  1251. }
  1252. /* Move the dirty bit to the physical page now the pte is gone. */
  1253. if (pte_dirty(pteval))
  1254. set_page_dirty(page);
  1255. /* Update high watermark before we lower rss */
  1256. update_hiwater_rss(mm);
  1257. if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
  1258. if (PageHuge(page)) {
  1259. hugetlb_count_sub(1 << compound_order(page), mm);
  1260. } else {
  1261. if (PageAnon(page))
  1262. dec_mm_counter(mm, MM_ANONPAGES);
  1263. else
  1264. dec_mm_counter(mm, MM_FILEPAGES);
  1265. }
  1266. set_pte_at(mm, address, pte,
  1267. swp_entry_to_pte(make_hwpoison_entry(page)));
  1268. } else if (pte_unused(pteval)) {
  1269. /*
  1270. * The guest indicated that the page content is of no
  1271. * interest anymore. Simply discard the pte, vmscan
  1272. * will take care of the rest.
  1273. */
  1274. if (PageAnon(page))
  1275. dec_mm_counter(mm, MM_ANONPAGES);
  1276. else
  1277. dec_mm_counter(mm, MM_FILEPAGES);
  1278. } else if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION)) {
  1279. swp_entry_t entry;
  1280. pte_t swp_pte;
  1281. /*
  1282. * Store the pfn of the page in a special migration
  1283. * pte. do_swap_page() will wait until the migration
  1284. * pte is removed and then restart fault handling.
  1285. */
  1286. entry = make_migration_entry(page, pte_write(pteval));
  1287. swp_pte = swp_entry_to_pte(entry);
  1288. if (pte_soft_dirty(pteval))
  1289. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  1290. set_pte_at(mm, address, pte, swp_pte);
  1291. } else if (PageAnon(page)) {
  1292. swp_entry_t entry = { .val = page_private(page) };
  1293. pte_t swp_pte;
  1294. /*
  1295. * Store the swap location in the pte.
  1296. * See handle_pte_fault() ...
  1297. */
  1298. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  1299. if (swap_duplicate(entry) < 0) {
  1300. set_pte_at(mm, address, pte, pteval);
  1301. ret = SWAP_FAIL;
  1302. goto out_unmap;
  1303. }
  1304. if (list_empty(&mm->mmlist)) {
  1305. spin_lock(&mmlist_lock);
  1306. if (list_empty(&mm->mmlist))
  1307. list_add(&mm->mmlist, &init_mm.mmlist);
  1308. spin_unlock(&mmlist_lock);
  1309. }
  1310. dec_mm_counter(mm, MM_ANONPAGES);
  1311. inc_mm_counter(mm, MM_SWAPENTS);
  1312. swp_pte = swp_entry_to_pte(entry);
  1313. if (pte_soft_dirty(pteval))
  1314. swp_pte = pte_swp_mksoft_dirty(swp_pte);
  1315. set_pte_at(mm, address, pte, swp_pte);
  1316. } else
  1317. dec_mm_counter(mm, MM_FILEPAGES);
  1318. page_remove_rmap(page);
  1319. page_cache_release(page);
  1320. out_unmap:
  1321. pte_unmap_unlock(pte, ptl);
  1322. if (ret != SWAP_FAIL && ret != SWAP_MLOCK && !(flags & TTU_MUNLOCK))
  1323. mmu_notifier_invalidate_page(mm, address);
  1324. out:
  1325. if (pmd_sharing_possible)
  1326. mmu_notifier_invalidate_range_end(vma->vm_mm,
  1327. spmd_start, spmd_end);
  1328. return ret;
  1329. }
  1330. bool is_vma_temporary_stack(struct vm_area_struct *vma)
  1331. {
  1332. int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
  1333. if (!maybe_stack)
  1334. return false;
  1335. if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
  1336. VM_STACK_INCOMPLETE_SETUP)
  1337. return true;
  1338. return false;
  1339. }
  1340. static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg)
  1341. {
  1342. return is_vma_temporary_stack(vma);
  1343. }
  1344. static int page_not_mapped(struct page *page)
  1345. {
  1346. return !page_mapped(page);
  1347. };
  1348. /**
  1349. * try_to_unmap - try to remove all page table mappings to a page
  1350. * @page: the page to get unmapped
  1351. * @flags: action and flags
  1352. *
  1353. * Tries to remove all the page table entries which are mapping this
  1354. * page, used in the pageout path. Caller must hold the page lock.
  1355. * Return values are:
  1356. *
  1357. * SWAP_SUCCESS - we succeeded in removing all mappings
  1358. * SWAP_AGAIN - we missed a mapping, try again later
  1359. * SWAP_FAIL - the page is unswappable
  1360. * SWAP_MLOCK - page is mlocked.
  1361. */
  1362. int try_to_unmap(struct page *page, enum ttu_flags flags)
  1363. {
  1364. int ret;
  1365. struct rmap_walk_control rwc = {
  1366. .rmap_one = try_to_unmap_one,
  1367. .arg = (void *)flags,
  1368. .done = page_not_mapped,
  1369. .anon_lock = page_lock_anon_vma_read,
  1370. };
  1371. VM_BUG_ON_PAGE(!PageHuge(page) && PageTransHuge(page), page);
  1372. /*
  1373. * During exec, a temporary VMA is setup and later moved.
  1374. * The VMA is moved under the anon_vma lock but not the
  1375. * page tables leading to a race where migration cannot
  1376. * find the migration ptes. Rather than increasing the
  1377. * locking requirements of exec(), migration skips
  1378. * temporary VMAs until after exec() completes.
  1379. */
  1380. if ((flags & TTU_MIGRATION) && !PageKsm(page) && PageAnon(page))
  1381. rwc.invalid_vma = invalid_migration_vma;
  1382. ret = rmap_walk(page, &rwc);
  1383. if (ret != SWAP_MLOCK && !page_mapped(page))
  1384. ret = SWAP_SUCCESS;
  1385. return ret;
  1386. }
  1387. /**
  1388. * try_to_munlock - try to munlock a page
  1389. * @page: the page to be munlocked
  1390. *
  1391. * Called from munlock code. Checks all of the VMAs mapping the page
  1392. * to make sure nobody else has this page mlocked. The page will be
  1393. * returned with PG_mlocked cleared if no other vmas have it mlocked.
  1394. *
  1395. * Return values are:
  1396. *
  1397. * SWAP_AGAIN - no vma is holding page mlocked, or,
  1398. * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
  1399. * SWAP_FAIL - page cannot be located at present
  1400. * SWAP_MLOCK - page is now mlocked.
  1401. */
  1402. int try_to_munlock(struct page *page)
  1403. {
  1404. int ret;
  1405. struct rmap_walk_control rwc = {
  1406. .rmap_one = try_to_unmap_one,
  1407. .arg = (void *)TTU_MUNLOCK,
  1408. .done = page_not_mapped,
  1409. .anon_lock = page_lock_anon_vma_read,
  1410. };
  1411. VM_BUG_ON_PAGE(!PageLocked(page) || PageLRU(page), page);
  1412. ret = rmap_walk(page, &rwc);
  1413. return ret;
  1414. }
  1415. void __put_anon_vma(struct anon_vma *anon_vma)
  1416. {
  1417. struct anon_vma *root = anon_vma->root;
  1418. anon_vma_free(anon_vma);
  1419. if (root != anon_vma && atomic_dec_and_test(&root->refcount))
  1420. anon_vma_free(root);
  1421. }
  1422. static struct anon_vma *rmap_walk_anon_lock(struct page *page,
  1423. struct rmap_walk_control *rwc)
  1424. {
  1425. struct anon_vma *anon_vma;
  1426. if (rwc->anon_lock)
  1427. return rwc->anon_lock(page);
  1428. /*
  1429. * Note: remove_migration_ptes() cannot use page_lock_anon_vma_read()
  1430. * because that depends on page_mapped(); but not all its usages
  1431. * are holding mmap_sem. Users without mmap_sem are required to
  1432. * take a reference count to prevent the anon_vma disappearing
  1433. */
  1434. anon_vma = page_anon_vma(page);
  1435. if (!anon_vma)
  1436. return NULL;
  1437. anon_vma_lock_read(anon_vma);
  1438. return anon_vma;
  1439. }
  1440. /*
  1441. * rmap_walk_anon - do something to anonymous page using the object-based
  1442. * rmap method
  1443. * @page: the page to be handled
  1444. * @rwc: control variable according to each walk type
  1445. *
  1446. * Find all the mappings of a page using the mapping pointer and the vma chains
  1447. * contained in the anon_vma struct it points to.
  1448. *
  1449. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  1450. * where the page was found will be held for write. So, we won't recheck
  1451. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  1452. * LOCKED.
  1453. */
  1454. static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc)
  1455. {
  1456. struct anon_vma *anon_vma;
  1457. pgoff_t pgoff;
  1458. struct anon_vma_chain *avc;
  1459. int ret = SWAP_AGAIN;
  1460. anon_vma = rmap_walk_anon_lock(page, rwc);
  1461. if (!anon_vma)
  1462. return ret;
  1463. pgoff = page_to_pgoff(page);
  1464. anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
  1465. struct vm_area_struct *vma = avc->vma;
  1466. unsigned long address = vma_address(page, vma);
  1467. cond_resched();
  1468. if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
  1469. continue;
  1470. ret = rwc->rmap_one(page, vma, address, rwc->arg);
  1471. if (ret != SWAP_AGAIN)
  1472. break;
  1473. if (rwc->done && rwc->done(page))
  1474. break;
  1475. }
  1476. anon_vma_unlock_read(anon_vma);
  1477. return ret;
  1478. }
  1479. /*
  1480. * rmap_walk_file - do something to file page using the object-based rmap method
  1481. * @page: the page to be handled
  1482. * @rwc: control variable according to each walk type
  1483. *
  1484. * Find all the mappings of a page using the mapping pointer and the vma chains
  1485. * contained in the address_space struct it points to.
  1486. *
  1487. * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
  1488. * where the page was found will be held for write. So, we won't recheck
  1489. * vm_flags for that VMA. That should be OK, because that vma shouldn't be
  1490. * LOCKED.
  1491. */
  1492. static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc)
  1493. {
  1494. struct address_space *mapping = page->mapping;
  1495. pgoff_t pgoff;
  1496. struct vm_area_struct *vma;
  1497. int ret = SWAP_AGAIN;
  1498. /*
  1499. * The page lock not only makes sure that page->mapping cannot
  1500. * suddenly be NULLified by truncation, it makes sure that the
  1501. * structure at mapping cannot be freed and reused yet,
  1502. * so we can safely take mapping->i_mmap_rwsem.
  1503. */
  1504. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1505. if (!mapping)
  1506. return ret;
  1507. pgoff = page_to_pgoff(page);
  1508. i_mmap_lock_read(mapping);
  1509. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  1510. unsigned long address = vma_address(page, vma);
  1511. cond_resched();
  1512. if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
  1513. continue;
  1514. ret = rwc->rmap_one(page, vma, address, rwc->arg);
  1515. if (ret != SWAP_AGAIN)
  1516. goto done;
  1517. if (rwc->done && rwc->done(page))
  1518. goto done;
  1519. }
  1520. done:
  1521. i_mmap_unlock_read(mapping);
  1522. return ret;
  1523. }
  1524. int rmap_walk(struct page *page, struct rmap_walk_control *rwc)
  1525. {
  1526. if (unlikely(PageKsm(page)))
  1527. return rmap_walk_ksm(page, rwc);
  1528. else if (PageAnon(page))
  1529. return rmap_walk_anon(page, rwc);
  1530. else
  1531. return rmap_walk_file(page, rwc);
  1532. }
  1533. #ifdef CONFIG_HUGETLB_PAGE
  1534. /*
  1535. * The following three functions are for anonymous (private mapped) hugepages.
  1536. * Unlike common anonymous pages, anonymous hugepages have no accounting code
  1537. * and no lru code, because we handle hugepages differently from common pages.
  1538. */
  1539. static void __hugepage_set_anon_rmap(struct page *page,
  1540. struct vm_area_struct *vma, unsigned long address, int exclusive)
  1541. {
  1542. struct anon_vma *anon_vma = vma->anon_vma;
  1543. BUG_ON(!anon_vma);
  1544. if (PageAnon(page))
  1545. return;
  1546. if (!exclusive)
  1547. anon_vma = anon_vma->root;
  1548. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  1549. page->mapping = (struct address_space *) anon_vma;
  1550. page->index = linear_page_index(vma, address);
  1551. }
  1552. void hugepage_add_anon_rmap(struct page *page,
  1553. struct vm_area_struct *vma, unsigned long address)
  1554. {
  1555. struct anon_vma *anon_vma = vma->anon_vma;
  1556. int first;
  1557. BUG_ON(!PageLocked(page));
  1558. BUG_ON(!anon_vma);
  1559. /* address might be in next vma when migration races vma_adjust */
  1560. first = atomic_inc_and_test(&page->_mapcount);
  1561. if (first)
  1562. __hugepage_set_anon_rmap(page, vma, address, 0);
  1563. }
  1564. void hugepage_add_new_anon_rmap(struct page *page,
  1565. struct vm_area_struct *vma, unsigned long address)
  1566. {
  1567. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  1568. atomic_set(&page->_mapcount, 0);
  1569. __hugepage_set_anon_rmap(page, vma, address, 1);
  1570. }
  1571. #endif /* CONFIG_HUGETLB_PAGE */