]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/proc/task_mmu.c
maps4: introduce a generic page walker
[mirror_ubuntu-bionic-kernel.git] / fs / proc / task_mmu.c
CommitLineData
1da177e4
LT
1#include <linux/mm.h>
2#include <linux/hugetlb.h>
3#include <linux/mount.h>
4#include <linux/seq_file.h>
e070ad49 5#include <linux/highmem.h>
5096add8 6#include <linux/ptrace.h>
6e21c8f1
CL
7#include <linux/pagemap.h>
8#include <linux/mempolicy.h>
e070ad49 9
1da177e4
LT
10#include <asm/elf.h>
11#include <asm/uaccess.h>
e070ad49 12#include <asm/tlbflush.h>
1da177e4
LT
13#include "internal.h"
14
15char *task_mem(struct mm_struct *mm, char *buffer)
16{
17 unsigned long data, text, lib;
365e9c87
HD
18 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
19
20 /*
21 * Note: to minimize their overhead, mm maintains hiwater_vm and
22 * hiwater_rss only when about to *lower* total_vm or rss. Any
23 * collector of these hiwater stats must therefore get total_vm
24 * and rss too, which will usually be the higher. Barriers? not
25 * worth the effort, such snapshots can always be inconsistent.
26 */
27 hiwater_vm = total_vm = mm->total_vm;
28 if (hiwater_vm < mm->hiwater_vm)
29 hiwater_vm = mm->hiwater_vm;
30 hiwater_rss = total_rss = get_mm_rss(mm);
31 if (hiwater_rss < mm->hiwater_rss)
32 hiwater_rss = mm->hiwater_rss;
1da177e4
LT
33
34 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
35 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
36 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
37 buffer += sprintf(buffer,
365e9c87 38 "VmPeak:\t%8lu kB\n"
1da177e4
LT
39 "VmSize:\t%8lu kB\n"
40 "VmLck:\t%8lu kB\n"
365e9c87 41 "VmHWM:\t%8lu kB\n"
1da177e4
LT
42 "VmRSS:\t%8lu kB\n"
43 "VmData:\t%8lu kB\n"
44 "VmStk:\t%8lu kB\n"
45 "VmExe:\t%8lu kB\n"
46 "VmLib:\t%8lu kB\n"
47 "VmPTE:\t%8lu kB\n",
365e9c87
HD
48 hiwater_vm << (PAGE_SHIFT-10),
49 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
1da177e4 50 mm->locked_vm << (PAGE_SHIFT-10),
365e9c87
HD
51 hiwater_rss << (PAGE_SHIFT-10),
52 total_rss << (PAGE_SHIFT-10),
1da177e4
LT
53 data << (PAGE_SHIFT-10),
54 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
55 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
56 return buffer;
57}
58
59unsigned long task_vsize(struct mm_struct *mm)
60{
61 return PAGE_SIZE * mm->total_vm;
62}
63
64int task_statm(struct mm_struct *mm, int *shared, int *text,
65 int *data, int *resident)
66{
4294621f 67 *shared = get_mm_counter(mm, file_rss);
1da177e4
LT
68 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
69 >> PAGE_SHIFT;
70 *data = mm->total_vm - mm->shared_vm;
4294621f 71 *resident = *shared + get_mm_counter(mm, anon_rss);
1da177e4
LT
72 return mm->total_vm;
73}
74
75int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
76{
77 struct vm_area_struct * vma;
78 int result = -ENOENT;
99f89551
EB
79 struct task_struct *task = get_proc_task(inode);
80 struct mm_struct * mm = NULL;
1da177e4 81
99f89551
EB
82 if (task) {
83 mm = get_task_mm(task);
84 put_task_struct(task);
85 }
1da177e4
LT
86 if (!mm)
87 goto out;
88 down_read(&mm->mmap_sem);
89
90 vma = mm->mmap;
91 while (vma) {
92 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
93 break;
94 vma = vma->vm_next;
95 }
96
97 if (vma) {
2fddfeef
JJS
98 *mnt = mntget(vma->vm_file->f_path.mnt);
99 *dentry = dget(vma->vm_file->f_path.dentry);
1da177e4
LT
100 result = 0;
101 }
102
103 up_read(&mm->mmap_sem);
104 mmput(mm);
105out:
106 return result;
107}
108
109static void pad_len_spaces(struct seq_file *m, int len)
110{
111 len = 25 + sizeof(void*) * 6 - len;
112 if (len < 1)
113 len = 1;
114 seq_printf(m, "%*c", len, ' ');
115}
116
ec4dd3eb
FW
117/*
118 * Proportional Set Size(PSS): my share of RSS.
119 *
120 * PSS of a process is the count of pages it has in memory, where each
121 * page is divided by the number of processes sharing it. So if a
122 * process has 1000 pages all to itself, and 1000 shared with one other
123 * process, its PSS will be 1500.
124 *
125 * To keep (accumulated) division errors low, we adopt a 64bit
126 * fixed-point pss counter to minimize division errors. So (pss >>
127 * PSS_SHIFT) would be the real byte count.
128 *
129 * A shift of 12 before division means (assuming 4K page size):
130 * - 1M 3-user-pages add up to 8KB errors;
131 * - supports mapcount up to 2^24, or 16M;
132 * - supports PSS up to 2^52 bytes, or 4PB.
133 */
134#define PSS_SHIFT 12
135
e070ad49
ML
136struct mem_size_stats
137{
138 unsigned long resident;
139 unsigned long shared_clean;
140 unsigned long shared_dirty;
141 unsigned long private_clean;
142 unsigned long private_dirty;
f79f177c 143 unsigned long referenced;
ec4dd3eb 144 u64 pss;
e070ad49
ML
145};
146
826fad1b
DR
147struct pmd_walker {
148 struct vm_area_struct *vma;
149 void *private;
150 void (*action)(struct vm_area_struct *, pmd_t *, unsigned long,
151 unsigned long, void *);
152};
153
e070ad49 154static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
1da177e4 155{
99f89551
EB
156 struct proc_maps_private *priv = m->private;
157 struct task_struct *task = priv->task;
e070ad49
ML
158 struct vm_area_struct *vma = v;
159 struct mm_struct *mm = vma->vm_mm;
160 struct file *file = vma->vm_file;
161 int flags = vma->vm_flags;
1da177e4
LT
162 unsigned long ino = 0;
163 dev_t dev = 0;
164 int len;
165
5096add8
KC
166 if (maps_protect && !ptrace_may_attach(task))
167 return -EACCES;
168
1da177e4 169 if (file) {
2fddfeef 170 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1da177e4
LT
171 dev = inode->i_sb->s_dev;
172 ino = inode->i_ino;
173 }
174
175 seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
e070ad49
ML
176 vma->vm_start,
177 vma->vm_end,
1da177e4
LT
178 flags & VM_READ ? 'r' : '-',
179 flags & VM_WRITE ? 'w' : '-',
180 flags & VM_EXEC ? 'x' : '-',
181 flags & VM_MAYSHARE ? 's' : 'p',
e070ad49 182 vma->vm_pgoff << PAGE_SHIFT,
1da177e4
LT
183 MAJOR(dev), MINOR(dev), ino, &len);
184
185 /*
186 * Print the dentry name for named mappings, and a
187 * special [heap] marker for the heap:
188 */
e070ad49 189 if (file) {
1da177e4 190 pad_len_spaces(m, len);
2fddfeef 191 seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n");
1da177e4 192 } else {
e6e5494c
IM
193 const char *name = arch_vma_name(vma);
194 if (!name) {
195 if (mm) {
196 if (vma->vm_start <= mm->start_brk &&
e070ad49 197 vma->vm_end >= mm->brk) {
e6e5494c
IM
198 name = "[heap]";
199 } else if (vma->vm_start <= mm->start_stack &&
200 vma->vm_end >= mm->start_stack) {
201 name = "[stack]";
1da177e4 202 }
e6e5494c
IM
203 } else {
204 name = "[vdso]";
1da177e4 205 }
e6e5494c
IM
206 }
207 if (name) {
1da177e4 208 pad_len_spaces(m, len);
e6e5494c 209 seq_puts(m, name);
1da177e4
LT
210 }
211 }
212 seq_putc(m, '\n');
e070ad49
ML
213
214 if (mss)
215 seq_printf(m,
f79f177c
DR
216 "Size: %8lu kB\n"
217 "Rss: %8lu kB\n"
ec4dd3eb 218 "Pss: %8lu kB\n"
f79f177c
DR
219 "Shared_Clean: %8lu kB\n"
220 "Shared_Dirty: %8lu kB\n"
221 "Private_Clean: %8lu kB\n"
222 "Private_Dirty: %8lu kB\n"
b813e931 223 "Referenced: %8lu kB\n",
e070ad49
ML
224 (vma->vm_end - vma->vm_start) >> 10,
225 mss->resident >> 10,
ec4dd3eb 226 (unsigned long)(mss->pss >> (10 + PSS_SHIFT)),
e070ad49
ML
227 mss->shared_clean >> 10,
228 mss->shared_dirty >> 10,
229 mss->private_clean >> 10,
f79f177c
DR
230 mss->private_dirty >> 10,
231 mss->referenced >> 10);
e070ad49
ML
232
233 if (m->count < m->size) /* vma is copied successfully */
234 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
1da177e4
LT
235 return 0;
236}
237
e070ad49
ML
238static int show_map(struct seq_file *m, void *v)
239{
0f5c79f2 240 return show_map_internal(m, v, NULL);
e070ad49
ML
241}
242
b813e931
DR
243static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
244 unsigned long addr, unsigned long end,
245 void *private)
e070ad49 246{
826fad1b 247 struct mem_size_stats *mss = private;
e070ad49 248 pte_t *pte, ptent;
705e87c0 249 spinlock_t *ptl;
e070ad49 250 struct page *page;
ec4dd3eb 251 int mapcount;
e070ad49 252
705e87c0 253 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
826fad1b 254 for (; addr != end; pte++, addr += PAGE_SIZE) {
e070ad49 255 ptent = *pte;
705e87c0 256 if (!pte_present(ptent))
e070ad49
ML
257 continue;
258
259 mss->resident += PAGE_SIZE;
ad820c5d
NP
260
261 page = vm_normal_page(vma, addr, ptent);
262 if (!page)
e070ad49
ML
263 continue;
264
f79f177c
DR
265 /* Accumulate the size in pages that have been accessed. */
266 if (pte_young(ptent) || PageReferenced(page))
267 mss->referenced += PAGE_SIZE;
ec4dd3eb
FW
268 mapcount = page_mapcount(page);
269 if (mapcount >= 2) {
e070ad49
ML
270 if (pte_dirty(ptent))
271 mss->shared_dirty += PAGE_SIZE;
272 else
273 mss->shared_clean += PAGE_SIZE;
ec4dd3eb 274 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
e070ad49
ML
275 } else {
276 if (pte_dirty(ptent))
277 mss->private_dirty += PAGE_SIZE;
278 else
279 mss->private_clean += PAGE_SIZE;
ec4dd3eb 280 mss->pss += (PAGE_SIZE << PSS_SHIFT);
e070ad49 281 }
826fad1b 282 }
705e87c0
HD
283 pte_unmap_unlock(pte - 1, ptl);
284 cond_resched();
e070ad49
ML
285}
286
b813e931
DR
287static void clear_refs_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
288 unsigned long addr, unsigned long end,
289 void *private)
290{
291 pte_t *pte, ptent;
292 spinlock_t *ptl;
293 struct page *page;
294
295 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
296 for (; addr != end; pte++, addr += PAGE_SIZE) {
297 ptent = *pte;
298 if (!pte_present(ptent))
299 continue;
300
301 page = vm_normal_page(vma, addr, ptent);
302 if (!page)
303 continue;
304
305 /* Clear accessed and referenced bits. */
306 ptep_test_and_clear_young(vma, addr, pte);
307 ClearPageReferenced(page);
308 }
309 pte_unmap_unlock(pte - 1, ptl);
310 cond_resched();
311}
312
313static inline void walk_pmd_range(struct pmd_walker *walker, pud_t *pud,
314 unsigned long addr, unsigned long end)
e070ad49
ML
315{
316 pmd_t *pmd;
317 unsigned long next;
318
826fad1b
DR
319 for (pmd = pmd_offset(pud, addr); addr != end;
320 pmd++, addr = next) {
e070ad49
ML
321 next = pmd_addr_end(addr, end);
322 if (pmd_none_or_clear_bad(pmd))
323 continue;
826fad1b
DR
324 walker->action(walker->vma, pmd, addr, next, walker->private);
325 }
e070ad49
ML
326}
327
b813e931
DR
328static inline void walk_pud_range(struct pmd_walker *walker, pgd_t *pgd,
329 unsigned long addr, unsigned long end)
e070ad49
ML
330{
331 pud_t *pud;
332 unsigned long next;
333
826fad1b
DR
334 for (pud = pud_offset(pgd, addr); addr != end;
335 pud++, addr = next) {
e070ad49
ML
336 next = pud_addr_end(addr, end);
337 if (pud_none_or_clear_bad(pud))
338 continue;
b813e931 339 walk_pmd_range(walker, pud, addr, next);
826fad1b 340 }
e070ad49
ML
341}
342
b813e931
DR
343/*
344 * walk_page_range - walk the page tables of a VMA with a callback
345 * @vma - VMA to walk
346 * @action - callback invoked for every bottom-level (PTE) page table
347 * @private - private data passed to the callback function
348 *
349 * Recursively walk the page table for the memory area in a VMA, calling
350 * a callback for every bottom-level (PTE) page table.
351 */
352static inline void walk_page_range(struct vm_area_struct *vma,
353 void (*action)(struct vm_area_struct *,
354 pmd_t *, unsigned long,
355 unsigned long, void *),
356 void *private)
e070ad49 357{
826fad1b
DR
358 unsigned long addr = vma->vm_start;
359 unsigned long end = vma->vm_end;
360 struct pmd_walker walker = {
361 .vma = vma,
362 .private = private,
363 .action = action,
364 };
e070ad49
ML
365 pgd_t *pgd;
366 unsigned long next;
367
826fad1b
DR
368 for (pgd = pgd_offset(vma->vm_mm, addr); addr != end;
369 pgd++, addr = next) {
e070ad49
ML
370 next = pgd_addr_end(addr, end);
371 if (pgd_none_or_clear_bad(pgd))
372 continue;
b813e931 373 walk_pud_range(&walker, pgd, addr, next);
826fad1b 374 }
e070ad49
ML
375}
376
377static int show_smap(struct seq_file *m, void *v)
378{
379 struct vm_area_struct *vma = v;
e070ad49
ML
380 struct mem_size_stats mss;
381
382 memset(&mss, 0, sizeof mss);
5ddfae16 383 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
b813e931 384 walk_page_range(vma, smaps_pte_range, &mss);
e070ad49
ML
385 return show_map_internal(m, v, &mss);
386}
387
b813e931
DR
388void clear_refs_smap(struct mm_struct *mm)
389{
390 struct vm_area_struct *vma;
391
392 down_read(&mm->mmap_sem);
393 for (vma = mm->mmap; vma; vma = vma->vm_next)
394 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
395 walk_page_range(vma, clear_refs_pte_range, NULL);
396 flush_tlb_mm(mm);
397 up_read(&mm->mmap_sem);
398}
399
1da177e4
LT
400static void *m_start(struct seq_file *m, loff_t *pos)
401{
99f89551 402 struct proc_maps_private *priv = m->private;
1da177e4
LT
403 unsigned long last_addr = m->version;
404 struct mm_struct *mm;
99f89551 405 struct vm_area_struct *vma, *tail_vma = NULL;
1da177e4
LT
406 loff_t l = *pos;
407
99f89551
EB
408 /* Clear the per syscall fields in priv */
409 priv->task = NULL;
410 priv->tail_vma = NULL;
411
1da177e4
LT
412 /*
413 * We remember last_addr rather than next_addr to hit with
414 * mmap_cache most of the time. We have zero last_addr at
e070ad49
ML
415 * the beginning and also after lseek. We will have -1 last_addr
416 * after the end of the vmas.
1da177e4
LT
417 */
418
419 if (last_addr == -1UL)
420 return NULL;
421
13b41b09 422 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
99f89551
EB
423 if (!priv->task)
424 return NULL;
425
831830b5 426 mm = mm_for_maps(priv->task);
1da177e4
LT
427 if (!mm)
428 return NULL;
429
99f89551 430 priv->tail_vma = tail_vma = get_gate_vma(priv->task);
1da177e4
LT
431
432 /* Start with last addr hint */
e070ad49
ML
433 if (last_addr && (vma = find_vma(mm, last_addr))) {
434 vma = vma->vm_next;
1da177e4
LT
435 goto out;
436 }
437
438 /*
e070ad49 439 * Check the vma index is within the range and do
1da177e4
LT
440 * sequential scan until m_index.
441 */
e070ad49 442 vma = NULL;
1da177e4 443 if ((unsigned long)l < mm->map_count) {
e070ad49
ML
444 vma = mm->mmap;
445 while (l-- && vma)
446 vma = vma->vm_next;
1da177e4
LT
447 goto out;
448 }
449
450 if (l != mm->map_count)
e070ad49 451 tail_vma = NULL; /* After gate vma */
1da177e4
LT
452
453out:
e070ad49
ML
454 if (vma)
455 return vma;
1da177e4 456
e070ad49
ML
457 /* End of vmas has been reached */
458 m->version = (tail_vma != NULL)? 0: -1UL;
1da177e4
LT
459 up_read(&mm->mmap_sem);
460 mmput(mm);
e070ad49 461 return tail_vma;
1da177e4
LT
462}
463
99f89551 464static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
1da177e4 465{
99f89551 466 if (vma && vma != priv->tail_vma) {
e070ad49 467 struct mm_struct *mm = vma->vm_mm;
1da177e4
LT
468 up_read(&mm->mmap_sem);
469 mmput(mm);
470 }
471}
472
473static void *m_next(struct seq_file *m, void *v, loff_t *pos)
474{
99f89551 475 struct proc_maps_private *priv = m->private;
e070ad49 476 struct vm_area_struct *vma = v;
99f89551 477 struct vm_area_struct *tail_vma = priv->tail_vma;
1da177e4
LT
478
479 (*pos)++;
e070ad49
ML
480 if (vma && (vma != tail_vma) && vma->vm_next)
481 return vma->vm_next;
99f89551 482 vma_stop(priv, vma);
e070ad49 483 return (vma != tail_vma)? tail_vma: NULL;
1da177e4
LT
484}
485
99f89551
EB
486static void m_stop(struct seq_file *m, void *v)
487{
488 struct proc_maps_private *priv = m->private;
489 struct vm_area_struct *vma = v;
490
491 vma_stop(priv, vma);
492 if (priv->task)
493 put_task_struct(priv->task);
494}
495
662795de 496static struct seq_operations proc_pid_maps_op = {
1da177e4
LT
497 .start = m_start,
498 .next = m_next,
499 .stop = m_stop,
500 .show = show_map
501};
6e21c8f1 502
662795de 503static struct seq_operations proc_pid_smaps_op = {
e070ad49
ML
504 .start = m_start,
505 .next = m_next,
506 .stop = m_stop,
507 .show = show_smap
508};
509
662795de
EB
510static int do_maps_open(struct inode *inode, struct file *file,
511 struct seq_operations *ops)
512{
99f89551
EB
513 struct proc_maps_private *priv;
514 int ret = -ENOMEM;
515 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
516 if (priv) {
13b41b09 517 priv->pid = proc_pid(inode);
99f89551
EB
518 ret = seq_open(file, ops);
519 if (!ret) {
520 struct seq_file *m = file->private_data;
521 m->private = priv;
522 } else {
523 kfree(priv);
524 }
662795de
EB
525 }
526 return ret;
527}
528
529static int maps_open(struct inode *inode, struct file *file)
530{
531 return do_maps_open(inode, file, &proc_pid_maps_op);
532}
533
00977a59 534const struct file_operations proc_maps_operations = {
662795de
EB
535 .open = maps_open,
536 .read = seq_read,
537 .llseek = seq_lseek,
99f89551 538 .release = seq_release_private,
662795de
EB
539};
540
6e21c8f1 541#ifdef CONFIG_NUMA
1a75a6c8 542extern int show_numa_map(struct seq_file *m, void *v);
6e21c8f1 543
5096add8
KC
544static int show_numa_map_checked(struct seq_file *m, void *v)
545{
546 struct proc_maps_private *priv = m->private;
547 struct task_struct *task = priv->task;
548
549 if (maps_protect && !ptrace_may_attach(task))
550 return -EACCES;
551
552 return show_numa_map(m, v);
553}
554
662795de 555static struct seq_operations proc_pid_numa_maps_op = {
1a75a6c8
CL
556 .start = m_start,
557 .next = m_next,
558 .stop = m_stop,
5096add8 559 .show = show_numa_map_checked
6e21c8f1 560};
662795de
EB
561
562static int numa_maps_open(struct inode *inode, struct file *file)
563{
564 return do_maps_open(inode, file, &proc_pid_numa_maps_op);
565}
566
00977a59 567const struct file_operations proc_numa_maps_operations = {
662795de
EB
568 .open = numa_maps_open,
569 .read = seq_read,
570 .llseek = seq_lseek,
99f89551 571 .release = seq_release_private,
662795de 572};
6e21c8f1 573#endif
662795de
EB
574
575static int smaps_open(struct inode *inode, struct file *file)
576{
577 return do_maps_open(inode, file, &proc_pid_smaps_op);
578}
579
00977a59 580const struct file_operations proc_smaps_operations = {
662795de
EB
581 .open = smaps_open,
582 .read = seq_read,
583 .llseek = seq_lseek,
99f89551 584 .release = seq_release_private,
662795de 585};