2 #include <linux/hugetlb.h>
3 #include <linux/mount.h>
4 #include <linux/seq_file.h>
5 #include <linux/highmem.h>
6 #include <linux/ptrace.h>
7 #include <linux/pagemap.h>
8 #include <linux/mempolicy.h>
11 #include <asm/uaccess.h>
12 #include <asm/tlbflush.h>
15 char *task_mem(struct mm_struct
*mm
, char *buffer
)
17 unsigned long data
, text
, lib
;
18 unsigned long hiwater_vm
, total_vm
, hiwater_rss
, total_rss
;
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.
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
;
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
,
48 hiwater_vm
<< (PAGE_SHIFT
-10),
49 (total_vm
- mm
->reserved_vm
) << (PAGE_SHIFT
-10),
50 mm
->locked_vm
<< (PAGE_SHIFT
-10),
51 hiwater_rss
<< (PAGE_SHIFT
-10),
52 total_rss
<< (PAGE_SHIFT
-10),
53 data
<< (PAGE_SHIFT
-10),
54 mm
->stack_vm
<< (PAGE_SHIFT
-10), text
, lib
,
55 (PTRS_PER_PTE
*sizeof(pte_t
)*mm
->nr_ptes
) >> 10);
59 unsigned long task_vsize(struct mm_struct
*mm
)
61 return PAGE_SIZE
* mm
->total_vm
;
64 int task_statm(struct mm_struct
*mm
, int *shared
, int *text
,
65 int *data
, int *resident
)
67 *shared
= get_mm_counter(mm
, file_rss
);
68 *text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
))
70 *data
= mm
->total_vm
- mm
->shared_vm
;
71 *resident
= *shared
+ get_mm_counter(mm
, anon_rss
);
75 int proc_exe_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
77 struct vm_area_struct
* vma
;
79 struct task_struct
*task
= get_proc_task(inode
);
80 struct mm_struct
* mm
= NULL
;
83 mm
= get_task_mm(task
);
84 put_task_struct(task
);
88 down_read(&mm
->mmap_sem
);
92 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
)
98 *mnt
= mntget(vma
->vm_file
->f_path
.mnt
);
99 *dentry
= dget(vma
->vm_file
->f_path
.dentry
);
103 up_read(&mm
->mmap_sem
);
109 static void pad_len_spaces(struct seq_file
*m
, int len
)
111 len
= 25 + sizeof(void*) * 6 - len
;
114 seq_printf(m
, "%*c", len
, ' ');
117 struct mem_size_stats
119 unsigned long resident
;
120 unsigned long shared_clean
;
121 unsigned long shared_dirty
;
122 unsigned long private_clean
;
123 unsigned long private_dirty
;
124 unsigned long referenced
;
128 struct vm_area_struct
*vma
;
130 void (*action
)(struct vm_area_struct
*, pmd_t
*, unsigned long,
131 unsigned long, void *);
134 static int show_map_internal(struct seq_file
*m
, void *v
, struct mem_size_stats
*mss
)
136 struct proc_maps_private
*priv
= m
->private;
137 struct task_struct
*task
= priv
->task
;
138 struct vm_area_struct
*vma
= v
;
139 struct mm_struct
*mm
= vma
->vm_mm
;
140 struct file
*file
= vma
->vm_file
;
141 int flags
= vma
->vm_flags
;
142 unsigned long ino
= 0;
146 if (maps_protect
&& !ptrace_may_attach(task
))
150 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
151 dev
= inode
->i_sb
->s_dev
;
155 seq_printf(m
, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
158 flags
& VM_READ
? 'r' : '-',
159 flags
& VM_WRITE
? 'w' : '-',
160 flags
& VM_EXEC
? 'x' : '-',
161 flags
& VM_MAYSHARE
? 's' : 'p',
162 vma
->vm_pgoff
<< PAGE_SHIFT
,
163 MAJOR(dev
), MINOR(dev
), ino
, &len
);
166 * Print the dentry name for named mappings, and a
167 * special [heap] marker for the heap:
170 pad_len_spaces(m
, len
);
171 seq_path(m
, file
->f_path
.mnt
, file
->f_path
.dentry
, "\n");
173 const char *name
= arch_vma_name(vma
);
176 if (vma
->vm_start
<= mm
->start_brk
&&
177 vma
->vm_end
>= mm
->brk
) {
179 } else if (vma
->vm_start
<= mm
->start_stack
&&
180 vma
->vm_end
>= mm
->start_stack
) {
188 pad_len_spaces(m
, len
);
198 "Shared_Clean: %8lu kB\n"
199 "Shared_Dirty: %8lu kB\n"
200 "Private_Clean: %8lu kB\n"
201 "Private_Dirty: %8lu kB\n"
202 "Referenced: %8lu kB\n",
203 (vma
->vm_end
- vma
->vm_start
) >> 10,
205 mss
->shared_clean
>> 10,
206 mss
->shared_dirty
>> 10,
207 mss
->private_clean
>> 10,
208 mss
->private_dirty
>> 10,
209 mss
->referenced
>> 10);
211 if (m
->count
< m
->size
) /* vma is copied successfully */
212 m
->version
= (vma
!= get_gate_vma(task
))? vma
->vm_start
: 0;
216 static int show_map(struct seq_file
*m
, void *v
)
218 return show_map_internal(m
, v
, NULL
);
221 static void smaps_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
222 unsigned long addr
, unsigned long end
,
225 struct mem_size_stats
*mss
= private;
230 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
231 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
233 if (!pte_present(ptent
))
236 mss
->resident
+= PAGE_SIZE
;
238 page
= vm_normal_page(vma
, addr
, ptent
);
242 /* Accumulate the size in pages that have been accessed. */
243 if (pte_young(ptent
) || PageReferenced(page
))
244 mss
->referenced
+= PAGE_SIZE
;
245 if (page_mapcount(page
) >= 2) {
246 if (pte_dirty(ptent
))
247 mss
->shared_dirty
+= PAGE_SIZE
;
249 mss
->shared_clean
+= PAGE_SIZE
;
251 if (pte_dirty(ptent
))
252 mss
->private_dirty
+= PAGE_SIZE
;
254 mss
->private_clean
+= PAGE_SIZE
;
257 pte_unmap_unlock(pte
- 1, ptl
);
261 static void clear_refs_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
262 unsigned long addr
, unsigned long end
,
269 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
270 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
272 if (!pte_present(ptent
))
275 page
= vm_normal_page(vma
, addr
, ptent
);
279 /* Clear accessed and referenced bits. */
280 ptep_test_and_clear_young(vma
, addr
, pte
);
281 ClearPageReferenced(page
);
283 pte_unmap_unlock(pte
- 1, ptl
);
287 static inline void walk_pmd_range(struct pmd_walker
*walker
, pud_t
*pud
,
288 unsigned long addr
, unsigned long end
)
293 for (pmd
= pmd_offset(pud
, addr
); addr
!= end
;
294 pmd
++, addr
= next
) {
295 next
= pmd_addr_end(addr
, end
);
296 if (pmd_none_or_clear_bad(pmd
))
298 walker
->action(walker
->vma
, pmd
, addr
, next
, walker
->private);
302 static inline void walk_pud_range(struct pmd_walker
*walker
, pgd_t
*pgd
,
303 unsigned long addr
, unsigned long end
)
308 for (pud
= pud_offset(pgd
, addr
); addr
!= end
;
309 pud
++, addr
= next
) {
310 next
= pud_addr_end(addr
, end
);
311 if (pud_none_or_clear_bad(pud
))
313 walk_pmd_range(walker
, pud
, addr
, next
);
318 * walk_page_range - walk the page tables of a VMA with a callback
320 * @action - callback invoked for every bottom-level (PTE) page table
321 * @private - private data passed to the callback function
323 * Recursively walk the page table for the memory area in a VMA, calling
324 * a callback for every bottom-level (PTE) page table.
326 static inline void walk_page_range(struct vm_area_struct
*vma
,
327 void (*action
)(struct vm_area_struct
*,
328 pmd_t
*, unsigned long,
329 unsigned long, void *),
332 unsigned long addr
= vma
->vm_start
;
333 unsigned long end
= vma
->vm_end
;
334 struct pmd_walker walker
= {
342 for (pgd
= pgd_offset(vma
->vm_mm
, addr
); addr
!= end
;
343 pgd
++, addr
= next
) {
344 next
= pgd_addr_end(addr
, end
);
345 if (pgd_none_or_clear_bad(pgd
))
347 walk_pud_range(&walker
, pgd
, addr
, next
);
351 static int show_smap(struct seq_file
*m
, void *v
)
353 struct vm_area_struct
*vma
= v
;
354 struct mem_size_stats mss
;
356 memset(&mss
, 0, sizeof mss
);
357 if (vma
->vm_mm
&& !is_vm_hugetlb_page(vma
))
358 walk_page_range(vma
, smaps_pte_range
, &mss
);
359 return show_map_internal(m
, v
, &mss
);
362 void clear_refs_smap(struct mm_struct
*mm
)
364 struct vm_area_struct
*vma
;
366 down_read(&mm
->mmap_sem
);
367 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
)
368 if (vma
->vm_mm
&& !is_vm_hugetlb_page(vma
))
369 walk_page_range(vma
, clear_refs_pte_range
, NULL
);
371 up_read(&mm
->mmap_sem
);
374 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
376 struct proc_maps_private
*priv
= m
->private;
377 unsigned long last_addr
= m
->version
;
378 struct mm_struct
*mm
;
379 struct vm_area_struct
*vma
, *tail_vma
= NULL
;
382 /* Clear the per syscall fields in priv */
384 priv
->tail_vma
= NULL
;
387 * We remember last_addr rather than next_addr to hit with
388 * mmap_cache most of the time. We have zero last_addr at
389 * the beginning and also after lseek. We will have -1 last_addr
390 * after the end of the vmas.
393 if (last_addr
== -1UL)
396 priv
->task
= get_pid_task(priv
->pid
, PIDTYPE_PID
);
400 mm
= mm_for_maps(priv
->task
);
404 priv
->tail_vma
= tail_vma
= get_gate_vma(priv
->task
);
406 /* Start with last addr hint */
407 if (last_addr
&& (vma
= find_vma(mm
, last_addr
))) {
413 * Check the vma index is within the range and do
414 * sequential scan until m_index.
417 if ((unsigned long)l
< mm
->map_count
) {
424 if (l
!= mm
->map_count
)
425 tail_vma
= NULL
; /* After gate vma */
431 /* End of vmas has been reached */
432 m
->version
= (tail_vma
!= NULL
)? 0: -1UL;
433 up_read(&mm
->mmap_sem
);
438 static void vma_stop(struct proc_maps_private
*priv
, struct vm_area_struct
*vma
)
440 if (vma
&& vma
!= priv
->tail_vma
) {
441 struct mm_struct
*mm
= vma
->vm_mm
;
442 up_read(&mm
->mmap_sem
);
447 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
449 struct proc_maps_private
*priv
= m
->private;
450 struct vm_area_struct
*vma
= v
;
451 struct vm_area_struct
*tail_vma
= priv
->tail_vma
;
454 if (vma
&& (vma
!= tail_vma
) && vma
->vm_next
)
457 return (vma
!= tail_vma
)? tail_vma
: NULL
;
460 static void m_stop(struct seq_file
*m
, void *v
)
462 struct proc_maps_private
*priv
= m
->private;
463 struct vm_area_struct
*vma
= v
;
467 put_task_struct(priv
->task
);
470 static struct seq_operations proc_pid_maps_op
= {
477 static struct seq_operations proc_pid_smaps_op
= {
484 static int do_maps_open(struct inode
*inode
, struct file
*file
,
485 struct seq_operations
*ops
)
487 struct proc_maps_private
*priv
;
489 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
491 priv
->pid
= proc_pid(inode
);
492 ret
= seq_open(file
, ops
);
494 struct seq_file
*m
= file
->private_data
;
503 static int maps_open(struct inode
*inode
, struct file
*file
)
505 return do_maps_open(inode
, file
, &proc_pid_maps_op
);
508 const struct file_operations proc_maps_operations
= {
512 .release
= seq_release_private
,
516 extern int show_numa_map(struct seq_file
*m
, void *v
);
518 static int show_numa_map_checked(struct seq_file
*m
, void *v
)
520 struct proc_maps_private
*priv
= m
->private;
521 struct task_struct
*task
= priv
->task
;
523 if (maps_protect
&& !ptrace_may_attach(task
))
526 return show_numa_map(m
, v
);
529 static struct seq_operations proc_pid_numa_maps_op
= {
533 .show
= show_numa_map_checked
536 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
538 return do_maps_open(inode
, file
, &proc_pid_numa_maps_op
);
541 const struct file_operations proc_numa_maps_operations
= {
542 .open
= numa_maps_open
,
545 .release
= seq_release_private
,
549 static int smaps_open(struct inode
*inode
, struct file
*file
)
551 return do_maps_open(inode
, file
, &proc_pid_smaps_op
);
554 const struct file_operations proc_smaps_operations
= {
558 .release
= seq_release_private
,