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;
425 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
428 flags & VM_READ ? 'r' : '-',
429 flags & VM_WRITE ? 'w' : '-',
430 flags & VM_EXEC ? 'x' : '-',
431 flags & VM_MAYSHARE ? 's' : 'p',
433 MAJOR(dev), MINOR(dev), ino, &len);
436 * Print the dentry name for named mappings, and a
437 * special [heap] marker for the heap:
440 pad_len_spaces(m, len);
441 seq_path(m, &file->f_path, "\n");
443 const char *name = arch_vma_name(vma);
446 if (vma->vm_start <= mm->brk &&
447 vma->vm_end >= mm->start_brk) {
449 } else if (vma->vm_start <= mm->start_stack &&
450 vma->vm_end >= mm->start_stack) {
458 pad_len_spaces(m, len);
465 static int show_map(struct seq_file *m, void *v)
467 struct vm_area_struct *vma = v;
468 struct proc_maps_private *priv = m->private;
469 struct task_struct *task = priv->task;
471 show_map_vma(m, vma);
473 if (m->count < m->size) /* vma is copied successfully */
474 m->version = (vma != get_gate_vma(task->mm))
479 static const struct seq_operations proc_pid_maps_op = {
486 static int maps_open(struct inode *inode, struct file *file)
488 return do_maps_open(inode, file, &proc_pid_maps_op);
491 const struct file_operations proc_maps_operations = {
495 .release = seq_release_private,
499 static int show_armv7_map(struct seq_file *m, void *v)
501 struct vm_area_struct *vma = v;
502 struct proc_maps_private *priv = m->private;
503 struct task_struct *task = priv->task;
505 show_arm_map_vma(m, vma);
507 if (m->count < m->size) /* vma is copied successfully */
508 m->version = (vma != get_gate_vma(task->mm))
513 static const struct seq_operations proc_pid_armv7_maps_op = {
517 .show = show_armv7_map
521 static int armv7_maps_open(struct inode *inode, struct file *file)
523 return do_maps_open(inode, file, &proc_pid_armv7_maps_op);
526 const struct file_operations proc_armv7_maps_operations = {
527 .open = armv7_maps_open,
530 .release = seq_release_private,
534 * Proportional Set Size(PSS): my share of RSS.
536 * PSS of a process is the count of pages it has in memory, where each
537 * page is divided by the number of processes sharing it. So if a
538 * process has 1000 pages all to itself, and 1000 shared with one other
539 * process, its PSS will be 1500.
541 * To keep (accumulated) division errors low, we adopt a 64bit
542 * fixed-point pss counter to minimize division errors. So (pss >>
543 * PSS_SHIFT) would be the real byte count.
545 * A shift of 12 before division means (assuming 4K page size):
546 * - 1M 3-user-pages add up to 8KB errors;
547 * - supports mapcount up to 2^24, or 16M;
548 * - supports PSS up to 2^52 bytes, or 4PB.
552 #ifdef CONFIG_PROC_PAGE_MONITOR
553 struct mem_size_stats {
554 struct vm_area_struct *vma;
555 unsigned long resident;
556 unsigned long shared_clean;
557 unsigned long shared_dirty;
558 unsigned long private_clean;
559 unsigned long private_dirty;
560 unsigned long referenced;
561 unsigned long anonymous;
562 unsigned long anonymous_thp;
568 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
569 unsigned long ptent_size, struct mm_walk *walk)
571 struct mem_size_stats *mss = walk->private;
572 struct vm_area_struct *vma = mss->vma;
576 if (is_swap_pte(ptent)) {
577 mss->swap += ptent_size;
581 if (!pte_present(ptent))
584 page = vm_normal_page(vma, addr, ptent);
589 mss->anonymous += ptent_size;
591 mss->resident += ptent_size;
592 /* Accumulate the size in pages that have been accessed. */
593 if (pte_young(ptent) || PageReferenced(page))
594 mss->referenced += ptent_size;
595 mapcount = page_mapcount(page);
597 if (pte_dirty(ptent) || PageDirty(page))
598 mss->shared_dirty += ptent_size;
600 mss->shared_clean += ptent_size;
601 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
603 if (pte_dirty(ptent) || PageDirty(page))
604 mss->private_dirty += ptent_size;
606 mss->private_clean += ptent_size;
607 mss->pss += (ptent_size << PSS_SHIFT);
611 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
612 struct mm_walk *walk)
614 struct mem_size_stats *mss = walk->private;
615 struct vm_area_struct *vma = mss->vma;
619 spin_lock(&walk->mm->page_table_lock);
620 if (pmd_trans_huge(*pmd)) {
621 if (pmd_trans_splitting(*pmd)) {
622 spin_unlock(&walk->mm->page_table_lock);
623 wait_split_huge_page(vma->anon_vma, pmd);
625 smaps_pte_entry(*(pte_t *)pmd, addr,
626 HPAGE_PMD_SIZE, walk);
627 spin_unlock(&walk->mm->page_table_lock);
628 mss->anonymous_thp += HPAGE_PMD_SIZE;
632 spin_unlock(&walk->mm->page_table_lock);
635 if (pmd_trans_unstable(pmd))
638 * The mmap_sem held all the way back in m_start() is what
639 * keeps khugepaged out of here and from collapsing things
642 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
643 for (; addr != end; pte++, addr += PAGE_SIZE)
644 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
645 pte_unmap_unlock(pte - 1, ptl);
650 static int show_smap(struct seq_file *m, void *v)
652 struct proc_maps_private *priv = m->private;
653 struct task_struct *task = priv->task;
654 struct vm_area_struct *vma = v;
655 struct mem_size_stats mss;
656 struct mm_walk smaps_walk = {
657 .pmd_entry = smaps_pte_range,
662 memset(&mss, 0, sizeof mss);
664 /* mmap_sem is held in m_start */
665 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
666 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
668 show_map_vma(m, vma);
674 "Shared_Clean: %8lu kB\n"
675 "Shared_Dirty: %8lu kB\n"
676 "Private_Clean: %8lu kB\n"
677 "Private_Dirty: %8lu kB\n"
678 "Referenced: %8lu kB\n"
679 "Anonymous: %8lu kB\n"
680 "AnonHugePages: %8lu kB\n"
682 "KernelPageSize: %8lu kB\n"
683 "MMUPageSize: %8lu kB\n"
685 (vma->vm_end - vma->vm_start) >> 10,
687 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
688 mss.shared_clean >> 10,
689 mss.shared_dirty >> 10,
690 mss.private_clean >> 10,
691 mss.private_dirty >> 10,
692 mss.referenced >> 10,
694 mss.anonymous_thp >> 10,
696 vma_kernel_pagesize(vma) >> 10,
697 vma_mmu_pagesize(vma) >> 10,
698 (vma->vm_flags & VM_LOCKED) ?
699 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
701 if (m->count < m->size) /* vma is copied successfully */
702 m->version = (vma != get_gate_vma(task->mm))
707 static const struct seq_operations proc_pid_smaps_op = {
714 static int smaps_open(struct inode *inode, struct file *file)
716 return do_maps_open(inode, file, &proc_pid_smaps_op);
719 const struct file_operations proc_smaps_operations = {
723 .release = seq_release_private,
726 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
727 unsigned long end, struct mm_walk *walk)
729 struct vm_area_struct *vma = walk->private;
734 split_huge_page_pmd(walk->mm, pmd);
735 if (pmd_trans_unstable(pmd))
738 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
739 for (; addr != end; pte++, addr += PAGE_SIZE) {
741 if (!pte_present(ptent))
744 page = vm_normal_page(vma, addr, ptent);
748 if (PageReserved(page))
751 /* Clear accessed and referenced bits. */
752 ptep_test_and_clear_young(vma, addr, pte);
753 ClearPageReferenced(page);
755 pte_unmap_unlock(pte - 1, ptl);
760 #define CLEAR_REFS_ALL 1
761 #define CLEAR_REFS_ANON 2
762 #define CLEAR_REFS_MAPPED 3
764 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
765 size_t count, loff_t *ppos)
767 struct task_struct *task;
768 char buffer[PROC_NUMBUF];
769 struct mm_struct *mm;
770 struct vm_area_struct *vma;
774 memset(buffer, 0, sizeof(buffer));
775 if (count > sizeof(buffer) - 1)
776 count = sizeof(buffer) - 1;
777 if (copy_from_user(buffer, buf, count))
779 rv = kstrtoint(strstrip(buffer), 10, &type);
782 if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
784 task = get_proc_task(file->f_path.dentry->d_inode);
787 mm = get_task_mm(task);
789 struct mm_walk clear_refs_walk = {
790 .pmd_entry = clear_refs_pte_range,
793 down_read(&mm->mmap_sem);
794 for (vma = mm->mmap; vma; vma = vma->vm_next) {
795 clear_refs_walk.private = vma;
796 if (is_vm_hugetlb_page(vma))
799 * Writing 1 to /proc/pid/clear_refs affects all pages.
801 * Writing 2 to /proc/pid/clear_refs only affects
804 * Writing 3 to /proc/pid/clear_refs only affects file
807 if (type == CLEAR_REFS_ANON && vma->vm_file)
809 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
811 walk_page_range(vma->vm_start, vma->vm_end,
815 up_read(&mm->mmap_sem);
818 put_task_struct(task);
823 const struct file_operations proc_clear_refs_operations = {
824 .write = clear_refs_write,
825 .llseek = noop_llseek,
829 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
834 #define PM_ENTRY_BYTES sizeof(u64)
835 #define PM_STATUS_BITS 3
836 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
837 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
838 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
839 #define PM_PSHIFT_BITS 6
840 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
841 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
842 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
843 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
844 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
846 #define PM_PRESENT PM_STATUS(4LL)
847 #define PM_SWAP PM_STATUS(2LL)
848 #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
849 #define PM_END_OF_BUFFER 1
851 static int add_to_pagemap(unsigned long addr, u64 pfn,
852 struct pagemapread *pm)
854 pm->buffer[pm->pos++] = pfn;
855 if (pm->pos >= pm->len)
856 return PM_END_OF_BUFFER;
860 static int pagemap_pte_hole(unsigned long start, unsigned long end,
861 struct mm_walk *walk)
863 struct pagemapread *pm = walk->private;
866 for (addr = start; addr < end; addr += PAGE_SIZE) {
867 err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
874 static u64 swap_pte_to_pagemap_entry(pte_t pte)
876 swp_entry_t e = pte_to_swp_entry(pte);
877 return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
880 static u64 pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte)
883 if (is_swap_pte(pte))
884 pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
885 | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
886 else if (pte_present(pte))
887 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte)) : 0)
888 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
892 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
893 struct mm_walk *walk)
895 struct vm_area_struct *vma;
896 struct pagemapread *pm = walk->private;
900 split_huge_page_pmd(walk->mm, pmd);
901 if (pmd_trans_unstable(pmd))
904 /* find the first VMA at or above 'addr' */
905 vma = find_vma(walk->mm, addr);
906 for (; addr != end; addr += PAGE_SIZE) {
907 u64 pfn = PM_NOT_PRESENT;
909 /* check to see if we've left 'vma' behind
910 * and need a new, higher one */
911 if (vma && (addr >= vma->vm_end))
912 vma = find_vma(walk->mm, addr);
914 /* check that 'vma' actually covers this address,
915 * and that it isn't a huge page vma */
916 if (vma && (vma->vm_start <= addr) &&
917 !is_vm_hugetlb_page(vma)) {
918 pte = pte_offset_map(pmd, addr);
919 pfn = pte_to_pagemap_entry(pm, *pte);
920 /* unmap before userspace copy */
923 err = add_to_pagemap(addr, pfn, pm);
933 #ifdef CONFIG_HUGETLB_PAGE
934 static u64 huge_pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte, int offset)
937 if (pte_present(pte))
938 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte) + offset) : 0)
939 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
943 /* This function walks within one hugetlb entry in the single call */
944 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
945 unsigned long addr, unsigned long end,
946 struct mm_walk *walk)
948 struct pagemapread *pm = walk->private;
952 for (; addr != end; addr += PAGE_SIZE) {
953 int offset = (addr & ~hmask) >> PAGE_SHIFT;
954 pfn = huge_pte_to_pagemap_entry(pm, *pte, offset);
955 err = add_to_pagemap(addr, pfn, pm);
964 #endif /* HUGETLB_PAGE */
967 * /proc/pid/pagemap - an array mapping virtual pages to pfns
969 * For each page in the address space, this file contains one 64-bit entry
970 * consisting of the following:
972 * Bits 0-55 page frame number (PFN) if present
973 * Bits 0-4 swap type if swapped
974 * Bits 5-55 swap offset if swapped
975 * Bits 55-60 page shift (page size = 1<<page shift)
976 * Bit 61 reserved for future use
977 * Bit 62 page swapped
978 * Bit 63 page present
980 * If the page is not present but in swap, then the PFN contains an
981 * encoding of the swap file number and the page's offset into the
982 * swap. Unmapped pages return a null PFN. This allows determining
983 * precisely which pages are mapped (or in swap) and comparing mapped
984 * pages between processes.
986 * Efficient users of this interface will use /proc/pid/maps to
987 * determine which areas of memory are actually mapped and llseek to
988 * skip over unmapped regions.
990 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
991 #define PAGEMAP_WALK_MASK (PMD_MASK)
992 static ssize_t pagemap_read(struct file *file, char __user *buf,
993 size_t count, loff_t *ppos)
995 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
996 struct mm_struct *mm;
997 struct pagemapread pm;
999 struct mm_walk pagemap_walk = {};
1001 unsigned long svpfn;
1002 unsigned long start_vaddr;
1003 unsigned long end_vaddr;
1010 /* file position must be aligned */
1011 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1018 /* do not disclose physical addresses: attack vector */
1019 pm.show_pfn = !security_capable(&init_user_ns, file->f_cred,
1022 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1023 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1028 mm = mm_for_maps(task);
1030 if (!mm || IS_ERR(mm))
1033 pagemap_walk.pmd_entry = pagemap_pte_range;
1034 pagemap_walk.pte_hole = pagemap_pte_hole;
1035 #ifdef CONFIG_HUGETLB_PAGE
1036 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1038 pagemap_walk.mm = mm;
1039 pagemap_walk.private = ±
1042 svpfn = src / PM_ENTRY_BYTES;
1043 start_vaddr = svpfn << PAGE_SHIFT;
1044 end_vaddr = TASK_SIZE_OF(task);
1046 /* watch out for wraparound */
1047 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1048 start_vaddr = end_vaddr;
1051 * The odds are that this will stop walking way
1052 * before end_vaddr, because the length of the
1053 * user buffer is tracked in "pm", and the walk
1054 * will stop when we hit the end of the buffer.
1057 while (count && (start_vaddr < end_vaddr)) {
1062 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1064 if (end < start_vaddr || end > end_vaddr)
1066 down_read(&mm->mmap_sem);
1067 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1068 up_read(&mm->mmap_sem);
1071 len = min(count, PM_ENTRY_BYTES * pm.pos);
1072 if (copy_to_user(buf, pm.buffer, len)) {
1081 if (!ret || ret == PM_END_OF_BUFFER)
1089 put_task_struct(task);
1094 const struct file_operations proc_pagemap_operations = {
1095 .llseek = mem_lseek, /* borrow this */
1096 .read = pagemap_read,
1098 #endif /* CONFIG_PROC_PAGE_MONITOR */
1103 struct vm_area_struct *vma;
1104 unsigned long pages;
1106 unsigned long active;
1107 unsigned long writeback;
1108 unsigned long mapcount_max;
1109 unsigned long dirty;
1110 unsigned long swapcache;
1111 unsigned long node[MAX_NUMNODES];
1114 struct numa_maps_private {
1115 struct proc_maps_private proc_maps;
1116 struct numa_maps md;
1119 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1120 unsigned long nr_pages)
1122 int count = page_mapcount(page);
1124 md->pages += nr_pages;
1125 if (pte_dirty || PageDirty(page))
1126 md->dirty += nr_pages;
1128 if (PageSwapCache(page))
1129 md->swapcache += nr_pages;
1131 if (PageActive(page) || PageUnevictable(page))
1132 md->active += nr_pages;
1134 if (PageWriteback(page))
1135 md->writeback += nr_pages;
1138 md->anon += nr_pages;
1140 if (count > md->mapcount_max)
1141 md->mapcount_max = count;
1143 md->node[page_to_nid(page)] += nr_pages;
1146 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1152 if (!pte_present(pte))
1155 page = vm_normal_page(vma, addr, pte);
1159 if (PageReserved(page))
1162 nid = page_to_nid(page);
1163 if (!node_isset(nid, node_states[N_HIGH_MEMORY]))
1169 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1170 unsigned long end, struct mm_walk *walk)
1172 struct numa_maps *md;
1178 spin_lock(&walk->mm->page_table_lock);
1179 if (pmd_trans_huge(*pmd)) {
1180 if (pmd_trans_splitting(*pmd)) {
1181 spin_unlock(&walk->mm->page_table_lock);
1182 wait_split_huge_page(md->vma->anon_vma, pmd);
1184 pte_t huge_pte = *(pte_t *)pmd;
1187 page = can_gather_numa_stats(huge_pte, md->vma, addr);
1189 gather_stats(page, md, pte_dirty(huge_pte),
1190 HPAGE_PMD_SIZE/PAGE_SIZE);
1191 spin_unlock(&walk->mm->page_table_lock);
1195 spin_unlock(&walk->mm->page_table_lock);
1198 if (pmd_trans_unstable(pmd))
1200 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1202 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
1205 gather_stats(page, md, pte_dirty(*pte), 1);
1207 } while (pte++, addr += PAGE_SIZE, addr != end);
1208 pte_unmap_unlock(orig_pte, ptl);
1211 #ifdef CONFIG_HUGETLB_PAGE
1212 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1213 unsigned long addr, unsigned long end, struct mm_walk *walk)
1215 struct numa_maps *md;
1221 page = pte_page(*pte);
1226 gather_stats(page, md, pte_dirty(*pte), 1);
1231 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1232 unsigned long addr, unsigned long end, struct mm_walk *walk)
1239 * Display pages allocated per node and memory policy via /proc.
1241 static int show_numa_map(struct seq_file *m, void *v)
1243 struct numa_maps_private *numa_priv = m->private;
1244 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1245 struct vm_area_struct *vma = v;
1246 struct numa_maps *md = &numa_priv->md;
1247 struct file *file = vma->vm_file;
1248 struct mm_struct *mm = vma->vm_mm;
1249 struct mm_walk walk = {};
1250 struct mempolicy *pol;
1257 /* Ensure we start with an empty set of numa_maps statistics. */
1258 memset(md, 0, sizeof(*md));
1262 walk.hugetlb_entry = gather_hugetbl_stats;
1263 walk.pmd_entry = gather_pte_stats;
1267 pol = get_vma_policy(proc_priv->task, vma, vma->vm_start);
1268 mpol_to_str(buffer, sizeof(buffer), pol, 0);
1271 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1274 seq_printf(m, " file=");
1275 seq_path(m, &file->f_path, "\n\t= ");
1276 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1277 seq_printf(m, " heap");
1278 } else if (vma->vm_start <= mm->start_stack &&
1279 vma->vm_end >= mm->start_stack) {
1280 seq_printf(m, " stack");
1283 if (is_vm_hugetlb_page(vma))
1284 seq_printf(m, " huge");
1286 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1292 seq_printf(m, " anon=%lu", md->anon);
1295 seq_printf(m, " dirty=%lu", md->dirty);
1297 if (md->pages != md->anon && md->pages != md->dirty)
1298 seq_printf(m, " mapped=%lu", md->pages);
1300 if (md->mapcount_max > 1)
1301 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1304 seq_printf(m, " swapcache=%lu", md->swapcache);
1306 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1307 seq_printf(m, " active=%lu", md->active);
1310 seq_printf(m, " writeback=%lu", md->writeback);
1312 for_each_node_state(n, N_HIGH_MEMORY)
1314 seq_printf(m, " N%d=%lu", n, md->node[n]);
1318 if (m->count < m->size)
1319 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1323 static const struct seq_operations proc_pid_numa_maps_op = {
1327 .show = show_numa_map,
1330 static int numa_maps_open(struct inode *inode, struct file *file)
1332 struct numa_maps_private *priv;
1334 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1336 priv->proc_maps.pid = proc_pid(inode);
1337 ret = seq_open(file, &proc_pid_numa_maps_op);
1339 struct seq_file *m = file->private_data;
1348 const struct file_operations proc_numa_maps_operations = {
1349 .open = numa_maps_open,
1351 .llseek = seq_lseek,
1352 .release = seq_release_private,
1354 #endif /* CONFIG_NUMA */