2 #include <linux/vmacache.h>
3 #include <linux/hugetlb.h>
4 #include <linux/huge_mm.h>
5 #include <linux/mount.h>
6 #include <linux/seq_file.h>
7 #include <linux/highmem.h>
8 #include <linux/ptrace.h>
9 #include <linux/slab.h>
10 #include <linux/pagemap.h>
11 #include <linux/mempolicy.h>
12 #include <linux/rmap.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/mmu_notifier.h>
18 #include <asm/uaccess.h>
19 #include <asm/tlbflush.h>
22 void task_mem(struct seq_file
*m
, struct mm_struct
*mm
)
24 unsigned long data
, text
, lib
, swap
, ptes
, pmds
;
25 unsigned long hiwater_vm
, total_vm
, hiwater_rss
, total_rss
;
28 * Note: to minimize their overhead, mm maintains hiwater_vm and
29 * hiwater_rss only when about to *lower* total_vm or rss. Any
30 * collector of these hiwater stats must therefore get total_vm
31 * and rss too, which will usually be the higher. Barriers? not
32 * worth the effort, such snapshots can always be inconsistent.
34 hiwater_vm
= total_vm
= mm
->total_vm
;
35 if (hiwater_vm
< mm
->hiwater_vm
)
36 hiwater_vm
= mm
->hiwater_vm
;
37 hiwater_rss
= total_rss
= get_mm_rss(mm
);
38 if (hiwater_rss
< mm
->hiwater_rss
)
39 hiwater_rss
= mm
->hiwater_rss
;
41 data
= mm
->total_vm
- mm
->shared_vm
- mm
->stack_vm
;
42 text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
)) >> 10;
43 lib
= (mm
->exec_vm
<< (PAGE_SHIFT
-10)) - text
;
44 swap
= get_mm_counter(mm
, MM_SWAPENTS
);
45 ptes
= PTRS_PER_PTE
* sizeof(pte_t
) * atomic_long_read(&mm
->nr_ptes
);
46 pmds
= PTRS_PER_PMD
* sizeof(pmd_t
) * mm_nr_pmds(mm
);
61 hiwater_vm
<< (PAGE_SHIFT
-10),
62 total_vm
<< (PAGE_SHIFT
-10),
63 mm
->locked_vm
<< (PAGE_SHIFT
-10),
64 mm
->pinned_vm
<< (PAGE_SHIFT
-10),
65 hiwater_rss
<< (PAGE_SHIFT
-10),
66 total_rss
<< (PAGE_SHIFT
-10),
67 data
<< (PAGE_SHIFT
-10),
68 mm
->stack_vm
<< (PAGE_SHIFT
-10), text
, lib
,
71 swap
<< (PAGE_SHIFT
-10));
74 unsigned long task_vsize(struct mm_struct
*mm
)
76 return PAGE_SIZE
* mm
->total_vm
;
79 unsigned long task_statm(struct mm_struct
*mm
,
80 unsigned long *shared
, unsigned long *text
,
81 unsigned long *data
, unsigned long *resident
)
83 *shared
= get_mm_counter(mm
, MM_FILEPAGES
);
84 *text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
))
86 *data
= mm
->total_vm
- mm
->shared_vm
;
87 *resident
= *shared
+ get_mm_counter(mm
, MM_ANONPAGES
);
93 * Save get_task_policy() for show_numa_map().
95 static void hold_task_mempolicy(struct proc_maps_private
*priv
)
97 struct task_struct
*task
= priv
->task
;
100 priv
->task_mempolicy
= get_task_policy(task
);
101 mpol_get(priv
->task_mempolicy
);
104 static void release_task_mempolicy(struct proc_maps_private
*priv
)
106 mpol_put(priv
->task_mempolicy
);
109 static void hold_task_mempolicy(struct proc_maps_private
*priv
)
112 static void release_task_mempolicy(struct proc_maps_private
*priv
)
117 static void vma_stop(struct proc_maps_private
*priv
)
119 struct mm_struct
*mm
= priv
->mm
;
121 release_task_mempolicy(priv
);
122 up_read(&mm
->mmap_sem
);
126 static struct vm_area_struct
*
127 m_next_vma(struct proc_maps_private
*priv
, struct vm_area_struct
*vma
)
129 if (vma
== priv
->tail_vma
)
131 return vma
->vm_next
?: priv
->tail_vma
;
134 static void m_cache_vma(struct seq_file
*m
, struct vm_area_struct
*vma
)
136 if (m
->count
< m
->size
) /* vma is copied successfully */
137 m
->version
= m_next_vma(m
->private, vma
) ? vma
->vm_start
: -1UL;
140 static void *m_start(struct seq_file
*m
, loff_t
*ppos
)
142 struct proc_maps_private
*priv
= m
->private;
143 unsigned long last_addr
= m
->version
;
144 struct mm_struct
*mm
;
145 struct vm_area_struct
*vma
;
146 unsigned int pos
= *ppos
;
148 /* See m_cache_vma(). Zero at the start or after lseek. */
149 if (last_addr
== -1UL)
152 priv
->task
= get_proc_task(priv
->inode
);
154 return ERR_PTR(-ESRCH
);
157 if (!mm
|| !atomic_inc_not_zero(&mm
->mm_users
))
160 down_read(&mm
->mmap_sem
);
161 hold_task_mempolicy(priv
);
162 priv
->tail_vma
= get_gate_vma(mm
);
165 vma
= find_vma(mm
, last_addr
);
166 if (vma
&& (vma
= m_next_vma(priv
, vma
)))
171 if (pos
< mm
->map_count
) {
172 for (vma
= mm
->mmap
; pos
; pos
--) {
173 m
->version
= vma
->vm_start
;
179 /* we do not bother to update m->version in this case */
180 if (pos
== mm
->map_count
&& priv
->tail_vma
)
181 return priv
->tail_vma
;
187 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
189 struct proc_maps_private
*priv
= m
->private;
190 struct vm_area_struct
*next
;
193 next
= m_next_vma(priv
, v
);
199 static void m_stop(struct seq_file
*m
, void *v
)
201 struct proc_maps_private
*priv
= m
->private;
203 if (!IS_ERR_OR_NULL(v
))
206 put_task_struct(priv
->task
);
211 static int proc_maps_open(struct inode
*inode
, struct file
*file
,
212 const struct seq_operations
*ops
, int psize
)
214 struct proc_maps_private
*priv
= __seq_open_private(file
, ops
, psize
);
220 priv
->mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
221 if (IS_ERR(priv
->mm
)) {
222 int err
= PTR_ERR(priv
->mm
);
224 seq_release_private(inode
, file
);
231 static int proc_map_release(struct inode
*inode
, struct file
*file
)
233 struct seq_file
*seq
= file
->private_data
;
234 struct proc_maps_private
*priv
= seq
->private;
239 return seq_release_private(inode
, file
);
242 static int do_maps_open(struct inode
*inode
, struct file
*file
,
243 const struct seq_operations
*ops
)
245 return proc_maps_open(inode
, file
, ops
,
246 sizeof(struct proc_maps_private
));
249 static pid_t
pid_of_stack(struct proc_maps_private
*priv
,
250 struct vm_area_struct
*vma
, bool is_pid
)
252 struct inode
*inode
= priv
->inode
;
253 struct task_struct
*task
;
257 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
259 task
= task_of_stack(task
, vma
, is_pid
);
261 ret
= task_pid_nr_ns(task
, inode
->i_sb
->s_fs_info
);
269 show_map_vma(struct seq_file
*m
, struct vm_area_struct
*vma
, int is_pid
)
271 struct mm_struct
*mm
= vma
->vm_mm
;
272 struct file
*file
= vma
->vm_file
;
273 struct proc_maps_private
*priv
= m
->private;
274 vm_flags_t flags
= vma
->vm_flags
;
275 unsigned long ino
= 0;
276 unsigned long long pgoff
= 0;
277 unsigned long start
, end
;
279 const char *name
= NULL
;
282 struct inode
*inode
= file_inode(vma
->vm_file
);
283 dev
= inode
->i_sb
->s_dev
;
285 pgoff
= ((loff_t
)vma
->vm_pgoff
) << PAGE_SHIFT
;
288 /* We don't show the stack guard page in /proc/maps */
289 start
= vma
->vm_start
;
290 if (stack_guard_page_start(vma
, start
))
293 if (stack_guard_page_end(vma
, end
))
296 seq_setwidth(m
, 25 + sizeof(void *) * 6 - 1);
297 seq_printf(m
, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
300 flags
& VM_READ
? 'r' : '-',
301 flags
& VM_WRITE
? 'w' : '-',
302 flags
& VM_EXEC
? 'x' : '-',
303 flags
& VM_MAYSHARE
? 's' : 'p',
305 MAJOR(dev
), MINOR(dev
), ino
);
308 * Print the dentry name for named mappings, and a
309 * special [heap] marker for the heap:
313 seq_file_path(m
, file
, "\n");
317 if (vma
->vm_ops
&& vma
->vm_ops
->name
) {
318 name
= vma
->vm_ops
->name(vma
);
323 name
= arch_vma_name(vma
);
332 if (vma
->vm_start
<= mm
->brk
&&
333 vma
->vm_end
>= mm
->start_brk
) {
338 tid
= pid_of_stack(priv
, vma
, is_pid
);
341 * Thread stack in /proc/PID/task/TID/maps or
342 * the main process stack.
344 if (!is_pid
|| (vma
->vm_start
<= mm
->start_stack
&&
345 vma
->vm_end
>= mm
->start_stack
)) {
348 /* Thread stack in /proc/PID/maps */
350 seq_printf(m
, "[stack:%d]", tid
);
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
{
439 unsigned long resident
;
440 unsigned long shared_clean
;
441 unsigned long shared_dirty
;
442 unsigned long private_clean
;
443 unsigned long private_dirty
;
444 unsigned long referenced
;
445 unsigned long anonymous
;
446 unsigned long anonymous_thp
;
452 static void smaps_account(struct mem_size_stats
*mss
, struct page
*page
,
453 unsigned long size
, bool young
, bool dirty
)
458 mss
->anonymous
+= size
;
460 mss
->resident
+= size
;
461 /* Accumulate the size in pages that have been accessed. */
462 if (young
|| PageReferenced(page
))
463 mss
->referenced
+= size
;
464 mapcount
= page_mapcount(page
);
468 if (dirty
|| PageDirty(page
))
469 mss
->shared_dirty
+= size
;
471 mss
->shared_clean
+= size
;
472 pss_delta
= (u64
)size
<< PSS_SHIFT
;
473 do_div(pss_delta
, mapcount
);
474 mss
->pss
+= pss_delta
;
476 if (dirty
|| PageDirty(page
))
477 mss
->private_dirty
+= size
;
479 mss
->private_clean
+= size
;
480 mss
->pss
+= (u64
)size
<< PSS_SHIFT
;
484 static void smaps_pte_entry(pte_t
*pte
, unsigned long addr
,
485 struct mm_walk
*walk
)
487 struct mem_size_stats
*mss
= walk
->private;
488 struct vm_area_struct
*vma
= walk
->vma
;
489 struct page
*page
= NULL
;
491 if (pte_present(*pte
)) {
492 page
= vm_normal_page(vma
, addr
, *pte
);
493 } else if (is_swap_pte(*pte
)) {
494 swp_entry_t swpent
= pte_to_swp_entry(*pte
);
496 if (!non_swap_entry(swpent
)) {
499 mss
->swap
+= PAGE_SIZE
;
500 mapcount
= swp_swapcount(swpent
);
502 u64 pss_delta
= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
504 do_div(pss_delta
, mapcount
);
505 mss
->swap_pss
+= pss_delta
;
507 mss
->swap_pss
+= (u64
)PAGE_SIZE
<< PSS_SHIFT
;
509 } else if (is_migration_entry(swpent
))
510 page
= migration_entry_to_page(swpent
);
515 smaps_account(mss
, page
, PAGE_SIZE
, pte_young(*pte
), pte_dirty(*pte
));
518 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
519 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
520 struct mm_walk
*walk
)
522 struct mem_size_stats
*mss
= walk
->private;
523 struct vm_area_struct
*vma
= walk
->vma
;
526 /* FOLL_DUMP will return -EFAULT on huge zero page */
527 page
= follow_trans_huge_pmd(vma
, addr
, pmd
, FOLL_DUMP
);
528 if (IS_ERR_OR_NULL(page
))
530 mss
->anonymous_thp
+= HPAGE_PMD_SIZE
;
531 smaps_account(mss
, page
, HPAGE_PMD_SIZE
,
532 pmd_young(*pmd
), pmd_dirty(*pmd
));
535 static void smaps_pmd_entry(pmd_t
*pmd
, unsigned long addr
,
536 struct mm_walk
*walk
)
541 static int smaps_pte_range(pmd_t
*pmd
, unsigned long addr
, unsigned long end
,
542 struct mm_walk
*walk
)
544 struct vm_area_struct
*vma
= walk
->vma
;
548 if (pmd_trans_huge_lock(pmd
, vma
, &ptl
) == 1) {
549 smaps_pmd_entry(pmd
, addr
, walk
);
554 if (pmd_trans_unstable(pmd
))
557 * The mmap_sem held all the way back in m_start() is what
558 * keeps khugepaged out of here and from collapsing things
561 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
562 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
)
563 smaps_pte_entry(pte
, addr
, walk
);
564 pte_unmap_unlock(pte
- 1, ptl
);
569 static void show_smap_vma_flags(struct seq_file
*m
, struct vm_area_struct
*vma
)
572 * Don't forget to update Documentation/ on changes.
574 static const char mnemonics
[BITS_PER_LONG
][2] = {
576 * In case if we meet a flag we don't know about.
578 [0 ... (BITS_PER_LONG
-1)] = "??",
580 [ilog2(VM_READ
)] = "rd",
581 [ilog2(VM_WRITE
)] = "wr",
582 [ilog2(VM_EXEC
)] = "ex",
583 [ilog2(VM_SHARED
)] = "sh",
584 [ilog2(VM_MAYREAD
)] = "mr",
585 [ilog2(VM_MAYWRITE
)] = "mw",
586 [ilog2(VM_MAYEXEC
)] = "me",
587 [ilog2(VM_MAYSHARE
)] = "ms",
588 [ilog2(VM_GROWSDOWN
)] = "gd",
589 [ilog2(VM_PFNMAP
)] = "pf",
590 [ilog2(VM_DENYWRITE
)] = "dw",
591 #ifdef CONFIG_X86_INTEL_MPX
592 [ilog2(VM_MPX
)] = "mp",
594 [ilog2(VM_LOCKED
)] = "lo",
595 [ilog2(VM_IO
)] = "io",
596 [ilog2(VM_SEQ_READ
)] = "sr",
597 [ilog2(VM_RAND_READ
)] = "rr",
598 [ilog2(VM_DONTCOPY
)] = "dc",
599 [ilog2(VM_DONTEXPAND
)] = "de",
600 [ilog2(VM_ACCOUNT
)] = "ac",
601 [ilog2(VM_NORESERVE
)] = "nr",
602 [ilog2(VM_HUGETLB
)] = "ht",
603 [ilog2(VM_ARCH_1
)] = "ar",
604 [ilog2(VM_DONTDUMP
)] = "dd",
605 #ifdef CONFIG_MEM_SOFT_DIRTY
606 [ilog2(VM_SOFTDIRTY
)] = "sd",
608 [ilog2(VM_MIXEDMAP
)] = "mm",
609 [ilog2(VM_HUGEPAGE
)] = "hg",
610 [ilog2(VM_NOHUGEPAGE
)] = "nh",
611 [ilog2(VM_MERGEABLE
)] = "mg",
612 [ilog2(VM_UFFD_MISSING
)]= "um",
613 [ilog2(VM_UFFD_WP
)] = "uw",
617 seq_puts(m
, "VmFlags: ");
618 for (i
= 0; i
< BITS_PER_LONG
; i
++) {
619 if (vma
->vm_flags
& (1UL << i
)) {
620 seq_printf(m
, "%c%c ",
621 mnemonics
[i
][0], mnemonics
[i
][1]);
627 static int show_smap(struct seq_file
*m
, void *v
, int is_pid
)
629 struct vm_area_struct
*vma
= v
;
630 struct mem_size_stats mss
;
631 struct mm_walk smaps_walk
= {
632 .pmd_entry
= smaps_pte_range
,
637 memset(&mss
, 0, sizeof mss
);
638 /* mmap_sem is held in m_start */
639 walk_page_vma(vma
, &smaps_walk
);
641 show_map_vma(m
, vma
, is_pid
);
647 "Shared_Clean: %8lu kB\n"
648 "Shared_Dirty: %8lu kB\n"
649 "Private_Clean: %8lu kB\n"
650 "Private_Dirty: %8lu kB\n"
651 "Referenced: %8lu kB\n"
652 "Anonymous: %8lu kB\n"
653 "AnonHugePages: %8lu kB\n"
656 "KernelPageSize: %8lu kB\n"
657 "MMUPageSize: %8lu kB\n"
659 (vma
->vm_end
- vma
->vm_start
) >> 10,
661 (unsigned long)(mss
.pss
>> (10 + PSS_SHIFT
)),
662 mss
.shared_clean
>> 10,
663 mss
.shared_dirty
>> 10,
664 mss
.private_clean
>> 10,
665 mss
.private_dirty
>> 10,
666 mss
.referenced
>> 10,
668 mss
.anonymous_thp
>> 10,
670 (unsigned long)(mss
.swap_pss
>> (10 + PSS_SHIFT
)),
671 vma_kernel_pagesize(vma
) >> 10,
672 vma_mmu_pagesize(vma
) >> 10,
673 (vma
->vm_flags
& VM_LOCKED
) ?
674 (unsigned long)(mss
.pss
>> (10 + PSS_SHIFT
)) : 0);
676 show_smap_vma_flags(m
, vma
);
681 static int show_pid_smap(struct seq_file
*m
, void *v
)
683 return show_smap(m
, v
, 1);
686 static int show_tid_smap(struct seq_file
*m
, void *v
)
688 return show_smap(m
, v
, 0);
691 static const struct seq_operations proc_pid_smaps_op
= {
695 .show
= show_pid_smap
698 static const struct seq_operations proc_tid_smaps_op
= {
702 .show
= show_tid_smap
705 static int pid_smaps_open(struct inode
*inode
, struct file
*file
)
707 return do_maps_open(inode
, file
, &proc_pid_smaps_op
);
710 static int tid_smaps_open(struct inode
*inode
, struct file
*file
)
712 return do_maps_open(inode
, file
, &proc_tid_smaps_op
);
715 const struct file_operations proc_pid_smaps_operations
= {
716 .open
= pid_smaps_open
,
719 .release
= proc_map_release
,
722 const struct file_operations proc_tid_smaps_operations
= {
723 .open
= tid_smaps_open
,
726 .release
= proc_map_release
,
729 enum clear_refs_types
{
733 CLEAR_REFS_SOFT_DIRTY
,
734 CLEAR_REFS_MM_HIWATER_RSS
,
738 struct clear_refs_private
{
739 enum clear_refs_types type
;
742 #ifdef CONFIG_MEM_SOFT_DIRTY
743 static inline void clear_soft_dirty(struct vm_area_struct
*vma
,
744 unsigned long addr
, pte_t
*pte
)
747 * The soft-dirty tracker uses #PF-s to catch writes
748 * to pages, so write-protect the pte as well. See the
749 * Documentation/vm/soft-dirty.txt for full description
750 * of how soft-dirty works.
754 if (pte_present(ptent
)) {
755 ptent
= pte_wrprotect(ptent
);
756 ptent
= pte_clear_flags(ptent
, _PAGE_SOFT_DIRTY
);
757 } else if (is_swap_pte(ptent
)) {
758 ptent
= pte_swp_clear_soft_dirty(ptent
);
761 set_pte_at(vma
->vm_mm
, addr
, pte
, ptent
);
764 static inline void clear_soft_dirty_pmd(struct vm_area_struct
*vma
,
765 unsigned long addr
, pmd_t
*pmdp
)
769 pmd
= pmd_wrprotect(pmd
);
770 pmd
= pmd_clear_flags(pmd
, _PAGE_SOFT_DIRTY
);
772 if (vma
->vm_flags
& VM_SOFTDIRTY
)
773 vma
->vm_flags
&= ~VM_SOFTDIRTY
;
775 set_pmd_at(vma
->vm_mm
, addr
, pmdp
, pmd
);
780 static inline void clear_soft_dirty(struct vm_area_struct
*vma
,
781 unsigned long addr
, pte_t
*pte
)
785 static inline void clear_soft_dirty_pmd(struct vm_area_struct
*vma
,
786 unsigned long addr
, pmd_t
*pmdp
)
791 static int clear_refs_pte_range(pmd_t
*pmd
, unsigned long addr
,
792 unsigned long end
, struct mm_walk
*walk
)
794 struct clear_refs_private
*cp
= walk
->private;
795 struct vm_area_struct
*vma
= walk
->vma
;
800 if (pmd_trans_huge_lock(pmd
, vma
, &ptl
) == 1) {
801 if (cp
->type
== CLEAR_REFS_SOFT_DIRTY
) {
802 clear_soft_dirty_pmd(vma
, addr
, pmd
);
806 page
= pmd_page(*pmd
);
808 /* Clear accessed and referenced bits. */
809 pmdp_test_and_clear_young(vma
, addr
, pmd
);
810 ClearPageReferenced(page
);
816 if (pmd_trans_unstable(pmd
))
819 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
820 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
823 if (cp
->type
== CLEAR_REFS_SOFT_DIRTY
) {
824 clear_soft_dirty(vma
, addr
, pte
);
828 if (!pte_present(ptent
))
831 page
= vm_normal_page(vma
, addr
, ptent
);
835 /* Clear accessed and referenced bits. */
836 ptep_test_and_clear_young(vma
, addr
, pte
);
837 ClearPageReferenced(page
);
839 pte_unmap_unlock(pte
- 1, ptl
);
844 static int clear_refs_test_walk(unsigned long start
, unsigned long end
,
845 struct mm_walk
*walk
)
847 struct clear_refs_private
*cp
= walk
->private;
848 struct vm_area_struct
*vma
= walk
->vma
;
850 if (vma
->vm_flags
& VM_PFNMAP
)
854 * Writing 1 to /proc/pid/clear_refs affects all pages.
855 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
856 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
857 * Writing 4 to /proc/pid/clear_refs affects all pages.
859 if (cp
->type
== CLEAR_REFS_ANON
&& vma
->vm_file
)
861 if (cp
->type
== CLEAR_REFS_MAPPED
&& !vma
->vm_file
)
866 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
867 size_t count
, loff_t
*ppos
)
869 struct task_struct
*task
;
870 char buffer
[PROC_NUMBUF
];
871 struct mm_struct
*mm
;
872 struct vm_area_struct
*vma
;
873 enum clear_refs_types type
;
877 memset(buffer
, 0, sizeof(buffer
));
878 if (count
> sizeof(buffer
) - 1)
879 count
= sizeof(buffer
) - 1;
880 if (copy_from_user(buffer
, buf
, count
))
882 rv
= kstrtoint(strstrip(buffer
), 10, &itype
);
885 type
= (enum clear_refs_types
)itype
;
886 if (type
< CLEAR_REFS_ALL
|| type
>= CLEAR_REFS_LAST
)
889 task
= get_proc_task(file_inode(file
));
892 mm
= get_task_mm(task
);
894 struct clear_refs_private cp
= {
897 struct mm_walk clear_refs_walk
= {
898 .pmd_entry
= clear_refs_pte_range
,
899 .test_walk
= clear_refs_test_walk
,
904 if (type
== CLEAR_REFS_MM_HIWATER_RSS
) {
906 * Writing 5 to /proc/pid/clear_refs resets the peak
907 * resident set size to this mm's current rss value.
909 down_write(&mm
->mmap_sem
);
910 reset_mm_hiwater_rss(mm
);
911 up_write(&mm
->mmap_sem
);
915 down_read(&mm
->mmap_sem
);
916 if (type
== CLEAR_REFS_SOFT_DIRTY
) {
917 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
918 if (!(vma
->vm_flags
& VM_SOFTDIRTY
))
920 up_read(&mm
->mmap_sem
);
921 down_write(&mm
->mmap_sem
);
922 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
923 vma
->vm_flags
&= ~VM_SOFTDIRTY
;
924 vma_set_page_prot(vma
);
926 downgrade_write(&mm
->mmap_sem
);
929 mmu_notifier_invalidate_range_start(mm
, 0, -1);
931 walk_page_range(0, ~0UL, &clear_refs_walk
);
932 if (type
== CLEAR_REFS_SOFT_DIRTY
)
933 mmu_notifier_invalidate_range_end(mm
, 0, -1);
935 up_read(&mm
->mmap_sem
);
939 put_task_struct(task
);
944 const struct file_operations proc_clear_refs_operations
= {
945 .write
= clear_refs_write
,
946 .llseek
= noop_llseek
,
954 int pos
, len
; /* units: PM_ENTRY_BYTES, not bytes */
955 pagemap_entry_t
*buffer
;
959 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
960 #define PAGEMAP_WALK_MASK (PMD_MASK)
962 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
963 #define PM_PFRAME_BITS 55
964 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
965 #define PM_SOFT_DIRTY BIT_ULL(55)
966 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
967 #define PM_FILE BIT_ULL(61)
968 #define PM_SWAP BIT_ULL(62)
969 #define PM_PRESENT BIT_ULL(63)
971 #define PM_END_OF_BUFFER 1
973 static inline pagemap_entry_t
make_pme(u64 frame
, u64 flags
)
975 return (pagemap_entry_t
) { .pme
= (frame
& PM_PFRAME_MASK
) | flags
};
978 static int add_to_pagemap(unsigned long addr
, pagemap_entry_t
*pme
,
979 struct pagemapread
*pm
)
981 pm
->buffer
[pm
->pos
++] = *pme
;
982 if (pm
->pos
>= pm
->len
)
983 return PM_END_OF_BUFFER
;
987 static int pagemap_pte_hole(unsigned long start
, unsigned long end
,
988 struct mm_walk
*walk
)
990 struct pagemapread
*pm
= walk
->private;
991 unsigned long addr
= start
;
995 struct vm_area_struct
*vma
= find_vma(walk
->mm
, addr
);
996 pagemap_entry_t pme
= make_pme(0, 0);
997 /* End of address space hole, which we mark as non-present. */
998 unsigned long hole_end
;
1001 hole_end
= min(end
, vma
->vm_start
);
1005 for (; addr
< hole_end
; addr
+= PAGE_SIZE
) {
1006 err
= add_to_pagemap(addr
, &pme
, pm
);
1014 /* Addresses in the VMA. */
1015 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1016 pme
= make_pme(0, PM_SOFT_DIRTY
);
1017 for (; addr
< min(end
, vma
->vm_end
); addr
+= PAGE_SIZE
) {
1018 err
= add_to_pagemap(addr
, &pme
, pm
);
1027 static pagemap_entry_t
pte_to_pagemap_entry(struct pagemapread
*pm
,
1028 struct vm_area_struct
*vma
, unsigned long addr
, pte_t pte
)
1030 u64 frame
= 0, flags
= 0;
1031 struct page
*page
= NULL
;
1033 if (pte_present(pte
)) {
1035 frame
= pte_pfn(pte
);
1036 flags
|= PM_PRESENT
;
1037 page
= vm_normal_page(vma
, addr
, pte
);
1038 if (pte_soft_dirty(pte
))
1039 flags
|= PM_SOFT_DIRTY
;
1040 } else if (is_swap_pte(pte
)) {
1042 if (pte_swp_soft_dirty(pte
))
1043 flags
|= PM_SOFT_DIRTY
;
1044 entry
= pte_to_swp_entry(pte
);
1045 frame
= swp_type(entry
) |
1046 (swp_offset(entry
) << MAX_SWAPFILES_SHIFT
);
1048 if (is_migration_entry(entry
))
1049 page
= migration_entry_to_page(entry
);
1052 if (page
&& !PageAnon(page
))
1054 if (page
&& page_mapcount(page
) == 1)
1055 flags
|= PM_MMAP_EXCLUSIVE
;
1056 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1057 flags
|= PM_SOFT_DIRTY
;
1059 return make_pme(frame
, flags
);
1062 static int pagemap_pmd_range(pmd_t
*pmdp
, unsigned long addr
, unsigned long end
,
1063 struct mm_walk
*walk
)
1065 struct vm_area_struct
*vma
= walk
->vma
;
1066 struct pagemapread
*pm
= walk
->private;
1068 pte_t
*pte
, *orig_pte
;
1071 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1072 if (pmd_trans_huge_lock(pmdp
, vma
, &ptl
) == 1) {
1073 u64 flags
= 0, frame
= 0;
1076 if ((vma
->vm_flags
& VM_SOFTDIRTY
) || pmd_soft_dirty(pmd
))
1077 flags
|= PM_SOFT_DIRTY
;
1080 * Currently pmd for thp is always present because thp
1081 * can not be swapped-out, migrated, or HWPOISONed
1082 * (split in such cases instead.)
1083 * This if-check is just to prepare for future implementation.
1085 if (pmd_present(pmd
)) {
1086 struct page
*page
= pmd_page(pmd
);
1088 if (page_mapcount(page
) == 1)
1089 flags
|= PM_MMAP_EXCLUSIVE
;
1091 flags
|= PM_PRESENT
;
1093 frame
= pmd_pfn(pmd
) +
1094 ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1097 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1098 pagemap_entry_t pme
= make_pme(frame
, flags
);
1100 err
= add_to_pagemap(addr
, &pme
, pm
);
1103 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1110 if (pmd_trans_unstable(pmdp
))
1112 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1115 * We can assume that @vma always points to a valid one and @end never
1116 * goes beyond vma->vm_end.
1118 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmdp
, addr
, &ptl
);
1119 for (; addr
< end
; pte
++, addr
+= PAGE_SIZE
) {
1120 pagemap_entry_t pme
;
1122 pme
= pte_to_pagemap_entry(pm
, vma
, addr
, *pte
);
1123 err
= add_to_pagemap(addr
, &pme
, pm
);
1127 pte_unmap_unlock(orig_pte
, ptl
);
1134 #ifdef CONFIG_HUGETLB_PAGE
1135 /* This function walks within one hugetlb entry in the single call */
1136 static int pagemap_hugetlb_range(pte_t
*ptep
, unsigned long hmask
,
1137 unsigned long addr
, unsigned long end
,
1138 struct mm_walk
*walk
)
1140 struct pagemapread
*pm
= walk
->private;
1141 struct vm_area_struct
*vma
= walk
->vma
;
1142 u64 flags
= 0, frame
= 0;
1146 if (vma
->vm_flags
& VM_SOFTDIRTY
)
1147 flags
|= PM_SOFT_DIRTY
;
1149 pte
= huge_ptep_get(ptep
);
1150 if (pte_present(pte
)) {
1151 struct page
*page
= pte_page(pte
);
1153 if (!PageAnon(page
))
1156 if (page_mapcount(page
) == 1)
1157 flags
|= PM_MMAP_EXCLUSIVE
;
1159 flags
|= PM_PRESENT
;
1161 frame
= pte_pfn(pte
) +
1162 ((addr
& ~hmask
) >> PAGE_SHIFT
);
1165 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
1166 pagemap_entry_t pme
= make_pme(frame
, flags
);
1168 err
= add_to_pagemap(addr
, &pme
, pm
);
1171 if (pm
->show_pfn
&& (flags
& PM_PRESENT
))
1179 #endif /* HUGETLB_PAGE */
1182 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1184 * For each page in the address space, this file contains one 64-bit entry
1185 * consisting of the following:
1187 * Bits 0-54 page frame number (PFN) if present
1188 * Bits 0-4 swap type if swapped
1189 * Bits 5-54 swap offset if swapped
1190 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1191 * Bit 56 page exclusively mapped
1193 * Bit 61 page is file-page or shared-anon
1194 * Bit 62 page swapped
1195 * Bit 63 page present
1197 * If the page is not present but in swap, then the PFN contains an
1198 * encoding of the swap file number and the page's offset into the
1199 * swap. Unmapped pages return a null PFN. This allows determining
1200 * precisely which pages are mapped (or in swap) and comparing mapped
1201 * pages between processes.
1203 * Efficient users of this interface will use /proc/pid/maps to
1204 * determine which areas of memory are actually mapped and llseek to
1205 * skip over unmapped regions.
1207 static ssize_t
pagemap_read(struct file
*file
, char __user
*buf
,
1208 size_t count
, loff_t
*ppos
)
1210 struct mm_struct
*mm
= file
->private_data
;
1211 struct pagemapread pm
;
1212 struct mm_walk pagemap_walk
= {};
1214 unsigned long svpfn
;
1215 unsigned long start_vaddr
;
1216 unsigned long end_vaddr
;
1217 int ret
= 0, copied
= 0;
1219 if (!mm
|| !atomic_inc_not_zero(&mm
->mm_users
))
1223 /* file position must be aligned */
1224 if ((*ppos
% PM_ENTRY_BYTES
) || (count
% PM_ENTRY_BYTES
))
1231 /* do not disclose physical addresses: attack vector */
1232 pm
.show_pfn
= file_ns_capable(file
, &init_user_ns
, CAP_SYS_ADMIN
);
1234 pm
.len
= (PAGEMAP_WALK_SIZE
>> PAGE_SHIFT
);
1235 pm
.buffer
= kmalloc(pm
.len
* PM_ENTRY_BYTES
, GFP_TEMPORARY
);
1240 pagemap_walk
.pmd_entry
= pagemap_pmd_range
;
1241 pagemap_walk
.pte_hole
= pagemap_pte_hole
;
1242 #ifdef CONFIG_HUGETLB_PAGE
1243 pagemap_walk
.hugetlb_entry
= pagemap_hugetlb_range
;
1245 pagemap_walk
.mm
= mm
;
1246 pagemap_walk
.private = &pm
;
1249 svpfn
= src
/ PM_ENTRY_BYTES
;
1250 start_vaddr
= svpfn
<< PAGE_SHIFT
;
1251 end_vaddr
= mm
->task_size
;
1253 /* watch out for wraparound */
1254 if (svpfn
> mm
->task_size
>> PAGE_SHIFT
)
1255 start_vaddr
= end_vaddr
;
1258 * The odds are that this will stop walking way
1259 * before end_vaddr, because the length of the
1260 * user buffer is tracked in "pm", and the walk
1261 * will stop when we hit the end of the buffer.
1264 while (count
&& (start_vaddr
< end_vaddr
)) {
1269 end
= (start_vaddr
+ PAGEMAP_WALK_SIZE
) & PAGEMAP_WALK_MASK
;
1271 if (end
< start_vaddr
|| end
> end_vaddr
)
1273 down_read(&mm
->mmap_sem
);
1274 ret
= walk_page_range(start_vaddr
, end
, &pagemap_walk
);
1275 up_read(&mm
->mmap_sem
);
1278 len
= min(count
, PM_ENTRY_BYTES
* pm
.pos
);
1279 if (copy_to_user(buf
, pm
.buffer
, len
)) {
1288 if (!ret
|| ret
== PM_END_OF_BUFFER
)
1299 static int pagemap_open(struct inode
*inode
, struct file
*file
)
1301 struct mm_struct
*mm
;
1303 mm
= proc_mem_open(inode
, PTRACE_MODE_READ
);
1306 file
->private_data
= mm
;
1310 static int pagemap_release(struct inode
*inode
, struct file
*file
)
1312 struct mm_struct
*mm
= file
->private_data
;
1319 const struct file_operations proc_pagemap_operations
= {
1320 .llseek
= mem_lseek
, /* borrow this */
1321 .read
= pagemap_read
,
1322 .open
= pagemap_open
,
1323 .release
= pagemap_release
,
1325 #endif /* CONFIG_PROC_PAGE_MONITOR */
1330 unsigned long pages
;
1332 unsigned long active
;
1333 unsigned long writeback
;
1334 unsigned long mapcount_max
;
1335 unsigned long dirty
;
1336 unsigned long swapcache
;
1337 unsigned long node
[MAX_NUMNODES
];
1340 struct numa_maps_private
{
1341 struct proc_maps_private proc_maps
;
1342 struct numa_maps md
;
1345 static void gather_stats(struct page
*page
, struct numa_maps
*md
, int pte_dirty
,
1346 unsigned long nr_pages
)
1348 int count
= page_mapcount(page
);
1350 md
->pages
+= nr_pages
;
1351 if (pte_dirty
|| PageDirty(page
))
1352 md
->dirty
+= nr_pages
;
1354 if (PageSwapCache(page
))
1355 md
->swapcache
+= nr_pages
;
1357 if (PageActive(page
) || PageUnevictable(page
))
1358 md
->active
+= nr_pages
;
1360 if (PageWriteback(page
))
1361 md
->writeback
+= nr_pages
;
1364 md
->anon
+= nr_pages
;
1366 if (count
> md
->mapcount_max
)
1367 md
->mapcount_max
= count
;
1369 md
->node
[page_to_nid(page
)] += nr_pages
;
1372 static struct page
*can_gather_numa_stats(pte_t pte
, struct vm_area_struct
*vma
,
1378 if (!pte_present(pte
))
1381 page
= vm_normal_page(vma
, addr
, pte
);
1385 if (PageReserved(page
))
1388 nid
= page_to_nid(page
);
1389 if (!node_isset(nid
, node_states
[N_MEMORY
]))
1395 static int gather_pte_stats(pmd_t
*pmd
, unsigned long addr
,
1396 unsigned long end
, struct mm_walk
*walk
)
1398 struct numa_maps
*md
= walk
->private;
1399 struct vm_area_struct
*vma
= walk
->vma
;
1404 if (pmd_trans_huge_lock(pmd
, vma
, &ptl
) == 1) {
1405 pte_t huge_pte
= *(pte_t
*)pmd
;
1408 page
= can_gather_numa_stats(huge_pte
, vma
, addr
);
1410 gather_stats(page
, md
, pte_dirty(huge_pte
),
1411 HPAGE_PMD_SIZE
/PAGE_SIZE
);
1416 if (pmd_trans_unstable(pmd
))
1418 orig_pte
= pte
= pte_offset_map_lock(walk
->mm
, pmd
, addr
, &ptl
);
1420 struct page
*page
= can_gather_numa_stats(*pte
, vma
, addr
);
1423 gather_stats(page
, md
, pte_dirty(*pte
), 1);
1425 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1426 pte_unmap_unlock(orig_pte
, ptl
);
1429 #ifdef CONFIG_HUGETLB_PAGE
1430 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1431 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1433 struct numa_maps
*md
;
1436 if (!pte_present(*pte
))
1439 page
= pte_page(*pte
);
1444 gather_stats(page
, md
, pte_dirty(*pte
), 1);
1449 static int gather_hugetlb_stats(pte_t
*pte
, unsigned long hmask
,
1450 unsigned long addr
, unsigned long end
, struct mm_walk
*walk
)
1457 * Display pages allocated per node and memory policy via /proc.
1459 static int show_numa_map(struct seq_file
*m
, void *v
, int is_pid
)
1461 struct numa_maps_private
*numa_priv
= m
->private;
1462 struct proc_maps_private
*proc_priv
= &numa_priv
->proc_maps
;
1463 struct vm_area_struct
*vma
= v
;
1464 struct numa_maps
*md
= &numa_priv
->md
;
1465 struct file
*file
= vma
->vm_file
;
1466 struct mm_struct
*mm
= vma
->vm_mm
;
1467 struct mm_walk walk
= {
1468 .hugetlb_entry
= gather_hugetlb_stats
,
1469 .pmd_entry
= gather_pte_stats
,
1473 struct mempolicy
*pol
;
1480 /* Ensure we start with an empty set of numa_maps statistics. */
1481 memset(md
, 0, sizeof(*md
));
1483 pol
= __get_vma_policy(vma
, vma
->vm_start
);
1485 mpol_to_str(buffer
, sizeof(buffer
), pol
);
1488 mpol_to_str(buffer
, sizeof(buffer
), proc_priv
->task_mempolicy
);
1491 seq_printf(m
, "%08lx %s", vma
->vm_start
, buffer
);
1494 seq_puts(m
, " file=");
1495 seq_file_path(m
, file
, "\n\t= ");
1496 } else if (vma
->vm_start
<= mm
->brk
&& vma
->vm_end
>= mm
->start_brk
) {
1497 seq_puts(m
, " heap");
1499 pid_t tid
= pid_of_stack(proc_priv
, vma
, is_pid
);
1502 * Thread stack in /proc/PID/task/TID/maps or
1503 * the main process stack.
1505 if (!is_pid
|| (vma
->vm_start
<= mm
->start_stack
&&
1506 vma
->vm_end
>= mm
->start_stack
))
1507 seq_puts(m
, " stack");
1509 seq_printf(m
, " stack:%d", tid
);
1513 if (is_vm_hugetlb_page(vma
))
1514 seq_puts(m
, " huge");
1516 /* mmap_sem is held by m_start */
1517 walk_page_vma(vma
, &walk
);
1523 seq_printf(m
, " anon=%lu", md
->anon
);
1526 seq_printf(m
, " dirty=%lu", md
->dirty
);
1528 if (md
->pages
!= md
->anon
&& md
->pages
!= md
->dirty
)
1529 seq_printf(m
, " mapped=%lu", md
->pages
);
1531 if (md
->mapcount_max
> 1)
1532 seq_printf(m
, " mapmax=%lu", md
->mapcount_max
);
1535 seq_printf(m
, " swapcache=%lu", md
->swapcache
);
1537 if (md
->active
< md
->pages
&& !is_vm_hugetlb_page(vma
))
1538 seq_printf(m
, " active=%lu", md
->active
);
1541 seq_printf(m
, " writeback=%lu", md
->writeback
);
1543 for_each_node_state(nid
, N_MEMORY
)
1545 seq_printf(m
, " N%d=%lu", nid
, md
->node
[nid
]);
1547 seq_printf(m
, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma
) >> 10);
1550 m_cache_vma(m
, vma
);
1554 static int show_pid_numa_map(struct seq_file
*m
, void *v
)
1556 return show_numa_map(m
, v
, 1);
1559 static int show_tid_numa_map(struct seq_file
*m
, void *v
)
1561 return show_numa_map(m
, v
, 0);
1564 static const struct seq_operations proc_pid_numa_maps_op
= {
1568 .show
= show_pid_numa_map
,
1571 static const struct seq_operations proc_tid_numa_maps_op
= {
1575 .show
= show_tid_numa_map
,
1578 static int numa_maps_open(struct inode
*inode
, struct file
*file
,
1579 const struct seq_operations
*ops
)
1581 return proc_maps_open(inode
, file
, ops
,
1582 sizeof(struct numa_maps_private
));
1585 static int pid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1587 return numa_maps_open(inode
, file
, &proc_pid_numa_maps_op
);
1590 static int tid_numa_maps_open(struct inode
*inode
, struct file
*file
)
1592 return numa_maps_open(inode
, file
, &proc_tid_numa_maps_op
);
1595 const struct file_operations proc_pid_numa_maps_operations
= {
1596 .open
= pid_numa_maps_open
,
1598 .llseek
= seq_lseek
,
1599 .release
= proc_map_release
,
1602 const struct file_operations proc_tid_numa_maps_operations
= {
1603 .open
= tid_numa_maps_open
,
1605 .llseek
= seq_lseek
,
1606 .release
= proc_map_release
,
1608 #endif /* CONFIG_NUMA */