dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / fs / proc / task_mmu.c
blob75691a20313c9cda1a46c9c3c711593406597770
1 #include <linux/mm.h>
2 #include <linux/vmacache.h>
3 #include <linux/hugetlb.h>
4 #include <linux/huge_mm.h>
5 #include <linux/mount.h>
6 #include <linux/seq_file.h>
7 #include <linux/highmem.h>
8 #include <linux/ptrace.h>
9 #include <linux/slab.h>
10 #include <linux/pagemap.h>
11 #include <linux/mempolicy.h>
12 #include <linux/rmap.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/mmu_notifier.h>
16 #include <linux/page_idle.h>
18 #include <asm/elf.h>
19 #include <asm/uaccess.h>
20 #include <asm/tlbflush.h>
21 #include "internal.h"
23 void task_mem(struct seq_file *m, struct mm_struct *mm)
25 unsigned long data, text, lib, swap, ptes, pmds;
26 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
29 * Note: to minimize their overhead, mm maintains hiwater_vm and
30 * hiwater_rss only when about to *lower* total_vm or rss. Any
31 * collector of these hiwater stats must therefore get total_vm
32 * and rss too, which will usually be the higher. Barriers? not
33 * worth the effort, such snapshots can always be inconsistent.
35 hiwater_vm = total_vm = mm->total_vm;
36 if (hiwater_vm < mm->hiwater_vm)
37 hiwater_vm = mm->hiwater_vm;
38 hiwater_rss = total_rss = get_mm_rss(mm);
39 if (hiwater_rss < mm->hiwater_rss)
40 hiwater_rss = mm->hiwater_rss;
42 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
43 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
44 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
45 swap = get_mm_counter(mm, MM_SWAPENTS);
46 ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
47 pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
48 seq_printf(m,
49 "VmPeak:\t%8lu kB\n"
50 "VmSize:\t%8lu kB\n"
51 "VmLck:\t%8lu kB\n"
52 "VmPin:\t%8lu kB\n"
53 "VmHWM:\t%8lu kB\n"
54 "VmRSS:\t%8lu kB\n"
55 "VmData:\t%8lu kB\n"
56 "VmStk:\t%8lu kB\n"
57 "VmExe:\t%8lu kB\n"
58 "VmLib:\t%8lu kB\n"
59 "VmPTE:\t%8lu kB\n"
60 "VmPMD:\t%8lu kB\n"
61 "VmSwap:\t%8lu kB\n",
62 hiwater_vm << (PAGE_SHIFT-10),
63 total_vm << (PAGE_SHIFT-10),
64 mm->locked_vm << (PAGE_SHIFT-10),
65 mm->pinned_vm << (PAGE_SHIFT-10),
66 hiwater_rss << (PAGE_SHIFT-10),
67 total_rss << (PAGE_SHIFT-10),
68 data << (PAGE_SHIFT-10),
69 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
70 ptes >> 10,
71 pmds >> 10,
72 swap << (PAGE_SHIFT-10));
73 hugetlb_report_usage(m, mm);
76 unsigned long task_vsize(struct mm_struct *mm)
78 return PAGE_SIZE * mm->total_vm;
81 unsigned long task_statm(struct mm_struct *mm,
82 unsigned long *shared, unsigned long *text,
83 unsigned long *data, unsigned long *resident)
85 *shared = get_mm_counter(mm, MM_FILEPAGES);
86 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
87 >> PAGE_SHIFT;
88 *data = mm->total_vm - mm->shared_vm;
89 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
90 return mm->total_vm;
93 #ifdef CONFIG_NUMA
95 * Save get_task_policy() for show_numa_map().
97 static void hold_task_mempolicy(struct proc_maps_private *priv)
99 struct task_struct *task = priv->task;
101 task_lock(task);
102 priv->task_mempolicy = get_task_policy(task);
103 mpol_get(priv->task_mempolicy);
104 task_unlock(task);
106 static void release_task_mempolicy(struct proc_maps_private *priv)
108 mpol_put(priv->task_mempolicy);
110 #else
111 static void hold_task_mempolicy(struct proc_maps_private *priv)
114 static void release_task_mempolicy(struct proc_maps_private *priv)
117 #endif
119 static void vma_stop(struct proc_maps_private *priv)
121 struct mm_struct *mm = priv->mm;
123 release_task_mempolicy(priv);
124 up_read(&mm->mmap_sem);
125 mmput(mm);
128 static struct vm_area_struct *
129 m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
131 if (vma == priv->tail_vma)
132 return NULL;
133 return vma->vm_next ?: priv->tail_vma;
136 static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
138 if (m->count < m->size) /* vma is copied successfully */
139 m->version = m_next_vma(m->private, vma) ? vma->vm_start : -1UL;
142 static void *m_start(struct seq_file *m, loff_t *ppos)
144 struct proc_maps_private *priv = m->private;
145 unsigned long last_addr = m->version;
146 struct mm_struct *mm;
147 struct vm_area_struct *vma;
148 unsigned int pos = *ppos;
150 /* See m_cache_vma(). Zero at the start or after lseek. */
151 if (last_addr == -1UL)
152 return NULL;
154 priv->task = get_proc_task(priv->inode);
155 if (!priv->task)
156 return ERR_PTR(-ESRCH);
158 mm = priv->mm;
159 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
160 return NULL;
162 down_read(&mm->mmap_sem);
163 hold_task_mempolicy(priv);
164 priv->tail_vma = get_gate_vma(mm);
166 if (last_addr) {
167 vma = find_vma(mm, last_addr);
168 if (vma && (vma = m_next_vma(priv, vma)))
169 return vma;
172 m->version = 0;
173 if (pos < mm->map_count) {
174 for (vma = mm->mmap; pos; pos--) {
175 m->version = vma->vm_start;
176 vma = vma->vm_next;
178 return vma;
181 /* we do not bother to update m->version in this case */
182 if (pos == mm->map_count && priv->tail_vma)
183 return priv->tail_vma;
185 vma_stop(priv);
186 return NULL;
189 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
191 struct proc_maps_private *priv = m->private;
192 struct vm_area_struct *next;
194 (*pos)++;
195 next = m_next_vma(priv, v);
196 if (!next)
197 vma_stop(priv);
198 return next;
201 static void m_stop(struct seq_file *m, void *v)
203 struct proc_maps_private *priv = m->private;
205 if (!IS_ERR_OR_NULL(v))
206 vma_stop(priv);
207 if (priv->task) {
208 put_task_struct(priv->task);
209 priv->task = NULL;
213 static int proc_maps_open(struct inode *inode, struct file *file,
214 const struct seq_operations *ops, int psize)
216 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
218 if (!priv)
219 return -ENOMEM;
221 priv->inode = inode;
222 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
223 if (IS_ERR(priv->mm)) {
224 int err = PTR_ERR(priv->mm);
226 seq_release_private(inode, file);
227 return err;
230 return 0;
233 static int proc_map_release(struct inode *inode, struct file *file)
235 struct seq_file *seq = file->private_data;
236 struct proc_maps_private *priv = seq->private;
238 if (priv->mm)
239 mmdrop(priv->mm);
241 return seq_release_private(inode, file);
244 static int do_maps_open(struct inode *inode, struct file *file,
245 const struct seq_operations *ops)
247 return proc_maps_open(inode, file, ops,
248 sizeof(struct proc_maps_private));
252 * Indicate if the VMA is a stack for the given task; for
253 * /proc/PID/maps that is the stack of the main task.
255 static int is_stack(struct proc_maps_private *priv,
256 struct vm_area_struct *vma)
259 * We make no effort to guess what a given thread considers to be
260 * its "stack". It's not even well-defined for programs written
261 * languages like Go.
263 return vma->vm_start <= vma->vm_mm->start_stack &&
264 vma->vm_end >= vma->vm_mm->start_stack;
267 static void
268 show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
270 struct mm_struct *mm = vma->vm_mm;
271 struct file *file = vma->vm_file;
272 struct proc_maps_private *priv = m->private;
273 vm_flags_t flags = vma->vm_flags;
274 unsigned long ino = 0;
275 unsigned long long pgoff = 0;
276 unsigned long start, end;
277 dev_t dev = 0;
278 const char *name = NULL;
280 if (file) {
281 struct inode *inode = file_inode(vma->vm_file);
282 dev = inode->i_sb->s_dev;
283 ino = inode->i_ino;
284 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
287 /* We don't show the stack guard page in /proc/maps */
288 start = vma->vm_start;
289 end = vma->vm_end;
291 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
292 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
293 start,
294 end,
295 flags & VM_READ ? 'r' : '-',
296 flags & VM_WRITE ? 'w' : '-',
297 flags & VM_EXEC ? 'x' : '-',
298 flags & VM_MAYSHARE ? 's' : 'p',
299 pgoff,
300 MAJOR(dev), MINOR(dev), ino);
303 * Print the dentry name for named mappings, and a
304 * special [heap] marker for the heap:
306 if (file) {
307 seq_pad(m, ' ');
308 seq_file_path(m, file, "\n");
309 goto done;
312 if (vma->vm_ops && vma->vm_ops->name) {
313 name = vma->vm_ops->name(vma);
314 if (name)
315 goto done;
318 name = arch_vma_name(vma);
319 if (!name) {
320 if (!mm) {
321 name = "[vdso]";
322 goto done;
325 if (vma->vm_start <= mm->brk &&
326 vma->vm_end >= mm->start_brk) {
327 name = "[heap]";
328 goto done;
331 if (is_stack(priv, vma))
332 name = "[stack]";
335 done:
336 if (name) {
337 seq_pad(m, ' ');
338 seq_puts(m, name);
340 seq_putc(m, '\n');
343 static int show_map(struct seq_file *m, void *v, int is_pid)
345 show_map_vma(m, v, is_pid);
346 m_cache_vma(m, v);
347 return 0;
350 static int show_pid_map(struct seq_file *m, void *v)
352 return show_map(m, v, 1);
355 static int show_tid_map(struct seq_file *m, void *v)
357 return show_map(m, v, 0);
360 static const struct seq_operations proc_pid_maps_op = {
361 .start = m_start,
362 .next = m_next,
363 .stop = m_stop,
364 .show = show_pid_map
367 static const struct seq_operations proc_tid_maps_op = {
368 .start = m_start,
369 .next = m_next,
370 .stop = m_stop,
371 .show = show_tid_map
374 static int pid_maps_open(struct inode *inode, struct file *file)
376 return do_maps_open(inode, file, &proc_pid_maps_op);
379 static int tid_maps_open(struct inode *inode, struct file *file)
381 return do_maps_open(inode, file, &proc_tid_maps_op);
384 const struct file_operations proc_pid_maps_operations = {
385 .open = pid_maps_open,
386 .read = seq_read,
387 .llseek = seq_lseek,
388 .release = proc_map_release,
391 const struct file_operations proc_tid_maps_operations = {
392 .open = tid_maps_open,
393 .read = seq_read,
394 .llseek = seq_lseek,
395 .release = proc_map_release,
399 * Proportional Set Size(PSS): my share of RSS.
401 * PSS of a process is the count of pages it has in memory, where each
402 * page is divided by the number of processes sharing it. So if a
403 * process has 1000 pages all to itself, and 1000 shared with one other
404 * process, its PSS will be 1500.
406 * To keep (accumulated) division errors low, we adopt a 64bit
407 * fixed-point pss counter to minimize division errors. So (pss >>
408 * PSS_SHIFT) would be the real byte count.
410 * A shift of 12 before division means (assuming 4K page size):
411 * - 1M 3-user-pages add up to 8KB errors;
412 * - supports mapcount up to 2^24, or 16M;
413 * - supports PSS up to 2^52 bytes, or 4PB.
415 #define PSS_SHIFT 12
417 #ifdef CONFIG_PROC_PAGE_MONITOR
418 struct mem_size_stats {
419 unsigned long resident;
420 unsigned long shared_clean;
421 unsigned long shared_dirty;
422 unsigned long private_clean;
423 unsigned long private_dirty;
424 unsigned long referenced;
425 unsigned long anonymous;
426 unsigned long anonymous_thp;
427 unsigned long swap;
428 unsigned long shared_hugetlb;
429 unsigned long private_hugetlb;
430 u64 pss;
431 u64 swap_pss;
434 static void smaps_account(struct mem_size_stats *mss, struct page *page,
435 unsigned long size, bool young, bool dirty)
437 int mapcount;
439 if (PageAnon(page))
440 mss->anonymous += size;
442 mss->resident += size;
443 /* Accumulate the size in pages that have been accessed. */
444 if (young || page_is_young(page) || PageReferenced(page))
445 mss->referenced += size;
446 mapcount = page_mapcount(page);
447 if (mapcount >= 2) {
448 u64 pss_delta;
450 if (dirty || PageDirty(page))
451 mss->shared_dirty += size;
452 else
453 mss->shared_clean += size;
454 pss_delta = (u64)size << PSS_SHIFT;
455 do_div(pss_delta, mapcount);
456 mss->pss += pss_delta;
457 } else {
458 if (dirty || PageDirty(page))
459 mss->private_dirty += size;
460 else
461 mss->private_clean += size;
462 mss->pss += (u64)size << PSS_SHIFT;
466 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
467 struct mm_walk *walk)
469 struct mem_size_stats *mss = walk->private;
470 struct vm_area_struct *vma = walk->vma;
471 struct page *page = NULL;
473 if (pte_present(*pte)) {
474 page = vm_normal_page(vma, addr, *pte);
475 } else if (is_swap_pte(*pte)) {
476 swp_entry_t swpent = pte_to_swp_entry(*pte);
478 if (!non_swap_entry(swpent)) {
479 int mapcount;
481 mss->swap += PAGE_SIZE;
482 mapcount = swp_swapcount(swpent);
483 if (mapcount >= 2) {
484 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
486 do_div(pss_delta, mapcount);
487 mss->swap_pss += pss_delta;
488 } else {
489 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
491 } else if (is_migration_entry(swpent))
492 page = migration_entry_to_page(swpent);
495 if (!page)
496 return;
497 smaps_account(mss, page, PAGE_SIZE, pte_young(*pte), pte_dirty(*pte));
500 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
501 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
502 struct mm_walk *walk)
504 struct mem_size_stats *mss = walk->private;
505 struct vm_area_struct *vma = walk->vma;
506 struct page *page;
508 /* FOLL_DUMP will return -EFAULT on huge zero page */
509 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
510 if (IS_ERR_OR_NULL(page))
511 return;
512 mss->anonymous_thp += HPAGE_PMD_SIZE;
513 smaps_account(mss, page, HPAGE_PMD_SIZE,
514 pmd_young(*pmd), pmd_dirty(*pmd));
516 #else
517 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
518 struct mm_walk *walk)
521 #endif
523 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
524 struct mm_walk *walk)
526 struct vm_area_struct *vma = walk->vma;
527 pte_t *pte;
528 spinlock_t *ptl;
530 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
531 smaps_pmd_entry(pmd, addr, walk);
532 spin_unlock(ptl);
533 return 0;
536 if (pmd_trans_unstable(pmd))
537 return 0;
539 * The mmap_sem held all the way back in m_start() is what
540 * keeps khugepaged out of here and from collapsing things
541 * in here.
543 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
544 for (; addr != end; pte++, addr += PAGE_SIZE)
545 smaps_pte_entry(pte, addr, walk);
546 pte_unmap_unlock(pte - 1, ptl);
547 cond_resched();
548 return 0;
551 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
554 * Don't forget to update Documentation/ on changes.
556 static const char mnemonics[BITS_PER_LONG][2] = {
558 * In case if we meet a flag we don't know about.
560 [0 ... (BITS_PER_LONG-1)] = "??",
562 [ilog2(VM_READ)] = "rd",
563 [ilog2(VM_WRITE)] = "wr",
564 [ilog2(VM_EXEC)] = "ex",
565 [ilog2(VM_SHARED)] = "sh",
566 [ilog2(VM_MAYREAD)] = "mr",
567 [ilog2(VM_MAYWRITE)] = "mw",
568 [ilog2(VM_MAYEXEC)] = "me",
569 [ilog2(VM_MAYSHARE)] = "ms",
570 [ilog2(VM_GROWSDOWN)] = "gd",
571 [ilog2(VM_PFNMAP)] = "pf",
572 [ilog2(VM_DENYWRITE)] = "dw",
573 #ifdef CONFIG_X86_INTEL_MPX
574 [ilog2(VM_MPX)] = "mp",
575 #endif
576 [ilog2(VM_LOCKED)] = "lo",
577 [ilog2(VM_IO)] = "io",
578 [ilog2(VM_SEQ_READ)] = "sr",
579 [ilog2(VM_RAND_READ)] = "rr",
580 [ilog2(VM_DONTCOPY)] = "dc",
581 [ilog2(VM_DONTEXPAND)] = "de",
582 [ilog2(VM_ACCOUNT)] = "ac",
583 [ilog2(VM_NORESERVE)] = "nr",
584 [ilog2(VM_HUGETLB)] = "ht",
585 [ilog2(VM_ARCH_1)] = "ar",
586 [ilog2(VM_DONTDUMP)] = "dd",
587 #ifdef CONFIG_MEM_SOFT_DIRTY
588 [ilog2(VM_SOFTDIRTY)] = "sd",
589 #endif
590 [ilog2(VM_MIXEDMAP)] = "mm",
591 [ilog2(VM_HUGEPAGE)] = "hg",
592 [ilog2(VM_NOHUGEPAGE)] = "nh",
593 [ilog2(VM_MERGEABLE)] = "mg",
594 [ilog2(VM_UFFD_MISSING)]= "um",
595 [ilog2(VM_UFFD_WP)] = "uw",
597 size_t i;
599 seq_puts(m, "VmFlags: ");
600 for (i = 0; i < BITS_PER_LONG; i++) {
601 if (vma->vm_flags & (1UL << i)) {
602 seq_printf(m, "%c%c ",
603 mnemonics[i][0], mnemonics[i][1]);
606 seq_putc(m, '\n');
609 #ifdef CONFIG_HUGETLB_PAGE
610 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
611 unsigned long addr, unsigned long end,
612 struct mm_walk *walk)
614 struct mem_size_stats *mss = walk->private;
615 struct vm_area_struct *vma = walk->vma;
616 struct page *page = NULL;
618 if (pte_present(*pte)) {
619 page = vm_normal_page(vma, addr, *pte);
620 } else if (is_swap_pte(*pte)) {
621 swp_entry_t swpent = pte_to_swp_entry(*pte);
623 if (is_migration_entry(swpent))
624 page = migration_entry_to_page(swpent);
626 if (page) {
627 int mapcount = page_mapcount(page);
629 if (mapcount >= 2)
630 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
631 else
632 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
634 return 0;
636 #endif /* HUGETLB_PAGE */
638 static int show_smap(struct seq_file *m, void *v, int is_pid)
640 struct vm_area_struct *vma = v;
641 struct mem_size_stats mss;
642 struct mm_walk smaps_walk = {
643 .pmd_entry = smaps_pte_range,
644 #ifdef CONFIG_HUGETLB_PAGE
645 .hugetlb_entry = smaps_hugetlb_range,
646 #endif
647 .mm = vma->vm_mm,
648 .private = &mss,
651 memset(&mss, 0, sizeof mss);
652 /* mmap_sem is held in m_start */
653 walk_page_vma(vma, &smaps_walk);
655 show_map_vma(m, vma, is_pid);
657 seq_printf(m,
658 "Size: %8lu kB\n"
659 "Rss: %8lu kB\n"
660 "Pss: %8lu kB\n"
661 "Shared_Clean: %8lu kB\n"
662 "Shared_Dirty: %8lu kB\n"
663 "Private_Clean: %8lu kB\n"
664 "Private_Dirty: %8lu kB\n"
665 "Referenced: %8lu kB\n"
666 "Anonymous: %8lu kB\n"
667 "AnonHugePages: %8lu kB\n"
668 "Shared_Hugetlb: %8lu kB\n"
669 "Private_Hugetlb: %7lu kB\n"
670 "Swap: %8lu kB\n"
671 "SwapPss: %8lu kB\n"
672 "KernelPageSize: %8lu kB\n"
673 "MMUPageSize: %8lu kB\n"
674 "Locked: %8lu kB\n",
675 (vma->vm_end - vma->vm_start) >> 10,
676 mss.resident >> 10,
677 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
678 mss.shared_clean >> 10,
679 mss.shared_dirty >> 10,
680 mss.private_clean >> 10,
681 mss.private_dirty >> 10,
682 mss.referenced >> 10,
683 mss.anonymous >> 10,
684 mss.anonymous_thp >> 10,
685 mss.shared_hugetlb >> 10,
686 mss.private_hugetlb >> 10,
687 mss.swap >> 10,
688 (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
689 vma_kernel_pagesize(vma) >> 10,
690 vma_mmu_pagesize(vma) >> 10,
691 (vma->vm_flags & VM_LOCKED) ?
692 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
694 show_smap_vma_flags(m, vma);
695 m_cache_vma(m, vma);
696 return 0;
699 static int show_pid_smap(struct seq_file *m, void *v)
701 return show_smap(m, v, 1);
704 static int show_tid_smap(struct seq_file *m, void *v)
706 return show_smap(m, v, 0);
709 static const struct seq_operations proc_pid_smaps_op = {
710 .start = m_start,
711 .next = m_next,
712 .stop = m_stop,
713 .show = show_pid_smap
716 static const struct seq_operations proc_tid_smaps_op = {
717 .start = m_start,
718 .next = m_next,
719 .stop = m_stop,
720 .show = show_tid_smap
723 static int pid_smaps_open(struct inode *inode, struct file *file)
725 return do_maps_open(inode, file, &proc_pid_smaps_op);
728 static int tid_smaps_open(struct inode *inode, struct file *file)
730 return do_maps_open(inode, file, &proc_tid_smaps_op);
733 const struct file_operations proc_pid_smaps_operations = {
734 .open = pid_smaps_open,
735 .read = seq_read,
736 .llseek = seq_lseek,
737 .release = proc_map_release,
740 const struct file_operations proc_tid_smaps_operations = {
741 .open = tid_smaps_open,
742 .read = seq_read,
743 .llseek = seq_lseek,
744 .release = proc_map_release,
747 enum clear_refs_types {
748 CLEAR_REFS_ALL = 1,
749 CLEAR_REFS_ANON,
750 CLEAR_REFS_MAPPED,
751 CLEAR_REFS_SOFT_DIRTY,
752 CLEAR_REFS_MM_HIWATER_RSS,
753 CLEAR_REFS_LAST,
756 struct clear_refs_private {
757 enum clear_refs_types type;
760 #ifdef CONFIG_MEM_SOFT_DIRTY
761 static inline void clear_soft_dirty(struct vm_area_struct *vma,
762 unsigned long addr, pte_t *pte)
765 * The soft-dirty tracker uses #PF-s to catch writes
766 * to pages, so write-protect the pte as well. See the
767 * Documentation/vm/soft-dirty.txt for full description
768 * of how soft-dirty works.
770 pte_t ptent = *pte;
772 if (pte_present(ptent)) {
773 ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte);
774 ptent = pte_wrprotect(ptent);
775 ptent = pte_clear_soft_dirty(ptent);
776 ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent);
777 } else if (is_swap_pte(ptent)) {
778 ptent = pte_swp_clear_soft_dirty(ptent);
779 set_pte_at(vma->vm_mm, addr, pte, ptent);
782 #else
783 static inline void clear_soft_dirty(struct vm_area_struct *vma,
784 unsigned long addr, pte_t *pte)
787 #endif
789 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
790 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
791 unsigned long addr, pmd_t *pmdp)
793 pmd_t pmd = *pmdp;
795 /* See comment in change_huge_pmd() */
796 pmdp_invalidate(vma, addr, pmdp);
797 if (pmd_dirty(*pmdp))
798 pmd = pmd_mkdirty(pmd);
799 if (pmd_young(*pmdp))
800 pmd = pmd_mkyoung(pmd);
802 pmd = pmd_wrprotect(pmd);
803 pmd = pmd_clear_soft_dirty(pmd);
805 if (vma->vm_flags & VM_SOFTDIRTY)
806 vma->vm_flags &= ~VM_SOFTDIRTY;
808 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
810 #else
811 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
812 unsigned long addr, pmd_t *pmdp)
815 #endif
817 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
818 unsigned long end, struct mm_walk *walk)
820 struct clear_refs_private *cp = walk->private;
821 struct vm_area_struct *vma = walk->vma;
822 pte_t *pte, ptent;
823 spinlock_t *ptl;
824 struct page *page;
826 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
827 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
828 clear_soft_dirty_pmd(vma, addr, pmd);
829 goto out;
832 page = pmd_page(*pmd);
834 /* Clear accessed and referenced bits. */
835 pmdp_test_and_clear_young(vma, addr, pmd);
836 test_and_clear_page_young(page);
837 ClearPageReferenced(page);
838 out:
839 spin_unlock(ptl);
840 return 0;
843 if (pmd_trans_unstable(pmd))
844 return 0;
846 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
847 for (; addr != end; pte++, addr += PAGE_SIZE) {
848 ptent = *pte;
850 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
851 clear_soft_dirty(vma, addr, pte);
852 continue;
855 if (!pte_present(ptent))
856 continue;
858 page = vm_normal_page(vma, addr, ptent);
859 if (!page)
860 continue;
862 /* Clear accessed and referenced bits. */
863 ptep_test_and_clear_young(vma, addr, pte);
864 test_and_clear_page_young(page);
865 ClearPageReferenced(page);
867 pte_unmap_unlock(pte - 1, ptl);
868 cond_resched();
869 return 0;
872 static int clear_refs_test_walk(unsigned long start, unsigned long end,
873 struct mm_walk *walk)
875 struct clear_refs_private *cp = walk->private;
876 struct vm_area_struct *vma = walk->vma;
878 if (vma->vm_flags & VM_PFNMAP)
879 return 1;
882 * Writing 1 to /proc/pid/clear_refs affects all pages.
883 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
884 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
885 * Writing 4 to /proc/pid/clear_refs affects all pages.
887 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
888 return 1;
889 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
890 return 1;
891 return 0;
894 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
895 size_t count, loff_t *ppos)
897 struct task_struct *task;
898 char buffer[PROC_NUMBUF];
899 struct mm_struct *mm;
900 struct vm_area_struct *vma;
901 enum clear_refs_types type;
902 int itype;
903 int rv;
905 memset(buffer, 0, sizeof(buffer));
906 if (count > sizeof(buffer) - 1)
907 count = sizeof(buffer) - 1;
908 if (copy_from_user(buffer, buf, count))
909 return -EFAULT;
910 rv = kstrtoint(strstrip(buffer), 10, &itype);
911 if (rv < 0)
912 return rv;
913 type = (enum clear_refs_types)itype;
914 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
915 return -EINVAL;
917 task = get_proc_task(file_inode(file));
918 if (!task)
919 return -ESRCH;
920 mm = get_task_mm(task);
921 if (mm) {
922 struct clear_refs_private cp = {
923 .type = type,
925 struct mm_walk clear_refs_walk = {
926 .pmd_entry = clear_refs_pte_range,
927 .test_walk = clear_refs_test_walk,
928 .mm = mm,
929 .private = &cp,
932 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
934 * Writing 5 to /proc/pid/clear_refs resets the peak
935 * resident set size to this mm's current rss value.
937 down_write(&mm->mmap_sem);
938 reset_mm_hiwater_rss(mm);
939 up_write(&mm->mmap_sem);
940 goto out_mm;
943 down_read(&mm->mmap_sem);
944 if (type == CLEAR_REFS_SOFT_DIRTY) {
945 for (vma = mm->mmap; vma; vma = vma->vm_next) {
946 if (!(vma->vm_flags & VM_SOFTDIRTY))
947 continue;
948 up_read(&mm->mmap_sem);
949 down_write(&mm->mmap_sem);
950 for (vma = mm->mmap; vma; vma = vma->vm_next) {
951 vma->vm_flags &= ~VM_SOFTDIRTY;
952 vma_set_page_prot(vma);
954 downgrade_write(&mm->mmap_sem);
955 break;
957 mmu_notifier_invalidate_range_start(mm, 0, -1);
959 walk_page_range(0, ~0UL, &clear_refs_walk);
960 if (type == CLEAR_REFS_SOFT_DIRTY)
961 mmu_notifier_invalidate_range_end(mm, 0, -1);
962 flush_tlb_mm(mm);
963 up_read(&mm->mmap_sem);
964 out_mm:
965 mmput(mm);
967 put_task_struct(task);
969 return count;
972 const struct file_operations proc_clear_refs_operations = {
973 .write = clear_refs_write,
974 .llseek = noop_llseek,
977 typedef struct {
978 u64 pme;
979 } pagemap_entry_t;
981 struct pagemapread {
982 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
983 pagemap_entry_t *buffer;
984 bool show_pfn;
987 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
988 #define PAGEMAP_WALK_MASK (PMD_MASK)
990 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
991 #define PM_PFRAME_BITS 55
992 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
993 #define PM_SOFT_DIRTY BIT_ULL(55)
994 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
995 #define PM_FILE BIT_ULL(61)
996 #define PM_SWAP BIT_ULL(62)
997 #define PM_PRESENT BIT_ULL(63)
999 #define PM_END_OF_BUFFER 1
1001 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1003 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1006 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1007 struct pagemapread *pm)
1009 pm->buffer[pm->pos++] = *pme;
1010 if (pm->pos >= pm->len)
1011 return PM_END_OF_BUFFER;
1012 return 0;
1015 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1016 struct mm_walk *walk)
1018 struct pagemapread *pm = walk->private;
1019 unsigned long addr = start;
1020 int err = 0;
1022 while (addr < end) {
1023 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1024 pagemap_entry_t pme = make_pme(0, 0);
1025 /* End of address space hole, which we mark as non-present. */
1026 unsigned long hole_end;
1028 if (vma)
1029 hole_end = min(end, vma->vm_start);
1030 else
1031 hole_end = end;
1033 for (; addr < hole_end; addr += PAGE_SIZE) {
1034 err = add_to_pagemap(addr, &pme, pm);
1035 if (err)
1036 goto out;
1039 if (!vma)
1040 break;
1042 /* Addresses in the VMA. */
1043 if (vma->vm_flags & VM_SOFTDIRTY)
1044 pme = make_pme(0, PM_SOFT_DIRTY);
1045 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1046 err = add_to_pagemap(addr, &pme, pm);
1047 if (err)
1048 goto out;
1051 out:
1052 return err;
1055 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1056 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1058 u64 frame = 0, flags = 0;
1059 struct page *page = NULL;
1061 if (pte_present(pte)) {
1062 if (pm->show_pfn)
1063 frame = pte_pfn(pte);
1064 flags |= PM_PRESENT;
1065 page = vm_normal_page(vma, addr, pte);
1066 if (pte_soft_dirty(pte))
1067 flags |= PM_SOFT_DIRTY;
1068 } else if (is_swap_pte(pte)) {
1069 swp_entry_t entry;
1070 if (pte_swp_soft_dirty(pte))
1071 flags |= PM_SOFT_DIRTY;
1072 entry = pte_to_swp_entry(pte);
1073 frame = swp_type(entry) |
1074 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1075 flags |= PM_SWAP;
1076 if (is_migration_entry(entry))
1077 page = migration_entry_to_page(entry);
1080 if (page && !PageAnon(page))
1081 flags |= PM_FILE;
1082 if (page && page_mapcount(page) == 1)
1083 flags |= PM_MMAP_EXCLUSIVE;
1084 if (vma->vm_flags & VM_SOFTDIRTY)
1085 flags |= PM_SOFT_DIRTY;
1087 return make_pme(frame, flags);
1090 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1091 struct mm_walk *walk)
1093 struct vm_area_struct *vma = walk->vma;
1094 struct pagemapread *pm = walk->private;
1095 spinlock_t *ptl;
1096 pte_t *pte, *orig_pte;
1097 int err = 0;
1099 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1100 if (pmd_trans_huge_lock(pmdp, vma, &ptl) == 1) {
1101 u64 flags = 0, frame = 0;
1102 pmd_t pmd = *pmdp;
1104 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
1105 flags |= PM_SOFT_DIRTY;
1108 * Currently pmd for thp is always present because thp
1109 * can not be swapped-out, migrated, or HWPOISONed
1110 * (split in such cases instead.)
1111 * This if-check is just to prepare for future implementation.
1113 if (pmd_present(pmd)) {
1114 struct page *page = pmd_page(pmd);
1116 if (page_mapcount(page) == 1)
1117 flags |= PM_MMAP_EXCLUSIVE;
1119 flags |= PM_PRESENT;
1120 if (pm->show_pfn)
1121 frame = pmd_pfn(pmd) +
1122 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1125 for (; addr != end; addr += PAGE_SIZE) {
1126 pagemap_entry_t pme = make_pme(frame, flags);
1128 err = add_to_pagemap(addr, &pme, pm);
1129 if (err)
1130 break;
1131 if (pm->show_pfn && (flags & PM_PRESENT))
1132 frame++;
1134 spin_unlock(ptl);
1135 return err;
1138 if (pmd_trans_unstable(pmdp))
1139 return 0;
1140 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1143 * We can assume that @vma always points to a valid one and @end never
1144 * goes beyond vma->vm_end.
1146 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1147 for (; addr < end; pte++, addr += PAGE_SIZE) {
1148 pagemap_entry_t pme;
1150 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1151 err = add_to_pagemap(addr, &pme, pm);
1152 if (err)
1153 break;
1155 pte_unmap_unlock(orig_pte, ptl);
1157 cond_resched();
1159 return err;
1162 #ifdef CONFIG_HUGETLB_PAGE
1163 /* This function walks within one hugetlb entry in the single call */
1164 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1165 unsigned long addr, unsigned long end,
1166 struct mm_walk *walk)
1168 struct pagemapread *pm = walk->private;
1169 struct vm_area_struct *vma = walk->vma;
1170 u64 flags = 0, frame = 0;
1171 int err = 0;
1172 pte_t pte;
1174 if (vma->vm_flags & VM_SOFTDIRTY)
1175 flags |= PM_SOFT_DIRTY;
1177 pte = huge_ptep_get(ptep);
1178 if (pte_present(pte)) {
1179 struct page *page = pte_page(pte);
1181 if (!PageAnon(page))
1182 flags |= PM_FILE;
1184 if (page_mapcount(page) == 1)
1185 flags |= PM_MMAP_EXCLUSIVE;
1187 flags |= PM_PRESENT;
1188 if (pm->show_pfn)
1189 frame = pte_pfn(pte) +
1190 ((addr & ~hmask) >> PAGE_SHIFT);
1193 for (; addr != end; addr += PAGE_SIZE) {
1194 pagemap_entry_t pme = make_pme(frame, flags);
1196 err = add_to_pagemap(addr, &pme, pm);
1197 if (err)
1198 return err;
1199 if (pm->show_pfn && (flags & PM_PRESENT))
1200 frame++;
1203 cond_resched();
1205 return err;
1207 #endif /* HUGETLB_PAGE */
1210 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1212 * For each page in the address space, this file contains one 64-bit entry
1213 * consisting of the following:
1215 * Bits 0-54 page frame number (PFN) if present
1216 * Bits 0-4 swap type if swapped
1217 * Bits 5-54 swap offset if swapped
1218 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1219 * Bit 56 page exclusively mapped
1220 * Bits 57-60 zero
1221 * Bit 61 page is file-page or shared-anon
1222 * Bit 62 page swapped
1223 * Bit 63 page present
1225 * If the page is not present but in swap, then the PFN contains an
1226 * encoding of the swap file number and the page's offset into the
1227 * swap. Unmapped pages return a null PFN. This allows determining
1228 * precisely which pages are mapped (or in swap) and comparing mapped
1229 * pages between processes.
1231 * Efficient users of this interface will use /proc/pid/maps to
1232 * determine which areas of memory are actually mapped and llseek to
1233 * skip over unmapped regions.
1235 static ssize_t pagemap_read(struct file *file, char __user *buf,
1236 size_t count, loff_t *ppos)
1238 struct mm_struct *mm = file->private_data;
1239 struct pagemapread pm;
1240 struct mm_walk pagemap_walk = {};
1241 unsigned long src;
1242 unsigned long svpfn;
1243 unsigned long start_vaddr;
1244 unsigned long end_vaddr;
1245 int ret = 0, copied = 0;
1247 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
1248 goto out;
1250 ret = -EINVAL;
1251 /* file position must be aligned */
1252 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1253 goto out_mm;
1255 ret = 0;
1256 if (!count)
1257 goto out_mm;
1259 /* do not disclose physical addresses: attack vector */
1260 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1262 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1263 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1264 ret = -ENOMEM;
1265 if (!pm.buffer)
1266 goto out_mm;
1268 pagemap_walk.pmd_entry = pagemap_pmd_range;
1269 pagemap_walk.pte_hole = pagemap_pte_hole;
1270 #ifdef CONFIG_HUGETLB_PAGE
1271 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1272 #endif
1273 pagemap_walk.mm = mm;
1274 pagemap_walk.private = &pm;
1276 src = *ppos;
1277 svpfn = src / PM_ENTRY_BYTES;
1278 start_vaddr = svpfn << PAGE_SHIFT;
1279 end_vaddr = mm->task_size;
1281 /* watch out for wraparound */
1282 if (svpfn > mm->task_size >> PAGE_SHIFT)
1283 start_vaddr = end_vaddr;
1286 * The odds are that this will stop walking way
1287 * before end_vaddr, because the length of the
1288 * user buffer is tracked in "pm", and the walk
1289 * will stop when we hit the end of the buffer.
1291 ret = 0;
1292 while (count && (start_vaddr < end_vaddr)) {
1293 int len;
1294 unsigned long end;
1296 pm.pos = 0;
1297 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1298 /* overflow ? */
1299 if (end < start_vaddr || end > end_vaddr)
1300 end = end_vaddr;
1301 down_read(&mm->mmap_sem);
1302 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1303 up_read(&mm->mmap_sem);
1304 start_vaddr = end;
1306 len = min(count, PM_ENTRY_BYTES * pm.pos);
1307 if (copy_to_user(buf, pm.buffer, len)) {
1308 ret = -EFAULT;
1309 goto out_free;
1311 copied += len;
1312 buf += len;
1313 count -= len;
1315 *ppos += copied;
1316 if (!ret || ret == PM_END_OF_BUFFER)
1317 ret = copied;
1319 out_free:
1320 kfree(pm.buffer);
1321 out_mm:
1322 mmput(mm);
1323 out:
1324 return ret;
1327 static int pagemap_open(struct inode *inode, struct file *file)
1329 struct mm_struct *mm;
1331 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1332 if (IS_ERR(mm))
1333 return PTR_ERR(mm);
1334 file->private_data = mm;
1335 return 0;
1338 static int pagemap_release(struct inode *inode, struct file *file)
1340 struct mm_struct *mm = file->private_data;
1342 if (mm)
1343 mmdrop(mm);
1344 return 0;
1347 const struct file_operations proc_pagemap_operations = {
1348 .llseek = mem_lseek, /* borrow this */
1349 .read = pagemap_read,
1350 .open = pagemap_open,
1351 .release = pagemap_release,
1353 #endif /* CONFIG_PROC_PAGE_MONITOR */
1355 #ifdef CONFIG_NUMA
1357 struct numa_maps {
1358 unsigned long pages;
1359 unsigned long anon;
1360 unsigned long active;
1361 unsigned long writeback;
1362 unsigned long mapcount_max;
1363 unsigned long dirty;
1364 unsigned long swapcache;
1365 unsigned long node[MAX_NUMNODES];
1368 struct numa_maps_private {
1369 struct proc_maps_private proc_maps;
1370 struct numa_maps md;
1373 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1374 unsigned long nr_pages)
1376 int count = page_mapcount(page);
1378 md->pages += nr_pages;
1379 if (pte_dirty || PageDirty(page))
1380 md->dirty += nr_pages;
1382 if (PageSwapCache(page))
1383 md->swapcache += nr_pages;
1385 if (PageActive(page) || PageUnevictable(page))
1386 md->active += nr_pages;
1388 if (PageWriteback(page))
1389 md->writeback += nr_pages;
1391 if (PageAnon(page))
1392 md->anon += nr_pages;
1394 if (count > md->mapcount_max)
1395 md->mapcount_max = count;
1397 md->node[page_to_nid(page)] += nr_pages;
1400 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1401 unsigned long addr)
1403 struct page *page;
1404 int nid;
1406 if (!pte_present(pte))
1407 return NULL;
1409 page = vm_normal_page(vma, addr, pte);
1410 if (!page)
1411 return NULL;
1413 if (PageReserved(page))
1414 return NULL;
1416 nid = page_to_nid(page);
1417 if (!node_isset(nid, node_states[N_MEMORY]))
1418 return NULL;
1420 return page;
1423 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1424 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1425 struct vm_area_struct *vma,
1426 unsigned long addr)
1428 struct page *page;
1429 int nid;
1431 if (!pmd_present(pmd))
1432 return NULL;
1434 page = vm_normal_page_pmd(vma, addr, pmd);
1435 if (!page)
1436 return NULL;
1438 if (PageReserved(page))
1439 return NULL;
1441 nid = page_to_nid(page);
1442 if (!node_isset(nid, node_states[N_MEMORY]))
1443 return NULL;
1445 return page;
1447 #endif
1449 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1450 unsigned long end, struct mm_walk *walk)
1452 struct numa_maps *md = walk->private;
1453 struct vm_area_struct *vma = walk->vma;
1454 spinlock_t *ptl;
1455 pte_t *orig_pte;
1456 pte_t *pte;
1458 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1459 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
1460 struct page *page;
1462 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1463 if (page)
1464 gather_stats(page, md, pmd_dirty(*pmd),
1465 HPAGE_PMD_SIZE/PAGE_SIZE);
1466 spin_unlock(ptl);
1467 return 0;
1470 if (pmd_trans_unstable(pmd))
1471 return 0;
1472 #endif
1473 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1474 do {
1475 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1476 if (!page)
1477 continue;
1478 gather_stats(page, md, pte_dirty(*pte), 1);
1480 } while (pte++, addr += PAGE_SIZE, addr != end);
1481 pte_unmap_unlock(orig_pte, ptl);
1482 return 0;
1484 #ifdef CONFIG_HUGETLB_PAGE
1485 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1486 unsigned long addr, unsigned long end, struct mm_walk *walk)
1488 pte_t huge_pte = huge_ptep_get(pte);
1489 struct numa_maps *md;
1490 struct page *page;
1492 if (!pte_present(huge_pte))
1493 return 0;
1495 page = pte_page(huge_pte);
1496 if (!page)
1497 return 0;
1499 md = walk->private;
1500 gather_stats(page, md, pte_dirty(huge_pte), 1);
1501 return 0;
1504 #else
1505 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1506 unsigned long addr, unsigned long end, struct mm_walk *walk)
1508 return 0;
1510 #endif
1513 * Display pages allocated per node and memory policy via /proc.
1515 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1517 struct numa_maps_private *numa_priv = m->private;
1518 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1519 struct vm_area_struct *vma = v;
1520 struct numa_maps *md = &numa_priv->md;
1521 struct file *file = vma->vm_file;
1522 struct mm_struct *mm = vma->vm_mm;
1523 struct mm_walk walk = {
1524 .hugetlb_entry = gather_hugetlb_stats,
1525 .pmd_entry = gather_pte_stats,
1526 .private = md,
1527 .mm = mm,
1529 struct mempolicy *pol;
1530 char buffer[64];
1531 int nid;
1533 if (!mm)
1534 return 0;
1536 /* Ensure we start with an empty set of numa_maps statistics. */
1537 memset(md, 0, sizeof(*md));
1539 pol = __get_vma_policy(vma, vma->vm_start);
1540 if (pol) {
1541 mpol_to_str(buffer, sizeof(buffer), pol);
1542 mpol_cond_put(pol);
1543 } else {
1544 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1547 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1549 if (file) {
1550 seq_puts(m, " file=");
1551 seq_file_path(m, file, "\n\t= ");
1552 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1553 seq_puts(m, " heap");
1554 } else if (is_stack(proc_priv, vma)) {
1555 seq_puts(m, " stack");
1558 if (is_vm_hugetlb_page(vma))
1559 seq_puts(m, " huge");
1561 /* mmap_sem is held by m_start */
1562 walk_page_vma(vma, &walk);
1564 if (!md->pages)
1565 goto out;
1567 if (md->anon)
1568 seq_printf(m, " anon=%lu", md->anon);
1570 if (md->dirty)
1571 seq_printf(m, " dirty=%lu", md->dirty);
1573 if (md->pages != md->anon && md->pages != md->dirty)
1574 seq_printf(m, " mapped=%lu", md->pages);
1576 if (md->mapcount_max > 1)
1577 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1579 if (md->swapcache)
1580 seq_printf(m, " swapcache=%lu", md->swapcache);
1582 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1583 seq_printf(m, " active=%lu", md->active);
1585 if (md->writeback)
1586 seq_printf(m, " writeback=%lu", md->writeback);
1588 for_each_node_state(nid, N_MEMORY)
1589 if (md->node[nid])
1590 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1592 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1593 out:
1594 seq_putc(m, '\n');
1595 m_cache_vma(m, vma);
1596 return 0;
1599 static int show_pid_numa_map(struct seq_file *m, void *v)
1601 return show_numa_map(m, v, 1);
1604 static int show_tid_numa_map(struct seq_file *m, void *v)
1606 return show_numa_map(m, v, 0);
1609 static const struct seq_operations proc_pid_numa_maps_op = {
1610 .start = m_start,
1611 .next = m_next,
1612 .stop = m_stop,
1613 .show = show_pid_numa_map,
1616 static const struct seq_operations proc_tid_numa_maps_op = {
1617 .start = m_start,
1618 .next = m_next,
1619 .stop = m_stop,
1620 .show = show_tid_numa_map,
1623 static int numa_maps_open(struct inode *inode, struct file *file,
1624 const struct seq_operations *ops)
1626 return proc_maps_open(inode, file, ops,
1627 sizeof(struct numa_maps_private));
1630 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1632 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1635 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1637 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1640 const struct file_operations proc_pid_numa_maps_operations = {
1641 .open = pid_numa_maps_open,
1642 .read = seq_read,
1643 .llseek = seq_lseek,
1644 .release = proc_map_release,
1647 const struct file_operations proc_tid_numa_maps_operations = {
1648 .open = tid_numa_maps_open,
1649 .read = seq_read,
1650 .llseek = seq_lseek,
1651 .release = proc_map_release,
1653 #endif /* CONFIG_NUMA */