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