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