rmap.c 55 KB

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