~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/proc/task_mmu.c

Version: ~ [ linux-5.13-rc5 ] ~ [ linux-5.12.9 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.42 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.124 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.193 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.235 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.271 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.271 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp