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 struct anon_vma_name *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 = anon_vma_name(vma); 331 if (anon_name) { 332 seq_pad(m, ' '); 333 seq_printf(m, "[anon:%s]", anon_name->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_dirty; 410 u64 pss_locked; 411 u64 swap_pss; 412 }; 413 414 static void smaps_page_accumulate(struct mem_size_stats *mss, 415 struct page *page, unsigned long size, unsigned long pss, 416 bool dirty, bool locked, bool private) 417 { 418 mss->pss += pss; 419 420 if (PageAnon(page)) 421 mss->pss_anon += pss; 422 else if (PageSwapBacked(page)) 423 mss->pss_shmem += pss; 424 else 425 mss->pss_file += pss; 426 427 if (locked) 428 mss->pss_locked += pss; 429 430 if (dirty || PageDirty(page)) { 431 mss->pss_dirty += pss; 432 if (private) 433 mss->private_dirty += size; 434 else 435 mss->shared_dirty += size; 436 } else { 437 if (private) 438 mss->private_clean += size; 439 else 440 mss->shared_clean += size; 441 } 442 } 443 444 static void smaps_account(struct mem_size_stats *mss, struct page *page, 445 bool compound, bool young, bool dirty, bool locked, 446 bool migration) 447 { 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 (PageAnon(page)) { 456 mss->anonymous += size; 457 if (!PageSwapBacked(page) && !dirty && !PageDirty(page)) 458 mss->lazyfree += size; 459 } 460 461 mss->resident += size; 462 /* Accumulate the size in pages that have been accessed. */ 463 if (young || page_is_young(page) || PageReferenced(page)) 464 mss->referenced += size; 465 466 /* 467 * Then accumulate quantities that may depend on sharing, or that may 468 * differ page-by-page. 469 * 470 * page_count(page) == 1 guarantees the page is mapped exactly once. 471 * If any subpage of the compound page mapped with PTE it would elevate 472 * page_count(). 473 * 474 * The page_mapcount() is called to get a snapshot of the mapcount. 475 * Without holding the page lock this snapshot can be slightly wrong as 476 * we cannot always read the mapcount atomically. It is not safe to 477 * call page_mapcount() even with PTL held if the page is not mapped, 478 * especially for migration entries. Treat regular migration entries 479 * as mapcount == 1. 480 */ 481 if ((page_count(page) == 1) || migration) { 482 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty, 483 locked, true); 484 return; 485 } 486 for (i = 0; i < nr; i++, page++) { 487 int mapcount = page_mapcount(page); 488 unsigned long pss = PAGE_SIZE << PSS_SHIFT; 489 if (mapcount >= 2) 490 pss /= mapcount; 491 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked, 492 mapcount < 2); 493 } 494 } 495 496 #ifdef CONFIG_SHMEM 497 static int smaps_pte_hole(unsigned long addr, unsigned long end, 498 __always_unused int depth, struct mm_walk *walk) 499 { 500 struct mem_size_stats *mss = walk->private; 501 struct vm_area_struct *vma = walk->vma; 502 503 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping, 504 linear_page_index(vma, addr), 505 linear_page_index(vma, end)); 506 507 return 0; 508 } 509 #else 510 #define smaps_pte_hole NULL 511 #endif /* CONFIG_SHMEM */ 512 513 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk) 514 { 515 #ifdef CONFIG_SHMEM 516 if (walk->ops->pte_hole) { 517 /* depth is not used */ 518 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk); 519 } 520 #endif 521 } 522 523 static void smaps_pte_entry(pte_t *pte, unsigned long addr, 524 struct mm_walk *walk) 525 { 526 struct mem_size_stats *mss = walk->private; 527 struct vm_area_struct *vma = walk->vma; 528 bool locked = !!(vma->vm_flags & VM_LOCKED); 529 struct page *page = NULL; 530 bool migration = false; 531 532 if (pte_present(*pte)) { 533 page = vm_normal_page(vma, addr, *pte); 534 } else if (is_swap_pte(*pte)) { 535 swp_entry_t swpent = pte_to_swp_entry(*pte); 536 537 if (!non_swap_entry(swpent)) { 538 int mapcount; 539 540 mss->swap += PAGE_SIZE; 541 mapcount = swp_swapcount(swpent); 542 if (mapcount >= 2) { 543 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT; 544 545 do_div(pss_delta, mapcount); 546 mss->swap_pss += pss_delta; 547 } else { 548 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT; 549 } 550 } else if (is_pfn_swap_entry(swpent)) { 551 if (is_migration_entry(swpent)) 552 migration = true; 553 page = pfn_swap_entry_to_page(swpent); 554 } 555 } else { 556 smaps_pte_hole_lookup(addr, walk); 557 return; 558 } 559 560 if (!page) 561 return; 562 563 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), 564 locked, migration); 565 } 566 567 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 568 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 569 struct mm_walk *walk) 570 { 571 struct mem_size_stats *mss = walk->private; 572 struct vm_area_struct *vma = walk->vma; 573 bool locked = !!(vma->vm_flags & VM_LOCKED); 574 struct page *page = NULL; 575 bool migration = false; 576 577 if (pmd_present(*pmd)) { 578 /* FOLL_DUMP will return -EFAULT on huge zero page */ 579 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP); 580 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) { 581 swp_entry_t entry = pmd_to_swp_entry(*pmd); 582 583 if (is_migration_entry(entry)) { 584 migration = true; 585 page = pfn_swap_entry_to_page(entry); 586 } 587 } 588 if (IS_ERR_OR_NULL(page)) 589 return; 590 if (PageAnon(page)) 591 mss->anonymous_thp += HPAGE_PMD_SIZE; 592 else if (PageSwapBacked(page)) 593 mss->shmem_thp += HPAGE_PMD_SIZE; 594 else if (is_zone_device_page(page)) 595 /* pass */; 596 else 597 mss->file_thp += HPAGE_PMD_SIZE; 598 599 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), 600 locked, migration); 601 } 602 #else 603 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 604 struct mm_walk *walk) 605 { 606 } 607 #endif 608 609 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 610 struct mm_walk *walk) 611 { 612 struct vm_area_struct *vma = walk->vma; 613 pte_t *pte; 614 spinlock_t *ptl; 615 616 ptl = pmd_trans_huge_lock(pmd, vma); 617 if (ptl) { 618 smaps_pmd_entry(pmd, addr, walk); 619 spin_unlock(ptl); 620 goto out; 621 } 622 623 if (pmd_trans_unstable(pmd)) 624 goto out; 625 /* 626 * The mmap_lock held all the way back in m_start() is what 627 * keeps khugepaged out of here and from collapsing things 628 * in here. 629 */ 630 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 631 for (; addr != end; pte++, addr += PAGE_SIZE) 632 smaps_pte_entry(pte, addr, walk); 633 pte_unmap_unlock(pte - 1, ptl); 634 out: 635 cond_resched(); 636 return 0; 637 } 638 639 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) 640 { 641 /* 642 * Don't forget to update Documentation/ on changes. 643 */ 644 static const char mnemonics[BITS_PER_LONG][2] = { 645 /* 646 * In case if we meet a flag we don't know about. 647 */ 648 [0 ... (BITS_PER_LONG-1)] = "??", 649 650 [ilog2(VM_READ)] = "rd", 651 [ilog2(VM_WRITE)] = "wr", 652 [ilog2(VM_EXEC)] = "ex", 653 [ilog2(VM_SHARED)] = "sh", 654 [ilog2(VM_MAYREAD)] = "mr", 655 [ilog2(VM_MAYWRITE)] = "mw", 656 [ilog2(VM_MAYEXEC)] = "me", 657 [ilog2(VM_MAYSHARE)] = "ms", 658 [ilog2(VM_GROWSDOWN)] = "gd", 659 [ilog2(VM_PFNMAP)] = "pf", 660 [ilog2(VM_LOCKED)] = "lo", 661 [ilog2(VM_IO)] = "io", 662 [ilog2(VM_SEQ_READ)] = "sr", 663 [ilog2(VM_RAND_READ)] = "rr", 664 [ilog2(VM_DONTCOPY)] = "dc", 665 [ilog2(VM_DONTEXPAND)] = "de", 666 [ilog2(VM_ACCOUNT)] = "ac", 667 [ilog2(VM_NORESERVE)] = "nr", 668 [ilog2(VM_HUGETLB)] = "ht", 669 [ilog2(VM_SYNC)] = "sf", 670 [ilog2(VM_ARCH_1)] = "ar", 671 [ilog2(VM_WIPEONFORK)] = "wf", 672 [ilog2(VM_DONTDUMP)] = "dd", 673 #ifdef CONFIG_ARM64_BTI 674 [ilog2(VM_ARM64_BTI)] = "bt", 675 #endif 676 #ifdef CONFIG_MEM_SOFT_DIRTY 677 [ilog2(VM_SOFTDIRTY)] = "sd", 678 #endif 679 [ilog2(VM_MIXEDMAP)] = "mm", 680 [ilog2(VM_HUGEPAGE)] = "hg", 681 [ilog2(VM_NOHUGEPAGE)] = "nh", 682 [ilog2(VM_MERGEABLE)] = "mg", 683 [ilog2(VM_UFFD_MISSING)]= "um", 684 [ilog2(VM_UFFD_WP)] = "uw", 685 #ifdef CONFIG_ARM64_MTE 686 [ilog2(VM_MTE)] = "mt", 687 [ilog2(VM_MTE_ALLOWED)] = "", 688 #endif 689 #ifdef CONFIG_ARCH_HAS_PKEYS 690 /* These come out via ProtectionKey: */ 691 [ilog2(VM_PKEY_BIT0)] = "", 692 [ilog2(VM_PKEY_BIT1)] = "", 693 [ilog2(VM_PKEY_BIT2)] = "", 694 [ilog2(VM_PKEY_BIT3)] = "", 695 #if VM_PKEY_BIT4 696 [ilog2(VM_PKEY_BIT4)] = "", 697 #endif 698 #endif /* CONFIG_ARCH_HAS_PKEYS */ 699 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 700 [ilog2(VM_UFFD_MINOR)] = "ui", 701 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ 702 }; 703 size_t i; 704 705 seq_puts(m, "VmFlags: "); 706 for (i = 0; i < BITS_PER_LONG; i++) { 707 if (!mnemonics[i][0]) 708 continue; 709 if (vma->vm_flags & (1UL << i)) { 710 seq_putc(m, mnemonics[i][0]); 711 seq_putc(m, mnemonics[i][1]); 712 seq_putc(m, ' '); 713 } 714 } 715 seq_putc(m, '\n'); 716 } 717 718 #ifdef CONFIG_HUGETLB_PAGE 719 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, 720 unsigned long addr, unsigned long end, 721 struct mm_walk *walk) 722 { 723 struct mem_size_stats *mss = walk->private; 724 struct vm_area_struct *vma = walk->vma; 725 struct page *page = NULL; 726 727 if (pte_present(*pte)) { 728 page = vm_normal_page(vma, addr, *pte); 729 } else if (is_swap_pte(*pte)) { 730 swp_entry_t swpent = pte_to_swp_entry(*pte); 731 732 if (is_pfn_swap_entry(swpent)) 733 page = pfn_swap_entry_to_page(swpent); 734 } 735 if (page) { 736 int mapcount = page_mapcount(page); 737 738 if (mapcount >= 2) 739 mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); 740 else 741 mss->private_hugetlb += huge_page_size(hstate_vma(vma)); 742 } 743 return 0; 744 } 745 #else 746 #define smaps_hugetlb_range NULL 747 #endif /* HUGETLB_PAGE */ 748 749 static const struct mm_walk_ops smaps_walk_ops = { 750 .pmd_entry = smaps_pte_range, 751 .hugetlb_entry = smaps_hugetlb_range, 752 }; 753 754 static const struct mm_walk_ops smaps_shmem_walk_ops = { 755 .pmd_entry = smaps_pte_range, 756 .hugetlb_entry = smaps_hugetlb_range, 757 .pte_hole = smaps_pte_hole, 758 }; 759 760 /* 761 * Gather mem stats from @vma with the indicated beginning 762 * address @start, and keep them in @mss. 763 * 764 * Use vm_start of @vma as the beginning address if @start is 0. 765 */ 766 static void smap_gather_stats(struct vm_area_struct *vma, 767 struct mem_size_stats *mss, unsigned long start) 768 { 769 const struct mm_walk_ops *ops = &smaps_walk_ops; 770 771 /* Invalid start */ 772 if (start >= vma->vm_end) 773 return; 774 775 #ifdef CONFIG_SHMEM 776 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) { 777 /* 778 * For shared or readonly shmem mappings we know that all 779 * swapped out pages belong to the shmem object, and we can 780 * obtain the swap value much more efficiently. For private 781 * writable mappings, we might have COW pages that are 782 * not affected by the parent swapped out pages of the shmem 783 * object, so we have to distinguish them during the page walk. 784 * Unless we know that the shmem object (or the part mapped by 785 * our VMA) has no swapped out pages at all. 786 */ 787 unsigned long shmem_swapped = shmem_swap_usage(vma); 788 789 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) || 790 !(vma->vm_flags & VM_WRITE))) { 791 mss->swap += shmem_swapped; 792 } else { 793 ops = &smaps_shmem_walk_ops; 794 } 795 } 796 #endif 797 /* mmap_lock is held in m_start */ 798 if (!start) 799 walk_page_vma(vma, ops, mss); 800 else 801 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss); 802 } 803 804 #define SEQ_PUT_DEC(str, val) \ 805 seq_put_decimal_ull_width(m, str, (val) >> 10, 8) 806 807 /* Show the contents common for smaps and smaps_rollup */ 808 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss, 809 bool rollup_mode) 810 { 811 SEQ_PUT_DEC("Rss: ", mss->resident); 812 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT); 813 SEQ_PUT_DEC(" kB\nPss_Dirty: ", mss->pss_dirty >> PSS_SHIFT); 814 if (rollup_mode) { 815 /* 816 * These are meaningful only for smaps_rollup, otherwise two of 817 * them are zero, and the other one is the same as Pss. 818 */ 819 SEQ_PUT_DEC(" kB\nPss_Anon: ", 820 mss->pss_anon >> PSS_SHIFT); 821 SEQ_PUT_DEC(" kB\nPss_File: ", 822 mss->pss_file >> PSS_SHIFT); 823 SEQ_PUT_DEC(" kB\nPss_Shmem: ", 824 mss->pss_shmem >> PSS_SHIFT); 825 } 826 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean); 827 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty); 828 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean); 829 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); 830 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); 831 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); 832 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); 833 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); 834 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp); 835 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp); 836 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb); 837 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ", 838 mss->private_hugetlb >> 10, 7); 839 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap); 840 SEQ_PUT_DEC(" kB\nSwapPss: ", 841 mss->swap_pss >> PSS_SHIFT); 842 SEQ_PUT_DEC(" kB\nLocked: ", 843 mss->pss_locked >> PSS_SHIFT); 844 seq_puts(m, " kB\n"); 845 } 846 847 static int show_smap(struct seq_file *m, void *v) 848 { 849 struct vm_area_struct *vma = v; 850 struct mem_size_stats mss; 851 852 memset(&mss, 0, sizeof(mss)); 853 854 smap_gather_stats(vma, &mss, 0); 855 856 show_map_vma(m, vma); 857 858 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start); 859 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma)); 860 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma)); 861 seq_puts(m, " kB\n"); 862 863 __show_smap(m, &mss, false); 864 865 seq_printf(m, "THPeligible: %d\n", 866 hugepage_vma_check(vma, vma->vm_flags, true, false)); 867 868 if (arch_pkeys_enabled()) 869 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); 870 show_smap_vma_flags(m, vma); 871 872 return 0; 873 } 874 875 static int show_smaps_rollup(struct seq_file *m, void *v) 876 { 877 struct proc_maps_private *priv = m->private; 878 struct mem_size_stats mss; 879 struct mm_struct *mm; 880 struct vm_area_struct *vma; 881 unsigned long last_vma_end = 0; 882 int ret = 0; 883 884 priv->task = get_proc_task(priv->inode); 885 if (!priv->task) 886 return -ESRCH; 887 888 mm = priv->mm; 889 if (!mm || !mmget_not_zero(mm)) { 890 ret = -ESRCH; 891 goto out_put_task; 892 } 893 894 memset(&mss, 0, sizeof(mss)); 895 896 ret = mmap_read_lock_killable(mm); 897 if (ret) 898 goto out_put_mm; 899 900 hold_task_mempolicy(priv); 901 902 for (vma = priv->mm->mmap; vma;) { 903 smap_gather_stats(vma, &mss, 0); 904 last_vma_end = vma->vm_end; 905 906 /* 907 * Release mmap_lock temporarily if someone wants to 908 * access it for write request. 909 */ 910 if (mmap_lock_is_contended(mm)) { 911 mmap_read_unlock(mm); 912 ret = mmap_read_lock_killable(mm); 913 if (ret) { 914 release_task_mempolicy(priv); 915 goto out_put_mm; 916 } 917 918 /* 919 * After dropping the lock, there are four cases to 920 * consider. See the following example for explanation. 921 * 922 * +------+------+-----------+ 923 * | VMA1 | VMA2 | VMA3 | 924 * +------+------+-----------+ 925 * | | | | 926 * 4k 8k 16k 400k 927 * 928 * Suppose we drop the lock after reading VMA2 due to 929 * contention, then we get: 930 * 931 * last_vma_end = 16k 932 * 933 * 1) VMA2 is freed, but VMA3 exists: 934 * 935 * find_vma(mm, 16k - 1) will return VMA3. 936 * In this case, just continue from VMA3. 937 * 938 * 2) VMA2 still exists: 939 * 940 * find_vma(mm, 16k - 1) will return VMA2. 941 * Iterate the loop like the original one. 942 * 943 * 3) No more VMAs can be found: 944 * 945 * find_vma(mm, 16k - 1) will return NULL. 946 * No more things to do, just break. 947 * 948 * 4) (last_vma_end - 1) is the middle of a vma (VMA'): 949 * 950 * find_vma(mm, 16k - 1) will return VMA' whose range 951 * contains last_vma_end. 952 * Iterate VMA' from last_vma_end. 953 */ 954 vma = find_vma(mm, last_vma_end - 1); 955 /* Case 3 above */ 956 if (!vma) 957 break; 958 959 /* Case 1 above */ 960 if (vma->vm_start >= last_vma_end) 961 continue; 962 963 /* Case 4 above */ 964 if (vma->vm_end > last_vma_end) 965 smap_gather_stats(vma, &mss, last_vma_end); 966 } 967 /* Case 2 above */ 968 vma = vma->vm_next; 969 } 970 971 show_vma_header_prefix(m, priv->mm->mmap->vm_start, 972 last_vma_end, 0, 0, 0, 0); 973 seq_pad(m, ' '); 974 seq_puts(m, "[rollup]\n"); 975 976 __show_smap(m, &mss, true); 977 978 release_task_mempolicy(priv); 979 mmap_read_unlock(mm); 980 981 out_put_mm: 982 mmput(mm); 983 out_put_task: 984 put_task_struct(priv->task); 985 priv->task = NULL; 986 987 return ret; 988 } 989 #undef SEQ_PUT_DEC 990 991 static const struct seq_operations proc_pid_smaps_op = { 992 .start = m_start, 993 .next = m_next, 994 .stop = m_stop, 995 .show = show_smap 996 }; 997 998 static int pid_smaps_open(struct inode *inode, struct file *file) 999 { 1000 return do_maps_open(inode, file, &proc_pid_smaps_op); 1001 } 1002 1003 static int smaps_rollup_open(struct inode *inode, struct file *file) 1004 { 1005 int ret; 1006 struct proc_maps_private *priv; 1007 1008 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT); 1009 if (!priv) 1010 return -ENOMEM; 1011 1012 ret = single_open(file, show_smaps_rollup, priv); 1013 if (ret) 1014 goto out_free; 1015 1016 priv->inode = inode; 1017 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 1018 if (IS_ERR(priv->mm)) { 1019 ret = PTR_ERR(priv->mm); 1020 1021 single_release(inode, file); 1022 goto out_free; 1023 } 1024 1025 return 0; 1026 1027 out_free: 1028 kfree(priv); 1029 return ret; 1030 } 1031 1032 static int smaps_rollup_release(struct inode *inode, struct file *file) 1033 { 1034 struct seq_file *seq = file->private_data; 1035 struct proc_maps_private *priv = seq->private; 1036 1037 if (priv->mm) 1038 mmdrop(priv->mm); 1039 1040 kfree(priv); 1041 return single_release(inode, file); 1042 } 1043 1044 const struct file_operations proc_pid_smaps_operations = { 1045 .open = pid_smaps_open, 1046 .read = seq_read, 1047 .llseek = seq_lseek, 1048 .release = proc_map_release, 1049 }; 1050 1051 const struct file_operations proc_pid_smaps_rollup_operations = { 1052 .open = smaps_rollup_open, 1053 .read = seq_read, 1054 .llseek = seq_lseek, 1055 .release = smaps_rollup_release, 1056 }; 1057 1058 enum clear_refs_types { 1059 CLEAR_REFS_ALL = 1, 1060 CLEAR_REFS_ANON, 1061 CLEAR_REFS_MAPPED, 1062 CLEAR_REFS_SOFT_DIRTY, 1063 CLEAR_REFS_MM_HIWATER_RSS, 1064 CLEAR_REFS_LAST, 1065 }; 1066 1067 struct clear_refs_private { 1068 enum clear_refs_types type; 1069 }; 1070 1071 #ifdef CONFIG_MEM_SOFT_DIRTY 1072 1073 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1074 { 1075 struct page *page; 1076 1077 if (!pte_write(pte)) 1078 return false; 1079 if (!is_cow_mapping(vma->vm_flags)) 1080 return false; 1081 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) 1082 return false; 1083 page = vm_normal_page(vma, addr, pte); 1084 if (!page) 1085 return false; 1086 return page_maybe_dma_pinned(page); 1087 } 1088 1089 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1090 unsigned long addr, pte_t *pte) 1091 { 1092 /* 1093 * The soft-dirty tracker uses #PF-s to catch writes 1094 * to pages, so write-protect the pte as well. See the 1095 * Documentation/admin-guide/mm/soft-dirty.rst for full description 1096 * of how soft-dirty works. 1097 */ 1098 pte_t ptent = *pte; 1099 1100 if (pte_present(ptent)) { 1101 pte_t old_pte; 1102 1103 if (pte_is_pinned(vma, addr, ptent)) 1104 return; 1105 old_pte = ptep_modify_prot_start(vma, addr, pte); 1106 ptent = pte_wrprotect(old_pte); 1107 ptent = pte_clear_soft_dirty(ptent); 1108 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); 1109 } else if (is_swap_pte(ptent)) { 1110 ptent = pte_swp_clear_soft_dirty(ptent); 1111 set_pte_at(vma->vm_mm, addr, pte, ptent); 1112 } 1113 } 1114 #else 1115 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1116 unsigned long addr, pte_t *pte) 1117 { 1118 } 1119 #endif 1120 1121 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 1122 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1123 unsigned long addr, pmd_t *pmdp) 1124 { 1125 pmd_t old, pmd = *pmdp; 1126 1127 if (pmd_present(pmd)) { 1128 /* See comment in change_huge_pmd() */ 1129 old = pmdp_invalidate(vma, addr, pmdp); 1130 if (pmd_dirty(old)) 1131 pmd = pmd_mkdirty(pmd); 1132 if (pmd_young(old)) 1133 pmd = pmd_mkyoung(pmd); 1134 1135 pmd = pmd_wrprotect(pmd); 1136 pmd = pmd_clear_soft_dirty(pmd); 1137 1138 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1139 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { 1140 pmd = pmd_swp_clear_soft_dirty(pmd); 1141 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1142 } 1143 } 1144 #else 1145 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1146 unsigned long addr, pmd_t *pmdp) 1147 { 1148 } 1149 #endif 1150 1151 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, 1152 unsigned long end, struct mm_walk *walk) 1153 { 1154 struct clear_refs_private *cp = walk->private; 1155 struct vm_area_struct *vma = walk->vma; 1156 pte_t *pte, ptent; 1157 spinlock_t *ptl; 1158 struct page *page; 1159 1160 ptl = pmd_trans_huge_lock(pmd, vma); 1161 if (ptl) { 1162 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1163 clear_soft_dirty_pmd(vma, addr, pmd); 1164 goto out; 1165 } 1166 1167 if (!pmd_present(*pmd)) 1168 goto out; 1169 1170 page = pmd_page(*pmd); 1171 1172 /* Clear accessed and referenced bits. */ 1173 pmdp_test_and_clear_young(vma, addr, pmd); 1174 test_and_clear_page_young(page); 1175 ClearPageReferenced(page); 1176 out: 1177 spin_unlock(ptl); 1178 return 0; 1179 } 1180 1181 if (pmd_trans_unstable(pmd)) 1182 return 0; 1183 1184 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 1185 for (; addr != end; pte++, addr += PAGE_SIZE) { 1186 ptent = *pte; 1187 1188 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1189 clear_soft_dirty(vma, addr, pte); 1190 continue; 1191 } 1192 1193 if (!pte_present(ptent)) 1194 continue; 1195 1196 page = vm_normal_page(vma, addr, ptent); 1197 if (!page) 1198 continue; 1199 1200 /* Clear accessed and referenced bits. */ 1201 ptep_test_and_clear_young(vma, addr, pte); 1202 test_and_clear_page_young(page); 1203 ClearPageReferenced(page); 1204 } 1205 pte_unmap_unlock(pte - 1, ptl); 1206 cond_resched(); 1207 return 0; 1208 } 1209 1210 static int clear_refs_test_walk(unsigned long start, unsigned long end, 1211 struct mm_walk *walk) 1212 { 1213 struct clear_refs_private *cp = walk->private; 1214 struct vm_area_struct *vma = walk->vma; 1215 1216 if (vma->vm_flags & VM_PFNMAP) 1217 return 1; 1218 1219 /* 1220 * Writing 1 to /proc/pid/clear_refs affects all pages. 1221 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages. 1222 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages. 1223 * Writing 4 to /proc/pid/clear_refs affects all pages. 1224 */ 1225 if (cp->type == CLEAR_REFS_ANON && vma->vm_file) 1226 return 1; 1227 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file) 1228 return 1; 1229 return 0; 1230 } 1231 1232 static const struct mm_walk_ops clear_refs_walk_ops = { 1233 .pmd_entry = clear_refs_pte_range, 1234 .test_walk = clear_refs_test_walk, 1235 }; 1236 1237 static ssize_t clear_refs_write(struct file *file, const char __user *buf, 1238 size_t count, loff_t *ppos) 1239 { 1240 struct task_struct *task; 1241 char buffer[PROC_NUMBUF]; 1242 struct mm_struct *mm; 1243 struct vm_area_struct *vma; 1244 enum clear_refs_types type; 1245 int itype; 1246 int rv; 1247 1248 memset(buffer, 0, sizeof(buffer)); 1249 if (count > sizeof(buffer) - 1) 1250 count = sizeof(buffer) - 1; 1251 if (copy_from_user(buffer, buf, count)) 1252 return -EFAULT; 1253 rv = kstrtoint(strstrip(buffer), 10, &itype); 1254 if (rv < 0) 1255 return rv; 1256 type = (enum clear_refs_types)itype; 1257 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) 1258 return -EINVAL; 1259 1260 task = get_proc_task(file_inode(file)); 1261 if (!task) 1262 return -ESRCH; 1263 mm = get_task_mm(task); 1264 if (mm) { 1265 struct mmu_notifier_range range; 1266 struct clear_refs_private cp = { 1267 .type = type, 1268 }; 1269 1270 if (mmap_write_lock_killable(mm)) { 1271 count = -EINTR; 1272 goto out_mm; 1273 } 1274 if (type == CLEAR_REFS_MM_HIWATER_RSS) { 1275 /* 1276 * Writing 5 to /proc/pid/clear_refs resets the peak 1277 * resident set size to this mm's current rss value. 1278 */ 1279 reset_mm_hiwater_rss(mm); 1280 goto out_unlock; 1281 } 1282 1283 if (type == CLEAR_REFS_SOFT_DIRTY) { 1284 for (vma = mm->mmap; vma; vma = vma->vm_next) { 1285 if (!(vma->vm_flags & VM_SOFTDIRTY)) 1286 continue; 1287 vma->vm_flags &= ~VM_SOFTDIRTY; 1288 vma_set_page_prot(vma); 1289 } 1290 1291 inc_tlb_flush_pending(mm); 1292 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY, 1293 0, NULL, mm, 0, -1UL); 1294 mmu_notifier_invalidate_range_start(&range); 1295 } 1296 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops, 1297 &cp); 1298 if (type == CLEAR_REFS_SOFT_DIRTY) { 1299 mmu_notifier_invalidate_range_end(&range); 1300 flush_tlb_mm(mm); 1301 dec_tlb_flush_pending(mm); 1302 } 1303 out_unlock: 1304 mmap_write_unlock(mm); 1305 out_mm: 1306 mmput(mm); 1307 } 1308 put_task_struct(task); 1309 1310 return count; 1311 } 1312 1313 const struct file_operations proc_clear_refs_operations = { 1314 .write = clear_refs_write, 1315 .llseek = noop_llseek, 1316 }; 1317 1318 typedef struct { 1319 u64 pme; 1320 } pagemap_entry_t; 1321 1322 struct pagemapread { 1323 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ 1324 pagemap_entry_t *buffer; 1325 bool show_pfn; 1326 }; 1327 1328 #define PAGEMAP_WALK_SIZE (PMD_SIZE) 1329 #define PAGEMAP_WALK_MASK (PMD_MASK) 1330 1331 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) 1332 #define PM_PFRAME_BITS 55 1333 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0) 1334 #define PM_SOFT_DIRTY BIT_ULL(55) 1335 #define PM_MMAP_EXCLUSIVE BIT_ULL(56) 1336 #define PM_UFFD_WP BIT_ULL(57) 1337 #define PM_FILE BIT_ULL(61) 1338 #define PM_SWAP BIT_ULL(62) 1339 #define PM_PRESENT BIT_ULL(63) 1340 1341 #define PM_END_OF_BUFFER 1 1342 1343 static inline pagemap_entry_t make_pme(u64 frame, u64 flags) 1344 { 1345 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags }; 1346 } 1347 1348 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme, 1349 struct pagemapread *pm) 1350 { 1351 pm->buffer[pm->pos++] = *pme; 1352 if (pm->pos >= pm->len) 1353 return PM_END_OF_BUFFER; 1354 return 0; 1355 } 1356 1357 static int pagemap_pte_hole(unsigned long start, unsigned long end, 1358 __always_unused int depth, struct mm_walk *walk) 1359 { 1360 struct pagemapread *pm = walk->private; 1361 unsigned long addr = start; 1362 int err = 0; 1363 1364 while (addr < end) { 1365 struct vm_area_struct *vma = find_vma(walk->mm, addr); 1366 pagemap_entry_t pme = make_pme(0, 0); 1367 /* End of address space hole, which we mark as non-present. */ 1368 unsigned long hole_end; 1369 1370 if (vma) 1371 hole_end = min(end, vma->vm_start); 1372 else 1373 hole_end = end; 1374 1375 for (; addr < hole_end; addr += PAGE_SIZE) { 1376 err = add_to_pagemap(addr, &pme, pm); 1377 if (err) 1378 goto out; 1379 } 1380 1381 if (!vma) 1382 break; 1383 1384 /* Addresses in the VMA. */ 1385 if (vma->vm_flags & VM_SOFTDIRTY) 1386 pme = make_pme(0, PM_SOFT_DIRTY); 1387 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) { 1388 err = add_to_pagemap(addr, &pme, pm); 1389 if (err) 1390 goto out; 1391 } 1392 } 1393 out: 1394 return err; 1395 } 1396 1397 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, 1398 struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1399 { 1400 u64 frame = 0, flags = 0; 1401 struct page *page = NULL; 1402 bool migration = false; 1403 1404 if (pte_present(pte)) { 1405 if (pm->show_pfn) 1406 frame = pte_pfn(pte); 1407 flags |= PM_PRESENT; 1408 page = vm_normal_page(vma, addr, pte); 1409 if (pte_soft_dirty(pte)) 1410 flags |= PM_SOFT_DIRTY; 1411 if (pte_uffd_wp(pte)) 1412 flags |= PM_UFFD_WP; 1413 } else if (is_swap_pte(pte)) { 1414 swp_entry_t entry; 1415 if (pte_swp_soft_dirty(pte)) 1416 flags |= PM_SOFT_DIRTY; 1417 if (pte_swp_uffd_wp(pte)) 1418 flags |= PM_UFFD_WP; 1419 entry = pte_to_swp_entry(pte); 1420 if (pm->show_pfn) 1421 frame = swp_type(entry) | 1422 (swp_offset(entry) << MAX_SWAPFILES_SHIFT); 1423 flags |= PM_SWAP; 1424 migration = is_migration_entry(entry); 1425 if (is_pfn_swap_entry(entry)) 1426 page = pfn_swap_entry_to_page(entry); 1427 if (pte_marker_entry_uffd_wp(entry)) 1428 flags |= PM_UFFD_WP; 1429 } 1430 1431 if (page && !PageAnon(page)) 1432 flags |= PM_FILE; 1433 if (page && !migration && page_mapcount(page) == 1) 1434 flags |= PM_MMAP_EXCLUSIVE; 1435 if (vma->vm_flags & VM_SOFTDIRTY) 1436 flags |= PM_SOFT_DIRTY; 1437 1438 return make_pme(frame, flags); 1439 } 1440 1441 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, 1442 struct mm_walk *walk) 1443 { 1444 struct vm_area_struct *vma = walk->vma; 1445 struct pagemapread *pm = walk->private; 1446 spinlock_t *ptl; 1447 pte_t *pte, *orig_pte; 1448 int err = 0; 1449 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1450 bool migration = false; 1451 1452 ptl = pmd_trans_huge_lock(pmdp, vma); 1453 if (ptl) { 1454 u64 flags = 0, frame = 0; 1455 pmd_t pmd = *pmdp; 1456 struct page *page = NULL; 1457 1458 if (vma->vm_flags & VM_SOFTDIRTY) 1459 flags |= PM_SOFT_DIRTY; 1460 1461 if (pmd_present(pmd)) { 1462 page = pmd_page(pmd); 1463 1464 flags |= PM_PRESENT; 1465 if (pmd_soft_dirty(pmd)) 1466 flags |= PM_SOFT_DIRTY; 1467 if (pmd_uffd_wp(pmd)) 1468 flags |= PM_UFFD_WP; 1469 if (pm->show_pfn) 1470 frame = pmd_pfn(pmd) + 1471 ((addr & ~PMD_MASK) >> PAGE_SHIFT); 1472 } 1473 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 1474 else if (is_swap_pmd(pmd)) { 1475 swp_entry_t entry = pmd_to_swp_entry(pmd); 1476 unsigned long offset; 1477 1478 if (pm->show_pfn) { 1479 offset = swp_offset(entry) + 1480 ((addr & ~PMD_MASK) >> PAGE_SHIFT); 1481 frame = swp_type(entry) | 1482 (offset << MAX_SWAPFILES_SHIFT); 1483 } 1484 flags |= PM_SWAP; 1485 if (pmd_swp_soft_dirty(pmd)) 1486 flags |= PM_SOFT_DIRTY; 1487 if (pmd_swp_uffd_wp(pmd)) 1488 flags |= PM_UFFD_WP; 1489 VM_BUG_ON(!is_pmd_migration_entry(pmd)); 1490 migration = is_migration_entry(entry); 1491 page = pfn_swap_entry_to_page(entry); 1492 } 1493 #endif 1494 1495 if (page && !migration && page_mapcount(page) == 1) 1496 flags |= PM_MMAP_EXCLUSIVE; 1497 1498 for (; addr != end; addr += PAGE_SIZE) { 1499 pagemap_entry_t pme = make_pme(frame, flags); 1500 1501 err = add_to_pagemap(addr, &pme, pm); 1502 if (err) 1503 break; 1504 if (pm->show_pfn) { 1505 if (flags & PM_PRESENT) 1506 frame++; 1507 else if (flags & PM_SWAP) 1508 frame += (1 << MAX_SWAPFILES_SHIFT); 1509 } 1510 } 1511 spin_unlock(ptl); 1512 return err; 1513 } 1514 1515 if (pmd_trans_unstable(pmdp)) 1516 return 0; 1517 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1518 1519 /* 1520 * We can assume that @vma always points to a valid one and @end never 1521 * goes beyond vma->vm_end. 1522 */ 1523 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl); 1524 for (; addr < end; pte++, addr += PAGE_SIZE) { 1525 pagemap_entry_t pme; 1526 1527 pme = pte_to_pagemap_entry(pm, vma, addr, *pte); 1528 err = add_to_pagemap(addr, &pme, pm); 1529 if (err) 1530 break; 1531 } 1532 pte_unmap_unlock(orig_pte, ptl); 1533 1534 cond_resched(); 1535 1536 return err; 1537 } 1538 1539 #ifdef CONFIG_HUGETLB_PAGE 1540 /* This function walks within one hugetlb entry in the single call */ 1541 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, 1542 unsigned long addr, unsigned long end, 1543 struct mm_walk *walk) 1544 { 1545 struct pagemapread *pm = walk->private; 1546 struct vm_area_struct *vma = walk->vma; 1547 u64 flags = 0, frame = 0; 1548 int err = 0; 1549 pte_t pte; 1550 1551 if (vma->vm_flags & VM_SOFTDIRTY) 1552 flags |= PM_SOFT_DIRTY; 1553 1554 pte = huge_ptep_get(ptep); 1555 if (pte_present(pte)) { 1556 struct page *page = pte_page(pte); 1557 1558 if (!PageAnon(page)) 1559 flags |= PM_FILE; 1560 1561 if (page_mapcount(page) == 1) 1562 flags |= PM_MMAP_EXCLUSIVE; 1563 1564 if (huge_pte_uffd_wp(pte)) 1565 flags |= PM_UFFD_WP; 1566 1567 flags |= PM_PRESENT; 1568 if (pm->show_pfn) 1569 frame = pte_pfn(pte) + 1570 ((addr & ~hmask) >> PAGE_SHIFT); 1571 } else if (pte_swp_uffd_wp_any(pte)) { 1572 flags |= PM_UFFD_WP; 1573 } 1574 1575 for (; addr != end; addr += PAGE_SIZE) { 1576 pagemap_entry_t pme = make_pme(frame, flags); 1577 1578 err = add_to_pagemap(addr, &pme, pm); 1579 if (err) 1580 return err; 1581 if (pm->show_pfn && (flags & PM_PRESENT)) 1582 frame++; 1583 } 1584 1585 cond_resched(); 1586 1587 return err; 1588 } 1589 #else 1590 #define pagemap_hugetlb_range NULL 1591 #endif /* HUGETLB_PAGE */ 1592 1593 static const struct mm_walk_ops pagemap_ops = { 1594 .pmd_entry = pagemap_pmd_range, 1595 .pte_hole = pagemap_pte_hole, 1596 .hugetlb_entry = pagemap_hugetlb_range, 1597 }; 1598 1599 /* 1600 * /proc/pid/pagemap - an array mapping virtual pages to pfns 1601 * 1602 * For each page in the address space, this file contains one 64-bit entry 1603 * consisting of the following: 1604 * 1605 * Bits 0-54 page frame number (PFN) if present 1606 * Bits 0-4 swap type if swapped 1607 * Bits 5-54 swap offset if swapped 1608 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst) 1609 * Bit 56 page exclusively mapped 1610 * Bit 57 pte is uffd-wp write-protected 1611 * Bits 58-60 zero 1612 * Bit 61 page is file-page or shared-anon 1613 * Bit 62 page swapped 1614 * Bit 63 page present 1615 * 1616 * If the page is not present but in swap, then the PFN contains an 1617 * encoding of the swap file number and the page's offset into the 1618 * swap. Unmapped pages return a null PFN. This allows determining 1619 * precisely which pages are mapped (or in swap) and comparing mapped 1620 * pages between processes. 1621 * 1622 * Efficient users of this interface will use /proc/pid/maps to 1623 * determine which areas of memory are actually mapped and llseek to 1624 * skip over unmapped regions. 1625 */ 1626 static ssize_t pagemap_read(struct file *file, char __user *buf, 1627 size_t count, loff_t *ppos) 1628 { 1629 struct mm_struct *mm = file->private_data; 1630 struct pagemapread pm; 1631 unsigned long src; 1632 unsigned long svpfn; 1633 unsigned long start_vaddr; 1634 unsigned long end_vaddr; 1635 int ret = 0, copied = 0; 1636 1637 if (!mm || !mmget_not_zero(mm)) 1638 goto out; 1639 1640 ret = -EINVAL; 1641 /* file position must be aligned */ 1642 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) 1643 goto out_mm; 1644 1645 ret = 0; 1646 if (!count) 1647 goto out_mm; 1648 1649 /* do not disclose physical addresses: attack vector */ 1650 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN); 1651 1652 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); 1653 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL); 1654 ret = -ENOMEM; 1655 if (!pm.buffer) 1656 goto out_mm; 1657 1658 src = *ppos; 1659 svpfn = src / PM_ENTRY_BYTES; 1660 end_vaddr = mm->task_size; 1661 1662 /* watch out for wraparound */ 1663 start_vaddr = end_vaddr; 1664 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) 1665 start_vaddr = untagged_addr(svpfn << PAGE_SHIFT); 1666 1667 /* Ensure the address is inside the task */ 1668 if (start_vaddr > mm->task_size) 1669 start_vaddr = end_vaddr; 1670 1671 /* 1672 * The odds are that this will stop walking way 1673 * before end_vaddr, because the length of the 1674 * user buffer is tracked in "pm", and the walk 1675 * will stop when we hit the end of the buffer. 1676 */ 1677 ret = 0; 1678 while (count && (start_vaddr < end_vaddr)) { 1679 int len; 1680 unsigned long end; 1681 1682 pm.pos = 0; 1683 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; 1684 /* overflow ? */ 1685 if (end < start_vaddr || end > end_vaddr) 1686 end = end_vaddr; 1687 ret = mmap_read_lock_killable(mm); 1688 if (ret) 1689 goto out_free; 1690 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm); 1691 mmap_read_unlock(mm); 1692 start_vaddr = end; 1693 1694 len = min(count, PM_ENTRY_BYTES * pm.pos); 1695 if (copy_to_user(buf, pm.buffer, len)) { 1696 ret = -EFAULT; 1697 goto out_free; 1698 } 1699 copied += len; 1700 buf += len; 1701 count -= len; 1702 } 1703 *ppos += copied; 1704 if (!ret || ret == PM_END_OF_BUFFER) 1705 ret = copied; 1706 1707 out_free: 1708 kfree(pm.buffer); 1709 out_mm: 1710 mmput(mm); 1711 out: 1712 return ret; 1713 } 1714 1715 static int pagemap_open(struct inode *inode, struct file *file) 1716 { 1717 struct mm_struct *mm; 1718 1719 mm = proc_mem_open(inode, PTRACE_MODE_READ); 1720 if (IS_ERR(mm)) 1721 return PTR_ERR(mm); 1722 file->private_data = mm; 1723 return 0; 1724 } 1725 1726 static int pagemap_release(struct inode *inode, struct file *file) 1727 { 1728 struct mm_struct *mm = file->private_data; 1729 1730 if (mm) 1731 mmdrop(mm); 1732 return 0; 1733 } 1734 1735 const struct file_operations proc_pagemap_operations = { 1736 .llseek = mem_lseek, /* borrow this */ 1737 .read = pagemap_read, 1738 .open = pagemap_open, 1739 .release = pagemap_release, 1740 }; 1741 #endif /* CONFIG_PROC_PAGE_MONITOR */ 1742 1743 #ifdef CONFIG_NUMA 1744 1745 struct numa_maps { 1746 unsigned long pages; 1747 unsigned long anon; 1748 unsigned long active; 1749 unsigned long writeback; 1750 unsigned long mapcount_max; 1751 unsigned long dirty; 1752 unsigned long swapcache; 1753 unsigned long node[MAX_NUMNODES]; 1754 }; 1755 1756 struct numa_maps_private { 1757 struct proc_maps_private proc_maps; 1758 struct numa_maps md; 1759 }; 1760 1761 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, 1762 unsigned long nr_pages) 1763 { 1764 int count = page_mapcount(page); 1765 1766 md->pages += nr_pages; 1767 if (pte_dirty || PageDirty(page)) 1768 md->dirty += nr_pages; 1769 1770 if (PageSwapCache(page)) 1771 md->swapcache += nr_pages; 1772 1773 if (PageActive(page) || PageUnevictable(page)) 1774 md->active += nr_pages; 1775 1776 if (PageWriteback(page)) 1777 md->writeback += nr_pages; 1778 1779 if (PageAnon(page)) 1780 md->anon += nr_pages; 1781 1782 if (count > md->mapcount_max) 1783 md->mapcount_max = count; 1784 1785 md->node[page_to_nid(page)] += nr_pages; 1786 } 1787 1788 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, 1789 unsigned long addr) 1790 { 1791 struct page *page; 1792 int nid; 1793 1794 if (!pte_present(pte)) 1795 return NULL; 1796 1797 page = vm_normal_page(vma, addr, pte); 1798 if (!page || is_zone_device_page(page)) 1799 return NULL; 1800 1801 if (PageReserved(page)) 1802 return NULL; 1803 1804 nid = page_to_nid(page); 1805 if (!node_isset(nid, node_states[N_MEMORY])) 1806 return NULL; 1807 1808 return page; 1809 } 1810 1811 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1812 static struct page *can_gather_numa_stats_pmd(pmd_t pmd, 1813 struct vm_area_struct *vma, 1814 unsigned long addr) 1815 { 1816 struct page *page; 1817 int nid; 1818 1819 if (!pmd_present(pmd)) 1820 return NULL; 1821 1822 page = vm_normal_page_pmd(vma, addr, pmd); 1823 if (!page) 1824 return NULL; 1825 1826 if (PageReserved(page)) 1827 return NULL; 1828 1829 nid = page_to_nid(page); 1830 if (!node_isset(nid, node_states[N_MEMORY])) 1831 return NULL; 1832 1833 return page; 1834 } 1835 #endif 1836 1837 static int gather_pte_stats(pmd_t *pmd, unsigned long addr, 1838 unsigned long end, struct mm_walk *walk) 1839 { 1840 struct numa_maps *md = walk->private; 1841 struct vm_area_struct *vma = walk->vma; 1842 spinlock_t *ptl; 1843 pte_t *orig_pte; 1844 pte_t *pte; 1845 1846 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1847 ptl = pmd_trans_huge_lock(pmd, vma); 1848 if (ptl) { 1849 struct page *page; 1850 1851 page = can_gather_numa_stats_pmd(*pmd, vma, addr); 1852 if (page) 1853 gather_stats(page, md, pmd_dirty(*pmd), 1854 HPAGE_PMD_SIZE/PAGE_SIZE); 1855 spin_unlock(ptl); 1856 return 0; 1857 } 1858 1859 if (pmd_trans_unstable(pmd)) 1860 return 0; 1861 #endif 1862 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); 1863 do { 1864 struct page *page = can_gather_numa_stats(*pte, vma, addr); 1865 if (!page) 1866 continue; 1867 gather_stats(page, md, pte_dirty(*pte), 1); 1868 1869 } while (pte++, addr += PAGE_SIZE, addr != end); 1870 pte_unmap_unlock(orig_pte, ptl); 1871 cond_resched(); 1872 return 0; 1873 } 1874 #ifdef CONFIG_HUGETLB_PAGE 1875 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 1876 unsigned long addr, unsigned long end, struct mm_walk *walk) 1877 { 1878 pte_t huge_pte = huge_ptep_get(pte); 1879 struct numa_maps *md; 1880 struct page *page; 1881 1882 if (!pte_present(huge_pte)) 1883 return 0; 1884 1885 page = pte_page(huge_pte); 1886 1887 md = walk->private; 1888 gather_stats(page, md, pte_dirty(huge_pte), 1); 1889 return 0; 1890 } 1891 1892 #else 1893 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 1894 unsigned long addr, unsigned long end, struct mm_walk *walk) 1895 { 1896 return 0; 1897 } 1898 #endif 1899 1900 static const struct mm_walk_ops show_numa_ops = { 1901 .hugetlb_entry = gather_hugetlb_stats, 1902 .pmd_entry = gather_pte_stats, 1903 }; 1904 1905 /* 1906 * Display pages allocated per node and memory policy via /proc. 1907 */ 1908 static int show_numa_map(struct seq_file *m, void *v) 1909 { 1910 struct numa_maps_private *numa_priv = m->private; 1911 struct proc_maps_private *proc_priv = &numa_priv->proc_maps; 1912 struct vm_area_struct *vma = v; 1913 struct numa_maps *md = &numa_priv->md; 1914 struct file *file = vma->vm_file; 1915 struct mm_struct *mm = vma->vm_mm; 1916 struct mempolicy *pol; 1917 char buffer[64]; 1918 int nid; 1919 1920 if (!mm) 1921 return 0; 1922 1923 /* Ensure we start with an empty set of numa_maps statistics. */ 1924 memset(md, 0, sizeof(*md)); 1925 1926 pol = __get_vma_policy(vma, vma->vm_start); 1927 if (pol) { 1928 mpol_to_str(buffer, sizeof(buffer), pol); 1929 mpol_cond_put(pol); 1930 } else { 1931 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy); 1932 } 1933 1934 seq_printf(m, "%08lx %s", vma->vm_start, buffer); 1935 1936 if (file) { 1937 seq_puts(m, " file="); 1938 seq_file_path(m, file, "\n\t= "); 1939 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { 1940 seq_puts(m, " heap"); 1941 } else if (is_stack(vma)) { 1942 seq_puts(m, " stack"); 1943 } 1944 1945 if (is_vm_hugetlb_page(vma)) 1946 seq_puts(m, " huge"); 1947 1948 /* mmap_lock is held by m_start */ 1949 walk_page_vma(vma, &show_numa_ops, md); 1950 1951 if (!md->pages) 1952 goto out; 1953 1954 if (md->anon) 1955 seq_printf(m, " anon=%lu", md->anon); 1956 1957 if (md->dirty) 1958 seq_printf(m, " dirty=%lu", md->dirty); 1959 1960 if (md->pages != md->anon && md->pages != md->dirty) 1961 seq_printf(m, " mapped=%lu", md->pages); 1962 1963 if (md->mapcount_max > 1) 1964 seq_printf(m, " mapmax=%lu", md->mapcount_max); 1965 1966 if (md->swapcache) 1967 seq_printf(m, " swapcache=%lu", md->swapcache); 1968 1969 if (md->active < md->pages && !is_vm_hugetlb_page(vma)) 1970 seq_printf(m, " active=%lu", md->active); 1971 1972 if (md->writeback) 1973 seq_printf(m, " writeback=%lu", md->writeback); 1974 1975 for_each_node_state(nid, N_MEMORY) 1976 if (md->node[nid]) 1977 seq_printf(m, " N%d=%lu", nid, md->node[nid]); 1978 1979 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10); 1980 out: 1981 seq_putc(m, '\n'); 1982 return 0; 1983 } 1984 1985 static const struct seq_operations proc_pid_numa_maps_op = { 1986 .start = m_start, 1987 .next = m_next, 1988 .stop = m_stop, 1989 .show = show_numa_map, 1990 }; 1991 1992 static int pid_numa_maps_open(struct inode *inode, struct file *file) 1993 { 1994 return proc_maps_open(inode, file, &proc_pid_numa_maps_op, 1995 sizeof(struct numa_maps_private)); 1996 } 1997 1998 const struct file_operations proc_pid_numa_maps_operations = { 1999 .open = pid_numa_maps_open, 2000 .read = seq_read, 2001 .llseek = seq_lseek, 2002 .release = proc_map_release, 2003 }; 2004 2005 #endif /* CONFIG_NUMA */ 2006