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> |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame^] | 5 | #include <linux/pagemap.h> |
| 6 | #include <linux/mempolicy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <asm/elf.h> |
| 8 | #include <asm/uaccess.h> |
| 9 | #include "internal.h" |
| 10 | |
| 11 | char *task_mem(struct mm_struct *mm, char *buffer) |
| 12 | { |
| 13 | unsigned long data, text, lib; |
| 14 | |
| 15 | data = mm->total_vm - mm->shared_vm - mm->stack_vm; |
| 16 | text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10; |
| 17 | lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text; |
| 18 | buffer += sprintf(buffer, |
| 19 | "VmSize:\t%8lu kB\n" |
| 20 | "VmLck:\t%8lu kB\n" |
| 21 | "VmRSS:\t%8lu kB\n" |
| 22 | "VmData:\t%8lu kB\n" |
| 23 | "VmStk:\t%8lu kB\n" |
| 24 | "VmExe:\t%8lu kB\n" |
| 25 | "VmLib:\t%8lu kB\n" |
| 26 | "VmPTE:\t%8lu kB\n", |
| 27 | (mm->total_vm - mm->reserved_vm) << (PAGE_SHIFT-10), |
| 28 | mm->locked_vm << (PAGE_SHIFT-10), |
| 29 | get_mm_counter(mm, rss) << (PAGE_SHIFT-10), |
| 30 | data << (PAGE_SHIFT-10), |
| 31 | mm->stack_vm << (PAGE_SHIFT-10), text, lib, |
| 32 | (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10); |
| 33 | return buffer; |
| 34 | } |
| 35 | |
| 36 | unsigned long task_vsize(struct mm_struct *mm) |
| 37 | { |
| 38 | return PAGE_SIZE * mm->total_vm; |
| 39 | } |
| 40 | |
| 41 | int task_statm(struct mm_struct *mm, int *shared, int *text, |
| 42 | int *data, int *resident) |
| 43 | { |
| 44 | int rss = get_mm_counter(mm, rss); |
| 45 | |
| 46 | *shared = rss - get_mm_counter(mm, anon_rss); |
| 47 | *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) |
| 48 | >> PAGE_SHIFT; |
| 49 | *data = mm->total_vm - mm->shared_vm; |
| 50 | *resident = rss; |
| 51 | return mm->total_vm; |
| 52 | } |
| 53 | |
| 54 | int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) |
| 55 | { |
| 56 | struct vm_area_struct * vma; |
| 57 | int result = -ENOENT; |
| 58 | struct task_struct *task = proc_task(inode); |
| 59 | struct mm_struct * mm = get_task_mm(task); |
| 60 | |
| 61 | if (!mm) |
| 62 | goto out; |
| 63 | down_read(&mm->mmap_sem); |
| 64 | |
| 65 | vma = mm->mmap; |
| 66 | while (vma) { |
| 67 | if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) |
| 68 | break; |
| 69 | vma = vma->vm_next; |
| 70 | } |
| 71 | |
| 72 | if (vma) { |
| 73 | *mnt = mntget(vma->vm_file->f_vfsmnt); |
| 74 | *dentry = dget(vma->vm_file->f_dentry); |
| 75 | result = 0; |
| 76 | } |
| 77 | |
| 78 | up_read(&mm->mmap_sem); |
| 79 | mmput(mm); |
| 80 | out: |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | static void pad_len_spaces(struct seq_file *m, int len) |
| 85 | { |
| 86 | len = 25 + sizeof(void*) * 6 - len; |
| 87 | if (len < 1) |
| 88 | len = 1; |
| 89 | seq_printf(m, "%*c", len, ' '); |
| 90 | } |
| 91 | |
| 92 | static int show_map(struct seq_file *m, void *v) |
| 93 | { |
| 94 | struct task_struct *task = m->private; |
| 95 | struct vm_area_struct *map = v; |
| 96 | struct mm_struct *mm = map->vm_mm; |
| 97 | struct file *file = map->vm_file; |
| 98 | int flags = map->vm_flags; |
| 99 | unsigned long ino = 0; |
| 100 | dev_t dev = 0; |
| 101 | int len; |
| 102 | |
| 103 | if (file) { |
| 104 | struct inode *inode = map->vm_file->f_dentry->d_inode; |
| 105 | dev = inode->i_sb->s_dev; |
| 106 | ino = inode->i_ino; |
| 107 | } |
| 108 | |
| 109 | seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n", |
| 110 | map->vm_start, |
| 111 | map->vm_end, |
| 112 | flags & VM_READ ? 'r' : '-', |
| 113 | flags & VM_WRITE ? 'w' : '-', |
| 114 | flags & VM_EXEC ? 'x' : '-', |
| 115 | flags & VM_MAYSHARE ? 's' : 'p', |
| 116 | map->vm_pgoff << PAGE_SHIFT, |
| 117 | MAJOR(dev), MINOR(dev), ino, &len); |
| 118 | |
| 119 | /* |
| 120 | * Print the dentry name for named mappings, and a |
| 121 | * special [heap] marker for the heap: |
| 122 | */ |
| 123 | if (map->vm_file) { |
| 124 | pad_len_spaces(m, len); |
| 125 | seq_path(m, file->f_vfsmnt, file->f_dentry, ""); |
| 126 | } else { |
| 127 | if (mm) { |
| 128 | if (map->vm_start <= mm->start_brk && |
| 129 | map->vm_end >= mm->brk) { |
| 130 | pad_len_spaces(m, len); |
| 131 | seq_puts(m, "[heap]"); |
| 132 | } else { |
| 133 | if (map->vm_start <= mm->start_stack && |
| 134 | map->vm_end >= mm->start_stack) { |
| 135 | |
| 136 | pad_len_spaces(m, len); |
| 137 | seq_puts(m, "[stack]"); |
| 138 | } |
| 139 | } |
| 140 | } else { |
| 141 | pad_len_spaces(m, len); |
| 142 | seq_puts(m, "[vdso]"); |
| 143 | } |
| 144 | } |
| 145 | seq_putc(m, '\n'); |
| 146 | if (m->count < m->size) /* map is copied successfully */ |
| 147 | m->version = (map != get_gate_vma(task))? map->vm_start: 0; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 152 | { |
| 153 | struct task_struct *task = m->private; |
| 154 | unsigned long last_addr = m->version; |
| 155 | struct mm_struct *mm; |
| 156 | struct vm_area_struct *map, *tail_map; |
| 157 | loff_t l = *pos; |
| 158 | |
| 159 | /* |
| 160 | * We remember last_addr rather than next_addr to hit with |
| 161 | * mmap_cache most of the time. We have zero last_addr at |
| 162 | * the begining and also after lseek. We will have -1 last_addr |
| 163 | * after the end of the maps. |
| 164 | */ |
| 165 | |
| 166 | if (last_addr == -1UL) |
| 167 | return NULL; |
| 168 | |
| 169 | mm = get_task_mm(task); |
| 170 | if (!mm) |
| 171 | return NULL; |
| 172 | |
| 173 | tail_map = get_gate_vma(task); |
| 174 | down_read(&mm->mmap_sem); |
| 175 | |
| 176 | /* Start with last addr hint */ |
| 177 | if (last_addr && (map = find_vma(mm, last_addr))) { |
| 178 | map = map->vm_next; |
| 179 | goto out; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Check the map index is within the range and do |
| 184 | * sequential scan until m_index. |
| 185 | */ |
| 186 | map = NULL; |
| 187 | if ((unsigned long)l < mm->map_count) { |
| 188 | map = mm->mmap; |
| 189 | while (l-- && map) |
| 190 | map = map->vm_next; |
| 191 | goto out; |
| 192 | } |
| 193 | |
| 194 | if (l != mm->map_count) |
| 195 | tail_map = NULL; /* After gate map */ |
| 196 | |
| 197 | out: |
| 198 | if (map) |
| 199 | return map; |
| 200 | |
| 201 | /* End of maps has reached */ |
| 202 | m->version = (tail_map != NULL)? 0: -1UL; |
| 203 | up_read(&mm->mmap_sem); |
| 204 | mmput(mm); |
| 205 | return tail_map; |
| 206 | } |
| 207 | |
| 208 | static void m_stop(struct seq_file *m, void *v) |
| 209 | { |
| 210 | struct task_struct *task = m->private; |
| 211 | struct vm_area_struct *map = v; |
| 212 | if (map && map != get_gate_vma(task)) { |
| 213 | struct mm_struct *mm = map->vm_mm; |
| 214 | up_read(&mm->mmap_sem); |
| 215 | mmput(mm); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 220 | { |
| 221 | struct task_struct *task = m->private; |
| 222 | struct vm_area_struct *map = v; |
| 223 | struct vm_area_struct *tail_map = get_gate_vma(task); |
| 224 | |
| 225 | (*pos)++; |
| 226 | if (map && (map != tail_map) && map->vm_next) |
| 227 | return map->vm_next; |
| 228 | m_stop(m, v); |
| 229 | return (map != tail_map)? tail_map: NULL; |
| 230 | } |
| 231 | |
| 232 | struct seq_operations proc_pid_maps_op = { |
| 233 | .start = m_start, |
| 234 | .next = m_next, |
| 235 | .stop = m_stop, |
| 236 | .show = show_map |
| 237 | }; |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame^] | 238 | |
| 239 | #ifdef CONFIG_NUMA |
| 240 | |
| 241 | struct numa_maps { |
| 242 | unsigned long pages; |
| 243 | unsigned long anon; |
| 244 | unsigned long mapped; |
| 245 | unsigned long mapcount_max; |
| 246 | unsigned long node[MAX_NUMNODES]; |
| 247 | }; |
| 248 | |
| 249 | /* |
| 250 | * Calculate numa node maps for a vma |
| 251 | */ |
| 252 | static struct numa_maps *get_numa_maps(const struct vm_area_struct *vma) |
| 253 | { |
| 254 | struct page *page; |
| 255 | unsigned long vaddr; |
| 256 | struct mm_struct *mm = vma->vm_mm; |
| 257 | int i; |
| 258 | struct numa_maps *md = kmalloc(sizeof(struct numa_maps), GFP_KERNEL); |
| 259 | |
| 260 | if (!md) |
| 261 | return NULL; |
| 262 | md->pages = 0; |
| 263 | md->anon = 0; |
| 264 | md->mapped = 0; |
| 265 | md->mapcount_max = 0; |
| 266 | for_each_node(i) |
| 267 | md->node[i] =0; |
| 268 | |
| 269 | spin_lock(&mm->page_table_lock); |
| 270 | for (vaddr = vma->vm_start; vaddr < vma->vm_end; vaddr += PAGE_SIZE) { |
| 271 | page = follow_page(mm, vaddr, 0); |
| 272 | if (page) { |
| 273 | int count = page_mapcount(page); |
| 274 | |
| 275 | if (count) |
| 276 | md->mapped++; |
| 277 | if (count > md->mapcount_max) |
| 278 | md->mapcount_max = count; |
| 279 | md->pages++; |
| 280 | if (PageAnon(page)) |
| 281 | md->anon++; |
| 282 | md->node[page_to_nid(page)]++; |
| 283 | } |
| 284 | } |
| 285 | spin_unlock(&mm->page_table_lock); |
| 286 | return md; |
| 287 | } |
| 288 | |
| 289 | static int show_numa_map(struct seq_file *m, void *v) |
| 290 | { |
| 291 | struct task_struct *task = m->private; |
| 292 | struct vm_area_struct *vma = v; |
| 293 | struct mempolicy *pol; |
| 294 | struct numa_maps *md; |
| 295 | struct zone **z; |
| 296 | int n; |
| 297 | int first; |
| 298 | |
| 299 | if (!vma->vm_mm) |
| 300 | return 0; |
| 301 | |
| 302 | md = get_numa_maps(vma); |
| 303 | if (!md) |
| 304 | return 0; |
| 305 | |
| 306 | seq_printf(m, "%08lx", vma->vm_start); |
| 307 | pol = get_vma_policy(task, vma, vma->vm_start); |
| 308 | /* Print policy */ |
| 309 | switch (pol->policy) { |
| 310 | case MPOL_PREFERRED: |
| 311 | seq_printf(m, " prefer=%d", pol->v.preferred_node); |
| 312 | break; |
| 313 | case MPOL_BIND: |
| 314 | seq_printf(m, " bind={"); |
| 315 | first = 1; |
| 316 | for (z = pol->v.zonelist->zones; *z; z++) { |
| 317 | |
| 318 | if (!first) |
| 319 | seq_putc(m, ','); |
| 320 | else |
| 321 | first = 0; |
| 322 | seq_printf(m, "%d/%s", (*z)->zone_pgdat->node_id, |
| 323 | (*z)->name); |
| 324 | } |
| 325 | seq_putc(m, '}'); |
| 326 | break; |
| 327 | case MPOL_INTERLEAVE: |
| 328 | seq_printf(m, " interleave={"); |
| 329 | first = 1; |
| 330 | for_each_node(n) { |
| 331 | if (test_bit(n, pol->v.nodes)) { |
| 332 | if (!first) |
| 333 | seq_putc(m,','); |
| 334 | else |
| 335 | first = 0; |
| 336 | seq_printf(m, "%d",n); |
| 337 | } |
| 338 | } |
| 339 | seq_putc(m, '}'); |
| 340 | break; |
| 341 | default: |
| 342 | seq_printf(m," default"); |
| 343 | break; |
| 344 | } |
| 345 | seq_printf(m, " MaxRef=%lu Pages=%lu Mapped=%lu", |
| 346 | md->mapcount_max, md->pages, md->mapped); |
| 347 | if (md->anon) |
| 348 | seq_printf(m," Anon=%lu",md->anon); |
| 349 | |
| 350 | for_each_online_node(n) { |
| 351 | if (md->node[n]) |
| 352 | seq_printf(m, " N%d=%lu", n, md->node[n]); |
| 353 | } |
| 354 | seq_putc(m, '\n'); |
| 355 | kfree(md); |
| 356 | if (m->count < m->size) /* vma is copied successfully */ |
| 357 | m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | struct seq_operations proc_pid_numa_maps_op = { |
| 362 | .start = m_start, |
| 363 | .next = m_next, |
| 364 | .stop = m_stop, |
| 365 | .show = show_numa_map |
| 366 | }; |
| 367 | #endif |