Blame view

fs/proc/task_nommu.c 6 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  
  #include <linux/mm.h>
  #include <linux/file.h>
eb28062f1   Bryan Wu   task_nommu: fix c...
4
  #include <linux/fdtable.h>
5ad4e53bd   Al Viro   Get rid of indire...
5
  #include <linux/fs_struct.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
  #include <linux/mount.h>
5096add84   Kees Cook   proc: maps protec...
7
  #include <linux/ptrace.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
8
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
025dfdafe   Frederik Schwarzer   trivial: fix then...
14
   * "non-shared". Shared memory may get counted more than once, for
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
   * each process that owns it. Non-shared memory is counted
   * accurately.
   */
df5f8314c   Eric W. Biederman   proc: seqfile con...
18
  void task_mem(struct seq_file *m, struct mm_struct *mm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  {
8feae1311   David Howells   NOMMU: Make VMAs ...
20
  	struct vm_area_struct *vma;
38f714795   David Howells   NOMMU: Improve pr...
21
  	struct vm_region *region;
8feae1311   David Howells   NOMMU: Make VMAs ...
22
  	struct rb_node *p;
38f714795   David Howells   NOMMU: Improve pr...
23
  	unsigned long bytes = 0, sbytes = 0, slack = 0, size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
          
  	down_read(&mm->mmap_sem);
8feae1311   David Howells   NOMMU: Make VMAs ...
26
27
  	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  		vma = rb_entry(p, struct vm_area_struct, vm_rb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

8feae1311   David Howells   NOMMU: Make VMAs ...
29
  		bytes += kobjsize(vma);
38f714795   David Howells   NOMMU: Improve pr...
30
31
32
33
34
35
36
37
  
  		region = vma->vm_region;
  		if (region) {
  			size = kobjsize(region);
  			size += region->vm_end - region->vm_start;
  		} else {
  			size = vma->vm_end - vma->vm_start;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  		if (atomic_read(&mm->mm_count) > 1 ||
8feae1311   David Howells   NOMMU: Make VMAs ...
39
  		    vma->vm_flags & VM_MAYSHARE) {
38f714795   David Howells   NOMMU: Improve pr...
40
  			sbytes += size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
  		} else {
38f714795   David Howells   NOMMU: Improve pr...
42
43
44
  			bytes += size;
  			if (region)
  				slack = region->vm_end - vma->vm_end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
  		}
  	}
  
  	if (atomic_read(&mm->mm_count) > 1)
  		sbytes += kobjsize(mm);
  	else
  		bytes += kobjsize(mm);
  	
498052bba   Al Viro   New locking/refco...
53
  	if (current->fs && current->fs->users > 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  		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 */
df5f8314c   Eric W. Biederman   proc: seqfile con...
69
  	seq_printf(m,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
73
74
75
76
77
78
  		"Mem:\t%8lu bytes
  "
  		"Slack:\t%8lu bytes
  "
  		"Shared:\t%8lu bytes
  ",
  		bytes, slack, sbytes);
  
  	up_read(&mm->mmap_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
81
82
  }
  
  unsigned long task_vsize(struct mm_struct *mm)
  {
8feae1311   David Howells   NOMMU: Make VMAs ...
83
84
  	struct vm_area_struct *vma;
  	struct rb_node *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
87
  	unsigned long vsize = 0;
  
  	down_read(&mm->mmap_sem);
8feae1311   David Howells   NOMMU: Make VMAs ...
88
89
  	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  		vma = rb_entry(p, struct vm_area_struct, vm_rb);
38f714795   David Howells   NOMMU: Improve pr...
90
  		vsize += vma->vm_end - vma->vm_start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
  	}
  	up_read(&mm->mmap_sem);
  	return vsize;
  }
a2ade7b6c   Alexey Dobriyan   proc: use unsigne...
95
96
97
  unsigned long task_statm(struct mm_struct *mm,
  			 unsigned long *shared, unsigned long *text,
  			 unsigned long *data, unsigned long *resident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  {
8feae1311   David Howells   NOMMU: Make VMAs ...
99
  	struct vm_area_struct *vma;
38f714795   David Howells   NOMMU: Improve pr...
100
  	struct vm_region *region;
8feae1311   David Howells   NOMMU: Make VMAs ...
101
  	struct rb_node *p;
a2ade7b6c   Alexey Dobriyan   proc: use unsigne...
102
  	unsigned long size = kobjsize(mm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
  
  	down_read(&mm->mmap_sem);
8feae1311   David Howells   NOMMU: Make VMAs ...
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);
38f714795   David Howells   NOMMU: Improve pr...
108
109
110
111
112
  		region = vma->vm_region;
  		if (region) {
  			size += kobjsize(region);
  			size += region->vm_end - region->vm_start;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
  	}
7e1e0ef22   Steven J. Magnani   procfs: use prope...
114
115
116
117
  	*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;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  	up_read(&mm->mmap_sem);
7e1e0ef22   Steven J. Magnani   procfs: use prope...
119
120
  	size >>= PAGE_SHIFT;
  	size += *text + *data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
  	*resident = size;
  	return size;
  }
3c26c9d95   Mike Frysinger   nommu: add '[stac...
124
125
126
127
128
129
130
  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, ' ');
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
  /*
8feae1311   David Howells   NOMMU: Make VMAs ...
132
133
134
135
   * display a single VMA to a sequenced file
   */
  static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
  {
3c26c9d95   Mike Frysinger   nommu: add '[stac...
136
  	struct mm_struct *mm = vma->vm_mm;
8feae1311   David Howells   NOMMU: Make VMAs ...
137
138
139
140
  	unsigned long ino = 0;
  	struct file *file;
  	dev_t dev = 0;
  	int flags, len;
6260a4b05   KAMEZAWA Hiroyuki   /proc/pid/maps: d...
141
  	unsigned long long pgoff = 0;
8feae1311   David Howells   NOMMU: Make VMAs ...
142
143
144
145
146
147
148
149
  
  	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;
4c967291f   Nobuhiro Iwamatsu   nommu: fix typo v...
150
  		pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
8feae1311   David Howells   NOMMU: Make VMAs ...
151
152
153
  	}
  
  	seq_printf(m,
33e5d7697   David Howells   nommu: fix a numb...
154
  		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
8feae1311   David Howells   NOMMU: Make VMAs ...
155
156
157
158
159
160
  		   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',
6260a4b05   KAMEZAWA Hiroyuki   /proc/pid/maps: d...
161
  		   pgoff,
8feae1311   David Howells   NOMMU: Make VMAs ...
162
163
164
  		   MAJOR(dev), MINOR(dev), ino, &len);
  
  	if (file) {
3c26c9d95   Mike Frysinger   nommu: add '[stac...
165
  		pad_len_spaces(m, len);
8feae1311   David Howells   NOMMU: Make VMAs ...
166
  		seq_path(m, &file->f_path, "");
3c26c9d95   Mike Frysinger   nommu: add '[stac...
167
168
169
170
171
172
  	} 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]");
  		}
8feae1311   David Howells   NOMMU: Make VMAs ...
173
174
175
176
177
178
179
180
  	}
  
  	seq_putc(m, '
  ');
  	return 0;
  }
  
  /*
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
181
   * display mapping lines for a particular process's /proc/pid/maps
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
   */
