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
,
47 hiwater_vm
<< (PAGE_SHIFT
-10),
48 (total_vm
- mm
->reserved_vm
) << (PAGE_SHIFT
-10),
49 mm
->locked_vm
<< (PAGE_SHIFT
-10),
50 hiwater_rss
<< (PAGE_SHIFT
-10),
51 total_rss
<< (PAGE_SHIFT
-10),
52 data
<< (PAGE_SHIFT
-10),
53 mm
->stack_vm
<< (PAGE_SHIFT
-10), text
, lib
,
54 (PTRS_PER_PTE
*sizeof(pte_t
)*mm
->nr_ptes
) >> 10);
58 unsigned long task_vsize(struct mm_struct
*mm
)
60 return PAGE_SIZE
* mm
->total_vm
;
63 int task_statm(struct mm_struct
*mm
, int *shared
, int *text
,
64 int *data
, int *resident
)
66 *shared
= get_mm_counter(mm
, file_rss
);
67 *text
= (PAGE_ALIGN(mm
->end_code
) - (mm
->start_code
& PAGE_MASK
))
69 *data
= mm
->total_vm
- mm
->shared_vm
;
70 *resident
= *shared
+ get_mm_counter(mm
, anon_rss
);
74 int proc_exe_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
76 struct vm_area_struct
* vma
;
78 struct task_struct
*task
= get_proc_task(inode
);
79 struct mm_struct
* mm
= NULL
;
82 mm
= get_task_mm(task
);
83 put_task_struct(task
);
87 down_read(&mm
->mmap_sem
);
91 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
)
97 *mnt
= mntget(vma
->vm_file
->f_path
.mnt
);
98 *dentry
= dget(vma
->vm_file
->f_path
.dentry
);
102 up_read(&mm
->mmap_sem
);
108 static void pad_len_spaces(struct seq_file
*m
, int len
)
110 len
= 25 + sizeof(void*) * 6 - len
;
113 seq_printf(m
, "%*c", len
, ' ');
116 struct mem_size_stats
118 unsigned long resident
;
119 unsigned long shared_clean
;
120 unsigned long shared_dirty
;
121 unsigned long private_clean
;
122 unsigned long private_dirty
;
123 unsigned long referenced
;
127 struct vm_area_struct
*vma
;
129 void (*action
)(struct vm_area_struct
*, pmd_t
*, unsigned long,
130 unsigned long, void *);
133 static int show_map_internal(struct seq_file
*m
, void *v
, struct mem_size_stats
*mss
)
135 struct proc_maps_private
*priv
= m
->private;
136 struct task_struct
*task
= priv
->task
;
137 struct vm_area_struct
*vma
= v
;
138 struct mm_struct
*mm
= vma
->vm_mm
;
139 struct file
*file
= vma
->vm_file
;
140 int flags
= vma
->vm_flags
;
141 unsigned long ino
= 0;
146 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
147 dev
= inode
->i_sb
->s_dev
;
151 seq_printf(m
, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
154 flags
& VM_READ
? 'r' : '-',
155 flags
& VM_WRITE
? 'w' : '-',
156 flags
& VM_EXEC
? 'x' : '-',
157 flags
& VM_MAYSHARE
? 's' : 'p',
158 vma
->vm_pgoff
<< PAGE_SHIFT
,
159 MAJOR(dev
), MINOR(dev
), ino
, &len
);
162 * Print the dentry name for named mappings, and a
163 * special [heap] marker for the heap:
166 pad_len_spaces(m
, len
);
167 seq_path(m
, file
->f_path
.mnt
, file
->f_path
.dentry
, "\n");
169 const char *name
= arch_vma_name(vma
);
172 if (vma
->vm_start
<= mm
->start_brk
&&
173 vma
->vm_end
>= mm
->brk
) {
175 } else if (vma
->vm_start
<= mm
->start_stack
&&
176 vma
->vm_end
>= mm
->start_stack
) {
184 pad_len_spaces(m
, len
);
194 "Shared_Clean: %8lu kB\n"
195 "Shared_Dirty: %8lu kB\n"
196 "Private_Clean: %8lu kB\n"
197 "Private_Dirty: %8lu kB\n"
198 "Referenced: %8lu kB\n",
199 (vma
->vm_end
- vma
->vm_start
) >> 10,
201 mss
->shared_clean
>> 10,
202 mss
->shared_dirty
>> 10,
203 mss
->private_clean
>> 10,
204 mss
->private_dirty
>> 10,
205 mss
->referenced
>> 10);
207 if (m
->count
< m
->size
) /* vma is copied successfully */
208 m
->version
= (vma
!= get_gate_vma(task
))? vma
->vm_start
: 0;
212 static int show_map(struct seq_file
*m
, void *v
)
214 return show_map_internal(m
, v
, NULL
);
217 static void smaps_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
218 unsigned long addr
, unsigned long end
,
221 struct mem_size_stats
*mss
= private;
226 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
227 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
229 if (!pte_present(ptent
))
232 mss
->resident
+= PAGE_SIZE
;
234 page
= vm_normal_page(vma
, addr
, ptent
);
238 /* Accumulate the size in pages that have been accessed. */
239 if (pte_young(ptent
) || PageReferenced(page
))
240 mss
->referenced
+= PAGE_SIZE
;
241 if (page_mapcount(page
) >= 2) {
242 if (pte_dirty(ptent
))
243 mss
->shared_dirty
+= PAGE_SIZE
;
245 mss
->shared_clean
+= PAGE_SIZE
;
247 if (pte_dirty(ptent
))
248 mss
->private_dirty
+= PAGE_SIZE
;
250 mss
->private_clean
+= PAGE_SIZE
;
253 pte_unmap_unlock(pte
- 1, ptl
);
257 static void clear_refs_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
258 unsigned long addr
, unsigned long end
,
265 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
266 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
) {
268 if (!pte_present(ptent
))
271 page
= vm_normal_page(vma
, addr
, ptent
);
275 /* Clear accessed and referenced bits. */
276 ptep_test_and_clear_young(vma
, addr
, pte
);
277 ClearPageReferenced(page
);
279 pte_unmap_unlock(pte
- 1, ptl
);
283 static inline void walk_pmd_range(struct pmd_walker
*walker
, pud_t
*pud
,
284 unsigned long addr
, unsigned long end
)
289 for (pmd
= pmd_offset(pud
, addr
); addr
!= end
;
290 pmd
++, addr
= next
) {
291 next
= pmd_addr_end(addr
, end
);
292 if (pmd_none_or_clear_bad(pmd
))
294 walker
->action(walker
->vma
, pmd
, addr
, next
, walker
->private);
298 static inline void walk_pud_range(struct pmd_walker
*walker
, pgd_t
*pgd
,
299 unsigned long addr
, unsigned long end
)
304 for (pud
= pud_offset(pgd
, addr
); addr
!= end
;
305 pud
++, addr
= next
) {
306 next
= pud_addr_end(addr
, end
);
307 if (pud_none_or_clear_bad(pud
))
309 walk_pmd_range(walker
, pud
, addr
, next
);
314 * walk_page_range - walk the page tables of a VMA with a callback
316 * @action - callback invoked for every bottom-level (PTE) page table
317 * @private - private data passed to the callback function
319 * Recursively walk the page table for the memory area in a VMA, calling
320 * a callback for every bottom-level (PTE) page table.
322 static inline void walk_page_range(struct vm_area_struct
*vma
,
323 void (*action
)(struct vm_area_struct
*,
324 pmd_t
*, unsigned long,
325 unsigned long, void *),
328 unsigned long addr
= vma
->vm_start
;
329 unsigned long end
= vma
->vm_end
;
330 struct pmd_walker walker
= {
338 for (pgd
= pgd_offset(vma
->vm_mm
, addr
); addr
!= end
;
339 pgd
++, addr
= next
) {
340 next
= pgd_addr_end(addr
, end
);
341 if (pgd_none_or_clear_bad(pgd
))
343 walk_pud_range(&walker
, pgd
, addr
, next
);
347 static int show_smap(struct seq_file
*m
, void *v
)
349 struct vm_area_struct
*vma
= v
;
350 struct mem_size_stats mss
;
352 memset(&mss
, 0, sizeof mss
);
353 if (vma
->vm_mm
&& !is_vm_hugetlb_page(vma
))
354 walk_page_range(vma
, smaps_pte_range
, &mss
);
355 return show_map_internal(m
, v
, &mss
);
358 void clear_refs_smap(struct mm_struct
*mm
)
360 struct vm_area_struct
*vma
;
362 down_read(&mm
->mmap_sem
);
363 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
)
364 if (vma
->vm_mm
&& !is_vm_hugetlb_page(vma
))
365 walk_page_range(vma
, clear_refs_pte_range
, NULL
);
367 up_read(&mm
->mmap_sem
);
370 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
372 struct proc_maps_private
*priv
= m
->private;
373 unsigned long last_addr
= m
->version
;
374 struct mm_struct
*mm
;
375 struct vm_area_struct
*vma
, *tail_vma
= NULL
;
378 /* Clear the per syscall fields in priv */
380 priv
->tail_vma
= NULL
;
383 * We remember last_addr rather than next_addr to hit with
384 * mmap_cache most of the time. We have zero last_addr at
385 * the beginning and also after lseek. We will have -1 last_addr
386 * after the end of the vmas.
389 if (last_addr
== -1UL)
392 priv
->task
= get_pid_task(priv
->pid
, PIDTYPE_PID
);
396 mm
= get_task_mm(priv
->task
);
400 priv
->tail_vma
= tail_vma
= get_gate_vma(priv
->task
);
401 down_read(&mm
->mmap_sem
);
403 /* Start with last addr hint */
404 if (last_addr
&& (vma
= find_vma(mm
, last_addr
))) {
410 * Check the vma index is within the range and do
411 * sequential scan until m_index.
414 if ((unsigned long)l
< mm
->map_count
) {
421 if (l
!= mm
->map_count
)
422 tail_vma
= NULL
; /* After gate vma */
428 /* End of vmas has been reached */
429 m
->version
= (tail_vma
!= NULL
)? 0: -1UL;
430 up_read(&mm
->mmap_sem
);
435 static void vma_stop(struct proc_maps_private
*priv
, struct vm_area_struct
*vma
)
437 if (vma
&& vma
!= priv
->tail_vma
) {
438 struct mm_struct
*mm
= vma
->vm_mm
;
439 up_read(&mm
->mmap_sem
);
444 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
446 struct proc_maps_private
*priv
= m
->private;
447 struct vm_area_struct
*vma
= v
;
448 struct vm_area_struct
*tail_vma
= priv
->tail_vma
;
451 if (vma
&& (vma
!= tail_vma
) && vma
->vm_next
)
454 return (vma
!= tail_vma
)? tail_vma
: NULL
;
457 static void m_stop(struct seq_file
*m
, void *v
)
459 struct proc_maps_private
*priv
= m
->private;
460 struct vm_area_struct
*vma
= v
;
464 put_task_struct(priv
->task
);
467 static struct seq_operations proc_pid_maps_op
= {
474 static struct seq_operations proc_pid_smaps_op
= {
481 static int do_maps_open(struct inode
*inode
, struct file
*file
,
482 struct seq_operations
*ops
)
484 struct proc_maps_private
*priv
;
486 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
488 priv
->pid
= proc_pid(inode
);
489 ret
= seq_open(file
, ops
);
491 struct seq_file
*m
= file
->private_data
;
500 static int maps_open(struct inode
*inode
, struct file
*file
)
502 return do_maps_open(inode
, file
, &proc_pid_maps_op
);
505 const struct file_operations proc_maps_operations
= {
509 .release
= seq_release_private
,
513 extern int show_numa_map(struct seq_file
*m
, void *v
);
515 static struct seq_operations proc_pid_numa_maps_op
= {
519 .show
= show_numa_map
522 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
524 return do_maps_open(inode
, file
, &proc_pid_numa_maps_op
);
527 const struct file_operations proc_numa_maps_operations
= {
528 .open
= numa_maps_open
,
531 .release
= seq_release_private
,
535 static int smaps_open(struct inode
*inode
, struct file
*file
)
537 return do_maps_open(inode
, file
, &proc_pid_smaps_op
);
540 const struct file_operations proc_smaps_operations
= {
544 .release
= seq_release_private
,