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;
235 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
238 flags & VM_READ ? 'r' : '-',
239 flags & VM_WRITE ? 'w' : '-',
240 flags & VM_EXEC ? 'x' : '-',
241 flags & VM_MAYSHARE ? 's' : 'p',
243 MAJOR(dev), MINOR(dev), ino, &len);
246 * Print the dentry name for named mappings, and a
247 * special [heap] marker for the heap:
250 pad_len_spaces(m, len);
251 seq_path(m, &file->f_path, "\n");
253 const char *name = arch_vma_name(vma);
256 if (vma->vm_start <= mm->brk &&
257 vma->vm_end >= mm->start_brk) {
259 } else if (vma->vm_start <= mm->start_stack &&
260 vma->vm_end >= mm->start_stack) {
268 pad_len_spaces(m, len);
275 static int show_map(struct seq_file *m, void *v)
277 struct vm_area_struct *vma = v;
278 struct proc_maps_private *priv = m->private;
279 struct task_struct *task = priv->task;
281 show_map_vma(m, vma);
283 if (m->count < m->size) /* vma is copied successfully */
284 m->version = (vma != get_gate_vma(task->mm))
289 static const struct seq_operations proc_pid_maps_op = {
296 static int maps_open(struct inode *inode, struct file *file)
298 return do_maps_open(inode, file, &proc_pid_maps_op);
301 const struct file_operations proc_maps_operations = {
305 .release = seq_release_private,
309 * Proportional Set Size(PSS): my share of RSS.
311 * PSS of a process is the count of pages it has in memory, where each
312 * page is divided by the number of processes sharing it. So if a
313 * process has 1000 pages all to itself, and 1000 shared with one other
314 * process, its PSS will be 1500.
316 * To keep (accumulated) division errors low, we adopt a 64bit
317 * fixed-point pss counter to minimize division errors. So (pss >>
318 * PSS_SHIFT) would be the real byte count.
320 * A shift of 12 before division means (assuming 4K page size):
321 * - 1M 3-user-pages add up to 8KB errors;
322 * - supports mapcount up to 2^24, or 16M;
323 * - supports PSS up to 2^52 bytes, or 4PB.
327 #ifdef CONFIG_PROC_PAGE_MONITOR
328 struct mem_size_stats {
329 struct vm_area_struct *vma;
330 unsigned long resident;
331 unsigned long shared_clean;
332 unsigned long shared_dirty;
333 unsigned long private_clean;
334 unsigned long private_dirty;
335 unsigned long referenced;
336 unsigned long anonymous;
337 unsigned long anonymous_thp;
343 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
344 unsigned long ptent_size, struct mm_walk *walk)
346 struct mem_size_stats *mss = walk->private;
347 struct vm_area_struct *vma = mss->vma;
351 if (is_swap_pte(ptent)) {
352 mss->swap += ptent_size;
356 if (!pte_present(ptent))
359 page = vm_normal_page(vma, addr, ptent);
364 mss->anonymous += ptent_size;
366 mss->resident += ptent_size;
367 /* Accumulate the size in pages that have been accessed. */
368 if (pte_young(ptent) || PageReferenced(page))
369 mss->referenced += ptent_size;
370 mapcount = page_mapcount(page);
372 if (pte_dirty(ptent) || PageDirty(page))
373 mss->shared_dirty += ptent_size;
375 mss->shared_clean += ptent_size;
376 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
378 if (pte_dirty(ptent) || PageDirty(page))
379 mss->private_dirty += ptent_size;
381 mss->private_clean += ptent_size;
382 mss->pss += (ptent_size << PSS_SHIFT);
386 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
387 struct mm_walk *walk)
389 struct mem_size_stats *mss = walk->private;
390 struct vm_area_struct *vma = mss->vma;
394 spin_lock(&walk->mm->page_table_lock);
395 if (pmd_trans_huge(*pmd)) {
396 if (pmd_trans_splitting(*pmd)) {
397 spin_unlock(&walk->mm->page_table_lock);
398 wait_split_huge_page(vma->anon_vma, pmd);
400 smaps_pte_entry(*(pte_t *)pmd, addr,
401 HPAGE_PMD_SIZE, walk);
402 spin_unlock(&walk->mm->page_table_lock);
403 mss->anonymous_thp += HPAGE_PMD_SIZE;
407 spin_unlock(&walk->mm->page_table_lock);
410 if (pmd_trans_unstable(pmd))
413 * The mmap_sem held all the way back in m_start() is what
414 * keeps khugepaged out of here and from collapsing things
417 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
418 for (; addr != end; pte++, addr += PAGE_SIZE)
419 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
420 pte_unmap_unlock(pte - 1, ptl);
425 static int show_smap(struct seq_file *m, void *v)
427 struct proc_maps_private *priv = m->private;
428 struct task_struct *task = priv->task;
429 struct vm_area_struct *vma = v;
430 struct mem_size_stats mss;
431 struct mm_walk smaps_walk = {
432 .pmd_entry = smaps_pte_range,
437 memset(&mss, 0, sizeof mss);
439 /* mmap_sem is held in m_start */
440 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
441 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
443 show_map_vma(m, vma);
449 "Shared_Clean: %8lu kB\n"
450 "Shared_Dirty: %8lu kB\n"
451 "Private_Clean: %8lu kB\n"
452 "Private_Dirty: %8lu kB\n"
453 "Referenced: %8lu kB\n"
454 "Anonymous: %8lu kB\n"
455 "AnonHugePages: %8lu kB\n"
457 "KernelPageSize: %8lu kB\n"
458 "MMUPageSize: %8lu kB\n"
460 (vma->vm_end - vma->vm_start) >> 10,
462 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
463 mss.shared_clean >> 10,
464 mss.shared_dirty >> 10,
465 mss.private_clean >> 10,
466 mss.private_dirty >> 10,
467 mss.referenced >> 10,
469 mss.anonymous_thp >> 10,
471 vma_kernel_pagesize(vma) >> 10,
472 vma_mmu_pagesize(vma) >> 10,
473 (vma->vm_flags & VM_LOCKED) ?
474 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
476 if (m->count < m->size) /* vma is copied successfully */
477 m->version = (vma != get_gate_vma(task->mm))
482 static const struct seq_operations proc_pid_smaps_op = {
489 static int smaps_open(struct inode *inode, struct file *file)
491 return do_maps_open(inode, file, &proc_pid_smaps_op);
494 const struct file_operations proc_smaps_operations = {
498 .release = seq_release_private,
501 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
502 unsigned long end, struct mm_walk *walk)
504 struct vm_area_struct *vma = walk->private;
509 split_huge_page_pmd(walk->mm, pmd);
510 if (pmd_trans_unstable(pmd))
513 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
514 for (; addr != end; pte++, addr += PAGE_SIZE) {
516 if (!pte_present(ptent))
519 page = vm_normal_page(vma, addr, ptent);
523 if (PageReserved(page))
526 /* Clear accessed and referenced bits. */
527 ptep_test_and_clear_young(vma, addr, pte);
528 ClearPageReferenced(page);
530 pte_unmap_unlock(pte - 1, ptl);
535 #define CLEAR_REFS_ALL 1
536 #define CLEAR_REFS_ANON 2
537 #define CLEAR_REFS_MAPPED 3
539 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
540 size_t count, loff_t *ppos)
542 struct task_struct *task;
543 char buffer[PROC_NUMBUF];
544 struct mm_struct *mm;
545 struct vm_area_struct *vma;
549 memset(buffer, 0, sizeof(buffer));
550 if (count > sizeof(buffer) - 1)
551 count = sizeof(buffer) - 1;
552 if (copy_from_user(buffer, buf, count))
554 rv = kstrtoint(strstrip(buffer), 10, &type);
557 if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
559 task = get_proc_task(file->f_path.dentry->d_inode);
562 mm = get_task_mm(task);
564 struct mm_walk clear_refs_walk = {
565 .pmd_entry = clear_refs_pte_range,
568 down_read(&mm->mmap_sem);
569 for (vma = mm->mmap; vma; vma = vma->vm_next) {
570 clear_refs_walk.private = vma;
571 if (is_vm_hugetlb_page(vma))
574 * Writing 1 to /proc/pid/clear_refs affects all pages.
576 * Writing 2 to /proc/pid/clear_refs only affects
579 * Writing 3 to /proc/pid/clear_refs only affects file
582 if (type == CLEAR_REFS_ANON && vma->vm_file)
584 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
586 walk_page_range(vma->vm_start, vma->vm_end,
590 up_read(&mm->mmap_sem);
593 put_task_struct(task);
598 const struct file_operations proc_clear_refs_operations = {
599 .write = clear_refs_write,
600 .llseek = noop_llseek,
604 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
609 #define PM_ENTRY_BYTES sizeof(u64)
610 #define PM_STATUS_BITS 3
611 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
612 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
613 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
614 #define PM_PSHIFT_BITS 6
615 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
616 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
617 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
618 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
619 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
621 #define PM_PRESENT PM_STATUS(4LL)
622 #define PM_SWAP PM_STATUS(2LL)
623 #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
624 #define PM_END_OF_BUFFER 1
626 static int add_to_pagemap(unsigned long addr, u64 pfn,
627 struct pagemapread *pm)
629 pm->buffer[pm->pos++] = pfn;
630 if (pm->pos >= pm->len)
631 return PM_END_OF_BUFFER;
635 static int pagemap_pte_hole(unsigned long start, unsigned long end,
636 struct mm_walk *walk)
638 struct pagemapread *pm = walk->private;
641 for (addr = start; addr < end; addr += PAGE_SIZE) {
642 err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
649 static u64 swap_pte_to_pagemap_entry(pte_t pte)
651 swp_entry_t e = pte_to_swp_entry(pte);
652 return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
655 static u64 pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte)
658 if (is_swap_pte(pte))
659 pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
660 | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
661 else if (pte_present(pte))
662 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte)) : 0)
663 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
667 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
668 struct mm_walk *walk)
670 struct vm_area_struct *vma;
671 struct pagemapread *pm = walk->private;
675 split_huge_page_pmd(walk->mm, pmd);
676 if (pmd_trans_unstable(pmd))
679 /* find the first VMA at or above 'addr' */
680 vma = find_vma(walk->mm, addr);
681 for (; addr != end; addr += PAGE_SIZE) {
682 u64 pfn = PM_NOT_PRESENT;
684 /* check to see if we've left 'vma' behind
685 * and need a new, higher one */
686 if (vma && (addr >= vma->vm_end))
687 vma = find_vma(walk->mm, addr);
689 /* check that 'vma' actually covers this address,
690 * and that it isn't a huge page vma */
691 if (vma && (vma->vm_start <= addr) &&
692 !is_vm_hugetlb_page(vma)) {
693 pte = pte_offset_map(pmd, addr);
694 pfn = pte_to_pagemap_entry(pm, *pte);
695 /* unmap before userspace copy */
698 err = add_to_pagemap(addr, pfn, pm);
708 #ifdef CONFIG_HUGETLB_PAGE
709 static u64 huge_pte_to_pagemap_entry(struct pagemapread *pm, pte_t pte, int offset)
712 if (pte_present(pte))
713 pme = (pm->show_pfn ? PM_PFRAME(pte_pfn(pte) + offset) : 0)
714 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
718 /* This function walks within one hugetlb entry in the single call */
719 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
720 unsigned long addr, unsigned long end,
721 struct mm_walk *walk)
723 struct pagemapread *pm = walk->private;
727 for (; addr != end; addr += PAGE_SIZE) {
728 int offset = (addr & ~hmask) >> PAGE_SHIFT;
729 pfn = huge_pte_to_pagemap_entry(pm, *pte, offset);
730 err = add_to_pagemap(addr, pfn, pm);
739 #endif /* HUGETLB_PAGE */
742 * /proc/pid/pagemap - an array mapping virtual pages to pfns
744 * For each page in the address space, this file contains one 64-bit entry
745 * consisting of the following:
747 * Bits 0-55 page frame number (PFN) if present
748 * Bits 0-4 swap type if swapped
749 * Bits 5-55 swap offset if swapped
750 * Bits 55-60 page shift (page size = 1<<page shift)
751 * Bit 61 reserved for future use
752 * Bit 62 page swapped
753 * Bit 63 page present
755 * If the page is not present but in swap, then the PFN contains an
756 * encoding of the swap file number and the page's offset into the
757 * swap. Unmapped pages return a null PFN. This allows determining
758 * precisely which pages are mapped (or in swap) and comparing mapped
759 * pages between processes.
761 * Efficient users of this interface will use /proc/pid/maps to
762 * determine which areas of memory are actually mapped and llseek to
763 * skip over unmapped regions.
765 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
766 #define PAGEMAP_WALK_MASK (PMD_MASK)
767 static ssize_t pagemap_read(struct file *file, char __user *buf,
768 size_t count, loff_t *ppos)
770 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
771 struct mm_struct *mm;
772 struct pagemapread pm;
774 struct mm_walk pagemap_walk = {};
777 unsigned long start_vaddr;
778 unsigned long end_vaddr;
785 /* file position must be aligned */
786 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
793 /* do not disclose physical addresses: attack vector */
794 pm.show_pfn = !security_capable(&init_user_ns, file->f_cred,
797 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
798 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
803 mm = mm_for_maps(task);
805 if (!mm || IS_ERR(mm))
808 pagemap_walk.pmd_entry = pagemap_pte_range;
809 pagemap_walk.pte_hole = pagemap_pte_hole;
810 #ifdef CONFIG_HUGETLB_PAGE
811 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
813 pagemap_walk.mm = mm;
814 pagemap_walk.private = ±
817 svpfn = src / PM_ENTRY_BYTES;
818 start_vaddr = svpfn << PAGE_SHIFT;
819 end_vaddr = TASK_SIZE_OF(task);
821 /* watch out for wraparound */
822 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
823 start_vaddr = end_vaddr;
826 * The odds are that this will stop walking way
827 * before end_vaddr, because the length of the
828 * user buffer is tracked in "pm", and the walk
829 * will stop when we hit the end of the buffer.
832 while (count && (start_vaddr < end_vaddr)) {
837 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
839 if (end < start_vaddr || end > end_vaddr)
841 down_read(&mm->mmap_sem);
842 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
843 up_read(&mm->mmap_sem);
846 len = min(count, PM_ENTRY_BYTES * pm.pos);
847 if (copy_to_user(buf, pm.buffer, len)) {
856 if (!ret || ret == PM_END_OF_BUFFER)
864 put_task_struct(task);
869 const struct file_operations proc_pagemap_operations = {
870 .llseek = mem_lseek, /* borrow this */
871 .read = pagemap_read,
873 #endif /* CONFIG_PROC_PAGE_MONITOR */
878 struct vm_area_struct *vma;
881 unsigned long active;
882 unsigned long writeback;
883 unsigned long mapcount_max;
885 unsigned long swapcache;
886 unsigned long node[MAX_NUMNODES];
889 struct numa_maps_private {
890 struct proc_maps_private proc_maps;
894 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
895 unsigned long nr_pages)
897 int count = page_mapcount(page);
899 md->pages += nr_pages;
900 if (pte_dirty || PageDirty(page))
901 md->dirty += nr_pages;
903 if (PageSwapCache(page))
904 md->swapcache += nr_pages;
906 if (PageActive(page) || PageUnevictable(page))
907 md->active += nr_pages;
909 if (PageWriteback(page))
910 md->writeback += nr_pages;
913 md->anon += nr_pages;
915 if (count > md->mapcount_max)
916 md->mapcount_max = count;
918 md->node[page_to_nid(page)] += nr_pages;
921 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
927 if (!pte_present(pte))
930 page = vm_normal_page(vma, addr, pte);
934 if (PageReserved(page))
937 nid = page_to_nid(page);
938 if (!node_isset(nid, node_states[N_HIGH_MEMORY]))
944 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
945 unsigned long end, struct mm_walk *walk)
947 struct numa_maps *md;
953 spin_lock(&walk->mm->page_table_lock);
954 if (pmd_trans_huge(*pmd)) {
955 if (pmd_trans_splitting(*pmd)) {
956 spin_unlock(&walk->mm->page_table_lock);
957 wait_split_huge_page(md->vma->anon_vma, pmd);
959 pte_t huge_pte = *(pte_t *)pmd;
962 page = can_gather_numa_stats(huge_pte, md->vma, addr);
964 gather_stats(page, md, pte_dirty(huge_pte),
965 HPAGE_PMD_SIZE/PAGE_SIZE);
966 spin_unlock(&walk->mm->page_table_lock);
970 spin_unlock(&walk->mm->page_table_lock);
973 if (pmd_trans_unstable(pmd))
975 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
977 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
980 gather_stats(page, md, pte_dirty(*pte), 1);
982 } while (pte++, addr += PAGE_SIZE, addr != end);
983 pte_unmap_unlock(orig_pte, ptl);
986 #ifdef CONFIG_HUGETLB_PAGE
987 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
988 unsigned long addr, unsigned long end, struct mm_walk *walk)
990 struct numa_maps *md;
996 page = pte_page(*pte);
1001 gather_stats(page, md, pte_dirty(*pte), 1);
1006 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1007 unsigned long addr, unsigned long end, struct mm_walk *walk)
1014 * Display pages allocated per node and memory policy via /proc.
1016 static int show_numa_map(struct seq_file *m, void *v)
1018 struct numa_maps_private *numa_priv = m->private;
1019 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1020 struct vm_area_struct *vma = v;
1021 struct numa_maps *md = &numa_priv->md;
1022 struct file *file = vma->vm_file;
1023 struct mm_struct *mm = vma->vm_mm;
1024 struct mm_walk walk = {};
1025 struct mempolicy *pol;
1032 /* Ensure we start with an empty set of numa_maps statistics. */
1033 memset(md, 0, sizeof(*md));
1037 walk.hugetlb_entry = gather_hugetbl_stats;
1038 walk.pmd_entry = gather_pte_stats;
1042 pol = get_vma_policy(proc_priv->task, vma, vma->vm_start);
1043 mpol_to_str(buffer, sizeof(buffer), pol, 0);
1046 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1049 seq_printf(m, " file=");
1050 seq_path(m, &file->f_path, "\n\t= ");
1051 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1052 seq_printf(m, " heap");
1053 } else if (vma->vm_start <= mm->start_stack &&
1054 vma->vm_end >= mm->start_stack) {
1055 seq_printf(m, " stack");
1058 if (is_vm_hugetlb_page(vma))
1059 seq_printf(m, " huge");
1061 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1067 seq_printf(m, " anon=%lu", md->anon);
1070 seq_printf(m, " dirty=%lu", md->dirty);
1072 if (md->pages != md->anon && md->pages != md->dirty)
1073 seq_printf(m, " mapped=%lu", md->pages);
1075 if (md->mapcount_max > 1)
1076 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1079 seq_printf(m, " swapcache=%lu", md->swapcache);
1081 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1082 seq_printf(m, " active=%lu", md->active);
1085 seq_printf(m, " writeback=%lu", md->writeback);
1087 for_each_node_state(n, N_HIGH_MEMORY)
1089 seq_printf(m, " N%d=%lu", n, md->node[n]);
1093 if (m->count < m->size)
1094 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1098 static const struct seq_operations proc_pid_numa_maps_op = {
1102 .show = show_numa_map,
1105 static int numa_maps_open(struct inode *inode, struct file *file)
1107 struct numa_maps_private *priv;
1109 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1111 priv->proc_maps.pid = proc_pid(inode);
1112 ret = seq_open(file, &proc_pid_numa_maps_op);
1114 struct seq_file *m = file->private_data;
1123 const struct file_operations proc_numa_maps_operations = {
1124 .open = numa_maps_open,
1126 .llseek = seq_lseek,
1127 .release = seq_release_private,
1129 #endif /* CONFIG_NUMA */