Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/mm.h> |
| 2 | #include <linux/hugetlb.h> |
| 3 | #include <linux/mount.h> |
| 4 | #include <linux/seq_file.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 5 | #include <linux/highmem.h> |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 6 | #include <linux/pagemap.h> |
| 7 | #include <linux/mempolicy.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <asm/elf.h> |
| 10 | #include <asm/uaccess.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 11 | #include <asm/tlbflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "internal.h" |
| 13 | |
| 14 | char *task_mem(struct mm_struct *mm, char *buffer) |
| 15 | { |
| 16 | unsigned long data, text, lib; |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 17 | unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; |
| 18 | |
| 19 | /* |
| 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. |
| 25 | */ |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 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, |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 37 | "VmPeak:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | "VmSize:\t%8lu kB\n" |
| 39 | "VmLck:\t%8lu kB\n" |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 40 | "VmHWM:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | "VmRSS:\t%8lu kB\n" |
| 42 | "VmData:\t%8lu kB\n" |
| 43 | "VmStk:\t%8lu kB\n" |
| 44 | "VmExe:\t%8lu kB\n" |
| 45 | "VmLib:\t%8lu kB\n" |
| 46 | "VmPTE:\t%8lu kB\n", |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 47 | hiwater_vm << (PAGE_SHIFT-10), |
| 48 | (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | mm->locked_vm << (PAGE_SHIFT-10), |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 50 | hiwater_rss << (PAGE_SHIFT-10), |
| 51 | total_rss << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 55 | return buffer; |
| 56 | } |
| 57 | |
| 58 | unsigned long task_vsize(struct mm_struct *mm) |
| 59 | { |
| 60 | return PAGE_SIZE * mm->total_vm; |
| 61 | } |
| 62 | |
| 63 | int task_statm(struct mm_struct *mm, int *shared, int *text, |
| 64 | int *data, int *resident) |
| 65 | { |
Hugh Dickins | 4294621 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 66 | *shared = get_mm_counter(mm, file_rss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) |
| 68 | >> PAGE_SHIFT; |
| 69 | *data = mm->total_vm - mm->shared_vm; |
Hugh Dickins | 4294621 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 70 | *resident = *shared + get_mm_counter(mm, anon_rss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return mm->total_vm; |
| 72 | } |
| 73 | |
| 74 | int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) |
| 75 | { |
| 76 | struct vm_area_struct * vma; |
| 77 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 78 | struct task_struct *task = get_proc_task(inode); |
| 79 | struct mm_struct * mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 81 | if (task) { |
| 82 | mm = get_task_mm(task); |
| 83 | put_task_struct(task); |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (!mm) |
| 86 | goto out; |
| 87 | down_read(&mm->mmap_sem); |
| 88 | |
| 89 | vma = mm->mmap; |
| 90 | while (vma) { |
| 91 | if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) |
| 92 | break; |
| 93 | vma = vma->vm_next; |
| 94 | } |
| 95 | |
| 96 | if (vma) { |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 97 | *mnt = mntget(vma->vm_file->f_path.mnt); |
| 98 | *dentry = dget(vma->vm_file->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | result = 0; |
| 100 | } |
| 101 | |
| 102 | up_read(&mm->mmap_sem); |
| 103 | mmput(mm); |
| 104 | out: |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | static void pad_len_spaces(struct seq_file *m, int len) |
| 109 | { |
| 110 | len = 25 + sizeof(void*) * 6 - len; |
| 111 | if (len < 1) |
| 112 | len = 1; |
| 113 | seq_printf(m, "%*c", len, ' '); |
| 114 | } |
| 115 | |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 116 | struct mem_size_stats |
| 117 | { |
| 118 | unsigned long resident; |
| 119 | unsigned long shared_clean; |
| 120 | unsigned long shared_dirty; |
| 121 | unsigned long private_clean; |
| 122 | unsigned long private_dirty; |
David Rientjes | f79f177 | 2007-05-06 14:49:22 -0700 | [diff] [blame] | 123 | unsigned long referenced; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 126 | struct pmd_walker { |
| 127 | struct vm_area_struct *vma; |
| 128 | void *private; |
| 129 | void (*action)(struct vm_area_struct *, pmd_t *, unsigned long, |
| 130 | unsigned long, void *); |
| 131 | }; |
| 132 | |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 133 | static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 135 | struct proc_maps_private *priv = m->private; |
| 136 | struct task_struct *task = priv->task; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | unsigned long ino = 0; |
| 142 | dev_t dev = 0; |
| 143 | int len; |
| 144 | |
| 145 | if (file) { |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 146 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | dev = inode->i_sb->s_dev; |
| 148 | ino = inode->i_ino; |
| 149 | } |
| 150 | |
| 151 | seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n", |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 152 | vma->vm_start, |
| 153 | vma->vm_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | flags & VM_READ ? 'r' : '-', |
| 155 | flags & VM_WRITE ? 'w' : '-', |
| 156 | flags & VM_EXEC ? 'x' : '-', |
| 157 | flags & VM_MAYSHARE ? 's' : 'p', |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 158 | vma->vm_pgoff << PAGE_SHIFT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | MAJOR(dev), MINOR(dev), ino, &len); |
| 160 | |
| 161 | /* |
| 162 | * Print the dentry name for named mappings, and a |
| 163 | * special [heap] marker for the heap: |
| 164 | */ |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 165 | if (file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | pad_len_spaces(m, len); |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 167 | seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } else { |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 169 | const char *name = arch_vma_name(vma); |
| 170 | if (!name) { |
| 171 | if (mm) { |
| 172 | if (vma->vm_start <= mm->start_brk && |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 173 | vma->vm_end >= mm->brk) { |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 174 | name = "[heap]"; |
| 175 | } else if (vma->vm_start <= mm->start_stack && |
| 176 | vma->vm_end >= mm->start_stack) { |
| 177 | name = "[stack]"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 179 | } else { |
| 180 | name = "[vdso]"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 182 | } |
| 183 | if (name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | pad_len_spaces(m, len); |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 185 | seq_puts(m, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | seq_putc(m, '\n'); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 189 | |
| 190 | if (mss) |
| 191 | seq_printf(m, |
David Rientjes | f79f177 | 2007-05-06 14:49:22 -0700 | [diff] [blame] | 192 | "Size: %8lu kB\n" |
| 193 | "Rss: %8lu kB\n" |
| 194 | "Shared_Clean: %8lu kB\n" |
| 195 | "Shared_Dirty: %8lu kB\n" |
| 196 | "Private_Clean: %8lu kB\n" |
| 197 | "Private_Dirty: %8lu kB\n" |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 198 | "Referenced: %8lu kB\n", |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 199 | (vma->vm_end - vma->vm_start) >> 10, |
| 200 | mss->resident >> 10, |
| 201 | mss->shared_clean >> 10, |
| 202 | mss->shared_dirty >> 10, |
| 203 | mss->private_clean >> 10, |
David Rientjes | f79f177 | 2007-05-06 14:49:22 -0700 | [diff] [blame] | 204 | mss->private_dirty >> 10, |
| 205 | mss->referenced >> 10); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 206 | |
| 207 | if (m->count < m->size) /* vma is copied successfully */ |
| 208 | m->version = (vma != get_gate_vma(task))? vma->vm_start: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 212 | static int show_map(struct seq_file *m, void *v) |
| 213 | { |
Luiz Fernando Capitulino | 0f5c79f | 2005-11-13 16:07:20 -0800 | [diff] [blame] | 214 | return show_map_internal(m, v, NULL); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 215 | } |
| 216 | |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 217 | static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd, |
| 218 | unsigned long addr, unsigned long end, |
| 219 | void *private) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 220 | { |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 221 | struct mem_size_stats *mss = private; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 222 | pte_t *pte, ptent; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 223 | spinlock_t *ptl; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 224 | struct page *page; |
| 225 | |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 226 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 227 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 228 | ptent = *pte; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 229 | if (!pte_present(ptent)) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 230 | continue; |
| 231 | |
| 232 | mss->resident += PAGE_SIZE; |
Nick Piggin | ad820c5d | 2006-03-06 15:42:58 -0800 | [diff] [blame] | 233 | |
| 234 | page = vm_normal_page(vma, addr, ptent); |
| 235 | if (!page) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 236 | continue; |
| 237 | |
David Rientjes | f79f177 | 2007-05-06 14:49:22 -0700 | [diff] [blame] | 238 | /* Accumulate the size in pages that have been accessed. */ |
| 239 | if (pte_young(ptent) || PageReferenced(page)) |
| 240 | mss->referenced += PAGE_SIZE; |
Nick Piggin | ad820c5d | 2006-03-06 15:42:58 -0800 | [diff] [blame] | 241 | if (page_mapcount(page) >= 2) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 242 | if (pte_dirty(ptent)) |
| 243 | mss->shared_dirty += PAGE_SIZE; |
| 244 | else |
| 245 | mss->shared_clean += PAGE_SIZE; |
| 246 | } else { |
| 247 | if (pte_dirty(ptent)) |
| 248 | mss->private_dirty += PAGE_SIZE; |
| 249 | else |
| 250 | mss->private_clean += PAGE_SIZE; |
| 251 | } |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 252 | } |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 253 | pte_unmap_unlock(pte - 1, ptl); |
| 254 | cond_resched(); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 257 | static void clear_refs_pte_range(struct vm_area_struct *vma, pmd_t *pmd, |
| 258 | unsigned long addr, unsigned long end, |
| 259 | void *private) |
| 260 | { |
| 261 | pte_t *pte, ptent; |
| 262 | spinlock_t *ptl; |
| 263 | struct page *page; |
| 264 | |
| 265 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 266 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
| 267 | ptent = *pte; |
| 268 | if (!pte_present(ptent)) |
| 269 | continue; |
| 270 | |
| 271 | page = vm_normal_page(vma, addr, ptent); |
| 272 | if (!page) |
| 273 | continue; |
| 274 | |
| 275 | /* Clear accessed and referenced bits. */ |
| 276 | ptep_test_and_clear_young(vma, addr, pte); |
| 277 | ClearPageReferenced(page); |
| 278 | } |
| 279 | pte_unmap_unlock(pte - 1, ptl); |
| 280 | cond_resched(); |
| 281 | } |
| 282 | |
| 283 | static inline void walk_pmd_range(struct pmd_walker *walker, pud_t *pud, |
| 284 | unsigned long addr, unsigned long end) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 285 | { |
| 286 | pmd_t *pmd; |
| 287 | unsigned long next; |
| 288 | |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 289 | for (pmd = pmd_offset(pud, addr); addr != end; |
| 290 | pmd++, addr = next) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 291 | next = pmd_addr_end(addr, end); |
| 292 | if (pmd_none_or_clear_bad(pmd)) |
| 293 | continue; |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 294 | walker->action(walker->vma, pmd, addr, next, walker->private); |
| 295 | } |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 296 | } |
| 297 | |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 298 | static inline void walk_pud_range(struct pmd_walker *walker, pgd_t *pgd, |
| 299 | unsigned long addr, unsigned long end) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 300 | { |
| 301 | pud_t *pud; |
| 302 | unsigned long next; |
| 303 | |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 304 | for (pud = pud_offset(pgd, addr); addr != end; |
| 305 | pud++, addr = next) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 306 | next = pud_addr_end(addr, end); |
| 307 | if (pud_none_or_clear_bad(pud)) |
| 308 | continue; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 309 | walk_pmd_range(walker, pud, addr, next); |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 310 | } |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 311 | } |
| 312 | |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 313 | /* |
| 314 | * walk_page_range - walk the page tables of a VMA with a callback |
| 315 | * @vma - VMA to walk |
| 316 | * @action - callback invoked for every bottom-level (PTE) page table |
| 317 | * @private - private data passed to the callback function |
| 318 | * |
| 319 | * Recursively walk the page table for the memory area in a VMA, calling |
| 320 | * a callback for every bottom-level (PTE) page table. |
| 321 | */ |
| 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 *), |
| 326 | void *private) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 327 | { |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 328 | unsigned long addr = vma->vm_start; |
| 329 | unsigned long end = vma->vm_end; |
| 330 | struct pmd_walker walker = { |
| 331 | .vma = vma, |
| 332 | .private = private, |
| 333 | .action = action, |
| 334 | }; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 335 | pgd_t *pgd; |
| 336 | unsigned long next; |
| 337 | |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 338 | for (pgd = pgd_offset(vma->vm_mm, addr); addr != end; |
| 339 | pgd++, addr = next) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 340 | next = pgd_addr_end(addr, end); |
| 341 | if (pgd_none_or_clear_bad(pgd)) |
| 342 | continue; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 343 | walk_pud_range(&walker, pgd, addr, next); |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 344 | } |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static int show_smap(struct seq_file *m, void *v) |
| 348 | { |
| 349 | struct vm_area_struct *vma = v; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 350 | struct mem_size_stats mss; |
| 351 | |
| 352 | memset(&mss, 0, sizeof mss); |
Nick Piggin | 5ddfae1 | 2006-03-06 15:42:57 -0800 | [diff] [blame] | 353 | if (vma->vm_mm && !is_vm_hugetlb_page(vma)) |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 354 | walk_page_range(vma, smaps_pte_range, &mss); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 355 | return show_map_internal(m, v, &mss); |
| 356 | } |
| 357 | |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 358 | void clear_refs_smap(struct mm_struct *mm) |
| 359 | { |
| 360 | struct vm_area_struct *vma; |
| 361 | |
| 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); |
| 366 | flush_tlb_mm(mm); |
| 367 | up_read(&mm->mmap_sem); |
| 368 | } |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 371 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 372 | struct proc_maps_private *priv = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | unsigned long last_addr = m->version; |
| 374 | struct mm_struct *mm; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 375 | struct vm_area_struct *vma, *tail_vma = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | loff_t l = *pos; |
| 377 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 378 | /* Clear the per syscall fields in priv */ |
| 379 | priv->task = NULL; |
| 380 | priv->tail_vma = NULL; |
| 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | /* |
| 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 |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 385 | * the beginning and also after lseek. We will have -1 last_addr |
| 386 | * after the end of the vmas. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | */ |
| 388 | |
| 389 | if (last_addr == -1UL) |
| 390 | return NULL; |
| 391 | |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 392 | priv->task = get_pid_task(priv->pid, PIDTYPE_PID); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 393 | if (!priv->task) |
| 394 | return NULL; |
| 395 | |
| 396 | mm = get_task_mm(priv->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (!mm) |
| 398 | return NULL; |
| 399 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 400 | priv->tail_vma = tail_vma = get_gate_vma(priv->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | down_read(&mm->mmap_sem); |
| 402 | |
| 403 | /* Start with last addr hint */ |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 404 | if (last_addr && (vma = find_vma(mm, last_addr))) { |
| 405 | vma = vma->vm_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | /* |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 410 | * Check the vma index is within the range and do |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * sequential scan until m_index. |
| 412 | */ |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 413 | vma = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if ((unsigned long)l < mm->map_count) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 415 | vma = mm->mmap; |
| 416 | while (l-- && vma) |
| 417 | vma = vma->vm_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | goto out; |
| 419 | } |
| 420 | |
| 421 | if (l != mm->map_count) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 422 | tail_vma = NULL; /* After gate vma */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | out: |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 425 | if (vma) |
| 426 | return vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 428 | /* End of vmas has been reached */ |
| 429 | m->version = (tail_vma != NULL)? 0: -1UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | up_read(&mm->mmap_sem); |
| 431 | mmput(mm); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 432 | return tail_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 435 | static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 437 | if (vma && vma != priv->tail_vma) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 438 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | up_read(&mm->mmap_sem); |
| 440 | mmput(mm); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 445 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 446 | struct proc_maps_private *priv = m->private; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 447 | struct vm_area_struct *vma = v; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 448 | struct vm_area_struct *tail_vma = priv->tail_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | (*pos)++; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 451 | if (vma && (vma != tail_vma) && vma->vm_next) |
| 452 | return vma->vm_next; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 453 | vma_stop(priv, vma); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 454 | return (vma != tail_vma)? tail_vma: NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 457 | static void m_stop(struct seq_file *m, void *v) |
| 458 | { |
| 459 | struct proc_maps_private *priv = m->private; |
| 460 | struct vm_area_struct *vma = v; |
| 461 | |
| 462 | vma_stop(priv, vma); |
| 463 | if (priv->task) |
| 464 | put_task_struct(priv->task); |
| 465 | } |
| 466 | |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 467 | static struct seq_operations proc_pid_maps_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | .start = m_start, |
| 469 | .next = m_next, |
| 470 | .stop = m_stop, |
| 471 | .show = show_map |
| 472 | }; |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 473 | |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 474 | static struct seq_operations proc_pid_smaps_op = { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 475 | .start = m_start, |
| 476 | .next = m_next, |
| 477 | .stop = m_stop, |
| 478 | .show = show_smap |
| 479 | }; |
| 480 | |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 481 | static int do_maps_open(struct inode *inode, struct file *file, |
| 482 | struct seq_operations *ops) |
| 483 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 484 | struct proc_maps_private *priv; |
| 485 | int ret = -ENOMEM; |
| 486 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 487 | if (priv) { |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 488 | priv->pid = proc_pid(inode); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 489 | ret = seq_open(file, ops); |
| 490 | if (!ret) { |
| 491 | struct seq_file *m = file->private_data; |
| 492 | m->private = priv; |
| 493 | } else { |
| 494 | kfree(priv); |
| 495 | } |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 496 | } |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static int maps_open(struct inode *inode, struct file *file) |
| 501 | { |
| 502 | return do_maps_open(inode, file, &proc_pid_maps_op); |
| 503 | } |
| 504 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 505 | const struct file_operations proc_maps_operations = { |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 506 | .open = maps_open, |
| 507 | .read = seq_read, |
| 508 | .llseek = seq_lseek, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 509 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 512 | #ifdef CONFIG_NUMA |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 513 | extern int show_numa_map(struct seq_file *m, void *v); |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 514 | |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 515 | static struct seq_operations proc_pid_numa_maps_op = { |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 516 | .start = m_start, |
| 517 | .next = m_next, |
| 518 | .stop = m_stop, |
| 519 | .show = show_numa_map |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 520 | }; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 521 | |
| 522 | static int numa_maps_open(struct inode *inode, struct file *file) |
| 523 | { |
| 524 | return do_maps_open(inode, file, &proc_pid_numa_maps_op); |
| 525 | } |
| 526 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 527 | const struct file_operations proc_numa_maps_operations = { |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 528 | .open = numa_maps_open, |
| 529 | .read = seq_read, |
| 530 | .llseek = seq_lseek, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 531 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 532 | }; |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 533 | #endif |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 534 | |
| 535 | static int smaps_open(struct inode *inode, struct file *file) |
| 536 | { |
| 537 | return do_maps_open(inode, file, &proc_pid_smaps_op); |
| 538 | } |
| 539 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 540 | const struct file_operations proc_smaps_operations = { |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 541 | .open = smaps_open, |
| 542 | .read = seq_read, |
| 543 | .llseek = seq_lseek, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 544 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 545 | }; |