1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/pagewalk.h>
3 #include <linux/mm_inline.h>
4 #include <linux/hugetlb.h>
5 #include <linux/huge_mm.h>
6 #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 struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv,
130 struct vm_area_struct *vma = vma_next(&priv->iter);
133 *ppos = vma->vm_start;
136 vma = get_gate_vma(priv->mm);
142 static void *m_start(struct seq_file *m, loff_t *ppos)
144 struct proc_maps_private *priv = m->private;
145 unsigned long last_addr = *ppos;
146 struct mm_struct *mm;
148 /* See m_next(). Zero at the start or after lseek. */
149 if (last_addr == -1UL)
152 priv->task = get_proc_task(priv->inode);
154 return ERR_PTR(-ESRCH);
157 if (!mm || !mmget_not_zero(mm)) {
158 put_task_struct(priv->task);
163 if (mmap_read_lock_killable(mm)) {
165 put_task_struct(priv->task);
167 return ERR_PTR(-EINTR);
170 vma_iter_init(&priv->iter, mm, last_addr);
171 hold_task_mempolicy(priv);
172 if (last_addr == -2UL)
173 return get_gate_vma(mm);
175 return proc_get_vma(priv, ppos);
178 static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
184 return proc_get_vma(m->private, ppos);
187 static void m_stop(struct seq_file *m, void *v)
189 struct proc_maps_private *priv = m->private;
190 struct mm_struct *mm = priv->mm;
195 release_task_mempolicy(priv);
196 mmap_read_unlock(mm);
198 put_task_struct(priv->task);
202 static int proc_maps_open(struct inode *inode, struct file *file,
203 const struct seq_operations *ops, int psize)
205 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
211 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
212 if (IS_ERR(priv->mm)) {
213 int err = PTR_ERR(priv->mm);
215 seq_release_private(inode, file);
222 static int proc_map_release(struct inode *inode, struct file *file)
224 struct seq_file *seq = file->private_data;
225 struct proc_maps_private *priv = seq->private;
230 return seq_release_private(inode, file);
233 static int do_maps_open(struct inode *inode, struct file *file,
234 const struct seq_operations *ops)
236 return proc_maps_open(inode, file, ops,
237 sizeof(struct proc_maps_private));
240 static void show_vma_header_prefix(struct seq_file *m,
241 unsigned long start, unsigned long end,
242 vm_flags_t flags, unsigned long long pgoff,
243 dev_t dev, unsigned long ino)
245 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
246 seq_put_hex_ll(m, NULL, start, 8);
247 seq_put_hex_ll(m, "-", end, 8);
249 seq_putc(m, flags & VM_READ ? 'r' : '-');
250 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
251 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
252 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
253 seq_put_hex_ll(m, " ", pgoff, 8);
254 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
255 seq_put_hex_ll(m, ":", MINOR(dev), 2);
256 seq_put_decimal_ull(m, " ", ino);
261 show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
263 struct anon_vma_name *anon_name = NULL;
264 struct mm_struct *mm = vma->vm_mm;
265 struct file *file = vma->vm_file;
266 vm_flags_t flags = vma->vm_flags;
267 unsigned long ino = 0;
268 unsigned long long pgoff = 0;
269 unsigned long start, end;
271 const char *name = NULL;
274 struct inode *inode = file_inode(vma->vm_file);
275 dev = inode->i_sb->s_dev;
277 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
280 start = vma->vm_start;
282 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
284 anon_name = anon_vma_name(vma);
287 * Print the dentry name for named mappings, and a
288 * special [heap] marker for the heap:
293 * If user named this anon shared memory via
294 * prctl(PR_SET_VMA ..., use the provided name.
297 seq_printf(m, "[anon_shmem:%s]", anon_name->name);
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_is_initial_heap(vma)) {
321 if (vma_is_initial_stack(vma)) {
328 seq_printf(m, "[anon:%s]", anon_name->name);
340 static int show_map(struct seq_file *m, void *v)
346 static const struct seq_operations proc_pid_maps_op = {
353 static int pid_maps_open(struct inode *inode, struct file *file)
355 return do_maps_open(inode, file, &proc_pid_maps_op);
358 const struct file_operations proc_pid_maps_operations = {
359 .open = pid_maps_open,
362 .release = proc_map_release,
366 * Proportional Set Size(PSS): my share of RSS.
368 * PSS of a process is the count of pages it has in memory, where each
369 * page is divided by the number of processes sharing it. So if a
370 * process has 1000 pages all to itself, and 1000 shared with one other
371 * process, its PSS will be 1500.
373 * To keep (accumulated) division errors low, we adopt a 64bit
374 * fixed-point pss counter to minimize division errors. So (pss >>
375 * PSS_SHIFT) would be the real byte count.
377 * A shift of 12 before division means (assuming 4K page size):
378 * - 1M 3-user-pages add up to 8KB errors;
379 * - supports mapcount up to 2^24, or 16M;
380 * - supports PSS up to 2^52 bytes, or 4PB.
384 #ifdef CONFIG_PROC_PAGE_MONITOR
385 struct mem_size_stats {
386 unsigned long resident;
387 unsigned long shared_clean;
388 unsigned long shared_dirty;
389 unsigned long private_clean;
390 unsigned long private_dirty;
391 unsigned long referenced;
392 unsigned long anonymous;
393 unsigned long lazyfree;
394 unsigned long anonymous_thp;
395 unsigned long shmem_thp;
396 unsigned long file_thp;
398 unsigned long shared_hugetlb;
399 unsigned long private_hugetlb;
410 static void smaps_page_accumulate(struct mem_size_stats *mss,
411 struct page *page, unsigned long size, unsigned long pss,
412 bool dirty, bool locked, bool private)
417 mss->pss_anon += pss;
418 else if (PageSwapBacked(page))
419 mss->pss_shmem += pss;
421 mss->pss_file += pss;
424 mss->pss_locked += pss;
426 if (dirty || PageDirty(page)) {
427 mss->pss_dirty += pss;
429 mss->private_dirty += size;
431 mss->shared_dirty += size;
434 mss->private_clean += size;
436 mss->shared_clean += size;
440 static void smaps_account(struct mem_size_stats *mss, struct page *page,
441 bool compound, bool young, bool dirty, bool locked,
444 int i, nr = compound ? compound_nr(page) : 1;
445 unsigned long size = nr * PAGE_SIZE;
448 * First accumulate quantities that depend only on |size| and the type
449 * of the compound page.
451 if (PageAnon(page)) {
452 mss->anonymous += size;
453 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
454 mss->lazyfree += size;
460 mss->resident += size;
461 /* Accumulate the size in pages that have been accessed. */
462 if (young || page_is_young(page) || PageReferenced(page))
463 mss->referenced += size;
466 * Then accumulate quantities that may depend on sharing, or that may
467 * differ page-by-page.
469 * page_count(page) == 1 guarantees the page is mapped exactly once.
470 * If any subpage of the compound page mapped with PTE it would elevate
473 * The page_mapcount() is called to get a snapshot of the mapcount.
474 * Without holding the page lock this snapshot can be slightly wrong as
475 * we cannot always read the mapcount atomically. It is not safe to
476 * call page_mapcount() even with PTL held if the page is not mapped,
477 * especially for migration entries. Treat regular migration entries
480 if ((page_count(page) == 1) || migration) {
481 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
485 for (i = 0; i < nr; i++, page++) {
486 int mapcount = page_mapcount(page);
487 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
490 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
496 static int smaps_pte_hole(unsigned long addr, unsigned long end,
497 __always_unused int depth, struct mm_walk *walk)
499 struct mem_size_stats *mss = walk->private;
500 struct vm_area_struct *vma = walk->vma;
502 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping,
503 linear_page_index(vma, addr),
504 linear_page_index(vma, end));
509 #define smaps_pte_hole NULL
510 #endif /* CONFIG_SHMEM */
512 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
515 if (walk->ops->pte_hole) {
516 /* depth is not used */
517 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk);
522 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
523 struct mm_walk *walk)
525 struct mem_size_stats *mss = walk->private;
526 struct vm_area_struct *vma = walk->vma;
527 bool locked = !!(vma->vm_flags & VM_LOCKED);
528 struct page *page = NULL;
529 bool migration = false, young = false, dirty = false;
530 pte_t ptent = ptep_get(pte);
532 if (pte_present(ptent)) {
533 page = vm_normal_page(vma, addr, ptent);
534 young = pte_young(ptent);
535 dirty = pte_dirty(ptent);
536 } else if (is_swap_pte(ptent)) {
537 swp_entry_t swpent = pte_to_swp_entry(ptent);
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 page = vm_normal_page_pmd(vma, addr, *pmd);
580 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
581 swp_entry_t entry = pmd_to_swp_entry(*pmd);
583 if (is_migration_entry(entry)) {
585 page = pfn_swap_entry_to_page(entry);
588 if (IS_ERR_OR_NULL(page))
591 mss->anonymous_thp += HPAGE_PMD_SIZE;
592 else if (PageSwapBacked(page))
593 mss->shmem_thp += HPAGE_PMD_SIZE;
594 else if (is_zone_device_page(page))
597 mss->file_thp += HPAGE_PMD_SIZE;
599 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
603 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
604 struct mm_walk *walk)
609 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
610 struct mm_walk *walk)
612 struct vm_area_struct *vma = walk->vma;
616 ptl = pmd_trans_huge_lock(pmd, vma);
618 smaps_pmd_entry(pmd, addr, walk);
623 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
625 walk->action = ACTION_AGAIN;
628 for (; addr != end; pte++, addr += PAGE_SIZE)
629 smaps_pte_entry(pte, addr, walk);
630 pte_unmap_unlock(pte - 1, ptl);
636 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
639 * Don't forget to update Documentation/ on changes.
641 static const char mnemonics[BITS_PER_LONG][2] = {
643 * In case if we meet a flag we don't know about.
645 [0 ... (BITS_PER_LONG-1)] = "??",
647 [ilog2(VM_READ)] = "rd",
648 [ilog2(VM_WRITE)] = "wr",
649 [ilog2(VM_EXEC)] = "ex",
650 [ilog2(VM_SHARED)] = "sh",
651 [ilog2(VM_MAYREAD)] = "mr",
652 [ilog2(VM_MAYWRITE)] = "mw",
653 [ilog2(VM_MAYEXEC)] = "me",
654 [ilog2(VM_MAYSHARE)] = "ms",
655 [ilog2(VM_GROWSDOWN)] = "gd",
656 [ilog2(VM_PFNMAP)] = "pf",
657 [ilog2(VM_LOCKED)] = "lo",
658 [ilog2(VM_IO)] = "io",
659 [ilog2(VM_SEQ_READ)] = "sr",
660 [ilog2(VM_RAND_READ)] = "rr",
661 [ilog2(VM_DONTCOPY)] = "dc",
662 [ilog2(VM_DONTEXPAND)] = "de",
663 [ilog2(VM_LOCKONFAULT)] = "lf",
664 [ilog2(VM_ACCOUNT)] = "ac",
665 [ilog2(VM_NORESERVE)] = "nr",
666 [ilog2(VM_HUGETLB)] = "ht",
667 [ilog2(VM_SYNC)] = "sf",
668 [ilog2(VM_ARCH_1)] = "ar",
669 [ilog2(VM_WIPEONFORK)] = "wf",
670 [ilog2(VM_DONTDUMP)] = "dd",
671 #ifdef CONFIG_ARM64_BTI
672 [ilog2(VM_ARM64_BTI)] = "bt",
674 #ifdef CONFIG_MEM_SOFT_DIRTY
675 [ilog2(VM_SOFTDIRTY)] = "sd",
677 [ilog2(VM_MIXEDMAP)] = "mm",
678 [ilog2(VM_HUGEPAGE)] = "hg",
679 [ilog2(VM_NOHUGEPAGE)] = "nh",
680 [ilog2(VM_MERGEABLE)] = "mg",
681 [ilog2(VM_UFFD_MISSING)]= "um",
682 [ilog2(VM_UFFD_WP)] = "uw",
683 #ifdef CONFIG_ARM64_MTE
684 [ilog2(VM_MTE)] = "mt",
685 [ilog2(VM_MTE_ALLOWED)] = "",
687 #ifdef CONFIG_ARCH_HAS_PKEYS
688 /* These come out via ProtectionKey: */
689 [ilog2(VM_PKEY_BIT0)] = "",
690 [ilog2(VM_PKEY_BIT1)] = "",
691 [ilog2(VM_PKEY_BIT2)] = "",
692 [ilog2(VM_PKEY_BIT3)] = "",
694 [ilog2(VM_PKEY_BIT4)] = "",
696 #endif /* CONFIG_ARCH_HAS_PKEYS */
697 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
698 [ilog2(VM_UFFD_MINOR)] = "ui",
699 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
700 #ifdef CONFIG_X86_USER_SHADOW_STACK
701 [ilog2(VM_SHADOW_STACK)] = "ss",
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;
727 pte_t ptent = ptep_get(pte);
729 if (pte_present(ptent)) {
730 page = vm_normal_page(vma, addr, ptent);
731 } else if (is_swap_pte(ptent)) {
732 swp_entry_t swpent = pte_to_swp_entry(ptent);
734 if (is_pfn_swap_entry(swpent))
735 page = pfn_swap_entry_to_page(swpent);
738 if (page_mapcount(page) >= 2 || hugetlb_pmd_shared(pte))
739 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
741 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
746 #define smaps_hugetlb_range NULL
747 #endif /* HUGETLB_PAGE */
749 static const struct mm_walk_ops smaps_walk_ops = {
750 .pmd_entry = smaps_pte_range,
751 .hugetlb_entry = smaps_hugetlb_range,
752 .walk_lock = PGWALK_RDLOCK,
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,
759 .walk_lock = PGWALK_RDLOCK,
763 * Gather mem stats from @vma with the indicated beginning
764 * address @start, and keep them in @mss.
766 * Use vm_start of @vma as the beginning address if @start is 0.
768 static void smap_gather_stats(struct vm_area_struct *vma,
769 struct mem_size_stats *mss, unsigned long start)
771 const struct mm_walk_ops *ops = &smaps_walk_ops;
774 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\nKSM: ", mss->ksm);
834 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
835 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
836 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
837 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
838 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
839 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
840 mss->private_hugetlb >> 10, 7);
841 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
842 SEQ_PUT_DEC(" kB\nSwapPss: ",
843 mss->swap_pss >> PSS_SHIFT);
844 SEQ_PUT_DEC(" kB\nLocked: ",
845 mss->pss_locked >> PSS_SHIFT);
846 seq_puts(m, " kB\n");
849 static int show_smap(struct seq_file *m, void *v)
851 struct vm_area_struct *vma = v;
852 struct mem_size_stats mss = {};
854 smap_gather_stats(vma, &mss, 0);
856 show_map_vma(m, vma);
858 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
859 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
860 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
861 seq_puts(m, " kB\n");
863 __show_smap(m, &mss, false);
865 seq_printf(m, "THPeligible: %8u\n",
866 hugepage_vma_check(vma, vma->vm_flags, true, false, true));
868 if (arch_pkeys_enabled())
869 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
870 show_smap_vma_flags(m, vma);
875 static int show_smaps_rollup(struct seq_file *m, void *v)
877 struct proc_maps_private *priv = m->private;
878 struct mem_size_stats mss = {};
879 struct mm_struct *mm = priv->mm;
880 struct vm_area_struct *vma;
881 unsigned long vma_start = 0, last_vma_end = 0;
883 VMA_ITERATOR(vmi, mm, 0);
885 priv->task = get_proc_task(priv->inode);
889 if (!mm || !mmget_not_zero(mm)) {
894 ret = mmap_read_lock_killable(mm);
898 hold_task_mempolicy(priv);
899 vma = vma_next(&vmi);
904 vma_start = vma->vm_start;
906 smap_gather_stats(vma, &mss, 0);
907 last_vma_end = vma->vm_end;
910 * Release mmap_lock temporarily if someone wants to
911 * access it for write request.
913 if (mmap_lock_is_contended(mm)) {
914 vma_iter_invalidate(&vmi);
915 mmap_read_unlock(mm);
916 ret = mmap_read_lock_killable(mm);
918 release_task_mempolicy(priv);
923 * After dropping the lock, there are four cases to
924 * consider. See the following example for explanation.
926 * +------+------+-----------+
927 * | VMA1 | VMA2 | VMA3 |
928 * +------+------+-----------+
932 * Suppose we drop the lock after reading VMA2 due to
933 * contention, then we get:
937 * 1) VMA2 is freed, but VMA3 exists:
939 * vma_next(vmi) will return VMA3.
940 * In this case, just continue from VMA3.
942 * 2) VMA2 still exists:
944 * vma_next(vmi) will return VMA3.
945 * In this case, just continue from VMA3.
947 * 3) No more VMAs can be found:
949 * vma_next(vmi) will return NULL.
950 * No more things to do, just break.
952 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
954 * vma_next(vmi) will return VMA' whose range
955 * contains last_vma_end.
956 * Iterate VMA' from last_vma_end.
958 vma = vma_next(&vmi);
963 /* Case 1 and 2 above */
964 if (vma->vm_start >= last_vma_end)
968 if (vma->vm_end > last_vma_end)
969 smap_gather_stats(vma, &mss, last_vma_end);
971 } for_each_vma(vmi, vma);
974 show_vma_header_prefix(m, vma_start, last_vma_end, 0, 0, 0, 0);
976 seq_puts(m, "[rollup]\n");
978 __show_smap(m, &mss, true);
980 release_task_mempolicy(priv);
981 mmap_read_unlock(mm);
986 put_task_struct(priv->task);
993 static const struct seq_operations proc_pid_smaps_op = {
1000 static int pid_smaps_open(struct inode *inode, struct file *file)
1002 return do_maps_open(inode, file, &proc_pid_smaps_op);
1005 static int smaps_rollup_open(struct inode *inode, struct file *file)
1008 struct proc_maps_private *priv;
1010 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
1014 ret = single_open(file, show_smaps_rollup, priv);
1018 priv->inode = inode;
1019 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
1020 if (IS_ERR(priv->mm)) {
1021 ret = PTR_ERR(priv->mm);
1023 single_release(inode, file);
1034 static int smaps_rollup_release(struct inode *inode, struct file *file)
1036 struct seq_file *seq = file->private_data;
1037 struct proc_maps_private *priv = seq->private;
1043 return single_release(inode, file);
1046 const struct file_operations proc_pid_smaps_operations = {
1047 .open = pid_smaps_open,
1049 .llseek = seq_lseek,
1050 .release = proc_map_release,
1053 const struct file_operations proc_pid_smaps_rollup_operations = {
1054 .open = smaps_rollup_open,
1056 .llseek = seq_lseek,
1057 .release = smaps_rollup_release,
1060 enum clear_refs_types {
1064 CLEAR_REFS_SOFT_DIRTY,
1065 CLEAR_REFS_MM_HIWATER_RSS,
1069 struct clear_refs_private {
1070 enum clear_refs_types type;
1073 #ifdef CONFIG_MEM_SOFT_DIRTY
1075 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1079 if (!pte_write(pte))
1081 if (!is_cow_mapping(vma->vm_flags))
1083 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
1085 page = vm_normal_page(vma, addr, pte);
1088 return page_maybe_dma_pinned(page);
1091 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1092 unsigned long addr, pte_t *pte)
1095 * The soft-dirty tracker uses #PF-s to catch writes
1096 * to pages, so write-protect the pte as well. See the
1097 * Documentation/admin-guide/mm/soft-dirty.rst for full description
1098 * of how soft-dirty works.
1100 pte_t ptent = ptep_get(pte);
1102 if (pte_present(ptent)) {
1105 if (pte_is_pinned(vma, addr, ptent))
1107 old_pte = ptep_modify_prot_start(vma, addr, pte);
1108 ptent = pte_wrprotect(old_pte);
1109 ptent = pte_clear_soft_dirty(ptent);
1110 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
1111 } else if (is_swap_pte(ptent)) {
1112 ptent = pte_swp_clear_soft_dirty(ptent);
1113 set_pte_at(vma->vm_mm, addr, pte, ptent);
1117 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1118 unsigned long addr, pte_t *pte)
1123 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1124 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1125 unsigned long addr, pmd_t *pmdp)
1127 pmd_t old, pmd = *pmdp;
1129 if (pmd_present(pmd)) {
1130 /* See comment in change_huge_pmd() */
1131 old = pmdp_invalidate(vma, addr, pmdp);
1133 pmd = pmd_mkdirty(pmd);
1135 pmd = pmd_mkyoung(pmd);
1137 pmd = pmd_wrprotect(pmd);
1138 pmd = pmd_clear_soft_dirty(pmd);
1140 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1141 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1142 pmd = pmd_swp_clear_soft_dirty(pmd);
1143 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1147 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1148 unsigned long addr, pmd_t *pmdp)
1153 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
1154 unsigned long end, struct mm_walk *walk)
1156 struct clear_refs_private *cp = walk->private;
1157 struct vm_area_struct *vma = walk->vma;
1162 ptl = pmd_trans_huge_lock(pmd, vma);
1164 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1165 clear_soft_dirty_pmd(vma, addr, pmd);
1169 if (!pmd_present(*pmd))
1172 page = pmd_page(*pmd);
1174 /* Clear accessed and referenced bits. */
1175 pmdp_test_and_clear_young(vma, addr, pmd);
1176 test_and_clear_page_young(page);
1177 ClearPageReferenced(page);
1183 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1185 walk->action = ACTION_AGAIN;
1188 for (; addr != end; pte++, addr += PAGE_SIZE) {
1189 ptent = ptep_get(pte);
1191 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1192 clear_soft_dirty(vma, addr, pte);
1196 if (!pte_present(ptent))
1199 page = vm_normal_page(vma, addr, ptent);
1203 /* Clear accessed and referenced bits. */
1204 ptep_test_and_clear_young(vma, addr, pte);
1205 test_and_clear_page_young(page);
1206 ClearPageReferenced(page);
1208 pte_unmap_unlock(pte - 1, ptl);
1213 static int clear_refs_test_walk(unsigned long start, unsigned long end,
1214 struct mm_walk *walk)
1216 struct clear_refs_private *cp = walk->private;
1217 struct vm_area_struct *vma = walk->vma;
1219 if (vma->vm_flags & VM_PFNMAP)
1223 * Writing 1 to /proc/pid/clear_refs affects all pages.
1224 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1225 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1226 * Writing 4 to /proc/pid/clear_refs affects all pages.
1228 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1230 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1235 static const struct mm_walk_ops clear_refs_walk_ops = {
1236 .pmd_entry = clear_refs_pte_range,
1237 .test_walk = clear_refs_test_walk,
1238 .walk_lock = PGWALK_WRLOCK,
1241 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1242 size_t count, loff_t *ppos)
1244 struct task_struct *task;
1245 char buffer[PROC_NUMBUF] = {};
1246 struct mm_struct *mm;
1247 struct vm_area_struct *vma;
1248 enum clear_refs_types type;
1252 if (count > sizeof(buffer) - 1)
1253 count = sizeof(buffer) - 1;
1254 if (copy_from_user(buffer, buf, count))
1256 rv = kstrtoint(strstrip(buffer), 10, &itype);
1259 type = (enum clear_refs_types)itype;
1260 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1263 task = get_proc_task(file_inode(file));
1266 mm = get_task_mm(task);
1268 VMA_ITERATOR(vmi, mm, 0);
1269 struct mmu_notifier_range range;
1270 struct clear_refs_private cp = {
1274 if (mmap_write_lock_killable(mm)) {
1278 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1280 * Writing 5 to /proc/pid/clear_refs resets the peak
1281 * resident set size to this mm's current rss value.
1283 reset_mm_hiwater_rss(mm);
1287 if (type == CLEAR_REFS_SOFT_DIRTY) {
1288 for_each_vma(vmi, vma) {
1289 if (!(vma->vm_flags & VM_SOFTDIRTY))
1291 vm_flags_clear(vma, VM_SOFTDIRTY);
1292 vma_set_page_prot(vma);
1295 inc_tlb_flush_pending(mm);
1296 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1298 mmu_notifier_invalidate_range_start(&range);
1300 walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
1301 if (type == CLEAR_REFS_SOFT_DIRTY) {
1302 mmu_notifier_invalidate_range_end(&range);
1304 dec_tlb_flush_pending(mm);
1307 mmap_write_unlock(mm);
1311 put_task_struct(task);
1316 const struct file_operations proc_clear_refs_operations = {
1317 .write = clear_refs_write,
1318 .llseek = noop_llseek,
1325 struct pagemapread {
1326 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
1327 pagemap_entry_t *buffer;
1331 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
1332 #define PAGEMAP_WALK_MASK (PMD_MASK)
1334 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1335 #define PM_PFRAME_BITS 55
1336 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1337 #define PM_SOFT_DIRTY BIT_ULL(55)
1338 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1339 #define PM_UFFD_WP BIT_ULL(57)
1340 #define PM_FILE BIT_ULL(61)
1341 #define PM_SWAP BIT_ULL(62)
1342 #define PM_PRESENT BIT_ULL(63)
1344 #define PM_END_OF_BUFFER 1
1346 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1348 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1351 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1352 struct pagemapread *pm)
1354 pm->buffer[pm->pos++] = *pme;
1355 if (pm->pos >= pm->len)
1356 return PM_END_OF_BUFFER;
1360 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1361 __always_unused int depth, struct mm_walk *walk)
1363 struct pagemapread *pm = walk->private;
1364 unsigned long addr = start;
1367 while (addr < end) {
1368 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1369 pagemap_entry_t pme = make_pme(0, 0);
1370 /* End of address space hole, which we mark as non-present. */
1371 unsigned long hole_end;
1374 hole_end = min(end, vma->vm_start);
1378 for (; addr < hole_end; addr += PAGE_SIZE) {
1379 err = add_to_pagemap(addr, &pme, pm);
1387 /* Addresses in the VMA. */
1388 if (vma->vm_flags & VM_SOFTDIRTY)
1389 pme = make_pme(0, PM_SOFT_DIRTY);
1390 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1391 err = add_to_pagemap(addr, &pme, pm);
1400 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1401 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1403 u64 frame = 0, flags = 0;
1404 struct page *page = NULL;
1405 bool migration = false;
1407 if (pte_present(pte)) {
1409 frame = pte_pfn(pte);
1410 flags |= PM_PRESENT;
1411 page = vm_normal_page(vma, addr, pte);
1412 if (pte_soft_dirty(pte))
1413 flags |= PM_SOFT_DIRTY;
1414 if (pte_uffd_wp(pte))
1415 flags |= PM_UFFD_WP;
1416 } else if (is_swap_pte(pte)) {
1418 if (pte_swp_soft_dirty(pte))
1419 flags |= PM_SOFT_DIRTY;
1420 if (pte_swp_uffd_wp(pte))
1421 flags |= PM_UFFD_WP;
1422 entry = pte_to_swp_entry(pte);
1426 * For PFN swap offsets, keeping the offset field
1427 * to be PFN only to be compatible with old smaps.
1429 if (is_pfn_swap_entry(entry))
1430 offset = swp_offset_pfn(entry);
1432 offset = swp_offset(entry);
1433 frame = swp_type(entry) |
1434 (offset << MAX_SWAPFILES_SHIFT);
1437 migration = is_migration_entry(entry);
1438 if (is_pfn_swap_entry(entry))
1439 page = pfn_swap_entry_to_page(entry);
1440 if (pte_marker_entry_uffd_wp(entry))
1441 flags |= PM_UFFD_WP;
1444 if (page && !PageAnon(page))
1446 if (page && !migration && page_mapcount(page) == 1)
1447 flags |= PM_MMAP_EXCLUSIVE;
1448 if (vma->vm_flags & VM_SOFTDIRTY)
1449 flags |= PM_SOFT_DIRTY;
1451 return make_pme(frame, flags);
1454 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1455 struct mm_walk *walk)
1457 struct vm_area_struct *vma = walk->vma;
1458 struct pagemapread *pm = walk->private;
1460 pte_t *pte, *orig_pte;
1462 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1463 bool migration = false;
1465 ptl = pmd_trans_huge_lock(pmdp, vma);
1467 u64 flags = 0, frame = 0;
1469 struct page *page = NULL;
1471 if (vma->vm_flags & VM_SOFTDIRTY)
1472 flags |= PM_SOFT_DIRTY;
1474 if (pmd_present(pmd)) {
1475 page = pmd_page(pmd);
1477 flags |= PM_PRESENT;
1478 if (pmd_soft_dirty(pmd))
1479 flags |= PM_SOFT_DIRTY;
1480 if (pmd_uffd_wp(pmd))
1481 flags |= PM_UFFD_WP;
1483 frame = pmd_pfn(pmd) +
1484 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1486 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1487 else if (is_swap_pmd(pmd)) {
1488 swp_entry_t entry = pmd_to_swp_entry(pmd);
1489 unsigned long offset;
1492 if (is_pfn_swap_entry(entry))
1493 offset = swp_offset_pfn(entry);
1495 offset = swp_offset(entry);
1497 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1498 frame = swp_type(entry) |
1499 (offset << MAX_SWAPFILES_SHIFT);
1502 if (pmd_swp_soft_dirty(pmd))
1503 flags |= PM_SOFT_DIRTY;
1504 if (pmd_swp_uffd_wp(pmd))
1505 flags |= PM_UFFD_WP;
1506 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1507 migration = is_migration_entry(entry);
1508 page = pfn_swap_entry_to_page(entry);
1512 if (page && !migration && page_mapcount(page) == 1)
1513 flags |= PM_MMAP_EXCLUSIVE;
1515 for (; addr != end; addr += PAGE_SIZE) {
1516 pagemap_entry_t pme = make_pme(frame, flags);
1518 err = add_to_pagemap(addr, &pme, pm);
1522 if (flags & PM_PRESENT)
1524 else if (flags & PM_SWAP)
1525 frame += (1 << MAX_SWAPFILES_SHIFT);
1531 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1534 * We can assume that @vma always points to a valid one and @end never
1535 * goes beyond vma->vm_end.
1537 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1539 walk->action = ACTION_AGAIN;
1542 for (; addr < end; pte++, addr += PAGE_SIZE) {
1543 pagemap_entry_t pme;
1545 pme = pte_to_pagemap_entry(pm, vma, addr, ptep_get(pte));
1546 err = add_to_pagemap(addr, &pme, pm);
1550 pte_unmap_unlock(orig_pte, ptl);
1557 #ifdef CONFIG_HUGETLB_PAGE
1558 /* This function walks within one hugetlb entry in the single call */
1559 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1560 unsigned long addr, unsigned long end,
1561 struct mm_walk *walk)
1563 struct pagemapread *pm = walk->private;
1564 struct vm_area_struct *vma = walk->vma;
1565 u64 flags = 0, frame = 0;
1569 if (vma->vm_flags & VM_SOFTDIRTY)
1570 flags |= PM_SOFT_DIRTY;
1572 pte = huge_ptep_get(ptep);
1573 if (pte_present(pte)) {
1574 struct page *page = pte_page(pte);
1576 if (!PageAnon(page))
1579 if (page_mapcount(page) == 1)
1580 flags |= PM_MMAP_EXCLUSIVE;
1582 if (huge_pte_uffd_wp(pte))
1583 flags |= PM_UFFD_WP;
1585 flags |= PM_PRESENT;
1587 frame = pte_pfn(pte) +
1588 ((addr & ~hmask) >> PAGE_SHIFT);
1589 } else if (pte_swp_uffd_wp_any(pte)) {
1590 flags |= PM_UFFD_WP;
1593 for (; addr != end; addr += PAGE_SIZE) {
1594 pagemap_entry_t pme = make_pme(frame, flags);
1596 err = add_to_pagemap(addr, &pme, pm);
1599 if (pm->show_pfn && (flags & PM_PRESENT))
1608 #define pagemap_hugetlb_range NULL
1609 #endif /* HUGETLB_PAGE */
1611 static const struct mm_walk_ops pagemap_ops = {
1612 .pmd_entry = pagemap_pmd_range,
1613 .pte_hole = pagemap_pte_hole,
1614 .hugetlb_entry = pagemap_hugetlb_range,
1615 .walk_lock = PGWALK_RDLOCK,
1619 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1621 * For each page in the address space, this file contains one 64-bit entry
1622 * consisting of the following:
1624 * Bits 0-54 page frame number (PFN) if present
1625 * Bits 0-4 swap type if swapped
1626 * Bits 5-54 swap offset if swapped
1627 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
1628 * Bit 56 page exclusively mapped
1629 * Bit 57 pte is uffd-wp write-protected
1631 * Bit 61 page is file-page or shared-anon
1632 * Bit 62 page swapped
1633 * Bit 63 page present
1635 * If the page is not present but in swap, then the PFN contains an
1636 * encoding of the swap file number and the page's offset into the
1637 * swap. Unmapped pages return a null PFN. This allows determining
1638 * precisely which pages are mapped (or in swap) and comparing mapped
1639 * pages between processes.
1641 * Efficient users of this interface will use /proc/pid/maps to
1642 * determine which areas of memory are actually mapped and llseek to
1643 * skip over unmapped regions.
1645 static ssize_t pagemap_read(struct file *file, char __user *buf,
1646 size_t count, loff_t *ppos)
1648 struct mm_struct *mm = file->private_data;
1649 struct pagemapread pm;
1651 unsigned long svpfn;
1652 unsigned long start_vaddr;
1653 unsigned long end_vaddr;
1654 int ret = 0, copied = 0;
1656 if (!mm || !mmget_not_zero(mm))
1660 /* file position must be aligned */
1661 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1668 /* do not disclose physical addresses: attack vector */
1669 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1671 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1672 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
1678 svpfn = src / PM_ENTRY_BYTES;
1679 end_vaddr = mm->task_size;
1681 /* watch out for wraparound */
1682 start_vaddr = end_vaddr;
1683 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
1686 ret = mmap_read_lock_killable(mm);
1689 start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
1690 mmap_read_unlock(mm);
1692 end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
1693 if (end >= start_vaddr && end < mm->task_size)
1697 /* Ensure the address is inside the task */
1698 if (start_vaddr > mm->task_size)
1699 start_vaddr = end_vaddr;
1702 while (count && (start_vaddr < end_vaddr)) {
1707 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1709 if (end < start_vaddr || end > end_vaddr)
1711 ret = mmap_read_lock_killable(mm);
1714 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
1715 mmap_read_unlock(mm);
1718 len = min(count, PM_ENTRY_BYTES * pm.pos);
1719 if (copy_to_user(buf, pm.buffer, len)) {
1728 if (!ret || ret == PM_END_OF_BUFFER)
1739 static int pagemap_open(struct inode *inode, struct file *file)
1741 struct mm_struct *mm;
1743 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1746 file->private_data = mm;
1750 static int pagemap_release(struct inode *inode, struct file *file)
1752 struct mm_struct *mm = file->private_data;
1759 const struct file_operations proc_pagemap_operations = {
1760 .llseek = mem_lseek, /* borrow this */
1761 .read = pagemap_read,
1762 .open = pagemap_open,
1763 .release = pagemap_release,
1765 #endif /* CONFIG_PROC_PAGE_MONITOR */
1770 unsigned long pages;
1772 unsigned long active;
1773 unsigned long writeback;
1774 unsigned long mapcount_max;
1775 unsigned long dirty;
1776 unsigned long swapcache;
1777 unsigned long node[MAX_NUMNODES];
1780 struct numa_maps_private {
1781 struct proc_maps_private proc_maps;
1782 struct numa_maps md;
1785 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1786 unsigned long nr_pages)
1788 int count = page_mapcount(page);
1790 md->pages += nr_pages;
1791 if (pte_dirty || PageDirty(page))
1792 md->dirty += nr_pages;
1794 if (PageSwapCache(page))
1795 md->swapcache += nr_pages;
1797 if (PageActive(page) || PageUnevictable(page))
1798 md->active += nr_pages;
1800 if (PageWriteback(page))
1801 md->writeback += nr_pages;
1804 md->anon += nr_pages;
1806 if (count > md->mapcount_max)
1807 md->mapcount_max = count;
1809 md->node[page_to_nid(page)] += nr_pages;
1812 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1818 if (!pte_present(pte))
1821 page = vm_normal_page(vma, addr, pte);
1822 if (!page || is_zone_device_page(page))
1825 if (PageReserved(page))
1828 nid = page_to_nid(page);
1829 if (!node_isset(nid, node_states[N_MEMORY]))
1835 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1836 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1837 struct vm_area_struct *vma,
1843 if (!pmd_present(pmd))
1846 page = vm_normal_page_pmd(vma, addr, pmd);
1850 if (PageReserved(page))
1853 nid = page_to_nid(page);
1854 if (!node_isset(nid, node_states[N_MEMORY]))
1861 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1862 unsigned long end, struct mm_walk *walk)
1864 struct numa_maps *md = walk->private;
1865 struct vm_area_struct *vma = walk->vma;
1870 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1871 ptl = pmd_trans_huge_lock(pmd, vma);
1875 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1877 gather_stats(page, md, pmd_dirty(*pmd),
1878 HPAGE_PMD_SIZE/PAGE_SIZE);
1883 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1885 walk->action = ACTION_AGAIN;
1889 pte_t ptent = ptep_get(pte);
1890 struct page *page = can_gather_numa_stats(ptent, vma, addr);
1893 gather_stats(page, md, pte_dirty(ptent), 1);
1895 } while (pte++, addr += PAGE_SIZE, addr != end);
1896 pte_unmap_unlock(orig_pte, ptl);
1900 #ifdef CONFIG_HUGETLB_PAGE
1901 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1902 unsigned long addr, unsigned long end, struct mm_walk *walk)
1904 pte_t huge_pte = huge_ptep_get(pte);
1905 struct numa_maps *md;
1908 if (!pte_present(huge_pte))
1911 page = pte_page(huge_pte);
1914 gather_stats(page, md, pte_dirty(huge_pte), 1);
1919 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1920 unsigned long addr, unsigned long end, struct mm_walk *walk)
1926 static const struct mm_walk_ops show_numa_ops = {
1927 .hugetlb_entry = gather_hugetlb_stats,
1928 .pmd_entry = gather_pte_stats,
1929 .walk_lock = PGWALK_RDLOCK,
1933 * Display pages allocated per node and memory policy via /proc.
1935 static int show_numa_map(struct seq_file *m, void *v)
1937 struct numa_maps_private *numa_priv = m->private;
1938 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1939 struct vm_area_struct *vma = v;
1940 struct numa_maps *md = &numa_priv->md;
1941 struct file *file = vma->vm_file;
1942 struct mm_struct *mm = vma->vm_mm;
1943 struct mempolicy *pol;
1950 /* Ensure we start with an empty set of numa_maps statistics. */
1951 memset(md, 0, sizeof(*md));
1953 pol = __get_vma_policy(vma, vma->vm_start);
1955 mpol_to_str(buffer, sizeof(buffer), pol);
1958 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1961 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1964 seq_puts(m, " file=");
1965 seq_file_path(m, file, "\n\t= ");
1966 } else if (vma_is_initial_heap(vma)) {
1967 seq_puts(m, " heap");
1968 } else if (vma_is_initial_stack(vma)) {
1969 seq_puts(m, " stack");
1972 if (is_vm_hugetlb_page(vma))
1973 seq_puts(m, " huge");
1975 /* mmap_lock is held by m_start */
1976 walk_page_vma(vma, &show_numa_ops, md);
1982 seq_printf(m, " anon=%lu", md->anon);
1985 seq_printf(m, " dirty=%lu", md->dirty);
1987 if (md->pages != md->anon && md->pages != md->dirty)
1988 seq_printf(m, " mapped=%lu", md->pages);
1990 if (md->mapcount_max > 1)
1991 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1994 seq_printf(m, " swapcache=%lu", md->swapcache);
1996 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1997 seq_printf(m, " active=%lu", md->active);
2000 seq_printf(m, " writeback=%lu", md->writeback);
2002 for_each_node_state(nid, N_MEMORY)
2004 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
2006 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
2012 static const struct seq_operations proc_pid_numa_maps_op = {
2016 .show = show_numa_map,
2019 static int pid_numa_maps_open(struct inode *inode, struct file *file)
2021 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
2022 sizeof(struct numa_maps_private));
2025 const struct file_operations proc_pid_numa_maps_operations = {
2026 .open = pid_numa_maps_open,
2028 .llseek = seq_lseek,
2029 .release = proc_map_release,
2032 #endif /* CONFIG_NUMA */