rmap.c 48 KB

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