mm: rename alloc_pages_exact_node() to __alloc_pages_node()
[linux/fpc-iii.git] / fs / proc / task_mmu.c
blob41f1a50c10c9e171c0138b3282cef92aa112e1c4
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>
17 #include <asm/elf.h>
18 #include <asm/uaccess.h>
19 #include <asm/tlbflush.h>
20 #include "internal.h"
22 void task_mem(struct seq_file *m, struct mm_struct *mm)
24 unsigned long data, text, lib, swap, ptes, pmds;
25 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
28 * Note: to minimize their overhead, mm maintains hiwater_vm and
29 * hiwater_rss only when about to *lower* total_vm or rss. Any
30 * collector of these hiwater stats must therefore get total_vm
31 * and rss too, which will usually be the higher. Barriers? not
32 * worth the effort, such snapshots can always be inconsistent.
34 hiwater_vm = total_vm = mm->total_vm;
35 if (hiwater_vm < mm->hiwater_vm)
36 hiwater_vm = mm->hiwater_vm;
37 hiwater_rss = total_rss = get_mm_rss(mm);
38 if (hiwater_rss < mm->hiwater_rss)
39 hiwater_rss = mm->hiwater_rss;
41 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
42 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
43 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
44 swap = get_mm_counter(mm, MM_SWAPENTS);
45 ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
46 pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
47 seq_printf(m,
48 "VmPeak:\t%8lu kB\n"
49 "VmSize:\t%8lu kB\n"
50 "VmLck:\t%8lu kB\n"
51 "VmPin:\t%8lu kB\n"
52 "VmHWM:\t%8lu kB\n"
53 "VmRSS:\t%8lu kB\n"
54 "VmData:\t%8lu kB\n"
55 "VmStk:\t%8lu kB\n"
56 "VmExe:\t%8lu kB\n"
57 "VmLib:\t%8lu kB\n"
58 "VmPTE:\t%8lu kB\n"
59 "VmPMD:\t%8lu kB\n"
60 "VmSwap:\t%8lu kB\n",
61 hiwater_vm << (PAGE_SHIFT-10),
62 total_vm << (PAGE_SHIFT-10),
63 mm->locked_vm << (PAGE_SHIFT-10),
64 mm->pinned_vm << (PAGE_SHIFT-10),
65 hiwater_rss << (PAGE_SHIFT-10),
66 total_rss << (PAGE_SHIFT-10),
67 data << (PAGE_SHIFT-10),
68 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
69 ptes >> 10,
70 pmds >> 10,
71 swap << (PAGE_SHIFT-10));
74 unsigned long task_vsize(struct mm_struct *mm)
76 return PAGE_SIZE * mm->total_vm;
79 unsigned long task_statm(struct mm_struct *mm,
80 unsigned long *shared, unsigned long *text,
81 unsigned long *data, unsigned long *resident)
83 *shared = get_mm_counter(mm, MM_FILEPAGES);
84 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
85 >> PAGE_SHIFT;
86 *data = mm->total_vm - mm->shared_vm;
87 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
88 return mm->total_vm;
91 #ifdef CONFIG_NUMA
93 * Save get_task_policy() for show_numa_map().
95 static void hold_task_mempolicy(struct proc_maps_private *priv)
97 struct task_struct *task = priv->task;
99 task_lock(task);
100 priv->task_mempolicy = get_task_policy(task);
101 mpol_get(priv->task_mempolicy);
102 task_unlock(task);
104 static void release_task_mempolicy(struct proc_maps_private *priv)
106 mpol_put(priv->task_mempolicy);
108 #else
109 static void hold_task_mempolicy(struct proc_maps_private *priv)
112 static void release_task_mempolicy(struct proc_maps_private *priv)
115 #endif
117 static void vma_stop(struct proc_maps_private *priv)
119 struct mm_struct *mm = priv->mm;
121 release_task_mempolicy(priv);
122 up_read(&mm->mmap_sem);
123 mmput(mm);
126 static struct vm_area_struct *
127 m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
129 if (vma == priv->tail_vma)
130 return NULL;
131 return vma->vm_next ?: priv->tail_vma;
134 static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
136 if (m->count < m->size) /* vma is copied successfully */
137 m->version = m_next_vma(m->private, vma) ? vma->vm_start : -1UL;
140 static void *m_start(struct seq_file *m, loff_t *ppos)
142 struct proc_maps_private *priv = m->private;
143 unsigned long last_addr = m->version;
144 struct mm_struct *mm;
145 struct vm_area_struct *vma;
146 unsigned int pos = *ppos;
148 /* See m_cache_vma(). Zero at the start or after lseek. */
149 if (last_addr == -1UL)
150 return NULL;
152 priv->task = get_proc_task(priv->inode);
153 if (!priv->task)
154 return ERR_PTR(-ESRCH);
156 mm = priv->mm;
157 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
158 return NULL;
160 down_read(&mm->mmap_sem);
161 hold_task_mempolicy(priv);
162 priv->tail_vma = get_gate_vma(mm);
164 if (last_addr) {
165 vma = find_vma(mm, last_addr);
166 if (vma && (vma = m_next_vma(priv, vma)))
167 return vma;
170 m->version = 0;
171 if (pos < mm->map_count) {
172 for (vma = mm->mmap; pos; pos--) {
173 m->version = vma->vm_start;
174 vma = vma->vm_next;
176 return vma;
179 /* we do not bother to update m->version in this case */
180 if (pos == mm->map_count && priv->tail_vma)
181 return priv->tail_vma;
183 vma_stop(priv);
184 return NULL;
187 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
189 struct proc_maps_private *priv = m->private;
190 struct vm_area_struct *next;
192 (*pos)++;
193 next = m_next_vma(priv, v);
194 if (!next)
195 vma_stop(priv);
196 return next;
199 static void m_stop(struct seq_file *m, void *v)
201 struct proc_maps_private *priv = m->private;
203 if (!IS_ERR_OR_NULL(v))
204 vma_stop(priv);
205 if (priv->task) {
206 put_task_struct(priv->task);
207 priv->task = NULL;
211 static int proc_maps_open(struct inode *inode, struct file *file,
212 const struct seq_operations *ops, int psize)
214 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
216 if (!priv)
217 return -ENOMEM;
219 priv->inode = inode;
220 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
221 if (IS_ERR(priv->mm)) {
222 int err = PTR_ERR(priv->mm);
224 seq_release_private(inode, file);
225 return err;
228 return 0;
231 static int proc_map_release(struct inode *inode, struct file *file)
233 struct seq_file *seq = file->private_data;
234 struct proc_maps_private *priv = seq->private;
236 if (priv->mm)
237 mmdrop(priv->mm);
239 return seq_release_private(inode, file);
242 static int do_maps_open(struct inode *inode, struct file *file,
243 const struct seq_operations *ops)
245 return proc_maps_open(inode, file, ops,
246 sizeof(struct proc_maps_private));
249 static pid_t pid_of_stack(struct proc_maps_private *priv,
250 struct vm_area_struct *vma, bool is_pid)
252 struct inode *inode = priv->inode;
253 struct task_struct *task;
254 pid_t ret = 0;
256 rcu_read_lock();
257 task = pid_task(proc_pid(inode), PIDTYPE_PID);
258 if (task) {
259 task = task_of_stack(task, vma, is_pid);
260 if (task)
261 ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
263 rcu_read_unlock();
265 return ret;
268 static void
269 show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
271 struct mm_struct *mm = vma->vm_mm;
272 struct file *file = vma->vm_file;
273 struct proc_maps_private *priv = m->private;
274 vm_flags_t flags = vma->vm_flags;
275 unsigned long ino = 0;
276 unsigned long long pgoff = 0;
277 unsigned long start, end;
278 dev_t dev = 0;
279 const char *name = NULL;
281 if (file) {
282 struct inode *inode = file_inode(vma->vm_file);
283 dev = inode->i_sb->s_dev;
284 ino = inode->i_ino;
285 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
288 /* We don't show the stack guard page in /proc/maps */
289 start = vma->vm_start;
290 if (stack_guard_page_start(vma, start))
291 start += PAGE_SIZE;
292 end = vma->vm_end;
293 if (stack_guard_page_end(vma, end))
294 end -= PAGE_SIZE;
296 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
297 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
298 start,
299 end,
300 flags & VM_READ ? 'r' : '-',
301 flags & VM_WRITE ? 'w' : '-',
302 flags & VM_EXEC ? 'x' : '-',
303 flags & VM_MAYSHARE ? 's' : 'p',
304 pgoff,
305 MAJOR(dev), MINOR(dev), ino);
308 * Print the dentry name for named mappings, and a
309 * special [heap] marker for the heap:
311 if (file) {
312 seq_pad(m, ' ');
313 seq_file_path(m, file, "\n");
314 goto done;
317 if (vma->vm_ops && vma->vm_ops->name) {
318 name = vma->vm_ops->name(vma);
319 if (name)
320 goto done;
323 name = arch_vma_name(vma);
324 if (!name) {
325 pid_t tid;
327 if (!mm) {
328 name = "[vdso]";
329 goto done;
332 if (vma->vm_start <= mm->brk &&
333 vma->vm_end >= mm->start_brk) {
334 name = "[heap]";
335 goto done;
338 tid = pid_of_stack(priv, vma, is_pid);
339 if (tid != 0) {
341 * Thread stack in /proc/PID/task/TID/maps or
342 * the main process stack.
344 if (!is_pid || (vma->vm_start <= mm->start_stack &&
345 vma->vm_end >= mm->start_stack)) {
346 name = "[stack]";
347 } else {
348 /* Thread stack in /proc/PID/maps */
349 seq_pad(m, ' ');
350 seq_printf(m, "[stack:%d]", tid);
355 done:
356 if (name) {
357 seq_pad(m, ' ');
358 seq_puts(m, name);
360 seq_putc(m, '\n');
363 static int show_map(struct seq_file *m, void *v, int is_pid)
365 show_map_vma(m, v, is_pid);
366 m_cache_vma(m, v);
367 return 0;
370 static int show_pid_map(struct seq_file *m, void *v)
372 return show_map(m, v, 1);
375 static int show_tid_map(struct seq_file *m, void *v)
377 return show_map(m, v, 0);
380 static const struct seq_operations proc_pid_maps_op = {
381 .start = m_start,
382 .next = m_next,
383 .stop = m_stop,
384 .show = show_pid_map
387 static const struct seq_operations proc_tid_maps_op = {
388 .start = m_start,
389 .next = m_next,
390 .stop = m_stop,
391 .show = show_tid_map
394 static int pid_maps_open(struct inode *inode, struct file *file)
396 return do_maps_open(inode, file, &proc_pid_maps_op);
399 static int tid_maps_open(struct inode *inode, struct file *file)
401 return do_maps_open(inode, file, &proc_tid_maps_op);
404 const struct file_operations proc_pid_maps_operations = {
405 .open = pid_maps_open,
406 .read = seq_read,
407 .llseek = seq_lseek,
408 .release = proc_map_release,
411 const struct file_operations proc_tid_maps_operations = {
412 .open = tid_maps_open,
413 .read = seq_read,
414 .llseek = seq_lseek,
415 .release = proc_map_release,
419 * Proportional Set Size(PSS): my share of RSS.
421 * PSS of a process is the count of pages it has in memory, where each
422 * page is divided by the number of processes sharing it. So if a
423 * process has 1000 pages all to itself, and 1000 shared with one other
424 * process, its PSS will be 1500.
426 * To keep (accumulated) division errors low, we adopt a 64bit
427 * fixed-point pss counter to minimize division errors. So (pss >>
428 * PSS_SHIFT) would be the real byte count.
430 * A shift of 12 before division means (assuming 4K page size):
431 * - 1M 3-user-pages add up to 8KB errors;
432 * - supports mapcount up to 2^24, or 16M;
433 * - supports PSS up to 2^52 bytes, or 4PB.
435 #define PSS_SHIFT 12
437 #ifdef CONFIG_PROC_PAGE_MONITOR
438 struct mem_size_stats {
439 unsigned long resident;
440 unsigned long shared_clean;
441 unsigned long shared_dirty;
442 unsigned long private_clean;
443 unsigned long private_dirty;
444 unsigned long referenced;
445 unsigned long anonymous;
446 unsigned long anonymous_thp;
447 unsigned long swap;
448 u64 pss;
449 u64 swap_pss;
452 static void smaps_account(struct mem_size_stats *mss, struct page *page,
453 unsigned long size, bool young, bool dirty)
455 int mapcount;
457 if (PageAnon(page))
458 mss->anonymous += size;
460 mss->resident += size;
461 /* Accumulate the size in pages that have been accessed. */
462 if (young || PageReferenced(page))
463 mss->referenced += size;
464 mapcount = page_mapcount(page);
465 if (mapcount >= 2) {
466 u64 pss_delta;
468 if (dirty || PageDirty(page))
469 mss->shared_dirty += size;
470 else
471 mss->shared_clean += size;
472 pss_delta = (u64)size << PSS_SHIFT;
473 do_div(pss_delta, mapcount);
474 mss->pss += pss_delta;
475 } else {
476 if (dirty || PageDirty(page))
477 mss->private_dirty += size;
478 else
479 mss->private_clean += size;
480 mss->pss += (u64)size << PSS_SHIFT;
484 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
485 struct mm_walk *walk)
487 struct mem_size_stats *mss = walk->private;
488 struct vm_area_struct *vma = walk->vma;
489 struct page *page = NULL;
491 if (pte_present(*pte)) {
492 page = vm_normal_page(vma, addr, *pte);
493 } else if (is_swap_pte(*pte)) {
494 swp_entry_t swpent = pte_to_swp_entry(*pte);
496 if (!non_swap_entry(swpent)) {
497 int mapcount;
499 mss->swap += PAGE_SIZE;
500 mapcount = swp_swapcount(swpent);
501 if (mapcount >= 2) {
502 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
504 do_div(pss_delta, mapcount);
505 mss->swap_pss += pss_delta;
506 } else {
507 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
509 } else if (is_migration_entry(swpent))
510 page = migration_entry_to_page(swpent);
513 if (!page)
514 return;
515 smaps_account(mss, page, PAGE_SIZE, pte_young(*pte), pte_dirty(*pte));
518 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
519 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
520 struct mm_walk *walk)
522 struct mem_size_stats *mss = walk->private;
523 struct vm_area_struct *vma = walk->vma;
524 struct page *page;
526 /* FOLL_DUMP will return -EFAULT on huge zero page */
527 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
528 if (IS_ERR_OR_NULL(page))
529 return;
530 mss->anonymous_thp += HPAGE_PMD_SIZE;
531 smaps_account(mss, page, HPAGE_PMD_SIZE,
532 pmd_young(*pmd), pmd_dirty(*pmd));
534 #else
535 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
536 struct mm_walk *walk)
539 #endif
541 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
542 struct mm_walk *walk)
544 struct vm_area_struct *vma = walk->vma;
545 pte_t *pte;
546 spinlock_t *ptl;
548 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
549 smaps_pmd_entry(pmd, addr, walk);
550 spin_unlock(ptl);
551 return 0;
554 if (pmd_trans_unstable(pmd))
555 return 0;
557 * The mmap_sem held all the way back in m_start() is what
558 * keeps khugepaged out of here and from collapsing things
559 * in here.
561 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
562 for (; addr != end; pte++, addr += PAGE_SIZE)
563 smaps_pte_entry(pte, addr, walk);
564 pte_unmap_unlock(pte - 1, ptl);
565 cond_resched();
566 return 0;
569 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
572 * Don't forget to update Documentation/ on changes.
574 static const char mnemonics[BITS_PER_LONG][2] = {
576 * In case if we meet a flag we don't know about.
578 [0 ... (BITS_PER_LONG-1)] = "??",
580 [ilog2(VM_READ)] = "rd",
581 [ilog2(VM_WRITE)] = "wr",
582 [ilog2(VM_EXEC)] = "ex",
583 [ilog2(VM_SHARED)] = "sh",
584 [ilog2(VM_MAYREAD)] = "mr",
585 [ilog2(VM_MAYWRITE)] = "mw",
586 [ilog2(VM_MAYEXEC)] = "me",
587 [ilog2(VM_MAYSHARE)] = "ms",
588 [ilog2(VM_GROWSDOWN)] = "gd",
589 [ilog2(VM_PFNMAP)] = "pf",
590 [ilog2(VM_DENYWRITE)] = "dw",
591 #ifdef CONFIG_X86_INTEL_MPX
592 [ilog2(VM_MPX)] = "mp",
593 #endif
594 [ilog2(VM_LOCKED)] = "lo",
595 [ilog2(VM_IO)] = "io",
596 [ilog2(VM_SEQ_READ)] = "sr",
597 [ilog2(VM_RAND_READ)] = "rr",
598 [ilog2(VM_DONTCOPY)] = "dc",
599 [ilog2(VM_DONTEXPAND)] = "de",
600 [ilog2(VM_ACCOUNT)] = "ac",
601 [ilog2(VM_NORESERVE)] = "nr",
602 [ilog2(VM_HUGETLB)] = "ht",
603 [ilog2(VM_ARCH_1)] = "ar",
604 [ilog2(VM_DONTDUMP)] = "dd",
605 #ifdef CONFIG_MEM_SOFT_DIRTY
606 [ilog2(VM_SOFTDIRTY)] = "sd",
607 #endif
608 [ilog2(VM_MIXEDMAP)] = "mm",
609 [ilog2(VM_HUGEPAGE)] = "hg",
610 [ilog2(VM_NOHUGEPAGE)] = "nh",
611 [ilog2(VM_MERGEABLE)] = "mg",
612 [ilog2(VM_UFFD_MISSING)]= "um",
613 [ilog2(VM_UFFD_WP)] = "uw",
615 size_t i;
617 seq_puts(m, "VmFlags: ");
618 for (i = 0; i < BITS_PER_LONG; i++) {
619 if (vma->vm_flags & (1UL << i)) {
620 seq_printf(m, "%c%c ",
621 mnemonics[i][0], mnemonics[i][1]);
624 seq_putc(m, '\n');
627 static int show_smap(struct seq_file *m, void *v, int is_pid)
629 struct vm_area_struct *vma = v;
630 struct mem_size_stats mss;
631 struct mm_walk smaps_walk = {
632 .pmd_entry = smaps_pte_range,
633 .mm = vma->vm_mm,
634 .private = &mss,
637 memset(&mss, 0, sizeof mss);
638 /* mmap_sem is held in m_start */
639 walk_page_vma(vma, &smaps_walk);
641 show_map_vma(m, vma, is_pid);
643 seq_printf(m,
644 "Size: %8lu kB\n"
645 "Rss: %8lu kB\n"
646 "Pss: %8lu kB\n"
647 "Shared_Clean: %8lu kB\n"
648 "Shared_Dirty: %8lu kB\n"
649 "Private_Clean: %8lu kB\n"
650 "Private_Dirty: %8lu kB\n"
651 "Referenced: %8lu kB\n"
652 "Anonymous: %8lu kB\n"
653 "AnonHugePages: %8lu kB\n"
654 "Swap: %8lu kB\n"
655 "SwapPss: %8lu kB\n"
656 "KernelPageSize: %8lu kB\n"
657 "MMUPageSize: %8lu kB\n"
658 "Locked: %8lu kB\n",
659 (vma->vm_end - vma->vm_start) >> 10,
660 mss.resident >> 10,
661 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
662 mss.shared_clean >> 10,
663 mss.shared_dirty >> 10,
664 mss.private_clean >> 10,
665 mss.private_dirty >> 10,
666 mss.referenced >> 10,
667 mss.anonymous >> 10,
668 mss.anonymous_thp >> 10,
669 mss.swap >> 10,
670 (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
671 vma_kernel_pagesize(vma) >> 10,
672 vma_mmu_pagesize(vma) >> 10,
673 (vma->vm_flags & VM_LOCKED) ?
674 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
676 show_smap_vma_flags(m, vma);
677 m_cache_vma(m, vma);
678 return 0;
681 static int show_pid_smap(struct seq_file *m, void *v)
683 return show_smap(m, v, 1);
686 static int show_tid_smap(struct seq_file *m, void *v)
688 return show_smap(m, v, 0);
691 static const struct seq_operations proc_pid_smaps_op = {
692 .start = m_start,
693 .next = m_next,
694 .stop = m_stop,
695 .show = show_pid_smap
698 static const struct seq_operations proc_tid_smaps_op = {
699 .start = m_start,
700 .next = m_next,
701 .stop = m_stop,
702 .show = show_tid_smap
705 static int pid_smaps_open(struct inode *inode, struct file *file)
707 return do_maps_open(inode, file, &proc_pid_smaps_op);
710 static int tid_smaps_open(struct inode *inode, struct file *file)
712 return do_maps_open(inode, file, &proc_tid_smaps_op);
715 const struct file_operations proc_pid_smaps_operations = {
716 .open = pid_smaps_open,
717 .read = seq_read,
718 .llseek = seq_lseek,
719 .release = proc_map_release,
722 const struct file_operations proc_tid_smaps_operations = {
723 .open = tid_smaps_open,
724 .read = seq_read,
725 .llseek = seq_lseek,
726 .release = proc_map_release,
729 enum clear_refs_types {
730 CLEAR_REFS_ALL = 1,
731 CLEAR_REFS_ANON,
732 CLEAR_REFS_MAPPED,
733 CLEAR_REFS_SOFT_DIRTY,
734 CLEAR_REFS_MM_HIWATER_RSS,
735 CLEAR_REFS_LAST,
738 struct clear_refs_private {
739 enum clear_refs_types type;
742 #ifdef CONFIG_MEM_SOFT_DIRTY
743 static inline void clear_soft_dirty(struct vm_area_struct *vma,
744 unsigned long addr, pte_t *pte)
747 * The soft-dirty tracker uses #PF-s to catch writes
748 * to pages, so write-protect the pte as well. See the
749 * Documentation/vm/soft-dirty.txt for full description
750 * of how soft-dirty works.
752 pte_t ptent = *pte;
754 if (pte_present(ptent)) {
755 ptent = pte_wrprotect(ptent);
756 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
757 } else if (is_swap_pte(ptent)) {
758 ptent = pte_swp_clear_soft_dirty(ptent);
761 set_pte_at(vma->vm_mm, addr, pte, ptent);
764 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
765 unsigned long addr, pmd_t *pmdp)
767 pmd_t pmd = *pmdp;
769 pmd = pmd_wrprotect(pmd);
770 pmd = pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
772 if (vma->vm_flags & VM_SOFTDIRTY)
773 vma->vm_flags &= ~VM_SOFTDIRTY;
775 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
778 #else
780 static inline void clear_soft_dirty(struct vm_area_struct *vma,
781 unsigned long addr, pte_t *pte)
785 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
786 unsigned long addr, pmd_t *pmdp)
789 #endif
791 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
792 unsigned long end, struct mm_walk *walk)
794 struct clear_refs_private *cp = walk->private;
795 struct vm_area_struct *vma = walk->vma;
796 pte_t *pte, ptent;
797 spinlock_t *ptl;
798 struct page *page;
800 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
801 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
802 clear_soft_dirty_pmd(vma, addr, pmd);
803 goto out;
806 page = pmd_page(*pmd);
808 /* Clear accessed and referenced bits. */
809 pmdp_test_and_clear_young(vma, addr, pmd);
810 ClearPageReferenced(page);
811 out:
812 spin_unlock(ptl);
813 return 0;
816 if (pmd_trans_unstable(pmd))
817 return 0;
819 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
820 for (; addr != end; pte++, addr += PAGE_SIZE) {
821 ptent = *pte;
823 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
824 clear_soft_dirty(vma, addr, pte);
825 continue;
828 if (!pte_present(ptent))
829 continue;
831 page = vm_normal_page(vma, addr, ptent);
832 if (!page)
833 continue;
835 /* Clear accessed and referenced bits. */
836 ptep_test_and_clear_young(vma, addr, pte);
837 ClearPageReferenced(page);
839 pte_unmap_unlock(pte - 1, ptl);
840 cond_resched();
841 return 0;
844 static int clear_refs_test_walk(unsigned long start, unsigned long end,
845 struct mm_walk *walk)
847 struct clear_refs_private *cp = walk->private;
848 struct vm_area_struct *vma = walk->vma;
850 if (vma->vm_flags & VM_PFNMAP)
851 return 1;
854 * Writing 1 to /proc/pid/clear_refs affects all pages.
855 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
856 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
857 * Writing 4 to /proc/pid/clear_refs affects all pages.
859 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
860 return 1;
861 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
862 return 1;
863 return 0;
866 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
867 size_t count, loff_t *ppos)
869 struct task_struct *task;
870 char buffer[PROC_NUMBUF];
871 struct mm_struct *mm;
872 struct vm_area_struct *vma;
873 enum clear_refs_types type;
874 int itype;
875 int rv;
877 memset(buffer, 0, sizeof(buffer));
878 if (count > sizeof(buffer) - 1)
879 count = sizeof(buffer) - 1;
880 if (copy_from_user(buffer, buf, count))
881 return -EFAULT;
882 rv = kstrtoint(strstrip(buffer), 10, &itype);
883 if (rv < 0)
884 return rv;
885 type = (enum clear_refs_types)itype;
886 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
887 return -EINVAL;
889 task = get_proc_task(file_inode(file));
890 if (!task)
891 return -ESRCH;
892 mm = get_task_mm(task);
893 if (mm) {
894 struct clear_refs_private cp = {
895 .type = type,
897 struct mm_walk clear_refs_walk = {
898 .pmd_entry = clear_refs_pte_range,
899 .test_walk = clear_refs_test_walk,
900 .mm = mm,
901 .private = &cp,
904 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
906 * Writing 5 to /proc/pid/clear_refs resets the peak
907 * resident set size to this mm's current rss value.
909 down_write(&mm->mmap_sem);
910 reset_mm_hiwater_rss(mm);
911 up_write(&mm->mmap_sem);
912 goto out_mm;
915 down_read(&mm->mmap_sem);
916 if (type == CLEAR_REFS_SOFT_DIRTY) {
917 for (vma = mm->mmap; vma; vma = vma->vm_next) {
918 if (!(vma->vm_flags & VM_SOFTDIRTY))
919 continue;
920 up_read(&mm->mmap_sem);
921 down_write(&mm->mmap_sem);
922 for (vma = mm->mmap; vma; vma = vma->vm_next) {
923 vma->vm_flags &= ~VM_SOFTDIRTY;
924 vma_set_page_prot(vma);
926 downgrade_write(&mm->mmap_sem);
927 break;
929 mmu_notifier_invalidate_range_start(mm, 0, -1);
931 walk_page_range(0, ~0UL, &clear_refs_walk);
932 if (type == CLEAR_REFS_SOFT_DIRTY)
933 mmu_notifier_invalidate_range_end(mm, 0, -1);
934 flush_tlb_mm(mm);
935 up_read(&mm->mmap_sem);
936 out_mm:
937 mmput(mm);
939 put_task_struct(task);
941 return count;
944 const struct file_operations proc_clear_refs_operations = {
945 .write = clear_refs_write,
946 .llseek = noop_llseek,
949 typedef struct {
950 u64 pme;
951 } pagemap_entry_t;
953 struct pagemapread {
954 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
955 pagemap_entry_t *buffer;
956 bool show_pfn;
959 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
960 #define PAGEMAP_WALK_MASK (PMD_MASK)
962 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
963 #define PM_PFRAME_BITS 55
964 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
965 #define PM_SOFT_DIRTY BIT_ULL(55)
966 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
967 #define PM_FILE BIT_ULL(61)
968 #define PM_SWAP BIT_ULL(62)
969 #define PM_PRESENT BIT_ULL(63)
971 #define PM_END_OF_BUFFER 1
973 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
975 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
978 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
979 struct pagemapread *pm)
981 pm->buffer[pm->pos++] = *pme;
982 if (pm->pos >= pm->len)
983 return PM_END_OF_BUFFER;
984 return 0;
987 static int pagemap_pte_hole(unsigned long start, unsigned long end,
988 struct mm_walk *walk)
990 struct pagemapread *pm = walk->private;
991 unsigned long addr = start;
992 int err = 0;
994 while (addr < end) {
995 struct vm_area_struct *vma = find_vma(walk->mm, addr);
996 pagemap_entry_t pme = make_pme(0, 0);
997 /* End of address space hole, which we mark as non-present. */
998 unsigned long hole_end;
1000 if (vma)
1001 hole_end = min(end, vma->vm_start);
1002 else
1003 hole_end = end;
1005 for (; addr < hole_end; addr += PAGE_SIZE) {
1006 err = add_to_pagemap(addr, &pme, pm);
1007 if (err)
1008 goto out;
1011 if (!vma)
1012 break;
1014 /* Addresses in the VMA. */
1015 if (vma->vm_flags & VM_SOFTDIRTY)
1016 pme = make_pme(0, PM_SOFT_DIRTY);
1017 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1018 err = add_to_pagemap(addr, &pme, pm);
1019 if (err)
1020 goto out;
1023 out:
1024 return err;
1027 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1028 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1030 u64 frame = 0, flags = 0;
1031 struct page *page = NULL;
1033 if (pte_present(pte)) {
1034 if (pm->show_pfn)
1035 frame = pte_pfn(pte);
1036 flags |= PM_PRESENT;
1037 page = vm_normal_page(vma, addr, pte);
1038 if (pte_soft_dirty(pte))
1039 flags |= PM_SOFT_DIRTY;
1040 } else if (is_swap_pte(pte)) {
1041 swp_entry_t entry;
1042 if (pte_swp_soft_dirty(pte))
1043 flags |= PM_SOFT_DIRTY;
1044 entry = pte_to_swp_entry(pte);
1045 frame = swp_type(entry) |
1046 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1047 flags |= PM_SWAP;
1048 if (is_migration_entry(entry))
1049 page = migration_entry_to_page(entry);
1052 if (page && !PageAnon(page))
1053 flags |= PM_FILE;
1054 if (page && page_mapcount(page) == 1)
1055 flags |= PM_MMAP_EXCLUSIVE;
1056 if (vma->vm_flags & VM_SOFTDIRTY)
1057 flags |= PM_SOFT_DIRTY;
1059 return make_pme(frame, flags);
1062 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1063 struct mm_walk *walk)
1065 struct vm_area_struct *vma = walk->vma;
1066 struct pagemapread *pm = walk->private;
1067 spinlock_t *ptl;
1068 pte_t *pte, *orig_pte;
1069 int err = 0;
1071 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1072 if (pmd_trans_huge_lock(pmdp, vma, &ptl) == 1) {
1073 u64 flags = 0, frame = 0;
1074 pmd_t pmd = *pmdp;
1076 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
1077 flags |= PM_SOFT_DIRTY;
1080 * Currently pmd for thp is always present because thp
1081 * can not be swapped-out, migrated, or HWPOISONed
1082 * (split in such cases instead.)
1083 * This if-check is just to prepare for future implementation.
1085 if (pmd_present(pmd)) {
1086 struct page *page = pmd_page(pmd);
1088 if (page_mapcount(page) == 1)
1089 flags |= PM_MMAP_EXCLUSIVE;
1091 flags |= PM_PRESENT;
1092 if (pm->show_pfn)
1093 frame = pmd_pfn(pmd) +
1094 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1097 for (; addr != end; addr += PAGE_SIZE) {
1098 pagemap_entry_t pme = make_pme(frame, flags);
1100 err = add_to_pagemap(addr, &pme, pm);
1101 if (err)
1102 break;
1103 if (pm->show_pfn && (flags & PM_PRESENT))
1104 frame++;
1106 spin_unlock(ptl);
1107 return err;
1110 if (pmd_trans_unstable(pmdp))
1111 return 0;
1112 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1115 * We can assume that @vma always points to a valid one and @end never
1116 * goes beyond vma->vm_end.
1118 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1119 for (; addr < end; pte++, addr += PAGE_SIZE) {
1120 pagemap_entry_t pme;
1122 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1123 err = add_to_pagemap(addr, &pme, pm);
1124 if (err)
1125 break;
1127 pte_unmap_unlock(orig_pte, ptl);
1129 cond_resched();
1131 return err;
1134 #ifdef CONFIG_HUGETLB_PAGE
1135 /* This function walks within one hugetlb entry in the single call */
1136 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1137 unsigned long addr, unsigned long end,
1138 struct mm_walk *walk)
1140 struct pagemapread *pm = walk->private;
1141 struct vm_area_struct *vma = walk->vma;
1142 u64 flags = 0, frame = 0;
1143 int err = 0;
1144 pte_t pte;
1146 if (vma->vm_flags & VM_SOFTDIRTY)
1147 flags |= PM_SOFT_DIRTY;
1149 pte = huge_ptep_get(ptep);
1150 if (pte_present(pte)) {
1151 struct page *page = pte_page(pte);
1153 if (!PageAnon(page))
1154 flags |= PM_FILE;
1156 if (page_mapcount(page) == 1)
1157 flags |= PM_MMAP_EXCLUSIVE;
1159 flags |= PM_PRESENT;
1160 if (pm->show_pfn)
1161 frame = pte_pfn(pte) +
1162 ((addr & ~hmask) >> PAGE_SHIFT);
1165 for (; addr != end; addr += PAGE_SIZE) {
1166 pagemap_entry_t pme = make_pme(frame, flags);
1168 err = add_to_pagemap(addr, &pme, pm);
1169 if (err)
1170 return err;
1171 if (pm->show_pfn && (flags & PM_PRESENT))
1172 frame++;
1175 cond_resched();
1177 return err;
1179 #endif /* HUGETLB_PAGE */
1182 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1184 * For each page in the address space, this file contains one 64-bit entry
1185 * consisting of the following:
1187 * Bits 0-54 page frame number (PFN) if present
1188 * Bits 0-4 swap type if swapped
1189 * Bits 5-54 swap offset if swapped
1190 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1191 * Bit 56 page exclusively mapped
1192 * Bits 57-60 zero
1193 * Bit 61 page is file-page or shared-anon
1194 * Bit 62 page swapped
1195 * Bit 63 page present
1197 * If the page is not present but in swap, then the PFN contains an
1198 * encoding of the swap file number and the page's offset into the
1199 * swap. Unmapped pages return a null PFN. This allows determining
1200 * precisely which pages are mapped (or in swap) and comparing mapped
1201 * pages between processes.
1203 * Efficient users of this interface will use /proc/pid/maps to
1204 * determine which areas of memory are actually mapped and llseek to
1205 * skip over unmapped regions.
1207 static ssize_t pagemap_read(struct file *file, char __user *buf,
1208 size_t count, loff_t *ppos)
1210 struct mm_struct *mm = file->private_data;
1211 struct pagemapread pm;
1212 struct mm_walk pagemap_walk = {};
1213 unsigned long src;
1214 unsigned long svpfn;
1215 unsigned long start_vaddr;
1216 unsigned long end_vaddr;
1217 int ret = 0, copied = 0;
1219 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
1220 goto out;
1222 ret = -EINVAL;
1223 /* file position must be aligned */
1224 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1225 goto out_mm;
1227 ret = 0;
1228 if (!count)
1229 goto out_mm;
1231 /* do not disclose physical addresses: attack vector */
1232 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1234 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1235 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1236 ret = -ENOMEM;
1237 if (!pm.buffer)
1238 goto out_mm;
1240 pagemap_walk.pmd_entry = pagemap_pmd_range;
1241 pagemap_walk.pte_hole = pagemap_pte_hole;
1242 #ifdef CONFIG_HUGETLB_PAGE
1243 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1244 #endif
1245 pagemap_walk.mm = mm;
1246 pagemap_walk.private = &pm;
1248 src = *ppos;
1249 svpfn = src / PM_ENTRY_BYTES;
1250 start_vaddr = svpfn << PAGE_SHIFT;
1251 end_vaddr = mm->task_size;
1253 /* watch out for wraparound */
1254 if (svpfn > mm->task_size >> PAGE_SHIFT)
1255 start_vaddr = end_vaddr;
1258 * The odds are that this will stop walking way
1259 * before end_vaddr, because the length of the
1260 * user buffer is tracked in "pm", and the walk
1261 * will stop when we hit the end of the buffer.
1263 ret = 0;
1264 while (count && (start_vaddr < end_vaddr)) {
1265 int len;
1266 unsigned long end;
1268 pm.pos = 0;
1269 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1270 /* overflow ? */
1271 if (end < start_vaddr || end > end_vaddr)
1272 end = end_vaddr;
1273 down_read(&mm->mmap_sem);
1274 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1275 up_read(&mm->mmap_sem);
1276 start_vaddr = end;
1278 len = min(count, PM_ENTRY_BYTES * pm.pos);
1279 if (copy_to_user(buf, pm.buffer, len)) {
1280 ret = -EFAULT;
1281 goto out_free;
1283 copied += len;
1284 buf += len;
1285 count -= len;
1287 *ppos += copied;
1288 if (!ret || ret == PM_END_OF_BUFFER)
1289 ret = copied;
1291 out_free:
1292 kfree(pm.buffer);
1293 out_mm:
1294 mmput(mm);
1295 out:
1296 return ret;
1299 static int pagemap_open(struct inode *inode, struct file *file)
1301 struct mm_struct *mm;
1303 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1304 if (IS_ERR(mm))
1305 return PTR_ERR(mm);
1306 file->private_data = mm;
1307 return 0;
1310 static int pagemap_release(struct inode *inode, struct file *file)
1312 struct mm_struct *mm = file->private_data;
1314 if (mm)
1315 mmdrop(mm);
1316 return 0;
1319 const struct file_operations proc_pagemap_operations = {
1320 .llseek = mem_lseek, /* borrow this */
1321 .read = pagemap_read,
1322 .open = pagemap_open,
1323 .release = pagemap_release,
1325 #endif /* CONFIG_PROC_PAGE_MONITOR */
1327 #ifdef CONFIG_NUMA
1329 struct numa_maps {
1330 unsigned long pages;
1331 unsigned long anon;
1332 unsigned long active;
1333 unsigned long writeback;
1334 unsigned long mapcount_max;
1335 unsigned long dirty;
1336 unsigned long swapcache;
1337 unsigned long node[MAX_NUMNODES];
1340 struct numa_maps_private {
1341 struct proc_maps_private proc_maps;
1342 struct numa_maps md;
1345 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1346 unsigned long nr_pages)
1348 int count = page_mapcount(page);
1350 md->pages += nr_pages;
1351 if (pte_dirty || PageDirty(page))
1352 md->dirty += nr_pages;
1354 if (PageSwapCache(page))
1355 md->swapcache += nr_pages;
1357 if (PageActive(page) || PageUnevictable(page))
1358 md->active += nr_pages;
1360 if (PageWriteback(page))
1361 md->writeback += nr_pages;
1363 if (PageAnon(page))
1364 md->anon += nr_pages;
1366 if (count > md->mapcount_max)
1367 md->mapcount_max = count;
1369 md->node[page_to_nid(page)] += nr_pages;
1372 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1373 unsigned long addr)
1375 struct page *page;
1376 int nid;
1378 if (!pte_present(pte))
1379 return NULL;
1381 page = vm_normal_page(vma, addr, pte);
1382 if (!page)
1383 return NULL;
1385 if (PageReserved(page))
1386 return NULL;
1388 nid = page_to_nid(page);
1389 if (!node_isset(nid, node_states[N_MEMORY]))
1390 return NULL;
1392 return page;
1395 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1396 unsigned long end, struct mm_walk *walk)
1398 struct numa_maps *md = walk->private;
1399 struct vm_area_struct *vma = walk->vma;
1400 spinlock_t *ptl;
1401 pte_t *orig_pte;
1402 pte_t *pte;
1404 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
1405 pte_t huge_pte = *(pte_t *)pmd;
1406 struct page *page;
1408 page = can_gather_numa_stats(huge_pte, vma, addr);
1409 if (page)
1410 gather_stats(page, md, pte_dirty(huge_pte),
1411 HPAGE_PMD_SIZE/PAGE_SIZE);
1412 spin_unlock(ptl);
1413 return 0;
1416 if (pmd_trans_unstable(pmd))
1417 return 0;
1418 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1419 do {
1420 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1421 if (!page)
1422 continue;
1423 gather_stats(page, md, pte_dirty(*pte), 1);
1425 } while (pte++, addr += PAGE_SIZE, addr != end);
1426 pte_unmap_unlock(orig_pte, ptl);
1427 return 0;
1429 #ifdef CONFIG_HUGETLB_PAGE
1430 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1431 unsigned long addr, unsigned long end, struct mm_walk *walk)
1433 struct numa_maps *md;
1434 struct page *page;
1436 if (!pte_present(*pte))
1437 return 0;
1439 page = pte_page(*pte);
1440 if (!page)
1441 return 0;
1443 md = walk->private;
1444 gather_stats(page, md, pte_dirty(*pte), 1);
1445 return 0;
1448 #else
1449 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1450 unsigned long addr, unsigned long end, struct mm_walk *walk)
1452 return 0;
1454 #endif
1457 * Display pages allocated per node and memory policy via /proc.
1459 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1461 struct numa_maps_private *numa_priv = m->private;
1462 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1463 struct vm_area_struct *vma = v;
1464 struct numa_maps *md = &numa_priv->md;
1465 struct file *file = vma->vm_file;
1466 struct mm_struct *mm = vma->vm_mm;
1467 struct mm_walk walk = {
1468 .hugetlb_entry = gather_hugetlb_stats,
1469 .pmd_entry = gather_pte_stats,
1470 .private = md,
1471 .mm = mm,
1473 struct mempolicy *pol;
1474 char buffer[64];
1475 int nid;
1477 if (!mm)
1478 return 0;
1480 /* Ensure we start with an empty set of numa_maps statistics. */
1481 memset(md, 0, sizeof(*md));
1483 pol = __get_vma_policy(vma, vma->vm_start);
1484 if (pol) {
1485 mpol_to_str(buffer, sizeof(buffer), pol);
1486 mpol_cond_put(pol);
1487 } else {
1488 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1491 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1493 if (file) {
1494 seq_puts(m, " file=");
1495 seq_file_path(m, file, "\n\t= ");
1496 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1497 seq_puts(m, " heap");
1498 } else {
1499 pid_t tid = pid_of_stack(proc_priv, vma, is_pid);
1500 if (tid != 0) {
1502 * Thread stack in /proc/PID/task/TID/maps or
1503 * the main process stack.
1505 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1506 vma->vm_end >= mm->start_stack))
1507 seq_puts(m, " stack");
1508 else
1509 seq_printf(m, " stack:%d", tid);
1513 if (is_vm_hugetlb_page(vma))
1514 seq_puts(m, " huge");
1516 /* mmap_sem is held by m_start */
1517 walk_page_vma(vma, &walk);
1519 if (!md->pages)
1520 goto out;
1522 if (md->anon)
1523 seq_printf(m, " anon=%lu", md->anon);
1525 if (md->dirty)
1526 seq_printf(m, " dirty=%lu", md->dirty);
1528 if (md->pages != md->anon && md->pages != md->dirty)
1529 seq_printf(m, " mapped=%lu", md->pages);
1531 if (md->mapcount_max > 1)
1532 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1534 if (md->swapcache)
1535 seq_printf(m, " swapcache=%lu", md->swapcache);
1537 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1538 seq_printf(m, " active=%lu", md->active);
1540 if (md->writeback)
1541 seq_printf(m, " writeback=%lu", md->writeback);
1543 for_each_node_state(nid, N_MEMORY)
1544 if (md->node[nid])
1545 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1547 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1548 out:
1549 seq_putc(m, '\n');
1550 m_cache_vma(m, vma);
1551 return 0;
1554 static int show_pid_numa_map(struct seq_file *m, void *v)
1556 return show_numa_map(m, v, 1);
1559 static int show_tid_numa_map(struct seq_file *m, void *v)
1561 return show_numa_map(m, v, 0);
1564 static const struct seq_operations proc_pid_numa_maps_op = {
1565 .start = m_start,
1566 .next = m_next,
1567 .stop = m_stop,
1568 .show = show_pid_numa_map,
1571 static const struct seq_operations proc_tid_numa_maps_op = {
1572 .start = m_start,
1573 .next = m_next,
1574 .stop = m_stop,
1575 .show = show_tid_numa_map,
1578 static int numa_maps_open(struct inode *inode, struct file *file,
1579 const struct seq_operations *ops)
1581 return proc_maps_open(inode, file, ops,
1582 sizeof(struct numa_maps_private));
1585 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1587 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1590 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1592 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1595 const struct file_operations proc_pid_numa_maps_operations = {
1596 .open = pid_numa_maps_open,
1597 .read = seq_read,
1598 .llseek = seq_lseek,
1599 .release = proc_map_release,
1602 const struct file_operations proc_tid_numa_maps_operations = {
1603 .open = tid_numa_maps_open,
1604 .read = seq_read,
1605 .llseek = seq_lseek,
1606 .release = proc_map_release,
1608 #endif /* CONFIG_NUMA */