3 #include <linux/file.h>
4 #include <linux/fdtable.h>
5 #include <linux/mount.h>
6 #include <linux/ptrace.h>
7 #include <linux/seq_file.h>
11 * Logic: we've got two memory sums for each process, "shared", and
12 * "non-shared". Shared memory may get counted more than once, for
13 * each process that owns it. Non-shared memory is counted
16 void task_mem(struct seq_file *m, struct mm_struct *mm)
18 struct vm_area_struct *vma;
20 unsigned long bytes = 0, sbytes = 0, slack = 0;
22 down_read(&mm->mmap_sem);
23 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
24 vma = rb_entry(p, struct vm_area_struct, vm_rb);
26 bytes += kobjsize(vma);
27 if (atomic_read(&mm->mm_count) > 1 ||
29 vma->vm_flags & VM_MAYSHARE) {
30 sbytes += kobjsize((void *) vma->vm_start);
32 sbytes += kobjsize(vma->vm_region);
34 bytes += kobjsize((void *) vma->vm_start);
35 slack += kobjsize((void *) vma->vm_start) -
36 (vma->vm_end - vma->vm_start);
40 if (atomic_read(&mm->mm_count) > 1)
41 sbytes += kobjsize(mm);
43 bytes += kobjsize(mm);
45 if (current->fs && atomic_read(¤t->fs->count) > 1)
46 sbytes += kobjsize(current->fs);
48 bytes += kobjsize(current->fs);
50 if (current->files && atomic_read(¤t->files->count) > 1)
51 sbytes += kobjsize(current->files);
53 bytes += kobjsize(current->files);
55 if (current->sighand && atomic_read(¤t->sighand->count) > 1)
56 sbytes += kobjsize(current->sighand);
58 bytes += kobjsize(current->sighand);
60 bytes += kobjsize(current); /* includes kernel stack */
64 "Slack:\t%8lu bytes\n"
65 "Shared:\t%8lu bytes\n",
66 bytes, slack, sbytes);
68 up_read(&mm->mmap_sem);
71 unsigned long task_vsize(struct mm_struct *mm)
73 struct vm_area_struct *vma;
75 unsigned long vsize = 0;
77 down_read(&mm->mmap_sem);
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;
82 up_read(&mm->mmap_sem);
86 int task_statm(struct mm_struct *mm, int *shared, int *text,
87 int *data, int *resident)
89 struct vm_area_struct *vma;
91 int size = kobjsize(mm);
93 down_read(&mm->mmap_sem);
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);
100 size += (*text = mm->end_code - mm->start_code);
101 size += (*data = mm->start_stack - mm->start_data);
102 up_read(&mm->mmap_sem);
108 * display a single VMA to a sequenced file
110 static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
112 unsigned long ino = 0;
117 flags = vma->vm_flags;
121 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
122 dev = inode->i_sb->s_dev;
127 "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
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);
138 len = 25 + sizeof(void *) * 6 - len;
141 seq_printf(m, "%*c", len, ' ');
142 seq_path(m, &file->f_path, "");
150 * display mapping lines for a particular process's /proc/pid/maps
152 static int show_map(struct seq_file *m, void *_p)
154 struct rb_node *p = _p;
156 return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb));
159 static void *m_start(struct seq_file *m, loff_t *pos)
161 struct proc_maps_private *priv = m->private;
162 struct mm_struct *mm;
166 /* pin the task and mm whilst we play with them */
167 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
171 mm = mm_for_maps(priv->task);
173 put_task_struct(priv->task);
178 /* start from the Nth VMA */
179 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
185 static void m_stop(struct seq_file *m, void *_vml)
187 struct proc_maps_private *priv = m->private;
190 struct mm_struct *mm = priv->task->mm;
191 up_read(&mm->mmap_sem);
193 put_task_struct(priv->task);
197 static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
199 struct rb_node *p = _p;
202 return p ? rb_next(p) : NULL;
205 static const struct seq_operations proc_pid_maps_ops = {
212 static int maps_open(struct inode *inode, struct file *file)
214 struct proc_maps_private *priv;
217 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
219 priv->pid = proc_pid(inode);
220 ret = seq_open(file, &proc_pid_maps_ops);
222 struct seq_file *m = file->private_data;
231 const struct file_operations proc_maps_operations = {
235 .release = seq_release_private,