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