2 #include <linux/hugetlb.h>
3 #include <linux/mount.h>
4 #include <linux/seq_file.h>
5 #include <linux/highmem.h>
6 #include <linux/pagemap.h>
7 #include <linux/mempolicy.h>
10 #include <asm/uaccess.h>
11 #include <asm/tlbflush.h>
14 char *task_mem(struct mm_struct *mm, char *buffer)
16 unsigned long data, text, lib;
17 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
20 * Note: to minimize their overhead, mm maintains hiwater_vm and
21 * hiwater_rss only when about to *lower* total_vm or rss. Any
22 * collector of these hiwater stats must therefore get total_vm
23 * and rss too, which will usually be the higher. Barriers? not
24 * worth the effort, such snapshots can always be inconsistent.
26 hiwater_vm = total_vm = mm->total_vm;
27 if (hiwater_vm < mm->hiwater_vm)
28 hiwater_vm = mm->hiwater_vm;
29 hiwater_rss = total_rss = get_mm_rss(mm);
30 if (hiwater_rss < mm->hiwater_rss)
31 hiwater_rss = mm->hiwater_rss;
33 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
34 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
35 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
36 buffer += sprintf(buffer,
51 hiwater_vm << (PAGE_SHIFT-10),
52 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
53 mm->locked_vm << (PAGE_SHIFT-10),
54 hiwater_rss << (PAGE_SHIFT-10),
55 total_rss << (PAGE_SHIFT-10),
56 data << (PAGE_SHIFT-10),
57 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
58 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
59 mm->start_brk, mm->brk, mm->start_stack);
62 buffer += sprintf(buffer,
63 "ExecLim:\t%08lx\n", mm->context.exec_limit);
68 unsigned long task_vsize(struct mm_struct *mm)
70 return PAGE_SIZE * mm->total_vm;
73 int task_statm(struct mm_struct *mm, int *shared, int *text,
74 int *data, int *resident)
76 *shared = get_mm_counter(mm, file_rss);
77 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
79 *data = mm->total_vm - mm->shared_vm;
80 *resident = *shared + get_mm_counter(mm, anon_rss);
84 int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
86 struct vm_area_struct * vma;
88 struct task_struct *task = get_proc_task(inode);
89 struct mm_struct * mm = NULL;
92 mm = get_task_mm(task);
93 put_task_struct(task);
97 down_read(&mm->mmap_sem);
101 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
107 *mnt = mntget(vma->vm_file->f_path.mnt);
108 *dentry = dget(vma->vm_file->f_path.dentry);
112 up_read(&mm->mmap_sem);
118 static void pad_len_spaces(struct seq_file *m, int len)
120 len = 25 + sizeof(void*) * 6 - len;
123 seq_printf(m, "%*c", len, ' ');
126 struct mem_size_stats
128 unsigned long resident;
129 unsigned long shared_clean;
130 unsigned long shared_dirty;
131 unsigned long private_clean;
132 unsigned long private_dirty;
135 static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
137 struct proc_maps_private *priv = m->private;
138 struct task_struct *task = priv->task;
140 struct mm_struct *tmm = get_task_mm(task);
142 struct vm_area_struct *vma = v;
143 struct mm_struct *mm = vma->vm_mm;
144 struct file *file = vma->vm_file;
145 int flags = vma->vm_flags;
146 unsigned long ino = 0;
151 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
152 dev = inode->i_sb->s_dev;
156 seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
159 flags & VM_READ ? 'r' : '-',
160 flags & VM_WRITE ? 'w' : '-',
163 || (!nx_enabled && tmm &&
164 (vma->vm_start < tmm->context.exec_limit))
168 flags & VM_MAYSHARE ? 's' : 'p',
169 vma->vm_pgoff << PAGE_SHIFT,
170 MAJOR(dev), MINOR(dev), ino, &len);
177 * Print the dentry name for named mappings, and a
178 * special [heap] marker for the heap:
181 pad_len_spaces(m, len);
182 seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n");
184 const char *name = arch_vma_name(vma);
187 if (vma->vm_start <= mm->start_brk &&
188 vma->vm_end >= mm->brk) {
190 } else if (vma->vm_start <= mm->start_stack &&
191 vma->vm_end >= mm->start_stack) {
199 pad_len_spaces(m, len);
209 "Shared_Clean: %8lu kB\n"
210 "Shared_Dirty: %8lu kB\n"
211 "Private_Clean: %8lu kB\n"
212 "Private_Dirty: %8lu kB\n",
213 (vma->vm_end - vma->vm_start) >> 10,
215 mss->shared_clean >> 10,
216 mss->shared_dirty >> 10,
217 mss->private_clean >> 10,
218 mss->private_dirty >> 10);
220 if (m->count < m->size) /* vma is copied successfully */
221 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
225 static int show_map(struct seq_file *m, void *v)
227 return show_map_internal(m, v, NULL);
230 static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
231 unsigned long addr, unsigned long end,
232 struct mem_size_stats *mss)
238 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
241 if (!pte_present(ptent))
244 mss->resident += PAGE_SIZE;
246 page = vm_normal_page(vma, addr, ptent);
250 if (page_mapcount(page) >= 2) {
251 if (pte_dirty(ptent))
252 mss->shared_dirty += PAGE_SIZE;
254 mss->shared_clean += PAGE_SIZE;
256 if (pte_dirty(ptent))
257 mss->private_dirty += PAGE_SIZE;
259 mss->private_clean += PAGE_SIZE;
261 } while (pte++, addr += PAGE_SIZE, addr != end);
262 pte_unmap_unlock(pte - 1, ptl);
266 static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
267 unsigned long addr, unsigned long end,
268 struct mem_size_stats *mss)
273 pmd = pmd_offset(pud, addr);
275 next = pmd_addr_end(addr, end);
276 if (pmd_none_or_clear_bad(pmd))
278 smaps_pte_range(vma, pmd, addr, next, mss);
279 } while (pmd++, addr = next, addr != end);
282 static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
283 unsigned long addr, unsigned long end,
284 struct mem_size_stats *mss)
289 pud = pud_offset(pgd, addr);
291 next = pud_addr_end(addr, end);
292 if (pud_none_or_clear_bad(pud))
294 smaps_pmd_range(vma, pud, addr, next, mss);
295 } while (pud++, addr = next, addr != end);
298 static inline void smaps_pgd_range(struct vm_area_struct *vma,
299 unsigned long addr, unsigned long end,
300 struct mem_size_stats *mss)
305 pgd = pgd_offset(vma->vm_mm, addr);
307 next = pgd_addr_end(addr, end);
308 if (pgd_none_or_clear_bad(pgd))
310 smaps_pud_range(vma, pgd, addr, next, mss);
311 } while (pgd++, addr = next, addr != end);
314 static int show_smap(struct seq_file *m, void *v)
316 struct vm_area_struct *vma = v;
317 struct mem_size_stats mss;
319 memset(&mss, 0, sizeof mss);
320 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
321 smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
322 return show_map_internal(m, v, &mss);
325 static void *m_start(struct seq_file *m, loff_t *pos)
327 struct proc_maps_private *priv = m->private;
328 unsigned long last_addr = m->version;
329 struct mm_struct *mm;
330 struct vm_area_struct *vma, *tail_vma = NULL;
333 /* Clear the per syscall fields in priv */
335 priv->tail_vma = NULL;
338 * We remember last_addr rather than next_addr to hit with
339 * mmap_cache most of the time. We have zero last_addr at
340 * the beginning and also after lseek. We will have -1 last_addr
341 * after the end of the vmas.
344 if (last_addr == -1UL)
347 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
351 mm = mm_for_maps(priv->task);
355 priv->tail_vma = tail_vma = get_gate_vma(priv->task);
357 /* Start with last addr hint */
358 if (last_addr && (vma = find_vma(mm, last_addr))) {
364 * Check the vma index is within the range and do
365 * sequential scan until m_index.
368 if ((unsigned long)l < mm->map_count) {
375 if (l != mm->map_count)
376 tail_vma = NULL; /* After gate vma */
382 /* End of vmas has been reached */
383 m->version = (tail_vma != NULL)? 0: -1UL;
384 up_read(&mm->mmap_sem);
389 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
391 if (vma && vma != priv->tail_vma) {
392 struct mm_struct *mm = vma->vm_mm;
393 up_read(&mm->mmap_sem);
398 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
400 struct proc_maps_private *priv = m->private;
401 struct vm_area_struct *vma = v;
402 struct vm_area_struct *tail_vma = priv->tail_vma;
405 if (vma && (vma != tail_vma) && vma->vm_next)
408 return (vma != tail_vma)? tail_vma: NULL;
411 static void m_stop(struct seq_file *m, void *v)
413 struct proc_maps_private *priv = m->private;
414 struct vm_area_struct *vma = v;
418 put_task_struct(priv->task);
421 static struct seq_operations proc_pid_maps_op = {
428 static struct seq_operations proc_pid_smaps_op = {
435 static int do_maps_open(struct inode *inode, struct file *file,
436 struct seq_operations *ops)
438 struct proc_maps_private *priv;
440 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
442 priv->pid = proc_pid(inode);
443 ret = seq_open(file, ops);
445 struct seq_file *m = file->private_data;
454 static int maps_open(struct inode *inode, struct file *file)
456 return do_maps_open(inode, file, &proc_pid_maps_op);
459 struct file_operations proc_maps_operations = {
463 .release = seq_release_private,
467 extern int show_numa_map(struct seq_file *m, void *v);
469 static struct seq_operations proc_pid_numa_maps_op = {
473 .show = show_numa_map
476 static int numa_maps_open(struct inode *inode, struct file *file)
478 return do_maps_open(inode, file, &proc_pid_numa_maps_op);
481 struct file_operations proc_numa_maps_operations = {
482 .open = numa_maps_open,
485 .release = seq_release_private,
489 static int smaps_open(struct inode *inode, struct file *file)
491 return do_maps_open(inode, file, &proc_pid_smaps_op);
494 struct file_operations proc_smaps_operations = {
498 .release = seq_release_private,