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