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