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

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

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

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

		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 已提交
37
		if (atomic_read(&mm->mm_count) > 1 ||
38
		    vma->vm_flags & VM_MAYSHARE) {
39
			sbytes += size;
L
Linus Torvalds 已提交
40
		} else {
41 42 43
			bytes += size;
			if (region)
				slack = region->vm_end - vma->vm_end;
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		}
	}

	if (atomic_read(&mm->mm_count) > 1)
		sbytes += kobjsize(mm);
	else
		bytes += kobjsize(mm);
	
	if (current->fs && atomic_read(&current->fs->count) > 1)
		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 */

69
	seq_printf(m,
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79
		"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)
{
80 81
	struct vm_area_struct *vma;
	struct rb_node *p;
L
Linus Torvalds 已提交
82 83 84
	unsigned long vsize = 0;

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

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

	down_read(&mm->mmap_sem);
102 103 104
	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);
105 106 107 108 109
		region = vma->vm_region;
		if (region) {
			size += kobjsize(region);
			size += region->vm_end - region->vm_start;
		}
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118
	}

	size += (*text = mm->end_code - mm->start_code);
	size += (*data = mm->start_stack - mm->start_data);
	up_read(&mm->mmap_sem);
	*resident = size;
	return size;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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)
{
	unsigned long ino = 0;
	struct file *file;
	dev_t dev = 0;
	int flags, len;

	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;
	}

	seq_printf(m,
139
		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
140 141 142 143 144 145
		   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',
146
		   (unsigned long long) vma->vm_pgoff << PAGE_SHIFT,
147 148 149 150 151 152 153 154 155 156 157 158 159 160
		   MAJOR(dev), MINOR(dev), ino, &len);

	if (file) {
		len = 25 + sizeof(void *) * 6 - len;
		if (len < 1)
			len = 1;
		seq_printf(m, "%*c", len, ' ');
		seq_path(m, &file->f_path, "");
	}

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

L
Linus Torvalds 已提交
161
/*
162
 * display mapping lines for a particular process's /proc/pid/maps
L
Linus Torvalds 已提交
163
 */
164
static int show_map(struct seq_file *m, void *_p)
L
Linus Torvalds 已提交
165
{
166
	struct rb_node *p = _p;
K
Kees Cook 已提交
167

168
	return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb));
L
Linus Torvalds 已提交
169
}
170

L
Linus Torvalds 已提交
171 172
static void *m_start(struct seq_file *m, loff_t *pos)
{
173 174
	struct proc_maps_private *priv = m->private;
	struct mm_struct *mm;
175
	struct rb_node *p;
176 177 178 179 180 181 182
	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)
		return NULL;

183
	mm = mm_for_maps(priv->task);
184 185 186 187 188 189 190
	if (!mm) {
		put_task_struct(priv->task);
		priv->task = NULL;
		return NULL;
	}

	/* start from the Nth VMA */
191
	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
192
		if (n-- == 0)
193
			return p;
L
Linus Torvalds 已提交
194 195
	return NULL;
}
196 197

static void m_stop(struct seq_file *m, void *_vml)
L
Linus Torvalds 已提交
198
{
199 200 201 202 203 204 205 206
	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 已提交
207
}
208

209
static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
L
Linus Torvalds 已提交
210
{
211
	struct rb_node *p = _p;
212 213

	(*pos)++;
214
	return p ? rb_next(p) : NULL;
L
Linus Torvalds 已提交
215
}
216

217
static const struct seq_operations proc_pid_maps_ops = {
L
Linus Torvalds 已提交
218 219 220 221 222
	.start	= m_start,
	.next	= m_next,
	.stop	= m_stop,
	.show	= show_map
};
223 224 225

static int maps_open(struct inode *inode, struct file *file)
{
226 227 228 229 230 231 232 233 234 235 236 237 238
	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);
		}
239 240 241 242
	}
	return ret;
}

243
const struct file_operations proc_maps_operations = {
244 245 246
	.open		= maps_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
247
	.release	= seq_release_private,
248 249
};