1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/pagewalk.h>
3 #include <linux/vmacache.h>
4 #include <linux/mm_inline.h>
5 #include <linux/hugetlb.h>
6 #include <linux/huge_mm.h>
7 #include <linux/mount.h>
8 #include <linux/seq_file.h>
9 #include <linux/highmem.h>
10 #include <linux/ptrace.h>
11 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/mempolicy.h>
14 #include <linux/rmap.h>
15 #include <linux/swap.h>
16 #include <linux/sched/mm.h>
17 #include <linux/swapops.h>
18 #include <linux/mmu_notifier.h>
19 #include <linux/page_idle.h>
20 #include <linux/shmem_fs.h>
21 #include <linux/uaccess.h>
22 #include <linux/pkeys.h>
26 #include <asm/tlbflush.h>
29 #define SEQ_PUT_DEC(str, val) \
30 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
31 void task_mem(struct seq_file *m, struct mm_struct *mm)
33 unsigned long text, lib, swap, anon, file, shmem;
34 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
36 anon = get_mm_counter(mm, MM_ANONPAGES);
37 file = get_mm_counter(mm, MM_FILEPAGES);
38 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
41 * Note: to minimize their overhead, mm maintains hiwater_vm and
42 * hiwater_rss only when about to *lower* total_vm or rss. Any
43 * collector of these hiwater stats must therefore get total_vm
44 * and rss too, which will usually be the higher. Barriers? not
45 * worth the effort, such snapshots can always be inconsistent.
47 hiwater_vm = total_vm = mm->total_vm;
48 if (hiwater_vm < mm->hiwater_vm)
49 hiwater_vm = mm->hiwater_vm;
50 hiwater_rss = total_rss = anon + file + shmem;
51 if (hiwater_rss < mm->hiwater_rss)
52 hiwater_rss = mm->hiwater_rss;
54 /* split executable areas between text and lib */
55 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK);
56 text = min(text, mm->exec_vm << PAGE_SHIFT);
57 lib = (mm->exec_vm << PAGE_SHIFT) - text;
59 swap = get_mm_counter(mm, MM_SWAPENTS);
60 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
61 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
62 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
63 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm));
64 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss);
65 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss);
66 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon);
67 SEQ_PUT_DEC(" kB\nRssFile:\t", file);
68 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem);
69 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm);
70 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm);
71 seq_put_decimal_ull_width(m,
72 " kB\nVmExe:\t", text >> 10, 8);
73 seq_put_decimal_ull_width(m,
74 " kB\nVmLib:\t", lib >> 10, 8);
75 seq_put_decimal_ull_width(m,
76 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);
77 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap);
79 hugetlb_report_usage(m, mm);
83 unsigned long task_vsize(struct mm_struct *mm)
85 return PAGE_SIZE * mm->total_vm;
88 unsigned long task_statm(struct mm_struct *mm,
89 unsigned long *shared, unsigned long *text,
90 unsigned long *data, unsigned long *resident)
92 *shared = get_mm_counter(mm, MM_FILEPAGES) +
93 get_mm_counter(mm, MM_SHMEMPAGES);
94 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
96 *data = mm->data_vm + mm->stack_vm;
97 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
103 * Save get_task_policy() for show_numa_map().
105 static void hold_task_mempolicy(struct proc_maps_private *priv)
107 struct task_struct *task = priv->task;
110 priv->task_mempolicy = get_task_policy(task);
111 mpol_get(priv->task_mempolicy);
114 static void release_task_mempolicy(struct proc_maps_private *priv)
116 mpol_put(priv->task_mempolicy);
119 static void hold_task_mempolicy(struct proc_maps_private *priv)
122 static void release_task_mempolicy(struct proc_maps_private *priv)
127 static void *m_start(struct seq_file *m, loff_t *ppos)
129 struct proc_maps_private *priv = m->private;
130 unsigned long last_addr = *ppos;
131 struct mm_struct *mm;
132 struct vm_area_struct *vma;
134 /* See m_next(). Zero at the start or after lseek. */
135 if (last_addr == -1UL)
138 priv->task = get_proc_task(priv->inode);
140 return ERR_PTR(-ESRCH);
143 if (!mm || !mmget_not_zero(mm)) {
144 put_task_struct(priv->task);
149 if (mmap_read_lock_killable(mm)) {
151 put_task_struct(priv->task);
153 return ERR_PTR(-EINTR);
156 hold_task_mempolicy(priv);
157 priv->tail_vma = get_gate_vma(mm);
159 vma = find_vma(mm, last_addr);
163 return priv->tail_vma;
166 static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
168 struct proc_maps_private *priv = m->private;
169 struct vm_area_struct *next, *vma = v;
171 if (vma == priv->tail_vma)
173 else if (vma->vm_next)
176 next = priv->tail_vma;
178 *ppos = next ? next->vm_start : -1UL;
183 static void m_stop(struct seq_file *m, void *v)
185 struct proc_maps_private *priv = m->private;
186 struct mm_struct *mm = priv->mm;
191 release_task_mempolicy(priv);
192 mmap_read_unlock(mm);
194 put_task_struct(priv->task);
198 static int proc_maps_open(struct inode *inode, struct file *file,
199 const struct seq_operations *ops, int psize)
201 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
207 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
208 if (IS_ERR(priv->mm)) {
209 int err = PTR_ERR(priv->mm);
211 seq_release_private(inode, file);
218 static int proc_map_release(struct inode *inode, struct file *file)
220 struct seq_file *seq = file->private_data;
221 struct proc_maps_private *priv = seq->private;
226 return seq_release_private(inode, file);
229 static int do_maps_open(struct inode *inode, struct file *file,
230 const struct seq_operations *ops)
232 return proc_maps_open(inode, file, ops,
233 sizeof(struct proc_maps_private));
237 * Indicate if the VMA is a stack for the given task; for
238 * /proc/PID/maps that is the stack of the main task.
240 static int is_stack(struct vm_area_struct *vma)
243 * We make no effort to guess what a given thread considers to be
244 * its "stack". It's not even well-defined for programs written
247 return vma->vm_start <= vma->vm_mm->start_stack &&
248 vma->vm_end >= vma->vm_mm->start_stack;
251 static void show_vma_header_prefix(struct seq_file *m,
252 unsigned long start, unsigned long end,
253 vm_flags_t flags, unsigned long long pgoff,
254 dev_t dev, unsigned long ino)
256 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
257 seq_put_hex_ll(m, NULL, start, 8);
258 seq_put_hex_ll(m, "-", end, 8);
260 seq_putc(m, flags & VM_READ ? 'r' : '-');
261 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
262 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
263 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
264 seq_put_hex_ll(m, " ", pgoff, 8);
265 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
266 seq_put_hex_ll(m, ":", MINOR(dev), 2);
267 seq_put_decimal_ull(m, " ", ino);
272 show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
274 struct mm_struct *mm = vma->vm_mm;
275 struct file *file = vma->vm_file;
276 vm_flags_t flags = vma->vm_flags;
277 unsigned long ino = 0;
278 unsigned long long pgoff = 0;
279 unsigned long start, end;
281 const char *name = NULL;
284 struct inode *inode = file_inode(vma->vm_file);
285 dev = inode->i_sb->s_dev;
287 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
290 start = vma->vm_start;
292 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
295 * Print the dentry name for named mappings, and a
296 * special [heap] marker for the heap:
300 seq_file_path(m, file, "\n");
304 if (vma->vm_ops && vma->vm_ops->name) {
305 name = vma->vm_ops->name(vma);
310 name = arch_vma_name(vma);
312 struct anon_vma_name *anon_name;
319 if (vma->vm_start <= mm->brk &&
320 vma->vm_end >= mm->start_brk) {
330 anon_name = anon_vma_name(vma);
333 seq_printf(m, "[anon:%s]", anon_name->name);
345 static int show_map(struct seq_file *m, void *v)
351 static const struct seq_operations proc_pid_maps_op = {
358 static int pid_maps_open(struct inode *inode, struct file *file)
360 return do_maps_open(inode, file, &proc_pid_maps_op);
363 const struct file_operations proc_pid_maps_operations = {
364 .open = pid_maps_open,
367 .release = proc_map_release,
371 * Proportional Set Size(PSS): my share of RSS.
373 * PSS of a process is the count of pages it has in memory, where each
374 * page is divided by the number of processes sharing it. So if a
375 * process has 1000 pages all to itself, and 1000 shared with one other
376 * process, its PSS will be 1500.
378 * To keep (accumulated) division errors low, we adopt a 64bit
379 * fixed-point pss counter to minimize division errors. So (pss >>
380 * PSS_SHIFT) would be the real byte count.
382 * A shift of 12 before division means (assuming 4K page size):
383 * - 1M 3-user-pages add up to 8KB errors;
384 * - supports mapcount up to 2^24, or 16M;
385 * - supports PSS up to 2^52 bytes, or 4PB.
389 #ifdef CONFIG_PROC_PAGE_MONITOR
390 struct mem_size_stats {
391 unsigned long resident;
392 unsigned long shared_clean;
393 unsigned long shared_dirty;
394 unsigned long private_clean;
395 unsigned long private_dirty;
396 unsigned long referenced;
397 unsigned long anonymous;
398 unsigned long lazyfree;
399 unsigned long anonymous_thp;
400 unsigned long shmem_thp;
401 unsigned long file_thp;
403 unsigned long shared_hugetlb;
404 unsigned long private_hugetlb;
414 static void smaps_page_accumulate(struct mem_size_stats *mss,
415 struct page *page, unsigned long size, unsigned long pss,
416 bool dirty, bool locked, bool private)
421 mss->pss_anon += pss;
422 else if (PageSwapBacked(page))
423 mss->pss_shmem += pss;
425 mss->pss_file += pss;
428 mss->pss_locked += pss;
430 if (dirty || PageDirty(page)) {
431 mss->pss_dirty += pss;
433 mss->private_dirty += size;
435 mss->shared_dirty += size;
438 mss->private_clean += size;
440 mss->shared_clean += size;
444 static void smaps_account(struct mem_size_stats *mss, struct page *page,
445 bool compound, bool young, bool dirty, bool locked,
448 int i, nr = compound ? compound_nr(page) : 1;
449 unsigned long size = nr * PAGE_SIZE;
452 * First accumulate quantities that depend only on |size| and the type
453 * of the compound page.
455 if (PageAnon(page)) {
456 mss->anonymous += size;
457 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
458 mss->lazyfree += size;
461 mss->resident += size;
462 /* Accumulate the size in pages that have been accessed. */
463 if (young || page_is_young(page) || PageReferenced(page))
464 mss->referenced += size;
467 * Then accumulate quantities that may depend on sharing, or that may
468 * differ page-by-page.
470 * page_count(page) == 1 guarantees the page is mapped exactly once.
471 * If any subpage of the compound page mapped with PTE it would elevate
474 * The page_mapcount() is called to get a snapshot of the mapcount.
475 * Without holding the page lock this snapshot can be slightly wrong as
476 * we cannot always read the mapcount atomically. It is not safe to
477 * call page_mapcount() even with PTL held if the page is not mapped,
478 * especially for migration entries. Treat regular migration entries
481 if ((page_count(page) == 1) || migration) {
482 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
486 for (i = 0; i < nr; i++, page++) {
487 int mapcount = page_mapcount(page);
488 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
491 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
497 static int smaps_pte_hole(unsigned long addr, unsigned long end,
498 __always_unused int depth, struct mm_walk *walk)
500 struct mem_size_stats *mss = walk->private;
501 struct vm_area_struct *vma = walk->vma;
503 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping,
504 linear_page_index(vma, addr),
505 linear_page_index(vma, end));
510 #define smaps_pte_hole NULL
511 #endif /* CONFIG_SHMEM */
513 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
516 if (walk->ops->pte_hole) {
517 /* depth is not used */
518 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk);
523 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
524 struct mm_walk *walk)
526 struct mem_size_stats *mss = walk->private;
527 struct vm_area_struct *vma = walk->vma;
528 bool locked = !!(vma->vm_flags & VM_LOCKED);
529 struct page *page = NULL;
530 bool migration = false, young = false, dirty = false;
532 if (pte_present(*pte)) {
533 page = vm_normal_page(vma, addr, *pte);
534 young = pte_young(*pte);
535 dirty = pte_dirty(*pte);
536 } else if (is_swap_pte(*pte)) {
537 swp_entry_t swpent = pte_to_swp_entry(*pte);
539 if (!non_swap_entry(swpent)) {
542 mss->swap += PAGE_SIZE;
543 mapcount = swp_swapcount(swpent);
545 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
547 do_div(pss_delta, mapcount);
548 mss->swap_pss += pss_delta;
550 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
552 } else if (is_pfn_swap_entry(swpent)) {
553 if (is_migration_entry(swpent))
555 page = pfn_swap_entry_to_page(swpent);
558 smaps_pte_hole_lookup(addr, walk);
565 smaps_account(mss, page, false, young, dirty, locked, migration);
568 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
569 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
570 struct mm_walk *walk)
572 struct mem_size_stats *mss = walk->private;
573 struct vm_area_struct *vma = walk->vma;
574 bool locked = !!(vma->vm_flags & VM_LOCKED);
575 struct page *page = NULL;
576 bool migration = false;
578 if (pmd_present(*pmd)) {
579 /* FOLL_DUMP will return -EFAULT on huge zero page */
580 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
581 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
582 swp_entry_t entry = pmd_to_swp_entry(*pmd);
584 if (is_migration_entry(entry)) {
586 page = pfn_swap_entry_to_page(entry);
589 if (IS_ERR_OR_NULL(page))
592 mss->anonymous_thp += HPAGE_PMD_SIZE;
593 else if (PageSwapBacked(page))
594 mss->shmem_thp += HPAGE_PMD_SIZE;
595 else if (is_zone_device_page(page))
598 mss->file_thp += HPAGE_PMD_SIZE;
600 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
604 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
605 struct mm_walk *walk)
610 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
611 struct mm_walk *walk)
613 struct vm_area_struct *vma = walk->vma;
617 ptl = pmd_trans_huge_lock(pmd, vma);
619 smaps_pmd_entry(pmd, addr, walk);
624 if (pmd_trans_unstable(pmd))
627 * The mmap_lock held all the way back in m_start() is what
628 * keeps khugepaged out of here and from collapsing things
631 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
632 for (; addr != end; pte++, addr += PAGE_SIZE)
633 smaps_pte_entry(pte, addr, walk);
634 pte_unmap_unlock(pte - 1, ptl);
640 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
643 * Don't forget to update Documentation/ on changes.
645 static const char mnemonics[BITS_PER_LONG][2] = {
647 * In case if we meet a flag we don't know about.
649 [0 ... (BITS_PER_LONG-1)] = "??",
651 [ilog2(VM_READ)] = "rd",
652 [ilog2(VM_WRITE)] = "wr",
653 [ilog2(VM_EXEC)] = "ex",
654 [ilog2(VM_SHARED)] = "sh",
655 [ilog2(VM_MAYREAD)] = "mr",
656 [ilog2(VM_MAYWRITE)] = "mw",
657 [ilog2(VM_MAYEXEC)] = "me",
658 [ilog2(VM_MAYSHARE)] = "ms",
659 [ilog2(VM_GROWSDOWN)] = "gd",
660 [ilog2(VM_PFNMAP)] = "pf",
661 [ilog2(VM_LOCKED)] = "lo",
662 [ilog2(VM_IO)] = "io",
663 [ilog2(VM_SEQ_READ)] = "sr",
664 [ilog2(VM_RAND_READ)] = "rr",
665 [ilog2(VM_DONTCOPY)] = "dc",
666 [ilog2(VM_DONTEXPAND)] = "de",
667 [ilog2(VM_ACCOUNT)] = "ac",
668 [ilog2(VM_NORESERVE)] = "nr",
669 [ilog2(VM_HUGETLB)] = "ht",
670 [ilog2(VM_SYNC)] = "sf",
671 [ilog2(VM_ARCH_1)] = "ar",
672 [ilog2(VM_WIPEONFORK)] = "wf",
673 [ilog2(VM_DONTDUMP)] = "dd",
674 #ifdef CONFIG_ARM64_BTI
675 [ilog2(VM_ARM64_BTI)] = "bt",
677 #ifdef CONFIG_MEM_SOFT_DIRTY
678 [ilog2(VM_SOFTDIRTY)] = "sd",
680 [ilog2(VM_MIXEDMAP)] = "mm",
681 [ilog2(VM_HUGEPAGE)] = "hg",
682 [ilog2(VM_NOHUGEPAGE)] = "nh",
683 [ilog2(VM_MERGEABLE)] = "mg",
684 [ilog2(VM_UFFD_MISSING)]= "um",
685 [ilog2(VM_UFFD_WP)] = "uw",
686 #ifdef CONFIG_ARM64_MTE
687 [ilog2(VM_MTE)] = "mt",
688 [ilog2(VM_MTE_ALLOWED)] = "",
690 #ifdef CONFIG_ARCH_HAS_PKEYS
691 /* These come out via ProtectionKey: */
692 [ilog2(VM_PKEY_BIT0)] = "",
693 [ilog2(VM_PKEY_BIT1)] = "",
694 [ilog2(VM_PKEY_BIT2)] = "",
695 [ilog2(VM_PKEY_BIT3)] = "",
697 [ilog2(VM_PKEY_BIT4)] = "",
699 #endif /* CONFIG_ARCH_HAS_PKEYS */
700 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
701 [ilog2(VM_UFFD_MINOR)] = "ui",
702 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
706 seq_puts(m, "VmFlags: ");
707 for (i = 0; i < BITS_PER_LONG; i++) {
708 if (!mnemonics[i][0])
710 if (vma->vm_flags & (1UL << i)) {
711 seq_putc(m, mnemonics[i][0]);
712 seq_putc(m, mnemonics[i][1]);
719 #ifdef CONFIG_HUGETLB_PAGE
720 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
721 unsigned long addr, unsigned long end,
722 struct mm_walk *walk)
724 struct mem_size_stats *mss = walk->private;
725 struct vm_area_struct *vma = walk->vma;
726 struct page *page = NULL;
728 if (pte_present(*pte)) {
729 page = vm_normal_page(vma, addr, *pte);
730 } else if (is_swap_pte(*pte)) {
731 swp_entry_t swpent = pte_to_swp_entry(*pte);
733 if (is_pfn_swap_entry(swpent))
734 page = pfn_swap_entry_to_page(swpent);
737 int mapcount = page_mapcount(page);
740 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
742 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
747 #define smaps_hugetlb_range NULL
748 #endif /* HUGETLB_PAGE */
750 static const struct mm_walk_ops smaps_walk_ops = {
751 .pmd_entry = smaps_pte_range,
752 .hugetlb_entry = smaps_hugetlb_range,
755 static const struct mm_walk_ops smaps_shmem_walk_ops = {
756 .pmd_entry = smaps_pte_range,
757 .hugetlb_entry = smaps_hugetlb_range,
758 .pte_hole = smaps_pte_hole,
762 * Gather mem stats from @vma with the indicated beginning
763 * address @start, and keep them in @mss.
765 * Use vm_start of @vma as the beginning address if @start is 0.
767 static void smap_gather_stats(struct vm_area_struct *vma,
768 struct mem_size_stats *mss, unsigned long start)
770 const struct mm_walk_ops *ops = &smaps_walk_ops;
773 if (start >= vma->vm_end)
777 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
779 * For shared or readonly shmem mappings we know that all
780 * swapped out pages belong to the shmem object, and we can
781 * obtain the swap value much more efficiently. For private
782 * writable mappings, we might have COW pages that are
783 * not affected by the parent swapped out pages of the shmem
784 * object, so we have to distinguish them during the page walk.
785 * Unless we know that the shmem object (or the part mapped by
786 * our VMA) has no swapped out pages at all.
788 unsigned long shmem_swapped = shmem_swap_usage(vma);
790 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
791 !(vma->vm_flags & VM_WRITE))) {
792 mss->swap += shmem_swapped;
794 ops = &smaps_shmem_walk_ops;
798 /* mmap_lock is held in m_start */
800 walk_page_vma(vma, ops, mss);
802 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
805 #define SEQ_PUT_DEC(str, val) \
806 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
808 /* Show the contents common for smaps and smaps_rollup */
809 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
812 SEQ_PUT_DEC("Rss: ", mss->resident);
813 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
814 SEQ_PUT_DEC(" kB\nPss_Dirty: ", mss->pss_dirty >> PSS_SHIFT);
817 * These are meaningful only for smaps_rollup, otherwise two of
818 * them are zero, and the other one is the same as Pss.
820 SEQ_PUT_DEC(" kB\nPss_Anon: ",
821 mss->pss_anon >> PSS_SHIFT);
822 SEQ_PUT_DEC(" kB\nPss_File: ",
823 mss->pss_file >> PSS_SHIFT);
824 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
825 mss->pss_shmem >> PSS_SHIFT);
827 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
828 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
829 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
830 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
831 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
832 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
833 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
834 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
835 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
836 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
837 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
838 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
839 mss->private_hugetlb >> 10, 7);
840 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
841 SEQ_PUT_DEC(" kB\nSwapPss: ",
842 mss->swap_pss >> PSS_SHIFT);
843 SEQ_PUT_DEC(" kB\nLocked: ",
844 mss->pss_locked >> PSS_SHIFT);
845 seq_puts(m, " kB\n");
848 static int show_smap(struct seq_file *m, void *v)
850 struct vm_area_struct *vma = v;
851 struct mem_size_stats mss;
853 memset(&mss, 0, sizeof(mss));
855 smap_gather_stats(vma, &mss, 0);
857 show_map_vma(m, vma);
859 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
860 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
861 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
862 seq_puts(m, " kB\n");
864 __show_smap(m, &mss, false);
866 seq_printf(m, "THPeligible: %d\n",
867 hugepage_vma_check(vma, vma->vm_flags, true, false));
869 if (arch_pkeys_enabled())
870 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
871 show_smap_vma_flags(m, vma);
876 static int show_smaps_rollup(struct seq_file *m, void *v)
878 struct proc_maps_private *priv = m->private;
879 struct mem_size_stats mss;
880 struct mm_struct *mm;
881 struct vm_area_struct *vma;
882 unsigned long last_vma_end = 0;
885 priv->task = get_proc_task(priv->inode);
890 if (!mm || !mmget_not_zero(mm)) {
895 memset(&mss, 0, sizeof(mss));
897 ret = mmap_read_lock_killable(mm);
901 hold_task_mempolicy(priv);
903 for (vma = priv->mm->mmap; vma;) {
904 smap_gather_stats(vma, &mss, 0);
905 last_vma_end = vma->vm_end;
908 * Release mmap_lock temporarily if someone wants to
909 * access it for write request.
911 if (mmap_lock_is_contended(mm)) {
912 mmap_read_unlock(mm);
913 ret = mmap_read_lock_killable(mm);
915 release_task_mempolicy(priv);
920 * After dropping the lock, there are four cases to
921 * consider. See the following example for explanation.
923 * +------+------+-----------+
924 * | VMA1 | VMA2 | VMA3 |
925 * +------+------+-----------+
929 * Suppose we drop the lock after reading VMA2 due to
930 * contention, then we get:
934 * 1) VMA2 is freed, but VMA3 exists:
936 * find_vma(mm, 16k - 1) will return VMA3.
937 * In this case, just continue from VMA3.
939 * 2) VMA2 still exists:
941 * find_vma(mm, 16k - 1) will return VMA2.
942 * Iterate the loop like the original one.
944 * 3) No more VMAs can be found:
946 * find_vma(mm, 16k - 1) will return NULL.
947 * No more things to do, just break.
949 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
951 * find_vma(mm, 16k - 1) will return VMA' whose range
952 * contains last_vma_end.
953 * Iterate VMA' from last_vma_end.
955 vma = find_vma(mm, last_vma_end - 1);
961 if (vma->vm_start >= last_vma_end)
965 if (vma->vm_end > last_vma_end)
966 smap_gather_stats(vma, &mss, last_vma_end);
972 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
973 last_vma_end, 0, 0, 0, 0);
975 seq_puts(m, "[rollup]\n");
977 __show_smap(m, &mss, true);
979 release_task_mempolicy(priv);
980 mmap_read_unlock(mm);
985 put_task_struct(priv->task);
992 static const struct seq_operations proc_pid_smaps_op = {
999 static int pid_smaps_open(struct inode *inode, struct file *file)
1001 return do_maps_open(inode, file, &proc_pid_smaps_op);
1004 static int smaps_rollup_open(struct inode *inode, struct file *file)
1007 struct proc_maps_private *priv;
1009 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
1013 ret = single_open(file, show_smaps_rollup, priv);
1017 priv->inode = inode;
1018 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
1019 if (IS_ERR(priv->mm)) {
1020 ret = PTR_ERR(priv->mm);
1022 single_release(inode, file);
1033 static int smaps_rollup_release(struct inode *inode, struct file *file)
1035 struct seq_file *seq = file->private_data;
1036 struct proc_maps_private *priv = seq->private;
1042 return single_release(inode, file);
1045 const struct file_operations proc_pid_smaps_operations = {
1046 .open = pid_smaps_open,
1048 .llseek = seq_lseek,
1049 .release = proc_map_release,
1052 const struct file_operations proc_pid_smaps_rollup_operations = {
1053 .open = smaps_rollup_open,
1055 .llseek = seq_lseek,
1056 .release = smaps_rollup_release,
1059 enum clear_refs_types {
1063 CLEAR_REFS_SOFT_DIRTY,
1064 CLEAR_REFS_MM_HIWATER_RSS,
1068 struct clear_refs_private {
1069 enum clear_refs_types type;
1072 #ifdef CONFIG_MEM_SOFT_DIRTY
1074 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1078 if (!pte_write(pte))
1080 if (!is_cow_mapping(vma->vm_flags))
1082 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
1084 page = vm_normal_page(vma, addr, pte);
1087 return page_maybe_dma_pinned(page);
1090 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1091 unsigned long addr, pte_t *pte)
1094 * The soft-dirty tracker uses #PF-s to catch writes
1095 * to pages, so write-protect the pte as well. See the
1096 * Documentation/admin-guide/mm/soft-dirty.rst for full description
1097 * of how soft-dirty works.
1101 if (pte_present(ptent)) {
1104 if (pte_is_pinned(vma, addr, ptent))
1106 old_pte = ptep_modify_prot_start(vma, addr, pte);
1107 ptent = pte_wrprotect(old_pte);
1108 ptent = pte_clear_soft_dirty(ptent);
1109 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
1110 } else if (is_swap_pte(ptent)) {
1111 ptent = pte_swp_clear_soft_dirty(ptent);
1112 set_pte_at(vma->vm_mm, addr, pte, ptent);
1116 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1117 unsigned long addr, pte_t *pte)
1122 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1123 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1124 unsigned long addr, pmd_t *pmdp)
1126 pmd_t old, pmd = *pmdp;
1128 if (pmd_present(pmd)) {
1129 /* See comment in change_huge_pmd() */
1130 old = pmdp_invalidate(vma, addr, pmdp);
1132 pmd = pmd_mkdirty(pmd);
1134 pmd = pmd_mkyoung(pmd);
1136 pmd = pmd_wrprotect(pmd);
1137 pmd = pmd_clear_soft_dirty(pmd);
1139 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1140 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1141 pmd = pmd_swp_clear_soft_dirty(pmd);
1142 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1146 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1147 unsigned long addr, pmd_t *pmdp)
1152 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
1153 unsigned long end, struct mm_walk *walk)
1155 struct clear_refs_private *cp = walk->private;
1156 struct vm_area_struct *vma = walk->vma;
1161 ptl = pmd_trans_huge_lock(pmd, vma);
1163 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1164 clear_soft_dirty_pmd(vma, addr, pmd);
1168 if (!pmd_present(*pmd))
1171 page = pmd_page(*pmd);
1173 /* Clear accessed and referenced bits. */
1174 pmdp_test_and_clear_young(vma, addr, pmd);
1175 test_and_clear_page_young(page);
1176 ClearPageReferenced(page);
1182 if (pmd_trans_unstable(pmd))
1185 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1186 for (; addr != end; pte++, addr += PAGE_SIZE) {
1189 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1190 clear_soft_dirty(vma, addr, pte);
1194 if (!pte_present(ptent))
1197 page = vm_normal_page(vma, addr, ptent);
1201 /* Clear accessed and referenced bits. */
1202 ptep_test_and_clear_young(vma, addr, pte);
1203 test_and_clear_page_young(page);
1204 ClearPageReferenced(page);
1206 pte_unmap_unlock(pte - 1, ptl);
1211 static int clear_refs_test_walk(unsigned long start, unsigned long end,
1212 struct mm_walk *walk)
1214 struct clear_refs_private *cp = walk->private;
1215 struct vm_area_struct *vma = walk->vma;
1217 if (vma->vm_flags & VM_PFNMAP)
1221 * Writing 1 to /proc/pid/clear_refs affects all pages.
1222 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1223 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1224 * Writing 4 to /proc/pid/clear_refs affects all pages.
1226 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1228 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1233 static const struct mm_walk_ops clear_refs_walk_ops = {
1234 .pmd_entry = clear_refs_pte_range,
1235 .test_walk = clear_refs_test_walk,
1238 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1239 size_t count, loff_t *ppos)
1241 struct task_struct *task;
1242 char buffer[PROC_NUMBUF];
1243 struct mm_struct *mm;
1244 struct vm_area_struct *vma;
1245 enum clear_refs_types type;
1249 memset(buffer, 0, sizeof(buffer));
1250 if (count > sizeof(buffer) - 1)
1251 count = sizeof(buffer) - 1;
1252 if (copy_from_user(buffer, buf, count))
1254 rv = kstrtoint(strstrip(buffer), 10, &itype);
1257 type = (enum clear_refs_types)itype;
1258 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1261 task = get_proc_task(file_inode(file));
1264 mm = get_task_mm(task);
1266 struct mmu_notifier_range range;
1267 struct clear_refs_private cp = {
1271 if (mmap_write_lock_killable(mm)) {
1275 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1277 * Writing 5 to /proc/pid/clear_refs resets the peak
1278 * resident set size to this mm's current rss value.
1280 reset_mm_hiwater_rss(mm);
1284 if (type == CLEAR_REFS_SOFT_DIRTY) {
1285 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1286 if (!(vma->vm_flags & VM_SOFTDIRTY))
1288 vma->vm_flags &= ~VM_SOFTDIRTY;
1289 vma_set_page_prot(vma);
1292 inc_tlb_flush_pending(mm);
1293 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1294 0, NULL, mm, 0, -1UL);
1295 mmu_notifier_invalidate_range_start(&range);
1297 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1299 if (type == CLEAR_REFS_SOFT_DIRTY) {
1300 mmu_notifier_invalidate_range_end(&range);
1302 dec_tlb_flush_pending(mm);
1305 mmap_write_unlock(mm);
1309 put_task_struct(task);
1314 const struct file_operations proc_clear_refs_operations = {
1315 .write = clear_refs_write,
1316 .llseek = noop_llseek,
1323 struct pagemapread {
1324 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
1325 pagemap_entry_t *buffer;
1329 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
1330 #define PAGEMAP_WALK_MASK (PMD_MASK)
1332 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1333 #define PM_PFRAME_BITS 55
1334 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1335 #define PM_SOFT_DIRTY BIT_ULL(55)
1336 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1337 #define PM_UFFD_WP BIT_ULL(57)
1338 #define PM_FILE BIT_ULL(61)
1339 #define PM_SWAP BIT_ULL(62)
1340 #define PM_PRESENT BIT_ULL(63)
1342 #define PM_END_OF_BUFFER 1
1344 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1346 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1349 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1350 struct pagemapread *pm)
1352 pm->buffer[pm->pos++] = *pme;
1353 if (pm->pos >= pm->len)
1354 return PM_END_OF_BUFFER;
1358 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1359 __always_unused int depth, struct mm_walk *walk)
1361 struct pagemapread *pm = walk->private;
1362 unsigned long addr = start;
1365 while (addr < end) {
1366 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1367 pagemap_entry_t pme = make_pme(0, 0);
1368 /* End of address space hole, which we mark as non-present. */
1369 unsigned long hole_end;
1372 hole_end = min(end, vma->vm_start);
1376 for (; addr < hole_end; addr += PAGE_SIZE) {
1377 err = add_to_pagemap(addr, &pme, pm);
1385 /* Addresses in the VMA. */
1386 if (vma->vm_flags & VM_SOFTDIRTY)
1387 pme = make_pme(0, PM_SOFT_DIRTY);
1388 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1389 err = add_to_pagemap(addr, &pme, pm);
1398 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1399 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1401 u64 frame = 0, flags = 0;
1402 struct page *page = NULL;
1403 bool migration = false;
1405 if (pte_present(pte)) {
1407 frame = pte_pfn(pte);
1408 flags |= PM_PRESENT;
1409 page = vm_normal_page(vma, addr, pte);
1410 if (pte_soft_dirty(pte))
1411 flags |= PM_SOFT_DIRTY;
1412 if (pte_uffd_wp(pte))
1413 flags |= PM_UFFD_WP;
1414 } else if (is_swap_pte(pte)) {
1416 if (pte_swp_soft_dirty(pte))
1417 flags |= PM_SOFT_DIRTY;
1418 if (pte_swp_uffd_wp(pte))
1419 flags |= PM_UFFD_WP;
1420 entry = pte_to_swp_entry(pte);
1422 frame = swp_type(entry) |
1423 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1425 migration = is_migration_entry(entry);
1426 if (is_pfn_swap_entry(entry))
1427 page = pfn_swap_entry_to_page(entry);
1428 if (pte_marker_entry_uffd_wp(entry))
1429 flags |= PM_UFFD_WP;
1432 if (page && !PageAnon(page))
1434 if (page && !migration && page_mapcount(page) == 1)
1435 flags |= PM_MMAP_EXCLUSIVE;
1436 if (vma->vm_flags & VM_SOFTDIRTY)
1437 flags |= PM_SOFT_DIRTY;
1439 return make_pme(frame, flags);
1442 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1443 struct mm_walk *walk)
1445 struct vm_area_struct *vma = walk->vma;
1446 struct pagemapread *pm = walk->private;
1448 pte_t *pte, *orig_pte;
1450 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1451 bool migration = false;
1453 ptl = pmd_trans_huge_lock(pmdp, vma);
1455 u64 flags = 0, frame = 0;
1457 struct page *page = NULL;
1459 if (vma->vm_flags & VM_SOFTDIRTY)
1460 flags |= PM_SOFT_DIRTY;
1462 if (pmd_present(pmd)) {
1463 page = pmd_page(pmd);
1465 flags |= PM_PRESENT;
1466 if (pmd_soft_dirty(pmd))
1467 flags |= PM_SOFT_DIRTY;
1468 if (pmd_uffd_wp(pmd))
1469 flags |= PM_UFFD_WP;
1471 frame = pmd_pfn(pmd) +
1472 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1474 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1475 else if (is_swap_pmd(pmd)) {
1476 swp_entry_t entry = pmd_to_swp_entry(pmd);
1477 unsigned long offset;
1480 offset = swp_offset(entry) +
1481 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1482 frame = swp_type(entry) |
1483 (offset << MAX_SWAPFILES_SHIFT);
1486 if (pmd_swp_soft_dirty(pmd))
1487 flags |= PM_SOFT_DIRTY;
1488 if (pmd_swp_uffd_wp(pmd))
1489 flags |= PM_UFFD_WP;
1490 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1491 migration = is_migration_entry(entry);
1492 page = pfn_swap_entry_to_page(entry);
1496 if (page && !migration && page_mapcount(page) == 1)
1497 flags |= PM_MMAP_EXCLUSIVE;
1499 for (; addr != end; addr += PAGE_SIZE) {
1500 pagemap_entry_t pme = make_pme(frame, flags);
1502 err = add_to_pagemap(addr, &pme, pm);
1506 if (flags & PM_PRESENT)
1508 else if (flags & PM_SWAP)
1509 frame += (1 << MAX_SWAPFILES_SHIFT);
1516 if (pmd_trans_unstable(pmdp))
1518 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1521 * We can assume that @vma always points to a valid one and @end never
1522 * goes beyond vma->vm_end.
1524 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1525 for (; addr < end; pte++, addr += PAGE_SIZE) {
1526 pagemap_entry_t pme;
1528 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1529 err = add_to_pagemap(addr, &pme, pm);
1533 pte_unmap_unlock(orig_pte, ptl);
1540 #ifdef CONFIG_HUGETLB_PAGE
1541 /* This function walks within one hugetlb entry in the single call */
1542 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1543 unsigned long addr, unsigned long end,
1544 struct mm_walk *walk)
1546 struct pagemapread *pm = walk->private;
1547 struct vm_area_struct *vma = walk->vma;
1548 u64 flags = 0, frame = 0;
1552 if (vma->vm_flags & VM_SOFTDIRTY)
1553 flags |= PM_SOFT_DIRTY;
1555 pte = huge_ptep_get(ptep);
1556 if (pte_present(pte)) {
1557 struct page *page = pte_page(pte);
1559 if (!PageAnon(page))
1562 if (page_mapcount(page) == 1)
1563 flags |= PM_MMAP_EXCLUSIVE;
1565 if (huge_pte_uffd_wp(pte))
1566 flags |= PM_UFFD_WP;
1568 flags |= PM_PRESENT;
1570 frame = pte_pfn(pte) +
1571 ((addr & ~hmask) >> PAGE_SHIFT);
1572 } else if (pte_swp_uffd_wp_any(pte)) {
1573 flags |= PM_UFFD_WP;
1576 for (; addr != end; addr += PAGE_SIZE) {
1577 pagemap_entry_t pme = make_pme(frame, flags);
1579 err = add_to_pagemap(addr, &pme, pm);
1582 if (pm->show_pfn && (flags & PM_PRESENT))
1591 #define pagemap_hugetlb_range NULL
1592 #endif /* HUGETLB_PAGE */
1594 static const struct mm_walk_ops pagemap_ops = {
1595 .pmd_entry = pagemap_pmd_range,
1596 .pte_hole = pagemap_pte_hole,
1597 .hugetlb_entry = pagemap_hugetlb_range,
1601 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1603 * For each page in the address space, this file contains one 64-bit entry
1604 * consisting of the following:
1606 * Bits 0-54 page frame number (PFN) if present
1607 * Bits 0-4 swap type if swapped
1608 * Bits 5-54 swap offset if swapped
1609 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
1610 * Bit 56 page exclusively mapped
1611 * Bit 57 pte is uffd-wp write-protected
1613 * Bit 61 page is file-page or shared-anon
1614 * Bit 62 page swapped
1615 * Bit 63 page present
1617 * If the page is not present but in swap, then the PFN contains an
1618 * encoding of the swap file number and the page's offset into the
1619 * swap. Unmapped pages return a null PFN. This allows determining
1620 * precisely which pages are mapped (or in swap) and comparing mapped
1621 * pages between processes.
1623 * Efficient users of this interface will use /proc/pid/maps to
1624 * determine which areas of memory are actually mapped and llseek to
1625 * skip over unmapped regions.
1627 static ssize_t pagemap_read(struct file *file, char __user *buf,
1628 size_t count, loff_t *ppos)
1630 struct mm_struct *mm = file->private_data;
1631 struct pagemapread pm;
1633 unsigned long svpfn;
1634 unsigned long start_vaddr;
1635 unsigned long end_vaddr;
1636 int ret = 0, copied = 0;
1638 if (!mm || !mmget_not_zero(mm))
1642 /* file position must be aligned */
1643 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1650 /* do not disclose physical addresses: attack vector */
1651 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1653 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1654 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
1660 svpfn = src / PM_ENTRY_BYTES;
1661 end_vaddr = mm->task_size;
1663 /* watch out for wraparound */
1664 start_vaddr = end_vaddr;
1665 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT))
1666 start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
1668 /* Ensure the address is inside the task */
1669 if (start_vaddr > mm->task_size)
1670 start_vaddr = end_vaddr;
1673 * The odds are that this will stop walking way
1674 * before end_vaddr, because the length of the
1675 * user buffer is tracked in "pm", and the walk
1676 * will stop when we hit the end of the buffer.
1679 while (count && (start_vaddr < end_vaddr)) {
1684 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1686 if (end < start_vaddr || end > end_vaddr)
1688 ret = mmap_read_lock_killable(mm);
1691 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
1692 mmap_read_unlock(mm);
1695 len = min(count, PM_ENTRY_BYTES * pm.pos);
1696 if (copy_to_user(buf, pm.buffer, len)) {
1705 if (!ret || ret == PM_END_OF_BUFFER)
1716 static int pagemap_open(struct inode *inode, struct file *file)
1718 struct mm_struct *mm;
1720 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1723 file->private_data = mm;
1727 static int pagemap_release(struct inode *inode, struct file *file)
1729 struct mm_struct *mm = file->private_data;
1736 const struct file_operations proc_pagemap_operations = {
1737 .llseek = mem_lseek, /* borrow this */
1738 .read = pagemap_read,
1739 .open = pagemap_open,
1740 .release = pagemap_release,
1742 #endif /* CONFIG_PROC_PAGE_MONITOR */
1747 unsigned long pages;
1749 unsigned long active;
1750 unsigned long writeback;
1751 unsigned long mapcount_max;
1752 unsigned long dirty;
1753 unsigned long swapcache;
1754 unsigned long node[MAX_NUMNODES];
1757 struct numa_maps_private {
1758 struct proc_maps_private proc_maps;
1759 struct numa_maps md;
1762 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1763 unsigned long nr_pages)
1765 int count = page_mapcount(page);
1767 md->pages += nr_pages;
1768 if (pte_dirty || PageDirty(page))
1769 md->dirty += nr_pages;
1771 if (PageSwapCache(page))
1772 md->swapcache += nr_pages;
1774 if (PageActive(page) || PageUnevictable(page))
1775 md->active += nr_pages;
1777 if (PageWriteback(page))
1778 md->writeback += nr_pages;
1781 md->anon += nr_pages;
1783 if (count > md->mapcount_max)
1784 md->mapcount_max = count;
1786 md->node[page_to_nid(page)] += nr_pages;
1789 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1795 if (!pte_present(pte))
1798 page = vm_normal_page(vma, addr, pte);
1799 if (!page || is_zone_device_page(page))
1802 if (PageReserved(page))
1805 nid = page_to_nid(page);
1806 if (!node_isset(nid, node_states[N_MEMORY]))
1812 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1813 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1814 struct vm_area_struct *vma,
1820 if (!pmd_present(pmd))
1823 page = vm_normal_page_pmd(vma, addr, pmd);
1827 if (PageReserved(page))
1830 nid = page_to_nid(page);
1831 if (!node_isset(nid, node_states[N_MEMORY]))
1838 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1839 unsigned long end, struct mm_walk *walk)
1841 struct numa_maps *md = walk->private;
1842 struct vm_area_struct *vma = walk->vma;
1847 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1848 ptl = pmd_trans_huge_lock(pmd, vma);
1852 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1854 gather_stats(page, md, pmd_dirty(*pmd),
1855 HPAGE_PMD_SIZE/PAGE_SIZE);
1860 if (pmd_trans_unstable(pmd))
1863 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1865 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1868 gather_stats(page, md, pte_dirty(*pte), 1);
1870 } while (pte++, addr += PAGE_SIZE, addr != end);
1871 pte_unmap_unlock(orig_pte, ptl);
1875 #ifdef CONFIG_HUGETLB_PAGE
1876 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1877 unsigned long addr, unsigned long end, struct mm_walk *walk)
1879 pte_t huge_pte = huge_ptep_get(pte);
1880 struct numa_maps *md;
1883 if (!pte_present(huge_pte))
1886 page = pte_page(huge_pte);
1889 gather_stats(page, md, pte_dirty(huge_pte), 1);
1894 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1895 unsigned long addr, unsigned long end, struct mm_walk *walk)
1901 static const struct mm_walk_ops show_numa_ops = {
1902 .hugetlb_entry = gather_hugetlb_stats,
1903 .pmd_entry = gather_pte_stats,
1907 * Display pages allocated per node and memory policy via /proc.
1909 static int show_numa_map(struct seq_file *m, void *v)
1911 struct numa_maps_private *numa_priv = m->private;
1912 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1913 struct vm_area_struct *vma = v;
1914 struct numa_maps *md = &numa_priv->md;
1915 struct file *file = vma->vm_file;
1916 struct mm_struct *mm = vma->vm_mm;
1917 struct mempolicy *pol;
1924 /* Ensure we start with an empty set of numa_maps statistics. */
1925 memset(md, 0, sizeof(*md));
1927 pol = __get_vma_policy(vma, vma->vm_start);
1929 mpol_to_str(buffer, sizeof(buffer), pol);
1932 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1935 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1938 seq_puts(m, " file=");
1939 seq_file_path(m, file, "\n\t= ");
1940 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1941 seq_puts(m, " heap");
1942 } else if (is_stack(vma)) {
1943 seq_puts(m, " stack");
1946 if (is_vm_hugetlb_page(vma))
1947 seq_puts(m, " huge");
1949 /* mmap_lock is held by m_start */
1950 walk_page_vma(vma, &show_numa_ops, md);
1956 seq_printf(m, " anon=%lu", md->anon);
1959 seq_printf(m, " dirty=%lu", md->dirty);
1961 if (md->pages != md->anon && md->pages != md->dirty)
1962 seq_printf(m, " mapped=%lu", md->pages);
1964 if (md->mapcount_max > 1)
1965 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1968 seq_printf(m, " swapcache=%lu", md->swapcache);
1970 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1971 seq_printf(m, " active=%lu", md->active);
1974 seq_printf(m, " writeback=%lu", md->writeback);
1976 for_each_node_state(nid, N_MEMORY)
1978 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1980 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1986 static const struct seq_operations proc_pid_numa_maps_op = {
1990 .show = show_numa_map,
1993 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1995 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1996 sizeof(struct numa_maps_private));
1999 const struct file_operations proc_pid_numa_maps_operations = {
2000 .open = pid_numa_maps_open,
2002 .llseek = seq_lseek,
2003 .release = proc_map_release,
2006 #endif /* CONFIG_NUMA */