Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | |
2 | #include <linux/mm.h> | |
3 | #include <linux/file.h> | |
eb28062f | 4 | #include <linux/fdtable.h> |
1da177e4 | 5 | #include <linux/mount.h> |
5096add8 | 6 | #include <linux/ptrace.h> |
1da177e4 LT |
7 | #include <linux/seq_file.h> |
8 | #include "internal.h" | |
9 | ||
10 | /* | |
11 | * Logic: we've got two memory sums for each process, "shared", and | |
025dfdaf | 12 | * "non-shared". Shared memory may get counted more than once, for |
1da177e4 LT |
13 | * each process that owns it. Non-shared memory is counted |
14 | * accurately. | |
15 | */ | |
df5f8314 | 16 | void task_mem(struct seq_file *m, struct mm_struct *mm) |
1da177e4 | 17 | { |
8feae131 DH |
18 | struct vm_area_struct *vma; |
19 | struct rb_node *p; | |
1da177e4 LT |
20 | unsigned long bytes = 0, sbytes = 0, slack = 0; |
21 | ||
22 | down_read(&mm->mmap_sem); | |
8feae131 DH |
23 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) { |
24 | vma = rb_entry(p, struct vm_area_struct, vm_rb); | |
1da177e4 | 25 | |
8feae131 | 26 | bytes += kobjsize(vma); |
1da177e4 | 27 | if (atomic_read(&mm->mm_count) > 1 || |
8feae131 DH |
28 | vma->vm_region || |
29 | vma->vm_flags & VM_MAYSHARE) { | |
30 | sbytes += kobjsize((void *) vma->vm_start); | |
31 | if (vma->vm_region) | |
32 | sbytes += kobjsize(vma->vm_region); | |
1da177e4 | 33 | } else { |
8feae131 DH |
34 | bytes += kobjsize((void *) vma->vm_start); |
35 | slack += kobjsize((void *) vma->vm_start) - | |
36 | (vma->vm_end - vma->vm_start); | |
1da177e4 LT |
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(¤t->fs->count) > 1) | |
46 | sbytes += kobjsize(current->fs); | |
47 | else | |
48 | bytes += kobjsize(current->fs); | |
49 | ||
50 | if (current->files && atomic_read(¤t->files->count) > 1) | |
51 | sbytes += kobjsize(current->files); | |
52 | else | |
53 | bytes += kobjsize(current->files); | |
54 | ||
55 | if (current->sighand && atomic_read(¤t->sighand->count) > 1) | |
56 | sbytes += kobjsize(current->sighand); | |
57 | else | |
58 | bytes += kobjsize(current->sighand); | |
59 | ||
60 | bytes += kobjsize(current); /* includes kernel stack */ | |
61 | ||
df5f8314 | 62 | seq_printf(m, |
1da177e4 LT |
63 | "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); | |
1da177e4 LT |
69 | } |
70 | ||
71 | unsigned long task_vsize(struct mm_struct *mm) | |
72 | { | |
8feae131 DH |
73 | struct vm_area_struct *vma; |
74 | struct rb_node *p; | |
1da177e4 LT |
75 | unsigned long vsize = 0; |
76 | ||
77 | down_read(&mm->mmap_sem); | |
8feae131 DH |
78 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) { |
79 | vma = rb_entry(p, struct vm_area_struct, vm_rb); | |
80 | vsize += vma->vm_region->vm_end - vma->vm_region->vm_start; | |
1da177e4 LT |
81 | } |
82 | up_read(&mm->mmap_sem); | |
83 | return vsize; | |
84 | } | |
85 | ||
86 | int task_statm(struct mm_struct *mm, int *shared, int *text, | |
87 | int *data, int *resident) | |
88 | { | |
8feae131 DH |
89 | struct vm_area_struct *vma; |
90 | struct rb_node *p; | |
1da177e4 LT |
91 | int size = kobjsize(mm); |
92 | ||
93 | down_read(&mm->mmap_sem); | |
8feae131 DH |
94 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) { |
95 | vma = rb_entry(p, struct vm_area_struct, vm_rb); | |
96 | size += kobjsize(vma); | |
97 | size += kobjsize((void *) vma->vm_start); | |
1da177e4 LT |
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 | ||
8feae131 DH |
107 | /* |
108 | * display a single VMA to a sequenced file | |
109 | */ | |
110 | static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma) | |
111 | { | |
112 | unsigned long ino = 0; | |
113 | struct file *file; | |
114 | dev_t dev = 0; | |
115 | int flags, len; | |
116 | ||
117 | flags = vma->vm_flags; | |
118 | file = vma->vm_file; | |
119 | ||
120 | if (file) { | |
121 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; | |
122 | dev = inode->i_sb->s_dev; | |
123 | ino = inode->i_ino; | |
124 | } | |
125 | ||
126 | seq_printf(m, | |
127 | "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n", | |
128 | vma->vm_start, | |
129 | vma->vm_end, | |
130 | flags & VM_READ ? 'r' : '-', | |
131 | flags & VM_WRITE ? 'w' : '-', | |
132 | flags & VM_EXEC ? 'x' : '-', | |
133 | flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p', | |
134 | vma->vm_pgoff << PAGE_SHIFT, | |
135 | MAJOR(dev), MINOR(dev), ino, &len); | |
136 | ||
137 | if (file) { | |
138 | len = 25 + sizeof(void *) * 6 - len; | |
139 | if (len < 1) | |
140 | len = 1; | |
141 | seq_printf(m, "%*c", len, ' '); | |
142 | seq_path(m, &file->f_path, ""); | |
143 | } | |
144 | ||
145 | seq_putc(m, '\n'); | |
146 | return 0; | |
147 | } | |
148 | ||
1da177e4 | 149 | /* |
dbf8685c | 150 | * display mapping lines for a particular process's /proc/pid/maps |
1da177e4 | 151 | */ |
8feae131 | 152 | static int show_map(struct seq_file *m, void *_p) |
1da177e4 | 153 | { |
8feae131 | 154 | struct rb_node *p = _p; |
5096add8 | 155 | |
8feae131 | 156 | return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb)); |
1da177e4 | 157 | } |
dbf8685c | 158 | |
1da177e4 LT |
159 | static void *m_start(struct seq_file *m, loff_t *pos) |
160 | { | |
dbf8685c | 161 | struct proc_maps_private *priv = m->private; |
dbf8685c | 162 | struct mm_struct *mm; |
8feae131 | 163 | struct rb_node *p; |
dbf8685c DH |
164 | loff_t n = *pos; |
165 | ||
166 | /* pin the task and mm whilst we play with them */ | |
167 | priv->task = get_pid_task(priv->pid, PIDTYPE_PID); | |
168 | if (!priv->task) | |
169 | return NULL; | |
170 | ||
831830b5 | 171 | mm = mm_for_maps(priv->task); |
dbf8685c DH |
172 | if (!mm) { |
173 | put_task_struct(priv->task); | |
174 | priv->task = NULL; | |
175 | return NULL; | |
176 | } | |
177 | ||
dbf8685c | 178 | /* start from the Nth VMA */ |
8feae131 | 179 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) |
dbf8685c | 180 | if (n-- == 0) |
8feae131 | 181 | return p; |
1da177e4 LT |
182 | return NULL; |
183 | } | |
dbf8685c DH |
184 | |
185 | static void m_stop(struct seq_file *m, void *_vml) | |
1da177e4 | 186 | { |
dbf8685c DH |
187 | struct proc_maps_private *priv = m->private; |
188 | ||
189 | if (priv->task) { | |
190 | struct mm_struct *mm = priv->task->mm; | |
191 | up_read(&mm->mmap_sem); | |
192 | mmput(mm); | |
193 | put_task_struct(priv->task); | |
194 | } | |
1da177e4 | 195 | } |
dbf8685c | 196 | |
8feae131 | 197 | static void *m_next(struct seq_file *m, void *_p, loff_t *pos) |
1da177e4 | 198 | { |
8feae131 | 199 | struct rb_node *p = _p; |
dbf8685c DH |
200 | |
201 | (*pos)++; | |
8feae131 | 202 | return p ? rb_next(p) : NULL; |
1da177e4 | 203 | } |
dbf8685c | 204 | |
03a44825 | 205 | static const struct seq_operations proc_pid_maps_ops = { |
1da177e4 LT |
206 | .start = m_start, |
207 | .next = m_next, | |
208 | .stop = m_stop, | |
209 | .show = show_map | |
210 | }; | |
662795de EB |
211 | |
212 | static int maps_open(struct inode *inode, struct file *file) | |
213 | { | |
dbf8685c DH |
214 | struct proc_maps_private *priv; |
215 | int ret = -ENOMEM; | |
216 | ||
217 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
218 | if (priv) { | |
219 | priv->pid = proc_pid(inode); | |
220 | ret = seq_open(file, &proc_pid_maps_ops); | |
221 | if (!ret) { | |
222 | struct seq_file *m = file->private_data; | |
223 | m->private = priv; | |
224 | } else { | |
225 | kfree(priv); | |
226 | } | |
662795de EB |
227 | } |
228 | return ret; | |
229 | } | |
230 | ||
00977a59 | 231 | const struct file_operations proc_maps_operations = { |
662795de EB |
232 | .open = maps_open, |
233 | .read = seq_read, | |
234 | .llseek = seq_lseek, | |
dbf8685c | 235 | .release = seq_release_private, |
662795de EB |
236 | }; |
237 |