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