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