blob: 5d84e7121df819cd0d9eb8b9bd54973edf5b77ed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2#include <linux/mm.h>
3#include <linux/file.h>
Bryan Wueb280622008-05-04 23:12:55 +08004#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/mount.h>
Kees Cook5096add2007-05-08 00:26:04 -07006#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/seq_file.h>
8#include "internal.h"
9
10/*
11 * Logic: we've got two memory sums for each process, "shared", and
12 * "non-shared". Shared memory may get counted more then once, for
13 * each process that owns it. Non-shared memory is counted
14 * accurately.
15 */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080016void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct vm_list_struct *vml;
19 unsigned long bytes = 0, sbytes = 0, slack = 0;
20
21 down_read(&mm->mmap_sem);
22 for (vml = mm->context.vmlist; vml; vml = vml->next) {
23 if (!vml->vma)
24 continue;
25
26 bytes += kobjsize(vml);
27 if (atomic_read(&mm->mm_count) > 1 ||
28 atomic_read(&vml->vma->vm_usage) > 1
29 ) {
30 sbytes += kobjsize((void *) vml->vma->vm_start);
31 sbytes += kobjsize(vml->vma);
32 } else {
33 bytes += kobjsize((void *) vml->vma->vm_start);
34 bytes += kobjsize(vml->vma);
35 slack += kobjsize((void *) vml->vma->vm_start) -
36 (vml->vma->vm_end - vml->vma->vm_start);
37 }
38 }
39
40 if (atomic_read(&mm->mm_count) > 1)
41 sbytes += kobjsize(mm);
42 else
43 bytes += kobjsize(mm);
44
45 if (current->fs && atomic_read(&current->fs->count) > 1)
46 sbytes += kobjsize(current->fs);
47 else
48 bytes += kobjsize(current->fs);
49
50 if (current->files && atomic_read(&current->files->count) > 1)
51 sbytes += kobjsize(current->files);
52 else
53 bytes += kobjsize(current->files);
54
55 if (current->sighand && atomic_read(&current->sighand->count) > 1)
56 sbytes += kobjsize(current->sighand);
57 else
58 bytes += kobjsize(current->sighand);
59
60 bytes += kobjsize(current); /* includes kernel stack */
61
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080062 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 "Mem:\t%8lu bytes\n"
64 "Slack:\t%8lu bytes\n"
65 "Shared:\t%8lu bytes\n",
66 bytes, slack, sbytes);
67
68 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71unsigned long task_vsize(struct mm_struct *mm)
72{
73 struct vm_list_struct *tbp;
74 unsigned long vsize = 0;
75
76 down_read(&mm->mmap_sem);
77 for (tbp = mm->context.vmlist; tbp; tbp = tbp->next) {
78 if (tbp->vma)
79 vsize += kobjsize((void *) tbp->vma->vm_start);
80 }
81 up_read(&mm->mmap_sem);
82 return vsize;
83}
84
85int task_statm(struct mm_struct *mm, int *shared, int *text,
86 int *data, int *resident)
87{
88 struct vm_list_struct *tbp;
89 int size = kobjsize(mm);
90
91 down_read(&mm->mmap_sem);
92 for (tbp = mm->context.vmlist; tbp; tbp = tbp->next) {
93 size += kobjsize(tbp);
94 if (tbp->vma) {
95 size += kobjsize(tbp->vma);
96 size += kobjsize((void *) tbp->vma->vm_start);
97 }
98 }
99
100 size += (*text = mm->end_code - mm->start_code);
101 size += (*data = mm->start_stack - mm->start_data);
102 up_read(&mm->mmap_sem);
103 *resident = size;
104 return size;
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
David Howellsdbf86852006-09-27 01:50:19 -0700108 * display mapping lines for a particular process's /proc/pid/maps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
David Howellsdbf86852006-09-27 01:50:19 -0700110static int show_map(struct seq_file *m, void *_vml)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
David Howellsdbf86852006-09-27 01:50:19 -0700112 struct vm_list_struct *vml = _vml;
Kees Cook5096add2007-05-08 00:26:04 -0700113 struct proc_maps_private *priv = m->private;
114 struct task_struct *task = priv->task;
115
Stephen Smalley006ebb42008-05-19 08:32:49 -0400116 if (maps_protect && !ptrace_may_access(task, PTRACE_MODE_READ))
Kees Cook5096add2007-05-08 00:26:04 -0700117 return -EACCES;
118
David Howellsdbf86852006-09-27 01:50:19 -0700119 return nommu_vma_show(m, vml->vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
David Howellsdbf86852006-09-27 01:50:19 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void *m_start(struct seq_file *m, loff_t *pos)
123{
David Howellsdbf86852006-09-27 01:50:19 -0700124 struct proc_maps_private *priv = m->private;
125 struct vm_list_struct *vml;
126 struct mm_struct *mm;
127 loff_t n = *pos;
128
129 /* pin the task and mm whilst we play with them */
130 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
131 if (!priv->task)
132 return NULL;
133
Al Viro831830b2008-01-02 14:09:57 +0000134 mm = mm_for_maps(priv->task);
David Howellsdbf86852006-09-27 01:50:19 -0700135 if (!mm) {
136 put_task_struct(priv->task);
137 priv->task = NULL;
138 return NULL;
139 }
140
David Howellsdbf86852006-09-27 01:50:19 -0700141 /* start from the Nth VMA */
142 for (vml = mm->context.vmlist; vml; vml = vml->next)
143 if (n-- == 0)
144 return vml;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return NULL;
146}
David Howellsdbf86852006-09-27 01:50:19 -0700147
148static void m_stop(struct seq_file *m, void *_vml)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
David Howellsdbf86852006-09-27 01:50:19 -0700150 struct proc_maps_private *priv = m->private;
151
152 if (priv->task) {
153 struct mm_struct *mm = priv->task->mm;
154 up_read(&mm->mmap_sem);
155 mmput(mm);
156 put_task_struct(priv->task);
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
David Howellsdbf86852006-09-27 01:50:19 -0700159
160static void *m_next(struct seq_file *m, void *_vml, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David Howellsdbf86852006-09-27 01:50:19 -0700162 struct vm_list_struct *vml = _vml;
163
164 (*pos)++;
165 return vml ? vml->next : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
David Howellsdbf86852006-09-27 01:50:19 -0700167
Jan Engelhardt03a44822008-02-08 04:21:19 -0800168static const struct seq_operations proc_pid_maps_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .start = m_start,
170 .next = m_next,
171 .stop = m_stop,
172 .show = show_map
173};
Eric W. Biederman662795d2006-06-26 00:25:48 -0700174
175static int maps_open(struct inode *inode, struct file *file)
176{
David Howellsdbf86852006-09-27 01:50:19 -0700177 struct proc_maps_private *priv;
178 int ret = -ENOMEM;
179
180 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
181 if (priv) {
182 priv->pid = proc_pid(inode);
183 ret = seq_open(file, &proc_pid_maps_ops);
184 if (!ret) {
185 struct seq_file *m = file->private_data;
186 m->private = priv;
187 } else {
188 kfree(priv);
189 }
Eric W. Biederman662795d2006-06-26 00:25:48 -0700190 }
191 return ret;
192}
193
Arjan van de Ven00977a52007-02-12 00:55:34 -0800194const struct file_operations proc_maps_operations = {
Eric W. Biederman662795d2006-06-26 00:25:48 -0700195 .open = maps_open,
196 .read = seq_read,
197 .llseek = seq_lseek,
David Howellsdbf86852006-09-27 01:50:19 -0700198 .release = seq_release_private,
Eric W. Biederman662795d2006-06-26 00:25:48 -0700199};
200