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