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