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