Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / proc / task_mmu.c
1 #include <linux/mm.h>
2 #include <linux/hugetlb.h>
3 #include <linux/mount.h>
4 #include <linux/seq_file.h>
5 #include <linux/highmem.h>
6 #include <linux/pagemap.h>
7 #include <linux/mempolicy.h>
8
9 #include <asm/elf.h>
10 #include <asm/uaccess.h>
11 #include <asm/tlbflush.h>
12 #include "internal.h"
13
14 char *task_mem(struct mm_struct *mm, char *buffer)
15 {
16         unsigned long data, text, lib;
17         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;
32
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,
37                 "VmPeak:\t%8lu kB\n"
38                 "VmSize:\t%8lu kB\n"
39                 "VmLck:\t%8lu kB\n"
40                 "VmHWM:\t%8lu kB\n"
41                 "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"
47                 "StaBrk:\t%08lx kB\n"
48                 "Brk:\t%08lx kB\n"
49                 "StaStk:\t%08lx kB\n"
50                 ,
51                 hiwater_vm << (PAGE_SHIFT-10),
52                 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
53                 mm->locked_vm << (PAGE_SHIFT-10),
54                 hiwater_rss << (PAGE_SHIFT-10),
55                 total_rss << (PAGE_SHIFT-10),
56                 data << (PAGE_SHIFT-10),
57                 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
58                 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
59                 mm->start_brk, mm->brk, mm->start_stack);
60 #ifdef __i386__
61         if (!nx_enabled)
62                 buffer += sprintf(buffer,
63                         "ExecLim:\t%08lx\n", mm->context.exec_limit);
64 #endif
65         return buffer;
66 }
67
68 unsigned long task_vsize(struct mm_struct *mm)
69 {
70         return PAGE_SIZE * mm->total_vm;
71 }
72
73 int task_statm(struct mm_struct *mm, int *shared, int *text,
74                int *data, int *resident)
75 {
76         *shared = get_mm_counter(mm, file_rss);
77         *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
78                                                                 >> PAGE_SHIFT;
79         *data = mm->total_vm - mm->shared_vm;
80         *resident = *shared + get_mm_counter(mm, anon_rss);
81         return mm->total_vm;
82 }
83
84 int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
85 {
86         struct vm_area_struct * vma;
87         int result = -ENOENT;
88         struct task_struct *task = get_proc_task(inode);
89         struct mm_struct * mm = NULL;
90
91         if (task) {
92                 mm = get_task_mm(task);
93                 put_task_struct(task);
94         }
95         if (!mm)
96                 goto out;
97         down_read(&mm->mmap_sem);
98
99         vma = mm->mmap;
100         while (vma) {
101                 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
102                         break;
103                 vma = vma->vm_next;
104         }
105
106         if (vma) {
107                 *mnt = mntget(vma->vm_file->f_vfsmnt);
108                 *dentry = dget(vma->vm_file->f_dentry);
109                 result = 0;
110         }
111
112         up_read(&mm->mmap_sem);
113         mmput(mm);
114 out:
115         return result;
116 }
117
118 static void pad_len_spaces(struct seq_file *m, int len)
119 {
120         len = 25 + sizeof(void*) * 6 - len;
121         if (len < 1)
122                 len = 1;
123         seq_printf(m, "%*c", len, ' ');
124 }
125
126 struct mem_size_stats
127 {
128         unsigned long resident;
129         unsigned long shared_clean;
130         unsigned long shared_dirty;
131         unsigned long private_clean;
132         unsigned long private_dirty;
133 };
134
135 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
136 {
137         return NULL;
138 }
139
140 static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
141 {
142         struct proc_maps_private *priv = m->private;
143         struct task_struct *task = priv->task;
144 #ifdef __i386__
145         struct mm_struct *tmm = get_task_mm(task);
146 #endif
147         struct vm_area_struct *vma = v;
148         struct mm_struct *mm = vma->vm_mm;
149         struct file *file = vma->vm_file;
150         int flags = vma->vm_flags;
151         unsigned long ino = 0;
152         dev_t dev = 0;
153         int len;
154
155         if (file) {
156                 struct inode *inode = vma->vm_file->f_dentry->d_inode;
157                 dev = inode->i_sb->s_dev;
158                 ino = inode->i_ino;
159         }
160
161         seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
162                         vma->vm_start,
163                         vma->vm_end,
164                         flags & VM_READ ? 'r' : '-',
165                         flags & VM_WRITE ? 'w' : '-',
166                         (flags & VM_EXEC
167 #ifdef __i386__
168                                 || (!nx_enabled && tmm &&
169                                 (vma->vm_start < tmm->context.exec_limit))
170 #endif
171                         )
172                                 ? 'x' : '-',
173                         flags & VM_MAYSHARE ? 's' : 'p',
174                         vma->vm_pgoff << PAGE_SHIFT,
175                         MAJOR(dev), MINOR(dev), ino, &len);
176 #ifdef __i386__
177         if (tmm)
178                 mmput(tmm);
179 #endif
180
181         /*
182          * Print the dentry name for named mappings, and a
183          * special [heap] marker for the heap:
184          */
185         if (file) {
186                 pad_len_spaces(m, len);
187                 seq_path(m, file->f_vfsmnt, file->f_dentry, "\n");
188         } else {
189                 const char *name = arch_vma_name(vma);
190                 if (!name) {
191                         if (mm) {
192                                 if (vma->vm_start <= mm->start_brk &&
193                                                 vma->vm_end >= mm->brk) {
194                                         name = "[heap]";
195                                 } else if (vma->vm_start <= mm->start_stack &&
196                                            vma->vm_end >= mm->start_stack) {
197                                         name = "[stack]";
198                                 }
199                         } else {
200                                 name = "[vdso]";
201                         }
202                 }
203                 if (name) {
204                         pad_len_spaces(m, len);
205                         seq_puts(m, name);
206                 }
207         }
208         seq_putc(m, '\n');
209
210         if (mss)
211                 seq_printf(m,
212                            "Size:          %8lu kB\n"
213                            "Rss:           %8lu kB\n"
214                            "Shared_Clean:  %8lu kB\n"
215                            "Shared_Dirty:  %8lu kB\n"
216                            "Private_Clean: %8lu kB\n"
217                            "Private_Dirty: %8lu kB\n",
218                            (vma->vm_end - vma->vm_start) >> 10,
219                            mss->resident >> 10,
220                            mss->shared_clean  >> 10,
221                            mss->shared_dirty  >> 10,
222                            mss->private_clean >> 10,
223                            mss->private_dirty >> 10);
224
225         if (m->count < m->size)  /* vma is copied successfully */
226                 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
227         return 0;
228 }
229
230 static int show_map(struct seq_file *m, void *v)
231 {
232         return show_map_internal(m, v, NULL);
233 }
234
235 static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
236                                 unsigned long addr, unsigned long end,
237                                 struct mem_size_stats *mss)
238 {
239         pte_t *pte, ptent;
240         spinlock_t *ptl;
241         struct page *page;
242
243         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
244         do {
245                 ptent = *pte;
246                 if (!pte_present(ptent))
247                         continue;
248
249                 mss->resident += PAGE_SIZE;
250
251                 page = vm_normal_page(vma, addr, ptent);
252                 if (!page)
253                         continue;
254
255                 if (page_mapcount(page) >= 2) {
256                         if (pte_dirty(ptent))
257                                 mss->shared_dirty += PAGE_SIZE;
258                         else
259                                 mss->shared_clean += PAGE_SIZE;
260                 } else {
261                         if (pte_dirty(ptent))
262                                 mss->private_dirty += PAGE_SIZE;
263                         else
264                                 mss->private_clean += PAGE_SIZE;
265                 }
266         } while (pte++, addr += PAGE_SIZE, addr != end);
267         pte_unmap_unlock(pte - 1, ptl);
268         cond_resched();
269 }
270
271 static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
272                                 unsigned long addr, unsigned long end,
273                                 struct mem_size_stats *mss)
274 {
275         pmd_t *pmd;
276         unsigned long next;
277
278         pmd = pmd_offset(pud, addr);
279         do {
280                 next = pmd_addr_end(addr, end);
281                 if (pmd_none_or_clear_bad(pmd))
282                         continue;
283                 smaps_pte_range(vma, pmd, addr, next, mss);
284         } while (pmd++, addr = next, addr != end);
285 }
286
287 static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
288                                 unsigned long addr, unsigned long end,
289                                 struct mem_size_stats *mss)
290 {
291         pud_t *pud;
292         unsigned long next;
293
294         pud = pud_offset(pgd, addr);
295         do {
296                 next = pud_addr_end(addr, end);
297                 if (pud_none_or_clear_bad(pud))
298                         continue;
299                 smaps_pmd_range(vma, pud, addr, next, mss);
300         } while (pud++, addr = next, addr != end);
301 }
302
303 static inline void smaps_pgd_range(struct vm_area_struct *vma,
304                                 unsigned long addr, unsigned long end,
305                                 struct mem_size_stats *mss)
306 {
307         pgd_t *pgd;
308         unsigned long next;
309
310         pgd = pgd_offset(vma->vm_mm, addr);
311         do {
312                 next = pgd_addr_end(addr, end);
313                 if (pgd_none_or_clear_bad(pgd))
314                         continue;
315                 smaps_pud_range(vma, pgd, addr, next, mss);
316         } while (pgd++, addr = next, addr != end);
317 }
318
319 static int show_smap(struct seq_file *m, void *v)
320 {
321         struct vm_area_struct *vma = v;
322         struct mem_size_stats mss;
323
324         memset(&mss, 0, sizeof mss);
325         if (vma->vm_mm && !is_vm_hugetlb_page(vma))
326                 smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
327         return show_map_internal(m, v, &mss);
328 }
329
330 static void *m_start(struct seq_file *m, loff_t *pos)
331 {
332         struct proc_maps_private *priv = m->private;
333         unsigned long last_addr = m->version;
334         struct mm_struct *mm;
335         struct vm_area_struct *vma, *tail_vma = NULL;
336         loff_t l = *pos;
337
338         /* Clear the per syscall fields in priv */
339         priv->task = NULL;
340         priv->tail_vma = NULL;
341
342         /*
343          * We remember last_addr rather than next_addr to hit with
344          * mmap_cache most of the time. We have zero last_addr at
345          * the beginning and also after lseek. We will have -1 last_addr
346          * after the end of the vmas.
347          */
348
349         if (last_addr == -1UL)
350                 return NULL;
351
352         priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
353         if (!priv->task)
354                 return NULL;
355
356         mm = mm_for_maps(priv->task);
357         if (!mm)
358                 return NULL;
359
360         priv->tail_vma = tail_vma = get_gate_vma(priv->task);
361
362         /* Start with last addr hint */
363         if (last_addr && (vma = find_vma(mm, last_addr))) {
364                 vma = vma->vm_next;
365                 goto out;
366         }
367
368         /*
369          * Check the vma index is within the range and do
370          * sequential scan until m_index.
371          */
372         vma = NULL;
373         if ((unsigned long)l < mm->map_count) {
374                 vma = mm->mmap;
375                 while (l-- && vma)
376                         vma = vma->vm_next;
377                 goto out;
378         }
379
380         if (l != mm->map_count)
381                 tail_vma = NULL; /* After gate vma */
382
383 out:
384         if (vma)
385                 return vma;
386
387         /* End of vmas has been reached */
388         m->version = (tail_vma != NULL)? 0: -1UL;
389         up_read(&mm->mmap_sem);
390         mmput(mm);
391         return tail_vma;
392 }
393
394 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
395 {
396         if (vma && vma != priv->tail_vma) {
397                 struct mm_struct *mm = vma->vm_mm;
398                 up_read(&mm->mmap_sem);
399                 mmput(mm);
400         }
401 }
402
403 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
404 {
405         struct proc_maps_private *priv = m->private;
406         struct vm_area_struct *vma = v;
407         struct vm_area_struct *tail_vma = priv->tail_vma;
408
409         (*pos)++;
410         if (vma && (vma != tail_vma) && vma->vm_next)
411                 return vma->vm_next;
412         vma_stop(priv, vma);
413         return (vma != tail_vma)? tail_vma: NULL;
414 }
415
416 static void m_stop(struct seq_file *m, void *v)
417 {
418         struct proc_maps_private *priv = m->private;
419         struct vm_area_struct *vma = v;
420
421         vma_stop(priv, vma);
422         if (priv->task)
423                 put_task_struct(priv->task);
424 }
425
426 static struct seq_operations proc_pid_maps_op = {
427         .start  = m_start,
428         .next   = m_next,
429         .stop   = m_stop,
430         .show   = show_map
431 };
432
433 static struct seq_operations proc_pid_smaps_op = {
434         .start  = m_start,
435         .next   = m_next,
436         .stop   = m_stop,
437         .show   = show_smap
438 };
439
440 static int do_maps_open(struct inode *inode, struct file *file,
441                         struct seq_operations *ops)
442 {
443         struct proc_maps_private *priv;
444         int ret = -ENOMEM;
445         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
446         if (priv) {
447                 priv->pid = proc_pid(inode);
448                 ret = seq_open(file, ops);
449                 if (!ret) {
450                         struct seq_file *m = file->private_data;
451                         m->private = priv;
452                 } else {
453                         kfree(priv);
454                 }
455         }
456         return ret;
457 }
458
459 static int maps_open(struct inode *inode, struct file *file)
460 {
461         return do_maps_open(inode, file, &proc_pid_maps_op);
462 }
463
464 struct file_operations proc_maps_operations = {
465         .open           = maps_open,
466         .read           = seq_read,
467         .llseek         = seq_lseek,
468         .release        = seq_release_private,
469 };
470
471 #ifdef CONFIG_NUMA
472 extern int show_numa_map(struct seq_file *m, void *v);
473
474 static struct seq_operations proc_pid_numa_maps_op = {
475         .start  = m_start,
476         .next   = m_next,
477         .stop   = m_stop,
478         .show   = show_numa_map
479 };
480
481 static int numa_maps_open(struct inode *inode, struct file *file)
482 {
483         return do_maps_open(inode, file, &proc_pid_numa_maps_op);
484 }
485
486 struct file_operations proc_numa_maps_operations = {
487         .open           = numa_maps_open,
488         .read           = seq_read,
489         .llseek         = seq_lseek,
490         .release        = seq_release_private,
491 };
492 #endif
493
494 static int smaps_open(struct inode *inode, struct file *file)
495 {
496         return do_maps_open(inode, file, &proc_pid_smaps_op);
497 }
498
499 struct file_operations proc_smaps_operations = {
500         .open           = smaps_open,
501         .read           = seq_read,
502         .llseek         = seq_lseek,
503         .release        = seq_release_private,
504 };