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
, bool locked
)
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
;
490 mss
->pss_locked
+= (u64
)size
<< PSS_SHIFT
;
494 for (i
= 0; i
< nr
; i
++, page
++) {
495 int mapcount
= page_mapcount(page
);
496 unsigned long pss
= (PAGE_SIZE
<< PSS_SHIFT
);
499 if (dirty
|| PageDirty(page
))
500 mss
->shared_dirty
+= PAGE_SIZE
;
502 mss
->shared_clean
+= PAGE_SIZE
;
503 mss
->pss
+= pss
/ mapcount
;
505 mss
->pss_locked
+= pss
/ mapcount
;
507 if (dirty
|| PageDirty(page
))
508 mss
->private_dirty
+= PAGE_SIZE
;
510 mss
->private_clean
+= PAGE_SIZE
;
513 mss
->pss_locked
+= pss
;
519 static int smaps_pte_hole(unsigned long addr
, unsigned long end
,
520 struct mm_walk
*walk
)
522 struct mem_size_stats
*mss
= walk
->private;
524 mss
->swap
+= shmem_partial_swap_usage(
525 walk
->vma
->vm_file
->f_mapping
, addr
, end
);
531 static void smaps_pte_entry(pte_t
*pte
, unsigned long addr
,
532 struct mm_walk
*walk
)
534 struct mem_size_stats
*mss
= walk
->private;
535 struct vm_area_struct
*vma
= walk
->vma
;
536 bool locked
= !!(vma
->vm_flags
& VM_LOCKED
);
537 struct page
*page
= NULL
;
539 if (pte_present(*pte
)) {
540 page
= vm_normal_page(vma
, addr
, *pte
);
541 } else if (is_swap_pte(*pte
)) {
542 swp_entry_t swpent
= pte_to_swp_entry(*pte
);
544 if (!non_swap_entry(swpent
)) {
547 mss
->swap
+= PAGE_SIZE
;
548 mapcount
= swp_swapcount(swpent
);
550 u64 pss_delta
= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
552 do_div(pss_delta
, mapcount
);
553 mss
->swap_pss
+= pss_delta
;
555 mss
->swap_pss
+= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
557 } else if (is_migration_entry(swpent
))
558 page
= migration_entry_to_page(swpent
);
559 else if (is_device_private_entry(swpent
))
560 page
= device_private_entry_to_page(swpent
);
561 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM
) && mss
->check_shmem_swap
562 && pte_none(*pte
))) {
563 page
= find_get_entry(vma
->vm_file
->f_mapping
,
564 linear_page_index(vma
, addr
));
568 if (radix_tree_exceptional_entry(page
))
569 mss
->swap
+= PAGE_SIZE
;
579 smaps_account(mss
, page
, false, pte_young(*pte
), pte_dirty(*pte
), locked
);
582 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
583 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
584 struct mm_walk
*walk
)
586 struct mem_size_stats
*mss
= walk
->private;
587 struct vm_area_struct
*vma
= walk
->vma
;
588 bool locked
= !!(vma
->vm_flags
& VM_LOCKED
);
591 /* FOLL_DUMP will return -EFAULT on huge zero page */
592 page
= follow_trans_huge_pmd(vma
, addr
, pmd
, FOLL_DUMP
);
593 if (IS_ERR_OR_NULL(page
))
596 mss
->anonymous_thp
+= HPAGE_PMD_SIZE
;
597 else if (PageSwapBacked(page
))
598 mss
->shmem_thp
+= HPAGE_PMD_SIZE
;
599 else if (is_zone_device_page(page
))
602 VM_BUG_ON_PAGE(1, page
);
603 smaps_account(mss
, page
, true, pmd_young(*pmd
), pmd_dirty(*pmd
), locked
);
606 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
607 struct mm_walk
*walk
)
612 static int smaps_pte_range(pmd_t
*pmd
, unsigned long addr
, unsigned long end
,
613 struct mm_walk
*walk
)
615 struct vm_area_struct
*vma
= walk
->vma
;
619 ptl
= pmd_trans_huge_lock(pmd
, vma
);
621 if (pmd_present(*pmd
))
622 smaps_pmd_entry(pmd
, addr
, walk
);
627 if (pmd_trans_unstable(pmd
))
630 * The mmap_sem held all the way back in m_start() is what
631 * keeps khugepaged out of here and from collapsing things
634 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
635 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
)
636 smaps_pte_entry(pte
, addr
, walk
);
637 pte_unmap_unlock(pte
- 1, ptl
);
643 static void show_smap_vma_flags(struct seq_file
*m
, struct vm_area_struct
*vma
)
646 * Don't forget to update Documentation/ on changes.
648 static const char mnemonics
[BITS_PER_LONG
][2] = {
650 * In case if we meet a flag we don't know about.
652 [0 ... (BITS_PER_LONG
-1)] = "??",
654 [ilog2(VM_READ
)] = "rd",
655 [ilog2(VM_WRITE
)] = "wr",
656 [ilog2(VM_EXEC
)] = "ex",
657 [ilog2(VM_SHARED
)] = "sh",
658 [ilog2(VM_MAYREAD
)] = "mr",
659 [ilog2(VM_MAYWRITE
)] = "mw",
660 [ilog2(VM_MAYEXEC
)] = "me",
661 [ilog2(VM_MAYSHARE
)] = "ms",
662 [ilog2(VM_GROWSDOWN
)] = "gd",
663 [ilog2(VM_PFNMAP
)] = "pf",
664 [ilog2(VM_DENYWRITE
)] = "dw",
665 #ifdef CONFIG_X86_INTEL_MPX
666 [ilog2(VM_MPX
)] = "mp",
668 [ilog2(VM_LOCKED
)] = "lo",
669 [ilog2(VM_IO
)] = "io",
670 [ilog2(VM_SEQ_READ
)] = "sr",
671 [ilog2(VM_RAND_READ
)] = "rr",
672 [ilog2(VM_DONTCOPY
)] = "dc",
673 [ilog2(VM_DONTEXPAND
)] = "de",
674 [ilog2(VM_ACCOUNT
)] = "ac",
675 [ilog2(VM_NORESERVE
)] = "nr",
676 [ilog2(VM_HUGETLB
)] = "ht",
677 [ilog2(VM_ARCH_1
)] = "ar",
678 [ilog2(VM_WIPEONFORK
)] = "wf",
679 [ilog2(VM_DONTDUMP
)] = "dd",
680 #ifdef CONFIG_MEM_SOFT_DIRTY
681 [ilog2(VM_SOFTDIRTY
)] = "sd",
683 [ilog2(VM_MIXEDMAP
)] = "mm",
684 [ilog2(VM_HUGEPAGE
)] = "hg",
685 [ilog2(VM_NOHUGEPAGE
)] = "nh",
686 [ilog2(VM_MERGEABLE
)] = "mg",
687 [ilog2(VM_UFFD_MISSING
)]= "um",
688 [ilog2(VM_UFFD_WP
)] = "uw",
689 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
690 /* These come out via ProtectionKey: */
691 [ilog2(VM_PKEY_BIT0
)] = "",
692 [ilog2(VM_PKEY_BIT1
)] = "",
693 [ilog2(VM_PKEY_BIT2
)] = "",
694 [ilog2(VM_PKEY_BIT3
)] = "",
699 seq_puts(m
, "VmFlags: ");
700 for (i
= 0; i
< BITS_PER_LONG
; i
++) {
701 if (!mnemonics
[i
][0])
703 if (vma
->vm_flags
& (1UL << i
)) {
704 seq_printf(m
, "%c%c ",
705 mnemonics
[i
][0], mnemonics
[i
][1]);
711 #ifdef CONFIG_HUGETLB_PAGE
712 static int smaps_hugetlb_range(pte_t
*pte
, unsigned long hmask
,
713 unsigned long addr
, unsigned long end
,
714 struct mm_walk
*walk
)
716 struct mem_size_stats
*mss
= walk
->private;
717 struct vm_area_struct
*vma
= walk
->vma
;
718 struct page
*page
= NULL
;
720 if (pte_present(*pte
)) {
721 page
= vm_normal_page(vma
, addr
, *pte
);
722 } else if (is_swap_pte(*pte
)) {
723 swp_entry_t swpent
= pte_to_swp_entry(*pte
);
725 if (is_migration_entry(swpent
))
726 page
= migration_entry_to_page(swpent
);
727 else if (is_device_private_entry(swpent
))
728 page
= device_private_entry_to_page(swpent
);
731 int mapcount
= page_mapcount(page
);
734 mss
->shared_hugetlb
+= huge_page_size(hstate_vma(vma
));
736 mss
->private_hugetlb
+= huge_page_size(hstate_vma(vma
));
740 #endif /* HUGETLB_PAGE */
742 void __weak
arch_show_smap(struct seq_file
*m
, struct vm_area_struct
*vma
)
746 static int show_smap(struct seq_file
*m
, void *v
, int is_pid
)
748 struct proc_maps_private
*priv
= m
->private;
749 struct vm_area_struct
*vma
= v
;
750 struct mem_size_stats mss_stack
;
751 struct mem_size_stats
*mss
;
752 struct mm_walk smaps_walk
= {
753 .pmd_entry
= smaps_pte_range
,
754 #ifdef CONFIG_HUGETLB_PAGE
755 .hugetlb_entry
= smaps_hugetlb_range
,
767 mss
->first_vma_start
= vma
->vm_start
;
770 last_vma
= !m_next_vma(priv
, vma
);
773 memset(&mss_stack
, 0, sizeof(mss_stack
));
777 smaps_walk
.private = mss
;
780 /* In case of smaps_rollup, reset the value from previous vma */
781 mss
->check_shmem_swap
= false;
782 if (vma
->vm_file
&& shmem_mapping(vma
->vm_file
->f_mapping
)) {
784 * For shared or readonly shmem mappings we know that all
785 * swapped out pages belong to the shmem object, and we can
786 * obtain the swap value much more efficiently. For private
787 * writable mappings, we might have COW pages that are
788 * not affected by the parent swapped out pages of the shmem
789 * object, so we have to distinguish them during the page walk.
790 * Unless we know that the shmem object (or the part mapped by
791 * our VMA) has no swapped out pages at all.
793 unsigned long shmem_swapped
= shmem_swap_usage(vma
);
795 if (!shmem_swapped
|| (vma
->vm_flags
& VM_SHARED
) ||
796 !(vma
->vm_flags
& VM_WRITE
)) {
797 mss
->swap
+= shmem_swapped
;
799 mss
->check_shmem_swap
= true;
800 smaps_walk
.pte_hole
= smaps_pte_hole
;
804 /* mmap_sem is held in m_start */
805 walk_page_vma(vma
, &smaps_walk
);
808 show_map_vma(m
, vma
, is_pid
);
809 } else if (last_vma
) {
810 show_vma_header_prefix(
811 m
, mss
->first_vma_start
, vma
->vm_end
, 0, 0, 0, 0);
813 seq_puts(m
, "[rollup]\n");
821 "KernelPageSize: %8lu kB\n"
822 "MMUPageSize: %8lu kB\n",
823 (vma
->vm_end
- vma
->vm_start
) >> 10,
824 vma_kernel_pagesize(vma
) >> 10,
825 vma_mmu_pagesize(vma
) >> 10);
828 if (!rollup_mode
|| last_vma
)
832 "Shared_Clean: %8lu kB\n"
833 "Shared_Dirty: %8lu kB\n"
834 "Private_Clean: %8lu kB\n"
835 "Private_Dirty: %8lu kB\n"
836 "Referenced: %8lu kB\n"
837 "Anonymous: %8lu kB\n"
838 "LazyFree: %8lu kB\n"
839 "AnonHugePages: %8lu kB\n"
840 "ShmemPmdMapped: %8lu kB\n"
841 "Shared_Hugetlb: %8lu kB\n"
842 "Private_Hugetlb: %7lu kB\n"
847 (unsigned long)(mss
->pss
>> (10 + PSS_SHIFT
)),
848 mss
->shared_clean
>> 10,
849 mss
->shared_dirty
>> 10,
850 mss
->private_clean
>> 10,
851 mss
->private_dirty
>> 10,
852 mss
->referenced
>> 10,
853 mss
->anonymous
>> 10,
855 mss
->anonymous_thp
>> 10,
856 mss
->shmem_thp
>> 10,
857 mss
->shared_hugetlb
>> 10,
858 mss
->private_hugetlb
>> 10,
860 (unsigned long)(mss
->swap_pss
>> (10 + PSS_SHIFT
)),
861 (unsigned long)(mss
->pss_locked
>> (10 + PSS_SHIFT
)));
864 arch_show_smap(m
, vma
);
865 show_smap_vma_flags(m
, vma
);
871 static int show_pid_smap(struct seq_file
*m
, void *v
)
873 return show_smap(m
, v
, 1);
876 static int show_tid_smap(struct seq_file
*m
, void *v
)
878 return show_smap(m
, v
, 0);
881 static const struct seq_operations proc_pid_smaps_op
= {
885 .show
= show_pid_smap
888 static const struct seq_operations proc_tid_smaps_op
= {
892 .show
= show_tid_smap
895 static int pid_smaps_open(struct inode
*inode
, struct file
*file
)
897 return do_maps_open(inode
, file
, &proc_pid_smaps_op
);
900 static int pid_smaps_rollup_open(struct inode
*inode
, struct file
*file
)
902 struct seq_file
*seq
;
903 struct proc_maps_private
*priv
;
904 int ret
= do_maps_open(inode
, file
, &proc_pid_smaps_op
);
908 seq
= file
->private_data
;
910 priv
->rollup
= kzalloc(sizeof(*priv
->rollup
), GFP_KERNEL
);
912 proc_map_release(inode
, file
);
915 priv
->rollup
->first
= true;
919 static int tid_smaps_open(struct inode
*inode
, struct file
*file
)
921 return do_maps_open(inode
, file
, &proc_tid_smaps_op
);
924 const struct file_operations proc_pid_smaps_operations
= {
925 .open
= pid_smaps_open
,
928 .release
= proc_map_release
,
931 const struct file_operations proc_pid_smaps_rollup_operations
= {
932 .open
= pid_smaps_rollup_open
,
935 .release
= proc_map_release
,
938 const struct file_operations proc_tid_smaps_operations
= {
939 .open
= tid_smaps_open
,
942 .release
= proc_map_release
,
945 enum clear_refs_types
{
949 CLEAR_REFS_SOFT_DIRTY
,
950 CLEAR_REFS_MM_HIWATER_RSS
,
954 struct clear_refs_private
{
955 enum clear_refs_types type
;
958 #ifdef CONFIG_MEM_SOFT_DIRTY
959 static inline void clear_soft_dirty(struct vm_area_struct
*vma
,
960 unsigned long addr
, pte_t
*pte
)
963 * The soft-dirty tracker uses #PF-s to catch writes
964 * to pages, so write-protect the pte as well. See the
965 * Documentation/vm/soft-dirty.txt for full description
966 * of how soft-dirty works.
970 if (pte_present(ptent
)) {
971 ptent
= ptep_modify_prot_start(vma
->vm_mm
, addr
, pte
);
972 ptent
= pte_wrprotect(ptent
);
973 ptent
= pte_clear_soft_dirty(ptent
);
974 ptep_modify_prot_commit(vma
->vm_mm
, addr
, pte
, ptent
);
975 } else if (is_swap_pte(ptent
)) {
976 ptent
= pte_swp_clear_soft_dirty(ptent
);
977 set_pte_at(vma
->vm_mm
, addr
, pte
, ptent
);
981 static inline void clear_soft_dirty(struct vm_area_struct
*vma
,
982 unsigned long addr
, pte_t
*pte
)
987 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
988 static inline void clear_soft_dirty_pmd(struct vm_area_struct
*vma
,
989 unsigned long addr
, pmd_t
*pmdp
)
993 if (pmd_present(pmd
)) {
994 /* See comment in change_huge_pmd() */
995 pmdp_invalidate(vma
, addr
, pmdp
);
996 if (pmd_dirty(*pmdp
))
997 pmd
= pmd_mkdirty(pmd
);
998 if (pmd_young(*pmdp
))
999 pmd
= pmd_mkyoung(pmd
);
1001 pmd
= pmd_wrprotect(pmd
);
1002 pmd
= pmd_clear_soft_dirty(pmd
);
1004 set_pmd_at(vma
->vm_mm
, addr
, pmdp
, pmd
);
1005 } else if (is_migration_entry(pmd_to_swp_entry(pmd
))) {
1006 pmd
= pmd_swp_clear_soft_dirty(pmd
);
1007 set_pmd_at(vma
->vm_mm
, addr
, pmdp
, pmd
);
1011 static inline void clear_soft_dirty_pmd(struct vm_area_struct
*vma
,
1012 unsigned long addr
, pmd_t
*pmdp
)
1017 static int clear_refs_pte_range(pmd_t
*pmd
, unsigned long addr
,
1018 unsigned long end
, struct mm_walk
*walk
)
1020 struct clear_refs_private
*cp
= walk
->private;
1021 struct vm_area_struct
*vma
= walk
->vma
;
1026 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1028 if (cp
->type
== CLEAR_REFS_SOFT_DIRTY
) {
1029 clear_soft_dirty_pmd(vma
, addr
, pmd
);
1033 if (!pmd_present(*pmd
))
1036 page
= pmd_page(*pmd
);
1038 /* Clear accessed and referenced bits. */
1039 pmdp_test_and_clear_young(vma
, addr
, pmd
);
1040 test_and_clear_page_young(page
);
1041 ClearPageReferenced(page
);
1047 if (pmd_trans_unstable(pmd
))
1050 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
1051 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
1054 if (cp
->type
== CLEAR_REFS_SOFT_DIRTY
) {
1055 clear_soft_dirty(vma
, addr
, pte
);
1059 if (!pte_present(ptent
))
1062 page
= vm_normal_page(vma
, addr
, ptent
);
1066 /* Clear accessed and referenced bits. */
1067 ptep_test_and_clear_young(vma
, addr
, pte
);
1068 test_and_clear_page_young(page
);
1069 ClearPageReferenced(page
);
1071 pte_unmap_unlock(pte
- 1, ptl
);
1076 static int clear_refs_test_walk(unsigned long start
, unsigned long end
,
1077 struct mm_walk
*walk
)
1079 struct clear_refs_private
*cp
= walk
->private;
1080 struct vm_area_struct
*vma
= walk
->vma
;
1082 if (vma
->vm_flags
& VM_PFNMAP
)
1086 * Writing 1 to /proc/pid/clear_refs affects all pages.
1087 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1088 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1089 * Writing 4 to /proc/pid/clear_refs affects all pages.
1091 if (cp
->type
== CLEAR_REFS_ANON
&& vma
->vm_file
)
1093 if (cp
->type
== CLEAR_REFS_MAPPED
&& !vma
->vm_file
)
1098 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
1099 size_t count
, loff_t
*ppos
)
1101 struct task_struct
*task
;
1102 char buffer
[PROC_NUMBUF
];
1103 struct mm_struct
*mm
;
1104 struct vm_area_struct
*vma
;
1105 enum clear_refs_types type
;
1106 struct mmu_gather tlb
;
1110 memset(buffer
, 0, sizeof(buffer
));
1111 if (count
> sizeof(buffer
) - 1)
1112 count
= sizeof(buffer
) - 1;
1113 if (copy_from_user(buffer
, buf
, count
))
1115 rv
= kstrtoint(strstrip(buffer
), 10, &itype
);
1118 type
= (enum clear_refs_types
)itype
;
1119 if (type
< CLEAR_REFS_ALL
|| type
>= CLEAR_REFS_LAST
)
1122 task
= get_proc_task(file_inode(file
));
1125 mm
= get_task_mm(task
);
1127 struct clear_refs_private cp
= {
1130 struct mm_walk clear_refs_walk
= {
1131 .pmd_entry
= clear_refs_pte_range
,
1132 .test_walk
= clear_refs_test_walk
,
1137 if (type
== CLEAR_REFS_MM_HIWATER_RSS
) {
1138 if (down_write_killable(&mm
->mmap_sem
)) {
1144 * Writing 5 to /proc/pid/clear_refs resets the peak
1145 * resident set size to this mm's current rss value.
1147 reset_mm_hiwater_rss(mm
);
1148 up_write(&mm
->mmap_sem
);
1152 down_read(&mm
->mmap_sem
);
1153 tlb_gather_mmu(&tlb
, mm
, 0, -1);
1154 if (type
== CLEAR_REFS_SOFT_DIRTY
) {
1155 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1156 if (!(vma
->vm_flags
& VM_SOFTDIRTY
))
1158 up_read(&mm
->mmap_sem
);
1159 if (down_write_killable(&mm
->mmap_sem
)) {
1164 * Avoid to modify vma->vm_flags
1165 * without locked ops while the
1166 * coredump reads the vm_flags.
1168 if (!mmget_still_valid(mm
)) {
1170 * Silently return "count"
1171 * like if get_task_mm()
1172 * failed. FIXME: should this
1173 * function have returned
1174 * -ESRCH if get_task_mm()
1176 * get_proc_task() fails?
1178 up_write(&mm
->mmap_sem
);
1181 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1182 vma
->vm_flags
&= ~VM_SOFTDIRTY
;
1183 vma_set_page_prot(vma
);
1185 downgrade_write(&mm
->mmap_sem
);
1188 mmu_notifier_invalidate_range_start(mm
, 0, -1);
1190 walk_page_range(0, mm
->highest_vm_end
, &clear_refs_walk
);
1191 if (type
== CLEAR_REFS_SOFT_DIRTY
)
1192 mmu_notifier_invalidate_range_end(mm
, 0, -1);
1193 tlb_finish_mmu(&tlb
, 0, -1);
1194 up_read(&mm
->mmap_sem
);
1198 put_task_struct(task
);
1203 const struct file_operations proc_clear_refs_operations
= {
1204 .write
= clear_refs_write
,
1205 .llseek
= noop_llseek
,
1212 struct pagemapread
{
1213 int pos
, len
; /* units: PM_ENTRY_BYTES, not bytes */
1214 pagemap_entry_t
*buffer
;
1218 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
1219 #define PAGEMAP_WALK_MASK (PMD_MASK)
1221 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1222 #define PM_PFRAME_BITS 55
1223 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1224 #define PM_SOFT_DIRTY BIT_ULL(55)
1225 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1226 #define PM_FILE BIT_ULL(61)
1227 #define PM_SWAP BIT_ULL(62)
1228 #define PM_PRESENT BIT_ULL(63)
1230 #define PM_END_OF_BUFFER 1
1232 static inline pagemap_entry_t
make_pme(u64 frame
, u64 flags
)
1234 return (pagemap_entry_t
) { .pme
= (frame
& PM_PFRAME_MASK
) | flags
};
1237 static int add_to_pagemap(unsigned long addr
, pagemap_entry_t
*pme
,
1238 struct pagemapread
*pm
)
1240 pm
->buffer
[pm
->pos
++] = *pme
;
1241 if (pm
->pos
>= pm
->len
)
1242 return PM_END_OF_BUFFER
;
1246 static int pagemap_pte_hole(unsigned long start
, unsigned long end
,
1247 struct mm_walk
*walk
)
1249 struct pagemapread
*pm
= walk
->private;
1250 unsigned long addr
= start
;
1253 while (addr
< end
) {
1254 struct vm_area_struct
*vma
= find_vma(walk
->mm
, addr
);
1255 pagemap_entry_t pme
= make_pme(0, 0);
1256 /* End of address space hole, which we mark as non-present. */
1257 unsigned long hole_end
;
1260 hole_end
= min(end
, vma
->vm_start
);
1264 for (; addr
< hole_end
; addr
+= PAGE_SIZE
) {
1265 err
= add_to_pagemap(addr
, &pme
, pm
);
1273 /* Addresses in the VMA. */
1274 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1275 pme
= make_pme(0, PM_SOFT_DIRTY
);
1276 for (; addr
< min(end
, vma
->vm_end
); addr
+= PAGE_SIZE
) {
1277 err
= add_to_pagemap(addr
, &pme
, pm
);
1286 static pagemap_entry_t
pte_to_pagemap_entry(struct pagemapread
*pm
,
1287 struct vm_area_struct
*vma
, unsigned long addr
, pte_t pte
)
1289 u64 frame
= 0, flags
= 0;
1290 struct page
*page
= NULL
;
1292 if (pte_present(pte
)) {
1294 frame
= pte_pfn(pte
);
1295 flags
|= PM_PRESENT
;
1296 page
= _vm_normal_page(vma
, addr
, pte
, true);
1297 if (pte_soft_dirty(pte
))
1298 flags
|= PM_SOFT_DIRTY
;
1299 } else if (is_swap_pte(pte
)) {
1301 if (pte_swp_soft_dirty(pte
))
1302 flags
|= PM_SOFT_DIRTY
;
1303 entry
= pte_to_swp_entry(pte
);
1305 frame
= swp_type(entry
) |
1306 (swp_offset(entry
) << MAX_SWAPFILES_SHIFT
);
1308 if (is_migration_entry(entry
))
1309 page
= migration_entry_to_page(entry
);
1311 if (is_device_private_entry(entry
))
1312 page
= device_private_entry_to_page(entry
);
1315 if (page
&& !PageAnon(page
))
1317 if (page
&& page_mapcount(page
) == 1)
1318 flags
|= PM_MMAP_EXCLUSIVE
;
1319 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1320 flags
|= PM_SOFT_DIRTY
;
1322 return make_pme(frame
, flags
);
1325 static int pagemap_pmd_range(pmd_t
*pmdp
, unsigned long addr
, unsigned long end
,
1326 struct mm_walk
*walk
)
1328 struct vm_area_struct
*vma
= walk
->vma
;
1329 struct pagemapread
*pm
= walk
->private;
1331 pte_t
*pte
, *orig_pte
;
1334 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1335 ptl
= pmd_trans_huge_lock(pmdp
, vma
);
1337 u64 flags
= 0, frame
= 0;
1339 struct page
*page
= NULL
;
1341 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1342 flags
|= PM_SOFT_DIRTY
;
1344 if (pmd_present(pmd
)) {
1345 page
= pmd_page(pmd
);
1347 flags
|= PM_PRESENT
;
1348 if (pmd_soft_dirty(pmd
))
1349 flags
|= PM_SOFT_DIRTY
;
1351 frame
= pmd_pfn(pmd
) +
1352 ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1354 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1355 else if (is_swap_pmd(pmd
)) {
1356 swp_entry_t entry
= pmd_to_swp_entry(pmd
);
1357 unsigned long offset
;
1360 offset
= swp_offset(entry
) +
1361 ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1362 frame
= swp_type(entry
) |
1363 (offset
<< MAX_SWAPFILES_SHIFT
);
1366 if (pmd_swp_soft_dirty(pmd
))
1367 flags
|= PM_SOFT_DIRTY
;
1368 VM_BUG_ON(!is_pmd_migration_entry(pmd
));
1369 page
= migration_entry_to_page(entry
);
1373 if (page
&& page_mapcount(page
) == 1)
1374 flags
|= PM_MMAP_EXCLUSIVE
;
1376 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1377 pagemap_entry_t pme
= make_pme(frame
, flags
);
1379 err
= add_to_pagemap(addr
, &pme
, pm
);
1383 if (flags
& PM_PRESENT
)
1385 else if (flags
& PM_SWAP
)
1386 frame
+= (1 << MAX_SWAPFILES_SHIFT
);
1393 if (pmd_trans_unstable(pmdp
))
1395 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1398 * We can assume that @vma always points to a valid one and @end never
1399 * goes beyond vma->vm_end.
1401 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmdp
, addr
, &ptl
);
1402 for (; addr
< end
; pte
++, addr
+= PAGE_SIZE
) {
1403 pagemap_entry_t pme
;
1405 pme
= pte_to_pagemap_entry(pm
, vma
, addr
, *pte
);
1406 err
= add_to_pagemap(addr
, &pme
, pm
);
1410 pte_unmap_unlock(orig_pte
, ptl
);
1417 #ifdef CONFIG_HUGETLB_PAGE
1418 /* This function walks within one hugetlb entry in the single call */
1419 static int pagemap_hugetlb_range(pte_t
*ptep
, unsigned long hmask
,
1420 unsigned long addr
, unsigned long end
,
1421 struct mm_walk
*walk
)
1423 struct pagemapread
*pm
= walk
->private;
1424 struct vm_area_struct
*vma
= walk
->vma
;
1425 u64 flags
= 0, frame
= 0;
1429 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1430 flags
|= PM_SOFT_DIRTY
;
1432 pte
= huge_ptep_get(ptep
);
1433 if (pte_present(pte
)) {
1434 struct page
*page
= pte_page(pte
);
1436 if (!PageAnon(page
))
1439 if (page_mapcount(page
) == 1)
1440 flags
|= PM_MMAP_EXCLUSIVE
;
1442 flags
|= PM_PRESENT
;
1444 frame
= pte_pfn(pte
) +
1445 ((addr
& ~hmask
) >> PAGE_SHIFT
);
1448 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1449 pagemap_entry_t pme
= make_pme(frame
, flags
);
1451 err
= add_to_pagemap(addr
, &pme
, pm
);
1454 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1462 #endif /* HUGETLB_PAGE */
1465 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1467 * For each page in the address space, this file contains one 64-bit entry
1468 * consisting of the following:
1470 * Bits 0-54 page frame number (PFN) if present
1471 * Bits 0-4 swap type if swapped
1472 * Bits 5-54 swap offset if swapped
1473 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1474 * Bit 56 page exclusively mapped
1476 * Bit 61 page is file-page or shared-anon
1477 * Bit 62 page swapped
1478 * Bit 63 page present
1480 * If the page is not present but in swap, then the PFN contains an
1481 * encoding of the swap file number and the page's offset into the
1482 * swap. Unmapped pages return a null PFN. This allows determining
1483 * precisely which pages are mapped (or in swap) and comparing mapped
1484 * pages between processes.
1486 * Efficient users of this interface will use /proc/pid/maps to
1487 * determine which areas of memory are actually mapped and llseek to
1488 * skip over unmapped regions.
1490 static ssize_t
pagemap_read(struct file
*file
, char __user
*buf
,
1491 size_t count
, loff_t
*ppos
)
1493 struct mm_struct
*mm
= file
->private_data
;
1494 struct pagemapread pm
;
1495 struct mm_walk pagemap_walk
= {};
1497 unsigned long svpfn
;
1498 unsigned long start_vaddr
;
1499 unsigned long end_vaddr
;
1500 int ret
= 0, copied
= 0;
1502 if (!mm
|| !mmget_not_zero(mm
))
1506 /* file position must be aligned */
1507 if ((*ppos
% PM_ENTRY_BYTES
) || (count
% PM_ENTRY_BYTES
))
1514 /* do not disclose physical addresses: attack vector */
1515 pm
.show_pfn
= file_ns_capable(file
, &init_user_ns
, CAP_SYS_ADMIN
);
1517 pm
.len
= (PAGEMAP_WALK_SIZE
>> PAGE_SHIFT
);
1518 pm
.buffer
= kmalloc(pm
.len
* PM_ENTRY_BYTES
, GFP_KERNEL
);
1523 pagemap_walk
.pmd_entry
= pagemap_pmd_range
;
1524 pagemap_walk
.pte_hole
= pagemap_pte_hole
;
1525 #ifdef CONFIG_HUGETLB_PAGE
1526 pagemap_walk
.hugetlb_entry
= pagemap_hugetlb_range
;
1528 pagemap_walk
.mm
= mm
;
1529 pagemap_walk
.private = &pm
;
1532 svpfn
= src
/ PM_ENTRY_BYTES
;
1533 start_vaddr
= svpfn
<< PAGE_SHIFT
;
1534 end_vaddr
= mm
->task_size
;
1536 /* watch out for wraparound */
1537 if (svpfn
> mm
->task_size
>> PAGE_SHIFT
)
1538 start_vaddr
= end_vaddr
;
1541 * The odds are that this will stop walking way
1542 * before end_vaddr, because the length of the
1543 * user buffer is tracked in "pm", and the walk
1544 * will stop when we hit the end of the buffer.
1547 while (count
&& (start_vaddr
< end_vaddr
)) {
1552 end
= (start_vaddr
+ PAGEMAP_WALK_SIZE
) & PAGEMAP_WALK_MASK
;
1554 if (end
< start_vaddr
|| end
> end_vaddr
)
1556 down_read(&mm
->mmap_sem
);
1557 ret
= walk_page_range(start_vaddr
, end
, &pagemap_walk
);
1558 up_read(&mm
->mmap_sem
);
1561 len
= min(count
, PM_ENTRY_BYTES
* pm
.pos
);
1562 if (copy_to_user(buf
, pm
.buffer
, len
)) {
1571 if (!ret
|| ret
== PM_END_OF_BUFFER
)
1582 static int pagemap_open(struct inode
*inode
, struct file
*file
)
1584 struct mm_struct
*mm
;
1586 mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
1589 file
->private_data
= mm
;
1593 static int pagemap_release(struct inode
*inode
, struct file
*file
)
1595 struct mm_struct
*mm
= file
->private_data
;
1602 const struct file_operations proc_pagemap_operations
= {
1603 .llseek
= mem_lseek
, /* borrow this */
1604 .read
= pagemap_read
,
1605 .open
= pagemap_open
,
1606 .release
= pagemap_release
,
1608 #endif /* CONFIG_PROC_PAGE_MONITOR */
1613 unsigned long pages
;
1615 unsigned long active
;
1616 unsigned long writeback
;
1617 unsigned long mapcount_max
;
1618 unsigned long dirty
;
1619 unsigned long swapcache
;
1620 unsigned long node
[MAX_NUMNODES
];
1623 struct numa_maps_private
{
1624 struct proc_maps_private proc_maps
;
1625 struct numa_maps md
;
1628 static void gather_stats(struct page
*page
, struct numa_maps
*md
, int pte_dirty
,
1629 unsigned long nr_pages
)
1631 int count
= page_mapcount(page
);
1633 md
->pages
+= nr_pages
;
1634 if (pte_dirty
|| PageDirty(page
))
1635 md
->dirty
+= nr_pages
;
1637 if (PageSwapCache(page
))
1638 md
->swapcache
+= nr_pages
;
1640 if (PageActive(page
) || PageUnevictable(page
))
1641 md
->active
+= nr_pages
;
1643 if (PageWriteback(page
))
1644 md
->writeback
+= nr_pages
;
1647 md
->anon
+= nr_pages
;
1649 if (count
> md
->mapcount_max
)
1650 md
->mapcount_max
= count
;
1652 md
->node
[page_to_nid(page
)] += nr_pages
;
1655 static struct page
*can_gather_numa_stats(pte_t pte
, struct vm_area_struct
*vma
,
1661 if (!pte_present(pte
))
1664 page
= vm_normal_page(vma
, addr
, pte
);
1668 if (PageReserved(page
))
1671 nid
= page_to_nid(page
);
1672 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1678 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1679 static struct page
*can_gather_numa_stats_pmd(pmd_t pmd
,
1680 struct vm_area_struct
*vma
,
1686 if (!pmd_present(pmd
))
1689 page
= vm_normal_page_pmd(vma
, addr
, pmd
);
1693 if (PageReserved(page
))
1696 nid
= page_to_nid(page
);
1697 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1704 static int gather_pte_stats(pmd_t
*pmd
, unsigned long addr
,
1705 unsigned long end
, struct mm_walk
*walk
)
1707 struct numa_maps
*md
= walk
->private;
1708 struct vm_area_struct
*vma
= walk
->vma
;
1713 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1714 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1718 page
= can_gather_numa_stats_pmd(*pmd
, vma
, addr
);
1720 gather_stats(page
, md
, pmd_dirty(*pmd
),
1721 HPAGE_PMD_SIZE
/PAGE_SIZE
);
1726 if (pmd_trans_unstable(pmd
))
1729 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmd
, addr
, &ptl
);
1731 struct page
*page
= can_gather_numa_stats(*pte
, vma
, addr
);
1734 gather_stats(page
, md
, pte_dirty(*pte
), 1);
1736 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1737 pte_unmap_unlock(orig_pte
, ptl
);
1741 #ifdef CONFIG_HUGETLB_PAGE
1742 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1743 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1745 pte_t huge_pte
= huge_ptep_get(pte
);
1746 struct numa_maps
*md
;
1749 if (!pte_present(huge_pte
))
1752 page
= pte_page(huge_pte
);
1757 gather_stats(page
, md
, pte_dirty(huge_pte
), 1);
1762 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1763 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1770 * Display pages allocated per node and memory policy via /proc.
1772 static int show_numa_map(struct seq_file
*m
, void *v
, int is_pid
)
1774 struct numa_maps_private
*numa_priv
= m
->private;
1775 struct proc_maps_private
*proc_priv
= &numa_priv
->proc_maps
;
1776 struct vm_area_struct
*vma
= v
;
1777 struct numa_maps
*md
= &numa_priv
->md
;
1778 struct file
*file
= vma
->vm_file
;
1779 struct mm_struct
*mm
= vma
->vm_mm
;
1780 struct mm_walk walk
= {
1781 .hugetlb_entry
= gather_hugetlb_stats
,
1782 .pmd_entry
= gather_pte_stats
,
1786 struct mempolicy
*pol
;
1793 /* Ensure we start with an empty set of numa_maps statistics. */
1794 memset(md
, 0, sizeof(*md
));
1796 pol
= __get_vma_policy(vma
, vma
->vm_start
);
1798 mpol_to_str(buffer
, sizeof(buffer
), pol
);
1801 mpol_to_str(buffer
, sizeof(buffer
), proc_priv
->task_mempolicy
);
1804 seq_printf(m
, "%08lx %s", vma
->vm_start
, buffer
);
1807 seq_puts(m
, " file=");
1808 seq_file_path(m
, file
, "\n\t= ");
1809 } else if (vma
->vm_start
<= mm
->brk
&& vma
->vm_end
>= mm
->start_brk
) {
1810 seq_puts(m
, " heap");
1811 } else if (is_stack(vma
)) {
1812 seq_puts(m
, " stack");
1815 if (is_vm_hugetlb_page(vma
))
1816 seq_puts(m
, " huge");
1818 /* mmap_sem is held by m_start */
1819 walk_page_vma(vma
, &walk
);
1825 seq_printf(m
, " anon=%lu", md
->anon
);
1828 seq_printf(m
, " dirty=%lu", md
->dirty
);
1830 if (md
->pages
!= md
->anon
&& md
->pages
!= md
->dirty
)
1831 seq_printf(m
, " mapped=%lu", md
->pages
);
1833 if (md
->mapcount_max
> 1)
1834 seq_printf(m
, " mapmax=%lu", md
->mapcount_max
);
1837 seq_printf(m
, " swapcache=%lu", md
->swapcache
);
1839 if (md
->active
< md
->pages
&& !is_vm_hugetlb_page(vma
))
1840 seq_printf(m
, " active=%lu", md
->active
);
1843 seq_printf(m
, " writeback=%lu", md
->writeback
);
1845 for_each_node_state(nid
, N_MEMORY
)
1847 seq_printf(m
, " N%d=%lu", nid
, md
->node
[nid
]);
1849 seq_printf(m
, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma
) >> 10);
1852 m_cache_vma(m
, vma
);
1856 static int show_pid_numa_map(struct seq_file
*m
, void *v
)
1858 return show_numa_map(m
, v
, 1);
1861 static int show_tid_numa_map(struct seq_file
*m
, void *v
)
1863 return show_numa_map(m
, v
, 0);
1866 static const struct seq_operations proc_pid_numa_maps_op
= {
1870 .show
= show_pid_numa_map
,
1873 static const struct seq_operations proc_tid_numa_maps_op
= {
1877 .show
= show_tid_numa_map
,
1880 static int numa_maps_open(struct inode
*inode
, struct file
*file
,
1881 const struct seq_operations
*ops
)
1883 return proc_maps_open(inode
, file
, ops
,
1884 sizeof(struct numa_maps_private
));
1887 static int pid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1889 return numa_maps_open(inode
, file
, &proc_pid_numa_maps_op
);
1892 static int tid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1894 return numa_maps_open(inode
, file
, &proc_tid_numa_maps_op
);
1897 const struct file_operations proc_pid_numa_maps_operations
= {
1898 .open
= pid_numa_maps_open
,
1900 .llseek
= seq_lseek
,
1901 .release
= proc_map_release
,
1904 const struct file_operations proc_tid_numa_maps_operations
= {
1905 .open
= tid_numa_maps_open
,
1907 .llseek
= seq_lseek
,
1908 .release
= proc_map_release
,
1910 #endif /* CONFIG_NUMA */