blob: 50bd5a8f0446d902cc6161032fde8ac5fcadd7a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/mm.h>
2#include <linux/hugetlb.h>
3#include <linux/mount.h>
4#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07005#include <linux/highmem.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07006#include <linux/pagemap.h>
7#include <linux/mempolicy.h>
Mauricio Line070ad42005-09-03 15:55:10 -07008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/elf.h>
10#include <asm/uaccess.h>
Mauricio Line070ad42005-09-03 15:55:10 -070011#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "internal.h"
13
14char *task_mem(struct mm_struct *mm, char *buffer)
15{
16 unsigned long data, text, lib;
Hugh Dickins365e9c872005-10-29 18:16:18 -070017 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
18
19 /*
20 * Note: to minimize their overhead, mm maintains hiwater_vm and
21 * hiwater_rss only when about to *lower* total_vm or rss. Any
22 * collector of these hiwater stats must therefore get total_vm
23 * and rss too, which will usually be the higher. Barriers? not
24 * worth the effort, such snapshots can always be inconsistent.
25 */
26 hiwater_vm = total_vm = mm->total_vm;
27 if (hiwater_vm < mm->hiwater_vm)
28 hiwater_vm = mm->hiwater_vm;
29 hiwater_rss = total_rss = get_mm_rss(mm);
30 if (hiwater_rss < mm->hiwater_rss)
31 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
34 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
35 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
36 buffer += sprintf(buffer,
Hugh Dickins365e9c872005-10-29 18:16:18 -070037 "VmPeak:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 "VmSize:\t%8lu kB\n"
39 "VmLck:\t%8lu kB\n"
Hugh Dickins365e9c872005-10-29 18:16:18 -070040 "VmHWM:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 "VmRSS:\t%8lu kB\n"
42 "VmData:\t%8lu kB\n"
43 "VmStk:\t%8lu kB\n"
44 "VmExe:\t%8lu kB\n"
45 "VmLib:\t%8lu kB\n"
46 "VmPTE:\t%8lu kB\n",
Hugh Dickins365e9c872005-10-29 18:16:18 -070047 hiwater_vm << (PAGE_SHIFT-10),
48 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 mm->locked_vm << (PAGE_SHIFT-10),
Hugh Dickins365e9c872005-10-29 18:16:18 -070050 hiwater_rss << (PAGE_SHIFT-10),
51 total_rss << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 data << (PAGE_SHIFT-10),
53 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
54 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
55 return buffer;
56}
57
58unsigned long task_vsize(struct mm_struct *mm)
59{
60 return PAGE_SIZE * mm->total_vm;
61}
62
63int task_statm(struct mm_struct *mm, int *shared, int *text,
64 int *data, int *resident)
65{
Hugh Dickins42946212005-10-29 18:16:05 -070066 *shared = get_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
68 >> PAGE_SHIFT;
69 *data = mm->total_vm - mm->shared_vm;
Hugh Dickins42946212005-10-29 18:16:05 -070070 *resident = *shared + get_mm_counter(mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return mm->total_vm;
72}
73
74int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
75{
76 struct vm_area_struct * vma;
77 int result = -ENOENT;
78 struct task_struct *task = proc_task(inode);
79 struct mm_struct * mm = get_task_mm(task);
80
81 if (!mm)
82 goto out;
83 down_read(&mm->mmap_sem);
84
85 vma = mm->mmap;
86 while (vma) {
87 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
88 break;
89 vma = vma->vm_next;
90 }
91
92 if (vma) {
93 *mnt = mntget(vma->vm_file->f_vfsmnt);
94 *dentry = dget(vma->vm_file->f_dentry);
95 result = 0;
96 }
97
98 up_read(&mm->mmap_sem);
99 mmput(mm);
100out:
101 return result;
102}
103
104static void pad_len_spaces(struct seq_file *m, int len)
105{
106 len = 25 + sizeof(void*) * 6 - len;
107 if (len < 1)
108 len = 1;
109 seq_printf(m, "%*c", len, ' ');
110}
111
Mauricio Line070ad42005-09-03 15:55:10 -0700112struct mem_size_stats
113{
114 unsigned long resident;
115 unsigned long shared_clean;
116 unsigned long shared_dirty;
117 unsigned long private_clean;
118 unsigned long private_dirty;
119};
120
121static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct task_struct *task = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700124 struct vm_area_struct *vma = v;
125 struct mm_struct *mm = vma->vm_mm;
126 struct file *file = vma->vm_file;
127 int flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned long ino = 0;
129 dev_t dev = 0;
130 int len;
131
132 if (file) {
Mauricio Line070ad42005-09-03 15:55:10 -0700133 struct inode *inode = vma->vm_file->f_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dev = inode->i_sb->s_dev;
135 ino = inode->i_ino;
136 }
137
138 seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
Mauricio Line070ad42005-09-03 15:55:10 -0700139 vma->vm_start,
140 vma->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 flags & VM_READ ? 'r' : '-',
142 flags & VM_WRITE ? 'w' : '-',
143 flags & VM_EXEC ? 'x' : '-',
144 flags & VM_MAYSHARE ? 's' : 'p',
Mauricio Line070ad42005-09-03 15:55:10 -0700145 vma->vm_pgoff << PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 MAJOR(dev), MINOR(dev), ino, &len);
147
148 /*
149 * Print the dentry name for named mappings, and a
150 * special [heap] marker for the heap:
151 */
Mauricio Line070ad42005-09-03 15:55:10 -0700152 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 pad_len_spaces(m, len);
Mauricio Line070ad42005-09-03 15:55:10 -0700154 seq_path(m, file->f_vfsmnt, file->f_dentry, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
156 if (mm) {
Mauricio Line070ad42005-09-03 15:55:10 -0700157 if (vma->vm_start <= mm->start_brk &&
158 vma->vm_end >= mm->brk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 pad_len_spaces(m, len);
160 seq_puts(m, "[heap]");
161 } else {
Mauricio Line070ad42005-09-03 15:55:10 -0700162 if (vma->vm_start <= mm->start_stack &&
163 vma->vm_end >= mm->start_stack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 pad_len_spaces(m, len);
166 seq_puts(m, "[stack]");
167 }
168 }
169 } else {
170 pad_len_spaces(m, len);
171 seq_puts(m, "[vdso]");
172 }
173 }
174 seq_putc(m, '\n');
Mauricio Line070ad42005-09-03 15:55:10 -0700175
176 if (mss)
177 seq_printf(m,
178 "Size: %8lu kB\n"
179 "Rss: %8lu kB\n"
180 "Shared_Clean: %8lu kB\n"
181 "Shared_Dirty: %8lu kB\n"
182 "Private_Clean: %8lu kB\n"
183 "Private_Dirty: %8lu kB\n",
184 (vma->vm_end - vma->vm_start) >> 10,
185 mss->resident >> 10,
186 mss->shared_clean >> 10,
187 mss->shared_dirty >> 10,
188 mss->private_clean >> 10,
189 mss->private_dirty >> 10);
190
191 if (m->count < m->size) /* vma is copied successfully */
192 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194}
195
Mauricio Line070ad42005-09-03 15:55:10 -0700196static int show_map(struct seq_file *m, void *v)
197{
Luiz Fernando Capitulino0f5c79f2005-11-13 16:07:20 -0800198 return show_map_internal(m, v, NULL);
Mauricio Line070ad42005-09-03 15:55:10 -0700199}
200
201static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
202 unsigned long addr, unsigned long end,
203 struct mem_size_stats *mss)
204{
205 pte_t *pte, ptent;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700206 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700207 unsigned long pfn;
208 struct page *page;
209
Hugh Dickins705e87c2005-10-29 18:16:27 -0700210 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Mauricio Line070ad42005-09-03 15:55:10 -0700211 do {
212 ptent = *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700213 if (!pte_present(ptent))
Mauricio Line070ad42005-09-03 15:55:10 -0700214 continue;
215
216 mss->resident += PAGE_SIZE;
217 pfn = pte_pfn(ptent);
218 if (!pfn_valid(pfn))
219 continue;
220
221 page = pfn_to_page(pfn);
222 if (page_count(page) >= 2) {
223 if (pte_dirty(ptent))
224 mss->shared_dirty += PAGE_SIZE;
225 else
226 mss->shared_clean += PAGE_SIZE;
227 } else {
228 if (pte_dirty(ptent))
229 mss->private_dirty += PAGE_SIZE;
230 else
231 mss->private_clean += PAGE_SIZE;
232 }
233 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700234 pte_unmap_unlock(pte - 1, ptl);
235 cond_resched();
Mauricio Line070ad42005-09-03 15:55:10 -0700236}
237
238static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
239 unsigned long addr, unsigned long end,
240 struct mem_size_stats *mss)
241{
242 pmd_t *pmd;
243 unsigned long next;
244
245 pmd = pmd_offset(pud, addr);
246 do {
247 next = pmd_addr_end(addr, end);
248 if (pmd_none_or_clear_bad(pmd))
249 continue;
250 smaps_pte_range(vma, pmd, addr, next, mss);
251 } while (pmd++, addr = next, addr != end);
252}
253
254static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
255 unsigned long addr, unsigned long end,
256 struct mem_size_stats *mss)
257{
258 pud_t *pud;
259 unsigned long next;
260
261 pud = pud_offset(pgd, addr);
262 do {
263 next = pud_addr_end(addr, end);
264 if (pud_none_or_clear_bad(pud))
265 continue;
266 smaps_pmd_range(vma, pud, addr, next, mss);
267 } while (pud++, addr = next, addr != end);
268}
269
270static inline void smaps_pgd_range(struct vm_area_struct *vma,
271 unsigned long addr, unsigned long end,
272 struct mem_size_stats *mss)
273{
274 pgd_t *pgd;
275 unsigned long next;
276
277 pgd = pgd_offset(vma->vm_mm, addr);
278 do {
279 next = pgd_addr_end(addr, end);
280 if (pgd_none_or_clear_bad(pgd))
281 continue;
282 smaps_pud_range(vma, pgd, addr, next, mss);
283 } while (pgd++, addr = next, addr != end);
284}
285
286static int show_smap(struct seq_file *m, void *v)
287{
288 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700289 struct mem_size_stats mss;
290
291 memset(&mss, 0, sizeof mss);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700292 if (vma->vm_mm)
Mauricio Line070ad42005-09-03 15:55:10 -0700293 smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
Mauricio Line070ad42005-09-03 15:55:10 -0700294 return show_map_internal(m, v, &mss);
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static void *m_start(struct seq_file *m, loff_t *pos)
298{
299 struct task_struct *task = m->private;
300 unsigned long last_addr = m->version;
301 struct mm_struct *mm;
Mauricio Line070ad42005-09-03 15:55:10 -0700302 struct vm_area_struct *vma, *tail_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 loff_t l = *pos;
304
305 /*
306 * We remember last_addr rather than next_addr to hit with
307 * mmap_cache most of the time. We have zero last_addr at
Mauricio Line070ad42005-09-03 15:55:10 -0700308 * the beginning and also after lseek. We will have -1 last_addr
309 * after the end of the vmas.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
311
312 if (last_addr == -1UL)
313 return NULL;
314
315 mm = get_task_mm(task);
316 if (!mm)
317 return NULL;
318
Mauricio Line070ad42005-09-03 15:55:10 -0700319 tail_vma = get_gate_vma(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 down_read(&mm->mmap_sem);
321
322 /* Start with last addr hint */
Mauricio Line070ad42005-09-03 15:55:10 -0700323 if (last_addr && (vma = find_vma(mm, last_addr))) {
324 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out;
326 }
327
328 /*
Mauricio Line070ad42005-09-03 15:55:10 -0700329 * Check the vma index is within the range and do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * sequential scan until m_index.
331 */
Mauricio Line070ad42005-09-03 15:55:10 -0700332 vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if ((unsigned long)l < mm->map_count) {
Mauricio Line070ad42005-09-03 15:55:10 -0700334 vma = mm->mmap;
335 while (l-- && vma)
336 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto out;
338 }
339
340 if (l != mm->map_count)
Mauricio Line070ad42005-09-03 15:55:10 -0700341 tail_vma = NULL; /* After gate vma */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343out:
Mauricio Line070ad42005-09-03 15:55:10 -0700344 if (vma)
345 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Mauricio Line070ad42005-09-03 15:55:10 -0700347 /* End of vmas has been reached */
348 m->version = (tail_vma != NULL)? 0: -1UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 up_read(&mm->mmap_sem);
350 mmput(mm);
Mauricio Line070ad42005-09-03 15:55:10 -0700351 return tail_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354static void m_stop(struct seq_file *m, void *v)
355{
356 struct task_struct *task = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700357 struct vm_area_struct *vma = v;
358 if (vma && vma != get_gate_vma(task)) {
359 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 up_read(&mm->mmap_sem);
361 mmput(mm);
362 }
363}
364
365static void *m_next(struct seq_file *m, void *v, loff_t *pos)
366{
367 struct task_struct *task = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700368 struct vm_area_struct *vma = v;
369 struct vm_area_struct *tail_vma = get_gate_vma(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 (*pos)++;
Mauricio Line070ad42005-09-03 15:55:10 -0700372 if (vma && (vma != tail_vma) && vma->vm_next)
373 return vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 m_stop(m, v);
Mauricio Line070ad42005-09-03 15:55:10 -0700375 return (vma != tail_vma)? tail_vma: NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378struct seq_operations proc_pid_maps_op = {
379 .start = m_start,
380 .next = m_next,
381 .stop = m_stop,
382 .show = show_map
383};
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700384
Mauricio Line070ad42005-09-03 15:55:10 -0700385struct seq_operations proc_pid_smaps_op = {
386 .start = m_start,
387 .next = m_next,
388 .stop = m_stop,
389 .show = show_smap
390};
391
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700392#ifdef CONFIG_NUMA
393
394struct numa_maps {
395 unsigned long pages;
396 unsigned long anon;
397 unsigned long mapped;
398 unsigned long mapcount_max;
399 unsigned long node[MAX_NUMNODES];
400};
401
402/*
403 * Calculate numa node maps for a vma
404 */
Linus Torvalds6aab3412005-11-28 14:34:23 -0800405static struct numa_maps *get_numa_maps(struct vm_area_struct *vma)
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700406{
Linus Torvalds6aab3412005-11-28 14:34:23 -0800407 int i;
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700408 struct page *page;
409 unsigned long vaddr;
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700410 struct numa_maps *md = kmalloc(sizeof(struct numa_maps), GFP_KERNEL);
411
412 if (!md)
413 return NULL;
414 md->pages = 0;
415 md->anon = 0;
416 md->mapped = 0;
417 md->mapcount_max = 0;
418 for_each_node(i)
419 md->node[i] =0;
420
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700421 for (vaddr = vma->vm_start; vaddr < vma->vm_end; vaddr += PAGE_SIZE) {
Linus Torvalds6aab3412005-11-28 14:34:23 -0800422 page = follow_page(vma, vaddr, 0);
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700423 if (page) {
424 int count = page_mapcount(page);
425
426 if (count)
427 md->mapped++;
428 if (count > md->mapcount_max)
429 md->mapcount_max = count;
430 md->pages++;
431 if (PageAnon(page))
432 md->anon++;
433 md->node[page_to_nid(page)]++;
434 }
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -0700435 cond_resched();
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700436 }
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700437 return md;
438}
439
440static int show_numa_map(struct seq_file *m, void *v)
441{
442 struct task_struct *task = m->private;
443 struct vm_area_struct *vma = v;
444 struct mempolicy *pol;
445 struct numa_maps *md;
446 struct zone **z;
447 int n;
448 int first;
449
450 if (!vma->vm_mm)
451 return 0;
452
453 md = get_numa_maps(vma);
454 if (!md)
455 return 0;
456
457 seq_printf(m, "%08lx", vma->vm_start);
458 pol = get_vma_policy(task, vma, vma->vm_start);
459 /* Print policy */
460 switch (pol->policy) {
461 case MPOL_PREFERRED:
462 seq_printf(m, " prefer=%d", pol->v.preferred_node);
463 break;
464 case MPOL_BIND:
465 seq_printf(m, " bind={");
466 first = 1;
467 for (z = pol->v.zonelist->zones; *z; z++) {
468
469 if (!first)
470 seq_putc(m, ',');
471 else
472 first = 0;
473 seq_printf(m, "%d/%s", (*z)->zone_pgdat->node_id,
474 (*z)->name);
475 }
476 seq_putc(m, '}');
477 break;
478 case MPOL_INTERLEAVE:
479 seq_printf(m, " interleave={");
480 first = 1;
481 for_each_node(n) {
Andi Kleendfcd3c02005-10-29 18:15:48 -0700482 if (node_isset(n, pol->v.nodes)) {
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700483 if (!first)
484 seq_putc(m,',');
485 else
486 first = 0;
487 seq_printf(m, "%d",n);
488 }
489 }
490 seq_putc(m, '}');
491 break;
492 default:
493 seq_printf(m," default");
494 break;
495 }
496 seq_printf(m, " MaxRef=%lu Pages=%lu Mapped=%lu",
497 md->mapcount_max, md->pages, md->mapped);
498 if (md->anon)
499 seq_printf(m," Anon=%lu",md->anon);
500
501 for_each_online_node(n) {
502 if (md->node[n])
503 seq_printf(m, " N%d=%lu", n, md->node[n]);
504 }
505 seq_putc(m, '\n');
506 kfree(md);
507 if (m->count < m->size) /* vma is copied successfully */
508 m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0;
509 return 0;
510}
511
512struct seq_operations proc_pid_numa_maps_op = {
513 .start = m_start,
514 .next = m_next,
515 .stop = m_stop,
516 .show = show_numa_map
517};
518#endif