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>
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 struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv,
129 struct vm_area_struct *vma = vma_next(&priv->iter);
132 *ppos = vma->vm_start;
135 vma = get_gate_vma(priv->mm);
141 static void *m_start(struct seq_file *m, loff_t *ppos)
143 struct proc_maps_private *priv = m->private;
144 unsigned long last_addr = *ppos;
145 struct mm_struct *mm;
147 /* See m_next(). Zero at the start or after lseek. */
148 if (last_addr == -1UL)
151 priv->task = get_proc_task(priv->inode);
153 return ERR_PTR(-ESRCH);
156 if (!mm || !mmget_not_zero(mm)) {
157 put_task_struct(priv->task);
162 if (mmap_read_lock_killable(mm)) {
164 put_task_struct(priv->task);
166 return ERR_PTR(-EINTR);
169 vma_iter_init(&priv->iter, mm, last_addr);
170 hold_task_mempolicy(priv);
171 if (last_addr == -2UL)
172 return get_gate_vma(mm);
174 return proc_get_vma(priv, ppos);
177 static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
183 return proc_get_vma(m->private, ppos);
186 static void m_stop(struct seq_file *m, void *v)
188 struct proc_maps_private *priv = m->private;
189 struct mm_struct *mm = priv->mm;
194 release_task_mempolicy(priv);
195 mmap_read_unlock(mm);
197 put_task_struct(priv->task);
201 static int proc_maps_open(struct inode *inode, struct file *file,
202 const struct seq_operations *ops, int psize)
204 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
210 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
211 if (IS_ERR(priv->mm)) {
212 int err = PTR_ERR(priv->mm);
214 seq_release_private(inode, file);
221 static int proc_map_release(struct inode *inode, struct file *file)
223 struct seq_file *seq = file->private_data;
224 struct proc_maps_private *priv = seq->private;
229 return seq_release_private(inode, file);
232 static int do_maps_open(struct inode *inode, struct file *file,
233 const struct seq_operations *ops)
235 return proc_maps_open(inode, file, ops,
236 sizeof(struct proc_maps_private));
239 static void show_vma_header_prefix(struct seq_file *m,
240 unsigned long start, unsigned long end,
241 vm_flags_t flags, unsigned long long pgoff,
242 dev_t dev, unsigned long ino)
244 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
245 seq_put_hex_ll(m, NULL, start, 8);
246 seq_put_hex_ll(m, "-", end, 8);
248 seq_putc(m, flags & VM_READ ? 'r' : '-');
249 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
250 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
251 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
252 seq_put_hex_ll(m, " ", pgoff, 8);
253 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
254 seq_put_hex_ll(m, ":", MINOR(dev), 2);
255 seq_put_decimal_ull(m, " ", ino);
260 show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
262 struct anon_vma_name *anon_name = NULL;
263 struct mm_struct *mm = vma->vm_mm;
264 struct file *file = vma->vm_file;
265 vm_flags_t flags = vma->vm_flags;
266 unsigned long ino = 0;
267 unsigned long long pgoff = 0;
268 unsigned long start, end;
270 const char *name = NULL;
273 struct inode *inode = file_inode(vma->vm_file);
274 dev = inode->i_sb->s_dev;
276 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
279 start = vma->vm_start;
281 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
283 anon_name = anon_vma_name(vma);
286 * Print the dentry name for named mappings, and a
287 * special [heap] marker for the heap:
292 * If user named this anon shared memory via
293 * prctl(PR_SET_VMA ..., use the provided name.
296 seq_printf(m, "[anon_shmem:%s]", anon_name->name);
298 seq_file_path(m, file, "\n");
302 if (vma->vm_ops && vma->vm_ops->name) {
303 name = vma->vm_ops->name(vma);
308 name = arch_vma_name(vma);
315 if (vma_is_initial_heap(vma)) {
320 if (vma_is_initial_stack(vma)) {
327 seq_printf(m, "[anon:%s]", anon_name->name);
339 static int show_map(struct seq_file *m, void *v)
345 static const struct seq_operations proc_pid_maps_op = {
352 static int pid_maps_open(struct inode *inode, struct file *file)
354 return do_maps_open(inode, file, &proc_pid_maps_op);
357 const struct file_operations proc_pid_maps_operations = {
358 .open = pid_maps_open,
361 .release = proc_map_release,
365 * Proportional Set Size(PSS): my share of RSS.
367 * PSS of a process is the count of pages it has in memory, where each
368 * page is divided by the number of processes sharing it. So if a
369 * process has 1000 pages all to itself, and 1000 shared with one other
370 * process, its PSS will be 1500.
372 * To keep (accumulated) division errors low, we adopt a 64bit
373 * fixed-point pss counter to minimize division errors. So (pss >>
374 * PSS_SHIFT) would be the real byte count.
376 * A shift of 12 before division means (assuming 4K page size):
377 * - 1M 3-user-pages add up to 8KB errors;
378 * - supports mapcount up to 2^24, or 16M;
379 * - supports PSS up to 2^52 bytes, or 4PB.
383 #ifdef CONFIG_PROC_PAGE_MONITOR
384 struct mem_size_stats {
385 unsigned long resident;
386 unsigned long shared_clean;
387 unsigned long shared_dirty;
388 unsigned long private_clean;
389 unsigned long private_dirty;
390 unsigned long referenced;
391 unsigned long anonymous;
392 unsigned long lazyfree;
393 unsigned long anonymous_thp;
394 unsigned long shmem_thp;
395 unsigned long file_thp;
397 unsigned long shared_hugetlb;
398 unsigned long private_hugetlb;
408 static void smaps_page_accumulate(struct mem_size_stats *mss,
409 struct page *page, unsigned long size, unsigned long pss,
410 bool dirty, bool locked, bool private)
415 mss->pss_anon += pss;
416 else if (PageSwapBacked(page))
417 mss->pss_shmem += pss;
419 mss->pss_file += pss;
422 mss->pss_locked += pss;
424 if (dirty || PageDirty(page)) {
425 mss->pss_dirty += pss;
427 mss->private_dirty += size;
429 mss->shared_dirty += size;
432 mss->private_clean += size;
434 mss->shared_clean += size;
438 static void smaps_account(struct mem_size_stats *mss, struct page *page,
439 bool compound, bool young, bool dirty, bool locked,
442 int i, nr = compound ? compound_nr(page) : 1;
443 unsigned long size = nr * PAGE_SIZE;
446 * First accumulate quantities that depend only on |size| and the type
447 * of the compound page.
449 if (PageAnon(page)) {
450 mss->anonymous += size;
451 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
452 mss->lazyfree += size;
455 mss->resident += size;
456 /* Accumulate the size in pages that have been accessed. */
457 if (young || page_is_young(page) || PageReferenced(page))
458 mss->referenced += size;
461 * Then accumulate quantities that may depend on sharing, or that may
462 * differ page-by-page.
464 * page_count(page) == 1 guarantees the page is mapped exactly once.
465 * If any subpage of the compound page mapped with PTE it would elevate
468 * The page_mapcount() is called to get a snapshot of the mapcount.
469 * Without holding the page lock this snapshot can be slightly wrong as
470 * we cannot always read the mapcount atomically. It is not safe to
471 * call page_mapcount() even with PTL held if the page is not mapped,
472 * especially for migration entries. Treat regular migration entries
475 if ((page_count(page) == 1) || migration) {
476 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
480 for (i = 0; i < nr; i++, page++) {
481 int mapcount = page_mapcount(page);
482 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
485 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
491 static int smaps_pte_hole(unsigned long addr, unsigned long end,
492 __always_unused int depth, struct mm_walk *walk)
494 struct mem_size_stats *mss = walk->private;
495 struct vm_area_struct *vma = walk->vma;
497 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping,
498 linear_page_index(vma, addr),
499 linear_page_index(vma, end));
504 #define smaps_pte_hole NULL
505 #endif /* CONFIG_SHMEM */
507 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
510 if (walk->ops->pte_hole) {
511 /* depth is not used */
512 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk);
517 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
518 struct mm_walk *walk)
520 struct mem_size_stats *mss = walk->private;
521 struct vm_area_struct *vma = walk->vma;
522 bool locked = !!(vma->vm_flags & VM_LOCKED);
523 struct page *page = NULL;
524 bool migration = false, young = false, dirty = false;
525 pte_t ptent = ptep_get(pte);
527 if (pte_present(ptent)) {
528 page = vm_normal_page(vma, addr, ptent);
529 young = pte_young(ptent);
530 dirty = pte_dirty(ptent);
531 } else if (is_swap_pte(ptent)) {
532 swp_entry_t swpent = pte_to_swp_entry(ptent);
534 if (!non_swap_entry(swpent)) {
537 mss->swap += PAGE_SIZE;
538 mapcount = swp_swapcount(swpent);
540 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
542 do_div(pss_delta, mapcount);
543 mss->swap_pss += pss_delta;
545 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
547 } else if (is_pfn_swap_entry(swpent)) {
548 if (is_migration_entry(swpent))
550 page = pfn_swap_entry_to_page(swpent);
553 smaps_pte_hole_lookup(addr, walk);
560 smaps_account(mss, page, false, young, dirty, locked, migration);
563 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
564 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
565 struct mm_walk *walk)
567 struct mem_size_stats *mss = walk->private;
568 struct vm_area_struct *vma = walk->vma;
569 bool locked = !!(vma->vm_flags & VM_LOCKED);
570 struct page *page = NULL;
571 bool migration = false;
573 if (pmd_present(*pmd)) {
574 page = vm_normal_page_pmd(vma, addr, *pmd);
575 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
576 swp_entry_t entry = pmd_to_swp_entry(*pmd);
578 if (is_migration_entry(entry)) {
580 page = pfn_swap_entry_to_page(entry);
583 if (IS_ERR_OR_NULL(page))
586 mss->anonymous_thp += HPAGE_PMD_SIZE;
587 else if (PageSwapBacked(page))
588 mss->shmem_thp += HPAGE_PMD_SIZE;
589 else if (is_zone_device_page(page))
592 mss->file_thp += HPAGE_PMD_SIZE;
594 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
598 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
599 struct mm_walk *walk)
604 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
605 struct mm_walk *walk)
607 struct vm_area_struct *vma = walk->vma;
611 ptl = pmd_trans_huge_lock(pmd, vma);
613 smaps_pmd_entry(pmd, addr, walk);
618 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
620 walk->action = ACTION_AGAIN;
623 for (; addr != end; pte++, addr += PAGE_SIZE)
624 smaps_pte_entry(pte, addr, walk);
625 pte_unmap_unlock(pte - 1, ptl);
631 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
634 * Don't forget to update Documentation/ on changes.
636 static const char mnemonics[BITS_PER_LONG][2] = {
638 * In case if we meet a flag we don't know about.
640 [0 ... (BITS_PER_LONG-1)] = "??",
642 [ilog2(VM_READ)] = "rd",
643 [ilog2(VM_WRITE)] = "wr",
644 [ilog2(VM_EXEC)] = "ex",
645 [ilog2(VM_SHARED)] = "sh",
646 [ilog2(VM_MAYREAD)] = "mr",
647 [ilog2(VM_MAYWRITE)] = "mw",
648 [ilog2(VM_MAYEXEC)] = "me",
649 [ilog2(VM_MAYSHARE)] = "ms",
650 [ilog2(VM_GROWSDOWN)] = "gd",
651 [ilog2(VM_PFNMAP)] = "pf",
652 [ilog2(VM_LOCKED)] = "lo",
653 [ilog2(VM_IO)] = "io",
654 [ilog2(VM_SEQ_READ)] = "sr",
655 [ilog2(VM_RAND_READ)] = "rr",
656 [ilog2(VM_DONTCOPY)] = "dc",
657 [ilog2(VM_DONTEXPAND)] = "de",
658 [ilog2(VM_LOCKONFAULT)] = "lf",
659 [ilog2(VM_ACCOUNT)] = "ac",
660 [ilog2(VM_NORESERVE)] = "nr",
661 [ilog2(VM_HUGETLB)] = "ht",
662 [ilog2(VM_SYNC)] = "sf",
663 [ilog2(VM_ARCH_1)] = "ar",
664 [ilog2(VM_WIPEONFORK)] = "wf",
665 [ilog2(VM_DONTDUMP)] = "dd",
666 #ifdef CONFIG_ARM64_BTI
667 [ilog2(VM_ARM64_BTI)] = "bt",
669 #ifdef CONFIG_MEM_SOFT_DIRTY
670 [ilog2(VM_SOFTDIRTY)] = "sd",
672 [ilog2(VM_MIXEDMAP)] = "mm",
673 [ilog2(VM_HUGEPAGE)] = "hg",
674 [ilog2(VM_NOHUGEPAGE)] = "nh",
675 [ilog2(VM_MERGEABLE)] = "mg",
676 [ilog2(VM_UFFD_MISSING)]= "um",
677 [ilog2(VM_UFFD_WP)] = "uw",
678 #ifdef CONFIG_ARM64_MTE
679 [ilog2(VM_MTE)] = "mt",
680 [ilog2(VM_MTE_ALLOWED)] = "",
682 #ifdef CONFIG_ARCH_HAS_PKEYS
683 /* These come out via ProtectionKey: */
684 [ilog2(VM_PKEY_BIT0)] = "",
685 [ilog2(VM_PKEY_BIT1)] = "",
686 [ilog2(VM_PKEY_BIT2)] = "",
687 [ilog2(VM_PKEY_BIT3)] = "",
689 [ilog2(VM_PKEY_BIT4)] = "",
691 #endif /* CONFIG_ARCH_HAS_PKEYS */
692 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
693 [ilog2(VM_UFFD_MINOR)] = "ui",
694 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
695 #ifdef CONFIG_X86_USER_SHADOW_STACK
696 [ilog2(VM_SHADOW_STACK)] = "ss",
701 seq_puts(m, "VmFlags: ");
702 for (i = 0; i < BITS_PER_LONG; i++) {
703 if (!mnemonics[i][0])
705 if (vma->vm_flags & (1UL << i)) {
706 seq_putc(m, mnemonics[i][0]);
707 seq_putc(m, mnemonics[i][1]);
714 #ifdef CONFIG_HUGETLB_PAGE
715 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
716 unsigned long addr, unsigned long end,
717 struct mm_walk *walk)
719 struct mem_size_stats *mss = walk->private;
720 struct vm_area_struct *vma = walk->vma;
721 struct page *page = NULL;
722 pte_t ptent = ptep_get(pte);
724 if (pte_present(ptent)) {
725 page = vm_normal_page(vma, addr, ptent);
726 } else if (is_swap_pte(ptent)) {
727 swp_entry_t swpent = pte_to_swp_entry(ptent);
729 if (is_pfn_swap_entry(swpent))
730 page = pfn_swap_entry_to_page(swpent);
733 if (page_mapcount(page) >= 2 || hugetlb_pmd_shared(pte))
734 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
736 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
741 #define smaps_hugetlb_range NULL
742 #endif /* HUGETLB_PAGE */
744 static const struct mm_walk_ops smaps_walk_ops = {
745 .pmd_entry = smaps_pte_range,
746 .hugetlb_entry = smaps_hugetlb_range,
747 .walk_lock = PGWALK_RDLOCK,
750 static const struct mm_walk_ops smaps_shmem_walk_ops = {
751 .pmd_entry = smaps_pte_range,
752 .hugetlb_entry = smaps_hugetlb_range,
753 .pte_hole = smaps_pte_hole,
754 .walk_lock = PGWALK_RDLOCK,
758 * Gather mem stats from @vma with the indicated beginning
759 * address @start, and keep them in @mss.
761 * Use vm_start of @vma as the beginning address if @start is 0.
763 static void smap_gather_stats(struct vm_area_struct *vma,
764 struct mem_size_stats *mss, unsigned long start)
766 const struct mm_walk_ops *ops = &smaps_walk_ops;
769 if (start >= vma->vm_end)
772 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
774 * For shared or readonly shmem mappings we know that all
775 * swapped out pages belong to the shmem object, and we can
776 * obtain the swap value much more efficiently. For private
777 * writable mappings, we might have COW pages that are
778 * not affected by the parent swapped out pages of the shmem
779 * object, so we have to distinguish them during the page walk.
780 * Unless we know that the shmem object (or the part mapped by
781 * our VMA) has no swapped out pages at all.
783 unsigned long shmem_swapped = shmem_swap_usage(vma);
785 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
786 !(vma->vm_flags & VM_WRITE))) {
787 mss->swap += shmem_swapped;
789 ops = &smaps_shmem_walk_ops;
793 /* mmap_lock is held in m_start */
795 walk_page_vma(vma, ops, mss);
797 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
800 #define SEQ_PUT_DEC(str, val) \
801 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
803 /* Show the contents common for smaps and smaps_rollup */
804 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
807 SEQ_PUT_DEC("Rss: ", mss->resident);
808 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
809 SEQ_PUT_DEC(" kB\nPss_Dirty: ", mss->pss_dirty >> PSS_SHIFT);
812 * These are meaningful only for smaps_rollup, otherwise two of
813 * them are zero, and the other one is the same as Pss.
815 SEQ_PUT_DEC(" kB\nPss_Anon: ",
816 mss->pss_anon >> PSS_SHIFT);
817 SEQ_PUT_DEC(" kB\nPss_File: ",
818 mss->pss_file >> PSS_SHIFT);
819 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
820 mss->pss_shmem >> PSS_SHIFT);
822 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
823 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
824 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
825 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
826 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
827 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
828 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
829 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
830 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
831 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
832 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
833 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
834 mss->private_hugetlb >> 10, 7);
835 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
836 SEQ_PUT_DEC(" kB\nSwapPss: ",
837 mss->swap_pss >> PSS_SHIFT);
838 SEQ_PUT_DEC(" kB\nLocked: ",
839 mss->pss_locked >> PSS_SHIFT);
840 seq_puts(m, " kB\n");
843 static int show_smap(struct seq_file *m, void *v)
845 struct vm_area_struct *vma = v;
846 struct mem_size_stats mss;
848 memset(&mss, 0, sizeof(mss));
850 smap_gather_stats(vma, &mss, 0);
852 show_map_vma(m, vma);
854 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
855 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
856 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
857 seq_puts(m, " kB\n");
859 __show_smap(m, &mss, false);
861 seq_printf(m, "THPeligible: %8u\n",
862 hugepage_vma_check(vma, vma->vm_flags, true, false, true));
864 if (arch_pkeys_enabled())
865 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
866 show_smap_vma_flags(m, vma);
871 static int show_smaps_rollup(struct seq_file *m, void *v)
873 struct proc_maps_private *priv = m->private;
874 struct mem_size_stats mss;
875 struct mm_struct *mm = priv->mm;
876 struct vm_area_struct *vma;
877 unsigned long vma_start = 0, last_vma_end = 0;
879 VMA_ITERATOR(vmi, mm, 0);
881 priv->task = get_proc_task(priv->inode);
885 if (!mm || !mmget_not_zero(mm)) {
890 memset(&mss, 0, sizeof(mss));
892 ret = mmap_read_lock_killable(mm);
896 hold_task_mempolicy(priv);
897 vma = vma_next(&vmi);
902 vma_start = vma->vm_start;
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 vma_iter_invalidate(&vmi);
913 mmap_read_unlock(mm);
914 ret = mmap_read_lock_killable(mm);
916 release_task_mempolicy(priv);
921 * After dropping the lock, there are four cases to
922 * consider. See the following example for explanation.
924 * +------+------+-----------+
925 * | VMA1 | VMA2 | VMA3 |
926 * +------+------+-----------+
930 * Suppose we drop the lock after reading VMA2 due to
931 * contention, then we get:
935 * 1) VMA2 is freed, but VMA3 exists:
937 * vma_next(vmi) will return VMA3.
938 * In this case, just continue from VMA3.
940 * 2) VMA2 still exists:
942 * vma_next(vmi) will return VMA3.
943 * In this case, just continue from VMA3.
945 * 3) No more VMAs can be found:
947 * vma_next(vmi) will return NULL.
948 * No more things to do, just break.
950 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
952 * vma_next(vmi) will return VMA' whose range
953 * contains last_vma_end.
954 * Iterate VMA' from last_vma_end.
956 vma = vma_next(&vmi);
961 /* Case 1 and 2 above */
962 if (vma->vm_start >= last_vma_end)
966 if (vma->vm_end > last_vma_end)
967 smap_gather_stats(vma, &mss, last_vma_end);
969 } for_each_vma(vmi, vma);
972 show_vma_header_prefix(m, vma_start, last_vma_end, 0, 0, 0, 0);
974 seq_puts(m, "[rollup]\n");
976 __show_smap(m, &mss, true);
978 release_task_mempolicy(priv);
979 mmap_read_unlock(mm);
984 put_task_struct(priv->task);
991 static const struct seq_operations proc_pid_smaps_op = {
998 static int pid_smaps_open(struct inode *inode, struct file *file)
1000 return do_maps_open(inode, file, &proc_pid_smaps_op);
1003 static int smaps_rollup_open(struct inode *inode, struct file *file)
1006 struct proc_maps_private *priv;
1008 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
1012 ret = single_open(file, show_smaps_rollup, priv);
1016 priv->inode = inode;
1017 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
1018 if (IS_ERR(priv->mm)) {
1019 ret = PTR_ERR(priv->mm);
1021 single_release(inode, file);
1032 static int smaps_rollup_release(struct inode *inode, struct file *file)
1034 struct seq_file *seq = file->private_data;
1035 struct proc_maps_private *priv = seq->private;
1041 return single_release(inode, file);
1044 const struct file_operations proc_pid_smaps_operations = {
1045 .open = pid_smaps_open,
1047 .llseek = seq_lseek,
1048 .release = proc_map_release,
1051 const struct file_operations proc_pid_smaps_rollup_operations = {
1052 .open = smaps_rollup_open,
1054 .llseek = seq_lseek,
1055 .release = smaps_rollup_release,
1058 enum clear_refs_types {
1062 CLEAR_REFS_SOFT_DIRTY,
1063 CLEAR_REFS_MM_HIWATER_RSS,
1067 struct clear_refs_private {
1068 enum clear_refs_types type;
1071 #ifdef CONFIG_MEM_SOFT_DIRTY
1073 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1077 if (!pte_write(pte))
1079 if (!is_cow_mapping(vma->vm_flags))
1081 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
1083 page = vm_normal_page(vma, addr, pte);
1086 return page_maybe_dma_pinned(page);
1089 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1090 unsigned long addr, pte_t *pte)
1093 * The soft-dirty tracker uses #PF-s to catch writes
1094 * to pages, so write-protect the pte as well. See the
1095 * Documentation/admin-guide/mm/soft-dirty.rst for full description
1096 * of how soft-dirty works.
1098 pte_t ptent = ptep_get(pte);
1100 if (pte_present(ptent)) {
1103 if (pte_is_pinned(vma, addr, ptent))
1105 old_pte = ptep_modify_prot_start(vma, addr, pte);
1106 ptent = pte_wrprotect(old_pte);
1107 ptent = pte_clear_soft_dirty(ptent);
1108 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
1109 } else if (is_swap_pte(ptent)) {
1110 ptent = pte_swp_clear_soft_dirty(ptent);
1111 set_pte_at(vma->vm_mm, addr, pte, ptent);
1115 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1116 unsigned long addr, pte_t *pte)
1121 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1122 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1123 unsigned long addr, pmd_t *pmdp)
1125 pmd_t old, pmd = *pmdp;
1127 if (pmd_present(pmd)) {
1128 /* See comment in change_huge_pmd() */
1129 old = pmdp_invalidate(vma, addr, pmdp);
1131 pmd = pmd_mkdirty(pmd);
1133 pmd = pmd_mkyoung(pmd);
1135 pmd = pmd_wrprotect(pmd);
1136 pmd = pmd_clear_soft_dirty(pmd);
1138 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1139 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1140 pmd = pmd_swp_clear_soft_dirty(pmd);
1141 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1145 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1146 unsigned long addr, pmd_t *pmdp)
1151 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
1152 unsigned long end, struct mm_walk *walk)
1154 struct clear_refs_private *cp = walk->private;
1155 struct vm_area_struct *vma = walk->vma;
1160 ptl = pmd_trans_huge_lock(pmd, vma);
1162 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1163 clear_soft_dirty_pmd(vma, addr, pmd);
1167 if (!pmd_present(*pmd))
1170 page = pmd_page(*pmd);
1172 /* Clear accessed and referenced bits. */
1173 pmdp_test_and_clear_young(vma, addr, pmd);
1174 test_and_clear_page_young(page);
1175 ClearPageReferenced(page);
1181 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1183 walk->action = ACTION_AGAIN;
1186 for (; addr != end; pte++, addr += PAGE_SIZE) {
1187 ptent = ptep_get(pte);
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,
1236 .walk_lock = PGWALK_WRLOCK,
1239 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1240 size_t count, loff_t *ppos)
1242 struct task_struct *task;
1243 char buffer[PROC_NUMBUF];
1244 struct mm_struct *mm;
1245 struct vm_area_struct *vma;
1246 enum clear_refs_types type;
1250 memset(buffer, 0, sizeof(buffer));
1251 if (count > sizeof(buffer) - 1)
1252 count = sizeof(buffer) - 1;
1253 if (copy_from_user(buffer, buf, count))
1255 rv = kstrtoint(strstrip(buffer), 10, &itype);
1258 type = (enum clear_refs_types)itype;
1259 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1262 task = get_proc_task(file_inode(file));
1265 mm = get_task_mm(task);
1267 VMA_ITERATOR(vmi, mm, 0);
1268 struct mmu_notifier_range range;
1269 struct clear_refs_private cp = {
1273 if (mmap_write_lock_killable(mm)) {
1277 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1279 * Writing 5 to /proc/pid/clear_refs resets the peak
1280 * resident set size to this mm's current rss value.
1282 reset_mm_hiwater_rss(mm);
1286 if (type == CLEAR_REFS_SOFT_DIRTY) {
1287 for_each_vma(vmi, vma) {
1288 if (!(vma->vm_flags & VM_SOFTDIRTY))
1290 vm_flags_clear(vma, VM_SOFTDIRTY);
1291 vma_set_page_prot(vma);
1294 inc_tlb_flush_pending(mm);
1295 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1297 mmu_notifier_invalidate_range_start(&range);
1299 walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
1300 if (type == CLEAR_REFS_SOFT_DIRTY) {
1301 mmu_notifier_invalidate_range_end(&range);
1303 dec_tlb_flush_pending(mm);
1306 mmap_write_unlock(mm);
1310 put_task_struct(task);
1315 const struct file_operations proc_clear_refs_operations = {
1316 .write = clear_refs_write,
1317 .llseek = noop_llseek,
1324 struct pagemapread {
1325 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
1326 pagemap_entry_t *buffer;
1330 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
1331 #define PAGEMAP_WALK_MASK (PMD_MASK)
1333 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1334 #define PM_PFRAME_BITS 55
1335 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1336 #define PM_SOFT_DIRTY BIT_ULL(55)
1337 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1338 #define PM_UFFD_WP BIT_ULL(57)
1339 #define PM_FILE BIT_ULL(61)
1340 #define PM_SWAP BIT_ULL(62)
1341 #define PM_PRESENT BIT_ULL(63)
1343 #define PM_END_OF_BUFFER 1
1345 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1347 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1350 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1351 struct pagemapread *pm)
1353 pm->buffer[pm->pos++] = *pme;
1354 if (pm->pos >= pm->len)
1355 return PM_END_OF_BUFFER;
1359 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1360 __always_unused int depth, struct mm_walk *walk)
1362 struct pagemapread *pm = walk->private;
1363 unsigned long addr = start;
1366 while (addr < end) {
1367 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1368 pagemap_entry_t pme = make_pme(0, 0);
1369 /* End of address space hole, which we mark as non-present. */
1370 unsigned long hole_end;
1373 hole_end = min(end, vma->vm_start);
1377 for (; addr < hole_end; addr += PAGE_SIZE) {
1378 err = add_to_pagemap(addr, &pme, pm);
1386 /* Addresses in the VMA. */
1387 if (vma->vm_flags & VM_SOFTDIRTY)
1388 pme = make_pme(0, PM_SOFT_DIRTY);
1389 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1390 err = add_to_pagemap(addr, &pme, pm);
1399 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1400 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1402 u64 frame = 0, flags = 0;
1403 struct page *page = NULL;
1404 bool migration = false;
1406 if (pte_present(pte)) {
1408 frame = pte_pfn(pte);
1409 flags |= PM_PRESENT;
1410 page = vm_normal_page(vma, addr, pte);
1411 if (pte_soft_dirty(pte))
1412 flags |= PM_SOFT_DIRTY;
1413 if (pte_uffd_wp(pte))
1414 flags |= PM_UFFD_WP;
1415 } else if (is_swap_pte(pte)) {
1417 if (pte_swp_soft_dirty(pte))
1418 flags |= PM_SOFT_DIRTY;
1419 if (pte_swp_uffd_wp(pte))
1420 flags |= PM_UFFD_WP;
1421 entry = pte_to_swp_entry(pte);
1425 * For PFN swap offsets, keeping the offset field
1426 * to be PFN only to be compatible with old smaps.
1428 if (is_pfn_swap_entry(entry))
1429 offset = swp_offset_pfn(entry);
1431 offset = swp_offset(entry);
1432 frame = swp_type(entry) |
1433 (offset << MAX_SWAPFILES_SHIFT);
1436 migration = is_migration_entry(entry);
1437 if (is_pfn_swap_entry(entry))
1438 page = pfn_swap_entry_to_page(entry);
1439 if (pte_marker_entry_uffd_wp(entry))
1440 flags |= PM_UFFD_WP;
1443 if (page && !PageAnon(page))
1445 if (page && !migration && page_mapcount(page) == 1)
1446 flags |= PM_MMAP_EXCLUSIVE;
1447 if (vma->vm_flags & VM_SOFTDIRTY)
1448 flags |= PM_SOFT_DIRTY;
1450 return make_pme(frame, flags);
1453 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1454 struct mm_walk *walk)
1456 struct vm_area_struct *vma = walk->vma;
1457 struct pagemapread *pm = walk->private;
1459 pte_t *pte, *orig_pte;
1461 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1462 bool migration = false;
1464 ptl = pmd_trans_huge_lock(pmdp, vma);
1466 u64 flags = 0, frame = 0;
1468 struct page *page = NULL;
1470 if (vma->vm_flags & VM_SOFTDIRTY)
1471 flags |= PM_SOFT_DIRTY;
1473 if (pmd_present(pmd)) {
1474 page = pmd_page(pmd);
1476 flags |= PM_PRESENT;
1477 if (pmd_soft_dirty(pmd))
1478 flags |= PM_SOFT_DIRTY;
1479 if (pmd_uffd_wp(pmd))
1480 flags |= PM_UFFD_WP;
1482 frame = pmd_pfn(pmd) +
1483 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1485 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1486 else if (is_swap_pmd(pmd)) {
1487 swp_entry_t entry = pmd_to_swp_entry(pmd);
1488 unsigned long offset;
1491 if (is_pfn_swap_entry(entry))
1492 offset = swp_offset_pfn(entry);
1494 offset = swp_offset(entry);
1496 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1497 frame = swp_type(entry) |
1498 (offset << MAX_SWAPFILES_SHIFT);
1501 if (pmd_swp_soft_dirty(pmd))
1502 flags |= PM_SOFT_DIRTY;
1503 if (pmd_swp_uffd_wp(pmd))
1504 flags |= PM_UFFD_WP;
1505 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1506 migration = is_migration_entry(entry);
1507 page = pfn_swap_entry_to_page(entry);
1511 if (page && !migration && page_mapcount(page) == 1)
1512 flags |= PM_MMAP_EXCLUSIVE;
1514 for (; addr != end; addr += PAGE_SIZE) {
1515 pagemap_entry_t pme = make_pme(frame, flags);
1517 err = add_to_pagemap(addr, &pme, pm);
1521 if (flags & PM_PRESENT)
1523 else if (flags & PM_SWAP)
1524 frame += (1 << MAX_SWAPFILES_SHIFT);
1530 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1533 * We can assume that @vma always points to a valid one and @end never
1534 * goes beyond vma->vm_end.
1536 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1538 walk->action = ACTION_AGAIN;
1541 for (; addr < end; pte++, addr += PAGE_SIZE) {
1542 pagemap_entry_t pme;
1544 pme = pte_to_pagemap_entry(pm, vma, addr, ptep_get(pte));
1545 err = add_to_pagemap(addr, &pme, pm);
1549 pte_unmap_unlock(orig_pte, ptl);
1556 #ifdef CONFIG_HUGETLB_PAGE
1557 /* This function walks within one hugetlb entry in the single call */
1558 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1559 unsigned long addr, unsigned long end,
1560 struct mm_walk *walk)
1562 struct pagemapread *pm = walk->private;
1563 struct vm_area_struct *vma = walk->vma;
1564 u64 flags = 0, frame = 0;
1568 if (vma->vm_flags & VM_SOFTDIRTY)
1569 flags |= PM_SOFT_DIRTY;
1571 pte = huge_ptep_get(ptep);
1572 if (pte_present(pte)) {
1573 struct page *page = pte_page(pte);
1575 if (!PageAnon(page))
1578 if (page_mapcount(page) == 1)
1579 flags |= PM_MMAP_EXCLUSIVE;
1581 if (huge_pte_uffd_wp(pte))
1582 flags |= PM_UFFD_WP;
1584 flags |= PM_PRESENT;
1586 frame = pte_pfn(pte) +
1587 ((addr & ~hmask) >> PAGE_SHIFT);
1588 } else if (pte_swp_uffd_wp_any(pte)) {
1589 flags |= PM_UFFD_WP;
1592 for (; addr != end; addr += PAGE_SIZE) {
1593 pagemap_entry_t pme = make_pme(frame, flags);
1595 err = add_to_pagemap(addr, &pme, pm);
1598 if (pm->show_pfn && (flags & PM_PRESENT))
1607 #define pagemap_hugetlb_range NULL
1608 #endif /* HUGETLB_PAGE */
1610 static const struct mm_walk_ops pagemap_ops = {
1611 .pmd_entry = pagemap_pmd_range,
1612 .pte_hole = pagemap_pte_hole,
1613 .hugetlb_entry = pagemap_hugetlb_range,
1614 .walk_lock = PGWALK_RDLOCK,
1618 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1620 * For each page in the address space, this file contains one 64-bit entry
1621 * consisting of the following:
1623 * Bits 0-54 page frame number (PFN) if present
1624 * Bits 0-4 swap type if swapped
1625 * Bits 5-54 swap offset if swapped
1626 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
1627 * Bit 56 page exclusively mapped
1628 * Bit 57 pte is uffd-wp write-protected
1630 * Bit 61 page is file-page or shared-anon
1631 * Bit 62 page swapped
1632 * Bit 63 page present
1634 * If the page is not present but in swap, then the PFN contains an
1635 * encoding of the swap file number and the page's offset into the
1636 * swap. Unmapped pages return a null PFN. This allows determining
1637 * precisely which pages are mapped (or in swap) and comparing mapped
1638 * pages between processes.
1640 * Efficient users of this interface will use /proc/pid/maps to
1641 * determine which areas of memory are actually mapped and llseek to
1642 * skip over unmapped regions.
1644 static ssize_t pagemap_read(struct file *file, char __user *buf,
1645 size_t count, loff_t *ppos)
1647 struct mm_struct *mm = file->private_data;
1648 struct pagemapread pm;
1650 unsigned long svpfn;
1651 unsigned long start_vaddr;
1652 unsigned long end_vaddr;
1653 int ret = 0, copied = 0;
1655 if (!mm || !mmget_not_zero(mm))
1659 /* file position must be aligned */
1660 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1667 /* do not disclose physical addresses: attack vector */
1668 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1670 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1671 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
1677 svpfn = src / PM_ENTRY_BYTES;
1678 end_vaddr = mm->task_size;
1680 /* watch out for wraparound */
1681 start_vaddr = end_vaddr;
1682 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
1685 ret = mmap_read_lock_killable(mm);
1688 start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
1689 mmap_read_unlock(mm);
1691 end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
1692 if (end >= start_vaddr && end < mm->task_size)
1696 /* Ensure the address is inside the task */
1697 if (start_vaddr > mm->task_size)
1698 start_vaddr = end_vaddr;
1701 while (count && (start_vaddr < end_vaddr)) {
1706 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1708 if (end < start_vaddr || end > end_vaddr)
1710 ret = mmap_read_lock_killable(mm);
1713 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
1714 mmap_read_unlock(mm);
1717 len = min(count, PM_ENTRY_BYTES * pm.pos);
1718 if (copy_to_user(buf, pm.buffer, len)) {
1727 if (!ret || ret == PM_END_OF_BUFFER)
1738 static int pagemap_open(struct inode *inode, struct file *file)
1740 struct mm_struct *mm;
1742 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1745 file->private_data = mm;
1749 static int pagemap_release(struct inode *inode, struct file *file)
1751 struct mm_struct *mm = file->private_data;
1758 const struct file_operations proc_pagemap_operations = {
1759 .llseek = mem_lseek, /* borrow this */
1760 .read = pagemap_read,
1761 .open = pagemap_open,
1762 .release = pagemap_release,
1764 #endif /* CONFIG_PROC_PAGE_MONITOR */
1769 unsigned long pages;
1771 unsigned long active;
1772 unsigned long writeback;
1773 unsigned long mapcount_max;
1774 unsigned long dirty;
1775 unsigned long swapcache;
1776 unsigned long node[MAX_NUMNODES];
1779 struct numa_maps_private {
1780 struct proc_maps_private proc_maps;
1781 struct numa_maps md;
1784 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1785 unsigned long nr_pages)
1787 int count = page_mapcount(page);
1789 md->pages += nr_pages;
1790 if (pte_dirty || PageDirty(page))
1791 md->dirty += nr_pages;
1793 if (PageSwapCache(page))
1794 md->swapcache += nr_pages;
1796 if (PageActive(page) || PageUnevictable(page))
1797 md->active += nr_pages;
1799 if (PageWriteback(page))
1800 md->writeback += nr_pages;
1803 md->anon += nr_pages;
1805 if (count > md->mapcount_max)
1806 md->mapcount_max = count;
1808 md->node[page_to_nid(page)] += nr_pages;
1811 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1817 if (!pte_present(pte))
1820 page = vm_normal_page(vma, addr, pte);
1821 if (!page || is_zone_device_page(page))
1824 if (PageReserved(page))
1827 nid = page_to_nid(page);
1828 if (!node_isset(nid, node_states[N_MEMORY]))
1834 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1835 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1836 struct vm_area_struct *vma,
1842 if (!pmd_present(pmd))
1845 page = vm_normal_page_pmd(vma, addr, pmd);
1849 if (PageReserved(page))
1852 nid = page_to_nid(page);
1853 if (!node_isset(nid, node_states[N_MEMORY]))
1860 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1861 unsigned long end, struct mm_walk *walk)
1863 struct numa_maps *md = walk->private;
1864 struct vm_area_struct *vma = walk->vma;
1869 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1870 ptl = pmd_trans_huge_lock(pmd, vma);
1874 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1876 gather_stats(page, md, pmd_dirty(*pmd),
1877 HPAGE_PMD_SIZE/PAGE_SIZE);
1882 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1884 walk->action = ACTION_AGAIN;
1888 pte_t ptent = ptep_get(pte);
1889 struct page *page = can_gather_numa_stats(ptent, vma, addr);
1892 gather_stats(page, md, pte_dirty(ptent), 1);
1894 } while (pte++, addr += PAGE_SIZE, addr != end);
1895 pte_unmap_unlock(orig_pte, ptl);
1899 #ifdef CONFIG_HUGETLB_PAGE
1900 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1901 unsigned long addr, unsigned long end, struct mm_walk *walk)
1903 pte_t huge_pte = huge_ptep_get(pte);
1904 struct numa_maps *md;
1907 if (!pte_present(huge_pte))
1910 page = pte_page(huge_pte);
1913 gather_stats(page, md, pte_dirty(huge_pte), 1);
1918 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1919 unsigned long addr, unsigned long end, struct mm_walk *walk)
1925 static const struct mm_walk_ops show_numa_ops = {
1926 .hugetlb_entry = gather_hugetlb_stats,
1927 .pmd_entry = gather_pte_stats,
1928 .walk_lock = PGWALK_RDLOCK,
1932 * Display pages allocated per node and memory policy via /proc.
1934 static int show_numa_map(struct seq_file *m, void *v)
1936 struct numa_maps_private *numa_priv = m->private;
1937 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1938 struct vm_area_struct *vma = v;
1939 struct numa_maps *md = &numa_priv->md;
1940 struct file *file = vma->vm_file;
1941 struct mm_struct *mm = vma->vm_mm;
1942 struct mempolicy *pol;
1949 /* Ensure we start with an empty set of numa_maps statistics. */
1950 memset(md, 0, sizeof(*md));
1952 pol = __get_vma_policy(vma, vma->vm_start);
1954 mpol_to_str(buffer, sizeof(buffer), pol);
1957 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1960 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1963 seq_puts(m, " file=");
1964 seq_file_path(m, file, "\n\t= ");
1965 } else if (vma_is_initial_heap(vma)) {
1966 seq_puts(m, " heap");
1967 } else if (vma_is_initial_stack(vma)) {
1968 seq_puts(m, " stack");
1971 if (is_vm_hugetlb_page(vma))
1972 seq_puts(m, " huge");
1974 /* mmap_lock is held by m_start */
1975 walk_page_vma(vma, &show_numa_ops, md);
1981 seq_printf(m, " anon=%lu", md->anon);
1984 seq_printf(m, " dirty=%lu", md->dirty);
1986 if (md->pages != md->anon && md->pages != md->dirty)
1987 seq_printf(m, " mapped=%lu", md->pages);
1989 if (md->mapcount_max > 1)
1990 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1993 seq_printf(m, " swapcache=%lu", md->swapcache);
1995 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1996 seq_printf(m, " active=%lu", md->active);
1999 seq_printf(m, " writeback=%lu", md->writeback);
2001 for_each_node_state(nid, N_MEMORY)
2003 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
2005 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
2011 static const struct seq_operations proc_pid_numa_maps_op = {
2015 .show = show_numa_map,
2018 static int pid_numa_maps_open(struct inode *inode, struct file *file)
2020 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
2021 sizeof(struct numa_maps_private));
2024 const struct file_operations proc_pid_numa_maps_operations = {
2025 .open = pid_numa_maps_open,
2027 .llseek = seq_lseek,
2028 .release = proc_map_release,
2031 #endif /* CONFIG_NUMA */