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