rmap.c 51 KB

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