2 #include <linux/hugetlb.h>
3 #include <linux/huge_mm.h>
4 #include <linux/mount.h>
5 #include <linux/seq_file.h>
6 #include <linux/highmem.h>
7 #include <linux/ptrace.h>
8 #include <linux/slab.h>
9 #include <linux/pagemap.h>
10 #include <linux/mempolicy.h>
11 #include <linux/rmap.h>
12 #include <linux/swap.h>
13 #include <linux/swapops.h>
14 #include <linux/mmu_notifier.h>
17 #include <asm/uaccess.h>
18 #include <asm/tlbflush.h>
21 void task_mem(struct seq_file *m, struct mm_struct *mm)
23 unsigned long data, text, lib, swap;
24 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
27 * Note: to minimize their overhead, mm maintains hiwater_vm and
28 * hiwater_rss only when about to *lower* total_vm or rss. Any
29 * collector of these hiwater stats must therefore get total_vm
30 * and rss too, which will usually be the higher. Barriers? not
31 * worth the effort, such snapshots can always be inconsistent.
33 hiwater_vm = total_vm = mm->total_vm;
34 if (hiwater_vm < mm->hiwater_vm)
35 hiwater_vm = mm->hiwater_vm;
36 hiwater_rss = total_rss = get_mm_rss(mm);
37 if (hiwater_rss < mm->hiwater_rss)
38 hiwater_rss = mm->hiwater_rss;
40 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
41 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
42 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
43 swap = get_mm_counter(mm, MM_SWAPENTS);
57 hiwater_vm << (PAGE_SHIFT-10),
58 total_vm << (PAGE_SHIFT-10),
59 mm->locked_vm << (PAGE_SHIFT-10),
60 mm->pinned_vm << (PAGE_SHIFT-10),
61 hiwater_rss << (PAGE_SHIFT-10),
62 total_rss << (PAGE_SHIFT-10),
63 data << (PAGE_SHIFT-10),
64 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
65 (PTRS_PER_PTE * sizeof(pte_t) *
66 atomic_long_read(&mm->nr_ptes)) >> 10,
67 swap << (PAGE_SHIFT-10));
70 unsigned long task_vsize(struct mm_struct *mm)
72 return PAGE_SIZE * mm->total_vm;
75 unsigned long task_statm(struct mm_struct *mm,
76 unsigned long *shared, unsigned long *text,
77 unsigned long *data, unsigned long *resident)
79 *shared = get_mm_counter(mm, MM_FILEPAGES);
80 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
82 *data = mm->total_vm - mm->shared_vm;
83 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
89 * These functions are for numa_maps but called in generic **maps seq_file
90 * ->start(), ->stop() ops.
92 * numa_maps scans all vmas under mmap_sem and checks their mempolicy.
93 * Each mempolicy object is controlled by reference counting. The problem here
94 * is how to avoid accessing dead mempolicy object.
96 * Because we're holding mmap_sem while reading seq_file, it's safe to access
97 * each vma's mempolicy, no vma objects will never drop refs to mempolicy.
99 * A task's mempolicy (task->mempolicy) has different behavior. task->mempolicy
100 * is set and replaced under mmap_sem but unrefed and cleared under task_lock().
101 * So, without task_lock(), we cannot trust get_vma_policy() because we cannot
102 * gurantee the task never exits under us. But taking task_lock() around
103 * get_vma_plicy() causes lock order problem.
105 * To access task->mempolicy without lock, we hold a reference count of an
106 * object pointed by task->mempolicy and remember it. This will guarantee
107 * that task->mempolicy points to an alive object or NULL in numa_maps accesses.
109 static void hold_task_mempolicy(struct proc_maps_private *priv)
111 struct task_struct *task = priv->task;
114 priv->task_mempolicy = task->mempolicy;
115 mpol_get(priv->task_mempolicy);
118 static void release_task_mempolicy(struct proc_maps_private *priv)
120 mpol_put(priv->task_mempolicy);
123 static void hold_task_mempolicy(struct proc_maps_private *priv)
126 static void release_task_mempolicy(struct proc_maps_private *priv)
131 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
133 if (vma && vma != priv->tail_vma) {
134 struct mm_struct *mm = vma->vm_mm;
135 release_task_mempolicy(priv);
136 up_read(&mm->mmap_sem);
141 static void *m_start(struct seq_file *m, loff_t *pos)
143 struct proc_maps_private *priv = m->private;
144 unsigned long last_addr = m->version;
145 struct mm_struct *mm;
146 struct vm_area_struct *vma, *tail_vma = NULL;
149 /* Clear the per syscall fields in priv */
151 priv->tail_vma = NULL;
154 * We remember last_addr rather than next_addr to hit with
155 * mmap_cache most of the time. We have zero last_addr at
156 * the beginning and also after lseek. We will have -1 last_addr
157 * after the end of the vmas.
160 if (last_addr == -1UL)
163 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
165 return ERR_PTR(-ESRCH);
167 mm = mm_access(priv->task, PTRACE_MODE_READ);
168 if (!mm || IS_ERR(mm))
170 down_read(&mm->mmap_sem);
172 tail_vma = get_gate_vma(priv->task->mm);
173 priv->tail_vma = tail_vma;
174 hold_task_mempolicy(priv);
175 /* Start with last addr hint */
176 vma = find_vma(mm, last_addr);
177 if (last_addr && vma) {
183 * Check the vma index is within the range and do
184 * sequential scan until m_index.
187 if ((unsigned long)l < mm->map_count) {
194 if (l != mm->map_count)
195 tail_vma = NULL; /* After gate vma */
201 release_task_mempolicy(priv);
202 /* End of vmas has been reached */
203 m->version = (tail_vma != NULL)? 0: -1UL;
204 up_read(&mm->mmap_sem);
209 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
211 struct proc_maps_private *priv = m->private;
212 struct vm_area_struct *vma = v;
213 struct vm_area_struct *tail_vma = priv->tail_vma;
216 if (vma && (vma != tail_vma) && vma->vm_next)
219 return (vma != tail_vma)? tail_vma: NULL;
222 static void m_stop(struct seq_file *m, void *v)
224 struct proc_maps_private *priv = m->private;
225 struct vm_area_struct *vma = v;
230 put_task_struct(priv->task);
233 static int do_maps_open(struct inode *inode, struct file *file,
234 const struct seq_operations *ops)
236 struct proc_maps_private *priv;
238 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
240 priv->pid = proc_pid(inode);
241 ret = seq_open(file, ops);
243 struct seq_file *m = file->private_data;
253 show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
255 struct mm_struct *mm = vma->vm_mm;
256 struct file *file = vma->vm_file;
257 struct proc_maps_private *priv = m->private;
258 struct task_struct *task = priv->task;
259 vm_flags_t flags = vma->vm_flags;
260 unsigned long ino = 0;
261 unsigned long long pgoff = 0;
262 unsigned long start, end;
264 const char *name = NULL;
267 struct inode *inode = file_inode(vma->vm_file);
268 dev = inode->i_sb->s_dev;
270 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
273 /* We don't show the stack guard page in /proc/maps */
274 start = vma->vm_start;
275 if (stack_guard_page_start(vma, start))
278 if (stack_guard_page_end(vma, end))
281 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
282 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
285 flags & VM_READ ? 'r' : '-',
286 flags & VM_WRITE ? 'w' : '-',
287 flags & VM_EXEC ? 'x' : '-',
288 flags & VM_MAYSHARE ? 's' : 'p',
290 MAJOR(dev), MINOR(dev), ino);
293 * Print the dentry name for named mappings, and a
294 * special [heap] marker for the heap:
298 seq_path(m, &file->f_path, "\n");
302 name = arch_vma_name(vma);
311 if (vma->vm_start <= mm->brk &&
312 vma->vm_end >= mm->start_brk) {
317 tid = vm_is_stack(task, vma, is_pid);
321 * Thread stack in /proc/PID/task/TID/maps or
322 * the main process stack.
324 if (!is_pid || (vma->vm_start <= mm->start_stack &&
325 vma->vm_end >= mm->start_stack)) {
328 /* Thread stack in /proc/PID/maps */
330 seq_printf(m, "[stack:%d]", tid);
343 static int show_map(struct seq_file *m, void *v, int is_pid)
345 struct vm_area_struct *vma = v;
346 struct proc_maps_private *priv = m->private;
347 struct task_struct *task = priv->task;
349 show_map_vma(m, vma, is_pid);
351 if (m->count < m->size) /* vma is copied successfully */
352 m->version = (vma != get_gate_vma(task->mm))
357 static int show_pid_map(struct seq_file *m, void *v)
359 return show_map(m, v, 1);
362 static int show_tid_map(struct seq_file *m, void *v)
364 return show_map(m, v, 0);
367 static const struct seq_operations proc_pid_maps_op = {
374 static const struct seq_operations proc_tid_maps_op = {
381 static int pid_maps_open(struct inode *inode, struct file *file)
383 return do_maps_open(inode, file, &proc_pid_maps_op);
386 static int tid_maps_open(struct inode *inode, struct file *file)
388 return do_maps_open(inode, file, &proc_tid_maps_op);
391 const struct file_operations proc_pid_maps_operations = {
392 .open = pid_maps_open,
395 .release = seq_release_private,
398 const struct file_operations proc_tid_maps_operations = {
399 .open = tid_maps_open,
402 .release = seq_release_private,
406 * Proportional Set Size(PSS): my share of RSS.
408 * PSS of a process is the count of pages it has in memory, where each
409 * page is divided by the number of processes sharing it. So if a
410 * process has 1000 pages all to itself, and 1000 shared with one other
411 * process, its PSS will be 1500.
413 * To keep (accumulated) division errors low, we adopt a 64bit
414 * fixed-point pss counter to minimize division errors. So (pss >>
415 * PSS_SHIFT) would be the real byte count.
417 * A shift of 12 before division means (assuming 4K page size):
418 * - 1M 3-user-pages add up to 8KB errors;
419 * - supports mapcount up to 2^24, or 16M;
420 * - supports PSS up to 2^52 bytes, or 4PB.
424 #ifdef CONFIG_PROC_PAGE_MONITOR
425 struct mem_size_stats {
426 struct vm_area_struct *vma;
427 unsigned long resident;
428 unsigned long shared_clean;
429 unsigned long shared_dirty;
430 unsigned long private_clean;
431 unsigned long private_dirty;
432 unsigned long referenced;
433 unsigned long anonymous;
434 unsigned long anonymous_thp;
436 unsigned long nonlinear;
441 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
442 unsigned long ptent_size, struct mm_walk *walk)
444 struct mem_size_stats *mss = walk->private;
445 struct vm_area_struct *vma = mss->vma;
446 pgoff_t pgoff = linear_page_index(vma, addr);
447 struct page *page = NULL;
450 if (pte_present(ptent)) {
451 page = vm_normal_page(vma, addr, ptent);
452 } else if (is_swap_pte(ptent)) {
453 swp_entry_t swpent = pte_to_swp_entry(ptent);
455 if (!non_swap_entry(swpent))
456 mss->swap += ptent_size;
457 else if (is_migration_entry(swpent))
458 page = migration_entry_to_page(swpent);
459 } else if (pte_file(ptent)) {
460 if (pte_to_pgoff(ptent) != pgoff)
461 mss->nonlinear += ptent_size;
468 mss->anonymous += ptent_size;
470 if (page->index != pgoff)
471 mss->nonlinear += ptent_size;
473 mss->resident += ptent_size;
474 /* Accumulate the size in pages that have been accessed. */
475 if (pte_young(ptent) || PageReferenced(page))
476 mss->referenced += ptent_size;
477 mapcount = page_mapcount(page);
479 if (pte_dirty(ptent) || PageDirty(page))
480 mss->shared_dirty += ptent_size;
482 mss->shared_clean += ptent_size;
483 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
485 if (pte_dirty(ptent) || PageDirty(page))
486 mss->private_dirty += ptent_size;
488 mss->private_clean += ptent_size;
489 mss->pss += (ptent_size << PSS_SHIFT);
493 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
494 struct mm_walk *walk)
496 struct mem_size_stats *mss = walk->private;
497 struct vm_area_struct *vma = mss->vma;
501 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
502 smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk);
504 mss->anonymous_thp += HPAGE_PMD_SIZE;
508 if (pmd_trans_unstable(pmd))
511 * The mmap_sem held all the way back in m_start() is what
512 * keeps khugepaged out of here and from collapsing things
515 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
516 for (; addr != end; pte++, addr += PAGE_SIZE)
517 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
518 pte_unmap_unlock(pte - 1, ptl);
523 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
526 * Don't forget to update Documentation/ on changes.
528 static const char mnemonics[BITS_PER_LONG][2] = {
530 * In case if we meet a flag we don't know about.
532 [0 ... (BITS_PER_LONG-1)] = "??",
534 [ilog2(VM_READ)] = "rd",
535 [ilog2(VM_WRITE)] = "wr",
536 [ilog2(VM_EXEC)] = "ex",
537 [ilog2(VM_SHARED)] = "sh",
538 [ilog2(VM_MAYREAD)] = "mr",
539 [ilog2(VM_MAYWRITE)] = "mw",
540 [ilog2(VM_MAYEXEC)] = "me",
541 [ilog2(VM_MAYSHARE)] = "ms",
542 [ilog2(VM_GROWSDOWN)] = "gd",
543 [ilog2(VM_PFNMAP)] = "pf",
544 [ilog2(VM_DENYWRITE)] = "dw",
545 [ilog2(VM_LOCKED)] = "lo",
546 [ilog2(VM_IO)] = "io",
547 [ilog2(VM_SEQ_READ)] = "sr",
548 [ilog2(VM_RAND_READ)] = "rr",
549 [ilog2(VM_DONTCOPY)] = "dc",
550 [ilog2(VM_DONTEXPAND)] = "de",
551 [ilog2(VM_ACCOUNT)] = "ac",
552 [ilog2(VM_NORESERVE)] = "nr",
553 [ilog2(VM_HUGETLB)] = "ht",
554 [ilog2(VM_NONLINEAR)] = "nl",
555 [ilog2(VM_ARCH_1)] = "ar",
556 [ilog2(VM_DONTDUMP)] = "dd",
557 #ifdef CONFIG_MEM_SOFT_DIRTY
558 [ilog2(VM_SOFTDIRTY)] = "sd",
560 [ilog2(VM_MIXEDMAP)] = "mm",
561 [ilog2(VM_HUGEPAGE)] = "hg",
562 [ilog2(VM_NOHUGEPAGE)] = "nh",
563 [ilog2(VM_MERGEABLE)] = "mg",
567 seq_puts(m, "VmFlags: ");
568 for (i = 0; i < BITS_PER_LONG; i++) {
569 if (vma->vm_flags & (1UL << i)) {
570 seq_printf(m, "%c%c ",
571 mnemonics[i][0], mnemonics[i][1]);
577 static int show_smap(struct seq_file *m, void *v, int is_pid)
579 struct proc_maps_private *priv = m->private;
580 struct task_struct *task = priv->task;
581 struct vm_area_struct *vma = v;
582 struct mem_size_stats mss;
583 struct mm_walk smaps_walk = {
584 .pmd_entry = smaps_pte_range,
589 memset(&mss, 0, sizeof mss);
591 /* mmap_sem is held in m_start */
592 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
593 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
595 show_map_vma(m, vma, is_pid);
601 "Shared_Clean: %8lu kB\n"
602 "Shared_Dirty: %8lu kB\n"
603 "Private_Clean: %8lu kB\n"
604 "Private_Dirty: %8lu kB\n"
605 "Referenced: %8lu kB\n"
606 "Anonymous: %8lu kB\n"
607 "AnonHugePages: %8lu kB\n"
609 "KernelPageSize: %8lu kB\n"
610 "MMUPageSize: %8lu kB\n"
612 (vma->vm_end - vma->vm_start) >> 10,
614 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
615 mss.shared_clean >> 10,
616 mss.shared_dirty >> 10,
617 mss.private_clean >> 10,
618 mss.private_dirty >> 10,
619 mss.referenced >> 10,
621 mss.anonymous_thp >> 10,
623 vma_kernel_pagesize(vma) >> 10,
624 vma_mmu_pagesize(vma) >> 10,
625 (vma->vm_flags & VM_LOCKED) ?
626 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
628 if (vma->vm_flags & VM_NONLINEAR)
629 seq_printf(m, "Nonlinear: %8lu kB\n",
630 mss.nonlinear >> 10);
632 show_smap_vma_flags(m, vma);
634 if (m->count < m->size) /* vma is copied successfully */
635 m->version = (vma != get_gate_vma(task->mm))
640 static int show_pid_smap(struct seq_file *m, void *v)
642 return show_smap(m, v, 1);
645 static int show_tid_smap(struct seq_file *m, void *v)
647 return show_smap(m, v, 0);
650 static const struct seq_operations proc_pid_smaps_op = {
654 .show = show_pid_smap
657 static const struct seq_operations proc_tid_smaps_op = {
661 .show = show_tid_smap
664 static int pid_smaps_open(struct inode *inode, struct file *file)
666 return do_maps_open(inode, file, &proc_pid_smaps_op);
669 static int tid_smaps_open(struct inode *inode, struct file *file)
671 return do_maps_open(inode, file, &proc_tid_smaps_op);
674 const struct file_operations proc_pid_smaps_operations = {
675 .open = pid_smaps_open,
678 .release = seq_release_private,
681 const struct file_operations proc_tid_smaps_operations = {
682 .open = tid_smaps_open,
685 .release = seq_release_private,
689 * We do not want to have constant page-shift bits sitting in
690 * pagemap entries and are about to reuse them some time soon.
692 * Here's the "migration strategy":
693 * 1. when the system boots these bits remain what they are,
694 * but a warning about future change is printed in log;
695 * 2. once anyone clears soft-dirty bits via clear_refs file,
696 * these flag is set to denote, that user is aware of the
697 * new API and those page-shift bits change their meaning.
698 * The respective warning is printed in dmesg;
699 * 3. In a couple of releases we will remove all the mentions
700 * of page-shift in pagemap entries.
703 static bool soft_dirty_cleared __read_mostly;
705 enum clear_refs_types {
709 CLEAR_REFS_SOFT_DIRTY,
713 struct clear_refs_private {
714 struct vm_area_struct *vma;
715 enum clear_refs_types type;
718 static inline void clear_soft_dirty(struct vm_area_struct *vma,
719 unsigned long addr, pte_t *pte)
721 #ifdef CONFIG_MEM_SOFT_DIRTY
723 * The soft-dirty tracker uses #PF-s to catch writes
724 * to pages, so write-protect the pte as well. See the
725 * Documentation/vm/soft-dirty.txt for full description
726 * of how soft-dirty works.
730 if (pte_present(ptent)) {
731 ptent = pte_wrprotect(ptent);
732 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
733 } else if (is_swap_pte(ptent)) {
734 ptent = pte_swp_clear_soft_dirty(ptent);
735 } else if (pte_file(ptent)) {
736 ptent = pte_file_clear_soft_dirty(ptent);
739 if (vma->vm_flags & VM_SOFTDIRTY)
740 vma->vm_flags &= ~VM_SOFTDIRTY;
742 set_pte_at(vma->vm_mm, addr, pte, ptent);
746 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
747 unsigned long end, struct mm_walk *walk)
749 struct clear_refs_private *cp = walk->private;
750 struct vm_area_struct *vma = cp->vma;
755 split_huge_page_pmd(vma, addr, pmd);
756 if (pmd_trans_unstable(pmd))
759 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
760 for (; addr != end; pte++, addr += PAGE_SIZE) {
763 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
764 clear_soft_dirty(vma, addr, pte);
768 if (!pte_present(ptent))
771 page = vm_normal_page(vma, addr, ptent);
775 /* Clear accessed and referenced bits. */
776 ptep_test_and_clear_young(vma, addr, pte);
777 ClearPageReferenced(page);
779 pte_unmap_unlock(pte - 1, ptl);
784 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
785 size_t count, loff_t *ppos)
787 struct task_struct *task;
788 char buffer[PROC_NUMBUF];
789 struct mm_struct *mm;
790 struct vm_area_struct *vma;
791 enum clear_refs_types type;
795 memset(buffer, 0, sizeof(buffer));
796 if (count > sizeof(buffer) - 1)
797 count = sizeof(buffer) - 1;
798 if (copy_from_user(buffer, buf, count))
800 rv = kstrtoint(strstrip(buffer), 10, &itype);
803 type = (enum clear_refs_types)itype;
804 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
807 if (type == CLEAR_REFS_SOFT_DIRTY) {
808 soft_dirty_cleared = true;
809 pr_warn_once("The pagemap bits 55-60 has changed their meaning! "
810 "See the linux/Documentation/vm/pagemap.txt for details.\n");
813 task = get_proc_task(file_inode(file));
816 mm = get_task_mm(task);
818 struct clear_refs_private cp = {
821 struct mm_walk clear_refs_walk = {
822 .pmd_entry = clear_refs_pte_range,
826 down_read(&mm->mmap_sem);
827 if (type == CLEAR_REFS_SOFT_DIRTY)
828 mmu_notifier_invalidate_range_start(mm, 0, -1);
829 for (vma = mm->mmap; vma; vma = vma->vm_next) {
831 if (is_vm_hugetlb_page(vma))
834 * Writing 1 to /proc/pid/clear_refs affects all pages.
836 * Writing 2 to /proc/pid/clear_refs only affects
839 * Writing 3 to /proc/pid/clear_refs only affects file
842 if (type == CLEAR_REFS_ANON && vma->vm_file)
844 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
846 walk_page_range(vma->vm_start, vma->vm_end,
849 if (type == CLEAR_REFS_SOFT_DIRTY)
850 mmu_notifier_invalidate_range_end(mm, 0, -1);
852 up_read(&mm->mmap_sem);
855 put_task_struct(task);
860 const struct file_operations proc_clear_refs_operations = {
861 .write = clear_refs_write,
862 .llseek = noop_llseek,
870 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
871 pagemap_entry_t *buffer;
875 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
876 #define PAGEMAP_WALK_MASK (PMD_MASK)
878 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
879 #define PM_STATUS_BITS 3
880 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
881 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
882 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
883 #define PM_PSHIFT_BITS 6
884 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
885 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
886 #define __PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
887 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
888 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
889 /* in "new" pagemap pshift bits are occupied with more status bits */
890 #define PM_STATUS2(v2, x) (__PM_PSHIFT(v2 ? x : PAGE_SHIFT))
892 #define __PM_SOFT_DIRTY (1LL)
893 #define PM_PRESENT PM_STATUS(4LL)
894 #define PM_SWAP PM_STATUS(2LL)
895 #define PM_FILE PM_STATUS(1LL)
896 #define PM_NOT_PRESENT(v2) PM_STATUS2(v2, 0)
897 #define PM_END_OF_BUFFER 1
899 static inline pagemap_entry_t make_pme(u64 val)
901 return (pagemap_entry_t) { .pme = val };
904 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
905 struct pagemapread *pm)
907 pm->buffer[pm->pos++] = *pme;
908 if (pm->pos >= pm->len)
909 return PM_END_OF_BUFFER;
913 static int pagemap_pte_hole(unsigned long start, unsigned long end,
914 struct mm_walk *walk)
916 struct pagemapread *pm = walk->private;
919 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
921 for (addr = start; addr < end; addr += PAGE_SIZE) {
922 err = add_to_pagemap(addr, &pme, pm);
929 static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
930 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
933 struct page *page = NULL;
936 if (pte_present(pte)) {
937 frame = pte_pfn(pte);
939 page = vm_normal_page(vma, addr, pte);
940 if (pte_soft_dirty(pte))
941 flags2 |= __PM_SOFT_DIRTY;
942 } else if (is_swap_pte(pte)) {
944 if (pte_swp_soft_dirty(pte))
945 flags2 |= __PM_SOFT_DIRTY;
946 entry = pte_to_swp_entry(pte);
947 frame = swp_type(entry) |
948 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
950 if (is_migration_entry(entry))
951 page = migration_entry_to_page(entry);
953 if (vma->vm_flags & VM_SOFTDIRTY)
954 flags2 |= __PM_SOFT_DIRTY;
955 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
959 if (page && !PageAnon(page))
961 if ((vma->vm_flags & VM_SOFTDIRTY))
962 flags2 |= __PM_SOFT_DIRTY;
964 *pme = make_pme(PM_PFRAME(frame) | PM_STATUS2(pm->v2, flags2) | flags);
967 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
968 static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
969 pmd_t pmd, int offset, int pmd_flags2)
972 * Currently pmd for thp is always present because thp can not be
973 * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
974 * This if-check is just to prepare for future implementation.
976 if (pmd_present(pmd))
977 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
978 | PM_STATUS2(pm->v2, pmd_flags2) | PM_PRESENT);
980 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, pmd_flags2));
983 static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
984 pmd_t pmd, int offset, int pmd_flags2)
989 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
990 struct mm_walk *walk)
992 struct vm_area_struct *vma;
993 struct pagemapread *pm = walk->private;
997 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
999 /* find the first VMA at or above 'addr' */
1000 vma = find_vma(walk->mm, addr);
1001 if (vma && pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
1004 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(*pmd))
1005 pmd_flags2 = __PM_SOFT_DIRTY;
1009 for (; addr != end; addr += PAGE_SIZE) {
1010 unsigned long offset;
1012 offset = (addr & ~PAGEMAP_WALK_MASK) >>
1014 thp_pmd_to_pagemap_entry(&pme, pm, *pmd, offset, pmd_flags2);
1015 err = add_to_pagemap(addr, &pme, pm);
1023 if (pmd_trans_unstable(pmd))
1025 for (; addr != end; addr += PAGE_SIZE) {
1028 /* check to see if we've left 'vma' behind
1029 * and need a new, higher one */
1030 if (vma && (addr >= vma->vm_end)) {
1031 vma = find_vma(walk->mm, addr);
1032 if (vma && (vma->vm_flags & VM_SOFTDIRTY))
1033 flags2 = __PM_SOFT_DIRTY;
1036 pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
1039 /* check that 'vma' actually covers this address,
1040 * and that it isn't a huge page vma */
1041 if (vma && (vma->vm_start <= addr) &&
1042 !is_vm_hugetlb_page(vma)) {
1043 pte = pte_offset_map(pmd, addr);
1044 pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
1045 /* unmap before userspace copy */
1048 err = add_to_pagemap(addr, &pme, pm);
1058 #ifdef CONFIG_HUGETLB_PAGE
1059 static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
1060 pte_t pte, int offset, int flags2)
1062 if (pte_present(pte))
1063 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) |
1064 PM_STATUS2(pm->v2, flags2) |
1067 *pme = make_pme(PM_NOT_PRESENT(pm->v2) |
1068 PM_STATUS2(pm->v2, flags2));
1071 /* This function walks within one hugetlb entry in the single call */
1072 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
1073 unsigned long addr, unsigned long end,
1074 struct mm_walk *walk)
1076 struct pagemapread *pm = walk->private;
1077 struct vm_area_struct *vma;
1080 pagemap_entry_t pme;
1082 vma = find_vma(walk->mm, addr);
1085 if (vma && (vma->vm_flags & VM_SOFTDIRTY))
1086 flags2 = __PM_SOFT_DIRTY;
1090 for (; addr != end; addr += PAGE_SIZE) {
1091 int offset = (addr & ~hmask) >> PAGE_SHIFT;
1092 huge_pte_to_pagemap_entry(&pme, pm, *pte, offset, flags2);
1093 err = add_to_pagemap(addr, &pme, pm);
1102 #endif /* HUGETLB_PAGE */
1105 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1107 * For each page in the address space, this file contains one 64-bit entry
1108 * consisting of the following:
1110 * Bits 0-54 page frame number (PFN) if present
1111 * Bits 0-4 swap type if swapped
1112 * Bits 5-54 swap offset if swapped
1113 * Bits 55-60 page shift (page size = 1<<page shift)
1114 * Bit 61 page is file-page or shared-anon
1115 * Bit 62 page swapped
1116 * Bit 63 page present
1118 * If the page is not present but in swap, then the PFN contains an
1119 * encoding of the swap file number and the page's offset into the
1120 * swap. Unmapped pages return a null PFN. This allows determining
1121 * precisely which pages are mapped (or in swap) and comparing mapped
1122 * pages between processes.
1124 * Efficient users of this interface will use /proc/pid/maps to
1125 * determine which areas of memory are actually mapped and llseek to
1126 * skip over unmapped regions.
1128 static ssize_t pagemap_read(struct file *file, char __user *buf,
1129 size_t count, loff_t *ppos)
1131 struct task_struct *task = get_proc_task(file_inode(file));
1132 struct mm_struct *mm;
1133 struct pagemapread pm;
1135 struct mm_walk pagemap_walk = {};
1137 unsigned long svpfn;
1138 unsigned long start_vaddr;
1139 unsigned long end_vaddr;
1146 /* file position must be aligned */
1147 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1154 pm.v2 = soft_dirty_cleared;
1155 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1156 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1161 mm = mm_access(task, PTRACE_MODE_READ);
1163 if (!mm || IS_ERR(mm))
1166 pagemap_walk.pmd_entry = pagemap_pte_range;
1167 pagemap_walk.pte_hole = pagemap_pte_hole;
1168 #ifdef CONFIG_HUGETLB_PAGE
1169 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1171 pagemap_walk.mm = mm;
1172 pagemap_walk.private = ±
1175 svpfn = src / PM_ENTRY_BYTES;
1176 start_vaddr = svpfn << PAGE_SHIFT;
1177 end_vaddr = TASK_SIZE_OF(task);
1179 /* watch out for wraparound */
1180 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1181 start_vaddr = end_vaddr;
1184 * The odds are that this will stop walking way
1185 * before end_vaddr, because the length of the
1186 * user buffer is tracked in "pm", and the walk
1187 * will stop when we hit the end of the buffer.
1190 while (count && (start_vaddr < end_vaddr)) {
1195 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1197 if (end < start_vaddr || end > end_vaddr)
1199 down_read(&mm->mmap_sem);
1200 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1201 up_read(&mm->mmap_sem);
1204 len = min(count, PM_ENTRY_BYTES * pm.pos);
1205 if (copy_to_user(buf, pm.buffer, len)) {
1214 if (!ret || ret == PM_END_OF_BUFFER)
1222 put_task_struct(task);
1227 static int pagemap_open(struct inode *inode, struct file *file)
1229 pr_warn_once("Bits 55-60 of /proc/PID/pagemap entries are about "
1230 "to stop being page-shift some time soon. See the "
1231 "linux/Documentation/vm/pagemap.txt for details.\n");
1235 const struct file_operations proc_pagemap_operations = {
1236 .llseek = mem_lseek, /* borrow this */
1237 .read = pagemap_read,
1238 .open = pagemap_open,
1240 #endif /* CONFIG_PROC_PAGE_MONITOR */
1245 struct vm_area_struct *vma;
1246 unsigned long pages;
1248 unsigned long active;
1249 unsigned long writeback;
1250 unsigned long mapcount_max;
1251 unsigned long dirty;
1252 unsigned long swapcache;
1253 unsigned long node[MAX_NUMNODES];
1256 struct numa_maps_private {
1257 struct proc_maps_private proc_maps;
1258 struct numa_maps md;
1261 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1262 unsigned long nr_pages)
1264 int count = page_mapcount(page);
1266 md->pages += nr_pages;
1267 if (pte_dirty || PageDirty(page))
1268 md->dirty += nr_pages;
1270 if (PageSwapCache(page))
1271 md->swapcache += nr_pages;
1273 if (PageActive(page) || PageUnevictable(page))
1274 md->active += nr_pages;
1276 if (PageWriteback(page))
1277 md->writeback += nr_pages;
1280 md->anon += nr_pages;
1282 if (count > md->mapcount_max)
1283 md->mapcount_max = count;
1285 md->node[page_to_nid(page)] += nr_pages;
1288 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1294 if (!pte_present(pte))
1297 page = vm_normal_page(vma, addr, pte);
1301 if (PageReserved(page))
1304 nid = page_to_nid(page);
1305 if (!node_isset(nid, node_states[N_MEMORY]))
1311 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1312 unsigned long end, struct mm_walk *walk)
1314 struct numa_maps *md;
1321 if (pmd_trans_huge_lock(pmd, md->vma, &ptl) == 1) {
1322 pte_t huge_pte = *(pte_t *)pmd;
1325 page = can_gather_numa_stats(huge_pte, md->vma, addr);
1327 gather_stats(page, md, pte_dirty(huge_pte),
1328 HPAGE_PMD_SIZE/PAGE_SIZE);
1333 if (pmd_trans_unstable(pmd))
1335 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1337 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
1340 gather_stats(page, md, pte_dirty(*pte), 1);
1342 } while (pte++, addr += PAGE_SIZE, addr != end);
1343 pte_unmap_unlock(orig_pte, ptl);
1346 #ifdef CONFIG_HUGETLB_PAGE
1347 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1348 unsigned long addr, unsigned long end, struct mm_walk *walk)
1350 struct numa_maps *md;
1356 page = pte_page(*pte);
1361 gather_stats(page, md, pte_dirty(*pte), 1);
1366 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1367 unsigned long addr, unsigned long end, struct mm_walk *walk)
1374 * Display pages allocated per node and memory policy via /proc.
1376 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1378 struct numa_maps_private *numa_priv = m->private;
1379 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1380 struct vm_area_struct *vma = v;
1381 struct numa_maps *md = &numa_priv->md;
1382 struct file *file = vma->vm_file;
1383 struct task_struct *task = proc_priv->task;
1384 struct mm_struct *mm = vma->vm_mm;
1385 struct mm_walk walk = {};
1386 struct mempolicy *pol;
1393 /* Ensure we start with an empty set of numa_maps statistics. */
1394 memset(md, 0, sizeof(*md));
1398 walk.hugetlb_entry = gather_hugetbl_stats;
1399 walk.pmd_entry = gather_pte_stats;
1403 pol = get_vma_policy(task, vma, vma->vm_start);
1404 mpol_to_str(buffer, sizeof(buffer), pol);
1407 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1410 seq_printf(m, " file=");
1411 seq_path(m, &file->f_path, "\n\t= ");
1412 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1413 seq_printf(m, " heap");
1415 pid_t tid = vm_is_stack(task, vma, is_pid);
1418 * Thread stack in /proc/PID/task/TID/maps or
1419 * the main process stack.
1421 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1422 vma->vm_end >= mm->start_stack))
1423 seq_printf(m, " stack");
1425 seq_printf(m, " stack:%d", tid);
1429 if (is_vm_hugetlb_page(vma))
1430 seq_printf(m, " huge");
1432 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1438 seq_printf(m, " anon=%lu", md->anon);
1441 seq_printf(m, " dirty=%lu", md->dirty);
1443 if (md->pages != md->anon && md->pages != md->dirty)
1444 seq_printf(m, " mapped=%lu", md->pages);
1446 if (md->mapcount_max > 1)
1447 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1450 seq_printf(m, " swapcache=%lu", md->swapcache);
1452 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1453 seq_printf(m, " active=%lu", md->active);
1456 seq_printf(m, " writeback=%lu", md->writeback);
1458 for_each_node_state(nid, N_MEMORY)
1460 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1464 if (m->count < m->size)
1465 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1469 static int show_pid_numa_map(struct seq_file *m, void *v)
1471 return show_numa_map(m, v, 1);
1474 static int show_tid_numa_map(struct seq_file *m, void *v)
1476 return show_numa_map(m, v, 0);
1479 static const struct seq_operations proc_pid_numa_maps_op = {
1483 .show = show_pid_numa_map,
1486 static const struct seq_operations proc_tid_numa_maps_op = {
1490 .show = show_tid_numa_map,
1493 static int numa_maps_open(struct inode *inode, struct file *file,
1494 const struct seq_operations *ops)
1496 struct numa_maps_private *priv;
1498 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1500 priv->proc_maps.pid = proc_pid(inode);
1501 ret = seq_open(file, ops);
1503 struct seq_file *m = file->private_data;
1512 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1514 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1517 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1519 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1522 const struct file_operations proc_pid_numa_maps_operations = {
1523 .open = pid_numa_maps_open,
1525 .llseek = seq_lseek,
1526 .release = seq_release_private,
1529 const struct file_operations proc_tid_numa_maps_operations = {
1530 .open = tid_numa_maps_open,
1532 .llseek = seq_lseek,
1533 .release = seq_release_private,
1535 #endif /* CONFIG_NUMA */