1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/vmacache.h>
4 #include <linux/hugetlb.h>
5 #include <linux/huge_mm.h>
6 #include <linux/mount.h>
7 #include <linux/seq_file.h>
8 #include <linux/highmem.h>
9 #include <linux/ptrace.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/mempolicy.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/sched/mm.h>
16 #include <linux/swapops.h>
17 #include <linux/mmu_notifier.h>
18 #include <linux/page_idle.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/uaccess.h>
24 #include <asm/tlbflush.h>
27 void task_mem(struct seq_file
*m
, struct mm_struct
*mm
)
29 unsigned long text
, lib
, swap
, ptes
, pmds
, anon
, file
, shmem
;
30 unsigned long hiwater_vm
, total_vm
, hiwater_rss
, total_rss
;
32 anon
= get_mm_counter(mm
, MM_ANONPAGES
);
33 file
= get_mm_counter(mm
, MM_FILEPAGES
);
34 shmem
= get_mm_counter(mm
, MM_SHMEMPAGES
);
37 * Note: to minimize their overhead, mm maintains hiwater_vm and
38 * hiwater_rss only when about to *lower* total_vm or rss. Any
39 * collector of these hiwater stats must therefore get total_vm
40 * and rss too, which will usually be the higher. Barriers? not
41 * worth the effort, such snapshots can always be inconsistent.
43 hiwater_vm
= total_vm
= mm
->total_vm
;
44 if (hiwater_vm
< mm
->hiwater_vm
)
45 hiwater_vm
= mm
->hiwater_vm
;
46 hiwater_rss
= total_rss
= anon
+ file
+ shmem
;
47 if (hiwater_rss
< mm
->hiwater_rss
)
48 hiwater_rss
= mm
->hiwater_rss
;
50 text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
)) >> 10;
51 lib
= (mm
->exec_vm
<< (PAGE_SHIFT
-10)) - text
;
52 swap
= get_mm_counter(mm
, MM_SWAPENTS
);
53 ptes
= PTRS_PER_PTE
* sizeof(pte_t
) * atomic_long_read(&mm
->nr_ptes
);
54 pmds
= PTRS_PER_PMD
* sizeof(pmd_t
) * mm_nr_pmds(mm
);
64 "RssShmem:\t%8lu kB\n"
72 hiwater_vm
<< (PAGE_SHIFT
-10),
73 total_vm
<< (PAGE_SHIFT
-10),
74 mm
->locked_vm
<< (PAGE_SHIFT
-10),
75 mm
->pinned_vm
<< (PAGE_SHIFT
-10),
76 hiwater_rss
<< (PAGE_SHIFT
-10),
77 total_rss
<< (PAGE_SHIFT
-10),
78 anon
<< (PAGE_SHIFT
-10),
79 file
<< (PAGE_SHIFT
-10),
80 shmem
<< (PAGE_SHIFT
-10),
81 mm
->data_vm
<< (PAGE_SHIFT
-10),
82 mm
->stack_vm
<< (PAGE_SHIFT
-10), text
, lib
,
85 swap
<< (PAGE_SHIFT
-10));
86 hugetlb_report_usage(m
, mm
);
89 unsigned long task_vsize(struct mm_struct
*mm
)
91 return PAGE_SIZE
* mm
->total_vm
;
94 unsigned long task_statm(struct mm_struct
*mm
,
95 unsigned long *shared
, unsigned long *text
,
96 unsigned long *data
, unsigned long *resident
)
98 *shared
= get_mm_counter(mm
, MM_FILEPAGES
) +
99 get_mm_counter(mm
, MM_SHMEMPAGES
);
100 *text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
))
102 *data
= mm
->data_vm
+ mm
->stack_vm
;
103 *resident
= *shared
+ get_mm_counter(mm
, MM_ANONPAGES
);
109 * Save get_task_policy() for show_numa_map().
111 static void hold_task_mempolicy(struct proc_maps_private
*priv
)
113 struct task_struct
*task
= priv
->task
;
116 priv
->task_mempolicy
= get_task_policy(task
);
117 mpol_get(priv
->task_mempolicy
);
120 static void release_task_mempolicy(struct proc_maps_private
*priv
)
122 mpol_put(priv
->task_mempolicy
);
125 static void hold_task_mempolicy(struct proc_maps_private
*priv
)
128 static void release_task_mempolicy(struct proc_maps_private
*priv
)
133 static void vma_stop(struct proc_maps_private
*priv
)
135 struct mm_struct
*mm
= priv
->mm
;
137 release_task_mempolicy(priv
);
138 up_read(&mm
->mmap_sem
);
142 static struct vm_area_struct
*
143 m_next_vma(struct proc_maps_private
*priv
, struct vm_area_struct
*vma
)
145 if (vma
== priv
->tail_vma
)
147 return vma
->vm_next
?: priv
->tail_vma
;
150 static void m_cache_vma(struct seq_file
*m
, struct vm_area_struct
*vma
)
152 if (m
->count
< m
->size
) /* vma is copied successfully */
153 m
->version
= m_next_vma(m
->private, vma
) ? vma
->vm_end
: -1UL;
156 static void *m_start(struct seq_file
*m
, loff_t
*ppos
)
158 struct proc_maps_private
*priv
= m
->private;
159 unsigned long last_addr
= m
->version
;
160 struct mm_struct
*mm
;
161 struct vm_area_struct
*vma
;
162 unsigned int pos
= *ppos
;
164 /* See m_cache_vma(). Zero at the start or after lseek. */
165 if (last_addr
== -1UL)
168 priv
->task
= get_proc_task(priv
->inode
);
170 return ERR_PTR(-ESRCH
);
173 if (!mm
|| !mmget_not_zero(mm
))
176 down_read(&mm
->mmap_sem
);
177 hold_task_mempolicy(priv
);
178 priv
->tail_vma
= get_gate_vma(mm
);
181 vma
= find_vma(mm
, last_addr
- 1);
182 if (vma
&& vma
->vm_start
<= last_addr
)
183 vma
= m_next_vma(priv
, vma
);
189 if (pos
< mm
->map_count
) {
190 for (vma
= mm
->mmap
; pos
; pos
--) {
191 m
->version
= vma
->vm_start
;
197 /* we do not bother to update m->version in this case */
198 if (pos
== mm
->map_count
&& priv
->tail_vma
)
199 return priv
->tail_vma
;
205 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
207 struct proc_maps_private
*priv
= m
->private;
208 struct vm_area_struct
*next
;
211 next
= m_next_vma(priv
, v
);
217 static void m_stop(struct seq_file
*m
, void *v
)
219 struct proc_maps_private
*priv
= m
->private;
221 if (!IS_ERR_OR_NULL(v
))
224 put_task_struct(priv
->task
);
229 static int proc_maps_open(struct inode
*inode
, struct file
*file
,
230 const struct seq_operations
*ops
, int psize
)
232 struct proc_maps_private
*priv
= __seq_open_private(file
, ops
, psize
);
238 priv
->mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
239 if (IS_ERR(priv
->mm
)) {
240 int err
= PTR_ERR(priv
->mm
);
242 seq_release_private(inode
, file
);
249 static int proc_map_release(struct inode
*inode
, struct file
*file
)
251 struct seq_file
*seq
= file
->private_data
;
252 struct proc_maps_private
*priv
= seq
->private;
258 return seq_release_private(inode
, file
);
261 static int do_maps_open(struct inode
*inode
, struct file
*file
,
262 const struct seq_operations
*ops
)
264 return proc_maps_open(inode
, file
, ops
,
265 sizeof(struct proc_maps_private
));
269 * Indicate if the VMA is a stack for the given task; for
270 * /proc/PID/maps that is the stack of the main task.
272 static int is_stack(struct vm_area_struct
*vma
)
275 * We make no effort to guess what a given thread considers to be
276 * its "stack". It's not even well-defined for programs written
279 return vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
280 vma
->vm_end
>= vma
->vm_mm
->start_stack
;
283 static void show_vma_header_prefix(struct seq_file
*m
,
284 unsigned long start
, unsigned long end
,
285 vm_flags_t flags
, unsigned long long pgoff
,
286 dev_t dev
, unsigned long ino
)
288 seq_setwidth(m
, 25 + sizeof(void *) * 6 - 1);
289 seq_printf(m
, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
292 flags
& VM_READ
? 'r' : '-',
293 flags
& VM_WRITE
? 'w' : '-',
294 flags
& VM_EXEC
? 'x' : '-',
295 flags
& VM_MAYSHARE
? 's' : 'p',
297 MAJOR(dev
), MINOR(dev
), ino
);
301 show_map_vma(struct seq_file
*m
, struct vm_area_struct
*vma
, int is_pid
)
303 struct mm_struct
*mm
= vma
->vm_mm
;
304 struct file
*file
= vma
->vm_file
;
305 vm_flags_t flags
= vma
->vm_flags
;
306 unsigned long ino
= 0;
307 unsigned long long pgoff
= 0;
308 unsigned long start
, end
;
310 const char *name
= NULL
;
313 struct inode
*inode
= file_inode(vma
->vm_file
);
314 dev
= inode
->i_sb
->s_dev
;
316 pgoff
= ((loff_t
)vma
->vm_pgoff
) << PAGE_SHIFT
;
319 start
= vma
->vm_start
;
321 show_vma_header_prefix(m
, start
, end
, flags
, pgoff
, dev
, ino
);
324 * Print the dentry name for named mappings, and a
325 * special [heap] marker for the heap:
329 seq_file_path(m
, file
, "\n");
333 if (vma
->vm_ops
&& vma
->vm_ops
->name
) {
334 name
= vma
->vm_ops
->name(vma
);
339 name
= arch_vma_name(vma
);
346 if (vma
->vm_start
<= mm
->brk
&&
347 vma
->vm_end
>= mm
->start_brk
) {
364 static int show_map(struct seq_file
*m
, void *v
, int is_pid
)
366 show_map_vma(m
, v
, is_pid
);
371 static int show_pid_map(struct seq_file
*m
, void *v
)
373 return show_map(m
, v
, 1);
376 static int show_tid_map(struct seq_file
*m
, void *v
)
378 return show_map(m
, v
, 0);
381 static const struct seq_operations proc_pid_maps_op
= {
388 static const struct seq_operations proc_tid_maps_op
= {
395 static int pid_maps_open(struct inode
*inode
, struct file
*file
)
397 return do_maps_open(inode
, file
, &proc_pid_maps_op
);
400 static int tid_maps_open(struct inode
*inode
, struct file
*file
)
402 return do_maps_open(inode
, file
, &proc_tid_maps_op
);
405 const struct file_operations proc_pid_maps_operations
= {
406 .open
= pid_maps_open
,
409 .release
= proc_map_release
,
412 const struct file_operations proc_tid_maps_operations
= {
413 .open
= tid_maps_open
,
416 .release
= proc_map_release
,
420 * Proportional Set Size(PSS): my share of RSS.
422 * PSS of a process is the count of pages it has in memory, where each
423 * page is divided by the number of processes sharing it. So if a
424 * process has 1000 pages all to itself, and 1000 shared with one other
425 * process, its PSS will be 1500.
427 * To keep (accumulated) division errors low, we adopt a 64bit
428 * fixed-point pss counter to minimize division errors. So (pss >>
429 * PSS_SHIFT) would be the real byte count.
431 * A shift of 12 before division means (assuming 4K page size):
432 * - 1M 3-user-pages add up to 8KB errors;
433 * - supports mapcount up to 2^24, or 16M;
434 * - supports PSS up to 2^52 bytes, or 4PB.
438 #ifdef CONFIG_PROC_PAGE_MONITOR
439 struct mem_size_stats
{
441 unsigned long resident
;
442 unsigned long shared_clean
;
443 unsigned long shared_dirty
;
444 unsigned long private_clean
;
445 unsigned long private_dirty
;
446 unsigned long referenced
;
447 unsigned long anonymous
;
448 unsigned long lazyfree
;
449 unsigned long anonymous_thp
;
450 unsigned long shmem_thp
;
452 unsigned long shared_hugetlb
;
453 unsigned long private_hugetlb
;
454 unsigned long first_vma_start
;
458 bool check_shmem_swap
;
461 static void smaps_account(struct mem_size_stats
*mss
, struct page
*page
,
462 bool compound
, bool young
, bool dirty
)
464 int i
, nr
= compound
? 1 << compound_order(page
) : 1;
465 unsigned long size
= nr
* PAGE_SIZE
;
467 if (PageAnon(page
)) {
468 mss
->anonymous
+= size
;
469 if (!PageSwapBacked(page
) && !dirty
&& !PageDirty(page
))
470 mss
->lazyfree
+= size
;
473 mss
->resident
+= size
;
474 /* Accumulate the size in pages that have been accessed. */
475 if (young
|| page_is_young(page
) || PageReferenced(page
))
476 mss
->referenced
+= size
;
479 * page_count(page) == 1 guarantees the page is mapped exactly once.
480 * If any subpage of the compound page mapped with PTE it would elevate
483 if (page_count(page
) == 1) {
484 if (dirty
|| PageDirty(page
))
485 mss
->private_dirty
+= size
;
487 mss
->private_clean
+= size
;
488 mss
->pss
+= (u64
)size
<< PSS_SHIFT
;
492 for (i
= 0; i
< nr
; i
++, page
++) {
493 int mapcount
= page_mapcount(page
);
496 if (dirty
|| PageDirty(page
))
497 mss
->shared_dirty
+= PAGE_SIZE
;
499 mss
->shared_clean
+= PAGE_SIZE
;
500 mss
->pss
+= (PAGE_SIZE
<< PSS_SHIFT
) / mapcount
;
502 if (dirty
|| PageDirty(page
))
503 mss
->private_dirty
+= PAGE_SIZE
;
505 mss
->private_clean
+= PAGE_SIZE
;
506 mss
->pss
+= PAGE_SIZE
<< PSS_SHIFT
;
512 static int smaps_pte_hole(unsigned long addr
, unsigned long end
,
513 struct mm_walk
*walk
)
515 struct mem_size_stats
*mss
= walk
->private;
517 mss
->swap
+= shmem_partial_swap_usage(
518 walk
->vma
->vm_file
->f_mapping
, addr
, end
);
524 static void smaps_pte_entry(pte_t
*pte
, unsigned long addr
,
525 struct mm_walk
*walk
)
527 struct mem_size_stats
*mss
= walk
->private;
528 struct vm_area_struct
*vma
= walk
->vma
;
529 struct page
*page
= NULL
;
531 if (pte_present(*pte
)) {
532 page
= vm_normal_page(vma
, addr
, *pte
);
533 } else if (is_swap_pte(*pte
)) {
534 swp_entry_t swpent
= pte_to_swp_entry(*pte
);
536 if (!non_swap_entry(swpent
)) {
539 mss
->swap
+= PAGE_SIZE
;
540 mapcount
= swp_swapcount(swpent
);
542 u64 pss_delta
= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
544 do_div(pss_delta
, mapcount
);
545 mss
->swap_pss
+= pss_delta
;
547 mss
->swap_pss
+= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
549 } else if (is_migration_entry(swpent
))
550 page
= migration_entry_to_page(swpent
);
551 else if (is_device_private_entry(swpent
))
552 page
= device_private_entry_to_page(swpent
);
553 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM
) && mss
->check_shmem_swap
554 && pte_none(*pte
))) {
555 page
= find_get_entry(vma
->vm_file
->f_mapping
,
556 linear_page_index(vma
, addr
));
560 if (radix_tree_exceptional_entry(page
))
561 mss
->swap
+= PAGE_SIZE
;
571 smaps_account(mss
, page
, false, pte_young(*pte
), pte_dirty(*pte
));
574 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
575 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
576 struct mm_walk
*walk
)
578 struct mem_size_stats
*mss
= walk
->private;
579 struct vm_area_struct
*vma
= walk
->vma
;
582 /* FOLL_DUMP will return -EFAULT on huge zero page */
583 page
= follow_trans_huge_pmd(vma
, addr
, pmd
, FOLL_DUMP
);
584 if (IS_ERR_OR_NULL(page
))
587 mss
->anonymous_thp
+= HPAGE_PMD_SIZE
;
588 else if (PageSwapBacked(page
))
589 mss
->shmem_thp
+= HPAGE_PMD_SIZE
;
590 else if (is_zone_device_page(page
))
593 VM_BUG_ON_PAGE(1, page
);
594 smaps_account(mss
, page
, true, pmd_young(*pmd
), pmd_dirty(*pmd
));
597 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
598 struct mm_walk
*walk
)
603 static int smaps_pte_range(pmd_t
*pmd
, unsigned long addr
, unsigned long end
,
604 struct mm_walk
*walk
)
606 struct vm_area_struct
*vma
= walk
->vma
;
610 ptl
= pmd_trans_huge_lock(pmd
, vma
);
612 if (pmd_present(*pmd
))
613 smaps_pmd_entry(pmd
, addr
, walk
);
618 if (pmd_trans_unstable(pmd
))
621 * The mmap_sem held all the way back in m_start() is what
622 * keeps khugepaged out of here and from collapsing things
625 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
626 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
)
627 smaps_pte_entry(pte
, addr
, walk
);
628 pte_unmap_unlock(pte
- 1, ptl
);
634 static void show_smap_vma_flags(struct seq_file
*m
, struct vm_area_struct
*vma
)
637 * Don't forget to update Documentation/ on changes.
639 static const char mnemonics
[BITS_PER_LONG
][2] = {
641 * In case if we meet a flag we don't know about.
643 [0 ... (BITS_PER_LONG
-1)] = "??",
645 [ilog2(VM_READ
)] = "rd",
646 [ilog2(VM_WRITE
)] = "wr",
647 [ilog2(VM_EXEC
)] = "ex",
648 [ilog2(VM_SHARED
)] = "sh",
649 [ilog2(VM_MAYREAD
)] = "mr",
650 [ilog2(VM_MAYWRITE
)] = "mw",
651 [ilog2(VM_MAYEXEC
)] = "me",
652 [ilog2(VM_MAYSHARE
)] = "ms",
653 [ilog2(VM_GROWSDOWN
)] = "gd",
654 [ilog2(VM_PFNMAP
)] = "pf",
655 [ilog2(VM_DENYWRITE
)] = "dw",
656 #ifdef CONFIG_X86_INTEL_MPX
657 [ilog2(VM_MPX
)] = "mp",
659 [ilog2(VM_LOCKED
)] = "lo",
660 [ilog2(VM_IO
)] = "io",
661 [ilog2(VM_SEQ_READ
)] = "sr",
662 [ilog2(VM_RAND_READ
)] = "rr",
663 [ilog2(VM_DONTCOPY
)] = "dc",
664 [ilog2(VM_DONTEXPAND
)] = "de",
665 [ilog2(VM_ACCOUNT
)] = "ac",
666 [ilog2(VM_NORESERVE
)] = "nr",
667 [ilog2(VM_HUGETLB
)] = "ht",
668 [ilog2(VM_ARCH_1
)] = "ar",
669 [ilog2(VM_WIPEONFORK
)] = "wf",
670 [ilog2(VM_DONTDUMP
)] = "dd",
671 #ifdef CONFIG_MEM_SOFT_DIRTY
672 [ilog2(VM_SOFTDIRTY
)] = "sd",
674 [ilog2(VM_MIXEDMAP
)] = "mm",
675 [ilog2(VM_HUGEPAGE
)] = "hg",
676 [ilog2(VM_NOHUGEPAGE
)] = "nh",
677 [ilog2(VM_MERGEABLE
)] = "mg",
678 [ilog2(VM_UFFD_MISSING
)]= "um",
679 [ilog2(VM_UFFD_WP
)] = "uw",
680 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
681 /* These come out via ProtectionKey: */
682 [ilog2(VM_PKEY_BIT0
)] = "",
683 [ilog2(VM_PKEY_BIT1
)] = "",
684 [ilog2(VM_PKEY_BIT2
)] = "",
685 [ilog2(VM_PKEY_BIT3
)] = "",
690 seq_puts(m
, "VmFlags: ");
691 for (i
= 0; i
< BITS_PER_LONG
; i
++) {
692 if (!mnemonics
[i
][0])
694 if (vma
->vm_flags
& (1UL << i
)) {
695 seq_printf(m
, "%c%c ",
696 mnemonics
[i
][0], mnemonics
[i
][1]);
702 #ifdef CONFIG_HUGETLB_PAGE
703 static int smaps_hugetlb_range(pte_t
*pte
, unsigned long hmask
,
704 unsigned long addr
, unsigned long end
,
705 struct mm_walk
*walk
)
707 struct mem_size_stats
*mss
= walk
->private;
708 struct vm_area_struct
*vma
= walk
->vma
;
709 struct page
*page
= NULL
;
711 if (pte_present(*pte
)) {
712 page
= vm_normal_page(vma
, addr
, *pte
);
713 } else if (is_swap_pte(*pte
)) {
714 swp_entry_t swpent
= pte_to_swp_entry(*pte
);
716 if (is_migration_entry(swpent
))
717 page
= migration_entry_to_page(swpent
);
718 else if (is_device_private_entry(swpent
))
719 page
= device_private_entry_to_page(swpent
);
722 int mapcount
= page_mapcount(page
);
725 mss
->shared_hugetlb
+= huge_page_size(hstate_vma(vma
));
727 mss
->private_hugetlb
+= huge_page_size(hstate_vma(vma
));
731 #endif /* HUGETLB_PAGE */
733 void __weak
arch_show_smap(struct seq_file
*m
, struct vm_area_struct
*vma
)
737 static int show_smap(struct seq_file
*m
, void *v
, int is_pid
)
739 struct proc_maps_private
*priv
= m
->private;
740 struct vm_area_struct
*vma
= v
;
741 struct mem_size_stats mss_stack
;
742 struct mem_size_stats
*mss
;
743 struct mm_walk smaps_walk
= {
744 .pmd_entry
= smaps_pte_range
,
745 #ifdef CONFIG_HUGETLB_PAGE
746 .hugetlb_entry
= smaps_hugetlb_range
,
758 mss
->first_vma_start
= vma
->vm_start
;
761 last_vma
= !m_next_vma(priv
, vma
);
764 memset(&mss_stack
, 0, sizeof(mss_stack
));
768 smaps_walk
.private = mss
;
771 if (vma
->vm_file
&& shmem_mapping(vma
->vm_file
->f_mapping
)) {
773 * For shared or readonly shmem mappings we know that all
774 * swapped out pages belong to the shmem object, and we can
775 * obtain the swap value much more efficiently. For private
776 * writable mappings, we might have COW pages that are
777 * not affected by the parent swapped out pages of the shmem
778 * object, so we have to distinguish them during the page walk.
779 * Unless we know that the shmem object (or the part mapped by
780 * our VMA) has no swapped out pages at all.
782 unsigned long shmem_swapped
= shmem_swap_usage(vma
);
784 if (!shmem_swapped
|| (vma
->vm_flags
& VM_SHARED
) ||
785 !(vma
->vm_flags
& VM_WRITE
)) {
786 mss
->swap
= shmem_swapped
;
788 mss
->check_shmem_swap
= true;
789 smaps_walk
.pte_hole
= smaps_pte_hole
;
794 /* mmap_sem is held in m_start */
795 walk_page_vma(vma
, &smaps_walk
);
796 if (vma
->vm_flags
& VM_LOCKED
)
797 mss
->pss_locked
+= mss
->pss
;
800 show_map_vma(m
, vma
, is_pid
);
801 } else if (last_vma
) {
802 show_vma_header_prefix(
803 m
, mss
->first_vma_start
, vma
->vm_end
, 0, 0, 0, 0);
805 seq_puts(m
, "[rollup]\n");
813 "KernelPageSize: %8lu kB\n"
814 "MMUPageSize: %8lu kB\n",
815 (vma
->vm_end
- vma
->vm_start
) >> 10,
816 vma_kernel_pagesize(vma
) >> 10,
817 vma_mmu_pagesize(vma
) >> 10);
820 if (!rollup_mode
|| last_vma
)
824 "Shared_Clean: %8lu kB\n"
825 "Shared_Dirty: %8lu kB\n"
826 "Private_Clean: %8lu kB\n"
827 "Private_Dirty: %8lu kB\n"
828 "Referenced: %8lu kB\n"
829 "Anonymous: %8lu kB\n"
830 "LazyFree: %8lu kB\n"
831 "AnonHugePages: %8lu kB\n"
832 "ShmemPmdMapped: %8lu kB\n"
833 "Shared_Hugetlb: %8lu kB\n"
834 "Private_Hugetlb: %7lu kB\n"
839 (unsigned long)(mss
->pss
>> (10 + PSS_SHIFT
)),
840 mss
->shared_clean
>> 10,
841 mss
->shared_dirty
>> 10,
842 mss
->private_clean
>> 10,
843 mss
->private_dirty
>> 10,
844 mss
->referenced
>> 10,
845 mss
->anonymous
>> 10,
847 mss
->anonymous_thp
>> 10,
848 mss
->shmem_thp
>> 10,
849 mss
->shared_hugetlb
>> 10,
850 mss
->private_hugetlb
>> 10,
852 (unsigned long)(mss
->swap_pss
>> (10 + PSS_SHIFT
)),
853 (unsigned long)(mss
->pss
>> (10 + PSS_SHIFT
)));
856 arch_show_smap(m
, vma
);
857 show_smap_vma_flags(m
, vma
);
863 static int show_pid_smap(struct seq_file
*m
, void *v
)
865 return show_smap(m
, v
, 1);
868 static int show_tid_smap(struct seq_file
*m
, void *v
)
870 return show_smap(m
, v
, 0);
873 static const struct seq_operations proc_pid_smaps_op
= {
877 .show
= show_pid_smap
880 static const struct seq_operations proc_tid_smaps_op
= {
884 .show
= show_tid_smap
887 static int pid_smaps_open(struct inode
*inode
, struct file
*file
)
889 return do_maps_open(inode
, file
, &proc_pid_smaps_op
);
892 static int pid_smaps_rollup_open(struct inode
*inode
, struct file
*file
)
894 struct seq_file
*seq
;
895 struct proc_maps_private
*priv
;
896 int ret
= do_maps_open(inode
, file
, &proc_pid_smaps_op
);
900 seq
= file
->private_data
;
902 priv
->rollup
= kzalloc(sizeof(*priv
->rollup
), GFP_KERNEL
);
904 proc_map_release(inode
, file
);
907 priv
->rollup
->first
= true;
911 static int tid_smaps_open(struct inode
*inode
, struct file
*file
)
913 return do_maps_open(inode
, file
, &proc_tid_smaps_op
);
916 const struct file_operations proc_pid_smaps_operations
= {
917 .open
= pid_smaps_open
,
920 .release
= proc_map_release
,
923 const struct file_operations proc_pid_smaps_rollup_operations
= {
924 .open
= pid_smaps_rollup_open
,
927 .release
= proc_map_release
,
930 const struct file_operations proc_tid_smaps_operations
= {
931 .open
= tid_smaps_open
,
934 .release
= proc_map_release
,
937 enum clear_refs_types
{
941 CLEAR_REFS_SOFT_DIRTY
,
942 CLEAR_REFS_MM_HIWATER_RSS
,
946 struct clear_refs_private
{
947 enum clear_refs_types type
;
950 #ifdef CONFIG_MEM_SOFT_DIRTY
951 static inline void clear_soft_dirty(struct vm_area_struct
*vma
,
952 unsigned long addr
, pte_t
*pte
)
955 * The soft-dirty tracker uses #PF-s to catch writes
956 * to pages, so write-protect the pte as well. See the
957 * Documentation/vm/soft-dirty.txt for full description
958 * of how soft-dirty works.
962 if (pte_present(ptent
)) {
963 ptent
= ptep_modify_prot_start(vma
->vm_mm
, addr
, pte
);
964 ptent
= pte_wrprotect(ptent
);
965 ptent
= pte_clear_soft_dirty(ptent
);
966 ptep_modify_prot_commit(vma
->vm_mm
, addr
, pte
, ptent
);
967 } else if (is_swap_pte(ptent
)) {
968 ptent
= pte_swp_clear_soft_dirty(ptent
);
969 set_pte_at(vma
->vm_mm
, addr
, pte
, ptent
);
973 static inline void clear_soft_dirty(struct vm_area_struct
*vma
,
974 unsigned long addr
, pte_t
*pte
)
979 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
980 static inline void clear_soft_dirty_pmd(struct vm_area_struct
*vma
,
981 unsigned long addr
, pmd_t
*pmdp
)
985 if (pmd_present(pmd
)) {
986 /* See comment in change_huge_pmd() */
987 pmdp_invalidate(vma
, addr
, pmdp
);
988 if (pmd_dirty(*pmdp
))
989 pmd
= pmd_mkdirty(pmd
);
990 if (pmd_young(*pmdp
))
991 pmd
= pmd_mkyoung(pmd
);
993 pmd
= pmd_wrprotect(pmd
);
994 pmd
= pmd_clear_soft_dirty(pmd
);
996 set_pmd_at(vma
->vm_mm
, addr
, pmdp
, pmd
);
997 } else if (is_migration_entry(pmd_to_swp_entry(pmd
))) {
998 pmd
= pmd_swp_clear_soft_dirty(pmd
);
999 set_pmd_at(vma
->vm_mm
, addr
, pmdp
, pmd
);
1003 static inline void clear_soft_dirty_pmd(struct vm_area_struct
*vma
,
1004 unsigned long addr
, pmd_t
*pmdp
)
1009 static int clear_refs_pte_range(pmd_t
*pmd
, unsigned long addr
,
1010 unsigned long end
, struct mm_walk
*walk
)
1012 struct clear_refs_private
*cp
= walk
->private;
1013 struct vm_area_struct
*vma
= walk
->vma
;
1018 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1020 if (cp
->type
== CLEAR_REFS_SOFT_DIRTY
) {
1021 clear_soft_dirty_pmd(vma
, addr
, pmd
);
1025 if (!pmd_present(*pmd
))
1028 page
= pmd_page(*pmd
);
1030 /* Clear accessed and referenced bits. */
1031 pmdp_test_and_clear_young(vma
, addr
, pmd
);
1032 test_and_clear_page_young(page
);
1033 ClearPageReferenced(page
);
1039 if (pmd_trans_unstable(pmd
))
1042 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
1043 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
1046 if (cp
->type
== CLEAR_REFS_SOFT_DIRTY
) {
1047 clear_soft_dirty(vma
, addr
, pte
);
1051 if (!pte_present(ptent
))
1054 page
= vm_normal_page(vma
, addr
, ptent
);
1058 /* Clear accessed and referenced bits. */
1059 ptep_test_and_clear_young(vma
, addr
, pte
);
1060 test_and_clear_page_young(page
);
1061 ClearPageReferenced(page
);
1063 pte_unmap_unlock(pte
- 1, ptl
);
1068 static int clear_refs_test_walk(unsigned long start
, unsigned long end
,
1069 struct mm_walk
*walk
)
1071 struct clear_refs_private
*cp
= walk
->private;
1072 struct vm_area_struct
*vma
= walk
->vma
;
1074 if (vma
->vm_flags
& VM_PFNMAP
)
1078 * Writing 1 to /proc/pid/clear_refs affects all pages.
1079 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1080 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1081 * Writing 4 to /proc/pid/clear_refs affects all pages.
1083 if (cp
->type
== CLEAR_REFS_ANON
&& vma
->vm_file
)
1085 if (cp
->type
== CLEAR_REFS_MAPPED
&& !vma
->vm_file
)
1090 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
1091 size_t count
, loff_t
*ppos
)
1093 struct task_struct
*task
;
1094 char buffer
[PROC_NUMBUF
];
1095 struct mm_struct
*mm
;
1096 struct vm_area_struct
*vma
;
1097 enum clear_refs_types type
;
1098 struct mmu_gather tlb
;
1102 memset(buffer
, 0, sizeof(buffer
));
1103 if (count
> sizeof(buffer
) - 1)
1104 count
= sizeof(buffer
) - 1;
1105 if (copy_from_user(buffer
, buf
, count
))
1107 rv
= kstrtoint(strstrip(buffer
), 10, &itype
);
1110 type
= (enum clear_refs_types
)itype
;
1111 if (type
< CLEAR_REFS_ALL
|| type
>= CLEAR_REFS_LAST
)
1114 task
= get_proc_task(file_inode(file
));
1117 mm
= get_task_mm(task
);
1119 struct clear_refs_private cp
= {
1122 struct mm_walk clear_refs_walk
= {
1123 .pmd_entry
= clear_refs_pte_range
,
1124 .test_walk
= clear_refs_test_walk
,
1129 if (type
== CLEAR_REFS_MM_HIWATER_RSS
) {
1130 if (down_write_killable(&mm
->mmap_sem
)) {
1136 * Writing 5 to /proc/pid/clear_refs resets the peak
1137 * resident set size to this mm's current rss value.
1139 reset_mm_hiwater_rss(mm
);
1140 up_write(&mm
->mmap_sem
);
1144 down_read(&mm
->mmap_sem
);
1145 tlb_gather_mmu(&tlb
, mm
, 0, -1);
1146 if (type
== CLEAR_REFS_SOFT_DIRTY
) {
1147 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1148 if (!(vma
->vm_flags
& VM_SOFTDIRTY
))
1150 up_read(&mm
->mmap_sem
);
1151 if (down_write_killable(&mm
->mmap_sem
)) {
1155 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1156 vma
->vm_flags
&= ~VM_SOFTDIRTY
;
1157 vma_set_page_prot(vma
);
1159 downgrade_write(&mm
->mmap_sem
);
1162 mmu_notifier_invalidate_range_start(mm
, 0, -1);
1164 walk_page_range(0, mm
->highest_vm_end
, &clear_refs_walk
);
1165 if (type
== CLEAR_REFS_SOFT_DIRTY
)
1166 mmu_notifier_invalidate_range_end(mm
, 0, -1);
1167 tlb_finish_mmu(&tlb
, 0, -1);
1168 up_read(&mm
->mmap_sem
);
1172 put_task_struct(task
);
1177 const struct file_operations proc_clear_refs_operations
= {
1178 .write
= clear_refs_write
,
1179 .llseek
= noop_llseek
,
1186 struct pagemapread
{
1187 int pos
, len
; /* units: PM_ENTRY_BYTES, not bytes */
1188 pagemap_entry_t
*buffer
;
1192 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
1193 #define PAGEMAP_WALK_MASK (PMD_MASK)
1195 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1196 #define PM_PFRAME_BITS 55
1197 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1198 #define PM_SOFT_DIRTY BIT_ULL(55)
1199 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1200 #define PM_FILE BIT_ULL(61)
1201 #define PM_SWAP BIT_ULL(62)
1202 #define PM_PRESENT BIT_ULL(63)
1204 #define PM_END_OF_BUFFER 1
1206 static inline pagemap_entry_t
make_pme(u64 frame
, u64 flags
)
1208 return (pagemap_entry_t
) { .pme
= (frame
& PM_PFRAME_MASK
) | flags
};
1211 static int add_to_pagemap(unsigned long addr
, pagemap_entry_t
*pme
,
1212 struct pagemapread
*pm
)
1214 pm
->buffer
[pm
->pos
++] = *pme
;
1215 if (pm
->pos
>= pm
->len
)
1216 return PM_END_OF_BUFFER
;
1220 static int pagemap_pte_hole(unsigned long start
, unsigned long end
,
1221 struct mm_walk
*walk
)
1223 struct pagemapread
*pm
= walk
->private;
1224 unsigned long addr
= start
;
1227 while (addr
< end
) {
1228 struct vm_area_struct
*vma
= find_vma(walk
->mm
, addr
);
1229 pagemap_entry_t pme
= make_pme(0, 0);
1230 /* End of address space hole, which we mark as non-present. */
1231 unsigned long hole_end
;
1234 hole_end
= min(end
, vma
->vm_start
);
1238 for (; addr
< hole_end
; addr
+= PAGE_SIZE
) {
1239 err
= add_to_pagemap(addr
, &pme
, pm
);
1247 /* Addresses in the VMA. */
1248 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1249 pme
= make_pme(0, PM_SOFT_DIRTY
);
1250 for (; addr
< min(end
, vma
->vm_end
); addr
+= PAGE_SIZE
) {
1251 err
= add_to_pagemap(addr
, &pme
, pm
);
1260 static pagemap_entry_t
pte_to_pagemap_entry(struct pagemapread
*pm
,
1261 struct vm_area_struct
*vma
, unsigned long addr
, pte_t pte
)
1263 u64 frame
= 0, flags
= 0;
1264 struct page
*page
= NULL
;
1266 if (pte_present(pte
)) {
1268 frame
= pte_pfn(pte
);
1269 flags
|= PM_PRESENT
;
1270 page
= _vm_normal_page(vma
, addr
, pte
, true);
1271 if (pte_soft_dirty(pte
))
1272 flags
|= PM_SOFT_DIRTY
;
1273 } else if (is_swap_pte(pte
)) {
1275 if (pte_swp_soft_dirty(pte
))
1276 flags
|= PM_SOFT_DIRTY
;
1277 entry
= pte_to_swp_entry(pte
);
1278 frame
= swp_type(entry
) |
1279 (swp_offset(entry
) << MAX_SWAPFILES_SHIFT
);
1281 if (is_migration_entry(entry
))
1282 page
= migration_entry_to_page(entry
);
1284 if (is_device_private_entry(entry
))
1285 page
= device_private_entry_to_page(entry
);
1288 if (page
&& !PageAnon(page
))
1290 if (page
&& page_mapcount(page
) == 1)
1291 flags
|= PM_MMAP_EXCLUSIVE
;
1292 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1293 flags
|= PM_SOFT_DIRTY
;
1295 return make_pme(frame
, flags
);
1298 static int pagemap_pmd_range(pmd_t
*pmdp
, unsigned long addr
, unsigned long end
,
1299 struct mm_walk
*walk
)
1301 struct vm_area_struct
*vma
= walk
->vma
;
1302 struct pagemapread
*pm
= walk
->private;
1304 pte_t
*pte
, *orig_pte
;
1307 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1308 ptl
= pmd_trans_huge_lock(pmdp
, vma
);
1310 u64 flags
= 0, frame
= 0;
1312 struct page
*page
= NULL
;
1314 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1315 flags
|= PM_SOFT_DIRTY
;
1317 if (pmd_present(pmd
)) {
1318 page
= pmd_page(pmd
);
1320 flags
|= PM_PRESENT
;
1321 if (pmd_soft_dirty(pmd
))
1322 flags
|= PM_SOFT_DIRTY
;
1324 frame
= pmd_pfn(pmd
) +
1325 ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1327 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1328 else if (is_swap_pmd(pmd
)) {
1329 swp_entry_t entry
= pmd_to_swp_entry(pmd
);
1331 frame
= swp_type(entry
) |
1332 (swp_offset(entry
) << MAX_SWAPFILES_SHIFT
);
1334 if (pmd_swp_soft_dirty(pmd
))
1335 flags
|= PM_SOFT_DIRTY
;
1336 VM_BUG_ON(!is_pmd_migration_entry(pmd
));
1337 page
= migration_entry_to_page(entry
);
1341 if (page
&& page_mapcount(page
) == 1)
1342 flags
|= PM_MMAP_EXCLUSIVE
;
1344 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1345 pagemap_entry_t pme
= make_pme(frame
, flags
);
1347 err
= add_to_pagemap(addr
, &pme
, pm
);
1350 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1357 if (pmd_trans_unstable(pmdp
))
1359 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1362 * We can assume that @vma always points to a valid one and @end never
1363 * goes beyond vma->vm_end.
1365 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmdp
, addr
, &ptl
);
1366 for (; addr
< end
; pte
++, addr
+= PAGE_SIZE
) {
1367 pagemap_entry_t pme
;
1369 pme
= pte_to_pagemap_entry(pm
, vma
, addr
, *pte
);
1370 err
= add_to_pagemap(addr
, &pme
, pm
);
1374 pte_unmap_unlock(orig_pte
, ptl
);
1381 #ifdef CONFIG_HUGETLB_PAGE
1382 /* This function walks within one hugetlb entry in the single call */
1383 static int pagemap_hugetlb_range(pte_t
*ptep
, unsigned long hmask
,
1384 unsigned long addr
, unsigned long end
,
1385 struct mm_walk
*walk
)
1387 struct pagemapread
*pm
= walk
->private;
1388 struct vm_area_struct
*vma
= walk
->vma
;
1389 u64 flags
= 0, frame
= 0;
1393 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1394 flags
|= PM_SOFT_DIRTY
;
1396 pte
= huge_ptep_get(ptep
);
1397 if (pte_present(pte
)) {
1398 struct page
*page
= pte_page(pte
);
1400 if (!PageAnon(page
))
1403 if (page_mapcount(page
) == 1)
1404 flags
|= PM_MMAP_EXCLUSIVE
;
1406 flags
|= PM_PRESENT
;
1408 frame
= pte_pfn(pte
) +
1409 ((addr
& ~hmask
) >> PAGE_SHIFT
);
1412 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1413 pagemap_entry_t pme
= make_pme(frame
, flags
);
1415 err
= add_to_pagemap(addr
, &pme
, pm
);
1418 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1426 #endif /* HUGETLB_PAGE */
1429 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1431 * For each page in the address space, this file contains one 64-bit entry
1432 * consisting of the following:
1434 * Bits 0-54 page frame number (PFN) if present
1435 * Bits 0-4 swap type if swapped
1436 * Bits 5-54 swap offset if swapped
1437 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1438 * Bit 56 page exclusively mapped
1440 * Bit 61 page is file-page or shared-anon
1441 * Bit 62 page swapped
1442 * Bit 63 page present
1444 * If the page is not present but in swap, then the PFN contains an
1445 * encoding of the swap file number and the page's offset into the
1446 * swap. Unmapped pages return a null PFN. This allows determining
1447 * precisely which pages are mapped (or in swap) and comparing mapped
1448 * pages between processes.
1450 * Efficient users of this interface will use /proc/pid/maps to
1451 * determine which areas of memory are actually mapped and llseek to
1452 * skip over unmapped regions.
1454 static ssize_t
pagemap_read(struct file
*file
, char __user
*buf
,
1455 size_t count
, loff_t
*ppos
)
1457 struct mm_struct
*mm
= file
->private_data
;
1458 struct pagemapread pm
;
1459 struct mm_walk pagemap_walk
= {};
1461 unsigned long svpfn
;
1462 unsigned long start_vaddr
;
1463 unsigned long end_vaddr
;
1464 int ret
= 0, copied
= 0;
1466 if (!mm
|| !mmget_not_zero(mm
))
1470 /* file position must be aligned */
1471 if ((*ppos
% PM_ENTRY_BYTES
) || (count
% PM_ENTRY_BYTES
))
1478 /* do not disclose physical addresses: attack vector */
1479 pm
.show_pfn
= file_ns_capable(file
, &init_user_ns
, CAP_SYS_ADMIN
);
1481 pm
.len
= (PAGEMAP_WALK_SIZE
>> PAGE_SHIFT
);
1482 pm
.buffer
= kmalloc(pm
.len
* PM_ENTRY_BYTES
, GFP_KERNEL
);
1487 pagemap_walk
.pmd_entry
= pagemap_pmd_range
;
1488 pagemap_walk
.pte_hole
= pagemap_pte_hole
;
1489 #ifdef CONFIG_HUGETLB_PAGE
1490 pagemap_walk
.hugetlb_entry
= pagemap_hugetlb_range
;
1492 pagemap_walk
.mm
= mm
;
1493 pagemap_walk
.private = &pm
;
1496 svpfn
= src
/ PM_ENTRY_BYTES
;
1497 start_vaddr
= svpfn
<< PAGE_SHIFT
;
1498 end_vaddr
= mm
->task_size
;
1500 /* watch out for wraparound */
1501 if (svpfn
> mm
->task_size
>> PAGE_SHIFT
)
1502 start_vaddr
= end_vaddr
;
1505 * The odds are that this will stop walking way
1506 * before end_vaddr, because the length of the
1507 * user buffer is tracked in "pm", and the walk
1508 * will stop when we hit the end of the buffer.
1511 while (count
&& (start_vaddr
< end_vaddr
)) {
1516 end
= (start_vaddr
+ PAGEMAP_WALK_SIZE
) & PAGEMAP_WALK_MASK
;
1518 if (end
< start_vaddr
|| end
> end_vaddr
)
1520 down_read(&mm
->mmap_sem
);
1521 ret
= walk_page_range(start_vaddr
, end
, &pagemap_walk
);
1522 up_read(&mm
->mmap_sem
);
1525 len
= min(count
, PM_ENTRY_BYTES
* pm
.pos
);
1526 if (copy_to_user(buf
, pm
.buffer
, len
)) {
1535 if (!ret
|| ret
== PM_END_OF_BUFFER
)
1546 static int pagemap_open(struct inode
*inode
, struct file
*file
)
1548 struct mm_struct
*mm
;
1550 mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
1553 file
->private_data
= mm
;
1557 static int pagemap_release(struct inode
*inode
, struct file
*file
)
1559 struct mm_struct
*mm
= file
->private_data
;
1566 const struct file_operations proc_pagemap_operations
= {
1567 .llseek
= mem_lseek
, /* borrow this */
1568 .read
= pagemap_read
,
1569 .open
= pagemap_open
,
1570 .release
= pagemap_release
,
1572 #endif /* CONFIG_PROC_PAGE_MONITOR */
1577 unsigned long pages
;
1579 unsigned long active
;
1580 unsigned long writeback
;
1581 unsigned long mapcount_max
;
1582 unsigned long dirty
;
1583 unsigned long swapcache
;
1584 unsigned long node
[MAX_NUMNODES
];
1587 struct numa_maps_private
{
1588 struct proc_maps_private proc_maps
;
1589 struct numa_maps md
;
1592 static void gather_stats(struct page
*page
, struct numa_maps
*md
, int pte_dirty
,
1593 unsigned long nr_pages
)
1595 int count
= page_mapcount(page
);
1597 md
->pages
+= nr_pages
;
1598 if (pte_dirty
|| PageDirty(page
))
1599 md
->dirty
+= nr_pages
;
1601 if (PageSwapCache(page
))
1602 md
->swapcache
+= nr_pages
;
1604 if (PageActive(page
) || PageUnevictable(page
))
1605 md
->active
+= nr_pages
;
1607 if (PageWriteback(page
))
1608 md
->writeback
+= nr_pages
;
1611 md
->anon
+= nr_pages
;
1613 if (count
> md
->mapcount_max
)
1614 md
->mapcount_max
= count
;
1616 md
->node
[page_to_nid(page
)] += nr_pages
;
1619 static struct page
*can_gather_numa_stats(pte_t pte
, struct vm_area_struct
*vma
,
1625 if (!pte_present(pte
))
1628 page
= vm_normal_page(vma
, addr
, pte
);
1632 if (PageReserved(page
))
1635 nid
= page_to_nid(page
);
1636 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1642 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1643 static struct page
*can_gather_numa_stats_pmd(pmd_t pmd
,
1644 struct vm_area_struct
*vma
,
1650 if (!pmd_present(pmd
))
1653 page
= vm_normal_page_pmd(vma
, addr
, pmd
);
1657 if (PageReserved(page
))
1660 nid
= page_to_nid(page
);
1661 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1668 static int gather_pte_stats(pmd_t
*pmd
, unsigned long addr
,
1669 unsigned long end
, struct mm_walk
*walk
)
1671 struct numa_maps
*md
= walk
->private;
1672 struct vm_area_struct
*vma
= walk
->vma
;
1677 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1678 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1682 page
= can_gather_numa_stats_pmd(*pmd
, vma
, addr
);
1684 gather_stats(page
, md
, pmd_dirty(*pmd
),
1685 HPAGE_PMD_SIZE
/PAGE_SIZE
);
1690 if (pmd_trans_unstable(pmd
))
1693 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmd
, addr
, &ptl
);
1695 struct page
*page
= can_gather_numa_stats(*pte
, vma
, addr
);
1698 gather_stats(page
, md
, pte_dirty(*pte
), 1);
1700 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1701 pte_unmap_unlock(orig_pte
, ptl
);
1705 #ifdef CONFIG_HUGETLB_PAGE
1706 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1707 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1709 pte_t huge_pte
= huge_ptep_get(pte
);
1710 struct numa_maps
*md
;
1713 if (!pte_present(huge_pte
))
1716 page
= pte_page(huge_pte
);
1721 gather_stats(page
, md
, pte_dirty(huge_pte
), 1);
1726 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1727 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1734 * Display pages allocated per node and memory policy via /proc.
1736 static int show_numa_map(struct seq_file
*m
, void *v
, int is_pid
)
1738 struct numa_maps_private
*numa_priv
= m
->private;
1739 struct proc_maps_private
*proc_priv
= &numa_priv
->proc_maps
;
1740 struct vm_area_struct
*vma
= v
;
1741 struct numa_maps
*md
= &numa_priv
->md
;
1742 struct file
*file
= vma
->vm_file
;
1743 struct mm_struct
*mm
= vma
->vm_mm
;
1744 struct mm_walk walk
= {
1745 .hugetlb_entry
= gather_hugetlb_stats
,
1746 .pmd_entry
= gather_pte_stats
,
1750 struct mempolicy
*pol
;
1757 /* Ensure we start with an empty set of numa_maps statistics. */
1758 memset(md
, 0, sizeof(*md
));
1760 pol
= __get_vma_policy(vma
, vma
->vm_start
);
1762 mpol_to_str(buffer
, sizeof(buffer
), pol
);
1765 mpol_to_str(buffer
, sizeof(buffer
), proc_priv
->task_mempolicy
);
1768 seq_printf(m
, "%08lx %s", vma
->vm_start
, buffer
);
1771 seq_puts(m
, " file=");
1772 seq_file_path(m
, file
, "\n\t= ");
1773 } else if (vma
->vm_start
<= mm
->brk
&& vma
->vm_end
>= mm
->start_brk
) {
1774 seq_puts(m
, " heap");
1775 } else if (is_stack(vma
)) {
1776 seq_puts(m
, " stack");
1779 if (is_vm_hugetlb_page(vma
))
1780 seq_puts(m
, " huge");
1782 /* mmap_sem is held by m_start */
1783 walk_page_vma(vma
, &walk
);
1789 seq_printf(m
, " anon=%lu", md
->anon
);
1792 seq_printf(m
, " dirty=%lu", md
->dirty
);
1794 if (md
->pages
!= md
->anon
&& md
->pages
!= md
->dirty
)
1795 seq_printf(m
, " mapped=%lu", md
->pages
);
1797 if (md
->mapcount_max
> 1)
1798 seq_printf(m
, " mapmax=%lu", md
->mapcount_max
);
1801 seq_printf(m
, " swapcache=%lu", md
->swapcache
);
1803 if (md
->active
< md
->pages
&& !is_vm_hugetlb_page(vma
))
1804 seq_printf(m
, " active=%lu", md
->active
);
1807 seq_printf(m
, " writeback=%lu", md
->writeback
);
1809 for_each_node_state(nid
, N_MEMORY
)
1811 seq_printf(m
, " N%d=%lu", nid
, md
->node
[nid
]);
1813 seq_printf(m
, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma
) >> 10);
1816 m_cache_vma(m
, vma
);
1820 static int show_pid_numa_map(struct seq_file
*m
, void *v
)
1822 return show_numa_map(m
, v
, 1);
1825 static int show_tid_numa_map(struct seq_file
*m
, void *v
)
1827 return show_numa_map(m
, v
, 0);
1830 static const struct seq_operations proc_pid_numa_maps_op
= {
1834 .show
= show_pid_numa_map
,
1837 static const struct seq_operations proc_tid_numa_maps_op
= {
1841 .show
= show_tid_numa_map
,
1844 static int numa_maps_open(struct inode
*inode
, struct file
*file
,
1845 const struct seq_operations
*ops
)
1847 return proc_maps_open(inode
, file
, ops
,
1848 sizeof(struct numa_maps_private
));
1851 static int pid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1853 return numa_maps_open(inode
, file
, &proc_pid_numa_maps_op
);
1856 static int tid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1858 return numa_maps_open(inode
, file
, &proc_tid_numa_maps_op
);
1861 const struct file_operations proc_pid_numa_maps_operations
= {
1862 .open
= pid_numa_maps_open
,
1864 .llseek
= seq_lseek
,
1865 .release
= proc_map_release
,
1868 const struct file_operations proc_tid_numa_maps_operations
= {
1869 .open
= tid_numa_maps_open
,
1871 .llseek
= seq_lseek
,
1872 .release
= proc_map_release
,
1874 #endif /* CONFIG_NUMA */