Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
task_mmu.c
Go to the documentation of this file.
1 #include <linux/mm.h>
2 #include <linux/hugetlb.h>
3 #include <linux/huge_mm.h>
4 #include <linux/mount.h>
5 #include <linux/seq_file.h>
6 #include <linux/highmem.h>
7 #include <linux/ptrace.h>
8 #include <linux/slab.h>
9 #include <linux/pagemap.h>
10 #include <linux/mempolicy.h>
11 #include <linux/rmap.h>
12 #include <linux/swap.h>
13 #include <linux/swapops.h>
14 
15 #include <asm/elf.h>
16 #include <asm/uaccess.h>
17 #include <asm/tlbflush.h>
18 #include "internal.h"
19 
20 void task_mem(struct seq_file *m, struct mm_struct *mm)
21 {
22  unsigned long data, text, lib, swap;
23  unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
24 
25  /*
26  * Note: to minimize their overhead, mm maintains hiwater_vm and
27  * hiwater_rss only when about to *lower* total_vm or rss. Any
28  * collector of these hiwater stats must therefore get total_vm
29  * and rss too, which will usually be the higher. Barriers? not
30  * worth the effort, such snapshots can always be inconsistent.
31  */
32  hiwater_vm = total_vm = mm->total_vm;
33  if (hiwater_vm < mm->hiwater_vm)
34  hiwater_vm = mm->hiwater_vm;
35  hiwater_rss = total_rss = get_mm_rss(mm);
36  if (hiwater_rss < mm->hiwater_rss)
37  hiwater_rss = mm->hiwater_rss;
38 
39  data = mm->total_vm - mm->shared_vm - mm->stack_vm;
40  text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
41  lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
42  swap = get_mm_counter(mm, MM_SWAPENTS);
43  seq_printf(m,
44  "VmPeak:\t%8lu kB\n"
45  "VmSize:\t%8lu kB\n"
46  "VmLck:\t%8lu kB\n"
47  "VmPin:\t%8lu kB\n"
48  "VmHWM:\t%8lu kB\n"
49  "VmRSS:\t%8lu kB\n"
50  "VmData:\t%8lu kB\n"
51  "VmStk:\t%8lu kB\n"
52  "VmExe:\t%8lu kB\n"
53  "VmLib:\t%8lu kB\n"
54  "VmPTE:\t%8lu kB\n"
55  "VmSwap:\t%8lu kB\n",
56  hiwater_vm << (PAGE_SHIFT-10),
57  total_vm << (PAGE_SHIFT-10),
58  mm->locked_vm << (PAGE_SHIFT-10),
59  mm->pinned_vm << (PAGE_SHIFT-10),
60  hiwater_rss << (PAGE_SHIFT-10),
61  total_rss << (PAGE_SHIFT-10),
62  data << (PAGE_SHIFT-10),
63  mm->stack_vm << (PAGE_SHIFT-10), text, lib,
64  (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
65  swap << (PAGE_SHIFT-10));
66 }
67 
68 unsigned long task_vsize(struct mm_struct *mm)
69 {
70  return PAGE_SIZE * mm->total_vm;
71 }
72 
73 unsigned long task_statm(struct mm_struct *mm,
74  unsigned long *shared, unsigned long *text,
75  unsigned long *data, unsigned long *resident)
76 {
77  *shared = get_mm_counter(mm, MM_FILEPAGES);
78  *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
79  >> PAGE_SHIFT;
80  *data = mm->total_vm - mm->shared_vm;
81  *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
82  return mm->total_vm;
83 }
84 
85 static void pad_len_spaces(struct seq_file *m, int len)
86 {
87  len = 25 + sizeof(void*) * 6 - len;
88  if (len < 1)
89  len = 1;
90  seq_printf(m, "%*c", len, ' ');
91 }
92 
93 #ifdef CONFIG_NUMA
94 /*
95  * These functions are for numa_maps but called in generic **maps seq_file
96  * ->start(), ->stop() ops.
97  *
98  * numa_maps scans all vmas under mmap_sem and checks their mempolicy.
99  * Each mempolicy object is controlled by reference counting. The problem here
100  * is how to avoid accessing dead mempolicy object.
101  *
102  * Because we're holding mmap_sem while reading seq_file, it's safe to access
103  * each vma's mempolicy, no vma objects will never drop refs to mempolicy.
104  *
105  * A task's mempolicy (task->mempolicy) has different behavior. task->mempolicy
106  * is set and replaced under mmap_sem but unrefed and cleared under task_lock().
107  * So, without task_lock(), we cannot trust get_vma_policy() because we cannot
108  * gurantee the task never exits under us. But taking task_lock() around
109  * get_vma_plicy() causes lock order problem.
110  *
111  * To access task->mempolicy without lock, we hold a reference count of an
112  * object pointed by task->mempolicy and remember it. This will guarantee
113  * that task->mempolicy points to an alive object or NULL in numa_maps accesses.
114  */
115 static void hold_task_mempolicy(struct proc_maps_private *priv)
116 {
117  struct task_struct *task = priv->task;
118 
119  task_lock(task);
120  priv->task_mempolicy = task->mempolicy;
121  mpol_get(priv->task_mempolicy);
122  task_unlock(task);
123 }
124 static void release_task_mempolicy(struct proc_maps_private *priv)
125 {
126  mpol_put(priv->task_mempolicy);
127 }
128 #else
129 static void hold_task_mempolicy(struct proc_maps_private *priv)
130 {
131 }
132 static void release_task_mempolicy(struct proc_maps_private *priv)
133 {
134 }
135 #endif
136 
137 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
138 {
139  if (vma && vma != priv->tail_vma) {
140  struct mm_struct *mm = vma->vm_mm;
141  release_task_mempolicy(priv);
142  up_read(&mm->mmap_sem);
143  mmput(mm);
144  }
145 }
146 
147 static void *m_start(struct seq_file *m, loff_t *pos)
148 {
149  struct proc_maps_private *priv = m->private;
150  unsigned long last_addr = m->version;
151  struct mm_struct *mm;
152  struct vm_area_struct *vma, *tail_vma = NULL;
153  loff_t l = *pos;
154 
155  /* Clear the per syscall fields in priv */
156  priv->task = NULL;
157  priv->tail_vma = NULL;
158 
159  /*
160  * We remember last_addr rather than next_addr to hit with
161  * mmap_cache most of the time. We have zero last_addr at
162  * the beginning and also after lseek. We will have -1 last_addr
163  * after the end of the vmas.
164  */
165 
166  if (last_addr == -1UL)
167  return NULL;
168 
169  priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
170  if (!priv->task)
171  return ERR_PTR(-ESRCH);
172 
173  mm = mm_access(priv->task, PTRACE_MODE_READ);
174  if (!mm || IS_ERR(mm))
175  return mm;
176  down_read(&mm->mmap_sem);
177 
178  tail_vma = get_gate_vma(priv->task->mm);
179  priv->tail_vma = tail_vma;
180  hold_task_mempolicy(priv);
181  /* Start with last addr hint */
182  vma = find_vma(mm, last_addr);
183  if (last_addr && vma) {
184  vma = vma->vm_next;
185  goto out;
186  }
187 
188  /*
189  * Check the vma index is within the range and do
190  * sequential scan until m_index.
191  */
192  vma = NULL;
193  if ((unsigned long)l < mm->map_count) {
194  vma = mm->mmap;
195  while (l-- && vma)
196  vma = vma->vm_next;
197  goto out;
198  }
199 
200  if (l != mm->map_count)
201  tail_vma = NULL; /* After gate vma */
202 
203 out:
204  if (vma)
205  return vma;
206 
207  release_task_mempolicy(priv);
208  /* End of vmas has been reached */
209  m->version = (tail_vma != NULL)? 0: -1UL;
210  up_read(&mm->mmap_sem);
211  mmput(mm);
212  return tail_vma;
213 }
214 
215 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
216 {
217  struct proc_maps_private *priv = m->private;
218  struct vm_area_struct *vma = v;
219  struct vm_area_struct *tail_vma = priv->tail_vma;
220 
221  (*pos)++;
222  if (vma && (vma != tail_vma) && vma->vm_next)
223  return vma->vm_next;
224  vma_stop(priv, vma);
225  return (vma != tail_vma)? tail_vma: NULL;
226 }
227 
228 static void m_stop(struct seq_file *m, void *v)
229 {
230  struct proc_maps_private *priv = m->private;
231  struct vm_area_struct *vma = v;
232 
233  if (!IS_ERR(vma))
234  vma_stop(priv, vma);
235  if (priv->task)
236  put_task_struct(priv->task);
237 }
238 
239 static int do_maps_open(struct inode *inode, struct file *file,
240  const struct seq_operations *ops)
241 {
242  struct proc_maps_private *priv;
243  int ret = -ENOMEM;
244  priv = kzalloc(sizeof(*priv), GFP_KERNEL);
245  if (priv) {
246  priv->pid = proc_pid(inode);
247  ret = seq_open(file, ops);
248  if (!ret) {
249  struct seq_file *m = file->private_data;
250  m->private = priv;
251  } else {
252  kfree(priv);
253  }
254  }
255  return ret;
256 }
257 
258 static void
259 show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
260 {
261  struct mm_struct *mm = vma->vm_mm;
262  struct file *file = vma->vm_file;
263  struct proc_maps_private *priv = m->private;
264  struct task_struct *task = priv->task;
265  vm_flags_t flags = vma->vm_flags;
266  unsigned long ino = 0;
267  unsigned long long pgoff = 0;
268  unsigned long start, end;
269  dev_t dev = 0;
270  int len;
271  const char *name = NULL;
272 
273  if (file) {
274  struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
275  dev = inode->i_sb->s_dev;
276  ino = inode->i_ino;
277  pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
278  }
279 
280  /* We don't show the stack guard page in /proc/maps */
281  start = vma->vm_start;
282  if (stack_guard_page_start(vma, start))
283  start += PAGE_SIZE;
284  end = vma->vm_end;
285  if (stack_guard_page_end(vma, end))
286  end -= PAGE_SIZE;
287 
288  seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
289  start,
290  end,
291  flags & VM_READ ? 'r' : '-',
292  flags & VM_WRITE ? 'w' : '-',
293  flags & VM_EXEC ? 'x' : '-',
294  flags & VM_MAYSHARE ? 's' : 'p',
295  pgoff,
296  MAJOR(dev), MINOR(dev), ino, &len);
297 
298  /*
299  * Print the dentry name for named mappings, and a
300  * special [heap] marker for the heap:
301  */
302  if (file) {
303  pad_len_spaces(m, len);
304  seq_path(m, &file->f_path, "\n");
305  goto done;
306  }
307 
308  name = arch_vma_name(vma);
309  if (!name) {
310  pid_t tid;
311 
312  if (!mm) {
313  name = "[vdso]";
314  goto done;
315  }
316 
317  if (vma->vm_start <= mm->brk &&
318  vma->vm_end >= mm->start_brk) {
319  name = "[heap]";
320  goto done;
321  }
322 
323  tid = vm_is_stack(task, vma, is_pid);
324 
325  if (tid != 0) {
326  /*
327  * Thread stack in /proc/PID/task/TID/maps or
328  * the main process stack.
329  */
330  if (!is_pid || (vma->vm_start <= mm->start_stack &&
331  vma->vm_end >= mm->start_stack)) {
332  name = "[stack]";
333  } else {
334  /* Thread stack in /proc/PID/maps */
335  pad_len_spaces(m, len);
336  seq_printf(m, "[stack:%d]", tid);
337  }
338  }
339  }
340 
341 done:
342  if (name) {
343  pad_len_spaces(m, len);
344  seq_puts(m, name);
345  }
346  seq_putc(m, '\n');
347 }
348 
349 static int show_map(struct seq_file *m, void *v, int is_pid)
350 {
351  struct vm_area_struct *vma = v;
352  struct proc_maps_private *priv = m->private;
353  struct task_struct *task = priv->task;
354 
355  show_map_vma(m, vma, is_pid);
356 
357  if (m->count < m->size) /* vma is copied successfully */
358  m->version = (vma != get_gate_vma(task->mm))
359  ? vma->vm_start : 0;
360  return 0;
361 }
362 
363 static int show_pid_map(struct seq_file *m, void *v)
364 {
365  return show_map(m, v, 1);
366 }
367 
368 static int show_tid_map(struct seq_file *m, void *v)
369 {
370  return show_map(m, v, 0);
371 }
372 
373 static const struct seq_operations proc_pid_maps_op = {
374  .start = m_start,
375  .next = m_next,
376  .stop = m_stop,
377  .show = show_pid_map
378 };
379 
380 static const struct seq_operations proc_tid_maps_op = {
381  .start = m_start,
382  .next = m_next,
383  .stop = m_stop,
384  .show = show_tid_map
385 };
386 
387 static int pid_maps_open(struct inode *inode, struct file *file)
388 {
389  return do_maps_open(inode, file, &proc_pid_maps_op);
390 }
391 
392 static int tid_maps_open(struct inode *inode, struct file *file)
393 {
394  return do_maps_open(inode, file, &proc_tid_maps_op);
395 }
396 
398  .open = pid_maps_open,
399  .read = seq_read,
400  .llseek = seq_lseek,
401  .release = seq_release_private,
402 };
403 
405  .open = tid_maps_open,
406  .read = seq_read,
407  .llseek = seq_lseek,
408  .release = seq_release_private,
409 };
410 
411 /*
412  * Proportional Set Size(PSS): my share of RSS.
413  *
414  * PSS of a process is the count of pages it has in memory, where each
415  * page is divided by the number of processes sharing it. So if a
416  * process has 1000 pages all to itself, and 1000 shared with one other
417  * process, its PSS will be 1500.
418  *
419  * To keep (accumulated) division errors low, we adopt a 64bit
420  * fixed-point pss counter to minimize division errors. So (pss >>
421  * PSS_SHIFT) would be the real byte count.
422  *
423  * A shift of 12 before division means (assuming 4K page size):
424  * - 1M 3-user-pages add up to 8KB errors;
425  * - supports mapcount up to 2^24, or 16M;
426  * - supports PSS up to 2^52 bytes, or 4PB.
427  */
428 #define PSS_SHIFT 12
429 
430 #ifdef CONFIG_PROC_PAGE_MONITOR
431 struct mem_size_stats {
432  struct vm_area_struct *vma;
433  unsigned long resident;
434  unsigned long shared_clean;
435  unsigned long shared_dirty;
436  unsigned long private_clean;
437  unsigned long private_dirty;
438  unsigned long referenced;
439  unsigned long anonymous;
440  unsigned long anonymous_thp;
441  unsigned long swap;
442  unsigned long nonlinear;
443  u64 pss;
444 };
445 
446 
447 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
448  unsigned long ptent_size, struct mm_walk *walk)
449 {
450  struct mem_size_stats *mss = walk->private;
451  struct vm_area_struct *vma = mss->vma;
452  pgoff_t pgoff = linear_page_index(vma, addr);
453  struct page *page = NULL;
454  int mapcount;
455 
456  if (pte_present(ptent)) {
457  page = vm_normal_page(vma, addr, ptent);
458  } else if (is_swap_pte(ptent)) {
459  swp_entry_t swpent = pte_to_swp_entry(ptent);
460 
461  if (!non_swap_entry(swpent))
462  mss->swap += ptent_size;
463  else if (is_migration_entry(swpent))
464  page = migration_entry_to_page(swpent);
465  } else if (pte_file(ptent)) {
466  if (pte_to_pgoff(ptent) != pgoff)
467  mss->nonlinear += ptent_size;
468  }
469 
470  if (!page)
471  return;
472 
473  if (PageAnon(page))
474  mss->anonymous += ptent_size;
475 
476  if (page->index != pgoff)
477  mss->nonlinear += ptent_size;
478 
479  mss->resident += ptent_size;
480  /* Accumulate the size in pages that have been accessed. */
481  if (pte_young(ptent) || PageReferenced(page))
482  mss->referenced += ptent_size;
483  mapcount = page_mapcount(page);
484  if (mapcount >= 2) {
485  if (pte_dirty(ptent) || PageDirty(page))
486  mss->shared_dirty += ptent_size;
487  else
488  mss->shared_clean += ptent_size;
489  mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
490  } else {
491  if (pte_dirty(ptent) || PageDirty(page))
492  mss->private_dirty += ptent_size;
493  else
494  mss->private_clean += ptent_size;
495  mss->pss += (ptent_size << PSS_SHIFT);
496  }
497 }
498 
499 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
500  struct mm_walk *walk)
501 {
502  struct mem_size_stats *mss = walk->private;
503  struct vm_area_struct *vma = mss->vma;
504  pte_t *pte;
505  spinlock_t *ptl;
506 
507  if (pmd_trans_huge_lock(pmd, vma) == 1) {
508  smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk);
509  spin_unlock(&walk->mm->page_table_lock);
510  mss->anonymous_thp += HPAGE_PMD_SIZE;
511  return 0;
512  }
513 
514  if (pmd_trans_unstable(pmd))
515  return 0;
516  /*
517  * The mmap_sem held all the way back in m_start() is what
518  * keeps khugepaged out of here and from collapsing things
519  * in here.
520  */
521  pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
522  for (; addr != end; pte++, addr += PAGE_SIZE)
523  smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
524  pte_unmap_unlock(pte - 1, ptl);
525  cond_resched();
526  return 0;
527 }
528 
529 static int show_smap(struct seq_file *m, void *v, int is_pid)
530 {
531  struct proc_maps_private *priv = m->private;
532  struct task_struct *task = priv->task;
533  struct vm_area_struct *vma = v;
534  struct mem_size_stats mss;
535  struct mm_walk smaps_walk = {
536  .pmd_entry = smaps_pte_range,
537  .mm = vma->vm_mm,
538  .private = &mss,
539  };
540 
541  memset(&mss, 0, sizeof mss);
542  mss.vma = vma;
543  /* mmap_sem is held in m_start */
544  if (vma->vm_mm && !is_vm_hugetlb_page(vma))
545  walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
546 
547  show_map_vma(m, vma, is_pid);
548 
549  seq_printf(m,
550  "Size: %8lu kB\n"
551  "Rss: %8lu kB\n"
552  "Pss: %8lu kB\n"
553  "Shared_Clean: %8lu kB\n"
554  "Shared_Dirty: %8lu kB\n"
555  "Private_Clean: %8lu kB\n"
556  "Private_Dirty: %8lu kB\n"
557  "Referenced: %8lu kB\n"
558  "Anonymous: %8lu kB\n"
559  "AnonHugePages: %8lu kB\n"
560  "Swap: %8lu kB\n"
561  "KernelPageSize: %8lu kB\n"
562  "MMUPageSize: %8lu kB\n"
563  "Locked: %8lu kB\n",
564  (vma->vm_end - vma->vm_start) >> 10,
565  mss.resident >> 10,
566  (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
567  mss.shared_clean >> 10,
568  mss.shared_dirty >> 10,
569  mss.private_clean >> 10,
570  mss.private_dirty >> 10,
571  mss.referenced >> 10,
572  mss.anonymous >> 10,
573  mss.anonymous_thp >> 10,
574  mss.swap >> 10,
575  vma_kernel_pagesize(vma) >> 10,
576  vma_mmu_pagesize(vma) >> 10,
577  (vma->vm_flags & VM_LOCKED) ?
578  (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
579 
580  if (vma->vm_flags & VM_NONLINEAR)
581  seq_printf(m, "Nonlinear: %8lu kB\n",
582  mss.nonlinear >> 10);
583 
584  if (m->count < m->size) /* vma is copied successfully */
585  m->version = (vma != get_gate_vma(task->mm))
586  ? vma->vm_start : 0;
587  return 0;
588 }
589 
590 static int show_pid_smap(struct seq_file *m, void *v)
591 {
592  return show_smap(m, v, 1);
593 }
594 
595 static int show_tid_smap(struct seq_file *m, void *v)
596 {
597  return show_smap(m, v, 0);
598 }
599 
600 static const struct seq_operations proc_pid_smaps_op = {
601  .start = m_start,
602  .next = m_next,
603  .stop = m_stop,
604  .show = show_pid_smap
605 };
606 
607 static const struct seq_operations proc_tid_smaps_op = {
608  .start = m_start,
609  .next = m_next,
610  .stop = m_stop,
611  .show = show_tid_smap
612 };
613 
614 static int pid_smaps_open(struct inode *inode, struct file *file)
615 {
616  return do_maps_open(inode, file, &proc_pid_smaps_op);
617 }
618 
619 static int tid_smaps_open(struct inode *inode, struct file *file)
620 {
621  return do_maps_open(inode, file, &proc_tid_smaps_op);
622 }
623 
625  .open = pid_smaps_open,
626  .read = seq_read,
627  .llseek = seq_lseek,
628  .release = seq_release_private,
629 };
630 
632  .open = tid_smaps_open,
633  .read = seq_read,
634  .llseek = seq_lseek,
635  .release = seq_release_private,
636 };
637 
638 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
639  unsigned long end, struct mm_walk *walk)
640 {
641  struct vm_area_struct *vma = walk->private;
642  pte_t *pte, ptent;
643  spinlock_t *ptl;
644  struct page *page;
645 
646  split_huge_page_pmd(walk->mm, pmd);
647  if (pmd_trans_unstable(pmd))
648  return 0;
649 
650  pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
651  for (; addr != end; pte++, addr += PAGE_SIZE) {
652  ptent = *pte;
653  if (!pte_present(ptent))
654  continue;
655 
656  page = vm_normal_page(vma, addr, ptent);
657  if (!page)
658  continue;
659 
660  /* Clear accessed and referenced bits. */
661  ptep_test_and_clear_young(vma, addr, pte);
662  ClearPageReferenced(page);
663  }
664  pte_unmap_unlock(pte - 1, ptl);
665  cond_resched();
666  return 0;
667 }
668 
669 #define CLEAR_REFS_ALL 1
670 #define CLEAR_REFS_ANON 2
671 #define CLEAR_REFS_MAPPED 3
672 
673 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
674  size_t count, loff_t *ppos)
675 {
676  struct task_struct *task;
677  char buffer[PROC_NUMBUF];
678  struct mm_struct *mm;
679  struct vm_area_struct *vma;
680  int type;
681  int rv;
682 
683  memset(buffer, 0, sizeof(buffer));
684  if (count > sizeof(buffer) - 1)
685  count = sizeof(buffer) - 1;
686  if (copy_from_user(buffer, buf, count))
687  return -EFAULT;
688  rv = kstrtoint(strstrip(buffer), 10, &type);
689  if (rv < 0)
690  return rv;
691  if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
692  return -EINVAL;
693  task = get_proc_task(file->f_path.dentry->d_inode);
694  if (!task)
695  return -ESRCH;
696  mm = get_task_mm(task);
697  if (mm) {
698  struct mm_walk clear_refs_walk = {
699  .pmd_entry = clear_refs_pte_range,
700  .mm = mm,
701  };
702  down_read(&mm->mmap_sem);
703  for (vma = mm->mmap; vma; vma = vma->vm_next) {
704  clear_refs_walk.private = vma;
705  if (is_vm_hugetlb_page(vma))
706  continue;
707  /*
708  * Writing 1 to /proc/pid/clear_refs affects all pages.
709  *
710  * Writing 2 to /proc/pid/clear_refs only affects
711  * Anonymous pages.
712  *
713  * Writing 3 to /proc/pid/clear_refs only affects file
714  * mapped pages.
715  */
716  if (type == CLEAR_REFS_ANON && vma->vm_file)
717  continue;
718  if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
719  continue;
720  walk_page_range(vma->vm_start, vma->vm_end,
721  &clear_refs_walk);
722  }
723  flush_tlb_mm(mm);
724  up_read(&mm->mmap_sem);
725  mmput(mm);
726  }
727  put_task_struct(task);
728 
729  return count;
730 }
731 
733  .write = clear_refs_write,
734  .llseek = noop_llseek,
735 };
736 
737 typedef struct {
738  u64 pme;
739 } pagemap_entry_t;
740 
741 struct pagemapread {
742  int pos, len;
743  pagemap_entry_t *buffer;
744 };
745 
746 #define PAGEMAP_WALK_SIZE (PMD_SIZE)
747 #define PAGEMAP_WALK_MASK (PMD_MASK)
748 
749 #define PM_ENTRY_BYTES sizeof(u64)
750 #define PM_STATUS_BITS 3
751 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
752 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
753 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
754 #define PM_PSHIFT_BITS 6
755 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
756 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
757 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
758 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
759 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
760 
761 #define PM_PRESENT PM_STATUS(4LL)
762 #define PM_SWAP PM_STATUS(2LL)
763 #define PM_FILE PM_STATUS(1LL)
764 #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
765 #define PM_END_OF_BUFFER 1
766 
767 static inline pagemap_entry_t make_pme(u64 val)
768 {
769  return (pagemap_entry_t) { .pme = val };
770 }
771 
772 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
773  struct pagemapread *pm)
774 {
775  pm->buffer[pm->pos++] = *pme;
776  if (pm->pos >= pm->len)
777  return PM_END_OF_BUFFER;
778  return 0;
779 }
780 
781 static int pagemap_pte_hole(unsigned long start, unsigned long end,
782  struct mm_walk *walk)
783 {
784  struct pagemapread *pm = walk->private;
785  unsigned long addr;
786  int err = 0;
787  pagemap_entry_t pme = make_pme(PM_NOT_PRESENT);
788 
789  for (addr = start; addr < end; addr += PAGE_SIZE) {
790  err = add_to_pagemap(addr, &pme, pm);
791  if (err)
792  break;
793  }
794  return err;
795 }
796 
797 static void pte_to_pagemap_entry(pagemap_entry_t *pme,
798  struct vm_area_struct *vma, unsigned long addr, pte_t pte)
799 {
800  u64 frame, flags;
801  struct page *page = NULL;
802 
803  if (pte_present(pte)) {
804  frame = pte_pfn(pte);
805  flags = PM_PRESENT;
806  page = vm_normal_page(vma, addr, pte);
807  } else if (is_swap_pte(pte)) {
808  swp_entry_t entry = pte_to_swp_entry(pte);
809 
810  frame = swp_type(entry) |
811  (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
812  flags = PM_SWAP;
813  if (is_migration_entry(entry))
814  page = migration_entry_to_page(entry);
815  } else {
816  *pme = make_pme(PM_NOT_PRESENT);
817  return;
818  }
819 
820  if (page && !PageAnon(page))
821  flags |= PM_FILE;
822 
823  *pme = make_pme(PM_PFRAME(frame) | PM_PSHIFT(PAGE_SHIFT) | flags);
824 }
825 
826 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
827 static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme,
828  pmd_t pmd, int offset)
829 {
830  /*
831  * Currently pmd for thp is always present because thp can not be
832  * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
833  * This if-check is just to prepare for future implementation.
834  */
835  if (pmd_present(pmd))
836  *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
838  else
839  *pme = make_pme(PM_NOT_PRESENT);
840 }
841 #else
842 static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme,
843  pmd_t pmd, int offset)
844 {
845 }
846 #endif
847 
848 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
849  struct mm_walk *walk)
850 {
851  struct vm_area_struct *vma;
852  struct pagemapread *pm = walk->private;
853  pte_t *pte;
854  int err = 0;
855  pagemap_entry_t pme = make_pme(PM_NOT_PRESENT);
856 
857  /* find the first VMA at or above 'addr' */
858  vma = find_vma(walk->mm, addr);
859  if (vma && pmd_trans_huge_lock(pmd, vma) == 1) {
860  for (; addr != end; addr += PAGE_SIZE) {
861  unsigned long offset;
862 
863  offset = (addr & ~PAGEMAP_WALK_MASK) >>
864  PAGE_SHIFT;
865  thp_pmd_to_pagemap_entry(&pme, *pmd, offset);
866  err = add_to_pagemap(addr, &pme, pm);
867  if (err)
868  break;
869  }
870  spin_unlock(&walk->mm->page_table_lock);
871  return err;
872  }
873 
874  if (pmd_trans_unstable(pmd))
875  return 0;
876  for (; addr != end; addr += PAGE_SIZE) {
877 
878  /* check to see if we've left 'vma' behind
879  * and need a new, higher one */
880  if (vma && (addr >= vma->vm_end)) {
881  vma = find_vma(walk->mm, addr);
882  pme = make_pme(PM_NOT_PRESENT);
883  }
884 
885  /* check that 'vma' actually covers this address,
886  * and that it isn't a huge page vma */
887  if (vma && (vma->vm_start <= addr) &&
888  !is_vm_hugetlb_page(vma)) {
889  pte = pte_offset_map(pmd, addr);
890  pte_to_pagemap_entry(&pme, vma, addr, *pte);
891  /* unmap before userspace copy */
892  pte_unmap(pte);
893  }
894  err = add_to_pagemap(addr, &pme, pm);
895  if (err)
896  return err;
897  }
898 
899  cond_resched();
900 
901  return err;
902 }
903 
904 #ifdef CONFIG_HUGETLB_PAGE
905 static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme,
906  pte_t pte, int offset)
907 {
908  if (pte_present(pte))
909  *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset)
911  else
912  *pme = make_pme(PM_NOT_PRESENT);
913 }
914 
915 /* This function walks within one hugetlb entry in the single call */
916 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
917  unsigned long addr, unsigned long end,
918  struct mm_walk *walk)
919 {
920  struct pagemapread *pm = walk->private;
921  int err = 0;
922  pagemap_entry_t pme;
923 
924  for (; addr != end; addr += PAGE_SIZE) {
925  int offset = (addr & ~hmask) >> PAGE_SHIFT;
926  huge_pte_to_pagemap_entry(&pme, *pte, offset);
927  err = add_to_pagemap(addr, &pme, pm);
928  if (err)
929  return err;
930  }
931 
932  cond_resched();
933 
934  return err;
935 }
936 #endif /* HUGETLB_PAGE */
937 
938 /*
939  * /proc/pid/pagemap - an array mapping virtual pages to pfns
940  *
941  * For each page in the address space, this file contains one 64-bit entry
942  * consisting of the following:
943  *
944  * Bits 0-54 page frame number (PFN) if present
945  * Bits 0-4 swap type if swapped
946  * Bits 5-54 swap offset if swapped
947  * Bits 55-60 page shift (page size = 1<<page shift)
948  * Bit 61 page is file-page or shared-anon
949  * Bit 62 page swapped
950  * Bit 63 page present
951  *
952  * If the page is not present but in swap, then the PFN contains an
953  * encoding of the swap file number and the page's offset into the
954  * swap. Unmapped pages return a null PFN. This allows determining
955  * precisely which pages are mapped (or in swap) and comparing mapped
956  * pages between processes.
957  *
958  * Efficient users of this interface will use /proc/pid/maps to
959  * determine which areas of memory are actually mapped and llseek to
960  * skip over unmapped regions.
961  */
962 static ssize_t pagemap_read(struct file *file, char __user *buf,
963  size_t count, loff_t *ppos)
964 {
965  struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
966  struct mm_struct *mm;
967  struct pagemapread pm;
968  int ret = -ESRCH;
969  struct mm_walk pagemap_walk = {};
970  unsigned long src;
971  unsigned long svpfn;
972  unsigned long start_vaddr;
973  unsigned long end_vaddr;
974  int copied = 0;
975 
976  if (!task)
977  goto out;
978 
979  ret = -EINVAL;
980  /* file position must be aligned */
981  if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
982  goto out_task;
983 
984  ret = 0;
985  if (!count)
986  goto out_task;
987 
988  pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
989  pm.buffer = kmalloc(pm.len, GFP_TEMPORARY);
990  ret = -ENOMEM;
991  if (!pm.buffer)
992  goto out_task;
993 
994  mm = mm_access(task, PTRACE_MODE_READ);
995  ret = PTR_ERR(mm);
996  if (!mm || IS_ERR(mm))
997  goto out_free;
998 
999  pagemap_walk.pmd_entry = pagemap_pte_range;
1000  pagemap_walk.pte_hole = pagemap_pte_hole;
1001 #ifdef CONFIG_HUGETLB_PAGE
1002  pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1003 #endif
1004  pagemap_walk.mm = mm;
1005  pagemap_walk.private = &pm;
1006 
1007  src = *ppos;
1008  svpfn = src / PM_ENTRY_BYTES;
1009  start_vaddr = svpfn << PAGE_SHIFT;
1010  end_vaddr = TASK_SIZE_OF(task);
1011 
1012  /* watch out for wraparound */
1013  if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1014  start_vaddr = end_vaddr;
1015 
1016  /*
1017  * The odds are that this will stop walking way
1018  * before end_vaddr, because the length of the
1019  * user buffer is tracked in "pm", and the walk
1020  * will stop when we hit the end of the buffer.
1021  */
1022  ret = 0;
1023  while (count && (start_vaddr < end_vaddr)) {
1024  int len;
1025  unsigned long end;
1026 
1027  pm.pos = 0;
1028  end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1029  /* overflow ? */
1030  if (end < start_vaddr || end > end_vaddr)
1031  end = end_vaddr;
1032  down_read(&mm->mmap_sem);
1033  ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1034  up_read(&mm->mmap_sem);
1035  start_vaddr = end;
1036 
1037  len = min(count, PM_ENTRY_BYTES * pm.pos);
1038  if (copy_to_user(buf, pm.buffer, len)) {
1039  ret = -EFAULT;
1040  goto out_mm;
1041  }
1042  copied += len;
1043  buf += len;
1044  count -= len;
1045  }
1046  *ppos += copied;
1047  if (!ret || ret == PM_END_OF_BUFFER)
1048  ret = copied;
1049 
1050 out_mm:
1051  mmput(mm);
1052 out_free:
1053  kfree(pm.buffer);
1054 out_task:
1055  put_task_struct(task);
1056 out:
1057  return ret;
1058 }
1059 
1061  .llseek = mem_lseek, /* borrow this */
1062  .read = pagemap_read,
1063 };
1064 #endif /* CONFIG_PROC_PAGE_MONITOR */
1065 
1066 #ifdef CONFIG_NUMA
1067 
1068 struct numa_maps {
1069  struct vm_area_struct *vma;
1070  unsigned long pages;
1071  unsigned long anon;
1072  unsigned long active;
1073  unsigned long writeback;
1074  unsigned long mapcount_max;
1075  unsigned long dirty;
1076  unsigned long swapcache;
1077  unsigned long node[MAX_NUMNODES];
1078 };
1079 
1080 struct numa_maps_private {
1081  struct proc_maps_private proc_maps;
1082  struct numa_maps md;
1083 };
1084 
1085 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1086  unsigned long nr_pages)
1087 {
1088  int count = page_mapcount(page);
1089 
1090  md->pages += nr_pages;
1091  if (pte_dirty || PageDirty(page))
1092  md->dirty += nr_pages;
1093 
1094  if (PageSwapCache(page))
1095  md->swapcache += nr_pages;
1096 
1097  if (PageActive(page) || PageUnevictable(page))
1098  md->active += nr_pages;
1099 
1100  if (PageWriteback(page))
1101  md->writeback += nr_pages;
1102 
1103  if (PageAnon(page))
1104  md->anon += nr_pages;
1105 
1106  if (count > md->mapcount_max)
1107  md->mapcount_max = count;
1108 
1109  md->node[page_to_nid(page)] += nr_pages;
1110 }
1111 
1112 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1113  unsigned long addr)
1114 {
1115  struct page *page;
1116  int nid;
1117 
1118  if (!pte_present(pte))
1119  return NULL;
1120 
1121  page = vm_normal_page(vma, addr, pte);
1122  if (!page)
1123  return NULL;
1124 
1125  if (PageReserved(page))
1126  return NULL;
1127 
1128  nid = page_to_nid(page);
1129  if (!node_isset(nid, node_states[N_HIGH_MEMORY]))
1130  return NULL;
1131 
1132  return page;
1133 }
1134 
1135 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1136  unsigned long end, struct mm_walk *walk)
1137 {
1138  struct numa_maps *md;
1139  spinlock_t *ptl;
1140  pte_t *orig_pte;
1141  pte_t *pte;
1142 
1143  md = walk->private;
1144 
1145  if (pmd_trans_huge_lock(pmd, md->vma) == 1) {
1146  pte_t huge_pte = *(pte_t *)pmd;
1147  struct page *page;
1148 
1149  page = can_gather_numa_stats(huge_pte, md->vma, addr);
1150  if (page)
1151  gather_stats(page, md, pte_dirty(huge_pte),
1153  spin_unlock(&walk->mm->page_table_lock);
1154  return 0;
1155  }
1156 
1157  if (pmd_trans_unstable(pmd))
1158  return 0;
1159  orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1160  do {
1161  struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
1162  if (!page)
1163  continue;
1164  gather_stats(page, md, pte_dirty(*pte), 1);
1165 
1166  } while (pte++, addr += PAGE_SIZE, addr != end);
1167  pte_unmap_unlock(orig_pte, ptl);
1168  return 0;
1169 }
1170 #ifdef CONFIG_HUGETLB_PAGE
1171 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1172  unsigned long addr, unsigned long end, struct mm_walk *walk)
1173 {
1174  struct numa_maps *md;
1175  struct page *page;
1176 
1177  if (pte_none(*pte))
1178  return 0;
1179 
1180  page = pte_page(*pte);
1181  if (!page)
1182  return 0;
1183 
1184  md = walk->private;
1185  gather_stats(page, md, pte_dirty(*pte), 1);
1186  return 0;
1187 }
1188 
1189 #else
1190 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1191  unsigned long addr, unsigned long end, struct mm_walk *walk)
1192 {
1193  return 0;
1194 }
1195 #endif
1196 
1197 /*
1198  * Display pages allocated per node and memory policy via /proc.
1199  */
1200 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1201 {
1202  struct numa_maps_private *numa_priv = m->private;
1203  struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1204  struct vm_area_struct *vma = v;
1205  struct numa_maps *md = &numa_priv->md;
1206  struct file *file = vma->vm_file;
1207  struct task_struct *task = proc_priv->task;
1208  struct mm_struct *mm = vma->vm_mm;
1209  struct mm_walk walk = {};
1210  struct mempolicy *pol;
1211  int n;
1212  char buffer[50];
1213 
1214  if (!mm)
1215  return 0;
1216 
1217  /* Ensure we start with an empty set of numa_maps statistics. */
1218  memset(md, 0, sizeof(*md));
1219 
1220  md->vma = vma;
1221 
1222  walk.hugetlb_entry = gather_hugetbl_stats;
1223  walk.pmd_entry = gather_pte_stats;
1224  walk.private = md;
1225  walk.mm = mm;
1226 
1227  pol = get_vma_policy(task, vma, vma->vm_start);
1228  mpol_to_str(buffer, sizeof(buffer), pol, 0);
1229  mpol_cond_put(pol);
1230 
1231  seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1232 
1233  if (file) {
1234  seq_printf(m, " file=");
1235  seq_path(m, &file->f_path, "\n\t= ");
1236  } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1237  seq_printf(m, " heap");
1238  } else {
1239  pid_t tid = vm_is_stack(task, vma, is_pid);
1240  if (tid != 0) {
1241  /*
1242  * Thread stack in /proc/PID/task/TID/maps or
1243  * the main process stack.
1244  */
1245  if (!is_pid || (vma->vm_start <= mm->start_stack &&
1246  vma->vm_end >= mm->start_stack))
1247  seq_printf(m, " stack");
1248  else
1249  seq_printf(m, " stack:%d", tid);
1250  }
1251  }
1252 
1253  if (is_vm_hugetlb_page(vma))
1254  seq_printf(m, " huge");
1255 
1256  walk_page_range(vma->vm_start, vma->vm_end, &walk);
1257 
1258  if (!md->pages)
1259  goto out;
1260 
1261  if (md->anon)
1262  seq_printf(m, " anon=%lu", md->anon);
1263 
1264  if (md->dirty)
1265  seq_printf(m, " dirty=%lu", md->dirty);
1266 
1267  if (md->pages != md->anon && md->pages != md->dirty)
1268  seq_printf(m, " mapped=%lu", md->pages);
1269 
1270  if (md->mapcount_max > 1)
1271  seq_printf(m, " mapmax=%lu", md->mapcount_max);
1272 
1273  if (md->swapcache)
1274  seq_printf(m, " swapcache=%lu", md->swapcache);
1275 
1276  if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1277  seq_printf(m, " active=%lu", md->active);
1278 
1279  if (md->writeback)
1280  seq_printf(m, " writeback=%lu", md->writeback);
1281 
1283  if (md->node[n])
1284  seq_printf(m, " N%d=%lu", n, md->node[n]);
1285 out:
1286  seq_putc(m, '\n');
1287 
1288  if (m->count < m->size)
1289  m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1290  return 0;
1291 }
1292 
1293 static int show_pid_numa_map(struct seq_file *m, void *v)
1294 {
1295  return show_numa_map(m, v, 1);
1296 }
1297 
1298 static int show_tid_numa_map(struct seq_file *m, void *v)
1299 {
1300  return show_numa_map(m, v, 0);
1301 }
1302 
1303 static const struct seq_operations proc_pid_numa_maps_op = {
1304  .start = m_start,
1305  .next = m_next,
1306  .stop = m_stop,
1307  .show = show_pid_numa_map,
1308 };
1309 
1310 static const struct seq_operations proc_tid_numa_maps_op = {
1311  .start = m_start,
1312  .next = m_next,
1313  .stop = m_stop,
1314  .show = show_tid_numa_map,
1315 };
1316 
1317 static int numa_maps_open(struct inode *inode, struct file *file,
1318  const struct seq_operations *ops)
1319 {
1320  struct numa_maps_private *priv;
1321  int ret = -ENOMEM;
1322  priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1323  if (priv) {
1324  priv->proc_maps.pid = proc_pid(inode);
1325  ret = seq_open(file, ops);
1326  if (!ret) {
1327  struct seq_file *m = file->private_data;
1328  m->private = priv;
1329  } else {
1330  kfree(priv);
1331  }
1332  }
1333  return ret;
1334 }
1335 
1336 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1337 {
1338  return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1339 }
1340 
1341 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1342 {
1343  return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1344 }
1345 
1347  .open = pid_numa_maps_open,
1348  .read = seq_read,
1349  .llseek = seq_lseek,
1350  .release = seq_release_private,
1351 };
1352 
1354  .open = tid_numa_maps_open,
1355  .read = seq_read,
1356  .llseek = seq_lseek,
1357  .release = seq_release_private,
1358 };
1359 #endif /* CONFIG_NUMA */