1 #include <linux/mm.h> 2 #include <linux/hugetlb.h> 3 #include <linux/huge_mm.h> 4 #include <linux/mount.h> 5 #include <linux/seq_file.h> 6 #include <linux/highmem.h> 7 #include <linux/ptrace.h> 8 #include <linux/slab.h> 9 #include <linux/pagemap.h> 10 #include <linux/mempolicy.h> 11 #include <linux/rmap.h> 12 #include <linux/swap.h> 13 #include <linux/swapops.h> 14 #include <linux/mmu_notifier.h> 15 16 #include <asm/elf.h> 17 #include <asm/uaccess.h> 18 #include <asm/tlbflush.h> 19 #include "internal.h" 20 21 void task_mem(struct seq_file *m, struct mm_struct *mm) 22 { 23 unsigned long data, text, lib, swap; 24 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; 25 26 /* 27 * Note: to minimize their overhead, mm maintains hiwater_vm and 28 * hiwater_rss only when about to *lower* total_vm or rss. Any 29 * collector of these hiwater stats must therefore get total_vm 30 * and rss too, which will usually be the higher. Barriers? not 31 * worth the effort, such snapshots can always be inconsistent. 32 */ 33 hiwater_vm = total_vm = mm->total_vm; 34 if (hiwater_vm < mm->hiwater_vm) 35 hiwater_vm = mm->hiwater_vm; 36 hiwater_rss = total_rss = get_mm_rss(mm); 37 if (hiwater_rss < mm->hiwater_rss) 38 hiwater_rss = mm->hiwater_rss; 39 40 data = mm->total_vm - mm->shared_vm - mm->stack_vm; 41 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10; 42 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text; 43 swap = get_mm_counter(mm, MM_SWAPENTS); 44 seq_printf(m, 45 "VmPeak:\t%8lu kB\n" 46 "VmSize:\t%8lu kB\n" 47 "VmLck:\t%8lu kB\n" 48 "VmPin:\t%8lu kB\n" 49 "VmHWM:\t%8lu kB\n" 50 "VmRSS:\t%8lu kB\n" 51 "VmData:\t%8lu kB\n" 52 "VmStk:\t%8lu kB\n" 53 "VmExe:\t%8lu kB\n" 54 "VmLib:\t%8lu kB\n" 55 "VmPTE:\t%8lu kB\n" 56 "VmSwap:\t%8lu kB\n", 57 hiwater_vm << (PAGE_SHIFT-10), 58 total_vm << (PAGE_SHIFT-10), 59 mm->locked_vm << (PAGE_SHIFT-10), 60 mm->pinned_vm << (PAGE_SHIFT-10), 61 hiwater_rss << (PAGE_SHIFT-10), 62 total_rss << (PAGE_SHIFT-10), 63 data << (PAGE_SHIFT-10), 64 mm->stack_vm << (PAGE_SHIFT-10), text, lib, 65 (PTRS_PER_PTE * sizeof(pte_t) * 66 atomic_long_read(&mm->nr_ptes)) >> 10, 67 swap << (PAGE_SHIFT-10)); 68 } 69 70 unsigned long task_vsize(struct mm_struct *mm) 71 { 72 return PAGE_SIZE * mm->total_vm; 73 } 74 75 unsigned long task_statm(struct mm_struct *mm, 76 unsigned long *shared, unsigned long *text, 77 unsigned long *data, unsigned long *resident) 78 { 79 *shared = get_mm_counter(mm, MM_FILEPAGES); 80 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) 81 >> PAGE_SHIFT; 82 *data = mm->total_vm - mm->shared_vm; 83 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); 84 return mm->total_vm; 85 } 86 87 #ifdef CONFIG_NUMA 88 /* 89 * These functions are for numa_maps but called in generic **maps seq_file 90 * ->start(), ->stop() ops. 91 * 92 * numa_maps scans all vmas under mmap_sem and checks their mempolicy. 93 * Each mempolicy object is controlled by reference counting. The problem here 94 * is how to avoid accessing dead mempolicy object. 95 * 96 * Because we're holding mmap_sem while reading seq_file, it's safe to access 97 * each vma's mempolicy, no vma objects will never drop refs to mempolicy. 98 * 99 * A task's mempolicy (task->mempolicy) has different behavior. task->mempolicy 100 * is set and replaced under mmap_sem but unrefed and cleared under task_lock(). 101 * So, without task_lock(), we cannot trust get_vma_policy() because we cannot 102 * gurantee the task never exits under us. But taking task_lock() around 103 * get_vma_plicy() causes lock order problem. 104 * 105 * To access task->mempolicy without lock, we hold a reference count of an 106 * object pointed by task->mempolicy and remember it. This will guarantee 107 * that task->mempolicy points to an alive object or NULL in numa_maps accesses. 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 = task->mempolicy; 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, struct vm_area_struct *vma) 132 { 133 if (vma && vma != priv->tail_vma) { 134 struct mm_struct *mm = vma->vm_mm; 135 release_task_mempolicy(priv); 136 up_read(&mm->mmap_sem); 137 mmput(mm); 138 } 139 } 140 141 static void *m_start(struct seq_file *m, loff_t *pos) 142 { 143 struct proc_maps_private *priv = m->private; 144 unsigned long last_addr = m->version; 145 struct mm_struct *mm; 146 struct vm_area_struct *vma, *tail_vma = NULL; 147 loff_t l = *pos; 148 149 /* Clear the per syscall fields in priv */ 150 priv->task = NULL; 151 priv->tail_vma = NULL; 152 153 /* 154 * We remember last_addr rather than next_addr to hit with 155 * mmap_cache most of the time. We have zero last_addr at 156 * the beginning and also after lseek. We will have -1 last_addr 157 * after the end of the vmas. 158 */ 159 160 if (last_addr == -1UL) 161 return NULL; 162 163 priv->task = get_pid_task(priv->pid, PIDTYPE_PID); 164 if (!priv->task) 165 return ERR_PTR(-ESRCH); 166 167 mm = mm_access(priv->task, PTRACE_MODE_READ); 168 if (!mm || IS_ERR(mm)) 169 return mm; 170 down_read(&mm->mmap_sem); 171 172 tail_vma = get_gate_vma(priv->task->mm); 173 priv->tail_vma = tail_vma; 174 hold_task_mempolicy(priv); 175 /* Start with last addr hint */ 176 vma = find_vma(mm, last_addr); 177 if (last_addr && vma) { 178 vma = vma->vm_next; 179 goto out; 180 } 181 182 /* 183 * Check the vma index is within the range and do 184 * sequential scan until m_index. 185 */ 186 vma = NULL; 187 if ((unsigned long)l < mm->map_count) { 188 vma = mm->mmap; 189 while (l-- && vma) 190 vma = vma->vm_next; 191 goto out; 192 } 193 194 if (l != mm->map_count) 195 tail_vma = NULL; /* After gate vma */ 196 197 out: 198 if (vma) 199 return vma; 200 201 release_task_mempolicy(priv); 202 /* End of vmas has been reached */ 203 m->version = (tail_vma != NULL)? 0: -1UL; 204 up_read(&mm->mmap_sem); 205 mmput(mm); 206 return tail_vma; 207 } 208 209 static void *m_next(struct seq_file *m, void *v, loff_t *pos) 210 { 211 struct proc_maps_private *priv = m->private; 212 struct vm_area_struct *vma = v; 213 struct vm_area_struct *tail_vma = priv->tail_vma; 214 215 (*pos)++; 216 if (vma && (vma != tail_vma) && vma->vm_next) 217 return vma->vm_next; 218 vma_stop(priv, vma); 219 return (vma != tail_vma)? tail_vma: NULL; 220 } 221 222 static void m_stop(struct seq_file *m, void *v) 223 { 224 struct proc_maps_private *priv = m->private; 225 struct vm_area_struct *vma = v; 226 227 if (!IS_ERR(vma)) 228 vma_stop(priv, vma); 229 if (priv->task) 230 put_task_struct(priv->task); 231 } 232 233 static int do_maps_open(struct inode *inode, struct file *file, 234 const struct seq_operations *ops) 235 { 236 struct proc_maps_private *priv; 237 int ret = -ENOMEM; 238 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 239 if (priv) { 240 priv->pid = proc_pid(inode); 241 ret = seq_open(file, ops); 242 if (!ret) { 243 struct seq_file *m = file->private_data; 244 m->private = priv; 245 } else { 246 kfree(priv); 247 } 248 } 249 return ret; 250 } 251 252 static void 253 show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid) 254 { 255 struct mm_struct *mm = vma->vm_mm; 256 struct file *file = vma->vm_file; 257 struct proc_maps_private *priv = m->private; 258 struct task_struct *task = priv->task; 259 vm_flags_t flags = vma->vm_flags; 260 unsigned long ino = 0; 261 unsigned long long pgoff = 0; 262 unsigned long start, end; 263 dev_t dev = 0; 264 const char *name = NULL; 265 266 if (file) { 267 struct inode *inode = file_inode(vma->vm_file); 268 dev = inode->i_sb->s_dev; 269 ino = inode->i_ino; 270 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; 271 } 272 273 /* We don't show the stack guard page in /proc/maps */ 274 start = vma->vm_start; 275 if (stack_guard_page_start(vma, start)) 276 start += PAGE_SIZE; 277 end = vma->vm_end; 278 if (stack_guard_page_end(vma, end)) 279 end -= PAGE_SIZE; 280 281 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); 282 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ", 283 start, 284 end, 285 flags & VM_READ ? 'r' : '-', 286 flags & VM_WRITE ? 'w' : '-', 287 flags & VM_EXEC ? 'x' : '-', 288 flags & VM_MAYSHARE ? 's' : 'p', 289 pgoff, 290 MAJOR(dev), MINOR(dev), ino); 291 292 /* 293 * Print the dentry name for named mappings, and a 294 * special [heap] marker for the heap: 295 */ 296 if (file) { 297 seq_pad(m, ' '); 298 seq_path(m, &file->f_path, "\n"); 299 goto done; 300 } 301 302 name = arch_vma_name(vma); 303 if (!name) { 304 pid_t tid; 305 306 if (!mm) { 307 name = "[vdso]"; 308 goto done; 309 } 310 311 if (vma->vm_start <= mm->brk && 312 vma->vm_end >= mm->start_brk) { 313 name = "[heap]"; 314 goto done; 315 } 316 317 tid = vm_is_stack(task, vma, is_pid); 318 319 if (tid != 0) { 320 /* 321 * Thread stack in /proc/PID/task/TID/maps or 322 * the main process stack. 323 */ 324 if (!is_pid || (vma->vm_start <= mm->start_stack && 325 vma->vm_end >= mm->start_stack)) { 326 name = "[stack]"; 327 } else { 328 /* Thread stack in /proc/PID/maps */ 329 seq_pad(m, ' '); 330 seq_printf(m, "[stack:%d]", tid); 331 } 332 } 333 } 334 335 done: 336 if (name) { 337 seq_pad(m, ' '); 338 seq_puts(m, name); 339 } 340 seq_putc(m, '\n'); 341 } 342 343 static int show_map(struct seq_file *m, void *v, int is_pid) 344 { 345 struct vm_area_struct *vma = v; 346 struct proc_maps_private *priv = m->private; 347 struct task_struct *task = priv->task; 348 349 show_map_vma(m, vma, is_pid); 350 351 if (m->count < m->size) /* vma is copied successfully */ 352 m->version = (vma != get_gate_vma(task->mm)) 353 ? vma->vm_start : 0; 354 return 0; 355 } 356 357 static int show_pid_map(struct seq_file *m, void *v) 358 { 359 return show_map(m, v, 1); 360 } 361 362 static int show_tid_map(struct seq_file *m, void *v) 363 { 364 return show_map(m, v, 0); 365 } 366 367 static const struct seq_operations proc_pid_maps_op = { 368 .start = m_start, 369 .next = m_next, 370 .stop = m_stop, 371 .show = show_pid_map 372 }; 373 374 static const struct seq_operations proc_tid_maps_op = { 375 .start = m_start, 376 .next = m_next, 377 .stop = m_stop, 378 .show = show_tid_map 379 }; 380 381 static int pid_maps_open(struct inode *inode, struct file *file) 382 { 383 return do_maps_open(inode, file, &proc_pid_maps_op); 384 } 385 386 static int tid_maps_open(struct inode *inode, struct file *file) 387 { 388 return do_maps_open(inode, file, &proc_tid_maps_op); 389 } 390 391 const struct file_operations proc_pid_maps_operations = { 392 .open = pid_maps_open, 393 .read = seq_read, 394 .llseek = seq_lseek, 395 .release = seq_release_private, 396 }; 397 398 const struct file_operations proc_tid_maps_operations = { 399 .open = tid_maps_open, 400 .read = seq_read, 401 .llseek = seq_lseek, 402 .release = seq_release_private, 403 }; 404 405 /* 406 * Proportional Set Size(PSS): my share of RSS. 407 * 408 * PSS of a process is the count of pages it has in memory, where each 409 * page is divided by the number of processes sharing it. So if a 410 * process has 1000 pages all to itself, and 1000 shared with one other 411 * process, its PSS will be 1500. 412 * 413 * To keep (accumulated) division errors low, we adopt a 64bit 414 * fixed-point pss counter to minimize division errors. So (pss >> 415 * PSS_SHIFT) would be the real byte count. 416 * 417 * A shift of 12 before division means (assuming 4K page size): 418 * - 1M 3-user-pages add up to 8KB errors; 419 * - supports mapcount up to 2^24, or 16M; 420 * - supports PSS up to 2^52 bytes, or 4PB. 421 */ 422 #define PSS_SHIFT 12 423 424 #ifdef CONFIG_PROC_PAGE_MONITOR 425 struct mem_size_stats { 426 struct vm_area_struct *vma; 427 unsigned long resident; 428 unsigned long shared_clean; 429 unsigned long shared_dirty; 430 unsigned long private_clean; 431 unsigned long private_dirty; 432 unsigned long referenced; 433 unsigned long anonymous; 434 unsigned long anonymous_thp; 435 unsigned long swap; 436 unsigned long nonlinear; 437 u64 pss; 438 }; 439 440 441 static void smaps_pte_entry(pte_t ptent, unsigned long addr, 442 unsigned long ptent_size, struct mm_walk *walk) 443 { 444 struct mem_size_stats *mss = walk->private; 445 struct vm_area_struct *vma = mss->vma; 446 pgoff_t pgoff = linear_page_index(vma, addr); 447 struct page *page = NULL; 448 int mapcount; 449 450 if (pte_present(ptent)) { 451 page = vm_normal_page(vma, addr, ptent); 452 } else if (is_swap_pte(ptent)) { 453 swp_entry_t swpent = pte_to_swp_entry(ptent); 454 455 if (!non_swap_entry(swpent)) 456 mss->swap += ptent_size; 457 else if (is_migration_entry(swpent)) 458 page = migration_entry_to_page(swpent); 459 } else if (pte_file(ptent)) { 460 if (pte_to_pgoff(ptent) != pgoff) 461 mss->nonlinear += ptent_size; 462 } 463 464 if (!page) 465 return; 466 467 if (PageAnon(page)) 468 mss->anonymous += ptent_size; 469 470 if (page->index != pgoff) 471 mss->nonlinear += ptent_size; 472 473 mss->resident += ptent_size; 474 /* Accumulate the size in pages that have been accessed. */ 475 if (pte_young(ptent) || PageReferenced(page)) 476 mss->referenced += ptent_size; 477 mapcount = page_mapcount(page); 478 if (mapcount >= 2) { 479 if (pte_dirty(ptent) || PageDirty(page)) 480 mss->shared_dirty += ptent_size; 481 else 482 mss->shared_clean += ptent_size; 483 mss->pss += (ptent_size << PSS_SHIFT) / mapcount; 484 } else { 485 if (pte_dirty(ptent) || PageDirty(page)) 486 mss->private_dirty += ptent_size; 487 else 488 mss->private_clean += ptent_size; 489 mss->pss += (ptent_size << PSS_SHIFT); 490 } 491 } 492 493 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 494 struct mm_walk *walk) 495 { 496 struct mem_size_stats *mss = walk->private; 497 struct vm_area_struct *vma = mss->vma; 498 pte_t *pte; 499 spinlock_t *ptl; 500 501 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) { 502 smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk); 503 spin_unlock(ptl); 504 mss->anonymous_thp += HPAGE_PMD_SIZE; 505 return 0; 506 } 507 508 if (pmd_trans_unstable(pmd)) 509 return 0; 510 /* 511 * The mmap_sem held all the way back in m_start() is what 512 * keeps khugepaged out of here and from collapsing things 513 * in here. 514 */ 515 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 516 for (; addr != end; pte++, addr += PAGE_SIZE) 517 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk); 518 pte_unmap_unlock(pte - 1, ptl); 519 cond_resched(); 520 return 0; 521 } 522 523 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) 524 { 525 /* 526 * Don't forget to update Documentation/ on changes. 527 */ 528 static const char mnemonics[BITS_PER_LONG][2] = { 529 /* 530 * In case if we meet a flag we don't know about. 531 */ 532 [0 ... (BITS_PER_LONG-1)] = "??", 533 534 [ilog2(VM_READ)] = "rd", 535 [ilog2(VM_WRITE)] = "wr", 536 [ilog2(VM_EXEC)] = "ex", 537 [ilog2(VM_SHARED)] = "sh", 538 [ilog2(VM_MAYREAD)] = "mr", 539 [ilog2(VM_MAYWRITE)] = "mw", 540 [ilog2(VM_MAYEXEC)] = "me", 541 [ilog2(VM_MAYSHARE)] = "ms", 542 [ilog2(VM_GROWSDOWN)] = "gd", 543 [ilog2(VM_PFNMAP)] = "pf", 544 [ilog2(VM_DENYWRITE)] = "dw", 545 [ilog2(VM_LOCKED)] = "lo", 546 [ilog2(VM_IO)] = "io", 547 [ilog2(VM_SEQ_READ)] = "sr", 548 [ilog2(VM_RAND_READ)] = "rr", 549 [ilog2(VM_DONTCOPY)] = "dc", 550 [ilog2(VM_DONTEXPAND)] = "de", 551 [ilog2(VM_ACCOUNT)] = "ac", 552 [ilog2(VM_NORESERVE)] = "nr", 553 [ilog2(VM_HUGETLB)] = "ht", 554 [ilog2(VM_NONLINEAR)] = "nl", 555 [ilog2(VM_ARCH_1)] = "ar", 556 [ilog2(VM_DONTDUMP)] = "dd", 557 #ifdef CONFIG_MEM_SOFT_DIRTY 558 [ilog2(VM_SOFTDIRTY)] = "sd", 559 #endif 560 [ilog2(VM_MIXEDMAP)] = "mm", 561 [ilog2(VM_HUGEPAGE)] = "hg", 562 [ilog2(VM_NOHUGEPAGE)] = "nh", 563 [ilog2(VM_MERGEABLE)] = "mg", 564 }; 565 size_t i; 566 567 seq_puts(m, "VmFlags: "); 568 for (i = 0; i < BITS_PER_LONG; i++) { 569 if (vma->vm_flags & (1UL << i)) { 570 seq_printf(m, "%c%c ", 571 mnemonics[i][0], mnemonics[i][1]); 572 } 573 } 574 seq_putc(m, '\n'); 575 } 576 577 static int show_smap(struct seq_file *m, void *v, int is_pid) 578 { 579 struct proc_maps_private *priv = m->private; 580 struct task_struct *task = priv->task; 581 struct vm_area_struct *vma = v; 582 struct mem_size_stats mss; 583 struct mm_walk smaps_walk = { 584 .pmd_entry = smaps_pte_range, 585 .mm = vma->vm_mm, 586 .private = &mss, 587 }; 588 589 memset(&mss, 0, sizeof mss); 590 mss.vma = vma; 591 /* mmap_sem is held in m_start */ 592 if (vma->vm_mm && !is_vm_hugetlb_page(vma)) 593 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk); 594 595 show_map_vma(m, vma, is_pid); 596 597 seq_printf(m, 598 "Size: %8lu kB\n" 599 "Rss: %8lu kB\n" 600 "Pss: %8lu kB\n" 601 "Shared_Clean: %8lu kB\n" 602 "Shared_Dirty: %8lu kB\n" 603 "Private_Clean: %8lu kB\n" 604 "Private_Dirty: %8lu kB\n" 605 "Referenced: %8lu kB\n" 606 "Anonymous: %8lu kB\n" 607 "AnonHugePages: %8lu kB\n" 608 "Swap: %8lu kB\n" 609 "KernelPageSize: %8lu kB\n" 610 "MMUPageSize: %8lu kB\n" 611 "Locked: %8lu kB\n", 612 (vma->vm_end - vma->vm_start) >> 10, 613 mss.resident >> 10, 614 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)), 615 mss.shared_clean >> 10, 616 mss.shared_dirty >> 10, 617 mss.private_clean >> 10, 618 mss.private_dirty >> 10, 619 mss.referenced >> 10, 620 mss.anonymous >> 10, 621 mss.anonymous_thp >> 10, 622 mss.swap >> 10, 623 vma_kernel_pagesize(vma) >> 10, 624 vma_mmu_pagesize(vma) >> 10, 625 (vma->vm_flags & VM_LOCKED) ? 626 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0); 627 628 if (vma->vm_flags & VM_NONLINEAR) 629 seq_printf(m, "Nonlinear: %8lu kB\n", 630 mss.nonlinear >> 10); 631 632 show_smap_vma_flags(m, vma); 633 634 if (m->count < m->size) /* vma is copied successfully */ 635 m->version = (vma != get_gate_vma(task->mm)) 636 ? vma->vm_start : 0; 637 return 0; 638 } 639 640 static int show_pid_smap(struct seq_file *m, void *v) 641 { 642 return show_smap(m, v, 1); 643 } 644 645 static int show_tid_smap(struct seq_file *m, void *v) 646 { 647 return show_smap(m, v, 0); 648 } 649 650 static const struct seq_operations proc_pid_smaps_op = { 651 .start = m_start, 652 .next = m_next, 653 .stop = m_stop, 654 .show = show_pid_smap 655 }; 656 657 static const struct seq_operations proc_tid_smaps_op = { 658 .start = m_start, 659 .next = m_next, 660 .stop = m_stop, 661 .show = show_tid_smap 662 }; 663 664 static int pid_smaps_open(struct inode *inode, struct file *file) 665 { 666 return do_maps_open(inode, file, &proc_pid_smaps_op); 667 } 668 669 static int tid_smaps_open(struct inode *inode, struct file *file) 670 { 671 return do_maps_open(inode, file, &proc_tid_smaps_op); 672 } 673 674 const struct file_operations proc_pid_smaps_operations = { 675 .open = pid_smaps_open, 676 .read = seq_read, 677 .llseek = seq_lseek, 678 .release = seq_release_private, 679 }; 680 681 const struct file_operations proc_tid_smaps_operations = { 682 .open = tid_smaps_open, 683 .read = seq_read, 684 .llseek = seq_lseek, 685 .release = seq_release_private, 686 }; 687 688 /* 689 * We do not want to have constant page-shift bits sitting in 690 * pagemap entries and are about to reuse them some time soon. 691 * 692 * Here's the "migration strategy": 693 * 1. when the system boots these bits remain what they are, 694 * but a warning about future change is printed in log; 695 * 2. once anyone clears soft-dirty bits via clear_refs file, 696 * these flag is set to denote, that user is aware of the 697 * new API and those page-shift bits change their meaning. 698 * The respective warning is printed in dmesg; 699 * 3. In a couple of releases we will remove all the mentions 700 * of page-shift in pagemap entries. 701 */ 702 703 static bool soft_dirty_cleared __read_mostly; 704 705 enum clear_refs_types { 706 CLEAR_REFS_ALL = 1, 707 CLEAR_REFS_ANON, 708 CLEAR_REFS_MAPPED, 709 CLEAR_REFS_SOFT_DIRTY, 710 CLEAR_REFS_LAST, 711 }; 712 713 struct clear_refs_private { 714 struct vm_area_struct *vma; 715 enum clear_refs_types type; 716 }; 717 718 static inline void clear_soft_dirty(struct vm_area_struct *vma, 719 unsigned long addr, pte_t *pte) 720 { 721 #ifdef CONFIG_MEM_SOFT_DIRTY 722 /* 723 * The soft-dirty tracker uses #PF-s to catch writes 724 * to pages, so write-protect the pte as well. See the 725 * Documentation/vm/soft-dirty.txt for full description 726 * of how soft-dirty works. 727 */ 728 pte_t ptent = *pte; 729 730 if (pte_present(ptent)) { 731 ptent = pte_wrprotect(ptent); 732 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY); 733 } else if (is_swap_pte(ptent)) { 734 ptent = pte_swp_clear_soft_dirty(ptent); 735 } else if (pte_file(ptent)) { 736 ptent = pte_file_clear_soft_dirty(ptent); 737 } 738 739 if (vma->vm_flags & VM_SOFTDIRTY) 740 vma->vm_flags &= ~VM_SOFTDIRTY; 741 742 set_pte_at(vma->vm_mm, addr, pte, ptent); 743 #endif 744 } 745 746 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, 747 unsigned long end, struct mm_walk *walk) 748 { 749 struct clear_refs_private *cp = walk->private; 750 struct vm_area_struct *vma = cp->vma; 751 pte_t *pte, ptent; 752 spinlock_t *ptl; 753 struct page *page; 754 755 split_huge_page_pmd(vma, addr, pmd); 756 if (pmd_trans_unstable(pmd)) 757 return 0; 758 759 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 760 for (; addr != end; pte++, addr += PAGE_SIZE) { 761 ptent = *pte; 762 763 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 764 clear_soft_dirty(vma, addr, pte); 765 continue; 766 } 767 768 if (!pte_present(ptent)) 769 continue; 770 771 page = vm_normal_page(vma, addr, ptent); 772 if (!page) 773 continue; 774 775 /* Clear accessed and referenced bits. */ 776 ptep_test_and_clear_young(vma, addr, pte); 777 ClearPageReferenced(page); 778 } 779 pte_unmap_unlock(pte - 1, ptl); 780 cond_resched(); 781 return 0; 782 } 783 784 static ssize_t clear_refs_write(struct file *file, const char __user *buf, 785 size_t count, loff_t *ppos) 786 { 787 struct task_struct *task; 788 char buffer[PROC_NUMBUF]; 789 struct mm_struct *mm; 790 struct vm_area_struct *vma; 791 enum clear_refs_types type; 792 int itype; 793 int rv; 794 795 memset(buffer, 0, sizeof(buffer)); 796 if (count > sizeof(buffer) - 1) 797 count = sizeof(buffer) - 1; 798 if (copy_from_user(buffer, buf, count)) 799 return -EFAULT; 800 rv = kstrtoint(strstrip(buffer), 10, &itype); 801 if (rv < 0) 802 return rv; 803 type = (enum clear_refs_types)itype; 804 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) 805 return -EINVAL; 806 807 if (type == CLEAR_REFS_SOFT_DIRTY) { 808 soft_dirty_cleared = true; 809 pr_warn_once("The pagemap bits 55-60 has changed their meaning! " 810 "See the linux/Documentation/vm/pagemap.txt for details.\n"); 811 } 812 813 task = get_proc_task(file_inode(file)); 814 if (!task) 815 return -ESRCH; 816 mm = get_task_mm(task); 817 if (mm) { 818 struct clear_refs_private cp = { 819 .type = type, 820 }; 821 struct mm_walk clear_refs_walk = { 822 .pmd_entry = clear_refs_pte_range, 823 .mm = mm, 824 .private = &cp, 825 }; 826 down_read(&mm->mmap_sem); 827 if (type == CLEAR_REFS_SOFT_DIRTY) 828 mmu_notifier_invalidate_range_start(mm, 0, -1); 829 for (vma = mm->mmap; vma; vma = vma->vm_next) { 830 cp.vma = vma; 831 if (is_vm_hugetlb_page(vma)) 832 continue; 833 /* 834 * Writing 1 to /proc/pid/clear_refs affects all pages. 835 * 836 * Writing 2 to /proc/pid/clear_refs only affects 837 * Anonymous pages. 838 * 839 * Writing 3 to /proc/pid/clear_refs only affects file 840 * mapped pages. 841 */ 842 if (type == CLEAR_REFS_ANON && vma->vm_file) 843 continue; 844 if (type == CLEAR_REFS_MAPPED && !vma->vm_file) 845 continue; 846 walk_page_range(vma->vm_start, vma->vm_end, 847 &clear_refs_walk); 848 } 849 if (type == CLEAR_REFS_SOFT_DIRTY) 850 mmu_notifier_invalidate_range_end(mm, 0, -1); 851 flush_tlb_mm(mm); 852 up_read(&mm->mmap_sem); 853 mmput(mm); 854 } 855 put_task_struct(task); 856 857 return count; 858 } 859 860 const struct file_operations proc_clear_refs_operations = { 861 .write = clear_refs_write, 862 .llseek = noop_llseek, 863 }; 864 865 typedef struct { 866 u64 pme; 867 } pagemap_entry_t; 868 869 struct pagemapread { 870 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ 871 pagemap_entry_t *buffer; 872 bool v2; 873 }; 874 875 #define PAGEMAP_WALK_SIZE (PMD_SIZE) 876 #define PAGEMAP_WALK_MASK (PMD_MASK) 877 878 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) 879 #define PM_STATUS_BITS 3 880 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS) 881 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET) 882 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK) 883 #define PM_PSHIFT_BITS 6 884 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS) 885 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET) 886 #define __PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK) 887 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1) 888 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK) 889 /* in "new" pagemap pshift bits are occupied with more status bits */ 890 #define PM_STATUS2(v2, x) (__PM_PSHIFT(v2 ? x : PAGE_SHIFT)) 891 892 #define __PM_SOFT_DIRTY (1LL) 893 #define PM_PRESENT PM_STATUS(4LL) 894 #define PM_SWAP PM_STATUS(2LL) 895 #define PM_FILE PM_STATUS(1LL) 896 #define PM_NOT_PRESENT(v2) PM_STATUS2(v2, 0) 897 #define PM_END_OF_BUFFER 1 898 899 static inline pagemap_entry_t make_pme(u64 val) 900 { 901 return (pagemap_entry_t) { .pme = val }; 902 } 903 904 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme, 905 struct pagemapread *pm) 906 { 907 pm->buffer[pm->pos++] = *pme; 908 if (pm->pos >= pm->len) 909 return PM_END_OF_BUFFER; 910 return 0; 911 } 912 913 static int pagemap_pte_hole(unsigned long start, unsigned long end, 914 struct mm_walk *walk) 915 { 916 struct pagemapread *pm = walk->private; 917 unsigned long addr; 918 int err = 0; 919 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2)); 920 921 for (addr = start; addr < end; addr += PAGE_SIZE) { 922 err = add_to_pagemap(addr, &pme, pm); 923 if (err) 924 break; 925 } 926 return err; 927 } 928 929 static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm, 930 struct vm_area_struct *vma, unsigned long addr, pte_t pte) 931 { 932 u64 frame, flags; 933 struct page *page = NULL; 934 int flags2 = 0; 935 936 if (pte_present(pte)) { 937 frame = pte_pfn(pte); 938 flags = PM_PRESENT; 939 page = vm_normal_page(vma, addr, pte); 940 if (pte_soft_dirty(pte)) 941 flags2 |= __PM_SOFT_DIRTY; 942 } else if (is_swap_pte(pte)) { 943 swp_entry_t entry; 944 if (pte_swp_soft_dirty(pte)) 945 flags2 |= __PM_SOFT_DIRTY; 946 entry = pte_to_swp_entry(pte); 947 frame = swp_type(entry) | 948 (swp_offset(entry) << MAX_SWAPFILES_SHIFT); 949 flags = PM_SWAP; 950 if (is_migration_entry(entry)) 951 page = migration_entry_to_page(entry); 952 } else { 953 if (vma->vm_flags & VM_SOFTDIRTY) 954 flags2 |= __PM_SOFT_DIRTY; 955 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2)); 956 return; 957 } 958 959 if (page && !PageAnon(page)) 960 flags |= PM_FILE; 961 if ((vma->vm_flags & VM_SOFTDIRTY)) 962 flags2 |= __PM_SOFT_DIRTY; 963 964 *pme = make_pme(PM_PFRAME(frame) | PM_STATUS2(pm->v2, flags2) | flags); 965 } 966 967 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 968 static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm, 969 pmd_t pmd, int offset, int pmd_flags2) 970 { 971 /* 972 * Currently pmd for thp is always present because thp can not be 973 * swapped-out, migrated, or HWPOISONed (split in such cases instead.) 974 * This if-check is just to prepare for future implementation. 975 */ 976 if (pmd_present(pmd)) 977 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset) 978 | PM_STATUS2(pm->v2, pmd_flags2) | PM_PRESENT); 979 else 980 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, pmd_flags2)); 981 } 982 #else 983 static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm, 984 pmd_t pmd, int offset, int pmd_flags2) 985 { 986 } 987 #endif 988 989 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 990 struct mm_walk *walk) 991 { 992 struct vm_area_struct *vma; 993 struct pagemapread *pm = walk->private; 994 spinlock_t *ptl; 995 pte_t *pte; 996 int err = 0; 997 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2)); 998 999 /* find the first VMA at or above 'addr' */ 1000 vma = find_vma(walk->mm, addr); 1001 if (vma && pmd_trans_huge_lock(pmd, vma, &ptl) == 1) { 1002 int pmd_flags2; 1003 1004 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(*pmd)) 1005 pmd_flags2 = __PM_SOFT_DIRTY; 1006 else 1007 pmd_flags2 = 0; 1008 1009 for (; addr != end; addr += PAGE_SIZE) { 1010 unsigned long offset; 1011 1012 offset = (addr & ~PAGEMAP_WALK_MASK) >> 1013 PAGE_SHIFT; 1014 thp_pmd_to_pagemap_entry(&pme, pm, *pmd, offset, pmd_flags2); 1015 err = add_to_pagemap(addr, &pme, pm); 1016 if (err) 1017 break; 1018 } 1019 spin_unlock(ptl); 1020 return err; 1021 } 1022 1023 if (pmd_trans_unstable(pmd)) 1024 return 0; 1025 for (; addr != end; addr += PAGE_SIZE) { 1026 int flags2; 1027 1028 /* check to see if we've left 'vma' behind 1029 * and need a new, higher one */ 1030 if (vma && (addr >= vma->vm_end)) { 1031 vma = find_vma(walk->mm, addr); 1032 if (vma && (vma->vm_flags & VM_SOFTDIRTY)) 1033 flags2 = __PM_SOFT_DIRTY; 1034 else 1035 flags2 = 0; 1036 pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2)); 1037 } 1038 1039 /* check that 'vma' actually covers this address, 1040 * and that it isn't a huge page vma */ 1041 if (vma && (vma->vm_start <= addr) && 1042 !is_vm_hugetlb_page(vma)) { 1043 pte = pte_offset_map(pmd, addr); 1044 pte_to_pagemap_entry(&pme, pm, vma, addr, *pte); 1045 /* unmap before userspace copy */ 1046 pte_unmap(pte); 1047 } 1048 err = add_to_pagemap(addr, &pme, pm); 1049 if (err) 1050 return err; 1051 } 1052 1053 cond_resched(); 1054 1055 return err; 1056 } 1057 1058 #ifdef CONFIG_HUGETLB_PAGE 1059 static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm, 1060 pte_t pte, int offset, int flags2) 1061 { 1062 if (pte_present(pte)) 1063 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) | 1064 PM_STATUS2(pm->v2, flags2) | 1065 PM_PRESENT); 1066 else 1067 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | 1068 PM_STATUS2(pm->v2, flags2)); 1069 } 1070 1071 /* This function walks within one hugetlb entry in the single call */ 1072 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask, 1073 unsigned long addr, unsigned long end, 1074 struct mm_walk *walk) 1075 { 1076 struct pagemapread *pm = walk->private; 1077 struct vm_area_struct *vma; 1078 int err = 0; 1079 int flags2; 1080 pagemap_entry_t pme; 1081 1082 vma = find_vma(walk->mm, addr); 1083 WARN_ON_ONCE(!vma); 1084 1085 if (vma && (vma->vm_flags & VM_SOFTDIRTY)) 1086 flags2 = __PM_SOFT_DIRTY; 1087 else 1088 flags2 = 0; 1089 1090 for (; addr != end; addr += PAGE_SIZE) { 1091 int offset = (addr & ~hmask) >> PAGE_SHIFT; 1092 huge_pte_to_pagemap_entry(&pme, pm, *pte, offset, flags2); 1093 err = add_to_pagemap(addr, &pme, pm); 1094 if (err) 1095 return err; 1096 } 1097 1098 cond_resched(); 1099 1100 return err; 1101 } 1102 #endif /* HUGETLB_PAGE */ 1103 1104 /* 1105 * /proc/pid/pagemap - an array mapping virtual pages to pfns 1106 * 1107 * For each page in the address space, this file contains one 64-bit entry 1108 * consisting of the following: 1109 * 1110 * Bits 0-54 page frame number (PFN) if present 1111 * Bits 0-4 swap type if swapped 1112 * Bits 5-54 swap offset if swapped 1113 * Bits 55-60 page shift (page size = 1<<page shift) 1114 * Bit 61 page is file-page or shared-anon 1115 * Bit 62 page swapped 1116 * Bit 63 page present 1117 * 1118 * If the page is not present but in swap, then the PFN contains an 1119 * encoding of the swap file number and the page's offset into the 1120 * swap. Unmapped pages return a null PFN. This allows determining 1121 * precisely which pages are mapped (or in swap) and comparing mapped 1122 * pages between processes. 1123 * 1124 * Efficient users of this interface will use /proc/pid/maps to 1125 * determine which areas of memory are actually mapped and llseek to 1126 * skip over unmapped regions. 1127 */ 1128 static ssize_t pagemap_read(struct file *file, char __user *buf, 1129 size_t count, loff_t *ppos) 1130 { 1131 struct task_struct *task = get_proc_task(file_inode(file)); 1132 struct mm_struct *mm; 1133 struct pagemapread pm; 1134 int ret = -ESRCH; 1135 struct mm_walk pagemap_walk = {}; 1136 unsigned long src; 1137 unsigned long svpfn; 1138 unsigned long start_vaddr; 1139 unsigned long end_vaddr; 1140 int copied = 0; 1141 1142 if (!task) 1143 goto out; 1144 1145 ret = -EINVAL; 1146 /* file position must be aligned */ 1147 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) 1148 goto out_task; 1149 1150 ret = 0; 1151 if (!count) 1152 goto out_task; 1153 1154 pm.v2 = soft_dirty_cleared; 1155 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); 1156 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY); 1157 ret = -ENOMEM; 1158 if (!pm.buffer) 1159 goto out_task; 1160 1161 mm = mm_access(task, PTRACE_MODE_READ); 1162 ret = PTR_ERR(mm); 1163 if (!mm || IS_ERR(mm)) 1164 goto out_free; 1165 1166 pagemap_walk.pmd_entry = pagemap_pte_range; 1167 pagemap_walk.pte_hole = pagemap_pte_hole; 1168 #ifdef CONFIG_HUGETLB_PAGE 1169 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range; 1170 #endif 1171 pagemap_walk.mm = mm; 1172 pagemap_walk.private = ± 1173 1174 src = *ppos; 1175 svpfn = src / PM_ENTRY_BYTES; 1176 start_vaddr = svpfn << PAGE_SHIFT; 1177 end_vaddr = TASK_SIZE_OF(task); 1178 1179 /* watch out for wraparound */ 1180 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT) 1181 start_vaddr = end_vaddr; 1182 1183 /* 1184 * The odds are that this will stop walking way 1185 * before end_vaddr, because the length of the 1186 * user buffer is tracked in "pm", and the walk 1187 * will stop when we hit the end of the buffer. 1188 */ 1189 ret = 0; 1190 while (count && (start_vaddr < end_vaddr)) { 1191 int len; 1192 unsigned long end; 1193 1194 pm.pos = 0; 1195 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; 1196 /* overflow ? */ 1197 if (end < start_vaddr || end > end_vaddr) 1198 end = end_vaddr; 1199 down_read(&mm->mmap_sem); 1200 ret = walk_page_range(start_vaddr, end, &pagemap_walk); 1201 up_read(&mm->mmap_sem); 1202 start_vaddr = end; 1203 1204 len = min(count, PM_ENTRY_BYTES * pm.pos); 1205 if (copy_to_user(buf, pm.buffer, len)) { 1206 ret = -EFAULT; 1207 goto out_mm; 1208 } 1209 copied += len; 1210 buf += len; 1211 count -= len; 1212 } 1213 *ppos += copied; 1214 if (!ret || ret == PM_END_OF_BUFFER) 1215 ret = copied; 1216 1217 out_mm: 1218 mmput(mm); 1219 out_free: 1220 kfree(pm.buffer); 1221 out_task: 1222 put_task_struct(task); 1223 out: 1224 return ret; 1225 } 1226 1227 static int pagemap_open(struct inode *inode, struct file *file) 1228 { 1229 pr_warn_once("Bits 55-60 of /proc/PID/pagemap entries are about " 1230 "to stop being page-shift some time soon. See the " 1231 "linux/Documentation/vm/pagemap.txt for details.\n"); 1232 return 0; 1233 } 1234 1235 const struct file_operations proc_pagemap_operations = { 1236 .llseek = mem_lseek, /* borrow this */ 1237 .read = pagemap_read, 1238 .open = pagemap_open, 1239 }; 1240 #endif /* CONFIG_PROC_PAGE_MONITOR */ 1241 1242 #ifdef CONFIG_NUMA 1243 1244 struct numa_maps { 1245 struct vm_area_struct *vma; 1246 unsigned long pages; 1247 unsigned long anon; 1248 unsigned long active; 1249 unsigned long writeback; 1250 unsigned long mapcount_max; 1251 unsigned long dirty; 1252 unsigned long swapcache; 1253 unsigned long node[MAX_NUMNODES]; 1254 }; 1255 1256 struct numa_maps_private { 1257 struct proc_maps_private proc_maps; 1258 struct numa_maps md; 1259 }; 1260 1261 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, 1262 unsigned long nr_pages) 1263 { 1264 int count = page_mapcount(page); 1265 1266 md->pages += nr_pages; 1267 if (pte_dirty || PageDirty(page)) 1268 md->dirty += nr_pages; 1269 1270 if (PageSwapCache(page)) 1271 md->swapcache += nr_pages; 1272 1273 if (PageActive(page) || PageUnevictable(page)) 1274 md->active += nr_pages; 1275 1276 if (PageWriteback(page)) 1277 md->writeback += nr_pages; 1278 1279 if (PageAnon(page)) 1280 md->anon += nr_pages; 1281 1282 if (count > md->mapcount_max) 1283 md->mapcount_max = count; 1284 1285 md->node[page_to_nid(page)] += nr_pages; 1286 } 1287 1288 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, 1289 unsigned long addr) 1290 { 1291 struct page *page; 1292 int nid; 1293 1294 if (!pte_present(pte)) 1295 return NULL; 1296 1297 page = vm_normal_page(vma, addr, pte); 1298 if (!page) 1299 return NULL; 1300 1301 if (PageReserved(page)) 1302 return NULL; 1303 1304 nid = page_to_nid(page); 1305 if (!node_isset(nid, node_states[N_MEMORY])) 1306 return NULL; 1307 1308 return page; 1309 } 1310 1311 static int gather_pte_stats(pmd_t *pmd, unsigned long addr, 1312 unsigned long end, struct mm_walk *walk) 1313 { 1314 struct numa_maps *md; 1315 spinlock_t *ptl; 1316 pte_t *orig_pte; 1317 pte_t *pte; 1318 1319 md = walk->private; 1320 1321 if (pmd_trans_huge_lock(pmd, md->vma, &ptl) == 1) { 1322 pte_t huge_pte = *(pte_t *)pmd; 1323 struct page *page; 1324 1325 page = can_gather_numa_stats(huge_pte, md->vma, addr); 1326 if (page) 1327 gather_stats(page, md, pte_dirty(huge_pte), 1328 HPAGE_PMD_SIZE/PAGE_SIZE); 1329 spin_unlock(ptl); 1330 return 0; 1331 } 1332 1333 if (pmd_trans_unstable(pmd)) 1334 return 0; 1335 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); 1336 do { 1337 struct page *page = can_gather_numa_stats(*pte, md->vma, addr); 1338 if (!page) 1339 continue; 1340 gather_stats(page, md, pte_dirty(*pte), 1); 1341 1342 } while (pte++, addr += PAGE_SIZE, addr != end); 1343 pte_unmap_unlock(orig_pte, ptl); 1344 return 0; 1345 } 1346 #ifdef CONFIG_HUGETLB_PAGE 1347 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask, 1348 unsigned long addr, unsigned long end, struct mm_walk *walk) 1349 { 1350 struct numa_maps *md; 1351 struct page *page; 1352 1353 if (pte_none(*pte)) 1354 return 0; 1355 1356 page = pte_page(*pte); 1357 if (!page) 1358 return 0; 1359 1360 md = walk->private; 1361 gather_stats(page, md, pte_dirty(*pte), 1); 1362 return 0; 1363 } 1364 1365 #else 1366 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask, 1367 unsigned long addr, unsigned long end, struct mm_walk *walk) 1368 { 1369 return 0; 1370 } 1371 #endif 1372 1373 /* 1374 * Display pages allocated per node and memory policy via /proc. 1375 */ 1376 static int show_numa_map(struct seq_file *m, void *v, int is_pid) 1377 { 1378 struct numa_maps_private *numa_priv = m->private; 1379 struct proc_maps_private *proc_priv = &numa_priv->proc_maps; 1380 struct vm_area_struct *vma = v; 1381 struct numa_maps *md = &numa_priv->md; 1382 struct file *file = vma->vm_file; 1383 struct task_struct *task = proc_priv->task; 1384 struct mm_struct *mm = vma->vm_mm; 1385 struct mm_walk walk = {}; 1386 struct mempolicy *pol; 1387 char buffer[64]; 1388 int nid; 1389 1390 if (!mm) 1391 return 0; 1392 1393 /* Ensure we start with an empty set of numa_maps statistics. */ 1394 memset(md, 0, sizeof(*md)); 1395 1396 md->vma = vma; 1397 1398 walk.hugetlb_entry = gather_hugetbl_stats; 1399 walk.pmd_entry = gather_pte_stats; 1400 walk.private = md; 1401 walk.mm = mm; 1402 1403 pol = get_vma_policy(task, vma, vma->vm_start); 1404 mpol_to_str(buffer, sizeof(buffer), pol); 1405 mpol_cond_put(pol); 1406 1407 seq_printf(m, "%08lx %s", vma->vm_start, buffer); 1408 1409 if (file) { 1410 seq_printf(m, " file="); 1411 seq_path(m, &file->f_path, "\n\t= "); 1412 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { 1413 seq_printf(m, " heap"); 1414 } else { 1415 pid_t tid = vm_is_stack(task, vma, is_pid); 1416 if (tid != 0) { 1417 /* 1418 * Thread stack in /proc/PID/task/TID/maps or 1419 * the main process stack. 1420 */ 1421 if (!is_pid || (vma->vm_start <= mm->start_stack && 1422 vma->vm_end >= mm->start_stack)) 1423 seq_printf(m, " stack"); 1424 else 1425 seq_printf(m, " stack:%d", tid); 1426 } 1427 } 1428 1429 if (is_vm_hugetlb_page(vma)) 1430 seq_printf(m, " huge"); 1431 1432 walk_page_range(vma->vm_start, vma->vm_end, &walk); 1433 1434 if (!md->pages) 1435 goto out; 1436 1437 if (md->anon) 1438 seq_printf(m, " anon=%lu", md->anon); 1439 1440 if (md->dirty) 1441 seq_printf(m, " dirty=%lu", md->dirty); 1442 1443 if (md->pages != md->anon && md->pages != md->dirty) 1444 seq_printf(m, " mapped=%lu", md->pages); 1445 1446 if (md->mapcount_max > 1) 1447 seq_printf(m, " mapmax=%lu", md->mapcount_max); 1448 1449 if (md->swapcache) 1450 seq_printf(m, " swapcache=%lu", md->swapcache); 1451 1452 if (md->active < md->pages && !is_vm_hugetlb_page(vma)) 1453 seq_printf(m, " active=%lu", md->active); 1454 1455 if (md->writeback) 1456 seq_printf(m, " writeback=%lu", md->writeback); 1457 1458 for_each_node_state(nid, N_MEMORY) 1459 if (md->node[nid]) 1460 seq_printf(m, " N%d=%lu", nid, md->node[nid]); 1461 out: 1462 seq_putc(m, '\n'); 1463 1464 if (m->count < m->size) 1465 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0; 1466 return 0; 1467 } 1468 1469 static int show_pid_numa_map(struct seq_file *m, void *v) 1470 { 1471 return show_numa_map(m, v, 1); 1472 } 1473 1474 static int show_tid_numa_map(struct seq_file *m, void *v) 1475 { 1476 return show_numa_map(m, v, 0); 1477 } 1478 1479 static const struct seq_operations proc_pid_numa_maps_op = { 1480 .start = m_start, 1481 .next = m_next, 1482 .stop = m_stop, 1483 .show = show_pid_numa_map, 1484 }; 1485 1486 static const struct seq_operations proc_tid_numa_maps_op = { 1487 .start = m_start, 1488 .next = m_next, 1489 .stop = m_stop, 1490 .show = show_tid_numa_map, 1491 }; 1492 1493 static int numa_maps_open(struct inode *inode, struct file *file, 1494 const struct seq_operations *ops) 1495 { 1496 struct numa_maps_private *priv; 1497 int ret = -ENOMEM; 1498 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1499 if (priv) { 1500 priv->proc_maps.pid = proc_pid(inode); 1501 ret = seq_open(file, ops); 1502 if (!ret) { 1503 struct seq_file *m = file->private_data; 1504 m->private = priv; 1505 } else { 1506 kfree(priv); 1507 } 1508 } 1509 return ret; 1510 } 1511 1512 static int pid_numa_maps_open(struct inode *inode, struct file *file) 1513 { 1514 return numa_maps_open(inode, file, &proc_pid_numa_maps_op); 1515 } 1516 1517 static int tid_numa_maps_open(struct inode *inode, struct file *file) 1518 { 1519 return numa_maps_open(inode, file, &proc_tid_numa_maps_op); 1520 } 1521 1522 const struct file_operations proc_pid_numa_maps_operations = { 1523 .open = pid_numa_maps_open, 1524 .read = seq_read, 1525 .llseek = seq_lseek, 1526 .release = seq_release_private, 1527 }; 1528 1529 const struct file_operations proc_tid_numa_maps_operations = { 1530 .open = tid_numa_maps_open, 1531 .read = seq_read, 1532 .llseek = seq_lseek, 1533 .release = seq_release_private, 1534 }; 1535 #endif /* CONFIG_NUMA */ 1536