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;
213 static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
215 struct mm_struct *mm = vma->vm_mm;
216 struct file *file = vma->vm_file;
217 vm_flags_t flags = vma->vm_flags;
218 unsigned long ino = 0;
219 unsigned long long pgoff = 0;
220 unsigned long start, end;
225 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
226 dev = inode->i_sb->s_dev;
228 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
231 /* We don't show the stack guard page in /proc/maps */
232 start = vma->vm_start;
233 if (stack_guard_page_start(vma, start))
236 if (stack_guard_page_end(vma, end))
239 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
242 flags & VM_READ ? 'r' : '-',
243 flags & VM_WRITE ? 'w' : '-',
244 flags & VM_EXEC ? 'x' : '-',
245 flags & VM_MAYSHARE ? 's' : 'p',
247 MAJOR(dev), MINOR(dev), ino, &len);
250 * Print the dentry name for named mappings, and a
251 * special [heap] marker for the heap:
254 pad_len_spaces(m, len);
255 seq_path(m, &file->f_path, "\n");
257 const char *name = arch_vma_name(vma);
260 if (vma->vm_start <= mm->brk &&
261 vma->vm_end >= mm->start_brk) {
263 } else if (vma->vm_start <= mm->start_stack &&
264 vma->vm_end >= mm->start_stack) {
272 pad_len_spaces(m, len);
279 static int show_map(struct seq_file *m, void *v)
281 struct vm_area_struct *vma = v;
282 struct proc_maps_private *priv = m->private;
283 struct task_struct *task = priv->task;
285 show_map_vma(m, vma);
287 if (m->count < m->size) /* vma is copied successfully */
288 m->version = (vma != get_gate_vma(task->mm))
293 static const struct seq_operations proc_pid_maps_op = {
300 static int maps_open(struct inode *inode, struct file *file)
302 return do_maps_open(inode, file, &proc_pid_maps_op);
305 const struct file_operations proc_maps_operations = {
309 .release = seq_release_private,
313 * Proportional Set Size(PSS): my share of RSS.
315 * PSS of a process is the count of pages it has in memory, where each
316 * page is divided by the number of processes sharing it. So if a
317 * process has 1000 pages all to itself, and 1000 shared with one other
318 * process, its PSS will be 1500.
320 * To keep (accumulated) division errors low, we adopt a 64bit
321 * fixed-point pss counter to minimize division errors. So (pss >>
322 * PSS_SHIFT) would be the real byte count.
324 * A shift of 12 before division means (assuming 4K page size):
325 * - 1M 3-user-pages add up to 8KB errors;
326 * - supports mapcount up to 2^24, or 16M;
327 * - supports PSS up to 2^52 bytes, or 4PB.
331 #ifdef CONFIG_PROC_PAGE_MONITOR
332 struct mem_size_stats {
333 struct vm_area_struct *vma;
334 unsigned long resident;
335 unsigned long shared_clean;
336 unsigned long shared_dirty;
337 unsigned long private_clean;
338 unsigned long private_dirty;
339 unsigned long referenced;
340 unsigned long anonymous;
341 unsigned long anonymous_thp;
347 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
348 unsigned long ptent_size, struct mm_walk *walk)
350 struct mem_size_stats *mss = walk->private;
351 struct vm_area_struct *vma = mss->vma;
355 if (is_swap_pte(ptent)) {
356 mss->swap += ptent_size;
360 if (!pte_present(ptent))
363 page = vm_normal_page(vma, addr, ptent);
368 mss->anonymous += ptent_size;
370 mss->resident += ptent_size;
371 /* Accumulate the size in pages that have been accessed. */
372 if (pte_young(ptent) || PageReferenced(page))
373 mss->referenced += ptent_size;
374 mapcount = page_mapcount(page);
376 if (pte_dirty(ptent) || PageDirty(page))
377 mss->shared_dirty += ptent_size;
379 mss->shared_clean += ptent_size;
380 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
382 if (pte_dirty(ptent) || PageDirty(page))
383 mss->private_dirty += ptent_size;
385 mss->private_clean += ptent_size;
386 mss->pss += (ptent_size << PSS_SHIFT);
390 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
391 struct mm_walk *walk)
393 struct mem_size_stats *mss = walk->private;
394 struct vm_area_struct *vma = mss->vma;
398 spin_lock(&walk->mm->page_table_lock);
399 if (pmd_trans_huge(*pmd)) {
400 if (pmd_trans_splitting(*pmd)) {
401 spin_unlock(&walk->mm->page_table_lock);
402 wait_split_huge_page(vma->anon_vma, pmd);
404 smaps_pte_entry(*(pte_t *)pmd, addr,
405 HPAGE_PMD_SIZE, walk);
406 spin_unlock(&walk->mm->page_table_lock);
407 mss->anonymous_thp += HPAGE_PMD_SIZE;
411 spin_unlock(&walk->mm->page_table_lock);
414 if (pmd_trans_unstable(pmd))
417 * The mmap_sem held all the way back in m_start() is what
418 * keeps khugepaged out of here and from collapsing things
421 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
422 for (; addr != end; pte++, addr += PAGE_SIZE)
423 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
424 pte_unmap_unlock(pte - 1, ptl);
429 static int show_smap(struct seq_file *m, void *v)
431 struct proc_maps_private *priv = m->private;
432 struct task_struct *task = priv->task;
433 struct vm_area_struct *vma = v;
434 struct mem_size_stats mss;
435 struct mm_walk smaps_walk = {
436 .pmd_entry = smaps_pte_range,
441 memset(&mss, 0, sizeof mss);
443 /* mmap_sem is held in m_start */
444 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
445 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
447 show_map_vma(m, vma);
453 "Shared_Clean: %8lu kB\n"
454 "Shared_Dirty: %8lu kB\n"
455 "Private_Clean: %8lu kB\n"
456 "Private_Dirty: %8lu kB\n"
457 "Referenced: %8lu kB\n"
458 "Anonymous: %8lu kB\n"
459 "AnonHugePages: %8lu kB\n"
461 "KernelPageSize: %8lu kB\n"
462 "MMUPageSize: %8lu kB\n"
464 (vma->vm_end - vma->vm_start) >> 10,
466 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
467 mss.shared_clean >> 10,
468 mss.shared_dirty >> 10,
469 mss.private_clean >> 10,
470 mss.private_dirty >> 10,
471 mss.referenced >> 10,
473 mss.anonymous_thp >> 10,
475 vma_kernel_pagesize(vma) >> 10,
476 vma_mmu_pagesize(vma) >> 10,
477 (vma->vm_flags & VM_LOCKED) ?
478 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
480 if (m->count < m->size) /* vma is copied successfully */
481 m->version = (vma != get_gate_vma(task->mm))
486 static const struct seq_operations proc_pid_smaps_op = {
493 static int smaps_open(struct inode *inode, struct file *file)
495 return do_maps_open(inode, file, &proc_pid_smaps_op);
498 const struct file_operations proc_smaps_operations = {
502 .release = seq_release_private,
505 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
506 unsigned long end, struct mm_walk *walk)
508 struct vm_area_struct *vma = walk->private;
513 split_huge_page_pmd(walk->mm, pmd);
514 if (pmd_trans_unstable(pmd))
517 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
518 for (; addr != end; pte++, addr += PAGE_SIZE) {
520 if (!pte_present(ptent))
523 page = vm_normal_page(vma, addr, ptent);
527 if (PageReserved(page))
530 /* Clear accessed and referenced bits. */
531 ptep_test_and_clear_young(vma, addr, pte);
532 ClearPageReferenced(page);
534 pte_unmap_unlock(pte - 1, ptl);
539 #define CLEAR_REFS_ALL 1
540 #define CLEAR_REFS_ANON 2
541 #define CLEAR_REFS_MAPPED 3
543 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
544 size_t count, loff_t *ppos)
546 struct task_struct *task;
547 char buffer[PROC_NUMBUF];
548 struct mm_struct *mm;
549 struct vm_area_struct *vma;
553 memset(buffer, 0, sizeof(buffer));
554 if (count > sizeof(buffer) - 1)
555 count = sizeof(buffer) - 1;
556 if (copy_from_user(buffer, buf, count))
558 rv = kstrtoint(strstrip(buffer), 10, &type);
561 if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
563 task = get_proc_task(file->f_path.dentry->d_inode);
566 mm = get_task_mm(task);
568 struct mm_walk clear_refs_walk = {
569 .pmd_entry = clear_refs_pte_range,
572 down_read(&mm->mmap_sem);
573 for (vma = mm->mmap; vma; vma = vma->vm_next) {
574 clear_refs_walk.private = vma;
575 if (is_vm_hugetlb_page(vma))
578 * Writing 1 to /proc/pid/clear_refs affects all pages.
580 * Writing 2 to /proc/pid/clear_refs only affects
583 * Writing 3 to /proc/pid/clear_refs only affects file
586 if (type == CLEAR_REFS_ANON && vma->vm_file)
588 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
590 walk_page_range(vma->vm_start, vma->vm_end,
594 up_read(&mm->mmap_sem);
597 put_task_struct(task);
602 const struct file_operations proc_clear_refs_operations = {
603 .write = clear_refs_write,
604 .llseek = noop_llseek,
608 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
613 #define PM_ENTRY_BYTES sizeof(u64)
614 #define PM_STATUS_BITS 3
615 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
616 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
617 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
618 #define PM_PSHIFT_BITS 6
619 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
620 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
621 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
622 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
623 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
625 #define PM_PRESENT PM_STATUS(4LL)
626 #define PM_SWAP PM_STATUS(2LL)
627 #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
628 #define PM_END_OF_BUFFER 1
630 static int add_to_pagemap(unsigned long addr, u64 pfn,
631 struct pagemapread *pm)
633 pm->buffer[pm->pos++] = pfn;
634 if (pm->pos >= pm->len)
635 return PM_END_OF_BUFFER;
639 static int pagemap_pte_hole(unsigned long start, unsigned long end,
640 struct mm_walk *walk)
642 struct pagemapread *pm = walk->private;
645 for (addr = start; addr < end; addr += PAGE_SIZE) {
646 err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
653 static u64 swap_pte_to_pagemap_entry(pte_t pte)
655 swp_entry_t e = pte_to_swp_entry(pte);
656 return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
659 static u64 pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte)
662 if (is_swap_pte(pte))
663 pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
664 | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
665 else if (pte_present(pte))
666 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte)) : 0)
667 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
671 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
672 struct mm_walk *walk)
674 struct vm_area_struct *vma;
675 struct pagemapread *pm = walk->private;
679 split_huge_page_pmd(walk->mm, pmd);
680 if (pmd_trans_unstable(pmd))
683 /* find the first VMA at or above 'addr' */
684 vma = find_vma(walk->mm, addr);
685 for (; addr != end; addr += PAGE_SIZE) {
686 u64 pfn = PM_NOT_PRESENT;
688 /* check to see if we've left 'vma' behind
689 * and need a new, higher one */
690 if (vma && (addr >= vma->vm_end))
691 vma = find_vma(walk->mm, addr);
693 /* check that 'vma' actually covers this address,
694 * and that it isn't a huge page vma */
695 if (vma && (vma->vm_start <= addr) &&
696 !is_vm_hugetlb_page(vma)) {
697 pte = pte_offset_map(pmd, addr);
698 pfn = pte_to_pagemap_entry(pm, *pte);
699 /* unmap before userspace copy */
702 err = add_to_pagemap(addr, pfn, pm);
712 #ifdef CONFIG_HUGETLB_PAGE
713 static u64 huge_pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte, int offset)
716 if (pte_present(pte))
717 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte) + offset) : 0)
718 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
722 /* This function walks within one hugetlb entry in the single call */
723 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
724 unsigned long addr, unsigned long end,
725 struct mm_walk *walk)
727 struct pagemapread *pm = walk->private;
731 for (; addr != end; addr += PAGE_SIZE) {
732 int offset = (addr & ~hmask) >> PAGE_SHIFT;
733 pfn = huge_pte_to_pagemap_entry(pm, *pte, offset);
734 err = add_to_pagemap(addr, pfn, pm);
743 #endif /* HUGETLB_PAGE */
746 * /proc/pid/pagemap - an array mapping virtual pages to pfns
748 * For each page in the address space, this file contains one 64-bit entry
749 * consisting of the following:
751 * Bits 0-55 page frame number (PFN) if present
752 * Bits 0-4 swap type if swapped
753 * Bits 5-55 swap offset if swapped
754 * Bits 55-60 page shift (page size = 1<<page shift)
755 * Bit 61 reserved for future use
756 * Bit 62 page swapped
757 * Bit 63 page present
759 * If the page is not present but in swap, then the PFN contains an
760 * encoding of the swap file number and the page's offset into the
761 * swap. Unmapped pages return a null PFN. This allows determining
762 * precisely which pages are mapped (or in swap) and comparing mapped
763 * pages between processes.
765 * Efficient users of this interface will use /proc/pid/maps to
766 * determine which areas of memory are actually mapped and llseek to
767 * skip over unmapped regions.
769 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
770 #define PAGEMAP_WALK_MASK (PMD_MASK)
771 static ssize_t pagemap_read(struct file *file, char __user *buf,
772 size_t count, loff_t *ppos)
774 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
775 struct mm_struct *mm;
776 struct pagemapread pm;
778 struct mm_walk pagemap_walk = {};
781 unsigned long start_vaddr;
782 unsigned long end_vaddr;
789 /* file position must be aligned */
790 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
797 /* do not disclose physical addresses: attack vector */
798 pm.show_pfn = !security_capable(&init_user_ns, file->f_cred,
801 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
802 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
807 mm = mm_for_maps(task);
809 if (!mm || IS_ERR(mm))
812 pagemap_walk.pmd_entry = pagemap_pte_range;
813 pagemap_walk.pte_hole = pagemap_pte_hole;
814 #ifdef CONFIG_HUGETLB_PAGE
815 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
817 pagemap_walk.mm = mm;
818 pagemap_walk.private = ±
821 svpfn = src / PM_ENTRY_BYTES;
822 start_vaddr = svpfn << PAGE_SHIFT;
823 end_vaddr = TASK_SIZE_OF(task);
825 /* watch out for wraparound */
826 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
827 start_vaddr = end_vaddr;
830 * The odds are that this will stop walking way
831 * before end_vaddr, because the length of the
832 * user buffer is tracked in "pm", and the walk
833 * will stop when we hit the end of the buffer.
836 while (count && (start_vaddr < end_vaddr)) {
841 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
843 if (end < start_vaddr || end > end_vaddr)
845 down_read(&mm->mmap_sem);
846 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
847 up_read(&mm->mmap_sem);
850 len = min(count, PM_ENTRY_BYTES * pm.pos);
851 if (copy_to_user(buf, pm.buffer, len)) {
860 if (!ret || ret == PM_END_OF_BUFFER)
868 put_task_struct(task);
873 const struct file_operations proc_pagemap_operations = {
874 .llseek = mem_lseek, /* borrow this */
875 .read = pagemap_read,
877 #endif /* CONFIG_PROC_PAGE_MONITOR */
882 struct vm_area_struct *vma;
885 unsigned long active;
886 unsigned long writeback;
887 unsigned long mapcount_max;
889 unsigned long swapcache;
890 unsigned long node[MAX_NUMNODES];
893 struct numa_maps_private {
894 struct proc_maps_private proc_maps;
898 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
899 unsigned long nr_pages)
901 int count = page_mapcount(page);
903 md->pages += nr_pages;
904 if (pte_dirty || PageDirty(page))
905 md->dirty += nr_pages;
907 if (PageSwapCache(page))
908 md->swapcache += nr_pages;
910 if (PageActive(page) || PageUnevictable(page))
911 md->active += nr_pages;
913 if (PageWriteback(page))
914 md->writeback += nr_pages;
917 md->anon += nr_pages;
919 if (count > md->mapcount_max)
920 md->mapcount_max = count;
922 md->node[page_to_nid(page)] += nr_pages;
925 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
931 if (!pte_present(pte))
934 page = vm_normal_page(vma, addr, pte);
938 if (PageReserved(page))
941 nid = page_to_nid(page);
942 if (!node_isset(nid, node_states[N_HIGH_MEMORY]))
948 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
949 unsigned long end, struct mm_walk *walk)
951 struct numa_maps *md;
957 spin_lock(&walk->mm->page_table_lock);
958 if (pmd_trans_huge(*pmd)) {
959 if (pmd_trans_splitting(*pmd)) {
960 spin_unlock(&walk->mm->page_table_lock);
961 wait_split_huge_page(md->vma->anon_vma, pmd);
963 pte_t huge_pte = *(pte_t *)pmd;
966 page = can_gather_numa_stats(huge_pte, md->vma, addr);
968 gather_stats(page, md, pte_dirty(huge_pte),
969 HPAGE_PMD_SIZE/PAGE_SIZE);
970 spin_unlock(&walk->mm->page_table_lock);
974 spin_unlock(&walk->mm->page_table_lock);
977 if (pmd_trans_unstable(pmd))
979 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
981 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
984 gather_stats(page, md, pte_dirty(*pte), 1);
986 } while (pte++, addr += PAGE_SIZE, addr != end);
987 pte_unmap_unlock(orig_pte, ptl);
990 #ifdef CONFIG_HUGETLB_PAGE
991 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
992 unsigned long addr, unsigned long end, struct mm_walk *walk)
994 struct numa_maps *md;
1000 page = pte_page(*pte);
1005 gather_stats(page, md, pte_dirty(*pte), 1);
1010 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1011 unsigned long addr, unsigned long end, struct mm_walk *walk)
1018 * Display pages allocated per node and memory policy via /proc.
1020 static int show_numa_map(struct seq_file *m, void *v)
1022 struct numa_maps_private *numa_priv = m->private;
1023 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1024 struct vm_area_struct *vma = v;
1025 struct numa_maps *md = &numa_priv->md;
1026 struct file *file = vma->vm_file;
1027 struct mm_struct *mm = vma->vm_mm;
1028 struct mm_walk walk = {};
1029 struct mempolicy *pol;
1036 /* Ensure we start with an empty set of numa_maps statistics. */
1037 memset(md, 0, sizeof(*md));
1041 walk.hugetlb_entry = gather_hugetbl_stats;
1042 walk.pmd_entry = gather_pte_stats;
1046 pol = get_vma_policy(proc_priv->task, vma, vma->vm_start);
1047 mpol_to_str(buffer, sizeof(buffer), pol, 0);
1050 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1053 seq_printf(m, " file=");
1054 seq_path(m, &file->f_path, "\n\t= ");
1055 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1056 seq_printf(m, " heap");
1057 } else if (vma->vm_start <= mm->start_stack &&
1058 vma->vm_end >= mm->start_stack) {
1059 seq_printf(m, " stack");
1062 if (is_vm_hugetlb_page(vma))
1063 seq_printf(m, " huge");
1065 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1071 seq_printf(m, " anon=%lu", md->anon);
1074 seq_printf(m, " dirty=%lu", md->dirty);
1076 if (md->pages != md->anon && md->pages != md->dirty)
1077 seq_printf(m, " mapped=%lu", md->pages);
1079 if (md->mapcount_max > 1)
1080 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1083 seq_printf(m, " swapcache=%lu", md->swapcache);
1085 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1086 seq_printf(m, " active=%lu", md->active);
1089 seq_printf(m, " writeback=%lu", md->writeback);
1091 for_each_node_state(n, N_HIGH_MEMORY)
1093 seq_printf(m, " N%d=%lu", n, md->node[n]);
1097 if (m->count < m->size)
1098 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1102 static const struct seq_operations proc_pid_numa_maps_op = {
1106 .show = show_numa_map,
1109 static int numa_maps_open(struct inode *inode, struct file *file)
1111 struct numa_maps_private *priv;
1113 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1115 priv->proc_maps.pid = proc_pid(inode);
1116 ret = seq_open(file, &proc_pid_numa_maps_op);
1118 struct seq_file *m = file->private_data;
1127 const struct file_operations proc_numa_maps_operations = {
1128 .open = numa_maps_open,
1130 .llseek = seq_lseek,
1131 .release = seq_release_private,
1133 #endif /* CONFIG_NUMA */