rmap.c 54 KB

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