1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/pagewalk.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>
25 #include <asm/tlbflush.h>
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)
32 unsigned long text, lib, swap, anon, file, shmem;
33 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
35 anon = get_mm_counter(mm, MM_ANONPAGES);
36 file = get_mm_counter(mm, MM_FILEPAGES);
37 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
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.
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;
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;
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);
78 hugetlb_report_usage(m, mm);
82 unsigned long task_vsize(struct mm_struct *mm)
84 return PAGE_SIZE * mm->total_vm;
87 unsigned long task_statm(struct mm_struct *mm,
88 unsigned long *shared, unsigned long *text,
89 unsigned long *data, unsigned long *resident)
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))
95 *data = mm->data_vm + mm->stack_vm;
96 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
102 * Save get_task_policy() for show_numa_map().
104 static void hold_task_mempolicy(struct proc_maps_private *priv)
106 struct task_struct *task = priv->task;
109 priv->task_mempolicy = get_task_policy(task);
110 mpol_get(priv->task_mempolicy);
113 static void release_task_mempolicy(struct proc_maps_private *priv)
115 mpol_put(priv->task_mempolicy);
118 static void hold_task_mempolicy(struct proc_maps_private *priv)
121 static void release_task_mempolicy(struct proc_maps_private *priv)
126 static void *m_start(struct seq_file *m, loff_t *ppos)
128 struct proc_maps_private *priv = m->private;
129 unsigned long last_addr = *ppos;
130 struct mm_struct *mm;
131 struct vm_area_struct *vma;
133 /* See m_next(). Zero at the start or after lseek. */
134 if (last_addr == -1UL)
137 priv->task = get_proc_task(priv->inode);
139 return ERR_PTR(-ESRCH);
142 if (!mm || !mmget_not_zero(mm)) {
143 put_task_struct(priv->task);
148 if (mmap_read_lock_killable(mm)) {
150 put_task_struct(priv->task);
152 return ERR_PTR(-EINTR);
155 hold_task_mempolicy(priv);
156 priv->tail_vma = get_gate_vma(mm);
158 vma = find_vma(mm, last_addr);
162 return priv->tail_vma;
165 static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
167 struct proc_maps_private *priv = m->private;
168 struct vm_area_struct *next, *vma = v;
170 if (vma == priv->tail_vma)
172 else if (vma->vm_next)
175 next = priv->tail_vma;
177 *ppos = next ? next->vm_start : -1UL;
182 static void m_stop(struct seq_file *m, void *v)
184 struct proc_maps_private *priv = m->private;
185 struct mm_struct *mm = priv->mm;
190 release_task_mempolicy(priv);
191 mmap_read_unlock(mm);
193 put_task_struct(priv->task);
197 static int proc_maps_open(struct inode *inode, struct file *file,
198 const struct seq_operations *ops, int psize)
200 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
206 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
207 if (IS_ERR(priv->mm)) {
208 int err = PTR_ERR(priv->mm);
210 seq_release_private(inode, file);
217 static int proc_map_release(struct inode *inode, struct file *file)
219 struct seq_file *seq = file->private_data;
220 struct proc_maps_private *priv = seq->private;
225 return seq_release_private(inode, file);
228 static int do_maps_open(struct inode *inode, struct file *file,
229 const struct seq_operations *ops)
231 return proc_maps_open(inode, file, ops,
232 sizeof(struct proc_maps_private));
236 * Indicate if the VMA is a stack for the given task; for
237 * /proc/PID/maps that is the stack of the main task.
239 static int is_stack(struct vm_area_struct *vma)
242 * We make no effort to guess what a given thread considers to be
243 * its "stack". It's not even well-defined for programs written
246 return vma->vm_start <= vma->vm_mm->start_stack &&
247 vma->vm_end >= vma->vm_mm->start_stack;
250 static void show_vma_header_prefix(struct seq_file *m,
251 unsigned long start, unsigned long end,
252 vm_flags_t flags, unsigned long long pgoff,
253 dev_t dev, unsigned long ino)
255 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
256 seq_put_hex_ll(m, NULL, start, 8);
257 seq_put_hex_ll(m, "-", end, 8);
259 seq_putc(m, flags & VM_READ ? 'r' : '-');
260 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
261 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
262 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
263 seq_put_hex_ll(m, " ", pgoff, 8);
264 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
265 seq_put_hex_ll(m, ":", MINOR(dev), 2);
266 seq_put_decimal_ull(m, " ", ino);
271 show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
273 struct mm_struct *mm = vma->vm_mm;
274 struct file *file = vma->vm_file;
275 vm_flags_t flags = vma->vm_flags;
276 unsigned long ino = 0;
277 unsigned long long pgoff = 0;
278 unsigned long start, end;
280 const char *name = NULL;
283 struct inode *inode = file_inode(vma->vm_file);
284 dev = inode->i_sb->s_dev;
286 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
289 start = vma->vm_start;
291 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
294 * Print the dentry name for named mappings, and a
295 * special [heap] marker for the heap:
299 seq_file_path(m, file, "\n");
303 if (vma->vm_ops && vma->vm_ops->name) {
304 name = vma->vm_ops->name(vma);
309 name = arch_vma_name(vma);
316 if (vma->vm_start <= mm->brk &&
317 vma->vm_end >= mm->start_brk) {
334 static int show_map(struct seq_file *m, void *v)
340 static const struct seq_operations proc_pid_maps_op = {
347 static int pid_maps_open(struct inode *inode, struct file *file)
349 return do_maps_open(inode, file, &proc_pid_maps_op);
352 const struct file_operations proc_pid_maps_operations = {
353 .open = pid_maps_open,
356 .release = proc_map_release,
360 * Proportional Set Size(PSS): my share of RSS.
362 * PSS of a process is the count of pages it has in memory, where each
363 * page is divided by the number of processes sharing it. So if a
364 * process has 1000 pages all to itself, and 1000 shared with one other
365 * process, its PSS will be 1500.
367 * To keep (accumulated) division errors low, we adopt a 64bit
368 * fixed-point pss counter to minimize division errors. So (pss >>
369 * PSS_SHIFT) would be the real byte count.
371 * A shift of 12 before division means (assuming 4K page size):
372 * - 1M 3-user-pages add up to 8KB errors;
373 * - supports mapcount up to 2^24, or 16M;
374 * - supports PSS up to 2^52 bytes, or 4PB.
378 #ifdef CONFIG_PROC_PAGE_MONITOR
379 struct mem_size_stats {
380 unsigned long resident;
381 unsigned long shared_clean;
382 unsigned long shared_dirty;
383 unsigned long private_clean;
384 unsigned long private_dirty;
385 unsigned long referenced;
386 unsigned long anonymous;
387 unsigned long lazyfree;
388 unsigned long anonymous_thp;
389 unsigned long shmem_thp;
390 unsigned long file_thp;
392 unsigned long shared_hugetlb;
393 unsigned long private_hugetlb;
400 bool check_shmem_swap;
403 static void smaps_page_accumulate(struct mem_size_stats *mss,
404 struct page *page, unsigned long size, unsigned long pss,
405 bool dirty, bool locked, bool private)
410 mss->pss_anon += pss;
411 else if (PageSwapBacked(page))
412 mss->pss_shmem += pss;
414 mss->pss_file += pss;
417 mss->pss_locked += pss;
419 if (dirty || PageDirty(page)) {
421 mss->private_dirty += size;
423 mss->shared_dirty += size;
426 mss->private_clean += size;
428 mss->shared_clean += size;
432 static void smaps_account(struct mem_size_stats *mss, struct page *page,
433 bool compound, bool young, bool dirty, bool locked)
435 int i, nr = compound ? compound_nr(page) : 1;
436 unsigned long size = nr * PAGE_SIZE;
439 * First accumulate quantities that depend only on |size| and the type
440 * of the compound page.
442 if (PageAnon(page)) {
443 mss->anonymous += size;
444 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
445 mss->lazyfree += size;
448 mss->resident += size;
449 /* Accumulate the size in pages that have been accessed. */
450 if (young || page_is_young(page) || PageReferenced(page))
451 mss->referenced += size;
454 * Then accumulate quantities that may depend on sharing, or that may
455 * differ page-by-page.
457 * page_count(page) == 1 guarantees the page is mapped exactly once.
458 * If any subpage of the compound page mapped with PTE it would elevate
461 if (page_count(page) == 1) {
462 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
466 for (i = 0; i < nr; i++, page++) {
467 int mapcount = page_mapcount(page);
468 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
471 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
477 static int smaps_pte_hole(unsigned long addr, unsigned long end,
478 __always_unused int depth, struct mm_walk *walk)
480 struct mem_size_stats *mss = walk->private;
482 mss->swap += shmem_partial_swap_usage(
483 walk->vma->vm_file->f_mapping, addr, end);
488 #define smaps_pte_hole NULL
489 #endif /* CONFIG_SHMEM */
491 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
492 struct mm_walk *walk)
494 struct mem_size_stats *mss = walk->private;
495 struct vm_area_struct *vma = walk->vma;
496 bool locked = !!(vma->vm_flags & VM_LOCKED);
497 struct page *page = NULL;
499 if (pte_present(*pte)) {
500 page = vm_normal_page(vma, addr, *pte);
501 } else if (is_swap_pte(*pte)) {
502 swp_entry_t swpent = pte_to_swp_entry(*pte);
504 if (!non_swap_entry(swpent)) {
507 mss->swap += PAGE_SIZE;
508 mapcount = swp_swapcount(swpent);
510 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
512 do_div(pss_delta, mapcount);
513 mss->swap_pss += pss_delta;
515 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
517 } else if (is_migration_entry(swpent))
518 page = migration_entry_to_page(swpent);
519 else if (is_device_private_entry(swpent))
520 page = device_private_entry_to_page(swpent);
521 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
522 && pte_none(*pte))) {
523 page = xa_load(&vma->vm_file->f_mapping->i_pages,
524 linear_page_index(vma, addr));
525 if (xa_is_value(page))
526 mss->swap += PAGE_SIZE;
533 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked);
536 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
537 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
538 struct mm_walk *walk)
540 struct mem_size_stats *mss = walk->private;
541 struct vm_area_struct *vma = walk->vma;
542 bool locked = !!(vma->vm_flags & VM_LOCKED);
543 struct page *page = NULL;
545 if (pmd_present(*pmd)) {
546 /* FOLL_DUMP will return -EFAULT on huge zero page */
547 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
548 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
549 swp_entry_t entry = pmd_to_swp_entry(*pmd);
551 if (is_migration_entry(entry))
552 page = migration_entry_to_page(entry);
554 if (IS_ERR_OR_NULL(page))
557 mss->anonymous_thp += HPAGE_PMD_SIZE;
558 else if (PageSwapBacked(page))
559 mss->shmem_thp += HPAGE_PMD_SIZE;
560 else if (is_zone_device_page(page))
563 mss->file_thp += HPAGE_PMD_SIZE;
564 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
567 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
568 struct mm_walk *walk)
573 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
574 struct mm_walk *walk)
576 struct vm_area_struct *vma = walk->vma;
580 ptl = pmd_trans_huge_lock(pmd, vma);
582 smaps_pmd_entry(pmd, addr, walk);
587 if (pmd_trans_unstable(pmd))
590 * The mmap_lock held all the way back in m_start() is what
591 * keeps khugepaged out of here and from collapsing things
594 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
595 for (; addr != end; pte++, addr += PAGE_SIZE)
596 smaps_pte_entry(pte, addr, walk);
597 pte_unmap_unlock(pte - 1, ptl);
603 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
606 * Don't forget to update Documentation/ on changes.
608 static const char mnemonics[BITS_PER_LONG][2] = {
610 * In case if we meet a flag we don't know about.
612 [0 ... (BITS_PER_LONG-1)] = "??",
614 [ilog2(VM_READ)] = "rd",
615 [ilog2(VM_WRITE)] = "wr",
616 [ilog2(VM_EXEC)] = "ex",
617 [ilog2(VM_SHARED)] = "sh",
618 [ilog2(VM_MAYREAD)] = "mr",
619 [ilog2(VM_MAYWRITE)] = "mw",
620 [ilog2(VM_MAYEXEC)] = "me",
621 [ilog2(VM_MAYSHARE)] = "ms",
622 [ilog2(VM_GROWSDOWN)] = "gd",
623 [ilog2(VM_PFNMAP)] = "pf",
624 [ilog2(VM_DENYWRITE)] = "dw",
625 [ilog2(VM_LOCKED)] = "lo",
626 [ilog2(VM_IO)] = "io",
627 [ilog2(VM_SEQ_READ)] = "sr",
628 [ilog2(VM_RAND_READ)] = "rr",
629 [ilog2(VM_DONTCOPY)] = "dc",
630 [ilog2(VM_DONTEXPAND)] = "de",
631 [ilog2(VM_ACCOUNT)] = "ac",
632 [ilog2(VM_NORESERVE)] = "nr",
633 [ilog2(VM_HUGETLB)] = "ht",
634 [ilog2(VM_SYNC)] = "sf",
635 [ilog2(VM_ARCH_1)] = "ar",
636 [ilog2(VM_WIPEONFORK)] = "wf",
637 [ilog2(VM_DONTDUMP)] = "dd",
638 #ifdef CONFIG_ARM64_BTI
639 [ilog2(VM_ARM64_BTI)] = "bt",
641 #ifdef CONFIG_MEM_SOFT_DIRTY
642 [ilog2(VM_SOFTDIRTY)] = "sd",
644 [ilog2(VM_MIXEDMAP)] = "mm",
645 [ilog2(VM_HUGEPAGE)] = "hg",
646 [ilog2(VM_NOHUGEPAGE)] = "nh",
647 [ilog2(VM_MERGEABLE)] = "mg",
648 [ilog2(VM_UFFD_MISSING)]= "um",
649 [ilog2(VM_UFFD_WP)] = "uw",
650 #ifdef CONFIG_ARM64_MTE
651 [ilog2(VM_MTE)] = "mt",
652 [ilog2(VM_MTE_ALLOWED)] = "",
654 #ifdef CONFIG_ARCH_HAS_PKEYS
655 /* These come out via ProtectionKey: */
656 [ilog2(VM_PKEY_BIT0)] = "",
657 [ilog2(VM_PKEY_BIT1)] = "",
658 [ilog2(VM_PKEY_BIT2)] = "",
659 [ilog2(VM_PKEY_BIT3)] = "",
661 [ilog2(VM_PKEY_BIT4)] = "",
663 #endif /* CONFIG_ARCH_HAS_PKEYS */
667 seq_puts(m, "VmFlags: ");
668 for (i = 0; i < BITS_PER_LONG; i++) {
669 if (!mnemonics[i][0])
671 if (vma->vm_flags & (1UL << i)) {
672 seq_putc(m, mnemonics[i][0]);
673 seq_putc(m, mnemonics[i][1]);
680 #ifdef CONFIG_HUGETLB_PAGE
681 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
682 unsigned long addr, unsigned long end,
683 struct mm_walk *walk)
685 struct mem_size_stats *mss = walk->private;
686 struct vm_area_struct *vma = walk->vma;
687 struct page *page = NULL;
689 if (pte_present(*pte)) {
690 page = vm_normal_page(vma, addr, *pte);
691 } else if (is_swap_pte(*pte)) {
692 swp_entry_t swpent = pte_to_swp_entry(*pte);
694 if (is_migration_entry(swpent))
695 page = migration_entry_to_page(swpent);
696 else if (is_device_private_entry(swpent))
697 page = device_private_entry_to_page(swpent);
700 int mapcount = page_mapcount(page);
703 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
705 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
710 #define smaps_hugetlb_range NULL
711 #endif /* HUGETLB_PAGE */
713 static const struct mm_walk_ops smaps_walk_ops = {
714 .pmd_entry = smaps_pte_range,
715 .hugetlb_entry = smaps_hugetlb_range,
718 static const struct mm_walk_ops smaps_shmem_walk_ops = {
719 .pmd_entry = smaps_pte_range,
720 .hugetlb_entry = smaps_hugetlb_range,
721 .pte_hole = smaps_pte_hole,
725 * Gather mem stats from @vma with the indicated beginning
726 * address @start, and keep them in @mss.
728 * Use vm_start of @vma as the beginning address if @start is 0.
730 static void smap_gather_stats(struct vm_area_struct *vma,
731 struct mem_size_stats *mss, unsigned long start)
733 const struct mm_walk_ops *ops = &smaps_walk_ops;
736 if (start >= vma->vm_end)
740 /* In case of smaps_rollup, reset the value from previous vma */
741 mss->check_shmem_swap = false;
742 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
744 * For shared or readonly shmem mappings we know that all
745 * swapped out pages belong to the shmem object, and we can
746 * obtain the swap value much more efficiently. For private
747 * writable mappings, we might have COW pages that are
748 * not affected by the parent swapped out pages of the shmem
749 * object, so we have to distinguish them during the page walk.
750 * Unless we know that the shmem object (or the part mapped by
751 * our VMA) has no swapped out pages at all.
753 unsigned long shmem_swapped = shmem_swap_usage(vma);
755 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
756 !(vma->vm_flags & VM_WRITE))) {
757 mss->swap += shmem_swapped;
759 mss->check_shmem_swap = true;
760 ops = &smaps_shmem_walk_ops;
764 /* mmap_lock is held in m_start */
766 walk_page_vma(vma, ops, mss);
768 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
771 #define SEQ_PUT_DEC(str, val) \
772 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
774 /* Show the contents common for smaps and smaps_rollup */
775 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
778 SEQ_PUT_DEC("Rss: ", mss->resident);
779 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
782 * These are meaningful only for smaps_rollup, otherwise two of
783 * them are zero, and the other one is the same as Pss.
785 SEQ_PUT_DEC(" kB\nPss_Anon: ",
786 mss->pss_anon >> PSS_SHIFT);
787 SEQ_PUT_DEC(" kB\nPss_File: ",
788 mss->pss_file >> PSS_SHIFT);
789 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
790 mss->pss_shmem >> PSS_SHIFT);
792 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
793 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
794 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
795 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
796 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
797 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
798 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
799 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
800 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
801 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
802 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
803 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
804 mss->private_hugetlb >> 10, 7);
805 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
806 SEQ_PUT_DEC(" kB\nSwapPss: ",
807 mss->swap_pss >> PSS_SHIFT);
808 SEQ_PUT_DEC(" kB\nLocked: ",
809 mss->pss_locked >> PSS_SHIFT);
810 seq_puts(m, " kB\n");
813 static int show_smap(struct seq_file *m, void *v)
815 struct vm_area_struct *vma = v;
816 struct mem_size_stats mss;
818 memset(&mss, 0, sizeof(mss));
820 smap_gather_stats(vma, &mss, 0);
822 show_map_vma(m, vma);
824 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
825 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
826 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
827 seq_puts(m, " kB\n");
829 __show_smap(m, &mss, false);
831 seq_printf(m, "THPeligible: %d\n",
832 transparent_hugepage_enabled(vma));
834 if (arch_pkeys_enabled())
835 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
836 show_smap_vma_flags(m, vma);
841 static int show_smaps_rollup(struct seq_file *m, void *v)
843 struct proc_maps_private *priv = m->private;
844 struct mem_size_stats mss;
845 struct mm_struct *mm;
846 struct vm_area_struct *vma;
847 unsigned long last_vma_end = 0;
850 priv->task = get_proc_task(priv->inode);
855 if (!mm || !mmget_not_zero(mm)) {
860 memset(&mss, 0, sizeof(mss));
862 ret = mmap_read_lock_killable(mm);
866 hold_task_mempolicy(priv);
868 for (vma = priv->mm->mmap; vma;) {
869 smap_gather_stats(vma, &mss, 0);
870 last_vma_end = vma->vm_end;
873 * Release mmap_lock temporarily if someone wants to
874 * access it for write request.
876 if (mmap_lock_is_contended(mm)) {
877 mmap_read_unlock(mm);
878 ret = mmap_read_lock_killable(mm);
880 release_task_mempolicy(priv);
885 * After dropping the lock, there are four cases to
886 * consider. See the following example for explanation.
888 * +------+------+-----------+
889 * | VMA1 | VMA2 | VMA3 |
890 * +------+------+-----------+
894 * Suppose we drop the lock after reading VMA2 due to
895 * contention, then we get:
899 * 1) VMA2 is freed, but VMA3 exists:
901 * find_vma(mm, 16k - 1) will return VMA3.
902 * In this case, just continue from VMA3.
904 * 2) VMA2 still exists:
906 * find_vma(mm, 16k - 1) will return VMA2.
907 * Iterate the loop like the original one.
909 * 3) No more VMAs can be found:
911 * find_vma(mm, 16k - 1) will return NULL.
912 * No more things to do, just break.
914 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
916 * find_vma(mm, 16k - 1) will return VMA' whose range
917 * contains last_vma_end.
918 * Iterate VMA' from last_vma_end.
920 vma = find_vma(mm, last_vma_end - 1);
926 if (vma->vm_start >= last_vma_end)
930 if (vma->vm_end > last_vma_end)
931 smap_gather_stats(vma, &mss, last_vma_end);
937 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
938 last_vma_end, 0, 0, 0, 0);
940 seq_puts(m, "[rollup]\n");
942 __show_smap(m, &mss, true);
944 release_task_mempolicy(priv);
945 mmap_read_unlock(mm);
950 put_task_struct(priv->task);
957 static const struct seq_operations proc_pid_smaps_op = {
964 static int pid_smaps_open(struct inode *inode, struct file *file)
966 return do_maps_open(inode, file, &proc_pid_smaps_op);
969 static int smaps_rollup_open(struct inode *inode, struct file *file)
972 struct proc_maps_private *priv;
974 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
978 ret = single_open(file, show_smaps_rollup, priv);
983 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
984 if (IS_ERR(priv->mm)) {
985 ret = PTR_ERR(priv->mm);
987 single_release(inode, file);
998 static int smaps_rollup_release(struct inode *inode, struct file *file)
1000 struct seq_file *seq = file->private_data;
1001 struct proc_maps_private *priv = seq->private;
1007 return single_release(inode, file);
1010 const struct file_operations proc_pid_smaps_operations = {
1011 .open = pid_smaps_open,
1013 .llseek = seq_lseek,
1014 .release = proc_map_release,
1017 const struct file_operations proc_pid_smaps_rollup_operations = {
1018 .open = smaps_rollup_open,
1020 .llseek = seq_lseek,
1021 .release = smaps_rollup_release,
1024 enum clear_refs_types {
1028 CLEAR_REFS_SOFT_DIRTY,
1029 CLEAR_REFS_MM_HIWATER_RSS,
1033 struct clear_refs_private {
1034 enum clear_refs_types type;
1037 #ifdef CONFIG_MEM_SOFT_DIRTY
1038 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1039 unsigned long addr, pte_t *pte)
1042 * The soft-dirty tracker uses #PF-s to catch writes
1043 * to pages, so write-protect the pte as well. See the
1044 * Documentation/admin-guide/mm/soft-dirty.rst for full description
1045 * of how soft-dirty works.
1049 if (pte_present(ptent)) {
1052 old_pte = ptep_modify_prot_start(vma, addr, pte);
1053 ptent = pte_wrprotect(old_pte);
1054 ptent = pte_clear_soft_dirty(ptent);
1055 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
1056 } else if (is_swap_pte(ptent)) {
1057 ptent = pte_swp_clear_soft_dirty(ptent);
1058 set_pte_at(vma->vm_mm, addr, pte, ptent);
1062 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1063 unsigned long addr, pte_t *pte)
1068 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1069 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1070 unsigned long addr, pmd_t *pmdp)
1072 pmd_t old, pmd = *pmdp;
1074 if (pmd_present(pmd)) {
1075 /* See comment in change_huge_pmd() */
1076 old = pmdp_invalidate(vma, addr, pmdp);
1078 pmd = pmd_mkdirty(pmd);
1080 pmd = pmd_mkyoung(pmd);
1082 pmd = pmd_wrprotect(pmd);
1083 pmd = pmd_clear_soft_dirty(pmd);
1085 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1086 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1087 pmd = pmd_swp_clear_soft_dirty(pmd);
1088 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1092 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1093 unsigned long addr, pmd_t *pmdp)
1098 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
1099 unsigned long end, struct mm_walk *walk)
1101 struct clear_refs_private *cp = walk->private;
1102 struct vm_area_struct *vma = walk->vma;
1107 ptl = pmd_trans_huge_lock(pmd, vma);
1109 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1110 clear_soft_dirty_pmd(vma, addr, pmd);
1114 if (!pmd_present(*pmd))
1117 page = pmd_page(*pmd);
1119 /* Clear accessed and referenced bits. */
1120 pmdp_test_and_clear_young(vma, addr, pmd);
1121 test_and_clear_page_young(page);
1122 ClearPageReferenced(page);
1128 if (pmd_trans_unstable(pmd))
1131 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1132 for (; addr != end; pte++, addr += PAGE_SIZE) {
1135 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1136 clear_soft_dirty(vma, addr, pte);
1140 if (!pte_present(ptent))
1143 page = vm_normal_page(vma, addr, ptent);
1147 /* Clear accessed and referenced bits. */
1148 ptep_test_and_clear_young(vma, addr, pte);
1149 test_and_clear_page_young(page);
1150 ClearPageReferenced(page);
1152 pte_unmap_unlock(pte - 1, ptl);
1157 static int clear_refs_test_walk(unsigned long start, unsigned long end,
1158 struct mm_walk *walk)
1160 struct clear_refs_private *cp = walk->private;
1161 struct vm_area_struct *vma = walk->vma;
1163 if (vma->vm_flags & VM_PFNMAP)
1167 * Writing 1 to /proc/pid/clear_refs affects all pages.
1168 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1169 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1170 * Writing 4 to /proc/pid/clear_refs affects all pages.
1172 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1174 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1179 static const struct mm_walk_ops clear_refs_walk_ops = {
1180 .pmd_entry = clear_refs_pte_range,
1181 .test_walk = clear_refs_test_walk,
1184 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1185 size_t count, loff_t *ppos)
1187 struct task_struct *task;
1188 char buffer[PROC_NUMBUF];
1189 struct mm_struct *mm;
1190 struct vm_area_struct *vma;
1191 enum clear_refs_types type;
1192 struct mmu_gather tlb;
1196 memset(buffer, 0, sizeof(buffer));
1197 if (count > sizeof(buffer) - 1)
1198 count = sizeof(buffer) - 1;
1199 if (copy_from_user(buffer, buf, count))
1201 rv = kstrtoint(strstrip(buffer), 10, &itype);
1204 type = (enum clear_refs_types)itype;
1205 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1208 task = get_proc_task(file_inode(file));
1211 mm = get_task_mm(task);
1213 struct mmu_notifier_range range;
1214 struct clear_refs_private cp = {
1218 if (mmap_write_lock_killable(mm)) {
1222 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1224 * Writing 5 to /proc/pid/clear_refs resets the peak
1225 * resident set size to this mm's current rss value.
1227 reset_mm_hiwater_rss(mm);
1231 tlb_gather_mmu(&tlb, mm, 0, -1);
1232 if (type == CLEAR_REFS_SOFT_DIRTY) {
1233 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1234 if (!(vma->vm_flags & VM_SOFTDIRTY))
1236 vma->vm_flags &= ~VM_SOFTDIRTY;
1237 vma_set_page_prot(vma);
1240 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1241 0, NULL, mm, 0, -1UL);
1242 mmu_notifier_invalidate_range_start(&range);
1244 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1246 if (type == CLEAR_REFS_SOFT_DIRTY)
1247 mmu_notifier_invalidate_range_end(&range);
1248 tlb_finish_mmu(&tlb, 0, -1);
1250 mmap_write_unlock(mm);
1254 put_task_struct(task);
1259 const struct file_operations proc_clear_refs_operations = {
1260 .write = clear_refs_write,
1261 .llseek = noop_llseek,
1268 struct pagemapread {
1269 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
1270 pagemap_entry_t *buffer;
1274 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
1275 #define PAGEMAP_WALK_MASK (PMD_MASK)
1277 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1278 #define PM_PFRAME_BITS 55
1279 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1280 #define PM_SOFT_DIRTY BIT_ULL(55)
1281 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1282 #define PM_FILE BIT_ULL(61)
1283 #define PM_SWAP BIT_ULL(62)
1284 #define PM_PRESENT BIT_ULL(63)
1286 #define PM_END_OF_BUFFER 1
1288 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1290 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1293 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1294 struct pagemapread *pm)
1296 pm->buffer[pm->pos++] = *pme;
1297 if (pm->pos >= pm->len)
1298 return PM_END_OF_BUFFER;
1302 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1303 __always_unused int depth, struct mm_walk *walk)
1305 struct pagemapread *pm = walk->private;
1306 unsigned long addr = start;
1309 while (addr < end) {
1310 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1311 pagemap_entry_t pme = make_pme(0, 0);
1312 /* End of address space hole, which we mark as non-present. */
1313 unsigned long hole_end;
1316 hole_end = min(end, vma->vm_start);
1320 for (; addr < hole_end; addr += PAGE_SIZE) {
1321 err = add_to_pagemap(addr, &pme, pm);
1329 /* Addresses in the VMA. */
1330 if (vma->vm_flags & VM_SOFTDIRTY)
1331 pme = make_pme(0, PM_SOFT_DIRTY);
1332 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1333 err = add_to_pagemap(addr, &pme, pm);
1342 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1343 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1345 u64 frame = 0, flags = 0;
1346 struct page *page = NULL;
1348 if (pte_present(pte)) {
1350 frame = pte_pfn(pte);
1351 flags |= PM_PRESENT;
1352 page = vm_normal_page(vma, addr, pte);
1353 if (pte_soft_dirty(pte))
1354 flags |= PM_SOFT_DIRTY;
1355 } else if (is_swap_pte(pte)) {
1357 if (pte_swp_soft_dirty(pte))
1358 flags |= PM_SOFT_DIRTY;
1359 entry = pte_to_swp_entry(pte);
1361 frame = swp_type(entry) |
1362 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1364 if (is_migration_entry(entry))
1365 page = migration_entry_to_page(entry);
1367 if (is_device_private_entry(entry))
1368 page = device_private_entry_to_page(entry);
1371 if (page && !PageAnon(page))
1373 if (page && page_mapcount(page) == 1)
1374 flags |= PM_MMAP_EXCLUSIVE;
1375 if (vma->vm_flags & VM_SOFTDIRTY)
1376 flags |= PM_SOFT_DIRTY;
1378 return make_pme(frame, flags);
1381 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1382 struct mm_walk *walk)
1384 struct vm_area_struct *vma = walk->vma;
1385 struct pagemapread *pm = walk->private;
1387 pte_t *pte, *orig_pte;
1390 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1391 ptl = pmd_trans_huge_lock(pmdp, vma);
1393 u64 flags = 0, frame = 0;
1395 struct page *page = NULL;
1397 if (vma->vm_flags & VM_SOFTDIRTY)
1398 flags |= PM_SOFT_DIRTY;
1400 if (pmd_present(pmd)) {
1401 page = pmd_page(pmd);
1403 flags |= PM_PRESENT;
1404 if (pmd_soft_dirty(pmd))
1405 flags |= PM_SOFT_DIRTY;
1407 frame = pmd_pfn(pmd) +
1408 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1410 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1411 else if (is_swap_pmd(pmd)) {
1412 swp_entry_t entry = pmd_to_swp_entry(pmd);
1413 unsigned long offset;
1416 offset = swp_offset(entry) +
1417 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1418 frame = swp_type(entry) |
1419 (offset << MAX_SWAPFILES_SHIFT);
1422 if (pmd_swp_soft_dirty(pmd))
1423 flags |= PM_SOFT_DIRTY;
1424 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1425 page = migration_entry_to_page(entry);
1429 if (page && page_mapcount(page) == 1)
1430 flags |= PM_MMAP_EXCLUSIVE;
1432 for (; addr != end; addr += PAGE_SIZE) {
1433 pagemap_entry_t pme = make_pme(frame, flags);
1435 err = add_to_pagemap(addr, &pme, pm);
1439 if (flags & PM_PRESENT)
1441 else if (flags & PM_SWAP)
1442 frame += (1 << MAX_SWAPFILES_SHIFT);
1449 if (pmd_trans_unstable(pmdp))
1451 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1454 * We can assume that @vma always points to a valid one and @end never
1455 * goes beyond vma->vm_end.
1457 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1458 for (; addr < end; pte++, addr += PAGE_SIZE) {
1459 pagemap_entry_t pme;
1461 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1462 err = add_to_pagemap(addr, &pme, pm);
1466 pte_unmap_unlock(orig_pte, ptl);
1473 #ifdef CONFIG_HUGETLB_PAGE
1474 /* This function walks within one hugetlb entry in the single call */
1475 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1476 unsigned long addr, unsigned long end,
1477 struct mm_walk *walk)
1479 struct pagemapread *pm = walk->private;
1480 struct vm_area_struct *vma = walk->vma;
1481 u64 flags = 0, frame = 0;
1485 if (vma->vm_flags & VM_SOFTDIRTY)
1486 flags |= PM_SOFT_DIRTY;
1488 pte = huge_ptep_get(ptep);
1489 if (pte_present(pte)) {
1490 struct page *page = pte_page(pte);
1492 if (!PageAnon(page))
1495 if (page_mapcount(page) == 1)
1496 flags |= PM_MMAP_EXCLUSIVE;
1498 flags |= PM_PRESENT;
1500 frame = pte_pfn(pte) +
1501 ((addr & ~hmask) >> PAGE_SHIFT);
1504 for (; addr != end; addr += PAGE_SIZE) {
1505 pagemap_entry_t pme = make_pme(frame, flags);
1507 err = add_to_pagemap(addr, &pme, pm);
1510 if (pm->show_pfn && (flags & PM_PRESENT))
1519 #define pagemap_hugetlb_range NULL
1520 #endif /* HUGETLB_PAGE */
1522 static const struct mm_walk_ops pagemap_ops = {
1523 .pmd_entry = pagemap_pmd_range,
1524 .pte_hole = pagemap_pte_hole,
1525 .hugetlb_entry = pagemap_hugetlb_range,
1529 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1531 * For each page in the address space, this file contains one 64-bit entry
1532 * consisting of the following:
1534 * Bits 0-54 page frame number (PFN) if present
1535 * Bits 0-4 swap type if swapped
1536 * Bits 5-54 swap offset if swapped
1537 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
1538 * Bit 56 page exclusively mapped
1540 * Bit 61 page is file-page or shared-anon
1541 * Bit 62 page swapped
1542 * Bit 63 page present
1544 * If the page is not present but in swap, then the PFN contains an
1545 * encoding of the swap file number and the page's offset into the
1546 * swap. Unmapped pages return a null PFN. This allows determining
1547 * precisely which pages are mapped (or in swap) and comparing mapped
1548 * pages between processes.
1550 * Efficient users of this interface will use /proc/pid/maps to
1551 * determine which areas of memory are actually mapped and llseek to
1552 * skip over unmapped regions.
1554 static ssize_t pagemap_read(struct file *file, char __user *buf,
1555 size_t count, loff_t *ppos)
1557 struct mm_struct *mm = file->private_data;
1558 struct pagemapread pm;
1560 unsigned long svpfn;
1561 unsigned long start_vaddr;
1562 unsigned long end_vaddr;
1563 int ret = 0, copied = 0;
1565 if (!mm || !mmget_not_zero(mm))
1569 /* file position must be aligned */
1570 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1577 /* do not disclose physical addresses: attack vector */
1578 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1580 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1581 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
1587 svpfn = src / PM_ENTRY_BYTES;
1588 end_vaddr = mm->task_size;
1590 /* watch out for wraparound */
1591 start_vaddr = end_vaddr;
1592 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT))
1593 start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
1595 /* Ensure the address is inside the task */
1596 if (start_vaddr > mm->task_size)
1597 start_vaddr = end_vaddr;
1600 * The odds are that this will stop walking way
1601 * before end_vaddr, because the length of the
1602 * user buffer is tracked in "pm", and the walk
1603 * will stop when we hit the end of the buffer.
1606 while (count && (start_vaddr < end_vaddr)) {
1611 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1613 if (end < start_vaddr || end > end_vaddr)
1615 ret = mmap_read_lock_killable(mm);
1618 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
1619 mmap_read_unlock(mm);
1622 len = min(count, PM_ENTRY_BYTES * pm.pos);
1623 if (copy_to_user(buf, pm.buffer, len)) {
1632 if (!ret || ret == PM_END_OF_BUFFER)
1643 static int pagemap_open(struct inode *inode, struct file *file)
1645 struct mm_struct *mm;
1647 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1650 file->private_data = mm;
1654 static int pagemap_release(struct inode *inode, struct file *file)
1656 struct mm_struct *mm = file->private_data;
1663 const struct file_operations proc_pagemap_operations = {
1664 .llseek = mem_lseek, /* borrow this */
1665 .read = pagemap_read,
1666 .open = pagemap_open,
1667 .release = pagemap_release,
1669 #endif /* CONFIG_PROC_PAGE_MONITOR */
1674 unsigned long pages;
1676 unsigned long active;
1677 unsigned long writeback;
1678 unsigned long mapcount_max;
1679 unsigned long dirty;
1680 unsigned long swapcache;
1681 unsigned long node[MAX_NUMNODES];
1684 struct numa_maps_private {
1685 struct proc_maps_private proc_maps;
1686 struct numa_maps md;
1689 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1690 unsigned long nr_pages)
1692 int count = page_mapcount(page);
1694 md->pages += nr_pages;
1695 if (pte_dirty || PageDirty(page))
1696 md->dirty += nr_pages;
1698 if (PageSwapCache(page))
1699 md->swapcache += nr_pages;
1701 if (PageActive(page) || PageUnevictable(page))
1702 md->active += nr_pages;
1704 if (PageWriteback(page))
1705 md->writeback += nr_pages;
1708 md->anon += nr_pages;
1710 if (count > md->mapcount_max)
1711 md->mapcount_max = count;
1713 md->node[page_to_nid(page)] += nr_pages;
1716 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1722 if (!pte_present(pte))
1725 page = vm_normal_page(vma, addr, pte);
1729 if (PageReserved(page))
1732 nid = page_to_nid(page);
1733 if (!node_isset(nid, node_states[N_MEMORY]))
1739 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1740 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1741 struct vm_area_struct *vma,
1747 if (!pmd_present(pmd))
1750 page = vm_normal_page_pmd(vma, addr, pmd);
1754 if (PageReserved(page))
1757 nid = page_to_nid(page);
1758 if (!node_isset(nid, node_states[N_MEMORY]))
1765 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1766 unsigned long end, struct mm_walk *walk)
1768 struct numa_maps *md = walk->private;
1769 struct vm_area_struct *vma = walk->vma;
1774 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1775 ptl = pmd_trans_huge_lock(pmd, vma);
1779 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1781 gather_stats(page, md, pmd_dirty(*pmd),
1782 HPAGE_PMD_SIZE/PAGE_SIZE);
1787 if (pmd_trans_unstable(pmd))
1790 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1792 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1795 gather_stats(page, md, pte_dirty(*pte), 1);
1797 } while (pte++, addr += PAGE_SIZE, addr != end);
1798 pte_unmap_unlock(orig_pte, ptl);
1802 #ifdef CONFIG_HUGETLB_PAGE
1803 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1804 unsigned long addr, unsigned long end, struct mm_walk *walk)
1806 pte_t huge_pte = huge_ptep_get(pte);
1807 struct numa_maps *md;
1810 if (!pte_present(huge_pte))
1813 page = pte_page(huge_pte);
1818 gather_stats(page, md, pte_dirty(huge_pte), 1);
1823 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1824 unsigned long addr, unsigned long end, struct mm_walk *walk)
1830 static const struct mm_walk_ops show_numa_ops = {
1831 .hugetlb_entry = gather_hugetlb_stats,
1832 .pmd_entry = gather_pte_stats,
1836 * Display pages allocated per node and memory policy via /proc.
1838 static int show_numa_map(struct seq_file *m, void *v)
1840 struct numa_maps_private *numa_priv = m->private;
1841 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1842 struct vm_area_struct *vma = v;
1843 struct numa_maps *md = &numa_priv->md;
1844 struct file *file = vma->vm_file;
1845 struct mm_struct *mm = vma->vm_mm;
1846 struct mempolicy *pol;
1853 /* Ensure we start with an empty set of numa_maps statistics. */
1854 memset(md, 0, sizeof(*md));
1856 pol = __get_vma_policy(vma, vma->vm_start);
1858 mpol_to_str(buffer, sizeof(buffer), pol);
1861 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1864 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1867 seq_puts(m, " file=");
1868 seq_file_path(m, file, "\n\t= ");
1869 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1870 seq_puts(m, " heap");
1871 } else if (is_stack(vma)) {
1872 seq_puts(m, " stack");
1875 if (is_vm_hugetlb_page(vma))
1876 seq_puts(m, " huge");
1878 /* mmap_lock is held by m_start */
1879 walk_page_vma(vma, &show_numa_ops, md);
1885 seq_printf(m, " anon=%lu", md->anon);
1888 seq_printf(m, " dirty=%lu", md->dirty);
1890 if (md->pages != md->anon && md->pages != md->dirty)
1891 seq_printf(m, " mapped=%lu", md->pages);
1893 if (md->mapcount_max > 1)
1894 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1897 seq_printf(m, " swapcache=%lu", md->swapcache);
1899 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1900 seq_printf(m, " active=%lu", md->active);
1903 seq_printf(m, " writeback=%lu", md->writeback);
1905 for_each_node_state(nid, N_MEMORY)
1907 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1909 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1915 static const struct seq_operations proc_pid_numa_maps_op = {
1919 .show = show_numa_map,
1922 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1924 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1925 sizeof(struct numa_maps_private));
1928 const struct file_operations proc_pid_numa_maps_operations = {
1929 .open = pid_numa_maps_open,
1931 .llseek = seq_lseek,
1932 .release = proc_map_release,
1935 #endif /* CONFIG_NUMA */