rmap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_RMAP_H
  3. #define _LINUX_RMAP_H
  4. /*
  5. * Declarations for Reverse Mapping functions in mm/rmap.c
  6. */
  7. #include <linux/list.h>
  8. #include <linux/slab.h>
  9. #include <linux/mm.h>
  10. #include <linux/rwsem.h>
  11. #include <linux/memcontrol.h>
  12. #include <linux/highmem.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/memremap.h>
  15. /*
  16. * The anon_vma heads a list of private "related" vmas, to scan if
  17. * an anonymous page pointing to this anon_vma needs to be unmapped:
  18. * the vmas on the list will be related by forking, or by splitting.
  19. *
  20. * Since vmas come and go as they are split and merged (particularly
  21. * in mprotect), the mapping field of an anonymous page cannot point
  22. * directly to a vma: instead it points to an anon_vma, on whose list
  23. * the related vmas can be easily linked or unlinked.
  24. *
  25. * After unlinking the last vma on the list, we must garbage collect
  26. * the anon_vma object itself: we're guaranteed no page can be
  27. * pointing to this anon_vma once its vma list is empty.
  28. */
  29. struct anon_vma {
  30. struct anon_vma *root; /* Root of this anon_vma tree */
  31. struct rw_semaphore rwsem; /* W: modification, R: walking the list */
  32. /*
  33. * The refcount is taken on an anon_vma when there is no
  34. * guarantee that the vma of page tables will exist for
  35. * the duration of the operation. A caller that takes
  36. * the reference is responsible for clearing up the
  37. * anon_vma if they are the last user on release
  38. */
  39. atomic_t refcount;
  40. /*
  41. * Count of child anon_vmas. Equals to the count of all anon_vmas that
  42. * have ->parent pointing to this one, including itself.
  43. *
  44. * This counter is used for making decision about reusing anon_vma
  45. * instead of forking new one. See comments in function anon_vma_clone.
  46. */
  47. unsigned long num_children;
  48. /* Count of VMAs whose ->anon_vma pointer points to this object. */
  49. unsigned long num_active_vmas;
  50. struct anon_vma *parent; /* Parent of this anon_vma */
  51. /*
  52. * NOTE: the LSB of the rb_root.rb_node is set by
  53. * mm_take_all_locks() _after_ taking the above lock. So the
  54. * rb_root must only be read/written after taking the above lock
  55. * to be sure to see a valid next pointer. The LSB bit itself
  56. * is serialized by a system wide lock only visible to
  57. * mm_take_all_locks() (mm_all_locks_mutex).
  58. */
  59. /* Interval tree of private "related" vmas */
  60. struct rb_root_cached rb_root;
  61. };
  62. /*
  63. * The copy-on-write semantics of fork mean that an anon_vma
  64. * can become associated with multiple processes. Furthermore,
  65. * each child process will have its own anon_vma, where new
  66. * pages for that process are instantiated.
  67. *
  68. * This structure allows us to find the anon_vmas associated
  69. * with a VMA, or the VMAs associated with an anon_vma.
  70. * The "same_vma" list contains the anon_vma_chains linking
  71. * all the anon_vmas associated with this VMA.
  72. * The "rb" field indexes on an interval tree the anon_vma_chains
  73. * which link all the VMAs associated with this anon_vma.
  74. */
  75. struct anon_vma_chain {
  76. struct vm_area_struct *vma;
  77. struct anon_vma *anon_vma;
  78. struct list_head same_vma; /* locked by mmap_lock & page_table_lock */
  79. struct rb_node rb; /* locked by anon_vma->rwsem */
  80. unsigned long rb_subtree_last;
  81. #ifdef CONFIG_DEBUG_VM_RB
  82. unsigned long cached_vma_start, cached_vma_last;
  83. #endif
  84. };
  85. enum ttu_flags {
  86. TTU_SPLIT_HUGE_PMD = 0x4, /* split huge PMD if any */
  87. TTU_IGNORE_MLOCK = 0x8, /* ignore mlock */
  88. TTU_SYNC = 0x10, /* avoid racy checks with PVMW_SYNC */
  89. TTU_HWPOISON = 0x20, /* do convert pte to hwpoison entry */
  90. TTU_BATCH_FLUSH = 0x40, /* Batch TLB flushes where possible
  91. * and caller guarantees they will
  92. * do a final flush if necessary */
  93. TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock:
  94. * caller holds it */
  95. };
  96. #ifdef CONFIG_MMU
  97. static inline void get_anon_vma(struct anon_vma *anon_vma)
  98. {
  99. atomic_inc(&anon_vma->refcount);
  100. }
  101. void __put_anon_vma(struct anon_vma *anon_vma);
  102. static inline void put_anon_vma(struct anon_vma *anon_vma)
  103. {
  104. if (atomic_dec_and_test(&anon_vma->refcount))
  105. __put_anon_vma(anon_vma);
  106. }
  107. static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
  108. {
  109. down_write(&anon_vma->root->rwsem);
  110. }
  111. static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
  112. {
  113. up_write(&anon_vma->root->rwsem);
  114. }
  115. static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
  116. {
  117. down_read(&anon_vma->root->rwsem);
  118. }
  119. static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
  120. {
  121. return down_read_trylock(&anon_vma->root->rwsem);
  122. }
  123. static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
  124. {
  125. up_read(&anon_vma->root->rwsem);
  126. }
  127. /*
  128. * anon_vma helper functions.
  129. */
  130. void anon_vma_init(void); /* create anon_vma_cachep */
  131. int __anon_vma_prepare(struct vm_area_struct *);
  132. void unlink_anon_vmas(struct vm_area_struct *);
  133. int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
  134. int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
  135. static inline int anon_vma_prepare(struct vm_area_struct *vma)
  136. {
  137. if (likely(vma->anon_vma))
  138. return 0;
  139. return __anon_vma_prepare(vma);
  140. }
  141. static inline void anon_vma_merge(struct vm_area_struct *vma,
  142. struct vm_area_struct *next)
  143. {
  144. VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
  145. unlink_anon_vmas(next);
  146. }
  147. struct anon_vma *folio_get_anon_vma(struct folio *folio);
  148. /* RMAP flags, currently only relevant for some anon rmap operations. */
  149. typedef int __bitwise rmap_t;
  150. /*
  151. * No special request: if the page is a subpage of a compound page, it is
  152. * mapped via a PTE. The mapped (sub)page is possibly shared between processes.
  153. */
  154. #define RMAP_NONE ((__force rmap_t)0)
  155. /* The (sub)page is exclusive to a single process. */
  156. #define RMAP_EXCLUSIVE ((__force rmap_t)BIT(0))
  157. /*
  158. * The compound page is not mapped via PTEs, but instead via a single PMD and
  159. * should be accounted accordingly.
  160. */
  161. #define RMAP_COMPOUND ((__force rmap_t)BIT(1))
  162. /*
  163. * rmap interfaces called when adding or removing pte of page
  164. */
  165. void page_move_anon_rmap(struct page *, struct vm_area_struct *);
  166. void page_add_anon_rmap(struct page *, struct vm_area_struct *,
  167. unsigned long address, rmap_t flags);
  168. void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
  169. unsigned long address);
  170. void page_add_file_rmap(struct page *, struct vm_area_struct *,
  171. bool compound);
  172. void page_remove_rmap(struct page *, struct vm_area_struct *,
  173. bool compound);
  174. void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *,
  175. unsigned long address, rmap_t flags);
  176. void hugepage_add_new_anon_rmap(struct page *, struct vm_area_struct *,
  177. unsigned long address);
  178. static inline void __page_dup_rmap(struct page *page, bool compound)
  179. {
  180. atomic_inc(compound ? compound_mapcount_ptr(page) : &page->_mapcount);
  181. }
  182. static inline void page_dup_file_rmap(struct page *page, bool compound)
  183. {
  184. __page_dup_rmap(page, compound);
  185. }
  186. /**
  187. * page_try_dup_anon_rmap - try duplicating a mapping of an already mapped
  188. * anonymous page
  189. * @page: the page to duplicate the mapping for
  190. * @compound: the page is mapped as compound or as a small page
  191. * @vma: the source vma
  192. *
  193. * The caller needs to hold the PT lock and the vma->vma_mm->write_protect_seq.
  194. *
  195. * Duplicating the mapping can only fail if the page may be pinned; device
  196. * private pages cannot get pinned and consequently this function cannot fail.
  197. *
  198. * If duplicating the mapping succeeds, the page has to be mapped R/O into
  199. * the parent and the child. It must *not* get mapped writable after this call.
  200. *
  201. * Returns 0 if duplicating the mapping succeeded. Returns -EBUSY otherwise.
  202. */
  203. static inline int page_try_dup_anon_rmap(struct page *page, bool compound,
  204. struct vm_area_struct *vma)
  205. {
  206. VM_BUG_ON_PAGE(!PageAnon(page), page);
  207. /*
  208. * No need to check+clear for already shared pages, including KSM
  209. * pages.
  210. */
  211. if (!PageAnonExclusive(page))
  212. goto dup;
  213. /*
  214. * If this page may have been pinned by the parent process,
  215. * don't allow to duplicate the mapping but instead require to e.g.,
  216. * copy the page immediately for the child so that we'll always
  217. * guarantee the pinned page won't be randomly replaced in the
  218. * future on write faults.
  219. */
  220. if (likely(!is_device_private_page(page) &&
  221. unlikely(page_needs_cow_for_dma(vma, page))))
  222. return -EBUSY;
  223. ClearPageAnonExclusive(page);
  224. /*
  225. * It's okay to share the anon page between both processes, mapping
  226. * the page R/O into both processes.
  227. */
  228. dup:
  229. __page_dup_rmap(page, compound);
  230. return 0;
  231. }
  232. /**
  233. * page_try_share_anon_rmap - try marking an exclusive anonymous page possibly
  234. * shared to prepare for KSM or temporary unmapping
  235. * @page: the exclusive anonymous page to try marking possibly shared
  236. *
  237. * The caller needs to hold the PT lock and has to have the page table entry
  238. * cleared/invalidated.
  239. *
  240. * This is similar to page_try_dup_anon_rmap(), however, not used during fork()
  241. * to duplicate a mapping, but instead to prepare for KSM or temporarily
  242. * unmapping a page (swap, migration) via page_remove_rmap().
  243. *
  244. * Marking the page shared can only fail if the page may be pinned; device
  245. * private pages cannot get pinned and consequently this function cannot fail.
  246. *
  247. * Returns 0 if marking the page possibly shared succeeded. Returns -EBUSY
  248. * otherwise.
  249. */
  250. static inline int page_try_share_anon_rmap(struct page *page)
  251. {
  252. VM_BUG_ON_PAGE(!PageAnon(page) || !PageAnonExclusive(page), page);
  253. /* device private pages cannot get pinned via GUP. */
  254. if (unlikely(is_device_private_page(page))) {
  255. ClearPageAnonExclusive(page);
  256. return 0;
  257. }
  258. /*
  259. * We have to make sure that when we clear PageAnonExclusive, that
  260. * the page is not pinned and that concurrent GUP-fast won't succeed in
  261. * concurrently pinning the page.
  262. *
  263. * Conceptually, PageAnonExclusive clearing consists of:
  264. * (A1) Clear PTE
  265. * (A2) Check if the page is pinned; back off if so.
  266. * (A3) Clear PageAnonExclusive
  267. * (A4) Restore PTE (optional, but certainly not writable)
  268. *
  269. * When clearing PageAnonExclusive, we cannot possibly map the page
  270. * writable again, because anon pages that may be shared must never
  271. * be writable. So in any case, if the PTE was writable it cannot
  272. * be writable anymore afterwards and there would be a PTE change. Only
  273. * if the PTE wasn't writable, there might not be a PTE change.
  274. *
  275. * Conceptually, GUP-fast pinning of an anon page consists of:
  276. * (B1) Read the PTE
  277. * (B2) FOLL_WRITE: check if the PTE is not writable; back off if so.
  278. * (B3) Pin the mapped page
  279. * (B4) Check if the PTE changed by re-reading it; back off if so.
  280. * (B5) If the original PTE is not writable, check if
  281. * PageAnonExclusive is not set; back off if so.
  282. *
  283. * If the PTE was writable, we only have to make sure that GUP-fast
  284. * observes a PTE change and properly backs off.
  285. *
  286. * If the PTE was not writable, we have to make sure that GUP-fast either
  287. * detects a (temporary) PTE change or that PageAnonExclusive is cleared
  288. * and properly backs off.
  289. *
  290. * Consequently, when clearing PageAnonExclusive(), we have to make
  291. * sure that (A1), (A2)/(A3) and (A4) happen in the right memory
  292. * order. In GUP-fast pinning code, we have to make sure that (B3),(B4)
  293. * and (B5) happen in the right memory order.
  294. *
  295. * We assume that there might not be a memory barrier after
  296. * clearing/invalidating the PTE (A1) and before restoring the PTE (A4),
  297. * so we use explicit ones here.
  298. */
  299. /* Paired with the memory barrier in try_grab_folio(). */
  300. if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
  301. smp_mb();
  302. if (unlikely(page_maybe_dma_pinned(page)))
  303. return -EBUSY;
  304. ClearPageAnonExclusive(page);
  305. /*
  306. * This is conceptually a smp_wmb() paired with the smp_rmb() in
  307. * gup_must_unshare().
  308. */
  309. if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
  310. smp_mb__after_atomic();
  311. return 0;
  312. }
  313. /*
  314. * Called from mm/vmscan.c to handle paging out
  315. */
  316. int folio_referenced(struct folio *, int is_locked,
  317. struct mem_cgroup *memcg, unsigned long *vm_flags);
  318. void try_to_migrate(struct folio *folio, enum ttu_flags flags);
  319. void try_to_unmap(struct folio *, enum ttu_flags flags);
  320. int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
  321. unsigned long end, struct page **pages,
  322. void *arg);
  323. /* Avoid racy checks */
  324. #define PVMW_SYNC (1 << 0)
  325. /* Look for migration entries rather than present PTEs */
  326. #define PVMW_MIGRATION (1 << 1)
  327. struct page_vma_mapped_walk {
  328. unsigned long pfn;
  329. unsigned long nr_pages;
  330. pgoff_t pgoff;
  331. struct vm_area_struct *vma;
  332. unsigned long address;
  333. pmd_t *pmd;
  334. pte_t *pte;
  335. spinlock_t *ptl;
  336. unsigned int flags;
  337. };
  338. #define DEFINE_PAGE_VMA_WALK(name, _page, _vma, _address, _flags) \
  339. struct page_vma_mapped_walk name = { \
  340. .pfn = page_to_pfn(_page), \
  341. .nr_pages = compound_nr(_page), \
  342. .pgoff = page_to_pgoff(_page), \
  343. .vma = _vma, \
  344. .address = _address, \
  345. .flags = _flags, \
  346. }
  347. #define DEFINE_FOLIO_VMA_WALK(name, _folio, _vma, _address, _flags) \
  348. struct page_vma_mapped_walk name = { \
  349. .pfn = folio_pfn(_folio), \
  350. .nr_pages = folio_nr_pages(_folio), \
  351. .pgoff = folio_pgoff(_folio), \
  352. .vma = _vma, \
  353. .address = _address, \
  354. .flags = _flags, \
  355. }
  356. static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
  357. {
  358. /* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
  359. if (pvmw->pte && !is_vm_hugetlb_page(pvmw->vma))
  360. pte_unmap(pvmw->pte);
  361. if (pvmw->ptl)
  362. spin_unlock(pvmw->ptl);
  363. }
  364. bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
  365. /*
  366. * Used by swapoff to help locate where page is expected in vma.
  367. */
  368. unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
  369. /*
  370. * Cleans the PTEs of shared mappings.
  371. * (and since clean PTEs should also be readonly, write protects them too)
  372. *
  373. * returns the number of cleaned PTEs.
  374. */
  375. int folio_mkclean(struct folio *);
  376. int pfn_mkclean_range(unsigned long pfn, unsigned long nr_pages, pgoff_t pgoff,
  377. struct vm_area_struct *vma);
  378. void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked);
  379. int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
  380. /*
  381. * rmap_walk_control: To control rmap traversing for specific needs
  382. *
  383. * arg: passed to rmap_one() and invalid_vma()
  384. * try_lock: bail out if the rmap lock is contended
  385. * contended: indicate the rmap traversal bailed out due to lock contention
  386. * rmap_one: executed on each vma where page is mapped
  387. * done: for checking traversing termination condition
  388. * anon_lock: for getting anon_lock by optimized way rather than default
  389. * invalid_vma: for skipping uninterested vma
  390. */
  391. struct rmap_walk_control {
  392. void *arg;
  393. bool try_lock;
  394. bool contended;
  395. /*
  396. * Return false if page table scanning in rmap_walk should be stopped.
  397. * Otherwise, return true.
  398. */
  399. bool (*rmap_one)(struct folio *folio, struct vm_area_struct *vma,
  400. unsigned long addr, void *arg);
  401. int (*done)(struct folio *folio);
  402. struct anon_vma *(*anon_lock)(struct folio *folio,
  403. struct rmap_walk_control *rwc);
  404. bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
  405. };
  406. void rmap_walk(struct folio *folio, struct rmap_walk_control *rwc);
  407. void rmap_walk_locked(struct folio *folio, struct rmap_walk_control *rwc);
  408. struct anon_vma *folio_lock_anon_vma_read(struct folio *folio,
  409. struct rmap_walk_control *rwc);
  410. #else /* !CONFIG_MMU */
  411. #define anon_vma_init() do {} while (0)
  412. #define anon_vma_prepare(vma) (0)
  413. #define anon_vma_link(vma) do {} while (0)
  414. static inline int folio_referenced(struct folio *folio, int is_locked,
  415. struct mem_cgroup *memcg,
  416. unsigned long *vm_flags)
  417. {
  418. *vm_flags = 0;
  419. return 0;
  420. }
  421. static inline void try_to_unmap(struct folio *folio, enum ttu_flags flags)
  422. {
  423. }
  424. static inline int folio_mkclean(struct folio *folio)
  425. {
  426. return 0;
  427. }
  428. #endif /* CONFIG_MMU */
  429. static inline int page_mkclean(struct page *page)
  430. {
  431. return folio_mkclean(page_folio(page));
  432. }
  433. #endif /* _LINUX_RMAP_H */