3 #include <linux/file.h>
4 #include <linux/mount.h>
5 #include <linux/ptrace.h>
6 #include <linux/seq_file.h>
10 * Logic: we've got two memory sums for each process, "shared", and
11 * "non-shared". Shared memory may get counted more then once, for
12 * each process that owns it. Non-shared memory is counted
15 void task_mem(struct seq_file
*m
, struct mm_struct
*mm
)
17 struct vm_list_struct
*vml
;
18 unsigned long bytes
= 0, sbytes
= 0, slack
= 0;
20 down_read(&mm
->mmap_sem
);
21 for (vml
= mm
->context
.vmlist
; vml
; vml
= vml
->next
) {
25 bytes
+= kobjsize(vml
);
26 if (atomic_read(&mm
->mm_count
) > 1 ||
27 atomic_read(&vml
->vma
->vm_usage
) > 1
29 sbytes
+= kobjsize((void *) vml
->vma
->vm_start
);
30 sbytes
+= kobjsize(vml
->vma
);
32 bytes
+= kobjsize((void *) vml
->vma
->vm_start
);
33 bytes
+= kobjsize(vml
->vma
);
34 slack
+= kobjsize((void *) vml
->vma
->vm_start
) -
35 (vml
->vma
->vm_end
- vml
->vma
->vm_start
);
39 if (atomic_read(&mm
->mm_count
) > 1)
40 sbytes
+= kobjsize(mm
);
42 bytes
+= kobjsize(mm
);
44 if (current
->fs
&& atomic_read(¤t
->fs
->count
) > 1)
45 sbytes
+= kobjsize(current
->fs
);
47 bytes
+= kobjsize(current
->fs
);
49 if (current
->files
&& atomic_read(¤t
->files
->count
) > 1)
50 sbytes
+= kobjsize(current
->files
);
52 bytes
+= kobjsize(current
->files
);
54 if (current
->sighand
&& atomic_read(¤t
->sighand
->count
) > 1)
55 sbytes
+= kobjsize(current
->sighand
);
57 bytes
+= kobjsize(current
->sighand
);
59 bytes
+= kobjsize(current
); /* includes kernel stack */
63 "Slack:\t%8lu bytes\n"
64 "Shared:\t%8lu bytes\n",
65 bytes
, slack
, sbytes
);
67 up_read(&mm
->mmap_sem
);
70 unsigned long task_vsize(struct mm_struct
*mm
)
72 struct vm_list_struct
*tbp
;
73 unsigned long vsize
= 0;
75 down_read(&mm
->mmap_sem
);
76 for (tbp
= mm
->context
.vmlist
; tbp
; tbp
= tbp
->next
) {
78 vsize
+= kobjsize((void *) tbp
->vma
->vm_start
);
80 up_read(&mm
->mmap_sem
);
84 int task_statm(struct mm_struct
*mm
, int *shared
, int *text
,
85 int *data
, int *resident
)
87 struct vm_list_struct
*tbp
;
88 int size
= kobjsize(mm
);
90 down_read(&mm
->mmap_sem
);
91 for (tbp
= mm
->context
.vmlist
; tbp
; tbp
= tbp
->next
) {
92 size
+= kobjsize(tbp
);
94 size
+= kobjsize(tbp
->vma
);
95 size
+= kobjsize((void *) tbp
->vma
->vm_start
);
99 size
+= (*text
= mm
->end_code
- mm
->start_code
);
100 size
+= (*data
= mm
->start_stack
- mm
->start_data
);
101 up_read(&mm
->mmap_sem
);
106 int proc_exe_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
108 struct vm_list_struct
*vml
;
109 struct vm_area_struct
*vma
;
110 struct task_struct
*task
= get_proc_task(inode
);
111 struct mm_struct
*mm
= get_task_mm(task
);
112 int result
= -ENOENT
;
116 down_read(&mm
->mmap_sem
);
118 vml
= mm
->context
.vmlist
;
121 if ((vml
->vma
->vm_flags
& VM_EXECUTABLE
) && vml
->vma
->vm_file
) {
129 *mnt
= mntget(vma
->vm_file
->f_path
.mnt
);
130 *dentry
= dget(vma
->vm_file
->f_path
.dentry
);
134 up_read(&mm
->mmap_sem
);
141 * display mapping lines for a particular process's /proc/pid/maps
143 static int show_map(struct seq_file
*m
, void *_vml
)
145 struct vm_list_struct
*vml
= _vml
;
146 struct proc_maps_private
*priv
= m
->private;
147 struct task_struct
*task
= priv
->task
;
149 if (maps_protect
&& !ptrace_may_attach(task
))
152 return nommu_vma_show(m
, vml
->vma
);
155 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
157 struct proc_maps_private
*priv
= m
->private;
158 struct vm_list_struct
*vml
;
159 struct mm_struct
*mm
;
162 /* pin the task and mm whilst we play with them */
163 priv
->task
= get_pid_task(priv
->pid
, PIDTYPE_PID
);
167 mm
= mm_for_maps(priv
->task
);
169 put_task_struct(priv
->task
);
174 /* start from the Nth VMA */
175 for (vml
= mm
->context
.vmlist
; vml
; vml
= vml
->next
)
181 static void m_stop(struct seq_file
*m
, void *_vml
)
183 struct proc_maps_private
*priv
= m
->private;
186 struct mm_struct
*mm
= priv
->task
->mm
;
187 up_read(&mm
->mmap_sem
);
189 put_task_struct(priv
->task
);
193 static void *m_next(struct seq_file
*m
, void *_vml
, loff_t
*pos
)
195 struct vm_list_struct
*vml
= _vml
;
198 return vml
? vml
->next
: NULL
;
201 static const struct seq_operations proc_pid_maps_ops
= {
208 static int maps_open(struct inode
*inode
, struct file
*file
)
210 struct proc_maps_private
*priv
;
213 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
215 priv
->pid
= proc_pid(inode
);
216 ret
= seq_open(file
, &proc_pid_maps_ops
);
218 struct seq_file
*m
= file
->private_data
;
227 const struct file_operations proc_maps_operations
= {
231 .release
= seq_release_private
,