task_nommu.c 6.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3

#include <linux/mm.h>
#include <linux/file.h>
4
#include <linux/fdtable.h>
5
#include <linux/fs_struct.h>
L
Linus Torvalds 已提交
6
#include <linux/mount.h>
K
Kees Cook 已提交
7
#include <linux/ptrace.h>
8
#include <linux/slab.h>
L
Linus Torvalds 已提交
9 10 11 12 13
#include <linux/seq_file.h>
#include "internal.h"

/*
 * Logic: we've got two memory sums for each process, "shared", and
14
 * "non-shared". Shared memory may get counted more than once, for
L
Linus Torvalds 已提交
15 16 17
 * each process that owns it. Non-shared memory is counted
 * accurately.
 */
18
void task_mem(struct seq_file *m, struct mm_struct *mm)
L
Linus Torvalds 已提交
19
{
20
	struct vm_area_struct *vma;
21
	struct vm_region *region;
22
	struct rb_node *p;
23
	unsigned long bytes = 0, sbytes = 0, slack = 0, size;
L
Linus Torvalds 已提交
24 25
        
	down_read(&mm->mmap_sem);
26 27
	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
		vma = rb_entry(p, struct vm_area_struct, vm_rb);
L
Linus Torvalds 已提交
28

29
		bytes += kobjsize(vma);
30 31 32 33 34 35 36 37 38

		region = vma->vm_region;
		if (region) {
			size = kobjsize(region);
			size += region->vm_end - region->vm_start;
		} else {
			size = vma->vm_end - vma->vm_start;
		}

L
Linus Torvalds 已提交
39
		if (atomic_read(&mm->mm_count) > 1 ||
40
		    vma->vm_flags & VM_MAYSHARE) {
41
			sbytes += size;
L
Linus Torvalds 已提交
42
		} else {
43 44 45
			bytes += size;
			if (region)
				slack = region->vm_end - vma->vm_end;
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53
		}
	}

	if (atomic_read(&mm->mm_count) > 1)
		sbytes += kobjsize(mm);
	else
		bytes += kobjsize(mm);
	
A
Al Viro 已提交
54
	if (current->fs && current->fs->users > 1)
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		sbytes += kobjsize(current->fs);
	else
		bytes += kobjsize(current->fs);

	if (current->files && atomic_read(&current->files->count) > 1)
		sbytes += kobjsize(current->files);
	else
		bytes += kobjsize(current->files);

	if (current->sighand && atomic_read(&current->sighand->count) > 1)
		sbytes += kobjsize(current->sighand);
	else
		bytes += kobjsize(current->sighand);

	bytes += kobjsize(current); /* includes kernel stack */

71
	seq_printf(m,
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81
		"Mem:\t%8lu bytes\n"
		"Slack:\t%8lu bytes\n"
		"Shared:\t%8lu bytes\n",
		bytes, slack, sbytes);

	up_read(&mm->mmap_sem);
}

unsigned long task_vsize(struct mm_struct *mm)
{
82 83
	struct vm_area_struct *vma;
	struct rb_node *p;
L
Linus Torvalds 已提交
84 85 86
	unsigned long vsize = 0;

	down_read(&mm->mmap_sem);
87 88
	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
		vma = rb_entry(p, struct vm_area_struct, vm_rb);
89
		vsize += vma->vm_end - vma->vm_start;
L
Linus Torvalds 已提交
90 91 92 93 94
	}
	up_read(&mm->mmap_sem);
	return vsize;
}

95 96 97
unsigned long task_statm(struct mm_struct *mm,
			 unsigned long *shared, unsigned long *text,
			 unsigned long *data, unsigned long *resident)
L
Linus Torvalds 已提交
98
{
99
	struct vm_area_struct *vma;
100
	struct vm_region *region;
101
	struct rb_node *p;
102
	unsigned long size = kobjsize(mm);
L
Linus Torvalds 已提交
103 104

	down_read(&mm->mmap_sem);
105 106 107
	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
		vma = rb_entry(p, struct vm_area_struct, vm_rb);
		size += kobjsize(vma);
108 109 110 111 112
		region = vma->vm_region;
		if (region) {
			size += kobjsize(region);
			size += region->vm_end - region->vm_start;
		}
L
Linus Torvalds 已提交
113 114
	}

115 116 117 118
	*text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
		>> PAGE_SHIFT;
	*data = (PAGE_ALIGN(mm->start_stack) - (mm->start_data & PAGE_MASK))
		>> PAGE_SHIFT;
L
Linus Torvalds 已提交
119
	up_read(&mm->mmap_sem);
120 121
	size >>= PAGE_SHIFT;
	size += *text + *data;
