1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/pagewalk.h> 3 #include <linux/mm_inline.h> 4 #include <linux/hugetlb.h> 5 #include <linux/huge_mm.h> 6 #include <linux/mount.h> 7 #include <linux/ksm.h> 8 #include <linux/seq_file.h> 9 #include <linux/highmem.h> 10 #include <linux/ptrace.h> 11 #include <linux/slab.h> 12 #include <linux/pagemap.h> 13 #include <linux/mempolicy.h> 14 #include <linux/rmap.h> 15 #include <linux/swap.h> 16 #include <linux/sched/mm.h> 17 #include <linux/swapops.h> 18 #include <linux/mmu_notifier.h> 19 #include <linux/page_idle.h> 20 #include <linux/shmem_fs.h> 21 #include <linux/uaccess.h> 22 #include <linux/pkeys.h> 23 #include <linux/minmax.h> 24 #include <linux/overflow.h> 25 26 #include <asm/elf.h> 27 #include <asm/tlb.h> 28 #include <asm/tlbflush.h> 29 #include "internal.h" 30 31 #define SEQ_PUT_DEC(str, val) \ 32 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8) 33 void task_mem(struct seq_file *m, struct mm_struct *mm) 34 { 35 unsigned long text, lib, swap, anon, file, shmem; 36 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; 37 38 anon = get_mm_counter(mm, MM_ANONPAGES); 39 file = get_mm_counter(mm, MM_FILEPAGES); 40 shmem = get_mm_counter(mm, MM_SHMEMPAGES); 41 42 /* 43 * Note: to minimize their overhead, mm maintains hiwater_vm and 44 * hiwater_rss only when about to *lower* total_vm or rss. Any 45 * collector of these hiwater stats must therefore get total_vm 46 * and rss too, which will usually be the higher. Barriers? not 47 * worth the effort, such snapshots can always be inconsistent. 48 */ 49 hiwater_vm = total_vm = mm->total_vm; 50 if (hiwater_vm < mm->hiwater_vm) 51 hiwater_vm = mm->hiwater_vm; 52 hiwater_rss = total_rss = anon + file + shmem; 53 if (hiwater_rss < mm->hiwater_rss) 54 hiwater_rss = mm->hiwater_rss; 55 56 /* split executable areas between text and lib */ 57 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK); 58 text = min(text, mm->exec_vm << PAGE_SHIFT); 59 lib = (mm->exec_vm << PAGE_SHIFT) - text; 60 61 swap = get_mm_counter(mm, MM_SWAPENTS); 62 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm); 63 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm); 64 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm); 65 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm)); 66 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss); 67 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss); 68 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon); 69 SEQ_PUT_DEC(" kB\nRssFile:\t", file); 70 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem); 71 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm); 72 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm); 73 seq_put_decimal_ull_width(m, 74 " kB\nVmExe:\t", text >> 10, 8); 75 seq_put_decimal_ull_width(m, 76 " kB\nVmLib:\t", lib >> 10, 8); 77 seq_put_decimal_ull_width(m, 78 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8); 79 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap); 80 seq_puts(m, " kB\n"); 81 hugetlb_report_usage(m, mm); 82 } 83 #undef SEQ_PUT_DEC 84 85 unsigned long task_vsize(struct mm_struct *mm) 86 { 87 return PAGE_SIZE * mm->total_vm; 88 } 89 90 unsigned long task_statm(struct mm_struct *mm, 91 unsigned long *shared, unsigned long *text, 92 unsigned long *data, unsigned long *resident) 93 { 94 *shared = get_mm_counter(mm, MM_FILEPAGES) + 95 get_mm_counter(mm, MM_SHMEMPAGES); 96 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) 97 >> PAGE_SHIFT; 98 *data = mm->data_vm + mm->stack_vm; 99 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); 100 return mm->total_vm; 101 } 102 103 #ifdef CONFIG_NUMA 104 /* 105 * Save get_task_policy() for show_numa_map(). 106 */ 107 static void hold_task_mempolicy(struct proc_maps_private *priv) 108 { 109 struct task_struct *task = priv->task; 110 111 task_lock(task); 112 priv->task_mempolicy = get_task_policy(task); 113 mpol_get(priv->task_mempolicy); 114 task_unlock(task); 115 } 116 static void release_task_mempolicy(struct proc_maps_private *priv) 117 { 118 mpol_put(priv->task_mempolicy); 119 } 120 #else 121 static void hold_task_mempolicy(struct proc_maps_private *priv) 122 { 123 } 124 static void release_task_mempolicy(struct proc_maps_private *priv) 125 { 126 } 127 #endif 128 129 static struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv, 130 loff_t *ppos) 131 { 132 struct vm_area_struct *vma = vma_next(&priv->iter); 133 134 if (vma) { 135 *ppos = vma->vm_start; 136 } else { 137 *ppos = -2UL; 138 vma = get_gate_vma(priv->mm); 139 } 140 141 return vma; 142 } 143 144 static void *m_start(struct seq_file *m, loff_t *ppos) 145 { 146 struct proc_maps_private *priv = m->private; 147 unsigned long last_addr = *ppos; 148 struct mm_struct *mm; 149 150 /* See m_next(). Zero at the start or after lseek. */ 151 if (last_addr == -1UL) 152 return NULL; 153 154 priv->task = get_proc_task(priv->inode); 155 if (!priv->task) 156 return ERR_PTR(-ESRCH); 157 158 mm = priv->mm; 159 if (!mm || !mmget_not_zero(mm)) { 160 put_task_struct(priv->task); 161 priv->task = NULL; 162 return NULL; 163 } 164 165 if (mmap_read_lock_killable(mm)) { 166 mmput(mm); 167 put_task_struct(priv->task); 168 priv->task = NULL; 169 return ERR_PTR(-EINTR); 170 } 171 172 vma_iter_init(&priv->iter, mm, last_addr); 173 hold_task_mempolicy(priv); 174 if (last_addr == -2UL) 175 return get_gate_vma(mm); 176 177 return proc_get_vma(priv, ppos); 178 } 179 180 static void *m_next(struct seq_file *m, void *v, loff_t *ppos) 181 { 182 if (*ppos == -2UL) { 183 *ppos = -1UL; 184 return NULL; 185 } 186 return proc_get_vma(m->private, ppos); 187 } 188 189 static void m_stop(struct seq_file *m, void *v) 190 { 191 struct proc_maps_private *priv = m->private; 192 struct mm_struct *mm = priv->mm; 193 194 if (!priv->task) 195 return; 196 197 release_task_mempolicy(priv); 198 mmap_read_unlock(mm); 199 mmput(mm); 200 put_task_struct(priv->task); 201 priv->task = NULL; 202 } 203 204 static int proc_maps_open(struct inode *inode, struct file *file, 205 const struct seq_operations *ops, int psize) 206 { 207 struct proc_maps_private *priv = __seq_open_private(file, ops, psize); 208 209 if (!priv) 210 return -ENOMEM; 211 212 priv->inode = inode; 213 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 214 if (IS_ERR(priv->mm)) { 215 int err = PTR_ERR(priv->mm); 216 217 seq_release_private(inode, file); 218 return err; 219 } 220 221 return 0; 222 } 223 224 static int proc_map_release(struct inode *inode, struct file *file) 225 { 226 struct seq_file *seq = file->private_data; 227 struct proc_maps_private *priv = seq->private; 228 229 if (priv->mm) 230 mmdrop(priv->mm); 231 232 return seq_release_private(inode, file); 233 } 234 235 static int do_maps_open(struct inode *inode, struct file *file, 236 const struct seq_operations *ops) 237 { 238 return proc_maps_open(inode, file, ops, 239 sizeof(struct proc_maps_private)); 240 } 241 242 static void show_vma_header_prefix(struct seq_file *m, 243 unsigned long start, unsigned long end, 244 vm_flags_t flags, unsigned long long pgoff, 245 dev_t dev, unsigned long ino) 246 { 247 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); 248 seq_put_hex_ll(m, NULL, start, 8); 249 seq_put_hex_ll(m, "-", end, 8); 250 seq_putc(m, ' '); 251 seq_putc(m, flags & VM_READ ? 'r' : '-'); 252 seq_putc(m, flags & VM_WRITE ? 'w' : '-'); 253 seq_putc(m, flags & VM_EXEC ? 'x' : '-'); 254 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p'); 255 seq_put_hex_ll(m, " ", pgoff, 8); 256 seq_put_hex_ll(m, " ", MAJOR(dev), 2); 257 seq_put_hex_ll(m, ":", MINOR(dev), 2); 258 seq_put_decimal_ull(m, " ", ino); 259 seq_putc(m, ' '); 260 } 261 262 static void 263 show_map_vma(struct seq_file *m, struct vm_area_struct *vma) 264 { 265 struct anon_vma_name *anon_name = NULL; 266 struct mm_struct *mm = vma->vm_mm; 267 struct file *file = vma->vm_file; 268 vm_flags_t flags = vma->vm_flags; 269 unsigned long ino = 0; 270 unsigned long long pgoff = 0; 271 unsigned long start, end; 272 dev_t dev = 0; 273 const char *name = NULL; 274 275 if (file) { 276 const struct inode *inode = file_user_inode(vma->vm_file); 277 278 dev = inode->i_sb->s_dev; 279 ino = inode->i_ino; 280 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; 281 } 282 283 start = vma->vm_start; 284 end = vma->vm_end; 285 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino); 286 if (mm) 287 anon_name = anon_vma_name(vma); 288 289 /* 290 * Print the dentry name for named mappings, and a 291 * special [heap] marker for the heap: 292 */ 293 if (file) { 294 seq_pad(m, ' '); 295 /* 296 * If user named this anon shared memory via 297 * prctl(PR_SET_VMA ..., use the provided name. 298 */ 299 if (anon_name) 300 seq_printf(m, "[anon_shmem:%s]", anon_name->name); 301 else 302 seq_path(m, file_user_path(file), "\n"); 303 goto done; 304 } 305 306 if (vma->vm_ops && vma->vm_ops->name) { 307 name = vma->vm_ops->name(vma); 308 if (name) 309 goto done; 310 } 311 312 name = arch_vma_name(vma); 313 if (!name) { 314 if (!mm) { 315 name = "[vdso]"; 316 goto done; 317 } 318 319 if (vma_is_initial_heap(vma)) { 320 name = "[heap]"; 321 goto done; 322 } 323 324 if (vma_is_initial_stack(vma)) { 325 name = "[stack]"; 326 goto done; 327 } 328 329 if (anon_name) { 330 seq_pad(m, ' '); 331 seq_printf(m, "[anon:%s]", anon_name->name); 332 } 333 } 334 335 done: 336 if (name) { 337 seq_pad(m, ' '); 338 seq_puts(m, name); 339 } 340 seq_putc(m, '\n'); 341 } 342 343 static int show_map(struct seq_file *m, void *v) 344 { 345 show_map_vma(m, v); 346 return 0; 347 } 348 349 static const struct seq_operations proc_pid_maps_op = { 350 .start = m_start, 351 .next = m_next, 352 .stop = m_stop, 353 .show = show_map 354 }; 355 356 static int pid_maps_open(struct inode *inode, struct file *file) 357 { 358 return do_maps_open(inode, file, &proc_pid_maps_op); 359 } 360 361 const struct file_operations proc_pid_maps_operations = { 362 .open = pid_maps_open, 363 .read = seq_read, 364 .llseek = seq_lseek, 365 .release = proc_map_release, 366 }; 367 368 /* 369 * Proportional Set Size(PSS): my share of RSS. 370 * 371 * PSS of a process is the count of pages it has in memory, where each 372 * page is divided by the number of processes sharing it. So if a 373 * process has 1000 pages all to itself, and 1000 shared with one other 374 * process, its PSS will be 1500. 375 * 376 * To keep (accumulated) division errors low, we adopt a 64bit 377 * fixed-point pss counter to minimize division errors. So (pss >> 378 * PSS_SHIFT) would be the real byte count. 379 * 380 * A shift of 12 before division means (assuming 4K page size): 381 * - 1M 3-user-pages add up to 8KB errors; 382 * - supports mapcount up to 2^24, or 16M; 383 * - supports PSS up to 2^52 bytes, or 4PB. 384 */ 385 #define PSS_SHIFT 12 386 387 #ifdef CONFIG_PROC_PAGE_MONITOR 388 struct mem_size_stats { 389 unsigned long resident; 390 unsigned long shared_clean; 391 unsigned long shared_dirty; 392 unsigned long private_clean; 393 unsigned long private_dirty; 394 unsigned long referenced; 395 unsigned long anonymous; 396 unsigned long lazyfree; 397 unsigned long anonymous_thp; 398 unsigned long shmem_thp; 399 unsigned long file_thp; 400 unsigned long swap; 401 unsigned long shared_hugetlb; 402 unsigned long private_hugetlb; 403 unsigned long ksm; 404 u64 pss; 405 u64 pss_anon; 406 u64 pss_file; 407 u64 pss_shmem; 408 u64 pss_dirty; 409 u64 pss_locked; 410 u64 swap_pss; 411 }; 412 413 static void smaps_page_accumulate(struct mem_size_stats *mss, 414 struct folio *folio, unsigned long size, unsigned long pss, 415 bool dirty, bool locked, bool private) 416 { 417 mss->pss += pss; 418 419 if (folio_test_anon(folio)) 420 mss->pss_anon += pss; 421 else if (folio_test_swapbacked(folio)) 422 mss->pss_shmem += pss; 423 else 424 mss->pss_file += pss; 425 426 if (locked) 427 mss->pss_locked += pss; 428 429 if (dirty || folio_test_dirty(folio)) { 430 mss->pss_dirty += pss; 431 if (private) 432 mss->private_dirty += size; 433 else 434 mss->shared_dirty += size; 435 } else { 436 if (private) 437 mss->private_clean += size; 438 else 439 mss->shared_clean += size; 440 } 441 } 442 443 static void smaps_account(struct mem_size_stats *mss, struct page *page, 444 bool compound, bool young, bool dirty, bool locked, 445 bool migration) 446 { 447 struct folio *folio = page_folio(page); 448 int i, nr = compound ? compound_nr(page) : 1; 449 unsigned long size = nr * PAGE_SIZE; 450 451 /* 452 * First accumulate quantities that depend only on |size| and the type 453 * of the compound page. 454 */ 455 if (folio_test_anon(folio)) { 456 mss->anonymous += size; 457 if (!folio_test_swapbacked(folio) && !dirty && 458 !folio_test_dirty(folio)) 459 mss->lazyfree += size; 460 } 461 462 if (folio_test_ksm(folio)) 463 mss->ksm += size; 464 465 mss->resident += size; 466 /* Accumulate the size in pages that have been accessed. */ 467 if (young || folio_test_young(folio) || folio_test_referenced(folio)) 468 mss->referenced += size; 469 470 /* 471 * Then accumulate quantities that may depend on sharing, or that may 472 * differ page-by-page. 473 * 474 * refcount == 1 guarantees the page is mapped exactly once. 475 * If any subpage of the compound page mapped with PTE it would elevate 476 * the refcount. 477 * 478 * The page_mapcount() is called to get a snapshot of the mapcount. 479 * Without holding the page lock this snapshot can be slightly wrong as 480 * we cannot always read the mapcount atomically. It is not safe to 481 * call page_mapcount() even with PTL held if the page is not mapped, 482 * especially for migration entries. Treat regular migration entries 483 * as mapcount == 1. 484 */ 485 if ((folio_ref_count(folio) == 1) || migration) { 486 smaps_page_accumulate(mss, folio, size, size << PSS_SHIFT, 487 dirty, locked, true); 488 return; 489 } 490 for (i = 0; i < nr; i++, page++) { 491 int mapcount = page_mapcount(page); 492 unsigned long pss = PAGE_SIZE << PSS_SHIFT; 493 if (mapcount >= 2) 494 pss /= mapcount; 495 smaps_page_accumulate(mss, folio, PAGE_SIZE, pss, 496 dirty, locked, mapcount < 2); 497 } 498 } 499 500 #ifdef CONFIG_SHMEM 501 static int smaps_pte_hole(unsigned long addr, unsigned long end, 502 __always_unused int depth, struct mm_walk *walk) 503 { 504 struct mem_size_stats *mss = walk->private; 505 struct vm_area_struct *vma = walk->vma; 506 507 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping, 508 linear_page_index(vma, addr), 509 linear_page_index(vma, end)); 510 511 return 0; 512 } 513 #else 514 #define smaps_pte_hole NULL 515 #endif /* CONFIG_SHMEM */ 516 517 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk) 518 { 519 #ifdef CONFIG_SHMEM 520 if (walk->ops->pte_hole) { 521 /* depth is not used */ 522 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk); 523 } 524 #endif 525 } 526 527 static void smaps_pte_entry(pte_t *pte, unsigned long addr, 528 struct mm_walk *walk) 529 { 530 struct mem_size_stats *mss = walk->private; 531 struct vm_area_struct *vma = walk->vma; 532 bool locked = !!(vma->vm_flags & VM_LOCKED); 533 struct page *page = NULL; 534 bool migration = false, young = false, dirty = false; 535 pte_t ptent = ptep_get(pte); 536 537 if (pte_present(ptent)) { 538 page = vm_normal_page(vma, addr, ptent); 539 young = pte_young(ptent); 540 dirty = pte_dirty(ptent); 541 } else if (is_swap_pte(ptent)) { 542 swp_entry_t swpent = pte_to_swp_entry(ptent); 543 544 if (!non_swap_entry(swpent)) { 545 int mapcount; 546 547 mss->swap += PAGE_SIZE; 548 mapcount = swp_swapcount(swpent); 549 if (mapcount >= 2) { 550 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT; 551 552 do_div(pss_delta, mapcount); 553 mss->swap_pss += pss_delta; 554 } else { 555 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT; 556 } 557 } else if (is_pfn_swap_entry(swpent)) { 558 if (is_migration_entry(swpent)) 559 migration = true; 560 page = pfn_swap_entry_to_page(swpent); 561 } 562 } else { 563 smaps_pte_hole_lookup(addr, walk); 564 return; 565 } 566 567 if (!page) 568 return; 569 570 smaps_account(mss, page, false, young, dirty, locked, migration); 571 } 572 573 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 574 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 575 struct mm_walk *walk) 576 { 577 struct mem_size_stats *mss = walk->private; 578 struct vm_area_struct *vma = walk->vma; 579 bool locked = !!(vma->vm_flags & VM_LOCKED); 580 struct page *page = NULL; 581 struct folio *folio; 582 bool migration = false; 583 584 if (pmd_present(*pmd)) { 585 page = vm_normal_page_pmd(vma, addr, *pmd); 586 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) { 587 swp_entry_t entry = pmd_to_swp_entry(*pmd); 588 589 if (is_migration_entry(entry)) { 590 migration = true; 591 page = pfn_swap_entry_to_page(entry); 592 } 593 } 594 if (IS_ERR_OR_NULL(page)) 595 return; 596 folio = page_folio(page); 597 if (folio_test_anon(folio)) 598 mss->anonymous_thp += HPAGE_PMD_SIZE; 599 else if (folio_test_swapbacked(folio)) 600 mss->shmem_thp += HPAGE_PMD_SIZE; 601 else if (folio_is_zone_device(folio)) 602 /* pass */; 603 else 604 mss->file_thp += HPAGE_PMD_SIZE; 605 606 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), 607 locked, migration); 608 } 609 #else 610 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 611 struct mm_walk *walk) 612 { 613 } 614 #endif 615 616 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 617 struct mm_walk *walk) 618 { 619 struct vm_area_struct *vma = walk->vma; 620 pte_t *pte; 621 spinlock_t *ptl; 622 623 ptl = pmd_trans_huge_lock(pmd, vma); 624 if (ptl) { 625 smaps_pmd_entry(pmd, addr, walk); 626 spin_unlock(ptl); 627 goto out; 628 } 629 630 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 631 if (!pte) { 632 walk->action = ACTION_AGAIN; 633 return 0; 634 } 635 for (; addr != end; pte++, addr += PAGE_SIZE) 636 smaps_pte_entry(pte, addr, walk); 637 pte_unmap_unlock(pte - 1, ptl); 638 out: 639 cond_resched(); 640 return 0; 641 } 642 643 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) 644 { 645 /* 646 * Don't forget to update Documentation/ on changes. 647 */ 648 static const char mnemonics[BITS_PER_LONG][2] = { 649 /* 650 * In case if we meet a flag we don't know about. 651 */ 652 [0 ... (BITS_PER_LONG-1)] = "??", 653 654 [ilog2(VM_READ)] = "rd", 655 [ilog2(VM_WRITE)] = "wr", 656 [ilog2(VM_EXEC)] = "ex", 657 [ilog2(VM_SHARED)] = "sh", 658 [ilog2(VM_MAYREAD)] = "mr", 659 [ilog2(VM_MAYWRITE)] = "mw", 660 [ilog2(VM_MAYEXEC)] = "me", 661 [ilog2(VM_MAYSHARE)] = "ms", 662 [ilog2(VM_GROWSDOWN)] = "gd", 663 [ilog2(VM_PFNMAP)] = "pf", 664 [ilog2(VM_LOCKED)] = "lo", 665 [ilog2(VM_IO)] = "io", 666 [ilog2(VM_SEQ_READ)] = "sr", 667 [ilog2(VM_RAND_READ)] = "rr", 668 [ilog2(VM_DONTCOPY)] = "dc", 669 [ilog2(VM_DONTEXPAND)] = "de", 670 [ilog2(VM_LOCKONFAULT)] = "lf", 671 [ilog2(VM_ACCOUNT)] = "ac", 672 [ilog2(VM_NORESERVE)] = "nr", 673 [ilog2(VM_HUGETLB)] = "ht", 674 [ilog2(VM_SYNC)] = "sf", 675 [ilog2(VM_ARCH_1)] = "ar", 676 [ilog2(VM_WIPEONFORK)] = "wf", 677 [ilog2(VM_DONTDUMP)] = "dd", 678 #ifdef CONFIG_ARM64_BTI 679 [ilog2(VM_ARM64_BTI)] = "bt", 680 #endif 681 #ifdef CONFIG_MEM_SOFT_DIRTY 682 [ilog2(VM_SOFTDIRTY)] = "sd", 683 #endif 684 [ilog2(VM_MIXEDMAP)] = "mm", 685 [ilog2(VM_HUGEPAGE)] = "hg", 686 [ilog2(VM_NOHUGEPAGE)] = "nh", 687 [ilog2(VM_MERGEABLE)] = "mg", 688 [ilog2(VM_UFFD_MISSING)]= "um", 689 [ilog2(VM_UFFD_WP)] = "uw", 690 #ifdef CONFIG_ARM64_MTE 691 [ilog2(VM_MTE)] = "mt", 692 [ilog2(VM_MTE_ALLOWED)] = "", 693 #endif 694 #ifdef CONFIG_ARCH_HAS_PKEYS 695 /* These come out via ProtectionKey: */ 696 [ilog2(VM_PKEY_BIT0)] = "", 697 [ilog2(VM_PKEY_BIT1)] = "", 698 [ilog2(VM_PKEY_BIT2)] = "", 699 [ilog2(VM_PKEY_BIT3)] = "", 700 #if VM_PKEY_BIT4 701 [ilog2(VM_PKEY_BIT4)] = "", 702 #endif 703 #endif /* CONFIG_ARCH_HAS_PKEYS */ 704 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 705 [ilog2(VM_UFFD_MINOR)] = "ui", 706 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ 707 #ifdef CONFIG_X86_USER_SHADOW_STACK 708 [ilog2(VM_SHADOW_STACK)] = "ss", 709 #endif 710 #ifdef CONFIG_64BIT 711 [ilog2(VM_SEALED)] = "sl", 712 #endif 713 }; 714 size_t i; 715 716 seq_puts(m, "VmFlags: "); 717 for (i = 0; i < BITS_PER_LONG; i++) { 718 if (!mnemonics[i][0]) 719 continue; 720 if (vma->vm_flags & (1UL << i)) { 721 seq_putc(m, mnemonics[i][0]); 722 seq_putc(m, mnemonics[i][1]); 723 seq_putc(m, ' '); 724 } 725 } 726 seq_putc(m, '\n'); 727 } 728 729 #ifdef CONFIG_HUGETLB_PAGE 730 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, 731 unsigned long addr, unsigned long end, 732 struct mm_walk *walk) 733 { 734 struct mem_size_stats *mss = walk->private; 735 struct vm_area_struct *vma = walk->vma; 736 pte_t ptent = huge_ptep_get(pte); 737 struct folio *folio = NULL; 738 739 if (pte_present(ptent)) { 740 folio = page_folio(pte_page(ptent)); 741 } else if (is_swap_pte(ptent)) { 742 swp_entry_t swpent = pte_to_swp_entry(ptent); 743 744 if (is_pfn_swap_entry(swpent)) 745 folio = pfn_swap_entry_folio(swpent); 746 } 747 if (folio) { 748 if (folio_likely_mapped_shared(folio) || 749 hugetlb_pmd_shared(pte)) 750 mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); 751 else 752 mss->private_hugetlb += huge_page_size(hstate_vma(vma)); 753 } 754 return 0; 755 } 756 #else 757 #define smaps_hugetlb_range NULL 758 #endif /* HUGETLB_PAGE */ 759 760 static const struct mm_walk_ops smaps_walk_ops = { 761 .pmd_entry = smaps_pte_range, 762 .hugetlb_entry = smaps_hugetlb_range, 763 .walk_lock = PGWALK_RDLOCK, 764 }; 765 766 static const struct mm_walk_ops smaps_shmem_walk_ops = { 767 .pmd_entry = smaps_pte_range, 768 .hugetlb_entry = smaps_hugetlb_range, 769 .pte_hole = smaps_pte_hole, 770 .walk_lock = PGWALK_RDLOCK, 771 }; 772 773 /* 774 * Gather mem stats from @vma with the indicated beginning 775 * address @start, and keep them in @mss. 776 * 777 * Use vm_start of @vma as the beginning address if @start is 0. 778 */ 779 static void smap_gather_stats(struct vm_area_struct *vma, 780 struct mem_size_stats *mss, unsigned long start) 781 { 782 const struct mm_walk_ops *ops = &smaps_walk_ops; 783 784 /* Invalid start */ 785 if (start >= vma->vm_end) 786 return; 787 788 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) { 789 /* 790 * For shared or readonly shmem mappings we know that all 791 * swapped out pages belong to the shmem object, and we can 792 * obtain the swap value much more efficiently. For private 793 * writable mappings, we might have COW pages that are 794 * not affected by the parent swapped out pages of the shmem 795 * object, so we have to distinguish them during the page walk. 796 * Unless we know that the shmem object (or the part mapped by 797 * our VMA) has no swapped out pages at all. 798 */ 799 unsigned long shmem_swapped = shmem_swap_usage(vma); 800 801 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) || 802 !(vma->vm_flags & VM_WRITE))) { 803 mss->swap += shmem_swapped; 804 } else { 805 ops = &smaps_shmem_walk_ops; 806 } 807 } 808 809 /* mmap_lock is held in m_start */ 810 if (!start) 811 walk_page_vma(vma, ops, mss); 812 else 813 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss); 814 } 815 816 #define SEQ_PUT_DEC(str, val) \ 817 seq_put_decimal_ull_width(m, str, (val) >> 10, 8) 818 819 /* Show the contents common for smaps and smaps_rollup */ 820 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss, 821 bool rollup_mode) 822 { 823 SEQ_PUT_DEC("Rss: ", mss->resident); 824 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT); 825 SEQ_PUT_DEC(" kB\nPss_Dirty: ", mss->pss_dirty >> PSS_SHIFT); 826 if (rollup_mode) { 827 /* 828 * These are meaningful only for smaps_rollup, otherwise two of 829 * them are zero, and the other one is the same as Pss. 830 */ 831 SEQ_PUT_DEC(" kB\nPss_Anon: ", 832 mss->pss_anon >> PSS_SHIFT); 833 SEQ_PUT_DEC(" kB\nPss_File: ", 834 mss->pss_file >> PSS_SHIFT); 835 SEQ_PUT_DEC(" kB\nPss_Shmem: ", 836 mss->pss_shmem >> PSS_SHIFT); 837 } 838 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean); 839 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty); 840 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean); 841 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); 842 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); 843 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); 844 SEQ_PUT_DEC(" kB\nKSM: ", mss->ksm); 845 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); 846 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); 847 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp); 848 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp); 849 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb); 850 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ", 851 mss->private_hugetlb >> 10, 7); 852 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap); 853 SEQ_PUT_DEC(" kB\nSwapPss: ", 854 mss->swap_pss >> PSS_SHIFT); 855 SEQ_PUT_DEC(" kB\nLocked: ", 856 mss->pss_locked >> PSS_SHIFT); 857 seq_puts(m, " kB\n"); 858 } 859 860 static int show_smap(struct seq_file *m, void *v) 861 { 862 struct vm_area_struct *vma = v; 863 struct mem_size_stats mss = {}; 864 865 smap_gather_stats(vma, &mss, 0); 866 867 show_map_vma(m, vma); 868 869 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start); 870 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma)); 871 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma)); 872 seq_puts(m, " kB\n"); 873 874 __show_smap(m, &mss, false); 875 876 seq_printf(m, "THPeligible: %8u\n", 877 !!thp_vma_allowable_orders(vma, vma->vm_flags, 878 TVA_SMAPS | TVA_ENFORCE_SYSFS, THP_ORDERS_ALL)); 879 880 if (arch_pkeys_enabled()) 881 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); 882 show_smap_vma_flags(m, vma); 883 884 return 0; 885 } 886 887 static int show_smaps_rollup(struct seq_file *m, void *v) 888 { 889 struct proc_maps_private *priv = m->private; 890 struct mem_size_stats mss = {}; 891 struct mm_struct *mm = priv->mm; 892 struct vm_area_struct *vma; 893 unsigned long vma_start = 0, last_vma_end = 0; 894 int ret = 0; 895 VMA_ITERATOR(vmi, mm, 0); 896 897 priv->task = get_proc_task(priv->inode); 898 if (!priv->task) 899 return -ESRCH; 900 901 if (!mm || !mmget_not_zero(mm)) { 902 ret = -ESRCH; 903 goto out_put_task; 904 } 905 906 ret = mmap_read_lock_killable(mm); 907 if (ret) 908 goto out_put_mm; 909 910 hold_task_mempolicy(priv); 911 vma = vma_next(&vmi); 912 913 if (unlikely(!vma)) 914 goto empty_set; 915 916 vma_start = vma->vm_start; 917 do { 918 smap_gather_stats(vma, &mss, 0); 919 last_vma_end = vma->vm_end; 920 921 /* 922 * Release mmap_lock temporarily if someone wants to 923 * access it for write request. 924 */ 925 if (mmap_lock_is_contended(mm)) { 926 vma_iter_invalidate(&vmi); 927 mmap_read_unlock(mm); 928 ret = mmap_read_lock_killable(mm); 929 if (ret) { 930 release_task_mempolicy(priv); 931 goto out_put_mm; 932 } 933 934 /* 935 * After dropping the lock, there are four cases to 936 * consider. See the following example for explanation. 937 * 938 * +------+------+-----------+ 939 * | VMA1 | VMA2 | VMA3 | 940 * +------+------+-----------+ 941 * | | | | 942 * 4k 8k 16k 400k 943 * 944 * Suppose we drop the lock after reading VMA2 due to 945 * contention, then we get: 946 * 947 * last_vma_end = 16k 948 * 949 * 1) VMA2 is freed, but VMA3 exists: 950 * 951 * vma_next(vmi) will return VMA3. 952 * In this case, just continue from VMA3. 953 * 954 * 2) VMA2 still exists: 955 * 956 * vma_next(vmi) will return VMA3. 957 * In this case, just continue from VMA3. 958 * 959 * 3) No more VMAs can be found: 960 * 961 * vma_next(vmi) will return NULL. 962 * No more things to do, just break. 963 * 964 * 4) (last_vma_end - 1) is the middle of a vma (VMA'): 965 * 966 * vma_next(vmi) will return VMA' whose range 967 * contains last_vma_end. 968 * Iterate VMA' from last_vma_end. 969 */ 970 vma = vma_next(&vmi); 971 /* Case 3 above */ 972 if (!vma) 973 break; 974 975 /* Case 1 and 2 above */ 976 if (vma->vm_start >= last_vma_end) { 977 smap_gather_stats(vma, &mss, 0); 978 last_vma_end = vma->vm_end; 979 continue; 980 } 981 982 /* Case 4 above */ 983 if (vma->vm_end > last_vma_end) { 984 smap_gather_stats(vma, &mss, last_vma_end); 985 last_vma_end = vma->vm_end; 986 } 987 } 988 } for_each_vma(vmi, vma); 989 990 empty_set: 991 show_vma_header_prefix(m, vma_start, last_vma_end, 0, 0, 0, 0); 992 seq_pad(m, ' '); 993 seq_puts(m, "[rollup]\n"); 994 995 __show_smap(m, &mss, true); 996 997 release_task_mempolicy(priv); 998 mmap_read_unlock(mm); 999 1000 out_put_mm: 1001 mmput(mm); 1002 out_put_task: 1003 put_task_struct(priv->task); 1004 priv->task = NULL; 1005 1006 return ret; 1007 } 1008 #undef SEQ_PUT_DEC 1009 1010 static const struct seq_operations proc_pid_smaps_op = { 1011 .start = m_start, 1012 .next = m_next, 1013 .stop = m_stop, 1014 .show = show_smap 1015 }; 1016 1017 static int pid_smaps_open(struct inode *inode, struct file *file) 1018 { 1019 return do_maps_open(inode, file, &proc_pid_smaps_op); 1020 } 1021 1022 static int smaps_rollup_open(struct inode *inode, struct file *file) 1023 { 1024 int ret; 1025 struct proc_maps_private *priv; 1026 1027 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT); 1028 if (!priv) 1029 return -ENOMEM; 1030 1031 ret = single_open(file, show_smaps_rollup, priv); 1032 if (ret) 1033 goto out_free; 1034 1035 priv->inode = inode; 1036 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 1037 if (IS_ERR(priv->mm)) { 1038 ret = PTR_ERR(priv->mm); 1039 1040 single_release(inode, file); 1041 goto out_free; 1042 } 1043 1044 return 0; 1045 1046 out_free: 1047 kfree(priv); 1048 return ret; 1049 } 1050 1051 static int smaps_rollup_release(struct inode *inode, struct file *file) 1052 { 1053 struct seq_file *seq = file->private_data; 1054 struct proc_maps_private *priv = seq->private; 1055 1056 if (priv->mm) 1057 mmdrop(priv->mm); 1058 1059 kfree(priv); 1060 return single_release(inode, file); 1061 } 1062 1063 const struct file_operations proc_pid_smaps_operations = { 1064 .open = pid_smaps_open, 1065 .read = seq_read, 1066 .llseek = seq_lseek, 1067 .release = proc_map_release, 1068 }; 1069 1070 const struct file_operations proc_pid_smaps_rollup_operations = { 1071 .open = smaps_rollup_open, 1072 .read = seq_read, 1073 .llseek = seq_lseek, 1074 .release = smaps_rollup_release, 1075 }; 1076 1077 enum clear_refs_types { 1078 CLEAR_REFS_ALL = 1, 1079 CLEAR_REFS_ANON, 1080 CLEAR_REFS_MAPPED, 1081 CLEAR_REFS_SOFT_DIRTY, 1082 CLEAR_REFS_MM_HIWATER_RSS, 1083 CLEAR_REFS_LAST, 1084 }; 1085 1086 struct clear_refs_private { 1087 enum clear_refs_types type; 1088 }; 1089 1090 #ifdef CONFIG_MEM_SOFT_DIRTY 1091 1092 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1093 { 1094 struct page *page; 1095 1096 if (!pte_write(pte)) 1097 return false; 1098 if (!is_cow_mapping(vma->vm_flags)) 1099 return false; 1100 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) 1101 return false; 1102 page = vm_normal_page(vma, addr, pte); 1103 if (!page) 1104 return false; 1105 return page_maybe_dma_pinned(page); 1106 } 1107 1108 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1109 unsigned long addr, pte_t *pte) 1110 { 1111 /* 1112 * The soft-dirty tracker uses #PF-s to catch writes 1113 * to pages, so write-protect the pte as well. See the 1114 * Documentation/admin-guide/mm/soft-dirty.rst for full description 1115 * of how soft-dirty works. 1116 */ 1117 pte_t ptent = ptep_get(pte); 1118 1119 if (pte_present(ptent)) { 1120 pte_t old_pte; 1121 1122 if (pte_is_pinned(vma, addr, ptent)) 1123 return; 1124 old_pte = ptep_modify_prot_start(vma, addr, pte); 1125 ptent = pte_wrprotect(old_pte); 1126 ptent = pte_clear_soft_dirty(ptent); 1127 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); 1128 } else if (is_swap_pte(ptent)) { 1129 ptent = pte_swp_clear_soft_dirty(ptent); 1130 set_pte_at(vma->vm_mm, addr, pte, ptent); 1131 } 1132 } 1133 #else 1134 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1135 unsigned long addr, pte_t *pte) 1136 { 1137 } 1138 #endif 1139 1140 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 1141 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1142 unsigned long addr, pmd_t *pmdp) 1143 { 1144 pmd_t old, pmd = *pmdp; 1145 1146 if (pmd_present(pmd)) { 1147 /* See comment in change_huge_pmd() */ 1148 old = pmdp_invalidate(vma, addr, pmdp); 1149 if (pmd_dirty(old)) 1150 pmd = pmd_mkdirty(pmd); 1151 if (pmd_young(old)) 1152 pmd = pmd_mkyoung(pmd); 1153 1154 pmd = pmd_wrprotect(pmd); 1155 pmd = pmd_clear_soft_dirty(pmd); 1156 1157 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1158 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { 1159 pmd = pmd_swp_clear_soft_dirty(pmd); 1160 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1161 } 1162 } 1163 #else 1164 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1165 unsigned long addr, pmd_t *pmdp) 1166 { 1167 } 1168 #endif 1169 1170 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, 1171 unsigned long end, struct mm_walk *walk) 1172 { 1173 struct clear_refs_private *cp = walk->private; 1174 struct vm_area_struct *vma = walk->vma; 1175 pte_t *pte, ptent; 1176 spinlock_t *ptl; 1177 struct folio *folio; 1178 1179 ptl = pmd_trans_huge_lock(pmd, vma); 1180 if (ptl) { 1181 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1182 clear_soft_dirty_pmd(vma, addr, pmd); 1183 goto out; 1184 } 1185 1186 if (!pmd_present(*pmd)) 1187 goto out; 1188 1189 folio = pmd_folio(*pmd); 1190 1191 /* Clear accessed and referenced bits. */ 1192 pmdp_test_and_clear_young(vma, addr, pmd); 1193 folio_test_clear_young(folio); 1194 folio_clear_referenced(folio); 1195 out: 1196 spin_unlock(ptl); 1197 return 0; 1198 } 1199 1200 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 1201 if (!pte) { 1202 walk->action = ACTION_AGAIN; 1203 return 0; 1204 } 1205 for (; addr != end; pte++, addr += PAGE_SIZE) { 1206 ptent = ptep_get(pte); 1207 1208 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1209 clear_soft_dirty(vma, addr, pte); 1210 continue; 1211 } 1212 1213 if (!pte_present(ptent)) 1214 continue; 1215 1216 folio = vm_normal_folio(vma, addr, ptent); 1217 if (!folio) 1218 continue; 1219 1220 /* Clear accessed and referenced bits. */ 1221 ptep_test_and_clear_young(vma, addr, pte); 1222 folio_test_clear_young(folio); 1223 folio_clear_referenced(folio); 1224 } 1225 pte_unmap_unlock(pte - 1, ptl); 1226 cond_resched(); 1227 return 0; 1228 } 1229 1230 static int clear_refs_test_walk(unsigned long start, unsigned long end, 1231 struct mm_walk *walk) 1232 { 1233 struct clear_refs_private *cp = walk->private; 1234 struct vm_area_struct *vma = walk->vma; 1235 1236 if (vma->vm_flags & VM_PFNMAP) 1237 return 1; 1238 1239 /* 1240 * Writing 1 to /proc/pid/clear_refs affects all pages. 1241 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages. 1242 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages. 1243 * Writing 4 to /proc/pid/clear_refs affects all pages. 1244 */ 1245 if (cp->type == CLEAR_REFS_ANON && vma->vm_file) 1246 return 1; 1247 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file) 1248 return 1; 1249 return 0; 1250 } 1251 1252 static const struct mm_walk_ops clear_refs_walk_ops = { 1253 .pmd_entry = clear_refs_pte_range, 1254 .test_walk = clear_refs_test_walk, 1255 .walk_lock = PGWALK_WRLOCK, 1256 }; 1257 1258 static ssize_t clear_refs_write(struct file *file, const char __user *buf, 1259 size_t count, loff_t *ppos) 1260 { 1261 struct task_struct *task; 1262 char buffer[PROC_NUMBUF] = {}; 1263 struct mm_struct *mm; 1264 struct vm_area_struct *vma; 1265 enum clear_refs_types type; 1266 int itype; 1267 int rv; 1268 1269 if (count > sizeof(buffer) - 1) 1270 count = sizeof(buffer) - 1; 1271 if (copy_from_user(buffer, buf, count)) 1272 return -EFAULT; 1273 rv = kstrtoint(strstrip(buffer), 10, &itype); 1274 if (rv < 0) 1275 return rv; 1276 type = (enum clear_refs_types)itype; 1277 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) 1278 return -EINVAL; 1279 1280 task = get_proc_task(file_inode(file)); 1281 if (!task) 1282 return -ESRCH; 1283 mm = get_task_mm(task); 1284 if (mm) { 1285 VMA_ITERATOR(vmi, mm, 0); 1286 struct mmu_notifier_range range; 1287 struct clear_refs_private cp = { 1288 .type = type, 1289 }; 1290 1291 if (mmap_write_lock_killable(mm)) { 1292 count = -EINTR; 1293 goto out_mm; 1294 } 1295 if (type == CLEAR_REFS_MM_HIWATER_RSS) { 1296 /* 1297 * Writing 5 to /proc/pid/clear_refs resets the peak 1298 * resident set size to this mm's current rss value. 1299 */ 1300 reset_mm_hiwater_rss(mm); 1301 goto out_unlock; 1302 } 1303 1304 if (type == CLEAR_REFS_SOFT_DIRTY) { 1305 for_each_vma(vmi, vma) { 1306 if (!(vma->vm_flags & VM_SOFTDIRTY)) 1307 continue; 1308 vm_flags_clear(vma, VM_SOFTDIRTY); 1309 vma_set_page_prot(vma); 1310 } 1311 1312 inc_tlb_flush_pending(mm); 1313 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY, 1314 0, mm, 0, -1UL); 1315 mmu_notifier_invalidate_range_start(&range); 1316 } 1317 walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp); 1318 if (type == CLEAR_REFS_SOFT_DIRTY) { 1319 mmu_notifier_invalidate_range_end(&range); 1320 flush_tlb_mm(mm); 1321 dec_tlb_flush_pending(mm); 1322 } 1323 out_unlock: 1324 mmap_write_unlock(mm); 1325 out_mm: 1326 mmput(mm); 1327 } 1328 put_task_struct(task); 1329 1330 return count; 1331 } 1332 1333 const struct file_operations proc_clear_refs_operations = { 1334 .write = clear_refs_write, 1335 .llseek = noop_llseek, 1336 }; 1337 1338 typedef struct { 1339 u64 pme; 1340 } pagemap_entry_t; 1341 1342 struct pagemapread { 1343 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ 1344 pagemap_entry_t *buffer; 1345 bool show_pfn; 1346 }; 1347 1348 #define PAGEMAP_WALK_SIZE (PMD_SIZE) 1349 #define PAGEMAP_WALK_MASK (PMD_MASK) 1350 1351 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) 1352 #define PM_PFRAME_BITS 55 1353 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0) 1354 #define PM_SOFT_DIRTY BIT_ULL(55) 1355 #define PM_MMAP_EXCLUSIVE BIT_ULL(56) 1356 #define PM_UFFD_WP BIT_ULL(57) 1357 #define PM_FILE BIT_ULL(61) 1358 #define PM_SWAP BIT_ULL(62) 1359 #define PM_PRESENT BIT_ULL(63) 1360 1361 #define PM_END_OF_BUFFER 1 1362 1363 static inline pagemap_entry_t make_pme(u64 frame, u64 flags) 1364 { 1365 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags }; 1366 } 1367 1368 static int add_to_pagemap(pagemap_entry_t *pme, struct pagemapread *pm) 1369 { 1370 pm->buffer[pm->pos++] = *pme; 1371 if (pm->pos >= pm->len) 1372 return PM_END_OF_BUFFER; 1373 return 0; 1374 } 1375 1376 static int pagemap_pte_hole(unsigned long start, unsigned long end, 1377 __always_unused int depth, struct mm_walk *walk) 1378 { 1379 struct pagemapread *pm = walk->private; 1380 unsigned long addr = start; 1381 int err = 0; 1382 1383 while (addr < end) { 1384 struct vm_area_struct *vma = find_vma(walk->mm, addr); 1385 pagemap_entry_t pme = make_pme(0, 0); 1386 /* End of address space hole, which we mark as non-present. */ 1387 unsigned long hole_end; 1388 1389 if (vma) 1390 hole_end = min(end, vma->vm_start); 1391 else 1392 hole_end = end; 1393 1394 for (; addr < hole_end; addr += PAGE_SIZE) { 1395 err = add_to_pagemap(&pme, pm); 1396 if (err) 1397 goto out; 1398 } 1399 1400 if (!vma) 1401 break; 1402 1403 /* Addresses in the VMA. */ 1404 if (vma->vm_flags & VM_SOFTDIRTY) 1405 pme = make_pme(0, PM_SOFT_DIRTY); 1406 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) { 1407 err = add_to_pagemap(&pme, pm); 1408 if (err) 1409 goto out; 1410 } 1411 } 1412 out: 1413 return err; 1414 } 1415 1416 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, 1417 struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1418 { 1419 u64 frame = 0, flags = 0; 1420 struct page *page = NULL; 1421 bool migration = false; 1422 1423 if (pte_present(pte)) { 1424 if (pm->show_pfn) 1425 frame = pte_pfn(pte); 1426 flags |= PM_PRESENT; 1427 page = vm_normal_page(vma, addr, pte); 1428 if (pte_soft_dirty(pte)) 1429 flags |= PM_SOFT_DIRTY; 1430 if (pte_uffd_wp(pte)) 1431 flags |= PM_UFFD_WP; 1432 } else if (is_swap_pte(pte)) { 1433 swp_entry_t entry; 1434 if (pte_swp_soft_dirty(pte)) 1435 flags |= PM_SOFT_DIRTY; 1436 if (pte_swp_uffd_wp(pte)) 1437 flags |= PM_UFFD_WP; 1438 entry = pte_to_swp_entry(pte); 1439 if (pm->show_pfn) { 1440 pgoff_t offset; 1441 /* 1442 * For PFN swap offsets, keeping the offset field 1443 * to be PFN only to be compatible with old smaps. 1444 */ 1445 if (is_pfn_swap_entry(entry)) 1446 offset = swp_offset_pfn(entry); 1447 else 1448 offset = swp_offset(entry); 1449 frame = swp_type(entry) | 1450 (offset << MAX_SWAPFILES_SHIFT); 1451 } 1452 flags |= PM_SWAP; 1453 migration = is_migration_entry(entry); 1454 if (is_pfn_swap_entry(entry)) 1455 page = pfn_swap_entry_to_page(entry); 1456 if (pte_marker_entry_uffd_wp(entry)) 1457 flags |= PM_UFFD_WP; 1458 } 1459 1460 if (page && !PageAnon(page)) 1461 flags |= PM_FILE; 1462 if (page && !migration && page_mapcount(page) == 1) 1463 flags |= PM_MMAP_EXCLUSIVE; 1464 if (vma->vm_flags & VM_SOFTDIRTY) 1465 flags |= PM_SOFT_DIRTY; 1466 1467 return make_pme(frame, flags); 1468 } 1469 1470 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, 1471 struct mm_walk *walk) 1472 { 1473 struct vm_area_struct *vma = walk->vma; 1474 struct pagemapread *pm = walk->private; 1475 spinlock_t *ptl; 1476 pte_t *pte, *orig_pte; 1477 int err = 0; 1478 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1479 bool migration = false; 1480 1481 ptl = pmd_trans_huge_lock(pmdp, vma); 1482 if (ptl) { 1483 u64 flags = 0, frame = 0; 1484 pmd_t pmd = *pmdp; 1485 struct page *page = NULL; 1486 1487 if (vma->vm_flags & VM_SOFTDIRTY) 1488 flags |= PM_SOFT_DIRTY; 1489 1490 if (pmd_present(pmd)) { 1491 page = pmd_page(pmd); 1492 1493 flags |= PM_PRESENT; 1494 if (pmd_soft_dirty(pmd)) 1495 flags |= PM_SOFT_DIRTY; 1496 if (pmd_uffd_wp(pmd)) 1497 flags |= PM_UFFD_WP; 1498 if (pm->show_pfn) 1499 frame = pmd_pfn(pmd) + 1500 ((addr & ~PMD_MASK) >> PAGE_SHIFT); 1501 } 1502 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 1503 else if (is_swap_pmd(pmd)) { 1504 swp_entry_t entry = pmd_to_swp_entry(pmd); 1505 unsigned long offset; 1506 1507 if (pm->show_pfn) { 1508 if (is_pfn_swap_entry(entry)) 1509 offset = swp_offset_pfn(entry); 1510 else 1511 offset = swp_offset(entry); 1512 offset = offset + 1513 ((addr & ~PMD_MASK) >> PAGE_SHIFT); 1514 frame = swp_type(entry) | 1515 (offset << MAX_SWAPFILES_SHIFT); 1516 } 1517 flags |= PM_SWAP; 1518 if (pmd_swp_soft_dirty(pmd)) 1519 flags |= PM_SOFT_DIRTY; 1520 if (pmd_swp_uffd_wp(pmd)) 1521 flags |= PM_UFFD_WP; 1522 VM_BUG_ON(!is_pmd_migration_entry(pmd)); 1523 migration = is_migration_entry(entry); 1524 page = pfn_swap_entry_to_page(entry); 1525 } 1526 #endif 1527 1528 if (page && !migration && page_mapcount(page) == 1) 1529 flags |= PM_MMAP_EXCLUSIVE; 1530 1531 for (; addr != end; addr += PAGE_SIZE) { 1532 pagemap_entry_t pme = make_pme(frame, flags); 1533 1534 err = add_to_pagemap(&pme, pm); 1535 if (err) 1536 break; 1537 if (pm->show_pfn) { 1538 if (flags & PM_PRESENT) 1539 frame++; 1540 else if (flags & PM_SWAP) 1541 frame += (1 << MAX_SWAPFILES_SHIFT); 1542 } 1543 } 1544 spin_unlock(ptl); 1545 return err; 1546 } 1547 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1548 1549 /* 1550 * We can assume that @vma always points to a valid one and @end never 1551 * goes beyond vma->vm_end. 1552 */ 1553 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl); 1554 if (!pte) { 1555 walk->action = ACTION_AGAIN; 1556 return err; 1557 } 1558 for (; addr < end; pte++, addr += PAGE_SIZE) { 1559 pagemap_entry_t pme; 1560 1561 pme = pte_to_pagemap_entry(pm, vma, addr, ptep_get(pte)); 1562 err = add_to_pagemap(&pme, pm); 1563 if (err) 1564 break; 1565 } 1566 pte_unmap_unlock(orig_pte, ptl); 1567 1568 cond_resched(); 1569 1570 return err; 1571 } 1572 1573 #ifdef CONFIG_HUGETLB_PAGE 1574 /* This function walks within one hugetlb entry in the single call */ 1575 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, 1576 unsigned long addr, unsigned long end, 1577 struct mm_walk *walk) 1578 { 1579 struct pagemapread *pm = walk->private; 1580 struct vm_area_struct *vma = walk->vma; 1581 u64 flags = 0, frame = 0; 1582 int err = 0; 1583 pte_t pte; 1584 1585 if (vma->vm_flags & VM_SOFTDIRTY) 1586 flags |= PM_SOFT_DIRTY; 1587 1588 pte = huge_ptep_get(ptep); 1589 if (pte_present(pte)) { 1590 struct folio *folio = page_folio(pte_page(pte)); 1591 1592 if (!folio_test_anon(folio)) 1593 flags |= PM_FILE; 1594 1595 if (!folio_likely_mapped_shared(folio) && 1596 !hugetlb_pmd_shared(ptep)) 1597 flags |= PM_MMAP_EXCLUSIVE; 1598 1599 if (huge_pte_uffd_wp(pte)) 1600 flags |= PM_UFFD_WP; 1601 1602 flags |= PM_PRESENT; 1603 if (pm->show_pfn) 1604 frame = pte_pfn(pte) + 1605 ((addr & ~hmask) >> PAGE_SHIFT); 1606 } else if (pte_swp_uffd_wp_any(pte)) { 1607 flags |= PM_UFFD_WP; 1608 } 1609 1610 for (; addr != end; addr += PAGE_SIZE) { 1611 pagemap_entry_t pme = make_pme(frame, flags); 1612 1613 err = add_to_pagemap(&pme, pm); 1614 if (err) 1615 return err; 1616 if (pm->show_pfn && (flags & PM_PRESENT)) 1617 frame++; 1618 } 1619 1620 cond_resched(); 1621 1622 return err; 1623 } 1624 #else 1625 #define pagemap_hugetlb_range NULL 1626 #endif /* HUGETLB_PAGE */ 1627 1628 static const struct mm_walk_ops pagemap_ops = { 1629 .pmd_entry = pagemap_pmd_range, 1630 .pte_hole = pagemap_pte_hole, 1631 .hugetlb_entry = pagemap_hugetlb_range, 1632 .walk_lock = PGWALK_RDLOCK, 1633 }; 1634 1635 /* 1636 * /proc/pid/pagemap - an array mapping virtual pages to pfns 1637 * 1638 * For each page in the address space, this file contains one 64-bit entry 1639 * consisting of the following: 1640 * 1641 * Bits 0-54 page frame number (PFN) if present 1642 * Bits 0-4 swap type if swapped 1643 * Bits 5-54 swap offset if swapped 1644 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst) 1645 * Bit 56 page exclusively mapped 1646 * Bit 57 pte is uffd-wp write-protected 1647 * Bits 58-60 zero 1648 * Bit 61 page is file-page or shared-anon 1649 * Bit 62 page swapped 1650 * Bit 63 page present 1651 * 1652 * If the page is not present but in swap, then the PFN contains an 1653 * encoding of the swap file number and the page's offset into the 1654 * swap. Unmapped pages return a null PFN. This allows determining 1655 * precisely which pages are mapped (or in swap) and comparing mapped 1656 * pages between processes. 1657 * 1658 * Efficient users of this interface will use /proc/pid/maps to 1659 * determine which areas of memory are actually mapped and llseek to 1660 * skip over unmapped regions. 1661 */ 1662 static ssize_t pagemap_read(struct file *file, char __user *buf, 1663 size_t count, loff_t *ppos) 1664 { 1665 struct mm_struct *mm = file->private_data; 1666 struct pagemapread pm; 1667 unsigned long src; 1668 unsigned long svpfn; 1669 unsigned long start_vaddr; 1670 unsigned long end_vaddr; 1671 int ret = 0, copied = 0; 1672 1673 if (!mm || !mmget_not_zero(mm)) 1674 goto out; 1675 1676 ret = -EINVAL; 1677 /* file position must be aligned */ 1678 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) 1679 goto out_mm; 1680 1681 ret = 0; 1682 if (!count) 1683 goto out_mm; 1684 1685 /* do not disclose physical addresses: attack vector */ 1686 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN); 1687 1688 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); 1689 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL); 1690 ret = -ENOMEM; 1691 if (!pm.buffer) 1692 goto out_mm; 1693 1694 src = *ppos; 1695 svpfn = src / PM_ENTRY_BYTES; 1696 end_vaddr = mm->task_size; 1697 1698 /* watch out for wraparound */ 1699 start_vaddr = end_vaddr; 1700 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) { 1701 unsigned long end; 1702 1703 ret = mmap_read_lock_killable(mm); 1704 if (ret) 1705 goto out_free; 1706 start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT); 1707 mmap_read_unlock(mm); 1708 1709 end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); 1710 if (end >= start_vaddr && end < mm->task_size) 1711 end_vaddr = end; 1712 } 1713 1714 /* Ensure the address is inside the task */ 1715 if (start_vaddr > mm->task_size) 1716 start_vaddr = end_vaddr; 1717 1718 ret = 0; 1719 while (count && (start_vaddr < end_vaddr)) { 1720 int len; 1721 unsigned long end; 1722 1723 pm.pos = 0; 1724 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; 1725 /* overflow ? */ 1726 if (end < start_vaddr || end > end_vaddr) 1727 end = end_vaddr; 1728 ret = mmap_read_lock_killable(mm); 1729 if (ret) 1730 goto out_free; 1731 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm); 1732 mmap_read_unlock(mm); 1733 start_vaddr = end; 1734 1735 len = min(count, PM_ENTRY_BYTES * pm.pos); 1736 if (copy_to_user(buf, pm.buffer, len)) { 1737 ret = -EFAULT; 1738 goto out_free; 1739 } 1740 copied += len; 1741 buf += len; 1742 count -= len; 1743 } 1744 *ppos += copied; 1745 if (!ret || ret == PM_END_OF_BUFFER) 1746 ret = copied; 1747 1748 out_free: 1749 kfree(pm.buffer); 1750 out_mm: 1751 mmput(mm); 1752 out: 1753 return ret; 1754 } 1755 1756 static int pagemap_open(struct inode *inode, struct file *file) 1757 { 1758 struct mm_struct *mm; 1759 1760 mm = proc_mem_open(inode, PTRACE_MODE_READ); 1761 if (IS_ERR(mm)) 1762 return PTR_ERR(mm); 1763 file->private_data = mm; 1764 return 0; 1765 } 1766 1767 static int pagemap_release(struct inode *inode, struct file *file) 1768 { 1769 struct mm_struct *mm = file->private_data; 1770 1771 if (mm) 1772 mmdrop(mm); 1773 return 0; 1774 } 1775 1776 #define PM_SCAN_CATEGORIES (PAGE_IS_WPALLOWED | PAGE_IS_WRITTEN | \ 1777 PAGE_IS_FILE | PAGE_IS_PRESENT | \ 1778 PAGE_IS_SWAPPED | PAGE_IS_PFNZERO | \ 1779 PAGE_IS_HUGE | PAGE_IS_SOFT_DIRTY) 1780 #define PM_SCAN_FLAGS (PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC) 1781 1782 struct pagemap_scan_private { 1783 struct pm_scan_arg arg; 1784 unsigned long masks_of_interest, cur_vma_category; 1785 struct page_region *vec_buf; 1786 unsigned long vec_buf_len, vec_buf_index, found_pages; 1787 struct page_region __user *vec_out; 1788 }; 1789 1790 static unsigned long pagemap_page_category(struct pagemap_scan_private *p, 1791 struct vm_area_struct *vma, 1792 unsigned long addr, pte_t pte) 1793 { 1794 unsigned long categories = 0; 1795 1796 if (pte_present(pte)) { 1797 struct page *page; 1798 1799 categories |= PAGE_IS_PRESENT; 1800 if (!pte_uffd_wp(pte)) 1801 categories |= PAGE_IS_WRITTEN; 1802 1803 if (p->masks_of_interest & PAGE_IS_FILE) { 1804 page = vm_normal_page(vma, addr, pte); 1805 if (page && !PageAnon(page)) 1806 categories |= PAGE_IS_FILE; 1807 } 1808 1809 if (is_zero_pfn(pte_pfn(pte))) 1810 categories |= PAGE_IS_PFNZERO; 1811 if (pte_soft_dirty(pte)) 1812 categories |= PAGE_IS_SOFT_DIRTY; 1813 } else if (is_swap_pte(pte)) { 1814 swp_entry_t swp; 1815 1816 categories |= PAGE_IS_SWAPPED; 1817 if (!pte_swp_uffd_wp_any(pte)) 1818 categories |= PAGE_IS_WRITTEN; 1819 1820 if (p->masks_of_interest & PAGE_IS_FILE) { 1821 swp = pte_to_swp_entry(pte); 1822 if (is_pfn_swap_entry(swp) && 1823 !folio_test_anon(pfn_swap_entry_folio(swp))) 1824 categories |= PAGE_IS_FILE; 1825 } 1826 if (pte_swp_soft_dirty(pte)) 1827 categories |= PAGE_IS_SOFT_DIRTY; 1828 } 1829 1830 return categories; 1831 } 1832 1833 static void make_uffd_wp_pte(struct vm_area_struct *vma, 1834 unsigned long addr, pte_t *pte, pte_t ptent) 1835 { 1836 if (pte_present(ptent)) { 1837 pte_t old_pte; 1838 1839 old_pte = ptep_modify_prot_start(vma, addr, pte); 1840 ptent = pte_mkuffd_wp(old_pte); 1841 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); 1842 } else if (is_swap_pte(ptent)) { 1843 ptent = pte_swp_mkuffd_wp(ptent); 1844 set_pte_at(vma->vm_mm, addr, pte, ptent); 1845 } else { 1846 set_pte_at(vma->vm_mm, addr, pte, 1847 make_pte_marker(PTE_MARKER_UFFD_WP)); 1848 } 1849 } 1850 1851 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1852 static unsigned long pagemap_thp_category(struct pagemap_scan_private *p, 1853 struct vm_area_struct *vma, 1854 unsigned long addr, pmd_t pmd) 1855 { 1856 unsigned long categories = PAGE_IS_HUGE; 1857 1858 if (pmd_present(pmd)) { 1859 struct page *page; 1860 1861 categories |= PAGE_IS_PRESENT; 1862 if (!pmd_uffd_wp(pmd)) 1863 categories |= PAGE_IS_WRITTEN; 1864 1865 if (p->masks_of_interest & PAGE_IS_FILE) { 1866 page = vm_normal_page_pmd(vma, addr, pmd); 1867 if (page && !PageAnon(page)) 1868 categories |= PAGE_IS_FILE; 1869 } 1870 1871 if (is_zero_pfn(pmd_pfn(pmd))) 1872 categories |= PAGE_IS_PFNZERO; 1873 if (pmd_soft_dirty(pmd)) 1874 categories |= PAGE_IS_SOFT_DIRTY; 1875 } else if (is_swap_pmd(pmd)) { 1876 swp_entry_t swp; 1877 1878 categories |= PAGE_IS_SWAPPED; 1879 if (!pmd_swp_uffd_wp(pmd)) 1880 categories |= PAGE_IS_WRITTEN; 1881 if (pmd_swp_soft_dirty(pmd)) 1882 categories |= PAGE_IS_SOFT_DIRTY; 1883 1884 if (p->masks_of_interest & PAGE_IS_FILE) { 1885 swp = pmd_to_swp_entry(pmd); 1886 if (is_pfn_swap_entry(swp) && 1887 !folio_test_anon(pfn_swap_entry_folio(swp))) 1888 categories |= PAGE_IS_FILE; 1889 } 1890 } 1891 1892 return categories; 1893 } 1894 1895 static void make_uffd_wp_pmd(struct vm_area_struct *vma, 1896 unsigned long addr, pmd_t *pmdp) 1897 { 1898 pmd_t old, pmd = *pmdp; 1899 1900 if (pmd_present(pmd)) { 1901 old = pmdp_invalidate_ad(vma, addr, pmdp); 1902 pmd = pmd_mkuffd_wp(old); 1903 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1904 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { 1905 pmd = pmd_swp_mkuffd_wp(pmd); 1906 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1907 } 1908 } 1909 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1910 1911 #ifdef CONFIG_HUGETLB_PAGE 1912 static unsigned long pagemap_hugetlb_category(pte_t pte) 1913 { 1914 unsigned long categories = PAGE_IS_HUGE; 1915 1916 /* 1917 * According to pagemap_hugetlb_range(), file-backed HugeTLB 1918 * page cannot be swapped. So PAGE_IS_FILE is not checked for 1919 * swapped pages. 1920 */ 1921 if (pte_present(pte)) { 1922 categories |= PAGE_IS_PRESENT; 1923 if (!huge_pte_uffd_wp(pte)) 1924 categories |= PAGE_IS_WRITTEN; 1925 if (!PageAnon(pte_page(pte))) 1926 categories |= PAGE_IS_FILE; 1927 if (is_zero_pfn(pte_pfn(pte))) 1928 categories |= PAGE_IS_PFNZERO; 1929 if (pte_soft_dirty(pte)) 1930 categories |= PAGE_IS_SOFT_DIRTY; 1931 } else if (is_swap_pte(pte)) { 1932 categories |= PAGE_IS_SWAPPED; 1933 if (!pte_swp_uffd_wp_any(pte)) 1934 categories |= PAGE_IS_WRITTEN; 1935 if (pte_swp_soft_dirty(pte)) 1936 categories |= PAGE_IS_SOFT_DIRTY; 1937 } 1938 1939 return categories; 1940 } 1941 1942 static void make_uffd_wp_huge_pte(struct vm_area_struct *vma, 1943 unsigned long addr, pte_t *ptep, 1944 pte_t ptent) 1945 { 1946 unsigned long psize; 1947 1948 if (is_hugetlb_entry_hwpoisoned(ptent) || is_pte_marker(ptent)) 1949 return; 1950 1951 psize = huge_page_size(hstate_vma(vma)); 1952 1953 if (is_hugetlb_entry_migration(ptent)) 1954 set_huge_pte_at(vma->vm_mm, addr, ptep, 1955 pte_swp_mkuffd_wp(ptent), psize); 1956 else if (!huge_pte_none(ptent)) 1957 huge_ptep_modify_prot_commit(vma, addr, ptep, ptent, 1958 huge_pte_mkuffd_wp(ptent)); 1959 else 1960 set_huge_pte_at(vma->vm_mm, addr, ptep, 1961 make_pte_marker(PTE_MARKER_UFFD_WP), psize); 1962 } 1963 #endif /* CONFIG_HUGETLB_PAGE */ 1964 1965 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE) 1966 static void pagemap_scan_backout_range(struct pagemap_scan_private *p, 1967 unsigned long addr, unsigned long end) 1968 { 1969 struct page_region *cur_buf = &p->vec_buf[p->vec_buf_index]; 1970 1971 if (cur_buf->start != addr) 1972 cur_buf->end = addr; 1973 else 1974 cur_buf->start = cur_buf->end = 0; 1975 1976 p->found_pages -= (end - addr) / PAGE_SIZE; 1977 } 1978 #endif 1979 1980 static bool pagemap_scan_is_interesting_page(unsigned long categories, 1981 const struct pagemap_scan_private *p) 1982 { 1983 categories ^= p->arg.category_inverted; 1984 if ((categories & p->arg.category_mask) != p->arg.category_mask) 1985 return false; 1986 if (p->arg.category_anyof_mask && !(categories & p->arg.category_anyof_mask)) 1987 return false; 1988 1989 return true; 1990 } 1991 1992 static bool pagemap_scan_is_interesting_vma(unsigned long categories, 1993 const struct pagemap_scan_private *p) 1994 { 1995 unsigned long required = p->arg.category_mask & PAGE_IS_WPALLOWED; 1996 1997 categories ^= p->arg.category_inverted; 1998 if ((categories & required) != required) 1999 return false; 2000 2001 return true; 2002 } 2003 2004 static int pagemap_scan_test_walk(unsigned long start, unsigned long end, 2005 struct mm_walk *walk) 2006 { 2007 struct pagemap_scan_private *p = walk->private; 2008 struct vm_area_struct *vma = walk->vma; 2009 unsigned long vma_category = 0; 2010 bool wp_allowed = userfaultfd_wp_async(vma) && 2011 userfaultfd_wp_use_markers(vma); 2012 2013 if (!wp_allowed) { 2014 /* User requested explicit failure over wp-async capability */ 2015 if (p->arg.flags & PM_SCAN_CHECK_WPASYNC) 2016 return -EPERM; 2017 /* 2018 * User requires wr-protect, and allows silently skipping 2019 * unsupported vmas. 2020 */ 2021 if (p->arg.flags & PM_SCAN_WP_MATCHING) 2022 return 1; 2023 /* 2024 * Then the request doesn't involve wr-protects at all, 2025 * fall through to the rest checks, and allow vma walk. 2026 */ 2027 } 2028 2029 if (vma->vm_flags & VM_PFNMAP) 2030 return 1; 2031 2032 if (wp_allowed) 2033 vma_category |= PAGE_IS_WPALLOWED; 2034 2035 if (vma->vm_flags & VM_SOFTDIRTY) 2036 vma_category |= PAGE_IS_SOFT_DIRTY; 2037 2038 if (!pagemap_scan_is_interesting_vma(vma_category, p)) 2039 return 1; 2040 2041 p->cur_vma_category = vma_category; 2042 2043 return 0; 2044 } 2045 2046 static bool pagemap_scan_push_range(unsigned long categories, 2047 struct pagemap_scan_private *p, 2048 unsigned long addr, unsigned long end) 2049 { 2050 struct page_region *cur_buf = &p->vec_buf[p->vec_buf_index]; 2051 2052 /* 2053 * When there is no output buffer provided at all, the sentinel values 2054 * won't match here. There is no other way for `cur_buf->end` to be 2055 * non-zero other than it being non-empty. 2056 */ 2057 if (addr == cur_buf->end && categories == cur_buf->categories) { 2058 cur_buf->end = end; 2059 return true; 2060 } 2061 2062 if (cur_buf->end) { 2063 if (p->vec_buf_index >= p->vec_buf_len - 1) 2064 return false; 2065 2066 cur_buf = &p->vec_buf[++p->vec_buf_index]; 2067 } 2068 2069 cur_buf->start = addr; 2070 cur_buf->end = end; 2071 cur_buf->categories = categories; 2072 2073 return true; 2074 } 2075 2076 static int pagemap_scan_output(unsigned long categories, 2077 struct pagemap_scan_private *p, 2078 unsigned long addr, unsigned long *end) 2079 { 2080 unsigned long n_pages, total_pages; 2081 int ret = 0; 2082 2083 if (!p->vec_buf) 2084 return 0; 2085 2086 categories &= p->arg.return_mask; 2087 2088 n_pages = (*end - addr) / PAGE_SIZE; 2089 if (check_add_overflow(p->found_pages, n_pages, &total_pages) || 2090 total_pages > p->arg.max_pages) { 2091 size_t n_too_much = total_pages - p->arg.max_pages; 2092 *end -= n_too_much * PAGE_SIZE; 2093 n_pages -= n_too_much; 2094 ret = -ENOSPC; 2095 } 2096 2097 if (!pagemap_scan_push_range(categories, p, addr, *end)) { 2098 *end = addr; 2099 n_pages = 0; 2100 ret = -ENOSPC; 2101 } 2102 2103 p->found_pages += n_pages; 2104 if (ret) 2105 p->arg.walk_end = *end; 2106 2107 return ret; 2108 } 2109 2110 static int pagemap_scan_thp_entry(pmd_t *pmd, unsigned long start, 2111 unsigned long end, struct mm_walk *walk) 2112 { 2113 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2114 struct pagemap_scan_private *p = walk->private; 2115 struct vm_area_struct *vma = walk->vma; 2116 unsigned long categories; 2117 spinlock_t *ptl; 2118 int ret = 0; 2119 2120 ptl = pmd_trans_huge_lock(pmd, vma); 2121 if (!ptl) 2122 return -ENOENT; 2123 2124 categories = p->cur_vma_category | 2125 pagemap_thp_category(p, vma, start, *pmd); 2126 2127 if (!pagemap_scan_is_interesting_page(categories, p)) 2128 goto out_unlock; 2129 2130 ret = pagemap_scan_output(categories, p, start, &end); 2131 if (start == end) 2132 goto out_unlock; 2133 2134 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2135 goto out_unlock; 2136 if (~categories & PAGE_IS_WRITTEN) 2137 goto out_unlock; 2138 2139 /* 2140 * Break huge page into small pages if the WP operation 2141 * needs to be performed on a portion of the huge page. 2142 */ 2143 if (end != start + HPAGE_SIZE) { 2144 spin_unlock(ptl); 2145 split_huge_pmd(vma, pmd, start); 2146 pagemap_scan_backout_range(p, start, end); 2147 /* Report as if there was no THP */ 2148 return -ENOENT; 2149 } 2150 2151 make_uffd_wp_pmd(vma, start, pmd); 2152 flush_tlb_range(vma, start, end); 2153 out_unlock: 2154 spin_unlock(ptl); 2155 return ret; 2156 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 2157 return -ENOENT; 2158 #endif 2159 } 2160 2161 static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start, 2162 unsigned long end, struct mm_walk *walk) 2163 { 2164 struct pagemap_scan_private *p = walk->private; 2165 struct vm_area_struct *vma = walk->vma; 2166 unsigned long addr, flush_end = 0; 2167 pte_t *pte, *start_pte; 2168 spinlock_t *ptl; 2169 int ret; 2170 2171 arch_enter_lazy_mmu_mode(); 2172 2173 ret = pagemap_scan_thp_entry(pmd, start, end, walk); 2174 if (ret != -ENOENT) { 2175 arch_leave_lazy_mmu_mode(); 2176 return ret; 2177 } 2178 2179 ret = 0; 2180 start_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl); 2181 if (!pte) { 2182 arch_leave_lazy_mmu_mode(); 2183 walk->action = ACTION_AGAIN; 2184 return 0; 2185 } 2186 2187 if ((p->arg.flags & PM_SCAN_WP_MATCHING) && !p->vec_out) { 2188 /* Fast path for performing exclusive WP */ 2189 for (addr = start; addr != end; pte++, addr += PAGE_SIZE) { 2190 pte_t ptent = ptep_get(pte); 2191 2192 if ((pte_present(ptent) && pte_uffd_wp(ptent)) || 2193 pte_swp_uffd_wp_any(ptent)) 2194 continue; 2195 make_uffd_wp_pte(vma, addr, pte, ptent); 2196 if (!flush_end) 2197 start = addr; 2198 flush_end = addr + PAGE_SIZE; 2199 } 2200 goto flush_and_return; 2201 } 2202 2203 if (!p->arg.category_anyof_mask && !p->arg.category_inverted && 2204 p->arg.category_mask == PAGE_IS_WRITTEN && 2205 p->arg.return_mask == PAGE_IS_WRITTEN) { 2206 for (addr = start; addr < end; pte++, addr += PAGE_SIZE) { 2207 unsigned long next = addr + PAGE_SIZE; 2208 pte_t ptent = ptep_get(pte); 2209 2210 if ((pte_present(ptent) && pte_uffd_wp(ptent)) || 2211 pte_swp_uffd_wp_any(ptent)) 2212 continue; 2213 ret = pagemap_scan_output(p->cur_vma_category | PAGE_IS_WRITTEN, 2214 p, addr, &next); 2215 if (next == addr) 2216 break; 2217 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2218 continue; 2219 make_uffd_wp_pte(vma, addr, pte, ptent); 2220 if (!flush_end) 2221 start = addr; 2222 flush_end = next; 2223 } 2224 goto flush_and_return; 2225 } 2226 2227 for (addr = start; addr != end; pte++, addr += PAGE_SIZE) { 2228 pte_t ptent = ptep_get(pte); 2229 unsigned long categories = p->cur_vma_category | 2230 pagemap_page_category(p, vma, addr, ptent); 2231 unsigned long next = addr + PAGE_SIZE; 2232 2233 if (!pagemap_scan_is_interesting_page(categories, p)) 2234 continue; 2235 2236 ret = pagemap_scan_output(categories, p, addr, &next); 2237 if (next == addr) 2238 break; 2239 2240 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2241 continue; 2242 if (~categories & PAGE_IS_WRITTEN) 2243 continue; 2244 2245 make_uffd_wp_pte(vma, addr, pte, ptent); 2246 if (!flush_end) 2247 start = addr; 2248 flush_end = next; 2249 } 2250 2251 flush_and_return: 2252 if (flush_end) 2253 flush_tlb_range(vma, start, addr); 2254 2255 pte_unmap_unlock(start_pte, ptl); 2256 arch_leave_lazy_mmu_mode(); 2257 2258 cond_resched(); 2259 return ret; 2260 } 2261 2262 #ifdef CONFIG_HUGETLB_PAGE 2263 static int pagemap_scan_hugetlb_entry(pte_t *ptep, unsigned long hmask, 2264 unsigned long start, unsigned long end, 2265 struct mm_walk *walk) 2266 { 2267 struct pagemap_scan_private *p = walk->private; 2268 struct vm_area_struct *vma = walk->vma; 2269 unsigned long categories; 2270 spinlock_t *ptl; 2271 int ret = 0; 2272 pte_t pte; 2273 2274 if (~p->arg.flags & PM_SCAN_WP_MATCHING) { 2275 /* Go the short route when not write-protecting pages. */ 2276 2277 pte = huge_ptep_get(ptep); 2278 categories = p->cur_vma_category | pagemap_hugetlb_category(pte); 2279 2280 if (!pagemap_scan_is_interesting_page(categories, p)) 2281 return 0; 2282 2283 return pagemap_scan_output(categories, p, start, &end); 2284 } 2285 2286 i_mmap_lock_write(vma->vm_file->f_mapping); 2287 ptl = huge_pte_lock(hstate_vma(vma), vma->vm_mm, ptep); 2288 2289 pte = huge_ptep_get(ptep); 2290 categories = p->cur_vma_category | pagemap_hugetlb_category(pte); 2291 2292 if (!pagemap_scan_is_interesting_page(categories, p)) 2293 goto out_unlock; 2294 2295 ret = pagemap_scan_output(categories, p, start, &end); 2296 if (start == end) 2297 goto out_unlock; 2298 2299 if (~categories & PAGE_IS_WRITTEN) 2300 goto out_unlock; 2301 2302 if (end != start + HPAGE_SIZE) { 2303 /* Partial HugeTLB page WP isn't possible. */ 2304 pagemap_scan_backout_range(p, start, end); 2305 p->arg.walk_end = start; 2306 ret = 0; 2307 goto out_unlock; 2308 } 2309 2310 make_uffd_wp_huge_pte(vma, start, ptep, pte); 2311 flush_hugetlb_tlb_range(vma, start, end); 2312 2313 out_unlock: 2314 spin_unlock(ptl); 2315 i_mmap_unlock_write(vma->vm_file->f_mapping); 2316 2317 return ret; 2318 } 2319 #else 2320 #define pagemap_scan_hugetlb_entry NULL 2321 #endif 2322 2323 static int pagemap_scan_pte_hole(unsigned long addr, unsigned long end, 2324 int depth, struct mm_walk *walk) 2325 { 2326 struct pagemap_scan_private *p = walk->private; 2327 struct vm_area_struct *vma = walk->vma; 2328 int ret, err; 2329 2330 if (!vma || !pagemap_scan_is_interesting_page(p->cur_vma_category, p)) 2331 return 0; 2332 2333 ret = pagemap_scan_output(p->cur_vma_category, p, addr, &end); 2334 if (addr == end) 2335 return ret; 2336 2337 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2338 return ret; 2339 2340 err = uffd_wp_range(vma, addr, end - addr, true); 2341 if (err < 0) 2342 ret = err; 2343 2344 return ret; 2345 } 2346 2347 static const struct mm_walk_ops pagemap_scan_ops = { 2348 .test_walk = pagemap_scan_test_walk, 2349 .pmd_entry = pagemap_scan_pmd_entry, 2350 .pte_hole = pagemap_scan_pte_hole, 2351 .hugetlb_entry = pagemap_scan_hugetlb_entry, 2352 }; 2353 2354 static int pagemap_scan_get_args(struct pm_scan_arg *arg, 2355 unsigned long uarg) 2356 { 2357 if (copy_from_user(arg, (void __user *)uarg, sizeof(*arg))) 2358 return -EFAULT; 2359 2360 if (arg->size != sizeof(struct pm_scan_arg)) 2361 return -EINVAL; 2362 2363 /* Validate requested features */ 2364 if (arg->flags & ~PM_SCAN_FLAGS) 2365 return -EINVAL; 2366 if ((arg->category_inverted | arg->category_mask | 2367 arg->category_anyof_mask | arg->return_mask) & ~PM_SCAN_CATEGORIES) 2368 return -EINVAL; 2369 2370 arg->start = untagged_addr((unsigned long)arg->start); 2371 arg->end = untagged_addr((unsigned long)arg->end); 2372 arg->vec = untagged_addr((unsigned long)arg->vec); 2373 2374 /* Validate memory pointers */ 2375 if (!IS_ALIGNED(arg->start, PAGE_SIZE)) 2376 return -EINVAL; 2377 if (!access_ok((void __user *)(long)arg->start, arg->end - arg->start)) 2378 return -EFAULT; 2379 if (!arg->vec && arg->vec_len) 2380 return -EINVAL; 2381 if (arg->vec && !access_ok((void __user *)(long)arg->vec, 2382 arg->vec_len * sizeof(struct page_region))) 2383 return -EFAULT; 2384 2385 /* Fixup default values */ 2386 arg->end = ALIGN(arg->end, PAGE_SIZE); 2387 arg->walk_end = 0; 2388 if (!arg->max_pages) 2389 arg->max_pages = ULONG_MAX; 2390 2391 return 0; 2392 } 2393 2394 static int pagemap_scan_writeback_args(struct pm_scan_arg *arg, 2395 unsigned long uargl) 2396 { 2397 struct pm_scan_arg __user *uarg = (void __user *)uargl; 2398 2399 if (copy_to_user(&uarg->walk_end, &arg->walk_end, sizeof(arg->walk_end))) 2400 return -EFAULT; 2401 2402 return 0; 2403 } 2404 2405 static int pagemap_scan_init_bounce_buffer(struct pagemap_scan_private *p) 2406 { 2407 if (!p->arg.vec_len) 2408 return 0; 2409 2410 p->vec_buf_len = min_t(size_t, PAGEMAP_WALK_SIZE >> PAGE_SHIFT, 2411 p->arg.vec_len); 2412 p->vec_buf = kmalloc_array(p->vec_buf_len, sizeof(*p->vec_buf), 2413 GFP_KERNEL); 2414 if (!p->vec_buf) 2415 return -ENOMEM; 2416 2417 p->vec_buf->start = p->vec_buf->end = 0; 2418 p->vec_out = (struct page_region __user *)(long)p->arg.vec; 2419 2420 return 0; 2421 } 2422 2423 static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p) 2424 { 2425 const struct page_region *buf = p->vec_buf; 2426 long n = p->vec_buf_index; 2427 2428 if (!p->vec_buf) 2429 return 0; 2430 2431 if (buf[n].end != buf[n].start) 2432 n++; 2433 2434 if (!n) 2435 return 0; 2436 2437 if (copy_to_user(p->vec_out, buf, n * sizeof(*buf))) 2438 return -EFAULT; 2439 2440 p->arg.vec_len -= n; 2441 p->vec_out += n; 2442 2443 p->vec_buf_index = 0; 2444 p->vec_buf_len = min_t(size_t, p->vec_buf_len, p->arg.vec_len); 2445 p->vec_buf->start = p->vec_buf->end = 0; 2446 2447 return n; 2448 } 2449 2450 static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg) 2451 { 2452 struct pagemap_scan_private p = {0}; 2453 unsigned long walk_start; 2454 size_t n_ranges_out = 0; 2455 int ret; 2456 2457 ret = pagemap_scan_get_args(&p.arg, uarg); 2458 if (ret) 2459 return ret; 2460 2461 p.masks_of_interest = p.arg.category_mask | p.arg.category_anyof_mask | 2462 p.arg.return_mask; 2463 ret = pagemap_scan_init_bounce_buffer(&p); 2464 if (ret) 2465 return ret; 2466 2467 for (walk_start = p.arg.start; walk_start < p.arg.end; 2468 walk_start = p.arg.walk_end) { 2469 struct mmu_notifier_range range; 2470 long n_out; 2471 2472 if (fatal_signal_pending(current)) { 2473 ret = -EINTR; 2474 break; 2475 } 2476 2477 ret = mmap_read_lock_killable(mm); 2478 if (ret) 2479 break; 2480 2481 /* Protection change for the range is going to happen. */ 2482 if (p.arg.flags & PM_SCAN_WP_MATCHING) { 2483 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0, 2484 mm, walk_start, p.arg.end); 2485 mmu_notifier_invalidate_range_start(&range); 2486 } 2487 2488 ret = walk_page_range(mm, walk_start, p.arg.end, 2489 &pagemap_scan_ops, &p); 2490 2491 if (p.arg.flags & PM_SCAN_WP_MATCHING) 2492 mmu_notifier_invalidate_range_end(&range); 2493 2494 mmap_read_unlock(mm); 2495 2496 n_out = pagemap_scan_flush_buffer(&p); 2497 if (n_out < 0) 2498 ret = n_out; 2499 else 2500 n_ranges_out += n_out; 2501 2502 if (ret != -ENOSPC) 2503 break; 2504 2505 if (p.arg.vec_len == 0 || p.found_pages == p.arg.max_pages) 2506 break; 2507 } 2508 2509 /* ENOSPC signifies early stop (buffer full) from the walk. */ 2510 if (!ret || ret == -ENOSPC) 2511 ret = n_ranges_out; 2512 2513 /* The walk_end isn't set when ret is zero */ 2514 if (!p.arg.walk_end) 2515 p.arg.walk_end = p.arg.end; 2516 if (pagemap_scan_writeback_args(&p.arg, uarg)) 2517 ret = -EFAULT; 2518 2519 kfree(p.vec_buf); 2520 return ret; 2521 } 2522 2523 static long do_pagemap_cmd(struct file *file, unsigned int cmd, 2524 unsigned long arg) 2525 { 2526 struct mm_struct *mm = file->private_data; 2527 2528 switch (cmd) { 2529 case PAGEMAP_SCAN: 2530 return do_pagemap_scan(mm, arg); 2531 2532 default: 2533 return -EINVAL; 2534 } 2535 } 2536 2537 const struct file_operations proc_pagemap_operations = { 2538 .llseek = mem_lseek, /* borrow this */ 2539 .read = pagemap_read, 2540 .open = pagemap_open, 2541 .release = pagemap_release, 2542 .unlocked_ioctl = do_pagemap_cmd, 2543 .compat_ioctl = do_pagemap_cmd, 2544 }; 2545 #endif /* CONFIG_PROC_PAGE_MONITOR */ 2546 2547 #ifdef CONFIG_NUMA 2548 2549 struct numa_maps { 2550 unsigned long pages; 2551 unsigned long anon; 2552 unsigned long active; 2553 unsigned long writeback; 2554 unsigned long mapcount_max; 2555 unsigned long dirty; 2556 unsigned long swapcache; 2557 unsigned long node[MAX_NUMNODES]; 2558 }; 2559 2560 struct numa_maps_private { 2561 struct proc_maps_private proc_maps; 2562 struct numa_maps md; 2563 }; 2564 2565 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, 2566 unsigned long nr_pages) 2567 { 2568 struct folio *folio = page_folio(page); 2569 int count = page_mapcount(page); 2570 2571 md->pages += nr_pages; 2572 if (pte_dirty || folio_test_dirty(folio)) 2573 md->dirty += nr_pages; 2574 2575 if (folio_test_swapcache(folio)) 2576 md->swapcache += nr_pages; 2577 2578 if (folio_test_active(folio) || folio_test_unevictable(folio)) 2579 md->active += nr_pages; 2580 2581 if (folio_test_writeback(folio)) 2582 md->writeback += nr_pages; 2583 2584 if (folio_test_anon(folio)) 2585 md->anon += nr_pages; 2586 2587 if (count > md->mapcount_max) 2588 md->mapcount_max = count; 2589 2590 md->node[folio_nid(folio)] += nr_pages; 2591 } 2592 2593 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, 2594 unsigned long addr) 2595 { 2596 struct page *page; 2597 int nid; 2598 2599 if (!pte_present(pte)) 2600 return NULL; 2601 2602 page = vm_normal_page(vma, addr, pte); 2603 if (!page || is_zone_device_page(page)) 2604 return NULL; 2605 2606 if (PageReserved(page)) 2607 return NULL; 2608 2609 nid = page_to_nid(page); 2610 if (!node_isset(nid, node_states[N_MEMORY])) 2611 return NULL; 2612 2613 return page; 2614 } 2615 2616 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2617 static struct page *can_gather_numa_stats_pmd(pmd_t pmd, 2618 struct vm_area_struct *vma, 2619 unsigned long addr) 2620 { 2621 struct page *page; 2622 int nid; 2623 2624 if (!pmd_present(pmd)) 2625 return NULL; 2626 2627 page = vm_normal_page_pmd(vma, addr, pmd); 2628 if (!page) 2629 return NULL; 2630 2631 if (PageReserved(page)) 2632 return NULL; 2633 2634 nid = page_to_nid(page); 2635 if (!node_isset(nid, node_states[N_MEMORY])) 2636 return NULL; 2637 2638 return page; 2639 } 2640 #endif 2641 2642 static int gather_pte_stats(pmd_t *pmd, unsigned long addr, 2643 unsigned long end, struct mm_walk *walk) 2644 { 2645 struct numa_maps *md = walk->private; 2646 struct vm_area_struct *vma = walk->vma; 2647 spinlock_t *ptl; 2648 pte_t *orig_pte; 2649 pte_t *pte; 2650 2651 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2652 ptl = pmd_trans_huge_lock(pmd, vma); 2653 if (ptl) { 2654 struct page *page; 2655 2656 page = can_gather_numa_stats_pmd(*pmd, vma, addr); 2657 if (page) 2658 gather_stats(page, md, pmd_dirty(*pmd), 2659 HPAGE_PMD_SIZE/PAGE_SIZE); 2660 spin_unlock(ptl); 2661 return 0; 2662 } 2663 #endif 2664 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); 2665 if (!pte) { 2666 walk->action = ACTION_AGAIN; 2667 return 0; 2668 } 2669 do { 2670 pte_t ptent = ptep_get(pte); 2671 struct page *page = can_gather_numa_stats(ptent, vma, addr); 2672 if (!page) 2673 continue; 2674 gather_stats(page, md, pte_dirty(ptent), 1); 2675 2676 } while (pte++, addr += PAGE_SIZE, addr != end); 2677 pte_unmap_unlock(orig_pte, ptl); 2678 cond_resched(); 2679 return 0; 2680 } 2681 #ifdef CONFIG_HUGETLB_PAGE 2682 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 2683 unsigned long addr, unsigned long end, struct mm_walk *walk) 2684 { 2685 pte_t huge_pte = huge_ptep_get(pte); 2686 struct numa_maps *md; 2687 struct page *page; 2688 2689 if (!pte_present(huge_pte)) 2690 return 0; 2691 2692 page = pte_page(huge_pte); 2693 2694 md = walk->private; 2695 gather_stats(page, md, pte_dirty(huge_pte), 1); 2696 return 0; 2697 } 2698 2699 #else 2700 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 2701 unsigned long addr, unsigned long end, struct mm_walk *walk) 2702 { 2703 return 0; 2704 } 2705 #endif 2706 2707 static const struct mm_walk_ops show_numa_ops = { 2708 .hugetlb_entry = gather_hugetlb_stats, 2709 .pmd_entry = gather_pte_stats, 2710 .walk_lock = PGWALK_RDLOCK, 2711 }; 2712 2713 /* 2714 * Display pages allocated per node and memory policy via /proc. 2715 */ 2716 static int show_numa_map(struct seq_file *m, void *v) 2717 { 2718 struct numa_maps_private *numa_priv = m->private; 2719 struct proc_maps_private *proc_priv = &numa_priv->proc_maps; 2720 struct vm_area_struct *vma = v; 2721 struct numa_maps *md = &numa_priv->md; 2722 struct file *file = vma->vm_file; 2723 struct mm_struct *mm = vma->vm_mm; 2724 char buffer[64]; 2725 struct mempolicy *pol; 2726 pgoff_t ilx; 2727 int nid; 2728 2729 if (!mm) 2730 return 0; 2731 2732 /* Ensure we start with an empty set of numa_maps statistics. */ 2733 memset(md, 0, sizeof(*md)); 2734 2735 pol = __get_vma_policy(vma, vma->vm_start, &ilx); 2736 if (pol) { 2737 mpol_to_str(buffer, sizeof(buffer), pol); 2738 mpol_cond_put(pol); 2739 } else { 2740 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy); 2741 } 2742 2743 seq_printf(m, "%08lx %s", vma->vm_start, buffer); 2744 2745 if (file) { 2746 seq_puts(m, " file="); 2747 seq_path(m, file_user_path(file), "\n\t= "); 2748 } else if (vma_is_initial_heap(vma)) { 2749 seq_puts(m, " heap"); 2750 } else if (vma_is_initial_stack(vma)) { 2751 seq_puts(m, " stack"); 2752 } 2753 2754 if (is_vm_hugetlb_page(vma)) 2755 seq_puts(m, " huge"); 2756 2757 /* mmap_lock is held by m_start */ 2758 walk_page_vma(vma, &show_numa_ops, md); 2759 2760 if (!md->pages) 2761 goto out; 2762 2763 if (md->anon) 2764 seq_printf(m, " anon=%lu", md->anon); 2765 2766 if (md->dirty) 2767 seq_printf(m, " dirty=%lu", md->dirty); 2768 2769 if (md->pages != md->anon && md->pages != md->dirty) 2770 seq_printf(m, " mapped=%lu", md->pages); 2771 2772 if (md->mapcount_max > 1) 2773 seq_printf(m, " mapmax=%lu", md->mapcount_max); 2774 2775 if (md->swapcache) 2776 seq_printf(m, " swapcache=%lu", md->swapcache); 2777 2778 if (md->active < md->pages && !is_vm_hugetlb_page(vma)) 2779 seq_printf(m, " active=%lu", md->active); 2780 2781 if (md->writeback) 2782 seq_printf(m, " writeback=%lu", md->writeback); 2783 2784 for_each_node_state(nid, N_MEMORY) 2785 if (md->node[nid]) 2786 seq_printf(m, " N%d=%lu", nid, md->node[nid]); 2787 2788 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10); 2789 out: 2790 seq_putc(m, '\n'); 2791 return 0; 2792 } 2793 2794 static const struct seq_operations proc_pid_numa_maps_op = { 2795 .start = m_start, 2796 .next = m_next, 2797 .stop = m_stop, 2798 .show = show_numa_map, 2799 }; 2800 2801 static int pid_numa_maps_open(struct inode *inode, struct file *file) 2802 { 2803 return proc_maps_open(inode, file, &proc_pid_numa_maps_op, 2804 sizeof(struct numa_maps_private)); 2805 } 2806 2807 const struct file_operations proc_pid_numa_maps_operations = { 2808 .open = pid_numa_maps_open, 2809 .read = seq_read, 2810 .llseek = seq_lseek, 2811 .release = proc_map_release, 2812 }; 2813 2814 #endif /* CONFIG_NUMA */ 2815