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 |
| 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. 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 | { |
| 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(¤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 | |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 62 | seq_printf(m, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | unsigned 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 | |
| 85 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 108 | * display mapping lines for a particular process's /proc/pid/maps |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | */ |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 110 | static int show_map(struct seq_file *m, void *_vml) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 112 | struct vm_list_struct *vml = _vml; |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 113 | |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 114 | return nommu_vma_show(m, vml->vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 118 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 119 | struct proc_maps_private *priv = m->private; |
| 120 | struct vm_list_struct *vml; |
| 121 | struct mm_struct *mm; |
| 122 | loff_t n = *pos; |
| 123 | |
| 124 | /* pin the task and mm whilst we play with them */ |
| 125 | priv->task = get_pid_task(priv->pid, PIDTYPE_PID); |
| 126 | if (!priv->task) |
| 127 | return NULL; |
| 128 | |
Al Viro | 831830b | 2008-01-02 14:09:57 +0000 | [diff] [blame] | 129 | mm = mm_for_maps(priv->task); |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 130 | if (!mm) { |
| 131 | put_task_struct(priv->task); |
| 132 | priv->task = NULL; |
| 133 | return NULL; |
| 134 | } |
| 135 | |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 136 | /* start from the Nth VMA */ |
| 137 | for (vml = mm->context.vmlist; vml; vml = vml->next) |
| 138 | if (n-- == 0) |
| 139 | return vml; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 142 | |
| 143 | static void m_stop(struct seq_file *m, void *_vml) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 145 | struct proc_maps_private *priv = m->private; |
| 146 | |
| 147 | if (priv->task) { |
| 148 | struct mm_struct *mm = priv->task->mm; |
| 149 | up_read(&mm->mmap_sem); |
| 150 | mmput(mm); |
| 151 | put_task_struct(priv->task); |
| 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 154 | |
| 155 | static void *m_next(struct seq_file *m, void *_vml, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 157 | struct vm_list_struct *vml = _vml; |
| 158 | |
| 159 | (*pos)++; |
| 160 | return vml ? vml->next : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 162 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 163 | static const struct seq_operations proc_pid_maps_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | .start = m_start, |
| 165 | .next = m_next, |
| 166 | .stop = m_stop, |
| 167 | .show = show_map |
| 168 | }; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 169 | |
| 170 | static int maps_open(struct inode *inode, struct file *file) |
| 171 | { |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 172 | struct proc_maps_private *priv; |
| 173 | int ret = -ENOMEM; |
| 174 | |
| 175 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 176 | if (priv) { |
| 177 | priv->pid = proc_pid(inode); |
| 178 | ret = seq_open(file, &proc_pid_maps_ops); |
| 179 | if (!ret) { |
| 180 | struct seq_file *m = file->private_data; |
| 181 | m->private = priv; |
| 182 | } else { |
| 183 | kfree(priv); |
| 184 | } |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 185 | } |
| 186 | return ret; |
| 187 | } |
| 188 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 189 | const struct file_operations proc_maps_operations = { |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 190 | .open = maps_open, |
| 191 | .read = seq_read, |
| 192 | .llseek = seq_lseek, |
David Howells | dbf8685 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 193 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 194 | }; |
| 195 | |