fcdbd233f25233670a23acf1c1a9781740dec5fa
[safe/jmp/linux-2.6] / 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/ptrace.h>
7 #include <linux/pagemap.h>
8 #include <linux/mempolicy.h>
9
10 #include <asm/elf.h>
11 #include <asm/uaccess.h>
12 #include <asm/tlbflush.h>
13 #include "internal.h"
14
15 char *task_mem(struct mm_struct *mm, char *buffer)
16 {
17         unsigned long data, text, lib;
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;
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,
38                 "VmPeak:\t%8lu kB\n"
39                 "VmSize:\t%8lu kB\n"
40                 "VmLck:\t%8lu kB\n"
41                 "VmHWM:\t%8lu kB\n"
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",
48                 hiwater_vm << (PAGE_SHIFT-10),
49                 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
50                 mm->locked_vm << (PAGE_SHIFT-10),
51                 hiwater_rss << (PAGE_SHIFT-10),
52                 total_rss << (PAGE_SHIFT-10),
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
59 unsigned long task_vsize(struct mm_struct *mm)
60 {
61         return PAGE_SIZE * mm->total_vm;
62 }
63
64 int task_statm(struct mm_struct *mm, int *shared, int *text,
65                int *data, int *resident)
66 {
67         *shared = get_mm_counter(mm, file_rss);
68         *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
69                                                                 >> PAGE_SHIFT;
70         *data = mm->total_vm - mm->shared_vm;
71         *resident = *shared + get_mm_counter(mm, anon_rss);
72         return mm->total_vm;
73 }
74
75 int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
76 {
77         struct vm_area_struct * vma;
78         int result = -ENOENT;
79         struct task_struct *task = get_proc_task(inode);
80         struct mm_struct * mm = NULL;
81
82         if (task) {
83                 mm = get_task_mm(task);
84                 put_task_struct(task);
85         }
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) {
98                 *mnt = mntget(vma->vm_file->f_path.mnt);
99                 *dentry = dget(vma->vm_file->f_path.dentry);
100                 result = 0;
101         }
102
103         up_read(&mm->mmap_sem);
104         mmput(mm);
105 out:
106         return result;
107 }
108
109 static 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
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
136 struct mem_size_stats
137 {
138         struct vm_area_struct *vma;
139         unsigned long resident;
140         unsigned long shared_clean;
141         unsigned long shared_dirty;
142         unsigned long private_clean;
143         unsigned long private_dirty;
144         unsigned long referenced;
145         u64 pss;
146 };
147
148 static int show_map(struct seq_file *m, void *v)
149 {
150         struct proc_maps_private *priv = m->private;
151         struct task_struct *task = priv->task;
152         struct vm_area_struct *vma = v;
153         struct mm_struct *mm = vma->vm_mm;
154         struct file *file = vma->vm_file;
155         int flags = vma->vm_flags;
156         unsigned long ino = 0;
157         dev_t dev = 0;
158         int len;
159
160         if (maps_protect && !ptrace_may_attach(task))
161                 return -EACCES;
162
163         if (file) {
164                 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
165                 dev = inode->i_sb->s_dev;
166                 ino = inode->i_ino;
167         }
168
169         seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
170                         vma->vm_start,
171                         vma->vm_end,
172                         flags & VM_READ ? 'r' : '-',
173                         flags & VM_WRITE ? 'w' : '-',
174                         flags & VM_EXEC ? 'x' : '-',
175                         flags & VM_MAYSHARE ? 's' : 'p',
176                         vma->vm_pgoff << PAGE_SHIFT,
177                         MAJOR(dev), MINOR(dev), ino, &len);
178
179         /*
180          * Print the dentry name for named mappings, and a
181          * special [heap] marker for the heap:
182          */
183         if (file) {
184                 pad_len_spaces(m, len);
185                 seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n");
186         } else {
187                 const char *name = arch_vma_name(vma);
188                 if (!name) {
189                         if (mm) {
190                                 if (vma->vm_start <= mm->start_brk &&
191                                                 vma->vm_end >= mm->brk) {
192                                         name = "[heap]";
193                                 } else if (vma->vm_start <= mm->start_stack &&
194                                            vma->vm_end >= mm->start_stack) {
195                                         name = "[stack]";
196                                 }
197                         } else {
198                                 name = "[vdso]";
199                         }
200                 }
201                 if (name) {
202                         pad_len_spaces(m, len);
203                         seq_puts(m, name);
204                 }
205         }
206         seq_putc(m, '\n');
207
208         if (m->count < m->size)  /* vma is copied successfully */
209                 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
210         return 0;
211 }
212
213 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
214                            void *private)
215 {
216         struct mem_size_stats *mss = private;
217         struct vm_area_struct *vma = mss->vma;
218         pte_t *pte, ptent;
219         spinlock_t *ptl;
220         struct page *page;
221         int mapcount;
222
223         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
224         for (; addr != end; pte++, addr += PAGE_SIZE) {
225                 ptent = *pte;
226                 if (!pte_present(ptent))
227                         continue;
228
229                 mss->resident += PAGE_SIZE;
230
231                 page = vm_normal_page(vma, addr, ptent);
232                 if (!page)
233                         continue;
234
235                 /* Accumulate the size in pages that have been accessed. */
236                 if (pte_young(ptent) || PageReferenced(page))
237                         mss->referenced += PAGE_SIZE;
238                 mapcount = page_mapcount(page);
239                 if (mapcount >= 2) {
240                         if (pte_dirty(ptent))
241                                 mss->shared_dirty += PAGE_SIZE;
242                         else
243                                 mss->shared_clean += PAGE_SIZE;
244                         mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
245                 } else {
246                         if (pte_dirty(ptent))
247                                 mss->private_dirty += PAGE_SIZE;
248                         else
249                                 mss->private_clean += PAGE_SIZE;
250                         mss->pss += (PAGE_SIZE << PSS_SHIFT);
251                 }
252         }
253         pte_unmap_unlock(pte - 1, ptl);
254         cond_resched();
255         return 0;
256 }
257
258 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
259                                 unsigned long end, void *private)
260 {
261         struct vm_area_struct *vma = private;
262         pte_t *pte, ptent;
263         spinlock_t *ptl;
264         struct page *page;
265
266         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
267         for (; addr != end; pte++, addr += PAGE_SIZE) {
268                 ptent = *pte;
269                 if (!pte_present(ptent))
270                         continue;
271
272                 page = vm_normal_page(vma, addr, ptent);
273                 if (!page)
274                         continue;
275
276                 /* Clear accessed and referenced bits. */
277                 ptep_test_and_clear_young(vma, addr, pte);
278                 ClearPageReferenced(page);
279         }
280         pte_unmap_unlock(pte - 1, ptl);
281         cond_resched();
282         return 0;
283 }
284
285 static struct mm_walk smaps_walk = { .pmd_entry = smaps_pte_range };
286
287 static int show_smap(struct seq_file *m, void *v)
288 {
289         struct vm_area_struct *vma = v;
290         struct mem_size_stats mss;
291         int ret;
292
293         memset(&mss, 0, sizeof mss);
294         mss.vma = vma;
295         if (vma->vm_mm && !is_vm_hugetlb_page(vma))
296                 walk_page_range(vma->vm_mm, vma->vm_start, vma->vm_end,
297                                 &smaps_walk, &mss);
298
299         ret = show_map(m, v);
300         if (ret)
301                 return ret;
302
303         seq_printf(m,
304                    "Size:           %8lu kB\n"
305                    "Rss:            %8lu kB\n"
306                    "Pss:            %8lu kB\n"
307                    "Shared_Clean:   %8lu kB\n"
308                    "Shared_Dirty:   %8lu kB\n"
309                    "Private_Clean:  %8lu kB\n"
310                    "Private_Dirty:  %8lu kB\n"
311                    "Referenced:     %8lu kB\n",
312                    (vma->vm_end - vma->vm_start) >> 10,
313                    mss.resident >> 10,
314                    (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
315                    mss.shared_clean  >> 10,
316                    mss.shared_dirty  >> 10,
317                    mss.private_clean >> 10,
318                    mss.private_dirty >> 10,
319                    mss.referenced >> 10);
320
321         return ret;
322 }
323
324 static struct mm_walk clear_refs_walk = { .pmd_entry = clear_refs_pte_range };
325
326 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
327                                 size_t count, loff_t *ppos)
328 {
329         struct task_struct *task;
330         char buffer[PROC_NUMBUF], *end;
331         struct mm_struct *mm;
332         struct vm_area_struct *vma;
333
334         memset(buffer, 0, sizeof(buffer));
335         if (count > sizeof(buffer) - 1)
336                 count = sizeof(buffer) - 1;
337         if (copy_from_user(buffer, buf, count))
338                 return -EFAULT;
339         if (!simple_strtol(buffer, &end, 0))
340                 return -EINVAL;
341         if (*end == '\n')
342                 end++;
343         task = get_proc_task(file->f_path.dentry->d_inode);
344         if (!task)
345                 return -ESRCH;
346         mm = get_task_mm(task);
347         if (mm) {
348                 down_read(&mm->mmap_sem);
349                 for (vma = mm->mmap; vma; vma = vma->vm_next)
350                         if (!is_vm_hugetlb_page(vma))
351                                 walk_page_range(mm, vma->vm_start, vma->vm_end,
352                                                 &clear_refs_walk, vma);
353                 flush_tlb_mm(mm);
354                 up_read(&mm->mmap_sem);
355                 mmput(mm);
356         }
357         put_task_struct(task);
358         if (end - buffer == 0)
359                 return -EIO;
360         return end - buffer;
361 }
362
363 const struct file_operations proc_clear_refs_operations = {
364         .write          = clear_refs_write,
365 };
366
367 static void *m_start(struct seq_file *m, loff_t *pos)
368 {
369         struct proc_maps_private *priv = m->private;
370         unsigned long last_addr = m->version;
371         struct mm_struct *mm;
372         struct vm_area_struct *vma, *tail_vma = NULL;
373         loff_t l = *pos;
374
375         /* Clear the per syscall fields in priv */
376         priv->task = NULL;
377         priv->tail_vma = NULL;
378
379         /*
380          * We remember last_addr rather than next_addr to hit with
381          * mmap_cache most of the time. We have zero last_addr at
382          * the beginning and also after lseek. We will have -1 last_addr
383          * after the end of the vmas.
384          */
385
386         if (last_addr == -1UL)
387                 return NULL;
388
389         priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
390         if (!priv->task)
391                 return NULL;
392
393         mm = mm_for_maps(priv->task);
394         if (!mm)
395                 return NULL;
396
397         priv->tail_vma = tail_vma = get_gate_vma(priv->task);
398
399         /* Start with last addr hint */
400         if (last_addr && (vma = find_vma(mm, last_addr))) {
401                 vma = vma->vm_next;
402                 goto out;
403         }
404
405         /*
406          * Check the vma index is within the range and do
407          * sequential scan until m_index.
408          */
409         vma = NULL;
410         if ((unsigned long)l < mm->map_count) {
411                 vma = mm->mmap;
412                 while (l-- && vma)
413                         vma = vma->vm_next;
414                 goto out;
415         }
416
417         if (l != mm->map_count)
418                 tail_vma = NULL; /* After gate vma */
419
420 out:
421         if (vma)
422                 return vma;
423
424         /* End of vmas has been reached */
425         m->version = (tail_vma != NULL)? 0: -1UL;
426         up_read(&mm->mmap_sem);
427         mmput(mm);
428         return tail_vma;
429 }
430
431 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
432 {
433         if (vma && vma != priv->tail_vma) {
434                 struct mm_struct *mm = vma->vm_mm;
435                 up_read(&mm->mmap_sem);
436                 mmput(mm);
437         }
438 }
439
440 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
441 {
442         struct proc_maps_private *priv = m->private;
443         struct vm_area_struct *vma = v;
444         struct vm_area_struct *tail_vma = priv->tail_vma;
445
446         (*pos)++;
447         if (vma && (vma != tail_vma) && vma->vm_next)
448                 return vma->vm_next;
449         vma_stop(priv, vma);
450         return (vma != tail_vma)? tail_vma: NULL;
451 }
452
453 static void m_stop(struct seq_file *m, void *v)
454 {
455         struct proc_maps_private *priv = m->private;
456         struct vm_area_struct *vma = v;
457
458         vma_stop(priv, vma);
459         if (priv->task)
460                 put_task_struct(priv->task);
461 }
462
463 static struct seq_operations proc_pid_maps_op = {
464         .start  = m_start,
465         .next   = m_next,
466         .stop   = m_stop,
467         .show   = show_map
468 };
469
470 static struct seq_operations proc_pid_smaps_op = {
471         .start  = m_start,
472         .next   = m_next,
473         .stop   = m_stop,
474         .show   = show_smap
475 };
476
477 static int do_maps_open(struct inode *inode, struct file *file,
478                         struct seq_operations *ops)
479 {
480         struct proc_maps_private *priv;
481         int ret = -ENOMEM;
482         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
483         if (priv) {
484                 priv->pid = proc_pid(inode);
485                 ret = seq_open(file, ops);
486                 if (!ret) {
487                         struct seq_file *m = file->private_data;
488                         m->private = priv;
489                 } else {
490                         kfree(priv);
491                 }
492         }
493         return ret;
494 }
495
496 static int maps_open(struct inode *inode, struct file *file)
497 {
498         return do_maps_open(inode, file, &proc_pid_maps_op);
499 }
500
501 const struct file_operations proc_maps_operations = {
502         .open           = maps_open,
503         .read           = seq_read,
504         .llseek         = seq_lseek,
505         .release        = seq_release_private,
506 };
507
508 #ifdef CONFIG_NUMA
509 extern int show_numa_map(struct seq_file *m, void *v);
510
511 static int show_numa_map_checked(struct seq_file *m, void *v)
512 {
513         struct proc_maps_private *priv = m->private;
514         struct task_struct *task = priv->task;
515
516         if (maps_protect && !ptrace_may_attach(task))
517                 return -EACCES;
518
519         return show_numa_map(m, v);
520 }
521
522 static struct seq_operations proc_pid_numa_maps_op = {
523         .start  = m_start,
524         .next   = m_next,
525         .stop   = m_stop,
526         .show   = show_numa_map_checked
527 };
528
529 static int numa_maps_open(struct inode *inode, struct file *file)
530 {
531         return do_maps_open(inode, file, &proc_pid_numa_maps_op);
532 }
533
534 const struct file_operations proc_numa_maps_operations = {
535         .open           = numa_maps_open,
536         .read           = seq_read,
537         .llseek         = seq_lseek,
538         .release        = seq_release_private,
539 };
540 #endif
541
542 static int smaps_open(struct inode *inode, struct file *file)
543 {
544         return do_maps_open(inode, file, &proc_pid_smaps_op);
545 }
546
547 const struct file_operations proc_smaps_operations = {
548         .open           = smaps_open,
549         .read           = seq_read,
550         .llseek         = seq_lseek,
551         .release        = seq_release_private,
552 };