blob: 23266694db117155f6e23284fecbf69800cecb09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2#include <linux/mm.h>
3#include <linux/file.h>
Bryan Wueb280622008-05-04 23:12:55 +08004#include <linux/fdtable.h>
Al Viro5ad4e532009-03-29 19:50:06 -04005#include <linux/fs_struct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/mount.h>
Kees Cook5096add2007-05-08 00:26:04 -07007#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/seq_file.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010010#include <linux/sched/mm.h>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "internal.h"
13
14/*
15 * Logic: we've got two memory sums for each process, "shared", and
Frederik Schwarzer025dfda2008-10-16 19:02:37 +020016 * "non-shared". Shared memory may get counted more than once, for
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * each process that owns it. Non-shared memory is counted
18 * accurately.
19 */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080020void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
David Howells8feae132009-01-08 12:04:47 +000022 struct vm_area_struct *vma;
David Howells38f71472009-01-08 12:04:47 +000023 struct vm_region *region;
David Howells8feae132009-01-08 12:04:47 +000024 struct rb_node *p;
David Howells38f71472009-01-08 12:04:47 +000025 unsigned long bytes = 0, sbytes = 0, slack = 0, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 down_read(&mm->mmap_sem);
David Howells8feae132009-01-08 12:04:47 +000028 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
29 vma = rb_entry(p, struct vm_area_struct, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
David Howells8feae132009-01-08 12:04:47 +000031 bytes += kobjsize(vma);
David Howells38f71472009-01-08 12:04:47 +000032
33 region = vma->vm_region;
34 if (region) {
35 size = kobjsize(region);
36 size += region->vm_end - region->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 } else {
David Howells38f71472009-01-08 12:04:47 +000038 size = vma->vm_end - vma->vm_start;
39 }
40
41 if (atomic_read(&mm->mm_count) > 1 ||
42 vma->vm_flags & VM_MAYSHARE) {
43 sbytes += size;
44 } else {
45 bytes += size;
46 if (region)
47 slack = region->vm_end - vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
49 }
50
51 if (atomic_read(&mm->mm_count) > 1)
52 sbytes += kobjsize(mm);
53 else
54 bytes += kobjsize(mm);
55
Al Viro498052b2009-03-30 07:20:30 -040056 if (current->fs && current->fs->users > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 sbytes += kobjsize(current->fs);
58 else
59 bytes += kobjsize(current->fs);
60
61 if (current->files && atomic_read(&current->files->count) > 1)
62 sbytes += kobjsize(current->files);
63 else
64 bytes += kobjsize(current->files);
65
66 if (current->sighand && atomic_read(&current->sighand->count) > 1)
67 sbytes += kobjsize(current->sighand);
68 else
69 bytes += kobjsize(current->sighand);
70
71 bytes += kobjsize(current); /* includes kernel stack */
72
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080073 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 "Mem:\t%8lu bytes\n"
75 "Slack:\t%8lu bytes\n"
76 "Shared:\t%8lu bytes\n",
77 bytes, slack, sbytes);
78
79 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82unsigned long task_vsize(struct mm_struct *mm)
83{
David Howells8feae132009-01-08 12:04:47 +000084 struct vm_area_struct *vma;
85 struct rb_node *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned long vsize = 0;
87
88 down_read(&mm->mmap_sem);
David Howells8feae132009-01-08 12:04:47 +000089 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
90 vma = rb_entry(p, struct vm_area_struct, vm_rb);
David Howells38f71472009-01-08 12:04:47 +000091 vsize += vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93 up_read(&mm->mmap_sem);
94 return vsize;
95}
96
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080097unsigned long task_statm(struct mm_struct *mm,
98 unsigned long *shared, unsigned long *text,
99 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
David Howells8feae132009-01-08 12:04:47 +0000101 struct vm_area_struct *vma;
David Howells38f71472009-01-08 12:04:47 +0000102 struct vm_region *region;
David Howells8feae132009-01-08 12:04:47 +0000103 struct rb_node *p;
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -0800104 unsigned long size = kobjsize(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 down_read(&mm->mmap_sem);
David Howells8feae132009-01-08 12:04:47 +0000107 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
108 vma = rb_entry(p, struct vm_area_struct, vm_rb);
109 size += kobjsize(vma);
David Howells38f71472009-01-08 12:04:47 +0000110 region = vma->vm_region;
111 if (region) {
112 size += kobjsize(region);
113 size += region->vm_end - region->vm_start;
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
Steven J. Magnani7e1e0ef2009-12-14 18:00:04 -0800117 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
118 >> PAGE_SHIFT;
119 *data = (PAGE_ALIGN(mm->start_stack) - (mm->start_data & PAGE_MASK))
120 >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 up_read(&mm->mmap_sem);
Steven J. Magnani7e1e0ef2009-12-14 18:00:04 -0800122 size >>= PAGE_SHIFT;
123 size += *text + *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *resident = size;
125 return size;
126}
127
Johannes Weiner65376df2016-02-02 16:57:29 -0800128static int is_stack(struct proc_maps_private *priv,
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700129 struct vm_area_struct *vma)
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700130{
Johannes Weiner65376df2016-02-02 16:57:29 -0800131 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700132
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700133 /*
134 * We make no effort to guess what a given thread considers to be
135 * its "stack". It's not even well-defined for programs written
136 * languages like Go.
137 */
138 return vma->vm_start <= mm->start_stack &&
139 vma->vm_end >= mm->start_stack;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
David Howells8feae132009-01-08 12:04:47 +0000143 * display a single VMA to a sequenced file
144 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700145static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
146 int is_pid)
David Howells8feae132009-01-08 12:04:47 +0000147{
Mike Frysinger3c26c9d2010-06-29 15:05:17 -0700148 struct mm_struct *mm = vma->vm_mm;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700149 struct proc_maps_private *priv = m->private;
David Howells8feae132009-01-08 12:04:47 +0000150 unsigned long ino = 0;
151 struct file *file;
152 dev_t dev = 0;
Tetsuo Handa652586d2013-11-14 14:31:57 -0800153 int flags;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700154 unsigned long long pgoff = 0;
David Howells8feae132009-01-08 12:04:47 +0000155
156 flags = vma->vm_flags;
157 file = vma->vm_file;
158
159 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500160 struct inode *inode = file_inode(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000161 dev = inode->i_sb->s_dev;
162 ino = inode->i_ino;
Nobuhiro Iwamatsu4c967292009-04-07 21:21:43 -0700163 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
David Howells8feae132009-01-08 12:04:47 +0000164 }
165
Tetsuo Handa652586d2013-11-14 14:31:57 -0800166 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
David Howells8feae132009-01-08 12:04:47 +0000167 seq_printf(m,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800168 "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
David Howells8feae132009-01-08 12:04:47 +0000169 vma->vm_start,
170 vma->vm_end,
171 flags & VM_READ ? 'r' : '-',
172 flags & VM_WRITE ? 'w' : '-',
173 flags & VM_EXEC ? 'x' : '-',
174 flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700175 pgoff,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800176 MAJOR(dev), MINOR(dev), ino);
David Howells8feae132009-01-08 12:04:47 +0000177
178 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800179 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200180 seq_file_path(m, file, "");
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700181 } else if (mm && is_stack(priv, vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -0800182 seq_pad(m, ' ');
183 seq_printf(m, "[stack]");
David Howells8feae132009-01-08 12:04:47 +0000184 }
185
186 seq_putc(m, '\n');
187 return 0;
188}
189
190/*
David Howellsdbf86852006-09-27 01:50:19 -0700191 * display mapping lines for a particular process's /proc/pid/maps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700193static int show_map(struct seq_file *m, void *_p, int is_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
David Howells8feae132009-01-08 12:04:47 +0000195 struct rb_node *p = _p;
Kees Cook5096add2007-05-08 00:26:04 -0700196
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700197 return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb),
198 is_pid);
199}
200
201static int show_pid_map(struct seq_file *m, void *_p)
202{
203 return show_map(m, _p, 1);
204}
205
206static int show_tid_map(struct seq_file *m, void *_p)
207{
208 return show_map(m, _p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
David Howellsdbf86852006-09-27 01:50:19 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static void *m_start(struct seq_file *m, loff_t *pos)
212{
David Howellsdbf86852006-09-27 01:50:19 -0700213 struct proc_maps_private *priv = m->private;
David Howellsdbf86852006-09-27 01:50:19 -0700214 struct mm_struct *mm;
David Howells8feae132009-01-08 12:04:47 +0000215 struct rb_node *p;
David Howellsdbf86852006-09-27 01:50:19 -0700216 loff_t n = *pos;
217
218 /* pin the task and mm whilst we play with them */
Oleg Nesterov2c033762014-10-09 15:25:51 -0700219 priv->task = get_proc_task(priv->inode);
David Howellsdbf86852006-09-27 01:50:19 -0700220 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500221 return ERR_PTR(-ESRCH);
David Howellsdbf86852006-09-27 01:50:19 -0700222
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700223 mm = priv->mm;
Vegard Nossum388f7932017-02-27 14:30:13 -0800224 if (!mm || !mmget_not_zero(mm))
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700225 return NULL;
David Howellsdbf86852006-09-27 01:50:19 -0700226
Oleg Nesterov47fecca2014-10-09 15:25:49 -0700227 down_read(&mm->mmap_sem);
David Howellsdbf86852006-09-27 01:50:19 -0700228 /* start from the Nth VMA */
David Howells8feae132009-01-08 12:04:47 +0000229 for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
David Howellsdbf86852006-09-27 01:50:19 -0700230 if (n-- == 0)
David Howells8feae132009-01-08 12:04:47 +0000231 return p;
Oleg Nesterov47fecca2014-10-09 15:25:49 -0700232
233 up_read(&mm->mmap_sem);
234 mmput(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return NULL;
236}
David Howellsdbf86852006-09-27 01:50:19 -0700237
238static void m_stop(struct seq_file *m, void *_vml)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
David Howellsdbf86852006-09-27 01:50:19 -0700240 struct proc_maps_private *priv = m->private;
241
Oleg Nesterov47fecca2014-10-09 15:25:49 -0700242 if (!IS_ERR_OR_NULL(_vml)) {
243 up_read(&priv->mm->mmap_sem);
244 mmput(priv->mm);
245 }
David Howellsdbf86852006-09-27 01:50:19 -0700246 if (priv->task) {
David Howellsdbf86852006-09-27 01:50:19 -0700247 put_task_struct(priv->task);
Oleg Nesterov47fecca2014-10-09 15:25:49 -0700248 priv->task = NULL;
David Howellsdbf86852006-09-27 01:50:19 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
David Howellsdbf86852006-09-27 01:50:19 -0700251
David Howells8feae132009-01-08 12:04:47 +0000252static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
David Howells8feae132009-01-08 12:04:47 +0000254 struct rb_node *p = _p;
David Howellsdbf86852006-09-27 01:50:19 -0700255
256 (*pos)++;
David Howells8feae132009-01-08 12:04:47 +0000257 return p ? rb_next(p) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
David Howellsdbf86852006-09-27 01:50:19 -0700259
Jan Engelhardt03a44822008-02-08 04:21:19 -0800260static const struct seq_operations proc_pid_maps_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .start = m_start,
262 .next = m_next,
263 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700264 .show = show_pid_map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
Eric W. Biederman662795d2006-06-26 00:25:48 -0700266
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700267static const struct seq_operations proc_tid_maps_ops = {
268 .start = m_start,
269 .next = m_next,
270 .stop = m_stop,
271 .show = show_tid_map
272};
273
274static int maps_open(struct inode *inode, struct file *file,
275 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -0700276{
David Howellsdbf86852006-09-27 01:50:19 -0700277 struct proc_maps_private *priv;
David Howellsdbf86852006-09-27 01:50:19 -0700278
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700279 priv = __seq_open_private(file, ops, sizeof(*priv));
Oleg Nesterovce34fdd2014-10-09 15:25:45 -0700280 if (!priv)
281 return -ENOMEM;
282
Oleg Nesterov2c033762014-10-09 15:25:51 -0700283 priv->inode = inode;
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700284 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
285 if (IS_ERR(priv->mm)) {
286 int err = PTR_ERR(priv->mm);
287
288 seq_release_private(inode, file);
289 return err;
290 }
291
Oleg Nesterovce34fdd2014-10-09 15:25:45 -0700292 return 0;
Eric W. Biederman662795d2006-06-26 00:25:48 -0700293}
294
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700295
296static int map_release(struct inode *inode, struct file *file)
297{
298 struct seq_file *seq = file->private_data;
299 struct proc_maps_private *priv = seq->private;
300
301 if (priv->mm)
302 mmdrop(priv->mm);
303
304 return seq_release_private(inode, file);
305}
306
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700307static int pid_maps_open(struct inode *inode, struct file *file)
308{
309 return maps_open(inode, file, &proc_pid_maps_ops);
310}
311
312static int tid_maps_open(struct inode *inode, struct file *file)
313{
314 return maps_open(inode, file, &proc_tid_maps_ops);
315}
316
317const struct file_operations proc_pid_maps_operations = {
318 .open = pid_maps_open,
319 .read = seq_read,
320 .llseek = seq_lseek,
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700321 .release = map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700322};
323
324const struct file_operations proc_tid_maps_operations = {
325 .open = tid_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -0700326 .read = seq_read,
327 .llseek = seq_lseek,
Oleg Nesterov27692cd2014-10-09 15:25:47 -0700328 .release = map_release,
Eric W. Biederman662795d2006-06-26 00:25:48 -0700329};
330