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