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;
19 struct vm_region *region;
21 unsigned long bytes = 0, sbytes = 0, slack = 0, size;
23 down_read(&mm->mmap_sem);
24 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
25 vma = rb_entry(p, struct vm_area_struct, vm_rb);
27 bytes += kobjsize(vma);
29 region = vma->vm_region;
31 size = kobjsize(region);
32 size += region->vm_end - region->vm_start;
34 size = vma->vm_end - vma->vm_start;
37 if (atomic_read(&mm->mm_count) > 1 ||
38 vma->vm_flags & VM_MAYSHARE) {
43 slack = region->vm_end - vma->vm_end;
47 if (atomic_read(&mm->mm_count) > 1)
48 sbytes += kobjsize(mm);
50 bytes += kobjsize(mm);
52 if (current->fs && atomic_read(¤t->fs->count) > 1)
53 sbytes += kobjsize(current->fs);
55 bytes += kobjsize(current->fs);
57 if (current->files && atomic_read(¤t->files->count) > 1)
58 sbytes += kobjsize(current->files);
60 bytes += kobjsize(current->files);
62 if (current->sighand && atomic_read(¤t->sighand->count) > 1)
63 sbytes += kobjsize(current->sighand);
65 bytes += kobjsize(current->sighand);
67 bytes += kobjsize(current); /* includes kernel stack */
71 "Slack:\t%8lu bytes\n"
72 "Shared:\t%8lu bytes\n",
73 bytes, slack, sbytes);
75 up_read(&mm->mmap_sem);
78 unsigned long task_vsize(struct mm_struct *mm)
80 struct vm_area_struct *vma;
82 unsigned long vsize = 0;
84 down_read(&mm->mmap_sem);
85 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
86 vma = rb_entry(p, struct vm_area_struct, vm_rb);
87 vsize += vma->vm_end - vma->vm_start;
89 up_read(&mm->mmap_sem);
93 int task_statm(struct mm_struct *mm, int *shared, int *text,
94 int *data, int *resident)
96 struct vm_area_struct *vma;
97 struct vm_region *region;
99 int size = kobjsize(mm);
101 down_read(&mm->mmap_sem);
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);
105 region = vma->vm_region;
107 size += kobjsize(region);
108 size += region->vm_end - region->vm_start;
112 size += (*text = mm->end_code - mm->start_code);
113 size += (*data = mm->start_stack - mm->start_data);
114 up_read(&mm->mmap_sem);
120 * display a single VMA to a sequenced file
122 static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
124 unsigned long ino = 0;
129 flags = vma->vm_flags;
133 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
134 dev = inode->i_sb->s_dev;
139 "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
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',
146 vma->vm_pgoff << PAGE_SHIFT,
147 MAJOR(dev), MINOR(dev), ino, &len);
150 len = 25 + sizeof(void *) * 6 - len;
153 seq_printf(m, "%*c", len, ' ');
154 seq_path(m, &file->f_path, "");
162 * display mapping lines for a particular process's /proc/pid/maps
164 static int show_map(struct seq_file *m, void *_p)
166 struct rb_node *p = _p;
168 return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb));
171 static void *m_start(struct seq_file *m, loff_t *pos)
173 struct proc_maps_private *priv = m->private;
174 struct mm_struct *mm;
178 /* pin the task and mm whilst we play with them */
179 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
183 mm = mm_for_maps(priv->task);
185 put_task_struct(priv->task);
190 /* start from the Nth VMA */
191 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
197 static void m_stop(struct seq_file *m, void *_vml)
199 struct proc_maps_private *priv = m->private;
202 struct mm_struct *mm = priv->task->mm;
203 up_read(&mm->mmap_sem);
205 put_task_struct(priv->task);
209 static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
211 struct rb_node *p = _p;
214 return p ? rb_next(p) : NULL;
217 static const struct seq_operations proc_pid_maps_ops = {
224 static int maps_open(struct inode *inode, struct file *file)
226 struct proc_maps_private *priv;
229 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
231 priv->pid = proc_pid(inode);
232 ret = seq_open(file, &proc_pid_maps_ops);
234 struct seq_file *m = file->private_data;
243 const struct file_operations proc_maps_operations = {
247 .release = seq_release_private,