1
2#include <linux/pagewalk.h>
3#include <linux/vmacache.h>
4#include <linux/hugetlb.h>
5#include <linux/huge_mm.h>
6#include <linux/mount.h>
7#include <linux/seq_file.h>
8#include <linux/highmem.h>
9#include <linux/ptrace.h>
10#include <linux/slab.h>
11#include <linux/pagemap.h>
12#include <linux/mempolicy.h>
13#include <linux/rmap.h>
14#include <linux/swap.h>
15#include <linux/sched/mm.h>
16#include <linux/swapops.h>
17#include <linux/mmu_notifier.h>
18#include <linux/page_idle.h>
19#include <linux/shmem_fs.h>
20#include <linux/uaccess.h>
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)
30void 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
41
42
43
44
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
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
82unsigned long task_vsize(struct mm_struct *mm)
83{
84 return PAGE_SIZE * mm->total_vm;
85}
86
87unsigned 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
103
104static 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}
113static void release_task_mempolicy(struct proc_maps_private *priv)
114{
115 mpol_put(priv->task_mempolicy);
116}
117#else
118static void hold_task_mempolicy(struct proc_maps_private *priv)
119{
120}
121static void release_task_mempolicy(struct proc_maps_private *priv)
122{
123}
124#endif
125
126static struct vm_area_struct *
127m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
128{
129 if (vma == priv->tail_vma)
130 return NULL;
131 return vma->vm_next ?: priv->tail_vma;
132}
133
134static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
135{
136 if (m->count < m->size)
137 m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL;
138}
139
140static void *m_start(struct seq_file *m, loff_t *ppos)
141{
142 struct proc_maps_private *priv = m->private;
143 unsigned long last_addr = m->version;
144 struct mm_struct *mm;
145 struct vm_area_struct *vma;
146 unsigned int pos = *ppos;
147
148
149 if (last_addr == -1UL)
150 return NULL;
151
152 priv->task = get_proc_task(priv->inode);
153 if (!priv->task)
154 return ERR_PTR(-ESRCH);
155
156 mm = priv->mm;
157 if (!mm || !mmget_not_zero(mm)) {
158 put_task_struct(priv->task);
159 priv->task = NULL;
160 return NULL;
161 }
162
163 if (mmap_read_lock_killable(mm)) {
164 mmput(mm);
165 put_task_struct(priv->task);
166 priv->task = NULL;
167 return ERR_PTR(-EINTR);
168 }
169
170 hold_task_mempolicy(priv);
171 priv->tail_vma = get_gate_vma(mm);
172
173 if (last_addr) {
174 vma = find_vma(mm, last_addr - 1);
175 if (vma && vma->vm_start <= last_addr)
176 vma = m_next_vma(priv, vma);
177 if (vma)
178 return vma;
179 }
180
181 m->version = 0;
182 if (pos < mm->map_count) {
183 for (vma = mm->mmap; pos; pos--) {
184 m->version = vma->vm_start;
185 vma = vma->vm_next;
186 }
187 return vma;
188 }
189
190
191 if (pos == mm->map_count && priv->tail_vma)
192 return priv->tail_vma;
193
194 return NULL;
195}
196
197static void *m_next(struct seq_file *m, void *v, loff_t *pos)
198{
199 struct proc_maps_private *priv = m->private;
200 struct vm_area_struct *next;
201
202 (*pos)++;
203 next = m_next_vma(priv, v);
204 return next;
205}
206
207static void m_stop(struct seq_file *m, void *v)
208{
209 struct proc_maps_private *priv = m->private;
210 struct mm_struct *mm = priv->mm;
211
212 if (!priv->task)
213 return;
214
215 release_task_mempolicy(priv);
216 mmap_read_unlock(mm);
217 mmput(mm);
218 put_task_struct(priv->task);
219 priv->task = NULL;
220}
221
222static int proc_maps_open(struct inode *inode, struct file *file,
223 const struct seq_operations *ops, int psize)
224{
225 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
226
227 if (!priv)
228 return -ENOMEM;
229
230 priv->inode = inode;
231 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
232 if (IS_ERR(priv->mm)) {
233 int err = PTR_ERR(priv->mm);
234
235 seq_release_private(inode, file);
236 return err;
237 }
238
239 return 0;
240}
241
242static int proc_map_release(struct inode *inode, struct file *file)
243{
244 struct seq_file *seq = file->private_data;
245 struct proc_maps_private *priv = seq->private;
246
247 if (priv->mm)
248 mmdrop(priv->mm);
249
250 return seq_release_private(inode, file);
251}
252
253static int do_maps_open(struct inode *inode, struct file *file,
254 const struct seq_operations *ops)
255{
256 return proc_maps_open(inode, file, ops,
257 sizeof(struct proc_maps_private));
258}
259
260
261
262
263
264static int is_stack(struct vm_area_struct *vma)
265{
266
267
268
269
270
271 return vma->vm_start <= vma->vm_mm->start_stack &&
272 vma->vm_end >= vma->vm_mm->start_stack;
273}
274
275static void show_vma_header_prefix(struct seq_file *m,
276 unsigned long start, unsigned long end,
277 vm_flags_t flags, unsigned long long pgoff,
278 dev_t dev, unsigned long ino)
279{
280 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
281 seq_put_hex_ll(m, NULL, start, 8);
282 seq_put_hex_ll(m, "-", end, 8);
283 seq_putc(m, ' ');
284 seq_putc(m, flags & VM_READ ? 'r' : '-');
285 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
286 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
287 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
288 seq_put_hex_ll(m, " ", pgoff, 8);
289 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
290 seq_put_hex_ll(m, ":", MINOR(dev), 2);
291 seq_put_decimal_ull(m, " ", ino);
292 seq_putc(m, ' ');
293}
294
295static void
296show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
297{
298 struct mm_struct *mm = vma->vm_mm;
299 struct file *file = vma->vm_file;
300 vm_flags_t flags = vma->vm_flags;
301 unsigned long ino = 0;
302 unsigned long long pgoff = 0;
303 unsigned long start, end;
304 dev_t dev = 0;
305 const char *name = NULL;
306
307 if (file) {
308 struct inode *inode = file_inode(vma->vm_file);
309 dev = inode->i_sb->s_dev;
310 ino = inode->i_ino;
311 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
312 }
313
314 start = vma->vm_start;
315 end = vma->vm_end;
316 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
317
318
319
320
321
322 if (file) {
323 seq_pad(m, ' ');
324 seq_file_path(m, file, "\n");
325 goto done;
326 }
327
328 if (vma->vm_ops && vma->vm_ops->name) {
329 name = vma->vm_ops->name(vma);
330 if (name)
331 goto done;
332 }
333
334 name = arch_vma_name(vma);
335 if (!name) {
336 if (!mm) {
337 name = "[vdso]";
338 goto done;
339 }
340
341 if (vma->vm_start <= mm->brk &&
342 vma->vm_end >= mm->start_brk) {
343 name = "[heap]";
344 goto done;
345 }
346
347 if (is_stack(vma))
348 name = "[stack]";
349 }
350
351done:
352 if (name) {
353 seq_pad(m, ' ');
354 seq_puts(m, name);
355 }
356 seq_putc(m, '\n');
357}
358
359static int show_map(struct seq_file *m, void *v)
360{
361 show_map_vma(m, v);
362 m_cache_vma(m, v);
363 return 0;
364}
365
366static const struct seq_operations proc_pid_maps_op = {
367 .start = m_start,
368 .next = m_next,
369 .stop = m_stop,
370 .show = show_map
371};
372
373static int pid_maps_open(struct inode *inode, struct file *file)
374{
375 return do_maps_open(inode, file, &proc_pid_maps_op);
376}
377
378const struct file_operations proc_pid_maps_operations = {
379 .open = pid_maps_open,
380 .read = seq_read,
381 .llseek = seq_lseek,
382 .release = proc_map_release,
383};
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402#define PSS_SHIFT 12
403
404#ifdef CONFIG_PROC_PAGE_MONITOR
405struct mem_size_stats {
406 unsigned long resident;
407 unsigned long shared_clean;
408 unsigned long shared_dirty;
409 unsigned long private_clean;
410 unsigned long private_dirty;
411 unsigned long referenced;
412 unsigned long anonymous;
413 unsigned long lazyfree;
414 unsigned long anonymous_thp;
415 unsigned long shmem_thp;
416 unsigned long file_thp;
417 unsigned long swap;
418 unsigned long shared_hugetlb;
419 unsigned long private_hugetlb;
420 u64 pss;
421 u64 pss_locked;
422 u64 swap_pss;
423 bool check_shmem_swap;
424};
425
426static void smaps_account(struct mem_size_stats *mss, struct page *page,
427 bool compound, bool young, bool dirty)
428{
429 int i, nr = compound ? compound_nr(page) : 1;
430 unsigned long size = nr * PAGE_SIZE;
431
432 if (PageAnon(page)) {
433 mss->anonymous += size;
434 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
435 mss->lazyfree += size;
436 }
437
438 mss->resident += size;
439
440 if (young || page_is_young(page) || PageReferenced(page))
441 mss->referenced += size;
442
443
444
445
446
447
448 if (page_count(page) == 1) {
449 if (dirty || PageDirty(page))
450 mss->private_dirty += size;
451 else
452 mss->private_clean += size;
453 mss->pss += (u64)size << PSS_SHIFT;
454 return;
455 }
456
457 for (i = 0; i < nr; i++, page++) {
458 int mapcount = page_mapcount(page);
459
460 if (mapcount >= 2) {
461 if (dirty || PageDirty(page))
462 mss->shared_dirty += PAGE_SIZE;
463 else
464 mss->shared_clean += PAGE_SIZE;
465 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
466 } else {
467 if (dirty || PageDirty(page))
468 mss->private_dirty += PAGE_SIZE;
469 else
470 mss->private_clean += PAGE_SIZE;
471 mss->pss += PAGE_SIZE << PSS_SHIFT;
472 }
473 }
474}
475
476#ifdef CONFIG_SHMEM
477static int smaps_pte_hole(unsigned long addr, unsigned long end,
478 __always_unused int depth, struct mm_walk *walk)
479{
480 struct mem_size_stats *mss = walk->private;
481
482 mss->swap += shmem_partial_swap_usage(
483 walk->vma->vm_file->f_mapping, addr, end);
484
485 return 0;
486}
487#else
488#define smaps_pte_hole NULL
489#endif
490
491static void smaps_pte_entry(pte_t *pte, unsigned long addr,
492 struct mm_walk *walk)
493{
494 struct mem_size_stats *mss = walk->private;
495 struct vm_area_struct *vma = walk->vma;
496 struct page *page = NULL;
497
498 if (pte_present(*pte)) {
499 page = vm_normal_page(vma, addr, *pte);
500 } else if (is_swap_pte(*pte)) {
501 swp_entry_t swpent = pte_to_swp_entry(*pte);
502
503 if (!non_swap_entry(swpent)) {
504 int mapcount;
505
506 mss->swap += PAGE_SIZE;
507 mapcount = swp_swapcount(swpent);
508 if (mapcount >= 2) {
509 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
510
511 do_div(pss_delta, mapcount);
512 mss->swap_pss += pss_delta;
513 } else {
514 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
515 }
516 } else if (is_migration_entry(swpent))
517 page = migration_entry_to_page(swpent);
518 else if (is_device_private_entry(swpent))
519 page = device_private_entry_to_page(swpent);
520 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
521 && pte_none(*pte))) {
522 page = find_get_entry(vma->vm_file->f_mapping,
523 linear_page_index(vma, addr));
524 if (!page)
525 return;
526
527 if (xa_is_value(page))
528 mss->swap += PAGE_SIZE;
529 else
530 put_page(page);
531
532 return;
533 }
534
535 if (!page)
536 return;
537
538 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
539}
540
541#ifdef CONFIG_TRANSPARENT_HUGEPAGE
542static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
543 struct mm_walk *walk)
544{
545 struct mem_size_stats *mss = walk->private;
546 struct vm_area_struct *vma = walk->vma;
547 struct page *page = NULL;
548
549 if (pmd_present(*pmd)) {
550
551 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
552 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
553 swp_entry_t entry = pmd_to_swp_entry(*pmd);
554
555 if (is_migration_entry(entry))
556 page = migration_entry_to_page(entry);
557 }
558 if (IS_ERR_OR_NULL(page))
559 return;
560 if (PageAnon(page))
561 mss->anonymous_thp += HPAGE_PMD_SIZE;
562 else if (PageSwapBacked(page))
563 mss->shmem_thp += HPAGE_PMD_SIZE;
564 else if (is_zone_device_page(page))
565 ;
566 else
567 mss->file_thp += HPAGE_PMD_SIZE;
568 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
569}
570#else
571static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
572 struct mm_walk *walk)
573{
574}
575#endif
576
577static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
578 struct mm_walk *walk)
579{
580 struct vm_area_struct *vma = walk->vma;
581 pte_t *pte;
582 spinlock_t *ptl;
583
584 ptl = pmd_trans_huge_lock(pmd, vma);
585 if (ptl) {
586 smaps_pmd_entry(pmd, addr, walk);
587 spin_unlock(ptl);
588 goto out;
589 }
590
591 if (pmd_trans_unstable(pmd))
592 goto out;
593
594
595
596
597
598 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
599 for (; addr != end; pte++, addr += PAGE_SIZE)
600 smaps_pte_entry(pte, addr, walk);
601 pte_unmap_unlock(pte - 1, ptl);
602out:
603 cond_resched();
604 return 0;
605}
606
607static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
608{
609
610
611
612 static const char mnemonics[BITS_PER_LONG][2] = {
613
614
615
616 [0 ... (BITS_PER_LONG-1)] = "??",
617
618 [ilog2(VM_READ)] = "rd",
619 [ilog2(VM_WRITE)] = "wr",
620 [ilog2(VM_EXEC)] = "ex",
621 [ilog2(VM_SHARED)] = "sh",
622 [ilog2(VM_MAYREAD)] = "mr",
623 [ilog2(VM_MAYWRITE)] = "mw",
624 [ilog2(VM_MAYEXEC)] = "me",
625 [ilog2(VM_MAYSHARE)] = "ms",
626 [ilog2(VM_GROWSDOWN)] = "gd",
627 [ilog2(VM_PFNMAP)] = "pf",
628 [ilog2(VM_DENYWRITE)] = "dw",
629#ifdef CONFIG_X86_INTEL_MPX
630 [ilog2(VM_MPX)] = "mp",
631#endif
632 [ilog2(VM_LOCKED)] = "lo",
633 [ilog2(VM_IO)] = "io",
634 [ilog2(VM_SEQ_READ)] = "sr",
635 [ilog2(VM_RAND_READ)] = "rr",
636 [ilog2(VM_DONTCOPY)] = "dc",
637 [ilog2(VM_DONTEXPAND)] = "de",
638 [ilog2(VM_ACCOUNT)] = "ac",
639 [ilog2(VM_NORESERVE)] = "nr",
640 [ilog2(VM_HUGETLB)] = "ht",
641 [ilog2(VM_SYNC)] = "sf",
642 [ilog2(VM_ARCH_1)] = "ar",
643 [ilog2(VM_WIPEONFORK)] = "wf",
644 [ilog2(VM_DONTDUMP)] = "dd",
645#ifdef CONFIG_MEM_SOFT_DIRTY
646 [ilog2(VM_SOFTDIRTY)] = "sd",
647#endif
648 [ilog2(VM_MIXEDMAP)] = "mm",
649 [ilog2(VM_HUGEPAGE)] = "hg",
650 [ilog2(VM_NOHUGEPAGE)] = "nh",
651 [ilog2(VM_MERGEABLE)] = "mg",
652 [ilog2(VM_UFFD_MISSING)]= "um",
653 [ilog2(VM_UFFD_WP)] = "uw",
654#ifdef CONFIG_ARCH_HAS_PKEYS
655
656 [ilog2(VM_PKEY_BIT0)] = "",
657 [ilog2(VM_PKEY_BIT1)] = "",
658 [ilog2(VM_PKEY_BIT2)] = "",
659 [ilog2(VM_PKEY_BIT3)] = "",
660#if VM_PKEY_BIT4
661 [ilog2(VM_PKEY_BIT4)] = "",
662#endif
663#endif
664 };
665 size_t i;
666
667 seq_puts(m, "VmFlags: ");
668 for (i = 0; i < BITS_PER_LONG; i++) {
669 if (!mnemonics[i][0])
670 continue;
671 if (vma->vm_flags & (1UL << i)) {
672 seq_putc(m, mnemonics[i][0]);
673 seq_putc(m, mnemonics[i][1]);
674 seq_putc(m, ' ');
675 }
676 }
677 seq_putc(m, '\n');
678}
679
680#ifdef CONFIG_HUGETLB_PAGE
681static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
682 unsigned long addr, unsigned long end,
683 struct mm_walk *walk)
684{
685 struct mem_size_stats *mss = walk->private;
686 struct vm_area_struct *vma = walk->vma;
687 struct page *page = NULL;
688
689 if (pte_present(*pte)) {
690 page = vm_normal_page(vma, addr, *pte);
691 } else if (is_swap_pte(*pte)) {
692 swp_entry_t swpent = pte_to_swp_entry(*pte);
693
694 if (is_migration_entry(swpent))
695 page = migration_entry_to_page(swpent);
696 else if (is_device_private_entry(swpent))
697 page = device_private_entry_to_page(swpent);
698 }
699 if (page) {
700 int mapcount = page_mapcount(page);
701
702 if (mapcount >= 2)
703 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
704 else
705 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
706 }
707 return 0;
708}
709#else
710#define smaps_hugetlb_range NULL
711#endif
712
713static const struct mm_walk_ops smaps_walk_ops = {
714 .pmd_entry = smaps_pte_range,
715 .hugetlb_entry = smaps_hugetlb_range,
716};
717
718static const struct mm_walk_ops smaps_shmem_walk_ops = {
719 .pmd_entry = smaps_pte_range,
720 .hugetlb_entry = smaps_hugetlb_range,
721 .pte_hole = smaps_pte_hole,
722};
723
724static void smap_gather_stats(struct vm_area_struct *vma,
725 struct mem_size_stats *mss)
726{
727 bool walking = false;
728
729#ifdef CONFIG_SHMEM
730
731 mss->check_shmem_swap = false;
732 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
733
734
735
736
737
738
739
740
741
742
743 unsigned long shmem_swapped = shmem_swap_usage(vma);
744
745 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
746 !(vma->vm_flags & VM_WRITE)) {
747 mss->swap += shmem_swapped;
748 } else {
749 mss->check_shmem_swap = true;
750 walk_page_vma(vma, &smaps_shmem_walk_ops, mss);
751 walking = true;
752 }
753 }
754#endif
755
756
757 if (!walking)
758 walk_page_vma(vma, &smaps_walk_ops, mss);
759 if (vma->vm_flags & VM_LOCKED)
760 mss->pss_locked += mss->pss;
761}
762
763#define SEQ_PUT_DEC(str, val) \
764 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
765
766
767static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss)
768{
769 SEQ_PUT_DEC("Rss: ", mss->resident);
770 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
771 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
772 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
773 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
774 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
775 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
776 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
777 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
778 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
779 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
780 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
781 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
782 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
783 mss->private_hugetlb >> 10, 7);
784 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
785 SEQ_PUT_DEC(" kB\nSwapPss: ",
786 mss->swap_pss >> PSS_SHIFT);
787 SEQ_PUT_DEC(" kB\nLocked: ",
788 mss->pss_locked >> PSS_SHIFT);
789 seq_puts(m, " kB\n");
790}
791
792static int show_smap(struct seq_file *m, void *v)
793{
794 struct vm_area_struct *vma = v;
795 struct mem_size_stats mss;
796
797 memset(&mss, 0, sizeof(mss));
798
799 smap_gather_stats(vma, &mss);
800
801 show_map_vma(m, vma);
802
803 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
804 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
805 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
806 seq_puts(m, " kB\n");
807
808 __show_smap(m, &mss);
809
810 seq_printf(m, "THPeligible: %d\n",
811 transparent_hugepage_enabled(vma));
812
813 if (arch_pkeys_enabled())
814 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
815 show_smap_vma_flags(m, vma);
816
817 m_cache_vma(m, vma);
818
819 return 0;
820}
821
822static int show_smaps_rollup(struct seq_file *m, void *v)
823{
824 struct proc_maps_private *priv = m->private;
825 struct mem_size_stats mss;
826 struct mm_struct *mm;
827 struct vm_area_struct *vma;
828 unsigned long last_vma_end = 0;
829 int ret = 0;
830
831 priv->task = get_proc_task(priv->inode);
832 if (!priv->task)
833 return -ESRCH;
834
835 mm = priv->mm;
836 if (!mm || !mmget_not_zero(mm)) {
837 ret = -ESRCH;
838 goto out_put_task;
839 }
840
841 memset(&mss, 0, sizeof(mss));
842
843 ret = mmap_read_lock_killable(mm);
844 if (ret)
845 goto out_put_mm;
846
847 hold_task_mempolicy(priv);
848
849 for (vma = priv->mm->mmap; vma; vma = vma->vm_next) {
850 smap_gather_stats(vma, &mss);
851 last_vma_end = vma->vm_end;
852 }
853
854 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
855 last_vma_end, 0, 0, 0, 0);
856 seq_pad(m, ' ');
857 seq_puts(m, "[rollup]\n");
858
859 __show_smap(m, &mss);
860
861 release_task_mempolicy(priv);
862 mmap_read_unlock(mm);
863
864out_put_mm:
865 mmput(mm);
866out_put_task:
867 put_task_struct(priv->task);
868 priv->task = NULL;
869
870 return ret;
871}
872#undef SEQ_PUT_DEC
873
874static const struct seq_operations proc_pid_smaps_op = {
875 .start = m_start,
876 .next = m_next,
877 .stop = m_stop,
878 .show = show_smap
879};
880
881static int pid_smaps_open(struct inode *inode, struct file *file)
882{
883 return do_maps_open(inode, file, &proc_pid_smaps_op);
884}
885
886static int smaps_rollup_open(struct inode *inode, struct file *file)
887{
888 int ret;
889 struct proc_maps_private *priv;
890
891 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
892 if (!priv)
893 return -ENOMEM;
894
895 ret = single_open(file, show_smaps_rollup, priv);
896 if (ret)
897 goto out_free;
898
899 priv->inode = inode;
900 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
901 if (IS_ERR(priv->mm)) {
902 ret = PTR_ERR(priv->mm);
903
904 single_release(inode, file);
905 goto out_free;
906 }
907
908 return 0;
909
910out_free:
911 kfree(priv);
912 return ret;
913}
914
915static int smaps_rollup_release(struct inode *inode, struct file *file)
916{
917 struct seq_file *seq = file->private_data;
918 struct proc_maps_private *priv = seq->private;
919
920 if (priv->mm)
921 mmdrop(priv->mm);
922
923 kfree(priv);
924 return single_release(inode, file);
925}
926
927const struct file_operations proc_pid_smaps_operations = {
928 .open = pid_smaps_open,
929 .read = seq_read,
930 .llseek = seq_lseek,
931 .release = proc_map_release,
932};
933
934const struct file_operations proc_pid_smaps_rollup_operations = {
935 .open = smaps_rollup_open,
936 .read = seq_read,
937 .llseek = seq_lseek,
938 .release = smaps_rollup_release,
939};
940
941enum clear_refs_types {
942 CLEAR_REFS_ALL = 1,
943 CLEAR_REFS_ANON,
944 CLEAR_REFS_MAPPED,
945 CLEAR_REFS_SOFT_DIRTY,
946 CLEAR_REFS_MM_HIWATER_RSS,
947 CLEAR_REFS_LAST,
948};
949
950struct clear_refs_private {
951 enum clear_refs_types type;
952};
953
954#ifdef CONFIG_MEM_SOFT_DIRTY
955static inline void clear_soft_dirty(struct vm_area_struct *vma,
956 unsigned long addr, pte_t *pte)
957{
958
959
960
961
962
963
964 pte_t ptent = *pte;
965
966 if (pte_present(ptent)) {
967 pte_t old_pte;
968
969 old_pte = ptep_modify_prot_start(vma, addr, pte);
970 ptent = pte_wrprotect(old_pte);
971 ptent = pte_clear_soft_dirty(ptent);
972 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
973 } else if (is_swap_pte(ptent)) {
974 ptent = pte_swp_clear_soft_dirty(ptent);
975 set_pte_at(vma->vm_mm, addr, pte, ptent);
976 }
977}
978#else
979static inline void clear_soft_dirty(struct vm_area_struct *vma,
980 unsigned long addr, pte_t *pte)
981{
982}
983#endif
984
985#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
986static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
987 unsigned long addr, pmd_t *pmdp)
988{
989 pmd_t old, pmd = *pmdp;
990
991 if (pmd_present(pmd)) {
992
993 old = pmdp_invalidate(vma, addr, pmdp);
994 if (pmd_dirty(old))
995 pmd = pmd_mkdirty(pmd);
996 if (pmd_young(old))
997 pmd = pmd_mkyoung(pmd);
998
999 pmd = pmd_wrprotect(pmd);
1000 pmd = pmd_clear_soft_dirty(pmd);
1001
1002 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1003 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1004 pmd = pmd_swp_clear_soft_dirty(pmd);
1005 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1006 }
1007}
1008#else
1009static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1010 unsigned long addr, pmd_t *pmdp)
1011{
1012}
1013#endif
1014
1015static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
1016 unsigned long end, struct mm_walk *walk)
1017{
1018 struct clear_refs_private *cp = walk->private;
1019 struct vm_area_struct *vma = walk->vma;
1020 pte_t *pte, ptent;
1021 spinlock_t *ptl;
1022 struct page *page;
1023
1024 ptl = pmd_trans_huge_lock(pmd, vma);
1025 if (ptl) {
1026 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1027 clear_soft_dirty_pmd(vma, addr, pmd);
1028 goto out;
1029 }
1030
1031 if (!pmd_present(*pmd))
1032 goto out;
1033
1034 page = pmd_page(*pmd);
1035
1036
1037 pmdp_test_and_clear_young(vma, addr, pmd);
1038 test_and_clear_page_young(page);
1039 ClearPageReferenced(page);
1040out:
1041 spin_unlock(ptl);
1042 return 0;
1043 }
1044
1045 if (pmd_trans_unstable(pmd))
1046 return 0;
1047
1048 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1049 for (; addr != end; pte++, addr += PAGE_SIZE) {
1050 ptent = *pte;
1051
1052 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1053 clear_soft_dirty(vma, addr, pte);
1054 continue;
1055 }
1056
1057 if (!pte_present(ptent))
1058 continue;
1059
1060 page = vm_normal_page(vma, addr, ptent);
1061 if (!page)
1062 continue;
1063
1064
1065 ptep_test_and_clear_young(vma, addr, pte);
1066 test_and_clear_page_young(page);
1067 ClearPageReferenced(page);
1068 }
1069 pte_unmap_unlock(pte - 1, ptl);
1070 cond_resched();
1071 return 0;
1072}
1073
1074static int clear_refs_test_walk(unsigned long start, unsigned long end,
1075 struct mm_walk *walk)
1076{
1077 struct clear_refs_private *cp = walk->private;
1078 struct vm_area_struct *vma = walk->vma;
1079
1080 if (vma->vm_flags & VM_PFNMAP)
1081 return 1;
1082
1083
1084
1085
1086
1087
1088
1089 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1090 return 1;
1091 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1092 return 1;
1093 return 0;
1094}
1095
1096static const struct mm_walk_ops clear_refs_walk_ops = {
1097 .pmd_entry = clear_refs_pte_range,
1098 .test_walk = clear_refs_test_walk,
1099};
1100
1101static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1102 size_t count, loff_t *ppos)
1103{
1104 struct task_struct *task;
1105 char buffer[PROC_NUMBUF];
1106 struct mm_struct *mm;
1107 struct vm_area_struct *vma;
1108 enum clear_refs_types type;
1109 struct mmu_gather tlb;
1110 int itype;
1111 int rv;
1112
1113 memset(buffer, 0, sizeof(buffer));
1114 if (count > sizeof(buffer) - 1)
1115 count = sizeof(buffer) - 1;
1116 if (copy_from_user(buffer, buf, count))
1117 return -EFAULT;
1118 rv = kstrtoint(strstrip(buffer), 10, &itype);
1119 if (rv < 0)
1120 return rv;
1121 type = (enum clear_refs_types)itype;
1122 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
1123 return -EINVAL;
1124
1125 task = get_proc_task(file_inode(file));
1126 if (!task)
1127 return -ESRCH;
1128 mm = get_task_mm(task);
1129 if (mm) {
1130 struct mmu_notifier_range range;
1131 struct clear_refs_private cp = {
1132 .type = type,
1133 };
1134
1135 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1136 if (mmap_write_lock_killable(mm)) {
1137 count = -EINTR;
1138 goto out_mm;
1139 }
1140
1141
1142
1143
1144
1145 reset_mm_hiwater_rss(mm);
1146 mmap_write_unlock(mm);
1147 goto out_mm;
1148 }
1149
1150 if (mmap_read_lock_killable(mm)) {
1151 count = -EINTR;
1152 goto out_mm;
1153 }
1154 tlb_gather_mmu(&tlb, mm, 0, -1);
1155 if (type == CLEAR_REFS_SOFT_DIRTY) {
1156 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1157 if (!(vma->vm_flags & VM_SOFTDIRTY))
1158 continue;
1159 mmap_read_unlock(mm);
1160 if (mmap_write_lock_killable(mm)) {
1161 count = -EINTR;
1162 goto out_mm;
1163 }
1164
1165
1166
1167
1168
1169 if (!mmget_still_valid(mm)) {
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 mmap_write_unlock(mm);
1180 goto out_mm;
1181 }
1182 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1183 vma->vm_flags &= ~VM_SOFTDIRTY;
1184 vma_set_page_prot(vma);
1185 }
1186 mmap_write_downgrade(mm);
1187 break;
1188 }
1189
1190 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1191 0, NULL, mm, 0, -1UL);
1192 mmu_notifier_invalidate_range_start(&range);
1193 }
1194 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1195 &cp);
1196 if (type == CLEAR_REFS_SOFT_DIRTY)
1197 mmu_notifier_invalidate_range_end(&range);
1198 tlb_finish_mmu(&tlb, 0, -1);
1199 mmap_read_unlock(mm);
1200out_mm:
1201 mmput(mm);
1202 }
1203 put_task_struct(task);
1204
1205 return count;
1206}
1207
1208const struct file_operations proc_clear_refs_operations = {
1209 .write = clear_refs_write,
1210 .llseek = noop_llseek,
1211};
1212
1213typedef struct {
1214 u64 pme;
1215} pagemap_entry_t;
1216
1217struct pagemapread {
1218 int pos, len;
1219 pagemap_entry_t *buffer;
1220 bool show_pfn;
1221};
1222
1223#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1224#define PAGEMAP_WALK_MASK (PMD_MASK)
1225
1226#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1227#define PM_PFRAME_BITS 55
1228#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1229#define PM_SOFT_DIRTY BIT_ULL(55)
1230#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1231#define PM_FILE BIT_ULL(61)
1232#define PM_SWAP BIT_ULL(62)
1233#define PM_PRESENT BIT_ULL(63)
1234
1235#define PM_END_OF_BUFFER 1
1236
1237static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
1238{
1239 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
1240}
1241
1242static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
1243 struct pagemapread *pm)
1244{
1245 pm->buffer[pm->pos++] = *pme;
1246 if (pm->pos >= pm->len)
1247 return PM_END_OF_BUFFER;
1248 return 0;
1249}
1250
1251static int pagemap_pte_hole(unsigned long start, unsigned long end,
1252 __always_unused int depth, struct mm_walk *walk)
1253{
1254 struct pagemapread *pm = walk->private;
1255 unsigned long addr = start;
1256 int err = 0;
1257
1258 while (addr < end) {
1259 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1260 pagemap_entry_t pme = make_pme(0, 0);
1261
1262 unsigned long hole_end;
1263
1264 if (vma)
1265 hole_end = min(end, vma->vm_start);
1266 else
1267 hole_end = end;
1268
1269 for (; addr < hole_end; addr += PAGE_SIZE) {
1270 err = add_to_pagemap(addr, &pme, pm);
1271 if (err)
1272 goto out;
1273 }
1274
1275 if (!vma)
1276 break;
1277
1278
1279 if (vma->vm_flags & VM_SOFTDIRTY)
1280 pme = make_pme(0, PM_SOFT_DIRTY);
1281 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1282 err = add_to_pagemap(addr, &pme, pm);
1283 if (err)
1284 goto out;
1285 }
1286 }
1287out:
1288 return err;
1289}
1290
1291static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
1292 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1293{
1294 u64 frame = 0, flags = 0;
1295 struct page *page = NULL;
1296
1297 if (pte_present(pte)) {
1298 if (pm->show_pfn)
1299 frame = pte_pfn(pte);
1300 flags |= PM_PRESENT;
1301 page = vm_normal_page(vma, addr, pte);
1302 if (pte_soft_dirty(pte))
1303 flags |= PM_SOFT_DIRTY;
1304 } else if (is_swap_pte(pte)) {
1305 swp_entry_t entry;
1306 if (pte_swp_soft_dirty(pte))
1307 flags |= PM_SOFT_DIRTY;
1308 entry = pte_to_swp_entry(pte);
1309 if (pm->show_pfn)
1310 frame = swp_type(entry) |
1311 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1312 flags |= PM_SWAP;
1313 if (is_migration_entry(entry))
1314 page = migration_entry_to_page(entry);
1315
1316 if (is_device_private_entry(entry))
1317 page = device_private_entry_to_page(entry);
1318 }
1319
1320 if (page && !PageAnon(page))
1321 flags |= PM_FILE;
1322 if (page && page_mapcount(page) == 1)
1323 flags |= PM_MMAP_EXCLUSIVE;
1324 if (vma->vm_flags & VM_SOFTDIRTY)
1325 flags |= PM_SOFT_DIRTY;
1326
1327 return make_pme(frame, flags);
1328}
1329
1330static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
1331 struct mm_walk *walk)
1332{
1333 struct vm_area_struct *vma = walk->vma;
1334 struct pagemapread *pm = walk->private;
1335 spinlock_t *ptl;
1336 pte_t *pte, *orig_pte;
1337 int err = 0;
1338
1339#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1340 ptl = pmd_trans_huge_lock(pmdp, vma);
1341 if (ptl) {
1342 u64 flags = 0, frame = 0;
1343 pmd_t pmd = *pmdp;
1344 struct page *page = NULL;
1345
1346 if (vma->vm_flags & VM_SOFTDIRTY)
1347 flags |= PM_SOFT_DIRTY;
1348
1349 if (pmd_present(pmd)) {
1350 page = pmd_page(pmd);
1351
1352 flags |= PM_PRESENT;
1353 if (pmd_soft_dirty(pmd))
1354 flags |= PM_SOFT_DIRTY;
1355 if (pm->show_pfn)
1356 frame = pmd_pfn(pmd) +
1357 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1358 }
1359#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1360 else if (is_swap_pmd(pmd)) {
1361 swp_entry_t entry = pmd_to_swp_entry(pmd);
1362 unsigned long offset;
1363
1364 if (pm->show_pfn) {
1365 offset = swp_offset(entry) +
1366 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1367 frame = swp_type(entry) |
1368 (offset << MAX_SWAPFILES_SHIFT);
1369 }
1370 flags |= PM_SWAP;
1371 if (pmd_swp_soft_dirty(pmd))
1372 flags |= PM_SOFT_DIRTY;
1373 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1374 page = migration_entry_to_page(entry);
1375 }
1376#endif
1377
1378 if (page && page_mapcount(page) == 1)
1379 flags |= PM_MMAP_EXCLUSIVE;
1380
1381 for (; addr != end; addr += PAGE_SIZE) {
1382 pagemap_entry_t pme = make_pme(frame, flags);
1383
1384 err = add_to_pagemap(addr, &pme, pm);
1385 if (err)
1386 break;
1387 if (pm->show_pfn) {
1388 if (flags & PM_PRESENT)
1389 frame++;
1390 else if (flags & PM_SWAP)
1391 frame += (1 << MAX_SWAPFILES_SHIFT);
1392 }
1393 }
1394 spin_unlock(ptl);
1395 return err;
1396 }
1397
1398 if (pmd_trans_unstable(pmdp))
1399 return 0;
1400#endif
1401
1402
1403
1404
1405
1406 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
1407 for (; addr < end; pte++, addr += PAGE_SIZE) {
1408 pagemap_entry_t pme;
1409
1410 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
1411 err = add_to_pagemap(addr, &pme, pm);
1412 if (err)
1413 break;
1414 }
1415 pte_unmap_unlock(orig_pte, ptl);
1416
1417 cond_resched();
1418
1419 return err;
1420}
1421
1422#ifdef CONFIG_HUGETLB_PAGE
1423
1424static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
1425 unsigned long addr, unsigned long end,
1426 struct mm_walk *walk)
1427{
1428 struct pagemapread *pm = walk->private;
1429 struct vm_area_struct *vma = walk->vma;
1430 u64 flags = 0, frame = 0;
1431 int err = 0;
1432 pte_t pte;
1433
1434 if (vma->vm_flags & VM_SOFTDIRTY)
1435 flags |= PM_SOFT_DIRTY;
1436
1437 pte = huge_ptep_get(ptep);
1438 if (pte_present(pte)) {
1439 struct page *page = pte_page(pte);
1440
1441 if (!PageAnon(page))
1442 flags |= PM_FILE;
1443
1444 if (page_mapcount(page) == 1)
1445 flags |= PM_MMAP_EXCLUSIVE;
1446
1447 flags |= PM_PRESENT;
1448 if (pm->show_pfn)
1449 frame = pte_pfn(pte) +
1450 ((addr & ~hmask) >> PAGE_SHIFT);
1451 }
1452
1453 for (; addr != end; addr += PAGE_SIZE) {
1454 pagemap_entry_t pme = make_pme(frame, flags);
1455
1456 err = add_to_pagemap(addr, &pme, pm);
1457 if (err)
1458 return err;
1459 if (pm->show_pfn && (flags & PM_PRESENT))
1460 frame++;
1461 }
1462
1463 cond_resched();
1464
1465 return err;
1466}
1467#else
1468#define pagemap_hugetlb_range NULL
1469#endif
1470
1471static const struct mm_walk_ops pagemap_ops = {
1472 .pmd_entry = pagemap_pmd_range,
1473 .pte_hole = pagemap_pte_hole,
1474 .hugetlb_entry = pagemap_hugetlb_range,
1475};
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503static ssize_t pagemap_read(struct file *file, char __user *buf,
1504 size_t count, loff_t *ppos)
1505{
1506 struct mm_struct *mm = file->private_data;
1507 struct pagemapread pm;
1508 unsigned long src;
1509 unsigned long svpfn;
1510 unsigned long start_vaddr;
1511 unsigned long end_vaddr;
1512 int ret = 0, copied = 0;
1513
1514 if (!mm || !mmget_not_zero(mm))
1515 goto out;
1516
1517 ret = -EINVAL;
1518
1519 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1520 goto out_mm;
1521
1522 ret = 0;
1523 if (!count)
1524 goto out_mm;
1525
1526
1527 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1528
1529 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1530 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
1531 ret = -ENOMEM;
1532 if (!pm.buffer)
1533 goto out_mm;
1534
1535 src = *ppos;
1536 svpfn = src / PM_ENTRY_BYTES;
1537 start_vaddr = svpfn << PAGE_SHIFT;
1538 end_vaddr = mm->task_size;
1539
1540
1541 if (svpfn > mm->task_size >> PAGE_SHIFT)
1542 start_vaddr = end_vaddr;
1543
1544
1545
1546
1547
1548
1549
1550 ret = 0;
1551 while (count && (start_vaddr < end_vaddr)) {
1552 int len;
1553 unsigned long end;
1554
1555 pm.pos = 0;
1556 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1557
1558 if (end < start_vaddr || end > end_vaddr)
1559 end = end_vaddr;
1560 ret = mmap_read_lock_killable(mm);
1561 if (ret)
1562 goto out_free;
1563 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
1564 mmap_read_unlock(mm);
1565 start_vaddr = end;
1566
1567 len = min(count, PM_ENTRY_BYTES * pm.pos);
1568 if (copy_to_user(buf, pm.buffer, len)) {
1569 ret = -EFAULT;
1570 goto out_free;
1571 }
1572 copied += len;
1573 buf += len;
1574 count -= len;
1575 }
1576 *ppos += copied;
1577 if (!ret || ret == PM_END_OF_BUFFER)
1578 ret = copied;
1579
1580out_free:
1581 kfree(pm.buffer);
1582out_mm:
1583 mmput(mm);
1584out:
1585 return ret;
1586}
1587
1588static int pagemap_open(struct inode *inode, struct file *file)
1589{
1590 struct mm_struct *mm;
1591
1592 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1593 if (IS_ERR(mm))
1594 return PTR_ERR(mm);
1595 file->private_data = mm;
1596 return 0;
1597}
1598
1599static int pagemap_release(struct inode *inode, struct file *file)
1600{
1601 struct mm_struct *mm = file->private_data;
1602
1603 if (mm)
1604 mmdrop(mm);
1605 return 0;
1606}
1607
1608const struct file_operations proc_pagemap_operations = {
1609 .llseek = mem_lseek,
1610 .read = pagemap_read,
1611 .open = pagemap_open,
1612 .release = pagemap_release,
1613};
1614#endif
1615
1616#ifdef CONFIG_NUMA
1617
1618struct numa_maps {
1619 unsigned long pages;
1620 unsigned long anon;
1621 unsigned long active;
1622 unsigned long writeback;
1623 unsigned long mapcount_max;
1624 unsigned long dirty;
1625 unsigned long swapcache;
1626 unsigned long node[MAX_NUMNODES];
1627};
1628
1629struct numa_maps_private {
1630 struct proc_maps_private proc_maps;
1631 struct numa_maps md;
1632};
1633
1634static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1635 unsigned long nr_pages)
1636{
1637 int count = page_mapcount(page);
1638
1639 md->pages += nr_pages;
1640 if (pte_dirty || PageDirty(page))
1641 md->dirty += nr_pages;
1642
1643 if (PageSwapCache(page))
1644 md->swapcache += nr_pages;
1645
1646 if (PageActive(page) || PageUnevictable(page))
1647 md->active += nr_pages;
1648
1649 if (PageWriteback(page))
1650 md->writeback += nr_pages;
1651
1652 if (PageAnon(page))
1653 md->anon += nr_pages;
1654
1655 if (count > md->mapcount_max)
1656 md->mapcount_max = count;
1657
1658 md->node[page_to_nid(page)] += nr_pages;
1659}
1660
1661static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1662 unsigned long addr)
1663{
1664 struct page *page;
1665 int nid;
1666
1667 if (!pte_present(pte))
1668 return NULL;
1669
1670 page = vm_normal_page(vma, addr, pte);
1671 if (!page)
1672 return NULL;
1673
1674 if (PageReserved(page))
1675 return NULL;
1676
1677 nid = page_to_nid(page);
1678 if (!node_isset(nid, node_states[N_MEMORY]))
1679 return NULL;
1680
1681 return page;
1682}
1683
1684#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1685static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1686 struct vm_area_struct *vma,
1687 unsigned long addr)
1688{
1689 struct page *page;
1690 int nid;
1691
1692 if (!pmd_present(pmd))
1693 return NULL;
1694
1695 page = vm_normal_page_pmd(vma, addr, pmd);
1696 if (!page)
1697 return NULL;
1698
1699 if (PageReserved(page))
1700 return NULL;
1701
1702 nid = page_to_nid(page);
1703 if (!node_isset(nid, node_states[N_MEMORY]))
1704 return NULL;
1705
1706 return page;
1707}
1708#endif
1709
1710static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1711 unsigned long end, struct mm_walk *walk)
1712{
1713 struct numa_maps *md = walk->private;
1714 struct vm_area_struct *vma = walk->vma;
1715 spinlock_t *ptl;
1716 pte_t *orig_pte;
1717 pte_t *pte;
1718
1719#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1720 ptl = pmd_trans_huge_lock(pmd, vma);
1721 if (ptl) {
1722 struct page *page;
1723
1724 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
1725 if (page)
1726 gather_stats(page, md, pmd_dirty(*pmd),
1727 HPAGE_PMD_SIZE/PAGE_SIZE);
1728 spin_unlock(ptl);
1729 return 0;
1730 }
1731
1732 if (pmd_trans_unstable(pmd))
1733 return 0;
1734#endif
1735 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1736 do {
1737 struct page *page = can_gather_numa_stats(*pte, vma, addr);
1738 if (!page)
1739 continue;
1740 gather_stats(page, md, pte_dirty(*pte), 1);
1741
1742 } while (pte++, addr += PAGE_SIZE, addr != end);
1743 pte_unmap_unlock(orig_pte, ptl);
1744 cond_resched();
1745 return 0;
1746}
1747#ifdef CONFIG_HUGETLB_PAGE
1748static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1749 unsigned long addr, unsigned long end, struct mm_walk *walk)
1750{
1751 pte_t huge_pte = huge_ptep_get(pte);
1752 struct numa_maps *md;
1753 struct page *page;
1754
1755 if (!pte_present(huge_pte))
1756 return 0;
1757
1758 page = pte_page(huge_pte);
1759 if (!page)
1760 return 0;
1761
1762 md = walk->private;
1763 gather_stats(page, md, pte_dirty(huge_pte), 1);
1764 return 0;
1765}
1766
1767#else
1768static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
1769 unsigned long addr, unsigned long end, struct mm_walk *walk)
1770{
1771 return 0;
1772}
1773#endif
1774
1775static const struct mm_walk_ops show_numa_ops = {
1776 .hugetlb_entry = gather_hugetlb_stats,
1777 .pmd_entry = gather_pte_stats,
1778};
1779
1780
1781
1782
1783static int show_numa_map(struct seq_file *m, void *v)
1784{
1785 struct numa_maps_private *numa_priv = m->private;
1786 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1787 struct vm_area_struct *vma = v;
1788 struct numa_maps *md = &numa_priv->md;
1789 struct file *file = vma->vm_file;
1790 struct mm_struct *mm = vma->vm_mm;
1791 struct mempolicy *pol;
1792 char buffer[64];
1793 int nid;
1794
1795 if (!mm)
1796 return 0;
1797
1798
1799 memset(md, 0, sizeof(*md));
1800
1801 pol = __get_vma_policy(vma, vma->vm_start);
1802 if (pol) {
1803 mpol_to_str(buffer, sizeof(buffer), pol);
1804 mpol_cond_put(pol);
1805 } else {
1806 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1807 }
1808
1809 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1810
1811 if (file) {
1812 seq_puts(m, " file=");
1813 seq_file_path(m, file, "\n\t= ");
1814 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1815 seq_puts(m, " heap");
1816 } else if (is_stack(vma)) {
1817 seq_puts(m, " stack");
1818 }
1819
1820 if (is_vm_hugetlb_page(vma))
1821 seq_puts(m, " huge");
1822
1823
1824 walk_page_vma(vma, &show_numa_ops, md);
1825
1826 if (!md->pages)
1827 goto out;
1828
1829 if (md->anon)
1830 seq_printf(m, " anon=%lu", md->anon);
1831
1832 if (md->dirty)
1833 seq_printf(m, " dirty=%lu", md->dirty);
1834
1835 if (md->pages != md->anon && md->pages != md->dirty)
1836 seq_printf(m, " mapped=%lu", md->pages);
1837
1838 if (md->mapcount_max > 1)
1839 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1840
1841 if (md->swapcache)
1842 seq_printf(m, " swapcache=%lu", md->swapcache);
1843
1844 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1845 seq_printf(m, " active=%lu", md->active);
1846
1847 if (md->writeback)
1848 seq_printf(m, " writeback=%lu", md->writeback);
1849
1850 for_each_node_state(nid, N_MEMORY)
1851 if (md->node[nid])
1852 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
1853
1854 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
1855out:
1856 seq_putc(m, '\n');
1857 m_cache_vma(m, vma);
1858 return 0;
1859}
1860
1861static const struct seq_operations proc_pid_numa_maps_op = {
1862 .start = m_start,
1863 .next = m_next,
1864 .stop = m_stop,
1865 .show = show_numa_map,
1866};
1867
1868static int pid_numa_maps_open(struct inode *inode, struct file *file)
1869{
1870 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1871 sizeof(struct numa_maps_private));
1872}
1873
1874const struct file_operations proc_pid_numa_maps_operations = {
1875 .open = pid_numa_maps_open,
1876 .read = seq_read,
1877 .llseek = seq_lseek,
1878 .release = proc_map_release,
1879};
1880
1881#endif
1882