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