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/security.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 - mm->reserved_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)*mm->nr_ptes) >> 10,
66 swap << (PAGE_SHIFT-10));
69 unsigned long task_vsize(struct mm_struct *mm)
71 return PAGE_SIZE * mm->total_vm;
74 unsigned long task_statm(struct mm_struct *mm,
75 unsigned long *shared, unsigned long *text,
76 unsigned long *data, unsigned long *resident)
78 *shared = get_mm_counter(mm, MM_FILEPAGES);
79 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
81 *data = mm->total_vm - mm->shared_vm;
82 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
86 static void pad_len_spaces(struct seq_file *m, int len)
88 len = 25 + sizeof(void*) * 6 - len;
91 seq_printf(m, "%*c", len, ' ');
94 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
96 if (vma && vma != priv->tail_vma) {
97 struct mm_struct *mm = vma->vm_mm;
98 up_read(&mm->mmap_sem);
103 static void *m_start(struct seq_file *m, loff_t *pos)
105 struct proc_maps_private *priv = m->private;
106 unsigned long last_addr = m->version;
107 struct mm_struct *mm;
108 struct vm_area_struct *vma, *tail_vma = NULL;
111 /* Clear the per syscall fields in priv */
113 priv->tail_vma = NULL;
116 * We remember last_addr rather than next_addr to hit with
117 * mmap_cache most of the time. We have zero last_addr at
118 * the beginning and also after lseek. We will have -1 last_addr
119 * after the end of the vmas.
122 if (last_addr == -1UL)
125 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
127 return ERR_PTR(-ESRCH);
129 mm = mm_for_maps(priv->task);
130 if (!mm || IS_ERR(mm))
132 down_read(&mm->mmap_sem);
134 tail_vma = get_gate_vma(priv->task->mm);
135 priv->tail_vma = tail_vma;
137 /* Start with last addr hint */
138 vma = find_vma(mm, last_addr);
139 if (last_addr && vma) {
145 * Check the vma index is within the range and do
146 * sequential scan until m_index.
149 if ((unsigned long)l < mm->map_count) {
156 if (l != mm->map_count)
157 tail_vma = NULL; /* After gate vma */
163 /* End of vmas has been reached */
164 m->version = (tail_vma != NULL)? 0: -1UL;
165 up_read(&mm->mmap_sem);
170 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
172 struct proc_maps_private *priv = m->private;
173 struct vm_area_struct *vma = v;
174 struct vm_area_struct *tail_vma = priv->tail_vma;
177 if (vma && (vma != tail_vma) && vma->vm_next)
180 return (vma != tail_vma)? tail_vma: NULL;
183 static void m_stop(struct seq_file *m, void *v)
185 struct proc_maps_private *priv = m->private;
186 struct vm_area_struct *vma = v;
191 put_task_struct(priv->task);
194 static int do_maps_open(struct inode *inode, struct file *file,
195 const struct seq_operations *ops)
197 struct proc_maps_private *priv;
199 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
201 priv->pid = proc_pid(inode);
202 ret = seq_open(file, ops);
204 struct seq_file *m = file->private_data;
214 static void show_arm_map_vma(struct seq_file *m, struct vm_area_struct *vma)
216 static const char *cache_attrs4[4] = { "noC", "WB-WA", "WT-noWA", "WB-noWA" };
217 struct mm_struct *mm = vma->vm_mm;
218 struct file *file = vma->vm_file;
219 vm_flags_t flags = vma->vm_flags;
220 unsigned long start, end, end_b;
221 const char *name = NULL;
240 name = arch_vma_name(vma);
242 if (vma->vm_start <= mm->brk &&
243 vma->vm_end >= mm->start_brk) {
245 } else if (vma->vm_start <= mm->start_stack &&
246 vma->vm_end >= mm->start_stack) {
252 arm_pgd = (u32 *)mm->pgd;
254 asm ("mrc p15, 0, %0, c1, c0, 0" : "=r"(control));
255 asm ("mrc p15, 0, %0, c10, c2, 0" : "=r"(prrr)); // primary region RR
256 asm ("mrc p15, 0, %0, c10, c2, 1" : "=r"(nmrr)); // normal memory RR
258 start = vma->vm_start;
261 while (start < end) {
264 desc1 = arm_pgd[start >> 20];
266 end_b = (start & ~0xfffff) + 0x100000;
267 for (; end_b < end; end_b += 0x100000)
268 if ((arm_pgd[end_b >> 20] ^ desc1) & 0xfffff)
273 sprintf(buf, "l1_fault");
278 tex_cb = ((desc1 >> 2) & 0x03) | ((desc1 >> 10) & 0x1c);
279 s = (desc1 >> 16) & 1;
280 xn = (desc1 >> 4) & 1;
281 ap = ((desc1 >> 10) & 3) | ((desc1 >> 13) & 4);
282 desc_type = (desc1 & (1 << 18)) ? 's' : 'h';
285 sprintf(buf, "reserved");
289 cpt = __va(desc1 & 0xfffffc00);
290 desc2 = cpt[(start >> 12) & 0xff];
294 for (end_b = start + 0x1000; end_b < end; end_b += 0x1000) {
295 if ((end_b & 0x000ff000) == 0) {
296 cpt_e = __va(arm_pgd[end_b >> 20] & 0xfffffc00);
297 if ((arm_pgd[end_b >> 20] ^ desc1) & 0x3ff)
301 // assume small pages
302 if ((cpt_e[(end_b >> 12) & 0xff] ^ desc2) & 0xfff)
308 sprintf(buf, "l2_fault");
311 tex_cb = ((desc2 >> 2) & 0x03) | ((desc2 >> 10) & 0x1c);
312 s = (desc2 >> 10) & 1;
313 xn = (desc2 >> 15) & 1;
314 ap = ((desc2 >> 4) & 3) | ((desc2 >> 7) & 4);
318 tex_cb = ((desc2 >> 2) & 0x03) | ((desc2 >> 4) & 0x1c);
319 s = (desc2 >> 10) & 1;
321 ap = ((desc2 >> 4) & 3) | ((desc2 >> 7) & 4);
326 if (control & (1 << 28)) { // TEX remap
327 // S (shareable) bit remapping
328 char s_normal[2] = { (prrr >> 18) & 1, (prrr >> 19) & 1 };
329 char s_device[2] = { (prrr >> 16) & 1, (prrr >> 17) & 1 };
333 type = (prrr >> tex_cb * 2) & 3;
336 sprintf(buf, "strongly-ordered");
339 sprintf(buf, "device");
343 sprintf(buf, "reserved/normal");
346 sprintf(buf + strlen(buf), "inner-%s-outer-%s",
347 cache_attrs4[(nmrr >> tex_cb * 2) & 3],
348 cache_attrs4[(nmrr >> (tex_cb * 2 + 16)) & 3]);
351 else if (tex_cb & 0x10) { // TEX[2] set
352 sprintf(buf, "inner-%s-outer-%s",
353 cache_attrs4[tex_cb & 3], cache_attrs4[(tex_cb >> 2) & 3]);
357 case 0x00: sprintf(buf, "strongly-ordered"); s = 1; break;
358 case 0x01: sprintf(buf, "shareable-device"); s = 1; break;
359 case 0x02: sprintf(buf, "inner-outer-WT-noWA"); break;
360 case 0x03: sprintf(buf, "inner-outer-WB-noWA"); break;
361 case 0x04: sprintf(buf, "inner-outer-non-cacheable"); break;
362 case 0x06: sprintf(buf, "implementation-defined"); break;
363 case 0x07: sprintf(buf, "inner-outer-WB-WA"); break;
364 case 0x08: sprintf(buf, "non-shareable-device"); s = 0; break;
365 default: sprintf(buf, "reserved"); break;
370 sprintf(buf + strlen(buf), "-shareable");
373 // use user permissions here
374 if (control & (1 << 29)) // AFE
375 sprintf(rw, "%c%c", (ap & 2) ? 'r' : '-',
376 ((ap & 2) && !(ap & 4)) ? 'w' : '-');
378 sprintf(rw, "%c%c", (ap & 2) ? 'r' : '-',
379 (ap == 3) ? 'w' : '-');
381 seq_printf(m, "%08lx-%08lx %s%c%c%c %-28s %n",
385 flags & VM_MAYSHARE ? 's' : 'p',
390 pad_len_spaces(m, len);
391 seq_path(m, &file->f_path, "\n");
393 pad_len_spaces(m, len);
403 static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
405 struct mm_struct *mm = vma->vm_mm;
406 struct file *file = vma->vm_file;
407 vm_flags_t flags = vma->vm_flags;
408 unsigned long ino = 0;
409 unsigned long long pgoff = 0;
410 unsigned long start, end;
415 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
416 dev = inode->i_sb->s_dev;
418 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
421 /* We don't show the stack guard page in /proc/maps */
422 start = vma->vm_start;
423 if (stack_guard_page_start(vma, start))
426 if (stack_guard_page_end(vma, end))
429 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
432 flags & VM_READ ? 'r' : '-',
433 flags & VM_WRITE ? 'w' : '-',
434 flags & VM_EXEC ? 'x' : '-',
435 flags & VM_MAYSHARE ? 's' : 'p',
437 MAJOR(dev), MINOR(dev), ino, &len);
440 * Print the dentry name for named mappings, and a
441 * special [heap] marker for the heap:
444 pad_len_spaces(m, len);
445 seq_path(m, &file->f_path, "\n");
447 const char *name = arch_vma_name(vma);
450 if (vma->vm_start <= mm->brk &&
451 vma->vm_end >= mm->start_brk) {
453 } else if (vma->vm_start <= mm->start_stack &&
454 vma->vm_end >= mm->start_stack) {
462 pad_len_spaces(m, len);
469 static int show_map(struct seq_file *m, void *v)
471 struct vm_area_struct *vma = v;
472 struct proc_maps_private *priv = m->private;
473 struct task_struct *task = priv->task;
475 show_map_vma(m, vma);
477 if (m->count < m->size) /* vma is copied successfully */
478 m->version = (vma != get_gate_vma(task->mm))
483 static const struct seq_operations proc_pid_maps_op = {
490 static int maps_open(struct inode *inode, struct file *file)
492 return do_maps_open(inode, file, &proc_pid_maps_op);
495 const struct file_operations proc_maps_operations = {
499 .release = seq_release_private,
503 static int show_armv7_map(struct seq_file *m, void *v)
505 struct vm_area_struct *vma = v;
506 struct proc_maps_private *priv = m->private;
507 struct task_struct *task = priv->task;
509 show_arm_map_vma(m, vma);
511 if (m->count < m->size) /* vma is copied successfully */
512 m->version = (vma != get_gate_vma(task->mm))
517 static const struct seq_operations proc_pid_armv7_maps_op = {
521 .show = show_armv7_map
525 static int armv7_maps_open(struct inode *inode, struct file *file)
527 return do_maps_open(inode, file, &proc_pid_armv7_maps_op);
530 const struct file_operations proc_armv7_maps_operations = {
531 .open = armv7_maps_open,
534 .release = seq_release_private,
538 * Proportional Set Size(PSS): my share of RSS.
540 * PSS of a process is the count of pages it has in memory, where each
541 * page is divided by the number of processes sharing it. So if a
542 * process has 1000 pages all to itself, and 1000 shared with one other
543 * process, its PSS will be 1500.
545 * To keep (accumulated) division errors low, we adopt a 64bit
546 * fixed-point pss counter to minimize division errors. So (pss >>
547 * PSS_SHIFT) would be the real byte count.
549 * A shift of 12 before division means (assuming 4K page size):
550 * - 1M 3-user-pages add up to 8KB errors;
551 * - supports mapcount up to 2^24, or 16M;
552 * - supports PSS up to 2^52 bytes, or 4PB.
556 #ifdef CONFIG_PROC_PAGE_MONITOR
557 struct mem_size_stats {
558 struct vm_area_struct *vma;
559 unsigned long resident;
560 unsigned long shared_clean;
561 unsigned long shared_dirty;
562 unsigned long private_clean;
563 unsigned long private_dirty;
564 unsigned long referenced;
565 unsigned long anonymous;
566 unsigned long anonymous_thp;
572 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
573 unsigned long ptent_size, struct mm_walk *walk)
575 struct mem_size_stats *mss = walk->private;
576 struct vm_area_struct *vma = mss->vma;
580 if (is_swap_pte(ptent)) {
581 mss->swap += ptent_size;
585 if (!pte_present(ptent))
588 page = vm_normal_page(vma, addr, ptent);
593 mss->anonymous += ptent_size;
595 mss->resident += ptent_size;
596 /* Accumulate the size in pages that have been accessed. */
597 if (pte_young(ptent) || PageReferenced(page))
598 mss->referenced += ptent_size;
599 mapcount = page_mapcount(page);
601 if (pte_dirty(ptent) || PageDirty(page))
602 mss->shared_dirty += ptent_size;
604 mss->shared_clean += ptent_size;
605 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
607 if (pte_dirty(ptent) || PageDirty(page))
608 mss->private_dirty += ptent_size;
610 mss->private_clean += ptent_size;
611 mss->pss += (ptent_size << PSS_SHIFT);
615 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
616 struct mm_walk *walk)
618 struct mem_size_stats *mss = walk->private;
619 struct vm_area_struct *vma = mss->vma;
623 spin_lock(&walk->mm->page_table_lock);
624 if (pmd_trans_huge(*pmd)) {
625 if (pmd_trans_splitting(*pmd)) {
626 spin_unlock(&walk->mm->page_table_lock);
627 wait_split_huge_page(vma->anon_vma, pmd);
629 smaps_pte_entry(*(pte_t *)pmd, addr,
630 HPAGE_PMD_SIZE, walk);
631 spin_unlock(&walk->mm->page_table_lock);
632 mss->anonymous_thp += HPAGE_PMD_SIZE;
636 spin_unlock(&walk->mm->page_table_lock);
639 if (pmd_trans_unstable(pmd))
642 * The mmap_sem held all the way back in m_start() is what
643 * keeps khugepaged out of here and from collapsing things
646 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
647 for (; addr != end; pte++, addr += PAGE_SIZE)
648 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
649 pte_unmap_unlock(pte - 1, ptl);
654 static int show_smap(struct seq_file *m, void *v)
656 struct proc_maps_private *priv = m->private;
657 struct task_struct *task = priv->task;
658 struct vm_area_struct *vma = v;
659 struct mem_size_stats mss;
660 struct mm_walk smaps_walk = {
661 .pmd_entry = smaps_pte_range,
666 memset(&mss, 0, sizeof mss);
668 /* mmap_sem is held in m_start */
669 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
670 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
672 show_map_vma(m, vma);
678 "Shared_Clean: %8lu kB\n"
679 "Shared_Dirty: %8lu kB\n"
680 "Private_Clean: %8lu kB\n"
681 "Private_Dirty: %8lu kB\n"
682 "Referenced: %8lu kB\n"
683 "Anonymous: %8lu kB\n"
684 "AnonHugePages: %8lu kB\n"
686 "KernelPageSize: %8lu kB\n"
687 "MMUPageSize: %8lu kB\n"
689 (vma->vm_end - vma->vm_start) >> 10,
691 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
692 mss.shared_clean >> 10,
693 mss.shared_dirty >> 10,
694 mss.private_clean >> 10,
695 mss.private_dirty >> 10,
696 mss.referenced >> 10,
698 mss.anonymous_thp >> 10,
700 vma_kernel_pagesize(vma) >> 10,
701 vma_mmu_pagesize(vma) >> 10,
702 (vma->vm_flags & VM_LOCKED) ?
703 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
705 if (m->count < m->size) /* vma is copied successfully */
706 m->version = (vma != get_gate_vma(task->mm))
711 static const struct seq_operations proc_pid_smaps_op = {
718 static int smaps_open(struct inode *inode, struct file *file)
720 return do_maps_open(inode, file, &proc_pid_smaps_op);
723 const struct file_operations proc_smaps_operations = {
727 .release = seq_release_private,
730 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
731 unsigned long end, struct mm_walk *walk)
733 struct vm_area_struct *vma = walk->private;
738 split_huge_page_pmd(walk->mm, pmd);
739 if (pmd_trans_unstable(pmd))
742 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
743 for (; addr != end; pte++, addr += PAGE_SIZE) {
745 if (!pte_present(ptent))
748 page = vm_normal_page(vma, addr, ptent);
752 if (PageReserved(page))
755 /* Clear accessed and referenced bits. */
756 ptep_test_and_clear_young(vma, addr, pte);
757 ClearPageReferenced(page);
759 pte_unmap_unlock(pte - 1, ptl);
764 #define CLEAR_REFS_ALL 1
765 #define CLEAR_REFS_ANON 2
766 #define CLEAR_REFS_MAPPED 3
768 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
769 size_t count, loff_t *ppos)
771 struct task_struct *task;
772 char buffer[PROC_NUMBUF];
773 struct mm_struct *mm;
774 struct vm_area_struct *vma;
778 memset(buffer, 0, sizeof(buffer));
779 if (count > sizeof(buffer) - 1)
780 count = sizeof(buffer) - 1;
781 if (copy_from_user(buffer, buf, count))
783 rv = kstrtoint(strstrip(buffer), 10, &type);
786 if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
788 task = get_proc_task(file->f_path.dentry->d_inode);
791 mm = get_task_mm(task);
793 struct mm_walk clear_refs_walk = {
794 .pmd_entry = clear_refs_pte_range,
797 down_read(&mm->mmap_sem);
798 for (vma = mm->mmap; vma; vma = vma->vm_next) {
799 clear_refs_walk.private = vma;
800 if (is_vm_hugetlb_page(vma))
803 * Writing 1 to /proc/pid/clear_refs affects all pages.
805 * Writing 2 to /proc/pid/clear_refs only affects
808 * Writing 3 to /proc/pid/clear_refs only affects file
811 if (type == CLEAR_REFS_ANON && vma->vm_file)
813 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
815 walk_page_range(vma->vm_start, vma->vm_end,
819 up_read(&mm->mmap_sem);
822 put_task_struct(task);
827 const struct file_operations proc_clear_refs_operations = {
828 .write = clear_refs_write,
829 .llseek = noop_llseek,
833 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
838 #define PM_ENTRY_BYTES sizeof(u64)
839 #define PM_STATUS_BITS 3
840 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
841 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
842 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
843 #define PM_PSHIFT_BITS 6
844 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
845 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
846 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
847 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
848 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
850 #define PM_PRESENT PM_STATUS(4LL)
851 #define PM_SWAP PM_STATUS(2LL)
852 #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
853 #define PM_END_OF_BUFFER 1
855 static int add_to_pagemap(unsigned long addr, u64 pfn,
856 struct pagemapread *pm)
858 pm->buffer[pm->pos++] = pfn;
859 if (pm->pos >= pm->len)
860 return PM_END_OF_BUFFER;
864 static int pagemap_pte_hole(unsigned long start, unsigned long end,
865 struct mm_walk *walk)
867 struct pagemapread *pm = walk->private;
870 for (addr = start; addr < end; addr += PAGE_SIZE) {
871 err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
878 static u64 swap_pte_to_pagemap_entry(pte_t pte)
880 swp_entry_t e = pte_to_swp_entry(pte);
881 return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
884 static u64 pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte)
887 if (is_swap_pte(pte))
888 pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
889 | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
890 else if (pte_present(pte))
891 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte)) : 0)
892 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
896 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
897 struct mm_walk *walk)
899 struct vm_area_struct *vma;
900 struct pagemapread *pm = walk->private;
904 split_huge_page_pmd(walk->mm, pmd);
905 if (pmd_trans_unstable(pmd))
908 /* find the first VMA at or above 'addr' */
909 vma = find_vma(walk->mm, addr);
910 for (; addr != end; addr += PAGE_SIZE) {
911 u64 pfn = PM_NOT_PRESENT;
913 /* check to see if we've left 'vma' behind
914 * and need a new, higher one */
915 if (vma && (addr >= vma->vm_end))
916 vma = find_vma(walk->mm, addr);
918 /* check that 'vma' actually covers this address,
919 * and that it isn't a huge page vma */
920 if (vma && (vma->vm_start <= addr) &&
921 !is_vm_hugetlb_page(vma)) {
922 pte = pte_offset_map(pmd, addr);
923 pfn = pte_to_pagemap_entry(pm, *pte);
924 /* unmap before userspace copy */
927 err = add_to_pagemap(addr, pfn, pm);
937 #ifdef CONFIG_HUGETLB_PAGE
938 static u64 huge_pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte, int offset)
941 if (pte_present(pte))
942 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte) + offset) : 0)
943 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
947 /* This function walks within one hugetlb entry in the single call */
948 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
949 unsigned long addr, unsigned long end,
950 struct mm_walk *walk)
952 struct pagemapread *pm = walk->private;
956 for (; addr != end; addr += PAGE_SIZE) {
957 int offset = (addr & ~hmask) >> PAGE_SHIFT;
958 pfn = huge_pte_to_pagemap_entry(pm, *pte, offset);
959 err = add_to_pagemap(addr, pfn, pm);
968 #endif /* HUGETLB_PAGE */
971 * /proc/pid/pagemap - an array mapping virtual pages to pfns
973 * For each page in the address space, this file contains one 64-bit entry
974 * consisting of the following:
976 * Bits 0-55 page frame number (PFN) if present
977 * Bits 0-4 swap type if swapped
978 * Bits 5-55 swap offset if swapped
979 * Bits 55-60 page shift (page size = 1<<page shift)
980 * Bit 61 reserved for future use
981 * Bit 62 page swapped
982 * Bit 63 page present
984 * If the page is not present but in swap, then the PFN contains an
985 * encoding of the swap file number and the page's offset into the
986 * swap. Unmapped pages return a null PFN. This allows determining
987 * precisely which pages are mapped (or in swap) and comparing mapped
988 * pages between processes.
990 * Efficient users of this interface will use /proc/pid/maps to
991 * determine which areas of memory are actually mapped and llseek to
992 * skip over unmapped regions.
994 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
995 #define PAGEMAP_WALK_MASK (PMD_MASK)
996 static ssize_t pagemap_read(struct file *file, char __user *buf,
997 size_t count, loff_t *ppos)
999 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1000 struct mm_struct *mm;
1001 struct pagemapread pm;
1003 struct mm_walk pagemap_walk = {};
1005 unsigned long svpfn;
1006 unsigned long start_vaddr;
1007 unsigned long end_vaddr;
1014 /* file position must be aligned */
1015 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1022 /* do not disclose physical addresses: attack vector */
1023 pm.show_pfn = !security_capable(&init_user_ns, file->f_cred,
1026 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1027 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1032 mm = mm_for_maps(task);
1034 if (!mm || IS_ERR(mm))
1037 pagemap_walk.pmd_entry = pagemap_pte_range;
1038 pagemap_walk.pte_hole = pagemap_pte_hole;
1039 #ifdef CONFIG_HUGETLB_PAGE
1040 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1042 pagemap_walk.mm = mm;
1043 pagemap_walk.private = ±
1046 svpfn = src / PM_ENTRY_BYTES;
1047 start_vaddr = svpfn << PAGE_SHIFT;
1048 end_vaddr = TASK_SIZE_OF(task);
1050 /* watch out for wraparound */
1051 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1052 start_vaddr = end_vaddr;
1055 * The odds are that this will stop walking way
1056 * before end_vaddr, because the length of the
1057 * user buffer is tracked in "pm", and the walk
1058 * will stop when we hit the end of the buffer.
1061 while (count && (start_vaddr < end_vaddr)) {
1066 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1068 if (end < start_vaddr || end > end_vaddr)
1070 down_read(&mm->mmap_sem);
1071 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1072 up_read(&mm->mmap_sem);
1075 len = min(count, PM_ENTRY_BYTES * pm.pos);
1076 if (copy_to_user(buf, pm.buffer, len)) {
1085 if (!ret || ret == PM_END_OF_BUFFER)
1093 put_task_struct(task);
1098 const struct file_operations proc_pagemap_operations = {
1099 .llseek = mem_lseek, /* borrow this */
1100 .read = pagemap_read,
1102 #endif /* CONFIG_PROC_PAGE_MONITOR */
1107 struct vm_area_struct *vma;
1108 unsigned long pages;
1110 unsigned long active;
1111 unsigned long writeback;
1112 unsigned long mapcount_max;
1113 unsigned long dirty;
1114 unsigned long swapcache;
1115 unsigned long node[MAX_NUMNODES];
1118 struct numa_maps_private {
1119 struct proc_maps_private proc_maps;
1120 struct numa_maps md;
1123 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1124 unsigned long nr_pages)
1126 int count = page_mapcount(page);
1128 md->pages += nr_pages;
1129 if (pte_dirty || PageDirty(page))
1130 md->dirty += nr_pages;
1132 if (PageSwapCache(page))
1133 md->swapcache += nr_pages;
1135 if (PageActive(page) || PageUnevictable(page))
1136 md->active += nr_pages;
1138 if (PageWriteback(page))
1139 md->writeback += nr_pages;
1142 md->anon += nr_pages;
1144 if (count > md->mapcount_max)
1145 md->mapcount_max = count;
1147 md->node[page_to_nid(page)] += nr_pages;
1150 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1156 if (!pte_present(pte))
1159 page = vm_normal_page(vma, addr, pte);
1163 if (PageReserved(page))
1166 nid = page_to_nid(page);
1167 if (!node_isset(nid, node_states[N_HIGH_MEMORY]))
1173 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1174 unsigned long end, struct mm_walk *walk)
1176 struct numa_maps *md;
1182 spin_lock(&walk->mm->page_table_lock);
1183 if (pmd_trans_huge(*pmd)) {
1184 if (pmd_trans_splitting(*pmd)) {
1185 spin_unlock(&walk->mm->page_table_lock);
1186 wait_split_huge_page(md->vma->anon_vma, pmd);
1188 pte_t huge_pte = *(pte_t *)pmd;
1191 page = can_gather_numa_stats(huge_pte, md->vma, addr);
1193 gather_stats(page, md, pte_dirty(huge_pte),
1194 HPAGE_PMD_SIZE/PAGE_SIZE);
1195 spin_unlock(&walk->mm->page_table_lock);
1199 spin_unlock(&walk->mm->page_table_lock);
1202 if (pmd_trans_unstable(pmd))
1204 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1206 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
1209 gather_stats(page, md, pte_dirty(*pte), 1);
1211 } while (pte++, addr += PAGE_SIZE, addr != end);
1212 pte_unmap_unlock(orig_pte, ptl);
1215 #ifdef CONFIG_HUGETLB_PAGE
1216 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1217 unsigned long addr, unsigned long end, struct mm_walk *walk)
1219 struct numa_maps *md;
1225 page = pte_page(*pte);
1230 gather_stats(page, md, pte_dirty(*pte), 1);
1235 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1236 unsigned long addr, unsigned long end, struct mm_walk *walk)
1243 * Display pages allocated per node and memory policy via /proc.
1245 static int show_numa_map(struct seq_file *m, void *v)
1247 struct numa_maps_private *numa_priv = m->private;
1248 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1249 struct vm_area_struct *vma = v;
1250 struct numa_maps *md = &numa_priv->md;
1251 struct file *file = vma->vm_file;
1252 struct mm_struct *mm = vma->vm_mm;
1253 struct mm_walk walk = {};
1254 struct mempolicy *pol;
1261 /* Ensure we start with an empty set of numa_maps statistics. */
1262 memset(md, 0, sizeof(*md));
1266 walk.hugetlb_entry = gather_hugetbl_stats;
1267 walk.pmd_entry = gather_pte_stats;
1271 pol = get_vma_policy(proc_priv->task, vma, vma->vm_start);
1272 mpol_to_str(buffer, sizeof(buffer), pol, 0);
1275 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1278 seq_printf(m, " file=");
1279 seq_path(m, &file->f_path, "\n\t= ");
1280 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1281 seq_printf(m, " heap");
1282 } else if (vma->vm_start <= mm->start_stack &&
1283 vma->vm_end >= mm->start_stack) {
1284 seq_printf(m, " stack");
1287 if (is_vm_hugetlb_page(vma))
1288 seq_printf(m, " huge");
1290 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1296 seq_printf(m, " anon=%lu", md->anon);
1299 seq_printf(m, " dirty=%lu", md->dirty);
1301 if (md->pages != md->anon && md->pages != md->dirty)
1302 seq_printf(m, " mapped=%lu", md->pages);
1304 if (md->mapcount_max > 1)
1305 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1308 seq_printf(m, " swapcache=%lu", md->swapcache);
1310 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1311 seq_printf(m, " active=%lu", md->active);
1314 seq_printf(m, " writeback=%lu", md->writeback);
1316 for_each_node_state(n, N_HIGH_MEMORY)
1318 seq_printf(m, " N%d=%lu", n, md->node[n]);
1322 if (m->count < m->size)
1323 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1327 static const struct seq_operations proc_pid_numa_maps_op = {
1331 .show = show_numa_map,
1334 static int numa_maps_open(struct inode *inode, struct file *file)
1336 struct numa_maps_private *priv;
1338 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1340 priv->proc_maps.pid = proc_pid(inode);
1341 ret = seq_open(file, &proc_pid_numa_maps_op);
1343 struct seq_file *m = file->private_data;
1352 const struct file_operations proc_numa_maps_operations = {
1353 .open = numa_maps_open,
1355 .llseek = seq_lseek,
1356 .release = seq_release_private,
1358 #endif /* CONFIG_NUMA */