8feae1311   David Howells   NOMMU: Make VMAs ...
183
  static int show_map(struct seq_file *m, void *_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  {
8feae1311   David Howells   NOMMU: Make VMAs ...
185
  	struct rb_node *p = _p;
5096add84   Kees Cook   proc: maps protec...
186

8feae1311   David Howells   NOMMU: Make VMAs ...
187
  	return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
  }
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
189

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
  static void *m_start(struct seq_file *m, loff_t *pos)
  {
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
192
  	struct proc_maps_private *priv = m->private;
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
193
  	struct mm_struct *mm;
8feae1311   David Howells   NOMMU: Make VMAs ...
194
  	struct rb_node *p;
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
195
196
197
198
199
  	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)
ec6fd8a43   Al Viro   report errors in ...
200
  		return ERR_PTR(-ESRCH);
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
201

831830b5a   Al Viro   restrict reading ...
202
  	mm = mm_for_maps(priv->task);
ec6fd8a43   Al Viro   report errors in ...
203
  	if (!mm || IS_ERR(mm)) {
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
204
205
  		put_task_struct(priv->task);
  		priv->task = NULL;
ec6fd8a43   Al Viro   report errors in ...
206
  		return mm;
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
207
  	}
00f89d218   Oleg Nesterov   mm_for_maps: shif...
208
  	down_read(&mm->mmap_sem);
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
209

dbf8685c8   David Howells   [PATCH] NOMMU: Im...
210
  	/* start from the Nth VMA */
8feae1311   David Howells   NOMMU: Make VMAs ...
211
  	for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
212
  		if (n-- == 0)
8feae1311   David Howells   NOMMU: Make VMAs ...
213
  			return p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
  	return NULL;
  }
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
216
217
  
  static void m_stop(struct seq_file *m, void *_vml)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  {
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
219
220
221
222
223
224
225
226
  	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);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  }
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
228

8feae1311   David Howells   NOMMU: Make VMAs ...
229
  static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
  {
8feae1311   David Howells   NOMMU: Make VMAs ...
231
  	struct rb_node *p = _p;
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
232
233
  
  	(*pos)++;
8feae1311   David Howells   NOMMU: Make VMAs ...
234
  	return p ? rb_next(p) : NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  }
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
236

03a44825b   Jan Engelhardt   procfs: constify ...
237
  static const struct seq_operations proc_pid_maps_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
239
240
241
242
  	.start	= m_start,
  	.next	= m_next,
  	.stop	= m_stop,
  	.show	= show_map
  };
662795deb   Eric W. Biederman   [PATCH] proc: Mov...
243
244
245
  
  static int maps_open(struct inode *inode, struct file *file)
  {
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
246
247
248
249
250
251
252
253
254
255
256
257
258
  	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);
  		}
662795deb   Eric W. Biederman   [PATCH] proc: Mov...
259
260
261
  	}
  	return ret;
  }
00977a59b   Arjan van de Ven   [PATCH] mark stru...
262
  const struct file_operations proc_maps_operations = {
662795deb   Eric W. Biederman   [PATCH] proc: Mov...
263
264
265
  	.open		= maps_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
dbf8685c8   David Howells   [PATCH] NOMMU: Im...
266
  	.release	= seq_release_private,
662795deb   Eric W. Biederman   [PATCH] proc: Mov...
267
  };