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

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

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ 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/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_FSCREDS);
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 = file_inode(vma->vm_file);
275                 dev = inode->i_sb->s_dev;
276                 ino = inode->i_ino;
277                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
278         }
279 
280         /* We don't show the stack guard page in /proc/maps */
281         start = vma->vm_start;
282         end = vma->vm_end;
283 
284         seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
285                         start,
286                         end,
287                         flags & VM_READ ? 'r' : '-',
288                         flags & VM_WRITE ? 'w' : '-',
289                         flags & VM_EXEC ? 'x' : '-',
290                         flags & VM_MAYSHARE ? 's' : 'p',
291                         pgoff,
292                         MAJOR(dev), MINOR(dev), ino, &len);
293 
294         /*
295          * Print the dentry name for named mappings, and a
296          * special [heap] marker for the heap:
297          */
298         if (file) {
299                 pad_len_spaces(m, len);
300                 seq_path(m, &file->f_path, "\n");
301                 goto done;
302         }
303 
304         name = arch_vma_name(vma);
305         if (!name) {
306                 pid_t tid;
307 
308                 if (!mm) {
309                         name = "[vdso]";
310                         goto done;
311                 }
312 
313                 if (vma->vm_start <= mm->brk &&
314                     vma->vm_end >= mm->start_brk) {
315                         name = "[heap]";
316                         goto done;
317                 }
318 
319                 tid = vm_is_stack(task, vma, is_pid);
320 
321                 if (tid != 0) {
322                         /*
323                          * Thread stack in /proc/PID/task/TID/maps or
324                          * the main process stack.
325                          */
326                         if (!is_pid || (vma->vm_start <= mm->start_stack &&
327                             vma->vm_end >= mm->start_stack)) {
328                                 name = "[stack]";
329                         } else {
330                                 /* Thread stack in /proc/PID/maps */
331                                 pad_len_spaces(m, len);
332                                 seq_printf(m, "[stack:%d]", tid);
333                         }
334                 }
335         }
336 
337 done:
338         if (name) {
339                 pad_len_spaces(m, len);
340                 seq_puts(m, name);
341         }
342         seq_putc(m, '\n');
343 }
344 
345 static int show_map(struct seq_file *m, void *v, int is_pid)
346 {
347         struct vm_area_struct *vma = v;
348         struct proc_maps_private *priv = m->private;
349         struct task_struct *task = priv->task;
350 
351         show_map_vma(m, vma, is_pid);
352 
353         if (m->count < m->size)  /* vma is copied successfully */
354                 m->version = (vma != get_gate_vma(task->mm))
355                         ? vma->vm_start : 0;
356         return 0;
357 }
358 
359 static int show_pid_map(struct seq_file *m, void *v)
360 {
361         return show_map(m, v, 1);
362 }
363 
364 static int show_tid_map(struct seq_file *m, void *v)
365 {
366         return show_map(m, v, 0);
367 }
368 
369 static const struct seq_operations proc_pid_maps_op = {
370         .start  = m_start,
371         .next   = m_next,
372         .stop   = m_stop,
373         .show   = show_pid_map
374 };
375 
376 static const struct seq_operations proc_tid_maps_op = {
377         .start  = m_start,
378         .next   = m_next,
379         .stop   = m_stop,
380         .show   = show_tid_map
381 };
382 
383 static int pid_maps_open(struct inode *inode, struct file *file)
384 {
385         return do_maps_open(inode, file, &proc_pid_maps_op);
386 }
387 
388 static int tid_maps_open(struct inode *inode, struct file *file)
389 {
390         return do_maps_open(inode, file, &proc_tid_maps_op);
391 }
392 
393 const struct file_operations proc_pid_maps_operations = {
394         .open           = pid_maps_open,
395         .read           = seq_read,
396         .llseek         = seq_lseek,
397         .release        = seq_release_private,
398 };
399 
400 const struct file_operations proc_tid_maps_operations = {
401         .open           = tid_maps_open,
402         .read           = seq_read,
403         .llseek         = seq_lseek,
404         .release        = seq_release_private,
405 };
406 
407 /*
408  * Proportional Set Size(PSS): my share of RSS.
409  *
410  * PSS of a process is the count of pages it has in memory, where each
411  * page is divided by the number of processes sharing it.  So if a
412  * process has 1000 pages all to itself, and 1000 shared with one other
413  * process, its PSS will be 1500.
414  *
415  * To keep (accumulated) division errors low, we adopt a 64bit
416  * fixed-point pss counter to minimize division errors. So (pss >>
417  * PSS_SHIFT) would be the real byte count.
418  *
419  * A shift of 12 before division means (assuming 4K page size):
420  *      - 1M 3-user-pages add up to 8KB errors;
421  *      - supports mapcount up to 2^24, or 16M;
422  *      - supports PSS up to 2^52 bytes, or 4PB.
423  */
424 #define PSS_SHIFT 12
425 
426 #ifdef CONFIG_PROC_PAGE_MONITOR
427 struct mem_size_stats {
428         struct vm_area_struct *vma;
429         unsigned long resident;
430         unsigned long shared_clean;
431         unsigned long shared_dirty;
432         unsigned long private_clean;
433         unsigned long private_dirty;
434         unsigned long referenced;
435         unsigned long anonymous;
436         unsigned long anonymous_thp;
437         unsigned long swap;
438         unsigned long nonlinear;
439         u64 pss;
440 };
441 
442 
443 static void smaps_pte_entry(pte_t ptent, unsigned long addr,
444                 unsigned long ptent_size, struct mm_walk *walk)
445 {
446         struct mem_size_stats *mss = walk->private;
447         struct vm_area_struct *vma = mss->vma;
448         pgoff_t pgoff = linear_page_index(vma, addr);
449         struct page *page = NULL;
450         int mapcount;
451 
452         if (pte_present(ptent)) {
453                 page = vm_normal_page(vma, addr, ptent);
454         } else if (is_swap_pte(ptent)) {
455                 swp_entry_t swpent = pte_to_swp_entry(ptent);
456 
457                 if (!non_swap_entry(swpent))
458                         mss->swap += ptent_size;
459                 else if (is_migration_entry(swpent))
460                         page = migration_entry_to_page(swpent);
461         } else if (pte_file(ptent)) {
462                 if (pte_to_pgoff(ptent) != pgoff)
463                         mss->nonlinear += ptent_size;
464         }
465 
466         if (!page)
467                 return;
468 
469         if (PageAnon(page))
470                 mss->anonymous += ptent_size;
471 
472         if (page->index != pgoff)
473                 mss->nonlinear += ptent_size;
474 
475         mss->resident += ptent_size;
476         /* Accumulate the size in pages that have been accessed. */
477         if (pte_young(ptent) || PageReferenced(page))
478                 mss->referenced += ptent_size;
479         mapcount = page_mapcount(page);
480         if (mapcount >= 2) {
481                 if (pte_dirty(ptent) || PageDirty(page))
482                         mss->shared_dirty += ptent_size;
483                 else
484                         mss->shared_clean += ptent_size;
485                 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
486         } else {
487                 if (pte_dirty(ptent) || PageDirty(page))
488                         mss->private_dirty += ptent_size;
489                 else
490                         mss->private_clean += ptent_size;
491                 mss->pss += (ptent_size << PSS_SHIFT);
492         }
493 }
494 
495 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
496                            struct mm_walk *walk)
497 {
498         struct mem_size_stats *mss = walk->private;
499         struct vm_area_struct *vma = mss->vma;
500         pte_t *pte;
501         spinlock_t *ptl;
502 
503         if (pmd_trans_huge_lock(pmd, vma) == 1) {
504                 smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk);
505                 spin_unlock(&walk->mm->page_table_lock);
506                 mss->anonymous_thp += HPAGE_PMD_SIZE;
507                 return 0;
508         }
509 
510         if (pmd_trans_unstable(pmd))
511                 return 0;
512         /*
513          * The mmap_sem held all the way back in m_start() is what
514          * keeps khugepaged out of here and from collapsing things
515          * in here.
516          */
517         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
518         for (; addr != end; pte++, addr += PAGE_SIZE)
519                 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
520         pte_unmap_unlock(pte - 1, ptl);
521         cond_resched();
522         return 0;
523 }
524 
525 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
526 {
527         /*
528          * Don't forget to update Documentation/ on changes.
529          */
530         static const char mnemonics[BITS_PER_LONG][2] = {
531                 /*
532                  * In case if we meet a flag we don't know about.
533                  */
534                 [0 ... (BITS_PER_LONG-1)] = "??",
535 
536                 [ilog2(VM_READ)]        = "rd",
537                 [ilog2(VM_WRITE)]       = "wr",
538                 [ilog2(VM_EXEC)]        = "ex",
539                 [ilog2(VM_SHARED)]      = "sh",
540                 [ilog2(VM_MAYREAD)]     = "mr",
541                 [ilog2(VM_MAYWRITE)]    = "mw",
542                 [ilog2(VM_MAYEXEC)]     = "me",
543                 [ilog2(VM_MAYSHARE)]    = "ms",
544                 [ilog2(VM_GROWSDOWN)]   = "gd",
545                 [ilog2(VM_PFNMAP)]      = "pf",
546                 [ilog2(VM_DENYWRITE)]   = "dw",
547                 [ilog2(VM_LOCKED)]      = "lo",
548                 [ilog2(VM_IO)]          = "io",
549                 [ilog2(VM_SEQ_READ)]    = "sr",
550                 [ilog2(VM_RAND_READ)]   = "rr",
551                 [ilog2(VM_DONTCOPY)]    = "dc",
552                 [ilog2(VM_DONTEXPAND)]  = "de",
553                 [ilog2(VM_ACCOUNT)]     = "ac",
554                 [ilog2(VM_NORESERVE)]   = "nr",
555                 [ilog2(VM_HUGETLB)]     = "ht",
556                 [ilog2(VM_NONLINEAR)]   = "nl",
557                 [ilog2(VM_ARCH_1)]      = "ar",
558                 [ilog2(VM_DONTDUMP)]    = "dd",
559                 [ilog2(VM_MIXEDMAP)]    = "mm",
560                 [ilog2(VM_HUGEPAGE)]    = "hg",
561                 [ilog2(VM_NOHUGEPAGE)]  = "nh",
562                 [ilog2(VM_MERGEABLE)]   = "mg",
563         };
564         size_t i;
565 
566         seq_puts(m, "VmFlags: ");
567         for (i = 0; i < BITS_PER_LONG; i++) {
568                 if (vma->vm_flags & (1UL << i)) {
569                         seq_printf(m, "%c%c ",
570                                    mnemonics[i][0], mnemonics[i][1]);
571                 }
572         }
573         seq_putc(m, '\n');
574 }
575 
576 static int show_smap(struct seq_file *m, void *v, int is_pid)
577 {
578         struct proc_maps_private *priv = m->private;
579         struct task_struct *task = priv->task;
580         struct vm_area_struct *vma = v;
581         struct mem_size_stats mss;
582         struct mm_walk smaps_walk = {
583                 .pmd_entry = smaps_pte_range,
584                 .mm = vma->vm_mm,
585                 .private = &mss,
586         };
587 
588         memset(&mss, 0, sizeof mss);
589         mss.vma = vma;
590         /* mmap_sem is held in m_start */
591         if (vma->vm_mm && !is_vm_hugetlb_page(vma))
592                 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
593 
594         show_map_vma(m, vma, is_pid);
595 
596         seq_printf(m,
597                    "Size:           %8lu kB\n"
598                    "Rss:            %8lu kB\n"
599                    "Pss:            %8lu kB\n"
600                    "Shared_Clean:   %8lu kB\n"
601                    "Shared_Dirty:   %8lu kB\n"
602                    "Private_Clean:  %8lu kB\n"
603                    "Private_Dirty:  %8lu kB\n"
604                    "Referenced:     %8lu kB\n"
605                    "Anonymous:      %8lu kB\n"
606                    "AnonHugePages:  %8lu kB\n"
607                    "Swap:           %8lu kB\n"
608                    "KernelPageSize: %8lu kB\n"
609                    "MMUPageSize:    %8lu kB\n"
610                    "Locked:         %8lu kB\n",
611                    (vma->vm_end - vma->vm_start) >> 10,
612                    mss.resident >> 10,
613                    (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
614                    mss.shared_clean  >> 10,
615                    mss.shared_dirty  >> 10,
616                    mss.private_clean >> 10,
617                    mss.private_dirty >> 10,
618                    mss.referenced >> 10,
619                    mss.anonymous >> 10,
620                    mss.anonymous_thp >> 10,
621                    mss.swap >> 10,
622                    vma_kernel_pagesize(vma) >> 10,
623                    vma_mmu_pagesize(vma) >> 10,
624                    (vma->vm_flags & VM_LOCKED) ?
625                         (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
626 
627         if (vma->vm_flags & VM_NONLINEAR)
628                 seq_printf(m, "Nonlinear:      %8lu kB\n",
629                                 mss.nonlinear >> 10);
630 
631         show_smap_vma_flags(m, vma);
632 
633         if (m->count < m->size)  /* vma is copied successfully */
634                 m->version = (vma != get_gate_vma(task->mm))
635                         ? vma->vm_start : 0;
636         return 0;
637 }
638 
639 static int show_pid_smap(struct seq_file *m, void *v)
640 {
641         return show_smap(m, v, 1);
642 }
643 
644 static int show_tid_smap(struct seq_file *m, void *v)
645 {
646         return show_smap(m, v, 0);
647 }
648 
649 static const struct seq_operations proc_pid_smaps_op = {
650         .start  = m_start,
651         .next   = m_next,
652         .stop   = m_stop,
653         .show   = show_pid_smap
654 };
655 
656 static const struct seq_operations proc_tid_smaps_op = {
657         .start  = m_start,
658         .next   = m_next,
659         .stop   = m_stop,
660         .show   = show_tid_smap
661 };
662 
663 static int pid_smaps_open(struct inode *inode, struct file *file)
664 {
665         return do_maps_open(inode, file, &proc_pid_smaps_op);
666 }
667 
668 static int tid_smaps_open(struct inode *inode, struct file *file)
669 {
670         return do_maps_open(inode, file, &proc_tid_smaps_op);
671 }
672 
673 const struct file_operations proc_pid_smaps_operations = {
674         .open           = pid_smaps_open,
675         .read           = seq_read,
676         .llseek         = seq_lseek,
677         .release        = seq_release_private,
678 };
679 
680 const struct file_operations proc_tid_smaps_operations = {
681         .open           = tid_smaps_open,
682         .read           = seq_read,
683         .llseek         = seq_lseek,
684         .release        = seq_release_private,
685 };
686 
687 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
688                                 unsigned long end, struct mm_walk *walk)
689 {
690         struct vm_area_struct *vma = walk->private;
691         pte_t *pte, ptent;
692         spinlock_t *ptl;
693         struct page *page;
694 
695         split_huge_page_pmd(vma, addr, pmd);
696         if (pmd_trans_unstable(pmd))
697                 return 0;
698 
699         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
700         for (; addr != end; pte++, addr += PAGE_SIZE) {
701                 ptent = *pte;
702                 if (!pte_present(ptent))
703                         continue;
704 
705                 page = vm_normal_page(vma, addr, ptent);
706                 if (!page)
707                         continue;
708 
709                 /* Clear accessed and referenced bits. */
710                 ptep_test_and_clear_young(vma, addr, pte);
711                 ClearPageReferenced(page);
712         }
713         pte_unmap_unlock(pte - 1, ptl);
714         cond_resched();
715         return 0;
716 }
717 
718 #define CLEAR_REFS_ALL 1
719 #define CLEAR_REFS_ANON 2
720 #define CLEAR_REFS_MAPPED 3
721 
722 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
723                                 size_t count, loff_t *ppos)
724 {
725         struct task_struct *task;
726         char buffer[PROC_NUMBUF];
727         struct mm_struct *mm;
728         struct vm_area_struct *vma;
729         int type;
730         int rv;
731 
732         memset(buffer, 0, sizeof(buffer));
733         if (count > sizeof(buffer) - 1)
734                 count = sizeof(buffer) - 1;
735         if (copy_from_user(buffer, buf, count))
736                 return -EFAULT;
737         rv = kstrtoint(strstrip(buffer), 10, &type);
738         if (rv < 0)
739                 return rv;
740         if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
741                 return -EINVAL;
742         task = get_proc_task(file_inode(file));
743         if (!task)
744                 return -ESRCH;
745         mm = get_task_mm(task);
746         if (mm) {
747                 struct mm_walk clear_refs_walk = {
748                         .pmd_entry = clear_refs_pte_range,
749                         .mm = mm,
750                 };
751                 down_read(&mm->mmap_sem);
752                 for (vma = mm->mmap; vma; vma = vma->vm_next) {
753                         clear_refs_walk.private = vma;
754                         if (is_vm_hugetlb_page(vma))
755                                 continue;
756                         /*
757                          * Writing 1 to /proc/pid/clear_refs affects all pages.
758                          *
759                          * Writing 2 to /proc/pid/clear_refs only affects
760                          * Anonymous pages.
761                          *
762                          * Writing 3 to /proc/pid/clear_refs only affects file
763                          * mapped pages.
764                          */
765                         if (type == CLEAR_REFS_ANON && vma->vm_file)
766                                 continue;
767                         if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
768                                 continue;
769                         walk_page_range(vma->vm_start, vma->vm_end,
770                                         &clear_refs_walk);
771                 }
772                 flush_tlb_mm(mm);
773                 up_read(&mm->mmap_sem);
774                 mmput(mm);
775         }
776         put_task_struct(task);
777 
778         return count;
779 }
780 
781 const struct file_operations proc_clear_refs_operations = {
782         .write          = clear_refs_write,
783         .llseek         = noop_llseek,
784 };
785 
786 typedef struct {
787         u64 pme;
788 } pagemap_entry_t;
789 
790 struct pagemapread {
791         int pos, len;           /* units: PM_ENTRY_BYTES, not bytes */
792         pagemap_entry_t *buffer;
793 };
794 
795 #define PAGEMAP_WALK_SIZE       (PMD_SIZE)
796 #define PAGEMAP_WALK_MASK       (PMD_MASK)
797 
798 #define PM_ENTRY_BYTES      sizeof(pagemap_entry_t)
799 #define PM_STATUS_BITS      3
800 #define PM_STATUS_OFFSET    (64 - PM_STATUS_BITS)
801 #define PM_STATUS_MASK      (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
802 #define PM_STATUS(nr)       (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
803 #define PM_PSHIFT_BITS      6
804 #define PM_PSHIFT_OFFSET    (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
805 #define PM_PSHIFT_MASK      (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
806 #define PM_PSHIFT(x)        (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
807 #define PM_PFRAME_MASK      ((1LL << PM_PSHIFT_OFFSET) - 1)
808 #define PM_PFRAME(x)        ((x) & PM_PFRAME_MASK)
809 
810 #define PM_PRESENT          PM_STATUS(4LL)
811 #define PM_SWAP             PM_STATUS(2LL)
812 #define PM_FILE             PM_STATUS(1LL)
813 #define PM_NOT_PRESENT      PM_PSHIFT(PAGE_SHIFT)
814 #define PM_END_OF_BUFFER    1
815 
816 static inline pagemap_entry_t make_pme(u64 val)
817 {
818         return (pagemap_entry_t) { .pme = val };
819 }
820 
821 static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
822                           struct pagemapread *pm)
823 {
824         pm->buffer[pm->pos++] = *pme;
825         if (pm->pos >= pm->len)
826                 return PM_END_OF_BUFFER;
827         return 0;
828 }
829 
830 static int pagemap_pte_hole(unsigned long start, unsigned long end,
831                                 struct mm_walk *walk)
832 {
833         struct pagemapread *pm = walk->private;
834         unsigned long addr;
835         int err = 0;
836         pagemap_entry_t pme = make_pme(PM_NOT_PRESENT);
837 
838         for (addr = start; addr < end; addr += PAGE_SIZE) {
839                 err = add_to_pagemap(addr, &pme, pm);
840                 if (err)
841                         break;
842         }
843         return err;
844 }
845 
846 static void pte_to_pagemap_entry(pagemap_entry_t *pme,
847                 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
848 {
849         u64 frame, flags;
850         struct page *page = NULL;
851 
852         if (pte_present(pte)) {
853                 frame = pte_pfn(pte);
854                 flags = PM_PRESENT;
855                 page = vm_normal_page(vma, addr, pte);
856         } else if (is_swap_pte(pte)) {
857                 swp_entry_t entry = pte_to_swp_entry(pte);
858 
859                 frame = swp_type(entry) |
860                         (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
861                 flags = PM_SWAP;
862                 if (is_migration_entry(entry))
863                         page = migration_entry_to_page(entry);
864         } else {
865                 *pme = make_pme(PM_NOT_PRESENT);
866                 return;
867         }
868 
869         if (page && !PageAnon(page))
870                 flags |= PM_FILE;
871 
872         *pme = make_pme(PM_PFRAME(frame) | PM_PSHIFT(PAGE_SHIFT) | flags);
873 }
874 
875 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
876 static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme,
877                                         pmd_t pmd, int offset)
878 {
879         /*
880          * Currently pmd for thp is always present because thp can not be
881          * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
882          * This if-check is just to prepare for future implementation.
883          */
884         if (pmd_present(pmd))
885                 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
886                                 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT);
887         else
888                 *pme = make_pme(PM_NOT_PRESENT);
889 }
890 #else
891 static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme,
892                                                 pmd_t pmd, int offset)
893 {
894 }
895 #endif
896 
897 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
898                              struct mm_walk *walk)
899 {
900         struct vm_area_struct *vma;
901         struct pagemapread *pm = walk->private;
902         pte_t *pte;
903         int err = 0;
904         pagemap_entry_t pme = make_pme(PM_NOT_PRESENT);
905 
906         /* find the first VMA at or above 'addr' */
907         vma = find_vma(walk->mm, addr);
908         if (vma && pmd_trans_huge_lock(pmd, vma) == 1) {
909                 for (; addr != end; addr += PAGE_SIZE) {
910                         unsigned long offset;
911 
912                         offset = (addr & ~PAGEMAP_WALK_MASK) >>
913                                         PAGE_SHIFT;
914                         thp_pmd_to_pagemap_entry(&pme, *pmd, offset);
915                         err = add_to_pagemap(addr, &pme, pm);
916                         if (err)
917                                 break;
918                 }
919                 spin_unlock(&walk->mm->page_table_lock);
920                 return err;
921         }
922 
923         if (pmd_trans_unstable(pmd))
924                 return 0;
925         for (; addr != end; addr += PAGE_SIZE) {
926 
927                 /* check to see if we've left 'vma' behind
928                  * and need a new, higher one */
929                 if (vma && (addr >= vma->vm_end)) {
930                         vma = find_vma(walk->mm, addr);
931                         pme = make_pme(PM_NOT_PRESENT);
932                 }
933 
934                 /* check that 'vma' actually covers this address,
935                  * and that it isn't a huge page vma */
936                 if (vma && (vma->vm_start <= addr) &&
937                     !is_vm_hugetlb_page(vma)) {
938                         pte = pte_offset_map(pmd, addr);
939                         pte_to_pagemap_entry(&pme, vma, addr, *pte);
940                         /* unmap before userspace copy */
941                         pte_unmap(pte);
942                 }
943                 err = add_to_pagemap(addr, &pme, pm);
944                 if (err)
945                         return err;
946         }
947 
948         cond_resched();
949 
950         return err;
951 }
952 
953 #ifdef CONFIG_HUGETLB_PAGE
954 static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme,
955                                         pte_t pte, int offset)
956 {
957         if (pte_present(pte))
958                 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset)
959                                 | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT);
960         else
961                 *pme = make_pme(PM_NOT_PRESENT);
962 }
963 
964 /* This function walks within one hugetlb entry in the single call */
965 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
966                                  unsigned long addr, unsigned long end,
967                                  struct mm_walk *walk)
968 {
969         struct pagemapread *pm = walk->private;
970         int err = 0;
971         pagemap_entry_t pme;
972 
973         for (; addr != end; addr += PAGE_SIZE) {
974                 int offset = (addr & ~hmask) >> PAGE_SHIFT;
975                 huge_pte_to_pagemap_entry(&pme, *pte, offset);
976                 err = add_to_pagemap(addr, &pme, pm);
977                 if (err)
978                         return err;
979         }
980 
981         cond_resched();
982 
983         return err;
984 }
985 #endif /* HUGETLB_PAGE */
986 
987 /*
988  * /proc/pid/pagemap - an array mapping virtual pages to pfns
989  *
990  * For each page in the address space, this file contains one 64-bit entry
991  * consisting of the following:
992  *
993  * Bits 0-54  page frame number (PFN) if present
994  * Bits 0-4   swap type if swapped
995  * Bits 5-54  swap offset if swapped
996  * Bits 55-60 page shift (page size = 1<<page shift)
997  * Bit  61    page is file-page or shared-anon
998  * Bit  62    page swapped
999  * Bit  63    page present
1000  *
1001  * If the page is not present but in swap, then the PFN contains an
1002  * encoding of the swap file number and the page's offset into the
1003  * swap. Unmapped pages return a null PFN. This allows determining
1004  * precisely which pages are mapped (or in swap) and comparing mapped
1005  * pages between processes.
1006  *
1007  * Efficient users of this interface will use /proc/pid/maps to
1008  * determine which areas of memory are actually mapped and llseek to
1009  * skip over unmapped regions.
1010  */
1011 static ssize_t pagemap_read(struct file *file, char __user *buf,
1012                             size_t count, loff_t *ppos)
1013 {
1014         struct task_struct *task = get_proc_task(file_inode(file));
1015         struct mm_struct *mm;
1016         struct pagemapread pm;
1017         int ret = -ESRCH;
1018         struct mm_walk pagemap_walk = {};
1019         unsigned long src;
1020         unsigned long svpfn;
1021         unsigned long start_vaddr;
1022         unsigned long end_vaddr;
1023         int copied = 0;
1024 
1025         if (!task)
1026                 goto out;
1027 
1028         ret = -EINVAL;
1029         /* file position must be aligned */
1030         if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
1031                 goto out_task;
1032 
1033         ret = 0;
1034         if (!count)
1035                 goto out_task;
1036 
1037         pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1038         pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
1039         ret = -ENOMEM;
1040         if (!pm.buffer)
1041                 goto out_task;
1042 
1043         mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1044         ret = PTR_ERR(mm);
1045         if (!mm || IS_ERR(mm))
1046                 goto out_free;
1047 
1048         pagemap_walk.pmd_entry = pagemap_pte_range;
1049         pagemap_walk.pte_hole = pagemap_pte_hole;
1050 #ifdef CONFIG_HUGETLB_PAGE
1051         pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1052 #endif
1053         pagemap_walk.mm = mm;
1054         pagemap_walk.private = &pm;
1055 
1056         src = *ppos;
1057         svpfn = src / PM_ENTRY_BYTES;
1058         start_vaddr = svpfn << PAGE_SHIFT;
1059         end_vaddr = TASK_SIZE_OF(task);
1060 
1061         /* watch out for wraparound */
1062         if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1063                 start_vaddr = end_vaddr;
1064 
1065         /*
1066          * The odds are that this will stop walking way
1067          * before end_vaddr, because the length of the
1068          * user buffer is tracked in "pm", and the walk
1069          * will stop when we hit the end of the buffer.
1070          */
1071         ret = 0;
1072         while (count && (start_vaddr < end_vaddr)) {
1073                 int len;
1074                 unsigned long end;
1075 
1076                 pm.pos = 0;
1077                 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
1078                 /* overflow ? */
1079                 if (end < start_vaddr || end > end_vaddr)
1080                         end = end_vaddr;
1081                 down_read(&mm->mmap_sem);
1082                 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1083                 up_read(&mm->mmap_sem);
1084                 start_vaddr = end;
1085 
1086                 len = min(count, PM_ENTRY_BYTES * pm.pos);
1087                 if (copy_to_user(buf, pm.buffer, len)) {
1088                         ret = -EFAULT;
1089                         goto out_mm;
1090                 }
1091                 copied += len;
1092                 buf += len;
1093                 count -= len;
1094         }
1095         *ppos += copied;
1096         if (!ret || ret == PM_END_OF_BUFFER)
1097                 ret = copied;
1098 
1099 out_mm:
1100         mmput(mm);
1101 out_free:
1102         kfree(pm.buffer);
1103 out_task:
1104         put_task_struct(task);
1105 out:
1106         return ret;
1107 }
1108 
1109 static int pagemap_open(struct inode *inode, struct file *file)
1110 {
1111         /* do not disclose physical addresses to unprivileged
1112            userspace (closes a rowhammer attack vector) */
1113         if (!capable(CAP_SYS_ADMIN))
1114                 return -EPERM;
1115         return 0;
1116 }
1117 
1118 const struct file_operations proc_pagemap_operations = {
1119         .llseek         = mem_lseek, /* borrow this */
1120         .read           = pagemap_read,
1121         .open           = pagemap_open,
1122 };
1123 #endif /* CONFIG_PROC_PAGE_MONITOR */
1124 
1125 #ifdef CONFIG_NUMA
1126 
1127 struct numa_maps {
1128         struct vm_area_struct *vma;
1129         unsigned long pages;
1130         unsigned long anon;
1131         unsigned long active;
1132         unsigned long writeback;
1133         unsigned long mapcount_max;
1134         unsigned long dirty;
1135         unsigned long swapcache;
1136         unsigned long node[MAX_NUMNODES];
1137 };
1138 
1139 struct numa_maps_private {
1140         struct proc_maps_private proc_maps;
1141         struct numa_maps md;
1142 };
1143 
1144 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1145                         unsigned long nr_pages)
1146 {
1147         int count = page_mapcount(page);
1148 
1149         md->pages += nr_pages;
1150         if (pte_dirty || PageDirty(page))
1151                 md->dirty += nr_pages;
1152 
1153         if (PageSwapCache(page))
1154                 md->swapcache += nr_pages;
1155 
1156         if (PageActive(page) || PageUnevictable(page))
1157                 md->active += nr_pages;
1158 
1159         if (PageWriteback(page))
1160                 md->writeback += nr_pages;
1161 
1162         if (PageAnon(page))
1163                 md->anon += nr_pages;
1164 
1165         if (count > md->mapcount_max)
1166                 md->mapcount_max = count;
1167 
1168         md->node[page_to_nid(page)] += nr_pages;
1169 }
1170 
1171 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1172                 unsigned long addr)
1173 {
1174         struct page *page;
1175         int nid;
1176 
1177         if (!pte_present(pte))
1178                 return NULL;
1179 
1180         page = vm_normal_page(vma, addr, pte);
1181         if (!page)
1182                 return NULL;
1183 
1184         if (PageReserved(page))
1185                 return NULL;
1186 
1187         nid = page_to_nid(page);
1188         if (!node_isset(nid, node_states[N_MEMORY]))
1189                 return NULL;
1190 
1191         return page;
1192 }
1193 
1194 static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1195                 unsigned long end, struct mm_walk *walk)
1196 {
1197         struct numa_maps *md;
1198         spinlock_t *ptl;
1199         pte_t *orig_pte;
1200         pte_t *pte;
1201 
1202         md = walk->private;
1203 
1204         if (pmd_trans_huge_lock(pmd, md->vma) == 1) {
1205                 pte_t huge_pte = *(pte_t *)pmd;
1206                 struct page *page;
1207 
1208                 page = can_gather_numa_stats(huge_pte, md->vma, addr);
1209                 if (page)
1210                         gather_stats(page, md, pte_dirty(huge_pte),
1211                                      HPAGE_PMD_SIZE/PAGE_SIZE);
1212                 spin_unlock(&walk->mm->page_table_lock);
1213                 return 0;
1214         }
1215 
1216         if (pmd_trans_unstable(pmd))
1217                 return 0;
1218         orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1219         do {
1220                 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
1221                 if (!page)
1222                         continue;
1223                 gather_stats(page, md, pte_dirty(*pte), 1);
1224 
1225         } while (pte++, addr += PAGE_SIZE, addr != end);
1226         pte_unmap_unlock(orig_pte, ptl);
1227         return 0;
1228 }
1229 #ifdef CONFIG_HUGETLB_PAGE
1230 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1231                 unsigned long addr, unsigned long end, struct mm_walk *walk)
1232 {
1233         struct numa_maps *md;
1234         struct page *page;
1235 
1236         if (pte_none(*pte))
1237                 return 0;
1238 
1239         page = pte_page(*pte);
1240         if (!page)
1241                 return 0;
1242 
1243         md = walk->private;
1244         gather_stats(page, md, pte_dirty(*pte), 1);
1245         return 0;
1246 }
1247 
1248 #else
1249 static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1250                 unsigned long addr, unsigned long end, struct mm_walk *walk)
1251 {
1252         return 0;
1253 }
1254 #endif
1255 
1256 /*
1257  * Display pages allocated per node and memory policy via /proc.
1258  */
1259 static int show_numa_map(struct seq_file *m, void *v, int is_pid)
1260 {
1261         struct numa_maps_private *numa_priv = m->private;
1262         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
1263         struct vm_area_struct *vma = v;
1264         struct numa_maps *md = &numa_priv->md;
1265         struct file *file = vma->vm_file;
1266         struct task_struct *task = proc_priv->task;
1267         struct mm_struct *mm = vma->vm_mm;
1268         struct mm_walk walk = {};
1269         struct mempolicy *pol;
1270         int n;
1271         char buffer[50];
1272 
1273         if (!mm)
1274                 return 0;
1275 
1276         /* Ensure we start with an empty set of numa_maps statistics. */
1277         memset(md, 0, sizeof(*md));
1278 
1279         md->vma = vma;
1280 
1281         walk.hugetlb_entry = gather_hugetbl_stats;
1282         walk.pmd_entry = gather_pte_stats;
1283         walk.private = md;
1284         walk.mm = mm;
1285 
1286         pol = get_vma_policy(task, vma, vma->vm_start);
1287         mpol_to_str(buffer, sizeof(buffer), pol);
1288         mpol_cond_put(pol);
1289 
1290         seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1291 
1292         if (file) {
1293                 seq_printf(m, " file=");
1294                 seq_path(m, &file->f_path, "\n\t= ");
1295         } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1296                 seq_printf(m, " heap");
1297         } else {
1298                 pid_t tid = vm_is_stack(task, vma, is_pid);
1299                 if (tid != 0) {
1300                         /*
1301                          * Thread stack in /proc/PID/task/TID/maps or
1302                          * the main process stack.
1303                          */
1304                         if (!is_pid || (vma->vm_start <= mm->start_stack &&
1305                             vma->vm_end >= mm->start_stack))
1306                                 seq_printf(m, " stack");
1307                         else
1308                                 seq_printf(m, " stack:%d", tid);
1309                 }
1310         }
1311 
1312         if (is_vm_hugetlb_page(vma))
1313                 seq_printf(m, " huge");
1314 
1315         walk_page_range(vma->vm_start, vma->vm_end, &walk);
1316 
1317         if (!md->pages)
1318                 goto out;
1319 
1320         if (md->anon)
1321                 seq_printf(m, " anon=%lu", md->anon);
1322 
1323         if (md->dirty)
1324                 seq_printf(m, " dirty=%lu", md->dirty);
1325 
1326         if (md->pages != md->anon && md->pages != md->dirty)
1327                 seq_printf(m, " mapped=%lu", md->pages);
1328 
1329         if (md->mapcount_max > 1)
1330                 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1331 
1332         if (md->swapcache)
1333                 seq_printf(m, " swapcache=%lu", md->swapcache);
1334 
1335         if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1336                 seq_printf(m, " active=%lu", md->active);
1337 
1338         if (md->writeback)
1339                 seq_printf(m, " writeback=%lu", md->writeback);
1340 
1341         for_each_node_state(n, N_MEMORY)
1342                 if (md->node[n])
1343                         seq_printf(m, " N%d=%lu", n, md->node[n]);
1344 out:
1345         seq_putc(m, '\n');
1346 
1347         if (m->count < m->size)
1348                 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
1349         return 0;
1350 }
1351 
1352 static int show_pid_numa_map(struct seq_file *m, void *v)
1353 {
1354         return show_numa_map(m, v, 1);
1355 }
1356 
1357 static int show_tid_numa_map(struct seq_file *m, void *v)
1358 {
1359         return show_numa_map(m, v, 0);
1360 }
1361 
1362 static const struct seq_operations proc_pid_numa_maps_op = {
1363         .start  = m_start,
1364         .next   = m_next,
1365         .stop   = m_stop,
1366         .show   = show_pid_numa_map,
1367 };
1368 
1369 static const struct seq_operations proc_tid_numa_maps_op = {
1370         .start  = m_start,
1371         .next   = m_next,
1372         .stop   = m_stop,
1373         .show   = show_tid_numa_map,
1374 };
1375 
1376 static int numa_maps_open(struct inode *inode, struct file *file,
1377                           const struct seq_operations *ops)
1378 {
1379         struct numa_maps_private *priv;
1380         int ret = -ENOMEM;
1381         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1382         if (priv) {
1383                 priv->proc_maps.pid = proc_pid(inode);
1384                 ret = seq_open(file, ops);
1385                 if (!ret) {
1386                         struct seq_file *m = file->private_data;
1387                         m->private = priv;
1388                 } else {
1389                         kfree(priv);
1390                 }
1391         }
1392         return ret;
1393 }
1394 
1395 static int pid_numa_maps_open(struct inode *inode, struct file *file)
1396 {
1397         return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1398 }
1399 
1400 static int tid_numa_maps_open(struct inode *inode, struct file *file)
1401 {
1402         return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1403 }
1404 
1405 const struct file_operations proc_pid_numa_maps_operations = {
1406         .open           = pid_numa_maps_open,
1407         .read           = seq_read,
1408         .llseek         = seq_lseek,
1409         .release        = seq_release_private,
1410 };
1411 
1412 const struct file_operations proc_tid_numa_maps_operations = {
1413         .open           = tid_numa_maps_open,
1414         .read           = seq_read,
1415         .llseek         = seq_lseek,
1416         .release        = seq_release_private,
1417 };
1418 #endif /* CONFIG_NUMA */
1419 

~ [ 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