blob: c7ef3e48e35bae1433c18a4736979e0949a59009 [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;
17
18 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
19 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
20 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
21 buffer += sprintf(buffer,
22 "VmSize:\t%8lu kB\n"
23 "VmLck:\t%8lu kB\n"
24 "VmRSS:\t%8lu kB\n"
25 "VmData:\t%8lu kB\n"
26 "VmStk:\t%8lu kB\n"
27 "VmExe:\t%8lu kB\n"
28 "VmLib:\t%8lu kB\n"
29 "VmPTE:\t%8lu kB\n",
30 (mm->total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
31 mm->locked_vm << (PAGE_SHIFT-10),
32 get_mm_counter(mm, rss) << (PAGE_SHIFT-10),
33 data << (PAGE_SHIFT-10),
34 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
35 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
36 return buffer;
37}
38
39unsigned long task_vsize(struct mm_struct *mm)
40{
41 return PAGE_SIZE * mm->total_vm;
42}
43
44int task_statm(struct mm_struct *mm, int *shared, int *text,
45 int *data, int *resident)
46{
47 int rss = get_mm_counter(mm, rss);
48
49 *shared = rss - get_mm_counter(mm, anon_rss);
50 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
51 >> PAGE_SHIFT;
52 *data = mm->total_vm - mm->shared_vm;
53 *resident = rss;
54 return mm->total_vm;
55}
56
57int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
58{
59 struct vm_area_struct * vma;
60 int result = -ENOENT;
61 struct task_struct *task = proc_task(inode);
62 struct mm_struct * mm = get_task_mm(task);
63
64 if (!mm)
65 goto out;
66 down_read(&mm->mmap_sem);
67
68 vma = mm->mmap;
69 while (vma) {
70 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
71 break;
72 vma = vma->vm_next;
73 }
74
75 if (vma) {
76 *mnt = mntget(vma->vm_file->f_vfsmnt);
77 *dentry = dget(vma->vm_file->f_dentry);
78 result = 0;
79 }
80
81 up_read(&mm->mmap_sem);
82 mmput(mm);
83out:
84 return result;
85}
86
87static void pad_len_spaces(struct seq_file *m, int len)
88{
89 len = 25 + sizeof(void*) * 6 - len;
90 if (len < 1)
91 len = 1;
92 seq_printf(m, "%*c", len, ' ');
93}
94
Mauricio Line070ad42005-09-03 15:55:10 -070095struct mem_size_stats
96{
97 unsigned long resident;
98 unsigned long shared_clean;
99 unsigned long shared_dirty;
100 unsigned long private_clean;
101 unsigned long private_dirty;
102};
103
104static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct task_struct *task = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700107 struct vm_area_struct *vma = v;
108 struct mm_struct *mm = vma->vm_mm;
109 struct file *file = vma->vm_file;
110 int flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long ino = 0;
112 dev_t dev = 0;
113 int len;
114
115 if (file) {
Mauricio Line070ad42005-09-03 15:55:10 -0700116 struct inode *inode = vma->vm_file->f_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 dev = inode->i_sb->s_dev;
118 ino = inode->i_ino;
119 }
120
121 seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
Mauricio Line070ad42005-09-03 15:55:10 -0700122 vma->vm_start,
123 vma->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 flags & VM_READ ? 'r' : '-',
125 flags & VM_WRITE ? 'w' : '-',
126 flags & VM_EXEC ? 'x' : '-',
127 flags & VM_MAYSHARE ? 's' : 'p',
Mauricio Line070ad42005-09-03 15:55:10 -0700128 vma->vm_pgoff << PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 MAJOR(dev), MINOR(dev), ino, &len);
130
131 /*
132 * Print the dentry name for named mappings, and a
133 * special [heap] marker for the heap:
134 */
Mauricio Line070ad42005-09-03 15:55:10 -0700135 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 pad_len_spaces(m, len);
Mauricio Line070ad42005-09-03 15:55:10 -0700137 seq_path(m, file->f_vfsmnt, file->f_dentry, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 } else {
139 if (mm) {
Mauricio Line070ad42005-09-03 15:55:10 -0700140 if (vma->vm_start <= mm->start_brk &&
141 vma->vm_end >= mm->brk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pad_len_spaces(m, len);
143 seq_puts(m, "[heap]");
144 } else {
Mauricio Line070ad42005-09-03 15:55:10 -0700145 if (vma->vm_start <= mm->start_stack &&
146 vma->vm_end >= mm->start_stack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 pad_len_spaces(m, len);
149 seq_puts(m, "[stack]");
150 }
151 }
152 } else {
153 pad_len_spaces(m, len);
154 seq_puts(m, "[vdso]");
155 }
156 }
157 seq_putc(m, '\n');
Mauricio Line070ad42005-09-03 15:55:10 -0700158
159 if (mss)
160 seq_printf(m,
161 "Size: %8lu kB\n"
162 "Rss: %8lu kB\n"
163 "Shared_Clean: %8lu kB\n"
164 "Shared_Dirty: %8lu kB\n"
165 "Private_Clean: %8lu kB\n"
166 "Private_Dirty: %8lu kB\n",
167 (vma->vm_end - vma->vm_start) >> 10,
168 mss->resident >> 10,
169 mss->shared_clean >> 10,
170 mss->shared_dirty >> 10,
171 mss->private_clean >> 10,
172 mss->private_dirty >> 10);
173
174 if (m->count < m->size) /* vma is copied successfully */
175 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
Mauricio Line070ad42005-09-03 15:55:10 -0700179static int show_map(struct seq_file *m, void *v)
180{
181 return show_map_internal(m, v, 0);
182}
183
184static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
185 unsigned long addr, unsigned long end,
186 struct mem_size_stats *mss)
187{
188 pte_t *pte, ptent;
189 unsigned long pfn;
190 struct page *page;
191
192 pte = pte_offset_map(pmd, addr);
193 do {
194 ptent = *pte;
195 if (pte_none(ptent) || !pte_present(ptent))
196 continue;
197
198 mss->resident += PAGE_SIZE;
199 pfn = pte_pfn(ptent);
200 if (!pfn_valid(pfn))
201 continue;
202
203 page = pfn_to_page(pfn);
204 if (page_count(page) >= 2) {
205 if (pte_dirty(ptent))
206 mss->shared_dirty += PAGE_SIZE;
207 else
208 mss->shared_clean += PAGE_SIZE;
209 } else {
210 if (pte_dirty(ptent))
211 mss->private_dirty += PAGE_SIZE;
212 else
213 mss->private_clean += PAGE_SIZE;
214 }
215 } while (pte++, addr += PAGE_SIZE, addr != end);
216 pte_unmap(pte - 1);
217 cond_resched_lock(&vma->vm_mm->page_table_lock);
218}
219
220static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
221 unsigned long addr, unsigned long end,
222 struct mem_size_stats *mss)
223{
224 pmd_t *pmd;
225 unsigned long next;
226
227 pmd = pmd_offset(pud, addr);
228 do {
229 next = pmd_addr_end(addr, end);
230 if (pmd_none_or_clear_bad(pmd))
231 continue;
232 smaps_pte_range(vma, pmd, addr, next, mss);
233 } while (pmd++, addr = next, addr != end);
234}
235
236static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
237 unsigned long addr, unsigned long end,
238 struct mem_size_stats *mss)
239{
240 pud_t *pud;
241 unsigned long next;
242
243 pud = pud_offset(pgd, addr);
244 do {
245 next = pud_addr_end(addr, end);
246 if (pud_none_or_clear_bad(pud))
247 continue;
248 smaps_pmd_range(vma, pud, addr, next, mss);
249 } while (pud++, addr = next, addr != end);
250}
251
252static inline void smaps_pgd_range(struct vm_area_struct *vma,
253 unsigned long addr, unsigned long end,
254 struct mem_size_stats *mss)
255{
256 pgd_t *pgd;
257 unsigned long next;
258
259 pgd = pgd_offset(vma->vm_mm, addr);
260 do {
261 next = pgd_addr_end(addr, end);
262 if (pgd_none_or_clear_bad(pgd))
263 continue;
264 smaps_pud_range(vma, pgd, addr, next, mss);
265 } while (pgd++, addr = next, addr != end);
266}
267
268static int show_smap(struct seq_file *m, void *v)
269{
270 struct vm_area_struct *vma = v;
271 struct mm_struct *mm = vma->vm_mm;
272 struct mem_size_stats mss;
273
274 memset(&mss, 0, sizeof mss);
275
276 if (mm) {
277 spin_lock(&mm->page_table_lock);
278 smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
279 spin_unlock(&mm->page_table_lock);
280 }
281
282 return show_map_internal(m, v, &mss);
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static void *m_start(struct seq_file *m, loff_t *pos)
286{
287 struct task_struct *task = m->private;
288 unsigned long last_addr = m->version;
289 struct mm_struct *mm;
Mauricio Line070ad42005-09-03 15:55:10 -0700290 struct vm_area_struct *vma, *tail_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 loff_t l = *pos;
292
293 /*
294 * We remember last_addr rather than next_addr to hit with
295 * mmap_cache most of the time. We have zero last_addr at
Mauricio Line070ad42005-09-03 15:55:10 -0700296 * the beginning and also after lseek. We will have -1 last_addr
297 * after the end of the vmas.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299
300 if (last_addr == -1UL)
301 return NULL;
302
303 mm = get_task_mm(task);
304 if (!mm)
305 return NULL;
306
Mauricio Line070ad42005-09-03 15:55:10 -0700307 tail_vma = get_gate_vma(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 down_read(&mm->mmap_sem);
309
310 /* Start with last addr hint */
Mauricio Line070ad42005-09-03 15:55:10 -0700311 if (last_addr && (vma = find_vma(mm, last_addr))) {
312 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto out;
314 }
315
316 /*
Mauricio Line070ad42005-09-03 15:55:10 -0700317 * Check the vma index is within the range and do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * sequential scan until m_index.
319 */
Mauricio Line070ad42005-09-03 15:55:10 -0700320 vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if ((unsigned long)l < mm->map_count) {
Mauricio Line070ad42005-09-03 15:55:10 -0700322 vma = mm->mmap;
323 while (l-- && vma)
324 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out;
326 }
327
328 if (l != mm->map_count)
Mauricio Line070ad42005-09-03 15:55:10 -0700329 tail_vma = NULL; /* After gate vma */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331out:
Mauricio Line070ad42005-09-03 15:55:10 -0700332 if (vma)
333 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Mauricio Line070ad42005-09-03 15:55:10 -0700335 /* End of vmas has been reached */
336 m->version = (tail_vma != NULL)? 0: -1UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 up_read(&mm->mmap_sem);
338 mmput(mm);
Mauricio Line070ad42005-09-03 15:55:10 -0700339 return tail_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static void m_stop(struct seq_file *m, void *v)
343{
344 struct task_struct *task = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700345 struct vm_area_struct *vma = v;
346 if (vma && vma != get_gate_vma(task)) {
347 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 up_read(&mm->mmap_sem);
349 mmput(mm);
350 }
351}
352
353static void *m_next(struct seq_file *m, void *v, loff_t *pos)
354{
355 struct task_struct *task = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700356 struct vm_area_struct *vma = v;
357 struct vm_area_struct *tail_vma = get_gate_vma(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 (*pos)++;
Mauricio Line070ad42005-09-03 15:55:10 -0700360 if (vma && (vma != tail_vma) && vma->vm_next)
361 return vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 m_stop(m, v);
Mauricio Line070ad42005-09-03 15:55:10 -0700363 return (vma != tail_vma)? tail_vma: NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366struct seq_operations proc_pid_maps_op = {
367 .start = m_start,
368 .next = m_next,
369 .stop = m_stop,
370 .show = show_map
371};
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700372
Mauricio Line070ad42005-09-03 15:55:10 -0700373struct seq_operations proc_pid_smaps_op = {
374 .start = m_start,
375 .next = m_next,
376 .stop = m_stop,
377 .show = show_smap
378};
379
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700380#ifdef CONFIG_NUMA
381
382struct numa_maps {
383 unsigned long pages;
384 unsigned long anon;
385 unsigned long mapped;
386 unsigned long mapcount_max;
387 unsigned long node[MAX_NUMNODES];
388};
389
390/*
391 * Calculate numa node maps for a vma
392 */
393static struct numa_maps *get_numa_maps(const struct vm_area_struct *vma)
394{
395 struct page *page;
396 unsigned long vaddr;
397 struct mm_struct *mm = vma->vm_mm;
398 int i;
399 struct numa_maps *md = kmalloc(sizeof(struct numa_maps), GFP_KERNEL);
400
401 if (!md)
402 return NULL;
403 md->pages = 0;
404 md->anon = 0;
405 md->mapped = 0;
406 md->mapcount_max = 0;
407 for_each_node(i)
408 md->node[i] =0;
409
410 spin_lock(&mm->page_table_lock);
411 for (vaddr = vma->vm_start; vaddr < vma->vm_end; vaddr += PAGE_SIZE) {
412 page = follow_page(mm, vaddr, 0);
413 if (page) {
414 int count = page_mapcount(page);
415
416 if (count)
417 md->mapped++;
418 if (count > md->mapcount_max)
419 md->mapcount_max = count;
420 md->pages++;
421 if (PageAnon(page))
422 md->anon++;
423 md->node[page_to_nid(page)]++;
424 }
425 }
426 spin_unlock(&mm->page_table_lock);
427 return md;
428}
429
430static int show_numa_map(struct seq_file *m, void *v)
431{
432 struct task_struct *task = m->private;
433 struct vm_area_struct *vma = v;
434 struct mempolicy *pol;
435 struct numa_maps *md;
436 struct zone **z;
437 int n;
438 int first;
439
440 if (!vma->vm_mm)
441 return 0;
442
443 md = get_numa_maps(vma);
444 if (!md)
445 return 0;
446
447 seq_printf(m, "%08lx", vma->vm_start);
448 pol = get_vma_policy(task, vma, vma->vm_start);
449 /* Print policy */
450 switch (pol->policy) {
451 case MPOL_PREFERRED:
452 seq_printf(m, " prefer=%d", pol->v.preferred_node);
453 break;
454 case MPOL_BIND:
455 seq_printf(m, " bind={");
456 first = 1;
457 for (z = pol->v.zonelist->zones; *z; z++) {
458
459 if (!first)
460 seq_putc(m, ',');
461 else
462 first = 0;
463 seq_printf(m, "%d/%s", (*z)->zone_pgdat->node_id,
464 (*z)->name);
465 }
466 seq_putc(m, '}');
467 break;
468 case MPOL_INTERLEAVE:
469 seq_printf(m, " interleave={");
470 first = 1;
471 for_each_node(n) {
472 if (test_bit(n, pol->v.nodes)) {
473 if (!first)
474 seq_putc(m,',');
475 else
476 first = 0;
477 seq_printf(m, "%d",n);
478 }
479 }
480 seq_putc(m, '}');
481 break;
482 default:
483 seq_printf(m," default");
484 break;
485 }
486 seq_printf(m, " MaxRef=%lu Pages=%lu Mapped=%lu",
487 md->mapcount_max, md->pages, md->mapped);
488 if (md->anon)
489 seq_printf(m," Anon=%lu",md->anon);
490
491 for_each_online_node(n) {
492 if (md->node[n])
493 seq_printf(m, " N%d=%lu", n, md->node[n]);
494 }
495 seq_putc(m, '\n');
496 kfree(md);
497 if (m->count < m->size) /* vma is copied successfully */
498 m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0;
499 return 0;
500}
501
502struct seq_operations proc_pid_numa_maps_op = {
503 .start = m_start,
504 .next = m_next,
505 .stop = m_stop,
506 .show = show_numa_map
507};
508#endif