Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/mm.h> |
| 2 | #include <linux/hugetlb.h> |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 3 | #include <linux/huge_mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/mount.h> |
| 5 | #include <linux/seq_file.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 6 | #include <linux/highmem.h> |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 7 | #include <linux/ptrace.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 9 | #include <linux/pagemap.h> |
| 10 | #include <linux/mempolicy.h> |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 11 | #include <linux/rmap.h> |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 12 | #include <linux/swap.h> |
| 13 | #include <linux/swapops.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/elf.h> |
| 16 | #include <asm/uaccess.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 17 | #include <asm/tlbflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 20 | void task_mem(struct seq_file *m, struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 22 | unsigned long data, text, lib, swap; |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 23 | unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; |
| 24 | |
| 25 | /* |
| 26 | * Note: to minimize their overhead, mm maintains hiwater_vm and |
| 27 | * hiwater_rss only when about to *lower* total_vm or rss. Any |
| 28 | * collector of these hiwater stats must therefore get total_vm |
| 29 | * and rss too, which will usually be the higher. Barriers? not |
| 30 | * worth the effort, such snapshots can always be inconsistent. |
| 31 | */ |
| 32 | hiwater_vm = total_vm = mm->total_vm; |
| 33 | if (hiwater_vm < mm->hiwater_vm) |
| 34 | hiwater_vm = mm->hiwater_vm; |
| 35 | hiwater_rss = total_rss = get_mm_rss(mm); |
| 36 | if (hiwater_rss < mm->hiwater_rss) |
| 37 | hiwater_rss = mm->hiwater_rss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | data = mm->total_vm - mm->shared_vm - mm->stack_vm; |
| 40 | text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10; |
| 41 | lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text; |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 42 | swap = get_mm_counter(mm, MM_SWAPENTS); |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 43 | seq_printf(m, |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 44 | "VmPeak:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | "VmSize:\t%8lu kB\n" |
| 46 | "VmLck:\t%8lu kB\n" |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 47 | "VmPin:\t%8lu kB\n" |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 48 | "VmHWM:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | "VmRSS:\t%8lu kB\n" |
| 50 | "VmData:\t%8lu kB\n" |
| 51 | "VmStk:\t%8lu kB\n" |
| 52 | "VmExe:\t%8lu kB\n" |
| 53 | "VmLib:\t%8lu kB\n" |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 54 | "VmPTE:\t%8lu kB\n" |
| 55 | "VmSwap:\t%8lu kB\n", |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 56 | hiwater_vm << (PAGE_SHIFT-10), |
| 57 | (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | mm->locked_vm << (PAGE_SHIFT-10), |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 59 | mm->pinned_vm << (PAGE_SHIFT-10), |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 60 | hiwater_rss << (PAGE_SHIFT-10), |
| 61 | total_rss << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | data << (PAGE_SHIFT-10), |
| 63 | mm->stack_vm << (PAGE_SHIFT-10), text, lib, |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 64 | (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10, |
| 65 | swap << (PAGE_SHIFT-10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | unsigned long task_vsize(struct mm_struct *mm) |
| 69 | { |
| 70 | return PAGE_SIZE * mm->total_vm; |
| 71 | } |
| 72 | |
Alexey Dobriyan | a2ade7b | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 73 | unsigned long task_statm(struct mm_struct *mm, |
| 74 | unsigned long *shared, unsigned long *text, |
| 75 | unsigned long *data, unsigned long *resident) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 77 | *shared = get_mm_counter(mm, MM_FILEPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) |
| 79 | >> PAGE_SHIFT; |
| 80 | *data = mm->total_vm - mm->shared_vm; |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 81 | *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return mm->total_vm; |
| 83 | } |
| 84 | |
Colin Cross | a9e6b18 | 2013-06-26 17:26:01 -0700 | [diff] [blame] | 85 | static void seq_print_vma_name(struct seq_file *m, struct vm_area_struct *vma) |
| 86 | { |
| 87 | const char __user *name = vma_get_anon_name(vma); |
| 88 | struct mm_struct *mm = vma->vm_mm; |
| 89 | |
| 90 | unsigned long page_start_vaddr; |
| 91 | unsigned long page_offset; |
| 92 | unsigned long num_pages; |
| 93 | unsigned long max_len = NAME_MAX; |
| 94 | int i; |
| 95 | |
| 96 | page_start_vaddr = (unsigned long)name & PAGE_MASK; |
| 97 | page_offset = (unsigned long)name - page_start_vaddr; |
| 98 | num_pages = DIV_ROUND_UP(page_offset + max_len, PAGE_SIZE); |
| 99 | |
| 100 | seq_puts(m, "[anon:"); |
| 101 | |
| 102 | for (i = 0; i < num_pages; i++) { |
| 103 | int len; |
| 104 | int write_len; |
| 105 | const char *kaddr; |
| 106 | long pages_pinned; |
| 107 | struct page *page; |
| 108 | |
| 109 | pages_pinned = get_user_pages(current, mm, page_start_vaddr, |
| 110 | 1, 0, 0, &page, NULL); |
| 111 | if (pages_pinned < 1) { |
| 112 | seq_puts(m, "<fault>]"); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | kaddr = (const char *)kmap(page); |
| 117 | len = min(max_len, PAGE_SIZE - page_offset); |
| 118 | write_len = strnlen(kaddr + page_offset, len); |
| 119 | seq_write(m, kaddr + page_offset, write_len); |
| 120 | kunmap(page); |
| 121 | put_page(page); |
| 122 | |
| 123 | /* if strnlen hit a null terminator then we're done */ |
| 124 | if (write_len != len) |
| 125 | break; |
| 126 | |
| 127 | max_len -= len; |
| 128 | page_offset = 0; |
| 129 | page_start_vaddr += PAGE_SIZE; |
| 130 | } |
| 131 | |
| 132 | seq_putc(m, ']'); |
| 133 | } |
| 134 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 135 | 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] | 136 | { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 137 | if (vma && vma != priv->tail_vma) { |
| 138 | struct mm_struct *mm = vma->vm_mm; |
| 139 | up_read(&mm->mmap_sem); |
| 140 | mmput(mm); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 145 | { |
| 146 | struct proc_maps_private *priv = m->private; |
| 147 | unsigned long last_addr = m->version; |
| 148 | struct mm_struct *mm; |
| 149 | struct vm_area_struct *vma, *tail_vma = NULL; |
| 150 | loff_t l = *pos; |
| 151 | |
| 152 | /* Clear the per syscall fields in priv */ |
| 153 | priv->task = NULL; |
| 154 | priv->tail_vma = NULL; |
| 155 | |
| 156 | /* |
| 157 | * We remember last_addr rather than next_addr to hit with |
| 158 | * mmap_cache most of the time. We have zero last_addr at |
| 159 | * the beginning and also after lseek. We will have -1 last_addr |
| 160 | * after the end of the vmas. |
| 161 | */ |
| 162 | |
| 163 | if (last_addr == -1UL) |
| 164 | return NULL; |
| 165 | |
| 166 | priv->task = get_pid_task(priv->pid, PIDTYPE_PID); |
| 167 | if (!priv->task) |
Al Viro | ec6fd8a | 2011-02-15 22:22:54 -0500 | [diff] [blame] | 168 | return ERR_PTR(-ESRCH); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 169 | |
| 170 | mm = mm_for_maps(priv->task); |
Al Viro | ec6fd8a | 2011-02-15 22:22:54 -0500 | [diff] [blame] | 171 | if (!mm || IS_ERR(mm)) |
| 172 | return mm; |
Oleg Nesterov | 00f89d2 | 2009-07-10 03:27:38 +0200 | [diff] [blame] | 173 | down_read(&mm->mmap_sem); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 174 | |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 175 | tail_vma = get_gate_vma(priv->task->mm); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 176 | priv->tail_vma = tail_vma; |
| 177 | |
| 178 | /* Start with last addr hint */ |
| 179 | vma = find_vma(mm, last_addr); |
| 180 | if (last_addr && vma) { |
| 181 | vma = vma->vm_next; |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Check the vma index is within the range and do |
| 187 | * sequential scan until m_index. |
| 188 | */ |
| 189 | vma = NULL; |
| 190 | if ((unsigned long)l < mm->map_count) { |
| 191 | vma = mm->mmap; |
| 192 | while (l-- && vma) |
| 193 | vma = vma->vm_next; |
| 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | if (l != mm->map_count) |
| 198 | tail_vma = NULL; /* After gate vma */ |
| 199 | |
| 200 | out: |
| 201 | if (vma) |
| 202 | return vma; |
| 203 | |
| 204 | /* End of vmas has been reached */ |
| 205 | m->version = (tail_vma != NULL)? 0: -1UL; |
| 206 | up_read(&mm->mmap_sem); |
| 207 | mmput(mm); |
| 208 | return tail_vma; |
| 209 | } |
| 210 | |
| 211 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 212 | { |
| 213 | struct proc_maps_private *priv = m->private; |
| 214 | struct vm_area_struct *vma = v; |
| 215 | struct vm_area_struct *tail_vma = priv->tail_vma; |
| 216 | |
| 217 | (*pos)++; |
| 218 | if (vma && (vma != tail_vma) && vma->vm_next) |
| 219 | return vma->vm_next; |
| 220 | vma_stop(priv, vma); |
| 221 | return (vma != tail_vma)? tail_vma: NULL; |
| 222 | } |
| 223 | |
| 224 | static void m_stop(struct seq_file *m, void *v) |
| 225 | { |
| 226 | struct proc_maps_private *priv = m->private; |
| 227 | struct vm_area_struct *vma = v; |
| 228 | |
Linus Torvalds | 76597cd | 2011-03-27 19:09:29 -0700 | [diff] [blame] | 229 | if (!IS_ERR(vma)) |
| 230 | vma_stop(priv, vma); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 231 | if (priv->task) |
| 232 | put_task_struct(priv->task); |
| 233 | } |
| 234 | |
| 235 | static int do_maps_open(struct inode *inode, struct file *file, |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 236 | const struct seq_operations *ops) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 237 | { |
| 238 | struct proc_maps_private *priv; |
| 239 | int ret = -ENOMEM; |
| 240 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 241 | if (priv) { |
| 242 | priv->pid = proc_pid(inode); |
| 243 | ret = seq_open(file, ops); |
| 244 | if (!ret) { |
| 245 | struct seq_file *m = file->private_data; |
| 246 | m->private = priv; |
| 247 | } else { |
| 248 | kfree(priv); |
| 249 | } |
| 250 | } |
| 251 | return ret; |
| 252 | } |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 253 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 254 | static void |
| 255 | show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 257 | struct mm_struct *mm = vma->vm_mm; |
| 258 | struct file *file = vma->vm_file; |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 259 | struct proc_maps_private *priv = m->private; |
| 260 | struct task_struct *task = priv->task; |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 261 | vm_flags_t flags = vma->vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | unsigned long ino = 0; |
KAMEZAWA Hiroyuki | 6260a4b | 2009-04-06 19:00:30 -0700 | [diff] [blame] | 263 | unsigned long long pgoff = 0; |
Mikulas Patocka | a09a79f | 2011-05-09 13:01:09 +0200 | [diff] [blame] | 264 | unsigned long start, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | dev_t dev = 0; |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 266 | const char *name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | if (file) { |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 269 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | dev = inode->i_sb->s_dev; |
| 271 | ino = inode->i_ino; |
KAMEZAWA Hiroyuki | 6260a4b | 2009-04-06 19:00:30 -0700 | [diff] [blame] | 272 | pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Linus Torvalds | d782437 | 2010-08-15 11:35:52 -0700 | [diff] [blame] | 275 | /* We don't show the stack guard page in /proc/maps */ |
| 276 | start = vma->vm_start; |
Mikulas Patocka | a09a79f | 2011-05-09 13:01:09 +0200 | [diff] [blame] | 277 | end = vma->vm_end; |
Linus Torvalds | d782437 | 2010-08-15 11:35:52 -0700 | [diff] [blame] | 278 | |
Tetsuo Handa | be8bd60 | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 279 | seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); |
| 280 | seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ", |
Linus Torvalds | d782437 | 2010-08-15 11:35:52 -0700 | [diff] [blame] | 281 | start, |
Mikulas Patocka | a09a79f | 2011-05-09 13:01:09 +0200 | [diff] [blame] | 282 | end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | flags & VM_READ ? 'r' : '-', |
| 284 | flags & VM_WRITE ? 'w' : '-', |
| 285 | flags & VM_EXEC ? 'x' : '-', |
| 286 | flags & VM_MAYSHARE ? 's' : 'p', |
KAMEZAWA Hiroyuki | 6260a4b | 2009-04-06 19:00:30 -0700 | [diff] [blame] | 287 | pgoff, |
Tetsuo Handa | be8bd60 | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 288 | MAJOR(dev), MINOR(dev), ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Print the dentry name for named mappings, and a |
| 292 | * special [heap] marker for the heap: |
| 293 | */ |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 294 | if (file) { |
Tetsuo Handa | be8bd60 | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 295 | seq_pad(m, ' '); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 296 | seq_path(m, &file->f_path, "\n"); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 297 | goto done; |
| 298 | } |
| 299 | |
| 300 | name = arch_vma_name(vma); |
| 301 | if (!name) { |
| 302 | pid_t tid; |
| 303 | |
| 304 | if (!mm) { |
| 305 | name = "[vdso]"; |
| 306 | goto done; |
| 307 | } |
| 308 | |
| 309 | if (vma->vm_start <= mm->brk && |
| 310 | vma->vm_end >= mm->start_brk) { |
| 311 | name = "[heap]"; |
| 312 | goto done; |
| 313 | } |
| 314 | |
| 315 | tid = vm_is_stack(task, vma, is_pid); |
| 316 | |
| 317 | if (tid != 0) { |
| 318 | /* |
| 319 | * Thread stack in /proc/PID/task/TID/maps or |
| 320 | * the main process stack. |
| 321 | */ |
| 322 | if (!is_pid || (vma->vm_start <= mm->start_stack && |
| 323 | vma->vm_end >= mm->start_stack)) { |
| 324 | name = "[stack]"; |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 325 | } else { |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 326 | /* Thread stack in /proc/PID/maps */ |
Tetsuo Handa | be8bd60 | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 327 | seq_pad(m, ' '); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 328 | seq_printf(m, "[stack:%d]", tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
Colin Cross | a9e6b18 | 2013-06-26 17:26:01 -0700 | [diff] [blame] | 330 | goto done; |
| 331 | } |
| 332 | |
| 333 | if (vma_get_anon_name(vma)) { |
| 334 | seq_pad(m, ' '); |
| 335 | seq_print_vma_name(m, vma); |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 336 | } |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | done: |
| 340 | if (name) { |
Tetsuo Handa | be8bd60 | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 341 | seq_pad(m, ' '); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 342 | seq_puts(m, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | seq_putc(m, '\n'); |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 345 | } |
| 346 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 347 | static int show_map(struct seq_file *m, void *v, int is_pid) |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 348 | { |
| 349 | struct vm_area_struct *vma = v; |
| 350 | struct proc_maps_private *priv = m->private; |
| 351 | struct task_struct *task = priv->task; |
| 352 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 353 | show_map_vma(m, vma, is_pid); |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 354 | |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 355 | if (m->count < m->size) /* vma is copied successfully */ |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 356 | m->version = (vma != get_gate_vma(task->mm)) |
| 357 | ? vma->vm_start : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 361 | static int show_pid_map(struct seq_file *m, void *v) |
| 362 | { |
| 363 | return show_map(m, v, 1); |
| 364 | } |
| 365 | |
| 366 | static int show_tid_map(struct seq_file *m, void *v) |
| 367 | { |
| 368 | return show_map(m, v, 0); |
| 369 | } |
| 370 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 371 | static const struct seq_operations proc_pid_maps_op = { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 372 | .start = m_start, |
| 373 | .next = m_next, |
| 374 | .stop = m_stop, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 375 | .show = show_pid_map |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 376 | }; |
| 377 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 378 | static const struct seq_operations proc_tid_maps_op = { |
| 379 | .start = m_start, |
| 380 | .next = m_next, |
| 381 | .stop = m_stop, |
| 382 | .show = show_tid_map |
| 383 | }; |
| 384 | |
| 385 | static int pid_maps_open(struct inode *inode, struct file *file) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 386 | { |
| 387 | return do_maps_open(inode, file, &proc_pid_maps_op); |
| 388 | } |
| 389 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 390 | static int tid_maps_open(struct inode *inode, struct file *file) |
| 391 | { |
| 392 | return do_maps_open(inode, file, &proc_tid_maps_op); |
| 393 | } |
| 394 | |
| 395 | const struct file_operations proc_pid_maps_operations = { |
| 396 | .open = pid_maps_open, |
| 397 | .read = seq_read, |
| 398 | .llseek = seq_lseek, |
| 399 | .release = seq_release_private, |
| 400 | }; |
| 401 | |
| 402 | const struct file_operations proc_tid_maps_operations = { |
| 403 | .open = tid_maps_open, |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 404 | .read = seq_read, |
| 405 | .llseek = seq_lseek, |
| 406 | .release = seq_release_private, |
| 407 | }; |
| 408 | |
| 409 | /* |
| 410 | * Proportional Set Size(PSS): my share of RSS. |
| 411 | * |
| 412 | * PSS of a process is the count of pages it has in memory, where each |
| 413 | * page is divided by the number of processes sharing it. So if a |
| 414 | * process has 1000 pages all to itself, and 1000 shared with one other |
| 415 | * process, its PSS will be 1500. |
| 416 | * |
| 417 | * To keep (accumulated) division errors low, we adopt a 64bit |
| 418 | * fixed-point pss counter to minimize division errors. So (pss >> |
| 419 | * PSS_SHIFT) would be the real byte count. |
| 420 | * |
| 421 | * A shift of 12 before division means (assuming 4K page size): |
| 422 | * - 1M 3-user-pages add up to 8KB errors; |
| 423 | * - supports mapcount up to 2^24, or 16M; |
| 424 | * - supports PSS up to 2^52 bytes, or 4PB. |
| 425 | */ |
| 426 | #define PSS_SHIFT 12 |
| 427 | |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 428 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 429 | struct mem_size_stats { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 430 | struct vm_area_struct *vma; |
| 431 | unsigned long resident; |
| 432 | unsigned long shared_clean; |
| 433 | unsigned long shared_dirty; |
| 434 | unsigned long private_clean; |
| 435 | unsigned long private_dirty; |
| 436 | unsigned long referenced; |
Nikanth Karthikesan | b40d4f8 | 2010-10-27 15:34:10 -0700 | [diff] [blame] | 437 | unsigned long anonymous; |
Dave Hansen | 4031a21 | 2011-03-22 16:33:01 -0700 | [diff] [blame] | 438 | unsigned long anonymous_thp; |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 439 | unsigned long swap; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 440 | u64 pss; |
| 441 | }; |
| 442 | |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 443 | |
| 444 | static void smaps_pte_entry(pte_t ptent, unsigned long addr, |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 445 | unsigned long ptent_size, struct mm_walk *walk) |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 446 | { |
| 447 | struct mem_size_stats *mss = walk->private; |
| 448 | struct vm_area_struct *vma = mss->vma; |
| 449 | struct page *page; |
| 450 | int mapcount; |
| 451 | |
| 452 | if (is_swap_pte(ptent)) { |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 453 | mss->swap += ptent_size; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (!pte_present(ptent)) |
| 458 | return; |
| 459 | |
| 460 | page = vm_normal_page(vma, addr, ptent); |
| 461 | if (!page) |
| 462 | return; |
| 463 | |
| 464 | if (PageAnon(page)) |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 465 | mss->anonymous += ptent_size; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 466 | |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 467 | mss->resident += ptent_size; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 468 | /* Accumulate the size in pages that have been accessed. */ |
| 469 | if (pte_young(ptent) || PageReferenced(page)) |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 470 | mss->referenced += ptent_size; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 471 | mapcount = page_mapcount(page); |
| 472 | if (mapcount >= 2) { |
| 473 | if (pte_dirty(ptent) || PageDirty(page)) |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 474 | mss->shared_dirty += ptent_size; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 475 | else |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 476 | mss->shared_clean += ptent_size; |
| 477 | mss->pss += (ptent_size << PSS_SHIFT) / mapcount; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 478 | } else { |
| 479 | if (pte_dirty(ptent) || PageDirty(page)) |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 480 | mss->private_dirty += ptent_size; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 481 | else |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 482 | mss->private_clean += ptent_size; |
| 483 | mss->pss += (ptent_size << PSS_SHIFT); |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 487 | static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 488 | struct mm_walk *walk) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 489 | { |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 490 | struct mem_size_stats *mss = walk->private; |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 491 | struct vm_area_struct *vma = mss->vma; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 492 | pte_t *pte; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 493 | spinlock_t *ptl; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 494 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 495 | if (pmd_trans_huge_lock(pmd, vma) == 1) { |
| 496 | smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk); |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 497 | spin_unlock(&walk->mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 498 | mss->anonymous_thp += HPAGE_PMD_SIZE; |
| 499 | return 0; |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 500 | } |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 501 | |
| 502 | if (pmd_trans_unstable(pmd)) |
| 503 | return 0; |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 504 | /* |
| 505 | * The mmap_sem held all the way back in m_start() is what |
| 506 | * keeps khugepaged out of here and from collapsing things |
| 507 | * in here. |
| 508 | */ |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 509 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 510 | for (; addr != end; pte++, addr += PAGE_SIZE) |
Dave Hansen | 3c9acc7 | 2011-03-22 16:32:59 -0700 | [diff] [blame] | 511 | smaps_pte_entry(*pte, addr, PAGE_SIZE, walk); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 512 | pte_unmap_unlock(pte - 1, ptl); |
| 513 | cond_resched(); |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 514 | return 0; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 517 | static int show_smap(struct seq_file *m, void *v, int is_pid) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 518 | { |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 519 | struct proc_maps_private *priv = m->private; |
| 520 | struct task_struct *task = priv->task; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 521 | struct vm_area_struct *vma = v; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 522 | struct mem_size_stats mss; |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 523 | struct mm_walk smaps_walk = { |
| 524 | .pmd_entry = smaps_pte_range, |
| 525 | .mm = vma->vm_mm, |
| 526 | .private = &mss, |
| 527 | }; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 528 | |
| 529 | memset(&mss, 0, sizeof mss); |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 530 | mss.vma = vma; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 531 | /* mmap_sem is held in m_start */ |
Nick Piggin | 5ddfae1 | 2006-03-06 15:42:57 -0800 | [diff] [blame] | 532 | if (vma->vm_mm && !is_vm_hugetlb_page(vma)) |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 533 | walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 534 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 535 | show_map_vma(m, vma, is_pid); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 536 | |
| 537 | seq_printf(m, |
| 538 | "Size: %8lu kB\n" |
| 539 | "Rss: %8lu kB\n" |
| 540 | "Pss: %8lu kB\n" |
| 541 | "Shared_Clean: %8lu kB\n" |
| 542 | "Shared_Dirty: %8lu kB\n" |
| 543 | "Private_Clean: %8lu kB\n" |
| 544 | "Private_Dirty: %8lu kB\n" |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 545 | "Referenced: %8lu kB\n" |
Nikanth Karthikesan | b40d4f8 | 2010-10-27 15:34:10 -0700 | [diff] [blame] | 546 | "Anonymous: %8lu kB\n" |
Dave Hansen | 4031a21 | 2011-03-22 16:33:01 -0700 | [diff] [blame] | 547 | "AnonHugePages: %8lu kB\n" |
Mel Gorman | 08fba69 | 2009-01-06 14:38:53 -0800 | [diff] [blame] | 548 | "Swap: %8lu kB\n" |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 549 | "KernelPageSize: %8lu kB\n" |
Nikanth Karthikesan | 2d90508 | 2011-01-13 15:45:53 -0800 | [diff] [blame] | 550 | "MMUPageSize: %8lu kB\n" |
| 551 | "Locked: %8lu kB\n", |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 552 | (vma->vm_end - vma->vm_start) >> 10, |
| 553 | mss.resident >> 10, |
| 554 | (unsigned long)(mss.pss >> (10 + PSS_SHIFT)), |
| 555 | mss.shared_clean >> 10, |
| 556 | mss.shared_dirty >> 10, |
| 557 | mss.private_clean >> 10, |
| 558 | mss.private_dirty >> 10, |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 559 | mss.referenced >> 10, |
Nikanth Karthikesan | b40d4f8 | 2010-10-27 15:34:10 -0700 | [diff] [blame] | 560 | mss.anonymous >> 10, |
Dave Hansen | 4031a21 | 2011-03-22 16:33:01 -0700 | [diff] [blame] | 561 | mss.anonymous_thp >> 10, |
Mel Gorman | 08fba69 | 2009-01-06 14:38:53 -0800 | [diff] [blame] | 562 | mss.swap >> 10, |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 563 | vma_kernel_pagesize(vma) >> 10, |
Nikanth Karthikesan | 2d90508 | 2011-01-13 15:45:53 -0800 | [diff] [blame] | 564 | vma_mmu_pagesize(vma) >> 10, |
| 565 | (vma->vm_flags & VM_LOCKED) ? |
| 566 | (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 567 | |
Colin Cross | a9e6b18 | 2013-06-26 17:26:01 -0700 | [diff] [blame] | 568 | if (vma_get_anon_name(vma)) { |
| 569 | seq_puts(m, "Name: "); |
| 570 | seq_print_vma_name(m, vma); |
| 571 | seq_putc(m, '\n'); |
| 572 | } |
| 573 | |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 574 | if (m->count < m->size) /* vma is copied successfully */ |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 575 | m->version = (vma != get_gate_vma(task->mm)) |
| 576 | ? vma->vm_start : 0; |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 577 | return 0; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 580 | static int show_pid_smap(struct seq_file *m, void *v) |
| 581 | { |
| 582 | return show_smap(m, v, 1); |
| 583 | } |
| 584 | |
| 585 | static int show_tid_smap(struct seq_file *m, void *v) |
| 586 | { |
| 587 | return show_smap(m, v, 0); |
| 588 | } |
| 589 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 590 | static const struct seq_operations proc_pid_smaps_op = { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 591 | .start = m_start, |
| 592 | .next = m_next, |
| 593 | .stop = m_stop, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 594 | .show = show_pid_smap |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 595 | }; |
| 596 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 597 | static const struct seq_operations proc_tid_smaps_op = { |
| 598 | .start = m_start, |
| 599 | .next = m_next, |
| 600 | .stop = m_stop, |
| 601 | .show = show_tid_smap |
| 602 | }; |
| 603 | |
| 604 | static int pid_smaps_open(struct inode *inode, struct file *file) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 605 | { |
| 606 | return do_maps_open(inode, file, &proc_pid_smaps_op); |
| 607 | } |
| 608 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 609 | static int tid_smaps_open(struct inode *inode, struct file *file) |
| 610 | { |
| 611 | return do_maps_open(inode, file, &proc_tid_smaps_op); |
| 612 | } |
| 613 | |
| 614 | const struct file_operations proc_pid_smaps_operations = { |
| 615 | .open = pid_smaps_open, |
| 616 | .read = seq_read, |
| 617 | .llseek = seq_lseek, |
| 618 | .release = seq_release_private, |
| 619 | }; |
| 620 | |
| 621 | const struct file_operations proc_tid_smaps_operations = { |
| 622 | .open = tid_smaps_open, |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 623 | .read = seq_read, |
| 624 | .llseek = seq_lseek, |
| 625 | .release = seq_release_private, |
| 626 | }; |
| 627 | |
| 628 | static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 629 | unsigned long end, struct mm_walk *walk) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 630 | { |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 631 | struct vm_area_struct *vma = walk->private; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 632 | pte_t *pte, ptent; |
| 633 | spinlock_t *ptl; |
| 634 | struct page *page; |
| 635 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 636 | split_huge_page_pmd(walk->mm, pmd); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 637 | if (pmd_trans_unstable(pmd)) |
| 638 | return 0; |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 639 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 640 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 641 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
| 642 | ptent = *pte; |
| 643 | if (!pte_present(ptent)) |
| 644 | continue; |
| 645 | |
| 646 | page = vm_normal_page(vma, addr, ptent); |
| 647 | if (!page) |
| 648 | continue; |
| 649 | |
| 650 | /* Clear accessed and referenced bits. */ |
| 651 | ptep_test_and_clear_young(vma, addr, pte); |
| 652 | ClearPageReferenced(page); |
| 653 | } |
| 654 | pte_unmap_unlock(pte - 1, ptl); |
| 655 | cond_resched(); |
| 656 | return 0; |
| 657 | } |
| 658 | |
Moussa A. Ba | 398499d | 2009-09-21 17:02:29 -0700 | [diff] [blame] | 659 | #define CLEAR_REFS_ALL 1 |
| 660 | #define CLEAR_REFS_ANON 2 |
| 661 | #define CLEAR_REFS_MAPPED 3 |
| 662 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 663 | static ssize_t clear_refs_write(struct file *file, const char __user *buf, |
| 664 | size_t count, loff_t *ppos) |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 665 | { |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 666 | struct task_struct *task; |
Vincent Li | fb92a4b | 2009-09-22 16:45:36 -0700 | [diff] [blame] | 667 | char buffer[PROC_NUMBUF]; |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 668 | struct mm_struct *mm; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 669 | struct vm_area_struct *vma; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 670 | int type; |
| 671 | int rv; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 672 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 673 | memset(buffer, 0, sizeof(buffer)); |
| 674 | if (count > sizeof(buffer) - 1) |
| 675 | count = sizeof(buffer) - 1; |
| 676 | if (copy_from_user(buffer, buf, count)) |
| 677 | return -EFAULT; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 678 | rv = kstrtoint(strstrip(buffer), 10, &type); |
| 679 | if (rv < 0) |
| 680 | return rv; |
Moussa A. Ba | 398499d | 2009-09-21 17:02:29 -0700 | [diff] [blame] | 681 | if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED) |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 682 | return -EINVAL; |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 683 | task = get_proc_task(file->f_path.dentry->d_inode); |
| 684 | if (!task) |
| 685 | return -ESRCH; |
| 686 | mm = get_task_mm(task); |
| 687 | if (mm) { |
Andrew Morton | 20cbc97 | 2008-07-05 12:29:05 -0700 | [diff] [blame] | 688 | struct mm_walk clear_refs_walk = { |
| 689 | .pmd_entry = clear_refs_pte_range, |
| 690 | .mm = mm, |
| 691 | }; |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 692 | down_read(&mm->mmap_sem); |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 693 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 694 | clear_refs_walk.private = vma; |
Moussa A. Ba | 398499d | 2009-09-21 17:02:29 -0700 | [diff] [blame] | 695 | if (is_vm_hugetlb_page(vma)) |
| 696 | continue; |
| 697 | /* |
| 698 | * Writing 1 to /proc/pid/clear_refs affects all pages. |
| 699 | * |
| 700 | * Writing 2 to /proc/pid/clear_refs only affects |
| 701 | * Anonymous pages. |
| 702 | * |
| 703 | * Writing 3 to /proc/pid/clear_refs only affects file |
| 704 | * mapped pages. |
| 705 | */ |
| 706 | if (type == CLEAR_REFS_ANON && vma->vm_file) |
| 707 | continue; |
| 708 | if (type == CLEAR_REFS_MAPPED && !vma->vm_file) |
| 709 | continue; |
| 710 | walk_page_range(vma->vm_start, vma->vm_end, |
| 711 | &clear_refs_walk); |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 712 | } |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 713 | flush_tlb_mm(mm); |
| 714 | up_read(&mm->mmap_sem); |
| 715 | mmput(mm); |
| 716 | } |
| 717 | put_task_struct(task); |
Vincent Li | fb92a4b | 2009-09-22 16:45:36 -0700 | [diff] [blame] | 718 | |
| 719 | return count; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 722 | const struct file_operations proc_clear_refs_operations = { |
| 723 | .write = clear_refs_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 724 | .llseek = noop_llseek, |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 725 | }; |
| 726 | |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 727 | typedef struct { |
| 728 | u64 pme; |
| 729 | } pagemap_entry_t; |
| 730 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 731 | struct pagemapread { |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 732 | int pos, len; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 733 | pagemap_entry_t *buffer; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 734 | }; |
| 735 | |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 736 | #define PAGEMAP_WALK_SIZE (PMD_SIZE) |
| 737 | #define PAGEMAP_WALK_MASK (PMD_MASK) |
| 738 | |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 739 | #define PM_ENTRY_BYTES sizeof(u64) |
| 740 | #define PM_STATUS_BITS 3 |
| 741 | #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS) |
| 742 | #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET) |
| 743 | #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK) |
| 744 | #define PM_PSHIFT_BITS 6 |
| 745 | #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS) |
| 746 | #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET) |
| 747 | #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK) |
| 748 | #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1) |
| 749 | #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK) |
| 750 | |
| 751 | #define PM_PRESENT PM_STATUS(4LL) |
| 752 | #define PM_SWAP PM_STATUS(2LL) |
| 753 | #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 754 | #define PM_END_OF_BUFFER 1 |
| 755 | |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 756 | static inline pagemap_entry_t make_pme(u64 val) |
| 757 | { |
| 758 | return (pagemap_entry_t) { .pme = val }; |
| 759 | } |
| 760 | |
| 761 | static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme, |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 762 | struct pagemapread *pm) |
| 763 | { |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 764 | pm->buffer[pm->pos++] = *pme; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 765 | if (pm->pos >= pm->len) |
Thomas Tuttle | aae8679 | 2008-06-05 22:46:31 -0700 | [diff] [blame] | 766 | return PM_END_OF_BUFFER; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int pagemap_pte_hole(unsigned long start, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 771 | struct mm_walk *walk) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 772 | { |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 773 | struct pagemapread *pm = walk->private; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 774 | unsigned long addr; |
| 775 | int err = 0; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 776 | pagemap_entry_t pme = make_pme(PM_NOT_PRESENT); |
| 777 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 778 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 779 | err = add_to_pagemap(addr, &pme, pm); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 780 | if (err) |
| 781 | break; |
| 782 | } |
| 783 | return err; |
| 784 | } |
| 785 | |
Adrian Bunk | 9d02dbc | 2008-04-28 02:12:11 -0700 | [diff] [blame] | 786 | static u64 swap_pte_to_pagemap_entry(pte_t pte) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 787 | { |
| 788 | swp_entry_t e = pte_to_swp_entry(pte); |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 789 | return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 792 | static void pte_to_pagemap_entry(pagemap_entry_t *pme, pte_t pte) |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 793 | { |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 794 | if (is_swap_pte(pte)) |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 795 | *pme = make_pme(PM_PFRAME(swap_pte_to_pagemap_entry(pte)) |
| 796 | | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP); |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 797 | else if (pte_present(pte)) |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 798 | *pme = make_pme(PM_PFRAME(pte_pfn(pte)) |
| 799 | | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT); |
Konstantin Khlebnikov | 16fbdce | 2012-05-10 13:01:43 -0700 | [diff] [blame] | 800 | else |
| 801 | *pme = make_pme(PM_NOT_PRESENT); |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 804 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 805 | static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, |
| 806 | pmd_t pmd, int offset) |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 807 | { |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 808 | /* |
| 809 | * Currently pmd for thp is always present because thp can not be |
| 810 | * swapped-out, migrated, or HWPOISONed (split in such cases instead.) |
| 811 | * This if-check is just to prepare for future implementation. |
| 812 | */ |
| 813 | if (pmd_present(pmd)) |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 814 | *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset) |
| 815 | | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT); |
Konstantin Khlebnikov | 16fbdce | 2012-05-10 13:01:43 -0700 | [diff] [blame] | 816 | else |
| 817 | *pme = make_pme(PM_NOT_PRESENT); |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 818 | } |
| 819 | #else |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 820 | static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, |
| 821 | pmd_t pmd, int offset) |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 822 | { |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 823 | } |
| 824 | #endif |
| 825 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 826 | static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 827 | struct mm_walk *walk) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 828 | { |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 829 | struct vm_area_struct *vma; |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 830 | struct pagemapread *pm = walk->private; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 831 | pte_t *pte; |
| 832 | int err = 0; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 833 | pagemap_entry_t pme = make_pme(PM_NOT_PRESENT); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 834 | |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 835 | /* find the first VMA at or above 'addr' */ |
| 836 | vma = find_vma(walk->mm, addr); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 837 | if (pmd_trans_huge_lock(pmd, vma) == 1) { |
| 838 | for (; addr != end; addr += PAGE_SIZE) { |
| 839 | unsigned long offset; |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 840 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 841 | offset = (addr & ~PAGEMAP_WALK_MASK) >> |
| 842 | PAGE_SHIFT; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 843 | thp_pmd_to_pagemap_entry(&pme, *pmd, offset); |
| 844 | err = add_to_pagemap(addr, &pme, pm); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 845 | if (err) |
| 846 | break; |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 847 | } |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 848 | spin_unlock(&walk->mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 849 | return err; |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Andrea Arcangeli | 45f83ce | 2012-03-28 14:42:40 -0700 | [diff] [blame] | 852 | if (pmd_trans_unstable(pmd)) |
| 853 | return 0; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 854 | for (; addr != end; addr += PAGE_SIZE) { |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 855 | |
| 856 | /* check to see if we've left 'vma' behind |
| 857 | * and need a new, higher one */ |
Konstantin Khlebnikov | 16fbdce | 2012-05-10 13:01:43 -0700 | [diff] [blame] | 858 | if (vma && (addr >= vma->vm_end)) { |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 859 | vma = find_vma(walk->mm, addr); |
Konstantin Khlebnikov | 16fbdce | 2012-05-10 13:01:43 -0700 | [diff] [blame] | 860 | pme = make_pme(PM_NOT_PRESENT); |
| 861 | } |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 862 | |
| 863 | /* check that 'vma' actually covers this address, |
| 864 | * and that it isn't a huge page vma */ |
| 865 | if (vma && (vma->vm_start <= addr) && |
| 866 | !is_vm_hugetlb_page(vma)) { |
| 867 | pte = pte_offset_map(pmd, addr); |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 868 | pte_to_pagemap_entry(&pme, *pte); |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 869 | /* unmap before userspace copy */ |
| 870 | pte_unmap(pte); |
| 871 | } |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 872 | err = add_to_pagemap(addr, &pme, pm); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 873 | if (err) |
| 874 | return err; |
| 875 | } |
| 876 | |
| 877 | cond_resched(); |
| 878 | |
| 879 | return err; |
| 880 | } |
| 881 | |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 882 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 883 | static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, |
| 884 | pte_t pte, int offset) |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 885 | { |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 886 | if (pte_present(pte)) |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 887 | *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) |
| 888 | | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT); |
Konstantin Khlebnikov | 16fbdce | 2012-05-10 13:01:43 -0700 | [diff] [blame] | 889 | else |
| 890 | *pme = make_pme(PM_NOT_PRESENT); |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 893 | /* This function walks within one hugetlb entry in the single call */ |
| 894 | static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask, |
| 895 | unsigned long addr, unsigned long end, |
| 896 | struct mm_walk *walk) |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 897 | { |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 898 | struct pagemapread *pm = walk->private; |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 899 | int err = 0; |
Konstantin Khlebnikov | 16fbdce | 2012-05-10 13:01:43 -0700 | [diff] [blame] | 900 | pagemap_entry_t pme; |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 901 | |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 902 | for (; addr != end; addr += PAGE_SIZE) { |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 903 | int offset = (addr & ~hmask) >> PAGE_SHIFT; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 904 | huge_pte_to_pagemap_entry(&pme, *pte, offset); |
| 905 | err = add_to_pagemap(addr, &pme, pm); |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 906 | if (err) |
| 907 | return err; |
| 908 | } |
| 909 | |
| 910 | cond_resched(); |
| 911 | |
| 912 | return err; |
| 913 | } |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 914 | #endif /* HUGETLB_PAGE */ |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 915 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 916 | /* |
| 917 | * /proc/pid/pagemap - an array mapping virtual pages to pfns |
| 918 | * |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 919 | * For each page in the address space, this file contains one 64-bit entry |
| 920 | * consisting of the following: |
| 921 | * |
| 922 | * Bits 0-55 page frame number (PFN) if present |
| 923 | * Bits 0-4 swap type if swapped |
| 924 | * Bits 5-55 swap offset if swapped |
| 925 | * Bits 55-60 page shift (page size = 1<<page shift) |
| 926 | * Bit 61 reserved for future use |
| 927 | * Bit 62 page swapped |
| 928 | * Bit 63 page present |
| 929 | * |
| 930 | * If the page is not present but in swap, then the PFN contains an |
| 931 | * encoding of the swap file number and the page's offset into the |
| 932 | * swap. Unmapped pages return a null PFN. This allows determining |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 933 | * precisely which pages are mapped (or in swap) and comparing mapped |
| 934 | * pages between processes. |
| 935 | * |
| 936 | * Efficient users of this interface will use /proc/pid/maps to |
| 937 | * determine which areas of memory are actually mapped and llseek to |
| 938 | * skip over unmapped regions. |
| 939 | */ |
| 940 | static ssize_t pagemap_read(struct file *file, char __user *buf, |
| 941 | size_t count, loff_t *ppos) |
| 942 | { |
| 943 | struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 944 | struct mm_struct *mm; |
| 945 | struct pagemapread pm; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 946 | int ret = -ESRCH; |
Alexey Dobriyan | ee1e6ab | 2008-07-21 14:21:36 -0700 | [diff] [blame] | 947 | struct mm_walk pagemap_walk = {}; |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 948 | unsigned long src; |
| 949 | unsigned long svpfn; |
| 950 | unsigned long start_vaddr; |
| 951 | unsigned long end_vaddr; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 952 | int copied = 0; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 953 | |
| 954 | if (!task) |
| 955 | goto out; |
| 956 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 957 | ret = -EINVAL; |
| 958 | /* file position must be aligned */ |
Thomas Tuttle | aae8679 | 2008-06-05 22:46:31 -0700 | [diff] [blame] | 959 | if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) |
Marcelo Tosatti | fb39380 | 2008-03-13 12:32:35 -0700 | [diff] [blame] | 960 | goto out_task; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 961 | |
| 962 | ret = 0; |
Vitaly Mayatskikh | 0816178 | 2009-04-30 15:08:18 -0700 | [diff] [blame] | 963 | if (!count) |
| 964 | goto out_task; |
| 965 | |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 966 | pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); |
| 967 | pm.buffer = kmalloc(pm.len, GFP_TEMPORARY); |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 968 | ret = -ENOMEM; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 969 | if (!pm.buffer) |
KOSAKI Motohiro | 98bc93e | 2011-05-26 16:25:53 -0700 | [diff] [blame] | 970 | goto out_task; |
| 971 | |
| 972 | mm = mm_for_maps(task); |
| 973 | ret = PTR_ERR(mm); |
| 974 | if (!mm || IS_ERR(mm)) |
| 975 | goto out_free; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 976 | |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 977 | pagemap_walk.pmd_entry = pagemap_pte_range; |
| 978 | pagemap_walk.pte_hole = pagemap_pte_hole; |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 979 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 980 | pagemap_walk.hugetlb_entry = pagemap_hugetlb_range; |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 981 | #endif |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 982 | pagemap_walk.mm = mm; |
| 983 | pagemap_walk.private = ± |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 984 | |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 985 | src = *ppos; |
| 986 | svpfn = src / PM_ENTRY_BYTES; |
| 987 | start_vaddr = svpfn << PAGE_SHIFT; |
| 988 | end_vaddr = TASK_SIZE_OF(task); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 989 | |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 990 | /* watch out for wraparound */ |
| 991 | if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT) |
| 992 | start_vaddr = end_vaddr; |
| 993 | |
| 994 | /* |
| 995 | * The odds are that this will stop walking way |
| 996 | * before end_vaddr, because the length of the |
| 997 | * user buffer is tracked in "pm", and the walk |
| 998 | * will stop when we hit the end of the buffer. |
| 999 | */ |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1000 | ret = 0; |
| 1001 | while (count && (start_vaddr < end_vaddr)) { |
| 1002 | int len; |
| 1003 | unsigned long end; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1004 | |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1005 | pm.pos = 0; |
Naoya Horiguchi | ea251c1 | 2010-11-24 12:57:13 -0800 | [diff] [blame] | 1006 | end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1007 | /* overflow ? */ |
| 1008 | if (end < start_vaddr || end > end_vaddr) |
| 1009 | end = end_vaddr; |
| 1010 | down_read(&mm->mmap_sem); |
| 1011 | ret = walk_page_range(start_vaddr, end, &pagemap_walk); |
| 1012 | up_read(&mm->mmap_sem); |
| 1013 | start_vaddr = end; |
| 1014 | |
| 1015 | len = min(count, PM_ENTRY_BYTES * pm.pos); |
Dan Carpenter | 309361e | 2010-04-06 13:45:39 +0300 | [diff] [blame] | 1016 | if (copy_to_user(buf, pm.buffer, len)) { |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1017 | ret = -EFAULT; |
KOSAKI Motohiro | 98bc93e | 2011-05-26 16:25:53 -0700 | [diff] [blame] | 1018 | goto out_mm; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1019 | } |
| 1020 | copied += len; |
| 1021 | buf += len; |
| 1022 | count -= len; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1023 | } |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1024 | *ppos += copied; |
| 1025 | if (!ret || ret == PM_END_OF_BUFFER) |
| 1026 | ret = copied; |
| 1027 | |
Marcelo Tosatti | fb39380 | 2008-03-13 12:32:35 -0700 | [diff] [blame] | 1028 | out_mm: |
| 1029 | mmput(mm); |
KOSAKI Motohiro | 98bc93e | 2011-05-26 16:25:53 -0700 | [diff] [blame] | 1030 | out_free: |
| 1031 | kfree(pm.buffer); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1032 | out_task: |
| 1033 | put_task_struct(task); |
| 1034 | out: |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
Kirill A. Shutemov | 2a8cc19 | 2015-03-09 23:11:12 +0200 | [diff] [blame^] | 1038 | static int pagemap_open(struct inode *inode, struct file *file) |
| 1039 | { |
| 1040 | /* do not disclose physical addresses: attack vector */ |
| 1041 | if (!capable(CAP_SYS_ADMIN)) |
| 1042 | return -EPERM; |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1046 | const struct file_operations proc_pagemap_operations = { |
| 1047 | .llseek = mem_lseek, /* borrow this */ |
| 1048 | .read = pagemap_read, |
Kirill A. Shutemov | 2a8cc19 | 2015-03-09 23:11:12 +0200 | [diff] [blame^] | 1049 | .open = pagemap_open, |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1050 | }; |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 1051 | #endif /* CONFIG_PROC_PAGE_MONITOR */ |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1052 | |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1053 | #ifdef CONFIG_NUMA |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1054 | |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1055 | struct numa_maps { |
| 1056 | struct vm_area_struct *vma; |
| 1057 | unsigned long pages; |
| 1058 | unsigned long anon; |
| 1059 | unsigned long active; |
| 1060 | unsigned long writeback; |
| 1061 | unsigned long mapcount_max; |
| 1062 | unsigned long dirty; |
| 1063 | unsigned long swapcache; |
| 1064 | unsigned long node[MAX_NUMNODES]; |
| 1065 | }; |
| 1066 | |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1067 | struct numa_maps_private { |
| 1068 | struct proc_maps_private proc_maps; |
| 1069 | struct numa_maps md; |
| 1070 | }; |
| 1071 | |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1072 | static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, |
| 1073 | unsigned long nr_pages) |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1074 | { |
| 1075 | int count = page_mapcount(page); |
| 1076 | |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1077 | md->pages += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1078 | if (pte_dirty || PageDirty(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1079 | md->dirty += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (PageSwapCache(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1082 | md->swapcache += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (PageActive(page) || PageUnevictable(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1085 | md->active += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1086 | |
| 1087 | if (PageWriteback(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1088 | md->writeback += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1089 | |
| 1090 | if (PageAnon(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1091 | md->anon += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1092 | |
| 1093 | if (count > md->mapcount_max) |
| 1094 | md->mapcount_max = count; |
| 1095 | |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1096 | md->node[page_to_nid(page)] += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Dave Hansen | 3200a8a | 2011-09-20 15:19:39 -0700 | [diff] [blame] | 1099 | static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, |
| 1100 | unsigned long addr) |
| 1101 | { |
| 1102 | struct page *page; |
| 1103 | int nid; |
| 1104 | |
| 1105 | if (!pte_present(pte)) |
| 1106 | return NULL; |
| 1107 | |
| 1108 | page = vm_normal_page(vma, addr, pte); |
| 1109 | if (!page) |
| 1110 | return NULL; |
| 1111 | |
| 1112 | if (PageReserved(page)) |
| 1113 | return NULL; |
| 1114 | |
| 1115 | nid = page_to_nid(page); |
| 1116 | if (!node_isset(nid, node_states[N_HIGH_MEMORY])) |
| 1117 | return NULL; |
| 1118 | |
| 1119 | return page; |
| 1120 | } |
| 1121 | |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1122 | static int gather_pte_stats(pmd_t *pmd, unsigned long addr, |
| 1123 | unsigned long end, struct mm_walk *walk) |
| 1124 | { |
| 1125 | struct numa_maps *md; |
| 1126 | spinlock_t *ptl; |
| 1127 | pte_t *orig_pte; |
| 1128 | pte_t *pte; |
| 1129 | |
| 1130 | md = walk->private; |
Dave Hansen | 32ef438 | 2011-09-20 15:19:41 -0700 | [diff] [blame] | 1131 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1132 | if (pmd_trans_huge_lock(pmd, md->vma) == 1) { |
| 1133 | pte_t huge_pte = *(pte_t *)pmd; |
| 1134 | struct page *page; |
| 1135 | |
| 1136 | page = can_gather_numa_stats(huge_pte, md->vma, addr); |
| 1137 | if (page) |
| 1138 | gather_stats(page, md, pte_dirty(huge_pte), |
| 1139 | HPAGE_PMD_SIZE/PAGE_SIZE); |
Dave Hansen | 32ef438 | 2011-09-20 15:19:41 -0700 | [diff] [blame] | 1140 | spin_unlock(&walk->mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1141 | return 0; |
Dave Hansen | 32ef438 | 2011-09-20 15:19:41 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1144 | if (pmd_trans_unstable(pmd)) |
| 1145 | return 0; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1146 | orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); |
| 1147 | do { |
Dave Hansen | 3200a8a | 2011-09-20 15:19:39 -0700 | [diff] [blame] | 1148 | struct page *page = can_gather_numa_stats(*pte, md->vma, addr); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1149 | if (!page) |
| 1150 | continue; |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1151 | gather_stats(page, md, pte_dirty(*pte), 1); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1152 | |
| 1153 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 1154 | pte_unmap_unlock(orig_pte, ptl); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | #ifdef CONFIG_HUGETLB_PAGE |
| 1158 | static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask, |
| 1159 | unsigned long addr, unsigned long end, struct mm_walk *walk) |
| 1160 | { |
| 1161 | struct numa_maps *md; |
| 1162 | struct page *page; |
| 1163 | |
| 1164 | if (pte_none(*pte)) |
| 1165 | return 0; |
| 1166 | |
| 1167 | page = pte_page(*pte); |
| 1168 | if (!page) |
| 1169 | return 0; |
| 1170 | |
| 1171 | md = walk->private; |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1172 | gather_stats(page, md, pte_dirty(*pte), 1); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | #else |
| 1177 | static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask, |
| 1178 | unsigned long addr, unsigned long end, struct mm_walk *walk) |
| 1179 | { |
| 1180 | return 0; |
| 1181 | } |
| 1182 | #endif |
| 1183 | |
| 1184 | /* |
| 1185 | * Display pages allocated per node and memory policy via /proc. |
| 1186 | */ |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1187 | static int show_numa_map(struct seq_file *m, void *v, int is_pid) |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1188 | { |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1189 | struct numa_maps_private *numa_priv = m->private; |
| 1190 | struct proc_maps_private *proc_priv = &numa_priv->proc_maps; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1191 | struct vm_area_struct *vma = v; |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1192 | struct numa_maps *md = &numa_priv->md; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1193 | struct file *file = vma->vm_file; |
| 1194 | struct mm_struct *mm = vma->vm_mm; |
| 1195 | struct mm_walk walk = {}; |
| 1196 | struct mempolicy *pol; |
| 1197 | int n; |
| 1198 | char buffer[50]; |
| 1199 | |
| 1200 | if (!mm) |
| 1201 | return 0; |
| 1202 | |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1203 | /* Ensure we start with an empty set of numa_maps statistics. */ |
| 1204 | memset(md, 0, sizeof(*md)); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1205 | |
| 1206 | md->vma = vma; |
| 1207 | |
| 1208 | walk.hugetlb_entry = gather_hugetbl_stats; |
| 1209 | walk.pmd_entry = gather_pte_stats; |
| 1210 | walk.private = md; |
| 1211 | walk.mm = mm; |
| 1212 | |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1213 | pol = get_vma_policy(proc_priv->task, vma, vma->vm_start); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1214 | mpol_to_str(buffer, sizeof(buffer), pol, 0); |
| 1215 | mpol_cond_put(pol); |
| 1216 | |
| 1217 | seq_printf(m, "%08lx %s", vma->vm_start, buffer); |
| 1218 | |
| 1219 | if (file) { |
| 1220 | seq_printf(m, " file="); |
| 1221 | seq_path(m, &file->f_path, "\n\t= "); |
| 1222 | } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { |
| 1223 | seq_printf(m, " heap"); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1224 | } else { |
| 1225 | pid_t tid = vm_is_stack(proc_priv->task, vma, is_pid); |
| 1226 | if (tid != 0) { |
| 1227 | /* |
| 1228 | * Thread stack in /proc/PID/task/TID/maps or |
| 1229 | * the main process stack. |
| 1230 | */ |
| 1231 | if (!is_pid || (vma->vm_start <= mm->start_stack && |
| 1232 | vma->vm_end >= mm->start_stack)) |
| 1233 | seq_printf(m, " stack"); |
| 1234 | else |
| 1235 | seq_printf(m, " stack:%d", tid); |
| 1236 | } |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Andrew Morton | fc360bd | 2011-10-31 17:06:32 -0700 | [diff] [blame] | 1239 | if (is_vm_hugetlb_page(vma)) |
| 1240 | seq_printf(m, " huge"); |
| 1241 | |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1242 | walk_page_range(vma->vm_start, vma->vm_end, &walk); |
| 1243 | |
| 1244 | if (!md->pages) |
| 1245 | goto out; |
| 1246 | |
| 1247 | if (md->anon) |
| 1248 | seq_printf(m, " anon=%lu", md->anon); |
| 1249 | |
| 1250 | if (md->dirty) |
| 1251 | seq_printf(m, " dirty=%lu", md->dirty); |
| 1252 | |
| 1253 | if (md->pages != md->anon && md->pages != md->dirty) |
| 1254 | seq_printf(m, " mapped=%lu", md->pages); |
| 1255 | |
| 1256 | if (md->mapcount_max > 1) |
| 1257 | seq_printf(m, " mapmax=%lu", md->mapcount_max); |
| 1258 | |
| 1259 | if (md->swapcache) |
| 1260 | seq_printf(m, " swapcache=%lu", md->swapcache); |
| 1261 | |
| 1262 | if (md->active < md->pages && !is_vm_hugetlb_page(vma)) |
| 1263 | seq_printf(m, " active=%lu", md->active); |
| 1264 | |
| 1265 | if (md->writeback) |
| 1266 | seq_printf(m, " writeback=%lu", md->writeback); |
| 1267 | |
| 1268 | for_each_node_state(n, N_HIGH_MEMORY) |
| 1269 | if (md->node[n]) |
| 1270 | seq_printf(m, " N%d=%lu", n, md->node[n]); |
| 1271 | out: |
| 1272 | seq_putc(m, '\n'); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1273 | |
| 1274 | if (m->count < m->size) |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1275 | m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1276 | return 0; |
| 1277 | } |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1278 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1279 | static int show_pid_numa_map(struct seq_file *m, void *v) |
| 1280 | { |
| 1281 | return show_numa_map(m, v, 1); |
| 1282 | } |
| 1283 | |
| 1284 | static int show_tid_numa_map(struct seq_file *m, void *v) |
| 1285 | { |
| 1286 | return show_numa_map(m, v, 0); |
| 1287 | } |
| 1288 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 1289 | static const struct seq_operations proc_pid_numa_maps_op = { |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1290 | .start = m_start, |
| 1291 | .next = m_next, |
| 1292 | .stop = m_stop, |
| 1293 | .show = show_pid_numa_map, |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1294 | }; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1295 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1296 | static const struct seq_operations proc_tid_numa_maps_op = { |
| 1297 | .start = m_start, |
| 1298 | .next = m_next, |
| 1299 | .stop = m_stop, |
| 1300 | .show = show_tid_numa_map, |
| 1301 | }; |
| 1302 | |
| 1303 | static int numa_maps_open(struct inode *inode, struct file *file, |
| 1304 | const struct seq_operations *ops) |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1305 | { |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1306 | struct numa_maps_private *priv; |
| 1307 | int ret = -ENOMEM; |
| 1308 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 1309 | if (priv) { |
| 1310 | priv->proc_maps.pid = proc_pid(inode); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1311 | ret = seq_open(file, ops); |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1312 | if (!ret) { |
| 1313 | struct seq_file *m = file->private_data; |
| 1314 | m->private = priv; |
| 1315 | } else { |
| 1316 | kfree(priv); |
| 1317 | } |
| 1318 | } |
| 1319 | return ret; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1322 | static int pid_numa_maps_open(struct inode *inode, struct file *file) |
| 1323 | { |
| 1324 | return numa_maps_open(inode, file, &proc_pid_numa_maps_op); |
| 1325 | } |
| 1326 | |
| 1327 | static int tid_numa_maps_open(struct inode *inode, struct file *file) |
| 1328 | { |
| 1329 | return numa_maps_open(inode, file, &proc_tid_numa_maps_op); |
| 1330 | } |
| 1331 | |
| 1332 | const struct file_operations proc_pid_numa_maps_operations = { |
| 1333 | .open = pid_numa_maps_open, |
| 1334 | .read = seq_read, |
| 1335 | .llseek = seq_lseek, |
| 1336 | .release = seq_release_private, |
| 1337 | }; |
| 1338 | |
| 1339 | const struct file_operations proc_tid_numa_maps_operations = { |
| 1340 | .open = tid_numa_maps_open, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1341 | .read = seq_read, |
| 1342 | .llseek = seq_lseek, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1343 | .release = seq_release_private, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1344 | }; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1345 | #endif /* CONFIG_NUMA */ |