xfs: fix type usage
[linux/fpc-iii.git] / fs / proc / task_mmu.c
blob6744bd706ecf018f0db0a3e335449945b523ea74
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mm.h>
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>
22 #include <asm/elf.h>
23 #include <asm/tlb.h>
24 #include <asm/tlbflush.h>
25 #include "internal.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);
55 seq_printf(m,
56 "VmPeak:\t%8lu kB\n"
57 "VmSize:\t%8lu kB\n"
58 "VmLck:\t%8lu kB\n"
59 "VmPin:\t%8lu kB\n"
60 "VmHWM:\t%8lu kB\n"
61 "VmRSS:\t%8lu kB\n"
62 "RssAnon:\t%8lu kB\n"
63 "RssFile:\t%8lu kB\n"
64 "RssShmem:\t%8lu kB\n"
65 "VmData:\t%8lu kB\n"
66 "VmStk:\t%8lu kB\n"
67 "VmExe:\t%8lu kB\n"
68 "VmLib:\t%8lu kB\n"
69 "VmPTE:\t%8lu kB\n"
70 "VmPMD:\t%8lu kB\n"
71 "VmSwap:\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,
83 ptes >> 10,
84 pmds >> 10,
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))
101 >> PAGE_SHIFT;
102 *data = mm->data_vm + mm->stack_vm;
103 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
104 return mm->total_vm;
107 #ifdef CONFIG_NUMA
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;
115 task_lock(task);
116 priv->task_mempolicy = get_task_policy(task);
117 mpol_get(priv->task_mempolicy);
118 task_unlock(task);
120 static void release_task_mempolicy(struct proc_maps_private *priv)
122 mpol_put(priv->task_mempolicy);
124 #else
125 static void hold_task_mempolicy(struct proc_maps_private *priv)
128 static void release_task_mempolicy(struct proc_maps_private *priv)
131 #endif
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);
139 mmput(mm);
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)
146 return NULL;
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)
166 return NULL;
168 priv->task = get_proc_task(priv->inode);
169 if (!priv->task)
170 return ERR_PTR(-ESRCH);
172 mm = priv->mm;
173 if (!mm || !mmget_not_zero(mm))
174 return NULL;
176 down_read(&mm->mmap_sem);
177 hold_task_mempolicy(priv);
178 priv->tail_vma = get_gate_vma(mm);
180 if (last_addr) {
181 vma = find_vma(mm, last_addr - 1);
182 if (vma && vma->vm_start <= last_addr)
183 vma = m_next_vma(priv, vma);
184 if (vma)
185 return vma;
188 m->version = 0;
189 if (pos < mm->map_count) {
190 for (vma = mm->mmap; pos; pos--) {
191 m->version = vma->vm_start;
192 vma = vma->vm_next;
194 return vma;
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;
201 vma_stop(priv);
202 return NULL;
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;
210 (*pos)++;
211 next = m_next_vma(priv, v);
212 if (!next)
213 vma_stop(priv);
214 return next;
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))
222 vma_stop(priv);
223 if (priv->task) {
224 put_task_struct(priv->task);
225 priv->task = NULL;
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);
234 if (!priv)
235 return -ENOMEM;
237 priv->inode = inode;
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);
243 return err;
246 return 0;
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;
254 if (priv->mm)
255 mmdrop(priv->mm);
257 kfree(priv->rollup);
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
277 * languages like Go.
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 ",
290 start,
291 end,
292 flags & VM_READ ? 'r' : '-',
293 flags & VM_WRITE ? 'w' : '-',
294 flags & VM_EXEC ? 'x' : '-',
295 flags & VM_MAYSHARE ? 's' : 'p',
296 pgoff,
297 MAJOR(dev), MINOR(dev), ino);
300 static void
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;
309 dev_t dev = 0;
310 const char *name = NULL;
312 if (file) {
313 struct inode *inode = file_inode(vma->vm_file);
314 dev = inode->i_sb->s_dev;
315 ino = inode->i_ino;
316 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
319 start = vma->vm_start;
320 end = vma->vm_end;
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:
327 if (file) {
328 seq_pad(m, ' ');
329 seq_file_path(m, file, "\n");
330 goto done;
333 if (vma->vm_ops && vma->vm_ops->name) {
334 name = vma->vm_ops->name(vma);
335 if (name)
336 goto done;
339 name = arch_vma_name(vma);
340 if (!name) {
341 if (!mm) {
342 name = "[vdso]";
343 goto done;
346 if (vma->vm_start <= mm->brk &&
347 vma->vm_end >= mm->start_brk) {
348 name = "[heap]";
349 goto done;
352 if (is_stack(vma))
353 name = "[stack]";
356 done:
357 if (name) {
358 seq_pad(m, ' ');
359 seq_puts(m, name);
361 seq_putc(m, '\n');
364 static int show_map(struct seq_file *m, void *v, int is_pid)
366 show_map_vma(m, v, is_pid);
367 m_cache_vma(m, v);
368 return 0;
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 = {
382 .start = m_start,
383 .next = m_next,
384 .stop = m_stop,
385 .show = show_pid_map
388 static const struct seq_operations proc_tid_maps_op = {
389 .start = m_start,
390 .next = m_next,
391 .stop = m_stop,
392 .show = show_tid_map
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,
407 .read = seq_read,
408 .llseek = seq_lseek,
409 .release = proc_map_release,
412 const struct file_operations proc_tid_maps_operations = {
413 .open = tid_maps_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
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.
436 #define PSS_SHIFT 12
438 #ifdef CONFIG_PROC_PAGE_MONITOR
439 struct mem_size_stats {
440 bool first;
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;
451 unsigned long swap;
452 unsigned long shared_hugetlb;
453 unsigned long private_hugetlb;
454 unsigned long first_vma_start;
455 u64 pss;
456 u64 pss_locked;
457 u64 swap_pss;
458 bool check_shmem_swap;
461 static void smaps_account(struct mem_size_stats *mss, struct page *page,
462 bool compound, bool young, bool dirty)
464 int i, nr = compound ? 1 << compound_order(page) : 1;
465 unsigned long size = nr * PAGE_SIZE;
467 if (PageAnon(page)) {
468 mss->anonymous += size;
469 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
470 mss->lazyfree += size;
473 mss->resident += size;
474 /* Accumulate the size in pages that have been accessed. */
475 if (young || page_is_young(page) || PageReferenced(page))
476 mss->referenced += size;
479 * page_count(page) == 1 guarantees the page is mapped exactly once.
480 * If any subpage of the compound page mapped with PTE it would elevate
481 * page_count().
483 if (page_count(page) == 1) {
484 if (dirty || PageDirty(page))
485 mss->private_dirty += size;
486 else
487 mss->private_clean += size;
488 mss->pss += (u64)size << PSS_SHIFT;
489 return;
492 for (i = 0; i < nr; i++, page++) {
493 int mapcount = page_mapcount(page);
495 if (mapcount >= 2) {
496 if (dirty || PageDirty(page))
497 mss->shared_dirty += PAGE_SIZE;
498 else
499 mss->shared_clean += PAGE_SIZE;
500 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
501 } else {
502 if (dirty || PageDirty(page))
503 mss->private_dirty += PAGE_SIZE;
504 else
505 mss->private_clean += PAGE_SIZE;
506 mss->pss += PAGE_SIZE << PSS_SHIFT;
511 #ifdef CONFIG_SHMEM
512 static int smaps_pte_hole(unsigned long addr, unsigned long end,
513 struct mm_walk *walk)
515 struct mem_size_stats *mss = walk->private;
517 mss->swap += shmem_partial_swap_usage(
518 walk->vma->vm_file->f_mapping, addr, end);
520 return 0;
522 #endif
524 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
525 struct mm_walk *walk)
527 struct mem_size_stats *mss = walk->private;
528 struct vm_area_struct *vma = walk->vma;
529 struct page *page = NULL;
531 if (pte_present(*pte)) {
532 page = vm_normal_page(vma, addr, *pte);
533 } else if (is_swap_pte(*pte)) {
534 swp_entry_t swpent = pte_to_swp_entry(*pte);
536 if (!non_swap_entry(swpent)) {
537 int mapcount;
539 mss->swap += PAGE_SIZE;
540 mapcount = swp_swapcount(swpent);
541 if (mapcount >= 2) {
542 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
544 do_div(pss_delta, mapcount);
545 mss->swap_pss += pss_delta;
546 } else {
547 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
549 } else if (is_migration_entry(swpent))
550 page = migration_entry_to_page(swpent);
551 else if (is_device_private_entry(swpent))
552 page = device_private_entry_to_page(swpent);
553 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
554 && pte_none(*pte))) {
555 page = find_get_entry(vma->vm_file->f_mapping,
556 linear_page_index(vma, addr));
557 if (!page)
558 return;
560 if (radix_tree_exceptional_entry(page))
561 mss->swap += PAGE_SIZE;
562 else
563 put_page(page);
565 return;
568 if (!page)
569 return;
571 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
574 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
575 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
576 struct mm_walk *walk)
578 struct mem_size_stats *mss = walk->private;
579 struct vm_area_struct *vma = walk->vma;
580 struct page *page;
582 /* FOLL_DUMP will return -EFAULT on huge zero page */
583 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
584 if (IS_ERR_OR_NULL(page))
585 return;
586 if (PageAnon(page))
587 mss->anonymous_thp += HPAGE_PMD_SIZE;
588 else if (PageSwapBacked(page))
589 mss->shmem_thp += HPAGE_PMD_SIZE;
590 else if (is_zone_device_page(page))
591 /* pass */;
592 else
593 VM_BUG_ON_PAGE(1, page);
594 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
596 #else
597 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
598 struct mm_walk *walk)
601 #endif
603 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
604 struct mm_walk *walk)
606 struct vm_area_struct *vma = walk->vma;
607 pte_t *pte;
608 spinlock_t *ptl;
610 ptl = pmd_trans_huge_lock(pmd, vma);
611 if (ptl) {
612 if (pmd_present(*pmd))
613 smaps_pmd_entry(pmd, addr, walk);
614 spin_unlock(ptl);
615 goto out;
618 if (pmd_trans_unstable(pmd))
619 goto out;
621 * The mmap_sem held all the way back in m_start() is what
622 * keeps khugepaged out of here and from collapsing things
623 * in here.
625 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
626 for (; addr != end; pte++, addr += PAGE_SIZE)
627 smaps_pte_entry(pte, addr, walk);
628 pte_unmap_unlock(pte - 1, ptl);
629 out:
630 cond_resched();
631 return 0;
634 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
637 * Don't forget to update Documentation/ on changes.
639 static const char mnemonics[BITS_PER_LONG][2] = {
641 * In case if we meet a flag we don't know about.
643 [0 ... (BITS_PER_LONG-1)] = "??",
645 [ilog2(VM_READ)] = "rd",
646 [ilog2(VM_WRITE)] = "wr",
647 [ilog2(VM_EXEC)] = "ex",
648 [ilog2(VM_SHARED)] = "sh",
649 [ilog2(VM_MAYREAD)] = "mr",
650 [ilog2(VM_MAYWRITE)] = "mw",
651 [ilog2(VM_MAYEXEC)] = "me",
652 [ilog2(VM_MAYSHARE)] = "ms",
653 [ilog2(VM_GROWSDOWN)] = "gd",
654 [ilog2(VM_PFNMAP)] = "pf",
655 [ilog2(VM_DENYWRITE)] = "dw",
656 #ifdef CONFIG_X86_INTEL_MPX
657 [ilog2(VM_MPX)] = "mp",
658 #endif
659 [ilog2(VM_LOCKED)] = "lo",
660 [ilog2(VM_IO)] = "io",
661 [ilog2(VM_SEQ_READ)] = "sr",
662 [ilog2(VM_RAND_READ)] = "rr",
663 [ilog2(VM_DONTCOPY)] = "dc",
664 [ilog2(VM_DONTEXPAND)] = "de",
665 [ilog2(VM_ACCOUNT)] = "ac",
666 [ilog2(VM_NORESERVE)] = "nr",
667 [ilog2(VM_HUGETLB)] = "ht",
668 [ilog2(VM_ARCH_1)] = "ar",
669 [ilog2(VM_WIPEONFORK)] = "wf",
670 [ilog2(VM_DONTDUMP)] = "dd",
671 #ifdef CONFIG_MEM_SOFT_DIRTY
672 [ilog2(VM_SOFTDIRTY)] = "sd",
673 #endif
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)] = "",
686 #endif
688 size_t i;
690 seq_puts(m, "VmFlags: ");
691 for (i = 0; i < BITS_PER_LONG; i++) {
692 if (!mnemonics[i][0])
693 continue;
694 if (vma->vm_flags & (1UL << i)) {
695 seq_printf(m, "%c%c ",
696 mnemonics[i][0], mnemonics[i][1]);
699 seq_putc(m, '\n');
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);
721 if (page) {
722 int mapcount = page_mapcount(page);
724 if (mapcount >= 2)
725 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
726 else
727 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
729 return 0;
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,
747 #endif
748 .mm = vma->vm_mm,
750 int ret = 0;
751 bool rollup_mode;
752 bool last_vma;
754 if (priv->rollup) {
755 rollup_mode = true;
756 mss = priv->rollup;
757 if (mss->first) {
758 mss->first_vma_start = vma->vm_start;
759 mss->first = false;
761 last_vma = !m_next_vma(priv, vma);
762 } else {
763 rollup_mode = false;
764 memset(&mss_stack, 0, sizeof(mss_stack));
765 mss = &mss_stack;
768 smaps_walk.private = mss;
770 #ifdef CONFIG_SHMEM
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;
787 } else {
788 mss->check_shmem_swap = true;
789 smaps_walk.pte_hole = smaps_pte_hole;
792 #endif
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;
799 if (!rollup_mode) {
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);
804 seq_pad(m, ' ');
805 seq_puts(m, "[rollup]\n");
806 } else {
807 ret = SEQ_SKIP;
810 if (!rollup_mode)
811 seq_printf(m,
812 "Size: %8lu kB\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)
821 seq_printf(m,
822 "Rss: %8lu kB\n"
823 "Pss: %8lu kB\n"
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"
835 "Swap: %8lu kB\n"
836 "SwapPss: %8lu kB\n"
837 "Locked: %8lu kB\n",
838 mss->resident >> 10,
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,
846 mss->lazyfree >> 10,
847 mss->anonymous_thp >> 10,
848 mss->shmem_thp >> 10,
849 mss->shared_hugetlb >> 10,
850 mss->private_hugetlb >> 10,
851 mss->swap >> 10,
852 (unsigned long)(mss->swap_pss >> (10 + PSS_SHIFT)),
853 (unsigned long)(mss->pss >> (10 + PSS_SHIFT)));
855 if (!rollup_mode) {
856 arch_show_smap(m, vma);
857 show_smap_vma_flags(m, vma);
859 m_cache_vma(m, vma);
860 return ret;
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 = {
874 .start = m_start,
875 .next = m_next,
876 .stop = m_stop,
877 .show = show_pid_smap
880 static const struct seq_operations proc_tid_smaps_op = {
881 .start = m_start,
882 .next = m_next,
883 .stop = m_stop,
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);
898 if (ret < 0)
899 return ret;
900 seq = file->private_data;
901 priv = seq->private;
902 priv->rollup = kzalloc(sizeof(*priv->rollup), GFP_KERNEL);
903 if (!priv->rollup) {
904 proc_map_release(inode, file);
905 return -ENOMEM;
907 priv->rollup->first = true;
908 return 0;
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,
918 .read = seq_read,
919 .llseek = seq_lseek,
920 .release = proc_map_release,
923 const struct file_operations proc_pid_smaps_rollup_operations = {
924 .open = pid_smaps_rollup_open,
925 .read = seq_read,
926 .llseek = seq_lseek,
927 .release = proc_map_release,
930 const struct file_operations proc_tid_smaps_operations = {
931 .open = tid_smaps_open,
932 .read = seq_read,
933 .llseek = seq_lseek,
934 .release = proc_map_release,
937 enum clear_refs_types {
938 CLEAR_REFS_ALL = 1,
939 CLEAR_REFS_ANON,
940 CLEAR_REFS_MAPPED,
941 CLEAR_REFS_SOFT_DIRTY,
942 CLEAR_REFS_MM_HIWATER_RSS,
943 CLEAR_REFS_LAST,
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.
960 pte_t ptent = *pte;
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);
972 #else
973 static inline void clear_soft_dirty(struct vm_area_struct *vma,
974 unsigned long addr, pte_t *pte)
977 #endif
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)
983 pmd_t pmd = *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);
1002 #else
1003 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1004 unsigned long addr, pmd_t *pmdp)
1007 #endif
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;
1014 pte_t *pte, ptent;
1015 spinlock_t *ptl;
1016 struct page *page;
1018 ptl = pmd_trans_huge_lock(pmd, vma);
1019 if (ptl) {
1020 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1021 clear_soft_dirty_pmd(vma, addr, pmd);
1022 goto out;
1025 if (!pmd_present(*pmd))
1026 goto out;
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);
1034 out:
1035 spin_unlock(ptl);
1036 return 0;
1039 if (pmd_trans_unstable(pmd))
1040 return 0;
1042 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1043 for (; addr != end; pte++, addr += PAGE_SIZE) {
1044 ptent = *pte;
1046 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1047 clear_soft_dirty(vma, addr, pte);
1048 continue;
1051 if (!pte_present(ptent))
1052 continue;
1054 page = vm_normal_page(vma, addr, ptent);
1055 if (!page)
1056 continue;
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);
1064 cond_resched();
1065 return 0;
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)
1075 return 1;
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)
1084 return 1;
1085 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1086 return 1;
1087 return 0;
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;
1099 int itype;
1100 int rv;
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))
1106 return -EFAULT;
1107 rv = kstrtoint(strstrip(buffer), 10, &itype);
1108 if (rv < 0)
1109 return rv;
1110 type = (enum clear_refs_types)itype;
1111 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1112 return -EINVAL;
1114 task = get_proc_task(file_inode(file));
1115 if (!task)
1116 return -ESRCH;
1117 mm = get_task_mm(task);
1118 if (mm) {
1119 struct clear_refs_private cp = {
1120 .type = type,
1122 struct mm_walk clear_refs_walk = {
1123 .pmd_entry = clear_refs_pte_range,
1124 .test_walk = clear_refs_test_walk,
1125 .mm = mm,
1126 .private = &cp,
1129 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1130 if (down_write_killable(&mm->mmap_sem)) {
1131 count = -EINTR;
1132 goto out_mm;
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);
1141 goto out_mm;
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))
1149 continue;
1150 up_read(&mm->mmap_sem);
1151 if (down_write_killable(&mm->mmap_sem)) {
1152 count = -EINTR;
1153 goto out_mm;
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);
1160 break;
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);
1169 out_mm:
1170 mmput(mm);
1172 put_task_struct(task);
1174 return count;
1177 const struct file_operations proc_clear_refs_operations = {
1178 .write = clear_refs_write,
1179 .llseek = noop_llseek,
1182 typedef struct {
1183 u64 pme;
1184 } pagemap_entry_t;
1186 struct pagemapread {
1187 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
1188 pagemap_entry_t *buffer;
1189 bool show_pfn;
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;
1217 return 0;
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;
1225 int err = 0;
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;
1233 if (vma)
1234 hole_end = min(end, vma->vm_start);
1235 else
1236 hole_end = end;
1238 for (; addr < hole_end; addr += PAGE_SIZE) {
1239 err = add_to_pagemap(addr, &pme, pm);
1240 if (err)
1241 goto out;
1244 if (!vma)
1245 break;
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);
1252 if (err)
1253 goto out;
1256 out:
1257 return err;
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)) {
1267 if (pm->show_pfn)
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)) {
1274 swp_entry_t entry;
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);
1280 flags |= PM_SWAP;
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))
1289 flags |= PM_FILE;
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;
1303 spinlock_t *ptl;
1304 pte_t *pte, *orig_pte;
1305 int err = 0;
1307 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1308 ptl = pmd_trans_huge_lock(pmdp, vma);
1309 if (ptl) {
1310 u64 flags = 0, frame = 0;
1311 pmd_t pmd = *pmdp;
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;
1323 if (pm->show_pfn)
1324 frame = pmd_pfn(pmd) +
1325 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1327 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1328 else if (is_swap_pmd(pmd)) {
1329 swp_entry_t entry = pmd_to_swp_entry(pmd);
1331 frame = swp_type(entry) |
1332 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1333 flags |= PM_SWAP;
1334 if (pmd_swp_soft_dirty(pmd))
1335 flags |= PM_SOFT_DIRTY;
1336 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1337 page = migration_entry_to_page(entry);
1339 #endif
1341 if (page && page_mapcount(page) == 1)
1342 flags |= PM_MMAP_EXCLUSIVE;
1344 for (; addr != end; addr += PAGE_SIZE) {
1345 pagemap_entry_t pme = make_pme(frame, flags);
1347 err = add_to_pagemap(addr, &pme, pm);
1348 if (err)
1349 break;
1350 if (pm->show_pfn && (flags & PM_PRESENT))
1351 frame++;
1353 spin_unlock(ptl);
1354 return err;
1357 if (pmd_trans_unstable(pmdp))
1358 return 0;
1359 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1362 * We can assume that @vma always points to a valid one and @end never
1363 * goes beyond vma->vm_end.
1365 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1366 for (; addr < end; pte++, addr += PAGE_SIZE) {
1367 pagemap_entry_t pme;
1369 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1370 err = add_to_pagemap(addr, &pme, pm);
1371 if (err)
1372 break;
1374 pte_unmap_unlock(orig_pte, ptl);
1376 cond_resched();
1378 return err;
1381 #ifdef CONFIG_HUGETLB_PAGE
1382 /* This function walks within one hugetlb entry in the single call */
1383 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1384 unsigned long addr, unsigned long end,
1385 struct mm_walk *walk)
1387 struct pagemapread *pm = walk->private;
1388 struct vm_area_struct *vma = walk->vma;
1389 u64 flags = 0, frame = 0;
1390 int err = 0;
1391 pte_t pte;
1393 if (vma->vm_flags & VM_SOFTDIRTY)
1394 flags |= PM_SOFT_DIRTY;
1396 pte = huge_ptep_get(ptep);
1397 if (pte_present(pte)) {
1398 struct page *page = pte_page(pte);
1400 if (!PageAnon(page))
1401 flags |= PM_FILE;
1403 if (page_mapcount(page) == 1)
1404 flags |= PM_MMAP_EXCLUSIVE;
1406 flags |= PM_PRESENT;
1407 if (pm->show_pfn)
1408 frame = pte_pfn(pte) +
1409 ((addr & ~hmask) >> PAGE_SHIFT);
1412 for (; addr != end; addr += PAGE_SIZE) {
1413 pagemap_entry_t pme = make_pme(frame, flags);
1415 err = add_to_pagemap(addr, &pme, pm);
1416 if (err)
1417 return err;
1418 if (pm->show_pfn && (flags & PM_PRESENT))
1419 frame++;
1422 cond_resched();
1424 return err;
1426 #endif /* HUGETLB_PAGE */
1429 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1431 * For each page in the address space, this file contains one 64-bit entry
1432 * consisting of the following:
1434 * Bits 0-54 page frame number (PFN) if present
1435 * Bits 0-4 swap type if swapped
1436 * Bits 5-54 swap offset if swapped
1437 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1438 * Bit 56 page exclusively mapped
1439 * Bits 57-60 zero
1440 * Bit 61 page is file-page or shared-anon
1441 * Bit 62 page swapped
1442 * Bit 63 page present
1444 * If the page is not present but in swap, then the PFN contains an
1445 * encoding of the swap file number and the page's offset into the
1446 * swap. Unmapped pages return a null PFN. This allows determining
1447 * precisely which pages are mapped (or in swap) and comparing mapped
1448 * pages between processes.
1450 * Efficient users of this interface will use /proc/pid/maps to
1451 * determine which areas of memory are actually mapped and llseek to
1452 * skip over unmapped regions.
1454 static ssize_t pagemap_read(struct file *file, char __user *buf,
1455 size_t count, loff_t *ppos)
1457 struct mm_struct *mm = file->private_data;
1458 struct pagemapread pm;
1459 struct mm_walk pagemap_walk = {};
1460 unsigned long src;
1461 unsigned long svpfn;
1462 unsigned long start_vaddr;
1463 unsigned long end_vaddr;
1464 int ret = 0, copied = 0;
1466 if (!mm || !mmget_not_zero(mm))
1467 goto out;
1469 ret = -EINVAL;
1470 /* file position must be aligned */
1471 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1472 goto out_mm;
1474 ret = 0;
1475 if (!count)
1476 goto out_mm;
1478 /* do not disclose physical addresses: attack vector */
1479 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1481 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1482 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_KERNEL);
1483 ret = -ENOMEM;
1484 if (!pm.buffer)
1485 goto out_mm;
1487 pagemap_walk.pmd_entry = pagemap_pmd_range;
1488 pagemap_walk.pte_hole = pagemap_pte_hole;
1489 #ifdef CONFIG_HUGETLB_PAGE
1490 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1491 #endif
1492 pagemap_walk.mm = mm;
1493 pagemap_walk.private = &pm;
1495 src = *ppos;
1496 svpfn = src / PM_ENTRY_BYTES;
1497 start_vaddr = svpfn << PAGE_SHIFT;
1498 end_vaddr = mm->task_size;
1500 /* watch out for wraparound */
1501 if (svpfn > mm->task_size >> PAGE_SHIFT)
1502 start_vaddr = end_vaddr;
1505 * The odds are that this will stop walking way
1506 * before end_vaddr, because the length of the
1507 * user buffer is tracked in "pm", and the walk
1508 * will stop when we hit the end of the buffer.
1510 ret = 0;
1511 while (count && (start_vaddr < end_vaddr)) {
1512 int len;
1513 unsigned long end;
1515 pm.pos = 0;
1516 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1517 /* overflow ? */
1518 if (end < start_vaddr || end > end_vaddr)
1519 end = end_vaddr;
1520 down_read(&mm->mmap_sem);
1521 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1522 up_read(&mm->mmap_sem);
1523 start_vaddr = end;
1525 len = min(count, PM_ENTRY_BYTES * pm.pos);
1526 if (copy_to_user(buf, pm.buffer, len)) {
1527 ret = -EFAULT;
1528 goto out_free;
1530 copied += len;
1531 buf += len;
1532 count -= len;
1534 *ppos += copied;
1535 if (!ret || ret == PM_END_OF_BUFFER)
1536 ret = copied;
1538 out_free:
1539 kfree(pm.buffer);
1540 out_mm:
1541 mmput(mm);
1542 out:
1543 return ret;
1546 static int pagemap_open(struct inode *inode, struct file *file)
1548 struct mm_struct *mm;
1550 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1551 if (IS_ERR(mm))
1552 return PTR_ERR(mm);
1553 file->private_data = mm;
1554 return 0;
1557 static int pagemap_release(struct inode *inode, struct file *file)
1559 struct mm_struct *mm = file->private_data;
1561 if (mm)
1562 mmdrop(mm);
1563 return 0;
1566 const struct file_operations proc_pagemap_operations = {
1567 .llseek = mem_lseek, /* borrow this */
1568 .read = pagemap_read,
1569 .open = pagemap_open,
1570 .release = pagemap_release,
1572 #endif /* CONFIG_PROC_PAGE_MONITOR */
1574 #ifdef CONFIG_NUMA
1576 struct numa_maps {
1577 unsigned long pages;
1578 unsigned long anon;
1579 unsigned long active;
1580 unsigned long writeback;
1581 unsigned long mapcount_max;
1582 unsigned long dirty;
1583 unsigned long swapcache;
1584 unsigned long node[MAX_NUMNODES];
1587 struct numa_maps_private {
1588 struct proc_maps_private proc_maps;
1589 struct numa_maps md;
1592 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1593 unsigned long nr_pages)
1595 int count = page_mapcount(page);
1597 md->pages += nr_pages;
1598 if (pte_dirty || PageDirty(page))
1599 md->dirty += nr_pages;
1601 if (PageSwapCache(page))
1602 md->swapcache += nr_pages;
1604 if (PageActive(page) || PageUnevictable(page))
1605 md->active += nr_pages;
1607 if (PageWriteback(page))
1608 md->writeback += nr_pages;
1610 if (PageAnon(page))
1611 md->anon += nr_pages;
1613 if (count > md->mapcount_max)
1614 md->mapcount_max = count;
1616 md->node[page_to_nid(page)] += nr_pages;
1619 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1620 unsigned long addr)
1622 struct page *page;
1623 int nid;
1625 if (!pte_present(pte))
1626 return NULL;
1628 page = vm_normal_page(vma, addr, pte);
1629 if (!page)
1630 return NULL;
1632 if (PageReserved(page))
1633 return NULL;
1635 nid = page_to_nid(page);
1636 if (!node_isset(nid, node_states[N_MEMORY]))
1637 return NULL;
1639 return page;
1642 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1643 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1644 struct vm_area_struct *vma,
1645 unsigned long addr)
1647 struct page *page;
1648 int nid;
1650 if (!pmd_present(pmd))
1651 return NULL;
1653 page = vm_normal_page_pmd(vma, addr, pmd);
1654 if (!page)
1655 return NULL;
1657 if (PageReserved(page))
1658 return NULL;
1660 nid = page_to_nid(page);
1661 if (!node_isset(nid, node_states[N_MEMORY]))
1662 return NULL;
1664 return page;
1666 #endif
1668 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1669 unsigned long end, struct mm_walk *walk)
1671 struct numa_maps *md = walk->private;
1672 struct vm_area_struct *vma = walk->vma;
1673 spinlock_t *ptl;
1674 pte_t *orig_pte;
1675 pte_t *pte;
1677 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1678 ptl = pmd_trans_huge_lock(pmd, vma);
1679 if (ptl) {
1680 struct page *page;
1682 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1683 if (page)
1684 gather_stats(page, md, pmd_dirty(*pmd),
1685 HPAGE_PMD_SIZE/PAGE_SIZE);
1686 spin_unlock(ptl);
1687 return 0;
1690 if (pmd_trans_unstable(pmd))
1691 return 0;
1692 #endif
1693 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1694 do {
1695 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1696 if (!page)
1697 continue;
1698 gather_stats(page, md, pte_dirty(*pte), 1);
1700 } while (pte++, addr += PAGE_SIZE, addr != end);
1701 pte_unmap_unlock(orig_pte, ptl);
1702 cond_resched();
1703 return 0;
1705 #ifdef CONFIG_HUGETLB_PAGE
1706 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1707 unsigned long addr, unsigned long end, struct mm_walk *walk)
1709 pte_t huge_pte = huge_ptep_get(pte);
1710 struct numa_maps *md;
1711 struct page *page;
1713 if (!pte_present(huge_pte))
1714 return 0;
1716 page = pte_page(huge_pte);
1717 if (!page)
1718 return 0;
1720 md = walk->private;
1721 gather_stats(page, md, pte_dirty(huge_pte), 1);
1722 return 0;
1725 #else
1726 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1727 unsigned long addr, unsigned long end, struct mm_walk *walk)
1729 return 0;
1731 #endif
1734 * Display pages allocated per node and memory policy via /proc.
1736 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1738 struct numa_maps_private *numa_priv = m->private;
1739 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1740 struct vm_area_struct *vma = v;
1741 struct numa_maps *md = &numa_priv->md;
1742 struct file *file = vma->vm_file;
1743 struct mm_struct *mm = vma->vm_mm;
1744 struct mm_walk walk = {
1745 .hugetlb_entry = gather_hugetlb_stats,
1746 .pmd_entry = gather_pte_stats,
1747 .private = md,
1748 .mm = mm,
1750 struct mempolicy *pol;
1751 char buffer[64];
1752 int nid;
1754 if (!mm)
1755 return 0;
1757 /* Ensure we start with an empty set of numa_maps statistics. */
1758 memset(md, 0, sizeof(*md));
1760 pol = __get_vma_policy(vma, vma->vm_start);
1761 if (pol) {
1762 mpol_to_str(buffer, sizeof(buffer), pol);
1763 mpol_cond_put(pol);
1764 } else {
1765 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1768 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1770 if (file) {
1771 seq_puts(m, " file=");
1772 seq_file_path(m, file, "\n\t= ");
1773 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1774 seq_puts(m, " heap");
1775 } else if (is_stack(vma)) {
1776 seq_puts(m, " stack");
1779 if (is_vm_hugetlb_page(vma))
1780 seq_puts(m, " huge");
1782 /* mmap_sem is held by m_start */
1783 walk_page_vma(vma, &walk);
1785 if (!md->pages)
1786 goto out;
1788 if (md->anon)
1789 seq_printf(m, " anon=%lu", md->anon);
1791 if (md->dirty)
1792 seq_printf(m, " dirty=%lu", md->dirty);
1794 if (md->pages != md->anon && md->pages != md->dirty)
1795 seq_printf(m, " mapped=%lu", md->pages);
1797 if (md->mapcount_max > 1)
1798 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1800 if (md->swapcache)
1801 seq_printf(m, " swapcache=%lu", md->swapcache);
1803 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1804 seq_printf(m, " active=%lu", md->active);
1806 if (md->writeback)
1807 seq_printf(m, " writeback=%lu", md->writeback);
1809 for_each_node_state(nid, N_MEMORY)
1810 if (md->node[nid])
1811 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1813 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1814 out:
1815 seq_putc(m, '\n');
1816 m_cache_vma(m, vma);
1817 return 0;
1820 static int show_pid_numa_map(struct seq_file *m, void *v)
1822 return show_numa_map(m, v, 1);
1825 static int show_tid_numa_map(struct seq_file *m, void *v)
1827 return show_numa_map(m, v, 0);
1830 static const struct seq_operations proc_pid_numa_maps_op = {
1831 .start = m_start,
1832 .next = m_next,
1833 .stop = m_stop,
1834 .show = show_pid_numa_map,
1837 static const struct seq_operations proc_tid_numa_maps_op = {
1838 .start = m_start,
1839 .next = m_next,
1840 .stop = m_stop,
1841 .show = show_tid_numa_map,
1844 static int numa_maps_open(struct inode *inode, struct file *file,
1845 const struct seq_operations *ops)
1847 return proc_maps_open(inode, file, ops,
1848 sizeof(struct numa_maps_private));
1851 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1853 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1856 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1858 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1861 const struct file_operations proc_pid_numa_maps_operations = {
1862 .open = pid_numa_maps_open,
1863 .read = seq_read,
1864 .llseek = seq_lseek,
1865 .release = proc_map_release,
1868 const struct file_operations proc_tid_numa_maps_operations = {
1869 .open = tid_numa_maps_open,
1870 .read = seq_read,
1871 .llseek = seq_lseek,
1872 .release = proc_map_release,
1874 #endif /* CONFIG_NUMA */