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
, 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
);
62 "RssShmem:\t%8lu kB\n"
69 hiwater_vm
<< (PAGE_SHIFT
-10),
70 total_vm
<< (PAGE_SHIFT
-10),
71 mm
->locked_vm
<< (PAGE_SHIFT
-10),
72 mm
->pinned_vm
<< (PAGE_SHIFT
-10),
73 hiwater_rss
<< (PAGE_SHIFT
-10),
74 total_rss
<< (PAGE_SHIFT
-10),
75 anon
<< (PAGE_SHIFT
-10),
76 file
<< (PAGE_SHIFT
-10),
77 shmem
<< (PAGE_SHIFT
-10),
78 mm
->data_vm
<< (PAGE_SHIFT
-10),
79 mm
->stack_vm
<< (PAGE_SHIFT
-10), text
, lib
,
80 mm_pgtables_bytes(mm
) >> 10,
81 swap
<< (PAGE_SHIFT
-10));
82 hugetlb_report_usage(m
, mm
);
85 unsigned long task_vsize(struct mm_struct
*mm
)
87 return PAGE_SIZE
* mm
->total_vm
;
90 unsigned long task_statm(struct mm_struct
*mm
,
91 unsigned long *shared
, unsigned long *text
,
92 unsigned long *data
, unsigned long *resident
)
94 *shared
= get_mm_counter(mm
, MM_FILEPAGES
) +
95 get_mm_counter(mm
, MM_SHMEMPAGES
);
96 *text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
))
98 *data
= mm
->data_vm
+ mm
->stack_vm
;
99 *resident
= *shared
+ get_mm_counter(mm
, MM_ANONPAGES
);
105 * Save get_task_policy() for show_numa_map().
107 static void hold_task_mempolicy(struct proc_maps_private
*priv
)
109 struct task_struct
*task
= priv
->task
;
112 priv
->task_mempolicy
= get_task_policy(task
);
113 mpol_get(priv
->task_mempolicy
);
116 static void release_task_mempolicy(struct proc_maps_private
*priv
)
118 mpol_put(priv
->task_mempolicy
);
121 static void hold_task_mempolicy(struct proc_maps_private
*priv
)
124 static void release_task_mempolicy(struct proc_maps_private
*priv
)
129 static void vma_stop(struct proc_maps_private
*priv
)
131 struct mm_struct
*mm
= priv
->mm
;
133 release_task_mempolicy(priv
);
134 up_read(&mm
->mmap_sem
);
138 static struct vm_area_struct
*
139 m_next_vma(struct proc_maps_private
*priv
, struct vm_area_struct
*vma
)
141 if (vma
== priv
->tail_vma
)
143 return vma
->vm_next
?: priv
->tail_vma
;
146 static void m_cache_vma(struct seq_file
*m
, struct vm_area_struct
*vma
)
148 if (m
->count
< m
->size
) /* vma is copied successfully */
149 m
->version
= m_next_vma(m
->private, vma
) ? vma
->vm_end
: -1UL;
152 static void *m_start(struct seq_file
*m
, loff_t
*ppos
)
154 struct proc_maps_private
*priv
= m
->private;
155 unsigned long last_addr
= m
->version
;
156 struct mm_struct
*mm
;
157 struct vm_area_struct
*vma
;
158 unsigned int pos
= *ppos
;
160 /* See m_cache_vma(). Zero at the start or after lseek. */
161 if (last_addr
== -1UL)
164 priv
->task
= get_proc_task(priv
->inode
);
166 return ERR_PTR(-ESRCH
);
169 if (!mm
|| !mmget_not_zero(mm
))
172 down_read(&mm
->mmap_sem
);
173 hold_task_mempolicy(priv
);
174 priv
->tail_vma
= get_gate_vma(mm
);
177 vma
= find_vma(mm
, last_addr
- 1);
178 if (vma
&& vma
->vm_start
<= last_addr
)
179 vma
= m_next_vma(priv
, vma
);
185 if (pos
< mm
->map_count
) {
186 for (vma
= mm
->mmap
; pos
; pos
--) {
187 m
->version
= vma
->vm_start
;
193 /* we do not bother to update m->version in this case */
194 if (pos
== mm
->map_count
&& priv
->tail_vma
)
195 return priv
->tail_vma
;
201 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
203 struct proc_maps_private
*priv
= m
->private;
204 struct vm_area_struct
*next
;
207 next
= m_next_vma(priv
, v
);
213 static void m_stop(struct seq_file
*m
, void *v
)
215 struct proc_maps_private
*priv
= m
->private;
217 if (!IS_ERR_OR_NULL(v
))
220 put_task_struct(priv
->task
);
225 static int proc_maps_open(struct inode
*inode
, struct file
*file
,
226 const struct seq_operations
*ops
, int psize
)
228 struct proc_maps_private
*priv
= __seq_open_private(file
, ops
, psize
);
234 priv
->mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
235 if (IS_ERR(priv
->mm
)) {
236 int err
= PTR_ERR(priv
->mm
);
238 seq_release_private(inode
, file
);
245 static int proc_map_release(struct inode
*inode
, struct file
*file
)
247 struct seq_file
*seq
= file
->private_data
;
248 struct proc_maps_private
*priv
= seq
->private;
254 return seq_release_private(inode
, file
);
257 static int do_maps_open(struct inode
*inode
, struct file
*file
,
258 const struct seq_operations
*ops
)
260 return proc_maps_open(inode
, file
, ops
,
261 sizeof(struct proc_maps_private
));
265 * Indicate if the VMA is a stack for the given task; for
266 * /proc/PID/maps that is the stack of the main task.
268 static int is_stack(struct vm_area_struct
*vma
)
271 * We make no effort to guess what a given thread considers to be
272 * its "stack". It's not even well-defined for programs written
275 return vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
276 vma
->vm_end
>= vma
->vm_mm
->start_stack
;
279 static void show_vma_header_prefix(struct seq_file
*m
,
280 unsigned long start
, unsigned long end
,
281 vm_flags_t flags
, unsigned long long pgoff
,
282 dev_t dev
, unsigned long ino
)
284 seq_setwidth(m
, 25 + sizeof(void *) * 6 - 1);
285 seq_printf(m
, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
288 flags
& VM_READ
? 'r' : '-',
289 flags
& VM_WRITE
? 'w' : '-',
290 flags
& VM_EXEC
? 'x' : '-',
291 flags
& VM_MAYSHARE
? 's' : 'p',
293 MAJOR(dev
), MINOR(dev
), ino
);
297 show_map_vma(struct seq_file
*m
, struct vm_area_struct
*vma
, int is_pid
)
299 struct mm_struct
*mm
= vma
->vm_mm
;
300 struct file
*file
= vma
->vm_file
;
301 vm_flags_t flags
= vma
->vm_flags
;
302 unsigned long ino
= 0;
303 unsigned long long pgoff
= 0;
304 unsigned long start
, end
;
306 const char *name
= NULL
;
311 file
= vma_pr_or_file(vma
);
312 inode
= file_inode(file
);
313 dev
= inode
->i_sb
->s_dev
;
315 pgoff
= ((loff_t
)vma
->vm_pgoff
) << PAGE_SHIFT
;
318 start
= vma
->vm_start
;
320 show_vma_header_prefix(m
, start
, end
, flags
, pgoff
, dev
, ino
);
323 * Print the dentry name for named mappings, and a
324 * special [heap] marker for the heap:
328 seq_file_path(m
, file
, "\n");
332 if (vma
->vm_ops
&& vma
->vm_ops
->name
) {
333 name
= vma
->vm_ops
->name(vma
);
338 name
= arch_vma_name(vma
);
345 if (vma
->vm_start
<= mm
->brk
&&
346 vma
->vm_end
>= mm
->start_brk
) {
363 static int show_map(struct seq_file
*m
, void *v
, int is_pid
)
365 show_map_vma(m
, v
, is_pid
);
370 static int show_pid_map(struct seq_file
*m
, void *v
)
372 return show_map(m
, v
, 1);
375 static int show_tid_map(struct seq_file
*m
, void *v
)
377 return show_map(m
, v
, 0);
380 static const struct seq_operations proc_pid_maps_op
= {
387 static const struct seq_operations proc_tid_maps_op
= {
394 static int pid_maps_open(struct inode
*inode
, struct file
*file
)
396 return do_maps_open(inode
, file
, &proc_pid_maps_op
);
399 static int tid_maps_open(struct inode
*inode
, struct file
*file
)
401 return do_maps_open(inode
, file
, &proc_tid_maps_op
);
404 const struct file_operations proc_pid_maps_operations
= {
405 .open
= pid_maps_open
,
408 .release
= proc_map_release
,
411 const struct file_operations proc_tid_maps_operations
= {
412 .open
= tid_maps_open
,
415 .release
= proc_map_release
,
419 * Proportional Set Size(PSS): my share of RSS.
421 * PSS of a process is the count of pages it has in memory, where each
422 * page is divided by the number of processes sharing it. So if a
423 * process has 1000 pages all to itself, and 1000 shared with one other
424 * process, its PSS will be 1500.
426 * To keep (accumulated) division errors low, we adopt a 64bit
427 * fixed-point pss counter to minimize division errors. So (pss >>
428 * PSS_SHIFT) would be the real byte count.
430 * A shift of 12 before division means (assuming 4K page size):
431 * - 1M 3-user-pages add up to 8KB errors;
432 * - supports mapcount up to 2^24, or 16M;
433 * - supports PSS up to 2^52 bytes, or 4PB.
437 #ifdef CONFIG_PROC_PAGE_MONITOR
438 struct mem_size_stats
{
440 unsigned long resident
;
441 unsigned long shared_clean
;
442 unsigned long shared_dirty
;
443 unsigned long private_clean
;
444 unsigned long private_dirty
;
445 unsigned long referenced
;
446 unsigned long anonymous
;
447 unsigned long lazyfree
;
448 unsigned long anonymous_thp
;
449 unsigned long shmem_thp
;
451 unsigned long shared_hugetlb
;
452 unsigned long private_hugetlb
;
453 unsigned long first_vma_start
;
457 bool check_shmem_swap
;
460 static void smaps_account(struct mem_size_stats
*mss
, struct page
*page
,
461 bool compound
, bool young
, bool dirty
)
463 int i
, nr
= compound
? 1 << compound_order(page
) : 1;
464 unsigned long size
= nr
* PAGE_SIZE
;
466 if (PageAnon(page
)) {
467 mss
->anonymous
+= size
;
468 if (!PageSwapBacked(page
) && !dirty
&& !PageDirty(page
))
469 mss
->lazyfree
+= size
;
472 mss
->resident
+= size
;
473 /* Accumulate the size in pages that have been accessed. */
474 if (young
|| page_is_young(page
) || PageReferenced(page
))
475 mss
->referenced
+= size
;
478 * page_count(page) == 1 guarantees the page is mapped exactly once.
479 * If any subpage of the compound page mapped with PTE it would elevate
482 if (page_count(page
) == 1) {
483 if (dirty
|| PageDirty(page
))
484 mss
->private_dirty
+= size
;
486 mss
->private_clean
+= size
;
487 mss
->pss
+= (u64
)size
<< PSS_SHIFT
;
491 for (i
= 0; i
< nr
; i
++, page
++) {
492 int mapcount
= page_mapcount(page
);
495 if (dirty
|| PageDirty(page
))
496 mss
->shared_dirty
+= PAGE_SIZE
;
498 mss
->shared_clean
+= PAGE_SIZE
;
499 mss
->pss
+= (PAGE_SIZE
<< PSS_SHIFT
) / mapcount
;
501 if (dirty
|| PageDirty(page
))
502 mss
->private_dirty
+= PAGE_SIZE
;
504 mss
->private_clean
+= PAGE_SIZE
;
505 mss
->pss
+= PAGE_SIZE
<< PSS_SHIFT
;
511 static int smaps_pte_hole(unsigned long addr
, unsigned long end
,
512 struct mm_walk
*walk
)
514 struct mem_size_stats
*mss
= walk
->private;
516 mss
->swap
+= shmem_partial_swap_usage(
517 walk
->vma
->vm_file
->f_mapping
, addr
, end
);
523 static void smaps_pte_entry(pte_t
*pte
, unsigned long addr
,
524 struct mm_walk
*walk
)
526 struct mem_size_stats
*mss
= walk
->private;
527 struct vm_area_struct
*vma
= walk
->vma
;
528 struct page
*page
= NULL
;
530 if (pte_present(*pte
)) {
531 page
= vm_normal_page(vma
, addr
, *pte
);
532 } else if (is_swap_pte(*pte
)) {
533 swp_entry_t swpent
= pte_to_swp_entry(*pte
);
535 if (!non_swap_entry(swpent
)) {
538 mss
->swap
+= PAGE_SIZE
;
539 mapcount
= swp_swapcount(swpent
);
541 u64 pss_delta
= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
543 do_div(pss_delta
, mapcount
);
544 mss
->swap_pss
+= pss_delta
;
546 mss
->swap_pss
+= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
548 } else if (is_migration_entry(swpent
))
549 page
= migration_entry_to_page(swpent
);
550 else if (is_device_private_entry(swpent
))
551 page
= device_private_entry_to_page(swpent
);
552 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM
) && mss
->check_shmem_swap
553 && pte_none(*pte
))) {
554 page
= find_get_entry(vma
->vm_file
->f_mapping
,
555 linear_page_index(vma
, addr
));
559 if (radix_tree_exceptional_entry(page
))
560 mss
->swap
+= PAGE_SIZE
;
570 smaps_account(mss
, page
, false, pte_young(*pte
), pte_dirty(*pte
));
573 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
574 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
575 struct mm_walk
*walk
)
577 struct mem_size_stats
*mss
= walk
->private;
578 struct vm_area_struct
*vma
= walk
->vma
;
581 /* FOLL_DUMP will return -EFAULT on huge zero page */
582 page
= follow_trans_huge_pmd(vma
, addr
, pmd
, FOLL_DUMP
);
583 if (IS_ERR_OR_NULL(page
))
586 mss
->anonymous_thp
+= HPAGE_PMD_SIZE
;
587 else if (PageSwapBacked(page
))
588 mss
->shmem_thp
+= HPAGE_PMD_SIZE
;
589 else if (is_zone_device_page(page
))
592 VM_BUG_ON_PAGE(1, page
);
593 smaps_account(mss
, page
, true, pmd_young(*pmd
), pmd_dirty(*pmd
));
596 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
597 struct mm_walk
*walk
)
602 static int smaps_pte_range(pmd_t
*pmd
, unsigned long addr
, unsigned long end
,
603 struct mm_walk
*walk
)
605 struct vm_area_struct
*vma
= walk
->vma
;
609 ptl
= pmd_trans_huge_lock(pmd
, vma
);
611 if (pmd_present(*pmd
))
612 smaps_pmd_entry(pmd
, addr
, walk
);
617 if (pmd_trans_unstable(pmd
))
620 * The mmap_sem held all the way back in m_start() is what
621 * keeps khugepaged out of here and from collapsing things
624 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
625 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
)
626 smaps_pte_entry(pte
, addr
, walk
);
627 pte_unmap_unlock(pte
- 1, ptl
);
633 static void show_smap_vma_flags(struct seq_file
*m
, struct vm_area_struct
*vma
)
636 * Don't forget to update Documentation/ on changes.
638 static const char mnemonics
[BITS_PER_LONG
][2] = {
640 * In case if we meet a flag we don't know about.
642 [0 ... (BITS_PER_LONG
-1)] = "??",
644 [ilog2(VM_READ
)] = "rd",
645 [ilog2(VM_WRITE
)] = "wr",
646 [ilog2(VM_EXEC
)] = "ex",
647 [ilog2(VM_SHARED
)] = "sh",
648 [ilog2(VM_MAYREAD
)] = "mr",
649 [ilog2(VM_MAYWRITE
)] = "mw",
650 [ilog2(VM_MAYEXEC
)] = "me",
651 [ilog2(VM_MAYSHARE
)] = "ms",
652 [ilog2(VM_GROWSDOWN
)] = "gd",
653 [ilog2(VM_PFNMAP
)] = "pf",
654 [ilog2(VM_DENYWRITE
)] = "dw",
655 #ifdef CONFIG_X86_INTEL_MPX
656 [ilog2(VM_MPX
)] = "mp",
658 [ilog2(VM_LOCKED
)] = "lo",
659 [ilog2(VM_IO
)] = "io",
660 [ilog2(VM_SEQ_READ
)] = "sr",
661 [ilog2(VM_RAND_READ
)] = "rr",
662 [ilog2(VM_DONTCOPY
)] = "dc",
663 [ilog2(VM_DONTEXPAND
)] = "de",
664 [ilog2(VM_ACCOUNT
)] = "ac",
665 [ilog2(VM_NORESERVE
)] = "nr",
666 [ilog2(VM_HUGETLB
)] = "ht",
667 [ilog2(VM_SYNC
)] = "sf",
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
);
1330 unsigned long offset
= swp_offset(entry
);
1332 offset
+= (addr
& ~PMD_MASK
) >> PAGE_SHIFT
;
1333 frame
= swp_type(entry
) |
1334 (offset
<< MAX_SWAPFILES_SHIFT
);
1336 if (pmd_swp_soft_dirty(pmd
))
1337 flags
|= PM_SOFT_DIRTY
;
1338 VM_BUG_ON(!is_pmd_migration_entry(pmd
));
1339 page
= migration_entry_to_page(entry
);
1343 if (page
&& page_mapcount(page
) == 1)
1344 flags
|= PM_MMAP_EXCLUSIVE
;
1346 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1347 pagemap_entry_t pme
= make_pme(frame
, flags
);
1349 err
= add_to_pagemap(addr
, &pme
, pm
);
1352 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1354 else if (flags
& PM_SWAP
)
1355 frame
+= (1 << MAX_SWAPFILES_SHIFT
);
1361 if (pmd_trans_unstable(pmdp
))
1363 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1366 * We can assume that @vma always points to a valid one and @end never
1367 * goes beyond vma->vm_end.
1369 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmdp
, addr
, &ptl
);
1370 for (; addr
< end
; pte
++, addr
+= PAGE_SIZE
) {
1371 pagemap_entry_t pme
;
1373 pme
= pte_to_pagemap_entry(pm
, vma
, addr
, *pte
);
1374 err
= add_to_pagemap(addr
, &pme
, pm
);
1378 pte_unmap_unlock(orig_pte
, ptl
);
1385 #ifdef CONFIG_HUGETLB_PAGE
1386 /* This function walks within one hugetlb entry in the single call */
1387 static int pagemap_hugetlb_range(pte_t
*ptep
, unsigned long hmask
,
1388 unsigned long addr
, unsigned long end
,
1389 struct mm_walk
*walk
)
1391 struct pagemapread
*pm
= walk
->private;
1392 struct vm_area_struct
*vma
= walk
->vma
;
1393 u64 flags
= 0, frame
= 0;
1397 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1398 flags
|= PM_SOFT_DIRTY
;
1400 pte
= huge_ptep_get(ptep
);
1401 if (pte_present(pte
)) {
1402 struct page
*page
= pte_page(pte
);
1404 if (!PageAnon(page
))
1407 if (page_mapcount(page
) == 1)
1408 flags
|= PM_MMAP_EXCLUSIVE
;
1410 flags
|= PM_PRESENT
;
1412 frame
= pte_pfn(pte
) +
1413 ((addr
& ~hmask
) >> PAGE_SHIFT
);
1416 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1417 pagemap_entry_t pme
= make_pme(frame
, flags
);
1419 err
= add_to_pagemap(addr
, &pme
, pm
);
1422 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1430 #endif /* HUGETLB_PAGE */
1433 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1435 * For each page in the address space, this file contains one 64-bit entry
1436 * consisting of the following:
1438 * Bits 0-54 page frame number (PFN) if present
1439 * Bits 0-4 swap type if swapped
1440 * Bits 5-54 swap offset if swapped
1441 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1442 * Bit 56 page exclusively mapped
1444 * Bit 61 page is file-page or shared-anon
1445 * Bit 62 page swapped
1446 * Bit 63 page present
1448 * If the page is not present but in swap, then the PFN contains an
1449 * encoding of the swap file number and the page's offset into the
1450 * swap. Unmapped pages return a null PFN. This allows determining
1451 * precisely which pages are mapped (or in swap) and comparing mapped
1452 * pages between processes.
1454 * Efficient users of this interface will use /proc/pid/maps to
1455 * determine which areas of memory are actually mapped and llseek to
1456 * skip over unmapped regions.
1458 static ssize_t
pagemap_read(struct file
*file
, char __user
*buf
,
1459 size_t count
, loff_t
*ppos
)
1461 struct mm_struct
*mm
= file
->private_data
;
1462 struct pagemapread pm
;
1463 struct mm_walk pagemap_walk
= {};
1465 unsigned long svpfn
;
1466 unsigned long start_vaddr
;
1467 unsigned long end_vaddr
;
1468 int ret
= 0, copied
= 0;
1470 if (!mm
|| !mmget_not_zero(mm
))
1474 /* file position must be aligned */
1475 if ((*ppos
% PM_ENTRY_BYTES
) || (count
% PM_ENTRY_BYTES
))
1482 /* do not disclose physical addresses: attack vector */
1483 pm
.show_pfn
= file_ns_capable(file
, &init_user_ns
, CAP_SYS_ADMIN
);
1485 pm
.len
= (PAGEMAP_WALK_SIZE
>> PAGE_SHIFT
);
1486 pm
.buffer
= kmalloc(pm
.len
* PM_ENTRY_BYTES
, GFP_KERNEL
);
1491 pagemap_walk
.pmd_entry
= pagemap_pmd_range
;
1492 pagemap_walk
.pte_hole
= pagemap_pte_hole
;
1493 #ifdef CONFIG_HUGETLB_PAGE
1494 pagemap_walk
.hugetlb_entry
= pagemap_hugetlb_range
;
1496 pagemap_walk
.mm
= mm
;
1497 pagemap_walk
.private = &pm
;
1500 svpfn
= src
/ PM_ENTRY_BYTES
;
1501 start_vaddr
= svpfn
<< PAGE_SHIFT
;
1502 end_vaddr
= mm
->task_size
;
1504 /* watch out for wraparound */
1505 if (svpfn
> mm
->task_size
>> PAGE_SHIFT
)
1506 start_vaddr
= end_vaddr
;
1509 * The odds are that this will stop walking way
1510 * before end_vaddr, because the length of the
1511 * user buffer is tracked in "pm", and the walk
1512 * will stop when we hit the end of the buffer.
1515 while (count
&& (start_vaddr
< end_vaddr
)) {
1520 end
= (start_vaddr
+ PAGEMAP_WALK_SIZE
) & PAGEMAP_WALK_MASK
;
1522 if (end
< start_vaddr
|| end
> end_vaddr
)
1524 down_read(&mm
->mmap_sem
);
1525 ret
= walk_page_range(start_vaddr
, end
, &pagemap_walk
);
1526 up_read(&mm
->mmap_sem
);
1529 len
= min(count
, PM_ENTRY_BYTES
* pm
.pos
);
1530 if (copy_to_user(buf
, pm
.buffer
, len
)) {
1539 if (!ret
|| ret
== PM_END_OF_BUFFER
)
1550 static int pagemap_open(struct inode
*inode
, struct file
*file
)
1552 struct mm_struct
*mm
;
1554 mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
1557 file
->private_data
= mm
;
1561 static int pagemap_release(struct inode
*inode
, struct file
*file
)
1563 struct mm_struct
*mm
= file
->private_data
;
1570 const struct file_operations proc_pagemap_operations
= {
1571 .llseek
= mem_lseek
, /* borrow this */
1572 .read
= pagemap_read
,
1573 .open
= pagemap_open
,
1574 .release
= pagemap_release
,
1576 #endif /* CONFIG_PROC_PAGE_MONITOR */
1581 unsigned long pages
;
1583 unsigned long active
;
1584 unsigned long writeback
;
1585 unsigned long mapcount_max
;
1586 unsigned long dirty
;
1587 unsigned long swapcache
;
1588 unsigned long node
[MAX_NUMNODES
];
1591 struct numa_maps_private
{
1592 struct proc_maps_private proc_maps
;
1593 struct numa_maps md
;
1596 static void gather_stats(struct page
*page
, struct numa_maps
*md
, int pte_dirty
,
1597 unsigned long nr_pages
)
1599 int count
= page_mapcount(page
);
1601 md
->pages
+= nr_pages
;
1602 if (pte_dirty
|| PageDirty(page
))
1603 md
->dirty
+= nr_pages
;
1605 if (PageSwapCache(page
))
1606 md
->swapcache
+= nr_pages
;
1608 if (PageActive(page
) || PageUnevictable(page
))
1609 md
->active
+= nr_pages
;
1611 if (PageWriteback(page
))
1612 md
->writeback
+= nr_pages
;
1615 md
->anon
+= nr_pages
;
1617 if (count
> md
->mapcount_max
)
1618 md
->mapcount_max
= count
;
1620 md
->node
[page_to_nid(page
)] += nr_pages
;
1623 static struct page
*can_gather_numa_stats(pte_t pte
, struct vm_area_struct
*vma
,
1629 if (!pte_present(pte
))
1632 page
= vm_normal_page(vma
, addr
, pte
);
1636 if (PageReserved(page
))
1639 nid
= page_to_nid(page
);
1640 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1646 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1647 static struct page
*can_gather_numa_stats_pmd(pmd_t pmd
,
1648 struct vm_area_struct
*vma
,
1654 if (!pmd_present(pmd
))
1657 page
= vm_normal_page_pmd(vma
, addr
, pmd
);
1661 if (PageReserved(page
))
1664 nid
= page_to_nid(page
);
1665 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1672 static int gather_pte_stats(pmd_t
*pmd
, unsigned long addr
,
1673 unsigned long end
, struct mm_walk
*walk
)
1675 struct numa_maps
*md
= walk
->private;
1676 struct vm_area_struct
*vma
= walk
->vma
;
1681 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1682 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1686 page
= can_gather_numa_stats_pmd(*pmd
, vma
, addr
);
1688 gather_stats(page
, md
, pmd_dirty(*pmd
),
1689 HPAGE_PMD_SIZE
/PAGE_SIZE
);
1694 if (pmd_trans_unstable(pmd
))
1697 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmd
, addr
, &ptl
);
1699 struct page
*page
= can_gather_numa_stats(*pte
, vma
, addr
);
1702 gather_stats(page
, md
, pte_dirty(*pte
), 1);
1704 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1705 pte_unmap_unlock(orig_pte
, ptl
);
1709 #ifdef CONFIG_HUGETLB_PAGE
1710 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1711 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1713 pte_t huge_pte
= huge_ptep_get(pte
);
1714 struct numa_maps
*md
;
1717 if (!pte_present(huge_pte
))
1720 page
= pte_page(huge_pte
);
1725 gather_stats(page
, md
, pte_dirty(huge_pte
), 1);
1730 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1731 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1738 * Display pages allocated per node and memory policy via /proc.
1740 static int show_numa_map(struct seq_file
*m
, void *v
, int is_pid
)
1742 struct numa_maps_private
*numa_priv
= m
->private;
1743 struct proc_maps_private
*proc_priv
= &numa_priv
->proc_maps
;
1744 struct vm_area_struct
*vma
= v
;
1745 struct numa_maps
*md
= &numa_priv
->md
;
1746 struct file
*file
= vma_pr_or_file(vma
);
1747 struct mm_struct
*mm
= vma
->vm_mm
;
1748 struct mm_walk walk
= {
1749 .hugetlb_entry
= gather_hugetlb_stats
,
1750 .pmd_entry
= gather_pte_stats
,
1754 struct mempolicy
*pol
;
1761 /* Ensure we start with an empty set of numa_maps statistics. */
1762 memset(md
, 0, sizeof(*md
));
1764 pol
= __get_vma_policy(vma
, vma
->vm_start
);
1766 mpol_to_str(buffer
, sizeof(buffer
), pol
);
1769 mpol_to_str(buffer
, sizeof(buffer
), proc_priv
->task_mempolicy
);
1772 seq_printf(m
, "%08lx %s", vma
->vm_start
, buffer
);
1775 seq_puts(m
, " file=");
1776 seq_file_path(m
, file
, "\n\t= ");
1777 } else if (vma
->vm_start
<= mm
->brk
&& vma
->vm_end
>= mm
->start_brk
) {
1778 seq_puts(m
, " heap");
1779 } else if (is_stack(vma
)) {
1780 seq_puts(m
, " stack");
1783 if (is_vm_hugetlb_page(vma
))
1784 seq_puts(m
, " huge");
1786 /* mmap_sem is held by m_start */
1787 walk_page_vma(vma
, &walk
);
1793 seq_printf(m
, " anon=%lu", md
->anon
);
1796 seq_printf(m
, " dirty=%lu", md
->dirty
);
1798 if (md
->pages
!= md
->anon
&& md
->pages
!= md
->dirty
)
1799 seq_printf(m
, " mapped=%lu", md
->pages
);
1801 if (md
->mapcount_max
> 1)
1802 seq_printf(m
, " mapmax=%lu", md
->mapcount_max
);
1805 seq_printf(m
, " swapcache=%lu", md
->swapcache
);
1807 if (md
->active
< md
->pages
&& !is_vm_hugetlb_page(vma
))
1808 seq_printf(m
, " active=%lu", md
->active
);
1811 seq_printf(m
, " writeback=%lu", md
->writeback
);
1813 for_each_node_state(nid
, N_MEMORY
)
1815 seq_printf(m
, " N%d=%lu", nid
, md
->node
[nid
]);
1817 seq_printf(m
, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma
) >> 10);
1820 m_cache_vma(m
, vma
);
1824 static int show_pid_numa_map(struct seq_file
*m
, void *v
)
1826 return show_numa_map(m
, v
, 1);
1829 static int show_tid_numa_map(struct seq_file
*m
, void *v
)
1831 return show_numa_map(m
, v
, 0);
1834 static const struct seq_operations proc_pid_numa_maps_op
= {
1838 .show
= show_pid_numa_map
,
1841 static const struct seq_operations proc_tid_numa_maps_op
= {
1845 .show
= show_tid_numa_map
,
1848 static int numa_maps_open(struct inode
*inode
, struct file
*file
,
1849 const struct seq_operations
*ops
)
1851 return proc_maps_open(inode
, file
, ops
,
1852 sizeof(struct numa_maps_private
));
1855 static int pid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1857 return numa_maps_open(inode
, file
, &proc_pid_numa_maps_op
);
1860 static int tid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1862 return numa_maps_open(inode
, file
, &proc_tid_numa_maps_op
);
1865 const struct file_operations proc_pid_numa_maps_operations
= {
1866 .open
= pid_numa_maps_open
,
1868 .llseek
= seq_lseek
,
1869 .release
= proc_map_release
,
1872 const struct file_operations proc_tid_numa_maps_operations
= {
1873 .open
= tid_numa_maps_open
,
1875 .llseek
= seq_lseek
,
1876 .release
= proc_map_release
,
1878 #endif /* CONFIG_NUMA */