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