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