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 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame^] | 117 | static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 118 | { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame^] | 119 | if (vma && vma != priv->tail_vma) { |
| 120 | struct mm_struct *mm = vma->vm_mm; |
| 121 | up_read(&mm->mmap_sem); |
| 122 | mmput(mm); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 127 | { |
| 128 | struct proc_maps_private *priv = m->private; |
| 129 | unsigned long last_addr = m->version; |
| 130 | struct mm_struct *mm; |
| 131 | struct vm_area_struct *vma, *tail_vma = NULL; |
| 132 | loff_t l = *pos; |
| 133 | |
| 134 | /* Clear the per syscall fields in priv */ |
| 135 | priv->task = NULL; |
| 136 | priv->tail_vma = NULL; |
| 137 | |
| 138 | /* |
| 139 | * We remember last_addr rather than next_addr to hit with |
| 140 | * mmap_cache most of the time. We have zero last_addr at |
| 141 | * the beginning and also after lseek. We will have -1 last_addr |
| 142 | * after the end of the vmas. |
| 143 | */ |
| 144 | |
| 145 | if (last_addr == -1UL) |
| 146 | return NULL; |
| 147 | |
| 148 | priv->task = get_pid_task(priv->pid, PIDTYPE_PID); |
| 149 | if (!priv->task) |
| 150 | return NULL; |
| 151 | |
| 152 | mm = mm_for_maps(priv->task); |
| 153 | if (!mm) |
| 154 | return NULL; |
| 155 | |
| 156 | tail_vma = get_gate_vma(priv->task); |
| 157 | priv->tail_vma = tail_vma; |
| 158 | |
| 159 | /* Start with last addr hint */ |
| 160 | vma = find_vma(mm, last_addr); |
| 161 | if (last_addr && vma) { |
| 162 | vma = vma->vm_next; |
| 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Check the vma index is within the range and do |
| 168 | * sequential scan until m_index. |
| 169 | */ |
| 170 | vma = NULL; |
| 171 | if ((unsigned long)l < mm->map_count) { |
| 172 | vma = mm->mmap; |
| 173 | while (l-- && vma) |
| 174 | vma = vma->vm_next; |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | if (l != mm->map_count) |
| 179 | tail_vma = NULL; /* After gate vma */ |
| 180 | |
| 181 | out: |
| 182 | if (vma) |
| 183 | return vma; |
| 184 | |
| 185 | /* End of vmas has been reached */ |
| 186 | m->version = (tail_vma != NULL)? 0: -1UL; |
| 187 | up_read(&mm->mmap_sem); |
| 188 | mmput(mm); |
| 189 | return tail_vma; |
| 190 | } |
| 191 | |
| 192 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 193 | { |
| 194 | struct proc_maps_private *priv = m->private; |
| 195 | struct vm_area_struct *vma = v; |
| 196 | struct vm_area_struct *tail_vma = priv->tail_vma; |
| 197 | |
| 198 | (*pos)++; |
| 199 | if (vma && (vma != tail_vma) && vma->vm_next) |
| 200 | return vma->vm_next; |
| 201 | vma_stop(priv, vma); |
| 202 | return (vma != tail_vma)? tail_vma: NULL; |
| 203 | } |
| 204 | |
| 205 | static void m_stop(struct seq_file *m, void *v) |
| 206 | { |
| 207 | struct proc_maps_private *priv = m->private; |
| 208 | struct vm_area_struct *vma = v; |
| 209 | |
| 210 | vma_stop(priv, vma); |
| 211 | if (priv->task) |
| 212 | put_task_struct(priv->task); |
| 213 | } |
| 214 | |
| 215 | static int do_maps_open(struct inode *inode, struct file *file, |
| 216 | struct seq_operations *ops) |
| 217 | { |
| 218 | struct proc_maps_private *priv; |
| 219 | int ret = -ENOMEM; |
| 220 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 221 | if (priv) { |
| 222 | priv->pid = proc_pid(inode); |
| 223 | ret = seq_open(file, ops); |
| 224 | if (!ret) { |
| 225 | struct seq_file *m = file->private_data; |
| 226 | m->private = priv; |
| 227 | } else { |
| 228 | kfree(priv); |
| 229 | } |
| 230 | } |
| 231 | return ret; |
| 232 | } |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 233 | |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 234 | static int show_map(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 236 | struct proc_maps_private *priv = m->private; |
| 237 | struct task_struct *task = priv->task; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 238 | struct vm_area_struct *vma = v; |
| 239 | struct mm_struct *mm = vma->vm_mm; |
| 240 | struct file *file = vma->vm_file; |
| 241 | int flags = vma->vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | unsigned long ino = 0; |
| 243 | dev_t dev = 0; |
| 244 | int len; |
| 245 | |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 246 | if (maps_protect && !ptrace_may_attach(task)) |
| 247 | return -EACCES; |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | if (file) { |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 250 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | dev = inode->i_sb->s_dev; |
| 252 | ino = inode->i_ino; |
| 253 | } |
| 254 | |
| 255 | 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] | 256 | vma->vm_start, |
| 257 | vma->vm_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | flags & VM_READ ? 'r' : '-', |
| 259 | flags & VM_WRITE ? 'w' : '-', |
| 260 | flags & VM_EXEC ? 'x' : '-', |
| 261 | flags & VM_MAYSHARE ? 's' : 'p', |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 262 | vma->vm_pgoff << PAGE_SHIFT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | MAJOR(dev), MINOR(dev), ino, &len); |
| 264 | |
| 265 | /* |
| 266 | * Print the dentry name for named mappings, and a |
| 267 | * special [heap] marker for the heap: |
| 268 | */ |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 269 | if (file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | pad_len_spaces(m, len); |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 271 | seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } else { |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 273 | const char *name = arch_vma_name(vma); |
| 274 | if (!name) { |
| 275 | if (mm) { |
| 276 | if (vma->vm_start <= mm->start_brk && |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 277 | vma->vm_end >= mm->brk) { |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 278 | name = "[heap]"; |
| 279 | } else if (vma->vm_start <= mm->start_stack && |
| 280 | vma->vm_end >= mm->start_stack) { |
| 281 | name = "[stack]"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 283 | } else { |
| 284 | name = "[vdso]"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 286 | } |
| 287 | if (name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | pad_len_spaces(m, len); |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 289 | seq_puts(m, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | seq_putc(m, '\n'); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 293 | |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 294 | if (m->count < m->size) /* vma is copied successfully */ |
| 295 | m->version = (vma != get_gate_vma(task))? vma->vm_start: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame^] | 299 | static struct seq_operations proc_pid_maps_op = { |
| 300 | .start = m_start, |
| 301 | .next = m_next, |
| 302 | .stop = m_stop, |
| 303 | .show = show_map |
| 304 | }; |
| 305 | |
| 306 | static int maps_open(struct inode *inode, struct file *file) |
| 307 | { |
| 308 | return do_maps_open(inode, file, &proc_pid_maps_op); |
| 309 | } |
| 310 | |
| 311 | const struct file_operations proc_maps_operations = { |
| 312 | .open = maps_open, |
| 313 | .read = seq_read, |
| 314 | .llseek = seq_lseek, |
| 315 | .release = seq_release_private, |
| 316 | }; |
| 317 | |
| 318 | /* |
| 319 | * Proportional Set Size(PSS): my share of RSS. |
| 320 | * |
| 321 | * PSS of a process is the count of pages it has in memory, where each |
| 322 | * page is divided by the number of processes sharing it. So if a |
| 323 | * process has 1000 pages all to itself, and 1000 shared with one other |
| 324 | * process, its PSS will be 1500. |
| 325 | * |
| 326 | * To keep (accumulated) division errors low, we adopt a 64bit |
| 327 | * fixed-point pss counter to minimize division errors. So (pss >> |
| 328 | * PSS_SHIFT) would be the real byte count. |
| 329 | * |
| 330 | * A shift of 12 before division means (assuming 4K page size): |
| 331 | * - 1M 3-user-pages add up to 8KB errors; |
| 332 | * - supports mapcount up to 2^24, or 16M; |
| 333 | * - supports PSS up to 2^52 bytes, or 4PB. |
| 334 | */ |
| 335 | #define PSS_SHIFT 12 |
| 336 | |
| 337 | struct mem_size_stats |
| 338 | { |
| 339 | struct vm_area_struct *vma; |
| 340 | unsigned long resident; |
| 341 | unsigned long shared_clean; |
| 342 | unsigned long shared_dirty; |
| 343 | unsigned long private_clean; |
| 344 | unsigned long private_dirty; |
| 345 | unsigned long referenced; |
| 346 | u64 pss; |
| 347 | }; |
| 348 | |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 349 | static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, |
| 350 | void *private) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 351 | { |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 352 | struct mem_size_stats *mss = private; |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 353 | struct vm_area_struct *vma = mss->vma; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 354 | pte_t *pte, ptent; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 355 | spinlock_t *ptl; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 356 | struct page *page; |
Fengguang Wu | ec4dd3e | 2008-02-04 22:28:56 -0800 | [diff] [blame] | 357 | int mapcount; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 358 | |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 359 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 360 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 361 | ptent = *pte; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 362 | if (!pte_present(ptent)) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 363 | continue; |
| 364 | |
| 365 | mss->resident += PAGE_SIZE; |
Nick Piggin | ad820c5d | 2006-03-06 15:42:58 -0800 | [diff] [blame] | 366 | |
| 367 | page = vm_normal_page(vma, addr, ptent); |
| 368 | if (!page) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 369 | continue; |
| 370 | |
David Rientjes | f79f177 | 2007-05-06 14:49:22 -0700 | [diff] [blame] | 371 | /* Accumulate the size in pages that have been accessed. */ |
| 372 | if (pte_young(ptent) || PageReferenced(page)) |
| 373 | mss->referenced += PAGE_SIZE; |
Fengguang Wu | ec4dd3e | 2008-02-04 22:28:56 -0800 | [diff] [blame] | 374 | mapcount = page_mapcount(page); |
| 375 | if (mapcount >= 2) { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 376 | if (pte_dirty(ptent)) |
| 377 | mss->shared_dirty += PAGE_SIZE; |
| 378 | else |
| 379 | mss->shared_clean += PAGE_SIZE; |
Fengguang Wu | ec4dd3e | 2008-02-04 22:28:56 -0800 | [diff] [blame] | 380 | mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 381 | } else { |
| 382 | if (pte_dirty(ptent)) |
| 383 | mss->private_dirty += PAGE_SIZE; |
| 384 | else |
| 385 | mss->private_clean += PAGE_SIZE; |
Fengguang Wu | ec4dd3e | 2008-02-04 22:28:56 -0800 | [diff] [blame] | 386 | mss->pss += (PAGE_SIZE << PSS_SHIFT); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 387 | } |
David Rientjes | 826fad1 | 2007-05-06 14:49:21 -0700 | [diff] [blame] | 388 | } |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 389 | pte_unmap_unlock(pte - 1, ptl); |
| 390 | cond_resched(); |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 391 | return 0; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 394 | static struct mm_walk smaps_walk = { .pmd_entry = smaps_pte_range }; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 395 | |
| 396 | static int show_smap(struct seq_file *m, void *v) |
| 397 | { |
| 398 | struct vm_area_struct *vma = v; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 399 | struct mem_size_stats mss; |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 400 | int ret; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 401 | |
| 402 | memset(&mss, 0, sizeof mss); |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 403 | mss.vma = vma; |
Nick Piggin | 5ddfae1 | 2006-03-06 15:42:57 -0800 | [diff] [blame] | 404 | if (vma->vm_mm && !is_vm_hugetlb_page(vma)) |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 405 | walk_page_range(vma->vm_mm, vma->vm_start, vma->vm_end, |
| 406 | &smaps_walk, &mss); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 407 | |
| 408 | ret = show_map(m, v); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | |
| 412 | seq_printf(m, |
| 413 | "Size: %8lu kB\n" |
| 414 | "Rss: %8lu kB\n" |
| 415 | "Pss: %8lu kB\n" |
| 416 | "Shared_Clean: %8lu kB\n" |
| 417 | "Shared_Dirty: %8lu kB\n" |
| 418 | "Private_Clean: %8lu kB\n" |
| 419 | "Private_Dirty: %8lu kB\n" |
| 420 | "Referenced: %8lu kB\n", |
| 421 | (vma->vm_end - vma->vm_start) >> 10, |
| 422 | mss.resident >> 10, |
| 423 | (unsigned long)(mss.pss >> (10 + PSS_SHIFT)), |
| 424 | mss.shared_clean >> 10, |
| 425 | mss.shared_dirty >> 10, |
| 426 | mss.private_clean >> 10, |
| 427 | mss.private_dirty >> 10, |
| 428 | mss.referenced >> 10); |
| 429 | |
| 430 | return ret; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame^] | 433 | static struct seq_operations proc_pid_smaps_op = { |
| 434 | .start = m_start, |
| 435 | .next = m_next, |
| 436 | .stop = m_stop, |
| 437 | .show = show_smap |
| 438 | }; |
| 439 | |
| 440 | static int smaps_open(struct inode *inode, struct file *file) |
| 441 | { |
| 442 | return do_maps_open(inode, file, &proc_pid_smaps_op); |
| 443 | } |
| 444 | |
| 445 | const struct file_operations proc_smaps_operations = { |
| 446 | .open = smaps_open, |
| 447 | .read = seq_read, |
| 448 | .llseek = seq_lseek, |
| 449 | .release = seq_release_private, |
| 450 | }; |
| 451 | |
| 452 | static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, |
| 453 | unsigned long end, void *private) |
| 454 | { |
| 455 | struct vm_area_struct *vma = private; |
| 456 | pte_t *pte, ptent; |
| 457 | spinlock_t *ptl; |
| 458 | struct page *page; |
| 459 | |
| 460 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 461 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
| 462 | ptent = *pte; |
| 463 | if (!pte_present(ptent)) |
| 464 | continue; |
| 465 | |
| 466 | page = vm_normal_page(vma, addr, ptent); |
| 467 | if (!page) |
| 468 | continue; |
| 469 | |
| 470 | /* Clear accessed and referenced bits. */ |
| 471 | ptep_test_and_clear_young(vma, addr, pte); |
| 472 | ClearPageReferenced(page); |
| 473 | } |
| 474 | pte_unmap_unlock(pte - 1, ptl); |
| 475 | cond_resched(); |
| 476 | return 0; |
| 477 | } |
| 478 | |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 479 | static struct mm_walk clear_refs_walk = { .pmd_entry = clear_refs_pte_range }; |
| 480 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 481 | static ssize_t clear_refs_write(struct file *file, const char __user *buf, |
| 482 | size_t count, loff_t *ppos) |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 483 | { |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 484 | struct task_struct *task; |
| 485 | char buffer[PROC_NUMBUF], *end; |
| 486 | struct mm_struct *mm; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 487 | struct vm_area_struct *vma; |
| 488 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 489 | memset(buffer, 0, sizeof(buffer)); |
| 490 | if (count > sizeof(buffer) - 1) |
| 491 | count = sizeof(buffer) - 1; |
| 492 | if (copy_from_user(buffer, buf, count)) |
| 493 | return -EFAULT; |
| 494 | if (!simple_strtol(buffer, &end, 0)) |
| 495 | return -EINVAL; |
| 496 | if (*end == '\n') |
| 497 | end++; |
| 498 | task = get_proc_task(file->f_path.dentry->d_inode); |
| 499 | if (!task) |
| 500 | return -ESRCH; |
| 501 | mm = get_task_mm(task); |
| 502 | if (mm) { |
| 503 | down_read(&mm->mmap_sem); |
| 504 | for (vma = mm->mmap; vma; vma = vma->vm_next) |
| 505 | if (!is_vm_hugetlb_page(vma)) |
| 506 | walk_page_range(mm, vma->vm_start, vma->vm_end, |
| 507 | &clear_refs_walk, vma); |
| 508 | flush_tlb_mm(mm); |
| 509 | up_read(&mm->mmap_sem); |
| 510 | mmput(mm); |
| 511 | } |
| 512 | put_task_struct(task); |
| 513 | if (end - buffer == 0) |
| 514 | return -EIO; |
| 515 | return end - buffer; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 518 | const struct file_operations proc_clear_refs_operations = { |
| 519 | .write = clear_refs_write, |
| 520 | }; |
| 521 | |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 522 | #ifdef CONFIG_NUMA |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 523 | extern int show_numa_map(struct seq_file *m, void *v); |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 524 | |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 525 | static int show_numa_map_checked(struct seq_file *m, void *v) |
| 526 | { |
| 527 | struct proc_maps_private *priv = m->private; |
| 528 | struct task_struct *task = priv->task; |
| 529 | |
| 530 | if (maps_protect && !ptrace_may_attach(task)) |
| 531 | return -EACCES; |
| 532 | |
| 533 | return show_numa_map(m, v); |
| 534 | } |
| 535 | |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 536 | static struct seq_operations proc_pid_numa_maps_op = { |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 537 | .start = m_start, |
| 538 | .next = m_next, |
| 539 | .stop = m_stop, |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 540 | .show = show_numa_map_checked |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 541 | }; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 542 | |
| 543 | static int numa_maps_open(struct inode *inode, struct file *file) |
| 544 | { |
| 545 | return do_maps_open(inode, file, &proc_pid_numa_maps_op); |
| 546 | } |
| 547 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 548 | const struct file_operations proc_numa_maps_operations = { |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 549 | .open = numa_maps_open, |
| 550 | .read = seq_read, |
| 551 | .llseek = seq_lseek, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 552 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 553 | }; |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 554 | #endif |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 555 | |