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