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