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

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

/*
 * Logic: we've got two memory sums for each process, "shared", and
 * "non-shared". Shared memory may get counted more then once, for
 * each process that owns it. Non-shared memory is counted
 * accurately.
 */
15
void task_mem(struct seq_file *m, struct mm_struct *mm)
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
	struct vm_list_struct *vml;
	unsigned long bytes = 0, sbytes = 0, slack = 0;
        
	down_read(&mm->mmap_sem);
	for (vml = mm->context.vmlist; vml; vml = vml->next) {
		if (!vml->vma)
			continue;

		bytes += kobjsize(vml);
		if (atomic_read(&mm->mm_count) > 1 ||
		    atomic_read(&vml->vma->vm_usage) > 1
		    ) {
			sbytes += kobjsize((void *) vml->vma->vm_start);
			sbytes += kobjsize(vml->vma);
		} else {
			bytes += kobjsize((void *) vml->vma->vm_start);
			bytes += kobjsize(vml->vma);
			slack += kobjsize((void *) vml->vma->vm_start) -
				(vml->vma->vm_end - vml->vma->vm_start);
		}
	}

	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 */

61
	seq_printf(m,
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		"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)
{
	struct vm_list_struct *tbp;
	unsigned long vsize = 0;

	down_read(&mm->mmap_sem);
	for (tbp = mm->context.vmlist; tbp; tbp = tbp->next) {
		if (tbp->vma)
			vsize += kobjsize((void *) tbp->vma->vm_start);
	}
	up_read(&mm->mmap_sem);
	return vsize;
}

int task_statm(struct mm_struct *mm, int *shared, int *text,
	       int *data, int *resident)
{
	struct vm_list_struct *tbp;
	int size = kobjsize(mm);

	down_read(&mm->mmap_sem);
	for (tbp = mm->context.vmlist; tbp; tbp = tbp->next) {
		size += kobjsize(tbp);
		if (tbp->vma) {
			size += kobjsize(tbp->vma);
			size += kobjsize((void *) tbp->vma->vm_start);
		}
	}

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

int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
{
	struct vm_list_struct *vml;
	struct vm_area_struct *vma;
110
	struct task_struct *task = get_proc_task(inode);
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	struct mm_struct *mm = get_task_mm(task);
	int result = -ENOENT;

	if (!mm)
		goto out;
	down_read(&mm->mmap_sem);

	vml = mm->context.vmlist;
	vma = NULL;
	while (vml) {
		if ((vml->vma->vm_flags & VM_EXECUTABLE) && vml->vma->vm_file) {
			vma = vml->vma;
			break;
		}
		vml = vml->next;
	}

	if (vma) {
129 130
		*mnt = mntget(vma->vm_file->f_path.mnt);
		*dentry = dget(vma->vm_file->f_path.dentry);
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139 140
		result = 0;
	}

	up_read(&mm->mmap_sem);
	mmput(mm);
out:
	return result;
}

/*
141
 * display mapping lines for a particular process's /proc/pid/maps
L
Linus Torvalds 已提交
142
 */
143
static int show_map(struct seq_file *m, void *_vml)
L
Linus Torvalds 已提交
144
{
145
	struct vm_list_struct *vml = _vml;
K
Kees Cook 已提交
146 147 148 149 150 151
	struct proc_maps_private *priv = m->private;
	struct task_struct *task = priv->task;

	if (maps_protect && !ptrace_may_attach(task))
		return -EACCES;

152
	return nommu_vma_show(m, vml->vma);
L
Linus Torvalds 已提交
153
}
154

L
Linus Torvalds 已提交
155 156
static void *m_start(struct seq_file *m, loff_t *pos)
{
157 158 159 160 161 162 163 164 165 166
	struct proc_maps_private *priv = m->private;
	struct vm_list_struct *vml;
	struct mm_struct *mm;
	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;

167
	mm = mm_for_maps(priv->task);
168 169 170 171 172 173 174 175 176 177
	if (!mm) {
		put_task_struct(priv->task);
		priv->task = NULL;
		return NULL;
	}

	/* start from the Nth VMA */
	for (vml = mm->context.vmlist; vml; vml = vml->next)
		if (n-- == 0)
			return vml;
L
Linus Torvalds 已提交
178 179
	return NULL;
}
180 181

static void m_stop(struct seq_file *m, void *_vml)
L
Linus Torvalds 已提交
182
{
183 184 185 186 187 188 189 190
	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 已提交
191
}
192 193

static void *m_next(struct seq_file *m, void *_vml, loff_t *pos)
L
Linus Torvalds 已提交
194
{
195 196 197 198
	struct vm_list_struct *vml = _vml;

	(*pos)++;
	return vml ? vml->next : NULL;
L
Linus Torvalds 已提交
199
}
200 201

static struct seq_operations proc_pid_maps_ops = {
L
Linus Torvalds 已提交
202 203 204 205 206
	.start	= m_start,
	.next	= m_next,
	.stop	= m_stop,
	.show	= show_map
};
207 208 209

static int maps_open(struct inode *inode, struct file *file)
{
210 211 212 213 214 215 216 217 218 219 220 221 222
	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);
		}
223 224 225 226
	}
	return ret;
}

227
const struct file_operations proc_maps_operations = {
228 229 230
	.open		= maps_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
231
	.release	= seq_release_private,
232 233
};