mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / fs / proc / task_mmu.c
blob309d24118f9a0c8c455f5773c878234e0aea266f
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, 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
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 if (locked)
490 mss->pss_locked += (u64)size << PSS_SHIFT;
491 return;
494 for (i = 0; i < nr; i++, page++) {
495 int mapcount = page_mapcount(page);
496 unsigned long pss = (PAGE_SIZE << PSS_SHIFT);
498 if (mapcount >= 2) {
499 if (dirty || PageDirty(page))
500 mss->shared_dirty += PAGE_SIZE;
501 else
502 mss->shared_clean += PAGE_SIZE;
503 mss->pss += pss / mapcount;
504 if (locked)
505 mss->pss_locked += pss / mapcount;
506 } else {
507 if (dirty || PageDirty(page))
508 mss->private_dirty += PAGE_SIZE;
509 else
510 mss->private_clean += PAGE_SIZE;
511 mss->pss += pss;
512 if (locked)
513 mss->pss_locked += pss;
518 #ifdef CONFIG_SHMEM
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);
527 return 0;
529 #endif
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)) {
545 int mapcount;
547 mss->swap += PAGE_SIZE;
548 mapcount = swp_swapcount(swpent);
549 if (mapcount >= 2) {
550 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
552 do_div(pss_delta, mapcount);
553 mss->swap_pss += pss_delta;
554 } else {
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));
565 if (!page)
566 return;
568 if (radix_tree_exceptional_entry(page))
569 mss->swap += PAGE_SIZE;
570 else
571 put_page(page);
573 return;
576 if (!page)
577 return;
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);
589 struct page *page;
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))
594 return;
595 if (PageAnon(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))
600 /* pass */;
601 else
602 VM_BUG_ON_PAGE(1, page);
603 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
605 #else
606 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
607 struct mm_walk *walk)
610 #endif
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;
616 pte_t *pte;
617 spinlock_t *ptl;
619 ptl = pmd_trans_huge_lock(pmd, vma);
620 if (ptl) {
621 if (pmd_present(*pmd))
622 smaps_pmd_entry(pmd, addr, walk);
623 spin_unlock(ptl);
624 goto out;
627 if (pmd_trans_unstable(pmd))
628 goto out;
630 * The mmap_sem held all the way back in m_start() is what
631 * keeps khugepaged out of here and from collapsing things
632 * in here.
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);
638 out:
639 cond_resched();
640 return 0;
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",
667 #endif
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",
682 #endif
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)] = "",
695 #endif
697 size_t i;
699 seq_puts(m, "VmFlags: ");
700 for (i = 0; i < BITS_PER_LONG; i++) {
701 if (!mnemonics[i][0])
702 continue;
703 if (vma->vm_flags & (1UL << i)) {
704 seq_printf(m, "%c%c ",
705 mnemonics[i][0], mnemonics[i][1]);
708 seq_putc(m, '\n');
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);
730 if (page) {
731 int mapcount = page_mapcount(page);
733 if (mapcount >= 2)
734 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
735 else
736 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
738 return 0;
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,
756 #endif
757 .mm = vma->vm_mm,
759 int ret = 0;
760 bool rollup_mode;
761 bool last_vma;
763 if (priv->rollup) {
764 rollup_mode = true;
765 mss = priv->rollup;
766 if (mss->first) {
767 mss->first_vma_start = vma->vm_start;
768 mss->first = false;
770 last_vma = !m_next_vma(priv, vma);
771 } else {
772 rollup_mode = false;
773 memset(&mss_stack, 0, sizeof(mss_stack));
774 mss = &mss_stack;
777 smaps_walk.private = mss;
779 #ifdef CONFIG_SHMEM
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;
798 } else {
799 mss->check_shmem_swap = true;
800 smaps_walk.pte_hole = smaps_pte_hole;
803 #endif
804 /* mmap_sem is held in m_start */
805 walk_page_vma(vma, &smaps_walk);
807 if (!rollup_mode) {
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);
812 seq_pad(m, ' ');
813 seq_puts(m, "[rollup]\n");
814 } else {
815 ret = SEQ_SKIP;
818 if (!rollup_mode)
819 seq_printf(m,
820 "Size: %8lu kB\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)
829 seq_printf(m,
830 "Rss: %8lu kB\n"
831 "Pss: %8lu kB\n"
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"
843 "Swap: %8lu kB\n"
844 "SwapPss: %8lu kB\n"
845 "Locked: %8lu kB\n",
846 mss->resident >> 10,
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,
854 mss->lazyfree >> 10,
855 mss->anonymous_thp >> 10,
856 mss->shmem_thp >> 10,
857 mss->shared_hugetlb >> 10,
858 mss->private_hugetlb >> 10,
859 mss->swap >> 10,
860 (unsigned long)(mss->swap_pss >> (10 + PSS_SHIFT)),
861 (unsigned long)(mss->pss_locked >> (10 + PSS_SHIFT)));
863 if (!rollup_mode) {
864 arch_show_smap(m, vma);
865 show_smap_vma_flags(m, vma);
867 m_cache_vma(m, vma);
868 return ret;
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 = {
882 .start = m_start,
883 .next = m_next,
884 .stop = m_stop,
885 .show = show_pid_smap
888 static const struct seq_operations proc_tid_smaps_op = {
889 .start = m_start,
890 .next = m_next,
891 .stop = m_stop,
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);
906 if (ret < 0)
907 return ret;
908 seq = file->private_data;
909 priv = seq->private;
910 priv->rollup = kzalloc(sizeof(*priv->rollup), GFP_KERNEL);
911 if (!priv->rollup) {
912 proc_map_release(inode, file);
913 return -ENOMEM;
915 priv->rollup->first = true;
916 return 0;
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,
926 .read = seq_read,
927 .llseek = seq_lseek,
928 .release = proc_map_release,
931 const struct file_operations proc_pid_smaps_rollup_operations = {
932 .open = pid_smaps_rollup_open,
933 .read = seq_read,
934 .llseek = seq_lseek,
935 .release = proc_map_release,
938 const struct file_operations proc_tid_smaps_operations = {
939 .open = tid_smaps_open,
940 .read = seq_read,
941 .llseek = seq_lseek,
942 .release = proc_map_release,
945 enum clear_refs_types {
946 CLEAR_REFS_ALL = 1,
947 CLEAR_REFS_ANON,
948 CLEAR_REFS_MAPPED,
949 CLEAR_REFS_SOFT_DIRTY,
950 CLEAR_REFS_MM_HIWATER_RSS,
951 CLEAR_REFS_LAST,
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.
968 pte_t ptent = *pte;
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);
980 #else
981 static inline void clear_soft_dirty(struct vm_area_struct *vma,
982 unsigned long addr, pte_t *pte)
985 #endif
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)
991 pmd_t pmd = *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);
1010 #else
1011 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1012 unsigned long addr, pmd_t *pmdp)
1015 #endif
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;
1022 pte_t *pte, ptent;
1023 spinlock_t *ptl;
1024 struct page *page;
1026 ptl = pmd_trans_huge_lock(pmd, vma);
1027 if (ptl) {
1028 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1029 clear_soft_dirty_pmd(vma, addr, pmd);
1030 goto out;
1033 if (!pmd_present(*pmd))
1034 goto out;
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);
1042 out:
1043 spin_unlock(ptl);
1044 return 0;
1047 if (pmd_trans_unstable(pmd))
1048 return 0;
1050 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1051 for (; addr != end; pte++, addr += PAGE_SIZE) {
1052 ptent = *pte;
1054 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1055 clear_soft_dirty(vma, addr, pte);
1056 continue;
1059 if (!pte_present(ptent))
1060 continue;
1062 page = vm_normal_page(vma, addr, ptent);
1063 if (!page)
1064 continue;
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);
1072 cond_resched();
1073 return 0;
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)
1083 return 1;
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)
1092 return 1;
1093 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1094 return 1;
1095 return 0;
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;
1107 int itype;
1108 int rv;
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))
1114 return -EFAULT;
1115 rv = kstrtoint(strstrip(buffer), 10, &itype);
1116 if (rv < 0)
1117 return rv;
1118 type = (enum clear_refs_types)itype;
1119 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1120 return -EINVAL;
1122 task = get_proc_task(file_inode(file));
1123 if (!task)
1124 return -ESRCH;
1125 mm = get_task_mm(task);
1126 if (mm) {
1127 struct clear_refs_private cp = {
1128 .type = type,
1130 struct mm_walk clear_refs_walk = {
1131 .pmd_entry = clear_refs_pte_range,
1132 .test_walk = clear_refs_test_walk,
1133 .mm = mm,
1134 .private = &cp,
1137 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1138 if (down_write_killable(&mm->mmap_sem)) {
1139 count = -EINTR;
1140 goto out_mm;
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);
1149 goto out_mm;
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))
1157 continue;
1158 up_read(&mm->mmap_sem);
1159 if (down_write_killable(&mm->mmap_sem)) {
1160 count = -EINTR;
1161 goto out_mm;
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()
1175 * failed like if
1176 * get_proc_task() fails?
1178 up_write(&mm->mmap_sem);
1179 goto out_mm;
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);
1186 break;
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);
1195 out_mm:
1196 mmput(mm);
1198 put_task_struct(task);
1200 return count;
1203 const struct file_operations proc_clear_refs_operations = {
1204 .write = clear_refs_write,
1205 .llseek = noop_llseek,
1208 typedef struct {
1209 u64 pme;
1210 } pagemap_entry_t;
1212 struct pagemapread {
1213 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
1214 pagemap_entry_t *buffer;
1215 bool show_pfn;
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;
1243 return 0;
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;
1251 int err = 0;
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;
1259 if (vma)
1260 hole_end = min(end, vma->vm_start);
1261 else
1262 hole_end = end;
1264 for (; addr < hole_end; addr += PAGE_SIZE) {
1265 err = add_to_pagemap(addr, &pme, pm);
1266 if (err)
1267 goto out;
1270 if (!vma)
1271 break;
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);
1278 if (err)
1279 goto out;
1282 out:
1283 return err;
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)) {
1293 if (pm->show_pfn)
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)) {
1300 swp_entry_t entry;
1301 if (pte_swp_soft_dirty(pte))
1302 flags |= PM_SOFT_DIRTY;
1303 entry = pte_to_swp_entry(pte);
1304 if (pm->show_pfn)
1305 frame = swp_type(entry) |
1306 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1307 flags |= PM_SWAP;
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))
1316 flags |= PM_FILE;
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;
1330 spinlock_t *ptl;
1331 pte_t *pte, *orig_pte;
1332 int err = 0;
1334 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1335 ptl = pmd_trans_huge_lock(pmdp, vma);
1336 if (ptl) {
1337 u64 flags = 0, frame = 0;
1338 pmd_t pmd = *pmdp;
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;
1350 if (pm->show_pfn)
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;
1359 if (pm->show_pfn) {
1360 offset = swp_offset(entry) +
1361 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1362 frame = swp_type(entry) |
1363 (offset << MAX_SWAPFILES_SHIFT);
1365 flags |= PM_SWAP;
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);
1371 #endif
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);
1380 if (err)
1381 break;
1382 if (pm->show_pfn) {
1383 if (flags & PM_PRESENT)
1384 frame++;
1385 else if (flags & PM_SWAP)
1386 frame += (1 << MAX_SWAPFILES_SHIFT);
1389 spin_unlock(ptl);
1390 return err;
1393 if (pmd_trans_unstable(pmdp))
1394 return 0;
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);
1407 if (err)
1408 break;
1410 pte_unmap_unlock(orig_pte, ptl);
1412 cond_resched();
1414 return err;
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;
1426 int err = 0;
1427 pte_t pte;
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))
1437 flags |= PM_FILE;
1439 if (page_mapcount(page) == 1)
1440 flags |= PM_MMAP_EXCLUSIVE;
1442 flags |= PM_PRESENT;
1443 if (pm->show_pfn)
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);
1452 if (err)
1453 return err;
1454 if (pm->show_pfn && (flags & PM_PRESENT))
1455 frame++;
1458 cond_resched();
1460 return err;
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
1475 * Bits 57-60 zero
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 = {};
1496 unsigned long src;
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))
1503 goto out;
1505 ret = -EINVAL;
1506 /* file position must be aligned */
1507 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1508 goto out_mm;
1510 ret = 0;
1511 if (!count)
1512 goto out_mm;
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);
1519 ret = -ENOMEM;
1520 if (!pm.buffer)
1521 goto out_mm;
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;
1527 #endif
1528 pagemap_walk.mm = mm;
1529 pagemap_walk.private = &pm;
1531 src = *ppos;
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.
1546 ret = 0;
1547 while (count && (start_vaddr < end_vaddr)) {
1548 int len;
1549 unsigned long end;
1551 pm.pos = 0;
1552 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1553 /* overflow ? */
1554 if (end < start_vaddr || end > end_vaddr)
1555 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);
1559 start_vaddr = end;
1561 len = min(count, PM_ENTRY_BYTES * pm.pos);
1562 if (copy_to_user(buf, pm.buffer, len)) {
1563 ret = -EFAULT;
1564 goto out_free;
1566 copied += len;
1567 buf += len;
1568 count -= len;
1570 *ppos += copied;
1571 if (!ret || ret == PM_END_OF_BUFFER)
1572 ret = copied;
1574 out_free:
1575 kfree(pm.buffer);
1576 out_mm:
1577 mmput(mm);
1578 out:
1579 return ret;
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);
1587 if (IS_ERR(mm))
1588 return PTR_ERR(mm);
1589 file->private_data = mm;
1590 return 0;
1593 static int pagemap_release(struct inode *inode, struct file *file)
1595 struct mm_struct *mm = file->private_data;
1597 if (mm)
1598 mmdrop(mm);
1599 return 0;
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 */
1610 #ifdef CONFIG_NUMA
1612 struct numa_maps {
1613 unsigned long pages;
1614 unsigned long anon;
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;
1646 if (PageAnon(page))
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,
1656 unsigned long addr)
1658 struct page *page;
1659 int nid;
1661 if (!pte_present(pte))
1662 return NULL;
1664 page = vm_normal_page(vma, addr, pte);
1665 if (!page)
1666 return NULL;
1668 if (PageReserved(page))
1669 return NULL;
1671 nid = page_to_nid(page);
1672 if (!node_isset(nid, node_states[N_MEMORY]))
1673 return NULL;
1675 return page;
1678 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1679 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1680 struct vm_area_struct *vma,
1681 unsigned long addr)
1683 struct page *page;
1684 int nid;
1686 if (!pmd_present(pmd))
1687 return NULL;
1689 page = vm_normal_page_pmd(vma, addr, pmd);
1690 if (!page)
1691 return NULL;
1693 if (PageReserved(page))
1694 return NULL;
1696 nid = page_to_nid(page);
1697 if (!node_isset(nid, node_states[N_MEMORY]))
1698 return NULL;
1700 return page;
1702 #endif
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;
1709 spinlock_t *ptl;
1710 pte_t *orig_pte;
1711 pte_t *pte;
1713 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1714 ptl = pmd_trans_huge_lock(pmd, vma);
1715 if (ptl) {
1716 struct page *page;
1718 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1719 if (page)
1720 gather_stats(page, md, pmd_dirty(*pmd),
1721 HPAGE_PMD_SIZE/PAGE_SIZE);
1722 spin_unlock(ptl);
1723 return 0;
1726 if (pmd_trans_unstable(pmd))
1727 return 0;
1728 #endif
1729 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1730 do {
1731 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1732 if (!page)
1733 continue;
1734 gather_stats(page, md, pte_dirty(*pte), 1);
1736 } while (pte++, addr += PAGE_SIZE, addr != end);
1737 pte_unmap_unlock(orig_pte, ptl);
1738 cond_resched();
1739 return 0;
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;
1747 struct page *page;
1749 if (!pte_present(huge_pte))
1750 return 0;
1752 page = pte_page(huge_pte);
1753 if (!page)
1754 return 0;
1756 md = walk->private;
1757 gather_stats(page, md, pte_dirty(huge_pte), 1);
1758 return 0;
1761 #else
1762 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1763 unsigned long addr, unsigned long end, struct mm_walk *walk)
1765 return 0;
1767 #endif
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,
1783 .private = md,
1784 .mm = mm,
1786 struct mempolicy *pol;
1787 char buffer[64];
1788 int nid;
1790 if (!mm)
1791 return 0;
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);
1797 if (pol) {
1798 mpol_to_str(buffer, sizeof(buffer), pol);
1799 mpol_cond_put(pol);
1800 } else {
1801 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1804 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1806 if (file) {
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);
1821 if (!md->pages)
1822 goto out;
1824 if (md->anon)
1825 seq_printf(m, " anon=%lu", md->anon);
1827 if (md->dirty)
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);
1836 if (md->swapcache)
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);
1842 if (md->writeback)
1843 seq_printf(m, " writeback=%lu", md->writeback);
1845 for_each_node_state(nid, N_MEMORY)
1846 if (md->node[nid])
1847 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1849 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1850 out:
1851 seq_putc(m, '\n');
1852 m_cache_vma(m, vma);
1853 return 0;
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 = {
1867 .start = m_start,
1868 .next = m_next,
1869 .stop = m_stop,
1870 .show = show_pid_numa_map,
1873 static const struct seq_operations proc_tid_numa_maps_op = {
1874 .start = m_start,
1875 .next = m_next,
1876 .stop = m_stop,
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,
1899 .read = seq_read,
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,
1906 .read = seq_read,
1907 .llseek = seq_lseek,
1908 .release = proc_map_release,
1910 #endif /* CONFIG_NUMA */