blob: 791b2d400be52b9672454422c546816b79312c5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/mm.h>
2#include <linux/hugetlb.h>
3#include <linux/mount.h>
4#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07005#include <linux/highmem.h>
Kees Cook5096add2007-05-08 00:26:04 -07006#include <linux/ptrace.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07007#include <linux/pagemap.h>
8#include <linux/mempolicy.h>
Mauricio Line070ad42005-09-03 15:55:10 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/elf.h>
11#include <asm/uaccess.h>
Mauricio Line070ad42005-09-03 15:55:10 -070012#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "internal.h"
14
15char *task_mem(struct mm_struct *mm, char *buffer)
16{
17 unsigned long data, text, lib;
Hugh Dickins365e9c872005-10-29 18:16:18 -070018 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
19
20 /*
21 * Note: to minimize their overhead, mm maintains hiwater_vm and
22 * hiwater_rss only when about to *lower* total_vm or rss. Any
23 * collector of these hiwater stats must therefore get total_vm
24 * and rss too, which will usually be the higher. Barriers? not
25 * worth the effort, such snapshots can always be inconsistent.
26 */
27 hiwater_vm = total_vm = mm->total_vm;
28 if (hiwater_vm < mm->hiwater_vm)
29 hiwater_vm = mm->hiwater_vm;
30 hiwater_rss = total_rss = get_mm_rss(mm);
31 if (hiwater_rss < mm->hiwater_rss)
32 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
35 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
36 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
37 buffer += sprintf(buffer,
Hugh Dickins365e9c872005-10-29 18:16:18 -070038 "VmPeak:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 "VmSize:\t%8lu kB\n"
40 "VmLck:\t%8lu kB\n"
Hugh Dickins365e9c872005-10-29 18:16:18 -070041 "VmHWM:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 "VmRSS:\t%8lu kB\n"
43 "VmData:\t%8lu kB\n"
44 "VmStk:\t%8lu kB\n"
45 "VmExe:\t%8lu kB\n"
46 "VmLib:\t%8lu kB\n"
47 "VmPTE:\t%8lu kB\n",
Hugh Dickins365e9c872005-10-29 18:16:18 -070048 hiwater_vm << (PAGE_SHIFT-10),
49 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 mm->locked_vm << (PAGE_SHIFT-10),
Hugh Dickins365e9c872005-10-29 18:16:18 -070051 hiwater_rss << (PAGE_SHIFT-10),
52 total_rss << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 data << (PAGE_SHIFT-10),
54 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
55 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
56 return buffer;
57}
58
59unsigned long task_vsize(struct mm_struct *mm)
60{
61 return PAGE_SIZE * mm->total_vm;
62}
63
64int task_statm(struct mm_struct *mm, int *shared, int *text,
65 int *data, int *resident)
66{
Hugh Dickins42946212005-10-29 18:16:05 -070067 *shared = get_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
69 >> PAGE_SHIFT;
70 *data = mm->total_vm - mm->shared_vm;
Hugh Dickins42946212005-10-29 18:16:05 -070071 *resident = *shared + get_mm_counter(mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return mm->total_vm;
73}
74
75int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
76{
77 struct vm_area_struct * vma;
78 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -070079 struct task_struct *task = get_proc_task(inode);
80 struct mm_struct * mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Eric W. Biederman99f89552006-06-26 00:25:55 -070082 if (task) {
83 mm = get_task_mm(task);
84 put_task_struct(task);
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (!mm)
87 goto out;
88 down_read(&mm->mmap_sem);
89
90 vma = mm->mmap;
91 while (vma) {
92 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
93 break;
94 vma = vma->vm_next;
95 }
96
97 if (vma) {
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -080098 *mnt = mntget(vma->vm_file->f_path.mnt);
99 *dentry = dget(vma->vm_file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 result = 0;
101 }
102
103 up_read(&mm->mmap_sem);
104 mmput(mm);
105out:
106 return result;
107}
108
109static void pad_len_spaces(struct seq_file *m, int len)
110{
111 len = 25 + sizeof(void*) * 6 - len;
112 if (len < 1)
113 len = 1;
114 seq_printf(m, "%*c", len, ' ');
115}
116
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800117/*
118 * Proportional Set Size(PSS): my share of RSS.
119 *
120 * PSS of a process is the count of pages it has in memory, where each
121 * page is divided by the number of processes sharing it. So if a
122 * process has 1000 pages all to itself, and 1000 shared with one other
123 * process, its PSS will be 1500.
124 *
125 * To keep (accumulated) division errors low, we adopt a 64bit
126 * fixed-point pss counter to minimize division errors. So (pss >>
127 * PSS_SHIFT) would be the real byte count.
128 *
129 * A shift of 12 before division means (assuming 4K page size):
130 * - 1M 3-user-pages add up to 8KB errors;
131 * - supports mapcount up to 2^24, or 16M;
132 * - supports PSS up to 2^52 bytes, or 4PB.
133 */
134#define PSS_SHIFT 12
135
Mauricio Line070ad42005-09-03 15:55:10 -0700136struct mem_size_stats
137{
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800138 struct vm_area_struct *vma;
Mauricio Line070ad42005-09-03 15:55:10 -0700139 unsigned long resident;
140 unsigned long shared_clean;
141 unsigned long shared_dirty;
142 unsigned long private_clean;
143 unsigned long private_dirty;
David Rientjesf79f1772007-05-06 14:49:22 -0700144 unsigned long referenced;
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800145 u64 pss;
Mauricio Line070ad42005-09-03 15:55:10 -0700146};
147
148static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700150 struct proc_maps_private *priv = m->private;
151 struct task_struct *task = priv->task;
Mauricio Line070ad42005-09-03 15:55:10 -0700152 struct vm_area_struct *vma = v;
153 struct mm_struct *mm = vma->vm_mm;
154 struct file *file = vma->vm_file;
155 int flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned long ino = 0;
157 dev_t dev = 0;
158 int len;
159
Kees Cook5096add2007-05-08 00:26:04 -0700160 if (maps_protect && !ptrace_may_attach(task))
161 return -EACCES;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (file) {
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800164 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dev = inode->i_sb->s_dev;
166 ino = inode->i_ino;
167 }
168
169 seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
Mauricio Line070ad42005-09-03 15:55:10 -0700170 vma->vm_start,
171 vma->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 flags & VM_READ ? 'r' : '-',
173 flags & VM_WRITE ? 'w' : '-',
174 flags & VM_EXEC ? 'x' : '-',
175 flags & VM_MAYSHARE ? 's' : 'p',
Mauricio Line070ad42005-09-03 15:55:10 -0700176 vma->vm_pgoff << PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 MAJOR(dev), MINOR(dev), ino, &len);
178
179 /*
180 * Print the dentry name for named mappings, and a
181 * special [heap] marker for the heap:
182 */
Mauricio Line070ad42005-09-03 15:55:10 -0700183 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 pad_len_spaces(m, len);
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800185 seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } else {
Ingo Molnare6e54942006-06-27 02:53:50 -0700187 const char *name = arch_vma_name(vma);
188 if (!name) {
189 if (mm) {
190 if (vma->vm_start <= mm->start_brk &&
Mauricio Line070ad42005-09-03 15:55:10 -0700191 vma->vm_end >= mm->brk) {
Ingo Molnare6e54942006-06-27 02:53:50 -0700192 name = "[heap]";
193 } else if (vma->vm_start <= mm->start_stack &&
194 vma->vm_end >= mm->start_stack) {
195 name = "[stack]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700197 } else {
198 name = "[vdso]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700200 }
201 if (name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 pad_len_spaces(m, len);
Ingo Molnare6e54942006-06-27 02:53:50 -0700203 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 }
206 seq_putc(m, '\n');
Mauricio Line070ad42005-09-03 15:55:10 -0700207
208 if (mss)
209 seq_printf(m,
David Rientjesf79f1772007-05-06 14:49:22 -0700210 "Size: %8lu kB\n"
211 "Rss: %8lu kB\n"
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800212 "Pss: %8lu kB\n"
David Rientjesf79f1772007-05-06 14:49:22 -0700213 "Shared_Clean: %8lu kB\n"
214 "Shared_Dirty: %8lu kB\n"
215 "Private_Clean: %8lu kB\n"
216 "Private_Dirty: %8lu kB\n"
David Rientjesb813e932007-05-06 14:49:24 -0700217 "Referenced: %8lu kB\n",
Mauricio Line070ad42005-09-03 15:55:10 -0700218 (vma->vm_end - vma->vm_start) >> 10,
219 mss->resident >> 10,
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800220 (unsigned long)(mss->pss >> (10 + PSS_SHIFT)),
Mauricio Line070ad42005-09-03 15:55:10 -0700221 mss->shared_clean >> 10,
222 mss->shared_dirty >> 10,
223 mss->private_clean >> 10,
David Rientjesf79f1772007-05-06 14:49:22 -0700224 mss->private_dirty >> 10,
225 mss->referenced >> 10);
Mauricio Line070ad42005-09-03 15:55:10 -0700226
227 if (m->count < m->size) /* vma is copied successfully */
228 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
Mauricio Line070ad42005-09-03 15:55:10 -0700232static int show_map(struct seq_file *m, void *v)
233{
Luiz Fernando Capitulino0f5c79f2005-11-13 16:07:20 -0800234 return show_map_internal(m, v, NULL);
Mauricio Line070ad42005-09-03 15:55:10 -0700235}
236
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800237static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
238 void *private)
Mauricio Line070ad42005-09-03 15:55:10 -0700239{
David Rientjes826fad12007-05-06 14:49:21 -0700240 struct mem_size_stats *mss = private;
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800241 struct vm_area_struct *vma = mss->vma;
Mauricio Line070ad42005-09-03 15:55:10 -0700242 pte_t *pte, ptent;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700243 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700244 struct page *page;
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800245 int mapcount;
Mauricio Line070ad42005-09-03 15:55:10 -0700246
Hugh Dickins705e87c2005-10-29 18:16:27 -0700247 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
David Rientjes826fad12007-05-06 14:49:21 -0700248 for (; addr != end; pte++, addr += PAGE_SIZE) {
Mauricio Line070ad42005-09-03 15:55:10 -0700249 ptent = *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700250 if (!pte_present(ptent))
Mauricio Line070ad42005-09-03 15:55:10 -0700251 continue;
252
253 mss->resident += PAGE_SIZE;
Nick Pigginad820c5d2006-03-06 15:42:58 -0800254
255 page = vm_normal_page(vma, addr, ptent);
256 if (!page)
Mauricio Line070ad42005-09-03 15:55:10 -0700257 continue;
258
David Rientjesf79f1772007-05-06 14:49:22 -0700259 /* Accumulate the size in pages that have been accessed. */
260 if (pte_young(ptent) || PageReferenced(page))
261 mss->referenced += PAGE_SIZE;
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800262 mapcount = page_mapcount(page);
263 if (mapcount >= 2) {
Mauricio Line070ad42005-09-03 15:55:10 -0700264 if (pte_dirty(ptent))
265 mss->shared_dirty += PAGE_SIZE;
266 else
267 mss->shared_clean += PAGE_SIZE;
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800268 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
Mauricio Line070ad42005-09-03 15:55:10 -0700269 } else {
270 if (pte_dirty(ptent))
271 mss->private_dirty += PAGE_SIZE;
272 else
273 mss->private_clean += PAGE_SIZE;
Fengguang Wuec4dd3e2008-02-04 22:28:56 -0800274 mss->pss += (PAGE_SIZE << PSS_SHIFT);
Mauricio Line070ad42005-09-03 15:55:10 -0700275 }
David Rientjes826fad12007-05-06 14:49:21 -0700276 }
Hugh Dickins705e87c2005-10-29 18:16:27 -0700277 pte_unmap_unlock(pte - 1, ptl);
278 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800279 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700280}
281
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800282static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
283 unsigned long end, void *private)
David Rientjesb813e932007-05-06 14:49:24 -0700284{
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800285 struct vm_area_struct *vma = private;
David Rientjesb813e932007-05-06 14:49:24 -0700286 pte_t *pte, ptent;
287 spinlock_t *ptl;
288 struct page *page;
289
290 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
291 for (; addr != end; pte++, addr += PAGE_SIZE) {
292 ptent = *pte;
293 if (!pte_present(ptent))
294 continue;
295
296 page = vm_normal_page(vma, addr, ptent);
297 if (!page)
298 continue;
299
300 /* Clear accessed and referenced bits. */
301 ptep_test_and_clear_young(vma, addr, pte);
302 ClearPageReferenced(page);
303 }
304 pte_unmap_unlock(pte - 1, ptl);
305 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800306 return 0;
David Rientjesb813e932007-05-06 14:49:24 -0700307}
308
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800309static struct mm_walk smaps_walk = { .pmd_entry = smaps_pte_range };
Mauricio Line070ad42005-09-03 15:55:10 -0700310
311static int show_smap(struct seq_file *m, void *v)
312{
313 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700314 struct mem_size_stats mss;
315
316 memset(&mss, 0, sizeof mss);
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800317 mss.vma = vma;
Nick Piggin5ddfae12006-03-06 15:42:57 -0800318 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800319 walk_page_range(vma->vm_mm, vma->vm_start, vma->vm_end,
320 &smaps_walk, &mss);
Mauricio Line070ad42005-09-03 15:55:10 -0700321 return show_map_internal(m, v, &mss);
322}
323
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800324static struct mm_walk clear_refs_walk = { .pmd_entry = clear_refs_pte_range };
325
David Rientjesb813e932007-05-06 14:49:24 -0700326void clear_refs_smap(struct mm_struct *mm)
327{
328 struct vm_area_struct *vma;
329
330 down_read(&mm->mmap_sem);
331 for (vma = mm->mmap; vma; vma = vma->vm_next)
332 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800333 walk_page_range(vma->vm_mm, vma->vm_start, vma->vm_end,
334 &clear_refs_walk, vma);
David Rientjesb813e932007-05-06 14:49:24 -0700335 flush_tlb_mm(mm);
336 up_read(&mm->mmap_sem);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static void *m_start(struct seq_file *m, loff_t *pos)
340{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700341 struct proc_maps_private *priv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned long last_addr = m->version;
343 struct mm_struct *mm;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700344 struct vm_area_struct *vma, *tail_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 loff_t l = *pos;
346
Eric W. Biederman99f89552006-06-26 00:25:55 -0700347 /* Clear the per syscall fields in priv */
348 priv->task = NULL;
349 priv->tail_vma = NULL;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
352 * We remember last_addr rather than next_addr to hit with
353 * mmap_cache most of the time. We have zero last_addr at
Mauricio Line070ad42005-09-03 15:55:10 -0700354 * the beginning and also after lseek. We will have -1 last_addr
355 * after the end of the vmas.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357
358 if (last_addr == -1UL)
359 return NULL;
360
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700361 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700362 if (!priv->task)
363 return NULL;
364
Al Viro831830b2008-01-02 14:09:57 +0000365 mm = mm_for_maps(priv->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!mm)
367 return NULL;
368
Eric W. Biederman99f89552006-06-26 00:25:55 -0700369 priv->tail_vma = tail_vma = get_gate_vma(priv->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* Start with last addr hint */
Mauricio Line070ad42005-09-03 15:55:10 -0700372 if (last_addr && (vma = find_vma(mm, last_addr))) {
373 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto out;
375 }
376
377 /*
Mauricio Line070ad42005-09-03 15:55:10 -0700378 * Check the vma index is within the range and do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * sequential scan until m_index.
380 */
Mauricio Line070ad42005-09-03 15:55:10 -0700381 vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if ((unsigned long)l < mm->map_count) {
Mauricio Line070ad42005-09-03 15:55:10 -0700383 vma = mm->mmap;
384 while (l-- && vma)
385 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto out;
387 }
388
389 if (l != mm->map_count)
Mauricio Line070ad42005-09-03 15:55:10 -0700390 tail_vma = NULL; /* After gate vma */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392out:
Mauricio Line070ad42005-09-03 15:55:10 -0700393 if (vma)
394 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Mauricio Line070ad42005-09-03 15:55:10 -0700396 /* End of vmas has been reached */
397 m->version = (tail_vma != NULL)? 0: -1UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 up_read(&mm->mmap_sem);
399 mmput(mm);
Mauricio Line070ad42005-09-03 15:55:10 -0700400 return tail_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Eric W. Biederman99f89552006-06-26 00:25:55 -0700403static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700405 if (vma && vma != priv->tail_vma) {
Mauricio Line070ad42005-09-03 15:55:10 -0700406 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 up_read(&mm->mmap_sem);
408 mmput(mm);
409 }
410}
411
412static void *m_next(struct seq_file *m, void *v, loff_t *pos)
413{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700414 struct proc_maps_private *priv = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700415 struct vm_area_struct *vma = v;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700416 struct vm_area_struct *tail_vma = priv->tail_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 (*pos)++;
Mauricio Line070ad42005-09-03 15:55:10 -0700419 if (vma && (vma != tail_vma) && vma->vm_next)
420 return vma->vm_next;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700421 vma_stop(priv, vma);
Mauricio Line070ad42005-09-03 15:55:10 -0700422 return (vma != tail_vma)? tail_vma: NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Eric W. Biederman99f89552006-06-26 00:25:55 -0700425static void m_stop(struct seq_file *m, void *v)
426{
427 struct proc_maps_private *priv = m->private;
428 struct vm_area_struct *vma = v;
429
430 vma_stop(priv, vma);
431 if (priv->task)
432 put_task_struct(priv->task);
433}
434
Eric W. Biederman662795d2006-06-26 00:25:48 -0700435static struct seq_operations proc_pid_maps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .start = m_start,
437 .next = m_next,
438 .stop = m_stop,
439 .show = show_map
440};
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700441
Eric W. Biederman662795d2006-06-26 00:25:48 -0700442static struct seq_operations proc_pid_smaps_op = {
Mauricio Line070ad42005-09-03 15:55:10 -0700443 .start = m_start,
444 .next = m_next,
445 .stop = m_stop,
446 .show = show_smap
447};
448
Eric W. Biederman662795d2006-06-26 00:25:48 -0700449static int do_maps_open(struct inode *inode, struct file *file,
450 struct seq_operations *ops)
451{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700452 struct proc_maps_private *priv;
453 int ret = -ENOMEM;
454 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
455 if (priv) {
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700456 priv->pid = proc_pid(inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700457 ret = seq_open(file, ops);
458 if (!ret) {
459 struct seq_file *m = file->private_data;
460 m->private = priv;
461 } else {
462 kfree(priv);
463 }
Eric W. Biederman662795d2006-06-26 00:25:48 -0700464 }
465 return ret;
466}
467
468static int maps_open(struct inode *inode, struct file *file)
469{
470 return do_maps_open(inode, file, &proc_pid_maps_op);
471}
472
Arjan van de Ven00977a52007-02-12 00:55:34 -0800473const struct file_operations proc_maps_operations = {
Eric W. Biederman662795d2006-06-26 00:25:48 -0700474 .open = maps_open,
475 .read = seq_read,
476 .llseek = seq_lseek,
Eric W. Biederman99f89552006-06-26 00:25:55 -0700477 .release = seq_release_private,
Eric W. Biederman662795d2006-06-26 00:25:48 -0700478};
479
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700480#ifdef CONFIG_NUMA
Christoph Lameter1a75a6c2006-01-08 01:01:02 -0800481extern int show_numa_map(struct seq_file *m, void *v);
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700482
Kees Cook5096add2007-05-08 00:26:04 -0700483static int show_numa_map_checked(struct seq_file *m, void *v)
484{
485 struct proc_maps_private *priv = m->private;
486 struct task_struct *task = priv->task;
487
488 if (maps_protect && !ptrace_may_attach(task))
489 return -EACCES;
490
491 return show_numa_map(m, v);
492}
493
Eric W. Biederman662795d2006-06-26 00:25:48 -0700494static struct seq_operations proc_pid_numa_maps_op = {
Christoph Lameter1a75a6c2006-01-08 01:01:02 -0800495 .start = m_start,
496 .next = m_next,
497 .stop = m_stop,
Kees Cook5096add2007-05-08 00:26:04 -0700498 .show = show_numa_map_checked
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700499};
Eric W. Biederman662795d2006-06-26 00:25:48 -0700500
501static int numa_maps_open(struct inode *inode, struct file *file)
502{
503 return do_maps_open(inode, file, &proc_pid_numa_maps_op);
504}
505
Arjan van de Ven00977a52007-02-12 00:55:34 -0800506const struct file_operations proc_numa_maps_operations = {
Eric W. Biederman662795d2006-06-26 00:25:48 -0700507 .open = numa_maps_open,
508 .read = seq_read,
509 .llseek = seq_lseek,
Eric W. Biederman99f89552006-06-26 00:25:55 -0700510 .release = seq_release_private,
Eric W. Biederman662795d2006-06-26 00:25:48 -0700511};
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700512#endif
Eric W. Biederman662795d2006-06-26 00:25:48 -0700513
514static int smaps_open(struct inode *inode, struct file *file)
515{
516 return do_maps_open(inode, file, &proc_pid_smaps_op);
517}
518
Arjan van de Ven00977a52007-02-12 00:55:34 -0800519const struct file_operations proc_smaps_operations = {
Eric W. Biederman662795d2006-06-26 00:25:48 -0700520 .open = smaps_open,
521 .read = seq_read,
522 .llseek = seq_lseek,
Eric W. Biederman99f89552006-06-26 00:25:55 -0700523 .release = seq_release_private,
Eric W. Biederman662795d2006-06-26 00:25:48 -0700524};