rmap.c 48 KB

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