mm: add /proc/pid/smaps_rollup
[sfrench/cifs-2.6.git] / fs / proc / task_mmu.c
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/sched/mm.h>
15 #include <linux/swapops.h>
16 #include <linux/mmu_notifier.h>
17 #include <linux/page_idle.h>
18 #include <linux/shmem_fs.h>
19 #include <linux/uaccess.h>
20
21 #include <asm/elf.h>
22 #include <asm/tlb.h>
23 #include <asm/tlbflush.h>
24 #include "internal.h"
25
26 void task_mem(struct seq_file *m, struct mm_struct *mm)
27 {
28         unsigned long text, lib, swap, ptes, pmds, anon, file, shmem;
29         unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
30
31         anon = get_mm_counter(mm, MM_ANONPAGES);
32         file = get_mm_counter(mm, MM_FILEPAGES);
33         shmem = get_mm_counter(mm, MM_SHMEMPAGES);
34
35         /*
36          * Note: to minimize their overhead, mm maintains hiwater_vm and
37          * hiwater_rss only when about to *lower* total_vm or rss.  Any
38          * collector of these hiwater stats must therefore get total_vm
39          * and rss too, which will usually be the higher.  Barriers? not
40          * worth the effort, such snapshots can always be inconsistent.
41          */
42         hiwater_vm = total_vm = mm->total_vm;
43         if (hiwater_vm < mm->hiwater_vm)
44                 hiwater_vm = mm->hiwater_vm;
45         hiwater_rss = total_rss = anon + file + shmem;
46         if (hiwater_rss < mm->hiwater_rss)
47                 hiwater_rss = mm->hiwater_rss;
48
49         text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
50         lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
51         swap = get_mm_counter(mm, MM_SWAPENTS);
52         ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
53         pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
54         seq_printf(m,
55                 "VmPeak:\t%8lu kB\n"
56                 "VmSize:\t%8lu kB\n"
57                 "VmLck:\t%8lu kB\n"
58                 "VmPin:\t%8lu kB\n"
59                 "VmHWM:\t%8lu kB\n"
60                 "VmRSS:\t%8lu kB\n"
61                 "RssAnon:\t%8lu kB\n"
62                 "RssFile:\t%8lu kB\n"
63                 "RssShmem:\t%8lu kB\n"
64                 "VmData:\t%8lu kB\n"
65                 "VmStk:\t%8lu kB\n"
66                 "VmExe:\t%8lu kB\n"
67                 "VmLib:\t%8lu kB\n"
68                 "VmPTE:\t%8lu kB\n"
69                 "VmPMD:\t%8lu kB\n"
70                 "VmSwap:\t%8lu kB\n",
71                 hiwater_vm << (PAGE_SHIFT-10),
72                 total_vm << (PAGE_SHIFT-10),
73                 mm->locked_vm << (PAGE_SHIFT-10),
74                 mm->pinned_vm << (PAGE_SHIFT-10),
75                 hiwater_rss << (PAGE_SHIFT-10),
76                 total_rss << (PAGE_SHIFT-10),
77                 anon << (PAGE_SHIFT-10),
78                 file << (PAGE_SHIFT-10),
79                 shmem << (PAGE_SHIFT-10),
80                 mm->data_vm << (PAGE_SHIFT-10),
81                 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
82                 ptes >> 10,
83                 pmds >> 10,
84                 swap << (PAGE_SHIFT-10));
85         hugetlb_report_usage(m, mm);
86 }
87
88 unsigned long task_vsize(struct mm_struct *mm)
89 {
90         return PAGE_SIZE * mm->total_vm;
91 }
92
93 unsigned long task_statm(struct mm_struct *mm,
94                          unsigned long *shared, unsigned long *text,
95                          unsigned long *data, unsigned long *resident)
96 {
97         *shared = get_mm_counter(mm, MM_FILEPAGES) +
98                         get_mm_counter(mm, MM_SHMEMPAGES);
99         *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
100                                                                 >> PAGE_SHIFT;
101         *data = mm->data_vm + mm->stack_vm;
102         *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
103         return mm->total_vm;
104 }
105
106 #ifdef CONFIG_NUMA
107 /*
108  * Save get_task_policy() for show_numa_map().
109  */
110 static void hold_task_mempolicy(struct proc_maps_private *priv)
111 {
112         struct task_struct *task = priv->task;
113
114         task_lock(task);
115         priv->task_mempolicy = get_task_policy(task);
116         mpol_get(priv->task_mempolicy);
117         task_unlock(task);
118 }
119 static void release_task_mempolicy(struct proc_maps_private *priv)
120 {
121         mpol_put(priv->task_mempolicy);
122 }
123 #else
124 static void hold_task_mempolicy(struct proc_maps_private *priv)
125 {
126 }
127 static void release_task_mempolicy(struct proc_maps_private *priv)
128 {
129 }
130 #endif
131
132 static void vma_stop(struct proc_maps_private *priv)
133 {
134         struct mm_struct *mm = priv->mm;
135
136         release_task_mempolicy(priv);
137         up_read(&mm->mmap_sem);
138         mmput(mm);
139 }
140
141 static struct vm_area_struct *
142 m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
143 {
144         if (vma == priv->tail_vma)
145                 return NULL;
146         return vma->vm_next ?: priv->tail_vma;
147 }
148
149 static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
150 {
151         if (m->count < m->size) /* vma is copied successfully */
152                 m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL;
153 }
154
155 static void *m_start(struct seq_file *m, loff_t *ppos)
156 {
157         struct proc_maps_private *priv = m->private;
158         unsigned long last_addr = m->version;
159         struct mm_struct *mm;
160         struct vm_area_struct *vma;
161         unsigned int pos = *ppos;
162
163         /* See m_cache_vma(). Zero at the start or after lseek. */
164         if (last_addr == -1UL)
165                 return NULL;
166
167         priv->task = get_proc_task(priv->inode);
168         if (!priv->task)
169                 return ERR_PTR(-ESRCH);
170
171         mm = priv->mm;
172         if (!mm || !mmget_not_zero(mm))
173                 return NULL;
174
175         down_read(&mm->mmap_sem);
176         hold_task_mempolicy(priv);
177         priv->tail_vma = get_gate_vma(mm);
178
179         if (last_addr) {
180                 vma = find_vma(mm, last_addr - 1);
181                 if (vma && vma->vm_start <= last_addr)
182                         vma = m_next_vma(priv, vma);
183                 if (vma)
184                         return vma;
185         }
186
187         m->version = 0;
188         if (pos < mm->map_count) {
189                 for (vma = mm->mmap; pos; pos--) {
190                         m->version = vma->vm_start;
191                         vma = vma->vm_next;
192                 }
193                 return vma;
194         }
195
196         /* we do not bother to update m->version in this case */
197         if (pos == mm->map_count && priv->tail_vma)
198                 return priv->tail_vma;
199
200         vma_stop(priv);
201         return NULL;
202 }
203
204 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
205 {
206         struct proc_maps_private *priv = m->private;
207         struct vm_area_struct *next;
208
209         (*pos)++;
210         next = m_next_vma(priv, v);
211         if (!next)
212                 vma_stop(priv);
213         return next;
214 }
215
216 static void m_stop(struct seq_file *m, void *v)
217 {
218         struct proc_maps_private *priv = m->private;
219
220         if (!IS_ERR_OR_NULL(v))
221                 vma_stop(priv);
222         if (priv->task) {
223                 put_task_struct(priv->task);
224                 priv->task = NULL;
225         }
226 }
227
228 static int proc_maps_open(struct inode *inode, struct file *file,
229                         const struct seq_operations *ops, int psize)
230 {
231         struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
232
233         if (!priv)
234                 return -ENOMEM;
235
236         priv->inode = inode;
237         priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
238         if (IS_ERR(priv->mm)) {
239                 int err = PTR_ERR(priv->mm);
240
241                 seq_release_private(inode, file);
242                 return err;
243         }
244
245         return 0;
246 }
247
248 static int proc_map_release(struct inode *inode, struct file *file)
249 {
250         struct seq_file *seq = file->private_data;
251         struct proc_maps_private *priv = seq->private;
252
253         if (priv->mm)
254                 mmdrop(priv->mm);
255
256         kfree(priv->rollup);
257         return seq_release_private(inode, file);
258 }
259
260 static int do_maps_open(struct inode *inode, struct file *file,
261                         const struct seq_operations *ops)
262 {
263         return proc_maps_open(inode, file, ops,
264                                 sizeof(struct proc_maps_private));
265 }
266
267 /*
268  * Indicate if the VMA is a stack for the given task; for
269  * /proc/PID/maps that is the stack of the main task.
270  */
271 static int is_stack(struct proc_maps_private *priv,
272                     struct vm_area_struct *vma)
273 {
274         /*
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.
278          */
279         return vma->vm_start <= vma->vm_mm->start_stack &&
280                 vma->vm_end >= vma->vm_mm->start_stack;
281 }
282
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)
287 {
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);
298 }
299
300 static void
301 show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
302 {
303         struct mm_struct *mm = vma->vm_mm;
304         struct file *file = vma->vm_file;
305         struct proc_maps_private *priv = m->private;
306         vm_flags_t flags = vma->vm_flags;
307         unsigned long ino = 0;
308         unsigned long long pgoff = 0;
309         unsigned long start, end;
310         dev_t dev = 0;
311         const char *name = NULL;
312
313         if (file) {
314                 struct inode *inode = file_inode(vma->vm_file);
315                 dev = inode->i_sb->s_dev;
316                 ino = inode->i_ino;
317                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
318         }
319
320         start = vma->vm_start;
321         end = vma->vm_end;
322         show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
323
324         /*
325          * Print the dentry name for named mappings, and a
326          * special [heap] marker for the heap:
327          */
328         if (file) {
329                 seq_pad(m, ' ');
330                 seq_file_path(m, file, "\n");
331                 goto done;
332         }
333
334         if (vma->vm_ops && vma->vm_ops->name) {
335                 name = vma->vm_ops->name(vma);
336                 if (name)
337                         goto done;
338         }
339
340         name = arch_vma_name(vma);
341         if (!name) {
342                 if (!mm) {
343                         name = "[vdso]";
344                         goto done;
345                 }
346
347                 if (vma->vm_start <= mm->brk &&
348                     vma->vm_end >= mm->start_brk) {
349                         name = "[heap]";
350                         goto done;
351                 }
352
353                 if (is_stack(priv, vma))
354                         name = "[stack]";
355         }
356
357 done:
358         if (name) {
359                 seq_pad(m, ' ');
360                 seq_puts(m, name);
361         }
362         seq_putc(m, '\n');
363 }
364
365 static int show_map(struct seq_file *m, void *v, int is_pid)
366 {
367         show_map_vma(m, v, is_pid);
368         m_cache_vma(m, v);
369         return 0;
370 }
371
372 static int show_pid_map(struct seq_file *m, void *v)
373 {
374         return show_map(m, v, 1);
375 }
376
377 static int show_tid_map(struct seq_file *m, void *v)
378 {
379         return show_map(m, v, 0);
380 }
381
382 static const struct seq_operations proc_pid_maps_op = {
383         .start  = m_start,
384         .next   = m_next,
385         .stop   = m_stop,
386         .show   = show_pid_map
387 };
388
389 static const struct seq_operations proc_tid_maps_op = {
390         .start  = m_start,
391         .next   = m_next,
392         .stop   = m_stop,
393         .show   = show_tid_map
394 };
395
396 static int pid_maps_open(struct inode *inode, struct file *file)
397 {
398         return do_maps_open(inode, file, &proc_pid_maps_op);
399 }
400
401 static int tid_maps_open(struct inode *inode, struct file *file)
402 {
403         return do_maps_open(inode, file, &proc_tid_maps_op);
404 }
405
406 const struct file_operations proc_pid_maps_operations = {
407         .open           = pid_maps_open,
408         .read           = seq_read,
409         .llseek         = seq_lseek,
410         .release        = proc_map_release,
411 };
412
413 const struct file_operations proc_tid_maps_operations = {
414         .open           = tid_maps_open,
415         .read           = seq_read,
416         .llseek         = seq_lseek,
417         .release        = proc_map_release,
418 };
419
420 /*
421  * Proportional Set Size(PSS): my share of RSS.
422  *
423  * PSS of a process is the count of pages it has in memory, where each
424  * page is divided by the number of processes sharing it.  So if a
425  * process has 1000 pages all to itself, and 1000 shared with one other
426  * process, its PSS will be 1500.
427  *
428  * To keep (accumulated) division errors low, we adopt a 64bit
429  * fixed-point pss counter to minimize division errors. So (pss >>
430  * PSS_SHIFT) would be the real byte count.
431  *
432  * A shift of 12 before division means (assuming 4K page size):
433  *      - 1M 3-user-pages add up to 8KB errors;
434  *      - supports mapcount up to 2^24, or 16M;
435  *      - supports PSS up to 2^52 bytes, or 4PB.
436  */
437 #define PSS_SHIFT 12
438
439 #ifdef CONFIG_PROC_PAGE_MONITOR
440 struct mem_size_stats {
441         bool first;
442         unsigned long resident;
443         unsigned long shared_clean;
444         unsigned long shared_dirty;
445         unsigned long private_clean;
446         unsigned long private_dirty;
447         unsigned long referenced;
448         unsigned long anonymous;
449         unsigned long lazyfree;
450         unsigned long anonymous_thp;
451         unsigned long shmem_thp;
452         unsigned long swap;
453         unsigned long shared_hugetlb;
454         unsigned long private_hugetlb;
455         unsigned long first_vma_start;
456         u64 pss;
457         u64 pss_locked;
458         u64 swap_pss;
459         bool check_shmem_swap;
460 };
461
462 static void smaps_account(struct mem_size_stats *mss, struct page *page,
463                 bool compound, bool young, bool dirty)
464 {
465         int i, nr = compound ? 1 << compound_order(page) : 1;
466         unsigned long size = nr * PAGE_SIZE;
467
468         if (PageAnon(page)) {
469                 mss->anonymous += size;
470                 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
471                         mss->lazyfree += size;
472         }
473
474         mss->resident += size;
475         /* Accumulate the size in pages that have been accessed. */
476         if (young || page_is_young(page) || PageReferenced(page))
477                 mss->referenced += size;
478
479         /*
480          * page_count(page) == 1 guarantees the page is mapped exactly once.
481          * If any subpage of the compound page mapped with PTE it would elevate
482          * page_count().
483          */
484         if (page_count(page) == 1) {
485                 if (dirty || PageDirty(page))
486                         mss->private_dirty += size;
487                 else
488                         mss->private_clean += size;
489                 mss->pss += (u64)size << PSS_SHIFT;
490                 return;
491         }
492
493         for (i = 0; i < nr; i++, page++) {
494                 int mapcount = page_mapcount(page);
495
496                 if (mapcount >= 2) {
497                         if (dirty || PageDirty(page))
498                                 mss->shared_dirty += PAGE_SIZE;
499                         else
500                                 mss->shared_clean += PAGE_SIZE;
501                         mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
502                 } else {
503                         if (dirty || PageDirty(page))
504                                 mss->private_dirty += PAGE_SIZE;
505                         else
506                                 mss->private_clean += PAGE_SIZE;
507                         mss->pss += PAGE_SIZE << PSS_SHIFT;
508                 }
509         }
510 }
511
512 #ifdef CONFIG_SHMEM
513 static int smaps_pte_hole(unsigned long addr, unsigned long end,
514                 struct mm_walk *walk)
515 {
516         struct mem_size_stats *mss = walk->private;
517
518         mss->swap += shmem_partial_swap_usage(
519                         walk->vma->vm_file->f_mapping, addr, end);
520
521         return 0;
522 }
523 #endif
524
525 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
526                 struct mm_walk *walk)
527 {
528         struct mem_size_stats *mss = walk->private;
529         struct vm_area_struct *vma = walk->vma;
530         struct page *page = NULL;
531
532         if (pte_present(*pte)) {
533                 page = vm_normal_page(vma, addr, *pte);
534         } else if (is_swap_pte(*pte)) {
535                 swp_entry_t swpent = pte_to_swp_entry(*pte);
536
537                 if (!non_swap_entry(swpent)) {
538                         int mapcount;
539
540                         mss->swap += PAGE_SIZE;
541                         mapcount = swp_swapcount(swpent);
542                         if (mapcount >= 2) {
543                                 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
544
545                                 do_div(pss_delta, mapcount);
546                                 mss->swap_pss += pss_delta;
547                         } else {
548                                 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
549                         }
550                 } else if (is_migration_entry(swpent))
551                         page = migration_entry_to_page(swpent);
552         } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
553                                                         && pte_none(*pte))) {
554                 page = find_get_entry(vma->vm_file->f_mapping,
555                                                 linear_page_index(vma, addr));
556                 if (!page)
557                         return;
558
559                 if (radix_tree_exceptional_entry(page))
560                         mss->swap += PAGE_SIZE;
561                 else
562                         put_page(page);
563
564                 return;
565         }
566
567         if (!page)
568                 return;
569
570         smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
571 }
572
573 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
574 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
575                 struct mm_walk *walk)
576 {
577         struct mem_size_stats *mss = walk->private;
578         struct vm_area_struct *vma = walk->vma;
579         struct page *page;
580
581         /* FOLL_DUMP will return -EFAULT on huge zero page */
582         page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
583         if (IS_ERR_OR_NULL(page))
584                 return;
585         if (PageAnon(page))
586                 mss->anonymous_thp += HPAGE_PMD_SIZE;
587         else if (PageSwapBacked(page))
588                 mss->shmem_thp += HPAGE_PMD_SIZE;
589         else if (is_zone_device_page(page))
590                 /* pass */;
591         else
592                 VM_BUG_ON_PAGE(1, page);
593         smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
594 }
595 #else
596 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
597                 struct mm_walk *walk)
598 {
599 }
600 #endif
601
602 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
603                            struct mm_walk *walk)
604 {
605         struct vm_area_struct *vma = walk->vma;
606         pte_t *pte;
607         spinlock_t *ptl;
608
609         ptl = pmd_trans_huge_lock(pmd, vma);
610         if (ptl) {
611                 smaps_pmd_entry(pmd, addr, walk);
612                 spin_unlock(ptl);
613                 return 0;
614         }
615
616         if (pmd_trans_unstable(pmd))
617                 return 0;
618         /*
619          * The mmap_sem held all the way back in m_start() is what
620          * keeps khugepaged out of here and from collapsing things
621          * in here.
622          */
623         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
624         for (; addr != end; pte++, addr += PAGE_SIZE)
625                 smaps_pte_entry(pte, addr, walk);
626         pte_unmap_unlock(pte - 1, ptl);
627         cond_resched();
628         return 0;
629 }
630
631 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
632 {
633         /*
634          * Don't forget to update Documentation/ on changes.
635          */
636         static const char mnemonics[BITS_PER_LONG][2] = {
637                 /*
638                  * In case if we meet a flag we don't know about.
639                  */
640                 [0 ... (BITS_PER_LONG-1)] = "??",
641
642                 [ilog2(VM_READ)]        = "rd",
643                 [ilog2(VM_WRITE)]       = "wr",
644                 [ilog2(VM_EXEC)]        = "ex",
645                 [ilog2(VM_SHARED)]      = "sh",
646                 [ilog2(VM_MAYREAD)]     = "mr",
647                 [ilog2(VM_MAYWRITE)]    = "mw",
648                 [ilog2(VM_MAYEXEC)]     = "me",
649                 [ilog2(VM_MAYSHARE)]    = "ms",
650                 [ilog2(VM_GROWSDOWN)]   = "gd",
651                 [ilog2(VM_PFNMAP)]      = "pf",
652                 [ilog2(VM_DENYWRITE)]   = "dw",
653 #ifdef CONFIG_X86_INTEL_MPX
654                 [ilog2(VM_MPX)]         = "mp",
655 #endif
656                 [ilog2(VM_LOCKED)]      = "lo",
657                 [ilog2(VM_IO)]          = "io",
658                 [ilog2(VM_SEQ_READ)]    = "sr",
659                 [ilog2(VM_RAND_READ)]   = "rr",
660                 [ilog2(VM_DONTCOPY)]    = "dc",
661                 [ilog2(VM_DONTEXPAND)]  = "de",
662                 [ilog2(VM_ACCOUNT)]     = "ac",
663                 [ilog2(VM_NORESERVE)]   = "nr",
664                 [ilog2(VM_HUGETLB)]     = "ht",
665                 [ilog2(VM_ARCH_1)]      = "ar",
666                 [ilog2(VM_DONTDUMP)]    = "dd",
667 #ifdef CONFIG_MEM_SOFT_DIRTY
668                 [ilog2(VM_SOFTDIRTY)]   = "sd",
669 #endif
670                 [ilog2(VM_MIXEDMAP)]    = "mm",
671                 [ilog2(VM_HUGEPAGE)]    = "hg",
672                 [ilog2(VM_NOHUGEPAGE)]  = "nh",
673                 [ilog2(VM_MERGEABLE)]   = "mg",
674                 [ilog2(VM_UFFD_MISSING)]= "um",
675                 [ilog2(VM_UFFD_WP)]     = "uw",
676 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
677                 /* These come out via ProtectionKey: */
678                 [ilog2(VM_PKEY_BIT0)]   = "",
679                 [ilog2(VM_PKEY_BIT1)]   = "",
680                 [ilog2(VM_PKEY_BIT2)]   = "",
681                 [ilog2(VM_PKEY_BIT3)]   = "",
682 #endif
683         };
684         size_t i;
685
686         seq_puts(m, "VmFlags: ");
687         for (i = 0; i < BITS_PER_LONG; i++) {
688                 if (!mnemonics[i][0])
689                         continue;
690                 if (vma->vm_flags & (1UL << i)) {
691                         seq_printf(m, "%c%c ",
692                                    mnemonics[i][0], mnemonics[i][1]);
693                 }
694         }
695         seq_putc(m, '\n');
696 }
697
698 #ifdef CONFIG_HUGETLB_PAGE
699 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
700                                  unsigned long addr, unsigned long end,
701                                  struct mm_walk *walk)
702 {
703         struct mem_size_stats *mss = walk->private;
704         struct vm_area_struct *vma = walk->vma;
705         struct page *page = NULL;
706
707         if (pte_present(*pte)) {
708                 page = vm_normal_page(vma, addr, *pte);
709         } else if (is_swap_pte(*pte)) {
710                 swp_entry_t swpent = pte_to_swp_entry(*pte);
711
712                 if (is_migration_entry(swpent))
713                         page = migration_entry_to_page(swpent);
714         }
715         if (page) {
716                 int mapcount = page_mapcount(page);
717
718                 if (mapcount >= 2)
719                         mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
720                 else
721                         mss->private_hugetlb += huge_page_size(hstate_vma(vma));
722         }
723         return 0;
724 }
725 #endif /* HUGETLB_PAGE */
726
727 void __weak arch_show_smap(struct seq_file *m, struct vm_area_struct *vma)
728 {
729 }
730
731 static int show_smap(struct seq_file *m, void *v, int is_pid)
732 {
733         struct proc_maps_private *priv = m->private;
734         struct vm_area_struct *vma = v;
735         struct mem_size_stats mss_stack;
736         struct mem_size_stats *mss;
737         struct mm_walk smaps_walk = {
738                 .pmd_entry = smaps_pte_range,
739 #ifdef CONFIG_HUGETLB_PAGE
740                 .hugetlb_entry = smaps_hugetlb_range,
741 #endif
742                 .mm = vma->vm_mm,
743         };
744         int ret = 0;
745         bool rollup_mode;
746         bool last_vma;
747
748         if (priv->rollup) {
749                 rollup_mode = true;
750                 mss = priv->rollup;
751                 if (mss->first) {
752                         mss->first_vma_start = vma->vm_start;
753                         mss->first = false;
754                 }
755                 last_vma = !m_next_vma(priv, vma);
756         } else {
757                 rollup_mode = false;
758                 memset(&mss_stack, 0, sizeof(mss_stack));
759                 mss = &mss_stack;
760         }
761
762         smaps_walk.private = mss;
763
764 #ifdef CONFIG_SHMEM
765         if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
766                 /*
767                  * For shared or readonly shmem mappings we know that all
768                  * swapped out pages belong to the shmem object, and we can
769                  * obtain the swap value much more efficiently. For private
770                  * writable mappings, we might have COW pages that are
771                  * not affected by the parent swapped out pages of the shmem
772                  * object, so we have to distinguish them during the page walk.
773                  * Unless we know that the shmem object (or the part mapped by
774                  * our VMA) has no swapped out pages at all.
775                  */
776                 unsigned long shmem_swapped = shmem_swap_usage(vma);
777
778                 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
779                                         !(vma->vm_flags & VM_WRITE)) {
780                         mss->swap = shmem_swapped;
781                 } else {
782                         mss->check_shmem_swap = true;
783                         smaps_walk.pte_hole = smaps_pte_hole;
784                 }
785         }
786 #endif
787
788         /* mmap_sem is held in m_start */
789         walk_page_vma(vma, &smaps_walk);
790         if (vma->vm_flags & VM_LOCKED)
791                 mss->pss_locked += mss->pss;
792
793         if (!rollup_mode) {
794                 show_map_vma(m, vma, is_pid);
795         } else if (last_vma) {
796                 show_vma_header_prefix(
797                         m, mss->first_vma_start, vma->vm_end, 0, 0, 0, 0);
798                 seq_pad(m, ' ');
799                 seq_puts(m, "[rollup]\n");
800         } else {
801                 ret = SEQ_SKIP;
802         }
803
804         if (!rollup_mode)
805                 seq_printf(m,
806                            "Size:           %8lu kB\n"
807                            "KernelPageSize: %8lu kB\n"
808                            "MMUPageSize:    %8lu kB\n",
809                            (vma->vm_end - vma->vm_start) >> 10,
810                            vma_kernel_pagesize(vma) >> 10,
811                            vma_mmu_pagesize(vma) >> 10);
812
813
814         if (!rollup_mode || last_vma)
815                 seq_printf(m,
816                            "Rss:            %8lu kB\n"
817                            "Pss:            %8lu kB\n"
818                            "Shared_Clean:   %8lu kB\n"
819                            "Shared_Dirty:   %8lu kB\n"
820                            "Private_Clean:  %8lu kB\n"
821                            "Private_Dirty:  %8lu kB\n"
822                            "Referenced:     %8lu kB\n"
823                            "Anonymous:      %8lu kB\n"
824                            "LazyFree:       %8lu kB\n"
825                            "AnonHugePages:  %8lu kB\n"
826                            "ShmemPmdMapped: %8lu kB\n"
827                            "Shared_Hugetlb: %8lu kB\n"
828                            "Private_Hugetlb: %7lu kB\n"
829                            "Swap:           %8lu kB\n"
830                            "SwapPss:        %8lu kB\n"
831                            "Locked:         %8lu kB\n",
832                            mss->resident >> 10,
833                            (unsigned long)(mss->pss >> (10 + PSS_SHIFT)),
834                            mss->shared_clean  >> 10,
835                            mss->shared_dirty  >> 10,
836                            mss->private_clean >> 10,
837                            mss->private_dirty >> 10,
838                            mss->referenced >> 10,
839                            mss->anonymous >> 10,
840                            mss->lazyfree >> 10,
841                            mss->anonymous_thp >> 10,
842                            mss->shmem_thp >> 10,
843                            mss->shared_hugetlb >> 10,
844                            mss->private_hugetlb >> 10,
845                            mss->swap >> 10,
846                            (unsigned long)(mss->swap_pss >> (10 + PSS_SHIFT)),
847                            (unsigned long)(mss->pss >> (10 + PSS_SHIFT)));
848
849         if (!rollup_mode) {
850                 arch_show_smap(m, vma);
851                 show_smap_vma_flags(m, vma);
852         }
853         m_cache_vma(m, vma);
854         return ret;
855 }
856
857 static int show_pid_smap(struct seq_file *m, void *v)
858 {
859         return show_smap(m, v, 1);
860 }
861
862 static int show_tid_smap(struct seq_file *m, void *v)
863 {
864         return show_smap(m, v, 0);
865 }
866
867 static const struct seq_operations proc_pid_smaps_op = {
868         .start  = m_start,
869         .next   = m_next,
870         .stop   = m_stop,
871         .show   = show_pid_smap
872 };
873
874 static const struct seq_operations proc_tid_smaps_op = {
875         .start  = m_start,
876         .next   = m_next,
877         .stop   = m_stop,
878         .show   = show_tid_smap
879 };
880
881 static int pid_smaps_open(struct inode *inode, struct file *file)
882 {
883         return do_maps_open(inode, file, &proc_pid_smaps_op);
884 }
885
886 static int pid_smaps_rollup_open(struct inode *inode, struct file *file)
887 {
888         struct seq_file *seq;
889         struct proc_maps_private *priv;
890         int ret = do_maps_open(inode, file, &proc_pid_smaps_op);
891
892         if (ret < 0)
893                 return ret;
894         seq = file->private_data;
895         priv = seq->private;
896         priv->rollup = kzalloc(sizeof(*priv->rollup), GFP_KERNEL);
897         if (!priv->rollup) {
898                 proc_map_release(inode, file);
899                 return -ENOMEM;
900         }
901         priv->rollup->first = true;
902         return 0;
903 }
904
905 static int tid_smaps_open(struct inode *inode, struct file *file)
906 {
907         return do_maps_open(inode, file, &proc_tid_smaps_op);
908 }
909
910 const struct file_operations proc_pid_smaps_operations = {
911         .open           = pid_smaps_open,
912         .read           = seq_read,
913         .llseek         = seq_lseek,
914         .release        = proc_map_release,
915 };
916
917 const struct file_operations proc_pid_smaps_rollup_operations = {
918         .open           = pid_smaps_rollup_open,
919         .read           = seq_read,
920         .llseek         = seq_lseek,
921         .release        = proc_map_release,
922 };
923
924 const struct file_operations proc_tid_smaps_operations = {
925         .open           = tid_smaps_open,
926         .read           = seq_read,
927         .llseek         = seq_lseek,
928         .release        = proc_map_release,
929 };
930
931 enum clear_refs_types {
932         CLEAR_REFS_ALL = 1,
933         CLEAR_REFS_ANON,
934         CLEAR_REFS_MAPPED,
935         CLEAR_REFS_SOFT_DIRTY,
936         CLEAR_REFS_MM_HIWATER_RSS,
937         CLEAR_REFS_LAST,
938 };
939
940 struct clear_refs_private {
941         enum clear_refs_types type;
942 };
943
944 #ifdef CONFIG_MEM_SOFT_DIRTY
945 static inline void clear_soft_dirty(struct vm_area_struct *vma,
946                 unsigned long addr, pte_t *pte)
947 {
948         /*
949          * The soft-dirty tracker uses #PF-s to catch writes
950          * to pages, so write-protect the pte as well. See the
951          * Documentation/vm/soft-dirty.txt for full description
952          * of how soft-dirty works.
953          */
954         pte_t ptent = *pte;
955
956         if (pte_present(ptent)) {
957                 ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte);
958                 ptent = pte_wrprotect(ptent);
959                 ptent = pte_clear_soft_dirty(ptent);
960                 ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent);
961         } else if (is_swap_pte(ptent)) {
962                 ptent = pte_swp_clear_soft_dirty(ptent);
963                 set_pte_at(vma->vm_mm, addr, pte, ptent);
964         }
965 }
966 #else
967 static inline void clear_soft_dirty(struct vm_area_struct *vma,
968                 unsigned long addr, pte_t *pte)
969 {
970 }
971 #endif
972
973 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
974 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
975                 unsigned long addr, pmd_t *pmdp)
976 {
977         pmd_t pmd = *pmdp;
978
979         /* See comment in change_huge_pmd() */
980         pmdp_invalidate(vma, addr, pmdp);
981         if (pmd_dirty(*pmdp))
982                 pmd = pmd_mkdirty(pmd);
983         if (pmd_young(*pmdp))
984                 pmd = pmd_mkyoung(pmd);
985
986         pmd = pmd_wrprotect(pmd);
987         pmd = pmd_clear_soft_dirty(pmd);
988
989         set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
990 }
991 #else
992 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
993                 unsigned long addr, pmd_t *pmdp)
994 {
995 }
996 #endif
997
998 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
999                                 unsigned long end, struct mm_walk *walk)
1000 {
1001         struct clear_refs_private *cp = walk->private;
1002         struct vm_area_struct *vma = walk->vma;
1003         pte_t *pte, ptent;
1004         spinlock_t *ptl;
1005         struct page *page;
1006
1007         ptl = pmd_trans_huge_lock(pmd, vma);
1008         if (ptl) {
1009                 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1010                         clear_soft_dirty_pmd(vma, addr, pmd);
1011                         goto out;
1012                 }
1013
1014                 page = pmd_page(*pmd);
1015
1016                 /* Clear accessed and referenced bits. */
1017                 pmdp_test_and_clear_young(vma, addr, pmd);
1018                 test_and_clear_page_young(page);
1019                 ClearPageReferenced(page);
1020 out:
1021                 spin_unlock(ptl);
1022                 return 0;
1023         }
1024
1025         if (pmd_trans_unstable(pmd))
1026                 return 0;
1027
1028         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1029         for (; addr != end; pte++, addr += PAGE_SIZE) {
1030                 ptent = *pte;
1031
1032                 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1033                         clear_soft_dirty(vma, addr, pte);
1034                         continue;
1035                 }
1036
1037                 if (!pte_present(ptent))
1038                         continue;
1039
1040                 page = vm_normal_page(vma, addr, ptent);
1041                 if (!page)
1042                         continue;
1043
1044                 /* Clear accessed and referenced bits. */
1045                 ptep_test_and_clear_young(vma, addr, pte);
1046                 test_and_clear_page_young(page);
1047                 ClearPageReferenced(page);
1048         }
1049         pte_unmap_unlock(pte - 1, ptl);
1050         cond_resched();
1051         return 0;
1052 }
1053
1054 static int clear_refs_test_walk(unsigned long start, unsigned long end,
1055                                 struct mm_walk *walk)
1056 {
1057         struct clear_refs_private *cp = walk->private;
1058         struct vm_area_struct *vma = walk->vma;
1059
1060         if (vma->vm_flags & VM_PFNMAP)
1061                 return 1;
1062
1063         /*
1064          * Writing 1 to /proc/pid/clear_refs affects all pages.
1065          * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1066          * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1067          * Writing 4 to /proc/pid/clear_refs affects all pages.
1068          */
1069         if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1070                 return 1;
1071         if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1072                 return 1;
1073         return 0;
1074 }
1075
1076 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1077                                 size_t count, loff_t *ppos)
1078 {
1079         struct task_struct *task;
1080         char buffer[PROC_NUMBUF];
1081         struct mm_struct *mm;
1082         struct vm_area_struct *vma;
1083         enum clear_refs_types type;
1084         struct mmu_gather tlb;
1085         int itype;
1086         int rv;
1087
1088         memset(buffer, 0, sizeof(buffer));
1089         if (count > sizeof(buffer) - 1)
1090                 count = sizeof(buffer) - 1;
1091         if (copy_from_user(buffer, buf, count))
1092                 return -EFAULT;
1093         rv = kstrtoint(strstrip(buffer), 10, &itype);
1094         if (rv < 0)
1095                 return rv;
1096         type = (enum clear_refs_types)itype;
1097         if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1098                 return -EINVAL;
1099
1100         task = get_proc_task(file_inode(file));
1101         if (!task)
1102                 return -ESRCH;
1103         mm = get_task_mm(task);
1104         if (mm) {
1105                 struct clear_refs_private cp = {
1106                         .type = type,
1107                 };
1108                 struct mm_walk clear_refs_walk = {
1109                         .pmd_entry = clear_refs_pte_range,
1110                         .test_walk = clear_refs_test_walk,
1111                         .mm = mm,
1112                         .private = &cp,
1113                 };
1114
1115                 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1116                         if (down_write_killable(&mm->mmap_sem)) {
1117                                 count = -EINTR;
1118                                 goto out_mm;
1119                         }
1120
1121                         /*
1122                          * Writing 5 to /proc/pid/clear_refs resets the peak
1123                          * resident set size to this mm's current rss value.
1124                          */
1125                         reset_mm_hiwater_rss(mm);
1126                         up_write(&mm->mmap_sem);
1127                         goto out_mm;
1128                 }
1129
1130                 down_read(&mm->mmap_sem);
1131                 tlb_gather_mmu(&tlb, mm, 0, -1);
1132                 if (type == CLEAR_REFS_SOFT_DIRTY) {
1133                         for (vma = mm->mmap; vma; vma = vma->vm_next) {
1134                                 if (!(vma->vm_flags & VM_SOFTDIRTY))
1135                                         continue;
1136                                 up_read(&mm->mmap_sem);
1137                                 if (down_write_killable(&mm->mmap_sem)) {
1138                                         count = -EINTR;
1139                                         goto out_mm;
1140                                 }
1141                                 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1142                                         vma->vm_flags &= ~VM_SOFTDIRTY;
1143                                         vma_set_page_prot(vma);
1144                                 }
1145                                 downgrade_write(&mm->mmap_sem);
1146                                 break;
1147                         }
1148                         mmu_notifier_invalidate_range_start(mm, 0, -1);
1149                 }
1150                 walk_page_range(0, mm->highest_vm_end, &clear_refs_walk);
1151                 if (type == CLEAR_REFS_SOFT_DIRTY)
1152                         mmu_notifier_invalidate_range_end(mm, 0, -1);
1153                 tlb_finish_mmu(&tlb, 0, -1);
1154                 up_read(&mm->mmap_sem);
1155 out_mm:
1156                 mmput(mm);
1157         }
1158         put_task_struct(task);
1159
1160         return count;
1161 }
1162
1163 const struct file_operations proc_clear_refs_operations = {
1164         .write          = clear_refs_write,
1165         .llseek         = noop_llseek,
1166 };
1167
1168 typedef struct {
1169         u64 pme;
1170 } pagemap_entry_t;
1171
1172 struct pagemapread {
1173         int pos, len;           /* units: PM_ENTRY_BYTES, not bytes */
1174         pagemap_entry_t *buffer;
1175         bool show_pfn;
1176 };
1177
1178 #define PAGEMAP_WALK_SIZE       (PMD_SIZE)
1179 #define PAGEMAP_WALK_MASK       (PMD_MASK)
1180
1181 #define PM_ENTRY_BYTES          sizeof(pagemap_entry_t)
1182 #define PM_PFRAME_BITS          55
1183 #define PM_PFRAME_MASK          GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1184 #define PM_SOFT_DIRTY           BIT_ULL(55)
1185 #define PM_MMAP_EXCLUSIVE       BIT_ULL(56)
1186 #define PM_FILE                 BIT_ULL(61)
1187 #define PM_SWAP                 BIT_ULL(62)
1188 #define PM_PRESENT              BIT_ULL(63)
1189
1190 #define PM_END_OF_BUFFER    1
1191
1192 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1193 {
1194         return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1195 }
1196
1197 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1198                           struct pagemapread *pm)
1199 {
1200         pm->buffer[pm->pos++] = *pme;
1201         if (pm->pos >= pm->len)
1202                 return PM_END_OF_BUFFER;
1203         return 0;
1204 }
1205
1206 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1207                                 struct mm_walk *walk)
1208 {
1209         struct pagemapread *pm = walk->private;
1210         unsigned long addr = start;
1211         int err = 0;
1212
1213         while (addr < end) {
1214                 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1215                 pagemap_entry_t pme = make_pme(0, 0);
1216                 /* End of address space hole, which we mark as non-present. */
1217                 unsigned long hole_end;
1218
1219                 if (vma)
1220                         hole_end = min(end, vma->vm_start);
1221                 else
1222                         hole_end = end;
1223
1224                 for (; addr < hole_end; addr += PAGE_SIZE) {
1225                         err = add_to_pagemap(addr, &pme, pm);
1226                         if (err)
1227                                 goto out;
1228                 }
1229
1230                 if (!vma)
1231                         break;
1232
1233                 /* Addresses in the VMA. */
1234                 if (vma->vm_flags & VM_SOFTDIRTY)
1235                         pme = make_pme(0, PM_SOFT_DIRTY);
1236                 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1237                         err = add_to_pagemap(addr, &pme, pm);
1238                         if (err)
1239                                 goto out;
1240                 }
1241         }
1242 out:
1243         return err;
1244 }
1245
1246 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1247                 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1248 {
1249         u64 frame = 0, flags = 0;
1250         struct page *page = NULL;
1251
1252         if (pte_present(pte)) {
1253                 if (pm->show_pfn)
1254                         frame = pte_pfn(pte);
1255                 flags |= PM_PRESENT;
1256                 page = vm_normal_page(vma, addr, pte);
1257                 if (pte_soft_dirty(pte))
1258                         flags |= PM_SOFT_DIRTY;
1259         } else if (is_swap_pte(pte)) {
1260                 swp_entry_t entry;
1261                 if (pte_swp_soft_dirty(pte))
1262                         flags |= PM_SOFT_DIRTY;
1263                 entry = pte_to_swp_entry(pte);
1264                 frame = swp_type(entry) |
1265                         (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1266                 flags |= PM_SWAP;
1267                 if (is_migration_entry(entry))
1268                         page = migration_entry_to_page(entry);
1269         }
1270
1271         if (page && !PageAnon(page))
1272                 flags |= PM_FILE;
1273         if (page && page_mapcount(page) == 1)
1274                 flags |= PM_MMAP_EXCLUSIVE;
1275         if (vma->vm_flags & VM_SOFTDIRTY)
1276                 flags |= PM_SOFT_DIRTY;
1277
1278         return make_pme(frame, flags);
1279 }
1280
1281 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1282                              struct mm_walk *walk)
1283 {
1284         struct vm_area_struct *vma = walk->vma;
1285         struct pagemapread *pm = walk->private;
1286         spinlock_t *ptl;
1287         pte_t *pte, *orig_pte;
1288         int err = 0;
1289
1290 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1291         ptl = pmd_trans_huge_lock(pmdp, vma);
1292         if (ptl) {
1293                 u64 flags = 0, frame = 0;
1294                 pmd_t pmd = *pmdp;
1295
1296                 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
1297                         flags |= PM_SOFT_DIRTY;
1298
1299                 /*
1300                  * Currently pmd for thp is always present because thp
1301                  * can not be swapped-out, migrated, or HWPOISONed
1302                  * (split in such cases instead.)
1303                  * This if-check is just to prepare for future implementation.
1304                  */
1305                 if (pmd_present(pmd)) {
1306                         struct page *page = pmd_page(pmd);
1307
1308                         if (page_mapcount(page) == 1)
1309                                 flags |= PM_MMAP_EXCLUSIVE;
1310
1311                         flags |= PM_PRESENT;
1312                         if (pm->show_pfn)
1313                                 frame = pmd_pfn(pmd) +
1314                                         ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1315                 }
1316
1317                 for (; addr != end; addr += PAGE_SIZE) {
1318                         pagemap_entry_t pme = make_pme(frame, flags);
1319
1320                         err = add_to_pagemap(addr, &pme, pm);
1321                         if (err)
1322                                 break;
1323                         if (pm->show_pfn && (flags & PM_PRESENT))
1324                                 frame++;
1325                 }
1326                 spin_unlock(ptl);
1327                 return err;
1328         }
1329
1330         if (pmd_trans_unstable(pmdp))
1331                 return 0;
1332 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1333
1334         /*
1335          * We can assume that @vma always points to a valid one and @end never
1336          * goes beyond vma->vm_end.
1337          */
1338         orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1339         for (; addr < end; pte++, addr += PAGE_SIZE) {
1340                 pagemap_entry_t pme;
1341
1342                 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1343                 err = add_to_pagemap(addr, &pme, pm);
1344                 if (err)
1345                         break;
1346         }
1347         pte_unmap_unlock(orig_pte, ptl);
1348
1349         cond_resched();
1350
1351         return err;
1352 }
1353
1354 #ifdef CONFIG_HUGETLB_PAGE
1355 /* This function walks within one hugetlb entry in the single call */
1356 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1357                                  unsigned long addr, unsigned long end,
1358                                  struct mm_walk *walk)
1359 {
1360         struct pagemapread *pm = walk->private;
1361         struct vm_area_struct *vma = walk->vma;
1362         u64 flags = 0, frame = 0;
1363         int err = 0;
1364         pte_t pte;
1365
1366         if (vma->vm_flags & VM_SOFTDIRTY)
1367                 flags |= PM_SOFT_DIRTY;
1368
1369         pte = huge_ptep_get(ptep);
1370         if (pte_present(pte)) {
1371                 struct page *page = pte_page(pte);
1372
1373                 if (!PageAnon(page))
1374                         flags |= PM_FILE;
1375
1376                 if (page_mapcount(page) == 1)
1377                         flags |= PM_MMAP_EXCLUSIVE;
1378
1379                 flags |= PM_PRESENT;
1380                 if (pm->show_pfn)
1381                         frame = pte_pfn(pte) +
1382                                 ((addr & ~hmask) >> PAGE_SHIFT);
1383         }
1384
1385         for (; addr != end; addr += PAGE_SIZE) {
1386                 pagemap_entry_t pme = make_pme(frame, flags);
1387
1388                 err = add_to_pagemap(addr, &pme, pm);
1389                 if (err)
1390                         return err;
1391                 if (pm->show_pfn && (flags & PM_PRESENT))
1392                         frame++;
1393         }
1394
1395         cond_resched();
1396
1397         return err;
1398 }
1399 #endif /* HUGETLB_PAGE */
1400
1401 /*
1402  * /proc/pid/pagemap - an array mapping virtual pages to pfns
1403  *
1404  * For each page in the address space, this file contains one 64-bit entry
1405  * consisting of the following:
1406  *
1407  * Bits 0-54  page frame number (PFN) if present
1408  * Bits 0-4   swap type if swapped
1409  * Bits 5-54  swap offset if swapped
1410  * Bit  55    pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1411  * Bit  56    page exclusively mapped
1412  * Bits 57-60 zero
1413  * Bit  61    page is file-page or shared-anon
1414  * Bit  62    page swapped
1415  * Bit  63    page present
1416  *
1417  * If the page is not present but in swap, then the PFN contains an
1418  * encoding of the swap file number and the page's offset into the
1419  * swap. Unmapped pages return a null PFN. This allows determining
1420  * precisely which pages are mapped (or in swap) and comparing mapped
1421  * pages between processes.
1422  *
1423  * Efficient users of this interface will use /proc/pid/maps to
1424  * determine which areas of memory are actually mapped and llseek to
1425  * skip over unmapped regions.
1426  */
1427 static ssize_t pagemap_read(struct file *file, char __user *buf,
1428                             size_t count, loff_t *ppos)
1429 {
1430         struct mm_struct *mm = file->private_data;
1431         struct pagemapread pm;
1432         struct mm_walk pagemap_walk = {};
1433         unsigned long src;
1434         unsigned long svpfn;
1435         unsigned long start_vaddr;
1436         unsigned long end_vaddr;
1437         int ret = 0, copied = 0;
1438
1439         if (!mm || !mmget_not_zero(mm))
1440                 goto out;
1441
1442         ret = -EINVAL;
1443         /* file position must be aligned */
1444         if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1445                 goto out_mm;
1446
1447         ret = 0;
1448         if (!count)
1449                 goto out_mm;
1450
1451         /* do not disclose physical addresses: attack vector */
1452         pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1453
1454         pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1455         pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1456         ret = -ENOMEM;
1457         if (!pm.buffer)
1458                 goto out_mm;
1459
1460         pagemap_walk.pmd_entry = pagemap_pmd_range;
1461         pagemap_walk.pte_hole = pagemap_pte_hole;
1462 #ifdef CONFIG_HUGETLB_PAGE
1463         pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1464 #endif
1465         pagemap_walk.mm = mm;
1466         pagemap_walk.private = &pm;
1467
1468         src = *ppos;
1469         svpfn = src / PM_ENTRY_BYTES;
1470         start_vaddr = svpfn << PAGE_SHIFT;
1471         end_vaddr = mm->task_size;
1472
1473         /* watch out for wraparound */
1474         if (svpfn > mm->task_size >> PAGE_SHIFT)
1475                 start_vaddr = end_vaddr;
1476
1477         /*
1478          * The odds are that this will stop walking way
1479          * before end_vaddr, because the length of the
1480          * user buffer is tracked in "pm", and the walk
1481          * will stop when we hit the end of the buffer.
1482          */
1483         ret = 0;
1484         while (count && (start_vaddr < end_vaddr)) {
1485                 int len;
1486                 unsigned long end;
1487
1488                 pm.pos = 0;
1489                 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1490                 /* overflow ? */
1491                 if (end < start_vaddr || end > end_vaddr)
1492                         end = end_vaddr;
1493                 down_read(&mm->mmap_sem);
1494                 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1495                 up_read(&mm->mmap_sem);
1496                 start_vaddr = end;
1497
1498                 len = min(count, PM_ENTRY_BYTES * pm.pos);
1499                 if (copy_to_user(buf, pm.buffer, len)) {
1500                         ret = -EFAULT;
1501                         goto out_free;
1502                 }
1503                 copied += len;
1504                 buf += len;
1505                 count -= len;
1506         }
1507         *ppos += copied;
1508         if (!ret || ret == PM_END_OF_BUFFER)
1509                 ret = copied;
1510
1511 out_free:
1512         kfree(pm.buffer);
1513 out_mm:
1514         mmput(mm);
1515 out:
1516         return ret;
1517 }
1518
1519 static int pagemap_open(struct inode *inode, struct file *file)
1520 {
1521         struct mm_struct *mm;
1522
1523         mm = proc_mem_open(inode, PTRACE_MODE_READ);
1524         if (IS_ERR(mm))
1525                 return PTR_ERR(mm);
1526         file->private_data = mm;
1527         return 0;
1528 }
1529
1530 static int pagemap_release(struct inode *inode, struct file *file)
1531 {
1532         struct mm_struct *mm = file->private_data;
1533
1534         if (mm)
1535                 mmdrop(mm);
1536         return 0;
1537 }
1538
1539 const struct file_operations proc_pagemap_operations = {
1540         .llseek         = mem_lseek, /* borrow this */
1541         .read           = pagemap_read,
1542         .open           = pagemap_open,
1543         .release        = pagemap_release,
1544 };
1545 #endif /* CONFIG_PROC_PAGE_MONITOR */
1546
1547 #ifdef CONFIG_NUMA
1548
1549 struct numa_maps {
1550         unsigned long pages;
1551         unsigned long anon;
1552         unsigned long active;
1553         unsigned long writeback;
1554         unsigned long mapcount_max;
1555         unsigned long dirty;
1556         unsigned long swapcache;
1557         unsigned long node[MAX_NUMNODES];
1558 };
1559
1560 struct numa_maps_private {
1561         struct proc_maps_private proc_maps;
1562         struct numa_maps md;
1563 };
1564
1565 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1566                         unsigned long nr_pages)
1567 {
1568         int count = page_mapcount(page);
1569
1570         md->pages += nr_pages;
1571         if (pte_dirty || PageDirty(page))
1572                 md->dirty += nr_pages;
1573
1574         if (PageSwapCache(page))
1575                 md->swapcache += nr_pages;
1576
1577         if (PageActive(page) || PageUnevictable(page))
1578                 md->active += nr_pages;
1579
1580         if (PageWriteback(page))
1581                 md->writeback += nr_pages;
1582
1583         if (PageAnon(page))
1584                 md->anon += nr_pages;
1585
1586         if (count > md->mapcount_max)
1587                 md->mapcount_max = count;
1588
1589         md->node[page_to_nid(page)] += nr_pages;
1590 }
1591
1592 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1593                 unsigned long addr)
1594 {
1595         struct page *page;
1596         int nid;
1597
1598         if (!pte_present(pte))
1599                 return NULL;
1600
1601         page = vm_normal_page(vma, addr, pte);
1602         if (!page)
1603                 return NULL;
1604
1605         if (PageReserved(page))
1606                 return NULL;
1607
1608         nid = page_to_nid(page);
1609         if (!node_isset(nid, node_states[N_MEMORY]))
1610                 return NULL;
1611
1612         return page;
1613 }
1614
1615 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1616 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1617                                               struct vm_area_struct *vma,
1618                                               unsigned long addr)
1619 {
1620         struct page *page;
1621         int nid;
1622
1623         if (!pmd_present(pmd))
1624                 return NULL;
1625
1626         page = vm_normal_page_pmd(vma, addr, pmd);
1627         if (!page)
1628                 return NULL;
1629
1630         if (PageReserved(page))
1631                 return NULL;
1632
1633         nid = page_to_nid(page);
1634         if (!node_isset(nid, node_states[N_MEMORY]))
1635                 return NULL;
1636
1637         return page;
1638 }
1639 #endif
1640
1641 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1642                 unsigned long end, struct mm_walk *walk)
1643 {
1644         struct numa_maps *md = walk->private;
1645         struct vm_area_struct *vma = walk->vma;
1646         spinlock_t *ptl;
1647         pte_t *orig_pte;
1648         pte_t *pte;
1649
1650 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1651         ptl = pmd_trans_huge_lock(pmd, vma);
1652         if (ptl) {
1653                 struct page *page;
1654
1655                 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1656                 if (page)
1657                         gather_stats(page, md, pmd_dirty(*pmd),
1658                                      HPAGE_PMD_SIZE/PAGE_SIZE);
1659                 spin_unlock(ptl);
1660                 return 0;
1661         }
1662
1663         if (pmd_trans_unstable(pmd))
1664                 return 0;
1665 #endif
1666         orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1667         do {
1668                 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1669                 if (!page)
1670                         continue;
1671                 gather_stats(page, md, pte_dirty(*pte), 1);
1672
1673         } while (pte++, addr += PAGE_SIZE, addr != end);
1674         pte_unmap_unlock(orig_pte, ptl);
1675         cond_resched();
1676         return 0;
1677 }
1678 #ifdef CONFIG_HUGETLB_PAGE
1679 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1680                 unsigned long addr, unsigned long end, struct mm_walk *walk)
1681 {
1682         pte_t huge_pte = huge_ptep_get(pte);
1683         struct numa_maps *md;
1684         struct page *page;
1685
1686         if (!pte_present(huge_pte))
1687                 return 0;
1688
1689         page = pte_page(huge_pte);
1690         if (!page)
1691                 return 0;
1692
1693         md = walk->private;
1694         gather_stats(page, md, pte_dirty(huge_pte), 1);
1695         return 0;
1696 }
1697
1698 #else
1699 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1700                 unsigned long addr, unsigned long end, struct mm_walk *walk)
1701 {
1702         return 0;
1703 }
1704 #endif
1705
1706 /*
1707  * Display pages allocated per node and memory policy via /proc.
1708  */
1709 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1710 {
1711         struct numa_maps_private *numa_priv = m->private;
1712         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1713         struct vm_area_struct *vma = v;
1714         struct numa_maps *md = &numa_priv->md;
1715         struct file *file = vma->vm_file;
1716         struct mm_struct *mm = vma->vm_mm;
1717         struct mm_walk walk = {
1718                 .hugetlb_entry = gather_hugetlb_stats,
1719                 .pmd_entry = gather_pte_stats,
1720                 .private = md,
1721                 .mm = mm,
1722         };
1723         struct mempolicy *pol;
1724         char buffer[64];
1725         int nid;
1726
1727         if (!mm)
1728                 return 0;
1729
1730         /* Ensure we start with an empty set of numa_maps statistics. */
1731         memset(md, 0, sizeof(*md));
1732
1733         pol = __get_vma_policy(vma, vma->vm_start);
1734         if (pol) {
1735                 mpol_to_str(buffer, sizeof(buffer), pol);
1736                 mpol_cond_put(pol);
1737         } else {
1738                 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1739         }
1740
1741         seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1742
1743         if (file) {
1744                 seq_puts(m, " file=");
1745                 seq_file_path(m, file, "\n\t= ");
1746         } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1747                 seq_puts(m, " heap");
1748         } else if (is_stack(proc_priv, vma)) {
1749                 seq_puts(m, " stack");
1750         }
1751
1752         if (is_vm_hugetlb_page(vma))
1753                 seq_puts(m, " huge");
1754
1755         /* mmap_sem is held by m_start */
1756         walk_page_vma(vma, &walk);
1757
1758         if (!md->pages)
1759                 goto out;
1760
1761         if (md->anon)
1762                 seq_printf(m, " anon=%lu", md->anon);
1763
1764         if (md->dirty)
1765                 seq_printf(m, " dirty=%lu", md->dirty);
1766
1767         if (md->pages != md->anon && md->pages != md->dirty)
1768                 seq_printf(m, " mapped=%lu", md->pages);
1769
1770         if (md->mapcount_max > 1)
1771                 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1772
1773         if (md->swapcache)
1774                 seq_printf(m, " swapcache=%lu", md->swapcache);
1775
1776         if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1777                 seq_printf(m, " active=%lu", md->active);
1778
1779         if (md->writeback)
1780                 seq_printf(m, " writeback=%lu", md->writeback);
1781
1782         for_each_node_state(nid, N_MEMORY)
1783                 if (md->node[nid])
1784                         seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1785
1786         seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1787 out:
1788         seq_putc(m, '\n');
1789         m_cache_vma(m, vma);
1790         return 0;
1791 }
1792
1793 static int show_pid_numa_map(struct seq_file *m, void *v)
1794 {
1795         return show_numa_map(m, v, 1);
1796 }
1797
1798 static int show_tid_numa_map(struct seq_file *m, void *v)
1799 {
1800         return show_numa_map(m, v, 0);
1801 }
1802
1803 static const struct seq_operations proc_pid_numa_maps_op = {
1804         .start  = m_start,
1805         .next   = m_next,
1806         .stop   = m_stop,
1807         .show   = show_pid_numa_map,
1808 };
1809
1810 static const struct seq_operations proc_tid_numa_maps_op = {
1811         .start  = m_start,
1812         .next   = m_next,
1813         .stop   = m_stop,
1814         .show   = show_tid_numa_map,
1815 };
1816
1817 static int numa_maps_open(struct inode *inode, struct file *file,
1818                           const struct seq_operations *ops)
1819 {
1820         return proc_maps_open(inode, file, ops,
1821                                 sizeof(struct numa_maps_private));
1822 }
1823
1824 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1825 {
1826         return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1827 }
1828
1829 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1830 {
1831         return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1832 }
1833
1834 const struct file_operations proc_pid_numa_maps_operations = {
1835         .open           = pid_numa_maps_open,
1836         .read           = seq_read,
1837         .llseek         = seq_lseek,
1838         .release        = proc_map_release,
1839 };
1840
1841 const struct file_operations proc_tid_numa_maps_operations = {
1842         .open           = tid_numa_maps_open,
1843         .read           = seq_read,
1844         .llseek         = seq_lseek,
1845         .release        = proc_map_release,
1846 };
1847 #endif /* CONFIG_NUMA */