Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 2 | #include <linux/mm.h> |
| 3 | #include <linux/file.h> |
Bryan Wu | eb28062 | 2008-05-04 23:12:55 +0800 | [diff] [blame] | 4 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/mount.h> |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 6 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 12 | * "non-shared". Shared memory may get counted more than once, for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * each process that owns it. Non-shared memory is counted |
| 14 | * accurately. |
| 15 | */ |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 16 | void task_mem(struct seq_file *m, struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 18 | struct vm_area_struct *vma; |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 19 | struct vm_region *region; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 20 | struct rb_node *p; |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 21 | unsigned long bytes = 0, sbytes = 0, slack = 0, size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | down_read(&mm->mmap_sem); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 24 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) { |
| 25 | vma = rb_entry(p, struct vm_area_struct, vm_rb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 27 | bytes += kobjsize(vma); |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 28 | |
| 29 | region = vma->vm_region; |
| 30 | if (region) { |
| 31 | size = kobjsize(region); |
| 32 | size += region->vm_end - region->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } else { |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 34 | size = vma->vm_end - vma->vm_start; |
| 35 | } |
| 36 | |
| 37 | if (atomic_read(&mm->mm_count) > 1 || |
| 38 | vma->vm_flags & VM_MAYSHARE) { |
| 39 | sbytes += size; |
| 40 | } else { |
| 41 | bytes += size; |
| 42 | if (region) |
| 43 | slack = region->vm_end - vma->vm_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | if (atomic_read(&mm->mm_count) > 1) |
| 48 | sbytes += kobjsize(mm); |
| 49 | else |
| 50 | bytes += kobjsize(mm); |
| 51 | |
| 52 | if (current->fs && atomic_read(¤t->fs->count) > 1) |
| 53 | sbytes += kobjsize(current->fs); |
| 54 | else |
| 55 | bytes += kobjsize(current->fs); |
| 56 | |
| 57 | if (current->files && atomic_read(¤t->files->count) > 1) |
| 58 | sbytes += kobjsize(current->files); |
| 59 | else |
| 60 | bytes += kobjsize(current->files); |
| 61 | |
| 62 | if (current->sighand && atomic_read(¤t->sighand->count) > 1) |
| 63 | sbytes += kobjsize(current->sighand); |
| 64 | else |
| 65 | bytes += kobjsize(current->sighand); |
| 66 | |
| 67 | bytes += kobjsize(current); /* includes kernel stack */ |
| 68 | |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 69 | seq_printf(m, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | "Mem:\t%8lu bytes\n" |
| 71 | "Slack:\t%8lu bytes\n" |
| 72 | "Shared:\t%8lu bytes\n", |
| 73 | bytes, slack, sbytes); |
| 74 | |
| 75 | up_read(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | unsigned long task_vsize(struct mm_struct *mm) |
| 79 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 80 | struct vm_area_struct *vma; |
| 81 | struct rb_node *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | unsigned long vsize = 0; |
| 83 | |
| 84 | down_read(&mm->mmap_sem); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 85 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) { |
| 86 | vma = rb_entry(p, struct vm_area_struct, vm_rb); |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 87 | vsize += vma->vm_end - vma->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | up_read(&mm->mmap_sem); |
| 90 | return vsize; |
| 91 | } |
| 92 | |
| 93 | int task_statm(struct mm_struct *mm, int *shared, int *text, |
| 94 | int *data, int *resident) |
| 95 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 96 | struct vm_area_struct *vma; |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 97 | struct vm_region *region; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 98 | struct rb_node *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | int size = kobjsize(mm); |
| 100 | |
| 101 | down_read(&mm->mmap_sem); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 102 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) { |
| 103 | vma = rb_entry(p, struct vm_area_struct, vm_rb); |
| 104 | size += kobjsize(vma); |
David Howells | 38f7147 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 105 | region = vma->vm_region; |
| 106 | if (region) { |
| 107 | size += kobjsize(region); |
| 108 | size += region->vm_end - region->vm_start; |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | size += (*text = mm->end_code - mm->start_code); |
| 113 | size += (*data = mm->start_stack - mm->start_data); |
| 114 | up_read(&mm->mmap_sem); |
| 115 | *resident = size; |
| 116 | return size; |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 120 | * display a single VMA to a sequenced file |
| 121 | */ |
| 122 | static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma) |
| 123 | { |
| 124 | unsigned long ino = 0; |
| 125 | struct file *file; |
| 126 | dev_t dev = 0; |
| 127 | int flags, len; |
| 128 | |
| 129 | flags = vma->vm_flags; |
| 130 | file = vma->vm_file; |
| 131 | |
| 132 | if (file) { |
| 133 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 134 | dev = inode->i_sb->s_dev; |
| 135 | ino = inode->i_ino; |
| 136 | } |
| 137 | |
| 138 | seq_printf(m, |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 139 | "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n", |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 140 | vma->vm_start, |
| 141 | vma->vm_end, |
| 142 | flags & VM_READ ? 'r' : '-', |
| 143 | flags & VM_WRITE ? 'w' : '-', |
| 144 | flags & VM_EXEC ? 'x' : '-', |
| 145 | flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p', |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 146 | (unsigned long long) vma->vm_pgoff << PAGE_SHIFT, |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 147 | MAJOR(dev), MINOR(dev), ino, &len); |
| 148 | |
| 149 | if (file) { |
| 150 | len = 25 + sizeof(void *) * 6 - len; |
| 151 | if (len < 1) |
| 152 | len = 1; |
| 153 | seq_printf(m, "%*c", len, ' '); |
| 154 | seq_path(m, &file->f_path, ""); |
| 155 | } |
| 156 | |
| 157 | seq_putc(m, '\n'); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 162 | * display mapping lines for a particular process's /proc/pid/maps |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 164 | static int show_map(struct seq_file *m, void *_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 166 | struct rb_node *p = _p; |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 167 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 168 | return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 172 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 173 | struct proc_maps_private *priv = m->private; |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 174 | struct mm_struct *mm; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 175 | struct rb_node *p; |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 176 | loff_t n = *pos; |
| 177 | |
| 178 | /* pin the task and mm whilst we play with them */ |
| 179 | priv->task = get_pid_task(priv->pid, PIDTYPE_PID); |
| 180 | if (!priv->task) |
| 181 | return NULL; |
| 182 | |
Al Viro | 831830b | 2008-01-02 14:09:57 +0000 | [diff] [blame] | 183 | mm = mm_for_maps(priv->task); |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 184 | if (!mm) { |
| 185 | put_task_struct(priv->task); |
| 186 | priv->task = NULL; |
| 187 | return NULL; |
| 188 | } |
| 189 | |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 190 | /* start from the Nth VMA */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 191 | for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 192 | if (n-- == 0) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 193 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 196 | |
| 197 | static void m_stop(struct seq_file *m, void *_vml) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 199 | struct proc_maps_private *priv = m->private; |
| 200 | |
| 201 | if (priv->task) { |
| 202 | struct mm_struct *mm = priv->task->mm; |
| 203 | up_read(&mm->mmap_sem); |
| 204 | mmput(mm); |
| 205 | put_task_struct(priv->task); |
| 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 208 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 209 | static void *m_next(struct seq_file *m, void *_p, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 211 | struct rb_node *p = _p; |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 212 | |
| 213 | (*pos)++; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 214 | return p ? rb_next(p) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 216 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 217 | static const struct seq_operations proc_pid_maps_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | .start = m_start, |
| 219 | .next = m_next, |
| 220 | .stop = m_stop, |
| 221 | .show = show_map |
| 222 | }; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 223 | |
| 224 | static int maps_open(struct inode *inode, struct file *file) |
| 225 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 226 | struct proc_maps_private *priv; |
| 227 | int ret = -ENOMEM; |
| 228 | |
| 229 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 230 | if (priv) { |
| 231 | priv->pid = proc_pid(inode); |
| 232 | ret = seq_open(file, &proc_pid_maps_ops); |
| 233 | if (!ret) { |
| 234 | struct seq_file *m = file->private_data; |
| 235 | m->private = priv; |
| 236 | } else { |
| 237 | kfree(priv); |
| 238 | } |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 239 | } |
| 240 | return ret; |
| 241 | } |
| 242 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 243 | const struct file_operations proc_maps_operations = { |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 244 | .open = maps_open, |
| 245 | .read = seq_read, |
| 246 | .llseek = seq_lseek, |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 247 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 248 | }; |
| 249 | |