L
Linus Torvalds 已提交
122 123 124 125
	*resident = size;
	return size;
}

126 127 128 129 130 131 132 133
static void pad_len_spaces(struct seq_file *m, int len)
{
	len = 25 + sizeof(void*) * 6 - len;
	if (len < 1)
		len = 1;
	seq_printf(m, "%*c", len, ' ');
}

134 135 136 137 138
/*
 * display a single VMA to a sequenced file
 */
static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
{
139
	struct mm_struct *mm = vma->vm_mm;
140 141 142 143
	unsigned long ino = 0;
	struct file *file;
	dev_t dev = 0;
	int flags, len;
144
	unsigned long long pgoff = 0;
145 146 147 148 149 150 151 152

	flags = vma->vm_flags;
	file = vma->vm_file;

	if (file) {
		struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
		dev = inode->i_sb->s_dev;
		ino = inode->i_ino;
153
		pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
154 155 156
	}

	seq_printf(m,
157
		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
158 159 160 161 162 163
		   vma->vm_start,
		   vma->vm_end,
		   flags & VM_READ ? 'r' : '-',
		   flags & VM_WRITE ? 'w' : '-',
		   flags & VM_EXEC ? 'x' : '-',
		   flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
164
		   pgoff,
165 166 167
		   MAJOR(dev), MINOR(dev), ino, &len);

	if (file) {
168
		pad_len_spaces(m, len);
169
		seq_path(m, &file->f_path, "");
170 171 172 173 174 175
	} else if (mm) {
		if (vma->vm_start <= mm->start_stack &&
			vma->vm_end >= mm->start_stack) {
			pad_len_spaces(m, len);
			seq_puts(m, "[stack]");
		}
176 177 178 179 180 181
	}

	seq_putc(m, '\n');
	return 0;
}

L
Linus Torvalds 已提交
182
/*
183
 * display mapping lines for a particular process's /proc/pid/maps
L
Linus Torvalds 已提交
184
 */
185
static int show_map(struct seq_file *m, void *_p)
L
Linus Torvalds 已提交
186
{
187
	struct rb_node *p = _p;
K
Kees Cook 已提交
188

189
	return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb));
L
Linus Torvalds 已提交
190
}
191

L
Linus Torvalds 已提交
192 193
static void *m_start(struct seq_file *m, loff_t *pos)
{
194 195
	struct proc_maps_private *priv = m->private;
	struct mm_struct *mm;
196
	struct rb_node *p;
197 198 199 200 201
	loff_t n = *pos;

	/* pin the task and mm whilst we play with them */
	priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
	if (!priv->task)
A
Al Viro 已提交
202
		return ERR_PTR(-ESRCH);
203

204
	mm = mm_for_maps(priv->task);
A
Al Viro 已提交
205
	if (!mm || IS_ERR(mm)) {
206 207
		put_task_struct(priv->task);
		priv->task = NULL;
A
Al Viro 已提交
208
		return mm;
209
	}
210
	down_read(&mm->mmap_sem);
211 212

	/* start from the Nth VMA */
213
	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
214
		if (n-- == 0)
215
			return p;
L
Linus Torvalds 已提交
216 217
	return NULL;
}
218 219

static void m_stop(struct seq_file *m, void *_vml)
L
Linus Torvalds 已提交
220
{
221 222 223 224 225 226 227 228
	struct proc_maps_private *priv = m->private;

	if (priv->task) {
		struct mm_struct *mm = priv->task->mm;
		up_read(&mm->mmap_sem);
		mmput(mm);
		put_task_struct(priv->task);
	}
L
Linus Torvalds 已提交
229
}
230

231
static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
L
Linus Torvalds 已提交
232
{
233
	struct rb_node *p = _p;
234 235

	(*pos)++;
236
	return p ? rb_next(p) : NULL;
L
Linus Torvalds 已提交
237
}
238

239
static const struct seq_operations proc_pid_maps_ops = {
L
Linus Torvalds 已提交
240 241 242 243 244
	.start	= m_start,
	.next	= m_next,
	.stop	= m_stop,
	.show	= show_map
};
245 246 247

static int maps_open(struct inode *inode, struct file *file)
{
248 249 250 251 252 253 254 255 256 257 258 259 260
	struct proc_maps_private *priv;
	int ret = -ENOMEM;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (priv) {
		priv->pid = proc_pid(inode);
		ret = seq_open(file, &proc_pid_maps_ops);
		if (!ret) {
			struct seq_file *m = file->private_data;
			m->private = priv;
		} else {
			kfree(priv);
		}
261 262 263 264
	}
	return ret;
}

265
const struct file_operations proc_maps_operations = {
266 267 268
	.open		= maps_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
269
	.release	= seq_release_private,
270 271
};