Merge patch series "bpf, riscv: use BPF prog pack allocator in BPF JIT"
[platform/kernel/linux-rpi.git] / fs / proc / task_mmu.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/pagewalk.h>
3 #include <linux/mm_inline.h>
4 #include <linux/hugetlb.h>
5 #include <linux/huge_mm.h>
6 #include <linux/mount.h>
7 #include <linux/seq_file.h>
8 #include <linux/highmem.h>
9 #include <linux/ptrace.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/mempolicy.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/sched/mm.h>
16 #include <linux/swapops.h>
17 #include <linux/mmu_notifier.h>
18 #include <linux/page_idle.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/uaccess.h>
21 #include <linux/pkeys.h>
22
23 #include <asm/elf.h>
24 #include <asm/tlb.h>
25 #include <asm/tlbflush.h>
26 #include "internal.h"
27
28 #define SEQ_PUT_DEC(str, val) \
29                 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
30 void task_mem(struct seq_file *m, struct mm_struct *mm)
31 {
32         unsigned long text, lib, swap, anon, file, shmem;
33         unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
34
35         anon = get_mm_counter(mm, MM_ANONPAGES);
36         file = get_mm_counter(mm, MM_FILEPAGES);
37         shmem = get_mm_counter(mm, MM_SHMEMPAGES);
38
39         /*
40          * Note: to minimize their overhead, mm maintains hiwater_vm and
41          * hiwater_rss only when about to *lower* total_vm or rss.  Any
42          * collector of these hiwater stats must therefore get total_vm
43          * and rss too, which will usually be the higher.  Barriers? not
44          * worth the effort, such snapshots can always be inconsistent.
45          */
46         hiwater_vm = total_vm = mm->total_vm;
47         if (hiwater_vm < mm->hiwater_vm)
48                 hiwater_vm = mm->hiwater_vm;
49         hiwater_rss = total_rss = anon + file + shmem;
50         if (hiwater_rss < mm->hiwater_rss)
51                 hiwater_rss = mm->hiwater_rss;
52
53         /* split executable areas between text and lib */
54         text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK);
55         text = min(text, mm->exec_vm << PAGE_SHIFT);
56         lib = (mm->exec_vm << PAGE_SHIFT) - text;
57
58         swap = get_mm_counter(mm, MM_SWAPENTS);
59         SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
60         SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
61         SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
62         SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm));
63         SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss);
64         SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss);
65         SEQ_PUT_DEC(" kB\nRssAnon:\t", anon);
66         SEQ_PUT_DEC(" kB\nRssFile:\t", file);
67         SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem);
68         SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm);
69         SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm);
70         seq_put_decimal_ull_width(m,
71                     " kB\nVmExe:\t", text >> 10, 8);
72         seq_put_decimal_ull_width(m,
73                     " kB\nVmLib:\t", lib >> 10, 8);
74         seq_put_decimal_ull_width(m,
75                     " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);
76         SEQ_PUT_DEC(" kB\nVmSwap:\t", swap);
77         seq_puts(m, " kB\n");
78         hugetlb_report_usage(m, mm);
79 }
80 #undef SEQ_PUT_DEC
81
82 unsigned long task_vsize(struct mm_struct *mm)
83 {
84         return PAGE_SIZE * mm->total_vm;
85 }
86
87 unsigned long task_statm(struct mm_struct *mm,
88                          unsigned long *shared, unsigned long *text,
89                          unsigned long *data, unsigned long *resident)
90 {
91         *shared = get_mm_counter(mm, MM_FILEPAGES) +
92                         get_mm_counter(mm, MM_SHMEMPAGES);
93         *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
94                                                                 >> PAGE_SHIFT;
95         *data = mm->data_vm + mm->stack_vm;
96         *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
97         return mm->total_vm;
98 }
99
100 #ifdef CONFIG_NUMA
101 /*
102  * Save get_task_policy() for show_numa_map().
103  */
104 static void hold_task_mempolicy(struct proc_maps_private *priv)
105 {
106         struct task_struct *task = priv->task;
107
108         task_lock(task);
109         priv->task_mempolicy = get_task_policy(task);
110         mpol_get(priv->task_mempolicy);
111         task_unlock(task);
112 }
113 static void release_task_mempolicy(struct proc_maps_private *priv)
114 {
115         mpol_put(priv->task_mempolicy);
116 }
117 #else
118 static void hold_task_mempolicy(struct proc_maps_private *priv)
119 {
120 }
121 static void release_task_mempolicy(struct proc_maps_private *priv)
122 {
123 }
124 #endif
125
126 static struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv,
127                                                 loff_t *ppos)
128 {
129         struct vm_area_struct *vma = vma_next(&priv->iter);
130
131         if (vma) {
132                 *ppos = vma->vm_start;
133         } else {
134                 *ppos = -2UL;
135                 vma = get_gate_vma(priv->mm);
136         }
137
138         return vma;
139 }
140
141 static void *m_start(struct seq_file *m, loff_t *ppos)
142 {
143         struct proc_maps_private *priv = m->private;
144         unsigned long last_addr = *ppos;
145         struct mm_struct *mm;
146
147         /* See m_next(). Zero at the start or after lseek. */
148         if (last_addr == -1UL)
149                 return NULL;
150
151         priv->task = get_proc_task(priv->inode);
152         if (!priv->task)
153                 return ERR_PTR(-ESRCH);
154
155         mm = priv->mm;
156         if (!mm || !mmget_not_zero(mm)) {
157                 put_task_struct(priv->task);
158                 priv->task = NULL;
159                 return NULL;
160         }
161
162         if (mmap_read_lock_killable(mm)) {
163                 mmput(mm);
164                 put_task_struct(priv->task);
165                 priv->task = NULL;
166                 return ERR_PTR(-EINTR);
167         }
168
169         vma_iter_init(&priv->iter, mm, last_addr);
170         hold_task_mempolicy(priv);
171         if (last_addr == -2UL)
172                 return get_gate_vma(mm);
173
174         return proc_get_vma(priv, ppos);
175 }
176
177 static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
178 {
179         if (*ppos == -2UL) {
180                 *ppos = -1UL;
181                 return NULL;
182         }
183         return proc_get_vma(m->private, ppos);
184 }
185
186 static void m_stop(struct seq_file *m, void *v)
187 {
188         struct proc_maps_private *priv = m->private;
189         struct mm_struct *mm = priv->mm;
190
191         if (!priv->task)
192                 return;
193
194         release_task_mempolicy(priv);
195         mmap_read_unlock(mm);
196         mmput(mm);
197         put_task_struct(priv->task);
198         priv->task = NULL;
199 }
200
201 static int proc_maps_open(struct inode *inode, struct file *file,
202                         const struct seq_operations *ops, int psize)
203 {
204         struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
205
206         if (!priv)
207                 return -ENOMEM;
208
209         priv->inode = inode;
210         priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
211         if (IS_ERR(priv->mm)) {
212                 int err = PTR_ERR(priv->mm);
213
214                 seq_release_private(inode, file);
215                 return err;
216         }
217
218         return 0;
219 }
220
221 static int proc_map_release(struct inode *inode, struct file *file)
222 {
223         struct seq_file *seq = file->private_data;
224         struct proc_maps_private *priv = seq->private;
225
226         if (priv->mm)
227                 mmdrop(priv->mm);
228
229         return seq_release_private(inode, file);
230 }
231
232 static int do_maps_open(struct inode *inode, struct file *file,
233                         const struct seq_operations *ops)
234 {
235         return proc_maps_open(inode, file, ops,
236                                 sizeof(struct proc_maps_private));
237 }
238
239 static void show_vma_header_prefix(struct seq_file *m,
240                                    unsigned long start, unsigned long end,
241                                    vm_flags_t flags, unsigned long long pgoff,
242                                    dev_t dev, unsigned long ino)
243 {
244         seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
245         seq_put_hex_ll(m, NULL, start, 8);
246         seq_put_hex_ll(m, "-", end, 8);
247         seq_putc(m, ' ');
248         seq_putc(m, flags & VM_READ ? 'r' : '-');
249         seq_putc(m, flags & VM_WRITE ? 'w' : '-');
250         seq_putc(m, flags & VM_EXEC ? 'x' : '-');
251         seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
252         seq_put_hex_ll(m, " ", pgoff, 8);
253         seq_put_hex_ll(m, " ", MAJOR(dev), 2);
254         seq_put_hex_ll(m, ":", MINOR(dev), 2);
255         seq_put_decimal_ull(m, " ", ino);
256         seq_putc(m, ' ');
257 }
258
259 static void
260 show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
261 {
262         struct anon_vma_name *anon_name = NULL;
263         struct mm_struct *mm = vma->vm_mm;
264         struct file *file = vma->vm_file;
265         vm_flags_t flags = vma->vm_flags;
266         unsigned long ino = 0;
267         unsigned long long pgoff = 0;
268         unsigned long start, end;
269         dev_t dev = 0;
270         const char *name = NULL;
271
272         if (file) {
273                 struct inode *inode = file_inode(vma->vm_file);
274                 dev = inode->i_sb->s_dev;
275                 ino = inode->i_ino;
276                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
277         }
278
279         start = vma->vm_start;
280         end = vma->vm_end;
281         show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
282         if (mm)
283                 anon_name = anon_vma_name(vma);
284
285         /*
286          * Print the dentry name for named mappings, and a
287          * special [heap] marker for the heap:
288          */
289         if (file) {
290                 seq_pad(m, ' ');
291                 /*
292                  * If user named this anon shared memory via
293                  * prctl(PR_SET_VMA ..., use the provided name.
294                  */
295                 if (anon_name)
296                         seq_printf(m, "[anon_shmem:%s]", anon_name->name);
297                 else
298                         seq_file_path(m, file, "\n");
299                 goto done;
300         }
301
302         if (vma->vm_ops && vma->vm_ops->name) {
303                 name = vma->vm_ops->name(vma);
304                 if (name)
305                         goto done;
306         }
307
308         name = arch_vma_name(vma);
309         if (!name) {
310                 if (!mm) {
311                         name = "[vdso]";
312                         goto done;
313                 }
314
315                 if (vma_is_initial_heap(vma)) {
316                         name = "[heap]";
317                         goto done;
318                 }
319
320                 if (vma_is_initial_stack(vma)) {
321                         name = "[stack]";
322                         goto done;
323                 }
324
325                 if (anon_name) {
326                         seq_pad(m, ' ');
327                         seq_printf(m, "[anon:%s]", anon_name->name);
328                 }
329         }
330
331 done:
332         if (name) {
333                 seq_pad(m, ' ');
334                 seq_puts(m, name);
335         }
336         seq_putc(m, '\n');
337 }
338
339 static int show_map(struct seq_file *m, void *v)
340 {
341         show_map_vma(m, v);
342         return 0;
343 }
344
345 static const struct seq_operations proc_pid_maps_op = {
346         .start  = m_start,
347         .next   = m_next,
348         .stop   = m_stop,
349         .show   = show_map
350 };
351
352 static int pid_maps_open(struct inode *inode, struct file *file)
353 {
354         return do_maps_open(inode, file, &proc_pid_maps_op);
355 }
356
357 const struct file_operations proc_pid_maps_operations = {
358         .open           = pid_maps_open,
359         .read           = seq_read,
360         .llseek         = seq_lseek,
361         .release        = proc_map_release,
362 };
363
364 /*
365  * Proportional Set Size(PSS): my share of RSS.
366  *
367  * PSS of a process is the count of pages it has in memory, where each
368  * page is divided by the number of processes sharing it.  So if a
369  * process has 1000 pages all to itself, and 1000 shared with one other
370  * process, its PSS will be 1500.
371  *
372  * To keep (accumulated) division errors low, we adopt a 64bit
373  * fixed-point pss counter to minimize division errors. So (pss >>
374  * PSS_SHIFT) would be the real byte count.
375  *
376  * A shift of 12 before division means (assuming 4K page size):
377  *      - 1M 3-user-pages add up to 8KB errors;
378  *      - supports mapcount up to 2^24, or 16M;
379  *      - supports PSS up to 2^52 bytes, or 4PB.
380  */
381 #define PSS_SHIFT 12
382
383 #ifdef CONFIG_PROC_PAGE_MONITOR
384 struct mem_size_stats {
385         unsigned long resident;
386         unsigned long shared_clean;
387         unsigned long shared_dirty;
388         unsigned long private_clean;
389         unsigned long private_dirty;
390         unsigned long referenced;
391         unsigned long anonymous;
392         unsigned long lazyfree;
393         unsigned long anonymous_thp;
394         unsigned long shmem_thp;
395         unsigned long file_thp;
396         unsigned long swap;
397         unsigned long shared_hugetlb;
398         unsigned long private_hugetlb;
399         u64 pss;
400         u64 pss_anon;
401         u64 pss_file;
402         u64 pss_shmem;
403         u64 pss_dirty;
404         u64 pss_locked;
405         u64 swap_pss;
406 };
407
408 static void smaps_page_accumulate(struct mem_size_stats *mss,
409                 struct page *page, unsigned long size, unsigned long pss,
410                 bool dirty, bool locked, bool private)
411 {
412         mss->pss += pss;
413
414         if (PageAnon(page))
415                 mss->pss_anon += pss;
416         else if (PageSwapBacked(page))
417                 mss->pss_shmem += pss;
418         else
419                 mss->pss_file += pss;
420
421         if (locked)
422                 mss->pss_locked += pss;
423
424         if (dirty || PageDirty(page)) {
425                 mss->pss_dirty += pss;
426                 if (private)
427                         mss->private_dirty += size;
428                 else
429                         mss->shared_dirty += size;
430         } else {
431                 if (private)
432                         mss->private_clean += size;
433                 else
434                         mss->shared_clean += size;
435         }
436 }
437
438 static void smaps_account(struct mem_size_stats *mss, struct page *page,
439                 bool compound, bool young, bool dirty, bool locked,
440                 bool migration)
441 {
442         int i, nr = compound ? compound_nr(page) : 1;
443         unsigned long size = nr * PAGE_SIZE;
444
445         /*
446          * First accumulate quantities that depend only on |size| and the type
447          * of the compound page.
448          */
449         if (PageAnon(page)) {
450                 mss->anonymous += size;
451                 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
452                         mss->lazyfree += size;
453         }
454
455         mss->resident += size;
456         /* Accumulate the size in pages that have been accessed. */
457         if (young || page_is_young(page) || PageReferenced(page))
458                 mss->referenced += size;
459
460         /*
461          * Then accumulate quantities that may depend on sharing, or that may
462          * differ page-by-page.
463          *
464          * page_count(page) == 1 guarantees the page is mapped exactly once.
465          * If any subpage of the compound page mapped with PTE it would elevate
466          * page_count().
467          *
468          * The page_mapcount() is called to get a snapshot of the mapcount.
469          * Without holding the page lock this snapshot can be slightly wrong as
470          * we cannot always read the mapcount atomically.  It is not safe to
471          * call page_mapcount() even with PTL held if the page is not mapped,
472          * especially for migration entries.  Treat regular migration entries
473          * as mapcount == 1.
474          */
475         if ((page_count(page) == 1) || migration) {
476                 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
477                         locked, true);
478                 return;
479         }
480         for (i = 0; i < nr; i++, page++) {
481                 int mapcount = page_mapcount(page);
482                 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
483                 if (mapcount >= 2)
484                         pss /= mapcount;
485                 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
486                                       mapcount < 2);
487         }
488 }
489
490 #ifdef CONFIG_SHMEM
491 static int smaps_pte_hole(unsigned long addr, unsigned long end,
492                           __always_unused int depth, struct mm_walk *walk)
493 {
494         struct mem_size_stats *mss = walk->private;
495         struct vm_area_struct *vma = walk->vma;
496
497         mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping,
498                                               linear_page_index(vma, addr),
499                                               linear_page_index(vma, end));
500
501         return 0;
502 }
503 #else
504 #define smaps_pte_hole          NULL
505 #endif /* CONFIG_SHMEM */
506
507 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
508 {
509 #ifdef CONFIG_SHMEM
510         if (walk->ops->pte_hole) {
511                 /* depth is not used */
512                 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk);
513         }
514 #endif
515 }
516
517 static void smaps_pte_entry(pte_t *pte, unsigned long addr,
518                 struct mm_walk *walk)
519 {
520         struct mem_size_stats *mss = walk->private;
521         struct vm_area_struct *vma = walk->vma;
522         bool locked = !!(vma->vm_flags & VM_LOCKED);
523         struct page *page = NULL;
524         bool migration = false, young = false, dirty = false;
525         pte_t ptent = ptep_get(pte);
526
527         if (pte_present(ptent)) {
528                 page = vm_normal_page(vma, addr, ptent);
529                 young = pte_young(ptent);
530                 dirty = pte_dirty(ptent);
531         } else if (is_swap_pte(ptent)) {
532                 swp_entry_t swpent = pte_to_swp_entry(ptent);
533
534                 if (!non_swap_entry(swpent)) {
535                         int mapcount;
536
537                         mss->swap += PAGE_SIZE;
538                         mapcount = swp_swapcount(swpent);
539                         if (mapcount >= 2) {
540                                 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
541
542                                 do_div(pss_delta, mapcount);
543                                 mss->swap_pss += pss_delta;
544                         } else {
545                                 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
546                         }
547                 } else if (is_pfn_swap_entry(swpent)) {
548                         if (is_migration_entry(swpent))
549                                 migration = true;
550                         page = pfn_swap_entry_to_page(swpent);
551                 }
552         } else {
553                 smaps_pte_hole_lookup(addr, walk);
554                 return;
555         }
556
557         if (!page)
558                 return;
559
560         smaps_account(mss, page, false, young, dirty, locked, migration);
561 }
562
563 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
564 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
565                 struct mm_walk *walk)
566 {
567         struct mem_size_stats *mss = walk->private;
568         struct vm_area_struct *vma = walk->vma;
569         bool locked = !!(vma->vm_flags & VM_LOCKED);
570         struct page *page = NULL;
571         bool migration = false;
572
573         if (pmd_present(*pmd)) {
574                 page = vm_normal_page_pmd(vma, addr, *pmd);
575         } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
576                 swp_entry_t entry = pmd_to_swp_entry(*pmd);
577
578                 if (is_migration_entry(entry)) {
579                         migration = true;
580                         page = pfn_swap_entry_to_page(entry);
581                 }
582         }
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                 mss->file_thp += HPAGE_PMD_SIZE;
593
594         smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
595                       locked, migration);
596 }
597 #else
598 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
599                 struct mm_walk *walk)
600 {
601 }
602 #endif
603
604 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
605                            struct mm_walk *walk)
606 {
607         struct vm_area_struct *vma = walk->vma;
608         pte_t *pte;
609         spinlock_t *ptl;
610
611         ptl = pmd_trans_huge_lock(pmd, vma);
612         if (ptl) {
613                 smaps_pmd_entry(pmd, addr, walk);
614                 spin_unlock(ptl);
615                 goto out;
616         }
617
618         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
619         if (!pte) {
620                 walk->action = ACTION_AGAIN;
621                 return 0;
622         }
623         for (; addr != end; pte++, addr += PAGE_SIZE)
624                 smaps_pte_entry(pte, addr, walk);
625         pte_unmap_unlock(pte - 1, ptl);
626 out:
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_LOCKED)]      = "lo",
653                 [ilog2(VM_IO)]          = "io",
654                 [ilog2(VM_SEQ_READ)]    = "sr",
655                 [ilog2(VM_RAND_READ)]   = "rr",
656                 [ilog2(VM_DONTCOPY)]    = "dc",
657                 [ilog2(VM_DONTEXPAND)]  = "de",
658                 [ilog2(VM_LOCKONFAULT)] = "lf",
659                 [ilog2(VM_ACCOUNT)]     = "ac",
660                 [ilog2(VM_NORESERVE)]   = "nr",
661                 [ilog2(VM_HUGETLB)]     = "ht",
662                 [ilog2(VM_SYNC)]        = "sf",
663                 [ilog2(VM_ARCH_1)]      = "ar",
664                 [ilog2(VM_WIPEONFORK)]  = "wf",
665                 [ilog2(VM_DONTDUMP)]    = "dd",
666 #ifdef CONFIG_ARM64_BTI
667                 [ilog2(VM_ARM64_BTI)]   = "bt",
668 #endif
669 #ifdef CONFIG_MEM_SOFT_DIRTY
670                 [ilog2(VM_SOFTDIRTY)]   = "sd",
671 #endif
672                 [ilog2(VM_MIXEDMAP)]    = "mm",
673                 [ilog2(VM_HUGEPAGE)]    = "hg",
674                 [ilog2(VM_NOHUGEPAGE)]  = "nh",
675                 [ilog2(VM_MERGEABLE)]   = "mg",
676                 [ilog2(VM_UFFD_MISSING)]= "um",
677                 [ilog2(VM_UFFD_WP)]     = "uw",
678 #ifdef CONFIG_ARM64_MTE
679                 [ilog2(VM_MTE)]         = "mt",
680                 [ilog2(VM_MTE_ALLOWED)] = "",
681 #endif
682 #ifdef CONFIG_ARCH_HAS_PKEYS
683                 /* These come out via ProtectionKey: */
684                 [ilog2(VM_PKEY_BIT0)]   = "",
685                 [ilog2(VM_PKEY_BIT1)]   = "",
686                 [ilog2(VM_PKEY_BIT2)]   = "",
687                 [ilog2(VM_PKEY_BIT3)]   = "",
688 #if VM_PKEY_BIT4
689                 [ilog2(VM_PKEY_BIT4)]   = "",
690 #endif
691 #endif /* CONFIG_ARCH_HAS_PKEYS */
692 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
693                 [ilog2(VM_UFFD_MINOR)]  = "ui",
694 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
695 #ifdef CONFIG_X86_USER_SHADOW_STACK
696                 [ilog2(VM_SHADOW_STACK)] = "ss",
697 #endif
698         };
699         size_t i;
700
701         seq_puts(m, "VmFlags: ");
702         for (i = 0; i < BITS_PER_LONG; i++) {
703                 if (!mnemonics[i][0])
704                         continue;
705                 if (vma->vm_flags & (1UL << i)) {
706                         seq_putc(m, mnemonics[i][0]);
707                         seq_putc(m, mnemonics[i][1]);
708                         seq_putc(m, ' ');
709                 }
710         }
711         seq_putc(m, '\n');
712 }
713
714 #ifdef CONFIG_HUGETLB_PAGE
715 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
716                                  unsigned long addr, unsigned long end,
717                                  struct mm_walk *walk)
718 {
719         struct mem_size_stats *mss = walk->private;
720         struct vm_area_struct *vma = walk->vma;
721         struct page *page = NULL;
722         pte_t ptent = ptep_get(pte);
723
724         if (pte_present(ptent)) {
725                 page = vm_normal_page(vma, addr, ptent);
726         } else if (is_swap_pte(ptent)) {
727                 swp_entry_t swpent = pte_to_swp_entry(ptent);
728
729                 if (is_pfn_swap_entry(swpent))
730                         page = pfn_swap_entry_to_page(swpent);
731         }
732         if (page) {
733                 if (page_mapcount(page) >= 2 || hugetlb_pmd_shared(pte))
734                         mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
735                 else
736                         mss->private_hugetlb += huge_page_size(hstate_vma(vma));
737         }
738         return 0;
739 }
740 #else
741 #define smaps_hugetlb_range     NULL
742 #endif /* HUGETLB_PAGE */
743
744 static const struct mm_walk_ops smaps_walk_ops = {
745         .pmd_entry              = smaps_pte_range,
746         .hugetlb_entry          = smaps_hugetlb_range,
747         .walk_lock              = PGWALK_RDLOCK,
748 };
749
750 static const struct mm_walk_ops smaps_shmem_walk_ops = {
751         .pmd_entry              = smaps_pte_range,
752         .hugetlb_entry          = smaps_hugetlb_range,
753         .pte_hole               = smaps_pte_hole,
754         .walk_lock              = PGWALK_RDLOCK,
755 };
756
757 /*
758  * Gather mem stats from @vma with the indicated beginning
759  * address @start, and keep them in @mss.
760  *
761  * Use vm_start of @vma as the beginning address if @start is 0.
762  */
763 static void smap_gather_stats(struct vm_area_struct *vma,
764                 struct mem_size_stats *mss, unsigned long start)
765 {
766         const struct mm_walk_ops *ops = &smaps_walk_ops;
767
768         /* Invalid start */
769         if (start >= vma->vm_end)
770                 return;
771
772         if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
773                 /*
774                  * For shared or readonly shmem mappings we know that all
775                  * swapped out pages belong to the shmem object, and we can
776                  * obtain the swap value much more efficiently. For private
777                  * writable mappings, we might have COW pages that are
778                  * not affected by the parent swapped out pages of the shmem
779                  * object, so we have to distinguish them during the page walk.
780                  * Unless we know that the shmem object (or the part mapped by
781                  * our VMA) has no swapped out pages at all.
782                  */
783                 unsigned long shmem_swapped = shmem_swap_usage(vma);
784
785                 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
786                                         !(vma->vm_flags & VM_WRITE))) {
787                         mss->swap += shmem_swapped;
788                 } else {
789                         ops = &smaps_shmem_walk_ops;
790                 }
791         }
792
793         /* mmap_lock is held in m_start */
794         if (!start)
795                 walk_page_vma(vma, ops, mss);
796         else
797                 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
798 }
799
800 #define SEQ_PUT_DEC(str, val) \
801                 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
802
803 /* Show the contents common for smaps and smaps_rollup */
804 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
805         bool rollup_mode)
806 {
807         SEQ_PUT_DEC("Rss:            ", mss->resident);
808         SEQ_PUT_DEC(" kB\nPss:            ", mss->pss >> PSS_SHIFT);
809         SEQ_PUT_DEC(" kB\nPss_Dirty:      ", mss->pss_dirty >> PSS_SHIFT);
810         if (rollup_mode) {
811                 /*
812                  * These are meaningful only for smaps_rollup, otherwise two of
813                  * them are zero, and the other one is the same as Pss.
814                  */
815                 SEQ_PUT_DEC(" kB\nPss_Anon:       ",
816                         mss->pss_anon >> PSS_SHIFT);
817                 SEQ_PUT_DEC(" kB\nPss_File:       ",
818                         mss->pss_file >> PSS_SHIFT);
819                 SEQ_PUT_DEC(" kB\nPss_Shmem:      ",
820                         mss->pss_shmem >> PSS_SHIFT);
821         }
822         SEQ_PUT_DEC(" kB\nShared_Clean:   ", mss->shared_clean);
823         SEQ_PUT_DEC(" kB\nShared_Dirty:   ", mss->shared_dirty);
824         SEQ_PUT_DEC(" kB\nPrivate_Clean:  ", mss->private_clean);
825         SEQ_PUT_DEC(" kB\nPrivate_Dirty:  ", mss->private_dirty);
826         SEQ_PUT_DEC(" kB\nReferenced:     ", mss->referenced);
827         SEQ_PUT_DEC(" kB\nAnonymous:      ", mss->anonymous);
828         SEQ_PUT_DEC(" kB\nLazyFree:       ", mss->lazyfree);
829         SEQ_PUT_DEC(" kB\nAnonHugePages:  ", mss->anonymous_thp);
830         SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
831         SEQ_PUT_DEC(" kB\nFilePmdMapped:  ", mss->file_thp);
832         SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
833         seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
834                                   mss->private_hugetlb >> 10, 7);
835         SEQ_PUT_DEC(" kB\nSwap:           ", mss->swap);
836         SEQ_PUT_DEC(" kB\nSwapPss:        ",
837                                         mss->swap_pss >> PSS_SHIFT);
838         SEQ_PUT_DEC(" kB\nLocked:         ",
839                                         mss->pss_locked >> PSS_SHIFT);
840         seq_puts(m, " kB\n");
841 }
842
843 static int show_smap(struct seq_file *m, void *v)
844 {
845         struct vm_area_struct *vma = v;
846         struct mem_size_stats mss;
847
848         memset(&mss, 0, sizeof(mss));
849
850         smap_gather_stats(vma, &mss, 0);
851
852         show_map_vma(m, vma);
853
854         SEQ_PUT_DEC("Size:           ", vma->vm_end - vma->vm_start);
855         SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
856         SEQ_PUT_DEC(" kB\nMMUPageSize:    ", vma_mmu_pagesize(vma));
857         seq_puts(m, " kB\n");
858
859         __show_smap(m, &mss, false);
860
861         seq_printf(m, "THPeligible:    %8u\n",
862                    hugepage_vma_check(vma, vma->vm_flags, true, false, true));
863
864         if (arch_pkeys_enabled())
865                 seq_printf(m, "ProtectionKey:  %8u\n", vma_pkey(vma));
866         show_smap_vma_flags(m, vma);
867
868         return 0;
869 }
870
871 static int show_smaps_rollup(struct seq_file *m, void *v)
872 {
873         struct proc_maps_private *priv = m->private;
874         struct mem_size_stats mss;
875         struct mm_struct *mm = priv->mm;
876         struct vm_area_struct *vma;
877         unsigned long vma_start = 0, last_vma_end = 0;
878         int ret = 0;
879         VMA_ITERATOR(vmi, mm, 0);
880
881         priv->task = get_proc_task(priv->inode);
882         if (!priv->task)
883                 return -ESRCH;
884
885         if (!mm || !mmget_not_zero(mm)) {
886                 ret = -ESRCH;
887                 goto out_put_task;
888         }
889
890         memset(&mss, 0, sizeof(mss));
891
892         ret = mmap_read_lock_killable(mm);
893         if (ret)
894                 goto out_put_mm;
895
896         hold_task_mempolicy(priv);
897         vma = vma_next(&vmi);
898
899         if (unlikely(!vma))
900                 goto empty_set;
901
902         vma_start = vma->vm_start;
903         do {
904                 smap_gather_stats(vma, &mss, 0);
905                 last_vma_end = vma->vm_end;
906
907                 /*
908                  * Release mmap_lock temporarily if someone wants to
909                  * access it for write request.
910                  */
911                 if (mmap_lock_is_contended(mm)) {
912                         vma_iter_invalidate(&vmi);
913                         mmap_read_unlock(mm);
914                         ret = mmap_read_lock_killable(mm);
915                         if (ret) {
916                                 release_task_mempolicy(priv);
917                                 goto out_put_mm;
918                         }
919
920                         /*
921                          * After dropping the lock, there are four cases to
922                          * consider. See the following example for explanation.
923                          *
924                          *   +------+------+-----------+
925                          *   | VMA1 | VMA2 | VMA3      |
926                          *   +------+------+-----------+
927                          *   |      |      |           |
928                          *  4k     8k     16k         400k
929                          *
930                          * Suppose we drop the lock after reading VMA2 due to
931                          * contention, then we get:
932                          *
933                          *      last_vma_end = 16k
934                          *
935                          * 1) VMA2 is freed, but VMA3 exists:
936                          *
937                          *    vma_next(vmi) will return VMA3.
938                          *    In this case, just continue from VMA3.
939                          *
940                          * 2) VMA2 still exists:
941                          *
942                          *    vma_next(vmi) will return VMA3.
943                          *    In this case, just continue from VMA3.
944                          *
945                          * 3) No more VMAs can be found:
946                          *
947                          *    vma_next(vmi) will return NULL.
948                          *    No more things to do, just break.
949                          *
950                          * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
951                          *
952                          *    vma_next(vmi) will return VMA' whose range
953                          *    contains last_vma_end.
954                          *    Iterate VMA' from last_vma_end.
955                          */
956                         vma = vma_next(&vmi);
957                         /* Case 3 above */
958                         if (!vma)
959                                 break;
960
961                         /* Case 1 and 2 above */
962                         if (vma->vm_start >= last_vma_end)
963                                 continue;
964
965                         /* Case 4 above */
966                         if (vma->vm_end > last_vma_end)
967                                 smap_gather_stats(vma, &mss, last_vma_end);
968                 }
969         } for_each_vma(vmi, vma);
970
971 empty_set:
972         show_vma_header_prefix(m, vma_start, last_vma_end, 0, 0, 0, 0);
973         seq_pad(m, ' ');
974         seq_puts(m, "[rollup]\n");
975
976         __show_smap(m, &mss, true);
977
978         release_task_mempolicy(priv);
979         mmap_read_unlock(mm);
980
981 out_put_mm:
982         mmput(mm);
983 out_put_task:
984         put_task_struct(priv->task);
985         priv->task = NULL;
986
987         return ret;
988 }
989 #undef SEQ_PUT_DEC
990
991 static const struct seq_operations proc_pid_smaps_op = {
992         .start  = m_start,
993         .next   = m_next,
994         .stop   = m_stop,
995         .show   = show_smap
996 };
997
998 static int pid_smaps_open(struct inode *inode, struct file *file)
999 {
1000         return do_maps_open(inode, file, &proc_pid_smaps_op);
1001 }
1002
1003 static int smaps_rollup_open(struct inode *inode, struct file *file)
1004 {
1005         int ret;
1006         struct proc_maps_private *priv;
1007
1008         priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
1009         if (!priv)
1010                 return -ENOMEM;
1011
1012         ret = single_open(file, show_smaps_rollup, priv);
1013         if (ret)
1014                 goto out_free;
1015
1016         priv->inode = inode;
1017         priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
1018         if (IS_ERR(priv->mm)) {
1019                 ret = PTR_ERR(priv->mm);
1020
1021                 single_release(inode, file);
1022                 goto out_free;
1023         }
1024
1025         return 0;
1026
1027 out_free:
1028         kfree(priv);
1029         return ret;
1030 }
1031
1032 static int smaps_rollup_release(struct inode *inode, struct file *file)
1033 {
1034         struct seq_file *seq = file->private_data;
1035         struct proc_maps_private *priv = seq->private;
1036
1037         if (priv->mm)
1038                 mmdrop(priv->mm);
1039
1040         kfree(priv);
1041         return single_release(inode, file);
1042 }
1043
1044 const struct file_operations proc_pid_smaps_operations = {
1045         .open           = pid_smaps_open,
1046         .read           = seq_read,
1047         .llseek         = seq_lseek,
1048         .release        = proc_map_release,
1049 };
1050
1051 const struct file_operations proc_pid_smaps_rollup_operations = {
1052         .open           = smaps_rollup_open,
1053         .read           = seq_read,
1054         .llseek         = seq_lseek,
1055         .release        = smaps_rollup_release,
1056 };
1057
1058 enum clear_refs_types {
1059         CLEAR_REFS_ALL = 1,
1060         CLEAR_REFS_ANON,
1061         CLEAR_REFS_MAPPED,
1062         CLEAR_REFS_SOFT_DIRTY,
1063         CLEAR_REFS_MM_HIWATER_RSS,
1064         CLEAR_REFS_LAST,
1065 };
1066
1067 struct clear_refs_private {
1068         enum clear_refs_types type;
1069 };
1070
1071 #ifdef CONFIG_MEM_SOFT_DIRTY
1072
1073 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1074 {
1075         struct page *page;
1076
1077         if (!pte_write(pte))
1078                 return false;
1079         if (!is_cow_mapping(vma->vm_flags))
1080                 return false;
1081         if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
1082                 return false;
1083         page = vm_normal_page(vma, addr, pte);
1084         if (!page)
1085                 return false;
1086         return page_maybe_dma_pinned(page);
1087 }
1088
1089 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1090                 unsigned long addr, pte_t *pte)
1091 {
1092         /*
1093          * The soft-dirty tracker uses #PF-s to catch writes
1094          * to pages, so write-protect the pte as well. See the
1095          * Documentation/admin-guide/mm/soft-dirty.rst for full description
1096          * of how soft-dirty works.
1097          */
1098         pte_t ptent = ptep_get(pte);
1099
1100         if (pte_present(ptent)) {
1101                 pte_t old_pte;
1102
1103                 if (pte_is_pinned(vma, addr, ptent))
1104                         return;
1105                 old_pte = ptep_modify_prot_start(vma, addr, pte);
1106                 ptent = pte_wrprotect(old_pte);
1107                 ptent = pte_clear_soft_dirty(ptent);
1108                 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
1109         } else if (is_swap_pte(ptent)) {
1110                 ptent = pte_swp_clear_soft_dirty(ptent);
1111                 set_pte_at(vma->vm_mm, addr, pte, ptent);
1112         }
1113 }
1114 #else
1115 static inline void clear_soft_dirty(struct vm_area_struct *vma,
1116                 unsigned long addr, pte_t *pte)
1117 {
1118 }
1119 #endif
1120
1121 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1122 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1123                 unsigned long addr, pmd_t *pmdp)
1124 {
1125         pmd_t old, pmd = *pmdp;
1126
1127         if (pmd_present(pmd)) {
1128                 /* See comment in change_huge_pmd() */
1129                 old = pmdp_invalidate(vma, addr, pmdp);
1130                 if (pmd_dirty(old))
1131                         pmd = pmd_mkdirty(pmd);
1132                 if (pmd_young(old))
1133                         pmd = pmd_mkyoung(pmd);
1134
1135                 pmd = pmd_wrprotect(pmd);
1136                 pmd = pmd_clear_soft_dirty(pmd);
1137
1138                 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1139         } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1140                 pmd = pmd_swp_clear_soft_dirty(pmd);
1141                 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1142         }
1143 }
1144 #else
1145 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1146                 unsigned long addr, pmd_t *pmdp)
1147 {
1148 }
1149 #endif
1150
1151 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
1152                                 unsigned long end, struct mm_walk *walk)
1153 {
1154         struct clear_refs_private *cp = walk->private;
1155         struct vm_area_struct *vma = walk->vma;
1156         pte_t *pte, ptent;
1157         spinlock_t *ptl;
1158         struct page *page;
1159
1160         ptl = pmd_trans_huge_lock(pmd, vma);
1161         if (ptl) {
1162                 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1163                         clear_soft_dirty_pmd(vma, addr, pmd);
1164                         goto out;
1165                 }
1166
1167                 if (!pmd_present(*pmd))
1168                         goto out;
1169
1170                 page = pmd_page(*pmd);
1171
1172                 /* Clear accessed and referenced bits. */
1173                 pmdp_test_and_clear_young(vma, addr, pmd);
1174                 test_and_clear_page_young(page);
1175                 ClearPageReferenced(page);
1176 out:
1177                 spin_unlock(ptl);
1178                 return 0;
1179         }
1180
1181         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1182         if (!pte) {
1183                 walk->action = ACTION_AGAIN;
1184                 return 0;
1185         }
1186         for (; addr != end; pte++, addr += PAGE_SIZE) {
1187                 ptent = ptep_get(pte);
1188
1189                 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1190                         clear_soft_dirty(vma, addr, pte);
1191                         continue;
1192                 }
1193
1194                 if (!pte_present(ptent))
1195                         continue;
1196
1197                 page = vm_normal_page(vma, addr, ptent);
1198                 if (!page)
1199                         continue;
1200
1201                 /* Clear accessed and referenced bits. */
1202                 ptep_test_and_clear_young(vma, addr, pte);
1203                 test_and_clear_page_young(page);
1204                 ClearPageReferenced(page);
1205         }
1206         pte_unmap_unlock(pte - 1, ptl);
1207         cond_resched();
1208         return 0;
1209 }
1210
1211 static int clear_refs_test_walk(unsigned long start, unsigned long end,
1212                                 struct mm_walk *walk)
1213 {
1214         struct clear_refs_private *cp = walk->private;
1215         struct vm_area_struct *vma = walk->vma;
1216
1217         if (vma->vm_flags & VM_PFNMAP)
1218                 return 1;
1219
1220         /*
1221          * Writing 1 to /proc/pid/clear_refs affects all pages.
1222          * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1223          * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1224          * Writing 4 to /proc/pid/clear_refs affects all pages.
1225          */
1226         if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1227                 return 1;
1228         if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1229                 return 1;
1230         return 0;
1231 }
1232
1233 static const struct mm_walk_ops clear_refs_walk_ops = {
1234         .pmd_entry              = clear_refs_pte_range,
1235         .test_walk              = clear_refs_test_walk,
1236         .walk_lock              = PGWALK_WRLOCK,
1237 };
1238
1239 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1240                                 size_t count, loff_t *ppos)
1241 {
1242         struct task_struct *task;
1243         char buffer[PROC_NUMBUF];
1244         struct mm_struct *mm;
1245         struct vm_area_struct *vma;
1246         enum clear_refs_types type;
1247         int itype;
1248         int rv;
1249
1250         memset(buffer, 0, sizeof(buffer));
1251         if (count > sizeof(buffer) - 1)
1252                 count = sizeof(buffer) - 1;
1253         if (copy_from_user(buffer, buf, count))
1254                 return -EFAULT;
1255         rv = kstrtoint(strstrip(buffer), 10, &itype);
1256         if (rv < 0)
1257                 return rv;
1258         type = (enum clear_refs_types)itype;
1259         if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1260                 return -EINVAL;
1261
1262         task = get_proc_task(file_inode(file));
1263         if (!task)
1264                 return -ESRCH;
1265         mm = get_task_mm(task);
1266         if (mm) {
1267                 VMA_ITERATOR(vmi, mm, 0);
1268                 struct mmu_notifier_range range;
1269                 struct clear_refs_private cp = {
1270                         .type = type,
1271                 };
1272
1273                 if (mmap_write_lock_killable(mm)) {
1274                         count = -EINTR;
1275                         goto out_mm;
1276                 }
1277                 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1278                         /*
1279                          * Writing 5 to /proc/pid/clear_refs resets the peak
1280                          * resident set size to this mm's current rss value.
1281                          */
1282                         reset_mm_hiwater_rss(mm);
1283                         goto out_unlock;
1284                 }
1285
1286                 if (type == CLEAR_REFS_SOFT_DIRTY) {
1287                         for_each_vma(vmi, vma) {
1288                                 if (!(vma->vm_flags & VM_SOFTDIRTY))
1289                                         continue;
1290                                 vm_flags_clear(vma, VM_SOFTDIRTY);
1291                                 vma_set_page_prot(vma);
1292                         }
1293
1294                         inc_tlb_flush_pending(mm);
1295                         mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1296                                                 0, mm, 0, -1UL);
1297                         mmu_notifier_invalidate_range_start(&range);
1298                 }
1299                 walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
1300                 if (type == CLEAR_REFS_SOFT_DIRTY) {
1301                         mmu_notifier_invalidate_range_end(&range);
1302                         flush_tlb_mm(mm);
1303                         dec_tlb_flush_pending(mm);
1304                 }
1305 out_unlock:
1306                 mmap_write_unlock(mm);
1307 out_mm:
1308                 mmput(mm);
1309         }
1310         put_task_struct(task);
1311
1312         return count;
1313 }
1314
1315 const struct file_operations proc_clear_refs_operations = {
1316         .write          = clear_refs_write,
1317         .llseek         = noop_llseek,
1318 };
1319
1320 typedef struct {
1321         u64 pme;
1322 } pagemap_entry_t;
1323
1324 struct pagemapread {
1325         int pos, len;           /* units: PM_ENTRY_BYTES, not bytes */
1326         pagemap_entry_t *buffer;
1327         bool show_pfn;
1328 };
1329
1330 #define PAGEMAP_WALK_SIZE       (PMD_SIZE)
1331 #define PAGEMAP_WALK_MASK       (PMD_MASK)
1332
1333 #define PM_ENTRY_BYTES          sizeof(pagemap_entry_t)
1334 #define PM_PFRAME_BITS          55
1335 #define PM_PFRAME_MASK          GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1336 #define PM_SOFT_DIRTY           BIT_ULL(55)
1337 #define PM_MMAP_EXCLUSIVE       BIT_ULL(56)
1338 #define PM_UFFD_WP              BIT_ULL(57)
1339 #define PM_FILE                 BIT_ULL(61)
1340 #define PM_SWAP                 BIT_ULL(62)
1341 #define PM_PRESENT              BIT_ULL(63)
1342
1343 #define PM_END_OF_BUFFER    1
1344
1345 static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1346 {
1347         return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1348 }
1349
1350 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1351                           struct pagemapread *pm)
1352 {
1353         pm->buffer[pm->pos++] = *pme;
1354         if (pm->pos >= pm->len)
1355                 return PM_END_OF_BUFFER;
1356         return 0;
1357 }
1358
1359 static int pagemap_pte_hole(unsigned long start, unsigned long end,
1360                             __always_unused int depth, struct mm_walk *walk)
1361 {
1362         struct pagemapread *pm = walk->private;
1363         unsigned long addr = start;
1364         int err = 0;
1365
1366         while (addr < end) {
1367                 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1368                 pagemap_entry_t pme = make_pme(0, 0);
1369                 /* End of address space hole, which we mark as non-present. */
1370                 unsigned long hole_end;
1371
1372                 if (vma)
1373                         hole_end = min(end, vma->vm_start);
1374                 else
1375                         hole_end = end;
1376
1377                 for (; addr < hole_end; addr += PAGE_SIZE) {
1378                         err = add_to_pagemap(addr, &pme, pm);
1379                         if (err)
1380                                 goto out;
1381                 }
1382
1383                 if (!vma)
1384                         break;
1385
1386                 /* Addresses in the VMA. */
1387                 if (vma->vm_flags & VM_SOFTDIRTY)
1388                         pme = make_pme(0, PM_SOFT_DIRTY);
1389                 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1390                         err = add_to_pagemap(addr, &pme, pm);
1391                         if (err)
1392                                 goto out;
1393                 }
1394         }
1395 out:
1396         return err;
1397 }
1398
1399 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1400                 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1401 {
1402         u64 frame = 0, flags = 0;
1403         struct page *page = NULL;
1404         bool migration = false;
1405
1406         if (pte_present(pte)) {
1407                 if (pm->show_pfn)
1408                         frame = pte_pfn(pte);
1409                 flags |= PM_PRESENT;
1410                 page = vm_normal_page(vma, addr, pte);
1411                 if (pte_soft_dirty(pte))
1412                         flags |= PM_SOFT_DIRTY;
1413                 if (pte_uffd_wp(pte))
1414                         flags |= PM_UFFD_WP;
1415         } else if (is_swap_pte(pte)) {
1416                 swp_entry_t entry;
1417                 if (pte_swp_soft_dirty(pte))
1418                         flags |= PM_SOFT_DIRTY;
1419                 if (pte_swp_uffd_wp(pte))
1420                         flags |= PM_UFFD_WP;
1421                 entry = pte_to_swp_entry(pte);
1422                 if (pm->show_pfn) {
1423                         pgoff_t offset;
1424                         /*
1425                          * For PFN swap offsets, keeping the offset field
1426                          * to be PFN only to be compatible with old smaps.
1427                          */
1428                         if (is_pfn_swap_entry(entry))
1429                                 offset = swp_offset_pfn(entry);
1430                         else
1431                                 offset = swp_offset(entry);
1432                         frame = swp_type(entry) |
1433                             (offset << MAX_SWAPFILES_SHIFT);
1434                 }
1435                 flags |= PM_SWAP;
1436                 migration = is_migration_entry(entry);
1437                 if (is_pfn_swap_entry(entry))
1438                         page = pfn_swap_entry_to_page(entry);
1439                 if (pte_marker_entry_uffd_wp(entry))
1440                         flags |= PM_UFFD_WP;
1441         }
1442
1443         if (page && !PageAnon(page))
1444                 flags |= PM_FILE;
1445         if (page && !migration && page_mapcount(page) == 1)
1446                 flags |= PM_MMAP_EXCLUSIVE;
1447         if (vma->vm_flags & VM_SOFTDIRTY)
1448                 flags |= PM_SOFT_DIRTY;
1449
1450         return make_pme(frame, flags);
1451 }
1452
1453 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1454                              struct mm_walk *walk)
1455 {
1456         struct vm_area_struct *vma = walk->vma;
1457         struct pagemapread *pm = walk->private;
1458         spinlock_t *ptl;
1459         pte_t *pte, *orig_pte;
1460         int err = 0;
1461 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1462         bool migration = false;
1463
1464         ptl = pmd_trans_huge_lock(pmdp, vma);
1465         if (ptl) {
1466                 u64 flags = 0, frame = 0;
1467                 pmd_t pmd = *pmdp;
1468                 struct page *page = NULL;
1469
1470                 if (vma->vm_flags & VM_SOFTDIRTY)
1471                         flags |= PM_SOFT_DIRTY;
1472
1473                 if (pmd_present(pmd)) {
1474                         page = pmd_page(pmd);
1475
1476                         flags |= PM_PRESENT;
1477                         if (pmd_soft_dirty(pmd))
1478                                 flags |= PM_SOFT_DIRTY;
1479                         if (pmd_uffd_wp(pmd))
1480                                 flags |= PM_UFFD_WP;
1481                         if (pm->show_pfn)
1482                                 frame = pmd_pfn(pmd) +
1483                                         ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1484                 }
1485 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1486                 else if (is_swap_pmd(pmd)) {
1487                         swp_entry_t entry = pmd_to_swp_entry(pmd);
1488                         unsigned long offset;
1489
1490                         if (pm->show_pfn) {
1491                                 if (is_pfn_swap_entry(entry))
1492                                         offset = swp_offset_pfn(entry);
1493                                 else
1494                                         offset = swp_offset(entry);
1495                                 offset = offset +
1496                                         ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1497                                 frame = swp_type(entry) |
1498                                         (offset << MAX_SWAPFILES_SHIFT);
1499                         }
1500                         flags |= PM_SWAP;
1501                         if (pmd_swp_soft_dirty(pmd))
1502                                 flags |= PM_SOFT_DIRTY;
1503                         if (pmd_swp_uffd_wp(pmd))
1504                                 flags |= PM_UFFD_WP;
1505                         VM_BUG_ON(!is_pmd_migration_entry(pmd));
1506                         migration = is_migration_entry(entry);
1507                         page = pfn_swap_entry_to_page(entry);
1508                 }
1509 #endif
1510
1511                 if (page && !migration && page_mapcount(page) == 1)
1512                         flags |= PM_MMAP_EXCLUSIVE;
1513
1514                 for (; addr != end; addr += PAGE_SIZE) {
1515                         pagemap_entry_t pme = make_pme(frame, flags);
1516
1517                         err = add_to_pagemap(addr, &pme, pm);
1518                         if (err)
1519                                 break;
1520                         if (pm->show_pfn) {
1521                                 if (flags & PM_PRESENT)
1522                                         frame++;
1523                                 else if (flags & PM_SWAP)
1524                                         frame += (1 << MAX_SWAPFILES_SHIFT);
1525                         }
1526                 }
1527                 spin_unlock(ptl);
1528                 return err;
1529         }
1530 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1531
1532         /*
1533          * We can assume that @vma always points to a valid one and @end never
1534          * goes beyond vma->vm_end.
1535          */
1536         orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1537         if (!pte) {
1538                 walk->action = ACTION_AGAIN;
1539                 return err;
1540         }
1541         for (; addr < end; pte++, addr += PAGE_SIZE) {
1542                 pagemap_entry_t pme;
1543
1544                 pme = pte_to_pagemap_entry(pm, vma, addr, ptep_get(pte));
1545                 err = add_to_pagemap(addr, &pme, pm);
1546                 if (err)
1547                         break;
1548         }
1549         pte_unmap_unlock(orig_pte, ptl);
1550
1551         cond_resched();
1552
1553         return err;
1554 }
1555
1556 #ifdef CONFIG_HUGETLB_PAGE
1557 /* This function walks within one hugetlb entry in the single call */
1558 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1559                                  unsigned long addr, unsigned long end,
1560                                  struct mm_walk *walk)
1561 {
1562         struct pagemapread *pm = walk->private;
1563         struct vm_area_struct *vma = walk->vma;
1564         u64 flags = 0, frame = 0;
1565         int err = 0;
1566         pte_t pte;
1567
1568         if (vma->vm_flags & VM_SOFTDIRTY)
1569                 flags |= PM_SOFT_DIRTY;
1570
1571         pte = huge_ptep_get(ptep);
1572         if (pte_present(pte)) {
1573                 struct page *page = pte_page(pte);
1574
1575                 if (!PageAnon(page))
1576                         flags |= PM_FILE;
1577
1578                 if (page_mapcount(page) == 1)
1579                         flags |= PM_MMAP_EXCLUSIVE;
1580
1581                 if (huge_pte_uffd_wp(pte))
1582                         flags |= PM_UFFD_WP;
1583
1584                 flags |= PM_PRESENT;
1585                 if (pm->show_pfn)
1586                         frame = pte_pfn(pte) +
1587                                 ((addr & ~hmask) >> PAGE_SHIFT);
1588         } else if (pte_swp_uffd_wp_any(pte)) {
1589                 flags |= PM_UFFD_WP;
1590         }
1591
1592         for (; addr != end; addr += PAGE_SIZE) {
1593                 pagemap_entry_t pme = make_pme(frame, flags);
1594
1595                 err = add_to_pagemap(addr, &pme, pm);
1596                 if (err)
1597                         return err;
1598                 if (pm->show_pfn && (flags & PM_PRESENT))
1599                         frame++;
1600         }
1601
1602         cond_resched();
1603
1604         return err;
1605 }
1606 #else
1607 #define pagemap_hugetlb_range   NULL
1608 #endif /* HUGETLB_PAGE */
1609
1610 static const struct mm_walk_ops pagemap_ops = {
1611         .pmd_entry      = pagemap_pmd_range,
1612         .pte_hole       = pagemap_pte_hole,
1613         .hugetlb_entry  = pagemap_hugetlb_range,
1614         .walk_lock      = PGWALK_RDLOCK,
1615 };
1616
1617 /*
1618  * /proc/pid/pagemap - an array mapping virtual pages to pfns
1619  *
1620  * For each page in the address space, this file contains one 64-bit entry
1621  * consisting of the following:
1622  *
1623  * Bits 0-54  page frame number (PFN) if present
1624  * Bits 0-4   swap type if swapped
1625  * Bits 5-54  swap offset if swapped
1626  * Bit  55    pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
1627  * Bit  56    page exclusively mapped
1628  * Bit  57    pte is uffd-wp write-protected
1629  * Bits 58-60 zero
1630  * Bit  61    page is file-page or shared-anon
1631  * Bit  62    page swapped
1632  * Bit  63    page present
1633  *
1634  * If the page is not present but in swap, then the PFN contains an
1635  * encoding of the swap file number and the page's offset into the
1636  * swap. Unmapped pages return a null PFN. This allows determining
1637  * precisely which pages are mapped (or in swap) and comparing mapped
1638  * pages between processes.
1639  *
1640  * Efficient users of this interface will use /proc/pid/maps to
1641  * determine which areas of memory are actually mapped and llseek to
1642  * skip over unmapped regions.
1643  */
1644 static ssize_t pagemap_read(struct file *file, char __user *buf,
1645                             size_t count, loff_t *ppos)
1646 {
1647         struct mm_struct *mm = file->private_data;
1648         struct pagemapread pm;
1649         unsigned long src;
1650         unsigned long svpfn;
1651         unsigned long start_vaddr;
1652         unsigned long end_vaddr;
1653         int ret = 0, copied = 0;
1654
1655         if (!mm || !mmget_not_zero(mm))
1656                 goto out;
1657
1658         ret = -EINVAL;
1659         /* file position must be aligned */
1660         if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1661                 goto out_mm;
1662
1663         ret = 0;
1664         if (!count)
1665                 goto out_mm;
1666
1667         /* do not disclose physical addresses: attack vector */
1668         pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1669
1670         pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1671         pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
1672         ret = -ENOMEM;
1673         if (!pm.buffer)
1674                 goto out_mm;
1675
1676         src = *ppos;
1677         svpfn = src / PM_ENTRY_BYTES;
1678         end_vaddr = mm->task_size;
1679
1680         /* watch out for wraparound */
1681         start_vaddr = end_vaddr;
1682         if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
1683                 unsigned long end;
1684
1685                 ret = mmap_read_lock_killable(mm);
1686                 if (ret)
1687                         goto out_free;
1688                 start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
1689                 mmap_read_unlock(mm);
1690
1691                 end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
1692                 if (end >= start_vaddr && end < mm->task_size)
1693                         end_vaddr = end;
1694         }
1695
1696         /* Ensure the address is inside the task */
1697         if (start_vaddr > mm->task_size)
1698                 start_vaddr = end_vaddr;
1699
1700         ret = 0;
1701         while (count && (start_vaddr < end_vaddr)) {
1702                 int len;
1703                 unsigned long end;
1704
1705                 pm.pos = 0;
1706                 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1707                 /* overflow ? */
1708                 if (end < start_vaddr || end > end_vaddr)
1709                         end = end_vaddr;
1710                 ret = mmap_read_lock_killable(mm);
1711                 if (ret)
1712                         goto out_free;
1713                 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
1714                 mmap_read_unlock(mm);
1715                 start_vaddr = end;
1716
1717                 len = min(count, PM_ENTRY_BYTES * pm.pos);
1718                 if (copy_to_user(buf, pm.buffer, len)) {
1719                         ret = -EFAULT;
1720                         goto out_free;
1721                 }
1722                 copied += len;
1723                 buf += len;
1724                 count -= len;
1725         }
1726         *ppos += copied;
1727         if (!ret || ret == PM_END_OF_BUFFER)
1728                 ret = copied;
1729
1730 out_free:
1731         kfree(pm.buffer);
1732 out_mm:
1733         mmput(mm);
1734 out:
1735         return ret;
1736 }
1737
1738 static int pagemap_open(struct inode *inode, struct file *file)
1739 {
1740         struct mm_struct *mm;
1741
1742         mm = proc_mem_open(inode, PTRACE_MODE_READ);
1743         if (IS_ERR(mm))
1744                 return PTR_ERR(mm);
1745         file->private_data = mm;
1746         return 0;
1747 }
1748
1749 static int pagemap_release(struct inode *inode, struct file *file)
1750 {
1751         struct mm_struct *mm = file->private_data;
1752
1753         if (mm)
1754                 mmdrop(mm);
1755         return 0;
1756 }
1757
1758 const struct file_operations proc_pagemap_operations = {
1759         .llseek         = mem_lseek, /* borrow this */
1760         .read           = pagemap_read,
1761         .open           = pagemap_open,
1762         .release        = pagemap_release,
1763 };
1764 #endif /* CONFIG_PROC_PAGE_MONITOR */
1765
1766 #ifdef CONFIG_NUMA
1767
1768 struct numa_maps {
1769         unsigned long pages;
1770         unsigned long anon;
1771         unsigned long active;
1772         unsigned long writeback;
1773         unsigned long mapcount_max;
1774         unsigned long dirty;
1775         unsigned long swapcache;
1776         unsigned long node[MAX_NUMNODES];
1777 };
1778
1779 struct numa_maps_private {
1780         struct proc_maps_private proc_maps;
1781         struct numa_maps md;
1782 };
1783
1784 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1785                         unsigned long nr_pages)
1786 {
1787         int count = page_mapcount(page);
1788
1789         md->pages += nr_pages;
1790         if (pte_dirty || PageDirty(page))
1791                 md->dirty += nr_pages;
1792
1793         if (PageSwapCache(page))
1794                 md->swapcache += nr_pages;
1795
1796         if (PageActive(page) || PageUnevictable(page))
1797                 md->active += nr_pages;
1798
1799         if (PageWriteback(page))
1800                 md->writeback += nr_pages;
1801
1802         if (PageAnon(page))
1803                 md->anon += nr_pages;
1804
1805         if (count > md->mapcount_max)
1806                 md->mapcount_max = count;
1807
1808         md->node[page_to_nid(page)] += nr_pages;
1809 }
1810
1811 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1812                 unsigned long addr)
1813 {
1814         struct page *page;
1815         int nid;
1816
1817         if (!pte_present(pte))
1818                 return NULL;
1819
1820         page = vm_normal_page(vma, addr, pte);
1821         if (!page || is_zone_device_page(page))
1822                 return NULL;
1823
1824         if (PageReserved(page))
1825                 return NULL;
1826
1827         nid = page_to_nid(page);
1828         if (!node_isset(nid, node_states[N_MEMORY]))
1829                 return NULL;
1830
1831         return page;
1832 }
1833
1834 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1835 static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1836                                               struct vm_area_struct *vma,
1837                                               unsigned long addr)
1838 {
1839         struct page *page;
1840         int nid;
1841
1842         if (!pmd_present(pmd))
1843                 return NULL;
1844
1845         page = vm_normal_page_pmd(vma, addr, pmd);
1846         if (!page)
1847                 return NULL;
1848
1849         if (PageReserved(page))
1850                 return NULL;
1851
1852         nid = page_to_nid(page);
1853         if (!node_isset(nid, node_states[N_MEMORY]))
1854                 return NULL;
1855
1856         return page;
1857 }
1858 #endif
1859
1860 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1861                 unsigned long end, struct mm_walk *walk)
1862 {
1863         struct numa_maps *md = walk->private;
1864         struct vm_area_struct *vma = walk->vma;
1865         spinlock_t *ptl;
1866         pte_t *orig_pte;
1867         pte_t *pte;
1868
1869 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1870         ptl = pmd_trans_huge_lock(pmd, vma);
1871         if (ptl) {
1872                 struct page *page;
1873
1874                 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1875                 if (page)
1876                         gather_stats(page, md, pmd_dirty(*pmd),
1877                                      HPAGE_PMD_SIZE/PAGE_SIZE);
1878                 spin_unlock(ptl);
1879                 return 0;
1880         }
1881 #endif
1882         orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1883         if (!pte) {
1884                 walk->action = ACTION_AGAIN;
1885                 return 0;
1886         }
1887         do {
1888                 pte_t ptent = ptep_get(pte);
1889                 struct page *page = can_gather_numa_stats(ptent, vma, addr);
1890                 if (!page)
1891                         continue;
1892                 gather_stats(page, md, pte_dirty(ptent), 1);
1893
1894         } while (pte++, addr += PAGE_SIZE, addr != end);
1895         pte_unmap_unlock(orig_pte, ptl);
1896         cond_resched();
1897         return 0;
1898 }
1899 #ifdef CONFIG_HUGETLB_PAGE
1900 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1901                 unsigned long addr, unsigned long end, struct mm_walk *walk)
1902 {
1903         pte_t huge_pte = huge_ptep_get(pte);
1904         struct numa_maps *md;
1905         struct page *page;
1906
1907         if (!pte_present(huge_pte))
1908                 return 0;
1909
1910         page = pte_page(huge_pte);
1911
1912         md = walk->private;
1913         gather_stats(page, md, pte_dirty(huge_pte), 1);
1914         return 0;
1915 }
1916
1917 #else
1918 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1919                 unsigned long addr, unsigned long end, struct mm_walk *walk)
1920 {
1921         return 0;
1922 }
1923 #endif
1924
1925 static const struct mm_walk_ops show_numa_ops = {
1926         .hugetlb_entry = gather_hugetlb_stats,
1927         .pmd_entry = gather_pte_stats,
1928         .walk_lock = PGWALK_RDLOCK,
1929 };
1930
1931 /*
1932  * Display pages allocated per node and memory policy via /proc.
1933  */
1934 static int show_numa_map(struct seq_file *m, void *v)
1935 {
1936         struct numa_maps_private *numa_priv = m->private;
1937         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1938         struct vm_area_struct *vma = v;
1939         struct numa_maps *md = &numa_priv->md;
1940         struct file *file = vma->vm_file;
1941         struct mm_struct *mm = vma->vm_mm;
1942         struct mempolicy *pol;
1943         char buffer[64];
1944         int nid;
1945
1946         if (!mm)
1947                 return 0;
1948
1949         /* Ensure we start with an empty set of numa_maps statistics. */
1950         memset(md, 0, sizeof(*md));
1951
1952         pol = __get_vma_policy(vma, vma->vm_start);
1953         if (pol) {
1954                 mpol_to_str(buffer, sizeof(buffer), pol);
1955                 mpol_cond_put(pol);
1956         } else {
1957                 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1958         }
1959
1960         seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1961
1962         if (file) {
1963                 seq_puts(m, " file=");
1964                 seq_file_path(m, file, "\n\t= ");
1965         } else if (vma_is_initial_heap(vma)) {
1966                 seq_puts(m, " heap");
1967         } else if (vma_is_initial_stack(vma)) {
1968                 seq_puts(m, " stack");
1969         }
1970
1971         if (is_vm_hugetlb_page(vma))
1972                 seq_puts(m, " huge");
1973
1974         /* mmap_lock is held by m_start */
1975         walk_page_vma(vma, &show_numa_ops, md);
1976
1977         if (!md->pages)
1978                 goto out;
1979
1980         if (md->anon)
1981                 seq_printf(m, " anon=%lu", md->anon);
1982
1983         if (md->dirty)
1984                 seq_printf(m, " dirty=%lu", md->dirty);
1985
1986         if (md->pages != md->anon && md->pages != md->dirty)
1987                 seq_printf(m, " mapped=%lu", md->pages);
1988
1989         if (md->mapcount_max > 1)
1990                 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1991
1992         if (md->swapcache)
1993                 seq_printf(m, " swapcache=%lu", md->swapcache);
1994
1995         if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1996                 seq_printf(m, " active=%lu", md->active);
1997
1998         if (md->writeback)
1999                 seq_printf(m, " writeback=%lu", md->writeback);
2000
2001         for_each_node_state(nid, N_MEMORY)
2002                 if (md->node[nid])
2003                         seq_printf(m, " N%d=%lu", nid, md->node[nid]);
2004
2005         seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
2006 out:
2007         seq_putc(m, '\n');
2008         return 0;
2009 }
2010
2011 static const struct seq_operations proc_pid_numa_maps_op = {
2012         .start  = m_start,
2013         .next   = m_next,
2014         .stop   = m_stop,
2015         .show   = show_numa_map,
2016 };
2017
2018 static int pid_numa_maps_open(struct inode *inode, struct file *file)
2019 {
2020         return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
2021                                 sizeof(struct numa_maps_private));
2022 }
2023
2024 const struct file_operations proc_pid_numa_maps_operations = {
2025         .open           = pid_numa_maps_open,
2026         .read           = seq_read,
2027         .llseek         = seq_lseek,
2028         .release        = proc_map_release,
2029 };
2030
2031 #endif /* CONFIG_NUMA */