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