1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/pagewalk.h> 3 #include <linux/vmacache.h> 4 #include <linux/mm_inline.h> 5 #include <linux/hugetlb.h> 6 #include <linux/huge_mm.h> 7 #include <linux/mount.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 24 #include <asm/elf.h> 25 #include <asm/tlb.h> 26 #include <asm/tlbflush.h> 27 #include "internal.h" 28 29 #define SEQ_PUT_DEC(str, val) \ 30 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8) 31 void task_mem(struct seq_file *m, struct mm_struct *mm) 32 { 33 unsigned long text, lib, swap, anon, file, shmem; 34 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; 35 36 anon = get_mm_counter(mm, MM_ANONPAGES); 37 file = get_mm_counter(mm, MM_FILEPAGES); 38 shmem = get_mm_counter(mm, MM_SHMEMPAGES); 39 40 /* 41 * Note: to minimize their overhead, mm maintains hiwater_vm and 42 * hiwater_rss only when about to *lower* total_vm or rss. Any 43 * collector of these hiwater stats must therefore get total_vm 44 * and rss too, which will usually be the higher. Barriers? not 45 * worth the effort, such snapshots can always be inconsistent. 46 */ 47 hiwater_vm = total_vm = mm->total_vm; 48 if (hiwater_vm < mm->hiwater_vm) 49 hiwater_vm = mm->hiwater_vm; 50 hiwater_rss = total_rss = anon + file + shmem; 51 if (hiwater_rss < mm->hiwater_rss) 52 hiwater_rss = mm->hiwater_rss; 53 54 /* split executable areas between text and lib */ 55 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK); 56 text = min(text, mm->exec_vm << PAGE_SHIFT); 57 lib = (mm->exec_vm << PAGE_SHIFT) - text; 58 59 swap = get_mm_counter(mm, MM_SWAPENTS); 60 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm); 61 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm); 62 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm); 63 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm)); 64 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss); 65 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss); 66 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon); 67 SEQ_PUT_DEC(" kB\nRssFile:\t", file); 68 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem); 69 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm); 70 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm); 71 seq_put_decimal_ull_width(m, 72 " kB\nVmExe:\t", text >> 10, 8); 73 seq_put_decimal_ull_width(m, 74 " kB\nVmLib:\t", lib >> 10, 8); 75 seq_put_decimal_ull_width(m, 76 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8); 77 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap); 78 seq_puts(m, " kB\n"); 79 hugetlb_report_usage(m, mm); 80 } 81 #undef SEQ_PUT_DEC 82 83 unsigned long task_vsize(struct mm_struct *mm) 84 { 85 return PAGE_SIZE * mm->total_vm; 86 } 87 88 unsigned long task_statm(struct mm_struct *mm, 89 unsigned long *shared, unsigned long *text, 90 unsigned long *data, unsigned long *resident) 91 { 92 *shared = get_mm_counter(mm, MM_FILEPAGES) + 93 get_mm_counter(mm, MM_SHMEMPAGES); 94 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) 95 >> PAGE_SHIFT; 96 *data = mm->data_vm + mm->stack_vm; 97 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); 98 return mm->total_vm; 99 } 100 101 #ifdef CONFIG_NUMA 102 /* 103 * Save get_task_policy() for show_numa_map(). 104 */ 105 static void hold_task_mempolicy(struct proc_maps_private *priv) 106 { 107 struct task_struct *task = priv->task; 108 109 task_lock(task); 110 priv->task_mempolicy = get_task_policy(task); 111 mpol_get(priv->task_mempolicy); 112 task_unlock(task); 113 } 114 static void release_task_mempolicy(struct proc_maps_private *priv) 115 { 116 mpol_put(priv->task_mempolicy); 117 } 118 #else 119 static void hold_task_mempolicy(struct proc_maps_private *priv) 120 { 121 } 122 static void release_task_mempolicy(struct proc_maps_private *priv) 123 { 124 } 125 #endif 126 127 static void *m_start(struct seq_file *m, loff_t *ppos) 128 { 129 struct proc_maps_private *priv = m->private; 130 unsigned long last_addr = *ppos; 131 struct mm_struct *mm; 132 struct vm_area_struct *vma; 133 134 /* See m_next(). Zero at the start or after lseek. */ 135 if (last_addr == -1UL) 136 return NULL; 137 138 priv->task = get_proc_task(priv->inode); 139 if (!priv->task) 140 return ERR_PTR(-ESRCH); 141 142 mm = priv->mm; 143 if (!mm || !mmget_not_zero(mm)) { 144 put_task_struct(priv->task); 145 priv->task = NULL; 146 return NULL; 147 } 148 149 if (mmap_read_lock_killable(mm)) { 150 mmput(mm); 151 put_task_struct(priv->task); 152 priv->task = NULL; 153 return ERR_PTR(-EINTR); 154 } 155 156 hold_task_mempolicy(priv); 157 priv->tail_vma = get_gate_vma(mm); 158 159 vma = find_vma(mm, last_addr); 160 if (vma) 161 return vma; 162 163 return priv->tail_vma; 164 } 165 166 static void *m_next(struct seq_file *m, void *v, loff_t *ppos) 167 { 168 struct proc_maps_private *priv = m->private; 169 struct vm_area_struct *next, *vma = v; 170 171 if (vma == priv->tail_vma) 172 next = NULL; 173 else if (vma->vm_next) 174 next = vma->vm_next; 175 else 176 next = priv->tail_vma; 177 178 *ppos = next ? next->vm_start : -1UL; 179 180 return next; 181 } 182 183 static void m_stop(struct seq_file *m, void *v) 184 { 185 struct proc_maps_private *priv = m->private; 186 struct mm_struct *mm = priv->mm; 187 188 if (!priv->task) 189 return; 190 191 release_task_mempolicy(priv); 192 mmap_read_unlock(mm); 193 mmput(mm); 194 put_task_struct(priv->task); 195 priv->task = NULL; 196 } 197 198 static int proc_maps_open(struct inode *inode, struct file *file, 199 const struct seq_operations *ops, int psize) 200 { 201 struct proc_maps_private *priv = __seq_open_private(file, ops, psize); 202 203 if (!priv) 204 return -ENOMEM; 205 206 priv->inode = inode; 207 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 208 if (IS_ERR(priv->mm)) { 209 int err = PTR_ERR(priv->mm); 210 211 seq_release_private(inode, file); 212 return err; 213 } 214 215 return 0; 216 } 217 218 static int proc_map_release(struct inode *inode, struct file *file) 219 { 220 struct seq_file *seq = file->private_data; 221 struct proc_maps_private *priv = seq->private; 222 223 if (priv->mm) 224 mmdrop(priv->mm); 225 226 return seq_release_private(inode, file); 227 } 228 229 static int do_maps_open(struct inode *inode, struct file *file, 230 const struct seq_operations *ops) 231 { 232 return proc_maps_open(inode, file, ops, 233 sizeof(struct proc_maps_private)); 234 } 235 236 /* 237 * Indicate if the VMA is a stack for the given task; for 238 * /proc/PID/maps that is the stack of the main task. 239 */ 240 static int is_stack(struct vm_area_struct *vma) 241 { 242 /* 243 * We make no effort to guess what a given thread considers to be 244 * its "stack". It's not even well-defined for programs written 245 * languages like Go. 246 */ 247 return vma->vm_start <= vma->vm_mm->start_stack && 248 vma->vm_end >= vma->vm_mm->start_stack; 249 } 250 251 static void show_vma_header_prefix(struct seq_file *m, 252 unsigned long start, unsigned long end, 253 vm_flags_t flags, unsigned long long pgoff, 254 dev_t dev, unsigned long ino) 255 { 256 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); 257 seq_put_hex_ll(m, NULL, start, 8); 258 seq_put_hex_ll(m, "-", end, 8); 259 seq_putc(m, ' '); 260 seq_putc(m, flags & VM_READ ? 'r' : '-'); 261 seq_putc(m, flags & VM_WRITE ? 'w' : '-'); 262 seq_putc(m, flags & VM_EXEC ? 'x' : '-'); 263 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p'); 264 seq_put_hex_ll(m, " ", pgoff, 8); 265 seq_put_hex_ll(m, " ", MAJOR(dev), 2); 266 seq_put_hex_ll(m, ":", MINOR(dev), 2); 267 seq_put_decimal_ull(m, " ", ino); 268 seq_putc(m, ' '); 269 } 270 271 static void 272 show_map_vma(struct seq_file *m, struct vm_area_struct *vma) 273 { 274 struct mm_struct *mm = vma->vm_mm; 275 struct file *file = vma->vm_file; 276 vm_flags_t flags = vma->vm_flags; 277 unsigned long ino = 0; 278 unsigned long long pgoff = 0; 279 unsigned long start, end; 280 dev_t dev = 0; 281 const char *name = NULL; 282 283 if (file) { 284 struct inode *inode = file_inode(vma->vm_file); 285 dev = inode->i_sb->s_dev; 286 ino = inode->i_ino; 287 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; 288 } 289 290 start = vma->vm_start; 291 end = vma->vm_end; 292 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino); 293 294 /* 295 * Print the dentry name for named mappings, and a 296 * special [heap] marker for the heap: 297 */ 298 if (file) { 299 seq_pad(m, ' '); 300 seq_file_path(m, file, "\n"); 301 goto done; 302 } 303 304 if (vma->vm_ops && vma->vm_ops->name) { 305 name = vma->vm_ops->name(vma); 306 if (name) 307 goto done; 308 } 309 310 name = arch_vma_name(vma); 311 if (!name) { 312 const char *anon_name; 313 314 if (!mm) { 315 name = "[vdso]"; 316 goto done; 317 } 318 319 if (vma->vm_start <= mm->brk && 320 vma->vm_end >= mm->start_brk) { 321 name = "[heap]"; 322 goto done; 323 } 324 325 if (is_stack(vma)) { 326 name = "[stack]"; 327 goto done; 328 } 329 330 anon_name = vma_anon_name(vma); 331 if (anon_name) { 332 seq_pad(m, ' '); 333 seq_printf(m, "[anon:%s]", anon_name); 334 } 335 } 336 337 done: 338 if (name) { 339 seq_pad(m, ' '); 340 seq_puts(m, name); 341 } 342 seq_putc(m, '\n'); 343 } 344 345 static int show_map(struct seq_file *m, void *v) 346 { 347 show_map_vma(m, v); 348 return 0; 349 } 350 351 static const struct seq_operations proc_pid_maps_op = { 352 .start = m_start, 353 .next = m_next, 354 .stop = m_stop, 355 .show = show_map 356 }; 357 358 static int pid_maps_open(struct inode *inode, struct file *file) 359 { 360 return do_maps_open(inode, file, &proc_pid_maps_op); 361 } 362 363 const struct file_operations proc_pid_maps_operations = { 364 .open = pid_maps_open, 365 .read = seq_read, 366 .llseek = seq_lseek, 367 .release = proc_map_release, 368 }; 369 370 /* 371 * Proportional Set Size(PSS): my share of RSS. 372 * 373 * PSS of a process is the count of pages it has in memory, where each 374 * page is divided by the number of processes sharing it. So if a 375 * process has 1000 pages all to itself, and 1000 shared with one other 376 * process, its PSS will be 1500. 377 * 378 * To keep (accumulated) division errors low, we adopt a 64bit 379 * fixed-point pss counter to minimize division errors. So (pss >> 380 * PSS_SHIFT) would be the real byte count. 381 * 382 * A shift of 12 before division means (assuming 4K page size): 383 * - 1M 3-user-pages add up to 8KB errors; 384 * - supports mapcount up to 2^24, or 16M; 385 * - supports PSS up to 2^52 bytes, or 4PB. 386 */ 387 #define PSS_SHIFT 12 388 389 #ifdef CONFIG_PROC_PAGE_MONITOR 390 struct mem_size_stats { 391 unsigned long resident; 392 unsigned long shared_clean; 393 unsigned long shared_dirty; 394 unsigned long private_clean; 395 unsigned long private_dirty; 396 unsigned long referenced; 397 unsigned long anonymous; 398 unsigned long lazyfree; 399 unsigned long anonymous_thp; 400 unsigned long shmem_thp; 401 unsigned long file_thp; 402 unsigned long swap; 403 unsigned long shared_hugetlb; 404 unsigned long private_hugetlb; 405 u64 pss; 406 u64 pss_anon; 407 u64 pss_file; 408 u64 pss_shmem; 409 u64 pss_locked; 410 u64 swap_pss; 411 }; 412 413 static void smaps_page_accumulate(struct mem_size_stats *mss, 414 struct page *page, unsigned long size, unsigned long pss, 415 bool dirty, bool locked, bool private) 416 { 417 mss->pss += pss; 418 419 if (PageAnon(page)) 420 mss->pss_anon += pss; 421 else if (PageSwapBacked(page)) 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 || PageDirty(page)) { 430 if (private) 431 mss->private_dirty += size; 432 else 433 mss->shared_dirty += size; 434 } else { 435 if (private) 436 mss->private_clean += size; 437 else 438 mss->shared_clean += size; 439 } 440 } 441 442 static void smaps_account(struct mem_size_stats *mss, struct page *page, 443 bool compound, bool young, bool dirty, bool locked) 444 { 445 int i, nr = compound ? compound_nr(page) : 1; 446 unsigned long size = nr * PAGE_SIZE; 447 448 /* 449 * First accumulate quantities that depend only on |size| and the type 450 * of the compound page. 451 */ 452 if (PageAnon(page)) { 453 mss->anonymous += size; 454 if (!PageSwapBacked(page) && !dirty && !PageDirty(page)) 455 mss->lazyfree += size; 456 } 457 458 mss->resident += size; 459 /* Accumulate the size in pages that have been accessed. */ 460 if (young || page_is_young(page) || PageReferenced(page)) 461 mss->referenced += size; 462 463 /* 464 * Then accumulate quantities that may depend on sharing, or that may 465 * differ page-by-page. 466 * 467 * page_count(page) == 1 guarantees the page is mapped exactly once. 468 * If any subpage of the compound page mapped with PTE it would elevate 469 * page_count(). 470 */ 471 if (page_count(page) == 1) { 472 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty, 473 locked, true); 474 return; 475 } 476 for (i = 0; i < nr; i++, page++) { 477 int mapcount = page_mapcount(page); 478 unsigned long pss = PAGE_SIZE << PSS_SHIFT; 479 if (mapcount >= 2) 480 pss /= mapcount; 481 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked, 482 mapcount < 2); 483 } 484 } 485 486 #ifdef CONFIG_SHMEM 487 static int smaps_pte_hole(unsigned long addr, unsigned long end, 488 __always_unused int depth, struct mm_walk *walk) 489 { 490 struct mem_size_stats *mss = walk->private; 491 struct vm_area_struct *vma = walk->vma; 492 493 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping, 494 linear_page_index(vma, addr), 495 linear_page_index(vma, end)); 496 497 return 0; 498 } 499 #else 500 #define smaps_pte_hole NULL 501 #endif /* CONFIG_SHMEM */ 502 503 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk) 504 { 505 #ifdef CONFIG_SHMEM 506 if (walk->ops->pte_hole) { 507 /* depth is not used */ 508 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk); 509 } 510 #endif 511 } 512 513 static void smaps_pte_entry(pte_t *pte, unsigned long addr, 514 struct mm_walk *walk) 515 { 516 struct mem_size_stats *mss = walk->private; 517 struct vm_area_struct *vma = walk->vma; 518 bool locked = !!(vma->vm_flags & VM_LOCKED); 519 struct page *page = NULL; 520 521 if (pte_present(*pte)) { 522 page = vm_normal_page(vma, addr, *pte); 523 } else if (is_swap_pte(*pte)) { 524 swp_entry_t swpent = pte_to_swp_entry(*pte); 525 526 if (!non_swap_entry(swpent)) { 527 int mapcount; 528 529 mss->swap += PAGE_SIZE; 530 mapcount = swp_swapcount(swpent); 531 if (mapcount >= 2) { 532 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT; 533 534 do_div(pss_delta, mapcount); 535 mss->swap_pss += pss_delta; 536 } else { 537 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT; 538 } 539 } else if (is_pfn_swap_entry(swpent)) 540 page = pfn_swap_entry_to_page(swpent); 541 } else { 542 smaps_pte_hole_lookup(addr, walk); 543 return; 544 } 545 546 if (!page) 547 return; 548 549 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked); 550 } 551 552 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 553 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 554 struct mm_walk *walk) 555 { 556 struct mem_size_stats *mss = walk->private; 557 struct vm_area_struct *vma = walk->vma; 558 bool locked = !!(vma->vm_flags & VM_LOCKED); 559 struct page *page = NULL; 560 561 if (pmd_present(*pmd)) { 562 /* FOLL_DUMP will return -EFAULT on huge zero page */ 563 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP); 564 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) { 565 swp_entry_t entry = pmd_to_swp_entry(*pmd); 566 567 if (is_migration_entry(entry)) 568 page = pfn_swap_entry_to_page(entry); 569 } 570 if (IS_ERR_OR_NULL(page)) 571 return; 572 if (PageAnon(page)) 573 mss->anonymous_thp += HPAGE_PMD_SIZE; 574 else if (PageSwapBacked(page)) 575 mss->shmem_thp += HPAGE_PMD_SIZE; 576 else if (is_zone_device_page(page)) 577 /* pass */; 578 else 579 mss->file_thp += HPAGE_PMD_SIZE; 580 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked); 581 } 582 #else 583 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 584 struct mm_walk *walk) 585 { 586 } 587 #endif 588 589 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 590 struct mm_walk *walk) 591 { 592 struct vm_area_struct *vma = walk->vma; 593 pte_t *pte; 594 spinlock_t *ptl; 595 596 ptl = pmd_trans_huge_lock(pmd, vma); 597 if (ptl) { 598 smaps_pmd_entry(pmd, addr, walk); 599 spin_unlock(ptl); 600 goto out; 601 } 602 603 if (pmd_trans_unstable(pmd)) 604 goto out; 605 /* 606 * The mmap_lock held all the way back in m_start() is what 607 * keeps khugepaged out of here and from collapsing things 608 * in here. 609 */ 610 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 611 for (; addr != end; pte++, addr += PAGE_SIZE) 612 smaps_pte_entry(pte, addr, walk); 613 pte_unmap_unlock(pte - 1, ptl); 614 out: 615 cond_resched(); 616 return 0; 617 } 618 619 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) 620 { 621 /* 622 * Don't forget to update Documentation/ on changes. 623 */ 624 static const char mnemonics[BITS_PER_LONG][2] = { 625 /* 626 * In case if we meet a flag we don't know about. 627 */ 628 [0 ... (BITS_PER_LONG-1)] = "??", 629 630 [ilog2(VM_READ)] = "rd", 631 [ilog2(VM_WRITE)] = "wr", 632 [ilog2(VM_EXEC)] = "ex", 633 [ilog2(VM_SHARED)] = "sh", 634 [ilog2(VM_MAYREAD)] = "mr", 635 [ilog2(VM_MAYWRITE)] = "mw", 636 [ilog2(VM_MAYEXEC)] = "me", 637 [ilog2(VM_MAYSHARE)] = "ms", 638 [ilog2(VM_GROWSDOWN)] = "gd", 639 [ilog2(VM_PFNMAP)] = "pf", 640 [ilog2(VM_LOCKED)] = "lo", 641 [ilog2(VM_IO)] = "io", 642 [ilog2(VM_SEQ_READ)] = "sr", 643 [ilog2(VM_RAND_READ)] = "rr", 644 [ilog2(VM_DONTCOPY)] = "dc", 645 [ilog2(VM_DONTEXPAND)] = "de", 646 [ilog2(VM_ACCOUNT)] = "ac", 647 [ilog2(VM_NORESERVE)] = "nr", 648 [ilog2(VM_HUGETLB)] = "ht", 649 [ilog2(VM_SYNC)] = "sf", 650 [ilog2(VM_ARCH_1)] = "ar", 651 [ilog2(VM_WIPEONFORK)] = "wf", 652 [ilog2(VM_DONTDUMP)] = "dd", 653 #ifdef CONFIG_ARM64_BTI 654 [ilog2(VM_ARM64_BTI)] = "bt", 655 #endif 656 #ifdef CONFIG_MEM_SOFT_DIRTY 657 [ilog2(VM_SOFTDIRTY)] = "sd", 658 #endif 659 [ilog2(VM_MIXEDMAP)] = "mm", 660 [ilog2(VM_HUGEPAGE)] = "hg", 661 [ilog2(VM_NOHUGEPAGE)] = "nh", 662 [ilog2(VM_MERGEABLE)] = "mg", 663 [ilog2(VM_UFFD_MISSING)]= "um", 664 [ilog2(VM_UFFD_WP)] = "uw", 665 #ifdef CONFIG_ARM64_MTE 666 [ilog2(VM_MTE)] = "mt", 667 [ilog2(VM_MTE_ALLOWED)] = "", 668 #endif 669 #ifdef CONFIG_ARCH_HAS_PKEYS 670 /* These come out via ProtectionKey: */ 671 [ilog2(VM_PKEY_BIT0)] = "", 672 [ilog2(VM_PKEY_BIT1)] = "", 673 [ilog2(VM_PKEY_BIT2)] = "", 674 [ilog2(VM_PKEY_BIT3)] = "", 675 #if VM_PKEY_BIT4 676 [ilog2(VM_PKEY_BIT4)] = "", 677 #endif 678 #endif /* CONFIG_ARCH_HAS_PKEYS */ 679 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 680 [ilog2(VM_UFFD_MINOR)] = "ui", 681 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ 682 }; 683 size_t i; 684 685 seq_puts(m, "VmFlags: "); 686 for (i = 0; i < BITS_PER_LONG; i++) { 687 if (!mnemonics[i][0]) 688 continue; 689 if (vma->vm_flags & (1UL << i)) { 690 seq_putc(m, mnemonics[i][0]); 691 seq_putc(m, mnemonics[i][1]); 692 seq_putc(m, ' '); 693 } 694 } 695 seq_putc(m, '\n'); 696 } 697 698 #ifdef CONFIG_HUGETLB_PAGE 699 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, 700 unsigned long addr, unsigned long end, 701 struct mm_walk *walk) 702 { 703 struct mem_size_stats *mss = walk->private; 704 struct vm_area_struct *vma = walk->vma; 705 struct page *page = NULL; 706 707 if (pte_present(*pte)) { 708 page = vm_normal_page(vma, addr, *pte); 709 } else if (is_swap_pte(*pte)) { 710 swp_entry_t swpent = pte_to_swp_entry(*pte); 711 712 if (is_pfn_swap_entry(swpent)) 713 page = pfn_swap_entry_to_page(swpent); 714 } 715 if (page) { 716 int mapcount = page_mapcount(page); 717 718 if (mapcount >= 2) 719 mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); 720 else 721 mss->private_hugetlb += huge_page_size(hstate_vma(vma)); 722 } 723 return 0; 724 } 725 #else 726 #define smaps_hugetlb_range NULL 727 #endif /* HUGETLB_PAGE */ 728 729 static const struct mm_walk_ops smaps_walk_ops = { 730 .pmd_entry = smaps_pte_range, 731 .hugetlb_entry = smaps_hugetlb_range, 732 }; 733 734 static const struct mm_walk_ops smaps_shmem_walk_ops = { 735 .pmd_entry = smaps_pte_range, 736 .hugetlb_entry = smaps_hugetlb_range, 737 .pte_hole = smaps_pte_hole, 738 }; 739 740 /* 741 * Gather mem stats from @vma with the indicated beginning 742 * address @start, and keep them in @mss. 743 * 744 * Use vm_start of @vma as the beginning address if @start is 0. 745 */ 746 static void smap_gather_stats(struct vm_area_struct *vma, 747 struct mem_size_stats *mss, unsigned long start) 748 { 749 const struct mm_walk_ops *ops = &smaps_walk_ops; 750 751 /* Invalid start */ 752 if (start >= vma->vm_end) 753 return; 754 755 #ifdef CONFIG_SHMEM 756 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) { 757 /* 758 * For shared or readonly shmem mappings we know that all 759 * swapped out pages belong to the shmem object, and we can 760 * obtain the swap value much more efficiently. For private 761 * writable mappings, we might have COW pages that are 762 * not affected by the parent swapped out pages of the shmem 763 * object, so we have to distinguish them during the page walk. 764 * Unless we know that the shmem object (or the part mapped by 765 * our VMA) has no swapped out pages at all. 766 */ 767 unsigned long shmem_swapped = shmem_swap_usage(vma); 768 769 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) || 770 !(vma->vm_flags & VM_WRITE))) { 771 mss->swap += shmem_swapped; 772 } else { 773 ops = &smaps_shmem_walk_ops; 774 } 775 } 776 #endif 777 /* mmap_lock is held in m_start */ 778 if (!start) 779 walk_page_vma(vma, ops, mss); 780 else 781 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss); 782 } 783 784 #define SEQ_PUT_DEC(str, val) \ 785 seq_put_decimal_ull_width(m, str, (val) >> 10, 8) 786 787 /* Show the contents common for smaps and smaps_rollup */ 788 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss, 789 bool rollup_mode) 790 { 791 SEQ_PUT_DEC("Rss: ", mss->resident); 792 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT); 793 if (rollup_mode) { 794 /* 795 * These are meaningful only for smaps_rollup, otherwise two of 796 * them are zero, and the other one is the same as Pss. 797 */ 798 SEQ_PUT_DEC(" kB\nPss_Anon: ", 799 mss->pss_anon >> PSS_SHIFT); 800 SEQ_PUT_DEC(" kB\nPss_File: ", 801 mss->pss_file >> PSS_SHIFT); 802 SEQ_PUT_DEC(" kB\nPss_Shmem: ", 803 mss->pss_shmem >> PSS_SHIFT); 804 } 805 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean); 806 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty); 807 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean); 808 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); 809 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); 810 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); 811 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); 812 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); 813 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp); 814 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp); 815 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb); 816 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ", 817 mss->private_hugetlb >> 10, 7); 818 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap); 819 SEQ_PUT_DEC(" kB\nSwapPss: ", 820 mss->swap_pss >> PSS_SHIFT); 821 SEQ_PUT_DEC(" kB\nLocked: ", 822 mss->pss_locked >> PSS_SHIFT); 823 seq_puts(m, " kB\n"); 824 } 825 826 static int show_smap(struct seq_file *m, void *v) 827 { 828 struct vm_area_struct *vma = v; 829 struct mem_size_stats mss; 830 831 memset(&mss, 0, sizeof(mss)); 832 833 smap_gather_stats(vma, &mss, 0); 834 835 show_map_vma(m, vma); 836 837 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start); 838 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma)); 839 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma)); 840 seq_puts(m, " kB\n"); 841 842 __show_smap(m, &mss, false); 843 844 seq_printf(m, "THPeligible: %d\n", 845 transparent_hugepage_active(vma)); 846 847 if (arch_pkeys_enabled()) 848 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); 849 show_smap_vma_flags(m, vma); 850 851 return 0; 852 } 853 854 static int show_smaps_rollup(struct seq_file *m, void *v) 855 { 856 struct proc_maps_private *priv = m->private; 857 struct mem_size_stats mss; 858 struct mm_struct *mm; 859 struct vm_area_struct *vma; 860 unsigned long last_vma_end = 0; 861 int ret = 0; 862 863 priv->task = get_proc_task(priv->inode); 864 if (!priv->task) 865 return -ESRCH; 866 867 mm = priv->mm; 868 if (!mm || !mmget_not_zero(mm)) { 869 ret = -ESRCH; 870 goto out_put_task; 871 } 872 873 memset(&mss, 0, sizeof(mss)); 874 875 ret = mmap_read_lock_killable(mm); 876 if (ret) 877 goto out_put_mm; 878 879 hold_task_mempolicy(priv); 880 881 for (vma = priv->mm->mmap; vma;) { 882 smap_gather_stats(vma, &mss, 0); 883 last_vma_end = vma->vm_end; 884 885 /* 886 * Release mmap_lock temporarily if someone wants to 887 * access it for write request. 888 */ 889 if (mmap_lock_is_contended(mm)) { 890 mmap_read_unlock(mm); 891 ret = mmap_read_lock_killable(mm); 892 if (ret) { 893 release_task_mempolicy(priv); 894 goto out_put_mm; 895 } 896 897 /* 898 * After dropping the lock, there are four cases to 899 * consider. See the following example for explanation. 900 * 901 * +------+------+-----------+ 902 * | VMA1 | VMA2 | VMA3 | 903 * +------+------+-----------+ 904 * | | | | 905 * 4k 8k 16k 400k 906 * 907 * Suppose we drop the lock after reading VMA2 due to 908 * contention, then we get: 909 * 910 * last_vma_end = 16k 911 * 912 * 1) VMA2 is freed, but VMA3 exists: 913 * 914 * find_vma(mm, 16k - 1) will return VMA3. 915 * In this case, just continue from VMA3. 916 * 917 * 2) VMA2 still exists: 918 * 919 * find_vma(mm, 16k - 1) will return VMA2. 920 * Iterate the loop like the original one. 921 * 922 * 3) No more VMAs can be found: 923 * 924 * find_vma(mm, 16k - 1) will return NULL. 925 * No more things to do, just break. 926 * 927 * 4) (last_vma_end - 1) is the middle of a vma (VMA'): 928 * 929 * find_vma(mm, 16k - 1) will return VMA' whose range 930 * contains last_vma_end. 931 * Iterate VMA' from last_vma_end. 932 */ 933 vma = find_vma(mm, last_vma_end - 1); 934 /* Case 3 above */ 935 if (!vma) 936 break; 937 938 /* Case 1 above */ 939 if (vma->vm_start >= last_vma_end) 940 continue; 941 942 /* Case 4 above */ 943 if (vma->vm_end > last_vma_end) 944 smap_gather_stats(vma, &mss, last_vma_end); 945 } 946 /* Case 2 above */ 947 vma = vma->vm_next; 948 } 949 950 show_vma_header_prefix(m, priv->mm->mmap->vm_start, 951 last_vma_end, 0, 0, 0, 0); 952 seq_pad(m, ' '); 953 seq_puts(m, "[rollup]\n"); 954 955 __show_smap(m, &mss, true); 956 957 release_task_mempolicy(priv); 958 mmap_read_unlock(mm); 959 960 out_put_mm: 961 mmput(mm); 962 out_put_task: 963 put_task_struct(priv->task); 964 priv->task = NULL; 965 966 return ret; 967 } 968 #undef SEQ_PUT_DEC 969 970 static const struct seq_operations proc_pid_smaps_op = { 971 .start = m_start, 972 .next = m_next, 973 .stop = m_stop, 974 .show = show_smap 975 }; 976 977 static int pid_smaps_open(struct inode *inode, struct file *file) 978 { 979 return do_maps_open(inode, file, &proc_pid_smaps_op); 980 } 981 982 static int smaps_rollup_open(struct inode *inode, struct file *file) 983 { 984 int ret; 985 struct proc_maps_private *priv; 986 987 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT); 988 if (!priv) 989 return -ENOMEM; 990 991 ret = single_open(file, show_smaps_rollup, priv); 992 if (ret) 993 goto out_free; 994 995 priv->inode = inode; 996 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 997 if (IS_ERR(priv->mm)) { 998 ret = PTR_ERR(priv->mm); 999 1000 single_release(inode, file); 1001 goto out_free; 1002 } 1003 1004 return 0; 1005 1006 out_free: 1007 kfree(priv); 1008 return ret; 1009 } 1010 1011 static int smaps_rollup_release(struct inode *inode, struct file *file) 1012 { 1013 struct seq_file *seq = file->private_data; 1014 struct proc_maps_private *priv = seq->private; 1015 1016 if (priv->mm) 1017 mmdrop(priv->mm); 1018 1019 kfree(priv); 1020 return single_release(inode, file); 1021 } 1022 1023 const struct file_operations proc_pid_smaps_operations = { 1024 .open = pid_smaps_open, 1025 .read = seq_read, 1026 .llseek = seq_lseek, 1027 .release = proc_map_release, 1028 }; 1029 1030 const struct file_operations proc_pid_smaps_rollup_operations = { 1031 .open = smaps_rollup_open, 1032 .read = seq_read, 1033 .llseek = seq_lseek, 1034 .release = smaps_rollup_release, 1035 }; 1036 1037 enum clear_refs_types { 1038 CLEAR_REFS_ALL = 1, 1039 CLEAR_REFS_ANON, 1040 CLEAR_REFS_MAPPED, 1041 CLEAR_REFS_SOFT_DIRTY, 1042 CLEAR_REFS_MM_HIWATER_RSS, 1043 CLEAR_REFS_LAST, 1044 }; 1045 1046 struct clear_refs_private { 1047 enum clear_refs_types type; 1048 }; 1049 1050 #ifdef CONFIG_MEM_SOFT_DIRTY 1051 1052 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1053 { 1054 struct page *page; 1055 1056 if (!pte_write(pte)) 1057 return false; 1058 if (!is_cow_mapping(vma->vm_flags)) 1059 return false; 1060 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) 1061 return false; 1062 page = vm_normal_page(vma, addr, pte); 1063 if (!page) 1064 return false; 1065 return page_maybe_dma_pinned(page); 1066 } 1067 1068 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1069 unsigned long addr, pte_t *pte) 1070 { 1071 /* 1072 * The soft-dirty tracker uses #PF-s to catch writes 1073 * to pages, so write-protect the pte as well. See the 1074 * Documentation/admin-guide/mm/soft-dirty.rst for full description 1075 * of how soft-dirty works. 1076 */ 1077 pte_t ptent = *pte; 1078 1079 if (pte_present(ptent)) { 1080 pte_t old_pte; 1081 1082 if (pte_is_pinned(vma, addr, ptent)) 1083 return; 1084 old_pte = ptep_modify_prot_start(vma, addr, pte); 1085 ptent = pte_wrprotect(old_pte); 1086 ptent = pte_clear_soft_dirty(ptent); 1087 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); 1088 } else if (is_swap_pte(ptent)) { 1089 ptent = pte_swp_clear_soft_dirty(ptent); 1090 set_pte_at(vma->vm_mm, addr, pte, ptent); 1091 } 1092 } 1093 #else 1094 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1095 unsigned long addr, pte_t *pte) 1096 { 1097 } 1098 #endif 1099 1100 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 1101 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1102 unsigned long addr, pmd_t *pmdp) 1103 { 1104 pmd_t old, pmd = *pmdp; 1105 1106 if (pmd_present(pmd)) { 1107 /* See comment in change_huge_pmd() */ 1108 old = pmdp_invalidate(vma, addr, pmdp); 1109 if (pmd_dirty(old)) 1110 pmd = pmd_mkdirty(pmd); 1111 if (pmd_young(old)) 1112 pmd = pmd_mkyoung(pmd); 1113 1114 pmd = pmd_wrprotect(pmd); 1115 pmd = pmd_clear_soft_dirty(pmd); 1116 1117 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1118 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { 1119 pmd = pmd_swp_clear_soft_dirty(pmd); 1120 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1121 } 1122 } 1123 #else 1124 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1125 unsigned long addr, pmd_t *pmdp) 1126 { 1127 } 1128 #endif 1129 1130 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, 1131 unsigned long end, struct mm_walk *walk) 1132 { 1133 struct clear_refs_private *cp = walk->private; 1134 struct vm_area_struct *vma = walk->vma; 1135 pte_t *pte, ptent; 1136 spinlock_t *ptl; 1137 struct page *page; 1138 1139 ptl = pmd_trans_huge_lock(pmd, vma); 1140 if (ptl) { 1141 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1142 clear_soft_dirty_pmd(vma, addr, pmd); 1143 goto out; 1144 } 1145 1146 if (!pmd_present(*pmd)) 1147 goto out; 1148 1149 page = pmd_page(*pmd); 1150 1151 /* Clear accessed and referenced bits. */ 1152 pmdp_test_and_clear_young(vma, addr, pmd); 1153 test_and_clear_page_young(page); 1154 ClearPageReferenced(page); 1155 out: 1156 spin_unlock(ptl); 1157 return 0; 1158 } 1159 1160 if (pmd_trans_unstable(pmd)) 1161 return 0; 1162 1163 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 1164 for (; addr != end; pte++, addr += PAGE_SIZE) { 1165 ptent = *pte; 1166 1167 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1168 clear_soft_dirty(vma, addr, pte); 1169 continue; 1170 } 1171 1172 if (!pte_present(ptent)) 1173 continue; 1174 1175 page = vm_normal_page(vma, addr, ptent); 1176 if (!page) 1177 continue; 1178 1179 /* Clear accessed and referenced bits. */ 1180 ptep_test_and_clear_young(vma, addr, pte); 1181 test_and_clear_page_young(page); 1182 ClearPageReferenced(page); 1183 } 1184 pte_unmap_unlock(pte - 1, ptl); 1185 cond_resched(); 1186 return 0; 1187 } 1188 1189 static int clear_refs_test_walk(unsigned long start, unsigned long end, 1190 struct mm_walk *walk) 1191 { 1192 struct clear_refs_private *cp = walk->private; 1193 struct vm_area_struct *vma = walk->vma; 1194 1195 if (vma->vm_flags & VM_PFNMAP) 1196 return 1; 1197 1198 /* 1199 * Writing 1 to /proc/pid/clear_refs affects all pages. 1200 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages. 1201 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages. 1202 * Writing 4 to /proc/pid/clear_refs affects all pages. 1203 */ 1204 if (cp->type == CLEAR_REFS_ANON && vma->vm_file) 1205 return 1; 1206 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file) 1207 return 1; 1208 return 0; 1209 } 1210 1211 static const struct mm_walk_ops clear_refs_walk_ops = { 1212 .pmd_entry = clear_refs_pte_range, 1213 .test_walk = clear_refs_test_walk, 1214 }; 1215 1216 static ssize_t clear_refs_write(struct file *file, const char __user *buf, 1217 size_t count, loff_t *ppos) 1218 { 1219 struct task_struct *task; 1220 char buffer[PROC_NUMBUF]; 1221 struct mm_struct *mm; 1222 struct vm_area_struct *vma; 1223 enum clear_refs_types type; 1224 int itype; 1225 int rv; 1226 1227 memset(buffer, 0, sizeof(buffer)); 1228 if (count > sizeof(buffer) - 1) 1229 count = sizeof(buffer) - 1; 1230 if (copy_from_user(buffer, buf, count)) 1231 return -EFAULT; 1232 rv = kstrtoint(strstrip(buffer), 10, &itype); 1233 if (rv < 0) 1234 return rv; 1235 type = (enum clear_refs_types)itype; 1236 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) 1237 return -EINVAL; 1238 1239 task = get_proc_task(file_inode(file)); 1240 if (!task) 1241 return -ESRCH; 1242 mm = get_task_mm(task); 1243 if (mm) { 1244 struct mmu_notifier_range range; 1245 struct clear_refs_private cp = { 1246 .type = type, 1247 }; 1248 1249 if (mmap_write_lock_killable(mm)) { 1250 count = -EINTR; 1251 goto out_mm; 1252 } 1253 if (type == CLEAR_REFS_MM_HIWATER_RSS) { 1254 /* 1255 * Writing 5 to /proc/pid/clear_refs resets the peak 1256 * resident set size to this mm's current rss value. 1257 */ 1258 reset_mm_hiwater_rss(mm); 1259 goto out_unlock; 1260 } 1261 1262 if (type == CLEAR_REFS_SOFT_DIRTY) { 1263 for (vma = mm->mmap; vma; vma = vma->vm_next) { 1264 if (!(vma->vm_flags & VM_SOFTDIRTY)) 1265 continue; 1266 vma->vm_flags &= ~VM_SOFTDIRTY; 1267 vma_set_page_prot(vma); 1268 } 1269 1270 inc_tlb_flush_pending(mm); 1271 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY, 1272 0, NULL, mm, 0, -1UL); 1273 mmu_notifier_invalidate_range_start(&range); 1274 } 1275 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops, 1276 &cp); 1277 if (type == CLEAR_REFS_SOFT_DIRTY) { 1278 mmu_notifier_invalidate_range_end(&range); 1279 flush_tlb_mm(mm); 1280 dec_tlb_flush_pending(mm); 1281 } 1282 out_unlock: 1283 mmap_write_unlock(mm); 1284 out_mm: 1285 mmput(mm); 1286 } 1287 put_task_struct(task); 1288 1289 return count; 1290 } 1291 1292 const struct file_operations proc_clear_refs_operations = { 1293 .write = clear_refs_write, 1294 .llseek = noop_llseek, 1295 }; 1296 1297 typedef struct { 1298 u64 pme; 1299 } pagemap_entry_t; 1300 1301 struct pagemapread { 1302 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ 1303 pagemap_entry_t *buffer; 1304 bool show_pfn; 1305 }; 1306 1307 #define PAGEMAP_WALK_SIZE (PMD_SIZE) 1308 #define PAGEMAP_WALK_MASK (PMD_MASK) 1309 1310 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) 1311 #define PM_PFRAME_BITS 55 1312 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0) 1313 #define PM_SOFT_DIRTY BIT_ULL(55) 1314 #define PM_MMAP_EXCLUSIVE BIT_ULL(56) 1315 #define PM_UFFD_WP BIT_ULL(57) 1316 #define PM_FILE BIT_ULL(61) 1317 #define PM_SWAP BIT_ULL(62) 1318 #define PM_PRESENT BIT_ULL(63) 1319 1320 #define PM_END_OF_BUFFER 1 1321 1322 static inline pagemap_entry_t make_pme(u64 frame, u64 flags) 1323 { 1324 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags }; 1325 } 1326 1327 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme, 1328 struct pagemapread *pm) 1329 { 1330 pm->buffer[pm->pos++] = *pme; 1331 if (pm->pos >= pm->len) 1332 return PM_END_OF_BUFFER; 1333 return 0; 1334 } 1335 1336 static int pagemap_pte_hole(unsigned long start, unsigned long end, 1337 __always_unused int depth, struct mm_walk *walk) 1338 { 1339 struct pagemapread *pm = walk->private; 1340 unsigned long addr = start; 1341 int err = 0; 1342 1343 while (addr < end) { 1344 struct vm_area_struct *vma = find_vma(walk->mm, addr); 1345 pagemap_entry_t pme = make_pme(0, 0); 1346 /* End of address space hole, which we mark as non-present. */ 1347 unsigned long hole_end; 1348 1349 if (vma) 1350 hole_end = min(end, vma->vm_start); 1351 else 1352 hole_end = end; 1353 1354 for (; addr < hole_end; addr += PAGE_SIZE) { 1355 err = add_to_pagemap(addr, &pme, pm); 1356 if (err) 1357 goto out; 1358 } 1359 1360 if (!vma) 1361 break; 1362 1363 /* Addresses in the VMA. */ 1364 if (vma->vm_flags & VM_SOFTDIRTY) 1365 pme = make_pme(0, PM_SOFT_DIRTY); 1366 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) { 1367 err = add_to_pagemap(addr, &pme, pm); 1368 if (err) 1369 goto out; 1370 } 1371 } 1372 out: 1373 return err; 1374 } 1375 1376 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, 1377 struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1378 { 1379 u64 frame = 0, flags = 0; 1380 struct page *page = NULL; 1381 1382 if (pte_present(pte)) { 1383 if (pm->show_pfn) 1384 frame = pte_pfn(pte); 1385 flags |= PM_PRESENT; 1386 page = vm_normal_page(vma, addr, pte); 1387 if (pte_soft_dirty(pte)) 1388 flags |= PM_SOFT_DIRTY; 1389 if (pte_uffd_wp(pte)) 1390 flags |= PM_UFFD_WP; 1391 } else if (is_swap_pte(pte)) { 1392 swp_entry_t entry; 1393 if (pte_swp_soft_dirty(pte)) 1394 flags |= PM_SOFT_DIRTY; 1395 if (pte_swp_uffd_wp(pte)) 1396 flags |= PM_UFFD_WP; 1397 entry = pte_to_swp_entry(pte); 1398 if (pm->show_pfn) 1399 frame = swp_type(entry) | 1400 (swp_offset(entry) << MAX_SWAPFILES_SHIFT); 1401 flags |= PM_SWAP; 1402 if (is_pfn_swap_entry(entry)) 1403 page = pfn_swap_entry_to_page(entry); 1404 } 1405 1406 if (page && !PageAnon(page)) 1407 flags |= PM_FILE; 1408 if (page && page_mapcount(page) == 1) 1409 flags |= PM_MMAP_EXCLUSIVE; 1410 if (vma->vm_flags & VM_SOFTDIRTY) 1411 flags |= PM_SOFT_DIRTY; 1412 1413 return make_pme(frame, flags); 1414 } 1415 1416 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, 1417 struct mm_walk *walk) 1418 { 1419 struct vm_area_struct *vma = walk->vma; 1420 struct pagemapread *pm = walk->private; 1421 spinlock_t *ptl; 1422 pte_t *pte, *orig_pte; 1423 int err = 0; 1424 1425 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1426 ptl = pmd_trans_huge_lock(pmdp, vma); 1427 if (ptl) { 1428 u64 flags = 0, frame = 0; 1429 pmd_t pmd = *pmdp; 1430 struct page *page = NULL; 1431 1432 if (vma->vm_flags & VM_SOFTDIRTY) 1433 flags |= PM_SOFT_DIRTY; 1434 1435 if (pmd_present(pmd)) { 1436 page = pmd_page(pmd); 1437 1438 flags |= PM_PRESENT; 1439 if (pmd_soft_dirty(pmd)) 1440 flags |= PM_SOFT_DIRTY; 1441 if (pmd_uffd_wp(pmd)) 1442 flags |= PM_UFFD_WP; 1443 if (pm->show_pfn) 1444 frame = pmd_pfn(pmd) + 1445 ((addr & ~PMD_MASK) >> PAGE_SHIFT); 1446 } 1447 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 1448 else if (is_swap_pmd(pmd)) { 1449 swp_entry_t entry = pmd_to_swp_entry(pmd); 1450 unsigned long offset; 1451 1452 if (pm->show_pfn) { 1453 offset = swp_offset(entry) + 1454 ((addr & ~PMD_MASK) >> PAGE_SHIFT); 1455 frame = swp_type(entry) | 1456 (offset << MAX_SWAPFILES_SHIFT); 1457 } 1458 flags |= PM_SWAP; 1459 if (pmd_swp_soft_dirty(pmd)) 1460 flags |= PM_SOFT_DIRTY; 1461 if (pmd_swp_uffd_wp(pmd)) 1462 flags |= PM_UFFD_WP; 1463 VM_BUG_ON(!is_pmd_migration_entry(pmd)); 1464 page = pfn_swap_entry_to_page(entry); 1465 } 1466 #endif 1467 1468 if (page && page_mapcount(page) == 1) 1469 flags |= PM_MMAP_EXCLUSIVE; 1470 1471 for (; addr != end; addr += PAGE_SIZE) { 1472 pagemap_entry_t pme = make_pme(frame, flags); 1473 1474 err = add_to_pagemap(addr, &pme, pm); 1475 if (err) 1476 break; 1477 if (pm->show_pfn) { 1478 if (flags & PM_PRESENT) 1479 frame++; 1480 else if (flags & PM_SWAP) 1481 frame += (1 << MAX_SWAPFILES_SHIFT); 1482 } 1483 } 1484 spin_unlock(ptl); 1485 return err; 1486 } 1487 1488 if (pmd_trans_unstable(pmdp)) 1489 return 0; 1490 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1491 1492 /* 1493 * We can assume that @vma always points to a valid one and @end never 1494 * goes beyond vma->vm_end. 1495 */ 1496 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl); 1497 for (; addr < end; pte++, addr += PAGE_SIZE) { 1498 pagemap_entry_t pme; 1499 1500 pme = pte_to_pagemap_entry(pm, vma, addr, *pte); 1501 err = add_to_pagemap(addr, &pme, pm); 1502 if (err) 1503 break; 1504 } 1505 pte_unmap_unlock(orig_pte, ptl); 1506 1507 cond_resched(); 1508 1509 return err; 1510 } 1511 1512 #ifdef CONFIG_HUGETLB_PAGE 1513 /* This function walks within one hugetlb entry in the single call */ 1514 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, 1515 unsigned long addr, unsigned long end, 1516 struct mm_walk *walk) 1517 { 1518 struct pagemapread *pm = walk->private; 1519 struct vm_area_struct *vma = walk->vma; 1520 u64 flags = 0, frame = 0; 1521 int err = 0; 1522 pte_t pte; 1523 1524 if (vma->vm_flags & VM_SOFTDIRTY) 1525 flags |= PM_SOFT_DIRTY; 1526 1527 pte = huge_ptep_get(ptep); 1528 if (pte_present(pte)) { 1529 struct page *page = pte_page(pte); 1530 1531 if (!PageAnon(page)) 1532 flags |= PM_FILE; 1533 1534 if (page_mapcount(page) == 1) 1535 flags |= PM_MMAP_EXCLUSIVE; 1536 1537 flags |= PM_PRESENT; 1538 if (pm->show_pfn) 1539 frame = pte_pfn(pte) + 1540 ((addr & ~hmask) >> PAGE_SHIFT); 1541 } 1542 1543 for (; addr != end; addr += PAGE_SIZE) { 1544 pagemap_entry_t pme = make_pme(frame, flags); 1545 1546 err = add_to_pagemap(addr, &pme, pm); 1547 if (err) 1548 return err; 1549 if (pm->show_pfn && (flags & PM_PRESENT)) 1550 frame++; 1551 } 1552 1553 cond_resched(); 1554 1555 return err; 1556 } 1557 #else 1558 #define pagemap_hugetlb_range NULL 1559 #endif /* HUGETLB_PAGE */ 1560 1561 static const struct mm_walk_ops pagemap_ops = { 1562 .pmd_entry = pagemap_pmd_range, 1563 .pte_hole = pagemap_pte_hole, 1564 .hugetlb_entry = pagemap_hugetlb_range, 1565 }; 1566 1567 /* 1568 * /proc/pid/pagemap - an array mapping virtual pages to pfns 1569 * 1570 * For each page in the address space, this file contains one 64-bit entry 1571 * consisting of the following: 1572 * 1573 * Bits 0-54 page frame number (PFN) if present 1574 * Bits 0-4 swap type if swapped 1575 * Bits 5-54 swap offset if swapped 1576 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst) 1577 * Bit 56 page exclusively mapped 1578 * Bits 57-60 zero 1579 * Bit 61 page is file-page or shared-anon 1580 * Bit 62 page swapped 1581 * Bit 63 page present 1582 * 1583 * If the page is not present but in swap, then the PFN contains an 1584 * encoding of the swap file number and the page's offset into the 1585 * swap. Unmapped pages return a null PFN. This allows determining 1586 * precisely which pages are mapped (or in swap) and comparing mapped 1587 * pages between processes. 1588 * 1589 * Efficient users of this interface will use /proc/pid/maps to 1590 * determine which areas of memory are actually mapped and llseek to 1591 * skip over unmapped regions. 1592 */ 1593 static ssize_t pagemap_read(struct file *file, char __user *buf, 1594 size_t count, loff_t *ppos) 1595 { 1596 struct mm_struct *mm = file->private_data; 1597 struct pagemapread pm; 1598 unsigned long src; 1599 unsigned long svpfn; 1600 unsigned long start_vaddr; 1601 unsigned long end_vaddr; 1602 int ret = 0, copied = 0; 1603 1604 if (!mm || !mmget_not_zero(mm)) 1605 goto out; 1606 1607 ret = -EINVAL; 1608 /* file position must be aligned */ 1609 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) 1610 goto out_mm; 1611 1612 ret = 0; 1613 if (!count) 1614 goto out_mm; 1615 1616 /* do not disclose physical addresses: attack vector */ 1617 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN); 1618 1619 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); 1620 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL); 1621 ret = -ENOMEM; 1622 if (!pm.buffer) 1623 goto out_mm; 1624 1625 src = *ppos; 1626 svpfn = src / PM_ENTRY_BYTES; 1627 end_vaddr = mm->task_size; 1628 1629 /* watch out for wraparound */ 1630 start_vaddr = end_vaddr; 1631 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) 1632 start_vaddr = untagged_addr(svpfn << PAGE_SHIFT); 1633 1634 /* Ensure the address is inside the task */ 1635 if (start_vaddr > mm->task_size) 1636 start_vaddr = end_vaddr; 1637 1638 /* 1639 * The odds are that this will stop walking way 1640 * before end_vaddr, because the length of the 1641 * user buffer is tracked in "pm", and the walk 1642 * will stop when we hit the end of the buffer. 1643 */ 1644 ret = 0; 1645 while (count && (start_vaddr < end_vaddr)) { 1646 int len; 1647 unsigned long end; 1648 1649 pm.pos = 0; 1650 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; 1651 /* overflow ? */ 1652 if (end < start_vaddr || end > end_vaddr) 1653 end = end_vaddr; 1654 ret = mmap_read_lock_killable(mm); 1655 if (ret) 1656 goto out_free; 1657 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm); 1658 mmap_read_unlock(mm); 1659 start_vaddr = end; 1660 1661 len = min(count, PM_ENTRY_BYTES * pm.pos); 1662 if (copy_to_user(buf, pm.buffer, len)) { 1663 ret = -EFAULT; 1664 goto out_free; 1665 } 1666 copied += len; 1667 buf += len; 1668 count -= len; 1669 } 1670 *ppos += copied; 1671 if (!ret || ret == PM_END_OF_BUFFER) 1672 ret = copied; 1673 1674 out_free: 1675 kfree(pm.buffer); 1676 out_mm: 1677 mmput(mm); 1678 out: 1679 return ret; 1680 } 1681 1682 static int pagemap_open(struct inode *inode, struct file *file) 1683 { 1684 struct mm_struct *mm; 1685 1686 mm = proc_mem_open(inode, PTRACE_MODE_READ); 1687 if (IS_ERR(mm)) 1688 return PTR_ERR(mm); 1689 file->private_data = mm; 1690 return 0; 1691 } 1692 1693 static int pagemap_release(struct inode *inode, struct file *file) 1694 { 1695 struct mm_struct *mm = file->private_data; 1696 1697 if (mm) 1698 mmdrop(mm); 1699 return 0; 1700 } 1701 1702 const struct file_operations proc_pagemap_operations = { 1703 .llseek = mem_lseek, /* borrow this */ 1704 .read = pagemap_read, 1705 .open = pagemap_open, 1706 .release = pagemap_release, 1707 }; 1708 #endif /* CONFIG_PROC_PAGE_MONITOR */ 1709 1710 #ifdef CONFIG_NUMA 1711 1712 struct numa_maps { 1713 unsigned long pages; 1714 unsigned long anon; 1715 unsigned long active; 1716 unsigned long writeback; 1717 unsigned long mapcount_max; 1718 unsigned long dirty; 1719 unsigned long swapcache; 1720 unsigned long node[MAX_NUMNODES]; 1721 }; 1722 1723 struct numa_maps_private { 1724 struct proc_maps_private proc_maps; 1725 struct numa_maps md; 1726 }; 1727 1728 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, 1729 unsigned long nr_pages) 1730 { 1731 int count = page_mapcount(page); 1732 1733 md->pages += nr_pages; 1734 if (pte_dirty || PageDirty(page)) 1735 md->dirty += nr_pages; 1736 1737 if (PageSwapCache(page)) 1738 md->swapcache += nr_pages; 1739 1740 if (PageActive(page) || PageUnevictable(page)) 1741 md->active += nr_pages; 1742 1743 if (PageWriteback(page)) 1744 md->writeback += nr_pages; 1745 1746 if (PageAnon(page)) 1747 md->anon += nr_pages; 1748 1749 if (count > md->mapcount_max) 1750 md->mapcount_max = count; 1751 1752 md->node[page_to_nid(page)] += nr_pages; 1753 } 1754 1755 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, 1756 unsigned long addr) 1757 { 1758 struct page *page; 1759 int nid; 1760 1761 if (!pte_present(pte)) 1762 return NULL; 1763 1764 page = vm_normal_page(vma, addr, pte); 1765 if (!page) 1766 return NULL; 1767 1768 if (PageReserved(page)) 1769 return NULL; 1770 1771 nid = page_to_nid(page); 1772 if (!node_isset(nid, node_states[N_MEMORY])) 1773 return NULL; 1774 1775 return page; 1776 } 1777 1778 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1779 static struct page *can_gather_numa_stats_pmd(pmd_t pmd, 1780 struct vm_area_struct *vma, 1781 unsigned long addr) 1782 { 1783 struct page *page; 1784 int nid; 1785 1786 if (!pmd_present(pmd)) 1787 return NULL; 1788 1789 page = vm_normal_page_pmd(vma, addr, pmd); 1790 if (!page) 1791 return NULL; 1792 1793 if (PageReserved(page)) 1794 return NULL; 1795 1796 nid = page_to_nid(page); 1797 if (!node_isset(nid, node_states[N_MEMORY])) 1798 return NULL; 1799 1800 return page; 1801 } 1802 #endif 1803 1804 static int gather_pte_stats(pmd_t *pmd, unsigned long addr, 1805 unsigned long end, struct mm_walk *walk) 1806 { 1807 struct numa_maps *md = walk->private; 1808 struct vm_area_struct *vma = walk->vma; 1809 spinlock_t *ptl; 1810 pte_t *orig_pte; 1811 pte_t *pte; 1812 1813 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1814 ptl = pmd_trans_huge_lock(pmd, vma); 1815 if (ptl) { 1816 struct page *page; 1817 1818 page = can_gather_numa_stats_pmd(*pmd, vma, addr); 1819 if (page) 1820 gather_stats(page, md, pmd_dirty(*pmd), 1821 HPAGE_PMD_SIZE/PAGE_SIZE); 1822 spin_unlock(ptl); 1823 return 0; 1824 } 1825 1826 if (pmd_trans_unstable(pmd)) 1827 return 0; 1828 #endif 1829 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); 1830 do { 1831 struct page *page = can_gather_numa_stats(*pte, vma, addr); 1832 if (!page) 1833 continue; 1834 gather_stats(page, md, pte_dirty(*pte), 1); 1835 1836 } while (pte++, addr += PAGE_SIZE, addr != end); 1837 pte_unmap_unlock(orig_pte, ptl); 1838 cond_resched(); 1839 return 0; 1840 } 1841 #ifdef CONFIG_HUGETLB_PAGE 1842 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 1843 unsigned long addr, unsigned long end, struct mm_walk *walk) 1844 { 1845 pte_t huge_pte = huge_ptep_get(pte); 1846 struct numa_maps *md; 1847 struct page *page; 1848 1849 if (!pte_present(huge_pte)) 1850 return 0; 1851 1852 page = pte_page(huge_pte); 1853 if (!page) 1854 return 0; 1855 1856 md = walk->private; 1857 gather_stats(page, md, pte_dirty(huge_pte), 1); 1858 return 0; 1859 } 1860 1861 #else 1862 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 1863 unsigned long addr, unsigned long end, struct mm_walk *walk) 1864 { 1865 return 0; 1866 } 1867 #endif 1868 1869 static const struct mm_walk_ops show_numa_ops = { 1870 .hugetlb_entry = gather_hugetlb_stats, 1871 .pmd_entry = gather_pte_stats, 1872 }; 1873 1874 /* 1875 * Display pages allocated per node and memory policy via /proc. 1876 */ 1877 static int show_numa_map(struct seq_file *m, void *v) 1878 { 1879 struct numa_maps_private *numa_priv = m->private; 1880 struct proc_maps_private *proc_priv = &numa_priv->proc_maps; 1881 struct vm_area_struct *vma = v; 1882 struct numa_maps *md = &numa_priv->md; 1883 struct file *file = vma->vm_file; 1884 struct mm_struct *mm = vma->vm_mm; 1885 struct mempolicy *pol; 1886 char buffer[64]; 1887 int nid; 1888 1889 if (!mm) 1890 return 0; 1891 1892 /* Ensure we start with an empty set of numa_maps statistics. */ 1893 memset(md, 0, sizeof(*md)); 1894 1895 pol = __get_vma_policy(vma, vma->vm_start); 1896 if (pol) { 1897 mpol_to_str(buffer, sizeof(buffer), pol); 1898 mpol_cond_put(pol); 1899 } else { 1900 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy); 1901 } 1902 1903 seq_printf(m, "%08lx %s", vma->vm_start, buffer); 1904 1905 if (file) { 1906 seq_puts(m, " file="); 1907 seq_file_path(m, file, "\n\t= "); 1908 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { 1909 seq_puts(m, " heap"); 1910 } else if (is_stack(vma)) { 1911 seq_puts(m, " stack"); 1912 } 1913 1914 if (is_vm_hugetlb_page(vma)) 1915 seq_puts(m, " huge"); 1916 1917 /* mmap_lock is held by m_start */ 1918 walk_page_vma(vma, &show_numa_ops, md); 1919 1920 if (!md->pages) 1921 goto out; 1922 1923 if (md->anon) 1924 seq_printf(m, " anon=%lu", md->anon); 1925 1926 if (md->dirty) 1927 seq_printf(m, " dirty=%lu", md->dirty); 1928 1929 if (md->pages != md->anon && md->pages != md->dirty) 1930 seq_printf(m, " mapped=%lu", md->pages); 1931 1932 if (md->mapcount_max > 1) 1933 seq_printf(m, " mapmax=%lu", md->mapcount_max); 1934 1935 if (md->swapcache) 1936 seq_printf(m, " swapcache=%lu", md->swapcache); 1937 1938 if (md->active < md->pages && !is_vm_hugetlb_page(vma)) 1939 seq_printf(m, " active=%lu", md->active); 1940 1941 if (md->writeback) 1942 seq_printf(m, " writeback=%lu", md->writeback); 1943 1944 for_each_node_state(nid, N_MEMORY) 1945 if (md->node[nid]) 1946 seq_printf(m, " N%d=%lu", nid, md->node[nid]); 1947 1948 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10); 1949 out: 1950 seq_putc(m, '\n'); 1951 return 0; 1952 } 1953 1954 static const struct seq_operations proc_pid_numa_maps_op = { 1955 .start = m_start, 1956 .next = m_next, 1957 .stop = m_stop, 1958 .show = show_numa_map, 1959 }; 1960 1961 static int pid_numa_maps_open(struct inode *inode, struct file *file) 1962 { 1963 return proc_maps_open(inode, file, &proc_pid_numa_maps_op, 1964 sizeof(struct numa_maps_private)); 1965 } 1966 1967 const struct file_operations proc_pid_numa_maps_operations = { 1968 .open = pid_numa_maps_open, 1969 .read = seq_read, 1970 .llseek = seq_lseek, 1971 .release = proc_map_release, 1972 }; 1973 1974 #endif /* CONFIG_NUMA */ 1975